[PATCH 2/2] LoongArch: Fix R_LARCH_IRELATIVE insertion after elf_link_sort_relocs

Xi Ruoyao xry111@xry111.site
Sun Sep 18 09:58:03 GMT 2022


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


More information about the Binutils mailing list