请选择 进入手机版 | 继续访问电脑版

risc-v中文社区

 找回密码
 立即注册
查看: 2100|回复: 0

基于 QEMU SYSTEM MODE 运行 ARM 程序

[复制链接]

347

主题

564

帖子

2237

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2237
发表于 2022-8-3 09:22:47 | 显示全部楼层 |阅读模式
工具准备
arm-linux-gnueabi-as: 可以通过下载 linaro 工具链获取
qemu-system-arm
程序范例
创建test.c,内容如下

volatile unsigned int *const UART0DR = (unsigned int *)0x101f1000;

void print_uart0(const char *s)
{
        while (*s) {
                *UART0DR = (unsigned int)(*s);
                s++;
        }
}

void c_entry()
{
        print_uart0("Hello World\n");
}
创建startup.S文件,内容如下

.global _Reset
_Reset:
        ldr sp, =stack_top
        bl c_entry
        b .
因为是在裸板运行,所以需要设置程序入口点、栈起始地址等,通过test.lds链接脚本设置,内容如下:

ENTRY(_Reset)
SECTIONS
{
        . = 0x10000;
        .startup . : { startup.o(.text) }
        .text : { *(.text) }
        .data : { *(.data) }
        .bss : { *(.bss COMMON) }
        . = ALIGN(8);
        . = . + 0x1000;
        stack_top = .;
}
使用交叉编译工具链编译并基于 qemu 运行

$ arm-linux-gnueabi-as -mcpu=arm926ej-s -g startup.S -o startup.o
$ arm-linux-gnueabi-gcc -c -mcpu=arm926ej-s -g test.c -o test.o
$ arm-linux-gnueabi-ld -T test.lds test.o startup.o -o test.elf
$ arm-linux-gnueabi-objcopy -O binary test.elf test.bin
$ qemu-system-arm -M versatilepb -m 128M -nographic -kernel test.bin # 通过 Ctrl+A 然后按 x 退出 qemu
参考
hello-world-for-bare-metal-arm-using-qemu
本文来自博客园,作者:Legend_Lone,转载请注明原文链接:https://www.cnblogs.com/sun-ye/p/14992511.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



Archiver|手机版|小黑屋|risc-v中文社区

GMT+8, 2024-3-28 20:58 , Processed in 0.023067 second(s), 17 queries .

risc-v中文社区论坛 官方网站

Copyright © 2018-2021, risc-v open source

快速回复 返回顶部 返回列表