History

KVM: selftests: Link selftests directly with lib object files

The linker does obey strong/weak symbols when linking static libraries, it simply resolves an undefined symbol to the first-encountered symbol. This means that defining __weak arch-generic functions and then defining arch-specific strong functions to override them in libkvm will not always work. More specifically, if we have: lib/generic.c: void __weak foo(void) { pr_info("weak\n"); } void bar(void) { foo(); } lib/x86_64/arch.c: void foo(void) { pr_info("strong\n"); } And a selftest that calls bar(), it will print "weak". Now if you make generic.o explicitly depend on arch.o (e.g. add function to arch.c that is called directly from generic.c) it will print "strong". In other words, it seems that the linker is free to throw out arch.o when linking because generic.o does not explicitly depend on it, which causes the linker to lose the strong symbol. One solution is to link libkvm.a with --whole-archive so that the linker doesn't throw away object files it thinks are unnecessary. However that is a bit difficult to plumb since we are using the common selftests makefile rules. An easier solution is to drop libkvm.a just link selftests with all the .o files that were originally in libkvm.a.

David Matlack committed 4 years ago

Commit cdc979d1 parent acf57731 file · +4 −7Browse files

Unified · Split

tools/testing/selftests/kvm/Makefile+4−7
@@ -173,12 +173,13 @@ LDFLAGS += -pthread $(no-pie-option) $(pgste-option) · 172 unmodified lines
173# $(TEST_GEN_PROGS) starts with $(OUTPUT)/173# $(TEST_GEN_PROGS) starts with $(OUTPUT)/
174include ../lib.mk174include ../lib.mk
175 175
176STATIC_LIBS := $(OUTPUT)/libkvm.a
177LIBKVM_C := $(filter %.c,$(LIBKVM))176LIBKVM_C := $(filter %.c,$(LIBKVM))
178LIBKVM_S := $(filter %.S,$(LIBKVM))177LIBKVM_S := $(filter %.S,$(LIBKVM))
179LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))178LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))
180LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))179LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))
181EXTRA_CLEAN += $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(STATIC_LIBS) cscope.*180LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ)
181
182EXTRA_CLEAN += $(LIBKVM_OBJS) cscope.*
182 183
183x := $(shell mkdir -p $(sort $(dir $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ))))184x := $(shell mkdir -p $(sort $(dir $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ))))
184$(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c185$(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c
@@ -187,12 +188,8 @@ $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c · 2 unmodified lines
187$(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S188$(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S
188 $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@189 $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
189 190
190LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ)
191$(OUTPUT)/libkvm.a: $(LIBKVM_OBJS)
192 $(AR) crs $@ $^
193
194x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS))))191x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS))))
195$(TEST_GEN_PROGS): $(STATIC_LIBS)192$(TEST_GEN_PROGS): $(LIBKVM_OBJS)
196 193
197cscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib ..194cscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib ..
198cscope:195cscope:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t