OBJCOPY(1)

πŸ“¦
version 2.46.1 Β· 5 versions
, in fedora 44

✨ objcopy(1) β€” GNU Development Tools ✨

okay bestie, sit DOWN, because we're about to talk about the single most useful little gremlin in the GNU binutils toolbox πŸ’–


🌸 NAME 🌸

objcopy β€” copy and translate object files. that's it, that's the vibe. copy one, get another one, possibly in a whole different format. slay.


πŸ¦‹ SYNOPSIS πŸ¦‹

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]

like, look at that list. it's giving options. it's giving main character energy. and every single one of them is real, so don't come for me. πŸ’…


πŸ’œ DESCRIPTION πŸ’œ

okay so here's the whole situation: the GNU objcopy utility copies the contents of an object file to another. that's the entire job description. it reads and writes object files using the GNU BFD Library, and the truly iconic part is that it can write the destination object file in a format that's different from the source object file's format. the exact behavior of objcopy is controlled by command-line options, so like… you're the one driving, bestie. πŸš—

heads up: objcopy should be able to copy a fully linked file between any two formats. however, copying a relocatable object file between any two formats may not work as expected. not me warning you in advance 😭

objcopy creates temporary files to do its translations and deletes them afterward. it uses BFD to do all its translation work, which means it has access to all the formats described in BFD, and thus it's able to recognize most formats without being told explicitly. lowkey psychic behavior, we love to see it ✨

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

you can also use objcopy to generate a raw binary file by using an output target of binary (e.g., use -O binary). when objcopy 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 β€” gone, bye, no thoughts head empty 🫢. the memory dump will start at the load address of the lowest section copied into the output file.

when you're 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. tiny little glow-ups, we love them. 🌸

⚠️ big note, bestie, please read this one: 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. iykyk. πŸ‘€)

πŸŽ€ OPTIONS πŸŽ€

  • infile, outfile β€” the input and output files, respectively. if you don't specify outfile, objcopy creates a temporary file and destructively renames the result with the name of infile. so like, be aware of that. she's thorough. πŸ’€
  • -I bfdname, --input-target=bfdname β€” just go ahead and tell her the source file's object format is bfdname, instead of making her deduce it like a detective. sometimes you just gotta say it out loud. πŸ—£οΈ
  • -O bfdname, --output-target=bfdname β€” write the output file using the object format bfdname. this is the glow-up format. the one she shows up as at the party. ✨
  • -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. it's the "no thoughts, just vibes, straight across" option. πŸ¦‹
  • -B bfdarch, --binary-architecture=bfdarch β€” useful when you're 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. those 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. literally turning a picture into code. iconic. πŸ–ΌοΈβž‘οΈπŸ’»
  • -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. it's the "yes to all of them except THAT one" move. πŸ™ˆ

  • -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. bestie. don't. 🚫

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. okay so she can be reasoned with. good to know. πŸ’…

  • --keep-section=sectionpattern β€” when removing sections from the output file, keep sections that match sectionpattern. a little "wait, not that one" πŸ₯Ί
  • --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. consistent queen. πŸ‘‘

  • --strip-section-headers β€” strip section header. this option is specific to ELF files. implies --strip-all and --merge-notes. she comes with friends. πŸ’…
  • -S, --strip-all β€” do not copy relocation and symbol information from the source file. also deletes debug sections. full clean-out, everything must go 🧹
  • -g, --strip-debug β€” do not copy debugging symbols or sections from the source file. just the debug stuff though, she's not being dramatic.
  • --strip-unneeded β€” remove all symbols that are not needed for relocation processing, in addition to debugging symbols and sections stripped by --strip-debug. it's --strip-debug but with extra attitude. πŸ”₯
  • -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. the "no, not THIS one, she's my friend" option 🫢
  • -N symbolname, --strip-symbol=symbolname β€” do not copy symbol symbolname from the source file. this option may be given more than once. bye bestie, blocked. πŸ“΅
  • --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. so like… conditional blocking. she'll unblock you if it's actually important. 😌

