OBJCOPY(1)

๐Ÿ“ฆ
version 2.35.1 ยท 2 versions
, in debian bookworm, ubuntu noble, ubuntu resolute

๐Ÿฆ‹ OBJCOPY(1) โ€” GNU Development Tools ๐Ÿฆ‹

okay bestie, welcome to objcopy โ€” a GNU Development Tools classic and honestly one of the most underrated little utilities in the whole binutils friend group. she copies object files. she translates them. she does it with main character energy and zero drama. let's get into it ๐Ÿ’…


๐ŸŒธ NAME ๐ŸŒธ

objcopy โ€” copy and translate object files โœจ


๐ŸŽ€ 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]
        [-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]
        [-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]
        [--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]
        [--keep-file-symbols]
        [--only-keep-debug]
        [--strip-dwo]
        [--extract-dwo]
        [--extract-symbol]
        [--writable-text]
        [--readonly-text]
        [--pure]
        [--impure]
        [--file-alignment=num]
        [--heap=size]
        [--image-base=address]
        [--section-alignment=num]
        [--stack=size]
        [--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]

๐Ÿ’– DESCRIPTION ๐Ÿ’–

so the GNU objcopy utility copies the contents of an object file into another one. that's the whole vibe. she uses the GNU BFD Library to read and write the object files, and yes she can write the destination object file in a format that's different from the source format. the exact behavior of objcopy is controlled by command-line options. ๐Ÿ“

heads up bestie: 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 her fault, that's just how it is ๐Ÿ˜ญ

objcopy creates temporary files to do its translations and deletes them afterward โ€” clean girl behavior ๐Ÿงนโœจ. objcopy uses BFD to do all its translation work; it has access to all the formats described in BFD and thus is able to recognize most formats without being told explicitly. she just knows ๐Ÿ”ฎ

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

objcopy can be used 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 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 file, big slay ๐Ÿ’…

Note โ€” 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.) ๐Ÿ”ฅ

๐Ÿ‘‘ OPTIONS ๐Ÿ‘‘

  • 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. yes, destructively. she's a little unhinged like that ๐Ÿ™ˆ
  • -I bfdname, --input-target=bfdname โ€” treat the source file's object format as bfdname, instead of trying to deduce it. no guessing games, you're telling her directly ๐Ÿ’โ€โ™€๏ธ
  • -O bfdname, --output-target=bfdname โ€” write the output file using the object format bfdname.
  • -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. copy-paste energy, no translation layer, periodt โœจ
  • -B bfdarch, --binary-architecture=bfdarch โ€” useful when transforming an architecture-less input file into an object file. In this 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
  • -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 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. sneaky little exclusion, love that for us ๐Ÿ˜Œ

  • -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. ๐Ÿšจ girl do not do that

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. the "wait no not that one" of flags ๐Ÿซถ
  • --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.

  • -S, --strip-all โ€” do not copy relocation and symbol information from the source file. full send, nothing kept ๐Ÿงผ
  • -g, --strip-debug โ€” do not copy debugging symbols or sections from the source file. debug girlies, you're on your own ๐ŸŽ€
  • --strip-unneeded โ€” strip all symbols that are not needed for relocation processing. if you're not pulling your weight, you're out โœ‚๏ธ
  • -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 the VIPs ๐Ÿ’–
  • -N symbolname, --strip-symbol=symbolname โ€” do not copy symbol symbolname from the source file. This option may be given more than once. sorry babe, not invited ๐Ÿšซ
  • --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. conditional friend breakup ๐Ÿ˜ญ
  • -G symbolname, --keep-global-symbol=symbolname โ€” keep only symbol symbolname global. Make all other symbols local to the file, so that they are not visible externally. This option may be given more than once. ๐Ÿ‘‘
