K210 exceptions handling not working

Hi all

I wonder if anyone else is using the K210 platform, specifically the MIAX One Dock:

platform = kendryte210
board = sipeed-maix-one-dock
framework = kendryte-freertos-sdk

I am having problems with the dma access methods as they use exceptions and seemed to cause a fatal crash when called. So I setup a standalone test and it looks like for some reason exceptions don’t work at all. When I run the code here: nemach-embedded / maix-pio-exceptions · GitLab I get

throwing exception        
W (39691089) SYSCALL: sys_exit called with 0x1

Does anyone know why exceptions are disabled or how to enable them?

Thanks
Neil

I’ve looked at the compiler flags

riscv64-unknown-elf-g++ -o .pio\build\sipeed-maix-one-dock\src\exception_test.o -c -std=gnu++17 -mcmodel=medany -mabi=lp64f -march=rv64imafc -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -ffast-math -fno-math-errno -fsingle-precision-constant -O2 -ggdb -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-error=maybe-uninitialized -Wextra -Werror=frame-larger-than=65536 -Wno-unused-parameter -Wno-unused-function -Wno-implicit-fallthrough -Wno-sign-compare -Wno-error=missing-braces -Wno-error=return-type -Wno-error=pointer-sign -Wno-missing-braces -Wno-strict-aliasing -Wno-implicit-fallthrough -Wno-missing-field-initializers -Wno-int-to-pointer-cast -Wno-error=comment -Wno-error=logical-not-parentheses -Wno-error=duplicate-decl-specifier -Wno-error=parentheses -Wno-error=maybe-uninitialized -DPLATFORMIO=50001 -DCONFIG_LOG_ENABLE -DCONFIG_LOG_LEVEL=LOG_INFO -DDEBUG=1 -D__riscv64 -DK210 -DARCH=K210 -DF_CPU=400000000L -Iinclude -Isrc [...] src\exception_test.cpp

and there’s no -fno-exceptions, so that’s ok.

So next looking for compiler errors in

"C:\Users\Maxi\.platformio\packages\toolchain-kendryte210\bin\riscv64-unknown-elf-gcc.exe" --version
riscv64-unknown-elf-gcc.exe (GCC) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.

related to exceptions brings up this

which pretty much describes the problem. The Standard C++ library (stdc++) must be linked before the standard GCC library ( c, gcc).

So what is the builder script doing?

Exactly the wrong order.

To verify this (since I don’t have real hardware to test on), please do:

  1. Find the PlatformIO paltforms folder (C:\Users\<user>\.platformio\platforms, /home/<user>/.platformio/platforms, …)
  2. Open the file \kendryte210\builder\frameworks\kendryte-freertos-sdk.py for edit
  3. Replace line 108 with
        "stdc++", "m", "atomic", "gcc", "c"
  1. Clean and recompile the project (pio run -t clean, pio run -e sipeed-maix-one-dock -t upload) and recheck output.

The github issue also has a minor error where -lgcc is mentioned twice. So if the above link order doesn’t work you can try things like

        "stdc++", "m", "atomic", "c", "gcc"

or the exact replication

        "stdc++", "m", "atomic", "gcc", "c", "gcc"

which looks weird but okay.

@maxgerhardt Thank you for the investigation and comprehensive reply - it’s really helpful for me to understand your investigation process and not just the conclusion! I will try what you suggest later today and report findings.

So I’ve tested various combinations of the flags but still cannot solve the error. Here is a tested linker command line

