Skip to contentSkip to conversation

tools/nolibc: assorted fixes and small updates

PATCH Superseded Willy Tarreau 3 participants 25 messages Jun 20, 2025 – Jun 29, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Willy Tarreau

author maintainer
#0 lore raw

cover tools/nolibc: assorted fixes and small updates

Trying to port a program to nolibc triggered a few trivial errors that are worth being addressed:
- inttypes.h is missing, while being generally recommended over stdint.h for being more portable. Here we add it, which simply includes stdint.h.

- sys/select.h is supposed to be where struct fd_set and FD_CLR() etc are defined. Till now it was still in types.h. Let's create the file and move these definitions there.

- memchr() was still missing, thus a trivial one was added.

Finally the help message from "make help" reported an inaccurate installation path, omitting the dependency on $OUTPUT. This was fixed as well.

Willy Tarreau (4):
tools/nolibc: fix misleading help message regarding installation path tools/nolibc: add the more portable inttypes.h tools/nolibc: move FD_* definitions to sys/select.h tools/nolibc: add missing memchr() to string.h

 tools/include/nolibc/Makefile                |  4 +-
 tools/include/nolibc/inttypes.h              |  8 +++
 tools/include/nolibc/string.h                | 15 ++++++
 tools/include/nolibc/sys/select.h            | 56 ++++++++++++++++++++
 tools/include/nolibc/types.h                 | 48 +----------------
 tools/testing/selftests/nolibc/nolibc-test.c |  2 +
 6 files changed, 85 insertions(+), 48 deletions(-)
 create mode 100644 tools/include/nolibc/inttypes.h
 create mode 100644 tools/include/nolibc/sys/select.h
Signature · 1 line
2.17.5
author maintainer
#1 lore raw

PATCH 1/4 tools/nolibc: fix misleading help message regarding installation path

The help message says the headers are going to be installed into tools/include/nolibc but this is only the default if $OUTPUT is not set, so better clarify this (the current value of $OUTPUT is already shown).

---
 tools/include/nolibc/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/nolibc/Makefile +1 -1
index c335ce0bd195c..f91d29705733a 100644
@@ -80,7 +80,7 @@ help:
 	@echo "Supported targets under nolibc:"
 	@echo "  all                 call \"headers\""
 	@echo "  clean               clean the sysroot"
-	@echo "  headers             prepare a sysroot in tools/include/nolibc/sysroot"
+	@echo "  headers             prepare a sysroot in \$${OUTPUT}sysroot"
 	@echo "  headers_standalone  like \"headers\", and also install kernel headers"
 	@echo "  help                this help"
 	@echo ""
Signature · 1 line
2.17.5
author maintainer
#2 lore raw

PATCH 2/4 tools/nolibc: add the more portable inttypes.h

In reply to Willy Tarreau

It's often recommended to only use inttypes.h instead of stdint.h for portability reasons since the former is always present when the latter is present, but not conversely, and the former includes the latter. Due to this some simple programs fail to build when including inttypes.h. Let's add one that simply includes stdint.h to better support these programs.

---
 tools/include/nolibc/Makefile   | 1 +
 tools/include/nolibc/inttypes.h | 8 ++++++++
 2 files changed, 9 insertions(+)
 create mode 100644 tools/include/nolibc/inttypes.h
tools/include/nolibc/Makefile +1 -0
index f91d29705733a..725cf49516185 100644
@@ -34,6 +34,7 @@ all_files := \
 		errno.h \
 		fcntl.h \
 		getopt.h \
+		inttypes.h \
 		limits.h \
 		math.h \
 		nolibc.h \
tools/include/nolibc/inttypes.h new file +8 -0
new file mode 100644
index 0000000000000..1088034ef7cca
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+
+#ifndef _NOLIBC_INTTYPES_H
+#define _NOLIBC_INTTYPES_H
+
+#include "stdint.h"
+
+#endif /* _NOLIBC_INTTYPES_H */
Signature · 1 line
2.17.5
author maintainer
#3 lore raw

PATCH 3/4 tools/nolibc: move FD_* definitions to sys/select.h

In reply to Willy Tarreau

Modern programs tend to include sys/select.h to get FD_SET() and FD_CLR() definitions as well as struct fd_set, but in our case it didn't exist. Let's move these definitions from types.h to sys/select.h to help port existing programs.

---
 tools/include/nolibc/Makefile     |  1 +
 tools/include/nolibc/sys/select.h | 56 +++++++++++++++++++++++++++++++
 tools/include/nolibc/types.h      | 48 +-------------------------
 3 files changed, 58 insertions(+), 47 deletions(-)
 create mode 100644 tools/include/nolibc/sys/select.h