Note: this option cannot be used in conjunction with the --globalize-symbol or --globalize-symbols options. pick a lane bestie ๐Ÿ’…

  • --localize-hidden โ€” in an ELF object, every symbol with hidden or internal visibility gets marked as local, periodt. And it stacks on top of the symbol-specific localization stuff like -L, so you can run both and they just vibe together ๐Ÿ’…
  • -L symbolname, --localize-symbol=symbolname โ€” takes a global or weak symbol named symbolname and turns it into a local symbol so nobody outside can see it. You can pass this one as many times as you want, go off ๐Ÿ‘‘. One lil heads up tho: unique symbols do NOT get converted, no cap.
  • -W symbolname, --weaken-symbol=symbolname โ€” makes symbolname weak. Also stackable, hand it over as many times as your heart desires โœจ
  • --globalize-symbol=symbolname โ€” gives symbolname global scoping so it's visible outside the file where it was defined. Repeatable too ๐Ÿ’–. But bestie, listen: this one cannot be used together with -G or --keep-global-symbol. Not a drill, it just won't work.
  • -w, --wildcard โ€” this one lets you use regular expressions in the symbol names you drop in other command-line options. The ?, *, \ and [] operators can go anywhere in the symbol name ๐ŸŒˆ. And here's the cute twist: if the first character of the symbol name is an exclamation point (!), the sense of the switch flips for that symbol. Like, for real, check this:
-w -W !foo -W fo*

that would make objcopy weaken all symbols starting with fo except the symbol foo. iconic behavior honestly ๐Ÿ’…

  • -x, --discard-all โ€” do not copy non-global symbols from the source file. bye ๐Ÿ‘‹
  • -X, --discard-locals โ€” do not copy compiler-generated local symbols. (Those lil guys usually start with L or . ๐Ÿฅบ)
  • -b byte, --byte=byte โ€” if you already turned on interleaving with --interleave, this says start the range of bytes to keep at the byteth byte. byte can be anything from 0 to breadth-1, where breadth is whatever value you handed to --interleave ๐Ÿ’ซ
  • -i [breadth], --interleave[=breadth]* โ€” only copy a range out of every breadth bytes. (Header data is unbothered, it stays as is.) You pick which byte in the range kicks off the copy with --byte, and you pick how wide the range is with --interleave-width ๐ŸŽ€

This is the one you want for making files to program ROM. It's typically used with an "srec" output target. And ngl, objcopy will complain if you forget to also specify the --byte option. So don't forget, bestie ๐Ÿ˜ญ

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. cute.

  • --interleave-width=width โ€” used alongside --interleave, this copies width bytes at a time. The start of the range is set by --byte, and the extent of the range comes from --interleave ๐ŸŒธ

The default for this option is 1. And important: the width value plus the byte value from --byte must not exceed the interleave breadth from --interleave. Math check, bestie โœ‹

Real 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' then the outputs would be '1256' and '3478' respectively. the way that works. obsessed ๐Ÿซถ

  • -p, --preserve-dates โ€” sets the access and modification dates of the output file to be the same as the input file's. matching, as they should ๐Ÿ’–
  • -D, --enable-deterministic-archives โ€” operate in deterministic mode. When copying archive members and writing the archive index, use zero for UIDs, GIDs and timestamps, and use consistent file modes for all files ๐Ÿงš

If binutils was configured with --enable-deterministic-archives, then this mode is on by default. You can turn it off with the -U option, right below โฌ‡๏ธ

  • -U, --disable-deterministic-archives โ€” do not operate in deterministic mode. It's the inverse of -D up there: 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. It's not the default because only certain debugging formats are supported and the conversion process can be time consuming ๐Ÿ˜ด
  • --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 that extra space with val โœจ
  • --pad-to address โ€” pad the output file up to the load address address. Done by increasing the size of the last section. The extra space gets filled in with whatever you gave --gap-fill (default is zero) ๐ŸŽ€
  • --set-start val โ€” sets the start address (aka the entry address) of the new file to val. Heads up: not all object file formats support setting the start address ๐Ÿ™ˆ
  • --change-start incr, --adjust-start incr โ€” change the start address (aka entry address) by adding incr. Same caveat: 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 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 so they load somewhere else, the program may fail. not a vibe ๐Ÿ’€
  • --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. See the comments under --change-addresses above ๐Ÿ‘†

