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>

int
main(void)
{
        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 ':
random.cc:(.text._ZNSt12_GLOBAL__N_117__libc_getentropyEPv+0x10): 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':
random.cc:(.text._ZNSt13random_device7_M_initERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x78): 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.

See: https://github.com/zephyrproject-rtos/gcc/pull/8

Edited by Sören Tempel

Merge request reports

Loading