ELF: Don't check relocations in non-loaded, non-alloced sections
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 4 Jun 2020 12:58:34 +0000 (05:58 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 4 Jun 2020 12:58:47 +0000 (05:58 -0700)
Don't do anything special with non-loaded, non-alloced sections.
In particular, any relocs in such sections should not affect GOT
and PLT reference counting (ie. we don't allow them to create GOT
or PLT entries), there's no possibility or desire to optimize TLS
relocs, and there's not much point in propagating relocs to shared
libs that the dynamic linker won't relocate.

Since check_relocs is no longer called on non-loaded, non-alloced
sections, remove SEC_ALLOC check.  Resolve relocation in debug section
against symbol defined in shared library to 0.

bfd/

PR ld/26080
* elf-m10300.c (mn10300_elf_relocate_section): Resolve relocation
in debug section against symbol defined in shared library to 0.
* elf32-i386.c (elf_i386_check_relocs): Remove SEC_ALLOC check.
* elf32-lm32.c (lm32_elf_check_relocs): Likewise.
* elf32-m32r.c (m32r_elf_check_relocs): Likewise.
* elf32-nds32.c (nds32_elf_check_relocs): Likewise.
* elf32-nios2.c (nios2_elf32_check_relocs): Likewise.
* elf32-or1k.c (or1k_elf_check_relocs): Likewise.
* elf32-ppc.c (ppc_elf_check_relocs): Likewise.
* elf32-sh.c (sh_elf_check_relocs): Likewise.
* elf32-xtensa.c (elf_xtensa_check_relocs): Likewise.
* elf64-alpha.c (elf64_alpha_check_relocs): Likewise.
* elf64-ppc.c (ppc64_elf_check_relocs): Likewise.
* elf64-x86-64.c (elf_x86_64_check_relocs): Likewise.
* elfxx-mips.c (_bfd_mips_elf_check_relocs): Likewise.
* elf32-vax.c (elf_vax_check_relocs): Set non_got_ref for non-GOT
reference.
(elf_vax_adjust_dynamic_symbol): Generate a copy reloc only if
there is non-GOT reference.
* elflink.c (_bfd_elf_link_check_relocs): Skip non-loaded,
non-alloced sections.

ld/

PR ld/26080
* testsuite/ld-elf/comm-data.exp: Remove copy_reloc.
* testsuite/ld-elf/comm-data2r.rd: Removed.
* testsuite/ld-elf/comm-data2r.sd: Likewise.
* testsuite/ld-elf/comm-data2r.xd: Likewise.

22 files changed:
bfd/ChangeLog
bfd/elf-m10300.c
bfd/elf32-i386.c
bfd/elf32-lm32.c
bfd/elf32-m32r.c
bfd/elf32-nds32.c
bfd/elf32-nios2.c
bfd/elf32-or1k.c
bfd/elf32-ppc.c
bfd/elf32-sh.c
bfd/elf32-vax.c
bfd/elf32-xtensa.c
bfd/elf64-alpha.c
bfd/elf64-ppc.c
bfd/elf64-x86-64.c
bfd/elflink.c
bfd/elfxx-mips.c
ld/ChangeLog
ld/testsuite/ld-elf/comm-data.exp
ld/testsuite/ld-elf/comm-data2r.rd [deleted file]
ld/testsuite/ld-elf/comm-data2r.sd [deleted file]
ld/testsuite/ld-elf/comm-data2r.xd [deleted file]

index 785951aa0a5d4e77e86077508dd26b0e30d6b5cc..13a3ed180b5c219700252bad36ada1de1dfd15ad 100644 (file)
@@ -1,3 +1,28 @@
+2020-06-04  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/26080
+       * elf-m10300.c (mn10300_elf_relocate_section): Resolve relocation
+       in debug section against symbol defined in shared library to 0.
+       * elf32-i386.c (elf_i386_check_relocs): Remove SEC_ALLOC check.
+       * elf32-lm32.c (lm32_elf_check_relocs): Likewise.
+       * elf32-m32r.c (m32r_elf_check_relocs): Likewise.
+       * elf32-nds32.c (nds32_elf_check_relocs): Likewise.
+       * elf32-nios2.c (nios2_elf32_check_relocs): Likewise.
+       * elf32-or1k.c (or1k_elf_check_relocs): Likewise.
+       * elf32-ppc.c (ppc_elf_check_relocs): Likewise.
+       * elf32-sh.c (sh_elf_check_relocs): Likewise.
+       * elf32-xtensa.c (elf_xtensa_check_relocs): Likewise.
+       * elf64-alpha.c (elf64_alpha_check_relocs): Likewise.
+       * elf64-ppc.c (ppc64_elf_check_relocs): Likewise.
+       * elf64-x86-64.c (elf_x86_64_check_relocs): Likewise.
+       * elfxx-mips.c (_bfd_mips_elf_check_relocs): Likewise.
+       * elf32-vax.c (elf_vax_check_relocs): Set non_got_ref for non-GOT
+       reference.
+       (elf_vax_adjust_dynamic_symbol): Generate a copy reloc only if
+       there is non-GOT reference.
+       * elflink.c (_bfd_elf_link_check_relocs): Skip non-loaded,
+       non-alloced sections.
+
 2020-06-03  Stephen Casner  <casner@acm.org>
 
        Copy several years of fixes from bfd/aoutx.h to bfd/pdp11.c.
index 696514ab054c64824f6a81392748046b85608d33..5a0bb9f005af556c2995971885762be40c7df8cc 100644 (file)
@@ -2066,12 +2066,12 @@ mn10300_elf_relocate_section (bfd *output_bfd,
                      && elf_hash_table (info)->dynamic_sections_created
                      && !SYMBOL_REFERENCES_LOCAL (info, hh))
                  || (r_type == R_MN10300_32
+                     && !SYMBOL_REFERENCES_LOCAL (info, hh)
                      /* _32 relocs in executables force _COPY relocs,
                         such that the address of the symbol ends up
                         being local.  */
-                     && !bfd_link_executable (info)
-                     && !SYMBOL_REFERENCES_LOCAL (info, hh)
-                     && ((input_section->flags & SEC_ALLOC) != 0
+                     && (((input_section->flags & SEC_ALLOC) != 0
+                          && !bfd_link_executable (info))
                          /* DWARF will emit R_MN10300_32 relocations
                             in its sections against symbols defined
                             externally in shared libraries.  We can't
index 544b9315521ef9ea46f72d2388cea0df68b7c0b7..f6f669957c3fa5180f61054e829b0b1808b0edba 100644 (file)
@@ -1487,15 +1487,6 @@ elf_i386_check_relocs (bfd *abfd,
   if (bfd_link_relocatable (info))
     return TRUE;
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
   htab = elf_x86_hash_table (info, I386_ELF_DATA);
   if (htab == NULL)
     {
index 3c31dd44c853d91a3cb058c4a7d118f40d9dd8e4..aba821ffd1a4245b1dee969b7db50974c776077e 100644 (file)
@@ -1128,15 +1128,6 @@ lm32_elf_check_relocs (bfd *abfd,
   if (bfd_link_relocatable (info))
     return TRUE;
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
index 931e138b37a04ad5ed67b99412f4c59b976a3ad2..740be9338295f29aa8db8d10a15ce78499fbff9b 100644 (file)
@@ -3424,15 +3424,6 @@ m32r_elf_check_relocs (bfd *abfd,
   if (bfd_link_relocatable (info))
     return TRUE;
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
   sreloc = NULL;
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
index 01ea277426fb345bb5aad237edc899ab86369bfd..1d3a0f7526f05e693f8b80c4a2804c4bafed6eaf 100644 (file)
@@ -7055,15 +7055,6 @@ nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
       return TRUE;
     }
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
   sym_hashes_end =
index 71200da9b57b05468df2c0c886a379c64f890a50..cdc11f97159810584216c31ccc1695c965b8148b 100644 (file)
@@ -4689,15 +4689,6 @@ nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
   if (bfd_link_relocatable (info))
     return TRUE;
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
   sym_hashes_end = (sym_hashes
index ac62d630ab34b2d59a62eee813f0789075c4ca15..b25f96b42de4a20456f3c57eb8a431996583ca37 100644 (file)
@@ -1880,15 +1880,6 @@ or1k_elf_check_relocs (bfd *abfd,
   if (bfd_link_relocatable (info))
     return TRUE;
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
 
index 0cb7f67504d4d4f56a4272f37d65f4f604a0f12c..995e1a95e23cb5b6cc2e91fdfc7067689c25f896 100644 (file)
@@ -2888,15 +2888,6 @@ ppc_elf_check_relocs (bfd *abfd,
   if (bfd_link_relocatable (info))
     return TRUE;
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
 #ifdef DEBUG
   _bfd_error_handler ("ppc_elf_check_relocs called for section %pA in %pB",
                      sec, abfd);
index 9ec745be199388b3183f0e67b1b6b06d5989d97d..dd670466c3b4219f49c1a94a62560d88d4af9d8e 100644 (file)
@@ -5350,15 +5350,6 @@ sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
   if (bfd_link_relocatable (info))
     return TRUE;
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
   BFD_ASSERT (is_sh_elf (abfd));
 
   symtab_hdr = &elf_symtab_hdr (abfd);
index fa84e0be19ae78349df667a2bffde8a0d8df0581..b5c3d8943a86e561dbd65b44fad5a2188cff0f87 100644 (file)
@@ -712,6 +712,11 @@ elf_vax_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
                h->plt.refcount++;
            }
 
+         /* Non-GOT reference may need a copy reloc in executable or
+            a dynamic reloc in shared library.  */
+         if (h != NULL)
+           h->non_got_ref = 1;
+
          /* If we are creating a shared library, we need to copy the
             reloc into the shared library.  */
          if (bfd_link_pic (info)
@@ -929,6 +934,11 @@ elf_vax_adjust_dynamic_symbol (struct bfd_link_info *info,
   if (bfd_link_pic (info))
     return TRUE;
 
+  /* If there are no references to this symbol that do not use the
+     GOT relocation, we don't need to generate a copy reloc.  */
+  if (!h->non_got_ref)
+    return TRUE;
+
   /* We must allocate the symbol in our .dynbss section, which will
      become part of the .bss section of the executable.  There will be
      an entry for this symbol in the .dynsym section.  The dynamic
index 05c4f8430a2046a9c48546c815f0eb2ebdb2f728..9dc815edbb33eebb252b6f95a829b8929a13427f 100644 (file)
@@ -1039,7 +1039,7 @@ elf_xtensa_check_relocs (bfd *abfd,
   const Elf_Internal_Rela *rel;
   const Elf_Internal_Rela *rel_end;
 
-  if (bfd_link_relocatable (info) || (sec->flags & SEC_ALLOC) == 0)
+  if (bfd_link_relocatable (info))
     return TRUE;
 
   BFD_ASSERT (is_xtensa_elf (abfd));
index 4e4efae0b1bf2d76980d7f51a6a4183674d3e1de..0b31d450dca1eaf36d04be0e99e26600b9e12084 100644 (file)
@@ -1782,15 +1782,6 @@ elf64_alpha_check_relocs (bfd *abfd, struct bfd_link_info *info,
   if (bfd_link_relocatable (info))
     return TRUE;
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
   BFD_ASSERT (is_alpha_elf (abfd));
 
   dynobj = elf_hash_table (info)->dynobj;
index 5f99d4344b7623c8d29c733a5f5d26a935093f5d..769afc5aa5624a7927341ed0fbacfd19718f9542 100644 (file)
@@ -4536,15 +4536,6 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   if (bfd_link_relocatable (info))
     return TRUE;
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
   BFD_ASSERT (is_ppc64_elf (abfd));
 
   htab = ppc_hash_table (info);
index 183c8083464efee694695f6ed5d9695255326bb4..eada0e53ed9e2e5da639224892cb49f1a86b0cb6 100644 (file)
@@ -1862,15 +1862,6 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
   if (bfd_link_relocatable (info))
     return TRUE;
 
-  /* Don't do anything special with non-loaded, non-alloced sections.
-     In particular, any relocs in such sections should not affect GOT
-     and PLT reference counting (ie. we don't allow them to create GOT
-     or PLT entries), there's no possibility or desire to optimize TLS
-     relocs, and there's not much point in propagating relocs to shared
-     libs that the dynamic linker won't relocate.  */
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
   htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
   if (htab == NULL)
     {
index ce6282a9dcfc0691ff05979222adba6e816d1c2f..7e86adec5bdeb9748a142d7c1b779297437cd894 100644 (file)
@@ -3956,8 +3956,16 @@ _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
          Elf_Internal_Rela *internal_relocs;
          bfd_boolean ok;
 
-         /* Don't check relocations in excluded sections.  */
-         if ((o->flags & SEC_RELOC) == 0
+         /* Don't check relocations in excluded sections.  Don't do
+            anything special with non-loaded, non-alloced sections.
+            In particular, any relocs in such sections should not
+            affect GOT and PLT reference counting (ie.  we don't
+            allow them to create GOT or PLT entries), there's no
+            possibility or desire to optimize TLS relocs, and
+            there's not much point in propagating relocs to shared
+            libs that the dynamic linker won't relocate.  */
+         if ((o->flags & SEC_ALLOC) == 0
+             || (o->flags & SEC_RELOC) == 0
              || (o->flags & SEC_EXCLUDE) != 0
              || o->reloc_count == 0
              || ((info->strip == strip_all || info->strip == strip_debugger)
index e563d56dbbedf96d38f0f388bef8a5b894c804d1..6c7aaa3c7c3994116a442d37ec6d06f30a45cb2f 100644 (file)
@@ -8654,10 +8654,8 @@ _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
       call_reloc_p = FALSE;
 
       /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
-        into account when deciding how to define the symbol.
-        Relocations in nonallocatable sections such as .pdr and
-        .debug* should have no effect.  */
-      constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
+        into account when deciding how to define the symbol.  */
+      constrain_symbol_p = TRUE;
 
       switch (r_type)
        {
index d8e7e001f76baa303ccc9e53eac42d3beae284a4..00066c59c07a987f25b3d32ba8d8eff40769ba00 100644 (file)
@@ -1,3 +1,11 @@
+2020-06-04  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/26080
+       * testsuite/ld-elf/comm-data.exp: Remove copy_reloc.
+       * testsuite/ld-elf/comm-data2r.rd: Removed.
+       * testsuite/ld-elf/comm-data2r.sd: Likewise.
+       * testsuite/ld-elf/comm-data2r.xd: Likewise.
+
 2020-06-04  Alan Modra  <amodra@gmail.com>
 
        * testsuite/config/default.exp: Remove global directive outside
index a53a77123c53280b2727165ab64def1b4509dd6c..c735fe244ceeb695daedbbef27fdd2f492ea6451 100644 (file)
@@ -79,12 +79,6 @@ setup_xfail "bfin-*-*"
 
 setup_xfail "arm*-*-*" "ld/13802"
 
-# List targets here that keep copy relocs rather than eliminating
-# them where possible in favour to dynamic relocs in the relevant
-# loadable sections; see also the "-z nocopyreloc" command-line
-# option and the ELIMINATE_COPY_RELOCS macro some backends use.
-set copy_reloc [expr [istarget mn10300-*-*] || [istarget vax-*-*]]
-
 # Verify that a common symbol has been converted to an undefined
 # reference to the global symbol of the same name defined above
 # and that the debug reference has been dropped.
@@ -95,12 +89,9 @@ run_ld_link_tests [list \
        "$AFLAGS" \
        { comm-data2.s } \
        [list \
-           [list readelf -s \
-               [expr { $copy_reloc ? "comm-data2r.sd" : "comm-data2.sd"}]] \
-           [list readelf -r \
-               [expr { $copy_reloc ? "comm-data2r.rd" : "comm-data2.rd"}]] \
-           [list readelf "-x .debug_foo" \
-               [expr { $copy_reloc ? "comm-data2r.xd" : "comm-data2.xd"}]]] \
+           [list readelf -s comm-data2.sd] \
+           [list readelf -r comm-data2.rd] \
+           [list readelf "-x .debug_foo" comm-data2.xd]] \
        "comm-data" \
     ] \
     [list \
diff --git a/ld/testsuite/ld-elf/comm-data2r.rd b/ld/testsuite/ld-elf/comm-data2r.rd
deleted file mode 100644 (file)
index 64c0396..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-Relocation section '\.rela\.dyn' at offset 0x[0-9a-f]+ contains 1 entry:
- +Offset +Info +Type +Sym\.Value +Sym\. Name \+ Addend
-0*12340000 +[0-9a-f]+ +R_.*_COPY +0*12340000 +foo \+ 0
diff --git a/ld/testsuite/ld-elf/comm-data2r.sd b/ld/testsuite/ld-elf/comm-data2r.sd
deleted file mode 100644 (file)
index 685b0be..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-Symbol table '\.dynsym' contains [0-9]+ entries:
- +Num: +Value +Size +Type +Bind +Vis +Ndx +Name
-#...
- +[0-9]+: +0*12340000 +4 +OBJECT +GLOBAL +DEFAULT +[0-9]+ +foo
-#...
-Symbol table '\.symtab' contains [0-9]+ entries:
- +Num: +Value +Size +Type +Bind +Vis +Ndx +Name
-#...
- +[0-9]+: +0*12340000 +4 +OBJECT +GLOBAL +DEFAULT +[0-9]+ +foo
-#pass
diff --git a/ld/testsuite/ld-elf/comm-data2r.xd b/ld/testsuite/ld-elf/comm-data2r.xd
deleted file mode 100644 (file)
index 58f6f2a..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-Hex dump of section '\.debug_foo':
- +0x0*76540000 (?:12340000 00000000|00003412 00000000|00000000 00003412) 00000000 00000000 .*
This page took 0.06049 seconds and 4 git commands to generate.