Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/nolibc: add tests for multi-object linkage

While uncommon, nolibc executables can be linked together from multiple
compilation units.
Add some tests to make sure everything works in that case.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Link: https://lore.kernel.org/lkml/20231012-nolibc-linkage-test-v1-1-315e682768b4@weissschuh.net/
Acked-by: Willy Tarreau <w@1wt.eu>

+45 -6
+6 -6
tools/testing/selftests/nolibc/Makefile
··· 171 171 $(Q)mv sysroot/sysroot sysroot/$(ARCH) 172 172 173 173 ifneq ($(NOLIBC_SYSROOT),0) 174 - nolibc-test: nolibc-test.c sysroot/$(ARCH)/include 174 + nolibc-test: nolibc-test.c nolibc-test-linkage.c sysroot/$(ARCH)/include 175 175 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 176 - -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include $< -lgcc 176 + -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c -lgcc 177 177 else 178 - nolibc-test: nolibc-test.c 178 + nolibc-test: nolibc-test.c nolibc-test-linkage.c 179 179 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 180 - -nostdlib -static -include ../../../include/nolibc/nolibc.h $< -lgcc 180 + -nostdlib -static -include ../../../include/nolibc/nolibc.h nolibc-test.c nolibc-test-linkage.c -lgcc 181 181 endif 182 182 183 - libc-test: nolibc-test.c 184 - $(QUIET_CC)$(HOSTCC) -o $@ $< 183 + libc-test: nolibc-test.c nolibc-test-linkage.c 184 + $(QUIET_CC)$(HOSTCC) -o $@ nolibc-test.c nolibc-test-linkage.c 185 185 186 186 # local libc-test 187 187 run-libc-test: libc-test
+26
tools/testing/selftests/nolibc/nolibc-test-linkage.c
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + 3 + #include "nolibc-test-linkage.h" 4 + 5 + #ifndef NOLIBC 6 + #include <errno.h> 7 + #endif 8 + 9 + void *linkage_test_errno_addr(void) 10 + { 11 + return &errno; 12 + } 13 + 14 + int linkage_test_constructor_test_value; 15 + 16 + __attribute__((constructor)) 17 + static void constructor1(void) 18 + { 19 + linkage_test_constructor_test_value = 2; 20 + } 21 + 22 + __attribute__((constructor)) 23 + static void constructor2(void) 24 + { 25 + linkage_test_constructor_test_value *= 3; 26 + }
+9
tools/testing/selftests/nolibc/nolibc-test-linkage.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + 3 + #ifndef _NOLIBC_TEST_LINKAGE_H 4 + #define _NOLIBC_TEST_LINKAGE_H 5 + 6 + void *linkage_test_errno_addr(void); 7 + extern int linkage_test_constructor_test_value; 8 + 9 + #endif /* _NOLIBC_TEST_LINKAGE_H */
+4
tools/testing/selftests/nolibc/nolibc-test.c
··· 41 41 #endif 42 42 #endif 43 43 44 + #include "nolibc-test-linkage.h" 45 + 44 46 /* for the type of int_fast16_t and int_fast32_t, musl differs from glibc and nolibc */ 45 47 #define SINT_MAX_OF_TYPE(type) (((type)1 << (sizeof(type) * 8 - 2)) - (type)1 + ((type)1 << (sizeof(type) * 8 - 2))) 46 48 #define SINT_MIN_OF_TYPE(type) (-SINT_MAX_OF_TYPE(type) - 1) ··· 649 647 CASE_TEST(auxv_addr); EXPECT_PTRGT(test_auxv != (void *)-1, test_auxv, brk); break; 650 648 CASE_TEST(auxv_AT_UID); EXPECT_EQ(1, getauxval(AT_UID), getuid()); break; 651 649 CASE_TEST(constructor); EXPECT_EQ(1, constructor_test_value, 2); break; 650 + CASE_TEST(linkage_errno); EXPECT_PTREQ(1, linkage_test_errno_addr(), &errno); break; 651 + CASE_TEST(linkage_constr); EXPECT_EQ(1, linkage_test_constructor_test_value, 6); break; 652 652 case __LINE__: 653 653 return ret; /* must be last */ 654 654 /* note: do not set any defaults so as to permit holes above */