[PATCH] x86: Properly handle relocation against hidden ABS symbol

H.J. Lu hjl.tools@gmail.com
Thu May 15 02:00:39 GMT 2025


With PIC on x86-64,

movq symbol@GOTPCREL(%rip), %rax

is used to get the symbol address via GOT.  If the symbol turns out to
be a hidden symbol, linker normally converts it into

leaq symbol(%rip), %rax

But if the symbol is an ABS symbol, RAX won't have the correct value at
run-time and it will have the symbol value + load address.  If the symbol
is declared as hidden, compiler will generate

leaq symbol(%rip), %rax

to get the symbol address.  If the hidden symbol is an ABS symbol, RAX
will have the same incorrect value at run-time.  On i386,

movl symbol@GOT(%reg1), %reg2

is used to get the symbol address via GOT.  If the symbol turns out to
be a hidden symbol, linker normally converts it into

leal symbol@GOTOFF(%reg1), %reg2

If the symbol is declared as hidden, compiler will generate

leal symbol@GOTOFF(%reg1), %reg2

In both cases, if the hidden symbol is an ABS symbol, REG2 will have the
same incorrect value at run-time.  This patch changes the x86-64 linker
to convert

movq symbol@GOTPCREL(%rip), %rax
and
leaq symbol(%rip), %rax
to
movq $symbol, %rax

and changes the i386 linker to convert

movl symbol@GOT(%reg1), %reg2
and
leal symbol@GOTOFF(%reg1), %reg2
to
movl $symbol, %reg2

for hidden ABS symbols.

bfd/

PR ld/32443
* elf32-i386.c (elf_i386_convert_load_reloc): Skip relocation
against linker-script hidden absolute symbol in PIC.
(elf_i386_finish_dynamic_symbol): Don't generate dynamic
relocation against inker-script hidden absolute symbol.
* elf64-x86-64.c (elf_x86_64_convert_load_reloc): Skip
relocation against linker-script hidden absolute symbol in PIC.
(elf_x86_64_finish_dynamic_symbol): Don't generate dynamic
relocation against inker-script hidden absolute symbol.
* elfxx-x86.c (elf_x86_allocate_dynrelocs): Don't allocate
dynamic relocation against inker-script hidden absolute symbol.
* elfxx-x86.h (HIDDEN_ABS_SYMBOL_P): New.

ld/

PR ld/32443
* testsuite/ld-elf/linux-x86.exp: Run PR ld/32443 tests.  Allow
PC32 relocation against hidden ABS symbol in PIE.
* testsuite/ld-elf/pr32443-hidden.rd: New file.
* testsuite/ld-elf/pr32443-hidden.t: Likewise.
* testsuite/ld-elf/pr32443.t: Likewise.
* testsuite/ld-elf/pr32443a.c: Likewise.
* testsuite/ld-elf/pr32443b.c: Likewise.
* testsuite/ld-i386/i386.exp: Run lea2, mov4a and mov4b.
* testsuite/ld-i386/lea2.d: New file.
* testsuite/ld-i386/lea2.s: Likewise.
* testsuite/ld-i386/mov4.s: Likewise.
* testsuite/ld-i386/mov4a.d: Likewise.
* testsuite/ld-i386/mov4b.d: Likewise.
* testsuite/ld-x86-64/lea2-x32.d: Likewise.
* testsuite/ld-x86-64/lea2.d: Likewise.
* testsuite/ld-x86-64/lea2.s: Likewise.
* testsuite/ld-x86-64/mov3.s: Likewise.
* testsuite/ld-x86-64/mov3a-x32.d: Likewise.
* testsuite/ld-x86-64/mov3a.d: Likewise.
* testsuite/ld-x86-64/mov3b-x32.d: Likewise.
* testsuite/ld-x86-64/mov3b.d: Likewise.
* testsuite/ld-x86-64/x86-64.exp: Run lea2, lea2-x32, mov3a,
mov3a-x32, mov3b and mov3b-x32.

-- 
H.J.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-x86-Properly-handle-relocation-against-hidden-ABS-sy.patch
Type: text/x-patch
Size: 27576 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20250515/cd708e36/attachment-0001.bin>


More information about the Binutils mailing list