LoongArch: Not alloc dynamic relocs if symbol is absolute

Message ID 20240706075925.28455-1-hejinyang@loongson.cn
State New
Headers
Series LoongArch: Not alloc dynamic relocs if symbol is absolute |

Checks

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

Commit Message

Jinyang He July 6, 2024, 7:59 a.m. UTC
  The absolute symbol should be resolved to const when link to dso or exe.
Alloc dynamic relocs will cause extra space and R_LARCH_NONE finally.
---
 bfd/elfnn-loongarch.c                              | 14 +++++++-------
 ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp |  2 ++
 ld/testsuite/ld-loongarch-elf/script_abssym.s      |  3 +++
 ld/testsuite/ld-loongarch-elf/script_abssym_pie.d  |  6 ++++++
 .../ld-loongarch-elf/script_abssym_shared.d        |  6 ++++++
 5 files changed, 24 insertions(+), 7 deletions(-)
 create mode 100644 ld/testsuite/ld-loongarch-elf/script_abssym.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/script_abssym_pie.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/script_abssym_shared.d
  

Comments

Fangrui Song July 6, 2024, 4:31 p.m. UTC | #1
On Sat, Jul 6, 2024 at 12:59 AM Jinyang He <hejinyang@loongson.cn> wrote:
>
> The absolute symbol should be resolved to const when link to dso or exe.
> Alloc dynamic relocs will cause extra space and R_LARCH_NONE finally.
> ---
>  bfd/elfnn-loongarch.c                              | 14 +++++++-------
>  ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp |  2 ++
>  ld/testsuite/ld-loongarch-elf/script_abssym.s      |  3 +++
>  ld/testsuite/ld-loongarch-elf/script_abssym_pie.d  |  6 ++++++
>  .../ld-loongarch-elf/script_abssym_shared.d        |  6 ++++++
>  5 files changed, 24 insertions(+), 7 deletions(-)
>  create mode 100644 ld/testsuite/ld-loongarch-elf/script_abssym.s
>  create mode 100644 ld/testsuite/ld-loongarch-elf/script_abssym_pie.d
>  create mode 100644 ld/testsuite/ld-loongarch-elf/script_abssym_shared.d
>
> diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
> index 06eec0ded6e..d2ce39359ba 100644
> --- a/bfd/elfnn-loongarch.c
> +++ b/bfd/elfnn-loongarch.c
> @@ -899,6 +899,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>        unsigned int r_type;
>        unsigned int r_symndx;
>        struct elf_link_hash_entry *h;
> +      bool is_abs_symbol = false;
>        Elf_Internal_Sym *isym = NULL;
>
>        r_symndx = ELFNN_R_SYM (rel->r_info);
> @@ -917,6 +918,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>           if (isym == NULL)
>             return false;
>
> +         is_abs_symbol = isym->st_shndx == SHN_ABS;
>           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
>             {
>               h = elfNN_loongarch_get_local_sym_hash (htab, abfd, rel, true);
> @@ -935,6 +937,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>           while (h->root.type == bfd_link_hash_indirect
>                  || h->root.type == bfd_link_hash_warning)
>             h = (struct elf_link_hash_entry *) h->root.u.i.link;
> +         is_abs_symbol = bfd_is_abs_symbol (&h->root);
>         }
>
>        /* It is referenced by a non-shared object.  */
> @@ -1142,13 +1145,6 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>               && bfd_link_pic (info)
>               && (sec->flags & SEC_ALLOC) != 0)
>             {
> -             bool is_abs_symbol = false;
> -
> -             if (r_symndx < symtab_hdr->sh_info)
> -               is_abs_symbol = isym->st_shndx == SHN_ABS;
> -             else
> -               is_abs_symbol = bfd_is_abs_symbol (&h->root);
> -
>               if (!is_abs_symbol)
>                 {
>                   _bfd_error_handler
> @@ -1165,6 +1161,10 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>         case R_LARCH_JUMP_SLOT:
>         case R_LARCH_64:
>
> +         /* Resolved to const. */
> +         if (is_abs_symbol)
> +           break;
> +
>           need_dynreloc = 1;
>
>           /* If resolved symbol is defined in this object,
> diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
> index 4606ede9fc0..e36dfe561fc 100644
> --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
> +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
> @@ -141,6 +141,7 @@ if [istarget "loongarch64-*-*"] {
>      run_dump_test "relr-discard-shared"
>      run_dump_test "relr-got-shared"
>      run_dump_test "relr-text-shared"
> +    run_dump_test "script_abssym_shared"
>    }
>
>    if [check_pie_support] {
> @@ -149,6 +150,7 @@ if [istarget "loongarch64-*-*"] {
>      run_dump_test "relr-discard-pie"
>      run_dump_test "relr-got-pie"
>      run_dump_test "relr-text-pie"
> +    run_dump_test "script_abssym_pie"
>    }
>
>    run_dump_test "max_imm_b16"
> diff --git a/ld/testsuite/ld-loongarch-elf/script_abssym.s b/ld/testsuite/ld-loongarch-elf/script_abssym.s
> new file mode 100644
> index 00000000000..3eacc766054
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/script_abssym.s
> @@ -0,0 +1,3 @@
> +.section .data,"aw"
> +.quad _size8
> +.word _size4
> diff --git a/ld/testsuite/ld-loongarch-elf/script_abssym_pie.d b/ld/testsuite/ld-loongarch-elf/script_abssym_pie.d
> new file mode 100644
> index 00000000000..84bda98b7a0
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/script_abssym_pie.d
> @@ -0,0 +1,6 @@
> +#source: script_abssym.s
> +#ld: -pie -e 0 --defsym _size8=0 --defsym _size4=0
> +#readelf: -r
> +#...
> +There are no relocations in this file.
> +#...
> diff --git a/ld/testsuite/ld-loongarch-elf/script_abssym_shared.d b/ld/testsuite/ld-loongarch-elf/script_abssym_shared.d
> new file mode 100644
> index 00000000000..2af8f97c851
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/script_abssym_shared.d
> @@ -0,0 +1,6 @@
> +#source: script_abssym.s
> +#ld: -shared --defsym _size8=0 --defsym _size4=0
> +#readelf: -r
> +#...
> +There are no relocations in this file.
> +#...
> --
> 2.45.2
>

LGTM. The test doesn't need to be named `script_*`, though. Absolute
symbols can be defined with `abs = 0x1000` in assembly or symbol
assignments (e.g. `--defsym`).
  

Patch

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 06eec0ded6e..d2ce39359ba 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -899,6 +899,7 @@  loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
       unsigned int r_type;
       unsigned int r_symndx;
       struct elf_link_hash_entry *h;
+      bool is_abs_symbol = false;
       Elf_Internal_Sym *isym = NULL;
 
       r_symndx = ELFNN_R_SYM (rel->r_info);
@@ -917,6 +918,7 @@  loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 	  if (isym == NULL)
 	    return false;
 
+	  is_abs_symbol = isym->st_shndx == SHN_ABS;
 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
 	    {
 	      h = elfNN_loongarch_get_local_sym_hash (htab, abfd, rel, true);
@@ -935,6 +937,7 @@  loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 	  while (h->root.type == bfd_link_hash_indirect
 		 || h->root.type == bfd_link_hash_warning)
 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+	  is_abs_symbol = bfd_is_abs_symbol (&h->root);
 	}
 
       /* It is referenced by a non-shared object.  */
@@ -1142,13 +1145,6 @@  loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 	      && bfd_link_pic (info)
 	      && (sec->flags & SEC_ALLOC) != 0)
 	    {
-	      bool is_abs_symbol = false;
-
-	      if (r_symndx < symtab_hdr->sh_info)
-		is_abs_symbol = isym->st_shndx == SHN_ABS;
-	      else
-		is_abs_symbol = bfd_is_abs_symbol (&h->root);
-
 	      if (!is_abs_symbol)
 		{
 		  _bfd_error_handler
@@ -1165,6 +1161,10 @@  loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 	case R_LARCH_JUMP_SLOT:
 	case R_LARCH_64:
 
+	  /* Resolved to const. */
+	  if (is_abs_symbol)
+	    break;
+
 	  need_dynreloc = 1;
 
 	  /* If resolved symbol is defined in this object,
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index 4606ede9fc0..e36dfe561fc 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -141,6 +141,7 @@  if [istarget "loongarch64-*-*"] {
     run_dump_test "relr-discard-shared"
     run_dump_test "relr-got-shared"
     run_dump_test "relr-text-shared"
+    run_dump_test "script_abssym_shared"
   }
 
   if [check_pie_support] {
@@ -149,6 +150,7 @@  if [istarget "loongarch64-*-*"] {
     run_dump_test "relr-discard-pie"
     run_dump_test "relr-got-pie"
     run_dump_test "relr-text-pie"
+    run_dump_test "script_abssym_pie"
   }
 
   run_dump_test "max_imm_b16"
diff --git a/ld/testsuite/ld-loongarch-elf/script_abssym.s b/ld/testsuite/ld-loongarch-elf/script_abssym.s
new file mode 100644
index 00000000000..3eacc766054
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/script_abssym.s
@@ -0,0 +1,3 @@ 
+.section .data,"aw"
+.quad _size8
+.word _size4
diff --git a/ld/testsuite/ld-loongarch-elf/script_abssym_pie.d b/ld/testsuite/ld-loongarch-elf/script_abssym_pie.d
new file mode 100644
index 00000000000..84bda98b7a0
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/script_abssym_pie.d
@@ -0,0 +1,6 @@ 
+#source: script_abssym.s
+#ld: -pie -e 0 --defsym _size8=0 --defsym _size4=0
+#readelf: -r
+#...
+There are no relocations in this file.
+#...
diff --git a/ld/testsuite/ld-loongarch-elf/script_abssym_shared.d b/ld/testsuite/ld-loongarch-elf/script_abssym_shared.d
new file mode 100644
index 00000000000..2af8f97c851
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/script_abssym_shared.d
@@ -0,0 +1,6 @@ 
+#source: script_abssym.s
+#ld: -shared --defsym _size8=0 --defsym _size4=0
+#readelf: -r
+#...
+There are no relocations in this file.
+#...