Skip to content

Commit 0139a83

Browse files
committed
fixup! [llvm-objcopy] Add support of symbol modification flags for MachO
1 parent 41cbb64 commit 0139a83

File tree

8 files changed

+100
-61
lines changed

8 files changed

+100
-61
lines changed

llvm/docs/CommandGuide/llvm-objcopy.rst

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,59 @@ multiple file formats.
7878
Enable deterministic mode when copying archives, i.e. use 0 for archive member
7979
header UIDs, GIDs and timestamp fields. On by default.
8080

81+
.. option:: --globalize-symbol <symbol>
82+
83+
Mark any defined symbols named ``<symbol>`` as global symbols in the output.
84+
Can be specified multiple times to mark multiple symbols.
85+
86+
This option is only supported for ELF and MachO.
87+
88+
.. option:: --globalize-symbols <filename>
89+
90+
Read a list of names from the file ``<filename>`` and mark defined symbols with
91+
those names as global in the output. In the file, each line represents a single
92+
symbol, with leading and trailing whitespace ignored, as is anything following
93+
a '#'. Can be specified multiple times to read names from multiple files.
94+
95+
This option is only supported for ELF and MachO.
96+
8197
.. option:: --help, -h
8298

8399
Print a summary of command line options.
84100

101+
.. option:: --keep-global-symbol <symbol>, -G
102+
103+
Mark all symbols local in the output, except for symbols with the name
104+
``<symbol>``. Can be specified multiple times to ignore multiple symbols.
105+
106+
This option is only supported for ELF and MachO.
107+
108+
.. option:: --keep-global-symbols <filename>
109+
110+
Mark all symbols local in the output, except for symbols named in the file
111+
``<filename>``. In the file, each line represents a single symbol, with leading
112+
and trailing whitespace ignored, as is anything following a '#'. Can be
113+
specified multiple times to read names from multiple files.
114+
115+
This option is only supported for ELF and MachO.
116+
117+
.. option:: --localize-symbol <symbol>, -L
118+
119+
Mark any defined non-common symbol named ``<symbol>`` as a local symbol in the
120+
output. Can be specified multiple times to mark multiple symbols as local.
121+
122+
This option is only supported for ELF and MachO.
123+
124+
.. option:: --localize-symbols <filename>
125+
126+
Read a list of names from the file ``<filename>`` and mark defined non-common
127+
symbols with those names as local in the output. In the file, each line
128+
represents a single symbol, with leading and trailing whitespace ignored, as is
129+
anything following a '#'. Can be specified multiple times to read names from
130+
multiple files.
131+
132+
This option is only supported for ELF and MachO.
133+
85134
.. option:: --only-keep-debug
86135

87136
Produce a debug file as the output that only preserves contents of sections
@@ -177,6 +226,23 @@ multiple file formats.
177226
flags.
178227
- `share` = add the `IMAGE_SCN_MEM_SHARED` and `IMAGE_SCN_MEM_READ` flags.
179228

229+
.. option:: --skip-symbol <symbol>
230+
231+
Do not change the parameters of symbol ``<symbol>`` when executing other
232+
options that can change the symbol's name, binding or visibility.
233+
234+
This option is only supported for ELF and MachO.
235+
236+
.. option:: --skip-symbols <filename>
237+
238+
Do not change the parameters of symbols named in the file ``<filename>`` when
239+
executing other options that can change the symbol's name, binding or
240+
visibility. In the file, each line represents a single symbol, with leading
241+
and trailing whitespace ignored, as is anything following a '#'.
242+
Can be specified multiple times to read names from multiple files.
243+
244+
This option is only supported for ELF and MachO.
245+
180246
.. option:: --strip-all-gnu
181247

182248
Remove all symbols, debug sections and relocations from the output. This option
@@ -355,18 +421,6 @@ them.
355421
For binary outputs, fill the gaps between sections with ``<value>`` instead
356422
of zero. The value must be an unsigned 8-bit integer.
357423

