blob: d5e9987df143eb854990a6d869919638cc76b876 [file] [log] [blame]
James Hendersone26ca962019-07-03 14:21:48 +00001llvm-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
Sergey Dmitrievcdeaac52019-07-26 17:06:41 +000039.. 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
Seiya Nuta9e119ad2019-12-16 14:05:06 +090046 For MachO objects, ``<section>`` must be formatted as
47 ``<segment name>,<section name>``.
48
Fangrui Song2f519d72019-09-14 01:36:31 +000049.. option:: --binary-architecture <arch>, -B
50
51 Ignored for compatibility.
52
James Hendersone26ca962019-07-03 14:21:48 +000053.. 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.
63
Seiya Nutad72a8a42019-11-25 12:29:58 +090064.. 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
James Hendersone26ca962019-07-03 14:21:48 +000076.. 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
Fangrui Song5ad01032019-10-24 15:48:32 -070085.. 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
James Hendersone26ca962019-07-03 14:21:48 +000094.. 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
Seiya Nuta7f19dd12019-10-28 15:40:37 +090099 For MachO objects, ``<section>`` must be formatted as
100 ``<segment name>,<section name>``.
101
Fangrui Song7af60252019-11-08 16:19:33 -0800102.. 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
James Hendersone26ca962019-07-03 14:21:48 +0000115.. 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
Seiya Nutabc118302019-11-15 12:37:55 +0900125 For MachO objects, ``<section>`` must be formatted as
126 ``<segment name>,<section name>``.
127
Fangrui Song671fb342019-10-02 12:41:25 +0000128.. 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
James Hendersone26ca962019-07-03 14:21:48 +0000133.. option:: --strip-all-gnu
134
135 Remove all symbols, debug sections and relocations from the output. This option
136 is equivalent to GNU :program:`objcopy`'s ``--strip-all`` switch.
137
138.. option:: --strip-all, -S
139
140 For ELF objects, remove from the output all symbols and non-alloc sections not
James Hendersonfb4a5502019-10-31 11:53:33 +0000141 within segments, except for .gnu.warning, .ARM.attribute sections and the
142 section name table.
James Hendersone26ca962019-07-03 14:21:48 +0000143
Seiya Nuta9bbf2a12019-10-31 13:51:11 +0900144 For COFF and Mach-O objects, remove all symbols, debug sections, and
145 relocations from the output.
James Hendersone26ca962019-07-03 14:21:48 +0000146
147.. option:: --strip-debug, -g
148
149 Remove all debug sections from the output.
150
151.. option:: --strip-symbol <symbol>, -N
152
153 Remove all symbols named ``<symbol>`` from the output. Can be specified
154 multiple times to remove multiple symbols.
155
156.. option:: --strip-symbols <filename>
157
158 Remove all symbols whose names appear in the file ``<filename>``, from the
159 output. In the file, each line represents a single symbol name, with leading
160 and trailing whitespace ignored, as is anything following a '#'. Can be
161 specified multiple times to read names from multiple files.
162
163.. option:: --strip-unneeded-symbol <symbol>
164
165 Remove from the output all symbols named ``<symbol>`` that are local or
166 undefined and are not required by any relocation.
167
168.. option:: --strip-unneeded-symbols <filename>
169
170 Remove all symbols whose names appear in the file ``<filename>``, from the
171 output, if they are local or undefined and are not required by any relocation.
172 In the file, each line represents a single symbol name, with leading and
173 trailing whitespace ignored, as is anything following a '#'. Can be specified
174 multiple times to read names from multiple files.
175
176.. option:: --strip-unneeded
177
178 Remove from the output all local or undefined symbols that are not required by
James Henderson818e5c92019-09-13 13:26:52 +0000179 relocations. Also remove all debug sections.
James Hendersone26ca962019-07-03 14:21:48 +0000180
181.. option:: --version, -V
182
James Henderson778a5e52019-09-17 11:43:42 +0000183 Display the version of the :program:`llvm-objcopy` executable.
James Hendersone26ca962019-07-03 14:21:48 +0000184
Michael Pozulpc45fd0c2019-09-14 01:14:43 +0000185.. option:: @<FILE>
186
James Henderson778a5e52019-09-17 11:43:42 +0000187 Read command-line options and commands from response file `<FILE>`.
Michael Pozulpc45fd0c2019-09-14 01:14:43 +0000188
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000189.. option:: --wildcard, -w
190
191 Allow wildcard syntax for symbol-related flags. On by default for
192 section-related flags. Incompatible with --regex.
193
194 Wildcard syntax allows the following special symbols:
195
196 ====================== ========================= ==================
197 Character Meaning Equivalent
198 ====================== ========================= ==================
199 ``*`` Any number of characters ``.*``
200 ``?`` Any single character ``.``
201 ``\`` Escape the next character ``\``
202 ``[a-z]`` Character class ``[a-z]``
203 ``[!a-z]``, ``[^a-z]`` Negated character class ``[^a-z]``
204 ====================== ========================= ==================
205
206 Additionally, starting a wildcard with '!' will prevent a match, even if
207 another flag matches. For example ``-w -N '*' -N '!x'`` will strip all symbols
208 except for ``x``.
209
210 The order of wildcards does not matter. For example, ``-w -N '*' -N '!x'`` is
211 the same as ``-w -N '!x' -N '*'``.
212
James Hendersone26ca962019-07-03 14:21:48 +0000213ELF-SPECIFIC OPTIONS
214--------------------
215
216The following options are implemented only for ELF objects. If used with other
217objects, :program:`llvm-objcopy` will either emit an error or silently ignore
218them.
219
James Hendersone26ca962019-07-03 14:21:48 +0000220.. option:: --add-symbol <name>=[<section>:]<value>[,<flags>]
221
James Henderson1a517a42019-07-05 11:57:07 +0000222 Add a new symbol called ``<name>`` to the output symbol table, in the section
223 named ``<section>``, with value ``<value>``. If ``<section>`` is not specified,
224 the symbol is added as an absolute symbol. The ``<flags>`` affect the symbol
225 properties. Accepted values are:
James Hendersone26ca962019-07-03 14:21:48 +0000226
227 - `global` = the symbol will have global binding.
228 - `local` = the symbol will have local binding.
229 - `weak` = the symbol will have weak binding.
230 - `default` = the symbol will have default visibility.
231 - `hidden` = the symbol will have hidden visibility.
Chris Jacksone5cdfbc2019-08-15 09:45:09 +0000232 - `protected` = the symbol will have protected visibility.
James Hendersone26ca962019-07-03 14:21:48 +0000233 - `file` = the symbol will be an `STT_FILE` symbol.
234 - `section` = the symbol will be an `STT_SECTION` symbol.
235 - `object` = the symbol will be an `STT_OBJECT` symbol.
236 - `function` = the symbol will be an `STT_FUNC` symbol.
237 - `indirect-function` = the symbol will be an `STT_GNU_IFUNC` symbol.
238
239 Additionally, the following flags are accepted but ignored: `debug`,
240 `constructor`, `warning`, `indirect`, `synthetic`, `unique-object`, `before`.
241
242 Can be specified multiple times to add multiple symbols.
243
244.. option:: --allow-broken-links
245
James Henderson1b103862019-09-24 13:41:39 +0000246 Allow :program:`llvm-objcopy` to remove sections even if it would leave invalid
247 section references. Any invalid sh_link fields will be set to zero.
James Hendersone26ca962019-07-03 14:21:48 +0000248
James Hendersone26ca962019-07-03 14:21:48 +0000249.. option:: --build-id-link-dir <dir>
250
251 Set the directory used by :option:`--build-id-link-input` and
252 :option:`--build-id-link-output`.
253
254.. option:: --build-id-link-input <suffix>
255
256 Hard-link the input to ``<dir>/xx/xxx<suffix>``, where ``<dir>`` is the directory
257 specified by :option:`--build-id-link-dir`. The path used is derived from the
258 hex build ID.
259
260.. option:: --build-id-link-output <suffix>
261
262 Hard-link the output to ``<dir>/xx/xxx<suffix>``, where ``<dir>`` is the directory
263 specified by :option:`--build-id-link-dir`. The path used is derived from the
264 hex build ID.
265
266.. option:: --change-start <incr>, --adjust-start
267
268 Add ``<incr>`` to the program's start address. Can be specified multiple
269 times, in which case the values will be applied cumulatively.
270
271.. option:: --compress-debug-sections [<style>]
272
273 Compress DWARF debug sections in the output, using the specified style.
274 Supported styles are `zlib-gnu` and `zlib`. Defaults to `zlib` if no style is
275 specified.
276
277.. option:: --decompress-debug-sections
278
279 Decompress any compressed DWARF debug sections in the output.
280
281.. option:: --discard-locals, -X
282
283 Remove local symbols starting with ".L" from the output.
284
James Hendersone26ca962019-07-03 14:21:48 +0000285.. option:: --extract-dwo
286
287 Remove all sections that are not DWARF .dwo sections from the output.
288
289.. option:: --extract-main-partition
290
291 Extract the main partition from the output.
292
293.. option:: --extract-partition <name>
294
295 Extract the named partition from the output.
296
297.. option:: --globalize-symbol <symbol>
298
299 Mark any defined symbols named ``<symbol>`` as global symbols in the output.
300 Can be specified multiple times to mark multiple symbols.
301
302.. option:: --globalize-symbols <filename>
303
304 Read a list of names from the file ``<filename>`` and mark defined symbols with
305 those names as global in the output. In the file, each line represents a single
306 symbol, with leading and trailing whitespace ignored, as is anything following
307 a '#'. Can be specified multiple times to read names from multiple files.
308
309.. option:: --input-target <format>, -I
310
311 Read the input as the specified format. See `SUPPORTED FORMATS`_ for a list of
312 valid ``<format>`` values. If unspecified, :program:`llvm-objcopy` will attempt
313 to determine the format automatically.
314
315.. option:: --keep-file-symbols
316
317 Keep symbols of type `STT_FILE`, even if they would otherwise be stripped.
318
319.. option:: --keep-global-symbol <symbol>
320
321 Make all symbols local in the output, except for symbols with the name
322 ``<symbol>``. Can be specified multiple times to ignore multiple symbols.
323
324.. option:: --keep-global-symbols <filename>
325
326 Make all symbols local in the output, except for symbols named in the file
327 ``<filename>``. In the file, each line represents a single symbol, with leading
328 and trailing whitespace ignored, as is anything following a '#'. Can be
329 specified multiple times to read names from multiple files.
330
331.. option:: --keep-section <section>
332
333 When removing sections from the output, do not remove sections named
334 ``<section>``. Can be specified multiple times to keep multiple sections.
335
336.. option:: --keep-symbol <symbol>, -K
337
338 When removing symbols from the output, do not remove symbols named
339 ``<symbol>``. Can be specified multiple times to keep multiple symbols.
340
341.. option:: --keep-symbols <filename>
342
343 When removing symbols from the output do not remove symbols named in the file
344 ``<filename>``. In the file, each line represents a single symbol, with leading
345 and trailing whitespace ignored, as is anything following a '#'. Can be
346 specified multiple times to read names from multiple files.
347
348.. option:: --localize-hidden
349
350 Make all symbols with hidden or internal visibility local in the output.
351
352.. option:: --localize-symbol <symbol>, -L
353
354 Mark any defined non-common symbol named ``<symbol>`` as a local symbol in the
355 output. Can be specified multiple times to mark multiple symbols as local.
356
357.. option:: --localize-symbols <filename>
358
359 Read a list of names from the file ``<filename>`` and mark defined non-common
360 symbols with those names as local in the output. In the file, each line
361 represents a single symbol, with leading and trailing whitespace ignored, as is
362 anything following a '#'. Can be specified multiple times to read names from
363 multiple files.
Chris Jacksonfa1fe932019-08-30 10:17:16 +0000364
365.. option:: --new-symbol-visibility <visibility>
366
367 Specify the visibility of the symbols automatically created when using binary
368 input or :option:`--add-symbol`. Valid options are:
369
370 - `default`
371 - `hidden`
372 - `internal`
373 - `protected`
374
375 The default is `default`.
James Hendersone26ca962019-07-03 14:21:48 +0000376
377.. option:: --output-target <format>, -O
378
379 Write the output as the specified format. See `SUPPORTED FORMATS`_ for a list
380 of valid ``<format>`` values. If unspecified, the output format is assumed to
381 be the same as the input file's format.
382
383.. option:: --prefix-alloc-sections <prefix>
384
385 Add ``<prefix>`` to the front of the names of all allocatable sections in the
386 output.
387
388.. option:: --prefix-symbols <prefix>
389
390 Add ``<prefix>`` to the front of every symbol name in the output.
391
392.. option:: --preserve-dates, -p
393
394 Preserve access and modification timestamps in the output.
395
James Hendersone26ca962019-07-03 14:21:48 +0000396.. option:: --rename-section <old>=<new>[,<flag>,...]
397
398 Rename sections called ``<old>`` to ``<new>`` in the output, and apply any
399 specified ``<flag>`` values. See :option:`--set-section-flags` for a list of
400 supported flags. Can be specified multiple times to rename multiple sections.
401
402.. option:: --set-section-flags <section>=<flag>[,<flag>,...]
403
404 Set section properties in the output of section ``<section>`` based on the
405 specified ``<flag>`` values. Can be specified multiple times to update multiple
406 sections.
407
408 Following is a list of supported flags and their effects:
409
410 - `alloc` = add the `SHF_ALLOC` flag.
411 - `load` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS`
412 section.
413 - `readonly` = if this flag is not specified, add the `SHF_WRITE` flag.
Sergey Dmitrieve4463222020-01-20 17:06:03 -0800414 - `exclude` = add the `SHF_EXCLUDE` flag.
James Hendersone26ca962019-07-03 14:21:48 +0000415 - `code` = add the `SHF_EXECINSTR` flag.
416 - `merge` = add the `SHF_MERGE` flag.
417 - `strings` = add the `SHF_STRINGS` flag.
418 - `contents` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS`
419 section.
420
421 The following flags are also accepted, but are ignored for GNU compatibility:
422 `noload`, `debug`, `data`, `rom`, `share`.
423
424.. option:: --set-start-addr <addr>
425
426 Set the start address of the output to ``<addr>``. Overrides any previously
427 specified :option:`--change-start` or :option:`--adjust-start` options.
428
429.. option:: --split-dwo <dwo-file>
430
431 Equivalent to running :program:`llvm-objcopy` with :option:`--extract-dwo` and
432 ``<dwo-file>`` as the output file and no other options, and then with
433 :option:`--strip-dwo` on the input file.
434
435.. option:: --strip-dwo
436
437 Remove all DWARF .dwo sections from the output.
438
439.. option:: --strip-non-alloc
440
441 Remove from the output all non-allocatable sections that are not within
442 segments.
443
444.. option:: --strip-sections
445
446 Remove from the output all section headers and all section data not within
447 segments. Note that many tools will not be able to use an object without
448 section headers.
449
450.. option:: --target <format>, -F
451
452 Equivalent to :option:`--input-target` and :option:`--output-target` for the
453 specified format. See `SUPPORTED FORMATS`_ for a list of valid ``<format>``
454 values.
455
456.. option:: --weaken-symbol <symbol>, -W
457
458 Mark any global symbol named ``<symbol>`` as a weak symbol in the output. Can
459 be specified multiple times to mark multiple symbols as weak.
460
461.. option:: --weaken-symbols <filename>
462
463 Read a list of names from the file ``<filename>`` and mark global symbols with
464 those names as weak in the output. In the file, each line represents a single
465 symbol, with leading and trailing whitespace ignored, as is anything following
466 a '#'. Can be specified multiple times to read names from multiple files.
467
468.. option:: --weaken
469
470 Mark all defined global symbols as weak in the output.
471
472SUPPORTED FORMATS
473-----------------
474
475The following values are currently supported by :program:`llvm-objcopy` for the
476:option:`--input-target`, :option:`--output-target`, and :option:`--target`
477options. For GNU :program:`objcopy` compatibility, the values are all bfdnames.
478
479- `binary`
480- `ihex`
481- `elf32-i386`
482- `elf32-x86-64`
483- `elf64-x86-64`
484- `elf32-iamcu`
485- `elf32-littlearm`
486- `elf64-aarch64`
487- `elf64-littleaarch64`
488- `elf32-littleriscv`
489- `elf64-littleriscv`
490- `elf32-powerpc`
491- `elf32-powerpcle`
492- `elf64-powerpc`
493- `elf64-powerpcle`
494- `elf32-bigmips`
495- `elf32-ntradbigmips`
496- `elf32-ntradlittlemips`
497- `elf32-tradbigmips`
498- `elf32-tradlittlemips`
499- `elf64-tradbigmips`
500- `elf64-tradlittlemips`
501- `elf32-sparc`
502- `elf32-sparcel`
503
James Henderson8cf99a112019-07-08 11:41:54 +0000504Additionally, all targets except `binary` and `ihex` can have `-freebsd` as a
505suffix.
506
507BINARY INPUT AND OUTPUT
508-----------------------
509
510If `binary` is used as the value for :option:`--input-target`, the input file
511will be embedded as a data section in an ELF relocatable object, with symbols
512``_binary_<file_name>_start``, ``_binary_<file_name>_end``, and
513``_binary_<file_name>_size`` representing the start, end and size of the data,
514where ``<file_name>`` is the path of the input file as specified on the command
515line with non-alphanumeric characters converted to ``_``.
516
517If `binary` is used as the value for :option:`--output-target`, the output file
518will be a raw binary file, containing the memory image of the input file.
519Symbols and relocation information will be discarded. The image will start at
520the address of the first loadable section in the output.
James Hendersone26ca962019-07-03 14:21:48 +0000521
522EXIT STATUS
523-----------
524
525:program:`llvm-objcopy` exits with a non-zero exit code if there is an error.
526Otherwise, it exits with code 0.
527
528BUGS
529----
530
531To report bugs, please visit <http://llvm.org/bugs/>.
532
533There is a known issue with :option:`--input-target` and :option:`--target`
534causing only ``binary`` and ``ihex`` formats to have any effect. Other values
535will be ignored and :program:`llvm-objcopy` will attempt to guess the input
536format.
537
538SEE ALSO
539--------
540
541:manpage:`llvm-strip(1)`