LinuxCommandLibrary

objcopy

Copy and translate object files

TLDR

Copy data to another file

$ objcopy [path/to/source_file] [path/to/target_file]
copy

Translate object files from one format to another
$ objcopy --input-target=[input_format] --output-target [output_format] [path/to/source_file] [path/to/target_file]
copy

Strip all symbol information from the file
$ objcopy --strip-all [path/to/source_file] [path/to/target_file]
copy

Strip debugging information from the file
$ objcopy --strip-debug [path/to/source_file] [path/to/target_file]
copy

Copy a specific section from the source file to the destination file
$ objcopy --only-section [section] [path/to/source_file] [path/to/target_file]
copy

SYNOPSIS

objcopy [options] infile [outfile]

PARAMETERS

-S, --strip-all
    Remove all symbol and relocation information from the output file.

-g, --strip-debug
    Remove debug symbols from the output file.

-K , --strip-symbol=
    Remove all symbols named <symbolname> from the output file.

-R , --remove-section=
    Remove the specified section <sectionname> from the output file.

-j , --only-section=
    Copy only the specified section(s) <sectionname> from the input file to the output file.

-O , --output-target=
    Set the output object file format to <bfdname>.

-I , --input-target=
    Set the input object file format to <bfdname>. Useful when input format cannot be determined automatically.

-B , --binary-architecture=
    Set the output architecture for binary dumps. This is for raw binary files without BFD headers.

-p, --preserve-dates
    Preserve the access and modification dates of the output file, matching the input file.

-v, --verbose
    List all object files copied, providing more detailed output.

--version
    Display the version information for objcopy.

--help
    Display a help message and exit.

DESCRIPTION

objcopy is a powerful utility from the GNU Binutils package. It allows users to copy an object file, translating it into a different format if desired, or making various modifications to its contents.

This tool is frequently used for tasks such as stripping symbols (debug, local, or all), changing the architecture or format of an object file, extracting specific sections, or adding/removing sections. It's an essential tool for embedded systems development, optimizing binary sizes, and preparing executables for distribution.

CAVEATS

Stripping symbols, especially all symbols, can make debugging the resultant binary much harder.

Incorrectly changing object file formats or architectures may render the output binary unusable.

Modifying or removing critical sections can break the program's functionality if not done carefully. Not all Binary File Descriptor (BFD) formats support all operations.

COMMON USE CASES

objcopy is extensively used in embedded systems development to convert ELF executable files into raw binary images, Intel HEX, or Motorola S-record formats suitable for direct flashing to microcontrollers. It's also frequently employed to reduce the size of executables and shared libraries by removing unnecessary debug or local symbols before deployment, thereby conserving disk space and memory.

BFD LIBRARY

The power of objcopy comes from its reliance on the BFD (Binary File Descriptor) library. BFD provides a common interface for reading and writing a wide range of object file formats (like ELF, COFF, PE, etc.) and architectures. This abstraction allows objcopy to perform format translations and manipulations without needing specific knowledge of each format internally, making it highly versatile.

HISTORY

objcopy is a fundamental part of the GNU Binutils, a collection of binary tools maintained by the GNU Project since the late 1980s. It was developed to address the need for flexible manipulation of object files across various architectures and formats, playing a crucial role in cross-compilation toolchains and optimizing binaries for different deployment scenarios, especially in embedded systems.

SEE ALSO

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

Copied to clipboard