Re: [RFC PATCH v2 10/10] selftests/nolibc: Add option for building with -static-pie

From: Thomas Weißschuh

Date: Mon Feb 16 2026 - 15:59:34 EST


On 2026-02-04 21:45:42+0900, Daniel Palmer wrote:
> Allow the tests to build with -static-pie instead of -static to
> test the self relocation code works.
>
> Note: This doesn't work 100% correctly. For x86_64 I can see the
> test binary is static or static-pie depending on if -static or
> -static-pie is used but for m68k it's always static and I had
> to hack in the workaround.
>
> I have somehow got the tests to seem to produce static PIE
> binaries when requested, and regular static ones when not but
> its a mess and this can't be used as-is.
>
> Signed-off-by: Daniel Palmer <daniel@xxxxxxxxx>
> ---
> tools/testing/selftests/nolibc/Makefile | 2 +-
> tools/testing/selftests/nolibc/Makefile.nolibc | 8 ++++----
> tools/testing/selftests/nolibc/run-tests.sh | 10 +++++++++-
> 3 files changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile
> index 40f5c2908dda..5d5c3231cc19 100644
> --- a/tools/testing/selftests/nolibc/Makefile
> +++ b/tools/testing/selftests/nolibc/Makefile
> @@ -9,7 +9,7 @@ cc-option = $(call __cc-option, $(CC),,$(1),$(2))
>
> include Makefile.include
>
> -CFLAGS = -nostdlib -nostdinc -static \
> +CFLAGS = -nostdlib -nostdinc \
> -isystem $(top_srcdir)/tools/include/nolibc -isystem $(top_srcdir)/usr/include \
> $(CFLAGS_NOLIBC_TEST)

It seems the -static is not necessary, even today.
Maybe split this into its own commit with some explanation to make this
clearer.

>
> diff --git a/tools/testing/selftests/nolibc/Makefile.nolibc b/tools/testing/selftests/nolibc/Makefile.nolibc
> index f9d43cbdc894..c55f4c17e06d 100644
> --- a/tools/testing/selftests/nolibc/Makefile.nolibc
> +++ b/tools/testing/selftests/nolibc/Makefile.nolibc
> @@ -304,12 +304,12 @@ sysroot/$(ARCH)/include:
>
> ifneq ($(NOLIBC_SYSROOT),0)
> nolibc-test: nolibc-test.c nolibc-test-linkage.c sysroot/$(ARCH)/include
> - $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
> - -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c $(LIBGCC)
> + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
> + -nostdlib -nostdinc -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c $(LIBGCC)
> else
> nolibc-test: nolibc-test.c nolibc-test-linkage.c
> - $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
> - -nostdlib -static -include $(srctree)/tools/include/nolibc/nolibc.h nolibc-test.c nolibc-test-linkage.c $(LIBGCC)
> + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
> + -nostdlib -include $(srctree)/tools/include/nolibc/nolibc.h nolibc-test.c nolibc-test-linkage.c $(LIBGCC)
> endif
>
> libc-test: nolibc-test.c nolibc-test-linkage.c
> diff --git a/tools/testing/selftests/nolibc/run-tests.sh b/tools/testing/selftests/nolibc/run-tests.sh
> index 3917cfb8fdc4..12b4774a77d8 100755
> --- a/tools/testing/selftests/nolibc/run-tests.sh
> +++ b/tools/testing/selftests/nolibc/run-tests.sh
> @@ -16,6 +16,7 @@ build_location="$(realpath "${cache_dir}"/nolibc-tests/)"
> perform_download=0
> test_mode=system
> werror=1
> +staticpie=0
> llvm=
> all_archs=(
> i386 x86_64 x32
> @@ -31,7 +32,7 @@ all_archs=(
> )
> archs="${all_archs[@]}"
>
> -TEMP=$(getopt -o 'j:d:c:b:a:m:pelh' -n "$0" -- "$@")
> +TEMP=$(getopt -o 'j:d:c:b:a:m:peslh' -n "$0" -- "$@")
>
> eval set -- "$TEMP"
> unset TEMP
> @@ -55,6 +56,7 @@ Options:
> -b [DIR] Build location (default: ${build_location})
> -m [MODE] Test mode user/system (default: ${test_mode})
> -e Disable -Werror
> + -s Enable static PIE (default: ${staticpie})
> -l Build with LLVM/clang
> EOF
> }
> @@ -85,6 +87,9 @@ while true; do
> '-e')
> werror=0
> shift; continue ;;
> + '-s')
> + staticpie=1
> + shift; continue ;;
> '-l')
> llvm=1
> shift; continue ;;
> @@ -171,6 +176,9 @@ test_arch() {
> if [ "$werror" -ne 0 ]; then
> CFLAGS_EXTRA="$CFLAGS_EXTRA -Werror -Wl,--fatal-warnings"
> fi
> + if [ "$staticpie" -ne 0 ]; then
> + CFLAGS_EXTRA="$CFLAGS_EXTRA -ggdb -fPIE -pie -Wl,--no-dynamic-linker -Wl,-ztext -Wl,-zseparate-code"

The -ggdb shouldn't be necessary here, or?

> + fi
> MAKE=(make -f Makefile.nolibc -j"${nproc}" XARCH="${arch}" CROSS_COMPILE="${cross_compile}" LLVM="${llvm}" O="${build_dir}")
>
> case "$test_mode" in
> --
> 2.51.0
>