Skip to content

community/gcc-cross-embedded: fix use of libstdc++ with newlib

Sören Tempel requested to merge nmeum/aports:newlib-g++-getentropy into master

Without this patch, linking any C++ software against libstdc++ fails with an undefined reference to getentropy(3). This is due to the fact that newlib declares a getentropy function prototype without providing the symbol. This causes libstdc++ to, incorrectly, assume that getentropy is available while it is not.

Without this patch:

$ cat hello_world.cpp
#include <iostream>

        std::cout << "Hello, World!" << std::endl;
        return 0;
$ riscv-none-elf-g++ -o test test.cpp -march=rv32i -mabi=ilp32
/usr/lib/gcc/riscv-none-elf/12.2.0/../../../../riscv-none-elf/bin/ld: /usr/lib/gcc/riscv-none-elf/12.2.0/../../../../riscv-none-elf/lib/rv32i/ilp32/libstdc++.a(random.o): in function `.L0 ': undefined reference to `getentropy'
/usr/lib/gcc/riscv-none-elf/12.2.0/../../../../riscv-none-elf/bin/ld: /usr/lib/gcc/riscv-none-elf/12.2.0/../../../../riscv-none-elf/lib/rv32i/ilp32/libstdc++.a(random.o): in function `.L23': undefined reference to `getentropy'
/usr/lib/gcc/riscv-none-elf/12.2.0/../../../../riscv-none-elf/bin/ld: /usr/lib/gcc/riscv-none-elf/12.2.0/../../../../riscv-none-elf/lib/rv32i/ilp32/libc.a(lib_a-arc4random.o): in function `_rs_stir':
arc4random.c:(.text._rs_stir+0x20): undefined reference to `getentropy'
collect2: error: ld returned 1 exit status

This commit fixes the issue by backporting a non-upstreamed GCC fix from the SDK for the Zephyr operating system. With this patch applied, libstdc++ does not attempt to use getentropy and hence the binary links fine.


Edited by Sören Tempel

Merge request reports