358-
.. option:: --globalize-symbol <symbol>
359-
360-
Mark any defined symbols named ``<symbol>`` as global symbols in the output.
361-
Can be specified multiple times to mark multiple symbols.
362-
363-
.. option:: --globalize-symbols <filename>
364-
365-
Read a list of names from the file ``<filename>`` and mark defined symbols with
366-
those names as global in the output. In the file, each line represents a single
367-
symbol, with leading and trailing whitespace ignored, as is anything following
368-
a '#'. Can be specified multiple times to read names from multiple files.
369-
370424
.. option:: --input-target <format>, -I
371425

372426
Read the input as the specified format. See `SUPPORTED FORMATS`_ for a list of
@@ -377,18 +431,6 @@ them.
377431

378432
Keep symbols of type `STT_FILE`, even if they would otherwise be stripped.
379433

380-
.. option:: --keep-global-symbol <symbol>, -G
381-
382-
Mark all symbols local in the output, except for symbols with the name
383-
``<symbol>``. Can be specified multiple times to ignore multiple symbols.
384-
385-
.. option:: --keep-global-symbols <filename>
386-
387-
Mark all symbols local in the output, except for symbols named in the file
388-
``<filename>``. In the file, each line represents a single symbol, with leading
389-
and trailing whitespace ignored, as is anything following a '#'. Can be
390-
specified multiple times to read names from multiple files.
391-
392434
.. option:: --keep-section <section>
393435

394436
When removing sections from the output, do not remove sections named
@@ -410,19 +452,6 @@ them.
410452

411453
Mark all symbols with hidden or internal visibility local in the output.
412454

413-
.. option:: --localize-symbol <symbol>, -L
414-
415-
Mark any defined non-common symbol named ``<symbol>`` as a local symbol in the
416-
output. Can be specified multiple times to mark multiple symbols as local.
417-
418-
.. option:: --localize-symbols <filename>
419-
420-
Read a list of names from the file ``<filename>`` and mark defined non-common
421-
symbols with those names as local in the output. In the file, each line
422-
represents a single symbol, with leading and trailing whitespace ignored, as is
423-
anything following a '#'. Can be specified multiple times to read names from
424-
multiple files.
425-
426455
.. option:: --new-symbol-visibility <visibility>
427456

428457
Specify the visibility of the symbols automatically created when using binary
@@ -489,19 +518,6 @@ them.
489518
Read a list of symbols from <filename> and change their visibility to the
490519
specified value. Visibility values: default, internal, hidden, protected.
491520

492-
.. option:: --skip-symbol <symbol>
493-
494-
Do not change the parameters of symbol ``<symbol>`` when executing other
495-
options that can change the symbol's name, binding or visibility.
496-
497-
.. option:: --skip-symbols <filename>
498-
499-
Do not change the parameters of symbols named in the file ``<filename>`` when
500-
executing other options that can change the symbol's name, binding or
501-
visibility. In the file, each line represents a single symbol, with leading
502-
and trailing whitespace ignored, as is anything following a '#'.
503-
Can be specified multiple times to read names from multiple files.
504-
505521
.. option:: --split-dwo <dwo-file>
506522

507523
Equivalent to running :program:`llvm-objcopy` with :option:`--extract-dwo` and

llvm/docs/ReleaseNotes.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,16 @@ Changes to the Debug Info
350350
Changes to the LLVM tools
351351
---------------------------------
352352

353+
* llvm-objcopy now supports the following options for MachO:
354+
`--globalize-symbol`, `--globalize-symbols`,
355+
`--keep-global-symbol`, `-G`, `--keep-global-symbols`,
356+
`--localize-symbol`, `-L`, `--localize-symbols`,
357+
`--skip-symbol`, `--skip-symbols`.
358+
353359
Changes to LLDB
354360
---------------------------------
355361

356-
* LLDB now now supports inline diagnostics for the expression evaluator and command line parser.
362+
* LLDB now supports inline diagnostics for the expression evaluator and command line parser.
357363

