[llvm] [llvm-objcopy] Add support of symbol modification flags for MachO (PR #120895)
Richard Dzenis via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 23 03:03:19 PST 2024
https://github.com/RIscRIpt updated https://github.com/llvm/llvm-project/pull/120895
>From 41cbb64ecddb9252bce8b4ba0b7ae69d72aadfea Mon Sep 17 00:00:00 2001
From: Richard Dzenis <richard at dzenis.dev>
Date: Tue, 17 Dec 2024 22:11:46 +0200
Subject: [PATCH 1/2] [llvm-objcopy] Add support of symbol modification flags
for MachO
This patch adds support of the following llvm-objcopy flags for MachO:
- --skip-symbol
- --localize-symbol
- --globalize-symbol
- --keep-global-symbol
Code in `updateAndRemoveSymbols` for MachO
is kept similar to its version for ELF.
---
llvm/lib/ObjCopy/ConfigManager.cpp | 6 +-
llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp | 33 +++-
llvm/lib/ObjCopy/MachO/MachOObject.cpp | 13 ++
llvm/lib/ObjCopy/MachO/MachOObject.h | 1 +
.../llvm-objcopy/MachO/globalize-symbol.test | 150 ++++++++++++++++
.../MachO/keep-global-symbol.test | 163 ++++++++++++++++++
.../llvm-objcopy/MachO/localize-symbol.test | 147 ++++++++++++++++
.../tools/llvm-objcopy/MachO/skip-symbol.test | 163 ++++++++++++++++++
8 files changed, 665 insertions(+), 11 deletions(-)
create mode 100644 llvm/test/tools/llvm-objcopy/MachO/globalize-symbol.test
create mode 100644 llvm/test/tools/llvm-objcopy/MachO/keep-global-symbol.test
create mode 100644 llvm/test/tools/llvm-objcopy/MachO/localize-symbol.test
create mode 100644 llvm/test/tools/llvm-objcopy/MachO/skip-symbol.test
diff --git a/llvm/lib/ObjCopy/ConfigManager.cpp b/llvm/lib/ObjCopy/ConfigManager.cpp
index 78fc0c451e1a3b..79bbb289d16233 100644
--- a/llvm/lib/ObjCopy/ConfigManager.cpp
+++ b/llvm/lib/ObjCopy/ConfigManager.cpp
@@ -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 ||
diff --git a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
index 91500c2d9dd47d..ecd9225230cd63 100644
--- a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
+++ b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
@@ -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) {
diff --git a/llvm/lib/ObjCopy/MachO/MachOObject.cpp b/llvm/lib/ObjCopy/MachO/MachOObject.cpp
index d593d6788e112f..ad26350ca04d96 100644
--- a/llvm/lib/ObjCopy/MachO/MachOObject.cpp
+++ b/llvm/lib/ObjCopy/MachO/MachOObject.cpp
@@ -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);
diff --git a/llvm/lib/ObjCopy/MachO/MachOObject.h b/llvm/lib/ObjCopy/MachO/MachOObject.h
index b3303fd291c82c..a454c4f502fd6f 100644
--- a/llvm/lib/ObjCopy/MachO/MachOObject.h
+++ b/llvm/lib/ObjCopy/MachO/MachOObject.h
@@ -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);
};
diff --git a/llvm/test/tools/llvm-objcopy/MachO/globalize-symbol.test b/llvm/test/tools/llvm-objcopy/MachO/globalize-symbol.test
new file mode 100644
index 00000000000000..ab81def8f545fd
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/MachO/globalize-symbol.test
@@ -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
+...
diff --git a/llvm/test/tools/llvm-objcopy/MachO/keep-global-symbol.test b/llvm/test/tools/llvm-objcopy/MachO/keep-global-symbol.test
new file mode 100644
index 00000000000000..22234f7c0cfff7
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/MachO/keep-global-symbol.test
@@ -0,0 +1,163 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy --keep-global-symbol _CommonSymbol %t %t.copy
+# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
+
+# CHECK: Symbols [
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: _PrivateSymbol
+# 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: 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: _CommonSymbol2
+# CHECK-NEXT: Type: Section (0xE)
+# CHECK-NEXT: Section: __text (0x1)
+# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
+# CHECK-NEXT: Flags [ (0x0)
+# CHECK-NEXT: ]
+# CHECK-NEXT: Value: 0x4
+# 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: 174
+ maxprot: 1
+ initprot: 1
+ nsects: 0
+ flags: 0
+ - cmd: LC_SYMTAB
+ cmdsize: 24
+ symoff: 4096
+ nsyms: 5
+ stroff: 4176
+ strsize: 94
+ - 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: 0x0F
+ n_sect: 1
+ n_desc: 0
+ n_value: 4
+ - n_strx: 69
+ n_type: 0x01
+ n_sect: 0
+ n_desc: 0
+ n_value: 0
+ StringTable:
+ - ' '
+ - _PrivateSymbol
+ - _PrivateExternalSymbol
+ - _CommonSymbol
+ - _CommonSymbol2
+ - _UndefinedExternalSymbol
+...
diff --git a/llvm/test/tools/llvm-objcopy/MachO/localize-symbol.test b/llvm/test/tools/llvm-objcopy/MachO/localize-symbol.test
new file mode 100644
index 00000000000000..92b26f0d29baf2
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/MachO/localize-symbol.test
@@ -0,0 +1,147 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy --wildcard --localize-symbol="*" %t %t.copy
+# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
+
+# CHECK: Symbols [
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: _PrivateSymbol
+# 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: 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: 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
+...
diff --git a/llvm/test/tools/llvm-objcopy/MachO/skip-symbol.test b/llvm/test/tools/llvm-objcopy/MachO/skip-symbol.test
new file mode 100644
index 00000000000000..73fe24c77aab8b
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/MachO/skip-symbol.test
@@ -0,0 +1,163 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy --wildcard --localize-symbol="*" --skip-symbol _CommonSymbol %t %t.copy
+# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
+
+# CHECK: Symbols [
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: _PrivateSymbol
+# 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: 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: _CommonSymbol2
+# CHECK-NEXT: Type: Section (0xE)
+# CHECK-NEXT: Section: __text (0x1)
+# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
+# CHECK-NEXT: Flags [ (0x0)
+# CHECK-NEXT: ]
+# CHECK-NEXT: Value: 0x4
+# 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: 174
+ maxprot: 1
+ initprot: 1
+ nsects: 0
+ flags: 0
+ - cmd: LC_SYMTAB
+ cmdsize: 24
+ symoff: 4096
+ nsyms: 5
+ stroff: 4176
+ strsize: 94
+ - 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: 0x0F
+ n_sect: 1
+ n_desc: 0
+ n_value: 4
+ - n_strx: 69
+ n_type: 0x01
+ n_sect: 0
+ n_desc: 0
+ n_value: 0
+ StringTable:
+ - ' '
+ - _PrivateSymbol
+ - _PrivateExternalSymbol
+ - _CommonSymbol
+ - _CommonSymbol2
+ - _UndefinedExternalSymbol
+...
>From 0139a83ab2b09ce3b3cb4b8e54b11acbb6f184e7 Mon Sep 17 00:00:00 2001
From: Richard Dzenis <richard at dzenis.dev>
Date: Mon, 23 Dec 2024 11:56:04 +0200
Subject: [PATCH 2/2] fixup! [llvm-objcopy] Add support of symbol modification
flags for MachO
---
llvm/docs/CommandGuide/llvm-objcopy.rst | 116 ++++++++++--------
llvm/docs/ReleaseNotes.md | 8 +-
llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp | 8 +-
llvm/lib/ObjCopy/MachO/MachOObject.cpp | 4 +-
.../llvm-objcopy/MachO/globalize-symbol.test | 6 +-
.../MachO/keep-global-symbol.test | 6 +-
.../llvm-objcopy/MachO/localize-symbol.test | 6 +-
.../tools/llvm-objcopy/MachO/skip-symbol.test | 7 +-
8 files changed, 100 insertions(+), 61 deletions(-)
diff --git a/llvm/docs/CommandGuide/llvm-objcopy.rst b/llvm/docs/CommandGuide/llvm-objcopy.rst
index e6af47ce9710a6..d808318d543589 100644
--- a/llvm/docs/CommandGuide/llvm-objcopy.rst
+++ b/llvm/docs/CommandGuide/llvm-objcopy.rst
@@ -78,10 +78,59 @@ multiple file formats.
Enable deterministic mode when copying archives, i.e. use 0 for archive member
header UIDs, GIDs and timestamp fields. On by default.
+.. option:: --globalize-symbol <symbol>
+
+ Mark any defined symbols named ``<symbol>`` as global symbols in the output.
+ Can be specified multiple times to mark multiple symbols.
+
+ This option is only supported for ELF and MachO.
+
+.. option:: --globalize-symbols <filename>
+
+ Read a list of names from the file ``<filename>`` and mark defined symbols with
+ those names as global in the output. In the file, each line represents a single
+ symbol, with leading and trailing whitespace ignored, as is anything following
+ a '#'. Can be specified multiple times to read names from multiple files.
+
+ This option is only supported for ELF and MachO.
+
.. option:: --help, -h
Print a summary of command line options.
+.. option:: --keep-global-symbol <symbol>, -G
+
+ Mark all symbols local in the output, except for symbols with the name
+ ``<symbol>``. Can be specified multiple times to ignore multiple symbols.
+
+ This option is only supported for ELF and MachO.
+
+.. option:: --keep-global-symbols <filename>
+
+ Mark all symbols local in the output, except for symbols named in the file
+ ``<filename>``. In the file, each line represents a single symbol, with leading
+ and trailing whitespace ignored, as is anything following a '#'. Can be
+ specified multiple times to read names from multiple files.
+
+ This option is only supported for ELF and MachO.
+
+.. option:: --localize-symbol <symbol>, -L
+
+ Mark any defined non-common symbol named ``<symbol>`` as a local symbol in the
+ output. Can be specified multiple times to mark multiple symbols as local.
+
+ This option is only supported for ELF and MachO.
+
+.. option:: --localize-symbols <filename>
+
+ Read a list of names from the file ``<filename>`` and mark defined non-common
+ symbols with those names as local in the output. In the file, each line
+ represents a single symbol, with leading and trailing whitespace ignored, as is
+ anything following a '#'. Can be specified multiple times to read names from
+ multiple files.
+
+ This option is only supported for ELF and MachO.
+
.. option:: --only-keep-debug
Produce a debug file as the output that only preserves contents of sections
@@ -177,6 +226,23 @@ multiple file formats.
flags.
- `share` = add the `IMAGE_SCN_MEM_SHARED` and `IMAGE_SCN_MEM_READ` flags.
+.. option:: --skip-symbol <symbol>
+
+ Do not change the parameters of symbol ``<symbol>`` when executing other
+ options that can change the symbol's name, binding or visibility.
+
+ This option is only supported for ELF and MachO.
+
+.. option:: --skip-symbols <filename>
+
+ Do not change the parameters of symbols named in the file ``<filename>`` when
+ executing other options that can change the symbol's name, binding or
+ visibility. In the file, each line represents a single symbol, with leading
+ and trailing whitespace ignored, as is anything following a '#'.
+ Can be specified multiple times to read names from multiple files.
+
+ This option is only supported for ELF and MachO.
+
.. option:: --strip-all-gnu
Remove all symbols, debug sections and relocations from the output. This option
@@ -355,18 +421,6 @@ them.
For binary outputs, fill the gaps between sections with ``<value>`` instead
of zero. The value must be an unsigned 8-bit integer.
-.. option:: --globalize-symbol <symbol>
-
- Mark any defined symbols named ``<symbol>`` as global symbols in the output.
- Can be specified multiple times to mark multiple symbols.
-
-.. option:: --globalize-symbols <filename>
-
- Read a list of names from the file ``<filename>`` and mark defined symbols with
- those names as global in the output. In the file, each line represents a single
- symbol, with leading and trailing whitespace ignored, as is anything following
- a '#'. Can be specified multiple times to read names from multiple files.
-
.. option:: --input-target <format>, -I
Read the input as the specified format. See `SUPPORTED FORMATS`_ for a list of
@@ -377,18 +431,6 @@ them.
Keep symbols of type `STT_FILE`, even if they would otherwise be stripped.
-.. option:: --keep-global-symbol <symbol>, -G
-
- Mark all symbols local in the output, except for symbols with the name
- ``<symbol>``. Can be specified multiple times to ignore multiple symbols.
-
-.. option:: --keep-global-symbols <filename>
-
- Mark all symbols local in the output, except for symbols named in the file
- ``<filename>``. In the file, each line represents a single symbol, with leading
- and trailing whitespace ignored, as is anything following a '#'. Can be
- specified multiple times to read names from multiple files.
-
.. option:: --keep-section <section>
When removing sections from the output, do not remove sections named
@@ -410,19 +452,6 @@ them.
Mark all symbols with hidden or internal visibility local in the output.
-.. option:: --localize-symbol <symbol>, -L
-
- Mark any defined non-common symbol named ``<symbol>`` as a local symbol in the
- output. Can be specified multiple times to mark multiple symbols as local.
-
-.. option:: --localize-symbols <filename>
-
- Read a list of names from the file ``<filename>`` and mark defined non-common
- symbols with those names as local in the output. In the file, each line
- represents a single symbol, with leading and trailing whitespace ignored, as is
- anything following a '#'. Can be specified multiple times to read names from
- multiple files.
-
.. option:: --new-symbol-visibility <visibility>
Specify the visibility of the symbols automatically created when using binary
@@ -489,19 +518,6 @@ them.
Read a list of symbols from <filename> and change their visibility to the
specified value. Visibility values: default, internal, hidden, protected.
-.. option:: --skip-symbol <symbol>
-
- Do not change the parameters of symbol ``<symbol>`` when executing other
- options that can change the symbol's name, binding or visibility.
-
-.. option:: --skip-symbols <filename>
-
- Do not change the parameters of symbols named in the file ``<filename>`` when
- executing other options that can change the symbol's name, binding or
- visibility. In the file, each line represents a single symbol, with leading
- and trailing whitespace ignored, as is anything following a '#'.
- Can be specified multiple times to read names from multiple files.
-
.. option:: --split-dwo <dwo-file>
Equivalent to running :program:`llvm-objcopy` with :option:`--extract-dwo` and
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index 2835ace34bff83..5f50f76bd5f018 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -350,10 +350,16 @@ Changes to the Debug Info
Changes to the LLVM tools
---------------------------------
+* llvm-objcopy now supports the following options for MachO:
+ `--globalize-symbol`, `--globalize-symbols`,
+ `--keep-global-symbol`, `-G`, `--keep-global-symbols`,
+ `--localize-symbol`, `-L`, `--localize-symbols`,
+ `--skip-symbol`, `--skip-symbols`.
+
Changes to LLDB
---------------------------------
-* LLDB now now supports inline diagnostics for the expression evaluator and command line parser.
+* LLDB now supports inline diagnostics for the expression evaluator and command line parser.
Old:
```
diff --git a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
index ecd9225230cd63..a188425b283fa6 100644
--- a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
+++ b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
@@ -98,7 +98,7 @@ static void updateAndRemoveSymbols(const CommonConfig &Config,
return;
if (!Sym.isUndefinedSymbol() && Config.SymbolsToLocalize.matches(Sym.Name))
- Sym.n_type &= ~llvm::MachO::N_EXT;
+ Sym.n_type &= ~MachO::N_EXT;
// Note: these two globalize flags have very similar names but different
// meanings:
@@ -112,14 +112,14 @@ static void updateAndRemoveSymbols(const CommonConfig &Config,
// --globalize-symbol second.
if (!Sym.isUndefinedSymbol() && !Config.SymbolsToKeepGlobal.empty() &&
!Config.SymbolsToKeepGlobal.matches(Sym.Name))
- Sym.n_type &= ~llvm::MachO::N_EXT;
+ Sym.n_type &= ~MachO::N_EXT;
if (!Sym.isUndefinedSymbol() && Config.SymbolsToGlobalize.matches(Sym.Name))
- Sym.n_type |= llvm::MachO::N_EXT;
+ Sym.n_type |= MachO::N_EXT;
if (Sym.isExternalSymbol() && !Sym.isUndefinedSymbol() &&
(Config.Weaken || Config.SymbolsToWeaken.matches(Sym.Name)))
- Sym.n_desc |= llvm::MachO::N_WEAK_DEF;
+ Sym.n_desc |= MachO::N_WEAK_DEF;
auto I = Config.SymbolsToRename.find(Sym.Name);
if (I != Config.SymbolsToRename.end())
diff --git a/llvm/lib/ObjCopy/MachO/MachOObject.cpp b/llvm/lib/ObjCopy/MachO/MachOObject.cpp
index ad26350ca04d96..8d2c02dc37c99e 100644
--- a/llvm/lib/ObjCopy/MachO/MachOObject.cpp
+++ b/llvm/lib/ObjCopy/MachO/MachOObject.cpp
@@ -38,10 +38,10 @@ void SymbolTable::updateSymbols(function_ref<void(SymbolEntry &)> Callable) {
Callable(*Sym);
// Partition symbols: local < defined external < undefined external.
- auto it_ext = std::stable_partition(
+ auto ExternalBegin = 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) {
+ std::stable_partition(ExternalBegin, std::end(Symbols), [](const auto &Sym) {
return !Sym->isUndefinedSymbol();
});
}
diff --git a/llvm/test/tools/llvm-objcopy/MachO/globalize-symbol.test b/llvm/test/tools/llvm-objcopy/MachO/globalize-symbol.test
index ab81def8f545fd..af0bd1156078e2 100644
--- a/llvm/test/tools/llvm-objcopy/MachO/globalize-symbol.test
+++ b/llvm/test/tools/llvm-objcopy/MachO/globalize-symbol.test
@@ -2,7 +2,11 @@
# RUN: llvm-objcopy --wildcard --globalize-symbol="*" %t %t.copy
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
-# CHECK: Symbols [
+# RUN: echo "*" > %t-star.txt
+# RUN: llvm-objcopy --wildcard --globalize-symbols="%t-star.txt" %t %t.copy
+# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
+
+# CHECK: Symbols [
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _PrivateSymbol
# CHECK-NEXT: Extern
diff --git a/llvm/test/tools/llvm-objcopy/MachO/keep-global-symbol.test b/llvm/test/tools/llvm-objcopy/MachO/keep-global-symbol.test
index 22234f7c0cfff7..6ac67866a1b5af 100644
--- a/llvm/test/tools/llvm-objcopy/MachO/keep-global-symbol.test
+++ b/llvm/test/tools/llvm-objcopy/MachO/keep-global-symbol.test
@@ -2,7 +2,11 @@
# RUN: llvm-objcopy --keep-global-symbol _CommonSymbol %t %t.copy
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
-# CHECK: Symbols [
+# RUN: echo _CommonSymbol > %t-sym-list.txt
+# RUN: llvm-objcopy --wildcard --keep-global-symbols="%t-sym-list.txt" %t %t.copy
+# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
+
+# CHECK: Symbols [
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _PrivateSymbol
# CHECK-NEXT: Type: Section (0xE)
diff --git a/llvm/test/tools/llvm-objcopy/MachO/localize-symbol.test b/llvm/test/tools/llvm-objcopy/MachO/localize-symbol.test
index 92b26f0d29baf2..1b87584b2c490a 100644
--- a/llvm/test/tools/llvm-objcopy/MachO/localize-symbol.test
+++ b/llvm/test/tools/llvm-objcopy/MachO/localize-symbol.test
@@ -2,7 +2,11 @@
# RUN: llvm-objcopy --wildcard --localize-symbol="*" %t %t.copy
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
-# CHECK: Symbols [
+# RUN: echo "*" > %t-star.txt
+# RUN: llvm-objcopy --wildcard --localize-symbols="%t-star.txt" %t %t.copy
+# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
+
+# CHECK: Symbols [
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _PrivateSymbol
# CHECK-NEXT: Type: Section (0xE)
diff --git a/llvm/test/tools/llvm-objcopy/MachO/skip-symbol.test b/llvm/test/tools/llvm-objcopy/MachO/skip-symbol.test
index 73fe24c77aab8b..917c2fa487c059 100644
--- a/llvm/test/tools/llvm-objcopy/MachO/skip-symbol.test
+++ b/llvm/test/tools/llvm-objcopy/MachO/skip-symbol.test
@@ -2,7 +2,12 @@
# RUN: llvm-objcopy --wildcard --localize-symbol="*" --skip-symbol _CommonSymbol %t %t.copy
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
-# CHECK: Symbols [
+# RUN: echo "*" > %t-star.txt
+# RUN: echo _CommonSymbol > %t-sym-list.txt
+# RUN: llvm-objcopy --wildcard --localize-symbols="%t-star.txt" --skip-symbols="%t-sym-list.txt" %t %t.copy
+# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
+
+# CHECK: Symbols [
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _PrivateSymbol
# CHECK-NEXT: Type: Section (0xE)
More information about the llvm-commits
mailing list