Skip to content

Commit 1b87ebc

Browse files
authored
[llvm-objcopy] Add --remove-symbol-prefix (#79415)
1 parent 67402fe commit 1b87ebc

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

llvm/docs/CommandGuide/llvm-objcopy.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ multiple file formats.
117117
If specified, symbol and section names specified by other switches are treated
118118
as extended POSIX regular expression patterns.
119119

120+
.. option:: --remove-symbol-prefix <prefix>
121+
122+
Remove ``<prefix>`` from the start of every symbol name. No-op for symbols that do
123+
not start with ``<prefix>``.
124+
120125
.. option:: --remove-section <section>, -R
121126

122127
Remove the specified section from the output. Can be specified multiple times

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/ConfigManager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace objcopy {
1515

1616
Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {
1717
if (!Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
18+
!Common.SymbolsPrefixRemove.empty() ||
1819
!Common.AllocSectionsPrefix.empty() || !Common.KeepSection.empty() ||
1920
!Common.SymbolsToGlobalize.empty() || !Common.SymbolsToKeep.empty() ||
2021
!Common.SymbolsToLocalize.empty() || !Common.SymbolsToWeaken.empty() ||
@@ -33,6 +34,7 @@ Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {
3334

3435
Expected<const MachOConfig &> ConfigManager::getMachOConfig() const {
3536
if (!Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
37+
!Common.SymbolsPrefixRemove.empty() ||
3638
!Common.AllocSectionsPrefix.empty() || !Common.KeepSection.empty() ||
3739
!Common.SymbolsToGlobalize.empty() || !Common.SymbolsToKeep.empty() ||
3840
!Common.SymbolsToLocalize.empty() ||
@@ -54,6 +56,7 @@ Expected<const MachOConfig &> ConfigManager::getMachOConfig() const {
5456
Expected<const WasmConfig &> ConfigManager::getWasmConfig() const {
5557
if (!Common.AddGnuDebugLink.empty() || Common.ExtractPartition ||
5658
!Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
59+
!Common.SymbolsPrefixRemove.empty() ||
5760
!Common.AllocSectionsPrefix.empty() ||
5861
Common.DiscardMode != DiscardType::None || !Common.SymbolsToAdd.empty() ||
5962
!Common.SymbolsToGlobalize.empty() || !Common.SymbolsToLocalize.empty() ||
@@ -74,6 +77,7 @@ Expected<const WasmConfig &> ConfigManager::getWasmConfig() const {
7477
Expected<const XCOFFConfig &> ConfigManager::getXCOFFConfig() const {
7578
if (!Common.AddGnuDebugLink.empty() || Common.ExtractPartition ||
7679
!Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
80+
!Common.SymbolsPrefixRemove.empty() ||
7781
!Common.AllocSectionsPrefix.empty() ||
7882
Common.DiscardMode != DiscardType::None || !Common.AddSection.empty() ||
7983
!Common.DumpSection.empty() || !Common.SymbolsToAdd.empty() ||

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
329329
if (I != Config.SymbolsToRename.end())
330330
Sym.Name = std::string(I->getValue());
331331

332+
if (!Config.SymbolsPrefixRemove.empty() && Sym.Type != STT_SECTION)
333+
if (Sym.Name.compare(0, Config.SymbolsPrefixRemove.size(),
334+
Config.SymbolsPrefixRemove) == 0)
335+
Sym.Name = Sym.Name.substr(Config.SymbolsPrefixRemove.size());
336+
332337
if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
333338
Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
334339
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# RUN: yaml2obj %s -o %t
2+
# RUN: llvm-objcopy --remove-symbol-prefix __pf_ %t %t2
3+
# RUN: llvm-readelf --symbols %t2 | FileCheck %s
4+
5+
## Show that an empty string is permitted as the argument to
6+
## --remove-symbol-prefix.
7+
# RUN: llvm-objcopy --remove-symbol-prefix= %t2 %t3
8+
# RUN: cmp %t2 %t3
9+
10+
## When both options are present, llvm-objcopy should remove
11+
## prefixes first, before adding prefixes.
12+
# RUN: llvm-objcopy --prefix-symbols=__1_ %t %t4
13+
# RUN: llvm-objcopy --prefix-symbols=__2_ %t %t5
14+
# RUN: llvm-objcopy --remove-symbol-prefix=__1_ --prefix-symbols=__2_ %t4 %t6
15+
# RUN: cmp %t5 %t6
16+
17+
## Show that the last --remove-symbol-prefix option wins.
18+
# RUN: llvm-objcopy --remove-symbol-prefix=__pf_ --remove-symbol-prefix=__ %t %7
19+
# RUN: llvm-objcopy --remove-symbol-prefix=__ %t %8
20+
# RUN: cmp %7 %8
21+
22+
!ELF
23+
FileHeader:
24+
Class: ELFCLASS64
25+
Data: ELFDATA2LSB
26+
Type: ET_REL
27+
Machine: EM_X86_64
28+
Sections:
29+
- Name: .text
30+
Type: SHT_PROGBITS
31+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
32+
Address: 0x1000
33+
AddressAlign: 0x0000000000000010
34+
Size: 64
35+
Symbols:
36+
- Name: __pf_foo
37+
Type: STT_SECTION
38+
Section: .text
39+
- Name: __pf_bar
40+
Type: STT_FILE
41+
Section: .text
42+
- Name: foobar
43+
Type: STT_FUNC
44+
Section: .text
45+
Binding: STB_GLOBAL
46+
- Name: foo__pf_bar1
47+
Type: STT_FUNC
48+
Section: .text
49+
Binding: STB_GLOBAL
50+
- Name: __pf_foo__pf_bar2
51+
Type: STT_FUNC
52+
Section: .text
53+
Binding: STB_GLOBAL
54+
- Name: undef
55+
Binding: STB_GLOBAL
56+
57+
# CHECK: Symbol table '.symtab' contains 7 entries:
58+
# CHECK-NEXT: Name
59+
# CHECK-NEXT: {{ $}}
60+
# CHECK-NEXT: __pf_foo
61+
# CHECK-NEXT: bar
62+
# CHECK-NEXT: foobar
63+
# CHECK-NEXT: foo__pf_bar1
64+
# CHECK-NEXT: foo__pf_bar2
65+
# CHECK-NEXT: undef

llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,11 @@ 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_remove_symbol_prefix);
738+
735739
Config.AllocSectionsPrefix =
736740
InputArgs.getLastArgValue(OBJCOPY_prefix_alloc_sections);
737741
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 remove_symbol_prefix
207+
: Eq<"remove-symbol-prefix",
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)