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