Setup
If you have worked through all the previous posts in this series, you can cd
to your riscv-from-scratch
directory and skip this section. If you’re new to this series and would like to follow along, keep reading!
- Follow these instructions from the first post to install the GNU RISC-V toolchain and a version of QEMU with RISC-V emulation capabilities.
- Clone or fork the riscv-from-scratch repo:
git clone git@github.com:twilco/riscv-from-scratch.git
# or `git clone https://github.com/twilco/riscv-from-scratch.git` to clone
# via HTTPS rather than SSH
# alternatively, if you are a GitHub user, you can fork this repo.
# https://help.github.com/en/articles/fork-a-repo
cd riscv-from-scratch
- Check out the
pre-function-prologue-impl
branch which contains the code prerequisites for this post in thesrc
directory:
git checkout pre-function-prologue-impl
- Copy the customized linker script
riscv64-virt.ld
, minimal C runtimecrt0.s
, NS16550A UART driver skeletonns16550a.s
, andmain.c
to our working directory:
# note: this will overwrite any existing files you may have in `work`
cp -a src/. work
riscv64-unknown-elf-gcc -g -ffreestanding -O0 -Wl,--gc-sections \ -nostartfiles -nostdlib -nodefaultlibs -Wl,-T,riscv64-virt.ld \ crt0.s main.c ns16550a.c
-ffreestanding 告诉编译器标准库可能不存在,因此不能做任何假设。在主机环境中运行应用程序时,此选项不是必需的,但是我们没有这样做,因为重要的是告诉编译器该信息。
-Wl 是逗号分隔的标志列表,以传递给链接器 ld。 --gc-sections 代表“垃圾收集 section”,告诉ld 在链接后删除未使用的节。 -nostartfiles,-nostdlib 和 -nodefaultlibs 分别告诉链接器不要链接任何标准系统启动文件(例如默认 crt0),任何标准系统 stdlib 实现或任何标准系统默认可链接库。我们提供了自己的 crt0 和链接描述文件,因此传递这些标志以告知编译器,我们不希望使用这些默认设置中的任何一个。
-T 允许你将你的链接器脚本路径传给链接器,在我们这次实验中就是 riscv64-virt.ld 。最后,加上我们想要编译的文件名就可以了。
https://twilco.github.io/riscv-from-scratch/2019/07/28/riscv-from-scratch-4.html