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