public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Jan Beulich <jbeulich@suse.com>
To: "Alex Xu (Hello71)" <alex_y_xu@yahoo.ca>
Cc: binutils@sourceware.org
Subject: Re: Non-zero RELA section contents
Date: Tue, 19 Apr 2022 09:18:00 +0200	[thread overview]
Message-ID: <e738c72a-593b-3595-7a81-4710bc48379a@suse.com> (raw)
In-Reply-To: <1650070699.hyefqn4i18.none@localhost>

On 16.04.2022 06:44, Alex Xu (Hello71) wrote:
> In ELF RELA relocations, the addend is stored in the relocation instead 
> of the section contents. This was the premise of commit 17c6c3b99156 
> ("x86-64/ELF: clear src_mask for all reloc types"); the value in the 
> section contents is not used.
> 
> I recently encountered a difficult-to-trace bug in 7-Zip, eventually 
> determined to be caused by JWasm-family assemblers incorrectly putting 
> the addend in the section contents, and setting the RELA addend to -4. 
> Previous versions of binutils added the existing value to the relocation 
> result, producing a working program; new versions of binutils overwrite 
> it, producing a non-working program.
> 
> Now, I want to be clear: while the relevant specifications are not clear 
> about what the linker should do in this case, they are reasonably clear 
> that the assembler should not generate this.

I think the ELF spec is quite clear. The term usually referenced as A
(addend) has a well-defined origin. The value coming from the field
being relocated represents A in REL but doesn't participate in the
calculation of the final value at all in RELA.

> With that being said, I think it would be a good idea for ld to either 
> revert to the previous behavior or issue a warning or error when 
> detecting such malformed object files. I think a warning at the least is 
> appropriate, because binaries are either silently incorrect on the old 
> version, or silently incorrect on the new version, and silently 
> incorrect relocations are extremely hard to diagnose.

Such a warning might be okay to add as long as it's off by default.
Object files are free to have being-relocated fields hold whichever
value they like when the value doesn't matter for relocation. These
values may provide hints to tools (e.g. disassemblers).

> One argument in 
> favor of reverting to the previous behavior is it is better to preserve 
> backwards compatibility in BFD linker when the new behavior is not 
> clearly superior (e.g. faster or closer to spec).

But the new behavior _is_ closer to the spec.

> One argument in favor 
> of the new behavior is that it is consistent with LLD and probably gold.

And, as said, consistent with the spec.

> --- a/bfd/reloc.c
> +++ b/bfd/reloc.c
> @@ -1509,6 +1509,11 @@ _bfd_relocate_contents (reloc_howto_type *howto,
>    relocation >>= (bfd_vma) rightshift;
>    relocation <<= (bfd_vma) bitpos;
>  
> +  if (!howto->src_mask && (x & howto->dst_mask))
> +    _bfd_error_handler
> +      (_("warning: %pB: existing value for %s relocation is %lx, expected 0"),
> +       input_bfd, howto->name, x);
> +
>    /* Add RELOCATION to the right bits of X.  */
>    x = ((x & ~howto->dst_mask)
>         | (((x & howto->src_mask) + relocation) & howto->dst_mask));
> 
> I have tested this patch on a single test file which printed the desired 
> warning. I believe %lx is probably wrong, but I don't know how to fix 
> it.

You will want to use BFD_VMA_FMT instead.

Jan


  parent reply	other threads:[~2022-04-19  7:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1650070699.hyefqn4i18.none.ref@localhost>
2022-04-16  4:44 ` Alex Xu (Hello71)
2022-04-18 16:01   ` H.J. Lu
2022-04-19  7:18   ` Jan Beulich [this message]
2022-04-23 21:45     ` Fangrui Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e738c72a-593b-3595-7a81-4710bc48379a@suse.com \
    --to=jbeulich@suse.com \
    --cc=alex_y_xu@yahoo.ca \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).