Clear HAS_RELOC if there are no relocations
authorH.J. Lu <hjl.tools@gmail.com>
Tue, 2 Feb 2016 11:30:21 +0000 (03:30 -0800)
committerH.J. Lu <hjl.tools@gmail.com>
Tue, 2 Feb 2016 11:30:48 +0000 (03:30 -0800)
The HAS_RELOC bit should be cleared when relocations are removed from
relocatable files.

bfd/

PR binutils/19547
* elf.c (assign_section_numbers): Clear HAS_RELOC if there are
no relocations in relocatable files.

binutils/

PR binutils/19547
* testsuite/binutils-all/objcopy.exp
(objcopy_test_without_global_symbol): New proc.
Run objcopy_test_without_global_symbol.
* testsuite/binutils-all/pr19547.c: New file.

bfd/ChangeLog
bfd/elf.c
binutils/ChangeLog
binutils/testsuite/binutils-all/objcopy.exp
binutils/testsuite/binutils-all/pr19547.c [new file with mode: 0644]

index 06596f9d5e86d66c09ce2924e367aafa749d6256..898af5684a42c376cc15f9fd24fde2a8c33bd2e2 100644 (file)
@@ -1,3 +1,9 @@
+2016-02-02  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR binutils/19547
+       * elf.c (assign_section_numbers): Clear HAS_RELOC if there are
+       no relocations in relocatable files.
+
 2016-02-01  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/19553
index 74c2f2d33a46057fb1551a5f50190cf001ff1779..30112ae90023a106e0d441364a7b9f0a7a29fba4 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -3355,6 +3355,8 @@ assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   /* SHT_GROUP sections are in relocatable files only.  */
   if (link_info == NULL || bfd_link_relocatable (link_info))
     {
+      bfd_size_type reloc_count = 0;
+
       /* Put SHT_GROUP sections first.  */
       for (sec = abfd->sections; sec != NULL; sec = sec->next)
        {
@@ -3371,7 +3373,14 @@ assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
              else
                d->this_idx = section_number++;
            }
+
+         /* Count relocations.  */
+         reloc_count += sec->reloc_count;
        }
+
+      /* Clear HAS_RELOC if there are no relocations.  */
+      if (reloc_count == 0)
+       abfd->flags &= ~HAS_RELOC;
     }
 
   for (sec = abfd->sections; sec; sec = sec->next)
index edc66cd4c0926cb1260bb489433490cdbbb3892d..32bbf9b66614f7dc180f51c712cd90be8af3f2a3 100644 (file)
@@ -1,3 +1,11 @@
+2016-02-02  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR binutils/19547
+       * testsuite/binutils-all/objcopy.exp
+       (objcopy_test_without_global_symbol): New proc.
+       Run objcopy_test_without_global_symbol.
+       * testsuite/binutils-all/pr19547.c: New file.
+
 2016-01-28  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR binutils/19523
index 376f3ed42fb0b3dac07daecf3315c168c35e4e9d..b2c54ea3a6c88fa193c1d0a831f0cd5a1f90776f 100644 (file)
@@ -1104,3 +1104,43 @@ if [is_elf_format] {
     run_dump_test "exclude-1b"
 }
 run_dump_test "localize-hidden-2"
+
+# Test objcopying an object file without global symbol
+
+proc objcopy_test_without_global_symbol { } {
+    global OBJCOPY
+    global OBJCOPYFLAGS
+    global OBJDUMP
+    global OBJDUMPFLAGS
+    global srcdir
+    global subdir
+
+    set test "strip without global symbol "
+
+    if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
+       untested $test
+       return
+    }
+
+    if [is_remote host] {
+       set objfile [remote_download host tmpdir/pr19547.o]
+    } else {
+       set objfile tmpdir/pr19547.o
+    }
+
+    set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
+    if ![string equal "" $exec_output] {
+       fail $test
+       return
+    }
+
+    set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
+    if {![regexp "no symbols" $exec_output]} {
+       fail $test
+       return
+    }
+
+    pass $test
+}
+
+objcopy_test_without_global_symbol
diff --git a/binutils/testsuite/binutils-all/pr19547.c b/binutils/testsuite/binutils-all/pr19547.c
new file mode 100644 (file)
index 0000000..4aef486
--- /dev/null
@@ -0,0 +1 @@
+static char foo[]__attribute__ ((used)) = "foo";