OBJCOPY(1)
๐ OBJCOPY(1) โ GNU Development Tools ๐
okay bestie, welcome to objcopy, the glow-up station for object files. it's part of the GNU Development Tools situation and it is lowkey one of the most powerful little utilities in your whole bin directory. buckle up, this one is long and we're doing it in parts. โจ
๐ธ NAME ๐ธ
objcopy โ copy and translate object files. that's it. that's the job. copy them, translate them, make them โจdifferentโจ. ๐
๐ฆ SYNOPSIS ๐ฆ
the full vibe-check of every flag it accepts, no cap:
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]
yes bestie, that's a lot of options. and yes, we're going through every single one. ๐๐
๐ฅ DESCRIPTION ๐ฅ
so the GNU objcopy utility copies the contents of an object file to another one. that's the whole concept. objcopy uses the GNU BFD Library to read the object files and to write them โ that's the glow-up engine under the hood. ๐ง
the iconic part: it can write the destination object file in a format different from the source's format. like, format change, main character move. the exact behavior of objcopy is controlled by command-line options โ so literally everything is your choice, you're the director of this movie. ๐
ngl one tiny little caveat: objcopy should be able to copy a fully linked file between any two formats. but copying a relocatable object file between any two formats may not work as expected. not me hyping it up and then warning you. ๐ญ
objcopy creates temporary files to do its translations and deletes them afterward. it uses BFD to do all its translation work, and since it has access to all the formats described in BFD, it can recognize most formats without you having to tell it explicitly. so smart, so self-sufficient. ๐
objcopy can be used to generate S-records by using an output target of srec (e.g., use -O srec). slay.
objcopy can also be used to generate a raw binary file by using an output target of binary (e.g., use -O binary). when it 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. important detail, don't forget it. ๐
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. two little besties doing cleanup duty together. ๐ซถ
note โ and this is important: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 don't come for it about endianness, it simply refuses. ๐ซ
โจ OPTIONS โจ
okay here we go, the buffet. every option, every flag, no skipping. let's eat. ๐
๐ The files themselves
infileoutfileโ the input and output files, respectively. if you do not specifyoutfile,objcopycreates a temporary file and destructively renames the result with the name ofinfile. so like, it will overwrite your input file. be normal about that. ๐ณ
๐ Format / target options
-Ibfdname,--input-target=bfdname โ treat the source file's object format as bfdname, instead of trying to deduce it. you're telling it what's up. โจ-Obfdname,--output-target=bfdname โ write the output file using the object format bfdname. the glow-up target. ๐-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. just a straight copy, no notes. ๐-Bbfdarch,--binary-architecture=bfdarch โ useful when 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. these symbols are called _binary_objfile_start, _binary_objfile_end and _binary_objfile_size. e.g. you can transform a picture file into an object file and then access it in your code using these symbols. iconic behavior tbh. ๐ผ๏ธ
๐ Section wrangling
-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.
-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. ๐ซฃ
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. a little protection spell. ๐ก๏ธ--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. so it's a two-for-one situation, we love a bundled deal. ๐ซ
๐ Stripping (the big glow-down, in a good way)
-S,--strip-allโ do not copy relocation and symbol information from the source file. also deletes debug sections. clean slate energy. ๐งผ-g,--strip-debugโ do not copy debugging symbols or sections from the source file. debug info? don't know her. ๐--strip-unneededโ remove all symbols that are not needed for relocation processing, in addition to debugging symbols and sections stripped by--strip-debug. ruthless. obsessed. ๐ฅ
๐ Symbol-level control
-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. protecting your faves. ๐ฅฐ-Nsymbolname,--strip-symbol=symbolname โ do not copy symbol symbolname from the source file. this option may be given more than once. blocking them, respectfully. ๐ซ--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. it's giving conditional friendship. ๐ญ
and that's part one bestie!! we're not done, there is so much more objcopy to go through โ stay with me, the rest is coming. ๐ฆ
-Gsymbolname โ--keep-global-symbol=symbolname โ bestie, this is the "only the VIP gets to stay global" option ๐ Keep only symbolname global, and everything else gets demoted to local so it's not visible outside the file. You can drop this option more than once if you've got multiple VIPs. ๐
โ ๏ธ Heads up: this cannot be used together with--globalize-symbolor--globalize-symbols. Pick a lane, fr fr.
--localize-hiddenโ in an ELF object, this marks every symbol rockin' hidden or internal visibility as local. And it stacks on top of the symbol-specific localization options like-L, so it's giving layered ๐ง-Lsymbolname โ--localize-symbol=symbolname โ takes a global or weak symbol named symbolname and turns it into a local one, so the outside world can't see it. Repeatable, go off. ๐
Not me about to sayโฆ unique symbols are not converted. Nope, not those ones. ๐
-Wsymbolname โ--weaken-symbol=symbolname โ makes symbolname weak. May be given more than once, so stack 'em up if you need to. ๐ซถ--binary-symbol-prefix=string โ when you're using--input-target=binary, this sets string as the base name for the symbols generated for the input file. Those symbols are:
<string>_start
<string>_end
<string>_size
By default, the binary input handler cooks up the prefix symbol name from a mangled version of the input file name plus the _binary prefix. This option lets you just say it explicitly instead. โจ
--globalize-symbol=symbolname โ gives symbolname global scoping so it's visible outside the file where it's defined. Repeatable! ๐
โ ๏ธ But like, this cannot be used with-Gor--keep-global-symbol. They literally cannot coexist, iykyk.
-wโ--wildcardโ this one lets you use regular expressions in symbolnames across your other command line options. The operators you can sprinkle anywhere in the symbol name are:?(question mark),*(asterisk),\(backslash) and[](square brackets). And if the first character of the symbol name is!, the sense of the switch gets reversed for that symbol. Iconic behaviour tbh. ๐
So for example, this:
-w -W !foo -W fo*
would make objcopy weaken all symbols that start with "fo" except for the symbol "foo". The ! is doing main character work. ๐ค
-xโ--discard-allโ do not copy non-global symbols from the source file. Bye bye, locals. ๐ฎ-Xโ--discard-localsโ do not copy compiler-generated local symbols. Those little guys usually start withLor.โ you know the ones. ๐-bbyte โ--byte=byte โ if interleaving is already enabled via--interleave, this is where the range of bytes to keep starts: at the byteth byte. byte can be anywhere from0tobreadth-1, wherebreadthis whatever you gave the--interleaveoption. ๐ฏ-i[breadth] โ--interleave[=breadth]โ only copy a range out of every breadth bytes. (header data is not affected, so don't worry about that.) You pick which byte in the range kicks off the copy with--byte, and you pick the width of the range with--interleave-width.
This is the move for making files to program ROM ๐, and it's typically used with an "srec" output target. And fr fr, objcopy will complain if you don't also specify --byte. It's not playing around.
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. Slay. โจ
--interleave-width=width โ when used with--interleave, this copies width bytes at a time. The start of the range is set by--byte, and the extent of the range is set with--interleave. Teamwork. ๐ซถ
The default value here is 1. And important: width plus the byte value set by --byte must not exceed the interleave breadth set by --interleave. Do the math bestie, it's not optional. ๐ญ
Here's the iconic use case: you can build images for two 16-bit flashes interleaved in a 32-bit bus by passing -b 0 -i 4 --interleave-width=2 and -b 2 -i 4 --interleave-width=2 to two objcopy commands. If the input was '12345678', the outputs would be '1256' and '3478' respectively. Chef's kiss. ๐
-pโ--preserve-datesโ sets the access and modification dates of the output file to be the same as those of the input file. Matching. ๐
This option 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, it uses zero for UIDs, GIDs, and timestamps, and uses consistent file modes for all files. So reproducible. So cute. ๐ธ
If binutils was configured with --enable-deterministic-archives, then this mode is on by default. And you can turn it off with the -U option, below.
-Uโ--disable-deterministic-archivesโ do not operate in deterministic mode. This is the exact inverse of the-Doption above: when copying archive members and writing the archive index, it uses their actual UID, GID, timestamp, and file mode values. Live your truth. ๐
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 ngl the conversion process can be time consuming. Be patient with her. ๐ญ--gap-fillval โ fill the gaps between sections with val. This applies to the load address (LMA) of the sections. How? By increasing the size of the section with the lower address, and filling in the extra space created with val. Filling in the blanks ๐--pad-toaddress โ pad 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 is zero).--set-startval โ set the start address (also known as the entry address) of the new file to val. But lowkey, not all object file formats support setting the start address. Just so you know. ๐คท--change-startincr โ--adjust-startincr โ change the start address (a.k.a. the entry address) by adding incr to it. Same disclaimer though: not all object file formats support setting the start address.--change-addressesincr โ--adjust-vmaincr โ change the VMA and LMA addresses of all sections, plus the start address, by adding incr. Some object file formats won't permit section addresses to be changed arbitrarily, so that's a thing. ๐
โ ๏ธ Note that this does not relocate the sections. If the program expects sections to be loaded at a certain address, and you use this option to move them somewhere else, the program may fail. Not "might be slightly off" โ may outright fail. Be careful, bestie. ๐ญ
--change-section-addresssectionpattern{=,+,-}val โ--adjust-section-vmasectionpattern{=,+,-}val โ set or change both the VMA address and the LMA address of any section matching sectionpattern. If=is used, the section address is set to val. Otherwise, val is added to or subtracted from the section address. Go read the comments under--change-addressesabove, seriously. ๐
And if sectionpattern doesn't match any sections in the input file, a warning gets issued โ unless --no-change-warnings is used. ๐ข
--change-section-lmasectionpattern{=,+,-}val โ set or change the LMA address of any sections matching sectionpattern. The LMA address is the address 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 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 โ set or change the VMA address of any section matching sectionpattern. The VMA address is the address where the section will be located once the program has started executing.
Normally it's the same as the LMA address, which is the address where the section will be loaded into memory, but โ again โ 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. ๐ข
โ ๏ธ 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. This is the "don't do it unless you know exactly why" zone. ๐
--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 behaviour, so it's already 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 doesn't match any sections. Silent mode. ๐คซ--set-section-flagssectionpattern=flags โ set the flags for any sections matching sectionpattern. The flags argument is a comma separated string of flag names, and the recognized names are:
alloc, contents, load, noload, readonly, code, data, rom, exclude, share, debug, and large.
You can set the contents flag for a section which does not have contents โ but it's not meaningful. And it is not meaningful to clear the contents flag of a section which does have contents โ bestie, just remove the section instead. ๐
Not all flags are meaningful for all object file formats either. 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. Very specific, very slay. โญ
--set-section-alignmentsectionpattern=align โ set the alignment for any sections matching sectionpattern. align specifies the alignment in bytes and must be a power of two, i.e.1,2,4,8.... Math is math. ๐ข
Heads up bestie: setting a section's alignment does not magically align its LMA or VMA too. If those need to shift, you gotta reach for--change-section-lmaand/or--change-section-vma. Also lowkey important: messing with VMAs can cause problems in fully linked binaries, because there might be code out there that expects the section contents to live at their old address. So like. Be careful. ๐
--add-sectionsectionname=filename โ adds a whole new section called sectionname while it's copying the file. The contents of the new section come straight from the file filename, and the section's size is just the size of that file. This only works on file formats that can support sections with arbitrary names. Note โ you may need to use--set-section-flagsto set the attributes of your shiny new section.--dump-sectionsectionname=filename โ takes the contents of the section named sectionname and drops them into the file filename, overwriting whatever was there before (rip to the old contents ๐ชฆ). This one is the inverse of--add-section. It's similar to--only-sectionexcept it doesn't create a formatted file โ it just dumps the raw binary data as-is, with no relocations applied. You can specify this option more than once. โจ--update-sectionsectionname=filename โ replaces the existing contents of a section named sectionname with the contents of filename. The section size gets adjusted to match the file size. The section flags for sectionname stay exactly the same. For ELF format files, the section-to-segment mapping also stays unchanged โ which is something you literally cannot pull off with--remove-sectionfollowed by--add-section. Can be specified more than once. ๐
Note โ you can use--rename-sectionand--update-sectiontogether on one command line to both update and rename a section. If you do that, pass the original section name to--update-section, and pass 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 a section, the symbol gets associated with and is relative to that section โ otherwise it's an ABS symbol. Specifying a section that doesn't exist = fatal error. There's no check on the value, it just takes whatever you said. You can specify symbol flags, but not all flags are meaningful for every object file format. 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 tacked onto the end of the symbol table in the order they appear. ๐--rename-sectionoldname=newname[,flags]โ renames a section from oldname to newname, and optionally swaps the section's flags to flags while it's at it. The glow-up here vs. using a linker script for the rename: your 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 extra helpful when the input format is binary, because that always creates a section called .data. Say you wanted a section called .rodata with binary data in it instead โ you'd do this:
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 enable or disable long section names in the output object; whendisableis in effect, any long section names in the input object get truncated. โ๏ธ Theenableoption only emits long section names if any are present in the inputs โ mostly the same askeep, but it's left undefined whetherenablemight force an empty string table into the output file. (mysterious behaviour, we stan a little ambiguity ๐)--change-leading-charโ some object file formats use special characters at the start of symbols, and the most common one is the underscore, which compilers love to slap in front of every symbol. This option tellsobjcopyto change the leading character of every symbol when it converts between object file formats. If both formats use the same leading character, this option does nothing (no notes ๐ค). Otherwise it'll add a character, 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 it. The most common leading character is the underscore, so this option strips a leading underscore off all global symbols. Handy if you want to link together objects from different file formats with different symbol-name conventions. This is different from--change-leading-charbecause it always changes the symbol name when appropriate, regardless of the output file's object file format. ๐- *
--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, periodt. The reversing happens before interleaving is performed.
This is typically used when generating ROM images for problematic target systems. For example, on some target boards, the 32-bit words fetched from 8-bit ROMs get re-assembled in little-endian byte order no matter what the CPU byte order is. Depending on the programming model, the endianness of the ROM might need to be modified.
Picture a simple file with a section containing these eight bytes: 12345678.
With --reverse-bytes=2 on that example, the bytes in the output file would be ordered 21436587.
With --reverse-bytes=4 on that example, the bytes in the output file would be ordered 43218765.
And if you use --reverse-bytes=2 on the example and then --reverse-bytes=4 on the output file, the bytes in the second output file would be ordered 34127856. math is math ๐งฎโจ
--srec-len=ival โ only meaningful for srec output. Sets the maximum length of the S-records being produced to ival. That length covers the address, data **and crc fields.--srec-forceS3โ only meaningful for srec output. Avoids generating S1/S2 records and creates an S3-only record format. no cap ๐--redefine-symold=new โ changes the name of a symbol from old to new. Useful when you're trying to link two things together but you don't have the source, and there are name collisions. (rename and move on bestie ๐ฆ)- *
--redefine-syms=filename* โ applies--redefine-symto each symbol pair"old new"listed in the file filename. filename is just a flat file with 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. (be soft, be gentle, be weak ๐ซถ)- *
--keep-symbols=filename* โ applies--keep-symbolto each symbol listed in the file filename. filename is just a flat file with one symbol name per line. Line comments can be introduced with the hash character. This option 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 with one symbol name per line. Line comments can be introduced with the hash character. This option 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 with one symbol name per line. Line comments can be introduced with the hash character. This option 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 with one symbol name per line. Line comments can be introduced with the hash character. This option 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 with one symbol name per line. Line comments can be introduced with the hash character. This option 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 with one symbol name per line. Line comments can be introduced with 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* โ applies--weaken-symbolto each symbol listed in the file filename. filename is just a flat file with one symbol name per line. Line comments can be introduced with 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 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. 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 your output file as demand paged. Lowkey this one isn't meaningful for every object file format, so don't be shocked if it just vibes there doing nothing ๐--impureโ marks the output file as impure instead. Same lil disclaimer though: not meaningful for all object file formats, bestie.--prefix-symbols=stringโ slaps string onto the front of every single symbol in the output file. Glow-up via prefix โจ--prefix-symbolsbut for sections โก๏ธ--prefix-sections=stringโ prefixes all section names in the output file with string.--prefix-alloc-sections=stringโ same energy but pickier: it prefixes the names of all the allocated sections in the output file with string. Only the allocated girlies get the makeover ๐--add-gnu-debuglink=path-to-fileโ this one builds a.gnu_debuglinksection that holds a reference to path-to-file, then yeets it into the output file. Periodt.
Heads up bestie: the file at path-to-file must exist. No file, no deal. ๐ญ
Part of adding that .gnu_debuglink section means embedding a checksum of the debug info file's contents into the section. Iconic but also a trap if you're not careful.
So here's the situation that ruins people's whole day: if the debug info file gets built in one location but is going to be installed at a different location later, do not put the path to the installed location. The --add-gnu-debuglink option will fail, because that installed file literally does not exist yet. Instead, drop the debug info file in the current directory and call the option with no directory components at all, like this:
objcopy --add-gnu-debuglink=foo.debug
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 which distribution you're on, 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. Slay.
--keep-section-symbolsโ when you're stripping a file (maybe with--strip-debugor--strip-unneeded), this keeps any symbols that specify section names โ the ones that would otherwise get stripped away. Protect them ๐ซถ--keep-file-symbolsโ same vibe, different symbols: when stripping a file, perhaps with--strip-debugor--strip-unneeded, retain any symbols specifying source file names</>, which would otherwise get stripped. Not today, stripper ๐ค--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. For 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 get discarded. The headers stick around 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. iykyk.
The intention here is that you use this alongside --add-gnu-debuglink to create a two part executable. Part one: a stripped binary that takes up less space in RAM and in a distribution. Part two: a debugging information file that's only needed if you actually want debugging abilities. The suggested procedure to build these files goes like this:
- Link the executable as normal. Assuming it's called
foothen... - 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, bestie. 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 doesn't have to be a file created by the --only-keep-debug switch. Flexible queen ๐
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 don't try it ๐ซ
--strip-dwoโ removes the contents of all DWARF.dwosections, leaving the remaining debugging sections and all symbols intact. This option is meant for the compiler to use as part of the-gsplit-dwarfoption, which splits debug information between the.ofile and a separate.dwofile. The compiler generates all debug info in the same file, then uses the--extract-dwooption to copy the.dwosections over to the.dwofile, then uses--strip-dwoto remove those sections from the original.ofile. It's giving teamwork ๐ง--extract-dwoโ extracts the contents of all DWARF.dwosections. See the--extract-dwo... no wait, see the--strip-dwooption above for more info, bestie ๐--file-alignment numโ specifies 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โ 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โ uses value as the base address of your program or dll. This is the lowest memory location that will be used when your program or dll is loaded. To reduce the need to relocate and improve the 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, the user, to decide if the LMA and VMA need updating. No pressure ๐
--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 for which are"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 for which. [This option is specific to PE targets.] Choose your fighter ๐--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 genuinely useful way of reducing the size of a --just-symbols linker input file. Efficient ๐
--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. Common sense era ๐
--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:
--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 get renamed to begin with.zdebug.--compress-debug-sections=zstdcompresses DWARF debug sections usingzstd.
Note โ if compression would actually make a section larger, then it is not compressed nor renamed. Stay small or stay home โจ
--decompress-debug-sectionsโ decompresses DWARF debug sections. For a.zdebugsection, the original name is restored. Undo button activated ๐--elf-stt-common=yes--elf-stt-common=no
For ELF files, these options control whether common symbols get converted to the "STT_COMMON" or "STT_OBJECT" type. --elf-stt-common=yes converts common symbol type to "STT_COMMON". --elf-stt-common=no converts common symbol type to "STT_OBJECT". Pick a lane ๐
--merge-notes--no-merge-notes
For ELF files, these attempt (or do not attempt) to reduce the size of any SHT_NOTE type sections by removing duplicate notes. Marie Kondo that notes section ๐งน
-V--versionโ shows the version number ofobjcopy. Know your queen's lineage ๐--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, bestie: lists all object files modified. In the case of archives,objcopy -Vlists all members of the archive. Spill the tea, tell me everything โ--helpโ shows a summary of the options toobjcopy. Your little cheat sheet ๐ซถ--infoโ displays a list showing all architectures and object formats available. Window shopping ๐๏ธ@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 is treated literally and not removed. No ghosting, it just stays there.
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 โ and any such options will be processed recursively. It's files all the way down ๐
๐ SEE ALSO ๐
okay bestie, when you're done here, go hang with the rest of the friend group ๐
ld(1)โ the linker, the one that actually puts everything togetherobjdump(1)โ for when you wanna stare at a file instead of rewriting it- the Info entries for
binutilsโ fr fr, the whole squad lives there ๐
๐ COPYRIGHT ๐
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, no Front-Cover Texts, and no Back-Cover Texts. ๐
A copy of the license is included in the section entitled "GNU Free Documentation License".
ngl this one's the legal bestie of the page, but she's iconic and she said what she said โจ
and that's a wrap, my love ๐ซถ this whole glow-up is from binutils-2.47, dated 2026-09-05, and it's giving manual page, but make it serve ๐ bye pookie, go rewrite some object files ๐
๐ธ
๐ฆ where this page comes from
- ubuntu stonking: binutils-arm-none-eabi 1:2.47-6ubuntu1+33
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms