OBJCOPY(1)

๐Ÿ’… OBJCOPY(1) โ€” GNU Development Tools ๐ŸŽ€

okay bestie, welcome to objcopy, the glow-up station for object files. it's part of the GNU Development Tools situation and it is lowkey one of the most powerful little utilities in your whole bin directory. buckle up, this one is long and we're doing it in parts. โœจ


๐ŸŒธ NAME ๐ŸŒธ

objcopy โ€” copy and translate object files. that's it. that's the job. copy them, translate them, make them โœจdifferentโœจ. ๐Ÿ˜Œ


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

the full vibe-check of every flag it accepts, no cap:

objcopy [-F bfdname|--target=bfdname]
        [-I bfdname|--input-target=bfdname]
        [-O bfdname|--output-target=bfdname]
        [-B bfdarch|--binary-architecture=bfdarch]
        [-S|--strip-all]
        [-g|--strip-debug]
        [--strip-unneeded]
        [-K symbolname|--keep-symbol=symbolname]
        [--keep-file-symbols]
        [--keep-section-symbols]
        [-N symbolname|--strip-symbol=symbolname]
        [--strip-unneeded-symbol=symbolname]
        [-G symbolname|--keep-global-symbol=symbolname]
        [--localize-hidden]
        [-L symbolname|--localize-symbol=symbolname]
        [--globalize-symbol=symbolname]
        [--globalize-symbols=filename]
        [-W symbolname|--weaken-symbol=symbolname]
        [-w|--wildcard]
        [-x|--discard-all]
        [-X|--discard-locals]
        [-b byte|--byte=byte]
        [-i [breadth]|--interleave[=breadth]]
        [--interleave-width=width]
        [-j sectionpattern|--only-section=sectionpattern]
        [-R sectionpattern|--remove-section=sectionpattern]
        [--keep-section=sectionpattern]
        [--remove-relocations=sectionpattern]
        [--strip-section-headers]
        [-p|--preserve-dates]
        [-D|--enable-deterministic-archives]
        [-U|--disable-deterministic-archives]
        [--debugging]
        [--gap-fill=val]
        [--pad-to=address]
        [--set-start=val]
        [--adjust-start=incr]
        [--change-addresses=incr]
        [--change-section-address sectionpattern{=,+,-}val]
        [--change-section-lma sectionpattern{=,+,-}val]
        [--change-section-vma sectionpattern{=,+,-}val]
        [--change-warnings] [--no-change-warnings]
        [--set-section-flags sectionpattern=flags]
        [--set-section-alignment sectionpattern=align]
        [--add-section sectionname=filename]
        [--dump-section sectionname=filename]
        [--update-section sectionname=filename]
        [--rename-section oldname=newname[,flags]]
        [--long-section-names {enable,disable,keep}]
        [--change-leading-char] [--remove-leading-char]
        [--reverse-bytes=num]
        [--srec-len=ival] [--srec-forceS3]
        [--redefine-sym old=new]
        [--redefine-syms=filename]
        [--weaken]
        [--keep-symbols=filename]
        [--strip-symbols=filename]
        [--strip-unneeded-symbols=filename]
        [--keep-global-symbols=filename]
        [--localize-symbols=filename]
        [--weaken-symbols=filename]
        [--binary-symbol-prefixstring]
        [--add-symbol name=[section:]value[,flags]]
        [--alt-machine-code=index]
        [--prefix-symbols=string]
        [--prefix-sections=string]
        [--prefix-alloc-sections=string]
        [--add-gnu-debuglink=path-to-file]
        [--only-keep-debug]
        [--strip-dwo]
        [--extract-dwo]
        [--extract-symbol]
        [--writable-text]
        [--readonly-text]
        [--pure]
        [--impure]
        [--file-alignment=num]
        [--heap=reserve[,commit]]
        [--image-base=address]
        [--section-alignment=num]
        [--stack=reserve[,commit]]
        [--subsystem=which:major.minor]
        [--compress-debug-sections]
        [--decompress-debug-sections]
        [--elf-stt-common=val]
        [--merge-notes]
        [--no-merge-notes]
        [--verilog-data-width=val]
        [-v|--verbose]
        [-V|--version]
        [--help] [--info]
        infile [outfile]

yes bestie, that's a lot of options. and yes, we're going through every single one. ๐Ÿ’€๐Ÿ’–


๐Ÿ”ฅ DESCRIPTION ๐Ÿ”ฅ

so the GNU objcopy utility copies the contents of an object file to another one. that's the whole concept. objcopy uses the GNU BFD Library to read the object files and to write them โ€” that's the glow-up engine under the hood. ๐Ÿงš

the iconic part: it can write the destination object file in a format different from the source's format. like, format change, main character move. the exact behavior of objcopy is controlled by command-line options โ€” so literally everything is your choice, you're the director of this movie. ๐Ÿ‘‘

ngl one tiny little caveat: objcopy should be able to copy a fully linked file between any two formats. but copying a relocatable object file between any two formats may not work as expected. not me hyping it up and then warning you. ๐Ÿ˜ญ

