summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Storsjo <martin@martin.st>2019-01-15 09:34:55 +0000
committerMartin Storsjo <martin@martin.st>2019-01-15 09:34:55 +0000
commit0820e5029ea67525d3f63637ec61fdec8769bdf8 (patch)
tree3d113a661c885e564aa8bc3bb7ca2b013039e1b3
parent596cf3df5c120538a3373b469f2eba1cfecfdd8f (diff)
[llvm-objcopy] [COFF] Implement --strip-all[-gnu] for symbols
Differential Revision: https://reviews.llvm.org/D56481
-rw-r--r--llvm/test/tools/llvm-objcopy/COFF/strip-all.yaml55
-rw-r--r--llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp10
-rw-r--r--llvm/tools/llvm-objcopy/COFF/Writer.cpp5
3 files changed, 68 insertions, 2 deletions
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-all.yaml b/llvm/test/tools/llvm-objcopy/COFF/strip-all.yaml
new file mode 100644
index 00000000000..8a92ac61509
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-all.yaml
@@ -0,0 +1,55 @@
+# RUN: yaml2obj %s > %t.in.o
+
+# RUN: llvm-objdump -t %t.in.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-PRE
+
+# RUN: llvm-objcopy --strip-all %t.in.o %t.out.o
+# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefix=SYMBOLS
+# RUN: llvm-readobj -relocs %t.out.o | FileCheck %s --check-prefix=RELOCS
+
+# Test that -S, llvm-strip without arguments and --strip-all-gnu produces
+# output identical to --strip-all above.
+# RUN: llvm-objcopy -S %t.in.o %t.out-short.o
+# RUN: cmp %t.out.o %t.out-short.o
+
+# RUN: cp %t.in.o %t.out-strip.o
+# RUN: llvm-strip %t.out-strip.o
+# RUN: cmp %t.out.o %t.out-strip.o
+
+# RUN: llvm-objcopy --strip-all-gnu %t.in.o %t.out-gnu.o
+# RUN: cmp %t.out.o %t.out-gnu.o
+
+# SYMBOLS: SYMBOL TABLE:
+# SYMBOLS-PRE-NEXT: external
+# SYMBOLS-PRE-NEXT: external_undefined
+# SYMBOLS-EMPTY:
+
+# RELOCS: Relocations [
+# RELOCS-NEXT: ]
+
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: [ ]
+sections:
+ - Name: .text
+ Characteristics: [ ]
+ Alignment: 4
+ SectionData: 488B0500000000C3
+ Relocations:
+ - VirtualAddress: 3
+ SymbolName: external_undefined
+ Type: IMAGE_REL_AMD64_REL32
+symbols:
+ - Name: external
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+ - Name: external_undefined
+ Value: 0
+ SectionNumber: 0
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
index ea46b841bcf..6b386d29979 100644
--- a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
+++ b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
@@ -28,6 +28,11 @@ using namespace object;
using namespace COFF;
static Error handleArgs(const CopyConfig &Config, Object &Obj) {
+ // StripAll removes all symbols and thus also removes all relocations.
+ if (Config.StripAll || Config.StripAllGNU)
+ for (Section &Sec : Obj.Sections)
+ Sec.Relocs.clear();
+
// If we need to do per-symbol removals, initialize the Referenced field.
if (Config.StripUnneeded || Config.DiscardAll ||
!Config.SymbolsToRemove.empty())
@@ -36,6 +41,11 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
// Actually do removals of symbols.
Obj.removeSymbols([&](const Symbol &Sym) {
+ // For StripAll, all relocations have been stripped and we remove all
+ // symbols.
+ if (Config.StripAll || Config.StripAllGNU)
+ return true;
+
if (is_contained(Config.SymbolsToRemove, Sym.Name)) {
// Explicitly removing a referenced symbol is an error.
if (Sym.Referenced)
diff --git a/llvm/tools/llvm-objcopy/COFF/Writer.cpp b/llvm/tools/llvm-objcopy/COFF/Writer.cpp
index e32bf6832d9..385d43b1bae 100644
--- a/llvm/tools/llvm-objcopy/COFF/Writer.cpp
+++ b/llvm/tools/llvm-objcopy/COFF/Writer.cpp
@@ -46,8 +46,9 @@ void COFFWriter::layoutSections() {
S.Header.PointerToRawData = FileSize;
FileSize += S.Header.SizeOfRawData; // For executables, this is already
// aligned to FileAlignment.
- if (S.Header.NumberOfRelocations > 0)
- S.Header.PointerToRelocations = FileSize;
+ S.Header.NumberOfRelocations = S.Relocs.size();
+ S.Header.PointerToRelocations =
+ S.Header.NumberOfRelocations > 0 ? FileSize : 0;
FileSize += S.Relocs.size() * sizeof(coff_relocation);
FileSize = alignTo(FileSize, FileAlignment);