If sectionpattern doesn't match any sections in the input file, a warning will be 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 it's the same as the VMA address, which is the address of the section at program run time โ€” but on some systems, especially ones where the program lives 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 matches nothing in the input file, you get a warning 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 same as the LMA address, which is where the section gets loaded into memory โ€” but again, on some systems, especially ROM-based ones, the two can differ ๐ŸŒธ

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 yep, if sectionpattern matches nothing in the input file, a warning gets issued unless --no-change-warnings is used ๐Ÿ’…

  • --change-warnings, --adjust-warnings โ€” if --change-section-address, --change-section-lma or --change-section-vma is used and the section pattern matches no sections, issue a warning. This is the default ๐Ÿ‘‘
  • --no-change-warnings, --no-adjust-warnings โ€” do not issue a warning if --change-section-address, --adjust-section-lma or --adjust-section-vma is used, even when the section pattern matches no sections. silence is golden ๐Ÿคซ
  • --set-section-flags sectionpattern=flags โ€” set the flags for any sections matching sectionpattern. The flags argument is a comma separated string of flag names. The recognized names are alloc, contents, load, noload, readonly, code, data, rom, exclude, share, and debug โœจ

You can set the contents flag for a section that has no contents, but it's not meaningful to clear the contents flag of a section that does have contents โ€” just remove the section instead. Also, not all flags are meaningful for all object file formats. In particular, the share flag is only meaningful for COFF format files, not for ELF format files ๐ŸŽ€

  • --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... ๐Ÿ“
  • --add-section sectionname=filename โ€” add a brand new section named sectionname while copying the file. The contents of the new section come from the file filename. The size of the section will be the size of the file ๐ŸŒˆ

This only works on file formats that can support sections with arbitrary names. And psst โ€” it may be necessary to use --set-section-flags to set the attributes of your newly created section ๐Ÿ‘€

  • --dump-section sectionname=filename โ€” put the contents of the section named sectionname into the file 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 contents as raw binary data, without applying any relocations. And you can specify this option more than once, go off ๐Ÿ”ฅ

  • --update-section sectionname=filename โ€” replace the existing contents of a section named sectionname with the contents of file filename. The size of the section 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 โ€” something that isn't possible using --remove-section followed by --add-section. slay. And yes, this option can be specified more than once ๐Ÿ’–


