Skip to contentSkip to conversation

nolibc: fix two build issues at -O0

PATCH In review Willy Tarreau 3 participants 5 messages Mar 23, 2022 Nolibc header file

lore
Share & tools
lore

Willy Tarreau

author maintainer
#0 lore raw

cover nolibc: fix two build issues at -O0

Hello Paul,

please find in this series two build fixes to apply on top of the series you've already queued:
- gcc errors at -O0 because it doesn't eliminate non-inline code and keeps the reference to the external "environ" from getenv()
- clang fails at -O0 because __builtin_strlen() calls strlen()

I don't care about optimizing at -O0 by definition but at least it should build and work correctly! I've verified that rcutorture still runs.

Thanks!
Willy

---
Willy Tarreau (2):
tools/nolibc/string: do not use __builtin_strlen() at -O0 tools/nolibc/stdlib: only reference the external environ when inlined

 tools/include/nolibc/stdlib.h | 22 +++++++++++++++-------
 tools/include/nolibc/string.h | 11 ++++++++++-
 2 files changed, 25 insertions(+), 8 deletions(-)
Signature · 1 line
2.35.1
author maintainer
#1 lore raw

PATCH 1/2 tools/nolibc/string: do not use __builtin_strlen() at -O0

clang wants to use strlen() for __builtin_strlen() at -O0. We don't really care about -O0 but it at least ought to build, so let's make sure we don't choke on this, by dropping the optimizationn for constant strings in this case.

---
 tools/include/nolibc/string.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
tools/include/nolibc/string.h +10 -1
index 0d5e870c7c0b..75a453870498 100644
@@ -122,7 +122,9 @@ char *strcpy(char *dst, const char *src)
 	return ret;
 }
 
-/* this function is only used with arguments that are not constants */
+/* this function is only used with arguments that are not constants or when
+ * it's not known because optimizations are disabled.
+ */
 static __attribute__((unused))
 size_t nolibc_strlen(const char *str)
 {
@@ -132,11 +134,18 @@ size_t nolibc_strlen(const char *str)
 	return len;
 }
 
+/* do not trust __builtin_constant_p() at -O0, as clang will emit a test and
+ * the two branches, then will rely on an external definition of strlen().
+ */
+#if defined(__OPTIMIZE__)
 #define strlen(str) ({                          \
 	__builtin_constant_p((str)) ?           \
 		__builtin_strlen((str)) :       \
 		nolibc_strlen((str));           \
 })
+#else
+#define strlen(str) nolibc_strlen((str))
+#endif
 
 static __attribute__((unused))
 size_t strlcat(char *dst, const char *src, size_t size)
Signature · 1 line
2.35.1
author maintainer
#2 lore raw

PATCH 2/2 tools/nolibc/stdlib: only reference the external environ when inlined

In reply to Willy Tarreau

When building with gcc at -O0 we're seeing link errors due to the "environ" variable being referenced by getenv(). The problem is that at -O0 gcc will not inline getenv() and will not drop the external reference. One solution would be to locally declare the variable as weak, but then it would appear in all programs even those not using it, and would be confusing to users of getenv() who would forget to set environ to envp.

An alternate approach used in this patch consists in always inlining the outer part of getenv() that references this extern so that it's always dropped when not used. The biggest part of the function was now moved to a new function called _getenv() that's still not inlined by default.

---
 tools/include/nolibc/stdlib.h | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
tools/include/nolibc/stdlib.h +15 -7
index aca8616335e3..8a07e263f0d0 100644
@@ -60,16 +60,17 @@ int atoi(const char *s)
 	return atol(s);
 }
 
-/* Tries to find the environment variable named <name> in the environment array
- * pointed to by global variable "environ" which must be declared as a char **,
- * and must be terminated by a NULL (it is recommended to set this variable to
- * the "envp" argument of main()). If the requested environment variable exists
- * its value is returned otherwise NULL is returned.
+/* getenv() tries to find the environment variable named <name> in the
+ * environment array pointed to by global variable "environ" which must be
+ * declared as a char **, and must be terminated by a NULL (it is recommended
+ * to set this variable to the "envp" argument of main()). If the requested
+ * environment variable exists its value is returned otherwise NULL is
+ * returned. getenv() is forcefully inlined so that the reference to "environ"
+ * will be dropped if unused, even at -O0.
  */
 static __attribute__((unused))
-char *getenv(const char *name)
+char *_getenv(const char *name, char **environ)
 {
-	extern char **environ;
 	int idx, i;
 
 	if (environ) {
@@ -83,6 +84,13 @@ char *getenv(const char *name)
 	return NULL;
 }
 
+static inline __attribute__((unused,always_inline))
+char *getenv(const char *name)
+{
+	extern char **environ;
+	return _getenv(name, environ);
+}
+
 /* Converts the unsigned long integer <in> to its hex representation into
  * buffer <buffer>, which must be long enough to store the number and the
  * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The
Signature · 1 line
2.35.1
Tested-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] tools/nolibc/stdlib: only reference the external environ when inlined

Quoted 14 lines · Willy Tarreau

When building with gcc at -O0 we're seeing link errors due to the "environ" variable being referenced by getenv(). The problem is that at -O0 gcc will not inline getenv() and will not drop the external reference. One solution would be to locally declare the variable as weak, but then it would appear in all programs even those not using it, and would be confusing to users of getenv() who would forget to set environ to envp.

An alternate approach used in this patch consists in always inlining the outer part of getenv() that references this extern so that it's always dropped when not used. The biggest part of the function was now moved to a new function called _getenv() that's still not inlined by default.

Quoted 3 lines

---

This one works nicely. I will resend my previous RFC after this one lands in Paul's tree to avoid conflict.

Thanks!

Signature · 1 line
Ammar Faizi
#4 lore raw
In reply to Willy Tarreau
Quoted 10 lines · Willy Tarreau

Hello Paul,

please find in this series two build fixes to apply on top of the series you've already queued:
- gcc errors at -O0 because it doesn't eliminate non-inline code and keeps the reference to the external "environ" from getenv()
- clang fails at -O0 because __builtin_strlen() calls strlen()

I don't care about optimizing at -O0 by definition but at least it should build and work correctly! I've verified that rcutorture still runs.

Queued with Ammar's Tested-by, thank you both!

Thanx, Paul

Quoted 15 lines

Thanks!
Willy

---
Willy Tarreau (2):
tools/nolibc/string: do not use __builtin_strlen() at -O0 tools/nolibc/stdlib: only reference the external environ when inlined

 tools/include/nolibc/stdlib.h | 22 +++++++++++++++-------
 tools/include/nolibc/string.h | 11 ++++++++++-
 2 files changed, 25 insertions(+), 8 deletions(-)
Signature · 1 line
2.35.1