tools/include/nolibc/Makefile +1 -0
index 725cf49516185..823bfa08c6756 100644
@@ -58,6 +58,7 @@ all_files := \
 		sys/random.h \
 		sys/reboot.h \
 		sys/resource.h \
+		sys/select.h \
 		sys/stat.h \
 		sys/syscall.h \
 		sys/sysmacros.h \
tools/include/nolibc/sys/select.h new file +56 -0

56 lines not on this page · raw message

tools/include/nolibc/types.h +1 -47

66 lines not on this page · raw message

Signature · 1 line
2.17.5
#4 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 6 lines · Willy Tarreau

Modern programs tend to include sys/select.h to get FD_SET() and FD_CLR() definitions as well as struct fd_set, but in our case it didn't exist. Let's move these definitions from types.h to sys/select.h to help port existing programs.

<snip>

tools/include/nolibc/types.h line 13

Quoted 9 lines
tools/include/nolibc/types.h +1 -0
index 16c6e9ec9451f..0b51ede4e0a9c 100644
@@ -10,6 +10,7 @@
 #ifndef _NOLIBC_TYPES_H
 #define _NOLIBC_TYPES_H
 
+#include "sys/select.h"

Is this really necessary?

Quoted 6 lines
tools/include/nolibc/types.h +0 -0
 #include "std.h"
 #include <linux/mman.h>
 #include <linux/stat.h>
@@ -70,11 +71,6 @@
 #define DT_LNK         0xa
 #define DT_SOCK        0xc

<snip>

author maintainer
#5 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Hi Thomas,

Quoted 18 lines · Thomas Weißschuh
Quoted 6 lines · Willy Tarreau

Modern programs tend to include sys/select.h to get FD_SET() and FD_CLR() definitions as well as struct fd_set, but in our case it didn't exist. Let's move these definitions from types.h to sys/select.h to help port existing programs.

<snip>

Quoted 8 lines
tools/include/nolibc/types.h +0 -0
index 16c6e9ec9451f..0b51ede4e0a9c 100644
@@ -10,6 +10,7 @@
 #ifndef _NOLIBC_TYPES_H
 #define _NOLIBC_TYPES_H
 
Quoted 3 lines

tools/include/nolibc/types.h line 13

Quoted 1 line
tools/include/nolibc/types.h +1 -0
+#include "sys/select.h"

Is this really necessary?