riscv64-unknown-elf-g++ -o .pio\build\sipeed-maix-one-dock\firmware.elf -T C:\Users\neil.platformio\packages\framework-kendryte-freertos-sdk\lds\kendryte.ld -nostartfiles -static -Wl,–gc-sections -Wl,-static -Wl,–start-group -Wl,–whole-archive -Wl,–no-whole-archive -Wl,–end-group -Wl,-EL -Wl,–no-relax -Wl,–start-group -lstdc++ -lm -lc -lgcc -lc -Wl,–end-group C:\Users\neil.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crti.o C:\Users\neil.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtbegin.o C:\Users\neil.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtend.o C:\Users\neil.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtn.o .pio\build\sipeed-maix-one-dock\src\exception_test.o .pio\build\sipeed-maix-one-dock\src\main.o -L.pio\build\sipeed-maix-one-dock -Wl,–start-group .pio\build\sipeed-maix-one-dock\libsdk-bsp.a .pio\build\sipeed-maix-one-dock\libsdk-drivers.a .pio\build\sipeed-maix-one-dock\libsdk-freertos.a .pio\build\sipeed-maix-one-dock\libsdk-hal.a .pio\build\sipeed-maix-one-dock\libsdk-posix.a .pio\build\sipeed-maix-one-dock\libthird_party-fatfs.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipcore.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipapi.a .pio\build\sipeed-maix-one-dock\libthird_party-lwiparch.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipnetif.a -lstdc++ -lm -latomic -lc -lgcc -lc -Wl,–end-group

Note that I also played around with the LINKFLAGS as they appear earlier in the command line - in this case I added -lstdc++ in front of the other libs. But my exceptions are still failing.

Any other ideas?

@maxgerhardt - I’ve managed to get exceptions working with my K210 board now by moving the crtend object files to the end of the command line:

riscv64-unknown-elf-g++ -o .pio\build\sipeed-maix-one-dock\firmware.elf -T C:\Users
neil.platformio\packages\framework-kendryte-freertos-sdk\lds\kendryte.ld -nostartfiles -static -Wl,–gc-sections -Wl,-static -Wl,–no-whole-archive -Wl,-EL -Wl,–no-relax -Wl,–start-group -lstdc++ -latomic -lm -lc -lgcc -lc -Wl,–end-group C:\Users\neil.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crti.o C:\Users\neil.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtbegin.o .pio\build\sipeed-maix-one-dock\src\exception_test.o .pio\build\sipeed-maix-one-dock\src\main.o -L.pio\build\sipeed-maix-one-dock -Wl,–start-group .pio\build\sipeed-maix-one-dock\libsdk-bsp.a .pio\build\sipeed-maix-one-dock\libsdk-drivers.a .pio\build\sipeed-maix-one-dock\libsdk-freertos.a .pio\build\sipeed-maix-one-dock\libsdk-hal.a .pio\build\sipeed-maix-one-dock\libsdk-posix.a .pio\build\sipeed-maix-one-dock\libthird_party-fatfs.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipcore.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipapi.a .pio\build\sipeed-maix-one-dock\libthird_party-lwiparch.a .pio\build\sipeed-maix-one-dock\libthird_party-lwipnetif.a -lstdc++ -latomic -lm -lc -lgcc -lc -Wl,–end-group C:\Users\neil.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtend.o C:\Users\neil.platformio\packages\toolchain-kendryte210\lib\gcc\riscv64-unknown-elf\8.2.0\crtn.o

But unfortunately I’m not sure how to get this into the platformio build process. The crt files are all currently included in the LINKFLAGS. Is it possible to split them? Or is there another way to reorder symbols, eg using the kendryte.ld files? Any hints appreciated…

Oh yeah that’s weird. I’ve overlooked the code in

…I don’t think it’s good that these libraries appear twice and also that stdc++ only appears in LIBS but not in the custom LINKFLAGS – I think that’s double. Maybe it works when LIBS is an empty array and all is in the linker flags? Or only the starting libs are in LIBS but the ending stuff is in LINKFLAGS?

At this point it’s probably also fair game to open an issue at Issues · sipeed/platform-kendryte210 · GitHub since you have a reproducable error and a manual fix for the issue.

After messing around some more I seem to have resolved the issue by adjusting the alignment of the eh_frame segment in the SDK ld file. See pull request for further info: