fix(newlib): adapt changes

This commit is contained in:
Alexey Lapshin
2026-02-05 13:40:11 +07:00
parent 09d25aba62
commit 7b1100db2d
38 changed files with 587 additions and 266 deletions
+2 -1
View File
@@ -9,6 +9,7 @@
#include "esp_system.h"
#include "soc/soc_memory_layout.h"
#include "esp_private/cache_utils.h"
#include "sdkconfig.h"
#define ASSERT_STR "assert failed: "
#define CACHE_DISABLED_STR "<cached disabled>"
@@ -39,7 +40,7 @@ void __attribute__((noreturn)) __assert_func(const char *file, int line, const c
esp_system_abort(buff);
#else
char addr[11] = { 0 };
char buff[200];
char buff[CONFIG_LIBC_ASSERT_BUFFER_SIZE];
char lbuf[5];
uint32_t rem_len = sizeof(buff) - 1;
uint32_t off = 0;
+21
View File
@@ -0,0 +1,21 @@
[mapping:esp_libc]
archive: libnewlib.a
entries:
if LIBC_OPTIMIZED_MISALIGNED_ACCESS = y:
memcpy (noflash)
memmove (noflash)
memcmp (noflash)
strcpy (noflash)
strncpy (noflash)
strcmp (noflash)
strncmp (noflash)
if LIBC_MISC_IN_IRAM = y:
if HEAP_PLACE_FUNCTION_INTO_FLASH = n:
heap (noflash)
abort (noflash)
assert (noflash)
stdatomic (noflash)
if STDATOMIC_S32C1I_SPIRAM_WORKAROUND = y:
stdatomic_s32c1i (noflash)
if STDATOMIC_S32C1I_SPIRAM_WORKAROUND = y:
stdatomic_s32c1i (noflash)
+1
View File
@@ -6,6 +6,7 @@
#include <sys/random.h>
#include <errno.h>
#include <stddef.h>
int getentropy(void *buffer, size_t length)
{
@@ -3,10 +3,6 @@
# and/or applications may assume that because these functions normally are in ROM, they are accessible even when flash is
# inaccessible. To work around this, this ld fragment places these functions in RAM instead. If the ROM functions are used,
# these defines do nothing, so they can still be included in that situation.
#
#
# Note: the only difference between esp32-spiram-rom-functions-c.lf
# and esp32-spiram-rom-functions-psram-workaround.lf is the archive name.
[mapping:libc]
archive:
-11
View File
@@ -1,11 +0,0 @@
[mapping:newlib]
archive: libnewlib.a
entries:
if LIBC_MISC_IN_IRAM = y:
if HEAP_PLACE_FUNCTION_INTO_FLASH = n:
heap (noflash)
abort (noflash)
assert (noflash)
stdatomic (noflash)
if STDATOMIC_S32C1I_SPIRAM_WORKAROUND = y:
stdatomic_s32c1i (noflash)
+1 -1
View File
@@ -101,7 +101,7 @@ static struct syscall_stub_table s_stub_table = {
._lock_release = &_lock_release,
._lock_release_recursive = &_lock_release_recursive,
#endif
#ifdef CONFIG_NEWLIB_NANO_FORMAT
#ifdef CONFIG_LIBC_NEWLIB_NANO_FORMAT
._printf_float = &_printf_float,
._scanf_float = &_scanf_float,
#else
@@ -13,9 +13,10 @@
#include <stdio-bufio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <signal.h>
#include <sys/unistd.h>
#include <sys/reent.h>
#include <sys/lock.h>
#include <assert.h>
#include <fcntl.h>
#include "esp_newlib.h"
+72 -24
View File
@@ -18,14 +18,11 @@
#include <string.h>
#include <stdint.h>
#include "esp_attr.h"
#include "../../string/local.h"
#include "string/local.h"
#define unlikely(X) __builtin_expect (!!(X), 0)
IRAM_ATTR
void *
__attribute__((optimize("-Os")))
__inhibit_loop_to_libcall
memcpy(void *__restrict aa, const void *__restrict bb, size_t n)
{
@@ -65,29 +62,80 @@ small:
if (unlikely(lend - la > 8)) {
while (lend - la > 8) {
long b0 = *lb++;
long b1 = *lb++;
long b2 = *lb++;
long b3 = *lb++;
long b4 = *lb++;
long b5 = *lb++;
long b6 = *lb++;
long b7 = *lb++;
long b8 = *lb++;
*la++ = b0;
*la++ = b1;
*la++ = b2;
*la++ = b3;
*la++ = b4;
*la++ = b5;
*la++ = b6;
*la++ = b7;
*la++ = b8;
/*
* long b0 = *lb++;
* long b1 = *lb++;
* long b2 = *lb++;
* long b3 = *lb++;
* long b4 = *lb++;
* long b5 = *lb++;
* long b6 = *lb++;
* long b7 = *lb++;
* long b8 = *lb++;
* *la++ = b0;
* *la++ = b1;
* *la++ = b2;
* *la++ = b3;
* *la++ = b4;
* *la++ = b5;
* *la++ = b6;
* *la++ = b7;
* *la++ = b8;
*/
long src0, src1, src2, src3;
long src4, src5, src6, src7;
long src8;
/* DIG-694: need at least 2 instructions between lw and sw */
asm volatile("lw %0, 0(%10)\n" // scr0 = lb[0];
"lw %1, 4(%10)\n" // scr1 = lb[1];
"lw %2, 8(%10)\n" // scr2 = lb[2];
"lw %3, 12(%10)\n" // scr3 = lb[3];
"lw %4, 16(%10)\n" // scr4 = lb[4];
"lw %5, 20(%10)\n" // scr5 = lb[5];
"lw %6, 24(%10)\n" // scr6 = lb[6];
"lw %7, 28(%10)\n" // scr7 = lb[7];
"lw %8, 32(%10)\n" // scr8 = lb[8];
"addi %9, %9, 36\n" // la += 8 * 9;
"addi %10, %10, 36\n" // lb += 8 * 9;
"sw %0, -36(%9)\n" // *(la - 9) = src;
"sw %1, -32(%9)\n" // *(la - 8) = src;
"sw %2, -28(%9)\n" // *(la - 7) = src;
"sw %3, -24(%9)\n" // *(la - 6) = src;
"sw %4, -20(%9)\n" // *(la - 5) = src;
"sw %5, -16(%9)\n" // *(la - 4) = src;
"sw %6, -12(%9)\n" // *(la - 3) = src;
"sw %7, -8(%9)\n" // *(la - 2) = src;
"sw %8, -4(%9)\n" // *(la - 1) = src;
: "=r"(src0), "=r"(src1), "=r"(src2), "=r"(src3),
"=r"(src4), "=r"(src5), "=r"(src6), "=r"(src7),
"=r"(src8),
"+r"(la), "+r"(lb)
:: "memory");
}
}
while (la < lend) {
BODY(la, lb, long);
/*
* BODY(la, lb, long);
*/
long src0;
#ifdef __OPTIMIZE_SIZE__
#error "Enabled Os optimization may not work properly for DIG-694"
/*
* Replacing the string:
* *la++ = tt;
* To:
* "addi %2, %4, 4\n" // la++;
* "sw %0, -4(%4)\n" // *(la-1) = src0;
* May break some optimizations and slightly reduce performance.
*/
#endif
/* DIG-694: need at least 2 instructions between lw and sw */
asm volatile("lw %0, 0(%1)\n" // long src0 = *lb;
"addi %1, %1, 4\n" // lb++;
"addi %2, %2, 4\n" // la++;
"sw %0, -4(%2)\n" // *(la-1) = src0;
: "=&r"(src0), "+r"(lb), "+r"(la)
:: "memory");
}
a = (char *)la;
+150
View File
@@ -0,0 +1,150 @@
/*
* SPDX-FileCopyrightText: 1994-2009 Red Hat, Inc.
*
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND Apache-2.0
*
* SPDX-FileContributor: 2025 Espressif Systems (Shanghai) CO LTD
*/
#include <string.h>
#include <_ansi.h>
#include <stddef.h>
#include <limits.h>
#include "string/local.h"
void *
__inhibit_loop_to_libcall
memmove(void *dst_void,
const void *src_void,
size_t length)
{
char *dst = dst_void;
const char *src = src_void;
long *aligned_dst;
const long *aligned_src;
if (src < dst && dst < src + length) {
/* Destructive overlap...have to copy backwards */
src += length;
dst += length;
if (!TOO_SMALL_LITTLE_BLOCK(length) && !UNALIGNED_X_Y(src, dst)) {
aligned_dst = (long*)dst;
aligned_src = (long*)src;
/* Copy one long word at a time if possible. */
while (!TOO_SMALL_LITTLE_BLOCK(length)) {
/*
* const long src0 = *--aligned_src;
* *--aligned_dst = src0;
* length -= LITTLE_BLOCK_SIZE;
*/
long src0;
/* DIG-694: need at least 2 instructions between lw and sw */
asm volatile("lw %0, -4(%1)\n" // src0 = *(aligned_src - 1);
"addi %1, %1, -4\n" // aligned_src--;
"addi %2, %2, -4\n" // aligned_dst--;
"addi %3, %3, -4\n" // length -= LITTLE_BLOCK_SIZE;
"sw %0, 0(%2)\n" // aligned_dst = src0;
: "=&r"(src0), "+r"(aligned_src), "+r"(aligned_dst), "+r"(length)
:: "memory");
}
/* Pick up any residual with a byte copier. */
dst = (char*)aligned_dst;
src = (char*)aligned_src;
}
while (length--) {
*--dst = *--src;
}
} else {
/* Use optimizing algorithm for a non-destructive copy to closely
match memcpy. If the size is small or either SRC or DST is unaligned,
then punt into the byte copy loop. This should be rare. */
if (!TOO_SMALL_LITTLE_BLOCK(length) && !UNALIGNED_X_Y(src, dst)) {
aligned_dst = (long*)dst;
aligned_src = (long*)src;
/* Copy 8X long words at a time if possible. */
while (length >= BIG_BLOCK_SIZE * 2) {
/*
* const long src0 = *aligned_src++;
* const long src1 = *aligned_src++;
* const long src2 = *aligned_src++;
* const long src3 = *aligned_src++;
* const long src4 = *aligned_src++;
* const long src5 = *aligned_src++;
* const long src6 = *aligned_src++;
* const long src7 = *aligned_src++;
* *aligned_dst++ = src0;
* *aligned_dst++ = src1;
* *aligned_dst++ = src2;
* *aligned_dst++ = src3;
* *aligned_dst++ = src4;
* *aligned_dst++ = src5;
* *aligned_dst++ = src6;
* *aligned_dst++ = src7;
*/
long src0, src1, src2, src3;
long src4, src5, src6, src7;
/* DIG-694: need at least 2 instructions between lw and sw */
asm volatile("lw %0, 0(%8)\n" // src0 = aligned_src[0];
"lw %1, 4(%8)\n" // src1 = aligned_src[1];
"lw %2, 8(%8)\n" // src2 = aligned_src[2];
"lw %3, 12(%8)\n" // src3 = aligned_src[3];
"lw %4, 16(%8)\n" // src4 = aligned_src[4];
"lw %5, 20(%8)\n" // src5 = aligned_src[5];
"lw %6, 24(%8)\n" // src6 = aligned_src[6];
"lw %7, 28(%8)\n" // src7 = aligned_src[7];
"addi %8, %8, 32\n" // aligned_src += BIG_BLOCK_SIZE * 2;
"addi %9, %9, 32\n" // aligned_dst += BIG_BLOCK_SIZE * 2;
"addi %10, %10, -32\n" // length -= BIG_BLOCK_SIZE * 2;
"sw %0, -32(%9)\n" // *(aligned_dst - 8) = src0;
"sw %1, -28(%9)\n" // *(aligned_dst - 7) = src1;
"sw %2, -24(%9)\n" // *(aligned_dst - 6) = src2;
"sw %3, -20(%9)\n" // *(aligned_dst - 5) = src3;
"sw %4, -16(%9)\n" // *(aligned_dst - 4) = src4;
"sw %5, -12(%9)\n" // *(aligned_dst - 3) = src5;
"sw %6, -8(%9)\n" // *(aligned_dst - 2) = src6;
"sw %7, -4(%9)\n" // *(aligned_dst - 1) = src7;
: "=r"(src0), "=r"(src1), "=r"(src2), "=r"(src3),
"=r"(src4), "=r"(src5), "=r"(src6), "=r"(src7),
"+r"(aligned_src), "+r"(aligned_dst), "+r"(length)
:: "memory");
}
/* Copy one long word at a time if possible. */
while (!TOO_SMALL_LITTLE_BLOCK(length)) {
/*
* const long src0 = *aligned_src++;
* *aligned_dst++ = src0;
* length -= LITTLE_BLOCK_SIZE;
*/
long src0;
/* DIG-694: need at least 2 instructions between lw and sw */
asm volatile("lw %0, 0(%1)\n" // long src0 = *aligned_src;
"addi %1, %1, 4\n" // aligned_src++;
"addi %2, %2, 4\n" // aligned_dst++;
"addi %3, %3, -4\n" // length -= LITTLE_BLOCK_SIZE;
"sw %0, -4(%2)\n" // *(aligned_dst-1) = src0;
: "=&r"(src0), "+r"(aligned_src), "+r"(aligned_dst), "+r"(length)
:: "memory");
}
/* Pick up any residual with a byte copier. */
dst = (char*)aligned_dst;
src = (char*)aligned_src;
}
while (length--) {
*dst++ = *src++;
}
}
return dst_void;
}
// Hook to force the linker to include this file
void esp_libc_include_memmove_impl(void)
{
}
+3 -1
View File
@@ -23,6 +23,7 @@ __attribute__((always_inline))
static inline
unsigned long __newlib__libc_detect_null(unsigned long w)
{
/* coverity[result_independent_of_operands] */
unsigned long mask = 0x7f7f7f7f;
if (sizeof(long) == 8) {
mask = ((mask << 16) << 16) | mask;
@@ -30,7 +31,6 @@ unsigned long __newlib__libc_detect_null(unsigned long w)
return ~(((w & mask) + mask) | w | mask);
}
__attribute__((optimize("-Os")))
char *strcpy(char *dst, const char *src)
{
char *dst0 = dst;
@@ -44,6 +44,7 @@ char *strcpy(char *dst, const char *src)
const long *lsrc = (const long *)src;
while (!__newlib__libc_detect_null(*lsrc)) {
/* DIG-694: there are enough instructions between lw and sw after compiler unrolls the loop */
*ldst++ = *lsrc++;
}
@@ -87,6 +88,7 @@ out:
return dst0;
}
/* coverity[unreachable] */
char ch;
do {
ch = *src;
+5 -1
View File
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <pthread.h>
#include <signal.h>
#include "esp_log.h"
const static char *TAG = "esp32_asio_pthread";
@@ -13,7 +14,7 @@ int pthread_setcancelstate(int state, int *oldstate)
return 0;
}
// This functions (pthread_sigmask(), sigfillset) are called from ASIO::signal_blocker to temporarily silence signals
// This functions (pthread_sigmask(), sigfillset) might be called from external libs to temporarily silence signals
// Since signals are not yet supported in ESP pthread these functions serve as no-ops
//
int pthread_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict oset)
@@ -22,6 +23,8 @@ int pthread_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict os
return 0;
}
// picolibc has sigfillset macro in signal.h
#if !CONFIG_LIBC_PICOLIBC
int sigfillset(sigset_t *what)
{
ESP_LOGD(TAG, "%s: Signals not supported in ESP pthread", __func__);
@@ -30,6 +33,7 @@ int sigfillset(sigset_t *what)
}
return 0;
}
#endif /* !CONFIG_LIBC_PICOLIBC */
void esp_libc_include_pthread_impl(void)
{
+1
View File
@@ -13,6 +13,7 @@
#include <errno.h>
#include <reent.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include "sdkconfig.h"
#include "esp_rom_uart.h"
#include "esp_system_console.h"
-62
View File
@@ -1,62 +0,0 @@
/*
* SPDX-FileCopyrightText: 1994-2009 Red Hat, Inc.
*
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND Apache-2.0
*
* SPDX-FileContributor: 2025 Espressif Systems (Shanghai) CO LTD
*/
#include <_ansi.h>
#include <limits.h>
/*
Taken from glibc:
Add the compiler optimization to inhibit loop transformation to library
calls. This is used to avoid recursive calls in memset and memmove
default implementations.
*/
# define __inhibit_loop_to_libcall \
__attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
/* Nonzero if X is not aligned on a "long" boundary.
* This macro is used to skip a few bytes to find an aligned pointer.
* It's better to keep it as is even if _HAVE_HW_MISALIGNED_ACCESS is enabled,
* to avoid small performance penalties (if they are not zero). */
#define UNALIGNED_X(X) ((long)X & (sizeof (long) - 1))
#define _HAVE_HW_MISALIGNED_ACCESS (__riscv_misaligned_fast || __riscv_misaligned_slow)
#if _HAVE_HW_MISALIGNED_ACCESS
/* Hardware performs unaligned operations with little
* to no penalty compared to byte-to-byte copy. */
#define UNALIGNED_X_Y(X, Y) (0)
#else /* _HAVE_HW_MISALIGNED_ACCESS */
/* Nonzero if either X or Y is not aligned on a "long" boundary. */
#define UNALIGNED_X_Y(X, Y) \
(((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
#endif /* _HAVE_HW_MISALIGNED_ACCESS */
/* How many bytes are copied each iteration of the word copy loop. */
#define LITTLE_BLOCK_SIZE (sizeof (long))
/* How many bytes are copied each iteration of the 4X unrolled loop. */
#define BIG_BLOCK_SIZE (sizeof (long) << 2)
/* Threshold for punting to the little block byte copier. */
#define TOO_SMALL_LITTLE_BLOCK(LEN) ((LEN) < LITTLE_BLOCK_SIZE)
/* Threshold for punting to the big block byte copier. */
#define TOO_SMALL_BIG_BLOCK(LEN) ((LEN) < BIG_BLOCK_SIZE)
/* Macros for detecting endchar. */
#if LONG_MAX == 2147483647L
#define DETECT_NULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
#else
#if LONG_MAX == 9223372036854775807L
/* Nonzero if X (a long int) contains a NULL byte. */
#define DETECT_NULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
#else
#error long int is not a 32bit or 64bit type.
#endif
#endif
/* Returns nonzero if (long)X contains the byte used to fill (long)MASK. */
#define DETECT_CHAR(X, MASK) (DETECT_NULL(X ^ MASK))
+1 -2
View File
@@ -6,9 +6,8 @@
* SPDX-FileContributor: 2025 Espressif Systems (Shanghai) CO LTD
*/
#include <string.h>
#include "local.h"
#include "string/local.h"
__attribute__((optimize("-Os")))
int
memcmp(const void *m1,
const void *m2,
-88
View File
@@ -1,88 +0,0 @@
/*
* SPDX-FileCopyrightText: 1994-2009 Red Hat, Inc.
*
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND Apache-2.0
*
* SPDX-FileContributor: 2025 Espressif Systems (Shanghai) CO LTD
*/
#include <string.h>
#include <_ansi.h>
#include <stddef.h>
#include <limits.h>
#include "local.h"
__attribute__((optimize("-Os")))
void *
__inhibit_loop_to_libcall
memmove(void *dst_void,
const void *src_void,
size_t length)
{
char *dst = dst_void;
const char *src = src_void;
long *aligned_dst;
const long *aligned_src;
if (src < dst && dst < src + length) {
/* Destructive overlap...have to copy backwards */
src += length;
dst += length;
if (!TOO_SMALL_LITTLE_BLOCK(length) && !UNALIGNED_X_Y(src, dst)) {
aligned_dst = (long*)dst;
aligned_src = (long*)src;
/* Copy one long word at a time if possible. */
while (!TOO_SMALL_LITTLE_BLOCK(length)) {
*--aligned_dst = *--aligned_src;
length -= LITTLE_BLOCK_SIZE;
}
/* Pick up any residual with a byte copier. */
dst = (char*)aligned_dst;
src = (char*)aligned_src;
}
while (length--) {
*--dst = *--src;
}
} else {
/* Use optimizing algorithm for a non-destructive copy to closely
match memcpy. If the size is small or either SRC or DST is unaligned,
then punt into the byte copy loop. This should be rare. */
if (!TOO_SMALL_LITTLE_BLOCK(length) && !UNALIGNED_X_Y(src, dst)) {
aligned_dst = (long*)dst;
aligned_src = (long*)src;
/* Copy 4X long words at a time if possible. */
while (!TOO_SMALL_BIG_BLOCK(length)) {
*aligned_dst++ = *aligned_src++;
*aligned_dst++ = *aligned_src++;
*aligned_dst++ = *aligned_src++;
*aligned_dst++ = *aligned_src++;
length -= BIG_BLOCK_SIZE;
}
/* Copy one long word at a time if possible. */
while (!TOO_SMALL_LITTLE_BLOCK(length)) {
*aligned_dst++ = *aligned_src++;
length -= LITTLE_BLOCK_SIZE;
}
/* Pick up any residual with a byte copier. */
dst = (char*)aligned_dst;
src = (char*)aligned_src;
}
while (length--) {
*dst++ = *src++;
}
}
return dst_void;
}
// Hook to force the linker to include this file
void esp_libc_include_memmove_impl(void)
{
}
+1 -2
View File
@@ -7,9 +7,8 @@
*/
#include <string.h>
#include <limits.h>
#include "local.h"
#include "string/local.h"
__attribute__((optimize("-Os")))
int
strncmp(const char *s1,
const char *s2,
+2 -2
View File
@@ -7,9 +7,8 @@
*/
#include <string.h>
#include <limits.h>
#include "local.h"
#include "string/local.h"
__attribute__((optimize("-Os")))
char *
strncpy(char *__restrict dst0,
const char *__restrict src0,
@@ -29,6 +28,7 @@ strncpy(char *__restrict dst0,
sized copies. */
while (!TOO_SMALL_LITTLE_BLOCK(count) && !DETECT_NULL(*aligned_src)) {
count -= sizeof(long int);
/* DIG-694: there are enough instructions between lw and sw after compiler unrolls the loop */
*aligned_dst++ = *aligned_src++;
}
+5 -1
View File
@@ -10,6 +10,7 @@
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "esp_attr.h"
#if CONFIG_LIBC_PICOLIBC
@@ -83,7 +84,7 @@ int rename(const char *src, const char *dst)
int isatty(int fd)
{
struct stat buf;
struct stat buf = {0};
if (_fstat_r(__getreent(), fd, &buf) < 0) {
return 0;
@@ -109,6 +110,7 @@ int getpid()
{
return _getpid_r(__getreent());
}
#endif // CONFIG_LIBC_PICOLIBC
void _exit(int __status)
@@ -121,6 +123,7 @@ int fstat(int fd, struct stat *st)
return _fstat_r(__getreent(), fd, st);
}
#if !CONFIG_LIBC_PICOLIBC
#if CONFIG_SPIRAM_CACHE_LIBMISC_IN_IRAM
IRAM_ATTR
#endif
@@ -128,6 +131,7 @@ int raise(int sig)
{
return _raise_r(__getreent(), sig);
}
#endif
#if CONFIG_SPIRAM_CACHE_LIBMISC_IN_IRAM
IRAM_ATTR