Trying to build the linux kernel from scratch with riscv-tools

To preface, I should make clear that I’m very, very new to all this. I’ve taken a couple courses on programming, data structures, algorithms and the like, but I only have surface-level knowledge about computer architecture and hardware. I’m pretty sure I’m way out of my depth here and that I’ll need a lot of hand-holding (or at least pointers to resources I can read up on) to get anything done.

Sooo that said, what I’m trying to do is follow the instructions in the readme here:

Specifically, the Linux/RISC-V Installation Manual. As the readme puts it, I’m in the “needs to hack the kernel source code”, “have special requirements for your OS environment”, and “have a very particular notion of ‘fun’” categories. With my lack of expertise (or any experience whatsoever), I figured it’d be easy enough to just follow the instructions, but I’ve run into quite a lot of trouble. From asking around on the repository’s issues forum I’ve learned the manual is pretty out-of-date, but I’m hoping it’s still usable to some extent, since I haven’t found anything else that can guide me through what I want to do.

So, here are the problems I’ve run into:

  1. “Building the Linux Kernel -> Obtaining and Patching the Kernel Sources”

Here’s one of those signs the manual’s up-to-date - they date the linux version they’re working with at Feb 2014. I’ve got no idea whether I should patch with the latest 3.14.xx sources like the manual recommends, patch with the latest sources period, or not patch at all. The later steps seem to work regardless of which of those options I go with, but considering all the problems I’ve been having occur in this directory, I have to wonder if I’m doing something wrong here.

  1. “Building Busybox”

My end goal here is actually to get an environment set up without Busybox, but I’m nowhere near that stage yet, so I’m following the instructions here just to getting something up and running (not that I’ve gotten to that stage yet).

The thing here is fairly minor, possibly not even an actual problem, having to do with the configuration options. There are a few instances where the defaults differ from what the manual claims, or options that’re missing entirely. Namely, “CONFIG_FEATURE_INSTALLER=y, listed as “Support --install [-s] to install applet links at runtime” in BusyBox Settings → General Configuration” is missing from the GUI configuration menu entirely, but is present in the actual config file set to “y” by default.

  1. “Creating a root disk image”

This is the biggest problem I have at the moment, and is where I’m currently “stuck”. The specific step is where the manual says “Don’t forget to rebuild riscv-linux and riscv-pk!”. Attempting to rebuild at this point results in a “relocated truncated to fit error”. I was directed to this page:

Which described the nature of the error pretty well, but as far as I can tell doesn’t have anything explicit on how to fix it.

So yes, those are the things I’m currently concerned about, number 3 being the biggest, since it’s preventing me from progressing further through the manual. Thank you all in advance if you’re able to spare any time or advice to help.

riscv-tools is poorly maintained and difficult to build. It may be useful for those who would like to learn how to build a system from scratch by hand the hard way. It isn’t very useful for a novice. I would recommend using sifive/freedom-u-sdk instead. “make” will build a kernel with buildroot for the root filesystem. “make qemu” will boot a kernel on qemu.

There are a great many ways to trigger the relocation truncated message. Just knowing that you hit it isn’t useful. We would need much more info, and considering that this is riscv-tools, this is probably not worth the trouble. The approach I would recommend here is to use the linker option --noinhibit-exec, which would be -Wl,–noinhibit-exec if using gcc to link, and then use objdump to look at the binary to determine the instruction, relocation, symbol addresses, etc, to figure out why you got the relocation truncated message. This requires some knowledge of how relocations work. This --noinhibit-exec option only works with a patch that isn’t in binutils-2.30, which means you need an updated binutils-2.31 toolchain, which is probably not in riscv-tools yet, because it is so very poorly maintained. It is in riscv-gnu-toolchain though, so you could try using a toolchain from there instead of the one in riscv-tools if not recent enough.

Hey, thanks for helping me out again Jim >_>

-noinhibit-exec actually is working, and I’m able to view the objdump, but I’m not sure what to do to resolve the error even if I can pinpoint the exact instruction and address. The article you linked me and I reposted above describes how relocations work in some detail, but it kind of just ends after demonstrating how to locate a truncated relocation without describing how to resolve it.

There is no simple answer here. There are a great many different ways that you can get this error. You have to look at the reloc, understand how that reloc works, look at the instruction address and symbol addresses, manually do the computations that the linker would do when it sees that reloc, and then figure out why you got the reloc overflow based on all of the info you have. You can find info on relocations in the psABI document, though it may be hard to understand if you haven’t worked with relocs before.

It might be easier if you start with something that is known to work, like freedom-u-sdk, and then move to trying to build your own stuff by hand, and when something doesn’t work, look at freedom-u-sdk to see how it is different.

Is there documentation for using freedom-u-sdk? Many of the scripts in riscv-tools don’t seem to have equivalents in freedom-u-sdk, so I’m at a bit of a loss about how to build.

Just type make to build a toolchain and a kernel. “make qemu” will build qemu and kernel+buildroot and boot the kernel with the buildroot root file system on qemu.

Trying that returns a make error: “The RISCV environment variable was set, but is not pointing at a toolchain install tree.” Where should it be pointing?

You can unset RISCV, and let it build a toolchain. Or you can set RISCV to a compiler install dir, so that $RISCV/bin/riscv64-unknown-linux-gnu-gcc is a valid compiler. Unsetting RISCV is the simple solution, because we know that the compiler in freedom-u-sdk can build a working system from the linux kernel sources in that same tree.

“make qemu” does seem to have properly booted the kernel with buildroot, after I installed the right packages. However, straight after the successful boot it’s prompting me for a login with:

Welcome to Buildroot
buildroot login:

Is there some sort of default login to use? I can’t seem to find mention of this login prompt in the buildroot documentation.

You can log in as root with password sifive. You can find this in buildroot/configs/riscv64_defconfig.

After running “make qemu”, how do I exit buildroot at the login screen? Then, how do I reboot the kernel without having to rebuild everything?

Login to the console, and then use the poweroff command to shutdown the OS. Or you can just type ctrl-a x which will exit qemu, but may cause some work to be lost or some minor file system corruption (which should be fixed at next boot if using a good file system).

To run qemu directly, look at the qemu: line in the Makefile, and run the command that appears on the next line. You could copy this into a shell script for instance.