Skip to content

Commit c759b8a

Browse files
keith-packardnashif
authored andcommitted
libc/picolibc: Split hooks into separate files
This splits the picolibc helper functions into separate files instead of smashing them all together. Signed-off-by: Keith Packard <[email protected]>
1 parent e4b830f commit c759b8a

File tree

11 files changed

+317
-260
lines changed

11 files changed

+317
-260
lines changed

boards/qemu/x86/qemu_x86_tiny.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ MEMORY
144144
#endif /* CONFIG_NEWLIB_LIBC */
145145

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

152152
#endif /* CONFIG_PICOLIBC */
153153

lib/libc/picolibc/CMakeLists.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# SPDX-License-Identifier: Apache-2.0
22

33
zephyr_library()
4-
zephyr_library_sources(libc-hooks.c)
5-
6-
# Do not allow LTO when compiling libc-hooks.c file
7-
set_source_files_properties(libc-hooks.c PROPERTIES COMPILE_OPTIONS $<TARGET_PROPERTY:compiler,prohibit_lto>)
4+
zephyr_library_sources(
5+
assert.c
6+
cbprintf.c
7+
chk_fail.c
8+
errno_wrap.c
9+
exit.c
10+
locks.c
11+
stdio.c
12+
)
813

914
# define __LINUX_ERRNO_EXTENSIONS__ so we get errno defines like -ESHUTDOWN
1015
# used by the network stack

lib/libc/picolibc/assert.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright © 2021, Keith Packard <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "picolibc-hooks.h"
8+
9+
#ifdef CONFIG_PICOLIBC_ASSERT_VERBOSE
10+
11+
FUNC_NORETURN void __assert_func(const char *file, int line,
12+
const char *function, const char *expression)
13+
{
14+
__ASSERT(0, "assertion \"%s\" failed: file \"%s\", line %d%s%s\n",
15+
expression, file, line,
16+
function ? ", function: " : "", function ? function : "");
17+
CODE_UNREACHABLE;
18+
}
19+
20+
#else
21+
22+
FUNC_NORETURN void __assert_no_args(void)
23+
{
24+
__ASSERT_NO_MSG(0);
25+
CODE_UNREACHABLE;
26+
}
27+
28+
#endif

lib/libc/picolibc/cbprintf.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright © 2021, Keith Packard <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "picolibc-hooks.h"
8+
9+
struct cb_bits {
10+
FILE f;
11+
cbprintf_cb out;
12+
void *ctx;
13+
};
14+
15+
static int cbputc(char c, FILE *_s)
16+
{
17+
struct cb_bits *s = (struct cb_bits *) _s;
18+
19+
(*s->out) (c, s->ctx);
20+
return 0;
21+
}
22+
23+
int cbvprintf(cbprintf_cb out, void *ctx, const char *fp, va_list ap)
24+
{
25+
struct cb_bits s = {
26+
.f = FDEV_SETUP_STREAM(cbputc, NULL, NULL, _FDEV_SETUP_WRITE),
27+
.out = out,
28+
.ctx = ctx,
29+
};
30+
return vfprintf(&s.f, fp, ap);
31+
}

lib/libc/picolibc/chk_fail.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright © 2021, Keith Packard <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "picolibc-hooks.h"
8+
9+
/* This function gets called if static buffer overflow detection is enabled on
10+
* stdlib side (Picolibc here), in case such an overflow is detected. Picolibc
11+
* provides an implementation not suitable for us, so we override it here.
12+
*/
13+
__weak FUNC_NORETURN void __chk_fail(void)
14+
{
15+
printk("* buffer overflow detected *\n");
16+
z_except_reason(K_ERR_STACK_CHK_FAIL);
17+
CODE_UNREACHABLE;
18+
}

lib/libc/picolibc/errno_wrap.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright © 2021, Keith Packard <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "picolibc-hooks.h"
8+
9+
#ifndef CONFIG_LIBC_ERRNO
10+
11+
/*
12+
* Picolibc needs to be able to declare this itself so that the library
13+
* doesn't end up needing zephyr header files. That means using a regular
14+
* function instead of an inline.
15+
*/
16+
int *z_errno_wrap(void)
17+
{
18+
return z_errno();
19+
}
20+
21+
#endif

lib/libc/picolibc/exit.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright © 2021, Keith Packard <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "picolibc-hooks.h"
8+
9+
__weak void _exit(int status)
10+
{
11+
printf("exit\n");
12+
while (1) {
13+
Z_SPIN_DELAY(100);
14+
}
15+
}

0 commit comments

Comments
 (0)