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 [option]... infile [outfile]

PARAMETERS

-h, --help
    Display usage summary and exit

-V, --version
    Print version information

-i, --info
    List BFD targets/formats

-I bfdname, --input-target=bfdname
    Set input file format (e.g., elf64-x86-64)

-O bfdname, --output-target=bfdname
    Set output file format (e.g., binary, ihex)

-F bfdname, --target=bfdname
    Set both input and output format

-B bfdarch, --binary-architecture=bfdarch
    Set output architecture (e.g., i386)

-j name, --only-section=name
    Copy only specified section(s)

-R name, --remove-section=name
    Remove specified section(s)

-S, -g, --strip-debug
    Remove debugging symbols

--strip-all, -s
    Remove all symbols and relocations

--strip-unneeded
    Remove unneeded symbols

-N name, --strip-symbol=name
    Remove specific symbol

--only-keep-debug
    Extract debug info to separate file

--add-gnu-debuglink=file
    Add .gnu_debuglink section pointing to debug file

--extract-symbol
    Extract symbols to separate file

--dump-section name=file
    Dump section contents to file

--rename-section old=new[,flags]
    Rename section and set flags

--change-section-address section{=+| -}val
    Change section VMA/LMA

--adjust-section-vma section{=+| -}delta
    Adjust section VMA

--set-section-flags section=flags
    Set section flags (alloc,load,etc.)

--change-addresses symbols=exp
    Transform addresses matching symbols

--weaken-symbols=filename
    Weaken symbols from file

--localize-symbols=filename
    Localize hidden symbols

--globalize-symbols=filename
    Globalize symbols from file

--keep-symbols=filename
    Keep only symbols from file

--localize-hidden
    Localize hidden symbols

--compress-sections[=TYPE]
    Compress eligible sections (zlib, none)

--update-section address=contents
    Update section contents

-C/i, --preserve-symbol-info
    Preserve common symbol info

DESCRIPTION

objcopy is a versatile utility from the GNU Binutils suite for copying, converting, and manipulating object files, libraries, and executables. It supports translation between formats like ELF, PE, COFF, a.out, and raw binary, making it ideal for cross-compilation, embedded systems, and debugging workflows.

Key capabilities include stripping symbols or sections (--strip-all, --strip-debug), extracting debug info (--only-keep-debug), adding GNU debug links (--add-gnu-debuglink), dumping sections to files (--dump-section), renaming sections/symbols (--rename-section), compressing sections, and adjusting addresses/VMAs. It processes single files or archives (.a files) and preserves or modifies metadata like timestamps and permissions.

Often used in build pipelines (e.g., Makefile rules for stripping releases), firmware extraction, or reverse engineering, objcopy requires BFD library support for formats/architectures. Verify targets with objcopy --help --info. It handles relocatable objects, shared libraries, and core dumps with fine-grained control.

CAVEATS

Not all BFD targets/archs supported on every system; use --info to list. May alter file permissions/timestamps. Risk of corrupting files if formats mismatch. Archives (.a) processed recursively but options apply per member.

COMMON EXAMPLES

objcopy --strip-debug prog prog.stripped : Strip debug info.
objcopy -O binary kernel.elf kernel.bin : ELF to raw binary.
objcopy --only-keep-debug prog prog.debug
objcopy --add-gnu-debuglink=prog.debug prog : Link stripped binary to debug file.
objcopy -j .text -O ihex prog.elf prog.hex : Extract text section to Intel Hex.

FORMAT DISCOVERY

List targets: objcopy --help --info
Detect format: file infile or objdump -f infile.

HISTORY

Developed as part of GNU Binutils by the Free Software Foundation since 1990 (Binutils 1.0). Evolved with GCC/GDB toolchains, adding support for new ISAs (ARM, RISC-V), formats (WebAssembly), and features like DWARF debug handling. Maintained actively for modern Linux distributions.

SEE ALSO

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

Copied to clipboard