Makefile 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # SPDX-License-Identifier: GPL-2.0
  2. #
  3. # The stub may be linked into the kernel proper or into a separate boot binary,
  4. # but in either case, it executes before the kernel does (with MMU disabled) so
  5. # things like ftrace and stack-protector are likely to cause trouble if left
  6. # enabled, even if doing so doesn't break the build.
  7. #
  8. # non-x86 reuses KBUILD_CFLAGS, x86 does not
  9. cflags-y := $(KBUILD_CFLAGS)
  10. cflags-$(CONFIG_X86_32) := -march=i386
  11. cflags-$(CONFIG_X86_64) := -mcmodel=small
  12. cflags-$(CONFIG_X86) += -m$(BITS) -D__KERNEL__ -std=gnu11 \
  13. -fPIC -fno-strict-aliasing -mno-red-zone \
  14. -mno-mmx -mno-sse -fshort-wchar \
  15. -Wno-pointer-sign \
  16. $(call cc-disable-warning, address-of-packed-member) \
  17. $(call cc-disable-warning, gnu) \
  18. -fno-asynchronous-unwind-tables \
  19. $(CLANG_FLAGS)
  20. # arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
  21. # disable the stackleak plugin
  22. cflags-$(CONFIG_ARM64) += -fpie $(DISABLE_STACKLEAK_PLUGIN) \
  23. -fno-unwind-tables -fno-asynchronous-unwind-tables
  24. cflags-$(CONFIG_ARM) += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
  25. -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
  26. -DEFI_HAVE_STRCMP -fno-builtin -fpic \
  27. $(call cc-option,-mno-single-pic-base) \
  28. $(DISABLE_STACKLEAK_PLUGIN)
  29. cflags-$(CONFIG_RISCV) += -fpic -DNO_ALTERNATIVE -mno-relax \
  30. $(DISABLE_STACKLEAK_PLUGIN)
  31. cflags-$(CONFIG_LOONGARCH) += -fpie
  32. cflags-$(CONFIG_EFI_PARAMS_FROM_FDT) += -I$(srctree)/scripts/dtc/libfdt
  33. KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \
  34. -Os -DDISABLE_BRANCH_PROFILING \
  35. -include $(srctree)/include/linux/hidden.h \
  36. -D__NO_FORTIFY \
  37. -ffreestanding \
  38. -fno-stack-protector \
  39. $(call cc-option,-fno-addrsig) \
  40. -D__DISABLE_EXPORTS
  41. #
  42. # struct randomization only makes sense for Linux internal types, which the EFI
  43. # stub code never touches, so let's turn off struct randomization for the stub
  44. # altogether
  45. #
  46. KBUILD_CFLAGS := $(filter-out $(RANDSTRUCT_CFLAGS), $(KBUILD_CFLAGS))
  47. # remove SCS flags from all objects in this directory
  48. KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
  49. # disable CFI
  50. KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_CFI), $(KBUILD_CFLAGS))
  51. # disable LTO
  52. KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))
  53. # The .data section would be renamed to .data.efistub, therefore, remove
  54. # `-fdata-sections` flag from KBUILD_CFLAGS_KERNEL
  55. KBUILD_CFLAGS_KERNEL := $(filter-out -fdata-sections, $(KBUILD_CFLAGS_KERNEL))
  56. lib-y := efi-stub-helper.o gop.o secureboot.o tpm.o \
  57. file.o mem.o random.o randomalloc.o pci.o \
  58. skip_spaces.o lib-cmdline.o lib-ctype.o \
  59. alignedmem.o relocate.o printk.o vsprintf.o
  60. # include the stub's libfdt dependencies from lib/ when needed
  61. libfdt-deps := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \
  62. fdt_empty_tree.c fdt_sw.c
  63. lib-$(CONFIG_EFI_PARAMS_FROM_FDT) += fdt.o \
  64. $(patsubst %.c,lib-%.o,$(libfdt-deps))
  65. $(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
  66. $(call if_changed_rule,cc_o_c)
  67. lib-$(CONFIG_EFI_GENERIC_STUB) += efi-stub.o string.o intrinsics.o systable.o \
  68. screen_info.o efi-stub-entry.o
  69. lib-$(CONFIG_ARM) += arm32-stub.o
  70. lib-$(CONFIG_ARM64) += kaslr.o arm64.o arm64-stub.o smbios.o
  71. lib-$(CONFIG_X86) += x86-stub.o smbios.o
  72. lib-$(CONFIG_X86_64) += x86-5lvl.o
  73. lib-$(CONFIG_RISCV) += kaslr.o riscv.o riscv-stub.o
  74. lib-$(CONFIG_LOONGARCH) += loongarch.o loongarch-stub.o
  75. CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET)
  76. zboot-obj-$(CONFIG_RISCV) := lib-clz_ctz.o lib-ashldi3.o
  77. lib-$(CONFIG_EFI_ZBOOT) += zboot.o $(zboot-obj-y)
  78. lib-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o bitmap.o find.o
  79. extra-y := $(lib-y)
  80. lib-y := $(patsubst %.o,%.stub.o,$(lib-y))
  81. # Even when -mbranch-protection=none is set, Clang will generate a
  82. # .note.gnu.property for code-less object files (like lib/ctype.c),
  83. # so work around this by explicitly removing the unwanted section.
  84. # https://llvm.org/pr46480
  85. STUBCOPY_FLAGS-y += --remove-section=.note.gnu.property
  86. STUBCOPY_RELOC-$(CONFIG_X86_32) := R_386_32
  87. STUBCOPY_RELOC-$(CONFIG_X86_64) := R_X86_64_64
  88. #
  89. # ARM discards the .data section because it disallows r/w data in the
  90. # decompressor. So move our .data to .data.efistub and .bss to .bss.efistub,
  91. # which are preserved explicitly by the decompressor linker script.
  92. #
  93. STUBCOPY_FLAGS-$(CONFIG_ARM) += --rename-section .data=.data.efistub \
  94. --rename-section .bss=.bss.efistub,load,alloc
  95. STUBCOPY_RELOC-$(CONFIG_ARM) := R_ARM_ABS
  96. #
  97. # arm64 puts the stub in the kernel proper, which will unnecessarily retain all
  98. # code indefinitely unless it is annotated as __init/__initdata/__initconst etc.
  99. # So let's apply the __init annotations at the section level, by prefixing
  100. # the section names directly. This will ensure that even all the inline string
  101. # literals are covered.
  102. # The fact that the stub and the kernel proper are essentially the same binary
  103. # also means that we need to be extra careful to make sure that the stub does
  104. # not rely on any absolute symbol references, considering that the virtual
  105. # kernel mapping that the linker uses is not active yet when the stub is
  106. # executing. So build all C dependencies of the EFI stub into libstub, and do
  107. # a verification pass to see if any absolute relocations exist in any of the
  108. # object files.
  109. #
  110. STUBCOPY_FLAGS-$(CONFIG_ARM64) += --prefix-alloc-sections=.init \
  111. --prefix-symbols=__efistub_
  112. STUBCOPY_RELOC-$(CONFIG_ARM64) := R_AARCH64_ABS
  113. # For RISC-V, we don't need anything special other than arm64. Keep all the
  114. # symbols in .init section and make sure that no absolute symbols references
  115. # exist.
  116. STUBCOPY_FLAGS-$(CONFIG_RISCV) += --prefix-alloc-sections=.init \
  117. --prefix-symbols=__efistub_
  118. STUBCOPY_RELOC-$(CONFIG_RISCV) := -E R_RISCV_HI20\|R_RISCV_$(BITS)\|R_RISCV_RELAX
  119. # For LoongArch, keep all the symbols in .init section and make sure that no
  120. # absolute symbols references exist.
  121. STUBCOPY_FLAGS-$(CONFIG_LOONGARCH) += --prefix-alloc-sections=.init \
  122. --prefix-symbols=__efistub_
  123. STUBCOPY_RELOC-$(CONFIG_LOONGARCH) := R_LARCH_MARK_LA
  124. $(obj)/%.stub.o: $(obj)/%.o FORCE
  125. $(call if_changed,stubcopy)
  126. #
  127. # Strip debug sections and some other sections that may legally contain
  128. # absolute relocations, so that we can inspect the remaining sections for
  129. # such relocations. If none are found, regenerate the output object, but
  130. # this time, use objcopy and leave all sections in place.
  131. #
  132. quiet_cmd_stubcopy = STUBCPY $@
  133. cmd_stubcopy = \
  134. $(STRIP) --strip-debug -o $@ $<; \
  135. if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); then \
  136. echo "$@: absolute symbol references not allowed in the EFI stub" >&2; \
  137. /bin/false; \
  138. fi; \
  139. $(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@