llvm.org GIT mirror llvm / 8185e09
[llvm-objcopy] [COFF] Implmement --strip-unneeded and -x/--discard-all for symbols Differential Revision: https://reviews.llvm.org/D56480 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@350927 91177308-0d34-0410-b5e6-96231b3b80d8 Martin Storsjo 7 months ago
4 changed file(s) with 96 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
0 --- !COFF
1 header:
2 Machine: IMAGE_FILE_MACHINE_AMD64
3 Characteristics: [ ]
4 sections:
5 - Name: .text
6 Characteristics: [ ]
7 Alignment: 4
8 SectionData: E800000000C3C3C3
9 Relocations:
10 - VirtualAddress: 1
11 SymbolName: local_referenced
12 Type: IMAGE_REL_AMD64_REL32
13 symbols:
14 - Name: external
15 Value: 0
16 SectionNumber: 1
17 SimpleType: IMAGE_SYM_TYPE_NULL
18 ComplexType: IMAGE_SYM_DTYPE_NULL
19 StorageClass: IMAGE_SYM_CLASS_EXTERNAL
20 - Name: external_undefined
21 Value: 0
22 SectionNumber: 0
23 SimpleType: IMAGE_SYM_TYPE_NULL
24 ComplexType: IMAGE_SYM_DTYPE_NULL
25 StorageClass: IMAGE_SYM_CLASS_EXTERNAL
26 - Name: local_unreferenced
27 Value: 6
28 SectionNumber: 1
29 SimpleType: IMAGE_SYM_TYPE_NULL
30 ComplexType: IMAGE_SYM_DTYPE_NULL
31 StorageClass: IMAGE_SYM_CLASS_STATIC
32 - Name: local_referenced
33 Value: 7
34 SectionNumber: 1
35 SimpleType: IMAGE_SYM_TYPE_NULL
36 ComplexType: IMAGE_SYM_DTYPE_NULL
37 StorageClass: IMAGE_SYM_CLASS_STATIC
38 - Name: local_undefined_unreferenced
39 Value: 0
40 SectionNumber: 0
41 SimpleType: IMAGE_SYM_TYPE_NULL
42 ComplexType: IMAGE_SYM_DTYPE_NULL
43 StorageClass: IMAGE_SYM_CLASS_STATIC
44 ...
0 RUN: yaml2obj %p/Inputs/discard-locals.yaml > %t.in.o
1
2 RUN: llvm-objdump -t %t.in.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-PRE
3
4 RUN: llvm-objcopy --discard-all %t.in.o %t.out.o
5 RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefixes=SYMBOLS
6
7 RUN: llvm-objcopy -x %t.in.o %t.out-x.o
8 RUN: cmp %t.out.o %t.out-x.o
9
10 RUN: cp %t.in.o %t.strip-x.o
11 RUN: llvm-strip -x %t.strip-x.o
12 RUN: cmp %t.out.o %t.strip-x.o
13
14 RUN: cp %t.in.o %t.strip-discard-all.o
15 RUN: llvm-strip --discard-all %t.strip-discard-all.o
16 RUN: cmp %t.out.o %t.strip-discard-all.o
17
18 SYMBOLS: SYMBOL TABLE:
19 SYMBOLS-NEXT: external
20 SYMBOLS-NEXT: external_undefined
21 SYMBOLS-PRE-NEXT: local_unreferenced
22 SYMBOLS-NEXT: local_referenced
23 SYMBOLS-NEXT: local_undefined_unreferenced
24 SYMBOLS-EMPTY:
0 RUN: yaml2obj %p/Inputs/discard-locals.yaml > %t.in.o
1
2 RUN: llvm-objdump -t %t.in.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-PRE
3
4 RUN: llvm-objcopy --strip-unneeded %t.in.o %t.out.o
5 RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefix=SYMBOLS
6
7 SYMBOLS: SYMBOL TABLE:
8 SYMBOLS-NEXT: external
9 SYMBOLS-NEXT: external_undefined
10 SYMBOLS-PRE-NEXT: local_unreferenced
11 SYMBOLS-NEXT: local_referenced
12 SYMBOLS-PRE-NEXT: local_undefined_unreferenced
13 SYMBOLS-EMPTY:
2828
2929 static Error handleArgs(const CopyConfig &Config, Object &Obj) {
3030 // If we need to do per-symbol removals, initialize the Referenced field.
31 if (!Config.SymbolsToRemove.empty())
31 if (Config.StripUnneeded || Config.DiscardAll ||
32 !Config.SymbolsToRemove.empty())
3233 if (Error E = Obj.markSymbols())
3334 return E;
3435
4344 "' because it is named in a relocation.",
4445 llvm::errc::invalid_argument));
4546 return true;
47 }
48
49 if (!Sym.Referenced && Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC) {
50 if (Config.StripUnneeded)
51 return true;
52 // GNU objcopy keeps referenced local symbols and external symbols
53 // if --discard-all is set, similar to what --strip-unneeded does,
54 // but undefined local symbols are kept when --discard-all is set.
55 if (Config.DiscardAll && Sym.Sym.SectionNumber != 0)
56 return true;
4657 }
4758
4859 return false;