okay so real talk: you can totally use --rename-section and --update-section together in one command line ๐Ÿ’…. In that case, pass the original section name to --update-section, and the original and new section names to --rename-section. Easy.

  • --add-symbol name=[section:]value[,flags] โ€” Add a new symbol named name while copying the file. This option may be specified multiple times. If the section is given, the symbol will be associated with and relative to that section, otherwise it will be an ABS symbol. Specifying an undefined section will result in a fatal error. There is no check for the value, it will be taken as specified. Symbol flags can be specified and not all flags will be meaningful for all object file formats. By default, the symbol will be global. The special flag 'before=othersym' will insert the new symbol in front of the specified othersym, otherwise the symbol(s) will be added at the end of the symbol table in the order they appear. โœจ
  • --rename-section oldname=newname[,flags] โ€” Rename a section from oldname to newname, optionally changing the section's flags to flags in the process. This has the advantage over using a linker script to perform the rename in that the output stays as an object file and does not become a linked executable. This option accepts the same set of flags as the --sect-section-flags option. ๐ŸŽ€ This option is particularly helpful when the input format is binary, since this will always create a section called .data. If for example, you wanted instead to create a section called .rodata containing binary data you could use the following command line to achieve it:
                     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 the handling of long section names when processing COFF and PE-COFF object formats. The default behaviour, keep, is to preserve long section names if any are present in the input file. The enable and disable options forcibly enable or disable the use of long section names in the output object; when disable is in effect, any long section names in the input object will be truncated. The enable option will only emit long section names if any are present in the inputs; this is mostly the same as keep, but it is left undefined whether the enable option 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. The most common such character is underscore, which compilers often add before every symbol. This option tells objcopy to change the leading character of every symbol when it converts between object file formats. If the object file formats use the same leading character, this option has no effect. Otherwise, it will add a character, or 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 the character. The most common symbol leading character is underscore. This option will remove a leading underscore from all global symbols. This can be useful if you want to link together objects of different file formats with 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 โ€” Reverse the bytes in a section with output contents. A section length must be evenly divisible by the value given in order for the swap to be able to take place. Reversing takes place before the interleaving is performed. This option is used typically in generating ROM images for problematic target systems. For example, on some target boards, the 32-bit words fetched from 8-bit ROMs are re-assembled in little-endian byte order regardless of the CPU byte order. Depending on the programming model, the endianness of the ROM may need to be modified. Consider a simple file with a section containing the following eight bytes: 12345678. Using --reverse-bytes=2 for the above example, the bytes in the output file would be ordered 21436587. Using --reverse-bytes=4 for the above example, the bytes in the output file would be ordered 43218765. By using --reverse-bytes=2 for the above example, followed by --reverse-bytes=4 on the output file, the bytes in the second output file would be ordered 34127856. ๐Ÿ”ฅ
  • --srec-len=ival โ€” Meaningful only for srec output. Set the maximum length of the Srecords being produced to ival. This length covers both address, data and crc fields. โญ
  • --srec-forceS3 โ€” Meaningful only for srec output. Avoid generation of S1/S2 records, creating S3-only record format. ๐ŸŒˆ
  • --redefine-sym old=new โ€” Change the name of a symbol old, to new. This can be useful when one is trying link two things together for which you have no source, and there are name collisions. ๐Ÿ‘‘
  • --redefine-syms=filename โ€” Apply --redefine-sym to each symbol pair "old new" listed in the file filename. filename is simply a flat file, with one symbol pair per line. Line comments may be introduced by the hash character. This option may be given more than once. ๐Ÿ“
  • --weaken โ€” Change all global symbols in the file to be weak. This can be useful when building an object which will be linked against other objects using the -R option to the linker. This option is only effective when using an object file format which supports weak symbols. ๐Ÿ’š
  • --keep-symbols=filename โ€” Apply --keep-symbol option to each symbol listed in the file filename. filename is simply a flat file, with one symbol name per line. Line comments may be introduced by the hash character. This option may be given more than once. ๐Ÿซถ
  • --strip-symbols=filename โ€” Apply --strip-symbol option to each symbol listed in the file filename. filename is simply a flat file, with one symbol name per line. Line comments may be introduced by the hash character. This option may be given more than once. โœ‚๏ธ
  • --strip-unneeded-symbols=filename โ€” Apply --strip-unneeded-symbol option to each symbol listed in the file filename. filename is simply a flat file, with one symbol name per line. Line comments may be introduced by the hash character. This option may be given more than once. ๐Ÿงน
  • --keep-global-symbols=filename โ€” Apply --keep-global-symbol option to each symbol listed in the file filename. filename is simply a flat file, with one symbol name per line. Line comments may be introduced by the hash character. This option may be given more than once. ๐ŸŒŸ
  • --localize-symbols=filename โ€” Apply --localize-symbol option to each symbol listed in the file filename. filename is simply a flat file, with one symbol name per line. Line comments may be introduced by the hash character. This option may be given more than once. ๐Ÿ“
  • --globalize-symbols=filename โ€” Apply --globalize-symbol option to each symbol listed in the file filename. filename is simply a flat file, with one symbol name per line. Line comments may be introduced by 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. โš ๏ธ
  • --weaken-symbols=filename โ€” Apply --weaken-symbol option to each symbol listed in the file filename. filename is simply a flat file, with one symbol name per line. Line comments may be introduced by 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. This is useful in case a machine is 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 does not exist then the value is treated as an absolute number to be stored in the e_machine field of the ELF header. ๐Ÿ”ง
  • --writable-text โ€” Mark the output text as writable. This option isn't meaningful for all object file formats. โœ๏ธ
  • --readonly-text โ€” Make the output text write protected. This option isn't meaningful for all object file formats. ๐Ÿ”’
  • --pure โ€” Mark the output file as demand paged. This option isn't meaningful for all object file formats. ๐Ÿ“„
  • --impure โ€” Mark the output file as impure. This option isn't meaningful for all object file formats. ๐Ÿ’€
  • --prefix-symbols=string โ€” Prefix all symbols in the output file with string. ๐Ÿ”ค
  • --prefix-sections=string โ€” Prefix all section names in the output file with string. ๐Ÿ“š
  • --prefix-alloc-sections=string โ€” Prefix all the names of all allocated sections in the output file with string. ๐Ÿท๏ธ
  • --add-gnu-debuglink=path-to-file โ€” Creates a .gnu_debuglink section which contains a reference to path-to-file and adds it to the output file. Note: the file at path-to-file must exist. Part of the process of adding the .gnu_debuglink section involves embedding a checksum of the contents of the debug info file into the section. If the debug info file is built in one location but it is going to be installed at a later time into a different location then do not use the path to the installed location. The --add-gnu-debuglink option will fail because the installed file does not exist yet. Instead put the debug info file in the current directory and use the --add-gnu-debuglink option without any directory components, like this:
                    objcopy --add-gnu-debuglink=foo.debug

