[llvm] 28a5dc7 - [llvm-objcopy][MachO] Implement --redefine-sym and --redefine-syms
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 15 10:06:20 PST 2019
Author: Fangrui Song
Date: 2019-11-15T10:05:36-08:00
New Revision: 28a5dc7fc587a2737cc6cc0bfa13129925cda133
URL: https://github.com/llvm/llvm-project/commit/28a5dc7fc587a2737cc6cc0bfa13129925cda133
DIFF: https://github.com/llvm/llvm-project/commit/28a5dc7fc587a2737cc6cc0bfa13129925cda133.diff
LOG: [llvm-objcopy][MachO] Implement --redefine-sym and --redefine-syms
Similar to D46029 (ELF) and D70036 (COFF), but for MachO.
Note, when --strip-symbol (not implemented for MachO) is also specified,
--redefine-sym executes before --strip-symbol.
Reviewed By: jhenderson, seiya
Differential Revision: https://reviews.llvm.org/D70212
Added:
llvm/test/tools/llvm-objcopy/MachO/redefine-symbol.s
Modified:
llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
llvm/tools/llvm-objcopy/MachO/Object.h
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-objcopy/MachO/redefine-symbol.s b/llvm/test/tools/llvm-objcopy/MachO/redefine-symbol.s
new file mode 100644
index 000000000000..82d20ca668d6
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/MachO/redefine-symbol.s
@@ -0,0 +1,51 @@
+# REQUIRES: x86-registered-target
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %s -o %t
+
+## A non-existent symbol does not error.
+# RUN: llvm-objcopy --redefine-sym _func=_cnuf1234 --redefine-sym _foo=_ba --redefine-sym=_notexist= %t %t2 2>&1 | count 0
+# RUN: llvm-readobj --symbols %t2 | FileCheck %s
+
+# RUN: echo '_func _cnuf1234 #rename func' > %t.rename.txt
+# RUN: echo ' _foo _ba ' >> %t.rename.txt
+# RUN: echo '_notexist _notexist' >> %t.rename.txt
+# RUN: llvm-objcopy --redefine-syms %t.rename.txt %t %t3 2>&1 | count 0
+# RUN: cmp %t2 %t3
+
+# CHECK: Symbol {
+# CHECK-NEXT: Name: _ba (1)
+# CHECK-NEXT: Extern
+# CHECK-NEXT: Type: Section (0xE)
+# CHECK-NEXT: Section: __const (0x2)
+# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
+# CHECK-NEXT: Flags [ (0x0)
+# CHECK-NEXT: ]
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: _cnuf1234 (5)
+# CHECK-NEXT: Extern
+# CHECK-NEXT: Type: Section (0xE)
+# CHECK-NEXT: Section: __text (0x1)
+# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
+# CHECK-NEXT: Flags [ (0x0)
+# CHECK-NEXT: ]
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: }
+
+## Check that --redefine-sym executes before --strip-symbol.
+# RUN: llvm-objcopy --strip-symbol _foo --redefine-sym _foo=_ba %t %t.notstripped
+# RUN: llvm-readobj --symbols %t.notstripped | FileCheck %s --check-prefix=NOTSTRIPPED
+# NOTSTRIPPED: Name: _ba
+# NOTSTRIPPED: Name: _func
+
+## FIXME: _ba should be stripped after --strip-symbol is implemented.
+# RUN: llvm-objcopy --strip-symbol _ba --redefine-sym _foo=_ba %t %t.noba
+# RUN: llvm-readobj --symbols %t.noba | FileCheck %s --check-prefix=NOTSTRIPPED
+
+.globl _func
+_func:
+
+.section __TEXT,__const
+.globl _foo
+_foo:
diff --git a/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp b/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
index cad96665847e..dbdd44cc9412 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
@@ -56,7 +56,13 @@ static void markSymbols(const CopyConfig &Config, Object &Obj) {
(*ISE.Symbol)->Referenced = true;
}
-static void removeSymbols(const CopyConfig &Config, Object &Obj) {
+static void updateAndRemoveSymbols(const CopyConfig &Config, Object &Obj) {
+ for (SymbolEntry &Sym : Obj.SymTable) {
+ auto I = Config.SymbolsToRename.find(Sym.Name);
+ if (I != Config.SymbolsToRename.end())
+ Sym.Name = I->getValue();
+ }
+
auto RemovePred = [Config](const std::unique_ptr<SymbolEntry> &N) {
if (N->Referenced)
return false;
@@ -75,7 +81,7 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
Config.NewSymbolVisibility || !Config.SymbolsToGlobalize.empty() ||
!Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
!Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
- !Config.SectionsToRename.empty() || !Config.SymbolsToRename.empty() ||
+ !Config.SectionsToRename.empty() ||
!Config.UnneededSymbolsToRemove.empty() ||
!Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
@@ -95,7 +101,7 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
if (Config.StripAll)
markSymbols(Config, Obj);
- removeSymbols(Config, Obj);
+ updateAndRemoveSymbols(Config, Obj);
if (Config.StripAll)
for (LoadCommand &LC : Obj.LoadCommands)
diff --git a/llvm/tools/llvm-objcopy/MachO/Object.h b/llvm/tools/llvm-objcopy/MachO/Object.h
index 8642ce3c0d94..fb5763730746 100644
--- a/llvm/tools/llvm-objcopy/MachO/Object.h
+++ b/llvm/tools/llvm-objcopy/MachO/Object.h
@@ -110,6 +110,12 @@ struct SymbolEntry {
struct SymbolTable {
std::vector<std::unique_ptr<SymbolEntry>> Symbols;
+ using iterator = pointee_iterator<
+ std::vector<std::unique_ptr<SymbolEntry>>::const_iterator>;
+
+ iterator begin() const { return iterator(Symbols.begin()); }
+ iterator end() const { return iterator(Symbols.end()); }
+
const SymbolEntry *getSymbolByIndex(uint32_t Index) const;
SymbolEntry *getSymbolByIndex(uint32_t Index);
void removeSymbols(
More information about the llvm-commits
mailing list