diff options
author | Xi Ruoyao <xry111@xry111.site> | 2024-12-25 12:41:44 +0800 |
---|---|---|
committer | cailulu <cailulu@loongson.cn> | 2024-12-27 17:52:29 +0800 |
commit | c1a964051be5471d91bfe7655155948ab9c653f8 (patch) | |
tree | a1ed265707e79ce07db405c46c4958560ce900fe /ld | |
parent | bd94e7cd84c19da4a37dbdbc2d5587db6383f28c (diff) | |
download | binutils-c1a964051be5471d91bfe7655155948ab9c653f8.zip binutils-c1a964051be5471d91bfe7655155948ab9c653f8.tar.gz binutils-c1a964051be5471d91bfe7655155948ab9c653f8.tar.bz2 |
LoongArch: Allow R_LARCH_PCALA_HI20 or R_LARCH_PCREL20_S2 against undefined weak symbols for static PIE
In a static PIE, undefined weak symbols should be just resolved to
runtime address 0, like those symbols with non-default visibility. This
was silently broken in all prior Binutils releases with "-static-pie
-mdirect-extern-access":
$ cat t.c
int x (void) __attribute__ ((weak));
int
main (void)
{
__builtin_printf("%p\n", x);
}
$ gcc t.c -static-pie -mdirect-extern-access
$ ./a.out
0x7ffff1d64000
Since commit 4cb77761d687 ("LoongArch: Check PC-relative relocations for
shared libraries), the situation has been improved: the linker errors
out instead of silently producing a wrong output file.
But logically, using -mdirect-extern-access for a static PIE perfectly
makes sense, and we should not prevent that even if the programmer uses
weak symbols. Linux kernel is such an example, and Linux < 6.10 now
fails to build with Binutils trunk. (The silent breakage with prior
Binutils releases was "benign" due to some blind luck.)
While since the 6.10 release Linux has removed those potentially
undefined weak symbols (due to performance issue), we still should
support weak symbols in -mdirect-extern-access -static-pie and unbreak
building old kernels.
Link: https://lore.kernel.org/loongarch/20241206085810.112341-1-chenhuacai@loongson.cn/
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Diffstat (limited to 'ld')
-rw-r--r-- | ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp | 1 | ||||
-rw-r--r-- | ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.d | 13 | ||||
-rw-r--r-- | ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.s | 8 |
3 files changed, 22 insertions, 0 deletions
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp index 35a3a4f..0b609c0 100644 --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp @@ -196,6 +196,7 @@ if [istarget "loongarch64-*-*"] { run_dump_test "relr-text-pie" run_dump_test "abssym_pie" run_dump_test "weak-undef-hidden-pie" + run_dump_test "weak-undef-static-pie" } run_dump_test "max_imm_b16" diff --git a/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.d b/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.d new file mode 100644 index 0000000..d95aa12 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.d @@ -0,0 +1,13 @@ +#ld: -static -pie --no-dynamic-linker -z text -Ttext=0x1111222233334ff0 +#objdump: -d + +#... +1111222233334ff0:[ ]+159999a4[ ]+lu12i.w[ ]+\$a0,[ ]+-209715 +1111222233334ff4:[ ]+02f77405[ ]+li.d[ ]+\$a1,[ ]+-547 +1111222233334ff8:[ ]+17777765[ ]+lu32i.d[ ]+\$a1,[ ]+-279621 +1111222233334ffc:[ ]+032aa8a5[ ]+lu52i.d[ ]+\$a1,[ ]+\$a1,[ ]+-1366 +1111222233335000:[ ]+00109484[ ]+add.d[ ]+\$a0,[ ]+\$a0,[ ]+\$a1 +1111222233335004:[ ]+029ffc06[ ]+li.w[ ]+\$a2,[ ]+2047 +1111222233335008:[ ]+02a00007[ ]+li.w[ ]+\$a3,[ ]+-2048 +111122223333500c:[ ]+4c000020[ ]+ret +#pass diff --git a/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.s b/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.s new file mode 100644 index 0000000..d6b2c7a --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.s @@ -0,0 +1,8 @@ +.weak undef + +.globl _start +_start: + la.pcrel $a0, $a1, undef + 0xaaabbbbbcccccddd + pcaddi $a2, undef + 0x7ff + pcaddi $a3, undef - 0x800 + ret |