At debug time the debugger will attempt to look for the separate debug info file in a set of known locations. The exact set of these locations varies depending upon the distribution being used, 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. ๐Ÿ›

so as long as the debug info file has been installed into one of these locations before you run the debugger, everything should work correctly. bestie, that's the whole point. ๐Ÿซถ


  • --keep-file-symbols โ€” when you're stripping a file (maybe with --strip-debug or --strip-unneeded), this keeps all the symbols that name source files, which would otherwise get yeeted. don't lose your receipts. ๐Ÿงพ
  • --only-keep-debug โ€” strips a file but does it the fancy way: it removes the contents of any sections that --strip-debug wouldn't touch, and leaves 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 of the section get discarded. why keep the headers?? so other tools can match the debuginfo file up with the real executable, even if that executable has been relocated to a whole different address space. slay, honestly.

the whole intention here is that you use this together with --add-gnu-debuglink to build a two part executable. one 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 to make these files is:

  1. Link the executable as normal. Assuming it is 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, pick whatever. 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 the --add-gnu-debuglink can be the full executable. it does not have to be a file created by the --only-keep-debug switch. lowkey freeing.

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. one (1) file. periodt.
  • --strip-dwo โ€” removes the contents of all DWARF .dwo sections, leaving the remaining debugging sections and all symbols intact. this option is meant to be used by the compiler as part of the -gsplit-dwarf option, which splits debug information between the .o file and a separate .dwo file. the compiler generates all the debug information in the same file, then uses --extract-dwo to copy the .dwo sections over to the .dwo file, then --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 --strip-dwo option for more info, bestie.
  • --file-alignment num โ€” specify the file alignment. sections in the file will always begin at file offsets which are multiples of this number. this defaults to 512. [this option is specific to PE targets.]
  • --heap reserve
  • --heap reserve,commit

specify 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 โ€” use 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 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 โ€” sets the section alignment field in the PE header. sections in memory will always begin at addresses which are a multiple of this number. defaults to 0x1000. [this option is specific to PE targets.]
  • --stack reserve
  • --stack reserve,commit

specify 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 also. numeric values are also accepted for which. [this option is specific to PE targets.]

  • --extract-symbol โ€” keep the file's section flags and symbols but remove all section data. specifically, the option:
    • removes the contents of all sections;
    • sets the size of every section to zero; and
    • 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. multitasking icon behaviour. ๐Ÿ‘‘

  • --compress-debug-sections โ€” compress 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 compression, we don't do that here. ๐Ÿ’€
  • --compress-debug-sections=none
  • --compress-debug-sections=zlib
  • --compress-debug-sections=zlib-gnu
  • --compress-debug-sections=zlib-gabi

for ELF files, these options control how DWARF debug sections are 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 zlib. the debug sections are renamed to begin with .zdebug instead of .debug.

note โ€” if compression would actually make a section larger, then it is not compressed nor renamed. it stays exactly as it is. respect. โœจ
  • --decompress-debug-sections โ€” decompress DWARF debug sections using zlib. the original section names of the compressed sections are restored, like a little glow-up reversal. ๐ŸŒธ
  • --elf-stt-common=yes
  • --elf-stt-common=no

for ELF files, these options 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. de-dupe era. ๐Ÿงš

  • -V
  • --version

show 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: list all object files modified. in the case of archives, objcopy -V lists all members of the archive.

  • --help โ€” show a summary of the options to objcopy.
  • --info โ€” display a list showing all architectures and object formats available.
  • @file โ€” read command-line options from file. the options read are inserted in place of the original @file option. if file does not exist, or cannot be read, then the option will be 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. it's giving inception but make it flags. ๐ŸŒ€


๐Ÿ‘€ SEE ALSO

ld(1), objdump(1), and the Info entries for binutils.


Copyright (c) 1991-2020 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".


and that's a wrap bestie!! ๐ŸŽ€ this whole thing is from binutils-2.35.1, dated 2022-11-21, and yes the manual page really is called OBJCOPY(1). go forth and strip responsibly. ๐Ÿ’–๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from