Not sure what you mean. Do you mean that you would have preferred it to be included from nolibc.h instead (which I'm equally fine with) or that you'd prefer to have an empty sys/select.h ?

Thanks,
Willy

#6 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 21 lines · Willy Tarreau

Hi Thomas,

Quoted 18 lines · Thomas Weißschuh
Quoted 6 lines · Willy Tarreau

Modern programs tend to include sys/select.h to get FD_SET() and FD_CLR() definitions as well as struct fd_set, but in our case it didn't exist. Let's move these definitions from types.h to sys/select.h to help port existing programs.

<snip>

Quoted 8 lines
tools/include/nolibc/types.h +0 -0
index 16c6e9ec9451f..0b51ede4e0a9c 100644
@@ -10,6 +10,7 @@
 #ifndef _NOLIBC_TYPES_H
 #define _NOLIBC_TYPES_H
 
Quoted 7 lines
Quoted 3 lines

tools/include/nolibc/types.h line 13

Quoted 1 line
tools/include/nolibc/types.h +1 -0
+#include "sys/select.h"

Is this really necessary?

Not sure what you mean. Do you mean that you would have preferred it to be included from nolibc.h instead (which I'm equally fine with) or that you'd prefer to have an empty sys/select.h ?

The former.

author maintainer
#7 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 26 lines · Thomas Weißschuh
Quoted 25 lines · Willy Tarreau

Hi Thomas,

Quoted 21 lines · Thomas Weißschuh
Quoted 6 lines · Willy Tarreau

Modern programs tend to include sys/select.h to get FD_SET() and FD_CLR() definitions as well as struct fd_set, but in our case it didn't exist. Let's move these definitions from types.h to sys/select.h to help port existing programs.

<snip>

tools/include/nolibc/types.h line 13

Quoted 9 lines
tools/include/nolibc/types.h +1 -0
index 16c6e9ec9451f..0b51ede4e0a9c 100644
@@ -10,6 +10,7 @@
 #ifndef _NOLIBC_TYPES_H
 #define _NOLIBC_TYPES_H
 
+#include "sys/select.h"

Is this really necessary?

Quoted 5 lines
Quoted 3 lines

Not sure what you mean. Do you mean that you would have preferred it to be included from nolibc.h instead (which I'm equally fine with) or that you'd prefer to have an empty sys/select.h ?

The former.

OK thanks, you're right, that's more consistent with the rest, I'll do that and push it.

Thanks!
Willy

author maintainer
#8 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 27 lines · Willy Tarreau
Quoted 26 lines · Thomas Weißschuh
Quoted 25 lines · Willy Tarreau

Hi Thomas,

Quoted 21 lines · Thomas Weißschuh
Quoted 6 lines · Willy Tarreau

Modern programs tend to include sys/select.h to get FD_SET() and FD_CLR() definitions as well as struct fd_set, but in our case it didn't exist. Let's move these definitions from types.h to sys/select.h to help port existing programs.

<snip>

tools/include/nolibc/types.h line 13

Quoted 9 lines
tools/include/nolibc/types.h +1 -0
index 16c6e9ec9451f..0b51ede4e0a9c 100644
@@ -10,6 +10,7 @@
 #ifndef _NOLIBC_TYPES_H
 #define _NOLIBC_TYPES_H
 
+#include "sys/select.h"

Is this really necessary?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Not sure what you mean. Do you mean that you would have preferred it to be included from nolibc.h instead (which I'm equally fine with) or that you'd prefer to have an empty sys/select.h ?

The former.

OK thanks, you're right, that's more consistent with the rest, I'll do that and push it.

Trying it has reopened the circular dependencies can of worms :-( It's the same problem as usual that we've worked around till now by placing some types in types.h, except that this time fd_set is defined based on the macros FD_* that I moved to sys/select.h.

I'm giving up on this one for now as I don't want us to revisit that painful dependencies sequence. In theory it should be as simple as guarding types and function definitions independently, but in reality it's never as rocket science as it can also pop up in macros and rare typedefs.

Instead I'll just provide a stub for sys/select.h just like for inttypes so that user code compiles without changing existing files.

Willy

#9 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 34 lines · Willy Tarreau
Quoted 33 lines · Willy Tarreau
Quoted 31 lines · Thomas Weißschuh
Quoted 21 lines · Willy Tarreau

Hi Thomas,

Quoted 18 lines · Thomas Weißschuh
Quoted 6 lines · Willy Tarreau

Modern programs tend to include sys/select.h to get FD_SET() and FD_CLR() definitions as well as struct fd_set, but in our case it didn't exist. Let's move these definitions from types.h to sys/select.h to help port existing programs.

<snip>

Quoted 8 lines
tools/include/nolibc/types.h +0 -0
index 16c6e9ec9451f..0b51ede4e0a9c 100644
@@ -10,6 +10,7 @@
 #ifndef _NOLIBC_TYPES_H
 #define _NOLIBC_TYPES_H
 
Quoted 7 lines
Quoted 3 lines

tools/include/nolibc/types.h line 13

Quoted 1 line
tools/include/nolibc/types.h +1 -0
+#include "sys/select.h"

Is this really necessary?

Not sure what you mean. Do you mean that you would have preferred it to be included from nolibc.h instead (which I'm equally fine with) or that you'd prefer to have an empty sys/select.h ?

The former.

Quoted 7 lines
Quoted 2 lines

OK thanks, you're right, that's more consistent with the rest, I'll do that and push it.

Trying it has reopened the circular dependencies can of worms :-( It's the same problem as usual that we've worked around till now by placing some types in types.h, except that this time fd_set is defined based on the macros FD_* that I moved to sys/select.h.

Can't fd_set also move to sys/select.h? This is how I read fd_set(3). Even if it is not standards compliant, for nolibc it won't matter as in the end everything is available anyaways.

Quoted 5 lines

I'm giving up on this one for now as I don't want us to revisit that painful dependencies sequence. In theory it should be as simple as guarding types and function definitions independently, but in reality it's never as rocket science as it can also pop up in macros and rare typedefs.

Also the headers are always included in the order written down in nolibc.h, so sys/select could be above types.h there.

Quoted 2 lines

Instead I'll just provide a stub for sys/select.h just like for inttypes so that user code compiles without changing existing files.

That works for me, too.

author maintainer
#10 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 38 lines · Thomas Weißschuh
Quoted 37 lines · Willy Tarreau
Quoted 35 lines · Willy Tarreau
Quoted 26 lines · Thomas Weißschuh
Quoted 25 lines · Willy Tarreau

Hi Thomas,

Quoted 21 lines · Thomas Weißschuh
Quoted 6 lines · Willy Tarreau

Modern programs tend to include sys/select.h to get FD_SET() and FD_CLR() definitions as well as struct fd_set, but in our case it didn't exist. Let's move these definitions from types.h to sys/select.h to help port existing programs.

<snip>

tools/include/nolibc/types.h line 13

Quoted 9 lines
tools/include/nolibc/types.h +1 -0
index 16c6e9ec9451f..0b51ede4e0a9c 100644
@@ -10,6 +10,7 @@
 #ifndef _NOLIBC_TYPES_H
 #define _NOLIBC_TYPES_H
 
+#include "sys/select.h"

Is this really necessary?

Quoted 5 lines
Quoted 3 lines

Not sure what you mean. Do you mean that you would have preferred it to be included from nolibc.h instead (which I'm equally fine with) or that you'd prefer to have an empty sys/select.h ?

The former.

OK thanks, you're right, that's more consistent with the rest, I'll do that and push it.

Quoted 6 lines
Quoted 4 lines

Trying it has reopened the circular dependencies can of worms :-( It's the same problem as usual that we've worked around till now by placing some types in types.h, except that this time fd_set is defined based on the macros FD_* that I moved to sys/select.h.

Can't fd_set also move to sys/select.h? This is how I read fd_set(3).

That was what I did and precisely what was causing the problem. We have sys.h defining select() with fd_set in it with sys/select not yet being included. I moved sys.h after all sys/* and it broke something else instead.

Quoted 2 lines

Even if it is not standards compliant, for nolibc it won't matter as in the end everything is available anyaways.

Yeah I totally agree with you on that!

Quoted 8 lines
Quoted 5 lines

I'm giving up on this one for now as I don't want us to revisit that painful dependencies sequence. In theory it should be as simple as guarding types and function definitions independently, but in reality it's never as rocket science as it can also pop up in macros and rare typedefs.

Also the headers are always included in the order written down in nolibc.h, so sys/select could be above types.h there.

In an ideal world we'd have all sys/* *before* anything else. But I do remember that we faced a few issues doing this. This can be refined though. I'm just careful because we have not been annoyed in a while, so I suspect we're close to something good and it's easy to cause more breakage than fixes by touching all of this.

Quoted 4 lines
Quoted 2 lines

Instead I'll just provide a stub for sys/select.h just like for inttypes so that user code compiles without changing existing files.

That works for me, too.

That's what I did in the end. We're just addressing annoying build issues and not providing new stuff that deserves a full reorganization!

Cheers,
Willy

#11 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 3 lines · Willy Tarreau
Quoted 2 lines · Thomas Weißschuh
Quoted 1 line · Willy Tarreau

On Mon, Jun 23, 2025 at 04:56:18AM +0200, Willy Tarreau wrote:

<snip>

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

Trying it has reopened the circular dependencies can of worms :-( It's the same problem as usual that we've worked around till now by placing some types in types.h, except that this time fd_set is defined based on the macros FD_* that I moved to sys/select.h.

Can't fd_set also move to sys/select.h? This is how I read fd_set(3).

That was what I did and precisely what was causing the problem. We have sys.h defining select() with fd_set in it with sys/select not yet being included. I moved sys.h after all sys/* and it broke something else instead.

Ah. Then move select() also into sys/select.h; where it belongs. :-)

<snip>

author maintainer
#12 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 12 lines · Thomas Weißschuh
Quoted 3 lines · Willy Tarreau
Quoted 2 lines · Thomas Weißschuh
Quoted 1 line · Willy Tarreau

On Mon, Jun 23, 2025 at 04:56:18AM +0200, Willy Tarreau wrote:

<snip>

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Trying it has reopened the circular dependencies can of worms :-( It's the same problem as usual that we've worked around till now by placing some types in types.h, except that this time fd_set is defined based on the macros FD_* that I moved to sys/select.h.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Can't fd_set also move to sys/select.h? This is how I read fd_set(3).

That was what I did and precisely what was causing the problem. We have sys.h defining select() with fd_set in it with sys/select not yet being included. I moved sys.h after all sys/* and it broke something else instead.

Ah. Then move select() also into sys/select.h; where it belongs. :-)

For an unknown reason I thought we avoided to move the syscall definitions there and only used sys/*, but I was apparently confused as we have exactly that in prctl or wait. I can give that one a try again.

Thanks,
Willy

author maintainer
#13 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 20 lines · Willy Tarreau
Quoted 19 lines · Thomas Weißschuh
Quoted 3 lines · Willy Tarreau
Quoted 2 lines · Thomas Weißschuh
Quoted 1 line · Willy Tarreau

On Mon, Jun 23, 2025 at 04:56:18AM +0200, Willy Tarreau wrote:

<snip>

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

Trying it has reopened the circular dependencies can of worms :-( It's the same problem as usual that we've worked around till now by placing some types in types.h, except that this time fd_set is defined based on the macros FD_* that I moved to sys/select.h.

Can't fd_set also move to sys/select.h? This is how I read fd_set(3).

That was what I did and precisely what was causing the problem. We have sys.h defining select() with fd_set in it with sys/select not yet being included. I moved sys.h after all sys/* and it broke something else instead.

Quoted 5 lines
Quoted 1 line

Ah. Then move select() also into sys/select.h; where it belongs. :-)

For an unknown reason I thought we avoided to move the syscall definitions there and only used sys/*, but I was apparently confused as we have exactly that in prctl or wait. I can give that one a try again.

So after one more hour on it, I'm admitting abandonning the battle. Adding the necessary includes there is causing "declared inside parameter" failures in random other totally unrelated stuff (e.g. in sys_getdents64() or msleep()). We'll have to really clear that circular includes mess again in a near future. For now I'll stay on the stub which works fine without affecting the rest.

Cheers,
Willy

#14 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 27 lines · Willy Tarreau
Quoted 20 lines · Willy Tarreau
Quoted 19 lines · Thomas Weißschuh
Quoted 3 lines · Willy Tarreau
Quoted 2 lines · Thomas Weißschuh
Quoted 1 line · Willy Tarreau

On Mon, Jun 23, 2025 at 04:56:18AM +0200, Willy Tarreau wrote:

<snip>

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

Trying it has reopened the circular dependencies can of worms :-( It's the same problem as usual that we've worked around till now by placing some types in types.h, except that this time fd_set is defined based on the macros FD_* that I moved to sys/select.h.

Can't fd_set also move to sys/select.h? This is how I read fd_set(3).

That was what I did and precisely what was causing the problem. We have sys.h defining select() with fd_set in it with sys/select not yet being included. I moved sys.h after all sys/* and it broke something else instead.

Quoted 5 lines
Quoted 1 line

Ah. Then move select() also into sys/select.h; where it belongs. :-)

For an unknown reason I thought we avoided to move the syscall definitions there and only used sys/*, but I was apparently confused as we have exactly that in prctl or wait. I can give that one a try again.

Quoted 6 lines

So after one more hour on it, I'm admitting abandonning the battle. Adding the necessary includes there is causing "declared inside parameter" failures in random other totally unrelated stuff (e.g. in sys_getdents64() or msleep()). We'll have to really clear that circular includes mess again in a near future. For now I'll stay on the stub which works fine without affecting the rest.

I saw the same issue, but only because of the changes to types.h. And these should not be necessary in the first place.

The below works nicely for me:

tools/include/nolibc/Makefile +1 -0
index 3fcee9fe4ece..125dbb2f1388 100644
@@ -56,6 +56,7 @@ all_files := \
 		sys/random.h \
 		sys/reboot.h \
 		sys/resource.h \
+		sys/select.h \
 		sys/stat.h \
 		sys/syscall.h \
 		sys/sysmacros.h \
tools/include/nolibc/nolibc.h +1 -0
index c199ade200c2..6dc2f2a6cbde 100644
@@ -104,6 +104,7 @@
 #include "sys/random.h"
 #include "sys/reboot.h"
 #include "sys/resource.h"
+#include "sys/select.h"
 #include "sys/stat.h"
 #include "sys/syscall.h"
 #include "sys/sysmacros.h"
tools/include/nolibc/sys.h +0 -47

53 lines not on this page · raw message

tools/include/nolibc/sys/select.h new file +109 -0

109 lines not on this page · raw message

tools/include/nolibc/types.h +0 -47

59 lines not on this page · raw message

author maintainer
#15 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 2 lines · Thomas Weißschuh

I saw the same issue, but only because of the changes to types.h. And these should not be necessary in the first place.

You mean fd_set definition ? It's solely a matter of includes ordering in fact, since it depends on FD_SETSIZE.

Quoted 1 line

The below works nicely for me:

OK. Do you prefer that we take that one or just a stub ? I'm personally fine with both.

Thanks!
Willy

#16 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 6 lines · Willy Tarreau
Quoted 2 lines · Thomas Weißschuh

I saw the same issue, but only because of the changes to types.h. And these should not be necessary in the first place.

You mean fd_set definition ? It's solely a matter of includes ordering in fact, since it depends on FD_SETSIZE.

No, I mean the '#include "sys/select.h"' in "types.h". That breaks the dependency order, as it pulls in all kinds of other stuff into the beginning of "types.h" which themselves depend on definitions of "types.h".

Quoted 4 lines
Quoted 1 line

The below works nicely for me:

OK. Do you prefer that we take that one or just a stub ? I'm personally fine with both.

I prefer moving the code.

author maintainer
#17 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 9 lines · Thomas Weißschuh
Quoted 6 lines · Willy Tarreau
Quoted 2 lines · Thomas Weißschuh

I saw the same issue, but only because of the changes to types.h. And these should not be necessary in the first place.

You mean fd_set definition ? It's solely a matter of includes ordering in fact, since it depends on FD_SETSIZE.

No, I mean the '#include "sys/select.h"' in "types.h".

I had already dropped it as well.

Quoted 3 lines

That breaks the dependency order, as it pulls in all kinds of other stuff into the beginning of "types.h" which themselves depend on definitions of "types.h".

It was not just this. I'm pretty sure that what unbroke it for you is keeping fd_set in types.h.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

The below works nicely for me:

OK. Do you prefer that we take that one or just a stub ? I'm personally fine with both.

I prefer moving the code.

OK. Do you want me to merge your patch or will you take care of it ?

Thanks,
Willy

#18 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 6 lines · Willy Tarreau
Quoted 5 lines · Thomas Weißschuh
Quoted 4 lines · Willy Tarreau
Quoted 2 lines · Thomas Weißschuh

I saw the same issue, but only because of the changes to types.h. And these should not be necessary in the first place.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

You mean fd_set definition ? It's solely a matter of includes ordering in fact, since it depends on FD_SETSIZE.

No, I mean the '#include "sys/select.h"' in "types.h".

I had already dropped it as well.

Hm, then I'm not sure where it commes from.

Quoted 6 lines
Quoted 3 lines

That breaks the dependency order, as it pulls in all kinds of other stuff into the beginning of "types.h" which themselves depend on definitions of "types.h".

It was not just this. I'm pretty sure that what unbroke it for you is keeping fd_set in types.h.

But I *did* move fd_set to sys/select.h.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

The below works nicely for me:

OK. Do you prefer that we take that one or just a stub ? I'm personally fine with both.

I prefer moving the code.

OK. Do you want me to merge your patch or will you take care of it ?

As you'll resubmit your series anyways, please pick up my proposal.

author maintainer
#19 lore raw

Re: [PATCH 3/4] tools/nolibc: move FD_* definitions to sys/select.h

Quoted 7 lines · Thomas Weißschuh
Quoted 6 lines · Willy Tarreau
Quoted 5 lines · Thomas Weißschuh
Quoted 4 lines · Willy Tarreau
Quoted 2 lines · Thomas Weißschuh

I saw the same issue, but only because of the changes to types.h. And these should not be necessary in the first place.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

You mean fd_set definition ? It's solely a matter of includes ordering in fact, since it depends on FD_SETSIZE.

No, I mean the '#include "sys/select.h"' in "types.h".

I had already dropped it as well.

Hm, then I'm not sure where it commes from.

I noticed that you included linux/unistd.h from sys/select.h, which I didn't have, maybe it plays a role as well.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

That breaks the dependency order, as it pulls in all kinds of other stuff into the beginning of "types.h" which themselves depend on definitions of "types.h".

It was not just this. I'm pretty sure that what unbroke it for you is keeping fd_set in types.h.

But I *did* move fd_set to sys/select.h.

Sorry then, I just didn't notice it while trying to comapre the differences.

Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

The below works nicely for me:

OK. Do you prefer that we take that one or just a stub ? I'm personally fine with both.

I prefer moving the code.

OK. Do you want me to merge your patch or will you take care of it ?

As you'll resubmit your series anyways, please pick up my proposal.

OK will do that. Thanks.
willy

author maintainer
#20 lore raw

PATCH 4/4 tools/nolibc: add missing memchr() to string.h

In reply to Willy Tarreau

Surprisingly we forgot to add this common one. It was added with a per-arch guard allowing to later implement it in arch-specific asm code like was done for a few other ones.

The test verifies that we don't search past the indicated length.

---
 tools/include/nolibc/string.h                | 15 +++++++++++++++
 tools/testing/selftests/nolibc/nolibc-test.c |  2 ++
 2 files changed, 17 insertions(+)
tools/include/nolibc/string.h +15 -0
index 163a17e7dd38b..4000926f44ac4 100644
@@ -93,6 +93,21 @@ void *memset(void *dst, int b, size_t len)
 }
 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMSET */
 
+#ifndef NOLIBC_ARCH_HAS_MEMCHR
+static __attribute__((unused))
+void *memchr(const void *s, int c, size_t len)
+{
+	char *p = (char *)s;
+
+	while (len--) {
+		if (*p == (char)c)
+			return p;
+		p++;
+	}
+	return NULL;
+}
+#endif /* #ifndef NOLIBC_ARCH_HAS_MEMCHR */
+
 static __attribute__((unused))
 char *strchr(const char *s, int c)
 {
tools/testing/selftests/nolibc/nolibc-test.c +2 -0
index dbe13000fb1ac..d832566265296 100644
@@ -1524,6 +1524,8 @@ int run_stdlib(int min, int max)
 		CASE_TEST(abs);                     EXPECT_EQ(1, abs(-10), 10); break;
 		CASE_TEST(abs_noop);                EXPECT_EQ(1, abs(10), 10); break;
 		CASE_TEST(difftime);                EXPECT_ZR(1, test_difftime()); break;
+		CASE_TEST(memchr_foobar6_o);        EXPECT_STREQ(1, memchr("foobar", 'o', 6), "oobar"); break;
+		CASE_TEST(memchr_foobar3_b);        EXPECT_STRZR(1, memchr("foobar", 'b', 3)); break;
 
 		case __LINE__:
 			return ret; /* must be last */
Signature · 1 line
2.17.5
#21 lore raw

Re: [PATCH 4/4] tools/nolibc: add missing memchr() to string.h

Quoted 17 lines · Willy Tarreau

Surprisingly we forgot to add this common one. It was added with a per-arch guard allowing to later implement it in arch-specific asm code like was done for a few other ones.

The test verifies that we don't search past the indicated length.

---
 tools/include/nolibc/string.h                | 15 +++++++++++++++
 tools/testing/selftests/nolibc/nolibc-test.c |  2 ++
 2 files changed, 17 insertions(+)
tools/include/nolibc/string.h +0 -0
index 163a17e7dd38b..4000926f44ac4 100644

tools/include/nolibc/string.h void *memset(void *dst, int b, size_t len) · line 96

Quoted 4 lines
tools/include/nolibc/string.h +1 -0
@@ -93,6 +93,21 @@ void *memset(void *dst, int b, size_t len)
 }
 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMSET */
 
+#ifndef NOLIBC_ARCH_HAS_MEMCHR

So far we only have added these guards when necessary, which they aren't here. Can we drop them?

tools/include/nolibc/string.h void *memset(void *dst, int b, size_t len) · line 100

Quoted 4 lines
tools/include/nolibc/string.h +4 -0
+static __attribute__((unused))
+void *memchr(const void *s, int c, size_t len)
+{
+	char *p = (char *)s;

The docs say that they are interpreted as "unsigned char". Also, can we keep the const?

Quoted 13 lines
tools/include/nolibc/string.h +10 -0
+
+	while (len--) {
+		if (*p == (char)c)
+			return p;
+		p++;
+	}
+	return NULL;
+}
+#endif /* #ifndef NOLIBC_ARCH_HAS_MEMCHR */
+
 static __attribute__((unused))
 char *strchr(const char *s, int c)
 {

<snip>

author maintainer
#22 lore raw

Re: [PATCH 4/4] tools/nolibc: add missing memchr() to string.h

Quoted 21 lines · Thomas Weißschuh
Quoted 20 lines · Willy Tarreau

Surprisingly we forgot to add this common one. It was added with a per-arch guard allowing to later implement it in arch-specific asm code like was done for a few other ones.

The test verifies that we don't search past the indicated length.

---
 tools/include/nolibc/string.h                | 15 +++++++++++++++
 tools/testing/selftests/nolibc/nolibc-test.c |  2 ++
 2 files changed, 17 insertions(+)
tools/include/nolibc/string.h +0 -0
index 163a17e7dd38b..4000926f44ac4 100644
@@ -93,6 +93,21 @@ void *memset(void *dst, int b, size_t len)
 }
 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMSET */
 
Quoted 4 lines

tools/include/nolibc/string.h void *memset(void *dst, int b, size_t len) · line 96

Quoted 1 line
tools/include/nolibc/string.h +1 -0
+#ifndef NOLIBC_ARCH_HAS_MEMCHR

So far we only have added these guards when necessary, which they aren't here. Can we drop them?

I intentionally placed them so that we can easily override them, as we did for the other ones on x86 where string operations are super short (repnz scasb is two bytes once you have the registers already loaded).

Quoted 6 lines

tools/include/nolibc/string.h void *memset(void *dst, int b, size_t len) · line 100

Quoted 4 lines
tools/include/nolibc/string.h +4 -0
+static __attribute__((unused))
+void *memchr(const void *s, int c, size_t len)
+{
+	char *p = (char *)s;

The docs say that they are interpreted as "unsigned char".

It does not change anything here, except adding an extra modifier (since we'll then also have to do it in the loop when comparing against c), thus IMHO it's extra noise.

Quoted 1 line

Also, can we keep the const?

It's memchr()'s definition which requires to return a void* so the const needs to be dropped somewhere. Here I found visually cleaner to have a single cast during the variable assignment rather than have a second one on the return statement. But it's a matter of taste. I tend to hate casts as they confuse the reader and remove the ability of the compiler to produce relevant warnings, so for me the less the better.

Thanks,
Willy

#23 lore raw

Re: [PATCH 4/4] tools/nolibc: add missing memchr() to string.h

Quoted 27 lines · Willy Tarreau
Quoted 21 lines · Thomas Weißschuh
Quoted 20 lines · Willy Tarreau

Surprisingly we forgot to add this common one. It was added with a per-arch guard allowing to later implement it in arch-specific asm code like was done for a few other ones.

The test verifies that we don't search past the indicated length.

---
 tools/include/nolibc/string.h                | 15 +++++++++++++++
 tools/testing/selftests/nolibc/nolibc-test.c |  2 ++
 2 files changed, 17 insertions(+)
tools/include/nolibc/string.h +0 -0
index 163a17e7dd38b..4000926f44ac4 100644
@@ -93,6 +93,21 @@ void *memset(void *dst, int b, size_t len)
 }
 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMSET */
 
Quoted 4 lines

tools/include/nolibc/string.h void *memset(void *dst, int b, size_t len) · line 96

Quoted 1 line
tools/include/nolibc/string.h +1 -0
+#ifndef NOLIBC_ARCH_HAS_MEMCHR

So far we only have added these guards when necessary, which they aren't here. Can we drop them?

Quoted 4 lines

I intentionally placed them so that we can easily override them, as we did for the other ones on x86 where string operations are super short (repnz scasb is two bytes once you have the registers already loaded).

Okay.

We do have different override mechanisms.
Both NOLIBC_ARCH_HAS_* and for example the mechanism for sys_fork. Not sure if it is worth aligning them.

Quoted 10 lines
Quoted 6 lines

tools/include/nolibc/string.h void *memset(void *dst, int b, size_t len) · line 100

Quoted 4 lines
tools/include/nolibc/string.h +4 -0
+static __attribute__((unused))
+void *memchr(const void *s, int c, size_t len)
+{
+	char *p = (char *)s;

The docs say that they are interpreted as "unsigned char".

It does not change anything here, except adding an extra modifier (since we'll then also have to do it in the loop when comparing against c), thus IMHO it's extra noise.

Fair enough.

Quoted 9 lines
Quoted 1 line

Also, can we keep the const?

It's memchr()'s definition which requires to return a void* so the const needs to be dropped somewhere. Here I found visually cleaner to have a single cast during the variable assignment rather than have a second one on the return statement. But it's a matter of taste. I tend to hate casts as they confuse the reader and remove the ability of the compiler to produce relevant warnings, so for me the less the better.

Ditto.

author maintainer
#24 lore raw

Re: [PATCH 4/4] tools/nolibc: add missing memchr() to string.h

On Sun, Jun 22, 2025 at 09:56:35PM +0200, Thomas Weißschuh wrote:
(...)

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
Quoted 1 line

+#ifndef NOLIBC_ARCH_HAS_MEMCHR

So far we only have added these guards when necessary, which they aren't here. Can we drop them?

I intentionally placed them so that we can easily override them, as we did for the other ones on x86 where string operations are super short (repnz scasb is two bytes once you have the registers already loaded).

Quoted 5 lines

Okay.

We do have different override mechanisms.
Both NOLIBC_ARCH_HAS_* and for example the mechanism for sys_fork. Not sure if it is worth aligning them.

I don't know either, because we're speaking about doing it with standard name functions (e.g. memchr), contrary to sys_* that we're bringing with nolibc. I think it requires a bit more thinking to be sure we're not going to cause trouble (e.g. with compiler builtin ones etc). At least whatever the outcome, I agree that trying to align all definitions using the same approach would be desirable, even if it means changing all of them.

Quoted 23 lines
Quoted 10 lines
Quoted 6 lines

tools/include/nolibc/string.h void *memset(void *dst, int b, size_t len) · line 100

Quoted 4 lines
tools/include/nolibc/string.h +4 -0
+static __attribute__((unused))
+void *memchr(const void *s, int c, size_t len)
+{
+	char *p = (char *)s;

The docs say that they are interpreted as "unsigned char".

It does not change anything here, except adding an extra modifier (since we'll then also have to do it in the loop when comparing against c), thus IMHO it's extra noise.

Fair enough.

Quoted 9 lines
Quoted 1 line

Also, can we keep the const?

It's memchr()'s definition which requires to return a void* so the const needs to be dropped somewhere. Here I found visually cleaner to have a single cast during the variable assignment rather than have a second one on the return statement. But it's a matter of taste. I tend to hate casts as they confuse the reader and remove the ability of the compiler to produce relevant warnings, so for me the less the better.

Quoted 1 line

Ditto.

OK, then I'll push it.

Thank you!
Willy