Skip to content

Enable dynamic build and .cwasm generation for core libraries (gnulib, libtirpc, openssl, zlib)#185

Open
vidyalakshmir wants to merge 4 commits intomainfrom
dynamic-libraries
Open

Enable dynamic build and .cwasm generation for core libraries (gnulib, libtirpc, openssl, zlib)#185
vidyalakshmir wants to merge 4 commits intomainfrom
dynamic-libraries

Conversation

@vidyalakshmir
Copy link
Copy Markdown
Contributor

@vidyalakshmir vidyalakshmir commented Apr 10, 2026

Closes #104

Context

Previously, our build scripts handled the configure and make steps for our core libraries producing static archives required for the static build of applications. However, we needed to convert the resulting static archives into fully optimized, dynamically linked WebAssembly shared objects to run the applications with dynamic library support.

This PR introduces the automated steps to link, export, optimize, and precompile the static archives for gnulib, libtirpc, openssl, and zlib into dynamic .cwasm libraries when dynamic linking is enabled.

Proposed Changes

  • Conditional Execution: The dynamic build pipeline for each library only triggers if LIND_DYLINK="1". Otherwise, it exits cleanly after the standard static build.
  • Position-Independent Static Archives: Modifies the underlying configure steps to force static archive generation (--disable-shared --enable-static) while explicitly injecting -fPIC and -fvisibility=default into the CFLAGS. This bypasses legacy Libtool/ELF linking errors that the compiled objects are properly configured to expose their public APIs to the WebAssembly export table during the final shared library generation.
  • Shared WASM Generation: Links the respective static archives alongside lind_debug.o into shared WebAssembly files using clang with experimental PIC and shared memory flags.
  • Symbol Exporting: Uses add-export-tool to explicitly export necessary symbols (__wasm_apply_tls_relocs, __wasm_apply_global_relocs, and __stack_pointer) for each library.
  • WASM Optimization & Instrumentation: Runs wasm-opt on the generated files to enable bulk memory, threads, epoch injection, and asyncify.
  • Precompilation: Uses lind_compile to compile the optimized WASM files into their final .cwasm formats.
  • Staging: Copies the final .cwasm artifacts to the overlay library directory as standard .so files.

Technical Details

  • Clang Linker Flags: Implements necessary dynamic flags including -Wl,--import-memory, -Wl,--export-dynamic, and -Wl,--experimental-pic. Uses --whole-archive to ensure all symbols from the static archives are included in the shared libraries.
  • Error Handling: Added strict existence checks ([[ ! -f ... ]]) and fail-safes (|| exit 1) after every major tool execution (clang, add-export-tool, wasm-opt, lind_compile) to ensure the build pipeline fails loudly if an intermediate artifact is missing for any of the libraries.

To test

These libraries can be tested by running the dynamic build of apps. OpenSSL and zlib can be tested with curl and git. libtirpc can be tested with lmbench .

Note: gnulib is always statically linked with coreutils and other apps and hence dynamic build of the library is not essential.

Comment thread zlib/compile_zlib.sh
echo "[zlib] done → $OVERLAY/usr/lib/wasm32-wasi/libz.a"

if [[ "$LIND_DYLINK" != "1" ]]; then
echo "[zlib] done ?~F~R $OVERLAY/usr/lib/wasm32-wasi/libz.a"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

broken unicode arrow?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seem to print properly though. Will recheck.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly fine but might just want to avoid this stuff not sure about portability

Comment thread gnulib/compile_gnulib.sh
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When LIND_DYLINK=1, it configures with --enable-shared --disable-static but then links the static archive (libgnu.a) into a shared wasm. If configure is building a shared lib, why re-link from the .a? If it's building static to
then convert, the configure flags should stay --enable-static

Copy link
Copy Markdown
Contributor

@rennergade rennergade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor comment and some confusion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dynamic build of libraries - gnulib, libtirpc, openssl, zlib

2 participants