more parts coming, bestie β€” don't go anywhere, we're only getting started πŸ’–

  • -G symbolname, --keep-global-symbol=symbolname β€” okay so this keeps ONLY symbolname global, and makes every other symbol local to the file so they're not visible externally. You can drop this option more than once.
Note: cannot be used with --globalize-symbol or --globalize-symbols. 🚫
  • --localize-hidden β€” in an ELF object, mark all symbols that have hidden or internal visibility as local. This stacks on top of symbol-specific localization options like -L. ✨
  • -L symbolname, --localize-symbol=symbolname β€” convert a global or weak symbol called symbolname into a local symbol so it's not visible externally. Can be given more than once.
Note: unique symbols are not converted. πŸ™…β€β™€οΈ
  • -W symbolname, --weaken-symbol=symbolname β€” make symbolname weak. Can be given more than once. πŸ’ͺ
  • --binary-symbol-prefix=string β€” when used with --input-target=binary, 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 derives the prefix symbol name from a mangled version of the input file name and _binary prefix. This option lets you specify it explicitly. 🧚

  • --globalize-symbol=symbolname β€” give symbolname global scoping so it's visible outside the file where it's defined. Can be given more than once.
Note: cannot be used with -G or --keep-global-symbol. 🚫
  • -w, --wildcard β€” permit regular expressions in symbol names used in other command line options. The ?, *, \, and [] operators can be used anywhere in the symbol name. If the first character of the symbol name is !, then the sense of the switch is reversed for that symbol. For example:
-w -W !foo -W fo*

would cause objcopy to weaken all symbols that start with "fo" except for the symbol "foo". 🀯

  • -x, --discard-all β€” do not copy non-global symbols from the source file. πŸ—‘οΈ
  • -X, --discard-locals β€” do not copy compiler-generated local symbols. (These usually start with L or ...) 🧹
  • -b byte, --byte=byte β€” if interleaving is enabled via --interleave, start the range of bytes to keep at the byteth byte. byte can be from 0 to breadth-1, where breadth is the value from --interleave. 🧩
  • -i [breadth], --interleave[=breadth] β€” only copy a range out of every breadth bytes. (Header data is not affected.) Pick which byte starts the copy with --byte. Pick the width of the range with --interleave-width. This is useful for making files to program ROM. Usually paired with an "srec" output target.
Note: objcopy will complain if you don't also specify --byte. 😀

The default interleave breadth is 4, so with --byte set to 0, objcopy copies the first byte out of every four bytes from input to output. πŸ”₯

  • --interleave-width=width β€” when used with --interleave, copy width bytes at a time. The start of the range is set by --byte, and the extent is set with --interleave. The default is 1. width plus the byte value from --byte must not exceed the interleave breadth from --interleave. You can use this to create 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' then the outputs would be '1256' and '3478' respectively. πŸ€“
  • -p, --preserve-dates β€” set the access and modification dates of the output file to match the input file. This 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, that variable is used as the date in the header, interpreted as seconds since the Unix epoch. πŸ“…
  • -D, --enable-deterministic-archives β€” operate in deterministic mode. When copying archive members and writing the archive index, use zero for UIDs, GIDs, timestamps, and use consistent file modes for all files. If binutils was configured with --enable-deterministic-archives, this mode is on by default. It can be disabled with -U. βš™οΈ
  • -U, --disable-deterministic-archives β€” do not operate in deterministic mode. This is the inverse of -D: when copying archive members and writing the archive index, use their actual UID, GID, timestamp, and file mode values. This is the default unless binutils was configured with --enable-deterministic-archives. πŸ”„
  • --debugging β€” convert debugging information, if possible. This is not the default because only certain debugging formats are supported, and the conversion process can be time consuming. πŸ›
  • --gap-fill val β€” fill gaps between sections with val. This applies to the load address (LMA) of the sections. It works by increasing the size of the section with the lower address and filling the extra space with val. πŸ•³οΈ
  • --pad-to address β€” pad the output file up to the load address address. This is done by increasing the size of the last section. The extra space is filled with the value from --gap-fill (default zero). 🧱
  • --set-start val β€” set the start address (also known as the entry address) of the new file to val. Not all object file formats support setting the start address. πŸš€
  • --change-start incr, --adjust-start incr β€” change the start address (also known as the entry address) by adding incr. 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 do not permit section addresses to be changed arbitrarily.
