Instructions for using objcopy in Makefile

objcopy copies the contents of one type of target file to another type of target file.

objcopy tool usage guide

objcopy parameter selection

Copy the binary file, possibly converting it in the process:
Usage: arm-wrs-linux-gnueabi-objcopy [option(s)] in-file [out-file]
The options are as follows:

-I --input-target <bfdname> Assume input file is in format <bfdname> Assume input file is in format <bfdname>
  -O --output-target <bfdname> Create an output file in format <bfdname> Create an output file in the following format
  -B --binary-architecture <arch> Set output arch, when input is arch-less
  -F --target <bfdname> Set both input and output format to <bfdname>
     --debugging Convert debugging information, if possible
  -p --preserve-dates Copy modified/access timestamps to the output
  -D --enable-deterministic-archives
                                   Produce deterministic output when stripping archives
  -U --disable-deterministic-archives
                                   Disable -D behavior (default)
  -j --only-section <name> Only copy section <name> into the output
     --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>
  -R --remove-section <name> Remove section <name> from the output
  -S --strip-all Remove all symbols and relocation information
  -g --strip-debug Remove all debugging symbols & sections
     --strip-dwo Remove all DWO sections
     --strip-unneeded Remove all symbols not needed by relocations
  -N --strip-symbol <name> Do not copy symbol <name>
     --strip-unneeded-symbol <name>
                                   Do not copy symbol <name> unless needed by
                                     relocations
     --only-keep-debug Strip everything but the debug information
     --extract-dwo Copy only DWO sections
     --extract-symbol Remove section contents but keep symbols
  -K --keep-symbol <name> Do not strip symbol <name>
     --keep-file-symbols Do not strip file symbol(s)
     --localize-hidden Turn all ELF hidden symbols into locals
  -L --localize-symbol <name> Force symbol <name> to be marked as a local
     --globalize-symbol <name> Force symbol <name> to be marked as a global
  -G --keep-global-symbol <name> Localize all symbols except <name>
  -W --weaken-symbol <name> Force symbol <name> to be marked as a weak
     --weaken Force all global symbols to be marked as weak
  -w --wildcard Permit wildcard in symbol comparison
  -x --discard-all Remove all non-global symbols
  -X --discard-locals Remove any compiler-generated symbols
  -i --interleave [<number>] Only copy N out of every <number> bytes
     --interleave-width <number> Set N for --interleave
  -b --byte <num> Select byte <num> in every interleaved block
     --gap-fill <val> Fill gaps between sections with <val>
     --pad-to <addr> Pad the last section up to address <addr>
     --set-start <addr> Set the start address to <addr>
    {--change-start|--adjust-start} <incr>
                                   Add <incr> to the start address
    {--change-addresses|--adjust-vma} <incr>
                                   Add <incr> to LMA, VMA and start addresses
    {--change-section-address|--adjust-section-vma} <name>{=| + |-}<val>
                                   Change LMA and VMA of section <name> by <val>
     --change-section-lma <name>{=| + |-}<val>
                                   Change the LMA of section <name> by <val>
     --change-section-vma <name>{=| + |-}<val>
                                   Change the VMA of section <name> by <val>
    {--[no-]change-warnings|--[no-]adjust-warnings}
                                   Warn if a named section does not exist
     --set-section-flags <name>=<flags>
                                   Set section <name>'s properties to <flags>
     --add-section <name>=<file> Add section <name> found in <file> to output
     --dump-section <name>=<file> Dump the contents of section <name> into <file>
     --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>
     --long-section-names {enable|disable|keep}
                                   Handle long section names in Coff objects.
     --change-leading-char Force output format's leading character style
     --remove-leading-char Remove leading character from global symbols
     --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content
     --redefine-sym <old>=<new> Redefine symbol name <old> to <new>
     --redefine-syms <file> --redefine-sym for all symbol pairs
                                     listed in <file>
     --srec-len <number> Restrict the length of generated Srecords
     --srec-forceS3 Restrict the type of generated Srecords to S3
     --strip-symbols <file> -N for all symbols listed in <file>
     --strip-unneeded-symbols <file>
                                   --strip-unneeded-symbol for all symbols listed
                                     in <file>
     --keep-symbols <file> -K for all symbols listed in <file>
     --localize-symbols <file> -L for all symbols listed in <file>
     --globalize-symbols <file> --globalize-symbol for all in <file>
     --keep-global-symbols <file> -G for all symbols listed in <file>
     --weaken-symbols <file> -W for all symbols listed in <file>
     --alt-machine-code <index> Use the target's <index>'th alternative machine
     --writable-text Mark the output text as writable
     --readonly-text Make the output text write protected
     --pure Mark the output file as demand paged
     --impure Mark the output file as impure
     --prefix-symbols <prefix> Add <prefix> to start of every symbol name
     --prefix-sections <prefix> Add <prefix> to start of every section name
     --prefix-alloc-sections <prefix>
                                   Add <prefix> to start of every allocatable
                                     section name
     --file-alignment <num> Set PE file alignment to <num>
     --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/
                                   <commit>
     --image-base <address> Set PE image base to <address>
     --section-alignment <num> Set PE section alignment to <num>
     --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/
                                   <commit>
     --subsystem <name>[:<version>]
                                   Set PE subsystem to <name> [ & amp; <version>]
     --compress-debug-sections Compress DWARF debug sections using zlib
     --decompress-debug-sections Decompress DWARF debug sections using zlib
  -v --verbose List all object files modified
  @<file> Read options from <file>
  -V --version Display this program's version number
  -h --help Display this output
     --info List object formats & architectures supported

