Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default wasm32-wasip2 objects to -fPIC by default #495

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Default wasm32-wasip2 objects to -fPIC by default
This commit updates all of the `*.a` archives built for the
`wasm32-wasip2` target to use `-fPIC` instead of the default non-PIC
flag. This makes them suitable for linking into either a dynamic library
or a non-dynamic binary without recompiling libc. This is intended to
help integrate shared libraries into other precompiled languages, such
as Rust's standard library, as it makes its way upstream.

Lots of discussion about this also happened on #429 so I wanted to
highlight that this is only happening for the wasm32-wasip2 target and
no other targets. For example neither wasm32-wasi nor wasm32-wasip1 are
affected. Only users of wasm32-wasip2, of which there aren't too too
many, are possibly affected by the runtime overheads here. I have not
measured the hypothetical runtime myself, however.
  • Loading branch information
alexcrichton committed May 7, 2024
commit 9a20e8658eaf02ca71e06c18c6c0f1171e849b5b
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,15 @@ $(OBJDIR)/libdl.so.a: $(LIBDL_SO_OBJS)

$(OBJDIR)/libsetjmp.so.a: $(LIBSETJMP_SO_OBJS)

# For the wasip2 target default to `-fPIC` for all archives to make them
# suitable for linking later into a shared object if desired. This can come with
# modest runtime overhead so this isn't the default for all targets, so other
# targets below compile non-PIC versions of objects and use those for archives
# instead.
ifeq ($(WASI_SNAPSHOT),p2)
$(SYSROOT_LIB)/%.a: $(OBJDIR)/%.so.a
cp $< $@
else
$(SYSROOT_LIB)/libc.a: $(LIBC_OBJS)

$(SYSROOT_LIB)/libc-printscan-long-double.a: $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS)
Expand All @@ -618,6 +627,7 @@ $(SYSROOT_LIB)/libwasi-emulated-signal.a: $(LIBWASI_EMULATED_SIGNAL_OBJS) $(LIBW
$(SYSROOT_LIB)/libdl.a: $(LIBDL_OBJS)

$(SYSROOT_LIB)/libsetjmp.a: $(LIBSETJMP_OBJS)
endif

%.a:
@mkdir -p "$(@D)"
Expand Down Expand Up @@ -836,7 +846,7 @@ check-symbols: startup_files libc
for undef_sym in $$("$(NM)" --undefined-only "$(SYSROOT_LIB)"/libc.a "$(SYSROOT_LIB)"/libc-*.a "$(SYSROOT_LIB)"/*.o \
|grep ' U ' |sed 's/.* U //' |LC_ALL=C sort |uniq); do \
grep -q '\<'$$undef_sym'\>' "$(DEFINED_SYMBOLS)" || echo $$undef_sym; \
done | grep -E -v "^__mul|__memory_base|__indirect_function_table" > "$(UNDEFINED_SYMBOLS)"
done | grep -E -v "^__mul|__memory_base|__indirect_function_table|__table_base" > "$(UNDEFINED_SYMBOLS)"
grep '^_*imported_wasi_' "$(UNDEFINED_SYMBOLS)" \
> "$(SYSROOT_LIB)/libc.imports"

Expand Down