Skip to content

Commit 66ee2c2

Browse files
committed
linuxkm/Makefile and linuxkm/Kbuild:
* refactor .PHONY Kbuild target rename-pie-text-and-data-sections into macro RENAME_PIE_TEXT_AND_DATA_SECTIONS, and execute it conditional on module_exports.c regeneration; * use .ONESHELL in the wrapper Makefile too, and rework the changes in bf5536d such that the recursive make is always executed, but will leave the target untouched if it was already up-to-date relative to its dependencies. these tweaks fix the module build to restore automatic rebuild when dependencies are updated.
1 parent 7ddf263 commit 66ee2c2

File tree

2 files changed

+43
-46
lines changed

2 files changed

+43
-46
lines changed

linuxkm/Kbuild

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
179179

180180
LDFLAGS_libwolfssl.o += -T $(src)/wolfcrypt.lds
181181

182-
rename-pie-text-and-data-sections: $(WOLFSSL_OBJ_TARGETS)
183-
184182
ifndef NM
185183
NM := nm
186184
endif
@@ -189,31 +187,30 @@ ifndef OBJCOPY
189187
OBJCOPY := objcopy
190188
endif
191189

192-
.PHONY: rename-pie-text-and-data-sections
193-
rename-pie-text-and-data-sections:
194-
ifneq "$(quiet)" "silent_"
195-
@echo -n ' Checking wolfCrypt for unresolved symbols and forbidden relocations... '
196-
endif
197-
@cd "$(obj)" || exit $$?
198-
$(LD) -relocatable -o wolfcrypt_test_link.o $(WOLFCRYPT_PIE_FILES) || exit $$?
199-
undefined=$$($(NM) --undefined-only wolfcrypt_test_link.o) || exit $$?
200-
GOT_relocs=$$($(READELF) --relocs --wide wolfcrypt_test_link.o | grep -E '^[^ ]+ +[^ ]+ +[^ ]*GOT[^ ]* ') || [ $$? = 1 ] || exit 2
201-
rm wolfcrypt_test_link.o
202-
if [ -n "$$undefined" ]; then
203-
echo "wolfCrypt container has unresolved symbols:" 1>&2
204-
echo "$$undefined" 1>&2
205-
exit 1
206-
fi
207-
if [ -n "$$GOT_relocs" ]; then
208-
echo "wolfCrypt container has GOT relocations (non-local function address used as operand?):" 1>&2
209-
echo "$$GOT_relocs" 1>&2
210-
exit 1
211-
fi
212-
ifneq "$(quiet)" "silent_"
213-
echo 'OK.'
214-
endif
215-
cd "$(obj)" || exit $$?
216-
for file in $(WOLFCRYPT_PIE_FILES); do
190+
RENAME_PIE_TEXT_AND_DATA_SECTIONS := \
191+
if [[ "$(quiet)" != "silent_" ]]; then \
192+
echo -n ' Checking wolfCrypt for unresolved symbols and forbidden relocations... '; \
193+
fi; \
194+
cd "$(obj)" || exit $$?; \
195+
$(LD) -relocatable -o wolfcrypt_test_link.o $(WOLFCRYPT_PIE_FILES) || exit $$?; \
196+
undefined=$$($(NM) --undefined-only wolfcrypt_test_link.o) || exit $$?; \
197+
GOT_relocs=$$($(READELF) --relocs --wide wolfcrypt_test_link.o | grep -E '^[^ ]+ +[^ ]+ +[^ ]*GOT[^ ]* ') || [ $$? = 1 ] || exit 2; \
198+
rm wolfcrypt_test_link.o; \
199+
if [ -n "$$undefined" ]; then \
200+
echo "wolfCrypt container has unresolved symbols:" 1>&2; \
201+
echo "$$undefined" 1>&2; \
202+
exit 1; \
203+
fi; \
204+
if [ -n "$$GOT_relocs" ]; then \
205+
echo "wolfCrypt container has GOT relocations (non-local function address used as operand?):" 1>&2; \
206+
echo "$$GOT_relocs" 1>&2; \
207+
exit 1; \
208+
fi; \
209+
if [[ "$(quiet)" != "silent_" ]]; then \
210+
echo 'OK.'; \
211+
fi; \
212+
cd "$(obj)" || exit $$?; \
213+
for file in $(WOLFCRYPT_PIE_FILES); do \
217214
$(OBJCOPY) --rename-section .text=.text.wolfcrypt \
218215
--rename-section .text.unlikely=.text.wolfcrypt \
219216
--rename-section .rodata=.rodata.wolfcrypt \
@@ -223,8 +220,8 @@ endif
223220
--rename-section .rodata.cst32=.rodata.wolfcrypt \
224221
--rename-section .data=.data.wolfcrypt \
225222
--rename-section .data.rel.local=.data.wolfcrypt \
226-
--rename-section .bss=.bss.wolfcrypt "$$file" || exit $$?
227-
done
223+
--rename-section .bss=.bss.wolfcrypt "$$file" || exit $$?; \
224+
done; \
228225
[ "$(KERNEL_ARCH_X86)" != "yes" ] || \
229226
{ $(READELF) --sections --syms --wide $(WOLFCRYPT_PIE_FILES) | \
230227
$(AWK) -v obj="$(obj)" ' \
@@ -298,19 +295,16 @@ endif
298295
} else { \
299296
exit(0); \
300297
}}'; } || \
301-
{ echo 'Error: symbol(s) missed by containerization.' >&2; exit 1; }
302-
ifneq "$(quiet)" "silent_"
303-
echo ' wolfCrypt .{text,data,rodata} sections containerized to .{text,data,rodata}.wolfcrypt'
304-
endif
305-
306-
$(obj)/linuxkm/module_exports.c: rename-pie-text-and-data-sections
307-
298+
{ echo 'Error: symbol(s) missed by containerization.' >&2; exit 1; }; \
299+
if [[ "$(quiet)" != "silent_" ]]; then \
300+
echo ' wolfCrypt .{text,data,rodata} sections containerized to .{text,data,rodata}.wolfcrypt'; \
301+
fi
308302
endif
309303

