OBJCOPY(1)
๐ธ NAME ๐ธ
okay so bestie, meet objcopy(1) from GNU Development Tools โ she literally exists to copy and translate object files. iconic behavior, no cap ๐
โจ 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]
๐ฆ DESCRIPTION ๐ฆ
okay so the GNU objcopy utility copies the contents of an object file to another object file. she uses the GNU BFD Library to read and write the object files, and she can write the destination object file in a format that's different from the source object file's format. every little behavior of objcopy is controlled by command-line options, which is very main-character of her.
quick vibe check though: 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. so likeโฆ be aware, bestie ๐ซถ
objcopy creates temporary files to do its translations and deletes them afterward. it 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 ๐
you can use objcopy to generate S-records by using an output target of srec (e.g., use -O srec). you can also use it 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. 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.
bestie, note 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.) ๐ญ
๐ OPTIONS ๐
okay bestie, here's the flag-by-flag runthrough โ yes, every single one in this part, no skipping, periodt โจ
infile,outfileโ the input and output files, respectively. if you do not specifyoutfile,objcopycreates a temporary file and destructively renames the result with the name ofinfile. yes, destructively. she said what she said ๐-I bfdname,--input-target=bfdnameโ treat the source file's object format asbfdname, instead of trying to deduce it. no guessing, just vibes.-O bfdname,--output-target=bfdnameโ write the output file using the object formatbfdname. slay.-F bfdname,--target=bfdnameโ usebfdnameas 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 but make it object file ๐-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 tobfdarch. this option will be ignored if the input file has a knownbfdarch. 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_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. iconic little trick, ngl ๐ผ๏ธโจ-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 insectionpattern.
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. the ! is giving โnot herโ energy ๐
-R sectionpattern,--remove-section=sectionpatternโ remove any section matchingsectionpatternfrom 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 insectionpattern.
undefined behaviour alert: using both the-jand-Roptions together results in undefined behaviour. do not do this to yourself, bestie ๐ญ
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. protecting her fave section, honestly ๐ซถ
--keep-section=sectionpatternโ when removing sections from the output file, keep sections that matchsectionpattern. literally โyou can go, but not you, you're stayingโ ๐--remove-relocations=sectionpatternโ remove non-dynamic relocations from the output file for any section matchingsectionpattern. 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 insectionpattern. 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. she's got priorities ๐
--strip-section-headersโ strips the section header. this option is specific to ELF files. it implies--strip-alland--merge-notes. ELF girlies only ๐-S,--strip-allโ do not copy relocation and symbol information from the source file. also deletes debug sections. she's cleaning house ๐งนโจ-g,--strip-debugโ do not copy debugging symbols or sections from the source file. debug info? not invited to the function.--strip-unneededโ remove all symbols that are not needed for relocation processing, in addition to debugging symbols and sections stripped by--strip-debug. ruthless but make it efficient ๐-K symbolname,--keep-symbol=symbolnameโ when stripping symbols, keep symbolsymbolnameeven if it would normally be stripped. this option may be given more than once. bestie is on the keep list ๐ซถ-N symbolname,--strip-symbol=symbolnameโ do not copy symbolsymbolnamefrom the source file. this option may be given more than once. blocked. unfollowed. stripped. ๐--strip-unneeded-symbol=symbolnameโ do not copy symbolsymbolnamefrom the source file unless it is needed by a relocation. this option may be given more than once. not to be dramatic, but it's giving โconditional friendshipโ ๐ญ-Gsymbolname,--keep-global-symbol=*symbolname*โ bestie, this one says: keep onlysymbolnameglobal. Everything else gets demoted to local so it's not visible externally, periodt. You can drop this option more than once. Heads up tho: you literally cannot combine it with--globalize-symbolor--globalize-symbolsโ they fight, and the tool will not be having it.--localize-hiddenโ for ELF objects, this snaps every symbol that has hidden or internal visibility into a local symbol. And it stacks on top of the symbol-specific localization stuff like-L, so it's giving "yes and" energy ๐-Lsymbolname,--localize-symbol=*symbolname*โ turns a global or weak symbol named symbolname into a local one, so it's not visible externally anymore. You can give this more than once. Note: unique symbols do not get converted โ they simply don't play that game.-Wsymbolname,--weaken-symbol=*symbolname*โ makes symbolname weak ๐ฅบ. Can be given more than once.--binary-symbol-prefix=*string*โ when you're running it with--input-target=binary, this sets string as the base name for the symbols generated for the input file. The symbols it's talking about are:
<string>_start
<string>_end
<string>_size
By default the binary input handler just derives the prefix symbol name from a mangled version of the input file name plus the _binary prefix. This option lets you say it explicitly instead. No more guessing games โจ
--globalize-symbol=*symbolname*โ the glow-up option ๐ gives symbolname global scoping so it's visible outside the file it's defined in. Can be given more than once. Note: this one cannot be used in conjunction with-Gor--keep-global-symbolโ those two are lowkey enemies.-w,--wildcardโ lets you use regular expressions in the symbolnames you pass to other command line options. The?,*,\and[]operators can go absolutely anywhere in the symbol name. And if the first character of the symbol name is!, the sense of the switch gets reversed for that symbol. Like, for example:
-w -W !foo -W fo*
would make objcopy weaken all symbols that start with "fo" except for the symbol "foo". Iconic exclusion behaviour, no cap ๐
-x,--discard-allโ do not copy non-global symbols from the source file. Bye ๐-X,--discard-localsโ do not copy compiler-generated local symbols. (These usually start withLor.โ yeah, the compiler's little scribbles.)-bbyte,--byte=*byte*โ if interleaving is already enabled via--interleave, this sets where the range of bytes to keep starts โ at the byteth byte. byte can be anywhere from0tobreadth-1, wherebreadthis whatever value you gave to--interleave.-i[breadth],--interleave[=*breadth*]โ only copy a range out of every breadth bytes. (Header data is not affected, it's built different.) Which byte in the range kicks off the copy? That's the--byteoption's job. How wide is the range? That's--interleave-width.
This is the ROM-programming era option โ super useful for making files to program ROM, and it's typically used with an "srec" output target. Just so you know: objcopy will complain at you if you don't also specify --byte. It's not flexible about this.
The default interleave breadth is 4, so with --byte set to 0, objcopy would copy the first byte out of every four bytes from the input to the output. Cute, efficient, slay.
--interleave-width=*width*โ when used with--interleave, copy width bytes at a time. Where the range starts is set by--byte, and the extent of the range is set with--interleave.
The default value is 1. And here's the catch: width plus the byte value from --byte must not exceed the interleave breadth from --interleave. Math check, bestie ๐งฎ
You can use this to make 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. Literally splitting the friend group, and both halves ate ๐
-p,--preserve-datesโ sets the access and modification dates of the output file to be the same as the input file's. Keeping the timestamps alive, we love to see it ๐ซถ
It also copies the date stored in a PE format file's header โ unless the SOURCE_DATE_EPOCH environment variable is defined. If it is defined, then that variable is used as the date stored in the header, interpreted as the number of seconds since the Unix epoch.
-D,--enable-deterministic-archivesโ deterministic mode, engaged โจ When copying archive members and writing the archive index, it uses zero for UIDs, GIDs, timestamps, and uses consistent file modes for all files. Everything's the same every time, it's giving consistency queen.
If binutils was configured with --enable-deterministic-archives, then this mode is on by default. You can turn it off with the -U option, below.
-U,--disable-deterministic-archivesโ do not operate in deterministic mode. It's the exact inverse of-D: when copying archive members and writing the archive index, it uses their actual UID, GID, timestamp, and file mode values. The real real, no filter ๐
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. So it's a "when you need it" vibe, not an "always on" vibe.--gap-fillval โ fills the gaps between sections with val. This applies to the load address (LMA) of the sections, and it works by growing the size of the section with the lower address and filling in that extra space with val. Filling the silence ๐ซถ--pad-toaddress โ pads the output file up to the load address address. Done by increasing the size of the last section, and the extra space gets filled in with whatever--gap-fillsays (default zero if you never set it).--set-startval โ sets 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 โ some of them are just not built for that.--change-startincr,--adjust-startincr โ changes the start address (aka the entry address) by adding incr to it. Again: not all object file formats support setting the start address.--change-addressesincr,--adjust-vmaincr โ changes the VMA and LMA addresses of all sections, plus the start address, by adding incr. Some object file formats don't permit section addresses to be changed arbitrarily, so don't be shocked. Also, important note: this does not relocate the sections. If the program expects sections to be loaded at a certain address, and you use this option to change them so they load somewhere else, the program may fail. Like, catastrophically. You've been warned ๐ญ--change-section-addresssectionpattern{=,+,-}val,--adjust-section-vmasectionpattern{=,+,-}val โ sets or changes 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-addressesabove (yes, the scary ones). And if sectionpattern doesn't match any sections in the input file, a warning gets issued โ unless you used--no-change-warnings.--change-section-lmasectionpattern{=,+,-}val โ sets or changes the LMA address of any sections matching sectionpattern. The LMA address is where the section gets loaded into memory at program load time. Normally that's the same as the VMA address (which is where the section is at program run time), but on some systems โ especially ones where the program lives in ROM โ the two can differ. If=is used, the section address is set to val. Otherwise val is added or subtracted. See the comments under--change-addressesabove. If sectionpattern matches nothing in the input file, warning time, unless--no-change-warningsis in play.--change-section-vmasectionpattern{=,+,-}val โ sets or changes 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 (where it gets loaded into memory), but on some systems โ again, ROM situations โ they can be different. If=is used, the section address is set to val. Otherwise val is added or subtracted. See the comments under--change-addressesabove. No matching sections? Warning, unless--no-change-warnings.
โ ๏ธ Also 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. Do not do this casually, bestie. This is not a vibe-check situation, this is a "you will break the binary" situation ๐
--change-warnings,--adjust-warningsโ if--change-section-addressor--change-section-lmaor--change-section-vmais used and the section pattern doesn't match any sections, issue a warning. This is the default. The tool is looking out for you ๐ซถ--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 matches zero sections. Silence is golden, apparently ๐--set-section-flagssectionpattern=flags โ sets the flags for any sections matching sectionpattern. The flags argument is a comma separated string of flag names. The recognized names arealloc,contents,load,noload,readonly,code,data,rom,exclude,share,debug, andlarge.
You can set the contents flag for a section that doesn't have contents, but it's not meaningful โ and it's not meaningful to clear the contents flag of a section that does have contents either. Just remove the section instead, it's cleaner. Not all flags are meaningful for all object file formats, obviously. In particular the share flag is only meaningful for COFF format files and not for ELF format files. And the ELF x86-64 specific flag large corresponds to SHF_X86_64_LARGE โจ
--set-section-alignmentsectionpattern=align โ sets 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.... Powers of two only, the math has to math ๐ง
๐ heads up bestie โ setting a section's alignment does not magically align its LMA or VMA addresses too. If you need those moved, you gotta reach for--change-section-lmaand/or--change-section-vmaseparately, no cap. And fr, changing VMAs can cause problems in fully linked binaries, because there may be code in there that literally expects the section contents to sit at their old address. so likeโฆ be careful. ๐
--add-sectionsectionname=filename โ bestie, this one adds a brand new section called sectionname while it's copying the file. the contents of the new section come straight from the file filename, and the size of the section will be the size of that file. it only works on file formats that can support sections with arbitrary names, so vibe check your format first. ๐ note: it may be necessary to also use the--set-section-flagsoption to set the attributes of your freshly created section.--dump-sectionsectionname=filename โ the inverse of--add-section, iconic behavior honestly. it places the contents of the section named sectionname into the file filename, overwriting any contents that may have been there before (sorry, old data ๐ญ). this one is similar to the--only-sectionoption except that it does not create a formatted file โ it just dumps the contents as raw binary data, without applying any relocations. you can specify this option more than once. โจ--update-sectionsectionname=filename โ replace the existing contents of a section named sectionname with the contents of the file filename. the size of the section will be adjusted to the size of the file, and the section flags for sectionname will stay unchanged. ๐ซถ for ELF format files the section to segment mapping also remains unchanged โ something which is literally not possible using--remove-sectionfollowed by--add-section. this option can be specified more than once.
๐ธ note: you can use--rename-sectionand--update-sectiontogether to both update and rename a section from one single command line. in that case, pass the original section name to--update-section, and pass 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. 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'll be an ABS symbol. โญ specifying an undefined section will result in a fatal error โ so maybe don't. 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-sectionoldname=newname[,flags]โ renames a section from oldname to newname, optionally changing the section's flags to flags while it's at it. the advantage over using a linker script to do the rename is 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--set-section-flagsoption.
this one is particularly helpful when the input format is binary, since that will always create a section called .data. so if, for example, you wanted to create a section called .rodata containing binary data instead, 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. ๐enableanddisableforcibly enable or disable the use of long section names in the output object; whendisableis in effect, any long section names in the input object will be truncated ๐ญ. theenableoption will only emit long section names if any are present in the inputs โ this is mostly the same askeep, but it is 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. the most common such character is underscore, which compilers often add before every symbol. this option tellsobjcopyto 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'll 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, so this option will remove a leading underscore from all global symbols. super useful if you want to link together objects of different file formats with 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 โ 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. math is mathing ๐
--srec-len=ival โ meaningful only for srec output. sets 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. avoids generation of S1/S2 records, creating S3-only record format. ๐--redefine-symold=new โ change the name of a symbol old, to new. this can be useful when you're trying to link two things together for which you have no source, and there are name collisions. we've all been there ๐ญ--redefine-syms=filename โ apply--redefine-symto 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. useful when building an object which will be linked against other objects using the-Roption to the linker. this option is only effective when using an object file format which supports weak symbols. ๐--keep-symbols=filename โ apply--keep-symboloption 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-symboloption 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-symboloption 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-symboloption 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-symboloption 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-symboloption 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-Gor--keep-global-symboloptions. pick a lane bestie.
--weaken-symbols=filename โ apply--weaken-symboloption 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. 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 thee_machinefield 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 either. โญ--pureโ Marks the output file as demand paged. This option isnโt meaningful for every object file format, bestie. ๐--impureโ Marks the output file as impure. Also not meaningful for all object file formats. ๐ซถ--prefix-symbols=stringโ Prefixes all symbols in the output file withstring. Slay.--prefix-sections=stringโ Prefixes all section names in the output file withstring. โจ--prefix-alloc-sections=stringโ Prefixes all the names of all allocated sections in the output file withstring. ๐--add-gnu-debuglink=path-to-fileโ Creates a.gnu_debuglinksection that contains a reference topath-to-file, then adds it to the output file. Note: the file atpath-to-filemust exist. Part of adding the.gnu_debuglinksection 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 is going to be installed later 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 doesnโt 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 on 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.
As long as the debug info file has been installed into one of these locations before the debugger is run, everything should work correctly. ๐
--keep-section-symbolsโ When stripping a file โ maybe with--strip-debugor--strip-unneededโ retains any symbols specifying section names, which would otherwise get stripped. ๐ฆ--keep-file-symbolsโ When stripping a file โ maybe with--strip-debugor--strip-unneededโ retains any symbols specifying source file names, which would otherwise get stripped. ๐ธ--only-keep-debugโ Strips a file, removing contents of any sections that would not be stripped by--strip-debug, and leaving the debugging sections intact. In ELF files, this preserves all note sections in the output. ๐ญ
Note โ the section headers of the stripped sections are preserved, including their sizes, but the contents of the section are discarded. The section headers are preserved so that 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 intention is that this option will be used together with --add-gnu-debuglink to create 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โs only needed if debugging abilities are required. The suggested procedure to create these files is as follows:
- Link the executable as normal. Assuming itโs called
"foo"thenโฆ - Run
"objcopy --only-keep-debug foo foo.dbg"to create a file containing the debugging info. - Run
"objcopy --strip-debug foo"to create a stripped executable. - 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. Also the "--only-keep-debug" step is optional. You could instead do this:
- Link the executable as normal.
- Copy
"foo"to"foo.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 doesnโt 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 option is intended for use by the compiler as part of the-gsplit-dwarfoption, which splits debug information between the.ofile and a separate.dwofile. The compiler generates all debug information in the same file, then uses the--extract-dwooption to copy the.dwosections to the.dwofile, then the--strip-dwooption to remove those sections from the original.ofile. ๐--extract-dwoโ Extracts the contents of all DWARF.dwosections. See the--strip-dwooption for more info. โญ--file-alignment numโ Specifies the file alignment. Sections in the file will always begin at file offsets that are multiples of this number. This defaults to512. [This option is specific to PE targets.] ๐--heap reserve--heap reserve,commitโ Specifies the number of bytes of memory to reserve (and optionally commit) to be used as heap for this program. [This option is specific to PE targets.] ๐--image-base valueโ Usesvalueas 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 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 that 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 have not 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. It will then be up to the user to decide if the LMA and VMA need updating. ๐ญ
--stack reserve--stack reserve,commitโ Specifies the number of bytes of memory to reserve (and optionally commit) to be used as stack for this program. [This option is specific to PE targets.] ๐ธ--subsystem which--subsystem which:major--subsystem which:major.minorโ Specifies the subsystem under which your program will execute. The legal values forwhichare"native","windows","console","posix","efi-app","efi-bsd","efi-rtd","sal-rtd", and"xbox". You may optionally set the subsystem version too. Numeric values are also accepted forwhich. [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. ๐--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 are compressed.--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 obsoleted zlib-gnu format. 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 is restored. ๐--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=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, attempts (or does not attempt) to reduce the size of anySHT_NOTEtype sections by removing duplicate notes. ๐ฅ-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. ๐ธ@fileโ Reads command-line options fromfile. The options read are inserted in place of the original@fileoption. Iffiledoes 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. ๐
๐ธ SEE ALSO ๐ธ
okay so when you're done vibing with objcopy and you want more unix besties to hang out with, go check these out:
ld(1)โ the linker bestie, fr fr. different job, same friend group.objdump(1)โ literally the reader toobjcopy's writer, the way they're such an iconic duo ๐- and the Info entries for
binutilsโ yes, the whole info system, not just a man page. it's giving deep lore ๐
๐ COPYRIGHT ๐
okay so the legal bit, and no we can't skip it, it's important:
Copyright (c) 1991-2026 Free Software Foundation, Inc.
and here's the permission part โ you are granted permission 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, and this matters:
- no Invariant Sections โจ
- no Front-Cover Texts โจ
- no Back-Cover Texts โจ
and a copy of the license is included in the section entitled "GNU Free Documentation License" โ so like, it's right there, go read it, iykyk ๐
lowkey the whole "no Invariant Sections, no Front-Cover Texts, no Back-Cover Texts" thing is just the license saying you have full freedom to remix this doc โ slay ๐
and that's the whole page bestie, you made it to the end, main character energy fr fr ๐๐
this glorious manual page is from binutils-2.47, dated 2026-08-31, and it's the OBJCOPY(1) page โ go forth and copy your objects, you absolute icon ๐ฆ๐
๐ฆ where this page comes from
- arch rolling: riscv64-linux-gnu-binutils 2.47-1 ยท GPL-2.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms