[PATCH] PR30437 aarch64: make RELA relocs idempotent

Richard Earnshaw (lists) Richard.Earnshaw@arm.com
Thu May 25 15:56:45 GMT 2023


On 25/05/2023 15:12, Michael Matz via Binutils wrote:
> Hello,
> 
> On Thu, 25 May 2023, Richard Earnshaw (lists) wrote:
> 
>> I'd be surprised if even that is totally correct.  ELF permits
>> relocations of a single 'place' to be chained provided that they are of
>> the same type (all REL or all RELA); not that I've ever seen that done
>> in practice.  When this is done the addend for the second and subsequent
>> relocations use the result of the previous relocation - they must be
>> processed in order.  Only the last relocation in a sequence writes a
>> value back to the location being relocated.
>>
>> So really, we shouldn't need two tables, we just need to know where to
>> get the addend value from at the start of a chain.
> 
> Sort of, except ... it's not just for a chain of relocs (to the same
> place) steming from a single input file, but also about the similar
> situation when the same place is relocated multiple times due to 'ld -r'
> or pre-existing (non-zero) section content in an input file to a final
> link.  In this situation it's not easily determined what's the start of a
> reloc chain.
> 
> I still agree with you that abstractly multiple howto tables shouldn't be
> needed, because RELA relocs _always_ should ignore section content on
> read-in, and hence the reloc routines could simply special-case the
> RELA/REL distinction by just not looking at src_mask or partial_inplace
> for RELA relocs.  It's just that BFD isn't structured in this way.
> The internal representation of relocs always has an addend, no matter if
> it comes from read-in section contents or a RELA reloc, and the
> information of where it came from is lost (and section content isn't
> zeroed on read-in for REL relocs, when transferring the addend to the bfd
> reloc structure).  So, to get this all somewhat working correctly right
> now multiple howto entries are necessary (or special-casing target
> reloc-swap-in and swap-out routines).
> 
> I'm totally sure, once one tries to _really_ start mixing REL and RELA
> relocs in a link, at the latest when ld -r and/or other toolchains are
> involved, that a heap of bugs will be uncovered :-)

It shouldn't be a big problem because ELF says it's undefined if the 
different types of reloc target the same location.  IF they're 
addressing different (presumed non-overlapping) locations then the 
ordering shouldn't matter.

> 
> (And of course, it's always possible that REL relocations cannot always be
> used to achieve idempotence even in presence of 'ld -r', in which case
> RELA must be used.  The aarch64 psABI thankfully even spells this out.)
> 

There's no fundamental reason why a toolchain doing 'ld -r' can't always 
emit RELA format relocs, even if the input format was REL.  In fact, I'd 
argue that really it has to in most cases as 'ld -r' destroys the 
information that's needed to reprocess the relocations.

But I agree with you, this is all likely broken in BFD because we have 
no way of testing it (gas can't create multiple relocs to a single 
location to the best of my knowledge).

R.



More information about the Binutils mailing list