Skip to content

Commit

Permalink
Support static compilation of the emulator
Browse files Browse the repository at this point in the history
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.

On Mac it dynamically links with zlib, but that's acceptable since zlib is always provided by MacOS. Also Zlib has been removed as a dependency for the next Sail compiler release.
  • Loading branch information
Timmmm authored Dec 6, 2024
1 parent f34a6cc commit cbc3396
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

# Optionally 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 -Wl,-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))

Expand Down Expand Up @@ -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 \
Expand All @@ -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
Expand Down

0 comments on commit cbc3396

Please sign in to comment.