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

-I bfdname
    Specify the input object file format.

-O bfdname
    Specify the output object file format.

-B architecture
    Specify the target architecture.

--strip-all
    Remove all symbol and relocation information.

--strip-debug
    Remove debugging symbols only.

--strip-unneeded
    Remove all symbols that are not needed for relocation processing.

--only-keep-debug
    Strip a file, leaving only debug information.

--add-section sectionname=filename
    Add a new section to the output file.

--remove-section sectionname
    Remove a section from the output file.

--redefine-section oldname=newname
    Rename a section in the output file.

--keep-section sectionname
    Keep only the specified section(s).

--set-section-flags sectionname=flags
    Set the flags of a section.

--rename-section .old=.new
    Rename sections.

DESCRIPTION

The objcopy command copies the contents of one object file to another. It uses the Binary File Descriptor (BFD) library to read and write object files. Objcopy can be used to perform a variety of transformations on object files, including changing the architecture, stripping symbols, and adding or removing sections. It's a crucial tool in the toolchain for embedded systems development, debugging, and reverse engineering. Commonly used for tasks like removing debug information from executables to reduce their size, converting object files to different formats (e.g., ELF to binary), and extracting specific sections from an object file. Objcopy supports a wide range of object file formats, including ELF, COFF, and PE.

CAVEATS

Using objcopy incorrectly can corrupt object files. Make sure to back up your files before using this command. Different versions of objcopy may have slightly different behaviors.

EXAMPLES

Example 1: Strip debugging information from an executable:
objcopy --strip-debug input.elf output.elf

Example 2: Convert an ELF file to a binary file:
objcopy -O binary input.elf output.bin

Example 3: Extract the '.text' section from an object file:
objcopy -j .text input.elf output.text

HISTORY

Objcopy is part of the GNU Binutils suite, which has been under development since the early days of the GNU project. It has evolved over time to support a wide range of object file formats and architectures. It continues to be actively maintained and updated.

SEE ALSO

ld(1), as(1), readelf(1), objdump(1)

Copied to clipboard