llvm.org GIT mirror llvm / 67b61d3
[llvm-objcopy] - Strip undefined symbols if they are no longer referenced following --only-section This is https://bugs.llvm.org/show_bug.cgi?id=40004. In this patch I teach llvm-objcopy to remove undefined symbols if them are not used anymore after applying -j/--only-section option. Differential revision: https://reviews.llvm.org/D62317 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361642 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar 4 months ago
2 changed file(s) with 67 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
0 ## Here we want to check that llvm-objcopy removes an undefined symbol
1 ## if all references to it have been stripped.
2
3 # RUN: yaml2obj --docnum=1 %s -o %t.o
4 # RUN: llvm-objcopy -j .other.section %t.o %t2.o
5 # RUN: llvm-readobj --symbols %t2.o | FileCheck %s --implicit-check-not=bar
6
7 # RUN: llvm-objcopy -j .text -j .rela.text1 %t.o %t2.o
8 # RUN: llvm-readobj --symbols %t2.o | FileCheck %s --check-prefix=BAR
9
10 # BAR: bar
11
12 --- !ELF
13 FileHeader:
14 Class: ELFCLASS64
15 Data: ELFDATA2LSB
16 Type: ET_REL
17 Machine: EM_X86_64
18 Sections:
19 - Name: .text
20 Type: SHT_PROGBITS
21 - Name: .rela.text1
22 Type: SHT_RELA
23 Relocations:
24 - Offset: 0x0000000000000001
25 Symbol: bar
26 Type: R_X86_64_32
27 - Name: .rela.text2
28 Type: SHT_RELA
29 Relocations:
30 - Offset: 0x0000000000000001
31 Symbol: bar
32 Type: R_X86_64_32
33 - Name: .other.section
34 Type: SHT_PROGBITS
35 Symbols:
36 - Name: bar
37 Binding: STB_GLOBAL
38 ...
39
40 ## Check we remove unreferenced undefined symbols, even if
41 ## they weren't previously referenced. This follows GNU.
42
43 # RUN: yaml2obj --docnum=2 %s -o %t.o
44 # RUN: llvm-objcopy -j .keep_me %t.o %t2.o
45 # RUN: llvm-readobj --symbols %t2.o | FileCheck %s --implicit-check-not=bar
46
47 --- !ELF
48 FileHeader:
49 Class: ELFCLASS64
50 Data: ELFDATA2LSB
51 Type: ET_REL
52 Machine: EM_X86_64
53 Sections:
54 - Name: .keep_me
55 Type: SHT_PROGBITS
56 Symbols:
57 - Name: bar
58 Binding: STB_GLOBAL
59 ...
386386 // The purpose of this loop is to mark symbols referenced by sections
387387 // (like GroupSection or RelocationSection). This way, we know which
388388 // symbols are still 'needed' and which are not.
389 if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty()) {
389 if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
390 !Config.OnlySection.empty()) {
390391 for (auto &Section : Obj.sections())
391392 Section.markSymbols();
392393 }
412413 if ((Config.StripUnneeded ||
413414 is_contained(Config.UnneededSymbolsToRemove, Sym.Name)) &&
414415 isUnneededSymbol(Sym))
416 return true;
417
418 // We want to remove undefined symbols if all references have been stripped.
419 if (!Config.OnlySection.empty() && !Sym.Referenced &&
420 Sym.getShndx() == SHN_UNDEF)
415421 return true;
416422
417423 return false;