Home QEMU-用户级模拟
Post
Cancel

QEMU-用户级模拟

qemu有两种仿真模式,一种是系统级仿真,一种是用户级仿真。前文中记录的都是系统级仿真。

系统级仿真,以模拟器程序名称一般都是 qemu-system-[arch]。如:

1
2
3
4
5
6
7
8
9
10
11
ls qemu-7.2.1/build/qemu-system-*
qemu-7.2.1/build/qemu-system-arm           
qemu-7.2.1/build/qemu-system-aarch64       
qemu-7.2.1/build/qemu-system-mips
qemu-7.2.1/build/qemu-system-i386          
qemu-7.2.1/build/qemu-system-x86_64
qemu-7.2.1/build/qemu-system-ppc       
qemu-7.2.1/build/qemu-system-ppc64     
qemu-7.2.1/build/qemu-system-riscv32   
qemu-7.2.1/build/qemu-system-riscv64
...

而用户级仿真的模拟器程序,名称一般都是 qemu-[arch][-static]。带 -static 后缀的是静态链接的程序,不带就就是动态链接的程序。 如:

1
2
3
4
5
6
7
8
9
10
qemu-7.2.1/build/qemu-arm           
qemu-7.2.1/build/qemu-aarch64       
qemu-7.2.1/build/qemu-mips
qemu-7.2.1/build/qemu-i386          
qemu-7.2.1/build/qemu-x86_64
qemu-7.2.1/build/qemu-ppc       
qemu-7.2.1/build/qemu-ppc64     
qemu-7.2.1/build/qemu-riscv32   
qemu-7.2.1/build/qemu-riscv64
...

编译时配置为静态链接吗,应该可以生成静态链接的模拟器。也可以直接安装历史版本的,使用apt安装静态的qemu用户级模拟程序, sudo apt install qemu-user-static

QEMU静态二进制转换(Static Binary Translation)的方式实现用户级仿真,这种方式可以将不同架构的应用程序在本地运行,比如在x86主机上运行ARM架构的应用程序。

运行静态链接的HelloWorld

编写测试程序

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char**argv)
{
	printf("this is my test helloworld.\n");
	return 0;
}

静态链接方式,使用arm交叉编译。

1
2
3
4
5
6
7
arm-linux-gnueabihf-gcc ./hellowd.c -static  -o static_hello

file ./static_hello 
# ./static_hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (GNU/Linux), statically linked, BuildID[sha1]=7948d70a47c7912ae368ac0880950658c3ac34c1, for GNU/Linux 3.2.0, not stripped

qemu-arm-static ./static_hello 
# this is my test helloworld.

修改arm根文件系统

通过模拟arm,可以直接在主机上方便的修改arm的文件系统,这里测试使用busybox根文件系统。其他如debootstrap就更方便使用了。

1
2
3
4
5
6
7
8
ls rootfs_arm/
bin  boot  dev  etc  home  lib  linuxrc  mnt  proc  root  sbin  sys  tmp  usr  var

cp /usr/bin/qemu-arm-static ./rootfs_arm/bin/

sudo chroot ./rootfs_arm/ bin/qemu-arm-static  bin/sh
/ # ls
bin      boot     dev      etc      home     lib      linuxrc  mnt      proc     root     sbin     sys      tmp      usr      var

可以用来修改完善根文件系统,测试程序运行环境等。如动态链接上面的HelloWorld程序,放入bin,需要将依赖的c库拷贝到这个arm文件系统的动态库目录,如/lib/下, 就可以模拟运行动态链接的arm的HelloWorld程序了。

This post is licensed under CC BY 4.0 by the author.