Makefile 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # SPDX-License-Identifier: GPL-2.0
  2. all:
  3. include ../lib.mk
  4. .PHONY: all all_32 all_64 warn_32bit_failure clean
  5. UNAME_M := $(shell uname -m)
  6. CAN_BUILD_I386 := $(shell ./check_cc.sh "$(CC)" trivial_32bit_program.c -m32)
  7. CAN_BUILD_X86_64 := $(shell ./check_cc.sh "$(CC)" trivial_64bit_program.c)
  8. CAN_BUILD_WITH_NOPIE := $(shell ./check_cc.sh "$(CC)" trivial_program.c -no-pie)
  9. TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt test_mremap_vdso \
  10. check_initial_reg_state sigreturn iopl ioperm \
  11. test_vsyscall mov_ss_trap sigtrap_loop \
  12. syscall_arg_fault fsgsbase_restore sigaltstack
  13. TARGETS_C_BOTHBITS += nx_stack
  14. TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
  15. test_FCMOV test_FCOMI test_FISTTP \
  16. vdso_restorer
  17. TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
  18. corrupt_xstate_header amx lam test_shadow_stack
  19. # Some selftests require 32bit support enabled also on 64bit systems
  20. TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
  21. TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY) $(TARGETS_C_32BIT_NEEDED)
  22. TARGETS_C_64BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_64BIT_ONLY)
  23. ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),11)
  24. TARGETS_C_64BIT_ALL += $(TARGETS_C_32BIT_NEEDED)
  25. endif
  26. BINARIES_32 := $(TARGETS_C_32BIT_ALL:%=%_32)
  27. BINARIES_64 := $(TARGETS_C_64BIT_ALL:%=%_64)
  28. BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
  29. BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64))
  30. CFLAGS := -O2 -g -std=gnu99 -pthread -Wall $(KHDR_INCLUDES)
  31. # call32_from_64 in thunks.S uses absolute addresses.
  32. ifeq ($(CAN_BUILD_WITH_NOPIE),1)
  33. CFLAGS += -no-pie
  34. ifneq ($(LLVM),)
  35. # clang only wants to see -no-pie during linking. Here, we don't have a separate
  36. # linking stage, so a compiler warning is unavoidable without (wastefully)
  37. # restructuring the Makefile. Avoid this by simply disabling that warning.
  38. CFLAGS += -Wno-unused-command-line-argument
  39. endif
  40. endif
  41. define gen-target-rule-32
  42. $(1) $(1)_32: $(OUTPUT)/$(1)_32
  43. .PHONY: $(1) $(1)_32
  44. endef
  45. define gen-target-rule-64
  46. $(1) $(1)_64: $(OUTPUT)/$(1)_64
  47. .PHONY: $(1) $(1)_64
  48. endef
  49. ifeq ($(CAN_BUILD_I386),1)
  50. all: all_32
  51. TEST_PROGS += $(BINARIES_32)
  52. EXTRA_CFLAGS += -DCAN_BUILD_32
  53. $(foreach t,$(TARGETS_C_32BIT_ALL),$(eval $(call gen-target-rule-32,$(t))))
  54. endif
  55. ifeq ($(CAN_BUILD_X86_64),1)
  56. all: all_64
  57. TEST_PROGS += $(BINARIES_64)
  58. EXTRA_CFLAGS += -DCAN_BUILD_64
  59. $(foreach t,$(TARGETS_C_64BIT_ALL),$(eval $(call gen-target-rule-64,$(t))))
  60. endif
  61. all_32: $(BINARIES_32)
  62. all_64: $(BINARIES_64)
  63. EXTRA_CLEAN := $(BINARIES_32) $(BINARIES_64) srso
  64. $(BINARIES_32): $(OUTPUT)/%_32: %.c helpers.h
  65. $(CC) -m32 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $< $(EXTRA_FILES) -lrt -ldl -lm
  66. $(BINARIES_64): $(OUTPUT)/%_64: %.c helpers.h
  67. $(CC) -m64 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $< $(EXTRA_FILES) -lrt -ldl
  68. # x86_64 users should be encouraged to install 32-bit libraries
  69. ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),01)
  70. all: warn_32bit_failure
  71. warn_32bit_failure:
  72. @echo "Warning: you seem to have a broken 32-bit build" 2>&1; \
  73. echo "environment. This will reduce test coverage of 64-bit" 2>&1; \
  74. echo "kernels. If you are using a Debian-like distribution," 2>&1; \
  75. echo "try:"; 2>&1; \
  76. echo ""; \
  77. echo " apt-get install gcc-multilib libc6-i386 libc6-dev-i386"; \
  78. echo ""; \
  79. echo "If you are using a Fedora-like distribution, try:"; \
  80. echo ""; \
  81. echo " yum install glibc-devel.*i686"; \
  82. echo ""; \
  83. echo "If you are using a SUSE-like distribution, try:"; \
  84. echo ""; \
  85. echo " zypper install gcc-32bit glibc-devel-static-32bit"; \
  86. exit 0;
  87. endif
  88. # Add an additional file to the source file list for a given target, and also
  89. # add a Makefile dependency on that same file. However, do these separately, so
  90. # that the compiler invocation ("$(CC) file1.c file2.S") is not combined with
  91. # the dependencies ("header3.h"), because clang, unlike gcc, will not accept
  92. # header files as an input to the compiler invocation.
  93. define extra-files
  94. $(OUTPUT)/$(1): EXTRA_FILES := $(2)
  95. $(OUTPUT)/$(1): $(2)
  96. endef
  97. $(eval $(call extra-files,sysret_ss_attrs_64,thunks.S))
  98. $(eval $(call extra-files,ptrace_syscall_32,raw_syscall_helper_32.S))
  99. $(eval $(call extra-files,test_syscall_vdso_32,thunks_32.S))
  100. $(eval $(call extra-files,fsgsbase_restore_64,clang_helpers_64.S))
  101. $(eval $(call extra-files,fsgsbase_restore_32,clang_helpers_32.S))
  102. $(eval $(call extra-files,sysret_rip_64,clang_helpers_64.S))
  103. # check_initial_reg_state is special: it needs a custom entry, and it
  104. # needs to be static so that its interpreter doesn't destroy its initial
  105. # state.
  106. $(OUTPUT)/check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static
  107. $(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static
  108. $(OUTPUT)/nx_stack_32: CFLAGS += -Wl,-z,noexecstack
  109. $(OUTPUT)/nx_stack_64: CFLAGS += -Wl,-z,noexecstack