Skip to content

Enable dynamic wasm build for grep#190

Open
vidyalakshmir wants to merge 1 commit intomainfrom
grep-dynamic
Open

Enable dynamic wasm build for grep#190
vidyalakshmir wants to merge 1 commit intomainfrom
grep-dynamic

Conversation

@vidyalakshmir
Copy link
Copy Markdown
Contributor

@vidyalakshmir vidyalakshmir commented Apr 13, 2026

Closes #102

Summary

This PR updates the GNU grep build pipeline to support cross-compiling as a WebAssembly Position Independent Executable (PIE). When LIND_DYLINK=1 is enabled, grep will be compiled to dynamically link its external dependencies (specifically the standard C library) at runtime via the lind-wasm environment.

Shared libraries that grep depends on:

  • libc

Technical Details

Like other Autotools-based projects, building grep for WebAssembly dynamic linking requires specific compiler flag workarounds to prevent ELF compatibility errors.

  1. Linker Flag Splitting (LDFLAGS)
    Autoconf feature tests fail if they encounter undefined symbols (like PIE imports) during the configure phase.

Fix: The script uses two LDFLAGS arrays. LDFLAGS_CONFIGURE uses strict static linking rules so feature detection works cleanly. LDFLAGS_WASM (injected only during the make step) includes the necessary -Wl,-pie, --allow-undefined, and --unresolved-symbols=import-dynamic flags required to build the Wasm PIE.

  1. Position-Independent Internal Build

Modifies the underlying configure steps to force static archive generation (--disable-shared --enable-static) while explicitly injecting -fPIC into the CFLAGS. This bypasses legacy ELF linking errors while guaranteeing that the compiled objects are properly configured for dynamic execution.

Statically Linked (gnulib): Compiled internally as a static archive with -fPIC. This keeps grep's internal polyfills self-contained and completely bypasses Libtool's incompatibility with generating Wasm shared libraries.

Dynamically Linked (libc): Standard C library functions are left unresolved using --unresolved-symbols=import-dynamic requiring the host environment to map libc.so into the WebAssembly env namespace at runtime.

  1. CRT & Export Handling

Injects required Lind-specific CRT objects (set_stack_pointer.o, crt1_shared.o).

Runs add-export-tool immediately after Wasm optimization to expose __wasm_apply_tls_relocs, __wasm_apply_global_relocs, and __stack_pointer to the runtime host.

Changes to grep test script

No changes are required for grep's run_tests.sh. The latest lind_run script (after the dynamic loading branch merge) handles dynamic standard libraries by automatically preloading libc.cwasm and libm.cwasm. Since grep has no external dependencies beyond libc, it requires no explicit --preload flags.

How to Test

Static build

make grep
APP=grep make test

Dynamic build

make grep
LIND_DYLINK= 1 APP=grep make test

The dynamic build test succeeds with 4/5 tests being successful.

The one test which fails is due to a missing symbol from libc, which is currently handled in this issue.

/home/lind/lind-wasm/scripts/lind_run /usr/local/bin/grep -i hello /tests/grep/hello.txt
failed to run main module
Caused by:
    0: failed to invoke command default
    1: unknown import: `env::towupper` has not been defined

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.

grep - dynamic build

1 participant