[PATCH] LoongArch: Check PC-relative relocations for shared libraries

Lulu Cai cailulu@loongson.cn
Sat Oct 5 09:57:13 GMT 2024


Building shared libraries should not be allowed for PC-relative
relocations against externally defined or undefined symbols.
Currently LoongArch has no corresponding checks and silently
generates wrong shared libraries.

However, PC-relative relocations are allowed when used as a function
call.
---
 bfd/elfnn-loongarch.c                         | 21 +++++++++++++++
 .../ld-loongarch-elf/bad_pcala_hi20.d         |  5 ++++
 .../ld-loongarch-elf/bad_pcala_hi20.s         |  8 ++++++
 .../ld-loongarch-elf/bad_pcrel20_s2.d         |  5 ++++
 .../ld-loongarch-elf/bad_pcrel20_s2.s         |  8 ++++++
 ld/testsuite/ld-loongarch-elf/data-plt.d      |  5 ++++
 .../ld-loongarch-elf/ld-loongarch-elf.exp     | 27 +++----------------
 7 files changed, 55 insertions(+), 24 deletions(-)
 create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/data-plt.d

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 63c6fdffd6b..6392a325465 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -1079,6 +1079,19 @@ 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
+	   (defined and undefined) 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 +1109,14 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 	      h->pointer_equality_needed = 1;
 	    }
 
+	  /* PC-relative relocations are allowed when calling a function.  */
+	  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.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.d
new file mode 100644
index 00000000000..481700b01ff
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.d
@@ -0,0 +1,5 @@
+#name: PC-relative relocation making shared
+#source: bad_pcala_hi20.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.s b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.s
new file mode 100644
index 00000000000..d8189e45370
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20.s
@@ -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_pcrel20_s2.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.d
new file mode 100644
index 00000000000..bd948a457a9
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.d
@@ -0,0 +1,5 @@
+#name: PC-relative relocation making shared
+#source: bad_pcrel20_s2.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.s b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.s
new file mode 100644
index 00000000000..39cedbd2743
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2.s
@@ -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/data-plt.d b/ld/testsuite/ld-loongarch-elf/data-plt.d
new file mode 100644
index 00000000000..90e878d7b3f
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/data-plt.d
@@ -0,0 +1,5 @@
+#name: data plt
+#source: data-plt.s
+#target: [check_shared_lib_support]
+#ld: -shared
+#error: .*: relocation R_LARCH_PCALA_HI20 against `a` can not be used when making a shared object; recompile with -fPIC
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index 6bef97c260f..115c8c22f19 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -73,30 +73,6 @@ if [istarget "loongarch64-*-*"] {
 	]
   }
 
-  # loongarch*-elf target do not support -shared option
-  if [check_shared_lib_support] {
-    run_ld_link_tests \
-	[list \
-	     [list \
-		  "data plt" \
-		  "-shared" "" \
-		  "" \
-		  {data-plt.s} \
-		  {} \
-		  "data-plt.so" \
-	     ] \
-	]
-
-    if [file exist "tmpdir/data-plt.so"] {
-      set objdump_output [run_host_cmd "objdump" "-d tmpdir/data-plt.so"]
-      if { [ regexp "<a@plt>" $objdump_output] } {
-	fail "data plt"
-      } {
-	pass "data plt"
-      }
-    }
-  }
-
   run_ld_link_tests \
       [list \
 	  [list \
@@ -166,6 +142,9 @@ if [istarget "loongarch64-*-*"] {
     run_dump_test "relr-got-shared"
     run_dump_test "relr-text-shared"
     run_dump_test "abssym_shared"
+    run_dump_test "data-plt"
+    run_dump_test "bad_pcrel20_s2"
+    run_dump_test "bad_pcala_hi20"
   }
 
   if [check_pie_support] {
-- 
2.34.1



More information about the Binutils mailing list