llvm-objcopy.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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:: --strip-all-gnu
  55. Remove all symbols, debug sections and relocations from the output. This option
  56. is equivalent to GNU :program:`objcopy`'s ``--strip-all`` switch.
  57. .. option:: --strip-all, -S
  58. For ELF objects, remove from the output all symbols and non-alloc sections not
  59. within segments, except for .gnu.warning sections and the section name table.
  60. For COFF objects, remove all symbols, debug sections, and relocations from the
  61. output.
  62. .. option:: --strip-debug, -g
  63. Remove all debug sections from the output.
  64. .. option:: --strip-symbol <symbol>, -N
  65. Remove all symbols named ``<symbol>`` from the output. Can be specified
  66. multiple times to remove multiple symbols.
  67. .. option:: --strip-symbols <filename>
  68. Remove all symbols whose names appear in the file ``<filename>``, from the
  69. output. In the file, each line represents a single symbol name, with leading
  70. and trailing whitespace ignored, as is anything following a '#'. Can be
  71. specified multiple times to read names from multiple files.
  72. .. option:: --strip-unneeded-symbol <symbol>
  73. Remove from the output all symbols named ``<symbol>`` that are local or
  74. undefined and are not required by any relocation.
  75. .. option:: --strip-unneeded-symbols <filename>
  76. Remove all symbols whose names appear in the file ``<filename>``, from the
  77. output, if they are local or undefined and are not required by any relocation.
  78. In the file, each line represents a single symbol name, with leading and
  79. trailing whitespace ignored, as is anything following a '#'. Can be specified
  80. multiple times to read names from multiple files.
  81. .. option:: --strip-unneeded
  82. Remove from the output all local or undefined symbols that are not required by
  83. relocations. Also remove all debug sections.
  84. .. option:: --version, -V
  85. Display the version of the :program:`llvm-objcopy` executable.
  86. .. option:: @<FILE>
  87. Read command-line options and commands from response file `<FILE>`.
  88. COFF-SPECIFIC OPTIONS
  89. ---------------------
  90. The following options are implemented only for COFF objects. If used with other
  91. objects, :program:`llvm-objcopy` will either emit an error or silently ignore
  92. them.
  93. .. option:: --only-keep-debug
  94. Remove the contents of non-debug sections from the output, but keep the section
  95. headers.
  96. ELF-SPECIFIC OPTIONS
  97. --------------------
  98. The following options are implemented only for ELF objects. If used with other
  99. objects, :program:`llvm-objcopy` will either emit an error or silently ignore
  100. them.
  101. .. option:: --add-symbol <name>=[<section>:]<value>[,<flags>]
  102. Add a new symbol called ``<name>`` to the output symbol table, in the section
  103. named ``<section>``, with value ``<value>``. If ``<section>`` is not specified,
  104. the symbol is added as an absolute symbol. The ``<flags>`` affect the symbol
  105. properties. Accepted values are:
  106. - `global` = the symbol will have global binding.
  107. - `local` = the symbol will have local binding.
  108. - `weak` = the symbol will have weak binding.
  109. - `default` = the symbol will have default visibility.
  110. - `hidden` = the symbol will have hidden visibility.
  111. - `protected` = the symbol will have protected visibility.
  112. - `file` = the symbol will be an `STT_FILE` symbol.
  113. - `section` = the symbol will be an `STT_SECTION` symbol.
  114. - `object` = the symbol will be an `STT_OBJECT` symbol.
  115. - `function` = the symbol will be an `STT_FUNC` symbol.
  116. - `indirect-function` = the symbol will be an `STT_GNU_IFUNC` symbol.
  117. Additionally, the following flags are accepted but ignored: `debug`,
  118. `constructor`, `warning`, `indirect`, `synthetic`, `unique-object`, `before`.
  119. Can be specified multiple times to add multiple symbols.
  120. .. option:: --allow-broken-links
  121. Allow llvm-objcopy to remove sections even if it would leave invalid section
  122. references. Any invalid sh_link fields will be set to zero.
  123. .. option:: --build-id-link-dir <dir>
  124. Set the directory used by :option:`--build-id-link-input` and
  125. :option:`--build-id-link-output`.
  126. .. option:: --build-id-link-input <suffix>
  127. Hard-link the input to ``<dir>/xx/xxx<suffix>``, where ``<dir>`` is the directory
  128. specified by :option:`--build-id-link-dir`. The path used is derived from the
  129. hex build ID.
  130. .. option:: --build-id-link-output <suffix>
  131. Hard-link the output to ``<dir>/xx/xxx<suffix>``, where ``<dir>`` is the directory
  132. specified by :option:`--build-id-link-dir`. The path used is derived from the
  133. hex build ID.
  134. .. option:: --change-start <incr>, --adjust-start
  135. Add ``<incr>`` to the program's start address. Can be specified multiple
  136. times, in which case the values will be applied cumulatively.
  137. .. option:: --compress-debug-sections [<style>]
  138. Compress DWARF debug sections in the output, using the specified style.
  139. Supported styles are `zlib-gnu` and `zlib`. Defaults to `zlib` if no style is
  140. specified.
  141. .. option:: --decompress-debug-sections
  142. Decompress any compressed DWARF debug sections in the output.
  143. .. option:: --discard-locals, -X
  144. Remove local symbols starting with ".L" from the output.
  145. .. option:: --dump-section <section>=<file>
  146. Dump the contents of section ``<section>`` into the file ``<file>``. Can be
  147. specified multiple times to dump multiple sections to different files.
  148. ``<file>`` is unrelated to the input and output files provided to
  149. :program:`llvm-objcopy` and as such the normal copying and editing
  150. operations will still be performed. No operations are performed on the sections
  151. prior to dumping them.
  152. .. option:: --extract-dwo
  153. Remove all sections that are not DWARF .dwo sections from the output.
  154. .. option:: --extract-main-partition
  155. Extract the main partition from the output.
  156. .. option:: --extract-partition <name>
  157. Extract the named partition from the output.
  158. .. option:: --globalize-symbol <symbol>
  159. Mark any defined symbols named ``<symbol>`` as global symbols in the output.
  160. Can be specified multiple times to mark multiple symbols.
  161. .. option:: --globalize-symbols <filename>
  162. Read a list of names from the file ``<filename>`` and mark defined symbols with
  163. those names as global in the output. In the file, each line represents a single
  164. symbol, with leading and trailing whitespace ignored, as is anything following
  165. a '#'. Can be specified multiple times to read names from multiple files.
  166. .. option:: --input-target <format>, -I
  167. Read the input as the specified format. See `SUPPORTED FORMATS`_ for a list of
  168. valid ``<format>`` values. If unspecified, :program:`llvm-objcopy` will attempt
  169. to determine the format automatically.
  170. .. option:: --keep-file-symbols
  171. Keep symbols of type `STT_FILE`, even if they would otherwise be stripped.
  172. .. option:: --keep-global-symbol <symbol>
  173. Make all symbols local in the output, except for symbols with the name
  174. ``<symbol>``. Can be specified multiple times to ignore multiple symbols.
  175. .. option:: --keep-global-symbols <filename>
  176. Make all symbols local in the output, except for symbols named in the file
  177. ``<filename>``. In the file, each line represents a single symbol, with leading
  178. and trailing whitespace ignored, as is anything following a '#'. Can be
  179. specified multiple times to read names from multiple files.
  180. .. option:: --keep-section <section>
  181. When removing sections from the output, do not remove sections named
  182. ``<section>``. Can be specified multiple times to keep multiple sections.
  183. .. option:: --keep-symbol <symbol>, -K
  184. When removing symbols from the output, do not remove symbols named
  185. ``<symbol>``. Can be specified multiple times to keep multiple symbols.
  186. .. option:: --keep-symbols <filename>
  187. When removing symbols from the output do not remove symbols named in the file
  188. ``<filename>``. In the file, each line represents a single symbol, with leading
  189. and trailing whitespace ignored, as is anything following a '#'. Can be
  190. specified multiple times to read names from multiple files.
  191. .. option:: --localize-hidden
  192. Make all symbols with hidden or internal visibility local in the output.
  193. .. option:: --localize-symbol <symbol>, -L
  194. Mark any defined non-common symbol named ``<symbol>`` as a local symbol in the
  195. output. Can be specified multiple times to mark multiple symbols as local.
  196. .. option:: --localize-symbols <filename>
  197. Read a list of names from the file ``<filename>`` and mark defined non-common
  198. symbols with those names as local in the output. In the file, each line
  199. represents a single symbol, with leading and trailing whitespace ignored, as is
  200. anything following a '#'. Can be specified multiple times to read names from
  201. multiple files.
  202. .. option:: --new-symbol-visibility <visibility>
  203. Specify the visibility of the symbols automatically created when using binary
  204. input or :option:`--add-symbol`. Valid options are:
  205. - `default`
  206. - `hidden`
  207. - `internal`
  208. - `protected`
  209. The default is `default`.
  210. .. option:: --output-target <format>, -O
  211. Write the output as the specified format. See `SUPPORTED FORMATS`_ for a list
  212. of valid ``<format>`` values. If unspecified, the output format is assumed to
  213. be the same as the input file's format.
  214. .. option:: --prefix-alloc-sections <prefix>
  215. Add ``<prefix>`` to the front of the names of all allocatable sections in the
  216. output.
  217. .. option:: --prefix-symbols <prefix>
  218. Add ``<prefix>`` to the front of every symbol name in the output.
  219. .. option:: --preserve-dates, -p
  220. Preserve access and modification timestamps in the output.
  221. .. option:: --redefine-sym <old>=<new>
  222. Rename symbols called ``<old>`` to ``<new>`` in the output. Can be specified
  223. multiple times to rename multiple symbols.
  224. .. option:: --redefine-syms <filename>
  225. Rename symbols in the output as described in the file ``<filename>``. In the
  226. file, each line represents a single symbol to rename, with the old name and new
  227. name separated by an equals sign. Leading and trailing whitespace is ignored,
  228. as is anything following a '#'. Can be specified multiple times to read names
  229. from multiple files.
  230. .. option:: --rename-section <old>=<new>[,<flag>,...]
  231. Rename sections called ``<old>`` to ``<new>`` in the output, and apply any
  232. specified ``<flag>`` values. See :option:`--set-section-flags` for a list of
  233. supported flags. Can be specified multiple times to rename multiple sections.
  234. .. option:: --set-section-flags <section>=<flag>[,<flag>,...]
  235. Set section properties in the output of section ``<section>`` based on the
  236. specified ``<flag>`` values. Can be specified multiple times to update multiple
  237. sections.
  238. Following is a list of supported flags and their effects:
  239. - `alloc` = add the `SHF_ALLOC` flag.
  240. - `load` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS`
  241. section.
  242. - `readonly` = if this flag is not specified, add the `SHF_WRITE` flag.
  243. - `code` = add the `SHF_EXECINSTR` flag.
  244. - `merge` = add the `SHF_MERGE` flag.
  245. - `strings` = add the `SHF_STRINGS` flag.
  246. - `contents` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS`
  247. section.
  248. The following flags are also accepted, but are ignored for GNU compatibility:
  249. `noload`, `debug`, `data`, `rom`, `share`.
  250. .. option:: --set-start-addr <addr>
  251. Set the start address of the output to ``<addr>``. Overrides any previously
  252. specified :option:`--change-start` or :option:`--adjust-start` options.
  253. .. option:: --split-dwo <dwo-file>
  254. Equivalent to running :program:`llvm-objcopy` with :option:`--extract-dwo` and
  255. ``<dwo-file>`` as the output file and no other options, and then with
  256. :option:`--strip-dwo` on the input file.
  257. .. option:: --strip-dwo
  258. Remove all DWARF .dwo sections from the output.
  259. .. option:: --strip-non-alloc
  260. Remove from the output all non-allocatable sections that are not within
  261. segments.
  262. .. option:: --strip-sections
  263. Remove from the output all section headers and all section data not within
  264. segments. Note that many tools will not be able to use an object without
  265. section headers.
  266. .. option:: --target <format>, -F
  267. Equivalent to :option:`--input-target` and :option:`--output-target` for the
  268. specified format. See `SUPPORTED FORMATS`_ for a list of valid ``<format>``
  269. values.
  270. .. option:: --weaken-symbol <symbol>, -W
  271. Mark any global symbol named ``<symbol>`` as a weak symbol in the output. Can
  272. be specified multiple times to mark multiple symbols as weak.
  273. .. option:: --weaken-symbols <filename>
  274. Read a list of names from the file ``<filename>`` and mark global symbols with
  275. those names as weak in the output. In the file, each line represents a single
  276. symbol, with leading and trailing whitespace ignored, as is anything following
  277. a '#'. Can be specified multiple times to read names from multiple files.
  278. .. option:: --weaken
  279. Mark all defined global symbols as weak in the output.
  280. SUPPORTED FORMATS
  281. -----------------
  282. The following values are currently supported by :program:`llvm-objcopy` for the
  283. :option:`--input-target`, :option:`--output-target`, and :option:`--target`
  284. options. For GNU :program:`objcopy` compatibility, the values are all bfdnames.
  285. - `binary`
  286. - `ihex`
  287. - `elf32-i386`
  288. - `elf32-x86-64`
  289. - `elf64-x86-64`
  290. - `elf32-iamcu`
  291. - `elf32-littlearm`
  292. - `elf64-aarch64`
  293. - `elf64-littleaarch64`
  294. - `elf32-littleriscv`
  295. - `elf64-littleriscv`
  296. - `elf32-powerpc`
  297. - `elf32-powerpcle`
  298. - `elf64-powerpc`
  299. - `elf64-powerpcle`
  300. - `elf32-bigmips`
  301. - `elf32-ntradbigmips`
  302. - `elf32-ntradlittlemips`
  303. - `elf32-tradbigmips`
  304. - `elf32-tradlittlemips`
  305. - `elf64-tradbigmips`
  306. - `elf64-tradlittlemips`
  307. - `elf32-sparc`
  308. - `elf32-sparcel`
  309. Additionally, all targets except `binary` and `ihex` can have `-freebsd` as a
  310. suffix.
  311. BINARY INPUT AND OUTPUT
  312. -----------------------
  313. If `binary` is used as the value for :option:`--input-target`, the input file
  314. will be embedded as a data section in an ELF relocatable object, with symbols
  315. ``_binary_<file_name>_start``, ``_binary_<file_name>_end``, and
  316. ``_binary_<file_name>_size`` representing the start, end and size of the data,
  317. where ``<file_name>`` is the path of the input file as specified on the command
  318. line with non-alphanumeric characters converted to ``_``.
  319. If `binary` is used as the value for :option:`--output-target`, the output file
  320. will be a raw binary file, containing the memory image of the input file.
  321. Symbols and relocation information will be discarded. The image will start at
  322. the address of the first loadable section in the output.
  323. EXIT STATUS
  324. -----------
  325. :program:`llvm-objcopy` exits with a non-zero exit code if there is an error.
  326. Otherwise, it exits with code 0.
  327. BUGS
  328. ----
  329. To report bugs, please visit <http://llvm.org/bugs/>.
  330. There is a known issue with :option:`--input-target` and :option:`--target`
  331. causing only ``binary`` and ``ihex`` formats to have any effect. Other values
  332. will be ignored and :program:`llvm-objcopy` will attempt to guess the input
  333. format.
  334. SEE ALSO
  335. --------
  336. :manpage:`llvm-strip(1)`