文章目录
- 二进制文件生成
- dd 命令copy文件
- 使用16进制对二进制文件显示
 
 
二进制文件生成
在嵌入的工作中,经常会使用到二进制文件,那么我们如何自己生成一个二进制文件呢?接下来介绍如何将一个只包含将32位数据的文件转化为二进制文件,原文件如下(数据一共 64bytes):
unsigned int data[] ={0x11223344,0x11223344,0x11223344,0x11223344,0x11223344,0x11223344,0x11223344,0x11223344,0x55556666,0x55556666,0x55556666,0x55556666,0x55556666,0x55556666,0x55556666,0x55556666
};
我们使用gcc 对齐先进行编译然后再进行反汇编:
arm-none-eabi-gcc --help
Usage: arm-none-eabi-gcc [options] file...
Options:...-E                       Preprocess only; do not compile, assemble or link.-S                       Compile only; do not assemble or link.-c                       Compile and assemble, but do not link.-o <file>                Place the output into <file>....
具体命令如下:
arm-none-eabi-gcc -c data.c -o data.bin
通过上面命令会生成一个elf 格式的 data.bin文件,由于我们需要的是个纯二进制文件,所以我们需要将 elf 文件的头和其它部分去掉。那么我们看下头有多大:
arm-none-eabi-readelf -h data.bin
ELF Header:Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00Class:                             ELF32Data:                              2's complement, little endianVersion:                           1 (current)OS/ABI:                            UNIX - System VABI Version:                       0Type:                              REL (Relocatable file)Machine:                           ARMVersion:                           0x1Entry point address:               0x0Start of program headers:          0 (bytes into file)Start of section headers:          448 (bytes into file)Flags:                             0x5000000, Version5 EABISize of this header:               52 (bytes)Size of program headers:           0 (bytes)Number of program headers:         0Size of section headers:           40 (bytes)Number of section headers:         9Section header string table index: 8
可以看到头(Size of this header)的大小为52个字节,所接下来我们就需要跳过这52个字节,只保留我们想要的内容。
dd 命令copy文件
在 linux 中,dd 是一个用于转换和复制文件的命令行工具,它可以执行许多底层操作,包括从文件的特定位置开始读取数据。要从原文件data.bin的第52个字节开始读取64个字节,可以使用以下命令:
dd if=data.bin of=new.bin bs=1 skip=51 count=64 
这里是命令参数的解释:
- if=data.bin: 指定输入文件(input file)的名称。
- of=new.bin: 指定输出文件(output file)的名称。
- bs=1: 设置块大小(block size)为1字节。
- skip=51: 跳过输入文件的前51个字节(因为我们是从第0字节开始计数的,所以51会跳过到第52个字节)。
- count=64: 读取64个字节的数据。
确保替换 data.bin 为您的源文件名。运行上述命令后,在当前目录下得到一个名为 new.bin 的文件,其中包含从 data.bin 文件的第52个字节开始的64个字节的数据。接下来我们看下如确认是否生成成功。
使用16进制对二进制文件显示
使用vim -b new.bin, 然后执行:%!xxd -e 可以看到如下内容:
00000000: 11223344 11223344 11223344 11223344  D3".D3".D3".D3".
00000010: 11223344 11223344 11223344 11223344  D3".D3".D3".D3".
00000020: 55556666 55556666 55556666 55556666  ffUUffUUffUUffUU
00000030: 55556666 55556666 55556666 55556666  ffUUffUUffUUffUU
00000040:       0a 
也可以使用xxd -p -c4 -e new.bin 直接在终端每行显示4个字节:
00000000: 11223344  D3".
00000004: 11223344  D3".
00000008: 11223344  D3".
0000000c: 11223344  D3".
00000010: 11223344  D3".
00000014: 11223344  D3".
00000018: 11223344  D3".
0000001c: 11223344  D3".
00000020: 55556666  ffUU
00000024: 55556666  ffUU
00000028: 55556666  ffUU
0000002c: 55556666  ffUU
00000030: 55556666  ffUU
00000034: 55556666  ffUU
00000038: 55556666  ffUU
0000003c: 55556666  ffUU
上面使用dd 命令对 elf 文件进行拷贝,这样每次我们还需要去确认头文件的大小,那么有么有方法可以一步到位呢?
 答案是肯定的,我们可以使用下面命令既可以一步到位:
arm-none-eabi-objcopy -O binary data.bin new.bin
这里是命令参数的解释:
- data.bin: 源文件,即你的 ELF 格式目标文件。
- new.bin: 输出文件,即去除文件头之后的纯二进制文件。
- -O binary: 指定输出格式为纯二进制 (- binary)。
运行这个命令后,new.bin 文件将只包含 data.bin 中的代码段和数据段的二进制数据,而不包含任何 ELF 格式的元数据或文件头信息。这在为嵌入式系统准备固件或加载文件到特定内存地址时非常有用。