From 534cfca985709515f4871f0aa1efc57c4e274a1a Mon Sep 17 00:00:00 2001
From: yala <zhaojunchao@loongson.cn>
Date: Fri, 3 Mar 2023 11:19:40 +0800
Subject: [PATCH] ld:Fix option unresolved-symbols=ignore-all does not work
 when LARCH_64 appears alone

Change-Id: Id06b7b77deefa64a2ea2c9f10a63130e5e4aaf65
---
 bfd/elfnn-loongarch.c                         | 11 +++++---
 ld/testsuite/ld-undefined/undefine-ignored.c  | 10 +++++++
 .../ld-undefined/undefine-ignored.exp         | 26 +++++++++++++++++++
 3 files changed, 43 insertions(+), 4 deletions(-)
 create mode 100644 ld/testsuite/ld-undefined/undefine-ignored.c
 create mode 100644 ld/testsuite/ld-undefined/undefine-ignored.exp

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 459dd0df..5bf528b9 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -1201,7 +1201,8 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 	}
     }
 
-  if (h->root.type == bfd_link_hash_undefweak)
+  if (h->root.type == bfd_link_hash_undefweak ||
+      h->root.type == bfd_link_hash_undefined)
     {
       if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)
 	  || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
@@ -1211,7 +1212,8 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 	{
 	  /* Make sure this symbol is output as a dynamic symbol.
 	     Undefined weak syms won't yet be marked as dynamic.  */
-	  if (!bfd_elf_link_record_dynamic_symbol (info, h))
+	  if (h->root.type == bfd_link_hash_undefweak &&
+              !bfd_elf_link_record_dynamic_symbol (info, h))
 	    return false;
 
 	  if (h->dynindx == -1)
@@ -2303,6 +2305,7 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
       char tls_type;
       bfd_vma relocation, off, ie_off;
       int i, j;
+      bool ignored = false;
 
       howto = loongarch_elf_rtype_to_howto (input_bfd, r_type);
       if (howto == NULL || r_type == R_LARCH_GNU_VTINHERIT
@@ -2344,8 +2347,8 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
 	}
       else
 	{
-	  bool warned, ignored;
 
+      bool warned;
 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
 				   r_symndx, symtab_hdr, sym_hashes,
 				   h, sec, relocation,
@@ -2497,7 +2500,7 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
 		{
 		  if (h->dynindx == -1)
 		    {
-		      if (h->root.type == bfd_link_hash_undefined)
+		      if (h->root.type == bfd_link_hash_undefined && !ignored)
 			(*info->callbacks->undefined_symbol)
 			  (info, name, input_bfd, input_section,
 			   rel->r_offset, true);
diff --git a/ld/testsuite/ld-undefined/undefine-ignored.c b/ld/testsuite/ld-undefined/undefine-ignored.c
new file mode 100644
index 00000000..7955df04
--- /dev/null
+++ b/ld/testsuite/ld-undefined/undefine-ignored.c
@@ -0,0 +1,10 @@
+#include <stdio.h>
+// a is R_LARCH_64 type
+extern struct wl_interface a;
+
+struct wl_interface * b = &a;
+
+int main(void)
+{
+    return 0;
+}
diff --git a/ld/testsuite/ld-undefined/undefine-ignored.exp b/ld/testsuite/ld-undefined/undefine-ignored.exp
new file mode 100644
index 00000000..7975fbdc
--- /dev/null
+++ b/ld/testsuite/ld-undefined/undefine-ignored.exp
@@ -0,0 +1,26 @@
+set testund "undefined-ignored"
+set testfn "undefined-ignored function"
+set testline "undefined-ignored line"
+
+if [istarget loongarch*-*-*] {
+    if { ![is_remote host] && [which $CC] == 0 } {
+        verbose "Could not find C compiler!" 1
+            untested $testund
+            untested $testfn
+            untested $testline
+    } elseif { ![ld_compile "$CC -c" "$srcdir/$subdir/undefine-ignored.c" \
+                 tmpdir/undefine-ignored.o] } {
+        verbose "Unable to compile test file!" 1
+            unresolved $testund
+            unresolved $testfn
+            unresolved $testline
+    }
+
+    if { ![is_elf_format] } then {
+        unsupported $testname
+    } elseif {![ld_link $ld tmpdir/a.out \
+        "-e main --unresolved-symbols=ignore-all tmpdir/undefine-ignored.o"]} then {
+            verbose "ld faile"
+            fail $testname
+        }
+}
-- 
2.27.0

