[PATCH] D56480: [llvm-objcopy] [COFF] Implmement --strip-unneeded and -x/--discard-all for symbols

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 9 01:40:48 PST 2019


mstorsjo created this revision.
mstorsjo added reviewers: jhenderson, alexshap, jakehehrlich, rnk.
Herald added a subscriber: rupprecht.

This depends on D55881 <https://reviews.llvm.org/D55881> for which I'm still awaiting an ack from @jakehehrlich.


Repository:
  rL LLVM

https://reviews.llvm.org/D56480

Files:
  test/tools/llvm-objcopy/COFF/strip-unneeded.yaml
  tools/llvm-objcopy/COFF/COFFObjcopy.cpp


Index: tools/llvm-objcopy/COFF/COFFObjcopy.cpp
===================================================================
--- tools/llvm-objcopy/COFF/COFFObjcopy.cpp
+++ tools/llvm-objcopy/COFF/COFFObjcopy.cpp
@@ -29,7 +29,8 @@
 
 static Error handleArgs(const CopyConfig &Config, Object &Obj) {
   // If we need to do per-symbol removals, initialize the Referenced field.
-  if (!Config.SymbolsToRemove.empty())
+  if (Config.StripUnneeded || Config.DiscardAll ||
+      !Config.SymbolsToRemove.empty())
     if (Error E = Obj.markSymbols())
       return E;
 
@@ -46,6 +47,12 @@
       return true;
     }
 
+    // GNU objcopy keeps referenced local symbols and external symbols
+    // if Config.DiscaredAll is set, similar to what StripUnneeded does.
+    if ((Config.StripUnneeded || Config.DiscardAll) && !Sym.Referenced &&
+        Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC)
+      return true;
+
     return false;
   });
   return Error::success();
Index: test/tools/llvm-objcopy/COFF/strip-unneeded.yaml
===================================================================
--- /dev/null
+++ test/tools/llvm-objcopy/COFF/strip-unneeded.yaml
@@ -0,0 +1,63 @@
+# RUN: yaml2obj %s > %t.in.o
+
+# RUN: llvm-objdump -t %t.in.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-PRE
+
+# RUN: llvm-objcopy --strip-unneeded %t.in.o %t.out.o
+# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefix=SYMBOLS
+
+# Despite the name, --discard-all (-x) also only removes unreferenced
+# local symbols.
+
+# RUN: llvm-objcopy --discard-all %t.in.o %t.out.o
+# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefix=SYMBOLS
+
+# RUN: llvm-objcopy -x %t.in.o %t.out.o
+# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefix=SYMBOLS
+
+# RUN: cp %t.in.o %t.out.o
+# RUN: llvm-strip -x %t.out.o
+# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefix=SYMBOLS
+
+# RUN: cp %t.in.o %t.out.o
+# RUN: llvm-strip --discard-all %t.out.o
+# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefix=SYMBOLS
+
+# SYMBOLS: SYMBOL TABLE:
+# SYMBOLS-NEXT: external
+# SYMBOLS-PRE-NEXT: local_unreferenced
+# SYMBOLS-NEXT: local_referenced
+# SYMBOLS-EMPTY:
+
+--- !COFF
+header:          
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:        
+  - Name:            .text
+    Characteristics: [  ]
+    Alignment:       4
+    SectionData:     E800000000C3C3C3
+    Relocations:     
+      - VirtualAddress:  1
+        SymbolName:      local_referenced
+        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:            local_unreferenced
+    Value:           6
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            local_referenced
+    Value:           7
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56480.180799.patch
Type: text/x-patch
Size: 3212 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190109/70e4666b/attachment.bin>


More information about the llvm-commits mailing list