[v2,3/3] LoongArch: Reword message for unresolvable relocs

Message ID 20241225044146.142652-4-xry111@xry111.site
State New
Headers
Series LoongArch: Allow and correctly resolve PC-relative relocs against local undefined weak symbols |

Commit Message

Xi Ruoyao Dec. 25, 2024, 4:41 a.m. UTC
  For PDE, "recompiling with -fPIE" just makes no sense.

For PIE, "recompiling with -fPIE" makes sense for unresolvable absolute
relocs, but not unresolveable PC-relative relocs: if the reloc is
already PC-relative, the problem is not the reloc is PC-relative or
absolute, but the reloc is not applicable for external symbols.

If we hit an unresolvable reloc in PDE or an unresolvable PC-relative
reloc in PIE, it means the programmer has somehow wrongly instructed the
compiler to treat external symbols as local symbols.  A misuse of
-mdirect-extern-access can cause the issue, so we can suggest
-mno-direct-extern-access.  And in all cases (DSO/PIE/PDE) a mismatching
symbol visibility can also cause the issue, so we should also suggest to
check the visibility.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
---
 bfd/elfnn-loongarch.c                         | 23 ++++++++++++++-----
 .../ld-loongarch-elf/bad_pcala_hi20_global.d  |  2 +-
 .../bad_pcala_hi20_global_pie.d               |  2 +-
 .../ld-loongarch-elf/bad_pcala_hi20_weak.d    |  2 +-
 .../bad_pcala_hi20_weak_pie.d                 |  2 +-
 .../ld-loongarch-elf/bad_pcrel20_s2_global.d  |  2 +-
 .../ld-loongarch-elf/bad_pcrel20_s2_weak.d    |  2 +-
 7 files changed, 23 insertions(+), 12 deletions(-)
  

Patch

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 9d3c0eef5e3..47467b4ddf3 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -863,9 +863,17 @@  bad_static_reloc (struct bfd_link_info *info,
 {
   reloc_howto_type * r = loongarch_elf_rtype_to_howto (abfd, r_type);
   const char *object;
-  const char *pic;
+  const char *pic_opt;
   const char *name = NULL;
 
+  /* If this, the problem is we are referring an external symbol in
+     a way only working for local symbols, not PC-relative vs.
+	 absolute.  */
+  bool bad_extern_access =
+    (bfd_link_pde (info)
+     || r_type == R_LARCH_PCREL20_S2
+     || r_type == R_LARCH_PCALA_HI20);
+
   if (h)
     name = h->root.root.string;
   else if (isym)
@@ -873,12 +881,12 @@  bad_static_reloc (struct bfd_link_info *info,
 					    elf_symtab_hdr (abfd).sh_link,
 					    isym->st_name);
   if (name == NULL || *name == '\0')
-    name ="<nameless>";
+    name = "<nameless>";
 
   if (bfd_link_dll (info))
     {
       object = _("a shared object");
-      pic = _("; recompile with -fPIC");
+      pic_opt = "-fPIC";
     }
   else
     {
@@ -886,13 +894,16 @@  bad_static_reloc (struct bfd_link_info *info,
 	object = _("a PIE object");
       else
 	object = _("a PDE object");
-      pic = _("; recompile with -fPIE");
+
+      pic_opt = bad_extern_access ? "-mno-direct-extern-access" : "-fPIE";
     }
 
   (*_bfd_error_handler)
    (_("%pB:(%pA+%#lx): relocation %s against `%s` can not be used when making "
-      "%s%s"),
-    abfd, sec, (long) rel->r_offset, r ? r->name : _("<unknown>"), name, object, pic);
+      "%s; recompile with %s%s"),
+    abfd, sec, (long) rel->r_offset, r ? r->name : _("<unknown>"), name,
+    object, pic_opt,
+    bad_extern_access ? _(" and check the symbol visibility") : "");
   bfd_set_error (bfd_error_bad_value);
   return false;
 }
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d
index 6ecefd1d48a..4552ddf334f 100644
--- a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d
@@ -2,4 +2,4 @@ 
 #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
+#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC and check the symbol visibility
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d
index 5dc2f7c4e4b..a5cd359ac63 100644
--- a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d
@@ -1,4 +1,4 @@ 
 #name: PC-relative relocation making executable
 #source: bad_pcala_hi20_global_pie.s
 #ld: -pie -z undefs --defsym _start=0
-#error: .*: relocation R_LARCH_PCALA_HI20 against `sym` can not be used when making a PIE object; recompile with -fPIE
+#error: .*: relocation R_LARCH_PCALA_HI20 against `sym` can not be used when making a PIE object; recompile with -mno-direct-extern-access and check the symbol visibility
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d
index cefc42cf89f..a39920875a0 100644
--- a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d
@@ -2,4 +2,4 @@ 
 #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
+#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC and check the symbol visibility
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d
index 5515f257191..62a3d8dae25 100644
--- a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d
@@ -1,4 +1,4 @@ 
 #name: PC-relative relocation making executable
 #source: bad_pcala_hi20_weak_pie.s
 #ld: -pie --defsym _start=0
-#error: .*: relocation R_LARCH_PCALA_HI20 against `sym` can not be used when making a PIE object; recompile with -fPIE
+#error: .*: relocation R_LARCH_PCALA_HI20 against `sym` can not be used when making a PIE object; recompile with -mno-direct-extern-access and check the symbol visibility
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d
index 8e063dfb0ec..d5a4cde420c 100644
--- a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d
@@ -2,4 +2,4 @@ 
 #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
+#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC and check the symbol visibility
diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d
index 605df24988a..23b644339f9 100644
--- a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d
+++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d
@@ -2,4 +2,4 @@ 
 #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
+#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC and check the symbol visibility