Skip to content

Commit 5d6e4f0

Browse files
committed
Add functionality to llvm-objcopy to remove prefixes
llvm-objcopy has functionality to add prefixes to symbols (--prefix-symbols). This adds an inverse action to undo the modification and return to the original.
1 parent f7a615a commit 5d6e4f0

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

llvm/include/llvm/ObjCopy/CommonConfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ struct CommonConfig {
218218
uint64_t PadTo = 0;
219219
StringRef SplitDWO;
220220
StringRef SymbolsPrefix;
221+
StringRef SymbolsPrefixRemove;
221222
StringRef AllocSectionsPrefix;
222223
DiscardType DiscardMode = DiscardType::None;
223224

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
331331

332332
if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
333333
Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
334+
335+
if (!Config.SymbolsPrefixRemove.empty() && Sym.Type != STT_SECTION)
336+
if (Sym.Name.rfind(Config.SymbolsPrefixRemove, 0) == 0)
337+
Sym.Name = Sym.Name.substr(Config.SymbolsPrefixRemove.size());
334338
});
335339

336340
// The purpose of this loop is to mark symbols referenced by sections
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# RUN: yaml2obj %s -o %t
2+
# RUN: llvm-objcopy --prefix-symbols-remove __pf_ %t %t2
3+
# RUN: llvm-readobj --symbols %t2 | FileCheck %s
4+
5+
!ELF
6+
FileHeader:
7+
Class: ELFCLASS64
8+
Data: ELFDATA2LSB
9+
Type: ET_REL
10+
Machine: EM_X86_64
11+
Sections:
12+
- Name: .text
13+
Type: SHT_PROGBITS
14+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
15+
Address: 0x1000
16+
AddressAlign: 0x0000000000000010
17+
Size: 64
18+
Symbols:
19+
- Name: __pf_foo
20+
Type: STT_SECTION
21+
Section: .text
22+
- Name: __pf_bar
23+
Type: STT_FILE
24+
Section: .text
25+
- Name: foobar
26+
Type: STT_FUNC
27+
Section: .text
28+
Binding: STB_GLOBAL
29+
- Name: undef
30+
Binding: STB_GLOBAL
31+
32+
# CHECK: Symbols [
33+
# NEXT: Symbol {
34+
# NEXT: Name:
35+
# NEXT: Value: 0x0
36+
# NEXT: Size: 0
37+
# NEXT: Binding: Local
38+
# NEXT: Type: None
39+
# NEXT: Other: 0
40+
# NEXT: Section: Undefined
41+
# NEXT: }
42+
# NEXT: Symbol {
43+
# NEXT: Name: foo
44+
# NEXT: Value: 0x0
45+
# NEXT: Size: 0
46+
# NEXT: Binding: Local
47+
# NEXT: Type: Section
48+
# NEXT: Other: 0
49+
# NEXT: Section: .text
50+
# NEXT: }
51+
# NEXT: Symbol {
52+
# NEXT: Name: bar
53+
# NEXT: Value: 0x0
54+
# NEXT: Size: 0
55+
# NEXT: Binding: Local
56+
# NEXT: Type: File
57+
# NEXT: Other: 0
58+
# NEXT: Section: .text
59+
# NEXT: }
60+
# NEXT: Symbol {
61+
# NEXT: Name: foobar
62+
# NEXT: Value: 0x0
63+
# NEXT: Size: 0
64+
# NEXT: Binding: Global
65+
# NEXT: Type: Function
66+
# NEXT: Other: 0
67+
# NEXT: Section: .text
68+
# NEXT: }
69+
# NEXT: Symbol {
70+
# NEXT: Name: undef
71+
# NEXT: Value: 0x0
72+
# NEXT: Size: 0
73+
# NEXT: Binding: Global
74+
# NEXT: Type: None
75+
# NEXT: Other: 0
76+
# NEXT: Section: Undefined
77+
# NEXT: }
78+
# NEXT: ]

llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,15 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
731731
llvm::crc32(arrayRefFromStringRef(Debug->getBuffer()));
732732
}
733733
Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
734+
734735
Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols);
736+
Config.SymbolsPrefixRemove =
737+
InputArgs.getLastArgValue(OBJCOPY_prefix_symbols_remove);
738+
if (!Config.SymbolsPrefix.empty() && !Config.SymbolsPrefixRemove.empty())
739+
return createStringError(
740+
errc::invalid_argument,
741+
"--prefix-symbols and --prefix-symbols-remove are mutualy exclusive");
742+
735743
Config.AllocSectionsPrefix =
736744
InputArgs.getLastArgValue(OBJCOPY_prefix_alloc_sections);
737745
if (auto Arg = InputArgs.getLastArg(OBJCOPY_extract_partition))

llvm/tools/llvm-objcopy/ObjcopyOpts.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ defm dump_section
203203
defm prefix_symbols
204204
: Eq<"prefix-symbols", "Add <prefix> to the start of every symbol name">,
205205
MetaVarName<"prefix">;
206+
defm prefix_symbols_remove
207+
: Eq<"prefix-symbols-remove",
208+
"Remove <prefix> from the start of every symbol name. No-op for symbols that do not start "
209+
"with <prefix>">,
210+
MetaVarName<"prefix">;
206211

207212
defm prefix_alloc_sections
208213
: Eq<"prefix-alloc-sections", "Add <prefix> to the start of every allocated section name">,

0 commit comments

Comments
 (0)