5 UNIX / Linux objcopy Command Examples

What is objcopy?
5 objcopy examples
Syntax and Options
Related Commands

What is objcopy?

This command is used to copy the object file from one kind of platform (like x86) to another kind of platform(ARM). This utility is used by experts who are trying to port the software from platform to another but do not have the source code to compile on the new platform. Please note that this utility do not change the endianness.

5 objcopy Examples

1. Simply copy object file from source to destination

Consider the following example :

$ objcopy assert new_assert
$

So the above command will copy ‘assert’ into a new file ‘new_assert’. Note that since in this case, ‘assert’ was compiled on the same system so the output ‘new_assert’ will be no different.

2. Copy the object file without providing the new file name

In the above example, the copied file was named as ‘new_assert’ as it was supplied along with options to the command. But, if no destination file name is supplied then the objcopy command replaces the original file with the copied file.

Consider the following example :

$ stat assert
  File: `assert'
  Size: 10914     	Blocks: 24         IO Block: 4096   regular file
Device: 805h/2053d	Inode: 1442346     Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/himanshu)   Gid: ( 1001/  family)
Access: 2012-08-26 18:21:53.010822686 +0530
Modify: 2011-12-30 08:22:09.800547686 +0530
Change: 2012-07-04 19:51:55.364437384 +0530

$ objcopy assert

$ stat assert
  File: `assert'
  Size: 10914     	Blocks: 24         IO Block: 4096   regular file
Device: 805h/2053d	Inode: 1442821     Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/himanshu)   Gid: ( 1001/  family)
Access: 2012-08-26 19:00:24.738500089 +0530
Modify: 2012-08-26 19:00:24.738500089 +0530
Change: 2012-08-26 19:00:24.738500089 +0530

So we see that the statistics of the object file ‘assert’ were completely changed as objcopy created it as a completely new file.

3. Copy only a particular section using -j option

If you wish to copy only one section from source object file to destination object file then -j option is used in this case.

Consider the following example :

$ objcopy -j.interp assert new_assert
$

The above command will actually copy only .interp section into the empty new_assert file.

If we confirm it :

$ objdump -s new_assert 

new_assert:     file format elf64-x86-64

Contents of section .interp:
 400238 2f6c6962 36342f6c 642d6c69 6e75782d  /lib64/ld-linux-
 400248 7838362d 36342e73 6f2e3200           x86-64.so.2.

So we see that this file contains only .interp section (that we copied from ‘assert’ ).

4. Remove only a particular section from the copied file using -R option

This option lets the objdump command to copy the complete source file except the sections specified along with this option.

Consider the following example :

$ objcopy -R.interp assert new_assert

If we confirm :

$ objdump -s -j.interp new_assert 

new_assert:     file format elf64-x86-64

So we see that no .interp section is in the new file ‘new_assert’.

5. Preserve the access and modification dates using -p option

The access and modification dates of the copied file can be preserved (can be kept same as the source) by using -p option along with this command.

Consider the following example:

$ objcopy -p assert new_assert

The above command will preserve the access and modification date/time.

If we confirm :

$ stat assert
  File: `assert'
  Size: 10914     	Blocks: 24         IO Block: 4096   regular file
Device: 805h/2053d	Inode: 1442821     Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/himanshu)   Gid: ( 1001/  family)
Access: 2012-08-26 19:02:35.888521246 +0530
Modify: 2012-08-26 19:00:24.738500089 +0530
Change: 2012-08-26 19:00:24.738500089 +0530

$ stat new_assert
  File: `new_assert'
  Size: 10914     	Blocks: 24         IO Block: 4096   regular file
Device: 805h/2053d	Inode: 1442647     Links: 1
Access: (0755/-rwxr-xr-x)  Uid: ( 1000/himanshu)   Gid: ( 1001/  family)
Access: 2012-08-26 19:02:35.000000000 +0530
Modify: 2012-08-26 19:00:24.000000000 +0530
Change: 2012-08-26 19:17:44.548523497 +0530

So we see that both access and modification time/date was preserved.

Syntax and Options

objcopy [options] infile [outfile]
Short Option Long Option Option Description
-I bfdname –input-target=bfdname Consider the source file’s object format to be bfdname, rather than attempting to deduce it.
-F bfdname –target=bfdname Use bfdname as the object format for both the input and the output file; i.e., simply transfer data from source to destination with no translation.
-j sectionname –only-section=sectionname Copy only the named section from the input file to the output file. This option may be given more than once. Note that using this option inappropriately may make the output file unusable.
-S –strip-all Do not copy relocation and symbol information from the source file.
–strip-unneeded Strip all symbols that are not needed for relocation processing.
-N symbolname –strip-symbol=symbolname Do not copy symbol symbolname from the source file. This option may be given more than once.
-G symbolname –keep-global-symbol=symbolname Keep only symbol symbolname global. Make all other symbols local to the file, so that they are not visible externally. This option may be given more than once.
-L symbolname –localize-symbol=symbolname Make symbol symbolname local to the file, so that it is not visible externally. This option may be given more than once.

Related Commands

ld
objdump

Comments on this entry are closed.

  • Marco July 2, 2013, 9:35 am

    Hi, great post!

    You mentioned objcopy could be used for applications porting when the source code is not available, can you provide a simple example or high level description of this process? I am really interested on it.

    Thanks,
    Marco