[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
This commit is contained in:
Fangrui Song 2019-11-13 13:10:15 -08:00
parent 8bcd01f48a
commit 28a5dc7fc5
3 changed files with 66 additions and 3 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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(