From b9e867d088935d9f0bf312e6dbf3e4976850dfd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B6rn=20SVENSSON?= Date: Tue, 8 Aug 2023 18:09:50 +0200 Subject: Add stub for getentropy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Example test case that fails with undefined reference to getentropy: Testing g++.robertl/eb77.C, -std=c++20 doing compile Executing on host: /build/gcc-13-2709-g9ac9fde961f/bin/arm-none-eabi-g++ /build/src/gcc/gcc/testsuite/g++.old-deja/g++.robertl/eb77.C -mthumb -march=armv6s-m -mcpu=cortex-m0 -mfloat-abi=soft -fdiagnostics-plain-output -fmessage-length=0 -std=c++20 -pedantic-errors -Wno-long-long -Wl,--start-group -lc -lm -Wl,--end-group --specs=nosys.specs -Wl,--allow-multiple-definition -Wl,-u,_isatty,-u,_fstat -Wl,-wrap,exit -Wl,-wrap,_exit -Wl,-wrap,main -Wl,-wrap,abort -Wl,g++_tg.o -lm -o ./eb77.exe (timeout = 800) spawn -ignore SIGHUP /build/gcc-13-2709-g9ac9fde961f/bin/arm-none-eabi-g++ /build/src/gcc/gcc/testsuite/g++.old-deja/g++.robertl/eb77.C -mthumb -march=armv6s-m -mcpu=cortex-m0 -mfloat-abi=soft -fdiagnostics-plain-output -fmessage-length=0 -std=c++20 -pedantic-errors -Wno-long-long -Wl,--start-group -lc -lm -Wl,--end-group --specs=nosys.specs -Wl,--allow-multiple-definition -Wl,-u,_isatty,-u,_fstat -Wl,-wrap,exit -Wl,-wrap,_exit -Wl,-wrap,main -Wl,-wrap,abort -Wl,g++_tg.o -lm -o ./eb77.exe pid is 28414 -28414 /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/bin/ld: /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libstdc++.a(random.o): in function `std::(anonymous namespace)::__libc_getentropy(void*)': (.text._ZNSt12_GLOBAL__N_117__libc_getentropyEPv+0x8): undefined reference to `getentropy' /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/bin/ld: /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libstdc++.a(random.o): in function `std::random_device::_M_init(std::__cxx11::basic_string, std::allocator > const&)': (.text._ZNSt13random_device7_M_initERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x58): undefined reference to `getentropy' /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/bin/ld: /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libc.a(libc_a-arc4random.o): in function `_rs_stir': (.text._rs_stir+0x8): undefined reference to `getentropy' collect2: error: ld returned 1 exit status pid is -1 close result is 28414 exp6 0 1 output is /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/bin/ld: /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libstdc++.a(random.o): in function `std::(anonymous namespace)::__libc_getentropy(void*)': (.text._ZNSt12_GLOBAL__N_117__libc_getentropyEPv+0x8): undefined reference to `getentropy' /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/bin/ld: /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libstdc++.a(random.o): in function `std::random_device::_M_init(std::__cxx11::basic_string, std::allocator > const&)': (.text._ZNSt13random_device7_M_initERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x58): undefined reference to `getentropy' /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/bin/ld: /build/gcc-13-2709-g9ac9fde961f/bin/../lib/gcc/arm-none-eabi/13.0.0/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libc.a(libc_a-arc4random.o): in function `_rs_stir': (.text._rs_stir+0x8): undefined reference to `getentropy' collect2: error: ld returned 1 exit status status 1 compiler exited with status 1 FAIL: g++.old-deja/g++.robertl/eb77.C -std=c++20 (test for excess errors) Excess errors: (.text._ZNSt12_GLOBAL__N_117__libc_getentropyEPv+0x8): undefined reference to `getentropy' (.text._ZNSt13random_device7_M_initERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x58): undefined reference to `getentropy' (.text._rs_stir+0x8): undefined reference to `getentropy' UNRESOLVED: g++.old-deja/g++.robertl/eb77.C -std=c++20 compilation failed to produce executable Contributed by STMicroelectronics Signed-off-by: Torbjörn SVENSSON --- newlib/libc/include/_syslist.h | 1 + newlib/libc/include/reent.h | 2 ++ newlib/libc/reent/Makefile.inc | 2 ++ newlib/libc/reent/getentropyr.c | 53 ++++++++++++++++++++++++++++++++++++ newlib/libc/syscalls/Makefile.inc | 1 + newlib/libc/syscalls/sysgetentropy.c | 12 ++++++++ 6 files changed, 71 insertions(+) create mode 100644 newlib/libc/reent/getentropyr.c create mode 100644 newlib/libc/syscalls/sysgetentropy.c (limited to 'newlib/libc') diff --git a/newlib/libc/include/_syslist.h b/newlib/libc/include/_syslist.h index 271644efa..2946af551 100644 --- a/newlib/libc/include/_syslist.h +++ b/newlib/libc/include/_syslist.h @@ -24,6 +24,7 @@ #define _unlink unlink #define _wait wait #define _write write +#define _getentropy getentropy #endif /* MISSING_SYSCALL_NAMES */ #if defined MISSING_SYSCALL_NAMES || !defined HAVE_OPENDIR diff --git a/newlib/libc/include/reent.h b/newlib/libc/include/reent.h index 23d572cd3..b1dd2a7c4 100644 --- a/newlib/libc/include/reent.h +++ b/newlib/libc/include/reent.h @@ -125,6 +125,7 @@ struct timezone; #define _unlink_r(__reent, __path) unlink(__path) #define _wait_r(__reent, __status) wait(__status) #define _write_r(__reent, __fd, __buff, __cnt) write(__fd, __buff, __cnt) +#define _getentropy_r(__reent, __buff, __cnt) getentropy(__buff, __cnt) #define _gettimeofday_r(__reent, __tp, __tzp) gettimeofday(__tp, __tzp) #ifdef __LARGE64_FILES @@ -156,6 +157,7 @@ extern _CLOCK_T_ _times_r (struct _reent *, struct tms *); extern int _unlink_r (struct _reent *, const char *); extern int _wait_r (struct _reent *, int *); extern _ssize_t _write_r (struct _reent *, int, const void *, size_t); +extern int _getentropy_r (struct _reent *, void *, size_t); /* This one is not guaranteed to be available on all targets. */ extern int _gettimeofday_r (struct _reent *, struct timeval *__tp, void *__tzp); diff --git a/newlib/libc/reent/Makefile.inc b/newlib/libc/reent/Makefile.inc index 38ddf5766..26f952bfa 100644 --- a/newlib/libc/reent/Makefile.inc +++ b/newlib/libc/reent/Makefile.inc @@ -4,6 +4,7 @@ libc_a_SOURCES += \ %D%/impure.c \ %D%/fcntlr.c \ %D%/fstatr.c \ + %D%/getentropyr.c \ %D%/getreent.c \ %D%/gettimeofdayr.c \ %D%/isattyr.c \ @@ -46,6 +47,7 @@ LIBC_CHEWOUT_FILES += \ %D%/execr.def \ %D%/fcntlr.def \ %D%/fstatr.def \ + %D%/getentropyr.def \ %D%/gettimeofdayr.def \ %D%/linkr.def \ %D%/lseekr.def \ diff --git a/newlib/libc/reent/getentropyr.c b/newlib/libc/reent/getentropyr.c new file mode 100644 index 000000000..e3f85e41b --- /dev/null +++ b/newlib/libc/reent/getentropyr.c @@ -0,0 +1,53 @@ +/* Reentrant version of getentropy system call. */ + +#include +#include +#include <_syslist.h> + +/* Some targets provides their own versions of these functions. Those + targets should define REENTRANT_SYSCALLS_PROVIDED in TARGET_CFLAGS. */ + +#ifdef _REENT_ONLY +#ifndef REENTRANT_SYSCALLS_PROVIDED +#define REENTRANT_SYSCALLS_PROVIDED +#endif +#endif + +#ifndef REENTRANT_SYSCALLS_PROVIDED + +/* We use the errno variable used by the system dependent layer. */ +#undef errno +extern int errno; + +/* +FUNCTION + <<_getentropy_r>>---Reentrant version of getentropy + +INDEX + _getentropy_r + +SYNOPSIS + #include + int _getentropy_r(struct _reent *<[ptr]>, + void *<[buf]>, size_t <[buflen]>); + +DESCRIPTION + This is a reentrant version of <>. It + takes a pointer to the global data block, which holds + <>. +*/ + +int +_getentropy_r (struct _reent *ptr, + void *buf, + size_t buflen) +{ + int ret; + + errno = 0; + if ((ret = _getentropy (buf, buflen)) == -1 && errno != 0) + _REENT_ERRNO(ptr) = errno; + return ret; +} + +#endif /* ! defined (REENTRANT_SYSCALLS_PROVIDED) */ diff --git a/newlib/libc/syscalls/Makefile.inc b/newlib/libc/syscalls/Makefile.inc index 627a4dfd2..7f7bbbddb 100644 --- a/newlib/libc/syscalls/Makefile.inc +++ b/newlib/libc/syscalls/Makefile.inc @@ -4,6 +4,7 @@ libc_a_SOURCES += \ %D%/sysclose.c \ %D%/sysfcntl.c \ %D%/sysfstat.c \ + %D%/sysgetentropy.c \ %D%/sysgetpid.c \ %D%/sysgettod.c \ %D%/sysisatty.c \ diff --git a/newlib/libc/syscalls/sysgetentropy.c b/newlib/libc/syscalls/sysgetentropy.c new file mode 100644 index 000000000..d68ccbc72 --- /dev/null +++ b/newlib/libc/syscalls/sysgetentropy.c @@ -0,0 +1,12 @@ +/* connector for getentropy */ + +#include +#include +#include + +int +getentropy (void *buf, + size_t buflen) +{ + return _getentropy_r (_REENT, buf, buflen); +} -- cgit v1.2.3