From ea7e9d0f17b8006ef31141fbf2dfc4ff79eb599e Mon Sep 17 00:00:00 2001 From: Tim Hutt Date: Wed, 27 Nov 2024 11:48:57 +0000 Subject: [PATCH] Support static compilation of the emulator Optionally wrap the libs with `-Wl,-Bstatic`, `-Wl,-Bdynamic` which will enable static compilation for those libs. We don't really want to use `-static` because it statically links glibc, which is a bad idea, and also increases the binary size. Linking with a sufficiently old version of glibc is a better option. --- Makefile | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3d8835260..29ff9d105 100644 --- a/Makefile +++ b/Makefile @@ -158,6 +158,8 @@ SOFTFLOAT_SPECIALIZE_TYPE = RISCV GMP_FLAGS = $(shell pkg-config --cflags gmp) # N.B. GMP does not have pkg-config metadata on Ubuntu 18.04 so default to -lgmp GMP_LIBS = $(shell pkg-config --libs gmp || echo -lgmp) + +# TODO: Remove Zlib when upgrading to Sail 0.19; it is no longer a requirement. ZLIB_FLAGS = $(shell pkg-config --cflags zlib) ZLIB_LIBS = $(shell pkg-config --libs zlib) @@ -191,6 +193,21 @@ SAIL_FLAGS += -c_coverage $(ALL_BRANCHES) -c_include sail_coverage.h C_LIBS += $(SAIL_LIB_DIR)/coverage/libsail_coverage.a -lm -lpthread -ldl endif +# Optional link C_LIBS statically. Unlike -static this will not +# link glibc statically which is generally a bad idea. +ifneq (,$(STATIC)) + UNAME_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') + ifeq ($(UNAME_S),Darwin) + # Unfortunately the Mac linker does not support -Bstatic. + GMP_LIBS = $(shell pkg-config --variable=libdir gmp)/libgmp.a + C_LIBS_WRAPPED = $(C_LIBS) + else + C_LIBS_WRAPPED = -Wl,--push-state,-Bstatic $(C_LIBS) -Wl,--pop-state + endif +else + C_LIBS_WRAPPED = $(C_LIBS) +endif + RISCV_EXTRAS_LEM_FILES = riscv_extras.lem mem_metadata.lem riscv_extras_fdext.lem RISCV_EXTRAS_LEM = $(addprefix handwritten_support/,$(RISCV_EXTRAS_LEM_FILES)) @@ -244,7 +261,7 @@ csim: c_emulator/riscv_sim_$(ARCH) rvfi: c_emulator/riscv_rvfi_$(ARCH) c_emulator/riscv_sim_$(ARCH): generated_definitions/c/riscv_model_$(ARCH).c $(C_INCS) $(C_SRCS) $(SOFTFLOAT_LIBS) Makefile - $(CC) -g $(C_WARNINGS) $(C_FLAGS) $< $(C_SRCS) $(SAIL_LIB_DIR)/*.c $(C_LIBS) -o $@ + $(CC) -g $(C_WARNINGS) $(C_FLAGS) $< $(C_SRCS) $(SAIL_LIB_DIR)/*.c $(C_LIBS_WRAPPED) -o $@ # Note: We have to add -c_preserve since the functions might be optimized out otherwise rvfi_preserve_fns=-c_preserve rvfi_set_instr_packet \ @@ -269,7 +286,7 @@ generated_definitions/c/riscv_rvfi_model_$(ARCH).c: $(SAIL_RVFI_SRCS) model/main mv $@.new $@ c_emulator/riscv_rvfi_$(ARCH): generated_definitions/c/riscv_rvfi_model_$(ARCH).c $(C_INCS) $(C_SRCS) $(SOFTFLOAT_LIBS) Makefile - $(CC) -g $(C_WARNINGS) $(C_FLAGS) $< -DRVFI_DII $(C_SRCS) $(SAIL_LIB_DIR)/*.c $(C_LIBS) -o $@ + $(CC) -g $(C_WARNINGS) $(C_FLAGS) $< -DRVFI_DII $(C_SRCS) $(SAIL_LIB_DIR)/*.c $(C_LIBS_WRAPPED) -o $@ latex: $(SAIL_SRCS) Makefile mkdir -p generated_definitions/latex