【问题标题】:How to run arm64 baremetal hello world program on qemu?如何在 qemu 上运行 arm64 baremetal hello world 程序?
【发布时间】:2022-01-15 00:00:27
【问题描述】:

通常一个问题会引导我进入另一个问题。
在尝试调试内联汇编代码时,我遇到了另一个基本问题。
长话短说,我想在 qemu 上运行 arm64 baremetal hello world 程序。

#include <stdio.h>

int main()
{
printf("Hello World!\n");
}

我这样编译它: aarch64-none-elf-gcc -g test.c

_exit_sbrk_write_close_lseek_read_fstat 和 _isatty 出现未定义的引用错误。我在过去了解到-specs=rdimon.specs 编译选项消除了这个错误。 于是我跑了

aarch64-none-elf-gcc -g test.c -specs=rdimon.specs

它可以使用 a.out 文件编译。
现在我运行 qemu baremetal 程序来调试代码。

qemu-system-aarch64 -machine virt,gic-version=max,secure=true,虚拟化=true -cpu cortex-a72 -kernel a.out -m 2048M -nographic -s -S

这是 gdb 的运行结果。

ckim@ckim-ubuntu:~/testdir/testinlinedebugprint$ aarch64-none-elf-gdb a.out
GNU gdb (GNU Toolchain for the A-profile Architecture 10.2-2020.11 (arm-10.16)) 10.1.90.20201028-git
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=x86_64-pc-linux-gnu --target=aarch64-none-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://bugs.linaro.org/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...
(gdb) set architecture aarch64
The target architecture is set to "aarch64".
(gdb) set serial baud 115200
(gdb) target remote :1234
Remote debugging using :1234
_start ()
    at /tmp/dgboter/bbs/build02--cen7x86_64/buildbot/cen7x86_64--aarch64-none-elf/build/src/newlib-cygwin/libgloss/aarch64/crt0.S:90
90  /tmp/dgboter/bbs/build02--cen7x86_64/buildbot/cen7x86_64--aarch64-none-elf/build/src/newlib-cygwin/libgloss/aarch64/crt0.S: No such file or directory.
(gdb) b main
Breakpoint 1 at 0x4002f8: file test.c, line 26.
(gdb) 
(gdb) r
The "remote" target does not support "run".  Try "help target" or "continue".
(gdb) c
Continuing.

它不会中断和挂起。
我究竟做错了什么?以及如何解决/tmp/dgboter/bbs/build02--cen7x86_64/buildbot/cen7x86_64--aarch64-none-elf/build/src/newlib-cygwin/libgloss/aarch64/crt0.S: No such file or directory. 问题? 任何帮助将不胜感激。谢谢!

添加:
我意识到我之前也问过同样的问题(How to compile baremetal hello_world.c and run it on qemu-system-aarch64?)(啊!我的记忆......)我意识到我需要所有的东西,比如 start.S crt0.S 和链接器脚本,. . .我愚蠢地认为,当我实际上必须填充非常低级的东西时,裸机编译器会自动处理它。在某些情况下,我从事过裸机程序,但这是在其他人已经设置了这些初始环境之后(有时我什至对它们进行了多次修改!)。在裸机中,您必须提供所有东西。没有任何东西可以视为理所当然,因为它是“裸机”。我这么晚才意识到这个基本的东西..

【问题讨论】:

    标签: arm gdb qemu


    【解决方案1】:

    当您为“裸机”构建程序时,这意味着您需要配置工具链以生成可在您尝试运行它的特定裸机上运行的二进制文件。例如,二进制文件必须:

    • 将其代码放在机器内存映射中的某个位置,那里有 ROM 或 RAM
    • 把它的数据放在有 RAM 的地方
    • 确保在启动时堆栈指针正确初始化为指向 RAM
    • 如果要打印输出,请包含访问该机器上合适设备的例程。这很可能是一个串口,而串口通常是完全不同的设备,位于不同的地址,在不同的机器上

    如果其中任何一个错误或与您运行的实际机器不匹配,结果通常与您所看到的完全一样——程序崩溃而没有输出。

    更具体地说,rdimon.specs 告诉编译器构建 C 库函数,这些函数通过“半主机”调试器 ABI(它支持“打印字符串”和其他一些东西)来完成其中的一些工作。您的 QEMU 命令行不启用半主机的实现(您可以使用 -semihosting 选项打开它),所以这根本不起作用。但是您可能还会遇到其他问题。

    【讨论】:

    • 您好,谢谢。我在 qemu 选项中添加了 -semihosting 并连接了 gdb。 $pc 值从 0x400178 <_start> 开始,但它会产生异常并且 $pc 停留在 0x200 中,即 synchronous exception, current EL with SPx。你能建议我进一步做什么吗?找不到工具链中使用的crt0.S文件,也看不到。
    • 我意识到程序从 0x400000 开始,但该区域是 arm 'virt' 机器中的闪存。所以我提取了默认链接器脚本(在 ld 命令期间使用 -Wl,--verbose)并尝试将程序条目更改为 0x80000000(DRAM)。但它给了我:aarch64-none-elf-ld:警告:找不到入口符号_start;默认为 0000000000400000。我稍后再试。也许我必须在程序中提供 Flash 图像。就像我几个月前所做的那样。
    • 啊!我意识到你的意思并更新了我的问题。我以前也问过同样的问题。 stackoverflow.com/questions/65896336/…
    猜你喜欢
    • 2014-09-05
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多