利用QEMU模拟大端序机器
简介
当前我们安装虚拟机,一般小端机器比较多,有时候想模拟大端机器测试程序,这时就有模拟大端机器的需求。
参考:利用 QEMU USER 模式运行 mips 程序 - sinpo828 - 博客园 (cnblogs.com)
根据这个博客的描述,可以实现大端机器的需求,其实现是模拟MIPS架构的环境。
需要使用到QEMU模拟处理器,QEMU主要有两种模拟模式:
- System Mode:它可以虚拟多种CPU架构的虚拟计算机系统,比如可以在x86 的 Ubuntu系统中虚拟出一个MIPS架构的Debian系统。
- User Mode:它可以运行为其他处理器编写的应用程序,比如可以在X64 Ubuntu系统中直接运行 MIPS Linux的应用程序。
步骤
注意:安装的虚拟机是Ubuntu系统。
-
安装QEMU支持MIPS架构:
$ apt install qemu-user-static qemu-system-mips Reading package lists... Done Building dependency tree Reading state information... Done qemu-system-mips is already the newest version (1:2.5+dfsg-5ubuntu10.51). qemu-system-mips set to manually installed. The following packages will be REMOVED: qemu-user-binfmt The following NEW packages will be installed: qemu-user-static 0 upgraded, 1 newly installed, 1 to remove and 1 not upgraded. Need to get 7,592 kB of archives. After this operation, 85.9 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://mirrors.aliyun.com/ubuntu xenial-updates/universe amd64 qemu-user-static amd64 1:2.5+dfsg-5ubuntu10.51 [7,592 kB] Fetched 7,592 kB in 2s (3,777 kB/s) (Reading database ... 221693 files and directories currently installed.) Removing qemu-user-binfmt (1:2.5+dfsg-5ubuntu10.51) ... Selecting previously unselected package qemu-user-static. (Reading database ... 221693 files and directories currently installed.) Preparing to unpack .../qemu-user-static_1%3a2.5+dfsg-5ubuntu10.51_amd64.deb ... Unpacking qemu-user-static (1:2.5+dfsg-5ubuntu10.51) ... Processing triggers for man-db (2.7.5-1) ... Setting up qemu-user-static (1:2.5+dfsg-5ubuntu10.51) ...
-
安装编译MIPS架构的GCC工具:
$ apt install gcc-mips-linux-gnu Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed:
-
编写测试程序进行测试
$ more xx.c #include <stdio.h> #include <endian.h> int main() { int x = 0x1234; printf("0x%x, htole32 0x%x, htobe32 0x%x\n", x, htole32(x), htobe32(x)); return 0; }
-
编译,可看出编译出的程序时MSB,大端序的。
需要注意的是要加上
-static
的编译选项否则运行时候会提示找不到链接库。$ mips-linux-gnu-gcc -static xx.c [root@ubuntu] ~ $ file a.out a.out: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1 (SYSV), statically linked, for GNU/Linux 3.2.0, BuildID[sha1]=48929411066f86d58ed7855e545b980bc92fd75b, not stripped
-
运行
$ qemu-mips-static ./a.out 0x1234, htole32 0x34120000, htobe32 0x1234