undefined reference to `__bswapdi2' when trying to link tinycbor for RISC-V


Stefan Hristozov
 

I am getting undefined reference to `__bswapdi2' when I am trying to link tinycbor for RISC-V (west build -b=hifive1)
stefan@t460s:~/workspaces/oscore-edhoc/test$ west build -b=hifive1
[1/5] Linking C executable zephyr/zephyr_prebuilt.elf
FAILED: zephyr/zephyr_prebuilt.elf
: && ccache /opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf/bin/riscv64-zephyr-elf-gcc    zephyr/CMakeFiles/zephyr_prebuilt.dir/misc/empty_file.c.obj  -o zephyr/zephyr_prebuilt.elf  -Wl,-T  zephyr/linker.cmd  -Wl,-Map=/home/stefan/workspaces/oscore-edhoc/test/build/zephyr/zephyr_prebuilt.map  -Wl,--whole-archive  app/libapp.a  zephyr/libzephyr.a  zephyr/arch/common/libarch__common.a  zephyr/arch/arch/riscv/core/libarch__riscv__core.a  zephyr/lib/libc/minimal/liblib__libc__minimal.a  zephyr/lib/posix/liblib__posix.a  zephyr/boards/riscv/hifive1/libboards__riscv__hifive1.a  zephyr/subsys/testsuite/ztest/libsubsys__testsuite__ztest.a  zephyr/drivers/gpio/libdrivers__gpio.a  zephyr/drivers/spi/libdrivers__spi.a  zephyr/drivers/serial/libdrivers__serial.a  -Wl,--no-whole-archive  zephyr/kernel/libkernel.a  zephyr/CMakeFiles/offsets.dir/arch/riscv/core/offsets/offsets.c.obj  -L"/opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf/bin/../lib/gcc/riscv64-zephyr-elf/9.2.0"  -L/home/stefan/workspaces/oscore-edhoc/test/build/zephyr  -lgcc  -Wl,--print-memory-usage  ../build_lib_test/libtest.a  -mabi=ilp32  -march=rv32ima  -Wl,--gc-sections  -Wl,--build-id=none  -Wl,--sort-common=descending  -Wl,--sort-section=alignment  -Wl,-u,_OffsetAbsSyms  -Wl,-u,_ConfigAbsSyms  -nostdlib  -static  -no-pie  -Wl,-X  -Wl,-N  -Wl,--orphan-handling=warn && :
Memory region         Used Size  Region Size  %age Used
             ROM:       73756 B        12 MB      0.59%
             RAM:       15600 B        16 KB     95.21%
        IDT_LIST:         553 B         2 KB     27.00/opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf/bin/../lib/gcc/riscv64-zephyr-elf/9.2.0/../../../../riscv64-zephyr-elf/bin/ld: ../build_lib_test/libtest.a(cborencoder.o): in function `put64':
/home/stefan/workspaces/oscore-edhoc/test/lib_test/../../externals/tinycbor/src/cborencoder.c:255: undefined reference to `__bswapdi2'
collect2: error: ld returned 1 exit status
%
ninja: build stopped: subcommand failed.
With grep I can see that this function is available in libgcc.a for RISC-V
stefan@t460s:/opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf$ grep -nrw '__bswapdi2'
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv32i/ilp32/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv32iac/ilp32/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv32imac/ilp32/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv64imafdc/lp64d/medany/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv64imafdc/lp64d/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv32imafc/ilp32f/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv32im/ilp32/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv64imac/lp64/medany/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv64imac/lp64/libgcc.a matches
Binary file libexec/gcc/riscv64-zephyr-elf/9.2.0/lto1 matches
Binary file libexec/gcc/riscv64-zephyr-elf/9.2.0/cc1 matches
Binary file libexec/gcc/riscv64-zephyr-elf/9.2.0/cc1plus matches
How to tell my zephyr project to use libgcc?


Kumar Gala
 

On Jan 29, 2021, at 1:42 PM, Stefan Hristozov <stefan.hristozov@aisec.fraunhofer.de> wrote:

