tools/nolibc/stdlib: only reference the external environ when inlined
authorWilly Tarreau <w@1wt.eu>
Wed, 23 Mar 2022 07:18:07 +0000 (08:18 +0100)
committerPaul E. McKenney <paulmck@kernel.org>
Thu, 21 Apr 2022 00:05:46 +0000 (17:05 -0700)
commit2475d37ac30b8a850d3dd4fcbcb20895928b73fd
tree587979e0fcf8f611a1912d0393c64b4b741d0e39
parent96980b833a21c6dc29d0dfdc8f211fb8a10256a7
tools/nolibc/stdlib: only reference the external environ when inlined

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.

Reported-by: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Tested-by: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
tools/include/nolibc/stdlib.h