Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add picolibc compatibilty #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ CCONF_H ?= config.h
XCHAIN ?= riscv64-unknown-elf-
CFLAGS = -O2 -Wall -g -mabi=ilp32 -march=rv32imc
CFLAGS += -include $(CCONF_H) -DNDEBUG
LDFLAGS = -Wl,-Bstatic,-T,flow/riscv.ld,--no-relax
# If we use picolibc, adapt the flags.
# NOTE: the local ld script mostly provides flash and RAM layout
# (in our case flash starts at 0 and is in fact part of RAM)
ifeq ($(USE_PICOLIBC),1)
CFLAGS += -specs=picolibc.specs
LDFLAGS = -Wl,-Bstatic,-T,flow/picolibc.ld,--no-relax
else
LDFLAGS = -Wl,-Bstatic,-T,flow/riscv.ld,--no-relax
endif
KATNUM ?= 10
CFLAGS += -DKATNUM=$(KATNUM)

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ See [slh/README.md](slh/README.md) for more information.

As a prerequisite for simulation, you'll need:

* [Verilator](https://github.com/verilator/verilator) verilog simulator.
* [Verilator](https://github.com/verilator/verilator) verilog simulator. This might be packaged on some Linux distros.
* A RISC-V cross-compiler that supports bare-metal targets. You can build a suitable [riscv-gnu-toolchain](https://github.com/riscv/riscv-gnu-toolchain)
with `./configure --enable-multilib` and `make newlib`.
with `./configure --enable-multilib` and `make newlib`. Under some Linux based distros (such as Debian), it is possible to use the packaged
`gcc-riscv64-unknown-elf` along with the packaged `picolibc-riscv64-unknown-elf`: note that in this case, you will have to export the `USE_PICOLIBC=1` environment
variable when compiling: `USE_PICOLIBC=1 make veri` (this is due to some divergence between `newlib` and `picolibc` C standard libraries linking scripts).

Both of these may be available as packages for Linux operating systems. The name of your toolchain is set in `XCHAIN` variable in the [Makefile](Makefile).
The name of your toolchain is set in `XCHAIN` variable in the [Makefile](Makefile).

To build and run a quick end-to-end test, try:
```
Expand Down
35 changes: 35 additions & 0 deletions drv/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@ int test_sloth(); // test_sloth.c
int test_bench(); // test_bench.c
int test_leak(); // test_leak.c

#ifdef _PICOLIBC__
// XXX In case of Picolibc, redirect stdio related stuff to uart
// (see https://github.com/picolibc/picolibc/blob/main/doc/os.md)
// This allows to use printf family of functions
#include <stdio.h>
#include <stdlib.h>
static int sample_putc(char c, FILE *file)
{
(void) file; /* Not used in this function */
sio_putc(c); /* Defined by underlying system */
return c;
}

static int sample_getc(FILE *file)
{
unsigned char c;
(void) file; /* Not used in this function */
c = sio_getc(); /* Defined by underlying system */
return c;
}

FILE __stdio = FDEV_SETUP_STREAM(sample_putc,
sample_getc,
NULL,
_FDEV_SETUP_RW);

FILE *const stdin = &__stdio; __strong_reference(stdin, stdout); __strong_reference(stdin, stderr);
#endif


int main()
{
int fail = 0;
Expand Down Expand Up @@ -68,6 +98,11 @@ int main()
sio_putc(4); // translated to EOF
sio_putc(0);

#ifdef _PICOLIBC__
// XXX: in case of picolibc, explicitly exit as
// this is not performed at the return of main
exit(0);
#endif
return 0;
}

7 changes: 6 additions & 1 deletion flow/eprof.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ def cnt_str(cnt):
for rfn in lsc:
pfl=len(os.path.commonprefix([os.getcwd(), rfn]))
wfn = rfn[pfl:].replace("/","_");
try:
# Try to open the source file, if not possible skip it
fr = open(rfn, "r")
except:
print("[-] Skipping %s (not found)" % rfn)
continue
print("writing:", wfn)
fr = open(rfn, "r")
fw = open(wfn, "w")
ln = 0
for sf in fr:
Expand Down
6 changes: 6 additions & 0 deletions flow/picolibc.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# XXX: sizes to be fixed according to the (hardware) allocated RAM size of SLotH
__flash = 0x00000000;
__flash_size = 64k;
__ram = __flash + __flash_size;
__ram_size = 64k;
__stack_size = 8k;