The GNU utility program objcopy is used to copy the contents of one target file to another target file.
Objcopy uses the GNU BFD library to read or write object files.
Objcopy can write the target file in a format different from the source target file (that is, it can convert a target file in one format into a target file in another format.
The specific operations of Objcopy can be controlled through the above command line options.

Objcopy will generate a temporary file when converting the target file, and delete the temporary file after the conversion is completed. Objcopy uses BFD to do the conversion work.
If there is no explicit format requirement, Objcopy will access all formats that have been described in the BFD library and can be recognized by it. Please refer to the “BFD Library” section and “BFD” in the “using ld” chapter of “GNUpro Decelopment Tools” “Specified Object File Format in the Library” section.

Objcopy can produce S-record format files by using srec as the output target (using the command line option -o srec).
Objcopy can produce raw binary files by using binary as the output target (using the command line option -o binary).
Using Objcopy to generate a raw binary file is essentially a memory dump of the contents of the input target file.
All symbols and relocation information will be discarded. The memory dump starts at the smallest virtual address of the portion of the input object file that will be copied to the output object file.

When using Objcopy to generate S record format files or raw binary files, the -S option and -R option may be useful. The -S option is used to delete the parts that contain debugging information, and the -R option is used to delete those parts that contain unnecessary content for the binary file.

Some parameter descriptions

  • input-file

  • outfile
    The parameters input-file and outfile represent the input target file (source target file) and the output target file (destination target file) respectively. If outfile is not explicitly specified on the command line, Objcopy will create a temporary file to store the target results, and then rename the temporary file with the name of input-file (at this time, the original input-file will be overwritten).

  • -I bfdname

  • –input-target=bfdname
    Tell Objcopy explicitly what the format of the source file is. bfdname is the standard format name described in the BFD library. This is much more efficient than the method of “letting Objcopy analyze the format of the source file by itself, and then compare it with the various formats described in BFD to learn the target format name of the source file.”
    -O bfdname
    –output-target= bfdname
    Write the output file (i.e. target file) using the specified format, bfdname is the standard format name described in the BFD library.

  • -F bfdname

  • –target= bfdname
    Clearly tell Objcopy what the format of the source file is, and also use this format to write the output file (that is, the target file). That is to say, in the process of copying the content of the source target file to the destination target file, only copying is performed. Format conversion, what format is the source target file, and what is the format of the destination target file.

  • -R sectionname

  • –remove-section= sectionname
    Remove all sections named sectionname from the output file. This option can be used multiple times.
    NOTE: Improper use of this option may result in unusable output files.

  • -S

  • –strip-all (strip strip, strip)
    Do not copy relocation information and symbol information from the source file to the output file (destination file).

  • -g
    –strip-debug
    Do not copy debugging symbols from the source file to the output file (destination file).

  • –strip-undeeded
    Strip all symbols that are not required for relocation processing.

  • -K symbolname

  • –keep-symbol= symbolname
    Only the symbol named symbolname is copied from the source file. This option can be used multiple times.

  • -N symbolname

  • –strip-symbol= symbolname
    The symbol named symbolname is not copied from the source file. This option can be used multiple times. It can be used in conjunction with other strip options (except -K symbolname | –keep-symbol= symbolname).

  • -L symbolname

  • –localize-symbol= symbolname
    Makes the symbol named symbolname localized within the file so that the symbol is not visible outside the file. This option can be used multiple times.

  • -W symbolname

  • -weaken-symbol= symbolname
    Debase the symbol named symbolname. This option can be used multiple times.

  • -x

  • –discard-all (discard discard, discard)
    Non-global symbols are not copied from the source file.

  • -X

  • –discard-locals
    Do not copy local symbols generated by the compiler from the source file (these symbols usually start with L or .).

  • -bbyte

  • –byte=byte
    Only every byte of the input file is retained (header data is not affected). Byte values range from 0 to interleave-1, where interleave is given by the -i or –interleave option, or the default value is 4. This option is useful for creating files to program ROM. It is typically used with the srec output target.

  • -i interleave

  • –interleave= interleave (interleave alternate lines, crossover)
    Only one copy is made from each interleaved byte. Use the -b or –byte option to select the bytes to copy. The default value is 4. If you do not specify -b or –byte, Objcopy ignores this option.

  • -p

  • –preserve-dates (preserve save, keep)
    Set the access and modification dates of the output file to be the same as the input file.

  • –debugging
    Convert debugging information if possible. Because only certain debug formats are supported and the conversion process takes some time, this option is not the default.

  • –gap-fill=val
    Use content val to fill the gaps between segments. This option works by increasing the size of the segment and filling the content val in an additional space at a lower address.

  • –pad-to=address
    Populate the output file to the virtual address address. By increasing the size of the last segment in the output file, the additional space between the end of the last segment in the output file and address is filled with the content val specified in the –gap-fill= val option (the default content is 0 , that is, without using the –gap-fill= val option).

  • –set-start=val
    Set the starting address of the new file (should it be the output file?) to val. Not all target file formats support setting the starting address.

  • –change-start=incr

  • –adjust-start= incr
    Change the starting address by increasing the value incr. Not all target file formats support setting the starting address.

  • –change-addresses incr

  • –adjust-vma incr
    Change the VMA and LMA addresses of all zones. , and the starting address, by adding incr. Some object file formats do not allow arbitrary changes to section addresses.
    By adding a value incr, change the VMA (Virtual Memory Address runtime address) and LMA (Load Memory Address) of all segments, as well as the starting address. Some object file formats do not allow arbitrary changes in segment addresses.

  • –change-section-address section{=, +,-} val

  • –adjust-section-vma section{=, +,-} val
    Set or change the VMA (Virtual Memory Address runtime address) and LMA (Load Memory Address) of the section named section. If “=” is used in this option, then the VMA (Virtual Memory Address runtime address) and LMA (Load Memory Address) of the section named section will be set to val; if “=” is used in this option -” or “+”, then the above two addresses will be set or changed to the current values of these two addresses minus or plus val. If the section named section does not exist in the input file, Objcopy will issue a warning unless the –no-change-warnings option is used.
    The segment address settings and changes here are based on the segments in the output file relative to the segments in the input file.
    For example:
    (1) –change-section-address .text = 10000
    This refers to the VMA (Virtual Memory Address runtime address) and LMA (Load Memory Address) of the .text segment in the output file after copying the segment named .text in the input file (i.e., the source file) to the output file. ) will both be set to 10000.
    (2) –change-section-address .text + 100
    This refers to the VMA (Virtual Memory Address runtime address) and LMA (Load Memory Address) of the .text segment in the output file after copying the segment named .text in the input file (i.e., the source file) to the output file. ) will be set to the value of the address of the .text segment in the previous input file (the current address) plus 100.

  • –change-section-lma section{=, +,-} val
    Only set or change the LMA (Load Memory Address) of the section named section.
    The LMA of a segment is the first address of the memory space into which the segment will be loaded when the program is loaded.
    Usually LMA and VMA (Virtual Memory Address runtime address) are the same, but in some systems, especially those systems where programs are placed in ROM, LMA and VMA are different.
    If “=” is used in this option, then the LMA (Load Memory Address) of the section named section will be set to val; if “-” or “+” is used in this option, then the LMA will be Is set or changed to the current value of these two addresses minus or plus the value of val.
    If the section named section does not exist in the input file, Objcopy will issue a warning unless the –no-change-warnings option is used.

  • –change-section-vma section{=, +,-} val
    Only set or change the VMA (Load Memory Address) of the section named section.
    The VMA of a segment is the location address of the segment when the program is running.
    Usually VMA and LMA (Virtual Memory Address runtime address) are the same, but in some systems, especially those systems where programs are placed in ROM, LMA and VMA are different.
    If “=” is used in this option, the LMA (Load Memory Address) of the section named section will be set to val.
    If “-” or “+” is used in this option, then the LMA will be set or changed to the current value of the two addresses minus or plus val. If the section named section does not exist in the input file, Objcopy will issue a warning unless the –no-change-warnings option is used.

  • –change-warnings

  • –adjust-warnings
    If –change-section-address section{=, +,-} val or –adjust-section-vma section{=, +,-} val, or –change-section-lma section{=, is used in the command line, + ,-} val, or –change-section-vma section{=, + ,-} val, and the section named section does not exist in the input file, Objcopy issues a warning. This is the default option.

  • –no-chagne-warnings

  • –no-adjust-warnings
    If –change-section-address section{=, +,-} val or –adjust-section-vma section{=, +,-} val, or –change-section-lma section{=, is used in the command line, + ,-} val, or –change-section-vma section{=, + ,-} val, Objcopy will not issue a warning even if the section named section does not exist in the input file.

  • –set-section-flags section=flags
    Set an identifier for the section. This flags variable can take multiple identification name strings separated by commas (these identification name strings can be recognized by the Objcopy program). The legal identification names include alloc, load, readonly, code, data and rom.
    You can set the content flag for a section that has no content, but it makes no sense to clear the content flag for a section that has content; just delete the section. Not all flags are meaningful for all target file formats.

  • –add-section sectionname=filename
    During the process of copying the target file, a new section named sectionname is added to the output file. The contents of this newly added section are obtained from the file filename. The size of this newly added segment is the size of the file filename. This option can only be used as long as the format of the output file allows the sections of the file to have arbitrary section names (section names are not standard, fixed).

  • –change-leading-char
    Some object file formats use special characters at the beginning of symbols. The most common character is the underscore, which the compiler usually adds before every symbol. This option tells objcopy to change the first character of each symbol when converting between object file formats. This option has no effect if the target file format uses the same leading characters. Otherwise, it will add a character, or delete a character, or change a character, as appropriate.

  • –remove-leading-char
    If the first character of the global symbol is a special symbol leading character used by the object file format, the character is deleted. The most common starting character of a symbol is the underscore. This option will remove leading underscores from all global symbols. This is useful if you wish to link together objects from different file formats that have different symbol name conventions.

  • –weaken
    Change all global symbols in the file to weak symbols. This is useful when building an object that will be linked with other objects using the linker’s -R option.
    This option only works when using an object file format that supports weak symbols.

  • -V

  • –version
    Display the version number of objcopy.

  • -v

  • –verbose
    Verbose output: Lists all modified object files. In the case of an archive, objcopy -V lists all members of the archive.

  • –help
    Displays a summary of objcopy’s options.

