LoongArch: Modify inconsistent behavior of ld with --unresolved-symbols=ignore-all

Message ID 20240220061459.3353898-1-fanpeng@loongson.cn
State New
Headers
Series LoongArch: Modify inconsistent behavior of ld with --unresolved-symbols=ignore-all |

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

ticat_fp Feb. 20, 2024, 6:14 a.m. UTC
  Ignore errors when producing executable files that reference external symbols defined
in other files.

Testcase is:
resolv.c:
int main(int argc, char *argv[]) {
    return argc;
}

t.c:

extern const struct my_struct ms1;
static const struct my_struct *ms = &ms1;

t.h:
typedef struct my_struct {
    char *str;
    int i;
} my_struct;

Compiling and linking command with:
gcc t.c -c ; gcc resolv.c -c
gcc resolv.o t.o -o resolv -Wl,--unresolved-symbols=ignore-all

Got error as:
~/install/usr/bin/ld: t.o:(.data.rel+0x0): undefined reference to `ms1'
collect2: error: ld returned 1 exit status

---
 bfd/elfnn-loongarch.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
  

Comments

mengqinggang Feb. 22, 2024, 9:12 a.m. UTC | #1
在 2024/2/20 下午2:14, Peng Fan 写道:
> Ignore errors when producing executable files that reference external symbols defined
> in other files.
>
> Testcase is:
> resolv.c:
> int main(int argc, char *argv[]) {
>      return argc;
> }
>
> t.c:
>
> extern const struct my_struct ms1;
> static const struct my_struct *ms = &ms1;
>
> t.h:
> typedef struct my_struct {
>      char *str;
>      int i;
> } my_struct;
>
> Compiling and linking command with:
> gcc t.c -c ; gcc resolv.c -c
> gcc resolv.o t.o -o resolv -Wl,--unresolved-symbols=ignore-all
>
> Got error as:
> ~/install/usr/bin/ld: t.o:(.data.rel+0x0): undefined reference to `ms1'
> collect2: error: ld returned 1 exit status
>
> ---
>   bfd/elfnn-loongarch.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
> index 1895699af06..6b2d5a7164d 100644
> --- a/bfd/elfnn-loongarch.c
> +++ b/bfd/elfnn-loongarch.c
> @@ -2662,6 +2662,7 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
>         char tls_type;
>         bfd_vma relocation, off, ie_off, desc_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
> @@ -2703,7 +2704,7 @@ 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,
> @@ -2867,7 +2868,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)

RELOC_FOR_GLOBAL_SYMBOL macro also check undefined symbol, this check can delete directly.

>   			(*info->callbacks->undefined_symbol)
>   			  (info, name, input_bfd, input_section,
>   			   rel->r_offset, true);
  

Patch

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 1895699af06..6b2d5a7164d 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -2662,6 +2662,7 @@  loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
       char tls_type;
       bfd_vma relocation, off, ie_off, desc_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
@@ -2703,7 +2704,7 @@  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,
@@ -2867,7 +2868,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);