[llvm] [llvm-objcopy][test] Use llvm-readelf instead for clearer visualization (PR #79874)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 29 09:54:41 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-binary-utilities
Author: Yi Kong (kongy)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/79874.diff
7 Files Affected:
- (modified) llvm/include/llvm/ObjCopy/CommonConfig.h (+1)
- (modified) llvm/lib/ObjCopy/ConfigManager.cpp (+4)
- (modified) llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp (+5)
- (added) llvm/test/tools/llvm-objcopy/ELF/prefix-symbols-remove.test (+115)
- (modified) llvm/test/tools/llvm-objcopy/ELF/prefix-symbols.test (+10-50)
- (modified) llvm/tools/llvm-objcopy/ObjcopyOptions.cpp (+4)
- (modified) llvm/tools/llvm-objcopy/ObjcopyOpts.td (+5)
``````````diff
diff --git a/llvm/include/llvm/ObjCopy/CommonConfig.h b/llvm/include/llvm/ObjCopy/CommonConfig.h
index 386c20aec184ded..0d9320ec2efd71b 100644
--- a/llvm/include/llvm/ObjCopy/CommonConfig.h
+++ b/llvm/include/llvm/ObjCopy/CommonConfig.h
@@ -218,6 +218,7 @@ struct CommonConfig {
uint64_t PadTo = 0;
StringRef SplitDWO;
StringRef SymbolsPrefix;
+ StringRef SymbolsPrefixRemove;
StringRef AllocSectionsPrefix;
DiscardType DiscardMode = DiscardType::None;
diff --git a/llvm/lib/ObjCopy/ConfigManager.cpp b/llvm/lib/ObjCopy/ConfigManager.cpp
index 10ece49028f2178..e46b595a56dc44c 100644
--- a/llvm/lib/ObjCopy/ConfigManager.cpp
+++ b/llvm/lib/ObjCopy/ConfigManager.cpp
@@ -15,6 +15,7 @@ namespace objcopy {
Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {
if (!Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
+ !Common.SymbolsPrefixRemove.empty() ||
!Common.AllocSectionsPrefix.empty() || !Common.KeepSection.empty() ||
!Common.SymbolsToGlobalize.empty() || !Common.SymbolsToKeep.empty() ||
!Common.SymbolsToLocalize.empty() || !Common.SymbolsToWeaken.empty() ||
@@ -33,6 +34,7 @@ Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {
Expected<const MachOConfig &> ConfigManager::getMachOConfig() const {
if (!Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
+ !Common.SymbolsPrefixRemove.empty() ||
!Common.AllocSectionsPrefix.empty() || !Common.KeepSection.empty() ||
!Common.SymbolsToGlobalize.empty() || !Common.SymbolsToKeep.empty() ||
!Common.SymbolsToLocalize.empty() ||
@@ -54,6 +56,7 @@ Expected<const MachOConfig &> ConfigManager::getMachOConfig() const {
Expected<const WasmConfig &> ConfigManager::getWasmConfig() const {
if (!Common.AddGnuDebugLink.empty() || Common.ExtractPartition ||
!Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
+ !Common.SymbolsPrefixRemove.empty() ||
!Common.AllocSectionsPrefix.empty() ||
Common.DiscardMode != DiscardType::None || !Common.SymbolsToAdd.empty() ||
!Common.SymbolsToGlobalize.empty() || !Common.SymbolsToLocalize.empty() ||
@@ -74,6 +77,7 @@ Expected<const WasmConfig &> ConfigManager::getWasmConfig() const {
Expected<const XCOFFConfig &> ConfigManager::getXCOFFConfig() const {
if (!Common.AddGnuDebugLink.empty() || Common.ExtractPartition ||
!Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
+ !Common.SymbolsPrefixRemove.empty() ||
!Common.AllocSectionsPrefix.empty() ||
Common.DiscardMode != DiscardType::None || !Common.AddSection.empty() ||
!Common.DumpSection.empty() || !Common.SymbolsToAdd.empty() ||
diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
index b6d77d17bae36c5..36f799446a04fda 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -329,6 +329,11 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
if (I != Config.SymbolsToRename.end())
Sym.Name = std::string(I->getValue());
+ if (!Config.SymbolsPrefixRemove.empty() && Sym.Type != STT_SECTION)
+ if (Sym.Name.compare(0, Config.SymbolsPrefixRemove.size(),
+ Config.SymbolsPrefixRemove) == 0)
+ Sym.Name = Sym.Name.substr(Config.SymbolsPrefixRemove.size());
+
if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
});
diff --git a/llvm/test/tools/llvm-objcopy/ELF/prefix-symbols-remove.test b/llvm/test/tools/llvm-objcopy/ELF/prefix-symbols-remove.test
new file mode 100644
index 000000000000000..402404c8d6e6818
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/prefix-symbols-remove.test
@@ -0,0 +1,115 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy --remove-prefix-symbols __pf_ %t %t2
+# RUN: llvm-readobj --symbols %t2 | FileCheck %s
+
+## Show that an empty string is permitted as the argument to
+## --remove-prefix-symbols.
+# RUN: llvm-objcopy --remove-prefix-symbols= %t2 %t3
+# RUN: cmp %t2 %t3
+
+## When both options are present, llvm-objcopy should remove
+## prefixes first, before adding prefixes.
+# RUN: llvm-objcopy --prefix-symbols=AAA --remove-prefix-symbols=AAA %t %t4
+# RUN: llvm-objcopy --prefix-symbols=AAA %t %t5
+# RUN: cmp %t4 %t5
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x1000
+ AddressAlign: 0x0000000000000010
+ Size: 64
+Symbols:
+ - Name: __pf_foo
+ Type: STT_SECTION
+ Section: .text
+ - Name: __pf_bar
+ Type: STT_FILE
+ Section: .text
+ - Name: foobar
+ Type: STT_FUNC
+ Section: .text
+ Binding: STB_GLOBAL
+ - Name: foo__pf_bar1
+ Type: STT_FUNC
+ Section: .text
+ Binding: STB_GLOBAL
+ - Name: __pf_foo__pf_bar2
+ Type: STT_FUNC
+ Section: .text
+ Binding: STB_GLOBAL
+ - Name: undef
+ Binding: STB_GLOBAL
+
+# CHECK: Symbols [
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name:
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Local
+# CHECK-NEXT: Type: None
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: Undefined
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: __pf_foo
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Local
+# CHECK-NEXT: Type: Section
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: .text
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: bar
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Local
+# CHECK-NEXT: Type: File
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: .text
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: foobar
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: Function
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: .text
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: foo__pf_bar1
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: Function
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: .text
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: foo__pf_bar2
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: Function
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: .text
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: undef
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: None
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: Undefined
+# CHECK-NEXT: }
+# CHECK-NEXT:]
diff --git a/llvm/test/tools/llvm-objcopy/ELF/prefix-symbols.test b/llvm/test/tools/llvm-objcopy/ELF/prefix-symbols.test
index 8c6503d4c8e09d7..ac8f4bc1a684f73 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/prefix-symbols.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/prefix-symbols.test
@@ -1,8 +1,8 @@
# RUN: yaml2obj %s -o %t
# RUN: llvm-objcopy --prefix-symbols prefix %t %t2
-# RUN: llvm-readobj --symbols %t2 | FileCheck %s --check-prefixes=COMMON,BASIC
+# RUN: llvm-readelf --symbols %t2 | FileCheck %s --check-prefixes=COMMON,BASIC
# RUN: llvm-objcopy --redefine-sym bar=baz --prefix-symbols prefix %t %t3
-# RUN: llvm-readobj --symbols %t3 | FileCheck %s --check-prefixes=COMMON,REDEF
+# RUN: llvm-readelf --symbols %t3 | FileCheck %s --check-prefixes=COMMON,REDEF
## Show that an empty string is permitted as the argument to
## --prefix-symbols.
@@ -40,51 +40,11 @@ Symbols:
- Name: undef
Binding: STB_GLOBAL
-# COMMON: Symbols [
-# COMMON-NEXT: Symbol {
-# COMMON-NEXT: Name:
-# COMMON-NEXT: Value: 0x0
-# COMMON-NEXT: Size: 0
-# COMMON-NEXT: Binding: Local
-# COMMON-NEXT: Type: None
-# COMMON-NEXT: Other: 0
-# COMMON-NEXT: Section: Undefined
-# COMMON-NEXT: }
-# COMMON-NEXT: Symbol {
-# COMMON-NEXT: Name: foo
-# COMMON-NEXT: Value: 0x0
-# COMMON-NEXT: Size: 0
-# COMMON-NEXT: Binding: Local
-# COMMON-NEXT: Type: Section
-# COMMON-NEXT: Other: 0
-# COMMON-NEXT: Section: .text
-# COMMON-NEXT: }
-# COMMON-NEXT: Symbol {
-# BASIC-NEXT: Name: prefixbar
-# REDEF-NEXT: Name: prefixbaz
-# COMMON-NEXT: Value: 0x0
-# COMMON-NEXT: Size: 0
-# COMMON-NEXT: Binding: Local
-# COMMON-NEXT: Type: File
-# COMMON-NEXT: Other: 0
-# COMMON-NEXT: Section: .text
-# COMMON-NEXT: }
-# COMMON-NEXT: Symbol {
-# COMMON-NEXT: Name: prefixfoobar
-# COMMON-NEXT: Value: 0x0
-# COMMON-NEXT: Size: 0
-# COMMON-NEXT: Binding: Global
-# COMMON-NEXT: Type: Function
-# COMMON-NEXT: Other: 0
-# COMMON-NEXT: Section: .text
-# COMMON-NEXT: }
-# COMMON-NEXT: Symbol {
-# COMMON-NEXT: Name: prefixundef
-# COMMON-NEXT: Value: 0x0
-# COMMON-NEXT: Size: 0
-# COMMON-NEXT: Binding: Global
-# COMMON-NEXT: Type: None
-# COMMON-NEXT: Other: 0
-# COMMON-NEXT: Section: Undefined
-# COMMON-NEXT: }
-# COMMON-NEXT:]
+#COMMON: Symbol table '.symtab' contains 5 entries:
+#COMMON-NEXT: Num: Value Size Type Bind Vis Ndx Name
+#COMMON-NEXT: 0: {{.*}} 0 NOTYPE LOCAL {{.*}}
+#COMMON-NEXT: 1: {{.*}} 0 SECTION LOCAL {{.*}} foo
+#BASIC-NEXT: 2: {{.*}} 0 FILE LOCAL {{.*}} prefixbar
+#REDEF-NEXT: 2: {{.*}} 0 FILE LOCAL {{.*}} prefixbaz
+#COMMON-NEXT: 3: {{.*}} 0 FUNC GLOBAL {{.*}} prefixfoobar
+#COMMON-NEXT: 4: {{.*}} 0 NOTYPE GLOBAL {{.*}} prefixundef
\ No newline at end of file
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index f15307181fad618..6fcb5e6cdd5da1e 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -731,7 +731,11 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
llvm::crc32(arrayRefFromStringRef(Debug->getBuffer()));
}
Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
+
Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols);
+ Config.SymbolsPrefixRemove =
+ InputArgs.getLastArgValue(OBJCOPY_remove_prefix_symbols);
+
Config.AllocSectionsPrefix =
InputArgs.getLastArgValue(OBJCOPY_prefix_alloc_sections);
if (auto Arg = InputArgs.getLastArg(OBJCOPY_extract_partition))
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOpts.td b/llvm/tools/llvm-objcopy/ObjcopyOpts.td
index ead8cd28d387791..5fe583cf2b40253 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOpts.td
+++ b/llvm/tools/llvm-objcopy/ObjcopyOpts.td
@@ -203,6 +203,11 @@ defm dump_section
defm prefix_symbols
: Eq<"prefix-symbols", "Add <prefix> to the start of every symbol name">,
MetaVarName<"prefix">;
+defm remove_prefix_symbols
+ : Eq<"remove-prefix-symbols",
+ "Remove <prefix> from the start of every symbol name. No-op for symbols that do not start "
+ "with <prefix>">,
+ MetaVarName<"prefix">;
defm prefix_alloc_sections
: Eq<"prefix-alloc-sections", "Add <prefix> to the start of every allocated section name">,
``````````
</details>
https://github.com/llvm/llvm-project/pull/79874
More information about the llvm-commits
mailing list