1# SPDX-License-Identifier: GPL-2.0
2all:
3
4include ../lib.mk
5
6.PHONY: all all_32 all_64 warn_32bit_failure clean
7
8TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt ptrace_syscall test_mremap_vdso \
9			check_initial_reg_state sigreturn ldt_gdt iopl mpx-mini-test ioperm \
10			protection_keys test_vdso test_vsyscall
11TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso unwind_vdso \
12			test_FCMOV test_FCOMI test_FISTTP \
13			vdso_restorer
14TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip 5lvl
15
16TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY)
17TARGETS_C_64BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_64BIT_ONLY)
18BINARIES_32 := $(TARGETS_C_32BIT_ALL:%=%_32)
19BINARIES_64 := $(TARGETS_C_64BIT_ALL:%=%_64)
20
21BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
22BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64))
23
24CFLAGS := -O2 -g -std=gnu99 -pthread -Wall -no-pie
25
26UNAME_M := $(shell uname -m)
27CAN_BUILD_I386 := $(shell ./check_cc.sh $(CC) trivial_32bit_program.c -m32)
28CAN_BUILD_X86_64 := $(shell ./check_cc.sh $(CC) trivial_64bit_program.c)
29
30define gen-target-rule-32
31$(1) $(1)_32: $(OUTPUT)/$(1)_32
32.PHONY: $(1) $(1)_32
33endef
34
35define gen-target-rule-64
36$(1) $(1)_64: $(OUTPUT)/$(1)_64
37.PHONY: $(1) $(1)_64
38endef
39
40ifeq ($(CAN_BUILD_I386),1)
41all: all_32
42TEST_PROGS += $(BINARIES_32)
43$(foreach t,$(TARGETS_C_32BIT_ALL),$(eval $(call gen-target-rule-32,$(t))))
44endif
45
46ifeq ($(CAN_BUILD_X86_64),1)
47all: all_64
48TEST_PROGS += $(BINARIES_64)
49$(foreach t,$(TARGETS_C_64BIT_ALL),$(eval $(call gen-target-rule-64,$(t))))
50endif
51
52all_32: $(BINARIES_32)
53
54all_64: $(BINARIES_64)
55
56EXTRA_CLEAN := $(BINARIES_32) $(BINARIES_64)
57
58$(BINARIES_32): $(OUTPUT)/%_32: %.c
59	$(CC) -m32 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $^ -lrt -ldl -lm
60
61$(BINARIES_64): $(OUTPUT)/%_64: %.c
62	$(CC) -m64 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $^ -lrt -ldl
63
64# x86_64 users should be encouraged to install 32-bit libraries
65ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),01)
66all: warn_32bit_failure
67
68warn_32bit_failure:
69	@echo "Warning: you seem to have a broken 32-bit build" 2>&1; 	\
70	echo "environment.  This will reduce test coverage of 64-bit" 2>&1; \
71	echo "kernels.  If you are using a Debian-like distribution," 2>&1; \
72	echo "try:"; 2>&1; \
73	echo "";							\
74	echo "  apt-get install gcc-multilib libc6-i386 libc6-dev-i386"; \
75	echo "";							\
76	echo "If you are using a Fedora-like distribution, try:";	\
77	echo "";							\
78	echo "  yum install glibc-devel.*i686";				\
79	exit 0;
80endif
81
82# Some tests have additional dependencies.
83$(OUTPUT)/sysret_ss_attrs_64: thunks.S
84$(OUTPUT)/ptrace_syscall_32: raw_syscall_helper_32.S
85$(OUTPUT)/test_syscall_vdso_32: thunks_32.S
86
87# check_initial_reg_state is special: it needs a custom entry, and it
88# needs to be static so that its interpreter doesn't destroy its initial
89# state.
90$(OUTPUT)/check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static
91$(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static
92