Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libc/picolibc: Split hooks into separate files #81105

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed anymore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I inherited this piece from newlib/CMakeLists.txt and I have no idea what it is supposed to do. There's certainly no code in this library which needs to be protected from LTO; usually that's used to avoid letting the compiler elide calls to things like memset when storage is freed or other similar operations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. From the commit message it kind of seemed like it may have been an oversight to remove this :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I hadn't remembered that part of the change as I did this a long time ago. Good to review and make sure nothing got missed.

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 : "");

Check notice on line 16 in lib/libc/picolibc/assert.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

lib/libc/picolibc/assert.c:16 -FUNC_NORETURN void __assert_func(const char *file, int line, - const char *function, const char *expression) +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, + __ASSERT(0, "assertion \"%s\" failed: file \"%s\", line %d%s%s\n", expression, file, line,
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),

Check notice on line 26 in lib/libc/picolibc/cbprintf.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

lib/libc/picolibc/cbprintf.c:26 - void *ctx; + void *ctx; }; static int cbputc(char c, FILE *_s) { - struct cb_bits *s = (struct cb_bits *) _s; + struct cb_bits *s = (struct cb_bits *)_s; - (*s->out) (c, s->ctx); + (*s->out)(c, s->ctx); return 0; } int cbvprintf(cbprintf_cb out, void *ctx, const char *fp, va_list ap) { - struct cb_bits s = { + struct cb_bits s = {
.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
Loading