Note: this does not relocate the sections; if the program expects sections to be loaded at a certain address, and this option changes them so they load somewhere else, the program may fail. πŸ’€
  • --change-section-address sectionpattern{=,+,-}val, --adjust-section-vma sectionpattern{=,+,-}val β€” set or change both the VMA and LMA addresses 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. See the comments under --change-addresses above. If sectionpattern does not match any sections in the input file, a warning is 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 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 those 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. If sectionpattern does not match any sections in the input file, a warning is 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 where the section will be located once the program has started executing. Normally this is the same as the LMA address, which is where the section will be loaded into memory, but on some systemsβ€”especially those 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. If sectionpattern does not match any sections in the input file, a warning is 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. πŸ’€
  • --change-warnings, --adjust-warnings β€” if --change-section-address or --change-section-lma or --change-section-vma is used, and the section pattern does not match any sections, issue a warning. This is the default. πŸ””
  • --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 does not match any sections. 🀫
  • --set-section-flags sectionpattern=flags β€” set the flags for any sections matching sectionpattern. The flags argument is a comma separated string of flag names. 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 is not meaningful to clear the contents flag of a section which does have contentsβ€”just remove the section instead. Not all flags are meaningful for all object file formats. In particular the share flag is only meaningful for COFF format files and not for ELF format files. The ELF x86-64 specific flag large corresponds to SHF_X86_64_LARGE. 🏷️
  • --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.... πŸ“

Note bestie β€” setting a section's alignment does NOT automatically align its LMA or VMA addresses. If those need changing too, you gotta reach for --change-section-lma and/or --change-section-vma. Also, heads up: changing VMAs can cause problems in fully linked binaries, because there may be code sitting there expecting the contents of the sections to live at their old address. 😭

  • --add-section sectionname=filename β€” adds a brand new section named sectionname while copying the file. The contents come straight from filename, and the section's size will be the size of that file. This only works on file formats that can support sections with arbitrary names. Note β€” you might need --set-section-flags to set the attributes of the newly created section. πŸŽ€
  • --dump-section sectionname=filename β€” takes the contents of the section named sectionname and drops them into filename, overwriting whatever was there before. This is the inverse of --add-section. It's similar to --only-section except it does not create a formatted file β€” it just dumps the raw binary data, no relocations applied. Can be specified more than once. πŸ¦‹
  • --update-section sectionname=filename β€” replaces the existing contents of the section named sectionname with the contents of filename. The section's size gets adjusted to the size of the file. The section flags for sectionname stay unchanged. For ELF format files the section-to-segment mapping also stays unchanged, which is something you can't get with --remove-section followed by --add-section. Can be specified more than once. ✨

Note β€” you can use --rename-section and --update-section together to both update and rename a section from one command line. In that case, pass the original section name to --update-section, and 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 the section, the symbol gets associated with and relative to that section; otherwise it'll be an ABS symbol. Specifying an undefined section is a fatal error. There's no check on the value β€” it's taken exactly as specified. Symbol flags can be specified, and not all flags will be meaningful for all object file formats. 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 added at the end of the symbol table, in the order they appear. πŸ‘‘
  • --rename-section oldname=newname[,flags] β€” renames a section from oldname to newname, optionally switching the section's flags to flags along the way. The advantage over using a linker script to do the rename is that the 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 especially helpful when the input format is binary, since that always creates a section called .data. So say you wanted a section called .rodata holding binary data instead β€” you could pull it off with this command line:

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 turn the use of long section names on or off in the output object; when disable is in effect, any long section names in the input object get truncated. enable will only emit long section names if any are present in the inputs β€” mostly the same as keep, but it's left undefined whether enable might force the creation of an empty string table in the output file. πŸ™ˆ
  • --change-leading-char β€” some object file formats use special characters at the start of symbols, and the most common one is underscore, which compilers love to slap in front of every symbol. This 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 does nothing at all. Otherwise it adds a character, removes a character, or changes 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 symbol leading character is underscore, so this removes a leading underscore from all global symbols. Handy if you wanna link together objects of different file formats that have different conventions for symbol names. This is different from --change-leading-char because it always changes the symbol name when appropriate, regardless of the object file format of the output file. πŸ’œ
  • --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. Reversing takes place before the interleaving is performed.

