xref: /llvm-project/llvm/docs/CommandGuide/llvm-objcopy.rst (revision be8bc3cf43f07f68d89c82db45e29f63b432ceb5)
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-symbol-prefix <prefix>
121
122Remove ``<prefix>`` from the start of every symbol name. No-op for symbols that do
123not start with ``<prefix>``.
124
125.. option:: --remove-section <section>, -R
126
127 Remove the specified section from the output. Can be specified multiple times
128 to remove multiple sections simultaneously.
129
130 For MachO objects, ``<section>`` must be formatted as
131 ``<segment name>,<section name>``.
132
133.. option:: --set-section-alignment <section>=<align>
134
135 Set the alignment of section ``<section>`` to ``<align>``. Can be specified
136 multiple times to update multiple sections.
137
138.. option:: --set-section-flags <section>=<flag>[,<flag>,...]
139
140 Set section properties in the output of section ``<section>`` based on the
141 specified ``<flag>`` values. Can be specified multiple times to update multiple
142 sections.
143
144 Supported flag names are `alloc`, `load`, `noload`, `readonly`, `exclude`,
145 `debug`, `code`, `data`, `rom`, `share`, `contents`, `merge`, `strings`, and
146 `large`. Not all flags are meaningful for all object file formats or target
147 architectures.
148
149 For ELF objects, the flags have the following effects:
150
151 - `alloc` = add the `SHF_ALLOC` flag.
152 - `load` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS`
153   section.
154 - `readonly` = if this flag is not specified, add the `SHF_WRITE` flag.
155 - `exclude` = add the `SHF_EXCLUDE` flag.
156 - `code` = add the `SHF_EXECINSTR` flag.
157 - `merge` = add the `SHF_MERGE` flag.
158 - `strings` = add the `SHF_STRINGS` flag.
159 - `contents` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS`
160   section.
161 - `large` = add the `SHF_X86_64_LARGE` on x86_64; rejected if the target
162   architecture is not x86_64.
163
164 For COFF objects, the flags have the following effects:
165
166 - `alloc` = add the `IMAGE_SCN_CNT_UNINITIALIZED_DATA` and `IMAGE_SCN_MEM_READ`
167   flags, unless the `load` flag is specified.
168 - `noload` = add the `IMAGE_SCN_LNK_REMOVE` and `IMAGE_SCN_MEM_READ` flags.
169 - `readonly` = if this flag is not specified, add the `IMAGE_SCN_MEM_WRITE`
170   flag.
171 - `exclude` = add the `IMAGE_SCN_LNK_REMOVE` and `IMAGE_SCN_MEM_READ` flags.
172 - `debug` = add the `IMAGE_SCN_CNT_INITIALIZED_DATA`,
173   `IMAGE_SCN_MEM_DISCARDABLE` and  `IMAGE_SCN_MEM_READ` flags.
174 - `code` = add the `IMAGE_SCN_CNT_CODE`, `IMAGE_SCN_MEM_EXECUTE` and
175   `IMAGE_SCN_MEM_READ` flags.
176 - `data` = add the `IMAGE_SCN_CNT_INITIALIZED_DATA` and `IMAGE_SCN_MEM_READ`
177   flags.
178 - `share` = add the `IMAGE_SCN_MEM_SHARED` and `IMAGE_SCN_MEM_READ` flags.
179
180.. option:: --strip-all-gnu
181
182 Remove all symbols, debug sections and relocations from the output. This option
183 is equivalent to GNU :program:`objcopy`'s ``--strip-all`` switch.
184
185.. option:: --strip-all, -S
186
187 For ELF objects, remove from the output all symbols and non-alloc sections not
188 within segments, except for .gnu.warning, .ARM.attribute sections and the
189 section name table.
190
191 For COFF and Mach-O objects, remove all symbols, debug sections, and
192 relocations from the output.
193
194.. option:: --strip-debug, -g
195
196 Remove all debug sections from the output.
197
198.. option:: --strip-symbol <symbol>, -N
199
200 Remove all symbols named ``<symbol>`` from the output. Can be specified
201 multiple times to remove multiple symbols.
202
203.. option:: --strip-symbols <filename>
204
205 Remove all symbols whose names appear in the file ``<filename>``, from the
206 output. In the file, each line represents a single symbol name, with leading
207 and trailing whitespace ignored, as is anything following a '#'. Can be
208 specified multiple times to read names from multiple files.
209
210.. option:: --strip-unneeded-symbol <symbol>
211
212 Remove from the output all symbols named ``<symbol>`` that are local or
213 undefined and are not required by any relocation.
214
215.. option:: --strip-unneeded-symbols <filename>
216
217 Remove all symbols whose names appear in the file ``<filename>``, from the
218 output, if they are local or undefined and are not required by any relocation.
219 In the file, each line represents a single symbol name, with leading and
220 trailing whitespace ignored, as is anything following a '#'. Can be specified
221 multiple times to read names from multiple files.
222
223.. option:: --strip-unneeded
224
225 Remove from the output all local or undefined symbols that are not required by
226 relocations. Also remove all debug sections.
227
228.. option:: --update-section <name>=<file>
229
230 Replace the contents of the section ``<name>`` with contents from the file
231 ``<file>``. If the section ``<name>`` is part of a segment, the new contents
232 cannot be larger than the existing section.
233
234.. option:: --version, -V
235
236 Display the version of the :program:`llvm-objcopy` executable.
237
238.. option:: --wildcard, -w
239
240  Allow wildcard syntax for symbol-related flags. On by default for
241  section-related flags. Incompatible with --regex.
242
243  Wildcard syntax allows the following special symbols:
244
245  ====================== ========================= ==================
246   Character              Meaning                   Equivalent
247  ====================== ========================= ==================
248  ``*``                  Any number of characters  ``.*``
249  ``?``                  Any single character      ``.``
250  ``\``                  Escape the next character ``\``
251  ``[a-z]``              Character class           ``[a-z]``
252  ``[!a-z]``, ``[^a-z]`` Negated character class   ``[^a-z]``
253  ====================== ========================= ==================
254
255  Additionally, starting a wildcard with '!' will prevent a match, even if
256  another flag matches. For example ``-w -N '*' -N '!x'`` will strip all symbols
257  except for ``x``.
258
259  The order of wildcards does not matter. For example, ``-w -N '*' -N '!x'`` is
260  the same as ``-w -N '!x' -N '*'``.
261
262.. option:: @<FILE>
263
264 Read command-line options and commands from response file `<FILE>`.
265
266ELF-SPECIFIC OPTIONS
267--------------------
268
269The following options are implemented only for ELF objects. If used with other
270objects, :program:`llvm-objcopy` will either emit an error or silently ignore
271them.
272
273.. option:: --add-symbol <name>=[<section>:]<value>[,<flags>]
274
275 Add a new symbol called ``<name>`` to the output symbol table, in the section
276 named ``<section>``, with value ``<value>``. If ``<section>`` is not specified,
277 the symbol is added as an absolute symbol. The ``<flags>`` affect the symbol
278 properties. Accepted values are:
279
280 - `global` = the symbol will have global binding.
281 - `local` = the symbol will have local binding.
282 - `weak` = the symbol will have weak binding.
283 - `default` = the symbol will have default visibility.
284 - `hidden` = the symbol will have hidden visibility.
285 - `protected` = the symbol will have protected visibility.
286 - `file` = the symbol will be an `STT_FILE` symbol.
287 - `section` = the symbol will be an `STT_SECTION` symbol.
288 - `object` = the symbol will be an `STT_OBJECT` symbol.
289 - `function` = the symbol will be an `STT_FUNC` symbol.
290 - `indirect-function` = the symbol will be an `STT_GNU_IFUNC` symbol.
291
292 Additionally, the following flags are accepted but ignored: `debug`,
293 `constructor`, `warning`, `indirect`, `synthetic`, `unique-object`, `before`.
294
295 Can be specified multiple times to add multiple symbols.
296
297.. option:: --allow-broken-links
298
299 Allow :program:`llvm-objcopy` to remove sections even if it would leave invalid
300 section references. Any invalid sh_link fields will be set to zero.
301
302.. option:: --change-start <incr>, --adjust-start
303
304 Add ``<incr>`` to the program's start address. Can be specified multiple
305 times, in which case the values will be applied cumulatively.
306
307.. option:: --compress-debug-sections [<format>]
308
309 Compress DWARF debug sections in the output, using the specified format.
310 Supported formats are ``zlib`` and ``zstd``. Use ``zlib`` if ``<format>`` is omitted.
311
312.. option:: --decompress-debug-sections
313
314 Decompress any compressed DWARF debug sections in the output.
315
316.. option:: --discard-locals, -X
317
318 Remove local symbols starting with ".L" from the output.
319
320.. option:: --extract-dwo
321
322 Remove all sections that are not DWARF .dwo sections from the output.
323
324.. option:: --extract-main-partition
325
326 Extract the main partition from the output.
327
328.. option:: --extract-partition <name>
329
330 Extract the named partition from the output.
331
332.. option:: --gap-fill <value>
333
334 For binary outputs, fill the gaps between sections with ``<value>`` instead
335 of zero. The value must be an unsigned 8-bit integer.
336
337.. option:: --globalize-symbol <symbol>
338
339 Mark any defined symbols named ``<symbol>`` as global symbols in the output.
340 Can be specified multiple times to mark multiple symbols.
341
342.. option:: --globalize-symbols <filename>
343
344 Read a list of names from the file ``<filename>`` and mark defined symbols with
345 those names as global in the output. In the file, each line represents a single
346 symbol, with leading and trailing whitespace ignored, as is anything following
347 a '#'. Can be specified multiple times to read names from multiple files.
348
349.. option:: --input-target <format>, -I
350
351 Read the input as the specified format. See `SUPPORTED FORMATS`_ for a list of
352 valid ``<format>`` values. If unspecified, :program:`llvm-objcopy` will attempt
353 to determine the format automatically.
354
355.. option:: --keep-file-symbols
356
357 Keep symbols of type `STT_FILE`, even if they would otherwise be stripped.
358
359.. option:: --keep-global-symbol <symbol>, -G
360
361 Make all symbols local in the output, except for symbols with the name
362 ``<symbol>``. Can be specified multiple times to ignore multiple symbols.
363
364.. option:: --keep-global-symbols <filename>
365
366 Make all symbols local in the output, except for symbols named in the file
367 ``<filename>``. In the file, each line represents a single symbol, with leading
368 and trailing whitespace ignored, as is anything following a '#'. Can be
369 specified multiple times to read names from multiple files.
370
371.. option:: --keep-section <section>
372
373 When removing sections from the output, do not remove sections named
374 ``<section>``. Can be specified multiple times to keep multiple sections.
375
376.. option:: --keep-symbol <symbol>, -K
377
378 When removing symbols from the output, do not remove symbols named
379 ``<symbol>``. Can be specified multiple times to keep multiple symbols.
380
381.. option:: --keep-symbols <filename>
382
383 When removing symbols from the output do not remove symbols named in the file
384 ``<filename>``. In the file, each line represents a single symbol, with leading
385 and trailing whitespace ignored, as is anything following a '#'. Can be
386 specified multiple times to read names from multiple files.
387
388.. option:: --localize-hidden
389
390 Make all symbols with hidden or internal visibility local in the output.
391
392.. option:: --localize-symbol <symbol>, -L
393
394 Mark any defined non-common symbol named ``<symbol>`` as a local symbol in the
395 output. Can be specified multiple times to mark multiple symbols as local.
396
397.. option:: --localize-symbols <filename>
398
399 Read a list of names from the file ``<filename>`` and mark defined non-common
400 symbols with those names as local in the output. In the file, each line
401 represents a single symbol, with leading and trailing whitespace ignored, as is
402 anything following a '#'. Can be specified multiple times to read names from
403 multiple files.
404
405.. option:: --new-symbol-visibility <visibility>
406
407 Specify the visibility of the symbols automatically created when using binary
408 input or :option:`--add-symbol`. Valid options are:
409
410 - `default`
411 - `hidden`
412 - `internal`
413 - `protected`
414
415 The default is `default`.
416
417.. option:: --output-target <format>, -O
418
419 Write the output as the specified format. See `SUPPORTED FORMATS`_ for a list
420 of valid ``<format>`` values. If unspecified, the output format is assumed to
421 be the same as the value specified for :option:`--input-target` or the input
422 file's format if that option is also unspecified.
423
424.. option:: --pad-to <address>
425
426 For binary outputs, pad the output to the load address ``<address>`` using a value
427 of zero or the value specified by :option:`--gap-fill`.
428
429.. option:: --prefix-alloc-sections <prefix>
430
431 Add ``<prefix>`` to the front of the names of all allocatable sections in the
432 output.
433
434.. option:: --prefix-symbols <prefix>
435
436 Add ``<prefix>`` to the front of every symbol name in the output.
437
438.. option:: --preserve-dates, -p
439
440 Preserve access and modification timestamps in the output.
441
442.. option:: --rename-section <old>=<new>[,<flag>,...]
443
444 Rename sections called ``<old>`` to ``<new>`` in the output, and apply any
445 specified ``<flag>`` values. See :option:`--set-section-flags` for a list of
446 supported flags. Can be specified multiple times to rename multiple sections.
447
448.. option:: --set-section-type <section>=<type>
449
450 Set the type of section ``<section>`` to the integer ``<type>``. Can be
451 specified multiple times to update multiple sections.
452
453.. option:: --set-start <addr>
454
455 Set the start address of the output to ``<addr>``. Overrides any previously
456 specified :option:`--change-start` or :option:`--adjust-start` options.
457
458.. option:: --set-symbol-visibility <symbol>=<visibility>
459
460 Change the visibility of a symbol to the specified value.
461
462.. option:: --set-symbols-visibility <filename>=<visibility>
463
464 Read a list of symbols from <filename> and change their visibility to the
465 specified value. Visibility values: default, internal, hidden, protected.
466
467.. option:: --skip-symbol <symbol>
468
469 Do not change the parameters of symbol ``<symbol>`` when executing other
470 options that can change the symbol's name, binding or visibility.
471
472.. option:: --skip-symbols <filename>
473
474 Do not change the parameters of symbols named in the file ``<filename>`` when
475 executing other options that can change the symbol's name, binding or
476 visibility. In the file, each line represents a single symbol, with leading
477 and trailing whitespace ignored, as is anything following a '#'.
478 Can be specified multiple times to read names from multiple files.
479
480.. option:: --split-dwo <dwo-file>
481
482 Equivalent to running :program:`llvm-objcopy` with :option:`--extract-dwo` and
483 ``<dwo-file>`` as the output file and no other options, and then with
484 :option:`--strip-dwo` on the input file.
485
486.. option:: --strip-dwo
487
488 Remove all DWARF .dwo sections from the output.
489
490.. option:: --strip-non-alloc
491
492 Remove from the output all non-allocatable sections that are not within
493 segments.
494
495.. option:: --strip-sections
496
497 Remove from the output all section headers and all section data not within
498 segments. Note that many tools will not be able to use an object without
499 section headers.
500
501.. option:: --target <format>, -F
502
503 Equivalent to :option:`--input-target` and :option:`--output-target` for the
504 specified format. See `SUPPORTED FORMATS`_ for a list of valid ``<format>``
505 values.
506
507.. option:: --weaken-symbol <symbol>, -W
508
509 Mark global symbols named ``<symbol>`` as weak symbols in the output. Can
510 be specified multiple times to mark multiple symbols as weak.
511
512.. option:: --weaken-symbols <filename>
513
514 Read a list of names from the file ``<filename>`` and mark global symbols with
515 those names as weak in the output. In the file, each line represents a single
516 symbol, with leading and trailing whitespace ignored, as is anything following
517 a '#'. Can be specified multiple times to read names from multiple files.
518
519.. option:: --weaken
520
521 Mark all defined global symbols as weak in the output.
522
523MACH-O-SPECIFIC OPTIONS
524-----------------------
525
526.. option:: --keep-undefined
527
528 Keep undefined symbols, even if they would otherwise be stripped.
529
530COFF-SPECIFIC OPTIONS
531---------------------
532
533.. option:: --subsystem <name>[:<version>]
534
535 Set the PE subsystem, and optionally subsystem version.
536
537SUPPORTED FORMATS
538-----------------
539
540The following values are currently supported by :program:`llvm-objcopy` for the
541:option:`--input-target`, :option:`--output-target`, and :option:`--target`
542options. For GNU :program:`objcopy` compatibility, the values are all bfdnames.
543
544- `binary`
545- `ihex`
546- `elf32-i386`
547- `elf32-x86-64`
548- `elf64-x86-64`
549- `elf32-iamcu`
550- `elf32-littlearm`
551- `elf64-aarch64`
552- `elf64-littleaarch64`
553- `elf32-littleriscv`
554- `elf64-littleriscv`
555- `elf32-powerpc`
556- `elf32-powerpcle`
557- `elf64-powerpc`
558- `elf64-powerpcle`
559- `elf32-bigmips`
560- `elf32-ntradbigmips`
561- `elf32-ntradlittlemips`
562- `elf32-tradbigmips`
563- `elf32-tradlittlemips`
564- `elf64-tradbigmips`
565- `elf64-tradlittlemips`
566- `elf32-sparc`
567- `elf32-sparcel`
568- `elf32-hexagon`
569- `elf32-loongarch`
570- `elf64-loongarch`
571- `elf64-s390`
572
573The following formats are suppoprted by :program:`llvm-objcopy` for the
574:option:`--output-target` only:
575
576- `srec`
577
578Additionally, all targets except `binary`, `ihex`, and `srec` can have
579`-freebsd` as a suffix.
580
581BINARY INPUT AND OUTPUT
582-----------------------
583
584If `binary` is used as the value for :option:`--input-target`, the input file
585will be embedded as a data section in an ELF relocatable object, with symbols
586``_binary_<file_name>_start``, ``_binary_<file_name>_end``, and
587``_binary_<file_name>_size`` representing the start, end and size of the data,
588where ``<file_name>`` is the path of the input file as specified on the command
589line with non-alphanumeric characters converted to ``_``.
590
591If `binary` is used as the value for :option:`--output-target`, the output file
592will be a raw binary file, containing the memory image of the input file.
593Symbols and relocation information will be discarded. The image will start at
594the address of the first loadable section in the output.
595
596EXIT STATUS
597-----------
598
599:program:`llvm-objcopy` exits with a non-zero exit code if there is an error.
600Otherwise, it exits with code 0.
601
602BUGS
603----
604
605To report bugs, please visit <https://github.com/llvm/llvm-project/labels/tools:llvm-objcopy/strip/>.
606
607There is a known issue with :option:`--input-target` and :option:`--target`
608causing only ``binary`` and ``ihex`` formats to have any effect. Other values
609will be ignored and :program:`llvm-objcopy` will attempt to guess the input
610format.
611
612SEE ALSO
613--------
614
615:manpage:`llvm-strip(1)`
616