]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
LoongArch: Check PC-relative relocations for shared libraries
authorLulu Cai <cailulu@loongson.cn>
Mon, 30 Sep 2024 08:08:59 +0000 (16:08 +0800)
committerliuzhensong <liuzhensong@loongson.cn>
Thu, 17 Oct 2024 13:01:52 +0000 (21:01 +0800)
Building shared libraries should not be allowed for PC-relative
relocations against external symbols.
Currently LoongArch has no corresponding checks and silently
generates wrong shared libraries.

However, In the first version of the medium cmodel, pcalau12i+jirl was
used for function calls, in which case PC-relative relocations were
allowed.

bfd/elfnn-loongarch.c
ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s [new file with mode: 0644]
ld/testsuite/ld-loongarch-elf/data-plt.s
ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp

index 9c3cd6745d60e1c454e4b31d7df45ee85bd6f32b..3b1a6a4f95ac06d6a33ec3c99d4cb4553c94f700 100644 (file)
@@ -1079,6 +1079,18 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
            h->non_got_ref = 1;
          break;
 
+       /* Since shared library global symbols interpose, any
+          PC-relative relocations against external symbols
+          should not be used to build shared libraries.  */
+       case R_LARCH_PCREL20_S2:
+         if (bfd_link_pic (info)
+             && (sec->flags & SEC_ALLOC) != 0
+             && (sec->flags & SEC_READONLY) != 0
+             && ! LARCH_REF_LOCAL (info, h))
+           return bad_static_reloc (abfd, rel, sec, r_type, h, NULL);
+
+         break;
+
        /* For normal cmodel, pcalau12i + addi.d/w used to data.
           For first version medium cmodel, pcalau12i + jirl are used to
           function call, it need to creat PLT entry for STT_FUNC and
@@ -1096,6 +1108,15 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
              h->pointer_equality_needed = 1;
            }
 
+         /* PC-relative relocations are allowed For first version
+            medium cmodel function call.  */
+         if (h != NULL && !h->needs_plt
+             && bfd_link_pic (info)
+             && (sec->flags & SEC_ALLOC) != 0
+             && (sec->flags & SEC_READONLY) != 0
+             && ! LARCH_REF_LOCAL (info, h))
+           return bad_static_reloc (abfd, rel, sec, r_type, h, NULL);
+
          break;
 
        case R_LARCH_B16:
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d
new file mode 100644 (file)
index 0000000..6ecefd1
--- /dev/null
@@ -0,0 +1,5 @@
+#name: PC-relative relocation making shared
+#source: bad_pcala_hi20_global.s
+#target: [check_shared_lib_support]
+#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
+#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.s
new file mode 100644 (file)
index 0000000..d8189e4
--- /dev/null
@@ -0,0 +1,8 @@
+    .hidden global_a
+    .text
+    .align 2
+main:
+    # Symbols defined .hidden are bound local and
+    # the linker should differenciate them.
+    la.pcrel  $a0, global_a
+    la.pcrel  $a0, global_b
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d
new file mode 100644 (file)
index 0000000..cefc42c
--- /dev/null
@@ -0,0 +1,5 @@
+#name: PC-relative relocation making shared
+#source: bad_pcala_hi20_weak.s
+#target: [check_shared_lib_support]
+#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
+#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.s
new file mode 100644 (file)
index 0000000..73c6ec5
--- /dev/null
@@ -0,0 +1,9 @@
+    .hidden global_a
+    .weak   global_b
+    .text
+    .align 2
+main:
+    # Symbols defined .hidden are bound local and
+    # the linker should differenciate them.
+    la.pcrel  $a0, global_a
+    la.pcrel  $a0, global_b
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d
new file mode 100644 (file)
index 0000000..8e063df
--- /dev/null
@@ -0,0 +1,5 @@
+#name: PC-relative relocation making shared
+#source: bad_pcrel20_s2_global.s
+#target: [check_shared_lib_support]
+#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
+#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.s
new file mode 100644 (file)
index 0000000..39cedbd
--- /dev/null
@@ -0,0 +1,8 @@
+    .hidden global_a
+    .text
+    .align 2
+main:
+    # Symbols defined .hidden are bound local and
+    # the linker should differenciate them.
+    pcaddi    $a0, %pcrel_20(global_a)
+    pcaddi    $a0, %pcrel_20(global_b)
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d
new file mode 100644 (file)
index 0000000..605df24
--- /dev/null
@@ -0,0 +1,5 @@
+#name: PC-relative relocation making shared
+#source: bad_pcrel20_s2_weak.s
+#target: [check_shared_lib_support]
+#ld: -shared --defsym global_a=0x10 --defsym global_b=0x20
+#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.s
new file mode 100644 (file)
index 0000000..82faa5b
--- /dev/null
@@ -0,0 +1,9 @@
+    .hidden global_a
+    .weak   global_b
+    .text
+    .align 2
+main:
+    # Symbols defined .hidden are bound local and
+    # the linker should differenciate them.
+    pcaddi    $a0, %pcrel_20(global_a)
+    pcaddi    $a0, %pcrel_20(global_b)
index faff052c6281c808c66166370867d848b8a65412..6c1a8eac35cc4f6fa245da0cd51a76e195c9caaf 100644 (file)
@@ -2,6 +2,7 @@
 # R_LARCH_PCALA_HI20 only need to generate PLT entry for function symbols.
        .text
        .globl  a
+       .hidden a
 
        .data
        .align  2
index 6bef97c260f83d9774de783819532c82d0dff574..4e0068d9f899b3bd6615890c51ff92b6072c6178 100644 (file)
@@ -166,6 +166,10 @@ if [istarget "loongarch64-*-*"] {
     run_dump_test "relr-got-shared"
     run_dump_test "relr-text-shared"
     run_dump_test "abssym_shared"
+    run_dump_test "bad_pcala_hi20_global"
+    run_dump_test "bad_pcala_hi20_weak"
+    run_dump_test "bad_pcrel20_s2_global"
+    run_dump_test "bad_pcrel20_s2_weak"
   }
 
   if [check_pie_support] {