This is typically used for generating ROM images for problematic target systems. Like, on some target boards, the 32-bit words fetched from 8-bit ROMs get re-assembled in little-endian byte order regardless of the CPU byte order. Depending on the programming model, the ROM's endianness may need to be modified.

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

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

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

And if you use --reverse-bytes=2 on the example, then --reverse-bytes=4 on the output file, the second output file's bytes would be ordered 34127856. πŸ”₯

  • --srec-len=ival β€” only meaningful for srec output. Sets the maximum length of the Srecords being produced to ival. This length covers both the address, data and crc fields.
  • --srec-forceS3 β€” only meaningful for srec output. Avoids generating S1/S2 records, creating an S3-only record format.
  • --redefine-sym old=new β€” changes the name of a symbol old to new. Useful when you're trying to link two things together and you have no source, and there are name collisions. 😀
  • --redefine-syms=filename β€” applies --redefine-sym to each symbol pair "old new" listed in the file filename. filename is just a flat file, 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.
  • --keep-symbols=filename β€” applies --keep-symbol to each symbol listed in the file filename. filename is just a flat file, one symbol name per line. Line comments can be introduced with the hash character. 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, one symbol name per line. Line comments can be introduced with the hash character. 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, one symbol name per line. Line comments can be introduced with the hash character. 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, one symbol name per line. Line comments can be introduced with the hash character. 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, one symbol name per line. Line comments can be introduced with the hash character. 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, one symbol name per line. Line comments can be introduced with the hash character. May be given more than once. Note: this option cannot be used in conjunction with the -G or --keep-global-symbol options. πŸ’€
  • --weaken-symbols=filename β€” applies --weaken-symbol to each symbol listed in the file filename. filename is just a flat file, one symbol name per line. Line comments can be introduced with the hash character. 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. Not meaningful for all object file formats.
  • --readonly-text β€” makes the output text write protected. Not meaningful for all object file formats. 🫢
  • --pure β€” marks the output file as demand paged. Heads up bestie: this one isn't meaningful for every object file format, so don't come for me if it does nothing. πŸ’…
  • --impure β€” marks the output file as impure. Same vibe check as above: not meaningful for all object file formats.
  • --prefix-symbols=string β€” slaps string on the front of every single symbol in the output file. ✨
  • --prefix-sections=string β€” same energy but for section names: every section name in the output file gets string prefixed onto it. πŸŽ€
  • --prefix-alloc-sections=string β€” and this one prefixes string onto the names of all the allocated sections in the output file. Only the allocated ones, iykyk. 🌸
  • --add-gnu-debuglink=path-to-file β€” builds a .gnu_debuglink section holding a reference to path-to-file and drops it into the output file. ⚠️ Real talk: the file at path-to-file must actually exist. Part of the whole process involves embedding a checksum of the contents of the debug info file into the section, so no fake files, okay?

Now here's the trap: if the debug info file gets built in one place but installed somewhere else later, do not use the path to the installed location. The --add-gnu-debuglink option will straight up fail because the installed file doesn't exist yet. 😭 Instead, put the debug info file in the current directory and call --add-gnu-debuglink with no directory components at all, like this:

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

Later, 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 your distribution, 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 those locations before the debugger runs, everything should work correctly. 🫢

  • --keep-section-symbols β€” when you're stripping a file (maybe with --strip-debug or --strip-unneeded), this keeps any symbols that specify section names, which would otherwise get stripped away. Keeping the icons, periodt. πŸ‘‘
  • --keep-file-symbols β€” same deal, but for symbols that specify source file names. Without this they'd get stripped too.
  • --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. In ELF files this preserves all note sections in the output. πŸ’–