310-
311304
# auto-generate the exported symbol list, leveraging the WOLFSSL_API visibility tags.
312305
# exclude symbols that don't match wc_* or wolf*.
313306
$(obj)/linuxkm/module_exports.c: $(src)/module_exports.c.template $(WOLFSSL_OBJ_TARGETS) $(obj)/linuxkm/module_hooks.o
307+
@$(RENAME_PIE_TEXT_AND_DATA_SECTIONS)
314308
@cp $< $@ || exit $$?
315309
if [[ "$${VERSION}" -gt 6 || ("$${VERSION}" -eq 6 && "$${PATCHLEVEL}" -ge 13) ]]; then
316310
# use ASCII octal escape to avoid syntax disruption in the awk script.

linuxkm/Makefile

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# along with this program; if not, write to the Free Software
1919
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020

21+
.ONESHELL:
2122
SHELL=bash
2223

2324
all: libwolfssl.ko libwolfssl.ko.signed
@@ -96,8 +97,6 @@ ifndef MAKE_TMPDIR
9697
MAKE_TMPDIR := $(TMPDIR)
9798
endif
9899

99-
libwolfssl.ko: libwolfssl.o
100-
101100
GENERATE_RELOC_TAB := $(READELF) --wide -r libwolfssl.ko | \
102101
$(AWK) 'BEGIN { \
103102
n=0; \
@@ -131,21 +130,25 @@ GENERATE_RELOC_TAB := $(READELF) --wide -r libwolfssl.ko | \
131130
print "~0U };\nconst size_t wc_linuxkm_pie_reloc_tab_length = sizeof wc_linuxkm_pie_reloc_tab / sizeof wc_linuxkm_pie_reloc_tab[0];";\
132131
}'
133132

