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

**URL:** <https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450>\
**Category:** RISC-V\
**Created:** [July 26, 2018, 9:16pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450 "2018-07-26T21:16:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjaminou4412](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@benjaminou4412](https://forums.sifive.com/u/benjaminou4412)\
**Post date:** [July 26, 2018, 9:16pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/1 "2018-07-26T21:16:27Z")

</div>

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:

> **[riscv/riscv-tools](https://github.com/riscv/riscv-tools)**
>
> RISC-V Tools (ISA Simulator and Tests). Contribute to riscv/riscv-tools development by creating an account on GitHub.

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:

> **[All Aboard, Part 2: Relocations in ELF Toolchains - SiFive](https://www.sifive.com/blog/all-aboard-part-2-relocations)**
>
> Our first stop on our exploration of the RISC-V toolchain will be an
> overview of ELF relocations and how they are used by the RISC-V
> toolchain. We'll shy away from discussing linker relaxations and their
> impact on performance for a follow-up blog...

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.

---

<div class="post-metadata">

**Author:** ![jimw](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jimw](https://forums.sifive.com/u/jimw)\
**Post date:** [July 26, 2018, 9:55pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/2 "2018-07-26T21:55:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![benjaminou4412](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@benjaminou4412](https://forums.sifive.com/u/benjaminou4412)\
**Post date:** [July 26, 2018, 10:16pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/3 "2018-07-26T22:16:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jimw](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jimw](https://forums.sifive.com/u/jimw)\
**Post date:** [July 27, 2018, 9:47pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/4 "2018-07-27T21:47:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![benjaminou4412](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@benjaminou4412](https://forums.sifive.com/u/benjaminou4412)\
**Post date:** [July 30, 2018, 8:37pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/5 "2018-07-30T20:37:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jimw](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jimw](https://forums.sifive.com/u/jimw)\
**Post date:** [July 30, 2018, 9:00pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/6 "2018-07-30T21:00:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![benjaminou4412](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@benjaminou4412](https://forums.sifive.com/u/benjaminou4412)\
**Post date:** [July 30, 2018, 10:51pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/7 "2018-07-30T22:51:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jimw](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jimw](https://forums.sifive.com/u/jimw)\
**Post date:** [July 30, 2018, 11:12pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/8 "2018-07-30T23:12:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![benjaminou4412](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@benjaminou4412](https://forums.sifive.com/u/benjaminou4412)\
**Post date:** [July 31, 2018, 10:39pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/9 "2018-07-31T22:39:39Z")

</div>

“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.

---

<div class="post-metadata">

**Author:** ![jimw](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jimw](https://forums.sifive.com/u/jimw)\
**Post date:** [July 31, 2018, 11:17pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/10 "2018-07-31T23:17:05Z")

</div>

You can log in as root with password sifive. You can find this in buildroot/configs/riscv64\_defconfig.

---

<div class="post-metadata">

**Author:** ![benjaminou4412](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@benjaminou4412](https://forums.sifive.com/u/benjaminou4412)\
**Post date:** [August 2, 2018, 8:14pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/11 "2018-08-02T20:14:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jimw](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jimw](https://forums.sifive.com/u/jimw)\
**Post date:** [August 2, 2018, 8:31pm UTC](https://forums.sifive.com/t/trying-to-build-the-linux-kernel-from-scratch-with-riscv-tools/1450/12 "2018-08-02T20:31:10Z")

</div>

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.
