OBJCOPY(1)
β¨ 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:objcopyis not able to change the endianness of its input files. if the input format has an endianness (some formats do not),objcopycan only copy the inputs into file formats that have the same endianness or which have no endianness (e.g.,srec). (however, see the--reverse-bytesoption. iykyk. π)
π OPTIONS π
infile,outfileβ the input and output files, respectively. if you don't specifyoutfile,objcopycreates a temporary file and destructively renames the result with the name ofinfile. so like, be aware of that. she's thorough. π-Ibfdname,--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. π£οΈ-Obfdname,--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. β¨-Fbfdname,--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. π¦-Bbfdarch,--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_endand_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. πΌοΈβ‘οΈπ»-jsectionpattern,--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. π
-Rsectionpattern,--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-jand-Roptions 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.pltfrom an executable or shared library with--remove-relocations=.pltwill 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-alland--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-debugbut with extra attitude. π₯-Ksymbolname,--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 π«Ά-Nsymbolname,--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 ONLYsymbolnameglobal, 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-symbolor--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 calledsymbolnameinto 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β makesymbolnameweak. Can be given more than once. πͺ--binary-symbol-prefix=stringβ when used with--input-target=binary, setsstringas 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β givesymbolnameglobal scoping so it's visible outside the file where it's defined. Can be given more than once.
Note: cannot be used with-Gor--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 withLor...) π§Ή-b byte,--byte=byteβ if interleaving is enabled via--interleave, start the range of bytes to keep at thebytethbyte.bytecan be from0tobreadth-1, wherebreadthis the value from--interleave. π§©-i [breadth],--interleave[=breadth]β only copy a range out of everybreadthbytes. (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, copywidthbytes at a time. The start of the range is set by--byte, and the extent is set with--interleave. The default is1.widthplus thebytevalue from--bytemust 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=2and-b 2 -i 4 --interleave-width=2to 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 theSOURCE_DATE_EPOCHenvironment 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 withval. 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 withval. π³οΈ--pad-to addressβ pad the output file up to the load addressaddress. 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 toval. 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 addingincr. 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 addingincr. 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 matchingsectionpattern. If=is used, the section address is set toval. Otherwise,valis added to or subtracted from the section address. See the comments under--change-addressesabove. Ifsectionpatterndoes not match any sections in the input file, a warning is issued unless--no-change-warningsis used. β οΈ--change-section-lma sectionpattern{=,+,-}valβ set or change the LMA address of any sections matchingsectionpattern. 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 toval. Otherwise,valis added to or subtracted from the section address. See the comments under--change-addressesabove. Ifsectionpatterndoes not match any sections in the input file, a warning is issued unless--no-change-warningsis used. ποΈ--change-section-vma sectionpattern{=,+,-}valβ set or change the VMA address of any section matchingsectionpattern. 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 toval. Otherwise,valis added to or subtracted from the section address. See the comments under--change-addressesabove. Ifsectionpatterndoes not match any sections in the input file, a warning is issued unless--no-change-warningsis 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-addressor--change-section-lmaor--change-section-vmais 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-addressor--adjust-section-lmaor--adjust-section-vmais used, even if the section pattern does not match any sections. π€«--set-section-flags sectionpattern=flagsβ set the flags for any sections matchingsectionpattern. Theflagsargument is a comma separated string of flag names. Recognized names arealloc,contents,load,noload,readonly,code,data,rom,exclude,share,debug, andlarge. You can set thecontentsflag for a section which does not have contents, but it is not meaningful to clear thecontentsflag of a section which does have contentsβjust remove the section instead. Not all flags are meaningful for all object file formats. In particular theshareflag is only meaningful for COFF format files and not for ELF format files. The ELF x86-64 specific flaglargecorresponds toSHF_X86_64_LARGE. π·οΈ--set-section-alignment sectionpattern=alignβ set the alignment for any sections matchingsectionpattern.alignspecifies 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-sectionsectionname=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-flagsto set the attributes of the newly created section. π--dump-sectionsectionname=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-sectionexcept it does not create a formatted file β it just dumps the raw binary data, no relocations applied. Can be specified more than once. π¦--update-sectionsectionname=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-sectionfollowed 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-symbolname=[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 anABSsymbol. 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 flagbefore=othersyminserts 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-sectionoldname=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.enableanddisableforcibly turn the use of long section names on or off in the output object; whendisableis in effect, any long section names in the input object get truncated.enablewill only emit long section names if any are present in the inputs β mostly the same askeep, but it's left undefined whetherenablemight 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 tellsobjcopyto 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-charbecause 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-symold=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-symto 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-Roption to the linker. Only effective when using an object file format that supports weak symbols.--keep-symbols=filenameβ applies--keep-symbolto 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-symbolto 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-symbolto 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-symbolto 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-symbolto 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-symbolto 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-Gor--keep-global-symboloptions. π--weaken-symbols=filenameβ applies--weaken-symbolto 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 thee_machinefield 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β slapsstringon 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 getsstringprefixed onto it. π--prefix-alloc-sections=stringβ and this one prefixesstringonto 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_debuglinksection holding a reference topath-to-fileand drops it into the output file. β οΈ Real talk: the file atpath-to-filemust 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-debugor--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-debugwouldn'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:
- Link the executable as normal. Assuming it's called
foo, then... - Run
objcopy --only-keep-debug foo foo.dbgto create a file containing the debugging info. - Run
objcopy --strip-debug footo create a stripped executable. - Run
objcopy --add-gnu-debuglink=foo.dbg footo add a link to the debugging info into the stripped executable.
Note β the choice of.dbgas an extension for the debug info file is completely arbitrary. Also the--only-keep-debugstep is optional. You could instead do this:
- Link the executable as normal.
- Copy
footofoo.full. - Run
objcopy --strip-debug foo. - 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.dwosections, leaving the remaining debugging sections and all symbols intact. This one's intended for the compiler to use as part of the-gsplit-dwarfoption, which splits debug info between the.ofile and a separate.dwofile. The compiler generates all debug info in the same file, then uses--extract-dwoto copy the.dwosections into the.dwofile, then uses--strip-dwoto remove those sections from the original.ofile. π¦--extract-dwoβ extracts the contents of all DWARF.dwosections. See the--strip-dwooption 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 to512. [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β usesvalueas 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 is0x400000for executables, and0x10000000for 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-lmaor--set-section-vmaoptions, 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 withSHF_COMPRESSEDfrom 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=noneis equivalent to--decompress-debug-sections.--compress-debug-sections=zliband--compress-debug-sections=zlib-gabiare equivalent to--compress-debug-sections.--compress-debug-sections=zlib-gnucompresses DWARF debug sections using the obsoletedzlib-gnuformat. The debug sections are renamed to begin with.zdebug.--compress-debug-sections=zstdcompresses 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.zdebugsection, 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=yesconverts common symbol type to"STT_COMMON".--elf-stt-common=noconverts common symbol type to"STT_OBJECT".--merge-notes/--no-merge-notesβ for ELF files, attempt (or do not attempt) to reduce the size of anySHT_NOTEtype 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 -Vlists 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@fileoption. 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 β¨
π COPYRIGHT π
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