[PATCH] bfd/ELF: refine PR binutils/31872 fix

ywx120702@tuta.io ywx120702@tuta.io
Wed Dec 25 14:04:12 GMT 2024


Thanks very much. I have been trying for weeks to figure out what's wrong with some seemingly innocent real-mode code, but didn't have a clue. To make bad worse, after upgrade to Fedora 41, it failed to link. Your fix reminds me something I had overlooked. Now I think I got a little understanding of the problem.

UEFI boot loader will make sure BSP is in long mode, before hand off to a kernel. However, 16-bit code is still required, because all AP processors are still in real mode.

x86_64 gcc emit elf32, with -m16 flag, despite it is a 64-bit compiler. Thus, we have to objcopy -I elf32-i386 -O elf64-x86-64 to convert it to elf64.

I don't think x86_64 ELF spec have ever mentioned REL. As Stephen Checkoway suggested in binutils bug 23261,  it would be better if objcopy convert REL to RELA. Nevertheless according to him, once upon a time, REL did work with ld.bfd.

Unfortuanately, REL relocations are broken with ld.bfd, maybe since commit 17c6c3b. I tried ld.lld, now it does respect implicit addend.

Dec 17, 2024, 18:59 by jbeulich@suse.com:

> The fix for PR binutils/31872 (commit b20ab53f81db) neglected the case
> of targets with only RELA support, where nevertheless object files using
> REL exist. In particular objcopy will create such objects for x86-64
> when converting from an i?86 ELF object (this by itself probably isn't
> quite right, but we ought to cope with what our own tools are doing).
>
> Restore the fallback to the RELA lookup, just without re-introducing the
> blind NULL de-ref that was there before.
>
> --- a/bfd/elfcode.h
> +++ b/bfd/elfcode.h
> @@ -1615,7 +1615,8 @@ elf_slurp_reloc_table_from_section (bfd
> relent->addend = rela.r_addend;
>
> res = false;
> -      if (entsize == sizeof (Elf_External_Rela)
> +      if ((entsize == sizeof (Elf_External_Rela)
> +	   || ebd->elf_info_to_howto_rel == NULL)
> && ebd->elf_info_to_howto != NULL)
> res = ebd->elf_info_to_howto (abfd, relent, &rela);
> else if (ebd->elf_info_to_howto_rel != NULL)
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/binutils/attachments/20241225/8d037ca7/attachment.htm>


More information about the Binutils mailing list