objcopy creates temporary files to do its translations and deletes them afterward. it uses BFD to do all its translation work, and since it has access to all the formats described in BFD, it can recognize most formats without you having to tell it explicitly. so smart, so self-sufficient. ๐Ÿ’…

objcopy can be used to generate S-records by using an output target of srec (e.g., use -O srec). slay.

objcopy can also be used to generate a raw binary file by using an output target of binary (e.g., use -O binary). when it generates a raw binary file, it will essentially produce a memory dump of the contents of the input object file. all symbols and relocation information will be discarded. the memory dump will start at the load address of the lowest section copied into the output file. important detail, don't forget it. ๐ŸŒˆ

when generating an S-record or a raw binary file, it may be helpful to use -S to remove sections containing debugging information. in some cases -R will be useful to remove sections which contain information that is not needed by the binary file. two little besties doing cleanup duty together. ๐Ÿซถ

note โ€” and this is important: objcopy is not able to change the endianness of its input files. if the input format has an endianness (some formats do not), objcopy can only copy the inputs into file formats that have the same endianness or which have no endianness (e.g., srec). (however, see the --reverse-bytes option.) so don't come for it about endianness, it simply refuses. ๐Ÿšซ

โœจ OPTIONS โœจ

okay here we go, the buffet. every option, every flag, no skipping. let's eat. ๐Ÿ“


๐ŸŒŸ The files themselves

  • infile
  • outfile โ€” the input and output files, respectively. if you do not specify outfile, objcopy creates a temporary file and destructively renames the result with the name of infile. so like, it will overwrite your input file. be normal about that. ๐Ÿ˜ณ

๐ŸŒŸ Format / target options

  • -I bfdname, --input-target=bfdname โ€” treat the source file's object format as bfdname, instead of trying to deduce it. you're telling it what's up. โœจ
  • -O bfdname, --output-target=bfdname โ€” write the output file using the object format bfdname. the glow-up target. ๐Ÿ’–
  • -F bfdname, --target=bfdname โ€” use bfdname as the object format for both the input and the output file. i.e., simply transfer data from source to destination with no translation. just a straight copy, no notes. ๐Ÿ˜Œ
  • -B bfdarch, --binary-architecture=bfdarch โ€” useful when transforming an architecture-less input file into an object file. in that case the output architecture can be set to bfdarch. this option will be ignored if the input file has a known bfdarch. ๐Ÿ˜ด

you can access this binary data inside a program by referencing the special symbols that are created by the conversion process. these symbols are called _binary_objfile_start, _binary_objfile_end and _binary_objfile_size. e.g. you can transform a picture file into an object file and then access it in your code using these symbols. iconic behavior tbh. ๐Ÿ–ผ๏ธ


๐ŸŒŸ Section wrangling

  • -j sectionpattern, --only-section=sectionpattern โ€” copy only the indicated sections from the input file to the output file. this option may be given more than once. note that using this option inappropriately may make the output file unusable. wildcard characters are accepted in sectionpattern. โš ๏ธ

if the first character of sectionpattern is the exclamation point (!) then matching sections will not be copied, even if an earlier use of --only-section on the same command line would otherwise copy it. for example:

--only-section=.text.* --only-section=!.text.foo

will copy all sections matching .text.* but not the section .text.foo.

  • -R sectionpattern, --remove-section=sectionpattern โ€” remove any section matching sectionpattern from the output file. this option may be given more than once. note that using this option inappropriately may make the output file unusable. wildcard characters are accepted in sectionpattern. using both the -j and -R options together results in undefined behaviour. ๐Ÿซฃ

if the first character of sectionpattern is the exclamation point (!) then matching sections will not be removed even if an earlier use of --remove-section on the same command line would otherwise remove it. for example:

--remove-section=.text.* --remove-section=!.text.foo

will remove all sections matching the pattern .text.*, but will not remove the section .text.foo.

  • --keep-section=sectionpattern โ€” when removing sections from the output file, keep sections that match sectionpattern. a little protection spell. ๐Ÿ›ก๏ธ
  • --remove-relocations=sectionpattern โ€” remove non-dynamic relocations from the output file for any section matching sectionpattern. this option may be given more than once. note that using this option inappropriately may make the output file unusable, and attempting to remove a dynamic relocation section such as .rela.plt from an executable or shared library with --remove-relocations=.plt will not work. wildcard characters are accepted in sectionpattern. for example:
--remove-relocations=.text.*

will remove the relocations for all sections matching the pattern .text.*.

if the first character of sectionpattern is the exclamation point (!) then matching sections will not have their relocation removed even if an earlier use of --remove-relocations on the same command line would otherwise cause the relocations to be removed. for example:

--remove-relocations=.text.* --remove-relocations=!.text.foo

will remove all relocations for sections matching the pattern .text.*, but will not remove relocations for the section .text.foo.

  • --strip-section-headers โ€” strip section header. this option is specific to ELF files. implies --strip-all and --merge-notes. so it's a two-for-one situation, we love a bundled deal. ๐Ÿ’ซ

