Kbuild 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Linux kernel-native Makefile ("Kbuild") for libwolfssl.ko
  2. #
  3. # Copyright (C) 2006-2021 wolfSSL Inc.
  4. #
  5. # This file is part of wolfSSL.
  6. #
  7. # wolfSSL is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # wolfSSL is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. #
  21. SHELL=/bin/bash
  22. ifeq "$(WOLFSSL_OBJ_FILES)" ""
  23. $(error $$WOLFSSL_OBJ_FILES is unset.)
  24. endif
  25. ifeq "$(WOLFSSL_CFLAGS)" ""
  26. $(error $$WOLFSSL_CFLAGS is unset.)
  27. endif
  28. WOLFSSL_CFLAGS += -ffreestanding -Wframe-larger-than=$(MAX_STACK_FRAME_SIZE)
  29. ifeq "$(KERNEL_ARCH)" "x86"
  30. WOLFSSL_CFLAGS += -mpreferred-stack-boundary=4
  31. endif
  32. obj-m := libwolfssl.o
  33. WOLFSSL_OBJ_TARGETS := $(patsubst %, $(obj)/%, $(WOLFSSL_OBJ_FILES))
  34. ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
  35. WOLFCRYPT_PIE_FILES := $(patsubst %, $(obj)/%, $(WOLFCRYPT_PIE_FILES))
  36. endif
  37. $(obj)/linuxkm/module_exports.o: $(WOLFSSL_OBJ_TARGETS)
  38. # this mechanism only works in kernel 5.x+ (fallback to hardcoded value)
  39. hostprogs := linuxkm/get_thread_size
  40. always-y := $(hostprogs)
  41. always := $(hostprogs)
  42. HOST_EXTRACFLAGS += $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(KBUILD_CFLAGS) -static
  43. # this rule is needed to get build to succeed in 4.x (get_thread_size still doesn't get built)
  44. $(obj)/linuxkm/get_thread_size: $(src)/linuxkm/get_thread_size.c
  45. $(WOLFSSL_OBJ_TARGETS): | $(obj)/linuxkm/get_thread_size
  46. KERNEL_THREAD_STACK_SIZE=$(shell test -x $(obj)/linuxkm/get_thread_size && $(obj)/linuxkm/get_thread_size || echo 16384)
  47. MAX_STACK_FRAME_SIZE=$(shell echo $$(( $(KERNEL_THREAD_STACK_SIZE) / 4)))
  48. libwolfssl-y := $(WOLFSSL_OBJ_FILES) linuxkm/module_hooks.o linuxkm/module_exports.o
  49. WOLFSSL_CFLAGS_NO_VECTOR_INSNS := $(CFLAGS_SIMD_DISABLE) $(CFLAGS_FPU_DISABLE)
  50. ifeq "$(ENABLED_ASM)" "yes"
  51. WOLFSSL_CFLAGS_YES_VECTOR_INSNS := $(CFLAGS_SIMD_ENABLE) $(CFLAGS_FPU_DISABLE) $(CFLAGS_AUTO_VECTORIZE_DISABLE)
  52. else
  53. WOLFSSL_CFLAGS_YES_VECTOR_INSNS := $(WOLFSSL_CFLAGS_NO_VECTOR_INSNS)
  54. endif
  55. ccflags-y := $(WOLFSSL_CFLAGS) $(WOLFSSL_CFLAGS_NO_VECTOR_INSNS)
  56. $(obj)/libwolfssl.mod.o: ccflags-y :=
  57. $(obj)/wolfcrypt/test/test.o: ccflags-y += -DNO_MAIN_DRIVER
  58. $(obj)/wolfcrypt/src/aes.o: ccflags-y = $(WOLFSSL_CFLAGS) $(WOLFSSL_CFLAGS_YES_VECTOR_INSNS)
  59. ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
  60. PIE_FLAGS := -fPIE -fno-stack-protector -fno-toplevel-reorder
  61. PIE_SUPPORT_FLAGS := -DUSE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
  62. ifeq "$(KERNEL_ARCH)" "x86"
  63. PIE_FLAGS += -mcmodel=small -mindirect-branch=keep -mfunction-return=keep
  64. endif
  65. ifeq "$(KERNEL_ARCH)" "mips"
  66. PIE_FLAGS += -mabicalls
  67. endif
  68. $(WOLFCRYPT_PIE_FILES): ccflags-y += $(PIE_SUPPORT_FLAGS) $(PIE_FLAGS)
  69. $(WOLFCRYPT_PIE_FILES): ccflags-remove-y += -pg
  70. # disabling retpoline generation leads to profuse warnings without this:
  71. $(WOLFCRYPT_PIE_FILES): OBJECT_FILES_NON_STANDARD := y
  72. $(obj)/linuxkm/module_hooks.o: ccflags-y += $(PIE_SUPPORT_FLAGS)
  73. endif
  74. asflags-y := $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPUSIMD_DISABLE)
  75. # vectorized algorithms protected by {SAVE,RESTORE}_VECTOR_REGISTERS() can be
  76. # safely included here, though many of these are not yet kernel-compatible:
  77. $(obj)/wolfcrypt/src/aes_asm.o: asflags-y = $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPU_DISABLE_SIMD_ENABLE)
  78. $(obj)/wolfcrypt/src/aes_gcm_asm.o: asflags-y = $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPU_DISABLE_SIMD_ENABLE)
  79. $(obj)/wolfcrypt/src/sha256_asm.o: asflags-y = $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPU_DISABLE_SIMD_ENABLE)
  80. $(obj)/wolfcrypt/src/sp_x86_64_asm.o: asflags-y = $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPU_DISABLE_SIMD_ENABLE)
  81. $(obj)/wolfcrypt/src/sha512_asm.o: asflags-y = $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPU_DISABLE_SIMD_ENABLE)
  82. $(obj)/wolfcrypt/src/poly1305_asm.o: asflags-y = $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPU_DISABLE_SIMD_ENABLE)
  83. $(obj)/wolfcrypt/src/chacha_asm.o: asflags-y = $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPU_DISABLE_SIMD_ENABLE)
  84. # these _asms are known kernel-compatible, but they still irritate objtool:
  85. $(obj)/wolfcrypt/src/aes_asm.o: OBJECT_FILES_NON_STANDARD := y
  86. $(obj)/wolfcrypt/src/aes_gcm_asm.o: OBJECT_FILES_NON_STANDARD := y
  87. $(obj)/wolfcrypt/src/sp_x86_64_asm.o: OBJECT_FILES_NON_STANDARD := y
  88. ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
  89. rename-pie-text-and-data-sections: $(WOLFSSL_OBJ_TARGETS)
  90. ifndef NM
  91. NM := nm
  92. endif
  93. ifndef READELF
  94. READELF := readelf
  95. endif
  96. ifndef OBJCOPY
  97. OBJCOPY := objcopy
  98. endif
  99. .PHONY: rename-pie-text-and-data-sections
  100. rename-pie-text-and-data-sections:
  101. ifneq "$(quiet)" "silent_"
  102. @echo -n ' Checking wolfCrypt for unresolved symbols and forbidden relocations... '
  103. endif
  104. @cd "$(obj)" || exit $$?; \
  105. $(LD) -relocatable -o wolfcrypt_test_link.o $(WOLFCRYPT_PIE_FILES) || exit $$?; \
  106. undefined=$$($(NM) --undefined-only wolfcrypt_test_link.o) || exit $$?; \
  107. GOT_relocs=$$($(READELF) --relocs --wide wolfcrypt_test_link.o | egrep '^[^ ]+ +[^ ]+ +[^ ]*GOT[^ ]* ') || [ $$? = 1 ] || exit 2; \
  108. rm wolfcrypt_test_link.o; \
  109. if [ -n "$$undefined" ]; then \
  110. echo "wolfCrypt container has unresolved symbols:" 1>&2; \
  111. echo "$$undefined" 1>&2; \
  112. exit 1; \
  113. fi; \
  114. if [ -n "$$GOT_relocs" ]; then \
  115. echo "wolfCrypt container has GOT relocations (non-local function address used as operand?):" 1>&2; \
  116. echo "$$GOT_relocs" 1>&2; \
  117. exit 1; \
  118. fi
  119. ifneq "$(quiet)" "silent_"
  120. @echo 'OK.'
  121. endif
  122. @cd "$(obj)" || exit $$?; \
  123. for file in $(WOLFCRYPT_PIE_FILES); do \
  124. $(OBJCOPY) --rename-section .text=.text.wolfcrypt --rename-section .data=.data.wolfcrypt "$$file" || exit $$?; \
  125. done
  126. ifneq "$(quiet)" "silent_"
  127. @echo ' wolfCrypt .{text,data} sections containerized to .{text,data}.wolfcrypt'
  128. endif
  129. $(src)/linuxkm/module_exports.c: rename-pie-text-and-data-sections
  130. endif
  131. # auto-generate the exported symbol list, leveraging the WOLFSSL_API visibility tags.
  132. # exclude symbols that don't match wc_* or wolf*.
  133. $(src)/linuxkm/module_exports.c: $(src)/linuxkm/module_exports.c.template $(WOLFSSL_OBJ_TARGETS)
  134. @cp $< $@
  135. @readelf --symbols --wide $(WOLFSSL_OBJ_TARGETS) | \
  136. awk '/^ *[0-9]+: / { \
  137. if ($$8 !~ /^(wc_|wolf|WOLF|TLSX_)/){next;} \
  138. if (($$4 == "FUNC") && ($$5 == "GLOBAL") && ($$6 == "DEFAULT")) { \
  139. print "EXPORT_SYMBOL_NS(" $$8 ", WOLFSSL);"; \
  140. } \
  141. }' >> $@
  142. @echo -e '#ifndef NO_CRYPT_TEST\nEXPORT_SYMBOL_NS(wolfcrypt_test, WOLFSSL);\n#endif' >> $@
  143. clean-files := module_exports.c