Nick Vaccaro has submitted this change. ( https://review.coreboot.org/c/coreboot/+/56719 )
Change subject: tests/Makefile.inc: Add function wrapping mechanism ......................................................................
tests/Makefile.inc: Add function wrapping mechanism
This patch extends mocks functionality to allowing wrapping of mocked functions. Original function name will be prefixed with `__real_`. Example: - Mocked function: cbfs_lookup() - New function name: __real_cbfs_lookup() - Mock name: cbfs_lookup()
Signed-off-by: Jakub Czapiga jacz@semihalf.com Change-Id: I7cd0d66a17029955cbf75c8b155a7ebb7f5513aa Reviewed-on: https://review.coreboot.org/c/coreboot/+/56719 Reviewed-by: Julius Werner jwerner@chromium.org Tested-by: build bot (Jenkins) no-reply@coreboot.org --- M tests/Makefile.inc 1 file changed, 18 insertions(+), 1 deletion(-)
Approvals: build bot (Jenkins): Verified Julius Werner: Looks good to me, approved
diff --git a/tests/Makefile.inc b/tests/Makefile.inc index 056ad7d..c3f5f51 100644 --- a/tests/Makefile.inc +++ b/tests/Makefile.inc @@ -18,6 +18,7 @@
CMAKE:= cmake OBJCOPY?= objcopy +OBJDUMP?= objdump
TEST_DEFAULT_CONFIG = $(top)/configs/config.emulation_qemu_x86_i440fx TEST_DOTCONFIG = $(testobj)/.config @@ -115,12 +116,28 @@
$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \ -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ + +# Weaken symbols listed as mocks to enable overriding in the code $($(1)-srcobjs): OBJCOPY_FLAGS += $$(foreach mock,$$($(1)-mocks),--globalize-symbol=$$(mock) --weaken-symbol=$$(mock)) + +# Compile sources and apply mocking/wrapping of selected symbols. +# For each listed mock add new symbol with prefix `__real_`, +# and pointing to the same section:address. $($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file) mkdir -p $$(dir $$@) $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \ -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig - $(OBJCOPY) $$@.orig $$(OBJCOPY_FLAGS) $$@ + $(OBJCOPY) $$@.orig $$(OBJCOPY_FLAGS) $$@.orig2 + objcopy_wrap_flags=''; \ + for sym in $$($(1)-mocks); do \ + sym_line="$$$$($(OBJDUMP) -t $$@.orig2 | grep -E "[0-9a-fA-F]+\s+w\s+F\s+.*\s$$$$sym$$$$")"; \ + if [ ! -z "$$$$sym_line" ] ; then \ + addr="$$$$(echo "$$$$sym_line" | awk '{ print $$$$1 }')"; \ + section="$$$$(echo "$$$$sym_line" | awk '{ print $$$$(NF - 2) }')"; \ + objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \ + fi \ + done ; \ + $(OBJCOPY) $$@.orig2 $$$$objcopy_wrap_flags $$@
$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB) $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@