xref: /openbsd-src/gnu/llvm/llvm/docs/CommandGuide/llvm-objcopy.rst (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
1llvm-objcopy - object copying and editing tool
2==============================================
3
4.. program:: llvm-objcopy
5
6SYNOPSIS
7--------
8
9:program:`llvm-objcopy` [*options*] *input* [*output*]
10
11DESCRIPTION
12-----------
13
14:program:`llvm-objcopy` is a tool to copy and manipulate objects. In basic
15usage, it makes a semantic copy of the input to the output. If any options are
16specified, the output may be modified along the way, e.g. by removing sections.
17
18If no output file is specified, the input file is modified in-place. If "-" is
19specified for the input file, the input is read from the program's standard
20input stream. If "-" is specified for the output file, the output is written to
21the standard output stream of the program.
22
23If the input is an archive, any requested operations will be applied to each
24archive member individually.
25
26The tool is still in active development, but in most scenarios it works as a
27drop-in replacement for GNU's :program:`objcopy`.
28
29GENERIC AND CROSS-PLATFORM OPTIONS
30----------------------------------
31
32The following options are either agnostic of the file format, or apply to
33multiple file formats.
34
35.. option:: --add-gnu-debuglink <debug-file>
36
37 Add a .gnu_debuglink section for ``<debug-file>`` to the output.
38
39.. option:: --add-section <section=file>
40
41 Add a section named ``<section>`` with the contents of ``<file>`` to the
42 output. For ELF objects the section will be of type `SHT_NOTE`, if the name
43 starts with ".note". Otherwise, it will have type `SHT_PROGBITS`. Can be
44 specified multiple times to add multiple sections.
45
46 For MachO objects, ``<section>`` must be formatted as
47 ``<segment name>,<section name>``.
48
49.. option:: --binary-architecture <arch>, -B
50
51 Ignored for compatibility.
52
53.. option:: --disable-deterministic-archives, -U
54
55 Use real values for UIDs, GIDs and timestamps when updating archive member
56 headers.
57
58.. option:: --discard-all, -x
59
60 Remove most local symbols from the output. Different file formats may limit
61 this to a subset of the local symbols. For example, file and section symbols in
62 ELF objects will not be discarded. Additionally, remove all debug sections.
63
64.. option:: --dump-section <section>=<file>
65
66 Dump the contents of section ``<section>`` into the file ``<file>``. Can be
67 specified multiple times to dump multiple sections to different files.
68 ``<file>`` is unrelated to the input and output files provided to
69 :program:`llvm-objcopy` and as such the normal copying and editing
70 operations will still be performed. No operations are performed on the sections
71 prior to dumping them.
72
73 For MachO objects, ``<section>`` must be formatted as
74 ``<segment name>,<section name>``.
75
76.. option:: --enable-deterministic-archives, -D
77
78 Enable deterministic mode when copying archives, i.e. use 0 for archive member
79 header UIDs, GIDs and timestamp fields. On by default.
80
81.. option:: --help, -h
82
83 Print a summary of command line options.
84
85.. option:: --only-keep-debug
86
87 Produce a debug file as the output that only preserves contents of sections
88 useful for debugging purposes.
89
90 For ELF objects, this removes the contents of `SHF_ALLOC` sections that are not
91 `SHT_NOTE` by making them `SHT_NOBITS` and shrinking the program headers where
92 possible.
93
94.. option:: --only-section <section>, -j
95
96 Remove all sections from the output, except for sections named ``<section>``.
97 Can be specified multiple times to keep multiple sections.
98
99 For MachO objects, ``<section>`` must be formatted as
100 ``<segment name>,<section name>``.
101
102.. option:: --redefine-sym <old>=<new>
103
104 Rename symbols called ``<old>`` to ``<new>`` in the output. Can be specified
105 multiple times to rename multiple symbols.
106
107.. option:: --redefine-syms <filename>
108
109 Rename symbols in the output as described in the file ``<filename>``. In the
110 file, each line represents a single symbol to rename, with the old name and new
111 name separated by whitespace. Leading and trailing whitespace is ignored, as is
112 anything following a '#'. Can be specified multiple times to read names from
113 multiple files.
114
115.. option:: --regex
116
117 If specified, symbol and section names specified by other switches are treated
118 as extended POSIX regular expression patterns.
119
120.. option:: --remove-section <section>, -R
121
122 Remove the specified section from the output. Can be specified multiple times
123 to remove multiple sections simultaneously.
124
125 For MachO objects, ``<section>`` must be formatted as
126 ``<segment name>,<section name>``.
127
128.. option:: --set-section-alignment <section>=<align>
129
130 Set the alignment of section ``<section>`` to ``<align>``. Can be specified
131 multiple times to update multiple sections.
132
133.. option:: --set-section-flags <section>=<flag>[,<flag>,...]
134
135 Set section properties in the output of section ``<section>`` based on the
136 specified ``<flag>`` values. Can be specified multiple times to update multiple
137 sections.
138
139 Supported flag names are `alloc`, `load`, `noload`, `readonly`, `exclude`,
140 `debug`, `code`, `data`, `rom`, `share`, `contents`, `merge` and `strings`. Not
141 all flags are meaningful for all object file formats.
142
143 For ELF objects, the flags have the following effects:
144
145 - `alloc` = add the `SHF_ALLOC` flag.
146 - `load` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS`
147   section.
148 - `readonly` = if this flag is not specified, add the `SHF_WRITE` flag.
149 - `exclude` = add the `SHF_EXCLUDE` flag.
150 - `code` = add the `SHF_EXECINSTR` flag.
151 - `merge` = add the `SHF_MERGE` flag.
152 - `strings` = add the `SHF_STRINGS` flag.
153 - `contents` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS`
154   section.
155
156 For COFF objects, the flags have the following effects:
157
158 - `alloc` = add the `IMAGE_SCN_CNT_UNINITIALIZED_DATA` and `IMAGE_SCN_MEM_READ`
159   flags, unless the `load` flag is specified.
160 - `noload` = add the `IMAGE_SCN_LNK_REMOVE` and `IMAGE_SCN_MEM_READ` flags.
161 - `readonly` = if this flag is not specified, add the `IMAGE_SCN_MEM_WRITE`
162   flag.
163 - `exclude` = add the `IMAGE_SCN_LNK_REMOVE` and `IMAGE_SCN_MEM_READ` flags.
164 - `debug` = add the `IMAGE_SCN_CNT_INITIALIZED_DATA`,
165   `IMAGE_SCN_MEM_DISCARDABLE` and  `IMAGE_SCN_MEM_READ` flags.
166 - `code` = add the `IMAGE_SCN_CNT_CODE`, `IMAGE_SCN_MEM_EXECUTE` and
167   `IMAGE_SCN_MEM_READ` flags.
168 - `data` = add the `IMAGE_SCN_CNT_INITIALIZED_DATA` and `IMAGE_SCN_MEM_READ`
169   flags.
170 - `share` = add the `IMAGE_SCN_MEM_SHARED` and `IMAGE_SCN_MEM_READ` flags.
171
172.. option:: --strip-all-gnu
173
174 Remove all symbols, debug sections and relocations from the output. This option
175 is equivalent to GNU :program:`objcopy`'s ``--strip-all`` switch.
176
177.. option:: --strip-all, -S
178
179 For ELF objects, remove from the output all symbols and non-alloc sections not
180 within segments, except for .gnu.warning, .ARM.attribute sections and the
181 section name table.
182
183 For COFF and Mach-O objects, remove all symbols, debug sections, and
184 relocations from the output.
185
186.. option:: --strip-debug, -g
187
188 Remove all debug sections from the output.
189
190.. option:: --strip-symbol <symbol>, -N
191
192 Remove all symbols named ``<symbol>`` from the output. Can be specified
193 multiple times to remove multiple symbols.
194
195.. option:: --strip-symbols <filename>
196
197 Remove all symbols whose names appear in the file ``<filename>``, from the
198 output. In the file, each line represents a single symbol name, with leading
199 and trailing whitespace ignored, as is anything following a '#'. Can be
200 specified multiple times to read names from multiple files.
201
202.. option:: --strip-unneeded-symbol <symbol>
203
204 Remove from the output all symbols named ``<symbol>`` that are local or
205 undefined and are not required by any relocation.
206
207.. option:: --strip-unneeded-symbols <filename>
208
209 Remove all symbols whose names appear in the file ``<filename>``, from the
210 output, if they are local or undefined and are not required by any relocation.
211 In the file, each line represents a single symbol name, with leading and
212 trailing whitespace ignored, as is anything following a '#'. Can be specified
213 multiple times to read names from multiple files.
214
215.. option:: --strip-unneeded
216
217 Remove from the output all local or undefined symbols that are not required by
218 relocations. Also remove all debug sections.
219
220.. option:: --update-section <name>=<file>
221
222 Replace the contents of the section ``<name>`` with contents from the file
223 ``<file>``. If the section ``<name>`` is part of a segment, the new contents
224 cannot be larger than the existing section.
225
226.. option:: --version, -V
227
228 Display the version of the :program:`llvm-objcopy` executable.
229
230.. option:: --wildcard, -w
231
232  Allow wildcard syntax for symbol-related flags. On by default for
233  section-related flags. Incompatible with --regex.
234
235  Wildcard syntax allows the following special symbols:
236
237  ====================== ========================= ==================
238   Character              Meaning                   Equivalent
239  ====================== ========================= ==================
240  ``*``                  Any number of characters  ``.*``
241  ``?``                  Any single character      ``.``
242  ``\``                  Escape the next character ``\``
243  ``[a-z]``              Character class           ``[a-z]``
244  ``[!a-z]``, ``[^a-z]`` Negated character class   ``[^a-z]``
245  ====================== ========================= ==================
246
247  Additionally, starting a wildcard with '!' will prevent a match, even if
248  another flag matches. For example ``-w -N '*' -N '!x'`` will strip all symbols
249  except for ``x``.
250
251  The order of wildcards does not matter. For example, ``-w -N '*' -N '!x'`` is
252  the same as ``-w -N '!x' -N '*'``.
253
254.. option:: @<FILE>
255
256 Read command-line options and commands from response file `<FILE>`.
257
258ELF-SPECIFIC OPTIONS
259--------------------
260
261The following options are implemented only for ELF objects. If used with other
262objects, :program:`llvm-objcopy` will either emit an error or silently ignore
263them.
264
265.. option:: --add-symbol <name>=[<section>:]<value>[,<flags>]
266
267 Add a new symbol called ``<name>`` to the output symbol table, in the section
268 named ``<section>``, with value ``<value>``. If ``<section>`` is not specified,
269 the symbol is added as an absolute symbol. The ``<flags>`` affect the symbol
270 properties. Accepted values are:
271
272 - `global` = the symbol will have global binding.
273 - `local` = the symbol will have local binding.
274 - `weak` = the symbol will have weak binding.
275 - `default` = the symbol will have default visibility.
276 - `hidden` = the symbol will have hidden visibility.
277 - `protected` = the symbol will have protected visibility.
278 - `file` = the symbol will be an `STT_FILE` symbol.
279 - `section` = the symbol will be an `STT_SECTION` symbol.
280 - `object` = the symbol will be an `STT_OBJECT` symbol.
281 - `function` = the symbol will be an `STT_FUNC` symbol.
282 - `indirect-function` = the symbol will be an `STT_GNU_IFUNC` symbol.
283
284 Additionally, the following flags are accepted but ignored: `debug`,
285 `constructor`, `warning`, `indirect`, `synthetic`, `unique-object`, `before`.
286
287 Can be specified multiple times to add multiple symbols.
288
289.. option:: --allow-broken-links
290
291 Allow :program:`llvm-objcopy` to remove sections even if it would leave invalid
292 section references. Any invalid sh_link fields will be set to zero.
293
294.. option:: --change-start <incr>, --adjust-start
295
296 Add ``<incr>`` to the program's start address. Can be specified multiple
297 times, in which case the values will be applied cumulatively.
298
299.. option:: --compress-debug-sections [<format>]
300
301 Compress DWARF debug sections in the output, using the specified format.
302 Supported formats are ``zlib`` and ``zstd``. Use ``zlib`` if ``<format>`` is omitted.
303
304.. option:: --decompress-debug-sections
305
306 Decompress any compressed DWARF debug sections in the output.
307
308.. option:: --discard-locals, -X
309
310 Remove local symbols starting with ".L" from the output.
311
312.. option:: --extract-dwo
313
314 Remove all sections that are not DWARF .dwo sections from the output.
315
316.. option:: --extract-main-partition
317
318 Extract the main partition from the output.
319
320.. option:: --extract-partition <name>
321
322 Extract the named partition from the output.
323
324.. option:: --globalize-symbol <symbol>
325
326 Mark any defined symbols named ``<symbol>`` as global symbols in the output.
327 Can be specified multiple times to mark multiple symbols.
328
329.. option:: --globalize-symbols <filename>
330
331 Read a list of names from the file ``<filename>`` and mark defined symbols with
332 those names as global in the output. In the file, each line represents a single
333 symbol, with leading and trailing whitespace ignored, as is anything following
334 a '#'. Can be specified multiple times to read names from multiple files.
335
336.. option:: --input-target <format>, -I
337
338 Read the input as the specified format. See `SUPPORTED FORMATS`_ for a list of
339 valid ``<format>`` values. If unspecified, :program:`llvm-objcopy` will attempt
340 to determine the format automatically.
341
342.. option:: --keep-file-symbols
343
344 Keep symbols of type `STT_FILE`, even if they would otherwise be stripped.
345
346.. option:: --keep-global-symbol <symbol>, -G
347
348 Make all symbols local in the output, except for symbols with the name
349 ``<symbol>``. Can be specified multiple times to ignore multiple symbols.
350
351.. option:: --keep-global-symbols <filename>
352
353 Make all symbols local in the output, except for symbols named in the file
354 ``<filename>``. In the file, each line represents a single symbol, with leading
355 and trailing whitespace ignored, as is anything following a '#'. Can be
356 specified multiple times to read names from multiple files.
357
358.. option:: --keep-section <section>
359
360 When removing sections from the output, do not remove sections named
361 ``<section>``. Can be specified multiple times to keep multiple sections.
362
363.. option:: --keep-symbol <symbol>, -K
364
365 When removing symbols from the output, do not remove symbols named
366 ``<symbol>``. Can be specified multiple times to keep multiple symbols.
367
368.. option:: --keep-symbols <filename>
369
370 When removing symbols from the output do not remove symbols named in the file
371 ``<filename>``. In the file, each line represents a single symbol, with leading
372 and trailing whitespace ignored, as is anything following a '#'. Can be
373 specified multiple times to read names from multiple files.
374
375.. option:: --localize-hidden
376
377 Make all symbols with hidden or internal visibility local in the output.
378
379.. option:: --localize-symbol <symbol>, -L
380
381 Mark any defined non-common symbol named ``<symbol>`` as a local symbol in the
382 output. Can be specified multiple times to mark multiple symbols as local.
383
384.. option:: --localize-symbols <filename>
385
386 Read a list of names from the file ``<filename>`` and mark defined non-common
387 symbols with those names as local in the output. In the file, each line
388 represents a single symbol, with leading and trailing whitespace ignored, as is
389 anything following a '#'. Can be specified multiple times to read names from
390 multiple files.
391
392.. option:: --new-symbol-visibility <visibility>
393
394 Specify the visibility of the symbols automatically created when using binary
395 input or :option:`--add-symbol`. Valid options are:
396
397 - `default`
398 - `hidden`
399 - `internal`
400 - `protected`
401
402 The default is `default`.
403
404.. option:: --output-target <format>, -O
405
406 Write the output as the specified format. See `SUPPORTED FORMATS`_ for a list
407 of valid ``<format>`` values. If unspecified, the output format is assumed to
408 be the same as the value specified for :option:`--input-target` or the input
409 file's format if that option is also unspecified.
410
411.. option:: --prefix-alloc-sections <prefix>
412
413 Add ``<prefix>`` to the front of the names of all allocatable sections in the
414 output.
415
416.. option:: --prefix-symbols <prefix>
417
418 Add ``<prefix>`` to the front of every symbol name in the output.
419
420.. option:: --preserve-dates, -p
421
422 Preserve access and modification timestamps in the output.
423
424.. option:: --rename-section <old>=<new>[,<flag>,...]
425
426 Rename sections called ``<old>`` to ``<new>`` in the output, and apply any
427 specified ``<flag>`` values. See :option:`--set-section-flags` for a list of
428 supported flags. Can be specified multiple times to rename multiple sections.
429
430.. option:: --set-section-type <section>=<type>
431
432 Set the type of section ``<section>`` to the integer ``<type>``. Can be
433 specified multiple times to update multiple sections.
434
435.. option:: --set-start-addr <addr>
436
437 Set the start address of the output to ``<addr>``. Overrides any previously
438 specified :option:`--change-start` or :option:`--adjust-start` options.
439
440.. option:: --split-dwo <dwo-file>
441
442 Equivalent to running :program:`llvm-objcopy` with :option:`--extract-dwo` and
443 ``<dwo-file>`` as the output file and no other options, and then with
444 :option:`--strip-dwo` on the input file.
445
446.. option:: --strip-dwo
447
448 Remove all DWARF .dwo sections from the output.
449
450.. option:: --strip-non-alloc
451
452 Remove from the output all non-allocatable sections that are not within
453 segments.
454
455.. option:: --strip-sections
456
457 Remove from the output all section headers and all section data not within
458 segments. Note that many tools will not be able to use an object without
459 section headers.
460
461.. option:: --target <format>, -F
462
463 Equivalent to :option:`--input-target` and :option:`--output-target` for the
464 specified format. See `SUPPORTED FORMATS`_ for a list of valid ``<format>``
465 values.
466
467.. option:: --weaken-symbol <symbol>, -W
468
469 Mark any global symbol named ``<symbol>`` as a weak symbol in the output. Can
470 be specified multiple times to mark multiple symbols as weak.
471
472.. option:: --weaken-symbols <filename>
473
474 Read a list of names from the file ``<filename>`` and mark global symbols with
475 those names as weak in the output. In the file, each line represents a single
476 symbol, with leading and trailing whitespace ignored, as is anything following
477 a '#'. Can be specified multiple times to read names from multiple files.
478
479.. option:: --weaken
480
481 Mark all defined global symbols as weak in the output.
482
483MACH-O-SPECIFIC OPTIONS
484-----------------------
485
486.. option:: --keep-undefined
487
488 Keep undefined symbols, even if they would otherwise be stripped.
489
490COFF-SPECIFIC OPTIONS
491---------------------
492
493.. option:: --subsystem <name>[:<version>]
494
495 Set the PE subsystem, and optionally subsystem version.
496
497SUPPORTED FORMATS
498-----------------
499
500The following values are currently supported by :program:`llvm-objcopy` for the
501:option:`--input-target`, :option:`--output-target`, and :option:`--target`
502options. For GNU :program:`objcopy` compatibility, the values are all bfdnames.
503
504- `binary`
505- `ihex`
506- `elf32-i386`
507- `elf32-x86-64`
508- `elf64-x86-64`
509- `elf32-iamcu`
510- `elf32-littlearm`
511- `elf64-aarch64`
512- `elf64-littleaarch64`
513- `elf32-littleriscv`
514- `elf64-littleriscv`
515- `elf32-powerpc`
516- `elf32-powerpcle`
517- `elf64-powerpc`
518- `elf64-powerpcle`
519- `elf32-bigmips`
520- `elf32-ntradbigmips`
521- `elf32-ntradlittlemips`
522- `elf32-tradbigmips`
523- `elf32-tradlittlemips`
524- `elf64-tradbigmips`
525- `elf64-tradlittlemips`
526- `elf32-sparc`
527- `elf32-sparcel`
528
529Additionally, all targets except `binary` and `ihex` can have `-freebsd` as a
530suffix.
531
532BINARY INPUT AND OUTPUT
533-----------------------
534
535If `binary` is used as the value for :option:`--input-target`, the input file
536will be embedded as a data section in an ELF relocatable object, with symbols
537``_binary_<file_name>_start``, ``_binary_<file_name>_end``, and
538``_binary_<file_name>_size`` representing the start, end and size of the data,
539where ``<file_name>`` is the path of the input file as specified on the command
540line with non-alphanumeric characters converted to ``_``.
541
542If `binary` is used as the value for :option:`--output-target`, the output file
543will be a raw binary file, containing the memory image of the input file.
544Symbols and relocation information will be discarded. The image will start at
545the address of the first loadable section in the output.
546
547EXIT STATUS
548-----------
549
550:program:`llvm-objcopy` exits with a non-zero exit code if there is an error.
551Otherwise, it exits with code 0.
552
553BUGS
554----
555
556To report bugs, please visit <https://github.com/llvm/llvm-project/labels/tools:llvm-objcopy/strip/>.
557
558There is a known issue with :option:`--input-target` and :option:`--target`
559causing only ``binary`` and ``ihex`` formats to have any effect. Other values
560will be ignored and :program:`llvm-objcopy` will attempt to guess the input
561format.
562
563SEE ALSO
564--------
565
566:manpage:`llvm-strip(1)`
567