手机网站优化技巧,安徽海外网络推广,wordpress插件管理本地资源,站内内容投放计划文章目录 二进制文件生成dd 命令copy文件使用16进制对二进制文件显示 二进制文件生成
在嵌入的工作中#xff0c;经常会使用到二进制文件#xff0c;那么我们如何自己生成一个二进制文件呢#xff1f;接下来介绍如何将一个只包含将32位数据的文件转化为二进制文件#xff… 文章目录 二进制文件生成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: 2s 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 ifdata.bin ofnew.bin bs1 skip51 count64 这里是命令参数的解释
ifdata.bin: 指定输入文件input file的名称。ofnew.bin: 指定输出文件output file的名称。bs1: 设置块大小block size为1字节。skip51: 跳过输入文件的前51个字节因为我们是从第0字节开始计数的所以51会跳过到第52个字节。count64: 读取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 格式的元数据或文件头信息。这在为嵌入式系统准备固件或加载文件到特定内存地址时非常有用。