public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: Xi Ruoyao <xry111@xry111.site>
Cc: Pierre Labastie <pierre.labastie@neuf.fr>,
	Binutils <binutils@sourceware.org>
Subject: Re: question: i386 ld.gold reports "internal error" linking files with SHT_RELA section
Date: Wed, 31 Aug 2022 12:20:52 -0700	[thread overview]
Message-ID: <CAMe9rOq=A1R_4McW2bj23RsmEVY_21EBhn3nX42x+MzjCESDTQ@mail.gmail.com> (raw)
In-Reply-To: <b26f5fe43b18cc8a43c86dd1d6c453e749616a8a.camel@xry111.site>

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.

  reply	other threads:[~2022-08-31 19:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-30 14:38 Xi Ruoyao
2022-08-30 18:21 ` H.J. Lu
2022-08-30 18:53   ` Pierre Labastie
2022-08-30 20:36     ` H.J. Lu
2022-08-31  4:43       ` Xi Ruoyao
2022-08-31 19:20         ` H.J. Lu [this message]
2022-09-01  0:47           ` Xi Ruoyao
2022-09-01 16:34             ` H.J. Lu

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='CAMe9rOq=A1R_4McW2bj23RsmEVY_21EBhn3nX42x+MzjCESDTQ@mail.gmail.com' \
    --to=hjl.tools@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=pierre.labastie@neuf.fr \
    --cc=xry111@xry111.site \
    /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).