llvm-objcopy.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. llvm-objcopy - object copying and editing tool
  2. ==============================================
  3. .. program:: llvm-objcopy
  4. SYNOPSIS
  5. --------
  6. :program:`llvm-objcopy` [*options*] *input* [*output*]
  7. DESCRIPTION
  8. -----------
  9. :program:`llvm-objcopy` is a tool to copy and manipulate objects. In basic
  10. usage, it makes a semantic copy of the input to the output. If any options are
  11. specified, the output may be modified along the way, e.g. by removing sections.
  12. If no output file is specified, the input file is modified in-place. If "-" is
  13. specified for the input file, the input is read from the program's standard
  14. input stream. If "-" is specified for the output file, the output is written to
  15. the standard output stream of the program.
  16. If the input is an archive, any requested operations will be applied to each
  17. archive member individually.
  18. The tool is still in active development, but in most scenarios it works as a
  19. drop-in replacement for GNU's :program:`objcopy`.
  20. GENERIC AND CROSS-PLATFORM OPTIONS
  21. ----------------------------------
  22. The following options are either agnostic of the file format, or apply to
  23. multiple file formats.
  24. .. option:: --add-gnu-debuglink <debug-file>
  25. Add a .gnu_debuglink section for ``<debug-file>`` to the output.
  26. .. option:: --add-section <section=file>
  27. Add a section named ``<section>`` with the contents of ``<file>`` to the
  28. output. For ELF objects the section will be of type `SHT_NOTE`, if the name
  29. starts with ".note". Otherwise, it will have type `SHT_PROGBITS`. Can be
  30. specified multiple times to add multiple sections.
  31. .. option:: --binary-architecture <arch>, -B
  32. Ignored for compatibility.
  33. .. option:: --disable-deterministic-archives, -U
  34. Use real values for UIDs, GIDs and timestamps when updating archive member
  35. headers.
  36. .. option:: --discard-all, -x
  37. Remove most local symbols from the output. Different file formats may limit
  38. this to a subset of the local symbols. For example, file and section symbols in
  39. ELF objects will not be discarded.
  40. .. option:: --enable-deterministic-archives, -D
  41. Enable deterministic mode when copying archives, i.e. use 0 for archive member
  42. header UIDs, GIDs and timestamp fields. On by default.
  43. .. option:: --help, -h
  44. Print a summary of command line options.
  45. .. option:: --only-section <section>, -j
  46. Remove all sections from the output, except for sections named ``<section>``.
  47. Can be specified multiple times to keep multiple sections.
  48. .. option:: --regex
  49. If specified, symbol and section names specified by other switches are treated
  50. as extended POSIX regular expression patterns.
  51. .. option:: --remove-section <section>, -R
  52. Remove the specified section from the output. Can be specified multiple times
  53. to remove multiple sections simultaneously.
  54. .. option:: --set-section-alignment <section>=<align>
  55. Set the alignment of section ``<section>`` to `<align>``. Can be specified
  56. multiple times to update multiple sections.
  57. .. option:: --strip-all-gnu
  58. Remove all symbols, debug sections and relocations from the output. This option
  59. is equivalent to GNU :program:`objcopy`'s ``--strip-all`` switch.
  60. .. option:: --strip-all, -S
  61. For ELF objects, remove from the output all symbols and non-alloc sections not
  62. within segments, except for .gnu.warning sections and the section name table.
  63. For COFF objects, remove all symbols, debug sections, and relocations from the
  64. output.
  65. .. option:: --strip-debug, -g
  66. Remove all debug sections from the output.
  67. .. option:: --strip-symbol <symbol>, -N
  68. Remove all symbols named ``<symbol>`` from the output. Can be specified
  69. multiple times to remove multiple symbols.
  70. .. option:: --strip-symbols <filename>
  71. Remove all symbols whose names appear in the file ``<filename>``, from the
  72. output. In the file, each line represents a single symbol name, with leading
  73. and trailing whitespace ignored, as is anything following a '#'. Can be
  74. specified multiple times to read names from multiple files.
  75. .. option:: --strip-unneeded-symbol <symbol>
  76. Remove from the output all symbols named ``<symbol>`` that are local or
  77. undefined and are not required by any relocation.
  78. .. option:: --strip-unneeded-symbols <filename>
  79. Remove all symbols whose names appear in the file ``<filename>``, from the
  80. output, if they are local or undefined and are not required by any relocation.
  81. In the file, each line represents a single symbol name, with leading and
  82. trailing whitespace ignored, as is anything following a '#'. Can be specified
  83. multiple times to read names from multiple files.
  84. .. option:: --strip-unneeded
  85. Remove from the output all local or undefined symbols that are not required by
  86. relocations. Also remove all debug sections.
  87. .. option:: --version, -V
  88. Display the version of the :program:`llvm-objcopy` executable.
  89. .. option:: @<FILE>
  90. Read command-line options and commands from response file `<FILE>`.
  91. .. option:: --wildcard, -w
  92. Allow wildcard syntax for symbol-related flags. On by default for
  93. section-related flags. Incompatible with --regex.
  94. Wildcard syntax allows the following special symbols:
  95. ====================== ========================= ==================
  96. Character Meaning Equivalent
  97. ====================== ========================= ==================
  98. ``*`` Any number of characters ``.*``
  99. ``?`` Any single character ``.``
  100. ``\`` Escape the next character ``\``
  101. ``[a-z]`` Character class ``[a-z]``
  102. ``[!a-z]``, ``[^a-z]`` Negated character class ``[^a-z]``
  103. ====================== ========================= ==================
  104. Additionally, starting a wildcard with '!' will prevent a match, even if
  105. another flag matches. For example ``-w -N '*' -N '!x'`` will strip all symbols
  106. except for ``x``.
  107. The order of wildcards does not matter. For example, ``-w -N '*' -N '!x'`` is
  108. the same as ``-w -N '!x' -N '*'``.
  109. COFF-SPECIFIC OPTIONS
  110. ---------------------
  111. The following options are implemented only for COFF objects. If used with other
  112. objects, :program:`llvm-objcopy` will either emit an error or silently ignore
  113. them.
  114. .. option:: --only-keep-debug
  115. Remove the contents of non-debug sections from the output, but keep the section
  116. headers.
  117. ELF-SPECIFIC OPTIONS
  118. --------------------
  119. The following options are implemented only for ELF objects. If used with other
  120. objects, :program:`llvm-objcopy` will either emit an error or silently ignore
  121. them.
  122. .. option:: --add-symbol <name>=[<section>:]<value>[,<flags>]
  123. Add a new symbol called ``<name>`` to the output symbol table, in the section
  124. named ``<section>``, with value ``<value>``. If ``<section>`` is not specified,
  125. the symbol is added as an absolute symbol. The ``<flags>`` affect the symbol
  126. properties. Accepted values are:
  127. - `global` = the symbol will have global binding.
  128. - `local` = the symbol will have local binding.
  129. - `weak` = the symbol will have weak binding.
  130. - `default` = the symbol will have default visibility.
  131. - `hidden` = the symbol will have hidden visibility.
  132. - `protected` = the symbol will have protected visibility.
  133. - `file` = the symbol will be an `STT_FILE` symbol.
  134. - `section` = the symbol will be an `STT_SECTION` symbol.
  135. - `object` = the symbol will be an `STT_OBJECT` symbol.
  136. - `function` = the symbol will be an `STT_FUNC` symbol.
  137. - `indirect-function` = the symbol will be an `STT_GNU_IFUNC` symbol.
  138. Additionally, the following flags are accepted but ignored: `debug`,
  139. `constructor`, `warning`, `indirect`, `synthetic`, `unique-object`, `before`.
  140. Can be specified multiple times to add multiple symbols.
  141. .. option:: --allow-broken-links
  142. Allow :program:`llvm-objcopy` to remove sections even if it would leave invalid
  143. section references. Any invalid sh_link fields will be set to zero.
  144. .. option:: --build-id-link-dir <dir>
  145. Set the directory used by :option:`--build-id-link-input` and
  146. :option:`--build-id-link-output`.
  147. .. option:: --build-id-link-input <suffix>
  148. Hard-link the input to ``<dir>/xx/xxx<suffix>``, where ``<dir>`` is the directory
  149. specified by :option:`--build-id-link-dir`. The path used is derived from the
  150. hex build ID.
  151. .. option:: --build-id-link-output <suffix>
  152. Hard-link the output to ``<dir>/xx/xxx<suffix>``, where ``<dir>`` is the directory
  153. specified by :option:`--build-id-link-dir`. The path used is derived from the
  154. hex build ID.
  155. .. option:: --change-start <incr>, --adjust-start
  156. Add ``<incr>`` to the program's start address. Can be specified multiple
  157. times, in which case the values will be applied cumulatively.
  158. .. option:: --compress-debug-sections [<style>]
  159. Compress DWARF debug sections in the output, using the specified style.
  160. Supported styles are `zlib-gnu` and `zlib`. Defaults to `zlib` if no style is
  161. specified.
  162. .. option:: --decompress-debug-sections
  163. Decompress any compressed DWARF debug sections in the output.
  164. .. option:: --discard-locals, -X
  165. Remove local symbols starting with ".L" from the output.
  166. .. option:: --dump-section <section>=<file>
  167. Dump the contents of section ``<section>`` into the file ``<file>``. Can be
  168. specified multiple times to dump multiple sections to different files.
  169. ``<file>`` is unrelated to the input and output files provided to
  170. :program:`llvm-objcopy` and as such the normal copying and editing
  171. operations will still be performed. No operations are performed on the sections
  172. prior to dumping them.
  173. .. option:: --extract-dwo
  174. Remove all sections that are not DWARF .dwo sections from the output.
  175. .. option:: --extract-main-partition
  176. Extract the main partition from the output.
  177. .. option:: --extract-partition <name>
  178. Extract the named partition from the output.
  179. .. option:: --globalize-symbol <symbol>
  180. Mark any defined symbols named ``<symbol>`` as global symbols in the output.
  181. Can be specified multiple times to mark multiple symbols.
  182. .. option:: --globalize-symbols <filename>
  183. Read a list of names from the file ``<filename>`` and mark defined symbols with
  184. those names as global in the output. In the file, each line represents a single
  185. symbol, with leading and trailing whitespace ignored, as is anything following
  186. a '#'. Can be specified multiple times to read names from multiple files.
  187. .. option:: --input-target <format>, -I
  188. Read the input as the specified format. See `SUPPORTED FORMATS`_ for a list of
  189. valid ``<format>`` values. If unspecified, :program:`llvm-objcopy` will attempt
  190. to determine the format automatically.
  191. .. option:: --keep-file-symbols
  192. Keep symbols of type `STT_FILE`, even if they would otherwise be stripped.
  193. .. option:: --keep-global-symbol <symbol>
  194. Make all symbols local in the output, except for symbols with the name
  195. ``<symbol>``. Can be specified multiple times to ignore multiple symbols.
  196. .. option:: --keep-global-symbols <filename>
  197. Make all symbols local in the output, except for symbols named in the file
  198. ``<filename>``. In the file, each line represents a single symbol, with leading
  199. and trailing whitespace ignored, as is anything following a '#'. Can be
  200. specified multiple times to read names from multiple files.
  201. .. option:: --keep-section <section>
  202. When removing sections from the output, do not remove sections named
  203. ``<section>``. Can be specified multiple times to keep multiple sections.
  204. .. option:: --keep-symbol <symbol>, -K
  205. When removing symbols from the output, do not remove symbols named
  206. ``<symbol>``. Can be specified multiple times to keep multiple symbols.
  207. .. option:: --keep-symbols <filename>
  208. When removing symbols from the output do not remove symbols named in the file
  209. ``<filename>``. In the file, each line represents a single symbol, with leading
  210. and trailing whitespace ignored, as is anything following a '#'. Can be
  211. specified multiple times to read names from multiple files.
  212. .. option:: --localize-hidden
  213. Make all symbols with hidden or internal visibility local in the output.
  214. .. option:: --localize-symbol <symbol>, -L
  215. Mark any defined non-common symbol named ``<symbol>`` as a local symbol in the
  216. output. Can be specified multiple times to mark multiple symbols as local.
  217. .. option:: --localize-symbols <filename>
  218. Read a list of names from the file ``<filename>`` and mark defined non-common
  219. symbols with those names as local in the output. In the file, each line
  220. represents a single symbol, with leading and trailing whitespace ignored, as is
  221. anything following a '#'. Can be specified multiple times to read names from
  222. multiple files.
  223. .. option:: --new-symbol-visibility <visibility>
  224. Specify the visibility of the symbols automatically created when using binary
  225. input or :option:`--add-symbol`. Valid options are:
  226. - `default`
  227. - `hidden`
  228. - `internal`
  229. - `protected`
  230. The default is `default`.
  231. .. option:: --output-target <format>, -O
  232. Write the output as the specified format. See `SUPPORTED FORMATS`_ for a list
  233. of valid ``<format>`` values. If unspecified, the output format is assumed to
  234. be the same as the input file's format.
  235. .. option:: --prefix-alloc-sections <prefix>
  236. Add ``<prefix>`` to the front of the names of all allocatable sections in the
  237. output.
  238. .. option:: --prefix-symbols <prefix>
  239. Add ``<prefix>`` to the front of every symbol name in the output.
  240. .. option:: --preserve-dates, -p
  241. Preserve access and modification timestamps in the output.
  242. .. option:: --redefine-sym <old>=<new>
  243. Rename symbols called ``<old>`` to ``<new>`` in the output. Can be specified
  244. multiple times to rename multiple symbols.
  245. .. option:: --redefine-syms <filename>
  246. Rename symbols in the output as described in the file ``<filename>``. In the
  247. file, each line represents a single symbol to rename, with the old name and new
  248. name separated by an equals sign. Leading and trailing whitespace is ignored,
  249. as is anything following a '#'. Can be specified multiple times to read names
  250. from multiple files.
  251. .. option:: --rename-section <old>=<new>[,<flag>,...]
  252. Rename sections called ``<old>`` to ``<new>`` in the output, and apply any
  253. specified ``<flag>`` values. See :option:`--set-section-flags` for a list of
  254. supported flags. Can be specified multiple times to rename multiple sections.
  255. .. option:: --set-section-flags <section>=<flag>[,<flag>,...]
  256. Set section properties in the output of section ``<section>`` based on the
  257. specified ``<flag>`` values. Can be specified multiple times to update multiple
  258. sections.
  259. Following is a list of supported flags and their effects:
  260. - `alloc` = add the `SHF_ALLOC` flag.
  261. - `load` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS`
  262. section.
  263. - `readonly` = if this flag is not specified, add the `SHF_WRITE` flag.
  264. - `code` = add the `SHF_EXECINSTR` flag.
  265. - `merge` = add the `SHF_MERGE` flag.
  266. - `strings` = add the `SHF_STRINGS` flag.
  267. - `contents` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS`
  268. section.
  269. The following flags are also accepted, but are ignored for GNU compatibility:
  270. `noload`, `debug`, `data`, `rom`, `share`.
  271. .. option:: --set-start-addr <addr>
  272. Set the start address of the output to ``<addr>``. Overrides any previously
  273. specified :option:`--change-start` or :option:`--adjust-start` options.
  274. .. option:: --split-dwo <dwo-file>
  275. Equivalent to running :program:`llvm-objcopy` with :option:`--extract-dwo` and
  276. ``<dwo-file>`` as the output file and no other options, and then with
  277. :option:`--strip-dwo` on the input file.
  278. .. option:: --strip-dwo
  279. Remove all DWARF .dwo sections from the output.
  280. .. option:: --strip-non-alloc
  281. Remove from the output all non-allocatable sections that are not within
  282. segments.
  283. .. option:: --strip-sections
  284. Remove from the output all section headers and all section data not within
  285. segments. Note that many tools will not be able to use an object without
  286. section headers.
  287. .. option:: --target <format>, -F
  288. Equivalent to :option:`--input-target` and :option:`--output-target` for the
  289. specified format. See `SUPPORTED FORMATS`_ for a list of valid ``<format>``
  290. values.
  291. .. option:: --weaken-symbol <symbol>, -W
  292. Mark any global symbol named ``<symbol>`` as a weak symbol in the output. Can
  293. be specified multiple times to mark multiple symbols as weak.
  294. .. option:: --weaken-symbols <filename>
  295. Read a list of names from the file ``<filename>`` and mark global symbols with
  296. those names as weak in the output. In the file, each line represents a single
  297. symbol, with leading and trailing whitespace ignored, as is anything following
  298. a '#'. Can be specified multiple times to read names from multiple files.
  299. .. option:: --weaken
  300. Mark all defined global symbols as weak in the output.
  301. SUPPORTED FORMATS
  302. -----------------
  303. The following values are currently supported by :program:`llvm-objcopy` for the
  304. :option:`--input-target`, :option:`--output-target`, and :option:`--target`
  305. options. For GNU :program:`objcopy` compatibility, the values are all bfdnames.
  306. - `binary`
  307. - `ihex`
  308. - `elf32-i386`
  309. - `elf32-x86-64`
  310. - `elf64-x86-64`
  311. - `elf32-iamcu`
  312. - `elf32-littlearm`
  313. - `elf64-aarch64`
  314. - `elf64-littleaarch64`
  315. - `elf32-littleriscv`
  316. - `elf64-littleriscv`
  317. - `elf32-powerpc`
  318. - `elf32-powerpcle`
  319. - `elf64-powerpc`
  320. - `elf64-powerpcle`
  321. - `elf32-bigmips`
  322. - `elf32-ntradbigmips`
  323. - `elf32-ntradlittlemips`
  324. - `elf32-tradbigmips`
  325. - `elf32-tradlittlemips`
  326. - `elf64-tradbigmips`
  327. - `elf64-tradlittlemips`
  328. - `elf32-sparc`
  329. - `elf32-sparcel`
  330. Additionally, all targets except `binary` and `ihex` can have `-freebsd` as a
  331. suffix.
  332. BINARY INPUT AND OUTPUT
  333. -----------------------
  334. If `binary` is used as the value for :option:`--input-target`, the input file
  335. will be embedded as a data section in an ELF relocatable object, with symbols
  336. ``_binary_<file_name>_start``, ``_binary_<file_name>_end``, and
  337. ``_binary_<file_name>_size`` representing the start, end and size of the data,
  338. where ``<file_name>`` is the path of the input file as specified on the command
  339. line with non-alphanumeric characters converted to ``_``.
  340. If `binary` is used as the value for :option:`--output-target`, the output file
  341. will be a raw binary file, containing the memory image of the input file.
  342. Symbols and relocation information will be discarded. The image will start at
  343. the address of the first loadable section in the output.
  344. EXIT STATUS
  345. -----------
  346. :program:`llvm-objcopy` exits with a non-zero exit code if there is an error.
  347. Otherwise, it exits with code 0.
  348. BUGS
  349. ----
  350. To report bugs, please visit <http://llvm.org/bugs/>.
  351. There is a known issue with :option:`--input-target` and :option:`--target`
  352. causing only ``binary`` and ``ihex`` formats to have any effect. Other values
  353. will be ignored and :program:`llvm-objcopy` will attempt to guess the input
  354. format.
  355. SEE ALSO
  356. --------
  357. :manpage:`llvm-strip(1)`