358364
Old:
359365
```

llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static void updateAndRemoveSymbols(const CommonConfig &Config,
9898
return;
9999

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

103103
// Note: these two globalize flags have very similar names but different
104104
// meanings:
@@ -112,14 +112,14 @@ static void updateAndRemoveSymbols(const CommonConfig &Config,
112112
// --globalize-symbol second.
113113
if (!Sym.isUndefinedSymbol() && !Config.SymbolsToKeepGlobal.empty() &&
114114
!Config.SymbolsToKeepGlobal.matches(Sym.Name))
115-
Sym.n_type &= ~llvm::MachO::N_EXT;
115+
Sym.n_type &= ~MachO::N_EXT;
116116

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

120120
if (Sym.isExternalSymbol() && !Sym.isUndefinedSymbol() &&
121121
(Config.Weaken || Config.SymbolsToWeaken.matches(Sym.Name)))
122-
Sym.n_desc |= llvm::MachO::N_WEAK_DEF;
122+
Sym.n_desc |= MachO::N_WEAK_DEF;
123123

124124
auto I = Config.SymbolsToRename.find(Sym.Name);
125125
if (I != Config.SymbolsToRename.end())

llvm/lib/ObjCopy/MachO/MachOObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ void SymbolTable::updateSymbols(function_ref<void(SymbolEntry &)> Callable) {
3838
Callable(*Sym);
3939

4040
// Partition symbols: local < defined external < undefined external.
41-
auto it_ext = std::stable_partition(
41+
auto ExternalBegin = std::stable_partition(
4242
std::begin(Symbols), std::end(Symbols),
4343
[](const auto &Sym) { return Sym->isLocalSymbol(); });
44-
std::stable_partition(it_ext, std::end(Symbols), [](const auto &Sym) {
44+
std::stable_partition(ExternalBegin, std::end(Symbols), [](const auto &Sym) {
4545
return !Sym->isUndefinedSymbol();
4646
});
4747
}

llvm/test/tools/llvm-objcopy/MachO/globalize-symbol.test

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
# RUN: llvm-objcopy --wildcard --globalize-symbol="*" %t %t.copy
33
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
44

5-
# CHECK: Symbols [
5+
# RUN: echo "*" > %t-star.txt
6+
# RUN: llvm-objcopy --wildcard --globalize-symbols="%t-star.txt" %t %t.copy
7+
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
8+
9+
# CHECK: Symbols [
610
# CHECK-NEXT: Symbol {
711
# CHECK-NEXT: Name: _PrivateSymbol
812
# CHECK-NEXT: Extern

llvm/test/tools/llvm-objcopy/MachO/keep-global-symbol.test

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
# RUN: llvm-objcopy --keep-global-symbol _CommonSymbol %t %t.copy
33
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
44

5-
# CHECK: Symbols [
5+
# RUN: echo _CommonSymbol > %t-sym-list.txt
6+
# RUN: llvm-objcopy --wildcard --keep-global-symbols="%t-sym-list.txt" %t %t.copy
7+
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
8+
9+
# CHECK: Symbols [
610
# CHECK-NEXT: Symbol {
711
# CHECK-NEXT: Name: _PrivateSymbol
812
# CHECK-NEXT: Type: Section (0xE)

llvm/test/tools/llvm-objcopy/MachO/localize-symbol.test

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
# RUN: llvm-objcopy --wildcard --localize-symbol="*" %t %t.copy
33
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
44

5-
# CHECK: Symbols [
5+
# RUN: echo "*" > %t-star.txt
6+
# RUN: llvm-objcopy --wildcard --localize-symbols="%t-star.txt" %t %t.copy
7+
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
8+
9+
# CHECK: Symbols [
610
# CHECK-NEXT: Symbol {
711
# CHECK-NEXT: Name: _PrivateSymbol
812
# CHECK-NEXT: Type: Section (0xE)

llvm/test/tools/llvm-objcopy/MachO/skip-symbol.test

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
# RUN: llvm-objcopy --wildcard --localize-symbol="*" --skip-symbol _CommonSymbol %t %t.copy
33
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
44

5-
# CHECK: Symbols [
5+
# RUN: echo "*" > %t-star.txt
6+
# RUN: echo _CommonSymbol > %t-sym-list.txt
7+
# RUN: llvm-objcopy --wildcard --localize-symbols="%t-star.txt" --skip-symbols="%t-sym-list.txt" %t %t.copy
8+
# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
9+
10+
# CHECK: Symbols [
611
# CHECK-NEXT: Symbol {
712
# CHECK-NEXT: Name: _PrivateSymbol
813
# CHECK-NEXT: Type: Section (0xE)

0 commit comments

Comments
 (0)