Skip to content

Commit b8c60e8

Browse files
committed
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>
1 parent 17362f3 commit b8c60e8

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

tools/testing/selftests/nolibc/Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ sysroot/$(ARCH)/include:
171171
$(Q)mv sysroot/sysroot sysroot/$(ARCH)
172172

173173
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
175175
$(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
177177
else
178-
nolibc-test: nolibc-test.c
178+
nolibc-test: nolibc-test.c nolibc-test-linkage.c
179179
$(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
181181
endif
182182

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
185185

186186
# local libc-test
187187
run-libc-test: libc-test
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 */

tools/testing/selftests/nolibc/nolibc-test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#endif
4242
#endif
4343

44+
#include "nolibc-test-linkage.h"
45+
4446
/* for the type of int_fast16_t and int_fast32_t, musl differs from glibc and nolibc */
4547
#define SINT_MAX_OF_TYPE(type) (((type)1 << (sizeof(type) * 8 - 2)) - (type)1 + ((type)1 << (sizeof(type) * 8 - 2)))
4648
#define SINT_MIN_OF_TYPE(type) (-SINT_MAX_OF_TYPE(type) - 1)
@@ -647,6 +649,8 @@ int run_startup(int min, int max)
647649
CASE_TEST(auxv_addr); EXPECT_PTRGT(test_auxv != (void *)-1, test_auxv, brk); break;
648650
CASE_TEST(auxv_AT_UID); EXPECT_EQ(1, getauxval(AT_UID), getuid()); break;
649651
CASE_TEST(constructor); EXPECT_EQ(1, constructor_test_value, 2); break;
652+
CASE_TEST(linkage_errno); EXPECT_PTREQ(1, linkage_test_errno_addr(), &errno); break;
653+
CASE_TEST(linkage_constr); EXPECT_EQ(1, linkage_test_constructor_test_value, 6); break;
650654
case __LINE__:
651655
return ret; /* must be last */
652656
/* note: do not set any defaults so as to permit holes above */

0 commit comments

Comments
 (0)