]> granicus.if.org Git - llvm/commitdiff
Revert [llvm-objcopy] Allow strip symtab from executables and DSOs
authorJordan Rupprecht <rupprecht@google.com>
Wed, 10 Jul 2019 23:32:44 +0000 (23:32 +0000)
committerJordan Rupprecht <rupprecht@google.com>
Wed, 10 Jul 2019 23:32:44 +0000 (23:32 +0000)
This reverts r365193 (git commit 194f16b3548bcb23a7f0fd638778ed72edd18d37)

This patch doesn't work with binaries built w/ `--emit-relocs`, e.g.

```
$ echo 'int main() { return 0; }' | clang -Wl,--emit-relocs -x c - -o foo && llvm-objcopy --strip-unneeded foo
llvm-objcopy: error: 'foo': not stripping symbol '__gmon_start__' because it is named in a relocation
```

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365712 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-objcopy/ELF/no-symbol-relocation.test
test/tools/llvm-objcopy/ELF/preserve-segment-contents-ehdr-phdrs.test
test/tools/llvm-objcopy/ELF/remove-multiple-sections.test
test/tools/llvm-objcopy/ELF/segment-shift-section-remove.test
test/tools/llvm-objcopy/ELF/segment-test-remove-section.test
test/tools/llvm-objcopy/ELF/strip-unneeded-all-symbols.test [deleted file]
tools/llvm-objcopy/ELF/ELFObjcopy.cpp
tools/llvm-objcopy/ELF/Object.cpp
tools/llvm-objcopy/ELF/Object.h

index 64d366318568a5d7275d9a555e385c1ef3d99da4..9def536c239c1d537fdc61a7ed1ae576612055f3 100644 (file)
@@ -6,7 +6,7 @@
 FileHeader:
   Class:           ELFCLASS64
   Data:            ELFDATA2LSB
-  Type:            ET_REL
+  Type:            ET_EXEC
   Machine:         EM_X86_64
 Sections:
   - Name:            .text
index 3352313c2714b0faa56f5a80102b5a16b2f5604a..9dc63d753f60b5af136c5e228831642c71697463 100644 (file)
@@ -13,7 +13,7 @@
 # BEFORE:      Type: PT_LOAD
 # BEFORE-NEXT: Offset: 0x240
 
-# AFTER:      SectionHeaderCount: 3
+# AFTER:      SectionHeaderCount: 5
 # AFTER:      Type: PT_LOAD
 # AFTER-NEXT: Offset: 0x0
 # AFTER:      Type: PT_LOAD
index a162303ccacfbec729c623496496d7c2e9c858f8..1f1bc4ce1910e3befd8d64a2ce2d0f44767f1edc 100644 (file)
@@ -87,6 +87,34 @@ Sections:
 # CHECK:   }
 # CHECK:   Section {
 # CHECK:     Index: 4
+# CHECK:     Name: .symtab
+# CHECK:     Type: SHT_SYMTAB (0x2)
+# CHECK:     Flags [ (0x0)
+# CHECK:     ]
+# CHECK:     Address: 0x0
+# CHECK:     Offset:
+# CHECK:     Size:
+# CHECK:     Link: 5
+# CHECK:     Info: 1
+# CHECK:     AddressAlignment: 8
+# CHECK:     EntrySize: 24
+# CHECK:   }
+# CHECK:   Section {
+# CHECK:     Index: 5
+# CHECK:     Name: .strtab
+# CHECK:     Type: SHT_STRTAB (0x3)
+# CHECK:     Flags [ (0x0)
+# CHECK:     ]
+# CHECK:     Address: 0x0
+# CHECK:     Offset:
+# CHECK:     Size:
+# CHECK:     Link: 0
+# CHECK:     Info: 0
+# CHECK:     AddressAlignment: 1
+# CHECK:     EntrySize: 0
+# CHECK:   }
+# CHECK:   Section {
+# CHECK:     Index: 6
 # CHECK:     Name: .shstrtab
 # CHECK:     Type: SHT_STRTAB (0x3)
 # CHECK:     Flags [ (0x0)
