首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于QEMU的Aarch64裸金属Hello程序

基于QEMU的Aarch64裸金属Hello程序
EN

Stack Overflow用户
提问于 2022-09-02 06:42:03
回答 1查看 57关注 0票数 1

我在ARM基金会平台上编译并运行了一个简单的hello world程序。代码如下所示。

代码语言:javascript
复制
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Hello world!\n");
    return 0;
}

该程序是编译和链接使用ARM GNU工具链,如下所示。

代码语言:javascript
复制
$aarch64-none-elf-gcc -c -march=armv8-a -g hello.c hello.o
  aarch64-none-elf-gcc: warning: hello.o: linker input file unused because linking not done


$aarch64-none-elf-gcc -specs=aem-ve.specs -Wl,-Map=linkmap.txt hello.o -o hello.axf

我可以在ARM基金会平台上成功地执行这个程序(我认为,基金会平台类似于ARM固定虚拟平台),并打印"Hello!“

“AEM-ve.specs”文件的内容如下所示:

代码语言:javascript
复制
cat ./aarch64-none-elf/lib/aem-ve.specs 
# aem-ve.specs
#
# Spec file for AArch64 baremetal newlib, libgloss on VE platform with version 2
# of AngelAPI semi-hosting.
#
# This Spec file is also appropriate for the foundation model.

%rename link old_link

*link:
-Ttext-segment 0x80000000 %(old_link)

%rename lib libc

*libgloss:
-lrdimon

*lib:
cpu-init/rdimon-aem-el3.o%s --start-group %(libc) %(libgloss) --end-group

*startfile:
crti%O%s crtbegin%O%s %{!pg:rdimon-crt0%O%s} %{pg:rdimon-crt0%O%s}

是否可以在QEMU上执行相同的二进制文件?如果是这样的话,请您共享示例命令。如果没有,请您分享在QEMU上执行它的正确和最好的方法吗?

我使用“跟踪”选项跟踪了基金会平台的指令执行情况,并使用'tarmac-profile‘工具和'Tarmac-calltree’工具进行了分析。产出如下:

代码语言:javascript
复制
$ ./tarmac-profile hello.trace --image hello.axf
Address     Count       Time        Function name
0x80000000  1           120001      
0x80001030  1           30001       register_fini
0x80001050  1           60001       deregister_tm_clones
0x800010c0  1           210001      __do_global_dtors_aux
0x80001148  1           23500001    frame_dummy
0x800012c0  1           10480001    main
0x80002000  1           40001       main
0x80003784  1           360001      main
0x80003818  1           460001      _cpu_init_hook
0x80003870  1           590001      _write_r
0x800038d0  1           1090001     __call_exitprocs
...
...
代码语言:javascript
复制
./tarmac-calltree hello.trace --image hello.axf 


o t:10000 l:2288 pc:0x80001148 - t:23510000 l:7819 pc:0x80008060 : frame_dummy
  - t:240000 l:2338 pc:0x800011a8 - t:720000 l:2443 pc:0x800011ac
    o t:250000 l:2340 pc:0x80003818 - t:710000 l:2442 pc:0x80003828 : _cpu_init_hook
      - t:260000 l:2343 pc:0x8000381c - t:320000 l:2354 pc:0x80003820
        o t:270000 l:2345 pc:0x80002000 - t:310000 l:2353 pc:0x80002010 : main
      - t:320000 l:2354 pc:0x80003820 - t:700000 l:2436 pc:0x80003824
        o t:330000 l:2356 pc:0x80003784 - t:690000 l:2435 pc:0x80003814 : main
  - t:760000 l:2453 pc:0x800011bc - t:2010000 l:2970 pc:0x800011c0
    o t:770000 l:2455 pc:0x80004200 - t:2000000 l:2969 pc:0x800042c0 : memset
  - t:2010000 l:2970 pc:0x800011c0 - t:4870000 l:3587 pc:0x800011c4
    o t:2020000 l:2972 pc:0x80007970 - t:4860000 l:3586 pc:0x80007b04 : initialise_monitor_handles
      - t:2960000 l:3165 pc:0x80007b24 - t:4340000 l:3465 pc:0x80007b28

我尝试过在QEMU上执行以下方法,但没有成功。我已经通过TCP端口使用基于gdb的调试启动了QEMU

代码语言:javascript
复制
$ qemu-system-aarch64 -semihosting -m 128M -nographic  -monitor none -serial stdio  -machine virt,gic-version=2,secure=on,virtualization=on -cpu cortex-a53 -kernel hello.axf -S -gdb tcp::9000

调试会话的结果如下:

代码语言:javascript
复制
gdb) target remote localhost:9000
Remote debugging using localhost:9000
_start () at /data/jenkins/workspace/GNU-toolchain/arm-11/src/newlib-cygwin/libgloss/aarch64/crt0.S:90
90  /data/jenkins/workspace/GNU-toolchain/arm-11/src/newlib-cygwin/libgloss/aarch64/crt0.S: No such file or directory.
(gdb) si

<The system hangs here>

我已经尝试使用gdb反汇编代码,其输出如下所示。看起来代码没有正确加载。

代码语言:javascript
复制
(gdb) disas frame_dummy
Dump of assembler code for function frame_dummy:
   0x0000000080001110 <+0>: udf #0
   0x0000000080001114 <+4>: udf #0
   0x0000000080001118 <+8>: udf #0
   0x000000008000111c <+12>:    udf #0
 
  0x0000000080001120 <+16>: udf #0
   0x0000000080001124 <+20>:    udf #0
   0x0000000080001128 <+24>:    udf #0
   0x000000008000112c <+28>:    udf #0
   0x0000000080001130 <+32>:    udf #0
   0x0000000080001134 <+36>:    udf #0
   0x0000000080001138 <+40>:    udf #0
   0x000000008000113c <+44>:    udf #0
   0x0000000080001140 <+48>:    udf #0
   0x0000000080001144 <+52>:    udf #0
End of assembler dump.

请你把这件事弄清楚。任何暗示都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2022-09-05 10:43:59

基金会模型和QEMU 'virt‘板不是相同的机器类型。它们在不同的物理地址上有不同的设备,特别是它们在相同的地址上没有RAM。要在“virt”机器类型上运行裸金属代码,您需要调整代码。这对于裸金属来说是正常的--关键是你直接运行在某个(模拟的)硬件上,并且需要匹配它的细节。

具体来说,这里您需要做的最小改变是'virt‘板上的RAM从0x4000_0000开始,而不是基金会模型使用的0x8000_0000。也许还有其他人,但这是你所看到的东西的直接原因。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73578706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档