objcopy的作用是拷貝一個目標文件的內容到另一個目標文件中。objcopy使用GNU BFD庫去讀或寫目標文件。objcopy可以使用不同於源目標文件的格式來寫目的目標文件(也即是說可以將一種格式的目標文件轉換成另一種格式的目標文件)。objcopy在進行目標文件的轉換時,將生成一個臨時文件,轉換完成后就將這個臨時文件刪掉。objcopy使用BFD做轉換工作。如果沒有明確地格式要求,則objcopy將訪問所有在BFD庫中已經描述了的並且它可以識別的格式。
請注意objcopy 所能支持的機器架構,及目標文件格式。
mipsel-linux-objcopy –-info顯示出該objcopy所支持的architecture 與目標文件格式。
通過使用srec作為輸出目標(使用命令行選項-O srec),objcopy可以產生S記錄格式文件。
通過使用binary作為輸出目標(使用命令行選項-O binary),objcopy可以產生原始的二進制文件。
使用objcopy生成S記錄格式文件或者原始的二進制文件的過程中,-S選項和-R選項可能會比較有用。-S選項是用來刪掉包含調試信息的部分,-R選項是用來刪掉包含了二進制文件不需要的內容的那些部分。
input-file
outfile
參數input-file和outfile分別表示輸入目標文件(源目標文件)和輸出目標文件(目的目標文件)。如果在命令行中沒有明確地指定outfile,那么objcopy將創建一個臨時文件來存放目標結果,然后使用input-file的名字來重命名這個臨時文件(這時候,原來的input-file將被覆蓋)。
-I bfdname
--input-target=bfdname
明確告訴objcopy,源文件的格式是什么,bfdname是BFD庫中描述的標准格式名。這樣做要比“讓objcopy自己去分析源文件的格式,然后去和BFD中描述的各種格式比較,通過而得知源文件的目標格式名”的方法要高效得多。
-O bfdname
--output-target= bfdname
使用指定的格式來寫輸出文件(即目標文件),bfdname是BFD庫中描述的標准格式名。
-B bfdarch
--binary-architecture=bfdarch
對於一個沒有指定架構的文件轉換成目標文件時是有用的。此時,目標文件架構將被指定為bfdarch 。但是當objcopy能夠識別輸入文件架構時該選項將被忽略。在該轉換過程中將會產生一些符號:
_binary_objfile_start,_binary_objfile_end,_binary_objfile_size,在其他的文件中我們能引用這些符號以此來獲得相應的數據。例如,當我們可以將一幅圖片轉換成目標文件,而后在程序中使用這些符號。
--reverse-bytes=num改變大小端,看示例
Reversing takes place before the interleaving is performed.
Consider a simple file with a section containing the following eight bytes: 12345678.
Using --reverse-bytes=2 for the above example, the bytes in the output file would be ordered 21436587.
Using --reverse-bytes=4 for the above example, the bytes in the output file would be ordered 43218765. By using --reverse-bytes=2 for the above example, followed by --reverse-bytes=4 on the output file, the bytes in the second output file would be ordered 34127856.
以下涉及到不同編譯器對符號的處理問題。比如我們寫個匯編文件,匯編后,匯編文件中的符號未變,但是當我們寫個C文件再生成目標文件后,源文件中的符號前可能加了下划線,當兩者之間發生引用關系時可能無法連接,此時我們會用到下面的命令。
--change-leading-char
Some object file formats use special characters at the start of symbols. The most common such character is underscore, which compilers often add before every symbol. This option tells objcopy to change the leading character of every symbol when it converts between object file formats. If the object file formats use the same leading character, this option has no effect. Otherwise, it will add a character, or remove a character, or change a character, as appropriate.
--remove-leading-char
If the first character of a global symbol is a special symbol leading character used by the object file format, remove the character. The most common symbol leading character is underscore. This option will remove a leading underscore from all global symbols. This can be useful if you want to link together objects of different file formats with different conventions for symbol names. This is different from --change-leading-char because it always changes the symbol name when appropriate, regardless of the object file format of the output file.
--prefix-symbols=string
Prefix all symbols in the output file with string.
--prefix-sections=string
Prefix all section names in the output file with string.
--prefix-alloc-sections=string
Prefix all the names of all allocated sections in the output file with string.
-i interleave
--interleave=interleave
interleave字節組成一組,我們只在這一組中選擇一個字節(具體是哪個由-b byte的byte指定)。
-b byte
--byte=byte
在interleave字節所構成的組中選擇第byte個字節(從0開始計)。