Note β€” the section headers of the stripped sections are preserved, sizes and all, but the actual contents get discarded. Why keep the headers? 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. 🧠

The whole intention here is that you use this option together with --add-gnu-debuglink to make a two-part executable. One part is a stripped binary that takes up less space in RAM and in a distribution, and the second is a debugging information file that you only need if you actually want debugging abilities. The suggested procedure 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 completely arbitrary. 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 does not have to be a file created by the --only-keep-debug switch. πŸ™Œ

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. πŸ’€
  • --strip-dwo β€” removes the contents of all DWARF .dwo sections, leaving the remaining debugging sections and all symbols intact. This one's intended for the compiler to use as part of the -gsplit-dwarf option, which splits debug info between the .o file and a separate .dwo file. The compiler generates all debug info in the same file, then uses --extract-dwo to copy the .dwo sections into the .dwo file, then uses --strip-dwo to remove those sections from the original .o file. πŸ¦‹
  • --extract-dwo β€” extracts the contents of all DWARF .dwo sections. See the --strip-dwo option above for the full lore.
  • --file-alignment num β€” sets 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 β€” tells it how many 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 gets used when your program or dll is loaded. To reduce the need to relocate and improve 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 to decide if the LMA and VMA need updating. 🫠
  • --stack reserve / --stack reserve,commit β€” tells it how many 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 can optionally set the subsystem version too. Numeric values are also accepted for which. [This option is specific to PE targets.]
  • --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 useful way of reducing the size of a --just-symbols linker input file. ⭐

  • --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. No glow-up, no touch. πŸ™…
  • --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. Here's the rundown:
    • --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 are 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. πŸ’…
  • --decompress-debug-sections β€” decompresses DWARF debug sections. For a .zdebug section, the original name gets restored. ✨
  • --elf-stt-common=yes / --elf-stt-common=no β€” for ELF files, these control whether common symbols should be 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".
  • --merge-notes / --no-merge-notes β€” for ELF files, attempt (or do not attempt) to reduce the size of any SHT_NOTE type sections by removing duplicate notes. Duplicates? Gone. πŸ—‘οΈ
  • -V / --version β€” shows the version number of objcopy.
  • --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: lists all object files modified. In the case of archives, objcopy -V lists all members of the archive. πŸ‘€
  • --help β€” shows a summary of the options to objcopy.
  • --info β€” displays a list showing all architectures and object formats available. Main character energy, honestly. πŸ‘‘
  • @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 gets treated literally, and not removed. πŸŽ€

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; any such options will be processed recursively. πŸŒ€

πŸ‘€ SEE ALSO πŸ‘€

okay bestie, when you're done copying objects and you need the rest of the gang:

  • ld(1) β€” the linker, aka the one who actually puts all your little objects together πŸ’…
  • objdump(1) β€” for when you wanna look at what's inside instead of transforming it πŸ”
  • the Info entries for binutils β€” the deep lore, go read it, it's giving encyclopedia ✨

so, legal moment, but make it cute:

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, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

no cap, that's the whole license situation, don't skip it πŸ™ˆ

and that's the whole page bestie, we ate πŸ“βœ¨ this was binutils-2.46.1, dated 2026-09-07, and it lives under the name OBJCOPY(1) β€” go forth and copy some objects, you absolute icon πŸ‘‘πŸ’–

πŸ“¦ where this page comes from
  • fedora 44: cross-binutils-common 2.46.1-1.fc44 Β· GPL-3.0-or-later AND (GPL-3.0-or-later WITH Bison-exception-2.2) AND (LGPL-2.0-or-later WITH GCC-exception-2.0) AND BSD-3-Clause AND GFDL-1.3-or-later AND GPL-2.0-or-later AND LGPL-2.1-or-later AND LGPL-2.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms