[v1] LoongArch: Add bad static relocation check and output more information to user

Message ID 20240321075427.1509853-1-cailulu@loongson.cn
State New
Headers
Series [v1] LoongArch: Add bad static relocation check and output more information to user |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Testing passed

Commit Message

Lulu Cai March 21, 2024, 7:54 a.m. UTC
  1. Absolute address symbols cannot be used with -shared.
We output more information to the user than just BFD_ASSETR.

2. When the tls_sec segment does not exist, calculating the
relocation of tls le will cause ld to crash. Added tls_tpoff_base
for calculation of tls le relocations.
---
 bfd/elfnn-loongarch.c                         | 33 +++++++++++++++++--
 .../ld-loongarch-elf/ld-loongarch-elf.exp     |  3 ++
 .../ld-loongarch-elf/reloc_abs_with_shared.d  |  6 ++++
 .../ld-loongarch-elf/reloc_abs_with_shared.s  | 13 ++++++++
 .../ld-loongarch-elf/reloc_le_with_shared.d   |  6 ++++
 .../ld-loongarch-elf/reloc_le_with_shared.s   | 12 +++++++
 .../ld-loongarch-elf/reloc_ler_with_shared.d  |  6 ++++
 .../ld-loongarch-elf/reloc_ler_with_shared.s  | 13 ++++++++
 8 files changed, 89 insertions(+), 3 deletions(-)
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.s
  

Patch

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index ee99fd7b2f8..5a6cdf88d7b 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -730,6 +730,21 @@  loongarch_tls_transition (bfd *input_bfd,
    allocate space in the global offset table or procedure linkage
    table.  */
 
+static bool
+bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h)
+{
+  /* We propably can improve the information to tell users that they should
+     be recompile the code with -fPIC or -fPIE, just like what x86 does.  */
+  reloc_howto_type * r = loongarch_elf_rtype_to_howto (abfd, r_type);
+  (*_bfd_error_handler)
+   (_("%pB: relocation %s against `%s` can not be used when making a shared "
+     "object; recompile with -fPIC"),
+    abfd, r ? r->name : _("<unknown>"),
+    h != NULL ? h->root.root.string : "a local symbol");
+  bfd_set_error (bfd_error_bad_value);
+  return false;
+}
+
 static bool
 loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 			    asection *sec, const Elf_Internal_Rela *relocs)
@@ -874,7 +889,7 @@  loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 	case R_LARCH_TLS_LE_HI20_R:
 	case R_LARCH_SOP_PUSH_TLS_TPREL:
 	  if (!bfd_link_executable (info))
-	    return false;
+	    return bad_static_reloc (abfd, r_type, h);
 
 	  if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
 							   r_symndx,
@@ -892,6 +907,9 @@  loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 
 	case R_LARCH_ABS_HI20:
 	case R_LARCH_SOP_PUSH_ABSOLUTE:
+	  if (bfd_link_pic (info))
+	    return bad_static_reloc (abfd, r_type, h);
+
 	  if (h != NULL)
 	    /* If this reloc is in a read-only section, we might
 	       need a copy reloc.  We can't check reliably at this
@@ -2550,6 +2568,15 @@  loongarch_reloc_is_fatal (struct bfd_link_info *info,
   })
 
 
+static bfd_vma
+tls_tpoff_base (struct bfd_link_info *info)
+{
+  /* If tls_sec is NULL, we should have signalled an error already.  */
+  if (elf_hash_table (info)->tls_sec == NULL)
+    return 0;
+  return elf_hash_table (info)->tls_sec->vma;
+}
+
 static bfd_vma
 tls_dtpoff_base (struct bfd_link_info *info)
 {
@@ -3416,7 +3443,7 @@  loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
 
 	case R_LARCH_TLS_LE_HI20_R:
 	  relocation += rel->r_addend;
-	  relocation -= elf_hash_table (info)->tls_sec->vma;
+	  relocation -= tls_tpoff_base (info);
 	  RELOCATE_TLS_TP32_HI20 (relocation);
 	  break;
 
@@ -3599,7 +3626,7 @@  loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
 	  BFD_ASSERT (resolved_local && elf_hash_table (info)->tls_sec);
 
 	  relocation += rel->r_addend;
-	  relocation -= elf_hash_table (info)->tls_sec->vma;
+	  relocation -= tls_tpoff_base (info);
 	  break;
 
 	/* TLS IE LD/GD process separately is troublesome.
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index 507b244869c..d2b43768b70 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -131,6 +131,9 @@  if [istarget "loongarch64-*-*"] {
     run_dump_test "tlsdesc-dso"
     run_dump_test "desc-norelax"
     run_dump_test "desc-relax"
+    run_dump_test "reloc_le_with_shared"
+    run_dump_test "reloc_ler_with_shared"
+    run_dump_test "reloc_abs_with_shared"
   }
 
   if [check_pie_support] {
diff --git a/ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.d b/ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.d
new file mode 100644
index 00000000000..532e84fb600
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.d
@@ -0,0 +1,6 @@ 
+#source: reloc_abs_with_shared.s
+#as:
+#ld: -shared
+#error: .*relocation R_LARCH_ABS_HI20 against `s` can not be used when making a shared object; recompile with -fPIC
+#...
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.s b/ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.s
new file mode 100644
index 00000000000..710fb5a38d8
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.s
@@ -0,0 +1,13 @@ 
+	.text
+	.globl  s
+	.section        .data,"awT",@progbits
+	.align	2
+	.type   s, @object
+	.size   s, 4
+ s:
+         .word  123
+
+	.text
+
+	lu12i.w $r4,%abs_hi20(s)
+	addi.d  $r4,$r4,%abs_lo12(s)
diff --git a/ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.d b/ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.d
new file mode 100644
index 00000000000..562b079af44
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.d
@@ -0,0 +1,6 @@ 
+#source: reloc_le_with_shared.s
+#as:
+#ld: -shared
+#error: .*relocation R_LARCH_TLS_LE_HI20 against `s` can not be used when making a shared object; recompile with -fPIC
+#...
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.s b/ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.s
new file mode 100644
index 00000000000..016fbf31d54
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.s
@@ -0,0 +1,12 @@ 
+	.text
+	.globl  s
+	.section        .tdata,"awT",@progbits
+	.align	2
+	.type   s, @object
+	.size   s, 4
+ s:
+         .word  123
+
+	.text
+
+	la.tls.le $r4, s
diff --git a/ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.d b/ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.d
new file mode 100644
index 00000000000..5a271c91d06
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.d
@@ -0,0 +1,6 @@ 
+#source: reloc_ler_with_shared.s
+#as:
+#ld: -shared
+#error:.*relocation R_LARCH_TLS_LE_HI20_R against `s` can not be used when making a shared object; recompile with -fPIC
+#...
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.s b/ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.s
new file mode 100644
index 00000000000..0d6840102d3
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.s
@@ -0,0 +1,13 @@ 
+	.text
+	.globl  s
+	.section        .tdata,"awT",@progbits
+	.align	2
+	.type   s, @object
+	.size   s, 4
+ s:
+         .word  123
+
+	.text
+
+	lu12i.w $r4,%le_hi20_r(s)
+	add.d   $r4,$r4,$r2,%le_add_r(s)