diff --git a/tests/Makefile.inc b/tests/Makefile.inc index 056ad7df85..c3f5f51d1a 100644 --- a/tests/Makefile.inc +++ b/tests/Makefile.inc @@ -18,6 +18,7 @@ CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so 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)-config-file): $(TEST_KCONFIG_AUTOHEADER) $($(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 $$@