基于 RISCV 的裸机程序

embedded/2024/10/16 2:24:10/

基于 RISCV 的裸机程序

  • 基于 RISCV 的裸机程序
    • 配置环境
    • 标准 rust 代码
      • 通过 `cargo` 生成一段标准代码
      • 搭建裸机程序
      • 添加退出系统调用
      • 添加 `println!` 宏
  • 参考文档

基于 RISCV 的裸机程序

配置环境

ansen@ansen-virtual-machine:~$ rustc --version
rustc 1.83.0-nightly (9ff5fc4ff 2024-10-03)ansen@ansen-virtual-machine:~$ qemu-system-riscv64 --version
QEMU emulator version 7.0.0
Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers

另外安装:

  • rustup target add riscv64gc-unknown-none-elf
  • cargo install cargo-binutils
  • rustup component add rust-src
  • rustup component add llvm-tools-preview

rust__24">标准 rust 代码

通过 cargo 生成一段标准代码

cargo new hello

src/main.rs 代码

rust">fn main() {println!("Hello, world!");
}

运行 cargo run 可以看到执行结果:

ansen@ansen-virtual-machine:~/work/src/tmp/hello$ cargo runCompiling hello v0.1.0 (/home/ansen/work/src/tmp/hello)Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.86sRunning `target/debug/hello`
Hello, world!

如果在 riscv 上运行,需要编译成 riscv 的指令。通过:

cargo run --target riscv64gc-unknown-none-elf

可以生成可执行代码,但会报错

ansen@ansen-virtual-machine:~/work/src/tmp/hello$ cargo run --target riscv64gc-unknown-none-elfCompiling hello v0.1.0 (/home/ansen/work/src/tmp/hello)
error[E0463]: can't find crate for `std`|= note: the `riscv64gc-unknown-none-elf` target may not support the standard library= note: `std` is required by `hello` because it does not declare `#![no_std]`= help: consider building the standard library from source with `cargo build -Zbuild-std`error: cannot find macro `println` in this scope--> src/main.rs:2:5|
2 |     println!("Hello, world!");|     ^^^^^^^error: `#[panic_handler]` function required, but not founderror: requires `sized` lang_itemFor more information about this error, try `rustc --explain E0463`.
error: could not compile `hello` (bin "hello") due to 4 previous errors

报错的原因是目标平台上没有 rust<


http://www.ppmy.cn/embedded/127386.html

相关文章

Spring Boot 3新特性@RSocketExchange轻松实现消息实时推送

Spring Boot 3新特性RSocketExchange轻松实现消息实时推送 随着微服务架构的普及&#xff0c;实时消息推送成为许多现代应用程序的核心需求。Spring Boot 3引入了RSocketExchange注解&#xff0c;这一新特性使得开发者能够轻松实现消息实时推送&#xff0c;极大地简化了客户端…

conda新建环境中存在大量ros相关python包

1 问题现象 新建的conda环境&#xff0c;执行pip list&#xff0c;出现了大量的ros相关包&#xff0c;环境不纯净。重新安装anaconda没有用。 2 问题原因 2.1 执行python -m site 执行python -m site获得以下结果 其中sys.path包含了’/opt/ros/noetic/lib/python3/dist-…

【unity框架开发12】从零手搓unity存档存储数据持久化系统,实现对存档的创建,获取,保存,加载,删除,缓存,加密,支持多存档

文章目录 前言一、Unity对Json数据的操作方法一、JsonUtility方法二、Newtonsoft 二、持久化的数据路径三、数据加密/解密加密方法解密方法 四、条件编译指令限制仅在编辑器模式下进行加密/解密四、数据持久化管理器1、存档工具类2、一个存档数据3、存档系统数据类4、数据存档存…

Linux的GDB学习与入门

GDB GDB&#xff08;GNU Debugger&#xff09;是一个功能强大的调试工具&#xff0c;广泛用于调试 C、C 和其他编程语言编写的程序。它是 GNU 项目的一部分&#xff0c;专为帮助开发者在程序执行时检测和修复错误设计。GDB 能够控制程序的执行&#xff0c;查看程序内部的状态&…

管家婆-本地化-重装数据库导入数据库mdf——未来之窗数据恢复专家

一、进入数据库管理软件 二、数据附加 三、选择文件mdf 四、错误处理 关闭管家婆和数据库服务重启 五、确定文件 六、确认附加 七、替换管家婆账套 八、阿雪技术观 拥抱开源与共享&#xff0c;见证科技进步奇迹&#xff0c;畅享人类幸福时光&#xff01; 让我们积极投身于技术…

C#中,重载(overload) 重写(override)的应用说明

一.重载(overload)& 重写(override)定义说明 1.重载(overload)& 重写(override) 1.1重载(overload)&#xff1a;指的是在同一个类中定义多个具有相同名称但参数列表不同的方法。通过参数列表的不同&#xff0c;编译器能够区分这些方法&#xff0c;并根据调用时传递的…

《云原生安全攻防》-- K8s攻击案例:从Pod容器逃逸到K8s权限提升

在本节课程中&#xff0c;我们将介绍一个完整K8s攻击链路的案例&#xff0c;其中包括了从web入侵到容器逃逸&#xff0c;再到K8s权限提升的过程。通过以攻击者的视角&#xff0c;可以更全面地了解K8s环境中常见的攻击技术。 在这个课程中&#xff0c;我们将学习以下内容&#…

redistemplate实现点赞相关功能

使用Redis的SET数据结构来存储每个实体的点赞用户ID列表&#xff0c;方便进行点赞数量的计数和用户点赞状态的检查。以下是一个小demo&#xff0c;只提供简单思路。 Service public class LikeService {Autowiredprivate RedisTemplate redisTemplate;//点赞public Long like(…