Next: objdump, Previous: nm, Up: Top
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] [-K symbolname|--keep-symbol= symbolname] [-N symbolname|--strip-symbol= symbolname] [--strip-unneeded-symbol= symbolname] [-G symbolname|--keep-global-symbol= symbolname] [--localize-hidden] [-L symbolname|--localize-symbol= symbolname] [--globalize-symbol= symbolname] [-W symbolname|--weaken-symbol= symbolname] [-w|--wildcard] [-x|--discard-all] [-X|--discard-locals] [-b byte|--byte= byte] [-i interleave|--interleave= interleave] [-j sectionname|--only-section= sectionname] [-R sectionname|--remove-section= sectionname] [-p|--preserve-dates] [--debugging] [--gap-fill= val] [--pad-to= address] [--set-start= val] [--adjust-start= incr] [--change-addresses= incr] [--change-section-address section{=,+,-}val] [--change-section-lma section{=,+,-}val] [--change-section-vma section{=,+,-}val] [--change-warnings] [--no-change-warnings] [--set-section-flags section=flags] [--add-section sectionname=filename] [--rename-section oldname=newname[,flags]] [--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] [--globalize-symbols= filename] [--weaken-symbols= filename] [--alt-machine-code= index] [--prefix-symbols= string] [--prefix-sections= string] [--prefix-alloc-sections= string] [--add-gnu-debuglink= path-to-file] [--keep-file-symbols] [--only-keep-debug] [--extract-symbol] [--writable-text] [--readonly-text] [--pure] [--impure] [-v|--verbose] [-V|--version] [--help] [--info] infile [outfile]
The gnu objcopy utility copies the contents of an object file to another. objcopy uses the gnu bfd Library to read and write the object files. It can write the destination object file in a format different from that of the source object file. The exact behavior of objcopy is controlled by command-line options. Note that 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.
objcopy creates temporary files to do its translations and deletes them afterward. objcopy uses bfd to do all its translation work; it has access to all the formats described in bfd and thus is able to recognize most formats without being told explicitly. See BFD (Using LD).
objcopy can be used to generate S-records by using an output target of srec (e.g., use -O srec).
objcopy can be used to generate a raw binary file by using an output target of binary (e.g., use -O binary). When objcopy generates a raw binary file, it will essentially produce a memory dump of the contents of the input object file. All symbols and relocation information will be discarded. 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.
Note—objcopy is not able to change the endianness of its input files. If the input format has an endianness (some formats do not), objcopy can only copy the inputs into file formats that have the same endianness or which have no endianness (e.g., srec). (However, see the --reverse-bytes option.)
infile outfile The input and output files, respectively. If you do not specify outfile, objcopy creates a temporary file and destructively renames the result with the name of infile.-w -W !foo -W fo*
would cause objcopy to weaken all symbols that start with “fo” except for the symbol “foo”.
-x --discard-all Do not copy non-global symbols from the source file. srec
output
target.
Change the start address by adding incr. Not all object file
formats support setting the start address.
--change-addresses
incr
--adjust-vma
incr
Change the VMA and LMA addresses of all sections, as well as the start
address, by adding incr. Some object file formats do not permit
section addresses to be changed arbitrarily. Note that this does not
relocate the sections; if the program expects sections to be loaded at a
certain address, and this option is used to change the sections such
that they are loaded at a different address, the program may fail.
--change-section-address
section
{=,+,-}
val
--adjust-section-vma
section
{=,+,-}
val
Set or change both the VMA address and the LMA address of the named
section. If = is used, the section address is set to
val. Otherwise, val is added to or subtracted from the
section address. See the comments under --change-addresses,
above. If section does not exist in the input file, a warning will
be issued, unless --no-change-warnings is used.
--change-section-lma
section
{=,+,-}
val
Set or change the LMA address of the named section. 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 those 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. If section does not exist in the input file, a warning
will be issued, unless --no-change-warnings is used.
--change-section-vma
section
{=,+,-}
val
Set or change the VMA address of the named section. The VMA
address is the address where the section will be located once the
program has started executing. Normally this is the same as the LMA
address, which is the address where the section will be loaded into
memory, but on some systems, especially those where a program is held in
ROM, the two can be different. If = is used, the section address
is set to val. Otherwise, val is added to or subtracted
from the section address. See the comments under
--change-addresses, above. If section does not exist in
the input file, a warning will be issued, unless
--no-change-warnings is used.
--change-warnings
--adjust-warnings
If --change-section-address or --change-section-lma or
--change-section-vma is used, and the named section does not
exist, issue a warning. This is the default.
--no-change-warnings
--no-adjust-warnings
Do not issue a warning if --change-section-address or
--adjust-section-lma or --adjust-section-vma is used, even
if the named section does not exist.
--set-section-flags
section
=
flags
Set the flags for the named section. The flags argument is a
comma separated string of flag names. The recognized names are
alloc, contents, load, noload,
readonly, code, data, rom, share, and
debug. You can set the contents flag for a section which
does not have contents, but it is not meaningful to clear the
contents flag of a section which does have contents–just remove
the section instead. Not all flags are meaningful for all object file
formats.
--add-section
sectionname
=
filename
Add a new section named sectionname while copying the file. The
contents of the new section are taken from the file filename. The
size of the section will be the size of the file. This option only
works on file formats which can support sections with arbitrary names.
--rename-section
oldname
=
newname
[,
flags
]
Rename a section from oldname to newname, optionally
changing the section's flags to flags in the process. This has
the advantage over usng a linker script to perform the rename in that
the output stays as an object file and does not become a linked
executable.
This option is particularly helpful when the input format is binary, since this will always create a section called .data. If for example, you wanted instead to create a section called .rodata containing binary data you could use the following command line to achieve it:
objcopy -I binary -O <output_format> -B <architecture> \ --rename-section .data=.rodata,alloc,load,readonly,data,contents \ <input_binary_file> <output_object_file>
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
.
The intention is that this option will be used in conjunction with --add-gnu-debuglink to create a two part executable. One a stripped binary which will occupy less space in RAM and in a distribution and the second a debugging information file which is only needed if debugging abilities are required. The suggested procedure to create these files is as follows:
foo
then...
objcopy --only-keep-debug foo foo.dbg
to
create a file containing the debugging info.
objcopy --strip-debug foo
to create a
stripped executable.
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:
foo
to foo.full
objcopy --strip-debug foo
objcopy --add-gnu-debuglink=foo.full foo
i.e., the file pointed to by the --add-gnu-debuglink can be the full executable. It does not have to be a file created by the --only-keep-debug switch.
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.
--extract-symbol Keep the file's section flags and symbols but remove all section data. Specifically, the option: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.
-V --version Show the version number of objcopy.Corrections, suggestions, and new documentation should be posted to the Forum.
The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.