diff options
author | Jakub Czapiga <jacz@semihalf.com> | 2021-07-29 13:27:54 +0200 |
---|---|---|
committer | Nick Vaccaro <nvaccaro@google.com> | 2021-08-09 18:01:54 +0000 |
commit | a7a49e64c8aa0c8634d3bf31dba3c1d354ffef01 (patch) | |
tree | 54f97d974fb8a4bc09ba79c0da998cadfa600262 /tests/Makefile.inc | |
parent | ecd1bf57857e579b4403742c912d0f9134ee69ea (diff) |
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>
Diffstat (limited to 'tests/Makefile.inc')
-rw-r--r-- | tests/Makefile.inc | 19 |
1 files changed, 18 insertions, 1 deletions
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 $$@ |