Skip to content

Commit

Permalink
libc/picolibc: Split hooks into separate files
Browse files Browse the repository at this point in the history
This splits the picolibc helper functions into separate files instead of
smashing them all together.

Signed-off-by: Keith Packard <[email protected]>
  • Loading branch information
keith-packard authored and nashif committed Nov 16, 2024
1 parent e4b830f commit c759b8a
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 260 deletions.
6 changes: 3 additions & 3 deletions boards/qemu/x86/qemu_x86_tiny.ld
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ MEMORY
#endif /* CONFIG_NEWLIB_LIBC */

#ifdef CONFIG_PICOLIBC
/* For Picolibc libc-hook.c. */
/* For Picolibc, all files under lib/libc/picolibc */
#define LIB_C_IN_SECT(lsect) \
*liblib__libc__picolibc.a:libc-hooks.c.obj(.##lsect) \
*liblib__libc__picolibc.a:libc-hooks.c.obj(.##lsect##.*)
*liblib__libc__picolibc.a:(.##lsect) \
*liblib__libc__picolibc.a:(.##lsect##.*)

#endif /* CONFIG_PICOLIBC */

Expand Down
13 changes: 9 additions & 4 deletions lib/libc/picolibc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# SPDX-License-Identifier: Apache-2.0

zephyr_library()
zephyr_library_sources(libc-hooks.c)

# Do not allow LTO when compiling libc-hooks.c file
set_source_files_properties(libc-hooks.c PROPERTIES COMPILE_OPTIONS $<TARGET_PROPERTY:compiler,prohibit_lto>)
zephyr_library_sources(
assert.c
cbprintf.c
chk_fail.c
errno_wrap.c
exit.c
locks.c
stdio.c
)

# define __LINUX_ERRNO_EXTENSIONS__ so we get errno defines like -ESHUTDOWN
# used by the network stack
Expand Down
28 changes: 28 additions & 0 deletions lib/libc/picolibc/assert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright © 2021, Keith Packard <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "picolibc-hooks.h"

#ifdef CONFIG_PICOLIBC_ASSERT_VERBOSE

FUNC_NORETURN void __assert_func(const char *file, int line,
const char *function, const char *expression)
{
__ASSERT(0, "assertion \"%s\" failed: file \"%s\", line %d%s%s\n",
expression, file, line,
function ? ", function: " : "", function ? function : "");
CODE_UNREACHABLE;
}

#else

FUNC_NORETURN void __assert_no_args(void)
{
__ASSERT_NO_MSG(0);
CODE_UNREACHABLE;
}

#endif
31 changes: 31 additions & 0 deletions lib/libc/picolibc/cbprintf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright © 2021, Keith Packard <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "picolibc-hooks.h"

struct cb_bits {
FILE f;
cbprintf_cb out;
void *ctx;
};

static int cbputc(char c, FILE *_s)
{
struct cb_bits *s = (struct cb_bits *) _s;

(*s->out) (c, s->ctx);
return 0;
}

int cbvprintf(cbprintf_cb out, void *ctx, const char *fp, va_list ap)
{
struct cb_bits s = {
.f = FDEV_SETUP_STREAM(cbputc, NULL, NULL, _FDEV_SETUP_WRITE),
.out = out,
.ctx = ctx,
};
return vfprintf(&s.f, fp, ap);
}
18 changes: 18 additions & 0 deletions lib/libc/picolibc/chk_fail.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright © 2021, Keith Packard <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "picolibc-hooks.h"

/* This function gets called if static buffer overflow detection is enabled on
* stdlib side (Picolibc here), in case such an overflow is detected. Picolibc
* provides an implementation not suitable for us, so we override it here.
*/
__weak FUNC_NORETURN void __chk_fail(void)
{
printk("* buffer overflow detected *\n");
z_except_reason(K_ERR_STACK_CHK_FAIL);
CODE_UNREACHABLE;
}
21 changes: 21 additions & 0 deletions lib/libc/picolibc/errno_wrap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright © 2021, Keith Packard <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "picolibc-hooks.h"

#ifndef CONFIG_LIBC_ERRNO

/*
* Picolibc needs to be able to declare this itself so that the library
* doesn't end up needing zephyr header files. That means using a regular
* function instead of an inline.
*/
int *z_errno_wrap(void)
{
return z_errno();
}

#endif
15 changes: 15 additions & 0 deletions lib/libc/picolibc/exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright © 2021, Keith Packard <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "picolibc-hooks.h"

__weak void _exit(int status)
{
printf("exit\n");
while (1) {
Z_SPIN_DELAY(100);
}
}
Loading

0 comments on commit c759b8a

Please sign in to comment.