OBJCOPY(1)
๐ OBJCOPY ๐
ok so this is objcopy from the GNU Development Tools, and bestieโฆ she is the object-file glow-up machine. Let's get into it ๐ซถ
โจ NAME โจ
objcopy โ copy and translate object files. that's it. that's the whole vibe. ๐ฅ
๐ SYNOPSIS ๐
lowkey a lot going on here but like, just breathe and read it:
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]
[--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 ๐ธ
ok so the GNU objcopy utility copies the contents of an object file into another one. iconic behavior honestly. โจ objcopy uses the GNU BFD Library to read and write the object files, and 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 โ she's very "you tell me what you want and I'll do it" energy.
โ ๏ธ heads up bestie: 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 her fault, it's just how it is ๐ญ
objcopy creates temporary files to do its translations and deletes them afterward. she uses BFD to do all her translation work โ she has access to all the formats described in BFD and thus can recognize most formats without being told explicitly. main character who already knows everyone at the party ๐
you can use objcopy to generate S-records by using an output target of srec (e.g., use -O srec). and 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, and the memory dump will start at the load address of the lowest section copied into the output file. gone. deleted. she doesn't keep that. ๐ฆ
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. just little tips from your girl ๐
๐ Note โ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.) so likeโฆ no endian-flipping, that's not her job, but there is a workaround-ish option, iykyk.
โญ OPTIONS โญ
okay this is the big one. buckle up bestie, every single flag is here:
infileoutfileโ the input and output files, respectively. if you do not specify outfile,objcopycreates a temporary file and destructively renames the result with the name of infile. ๐ซฃ "destructively renames" like she's not even sorry.-I bfdname,--input-target=bfdnameโ treat the source file's object format as bfdname, rather than attempting to deduce it. no detective work, you just tell her.-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. like copy-paste but make it binary ๐-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_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 pic into code, that's so 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 ๐ญ so like, be careful. 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. the ! is literally "not you bestie" ๐
-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-jand-Roptions together results in undefined behaviour โ like, genuinely undefined, don't do it, that's a "we don't know what happens" situation ๐
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 "no wait not THAT one" flag ๐ซถ--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.
--strip-section-headersโ strip section header. this option is specific to ELF files. implies--strip-alland--merge-notes. โจ she takes the whole header section out.-S,--strip-allโ do not copy relocation and symbol information from the source file. also deletes debug sections. everything must go ๐งน-g,--strip-debugโ do not copy debugging symbols or sections from the source file.--strip-unneededโ remove all symbols that are not needed for relocation processing, in addition to debugging symbols and sections stripped by--strip-debug. the "if you're not contributing to relocations you're out" flag ๐ค-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. your fave gets VIP access ๐-N symbolname,--strip-symbol=symbolnameโ do not copy symbol symbolname from the source file. this option may be given more than once.--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.
ok that's part 1, and there's so much more where that came fromโฆ ๐
-Gsymbolname, **--keep-global-symbol=*symbolname*** โ okay so this one keeps *only*symbolnameglobal, and makes literally every other symbol local to the file so they're not visible externally. You can drop this option more than once bestie, it stacks. **But** โ ๏ธ heads up: you cannot use it alongside--globalize-symbolor--globalize-symbols`, they fight.--localize-hiddenโ in an ELF object, this marks every symbol that has hidden or internal visibility as local. And it stacks โ it applies on top of symbol-specific localization options like-L. ๐-Lsymbolname,--localize-symbol=*symbolname*** โ takes a global or weak symbol calledsymbolname` and converts it into a local symbol, so it's not visible externally anymore. Repeatable, use it as many times as you want. ๐ Small note: unique symbols are **not converted, no cap.-Wsymbolname, **--weaken-symbol=*symbolname*** โ makes symbolsymbolname` weak. Also repeatable, go off. ๐ฆ- **
--globalize-symbol=*symbolname*** โ givessymbolnameglobal scoping so it's visible outside the file it's defined in. Repeatable. โ ๏ธ But it **cannot** be used in conjunction with-Gor--keep-global-symbol` โ pick a side, babe. -w,--wildcardโ this one lets you use regular expressions in the symbolnames you pass to other command line options. The?,*,\and[]operators can go anywhere in the symbol name. And if the first character of the symbol name is!then the sense of the switch is flipped for that one symbol. For example:
-w -W !foo -W fo*
would make objcopy weaken all symbols that start with "fo" except for the symbol "foo". The exclusion ate, honestly. โจ
-x,--discard-allโ do not copy non-global symbols from the source file. Simple as that, we're leaving them behind. ๐-X,--discard-localsโ do not copy compiler-generated local symbols. Those ones usually start withLor.btw, so you know exactly which ones we're ghosting. ๐-bbyte, **--byte=*byte*** โ so *if* interleaving has been enabled via--interleave, this starts the range of bytes to keep at the *byte*th byte.bytecan be anything from0tobreadth-1, where breadth is the value you gave to--interleave`.-i[breadth],--interleave[=*breadth*]โ only copies a range out of everybreadthbytes. (Header data is not affected, fyi.) You pick which byte in the range starts the copy with--byte, and you pick the width of the range with--interleave-width. ๐
This one is genuinely iconic for making files to program ROM with, and it's typically used with an "srec" output target. โ ๏ธ Note that objcopy will complain if you don't also specify the --byte option โ it needs both, periodt.
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 and efficient. โจ
- **
--interleave-width=*width*** โ when used with the--interleaveoption, this copieswidthbytes at a time. The start of the range is set by--byte, and the extent of the range is set with--interleave`.
Default value is 1. And the math has to work: the value of width plus the byte value set by --byte must not exceed the interleave breadth set by --interleave. ๐ฅ
This is how you make images for two 16-bit flashes interleaved in a 32-bit bus โ pass -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. Look at that split, she's beautiful ๐ญ๐
-p,--preserve-datesโ sets the access and modification dates of the output file to be the same as the input file's. Time travel but make it file metadata. ๐ฐ๏ธ
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 gets used as the date stored in the header, interpreted as the number of seconds since the Unix epoch.
-D,--enable-deterministic-archivesโ operate in deterministic mode. When copying archive members and writing the archive index, use zero for UIDs, GIDs, timestamps, and use consistent file modes for all files. 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-Dabove: 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. Simple as. โจ
--debuggingโ converts debugging information, if possible. It's not the default because only certain debugging formats are supported, and ngl the conversion process can be time consuming. ๐ฎโ๐จ--gap-fillval โ fills the gaps between sections withval. This applies to the load address (LMA) of the sections. How? By increasing the size of the section with the lower address, and filling in the extra space created withval.--pad-toaddress โ pads the output file up to the load addressaddress. Done by increasing the size of the last section, and the extra space gets filled in with the value specified by--gap-fill(default is zero). ๐--set-startval โ sets the start address (also known as the entry address) of the new file toval. โ ๏ธ Not all object file formats support setting the start address, so vibe check your format first.--change-startincr,--adjust-startincr โ changes the start address (again, the entry address) by addingincr. โ ๏ธ Same caveat: 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 addingincr. Some object file formats do not permit section addresses to be changed arbitrarily.
โ ๏ธ Big one: 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 the sections so they load at a different address, the program may fail. Do not do this casually bestie.
--change-section-addresssectionpattern{=,+,-}val,--adjust-section-vmasectionpattern{=,+,-}val โ sets or changes both the VMA address and the LMA address 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 (yes, including the dangerous part ๐). And ifsectionpatterndoesn't match any sections in the input file, a warning gets issued โ unless--no-change-warningsis used.--change-section-lmasectionpattern{=,+,-}val โ sets or changes the LMA address of any sections matchingsectionpattern. So what's LMA? It's the address 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 at program run time), but on some systems โ especially ones where a program is held in ROM โ the two can be different. ๐ง
If = is used, the section address is set to val. Otherwise val is added to or subtracted from the section address. See the comments under --change-addresses above. And if sectionpattern doesn't match any sections in the input file, a warning gets issued โ unless --no-change-warnings is used.
--change-section-vmasectionpattern{=,+,-}val โ sets or changes the VMA address of any section matchingsectionpattern. The VMA is the address where the section will be located once the program has started executing. Normally it's the same as the LMA (the address where the section is loaded into memory), but on some systems โ again, especially ones where a program lives in ROM โ 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 if sectionpattern doesn't match any sections, a warning gets issued โ unless --no-change-warnings is used.
โ ๏ธ Note: changing the VMA of sections in a fully linked binary can be dangerous, since there may be code that expects the sections to be located at their old address. She said what she said.
--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 behavior. ๐ฃ--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 doesn't match any sections. Silence is golden, apparently ๐คซ--set-section-flagssectionpattern=flags โ sets the flags for any sections matchingsectionpattern. Theflagsargument is a comma separated string of flag names, and the recognized ones arealloc,contents,load,noload,readonly,code,data,rom,exclude,share,debug, andlarge. ๐ท๏ธ
You can set the contents flag for a section which doesn't have contents, but it's not meaningful to clear the contents flag of a section that does have contents โ just remove the section instead, babe. ๐
Not all flags are meaningful for all object file formats. In particular the share flag is only meaningful for COFF format files and not for ELF format files. And the ELF x86-64 specific flag large corresponds to SHF_X86_64_LARGE.
--set-section-alignmentsectionpattern=align โ sets the alignment for any sections matchingsectionpattern.alignspecifies the alignment in bytes and must be a power of two, i.e.1,2,4,8.... No chaos allowed, only powers of two โญ
โ ๏ธ Note: setting a section's alignment will not automatically align its LMA or VMA addresses. If those need changing too, use the--change-section-lmaand/or--change-section-vmaoptions. Also note that changing VMAs can cause problems in fully linked binaries, where there may be code that expects the contents of the sections to be located at their old address. Yep, again. ๐ญ
--add-section sectionname=filenameโ okay so this one adds a brand-new section named sectionname while copying the file ๐. The contents of the new section come straight from filename, and the section size is literally the size of that file. It only works on file formats that can support sections with arbitrary names. Note โ it may be necessary to use--set-section-flagsto set the attributes of the newly created section.--dump-section sectionname=filenameโ puts the contents of the section named sectionname into filename, overwriting anything that was already there ๐ . This is the inverse of--add-section. Itโs similar to--only-sectionexcept it does not create a formatted file โ it just dumps the contents as raw binary data, without applying any relocations. Can be specified more than once.--update-section sectionname=filenameโ replaces the existing contents of the section named sectionname with the contents of filename. The section 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 remains unchanged โ something that is not possible with--remove-sectionfollowed by--add-section. Can be specified more than once.
Note โ you can use --rename-section and --update-section to both update and rename a section from one command line. In that case, pass the original section name to --update-section, and the original and new section names to --rename-section.
--add-symbol name=[section:]value[,flags]โ adds a new symbol named name while copying the file. May be specified multiple times. If section is given, the symbol will be associated with and relative to that section; otherwise itโs an ABS symbol. Specifying an undefined section results in a fatal error ๐ญ. There is no check for the value โ itโs taken as specified. Symbol flags can be specified, and not all flags will be meaningful for all object file formats. By default, the symbol is global. The special flag'before=othersym'inserts the new symbol in front of the specified othersym; otherwise the symbol(s) get added at the end of the symbol table in the order they appear.--rename-section oldname=newname[,flags]โ renames a section from oldname to newname, optionally changing the sectionโs flags to flags in the process โจ. This has the advantage over using a linker script to perform the rename because the output stays as an object file and does not become a linked executable. It accepts the same set of flags as--set-section-flags. This is especially helpful when the input format is binary, since that always creates a section called.data. If, for example, you wanted to create a section called.rodatacontaining binary data instead, you could use 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 are handled 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 only emits long section names if any are present in the inputs โ 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, and the most common one is an underscore, which compilers often add before every symbol ๐ธ. This tellsobjcopyto change the leading character of every symbol when it converts between object file formats. If the formats use the same leading character, this has no effect. 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, it removes that character โ๏ธ. The most common leading character is underscore. This removes a leading underscore from all global symbols. Useful if you want to link together objects of different file formats with different conventions for symbol names. Itโs 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 ๐ฅ. A section length must be evenly divisible by the value given for the swap to be able to take place. Reversing happens before interleaving is performed. This is used typically when 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 these 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. 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-sym old=newโ changes the name of a symbol from 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 ๐.--redefine-syms=filenameโ applies--redefine-symto each symbol pair"old new"listed in 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โ changes 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-Roption to the linker. This option is only effective when using an object file format which supports weak symbols.--keep-symbols=filenameโ applies--keep-symbolto each symbol listed in 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โ applies--strip-symbolto each symbol listed in 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โ applies--strip-unneeded-symbolto each symbol listed in 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โ applies--keep-global-symbolto each symbol listed in 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โ applies--localize-symbolto each symbol listed in 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โ applies--globalize-symbolto each symbol listed in 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 โ ๏ธ.--weaken-symbols=filenameโ applies--weaken-symbolto each symbol listed in 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 thee_machinefield of the ELF header.--writable-textโ marks the output text as writable. This option isnโt meaningful for all object file formats.--readonly-textโ makes the output text write protected. This option isnโt meaningful for all object file formats.--pureโ marks the output file as demand paged. This option isnโt meaningful for all object file formats.--impureโ marks the output file as impure. This option isnโt meaningful for all object file formats.--prefix-symbols=stringโ prefixes all symbols in the output file with string.--prefix-sections=stringโ prefixes all section names in the output file with string ๐.--prefix-alloc-sections=stringโ Slap string onto the front of every allocated section name in the output file. Cute little name tag for sections, periodt. ๐--add-gnu-debuglink=path-to-fileโ Makes a.gnu_debuglinksection that points at path-to-file and drops it into the output file. Part of this whole process is embedding a checksum of the debug info fileโs contents into that section.
Note: the file at path-to-file absolutely must exist. No ghost files, bestie. ๐ป
If your debug info file gets built in one place but is gonna be installed later somewhere else, do not use the path to the installed location. --add-gnu-debuglink will fail because the installed file doesnโt exist yet. Instead, put the debug info file in the current directory and use --add-gnu-debuglink with no directory components, like this:
objcopy --add-gnu-debuglink=foo.debug
At debug time, the debugger tries to find the separate debug info file in a set of known locations. The exact set varies by 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 these locations before the debugger runs, everything should work correctly. โจ
--keep-section-symbolsโ When stripping a file โ maybe with--strip-debugor--strip-unneededโ keep any symbols that specify section names. Those would otherwise get stripped. Protect the section-name girlies. ๐ซถ--keep-file-symbolsโ When stripping a file โ again, maybe with--strip-debugor--strip-unneededโ keep any symbols that specify source file names. Those would otherwise get stripped too. ๐--only-keep-debugโ Strips a file by removing the contents of any sections that would not be stripped by--strip-debug, while 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 headers stay 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. Itโs giving โreceipts but no actual stuff inside.โ ๐ญ
The intention is that this option gets used with --add-gnu-debuglink to create a two-part executable: one stripped binary that takes up less space in RAM and in a distribution, and one debugging information file thatโs only needed if debugging abilities are required. The suggested procedure is:
- 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 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. So no, it canโt hold your whole debug harem. ๐
--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. Compiler choreography, honestly. ๐ง--extract-dwoโ Extracts the contents of all DWARF.dwosections. See the--strip-dwooption for more info, bestie. ๐ฅ--file-alignment numโ Sets the file alignment. Sections in the file 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โ Says how many bytes of memory to reserve (and optionally commit) for this programโs heap. This option is specific to PE targets. Heap space secured. ๐ซถ--image-base valueโ Uses value as the base address of your program or dll. This is the lowest memory location 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.
It sets the section alignment field in the PE header โ if one is present in the binary. Sections in memory 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-lmaor--set-section-vmaoptions, and the file has been fully relocated, then a warning message will be issued. After that itโs up to the user to decide if the LMA and VMA need updating. Youโve been warned, diva. ๐จ
--stack reserve--stack reserve,commitโ Says how many bytes of memory to reserve (and optionally commit) for this programโs stack. This option is specific to PE targets. Stack snack secured. ๐ฅช--subsystem which--subsystem which:major--subsystem which:major.minorโ Specifies the subsystem under which your program will execute. Legal values for which arenative,windows,console,posix,efi-app,efi-bsd,efi-rtd,sal-rtd, andxbox. You may 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, the 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. Minimalist icon behavior. โจ
--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 squeeze. ๐
--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. The section stays exactly as it is, no forced makeover. ๐
--decompress-debug-sectionsโ Decompresses DWARF debug sections. For a.zdebugsection, the original name is restored. Name rehab complete. ๐ฆ--elf-stt-common=yes--elf-stt-common=noโ For ELF files, these options control whether common symbols should be converted to theSTT_COMMONorSTT_OBJECTtype.--elf-stt-common=yesconverts common symbol type toSTT_COMMON.--elf-stt-common=noconverts common symbol type toSTT_OBJECT.
Symbol type switch-up, no cap. ๐
--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. Duplicate note cleanup, very demure. ๐งน-V--versionโ Shows the version number ofobjcopy. The classic โwho are youโ moment. ๐--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. Endianness stays in charge, bestie. ๐-v--verboseโ Verbose output: lists all object files modified. In the case of archives,objcopy -Vlists all members of the archive. Yes, thatโs what the page says โ keep it exactly like that. ๐--helpโ Shows a summary of the options toobjcopy. The menu, basically. ๐--infoโ Displays a list showing all architectures and object formats available. The whole buffet of targets. ๐@fileโ Reads command-line options from file. The options read are inserted in place of the original@fileoption. If file does not exist, or cannot be read, then the option is 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 options inception, fr fr. ๐
๐ SEE ALSO ๐
ld(1),objdump(1), and the Info entries forbinutils.
๐ COPYRIGHT ๐
Okay legal besties, gather round, because this part is giving official paperwork energy ๐
Copyright (c) 1991-2025 Free Software Foundation, Inc.โ yep, thatโs the exact copyright line, protect it with your life ๐ซถ
The official permission vibes are: youโre allowed to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation โ with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License" โจ
This iconic little manual page is from binutils-2.45.1, dated 2026-01-23. Byeee, go read your licenses bestie ๐๐ฆ
๐ฆ where this page comes from
- arch rolling: riscv32-elf-binutils 2.45.1-1 ยท GPL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms