x86_64用户态模拟arm程序(helloworld)

来源:互联网 发布:北方广电网络客服电话 编辑:程序博客网 时间:2024/05/12 00:57
用户态模拟arm 运行程序 - "hello world"

编译一个运行在arm 上的helloworld.
----------------------------------------
甲: 代码
----------------------------------------
$cat hello.c
#include <stdio.h>
void main()
{
    printf("---------- hello qemu ------------\n");
//    return 0;
    while(1);
}

注意结尾是while(1); 而不是return 0;
   这里模拟一个永不退出的主进程

----------------------------------------
乙: 编译
----------------------------------------
$cat Makefile
CC=arm-none-linux-gnueabi-gcc
all: initramfs

hello:
    $(CC) -o hello hello.c -static
initramfs: hello

    echo hello |cpio -o --format=newc > initramfs

# 说明: hello 是文件名称, -o 是创建, initramfs 是归档名称


    
clean:
    rm hello initramfs
用来交叉编译出arm 执行文件和打包文件
打包文件是为系统起动ramfs 镜像, 研究系统时使用

----------------------------------------
丙: 运行
----------------------------------------
$ qemu-arm hello
---------- hello qemu -----------
^C
 qemu-arm 是用户态arm模拟运行程序

说明:
如果编译时不加-static 选项,则运行时需要指明运行库搜索路径。
arm-none-linux-gnueabi-gcc -o hello hello.c
这个hello 需要动态链接库
$ qemu-arm hello
/lib/ld-linux.so.3: No such file or directory

查询所需要的动态库:
arm-none-linux-gnueabi-readelf -a hello |grep lib
      [Requesting program interpreter: /lib/ld-linux.so.3]

对于我的环境,指明库路径:
$ qemu-arm  -L /home/hjj/CodeBench/arm-2014.05/arm-none-linux-gnueabi/libc hello
---------- hello qemu ------------
^C

0 0