objcopy

copies the contents of an object file to another, optionally transforming it

TLDR

Copy an object file to a new path
$ objcopy [source] [dest]
Strip all symbols into a new file
$ objcopy --strip-all [source] [dest]
Strip only debug sections
$ objcopy --strip-debug [source] [dest]
Copy a single section only
$ objcopy --only-section=[section] [source] [dest]
Convert between object formats
$ objcopy --input-target=[in_fmt] --output-target=[out_fmt] [source] [dest]

SYNOPSIS

objcopy [-F bfdname | --target=bfdname] [-I bfdname | --input-target=bfdname] [-O bfdname | --output-target=bfdname] [options] infile [outfile]

DESCRIPTION

objcopy copies the contents of an object file to another, optionally transforming it in the process. It uses the GNU BFD library to read and write object files in various formats including ELF, COFF, S-records, and raw binary.Common uses include: stripping symbols to reduce binary size, extracting debug information to separate files, converting between executable formats (e.g., ELF to raw binary for embedded systems), and copying or removing specific sections from object files.

PARAMETERS

-I bfdname, --input-target=bfdname

Consider the source file's object format to be bfdname
-O bfdname, --output-target=bfdname
Write the output file using the object format bfdname
-F bfdname, --target=bfdname
Use bfdname as the format for both input and output
-j name, --only-section=name
Copy only the specified section to the output
-R name, --remove-section=name
Remove the specified section from the output
-S, --strip-all
Remove all symbol and relocation information
-g, --strip-debug
Remove debugging symbols only
--only-keep-debug
Strip everything except debugging information
--add-gnu-debuglink=file
Add a .gnudebuglink section linking to file_
--dump-section name=file
Dump contents of section name to file
--rename-section old=new[,flags]
Rename section old to new with optional flags
-B bfdarch, --binary-architecture=bfdarch
Set architecture when input is binary format
-i, --info
List all available BFD target formats

INSTALL

sudo apt install binutils
sudo dnf install binutils
sudo pacman -S binutils
sudo apk add binutils
sudo zypper install binutils
brew install binutils
nix profile install nixpkgs#binutils

CAVEATS

Cannot change the endianness of input files; target format must match source endianness or have no endianness (like S-records). Copying relocatable object files between formats may not preserve all information correctly. Fully linked executables copy more reliably across formats.

HISTORY

Part of GNU Binutils, first released in 1991 alongside other binary utilities like objdump and nm. Developed to complement the GNU toolchain (GCC, GAS, ld) for cross-platform development and embedded systems programming.

SEE ALSO

objdump(1), strip(1), readelf(1), nm(1), ld(1)