question: i386 ld.gold reports "internal error" linking files with SHT_RELA section

H.J. Lu hjl.tools@gmail.com
Wed Aug 31 19:20:52 GMT 2022


On Tue, Aug 30, 2022 at 9:43 PM Xi Ruoyao <xry111@xry111.site> wrote:
>
> On Tue, 2022-08-30 at 13:36 -0700, H.J. Lu wrote:
>
> > > >  SHT_RELA shouldn't be used for i386.
> > >
> > > But it is accepted by ld.bfd...
> >
> > Do you have .o files for ld.bfd I can take a look at?
> >
> > > FWIIW, only the dwarf debug sections are generated as SHT_RELA by
> > > nasm.
>
> Hi,
>
> $ cat t.asm
> section .text
> extern printf
> global main
> main:
>   push ebp
>   mov ebp, esp
>   push msg
>   call printf
>   add esp, 4
>   leave
>   ret
>
> section .data
>   msg db 'Hello, world!', 13, 10, 0
>
> section .note.GNU-stack noalloc noexec nowrite progbits
> $ nasm t.asm -f elf32 -F dwarf
> $ cc t.o && ./a.out
> Hello, world!
> $ cc -fuse-ld=gold t.o
> /usr/bin/ld.gold: internal error in relocate_section, at ../../gold/i386.cc:3683
> collect2: error: ld returned 1 exit status
>
> I'll attach "t.asm' and "t.o.gz".
>
> --
> Xi Ruoyao <xry111@xry111.site>
> School of Aerospace Science and Technology, Xidian University

What happened are

1. nasm generates RELA relocation for debug sections.  But addend
in RELA relocation is 0:

Relocation section '.rela.debug_info' at offset 0x3a0 contains 5 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name + Addend
00000006  00000501 R_386_32               00000000   .debug_abbrev + 0
0000000c  00000201 R_386_32               00000000   .text + 0
00000010  00000201 R_386_32               00000000   .text + 0
00000014  00000601 R_386_32               00000000   .debug_line + 0
0000002c  00000201 R_386_32               00000000   .text + 0

and addend is stored at the relocation offset like REL relocation:

Hex dump of section '.debug_info':
  0x00000000 31000000 02000000 00000401 00000000 1...............
  0x00000010 12000000 00000000 782e7300 4e41534d ........x.s.NASM
  0x00000020 20322e31 352e3035 00018002 00000000  2.15.05........
  0x00000030 00000000 00

2. ld.bfd treats RELA relocation like REL relocation.  It uses addend at
the relocation offset, not in RELA relocation.

It just works with ld.bfd by accident.  A linker, like gold, can disallow RELA
relocation.  A linker, like lld, can use addend in RELA relocation and addend
at the relocation offset is ignored.


-- 
H.J.


More information about the Binutils mailing list