This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Add driver for riscv32-unknown-elf baremetal target
ClosedPublic

Authored by asb on May 14 2018, 2:36 AM.

Details

Summary

This patch adds a driver for the baremetal RISC-V target (i.e. riscv32-unknown-elf). For reference, D39963 added basic target info and added support for riscv32-linux-unknown-elf.

Patch by: asb (Alex Bradbury)

Diff Detail

Repository
rC Clang

Event Timeline

asb created this revision.May 14 2018, 2:36 AM

Could you include some documentation for how to construct a baremetal environment like the one this code expects? It's not clear what exactly you expect to be installed where.

asb added a comment.May 15 2018, 2:02 PM

Could you include some documentation for how to construct a baremetal environment like the one this code expects? It's not clear what exactly you expect to be installed where.

Thanks for taking a look Eli.

Building gcc+binutils+newlib will spit out a directory tree with the expected setup. e.g. a while ago I wrote a blog post trying to show how to build upstream RISC-V gcc+binutils+newlib in the simplest way possible. You need a multistage build for C++ support and other features, but those instructions are enough to get a baremetal cross compiler. The directory layout is the same as if you use the build script in the riscv-gnu-toolchain repo, doing ./configure --prefix=/my/target/path --with-arch=rv32i --with-abi=ilp32; make -j$(nproc). This is a more advanced multi-stage build.

Would you like to see more information on this added to the commit message, as comments in the code, or elsewhere?

e.g. this is the directory listing of the build output if you follow the linked blog instructions: https://pastebin.com/8HeCMpxF

Just a brief comment in the code explaining that would be fine.

mgrang accepted this revision.May 23 2018, 10:52 AM

LGTM.

lib/Driver/ToolChains/Gnu.cpp
1885

Alphabetically riscv32-unknown-elf should be the first in the list of triples.

This revision is now accepted and ready to land.May 23 2018, 10:52 AM

ping, Alex, could you commit that?

xbolva00 edited the summary of this revision. (Show Details)Jul 31 2018, 7:19 AM
This revision was automatically updated to reflect the committed changes.

Please don't commit patches on behalf of someone else if the author hasn't specifically requested it. There might be some issue which the author forgot to note on the review.

Please don't commit patches on behalf of someone else if the author hasn't specifically requested it. There might be some issue which the author forgot to note on the review.

Oh, ok.

It seems the ability to link objects has been broken by this change. As an example from our nightly tests:

Executing on host: riscv32-unknown-elf-clang /data/jenkins/workspace/riscv32-llvm-gcc/gcc-tests/gcc/testsuite/gcc.c-torture/execute/20080502-1.c  -march=rv32gc -mabi=ilp32      -O1  -w      -lm  -o ./20080502-1.exe    (timeout = 600)
spawn -ignore SIGHUP riscv32-unknown-elf-clang /data/jenkins/workspace/riscv32-llvm-gcc/gcc-tests/gcc/testsuite/gcc.c-torture/execute/20080502-1.c -march=rv32gc -mabi=ilp32 -O1 -w -lm -o ./20080502-1.exe
/data/jenkins/workspace/riscv32-llvm-gcc/install/bin/riscv32-unknown-elf-ld: cannot find crt0.o: No such file or directory
clang-8: error: ld command failed with exit code 1 (use -v to see invocation)
compiler exited with status 1
FAIL: gcc.c-torture/execute/20080502-1.c   -O1  (test for excess errors)

Running with -v shows the link command as:

"/data/jenkins/workspace/riscv32-llvm-gcc/install/bin/riscv32-unknown-elf-ld" crt0.o /data/jenkins/workspace/riscv32-llvm-gcc/install/bin/../lib/gcc/riscv32-unknown-elf/8.1.0/crtbegin.o -L/lib -L/data/jenkins/workspace/riscv32-llvm-gcc/install/bin/../lib/gcc/riscv32-unknown-elf/8.1.0 /tmp/20080502-1-2b0022.o -lm --start-group -lc -lgloss --end-group -lgcc /data/jenkins/workspace/riscv32-llvm-gcc/install/bin/../lib/gcc/riscv32-unknown-elf/8.1.0/crtend.o -o ./20080502-1.exe

What's noticeable is crt0.o is specified just as a file, and we're trying to link in my host /lib/ (-L/lib)? If I manually specify a sysroot (via --sysroot=$INSTALLDIR/riscv32-unknown-elf), then my link succeeds, adding the path to crt0.o, and the correct /lib:

"/data/jenkins/workspace/riscv32-llvm-gcc/install/bin/riscv32-unknown-elf-ld" --sysroot=/data/jenkins/workspace/riscv32-llvm-gcc/install/riscv32-unknown-elf /data/jenkins/workspace/riscv32-llvm-gcc/install/riscv32-unknown-elf/lib/crt0.o /data/jenkins/workspace/riscv32-llvm-gcc/install/bin/../lib/gcc/riscv32-unknown-elf/8.1.0/crtbegin.o -L/data/jenkins/workspace/riscv32-llvm-gcc/install/riscv32-unknown-elf/lib -L/data/jenkins/workspace/riscv32-llvm-gcc/install/bin/../lib/gcc/riscv32-unknown-elf/8.1.0 /tmp/20080502-1-d53be0.o -lm --start-group -lc -lgloss --end-group -lgcc /data/jenkins/workspace/riscv32-llvm-gcc/install/bin/../lib/gcc/riscv32-unknown-elf/8.1.0/crtend.o -o ./20080502-1.exe

I think there's some missing calculate the correct sysroot directory logic missing, unless linking clang to riscv32-unknown-elf-clang is no longer sufficient for everything to work now?

I've submitted a patch to address Simon's issues in D50246