Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions llvm/lib/ObjCopy/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {

Expected<const MachOConfig &> ConfigManager::getMachOConfig() const {
if (!Common.SplitDWO.empty() || !Common.SymbolsPrefix.empty() ||
!Common.SymbolsPrefixRemove.empty() || !Common.SymbolsToSkip.empty() ||
!Common.SymbolsPrefixRemove.empty() ||
!Common.AllocSectionsPrefix.empty() || !Common.KeepSection.empty() ||
!Common.SymbolsToGlobalize.empty() || !Common.SymbolsToKeep.empty() ||
!Common.SymbolsToLocalize.empty() ||
!Common.SymbolsToKeepGlobal.empty() || !Common.SectionsToRename.empty() ||
!Common.SymbolsToKeep.empty() || !Common.SectionsToRename.empty() ||
!Common.UnneededSymbolsToRemove.empty() ||
!Common.SetSectionAlignment.empty() || !Common.SetSectionFlags.empty() ||
!Common.SetSectionType.empty() || Common.ExtractDWO ||
Expand Down
33 changes: 26 additions & 7 deletions llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,38 @@ static void markSymbols(const CommonConfig &, Object &Obj) {
static void updateAndRemoveSymbols(const CommonConfig &Config,
const MachOConfig &MachOConfig,
Object &Obj) {
for (SymbolEntry &Sym : Obj.SymTable) {
// Weaken symbols first to match ELFObjcopy behavior.
bool IsExportedAndDefined =
(Sym.n_type & llvm::MachO::N_EXT) &&
(Sym.n_type & llvm::MachO::N_TYPE) != llvm::MachO::N_UNDF;
if (IsExportedAndDefined &&
Obj.SymTable.updateSymbols([&](SymbolEntry &Sym) {
if (Config.SymbolsToSkip.matches(Sym.Name))
return;

if (!Sym.isUndefinedSymbol() && Config.SymbolsToLocalize.matches(Sym.Name))
Sym.n_type &= ~llvm::MachO::N_EXT;

// Note: these two globalize flags have very similar names but different
// meanings:
//
// --globalize-symbol: promote a symbol to global
// --keep-global-symbol: all symbols except for these should be made local
//
// If --globalize-symbol is specified for a given symbol, it will be
// global in the output file even if it is not included via
// --keep-global-symbol. Because of that, make sure to check
// --globalize-symbol second.
if (!Sym.isUndefinedSymbol() && !Config.SymbolsToKeepGlobal.empty() &&
!Config.SymbolsToKeepGlobal.matches(Sym.Name))
Sym.n_type &= ~llvm::MachO::N_EXT;

if (!Sym.isUndefinedSymbol() && Config.SymbolsToGlobalize.matches(Sym.Name))
Sym.n_type |= llvm::MachO::N_EXT;

if (Sym.isExternalSymbol() && !Sym.isUndefinedSymbol() &&
(Config.Weaken || Config.SymbolsToWeaken.matches(Sym.Name)))
Sym.n_desc |= llvm::MachO::N_WEAK_DEF;

auto I = Config.SymbolsToRename.find(Sym.Name);
if (I != Config.SymbolsToRename.end())
Sym.Name = std::string(I->getValue());
}
});

auto RemovePred = [&Config, &MachOConfig,
&Obj](const std::unique_ptr<SymbolEntry> &N) {
Expand Down
13 changes: 13 additions & 0 deletions llvm/lib/ObjCopy/MachO/MachOObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ SymbolEntry *SymbolTable::getSymbolByIndex(uint32_t Index) {
static_cast<const SymbolTable *>(this)->getSymbolByIndex(Index));
}

void SymbolTable::updateSymbols(function_ref<void(SymbolEntry &)> Callable) {
for (auto &Sym : Symbols)
Callable(*Sym);

// Partition symbols: local < defined external < undefined external.
auto it_ext = std::stable_partition(
std::begin(Symbols), std::end(Symbols),
[](const auto &Sym) { return Sym->isLocalSymbol(); });
std::stable_partition(it_ext, std::end(Symbols), [](const auto &Sym) {
return !Sym->isUndefinedSymbol();
});
}

void SymbolTable::removeSymbols(
function_ref<bool(const std::unique_ptr<SymbolEntry> &)> ToRemove) {
llvm::erase_if(Symbols, ToRemove);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/ObjCopy/MachO/MachOObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ struct SymbolTable {

const SymbolEntry *getSymbolByIndex(uint32_t Index) const;
SymbolEntry *getSymbolByIndex(uint32_t Index);
void updateSymbols(function_ref<void(SymbolEntry &)> Callable);
void removeSymbols(
function_ref<bool(const std::unique_ptr<SymbolEntry> &)> ToRemove);
};
Expand Down
150 changes: 150 additions & 0 deletions llvm/test/tools/llvm-objcopy/MachO/globalize-symbol.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# RUN: yaml2obj %s -o %t
# RUN: llvm-objcopy --wildcard --globalize-symbol="*" %t %t.copy
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s

# CHECK: Symbols [
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _PrivateSymbol
# 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: 0x1
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _PrivateExternalSymbol
# CHECK-NEXT: PrivateExtern
# 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: 0x2
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _CommonSymbol
# 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: 0x3
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _UndefinedExternalSymbol
# CHECK-NEXT: Extern
# CHECK-NEXT: Type: Undef (0x0)
# CHECK-NEXT: Section: (0x0)
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
# CHECK-NEXT: Flags [ (0x0)
# CHECK-NEXT: ]
# CHECK-NEXT: Value: 0x0
# CHECK-NEXT: }
# CHECK-NEXT: ]

--- !mach-o
FileHeader:
magic: 0xFEEDFACF
cputype: 0x100000C
cpusubtype: 0x0
filetype: 0x2
ncmds: 4
sizeofcmds: 328
flags: 0x200085
reserved: 0x0
LoadCommands:
- cmd: LC_SEGMENT_64
cmdsize: 152
segname: __TEXT
vmaddr: 4294967296
vmsize: 4096
fileoff: 0
filesize: 4096
maxprot: 5
initprot: 5
nsects: 1
flags: 0
Sections:
- sectname: __text
segname: __TEXT
addr: 0x100000FF8
size: 8
offset: 0xFF8
align: 2
reloff: 0x0
nreloc: 0
flags: 0x80000400
reserved1: 0x0
reserved2: 0x0
reserved3: 0x0
content: 00008052C0035FD6
- cmd: LC_SEGMENT_64
cmdsize: 72
segname: __LINKEDIT
vmaddr: 4294971392
vmsize: 4096
fileoff: 4096
filesize: 147
maxprot: 1
initprot: 1
nsects: 0
flags: 0
- cmd: LC_SYMTAB
cmdsize: 24
symoff: 4096
nsyms: 4
stroff: 4164
strsize: 79
- cmd: LC_DYSYMTAB
cmdsize: 80
ilocalsym: 0
nlocalsym: 1
iextdefsym: 1
nextdefsym: 1
iundefsym: 1
nundefsym: 1
tocoff: 0
ntoc: 0
modtaboff: 0
nmodtab: 0
extrefsymoff: 0
nextrefsyms: 0
indirectsymoff: 0
nindirectsyms: 0
extreloff: 0
nextrel: 0
locreloff: 0
nlocrel: 0
LinkEditData:
NameList:
- n_strx: 2
n_type: 0x0E
n_sect: 1
n_desc: 0
n_value: 1
- n_strx: 17
n_type: 0x1E
n_sect: 1
n_desc: 0
n_value: 2
- n_strx: 40
n_type: 0x0F
n_sect: 1
n_desc: 0
n_value: 3
- n_strx: 54
n_type: 0x01
n_sect: 0
n_desc: 0
n_value: 0
StringTable:
- ' '
- _PrivateSymbol
- _PrivateExternalSymbol
- _CommonSymbol
- _UndefinedExternalSymbol
...
Loading
Loading