I am getting undefined reference to `__bswapdi2' when I am trying to link tinycbor for RISC-V (west build -b=hifive1)
stefan@t460s:~/workspaces/oscore-edhoc/test$ west build -b=hifive1
[1/5] Linking C executable zephyr/zephyr_prebuilt.elf
FAILED: zephyr/zephyr_prebuilt.elf
: && ccache /opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf/bin/riscv64-zephyr-elf-gcc zephyr/CMakeFiles/zephyr_prebuilt.dir/misc/empty_file.c.obj -o zephyr/zephyr_prebuilt.elf -Wl,-T zephyr/linker.cmd -Wl,-Map=/home/stefan/workspaces/oscore-edhoc/test/build/zephyr/zephyr_prebuilt.map -Wl,--whole-archive app/libapp.a zephyr/libzephyr.a zephyr/arch/common/libarch__common.a zephyr/arch/arch/riscv/core/libarch__riscv__core.a zephyr/lib/libc/minimal/liblib__libc__minimal.a zephyr/lib/posix/liblib__posix.a zephyr/boards/riscv/hifive1/libboards__riscv__hifive1.a zephyr/subsys/testsuite/ztest/libsubsys__testsuite__ztest.a zephyr/drivers/gpio/libdrivers__gpio.a zephyr/drivers/spi/libdrivers__spi.a zephyr/drivers/serial/libdrivers__serial.a -Wl,--no-whole-archive zephyr/kernel/libkernel.a zephyr/CMakeFiles/offsets.dir/arch/riscv/core/offsets/offsets.c.obj -L"/opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf/bin/../lib/gcc/riscv64-zephyr-elf/9.2.0" -L/home/stefan/workspaces/oscore-edhoc/test/build/zephyr -lgcc -Wl,--print-memory-usage ../build_lib_test/libtest.a -mabi=ilp32 -march=rv32ima -Wl,--gc-sections -Wl,--build-id=none -Wl,--sort-common=descending -Wl,--sort-section=alignment -Wl,-u,_OffsetAbsSyms -Wl,-u,_ConfigAbsSyms -nostdlib -static -no-pie -Wl,-X -Wl,-N -Wl,--orphan-handling=warn && :
Memory region Used Size Region Size %age Used
ROM: 73756 B 12 MB 0.59%
RAM: 15600 B 16 KB 95.21%
IDT_LIST: 553 B 2 KB 27.00/opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf/bin/../lib/gcc/riscv64-zephyr-elf/9.2.0/../../../../riscv64-zephyr-elf/bin/ld: ../build_lib_test/libtest.a(cborencoder.o): in function `put64':
/home/stefan/workspaces/oscore-edhoc/test/lib_test/../../externals/tinycbor/src/cborencoder.c:255: undefined reference to `__bswapdi2'
collect2: error: ld returned 1 exit status
%
ninja: build stopped: subcommand failed.
With grep I can see that this function is available in libgcc.a for RISC-V
stefan@t460s:/opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf$ grep -nrw '__bswapdi2'
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv32i/ilp32/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv32iac/ilp32/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv32imac/ilp32/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv64imafdc/lp64d/medany/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv64imafdc/lp64d/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv32imafc/ilp32f/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv32im/ilp32/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv64imac/lp64/medany/libgcc.a matches
Binary file lib/gcc/riscv64-zephyr-elf/9.2.0/rv64imac/lp64/libgcc.a matches
Binary file libexec/gcc/riscv64-zephyr-elf/9.2.0/lto1 matches
Binary file libexec/gcc/riscv64-zephyr-elf/9.2.0/cc1 matches
Binary file libexec/gcc/riscv64-zephyr-elf/9.2.0/cc1plus matches
How to tell my zephyr project to use libgcc?
Based on the gcc line, you are getting libgcc pulled in via -L"/opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf/bin/../lib/gcc/riscv64-zephyr-elf/9.2.0” and -lgcc.

What version of zephyr are you using? Have you tried running the link with a -Wl,--start-group …. -Wl,—end-group?

- k


Stefan Hristozov
 

Hi
I am using Zephyr version: 2.3.99 with West version: v0.7.2.

How to provide -Wl,--start-group …. -Wl,—end-group linker flags to my Zephyr project?

Br,
Stefan


Kumar Gala
 

On Feb 8, 2021, at 1:16 PM, Stefan Hristozov <stefan.hristozov@aisec.fraunhofer.de> wrote:

Hi
I am using Zephyr version: 2.3.99 with West version: v0.7.2.

How to provide -Wl,--start-group …. -Wl,—end-group linker flags to my Zephyr project?
Try something like:

/opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf/bin/riscv64-zephyr-elf-gcc … -Wl,--start-group zephyr/kernel/libkernel.a zephyr/CMakeFiles/offsets.dir/arch/riscv/core/offsets/offsets.c.obj -L"/opt/zephyr-sdk-0.11.3/riscv64-zephyr-elf/bin/../lib/gcc/riscv64-zephyr-elf/9.2.0" -L/home/stefan/workspaces/oscore-edhoc/test/build/zephyr -lgcc -Wl,—end-group …

- k