使用QEMU进行大端测试
Big-Endian Testing with QEMU

原始链接: https://www.hanshq.net/big-endian-qemu.html

## 大端序 vs. 小端序 在计算机中,**endianness(字节序)** 定义了字节在内存中的存储顺序。**大端序(Big-endian)** 系统先存储最高有效字节,而 **小端序(Little-endian)** 系统先存储最低有效字节。 这会影响对多字节数据(如十六进制值)的解读。 这些术语源自乔纳森·斯威夫特的《格列佛游记》,指的是关于从鸡蛋的哪一端打破的争论。 大多数现代计算机(Intel x86_64, ARM AArch64)是小端序的。 然而,理解字节序对于可移植代码至关重要,可以避免“字节序谬误”。 在没有专用硬件的情况下,在⼤端序系统上测试代码可能具有挑战性。 **QEMU** 提供了一种解决方案:它允许模拟不同的架构。 通过使用 QEMU 和交叉编译器(如用于 MIPS 或 s390x 的 GCC),开发人员可以编译和运行代码,就好像它正在大端序机器上执行一样,从而验证无论底层系统的字节序如何,代码的行为是否正确。 提供的示例通过展示在小端序 Linux 与模拟的大端序 MIPS 和 s390x 架构上,一个简单程序的不同内存输出,来说明这一点。

黑客新闻 新的 | 过去的 | 评论 | 提问 | 展示 | 工作 | 提交 登录 使用 QEMU 进行大端测试 (hanshq.net) 12 分,jandeboevrie 发表于 59 分钟前 | 隐藏 | 过去的 | 收藏 | 2 条评论 帮助 pragmaticviber 7 分钟前 [–] 一切都很有趣,直到你必须弄清楚字节序错误是在你的代码中还是在 QEMU 的 s390x 模拟中。回复 rurban 3 分钟前 | 父评论 [–] 尚未在 QEMU 的 s390x 中发现任何错误,但在字节序代码中发现了许多错误。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

In computing, the terms big endian and little endian refer to the order in which a value's bytes are stored in memory: with the most significant (big) or least significant (little) part first.

For example, the hexadecimal value 0x12345678 consists of four bytes. If we instruct a computer to store it at some memory address mem, the computer's endianness determines whether the least significant byte (0x78) or the most significant byte (0x12) gets stored at the first memory address, mem[0].

According to Wikipedia, this terminology is borrowed from the book Gulliver's Travels which tells of a conflict between those who break boiled eggs at the larger end (the Big-Endians) and the smaller end (the Little-Endians).

Some processor architectures support both modes, but most modern personal computers and smartphones are little-endian systems. (Intel x86_64 or ARM AArch64.)

When programming, it is still important to write code that runs correctly on systems with either byte order (see for example The byte order fallacy). But without access to a big-endian machine, how does one test it? QEMU provides a convenient solution. With its user mode emulation we can easily run a binary on an emulated big-endian system, and we can use GCC to cross-compile to that system.

This program (endian.c) shows the effect of the byte order:

#include <stdint.h>
#include <stdio.h>

int main(void)
{
        uint32_t x = 0x12345678;
        int i;

        for (i = 0; i < sizeof(x); i++) {
                printf("mem[%d] = 0x%02x\n", i, ((char*)&x)[i]);
        }

        return 0;
}

On my little-endian Linux machine it runs like this:

$ gcc endian.c && ./a.out
mem[0] = 0x78
mem[1] = 0x56
mem[2] = 0x34
mem[3] = 0x12

MIPS is a big-endian architecture. On a Debian system, we can install QEMU user mode emulation and GCC for MIPS like this:

$ sudo apt-get install qemu-user gcc-mips-linux-gnu

and build and run the same program:

$ mips-linux-gnu-gcc -static endian.c && qemu-mips a.out
mem[0] = 0x12
mem[1] = 0x34
mem[2] = 0x56
mem[3] = 0x78

Now we see the value stored in big-endian order!

We can get even more exotic, targeting IBM z/Architecture (s390x) which is also big-endian:

$ sudo apt-get install gcc-s390x-linux-gnu
$ s390x-linux-gnu-gcc -static endian.c && qemu-s390x a.out
mem[0] = 0x12
mem[1] = 0x34
mem[2] = 0x56
mem[3] = 0x78

Pretty neat!

联系我们 contact @ memedata.com