对模拟代码中字节顺序的重要性感到非常困惑
Very confused about the significance of endianness in emulated code
老实说,我无法理解这一点。我正在为 aarch64 生成代码,看来我可以 运行 完全相同的代码 与 qemu-aarch64
和 qemu-aarch64_be
仅通过更改字节顺序ELF headers。否则,可执行文件 byte-for-byte 相同。在这两种情况下,Objdump 也能正确地反汇编代码。怎么可能呢?生成的代码(我认为)在磁盘上 little-endian,事实上,如果它是相反的字节顺序,它似乎不起作用。
Big-endian(headers 和前四个指令):
00000000 7f 45 4c 46 02 02 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 00 02 00 b7 00 00 00 01 00 00 00 00 00 00 10 80 |................|
00000020 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 |.......@........|
00000030 00 00 00 00 00 40 00 38 00 01 00 00 00 00 00 00 |.....@.8........|
00000040 00 00 00 01 00 00 00 05 00 00 00 00 00 00 00 80 |................|
00000050 00 00 00 00 00 00 10 80 00 00 00 00 00 00 00 00 |................|
00000060 00 00 00 00 00 00 03 0c 00 00 00 00 00 00 03 0c |................|
00000070 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 00 |....... ........|
00000080 ff 4f 00 d1 e0 03 40 39 00 20 00 11 e0 03 00 39 |.O....@9. .....9|
80: d1004fff sub sp, sp, #0x13
84: 394003e0 ldrb w0, [sp]
88: 11002000 add w0, w0, #0x8
8c: 390003e0 strb w0, [sp]
小端:
00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 02 00 b7 00 01 00 00 00 80 10 00 00 00 00 00 00 |................|
00000020 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |@...............|
00000030 00 00 00 00 40 00 38 00 01 00 00 00 00 00 00 00 |....@.8.........|
00000040 01 00 00 00 05 00 00 00 80 00 00 00 00 00 00 00 |................|
00000050 80 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000060 0c 03 00 00 00 00 00 00 0c 03 00 00 00 00 00 00 |................|
00000070 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ...............|
00000080 ff 4f 00 d1 e0 03 40 39 00 20 00 11 e0 03 00 39 |.O....@9. .....9|
80: d1004fff sub sp, sp, #0x13
84: 394003e0 ldrb w0, [sp]
88: 11002000 add w0, w0, #0x8
8c: 390003e0 strb w0, [sp]
我觉得我错过了什么。这里发生了什么?我唯一能想到的是 qemu 在幕后做了一些魔术,因为数据的布局方式显然没有任何效果。但是,如果是这样的话,它怎么知道字节序是正确的还是不正确的?
B2.6.2 Instruction endianness
In Armv8-A, A64 instructions have a fixed length of 32 bits
and are always little-endian.
所以只有内存加载和存储受字节顺序影响。我仍然希望您的二进制文件以某种方式破坏在编译时初始化的数据,但从技术上讲,没有任何此类数据是可能的。
老实说,我无法理解这一点。我正在为 aarch64 生成代码,看来我可以 运行 完全相同的代码 与 qemu-aarch64
和 qemu-aarch64_be
仅通过更改字节顺序ELF headers。否则,可执行文件 byte-for-byte 相同。在这两种情况下,Objdump 也能正确地反汇编代码。怎么可能呢?生成的代码(我认为)在磁盘上 little-endian,事实上,如果它是相反的字节顺序,它似乎不起作用。
Big-endian(headers 和前四个指令):
00000000 7f 45 4c 46 02 02 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 00 02 00 b7 00 00 00 01 00 00 00 00 00 00 10 80 |................|
00000020 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 |.......@........|
00000030 00 00 00 00 00 40 00 38 00 01 00 00 00 00 00 00 |.....@.8........|
00000040 00 00 00 01 00 00 00 05 00 00 00 00 00 00 00 80 |................|
00000050 00 00 00 00 00 00 10 80 00 00 00 00 00 00 00 00 |................|
00000060 00 00 00 00 00 00 03 0c 00 00 00 00 00 00 03 0c |................|
00000070 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 00 |....... ........|
00000080 ff 4f 00 d1 e0 03 40 39 00 20 00 11 e0 03 00 39 |.O....@9. .....9|
80: d1004fff sub sp, sp, #0x13
84: 394003e0 ldrb w0, [sp]
88: 11002000 add w0, w0, #0x8
8c: 390003e0 strb w0, [sp]
小端:
00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 02 00 b7 00 01 00 00 00 80 10 00 00 00 00 00 00 |................|
00000020 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |@...............|
00000030 00 00 00 00 40 00 38 00 01 00 00 00 00 00 00 00 |....@.8.........|
00000040 01 00 00 00 05 00 00 00 80 00 00 00 00 00 00 00 |................|
00000050 80 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000060 0c 03 00 00 00 00 00 00 0c 03 00 00 00 00 00 00 |................|
00000070 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ...............|
00000080 ff 4f 00 d1 e0 03 40 39 00 20 00 11 e0 03 00 39 |.O....@9. .....9|
80: d1004fff sub sp, sp, #0x13
84: 394003e0 ldrb w0, [sp]
88: 11002000 add w0, w0, #0x8
8c: 390003e0 strb w0, [sp]
我觉得我错过了什么。这里发生了什么?我唯一能想到的是 qemu 在幕后做了一些魔术,因为数据的布局方式显然没有任何效果。但是,如果是这样的话,它怎么知道字节序是正确的还是不正确的?
B2.6.2 Instruction endianness
In Armv8-A, A64 instructions have a fixed length of 32 bits and are always little-endian.
所以只有内存加载和存储受字节顺序影响。我仍然希望您的二进制文件以某种方式破坏在编译时初始化的数据,但从技术上讲,没有任何此类数据是可能的。