Usage examples

For example: the program prints out its own code

//test.c
#include <stdio.h>
#include <stdlib.h>
extern char * _binary_test_c_start;
int main()
{<!-- -->
printf("%s", (char *) & amp;_binary_test_c_start);
return 0;
}

Makefile writing method

SRC = test.c
TAR = t.exe
PE = pe-i386
ELF = elf32-i386
ALL:test.c
objcopy -I binary -O $(ELF) -B i386 test.c test.bin
gcc -o t.exe test.c test.bin

(1) Compile the image into an executable file

Q: How to convert a binary file, such as a picture or dictionary, into an .o file and directly link it to the executable file?
A:
$ objcopy -I binary -O elf32-i386 -B i386 14_95_13.jpg image.o
$ gcc image.o tt.o -o tt
$ nm tt | grep 14_95
0805d6c7 D _binary_14_95_13_jpg_end
00014213 A _binary_14_95_13_jpg_size
080494b4 D _binary_14_95_13_jpg_start

(2) Use objcopy to remove unused information:

$ objcopy -R .comment -R .note halo halo.min

(3) Use objcopy to regenerate some information

$ objcopy -R .note -R .comment -S -O binary xyb xyb.bin
-R .note -R .comment means removing the .note and .comment sections
-S removes all flags and relocation information
-O binary xyb xyb.bin indicates that xyb generates binary file xyb.bin