134-
libwolfssl.o:
133+
.PHONY: libwolfssl.ko
134+
libwolfssl.ko:
135135
@if test -z '$(KERNEL_ROOT)'; then echo '$$KERNEL_ROOT is unset' >&2; exit 1; fi
136136
@if test -z '$(AM_CFLAGS)$(CFLAGS)'; then echo '$$AM_CFLAGS and $$CFLAGS are both unset.' >&2; exit 1; fi
137137
@if test -z '$(src_libwolfssl_la_OBJECTS)'; then echo '$$src_libwolfssl_la_OBJECTS is unset.' >&2; exit 1; fi
138138
# after commit 9a0ebe5011 (6.10), sources must be in $(obj). work around this by making links to all needed sources:
139139
@mkdir -p '$(MODULE_TOP)/linuxkm'
140-
@test '$(MODULE_TOP)/module_hooks.c' -ef '$(MODULE_TOP)/linuxkm/module_hooks.c' || cp --no-dereference --symbolic-link --no-clobber '$(MODULE_TOP)'/*.[ch] '$(MODULE_TOP)/linuxkm/'
141-
@test '$(SRC_TOP)/wolfcrypt/src/wc_port.c' -ef '$(MODULE_TOP)/wolfcrypt/src/wc_port.c' || cp --no-dereference --symbolic-link --no-clobber --recursive '$(SRC_TOP)/wolfcrypt' '$(MODULE_TOP)/'
142-
@test '$(SRC_TOP)/src/wolfio.c' -ef '$(MODULE_TOP)/src/wolfio.c' || cp --no-dereference --symbolic-link --no-clobber --recursive '$(SRC_TOP)/src' '$(MODULE_TOP)/'
140+
@test '$(MODULE_TOP)/module_hooks.c' -ef '$(MODULE_TOP)/linuxkm/module_hooks.c' || cp --verbose --no-dereference --symbolic-link --no-clobber '$(MODULE_TOP)'/*.[ch] '$(MODULE_TOP)/linuxkm/'
141+
@test '$(SRC_TOP)/wolfcrypt/src/wc_port.c' -ef '$(MODULE_TOP)/wolfcrypt/src/wc_port.c' || cp --verbose --no-dereference --symbolic-link --no-clobber --recursive '$(SRC_TOP)/wolfcrypt' '$(MODULE_TOP)/'
142+
@test '$(SRC_TOP)/src/wolfio.c' -ef '$(MODULE_TOP)/src/wolfio.c' || cp --verbose --no-dereference --symbolic-link --no-clobber --recursive '$(SRC_TOP)/src' '$(MODULE_TOP)/'
143143
ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
144-
@echo -e "const unsigned int wc_linuxkm_pie_reloc_tab[] = { ~0U };\nconst size_t wc_linuxkm_pie_reloc_tab_length = 1;" > wc_linuxkm_pie_reloc_tab.c
144+
@$(eval RELOC_TMP := $(shell mktemp "$(MAKE_TMPDIR)/wc_linuxkm_pie_reloc_tab.c.XXXXXX"))
145+
@[[ -f wc_linuxkm_pie_reloc_tab.c ]] || echo -e "const unsigned int wc_linuxkm_pie_reloc_tab[] = { ~0U };\nconst size_t wc_linuxkm_pie_reloc_tab_length = 1;" > wc_linuxkm_pie_reloc_tab.c
146+
@if [[ -f libwolfssl.ko ]]; then touch -r libwolfssl.ko "$(RELOC_TMP)"; fi
145147
+$(MAKE) ARCH='$(KERNEL_ARCH)' $(OVERRIDE_PATHS) $(CROSS_COMPILE) -C '$(KERNEL_ROOT)' M='$(MODULE_TOP)' $(KBUILD_EXTRA_FLAGS) CC_FLAGS_FTRACE=
146-
@$(GENERATE_RELOC_TAB) > wc_linuxkm_pie_reloc_tab.c
148+
# if the above make didn't build a fresh libwolfssl.ko, then the module is already up to date and we leave it untouched, assuring stability for purposes of module-update-fips-hash.
149+
@if [[ ! libwolfssl.ko -nt "$(RELOC_TMP)" ]]; then rm "$(RELOC_TMP)"; exit 0; fi
150+
@$(GENERATE_RELOC_TAB) >| wc_linuxkm_pie_reloc_tab.c
147151
+$(MAKE) ARCH='$(KERNEL_ARCH)' $(OVERRIDE_PATHS) $(CROSS_COMPILE) -C '$(KERNEL_ROOT)' M='$(MODULE_TOP)' $(KBUILD_EXTRA_FLAGS) CC_FLAGS_FTRACE=
148-
@$(eval RELOC_TMP := $(shell mktemp "$(MAKE_TMPDIR)/wc_linuxkm_pie_reloc_tab.c.XXXXXX"))
149152
@$(GENERATE_RELOC_TAB) >| $(RELOC_TMP)
150153
@if diff wc_linuxkm_pie_reloc_tab.c $(RELOC_TMP); then echo " Relocation table is stable."; else echo "PIE failed: relocation table is unstable." 1>&2; rm $(RELOC_TMP); exit 1; fi
151154
@rm $(RELOC_TMP)

0 commit comments

Comments
 (0)