index cef783cb9b29bec259c8d08dd416d20e66db3d14..563a9e3f836ba8ce3473a50a788c5a2c90f3ac48 100644 (file)
@@ -41,7 +41,7 @@ ProgramHeaders:
     Sections:
       - Section: .text3
 
-#CHECK: SectionHeaderCount: 4
+#CHECK: SectionHeaderCount: 6
 
 # CHECK:           Sections [
 # CHECK-NEXT:        Section {
@@ -92,6 +92,34 @@ ProgramHeaders:
 # CHECK-NEXT:        }
 # CHECK-NEXT:        Section {
 # CHECK-NEXT:          Index: 3
+# CHECK-NEXT:          Name: .symtab
+# CHECK-NEXT:          Type: SHT_SYMTAB (0x2)
+# CHECK-NEXT:          Flags [ (0x0)
+# CHECK-NEXT:          ]
+# CHECK-NEXT:          Address: 0x0
+# CHECK-NEXT:          Offset: 0x3000
+# CHECK-NEXT:          Size: 24
+# CHECK-NEXT:          Link: 4
+# CHECK-NEXT:          Info: 1
+# CHECK-NEXT:          AddressAlignment: 8
+# CHECK-NEXT:          EntrySize: 24
+# CHECK-NEXT:        }
+# CHECK-NEXT:        Section {
+# CHECK-NEXT:          Index: 4
+# CHECK-NEXT:          Name: .strtab
+# CHECK-NEXT:          Type: SHT_STRTAB (0x3)
+# CHECK-NEXT:          Flags [ (0x0)
+# CHECK-NEXT:          ]
+# CHECK-NEXT:          Address: 0x0
+# CHECK-NEXT:          Offset: 0x3018
+# CHECK-NEXT:          Size:
+# CHECK-NEXT:          Link: 0
+# CHECK-NEXT:          Info: 0
+# CHECK-NEXT:          AddressAlignment: 1
+# CHECK-NEXT:          EntrySize: 0
+# CHECK-NEXT:        }
+# CHECK-NEXT:        Section {
+# CHECK-NEXT:          Index: 5
 # CHECK-NEXT:          Name: .shstrtab
 # CHECK-NEXT:          Type: SHT_STRTAB (0x3)
 # CHECK-NEXT:          Flags [ (0x0)
index 60cd5f3483d3702a7e5032dfbb27435707e4db66..f9a5dd7811e70de43a3a0935e39458e22dd85f34 100644 (file)
@@ -46,7 +46,7 @@ ProgramHeaders:
 # Make sure that when we remove a section we overwrite it with zeros
 # DATA: {{^[^[:blank:]]+}} 00 00 00 00
 
-#CHECK: SectionHeaderCount: 4
+#CHECK: SectionHeaderCount: 6
 
 # CHECK: Sections [
 # CHECK:   Section {
