Skip to content

Commit cf51c80

Browse files
committedMay 26, 2018
[llvm-objcopy] Add --keep-file-symbols option
This option prevent from removing file symbols while removing symbols. Differential Revision: https://reviews.llvm.org/D46830 llvm-svn: 333339
1 parent fb89e7a commit cf51c80

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# RUN: yaml2obj %s > %t
2+
# RUN: llvm-objcopy --strip-all --keep-file-symbols %t %t2
3+
# RUN: llvm-readobj -symbols %t2 | FileCheck %s --check-prefix=STRIPALL
4+
# RUN: llvm-objcopy --keep-file-symbols --strip-symbol foo %t %t2
5+
# RUN: llvm-readobj -symbols %t2 | FileCheck %s --check-prefix=STRIP
6+
7+
!ELF
8+
FileHeader:
9+
Class: ELFCLASS64
10+
Data: ELFDATA2LSB
11+
Type: ET_REL
12+
Machine: EM_X86_64
13+
Sections:
14+
- Name: .text
15+
Type: SHT_PROGBITS
16+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
17+
Address: 0x1000
18+
AddressAlign: 0x0000000000000010
19+
Size: 64
20+
Symbols:
21+
Local:
22+
- Name: foo
23+
Type: STT_FILE
24+
Section: .text
25+
Global:
26+
- Name: bar
27+
Type: STT_FUNC
28+
Section: .text
29+
30+
#STRIPALL: Symbols [
31+
#STRIPALL-NEXT: Symbol {
32+
#STRIPALL-NEXT: Name: foo
33+
#STRIPALL-NEXT: Value: 0x0
34+
#STRIPALL-NEXT: Size: 0
35+
#STRIPALL-NEXT: Binding: Local
36+
#STRIPALL-NEXT: Type: File
37+
#STRIPALL-NEXT: Other: 0
38+
#STRIPALL-NEXT: Section: .text
39+
#STRIPALL-NEXT: }
40+
#STRIPALL-NEXT:]
41+
42+
#STRIP: Symbols [
43+
#STRIP-NEXT: Symbol {
44+
#STRIP-NEXT: Name:
45+
#STRIP-NEXT: Value: 0x0
46+
#STRIP-NEXT: Size: 0
47+
#STRIP-NEXT: Binding: Local
48+
#STRIP-NEXT: Type: None
49+
#STRIP-NEXT: Other: 0
50+
#STRIP-NEXT: Section: Undefined
51+
#STRIP-NEXT: }
52+
#STRIP-NEXT: Symbol {
53+
#STRIP-NEXT: Name: foo
54+
#STRIP-NEXT: Value: 0x0
55+
#STRIP-NEXT: Size: 0
56+
#STRIP-NEXT: Binding: Local
57+
#STRIP-NEXT: Type: File
58+
#STRIP-NEXT: Other: 0
59+
#STRIP-NEXT: Section: .text
60+
#STRIP-NEXT: }
61+
#STRIP-NEXT: Symbol {
62+
#STRIP-NEXT: Name: bar
63+
#STRIP-NEXT: Value: 0x0
64+
#STRIP-NEXT: Size: 0
65+
#STRIP-NEXT: Binding: Global
66+
#STRIP-NEXT: Type: Function
67+
#STRIP-NEXT: Other: 0
68+
#STRIP-NEXT: Section: .text
69+
#STRIP-NEXT: }
70+
#STRIP-NEXT:]

‎llvm/tools/llvm-objcopy/ObjcopyOpts.td

+2
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ def only_keep_debug : Flag<["-", "--"], "only-keep-debug">,
9292
HelpText<"Currently ignored. Only for compaitability with GNU objcopy.">;
9393
def strip_unneeded : Flag<["-", "--"], "strip-unneeded">,
9494
HelpText<"Remove all symbols not needed by relocations">;
95+
def keep_file_symbols : Flag<["-", "--"], "keep-file-symbols">,
96+
HelpText<"Do not remove file symbols">;

‎llvm/tools/llvm-objcopy/llvm-objcopy.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ struct CopyConfig {
174174
bool Weaken = false;
175175
bool DiscardAll = false;
176176
bool OnlyKeepDebug = false;
177+
bool KeepFileSymbols = false;
177178
};
178179

179180
using SectionPred = std::function<bool(const SectionBase &Sec)>;
@@ -273,8 +274,9 @@ void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
273274
}
274275

275276
Obj.removeSymbols([&](const Symbol &Sym) {
276-
if (!Config.SymbolsToKeep.empty() &&
277-
is_contained(Config.SymbolsToKeep, Sym.Name))
277+
if ((!Config.SymbolsToKeep.empty() &&
278+
is_contained(Config.SymbolsToKeep, Sym.Name)) ||
279+
(Config.KeepFileSymbols && Sym.Type == STT_FILE))
278280
return false;
279281

280282
if (Config.DiscardAll && Sym.Binding == STB_LOCAL &&
@@ -410,7 +412,8 @@ void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
410412
// and at least one of those symbols is present
411413
// (equivalently, the updated symbol table is not empty)
412414
// the symbol table and the string table should not be removed.
413-
if (!Config.SymbolsToKeep.empty() && !Obj.SymbolTable->empty()) {
415+
if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
416+
!Obj.SymbolTable->empty()) {
414417
RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
415418
if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
416419
return false;
@@ -531,6 +534,7 @@ CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
531534
Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
532535
Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all);
533536
Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
537+
Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
534538
for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
535539
Config.SymbolsToLocalize.push_back(Arg->getValue());
536540
for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))

0 commit comments

Comments
 (0)
Please sign in to comment.