๐ŸŒŸ Stripping (the big glow-down, in a good way)

  • -S, --strip-all โ€” do not copy relocation and symbol information from the source file. also deletes debug sections. clean slate energy. ๐Ÿงผ
  • -g, --strip-debug โ€” do not copy debugging symbols or sections from the source file. debug info? don't know her. ๐Ÿ’…
  • --strip-unneeded โ€” remove all symbols that are not needed for relocation processing, in addition to debugging symbols and sections stripped by --strip-debug. ruthless. obsessed. ๐Ÿ”ฅ

๐ŸŒŸ Symbol-level control

  • -K symbolname, --keep-symbol=symbolname โ€” when stripping symbols, keep symbol symbolname even if it would normally be stripped. this option may be given more than once. protecting your faves. ๐Ÿฅฐ
  • -N symbolname, --strip-symbol=symbolname โ€” do not copy symbol symbolname from the source file. this option may be given more than once. blocking them, respectfully. ๐Ÿšซ
  • --strip-unneeded-symbol=symbolname โ€” do not copy symbol symbolname from the source file unless it is needed by a relocation. this option may be given more than once. it's giving conditional friendship. ๐Ÿ˜ญ

and that's part one bestie!! we're not done, there is so much more objcopy to go through โ€” stay with me, the rest is coming. ๐Ÿฆ‹

  • -G symbolname โ€” --keep-global-symbol=symbolname โ€” bestie, this is the "only the VIP gets to stay global" option ๐Ÿ’… Keep only symbolname global, and everything else gets demoted to local so it's not visible outside the file. You can drop this option more than once if you've got multiple VIPs. ๐Ÿ‘‘
โš ๏ธ Heads up: this cannot be used together with --globalize-symbol or --globalize-symbols. Pick a lane, fr fr.
  • --localize-hidden โ€” in an ELF object, this marks every symbol rockin' hidden or internal visibility as local. And it stacks on top of the symbol-specific localization options like -L, so it's giving layered ๐Ÿงš
  • -L symbolname โ€” --localize-symbol=symbolname โ€” takes a global or weak symbol named symbolname and turns it into a local one, so the outside world can't see it. Repeatable, go off. ๐Ÿ“‰
Not me about to sayโ€ฆ unique symbols are not converted. Nope, not those ones. ๐Ÿ™ˆ
  • -W symbolname โ€” --weaken-symbol=symbolname โ€” makes symbolname weak. May be given more than once, so stack 'em up if you need to. ๐Ÿซถ
  • --binary-symbol-prefix=string โ€” when you're using --input-target=binary, this sets string as the base name for the symbols generated for the input file. Those symbols are:
<string>_start
<string>_end
<string>_size

By default, the binary input handler cooks up the prefix symbol name from a mangled version of the input file name plus the _binary prefix. This option lets you just say it explicitly instead. โœจ

  • --globalize-symbol=symbolname โ€” gives symbolname global scoping so it's visible outside the file where it's defined. Repeatable! ๐Ÿ”
โš ๏ธ But like, this cannot be used with -G or --keep-global-symbol. They literally cannot coexist, iykyk.
  • -w โ€” --wildcard โ€” this one lets you use regular expressions in symbolnames across your other command line options. The operators you can sprinkle anywhere in the symbol name are: ? (question mark), * (asterisk), \ (backslash) and [] (square brackets). And if the first character of the symbol name is !, the sense of the switch gets reversed for that symbol. Iconic behaviour tbh. ๐Ÿ’…

So for example, this:

-w -W !foo -W fo*

