public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@xry111.site>
To: liuzhensong <liuzhensong@loongson.cn>, binutils@sourceware.org
Cc: Chenghua Xu <xuchenghua@loongson.cn>,
	Lulu Cheng <chenglulu@loongson.cn>,  Wang Xuerui <i@xen0n.name>
Subject: Re: [PATCH 2/2] LoongArch: Fix R_LARCH_IRELATIVE insertion after elf_link_sort_relocs
Date: Sun, 18 Sep 2022 17:58:03 +0800	[thread overview]
Message-ID: <793d0e700362794e814ad67c78aa532458202aef.camel@xry111.site> (raw)
In-Reply-To: <f262b6b355809d1d6b757f90b792bcab6ab71872.camel@xry111.site>

On Sat, 2022-09-17 at 04:11 +0800, Xi Ruoyao via Binutils wrote:

> The problem is loongarch_elf_finish_dynamic_symbol runs after
> elf_link_sort_relocs.  elf_link_sort_relocs basically does:
> 
> (pseudo code)
> 
> buffer = []
> for section in {input of .rela.dyn}
>     buffer += (section.content) as [RELA]
> 
> buffer.sort_by(some_comparator)
> 
> buffer = buffer as [byte]
> for section in {input of .rela.dyn}
>     section.content = buffer[..section.content.len()]
>     buffer = buffer[section.content.len()..]
> 
> So a "slot" in relplt->contents can end up elsewhere.  You can read the
> code of elf_link_sort_relocs to see it...
> 
> I can try to write a test case for this, but it's 04:00 AM now so I'd do
> it after a sleep...

Take this as an example, note that the R_LARCH_IRELATIVE relocation is
missing with -z combreloc:

$ cat b1.s
.text
.align 2

.local ifunc
.type ifunc, @gnu_indirect_function
.set ifunc, resolver

resolver:
  la.local $a0, impl
  jr $ra

impl:
  li.w $a0, 42
  jr $ra

.global test
.type test, @function
test:
  move $s0, $ra
  bl ifunc
  xori $a0, $a0, 42
  jr $s0

.data
.global ptr
.type ptr, @object
ptr:
  .dword test
$ gas/as-new b1.s -o b1.o
$ ld/ld-new -shared b1.o  # "-z combreloc" is the default
$ readelf -r a.out

Relocation section '.rela.dyn' at offset 0x1f0 contains 2 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000000000  000000000000 R_LARCH_NONE                         8018
000000008000  000400000002 R_LARCH_64        0000000000000264 test + 0
$ ld/ld-new -shared b1.o -z nocombreloc
$ readelf -r a.out

Relocation section '.rela.data' at offset 0x1f0 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000008000  000400000002 R_LARCH_64        0000000000000264 test + 0

Relocation section '.rela.got' at offset 0x208 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000008018  00000000000c R_LARCH_IRELATIVE                    250


-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

      reply	other threads:[~2022-09-18  9:58 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-13 15:44 [PATCH 0/2] LoongArch: Fix two bugs breaking IFUNC in static PIE Xi Ruoyao
2022-09-13 15:44 ` [PATCH 1/2] LoongArch: Avoid heap-buffer-overflow in loongarch_elf_relocate_section Xi Ruoyao
2022-09-14  8:57   ` liuzhensong
2022-09-14 10:15     ` Xi Ruoyao
2022-09-14 11:15       ` Xi Ruoyao
2022-09-15  1:47         ` liuzhensong
2022-09-15  2:56           ` Xi Ruoyao
2022-09-15  3:54             ` liuzhensong
2022-09-15  6:13               ` Xi Ruoyao
2022-09-13 15:44 ` [PATCH 2/2] LoongArch: Fix R_LARCH_IRELATIVE insertion after elf_link_sort_relocs Xi Ruoyao
2022-09-15 13:03   ` liuzhensong
2022-09-16 20:11     ` Xi Ruoyao
2022-09-18  9:58       ` Xi Ruoyao [this message]

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=793d0e700362794e814ad67c78aa532458202aef.camel@xry111.site \
    --to=xry111@xry111.site \
    --cc=binutils@sourceware.org \
    --cc=chenglulu@loongson.cn \
    --cc=i@xen0n.name \
    --cc=liuzhensong@loongson.cn \
    --cc=xuchenghua@loongson.cn \
    /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).