@@ -97,6 +97,34 @@ ProgramHeaders:
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Section {
 # CHECK-NEXT:     Index: 3
+# CHECK-NEXT:     Name: .symtab
+# CHECK-NEXT:     Type: SHT_SYMTAB (0x2)
+# CHECK-NEXT:     Flags [ (0x0)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0x0
+# CHECK-NEXT:     Offset:
+# CHECK-NEXT:     Size:
+# CHECK-NEXT:     Link: 4
+# CHECK-NEXT:     Info: 1
+# CHECK-NEXT:     AddressAlignment: 8
+# CHECK-NEXT:     EntrySize: 24
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Section {
+# CHECK-NEXT:     Index: 4
+# CHECK-NEXT:     Name: .strtab
+# CHECK-NEXT:     Type: SHT_STRTAB (0x3)
+# CHECK-NEXT:     Flags [ (0x0)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0x0
+# CHECK-NEXT:     Offset:
+# CHECK-NEXT:     Size:
+# CHECK-NEXT:     Link: 0
+# CHECK-NEXT:     Info: 0
+# CHECK-NEXT:     AddressAlignment: 1
+# CHECK-NEXT:     EntrySize: 0
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Section {
+# CHECK-NEXT:     Index: 5
 # CHECK-NEXT:     Name: .shstrtab
 # CHECK-NEXT:     Type: SHT_STRTAB (0x3)
 # CHECK-NEXT:     Flags [ (0x0)
diff --git a/test/tools/llvm-objcopy/ELF/strip-unneeded-all-symbols.test b/test/tools/llvm-objcopy/ELF/strip-unneeded-all-symbols.test
deleted file mode 100644 (file)
index e2fd420..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-## Stripping unneeded symbols from execuatble/DSO should
-## eliminate the static symbol table, because it's not used
-## by the dynamic loader. 
-
-# RUN: yaml2obj %s > %t
-# RUN: cp %t %t1
-# RUN: llvm-objcopy --strip-unneeded %t %t2
-# RUN: llvm-readobj --section-headers %t2 | FileCheck %s
-
-!ELF
-FileHeader:
-  Class:    ELFCLASS64
-  Data:     ELFDATA2LSB
-  Type:     ET_EXEC
-  Machine:  EM_X86_64
-Symbols:
-  - Name:     bar
-  - Name:     foo
-    Binding:  STB_GLOBAL
-
-# CHECK-NOT: .symtab
index bbaac96f070bd565c6df2afb6d0a792b368ef61c..b366c6e5598770426fa03fad1702ef88328faec4 100644 (file)
@@ -423,7 +423,7 @@ static Error updateAndRemoveSymbols(const CopyConfig &Config, Object &Obj) {
 
     if ((Config.StripUnneeded ||
          is_contained(Config.UnneededSymbolsToRemove, Sym.Name)) &&
-        (!Obj.isRelocatable() || isUnneededSymbol(Sym)))
+        isUnneededSymbol(Sym))
       return true;
 
     // We want to remove undefined symbols if all references have been stripped.
index 2d85b3ad36f8fc4d788e7ba54996eebf5c0a1004..fa696380e17c51437cfd44b3375f658b3d28322b 100644 (file)
@@ -1995,25 +1995,6 @@ template <class ELFT> Error ELFWriter<ELFT>::write() {
   return Buf.commit();
 }
 
-static Error removeUnneededSections(Object &Obj) {
-  // We can remove an empty symbol table from non-relocatable objects.
-  // Relocatable objects typically have relocation sections whose
-  // sh_link field points to .symtab, so we can't remove .symtab
-  // even if it is empty.
-  if (Obj.isRelocatable() || Obj.SymbolTable == nullptr ||
-      !Obj.SymbolTable->empty())
-    return Error::success();
-
-  // .strtab can be used for section names. In such a case we shouldn't
-  // remove it.
-  auto *StrTab = Obj.SymbolTable->getStrTab() == Obj.SectionNames
-                     ? nullptr
-                     : Obj.SymbolTable->getStrTab();
-  return Obj.removeSections(false, [&](const SectionBase &Sec) {
-    return &Sec == Obj.SymbolTable || &Sec == StrTab;
-  });
-}
-
 template <class ELFT> Error ELFWriter<ELFT>::finalize() {
   // It could happen that SectionNames has been removed and yet the user wants
   // a section header table output. We need to throw an error if a user tries
@@ -2023,8 +2004,6 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
                              "cannot write section header table because "
                              "section header string table was removed");
 
-  if (Error E = removeUnneededSections(Obj))
-    return E;
   Obj.sortSections();
 
   // We need to assign indexes before we perform layout because we need to know
index e56c155cfef1d3d59e475a8bb2865ae64f3cf183..f3df93b9662fb230667b606f9512dc9800b9499c 100644 (file)
@@ -1051,9 +1051,6 @@ public:
     Segments.emplace_back(llvm::make_unique<Segment>(Data));
     return *Segments.back();
   }
-  bool isRelocatable() const {
-    return Type != ELF::ET_DYN && Type != ELF::ET_EXEC;
-  }
 };
 
 } // end namespace elf