would make objcopy weaken all symbols that start with "fo" except for the symbol "foo". The ! is doing main character work. ๐Ÿ˜ค

  • -x โ€” --discard-all โ€” do not copy non-global symbols from the source file. Bye bye, locals. ๐Ÿšฎ
  • -X โ€” --discard-locals โ€” do not copy compiler-generated local symbols. Those little guys usually start with L or . โ€” you know the ones. ๐Ÿ™ˆ
  • -b byte โ€” --byte=byte โ€” if interleaving is already enabled via --interleave, this is where the range of bytes to keep starts: at the byteth byte. byte can be anywhere from 0 to breadth-1, where breadth is whatever you gave the --interleave option. ๐ŸŽฏ
  • -i [breadth] โ€” --interleave[=breadth] โ€” only copy a range out of every breadth bytes. (header data is not affected, so don't worry about that.) You pick which byte in the range kicks off the copy with --byte, and you pick the width of the range with --interleave-width.

This is the move for making files to program ROM ๐Ÿ“, and it's typically used with an "srec" output target. And fr fr, objcopy will complain if you don't also specify --byte. It's not playing around.

The default interleave breadth is 4, so with --byte set to 0, objcopy would copy the first byte out of every four bytes from the input to the output. Slay. โœจ

  • --interleave-width=width โ€” when used with --interleave, this copies width bytes at a time. The start of the range is set by --byte, and the extent of the range is set with --interleave. Teamwork. ๐Ÿซถ

The default value here is 1. And important: width plus the byte value set by --byte must not exceed the interleave breadth set by --interleave. Do the math bestie, it's not optional. ๐Ÿ˜ญ

Here's the iconic use case: you can build images for two 16-bit flashes interleaved in a 32-bit bus by passing -b 0 -i 4 --interleave-width=2 and -b 2 -i 4 --interleave-width=2 to two objcopy commands. If the input was '12345678', the outputs would be '1256' and '3478' respectively. Chef's kiss. ๐Ÿ‘‘

  • -p โ€” --preserve-dates โ€” sets the access and modification dates of the output file to be the same as those of the input file. Matching. ๐Ÿ’–

This option also copies the date stored in a PE format file's header โ€” unless the SOURCE_DATE_EPOCH environment variable is defined. If it is defined, then that variable gets used as the date stored in the header, interpreted as the number of seconds since the Unix epoch. โฐ

  • -D โ€” --enable-deterministic-archives โ€” operate in deterministic mode. When copying archive members and writing the archive index, it uses zero for UIDs, GIDs, and timestamps, and uses consistent file modes for all files. So reproducible. So cute. ๐ŸŒธ

If binutils was configured with --enable-deterministic-archives, then this mode is on by default. And you can turn it off with the -U option, below.

  • -U โ€” --disable-deterministic-archives โ€” do not operate in deterministic mode. This is the exact inverse of the -D option above: when copying archive members and writing the archive index, it uses their actual UID, GID, timestamp, and file mode values. Live your truth. ๐Ÿ’…

This is the default unless binutils was configured with --enable-deterministic-archives.

  • --debugging โ€” convert debugging information, if possible. It's not the default because only certain debugging formats are supported, and ngl the conversion process can be time consuming. Be patient with her. ๐Ÿ˜ญ
  • --gap-fill val โ€” fill the gaps between sections with val. This applies to the load address (LMA) of the sections. How? By increasing the size of the section with the lower address, and filling in the extra space created with val. Filling in the blanks ๐ŸŽ€
  • --pad-to address โ€” pad the output file up to the load address address. Done by increasing the size of the last section, and the extra space gets filled in with whatever --gap-fill says (default is zero).
  • --set-start val โ€” set the start address (also known as the entry address) of the new file to val. But lowkey, not all object file formats support setting the start address. Just so you know. ๐Ÿคท
  • --change-start incr โ€” --adjust-start incr โ€” change the start address (a.k.a. the entry address) by adding incr to it. Same disclaimer though: not all object file formats support setting the start address.
  • --change-addresses incr โ€” --adjust-vma incr โ€” change the VMA and LMA addresses of all sections, plus the start address, by adding incr. Some object file formats won't permit section addresses to be changed arbitrarily, so that's a thing. ๐Ÿ’€
โš ๏ธ Note that this does not relocate the sections. If the program expects sections to be loaded at a certain address, and you use this option to move them somewhere else, the program may fail. Not "might be slightly off" โ€” may outright fail. Be careful, bestie. ๐Ÿ˜ญ
  • --change-section-address sectionpattern{=,+,-}val โ€” --adjust-section-vma sectionpattern{=,+,-}val โ€” set or change both the VMA address and the LMA address of any section matching sectionpattern. If = is used, the section address is set to val. Otherwise, val is added to or subtracted from the section address. Go read the comments under --change-addresses above, seriously. ๐Ÿ‘€

And if sectionpattern doesn't match any sections in the input file, a warning gets issued โ€” unless --no-change-warnings is used. ๐Ÿ“ข

  • --change-section-lma sectionpattern{=,+,-}val โ€” set or change the LMA address of any sections matching sectionpattern. The LMA address is the address where the section will be loaded into memory at program load time.

Normally this is the same as the VMA address, which is the address of the section at program run time โ€” but on some systems, especially ones where a program is held in ROM, the two can be different. ๐ŸŒˆ

If = is used, the section address is set to val. Otherwise val is added to or subtracted from the section address. See the comments under --change-addresses, above. And if sectionpattern doesn't match any sections in the input file, a warning gets issued โ€” unless --no-change-warnings is used. ๐Ÿ“ข

  • --change-section-vma sectionpattern{=,+,-}val โ€” set or change the VMA address of any section matching sectionpattern. The VMA address is the address where the section will be located once the program has started executing.

Normally it's the same as the LMA address, which is the address where the section will be loaded into memory, but โ€” again โ€” on some systems, especially ones where a program is held in ROM, the two can be different. ๐Ÿฆ‹

If = is used, the section address is set to val. Otherwise val is added to or subtracted from the section address. See the comments under --change-addresses, above. And if sectionpattern doesn't match any sections in the input file, a warning gets issued โ€” unless --no-change-warnings is used. ๐Ÿ“ข

โš ๏ธ Note โ€” changing the VMA of sections in a fully linked binary can be dangerous, since there may be code that expects the sections to be located at their old address. This is the "don't do it unless you know exactly why" zone. ๐Ÿ’€
  • --change-warnings โ€” --adjust-warnings โ€” if --change-section-address or --change-section-lma or --change-section-vma is used, and the section pattern doesn't match any sections, issue a warning. This is the default behaviour, so it's already looking out for you. ๐Ÿซถ
  • --no-change-warnings โ€” --no-adjust-warnings โ€” do not issue a warning if --change-section-address or --adjust-section-lma or --adjust-section-vma is used, even if the section pattern doesn't match any sections. Silent mode. ๐Ÿคซ
  • --set-section-flags sectionpattern=flags โ€” set the flags for any sections matching sectionpattern. The flags argument is a comma separated string of flag names, and the recognized names are:

alloc, contents, load, noload, readonly, code, data, rom, exclude, share, debug, and large.

You can set the contents flag for a section which does not have contents โ€” but it's not meaningful. And it is not meaningful to clear the contents flag of a section which does have contents โ€” bestie, just remove the section instead. ๐Ÿ™ˆ

Not all flags are meaningful for all object file formats either. In particular the share flag is only meaningful for COFF format files and not for ELF format files. And the ELF x86-64 specific flag large corresponds to SHF_X86_64_LARGE. Very specific, very slay. โญ

  • --set-section-alignment sectionpattern=align โ€” set the alignment for any sections matching sectionpattern. align specifies the alignment in bytes and must be a power of two, i.e. 1, 2, 4, 8.... Math is math. ๐Ÿ”ข
Heads up bestie: setting a section's alignment does not magically align its LMA or VMA too. If those need to shift, you gotta reach for --change-section-lma and/or --change-section-vma. Also lowkey important: messing with VMAs can cause problems in fully linked binaries, because there might be code out there that expects the section contents to live at their old address. So like. Be careful. ๐Ÿ’€

  • --add-section sectionname=filename โ€” adds a whole new section called sectionname while it's copying the file. The contents of the new section come straight from the file filename, and the section's size is just the size of that file. This only works on file formats that can support sections with arbitrary names. Note โ€” you may need to use --set-section-flags to set the attributes of your shiny new section.
  • --dump-section sectionname=filename โ€” takes the contents of the section named sectionname and drops them into the file filename, overwriting whatever was there before (rip to the old contents ๐Ÿชฆ). This one is the inverse of --add-section. It's similar to --only-section except it doesn't create a formatted file โ€” it just dumps the raw binary data as-is, with no relocations applied. You can specify this option more than once. โœจ
  • --update-section sectionname=filename โ€” replaces the existing contents of a section named sectionname with the contents of filename. The section size gets adjusted to match the file size. The section flags for sectionname stay exactly the same. For ELF format files, the section-to-segment mapping also stays unchanged โ€” which is something you literally cannot pull off with --remove-section followed by --add-section. Can be specified more than once. ๐Ÿ’…
Note โ€” you can use --rename-section and --update-section together on one command line to both update and rename a section. If you do that, pass the original section name to --update-section, and pass both the original and new section names to --rename-section. iykyk ๐Ÿซถ
  • --add-symbol name=[section:]value[,flags] โ€” adds a new symbol named name while copying the file. Can be specified multiple times. If you give a section, the symbol gets associated with and is relative to that section โ€” otherwise it's an ABS symbol. Specifying a section that doesn't exist = fatal error. There's no check on the value, it just takes whatever you said. You can specify symbol flags, but not all flags are meaningful for every object file format. By default the symbol is global. The special flag 'before=othersym' inserts the new symbol in front of the specified othersym โ€” otherwise the symbol(s) get tacked onto the end of the symbol table in the order they appear. ๐ŸŒˆ
  • --rename-section oldname=newname[,flags] โ€” renames a section from oldname to newname, and optionally swaps the section's flags to flags while it's at it. The glow-up here vs. using a linker script for the rename: your output stays an object file and does not become a linked executable. ๐Ÿ‘‘ This option accepts the same set of flags as --set-section-flags.

It's extra helpful when the input format is binary, because that always creates a section called .data. Say you wanted a section called .rodata with binary data in it instead โ€” you'd do this:

objcopy -I binary -O <output_format> -B <architecture> \
 --rename-section .data=.rodata,alloc,load,readonly,data,contents \
 <input_binary_file> <output_object_file>
  • --long-section-names {enable,disable,keep} โ€” controls how long section names get handled when processing "COFF" and "PE-COFF" object formats. The default behaviour, keep, preserves long section names if any are present in the input file. enable and disable forcibly enable or disable long section names in the output object; when disable is in effect, any long section names in the input object get truncated. โœ‚๏ธ The enable option only emits long section names if any are present in the inputs โ€” mostly the same as keep, but it's left undefined whether enable might force an empty string table into the output file. (mysterious behaviour, we stan a little ambiguity ๐Ÿ™ˆ)
  • --change-leading-char โ€” some object file formats use special characters at the start of symbols, and the most common one is the underscore, which compilers love to slap in front of every symbol. This option tells objcopy to change the leading character of every symbol when it converts between object file formats. If both formats use the same leading character, this option does nothing (no notes ๐Ÿ’ค). Otherwise it'll add a character, remove a character, or change a character, as appropriate. ๐Ÿ’–
  • --remove-leading-char โ€” if the first character of a global symbol is a special symbol leading character used by the object file format, remove it. The most common leading character is the underscore, so this option strips a leading underscore off all global symbols. Handy if you want to link together objects from different file formats with different symbol-name conventions. This is different from --change-leading-char because it always changes the symbol name when appropriate, regardless of the output file's object file format. ๐ŸŽ€
  • *--reverse-bytes=num* โ€” reverses the bytes in a section with output contents. The section length must be evenly divisible by the value you give, otherwise the swap can't happen, periodt. The reversing happens before interleaving is performed.

This is typically used when generating ROM images for problematic target systems. For example, on some target boards, the 32-bit words fetched from 8-bit ROMs get re-assembled in little-endian byte order no matter what the CPU byte order is. Depending on the programming model, the endianness of the ROM might need to be modified.

Picture a simple file with a section containing these eight bytes: 12345678.

With --reverse-bytes=2 on that example, the bytes in the output file would be ordered 21436587.

With --reverse-bytes=4 on that example, the bytes in the output file would be ordered 43218765.

And if you use --reverse-bytes=2 on the example and then --reverse-bytes=4 on the output file, the bytes in the second output file would be ordered 34127856. math is math ๐Ÿงฎโœจ

  • --srec-len=ival โ€” only meaningful for srec output. Sets the maximum length of the S-records being produced to ival. That length covers the address, data **and crc fields.
  • --srec-forceS3 โ€” only meaningful for srec output. Avoids generating S1/S2 records and creates an S3-only record format. no cap ๐Ÿ’…
  • --redefine-sym old=new โ€” changes the name of a symbol from old to new. Useful when you're trying to link two things together but you don't have the source, and there are name collisions. (rename and move on bestie ๐Ÿฆ‹)
  • *--redefine-syms=filename* โ€” applies --redefine-sym to each symbol pair "old new" listed in the file filename. filename is just a flat file with one symbol pair per line. Line comments can be introduced with the hash character. This option may be given more than once.
  • --weaken โ€” changes all global symbols in the file to be weak. Useful when building an object that'll be linked against other objects using the -R option to the linker. Only effective when using an object file format that supports weak symbols. (be soft, be gentle, be weak ๐Ÿซถ)
  • *--keep-symbols=filename* โ€” applies --keep-symbol to each symbol listed in the file filename. filename is just a flat file with one symbol name per line. Line comments can be introduced with the hash character. This option may be given more than once.
  • *--strip-symbols=filename* โ€” applies --strip-symbol to each symbol listed in the file filename. filename is just a flat file with one symbol name per line. Line comments can be introduced with the hash character. This option may be given more than once.
  • *--strip-unneeded-symbols=filename* โ€” applies --strip-unneeded-symbol to each symbol listed in the file filename. filename is just a flat file with one symbol name per line. Line comments can be introduced with the hash character. This option may be given more than once.
  • *--keep-global-symbols=filename* โ€” applies --keep-global-symbol to each symbol listed in the file filename. filename is just a flat file with one symbol name per line. Line comments can be introduced with the hash character. This option may be given more than once.
  • *--localize-symbols=filename* โ€” applies --localize-symbol to each symbol listed in the file filename. filename is just a flat file with one symbol name per line. Line comments can be introduced with the hash character. This option may be given more than once.
  • *--globalize-symbols=filename* โ€” applies --globalize-symbol to each symbol listed in the file filename. filename is just a flat file with one symbol name per line. Line comments can be introduced with the hash character. This option may be given more than once. Note: this option cannot be used in conjunction with the -G or --keep-global-symbol options. pick a lane bestie ๐Ÿ˜ค
  • *--weaken-symbols=filename* โ€” applies --weaken-symbol to each symbol listed in the file filename. filename is just a flat file with one symbol name per line. Line comments can be introduced with the hash character. This option may be given more than once.
  • *--alt-machine-code=index* โ€” if the output architecture has alternate machine codes, use the indexth code instead of the default one. Useful when a machine gets assigned an official code and the tool-chain adopts the new code, but other applications still depend on the original code being used. For ELF-based architectures, if the index alternative doesn't exist then the value is treated as an absolute number to be stored in the e_machine field of the ELF header. ๐ŸŒŸ
  • --writable-text โ€” marks the output text as writable. This option isn't meaningful for all object file formats.
  • --readonly-text โ€” makes the output text write protected. ๐Ÿ”’ This option isn't meaningful for all object file formats.
  • --pure โ€” marks your output file as demand paged. Lowkey this one isn't meaningful for every object file format, so don't be shocked if it just vibes there doing nothing ๐Ÿ’…
  • --impure โ€” marks the output file as impure instead. Same lil disclaimer though: not meaningful for all object file formats, bestie.
  • --prefix-symbols=string โ€” slaps string onto the front of every single symbol in the output file. Glow-up via prefix โœจ
  • --prefix-symbols but for sections โžก๏ธ
  • --prefix-sections=string โ€” prefixes all section names in the output file with string.
  • --prefix-alloc-sections=string โ€” same energy but pickier: it prefixes the names of all the allocated sections in the output file with string. Only the allocated girlies get the makeover ๐ŸŽ€
  • --add-gnu-debuglink=path-to-file โ€” this one builds a .gnu_debuglink section that holds a reference to path-to-file, then yeets it into the output file. Periodt.
Heads up bestie: the file at path-to-file must exist. No file, no deal. ๐Ÿ˜ญ

Part of adding that .gnu_debuglink section means embedding a checksum of the debug info file's contents into the section. Iconic but also a trap if you're not careful.

So here's the situation that ruins people's whole day: if the debug info file gets built in one location but is going to be installed at a different location later, do not put the path to the installed location. The --add-gnu-debuglink option will fail, because that installed file literally does not exist yet. Instead, drop the debug info file in the current directory and call the option with no directory components at all, like this:

objcopy --add-gnu-debuglink=foo.debug

At debug time, the debugger goes hunting for the separate debug info file in a set of known locations. The exact set varies depending on which distribution you're on, but it typically includes:

  • The same directory as the executable. ๐Ÿ“
  • A sub-directory of the directory containing the executable called .debug.
  • A global debug directory such as /usr/lib/debug.

As long as the debug info file has been installed into one of these locations before the debugger runs, everything should work correctly. Slay.

  • --keep-section-symbols โ€” when you're stripping a file (maybe with --strip-debug or --strip-unneeded), this keeps any symbols that specify section names โ€” the ones that would otherwise get stripped away. Protect them ๐Ÿซถ
  • --keep-file-symbols โ€” same vibe, different symbols: when stripping a file, perhaps with --strip-debug or --strip-unneeded, retain any symbols specifying source file names</>, which would otherwise get stripped. Not today, stripper ๐Ÿ˜ค
  • --only-keep-debug โ€” strips a file by removing the contents of any sections that --strip-debug wouldn't strip, while leaving the debugging sections fully intact. For ELF files, this preserves all note sections in the output. โœจ
Note: the section headers of the stripped sections are preserved, including their sizes, but the contents of the section get discarded. The headers stick around so other tools can match up the debuginfo file with the real executable, even if that executable has been relocated to a different address space. iykyk.

The intention here is that you use this alongside --add-gnu-debuglink to create a two part executable. Part one: a stripped binary that takes up less space in RAM and in a distribution. Part two: a debugging information file that's only needed if you actually want debugging abilities. The suggested procedure to build these files goes like this:

  1. Link the executable as normal. Assuming it's called foo then...
  2. Run objcopy --only-keep-debug foo foo.dbg to create a file containing the debugging info.
  3. Run objcopy --strip-debug foo to create a stripped executable.
  4. Run objcopy --add-gnu-debuglink=foo.dbg foo to add a link to the debugging info into the stripped executable.
Note โ€” the choice of .dbg as an extension for the debug info file is arbitrary, bestie. Also the --only-keep-debug step is optional. You could instead do this:
  1. Link the executable as normal.
  2. Copy foo to foo.full.
  3. Run objcopy --strip-debug foo.
  4. Run objcopy --add-gnu-debuglink=foo.full foo.

i.e., the file pointed to by --add-gnu-debuglink can be the full executable. It doesn't have to be a file created by the --only-keep-debug switch. Flexible queen ๐Ÿ’…

Note โ€” this switch is only intended for use on fully linked files. It does not make sense to use it on object files where the debugging information may be incomplete. Besides, the gnu_debuglink feature currently only supports the presence of one filename containing debugging information โ€” not multiple filenames on a one-per-object-file basis. So don't try it ๐Ÿšซ
  • --strip-dwo โ€” removes the contents of all DWARF .dwo sections, leaving the remaining debugging sections and all symbols intact. This option is meant for the compiler to use as part of the -gsplit-dwarf option, which splits debug information between the .o file and a separate .dwo file. The compiler generates all debug info in the same file, then uses the --extract-dwo option to copy the .dwo sections over to the .dwo file, then uses --strip-dwo to remove those sections from the original .o file. It's giving teamwork ๐Ÿงš
  • --extract-dwo โ€” extracts the contents of all DWARF .dwo sections. See the --extract-dwo... no wait, see the --strip-dwo option above for more info, bestie ๐Ÿ‘†
  • --file-alignment num โ€” specifies the file alignment. Sections in the file will always begin at file offsets that are multiples of this number. Defaults to 512. [This option is specific to PE targets.]
  • --heap reserve
  • --heap reserve,commit โ€” specifies the number of bytes of memory to reserve (and optionally commit) to be used as heap for this program. [This option is specific to PE targets.]
  • --image-base value โ€” uses value as the base address of your program or dll. This is the lowest memory location that will be used when your program or dll is loaded. To reduce the need to relocate and improve the performance of your dlls, each one should have a unique base address and not overlap any other dlls. The default is 0x400000 for executables, and 0x10000000 for dlls. [This option is specific to PE targets.]
  • --section-alignment num โ€” [This option is specific to PE targets.]

Sets the section alignment field in the PE header โ€” if one is present in the binary. Sections in memory will always begin at addresses which are a multiple of this number. Defaults to 0x1000.

Note โ€” if a section's LMA or VMA addresses are no longer aligned, and those addresses haven't been set via the --set-section-lma or --set-section-vma options, and the file has been fully relocated, then a warning message will be issued. After that it's up to you, the user, to decide if the LMA and VMA need updating. No pressure ๐Ÿ™ƒ
  • --stack reserve
  • --stack reserve,commit โ€” specifies the number of bytes of memory to reserve (and optionally commit) to be used as stack for this program. [This option is specific to PE targets.]
  • --subsystem which
  • --subsystem which:major
  • --subsystem which:major.minor โ€” specifies the subsystem under which your program will execute. The legal values for which are "native", "windows", "console", "posix", "efi-app", "efi-bsd", "efi-rtd", "sal-rtd", and "xbox". You may optionally set the subsystem version too. Numeric values are also accepted for which. [This option is specific to PE targets.] Choose your fighter ๐ŸŒˆ
  • --extract-symbol โ€” keeps the file's section flags and symbols but removes all section data. Specifically, this option:
    • Removes the contents of all sections.
    • Sets the size of every section to zero.
    • Sets the file's start address to zero.

This option is used to build a .sym file for a VxWorks kernel. It can also be a genuinely useful way of reducing the size of a --just-symbols linker input file. Efficient ๐Ÿ‘‘

  • --compress-debug-sections โ€” compresses DWARF debug sections using zlib with SHF_COMPRESSED from the ELF ABI.
Note โ€” if compression would actually make a section larger, then it is not compressed. Common sense era ๐Ÿ’€
  • --compress-debug-sections=none
  • --compress-debug-sections=zlib
  • --compress-debug-sections=zlib-gnu
  • --compress-debug-sections=zlib-gabi
  • --compress-debug-sections=zstd

For ELF files, these options control how DWARF debug sections get compressed:

  • --compress-debug-sections=none is equivalent to --decompress-debug-sections.
  • --compress-debug-sections=zlib and --compress-debug-sections=zlib-gabi are equivalent to --compress-debug-sections.
  • --compress-debug-sections=zlib-gnu compresses DWARF debug sections using the obsoleted zlib-gnu format. The debug sections get renamed to begin with .zdebug.
  • --compress-debug-sections=zstd compresses DWARF debug sections using zstd.
Note โ€” if compression would actually make a section larger, then it is not compressed nor renamed. Stay small or stay home โœจ
  • --decompress-debug-sections โ€” decompresses DWARF debug sections. For a .zdebug section, the original name is restored. Undo button activated ๐Ÿ”„
  • --elf-stt-common=yes
  • --elf-stt-common=no

For ELF files, these options control whether common symbols get converted to the "STT_COMMON" or "STT_OBJECT" type. --elf-stt-common=yes converts common symbol type to "STT_COMMON". --elf-stt-common=no converts common symbol type to "STT_OBJECT". Pick a lane ๐ŸŽ€

  • --merge-notes
  • --no-merge-notes

For ELF files, these attempt (or do not attempt) to reduce the size of any SHT_NOTE type sections by removing duplicate notes. Marie Kondo that notes section ๐Ÿงน

  • -V
  • --version โ€” shows the version number of objcopy. Know your queen's lineage ๐Ÿ‘‘
  • --verilog-data-width=bytes โ€” for Verilog output, this option controls the number of bytes converted for each output data element. The input target controls the endianness of the conversion. ๐Ÿฆ‹
  • -v
  • --verbose โ€” verbose output, bestie: lists all object files modified. In the case of archives, objcopy -V lists all members of the archive. Spill the tea, tell me everything โ˜•
  • --help โ€” shows a summary of the options to objcopy. Your little cheat sheet ๐Ÿซถ
  • --info โ€” displays a list showing all architectures and object formats available. Window shopping ๐Ÿ›๏ธ
  • @file โ€” reads command-line options from file. The options that get read are inserted in place of the original @file option. If file doesn't exist, or can't be read, then the option is treated literally and not removed. No ghosting, it just stays there.

Options in file are separated by whitespace. A whitespace character may be included in an option by surrounding the entire option in either single or double quotes. Any character (including a backslash) may be included by prefixing the character to be included with a backslash. The file may itself contain additional @file options โ€” and any such options will be processed recursively. It's files all the way down ๐ŸŒ€

๐ŸŒˆ SEE ALSO ๐ŸŒˆ

okay bestie, when you're done here, go hang with the rest of the friend group ๐Ÿ’…

  • ld(1) โ€” the linker, the one that actually puts everything together
  • objdump(1) โ€” for when you wanna stare at a file instead of rewriting it
  • the Info entries for binutils โ€” fr fr, the whole squad lives there ๐Ÿ“š

Copyright (c) 1991-2026 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation โ€” with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. ๐Ÿ’–

A copy of the license is included in the section entitled "GNU Free Documentation License".

ngl this one's the legal bestie of the page, but she's iconic and she said what she said โœจ

and that's a wrap, my love ๐Ÿซถ this whole glow-up is from binutils-2.47, dated 2026-09-05, and it's giving manual page, but make it serve ๐Ÿ‘‘ bye pookie, go rewrite some object files ๐Ÿ’…๐ŸŒธ

๐Ÿ“ฆ where this page comes from