objcopy Command in Linux



The objcopy command in Linux copies and translates the object files. It is primarily used to copy or modify object files in various ways, such as changing formats, stripping symbols, removing sections, or adding new sections. The objcopy command uses the GNU BFD Library to read and write object files and can handle most formats automatically.

Table of Contents

Here is a comprehensive guide to the options available with the objcopy command −

Note − The behavior of the objcopy command may vary slightly across systems due to the handling of architecture-specific formats.

Installation of objcopy Command

By default, the objcopy tool may not be available in Linux to install it, use the instructions given below −

To install objcopy on Ubuntu, Kali Linux, Debian, and other Debian-based distributions, use the following command −

sudo apt install binutils

To install it on Arch Linux, use the command given below −

sudo pacman -S aarch64-linux-gnu-binutils

To install it on CentOS, use −

sudo yum install binutils

To install it on Fedora, use the following command −

sudo dnf install binutils-arc-linux-gnu

To verify the installation, check the version of the objcopy command −

objcopy -V
objcopy Command in Linux1

Or check the binary path using the which command −

which objcopy
objcopy Command in Linux2

Syntax of objcopy Command

The syntax of the objcopy command is as follows −

objcopy [options] [input-file] [output-file]

The [options] field is used to specify various options to change the command's behavior. The [input-file] is the object file, and the [output-file] is the file in which the modified content of the input file will be saved.

objcopy Command Options

The options of the Linux objcopy command are listed below −

Flags Options Description
-I --input-target <bfdname> Specify the input file format as <bfdname>
-O --output-target <bfdname> Specify the output file format as <bfdname>
-B --binary-architecture <arch> Set the output architecture if the input lacks one
-F --target <bfdname> Set both input and output formats to <bfdname>
--debugging Convert debugging information if possible
--add-gnu-debuglink=<file> Add .gnu_debuglink section linking to <file>
-D --enable-deterministic-archives Produce deterministic archives (default)
-U --disable-deterministic-archives Disable deterministic archive behavior
-j --only-section <name> Copy only section <name> to the output
-K --keep-symbol <name> Retain symbol <name> without stripping
--keep-section-symbols Retain all section symbols
--keep-file-symbols Retain all file symbols
--localize-hidden Convert all ELF hidden symbols to local symbols
-L --localize-symbol <name> Explicitly mark symbol <name> as local
--globalize-symbol <name> Explicitly mark symbol <name> as global
-G --keep-global-symbol <name> Localize all symbols except <name>
-p --preserve-dates Copy modified/access timestamps to the output
-R --remove-section <name> Remove section <name> from the output
--remove-relocations <name> Remove relocations from section <name>
--strip-section-headers Strip section headers
--strip-dwo Remove all .dwo sections
--extract-dwo Copy only .dwo sections
-S --strip-all Remove all symbols and relocation information
-g --strip-debug Remove all debugging symbols and sections
--strip-unneeded Remove symbols not needed for relocations
-N --strip-symbol <name> Exclude symbol <name>
--only-keep-debug Retain only debug information
--redefine-sym <old>=<new> Rename symbol <old> to <new>
--pad-to <addr> Pad the last section up to <addr>
--set-start <addr> Set the start address to <addr>
--rename-section old=new Renames a section
-v --verbose Display details of modified files
-V --version To display the command's version
--decompress-debug-sections Decompress DWARF debug sections
-W --weaken-symbol <name> Mark symbol <name> as weak
--weaken Mark all global symbols as weak
-w --wildcard Allow wildcard usage in symbol-matching
-x --discard-all Remove all non-global symbols
-X --discard-locals Remove all compiler-generated symbols
-i --interleave[=<number>] Copy only N out of every <number> bytes
--interleave-width <number> Define N for use with the --interleave option
-h --help Show help information
@<file> Read options from <file>
--info List supported object formats and architectures

Examples of objcopy Command in Linux

This section demonstrates the usage of the objcopy command in Linux with examples −

Converting a File to another Format

To convert a file to another format, use the -O or --output-target option with the objcopy command −

objcopy -O binary code output.bin
objcopy Command in Linux3

The above command converts the code which is an elf to binary. Other formats that can be used to convert an elf file are listed below −

Format Description
elf32-mips ELF file for the 32-bit MIPS architecture
elf64-mips ELF file for 64-bit MIPS architecture
srec Motorola S-records format
ihex Intel Hexadecimal object file format
binary Raw binary format (no headers or metadata)
tekhex Tektronix hexadecimal object file format
a.out-i386 a.out file format for 32-bit Intel x86
elf64-x86-64 ELF file for 64-bit x86-64 architecture
pei-i386 PE/COFF file for 32-bit Intel x86 (Windows executables)
pei-x86-64 PE/COFF file for 64-bit x86-64
elf32-arm ELF file for 32-bit ARM architecture

These formats are dependent on the architecture of the system.

Stripping Debug Symbol

To strip the debug symbol, use the --strip-debug option with the objcopy command −

objcopy --strip-debug code output.elf

In the above command, the code is the input file, while output.elf is the output file with stripped debug information.

Extracting a Specific Section

To extract a specific section from the object file, use the -j or --only-section option. For example, to extract .text section use the following command −

objcopy -j .text -O binary code text_section.bin

Removing a Specific Section

To remove a specific section, from the object file, use the -R or --remove-section option. For example, to remove the .data section use the command given below −

objcopy -R .data code output.elf
objcopy Command in Linux4

Saving Debug Information in a Separate File

To keep the debug information in a separate file, use the command given below −

objcopy --only-keep-debug code output.elf

Removing Unused Symbols

To remove all the unused symbols, use the --strip-unneeded option −

objcopy --strip-unneeded code output.elf

Renaming a Section

To rename a section, use the --rename-section option. For example, to rename the .text section to .mytext use the following command −

objcopy --rename-section .text=.mytext code output.elf
objcopy Command in Linux5

Preserving the Dates

To preserve the access and modification timestamps of the output file, matching them to those of the input file, use the -p or --preserve-dates option −

objcopy -p code output.elf

Marking a Symbol as Weak

To mark a symbol as weak, use the --weaken-symbol option with the objcopy command −

objcopy --weaken-symbol=symbol code output.elf

The symbol in the above command can be a function or variable.

Displaying Help

To display help related to the objcopy command, use the -h or --help option −

objcopy --help

Conclusion

The objcopy command in Linux is a handy tool for manipulating object files. It supports various options for customizing the behavior, including the ability to extract or remove specific sections, rename them, and manage debugging information.

Overall, objcopy is an essential utility for managing and optimizing object files in Linux environments.

Advertisements