Disable libc link in emscripten

I am curious if it is possible to create an emscripten binary without libc.

If I have simple.c:

int add1(int x) { return x + 1; } 

And I do not want to include any of libc, is this possible?

My best attempt:

 emcc simple.c -s NO_FILESYSTEM=1 -s DISABLE_EXCEPTION_CATCHING=1 -s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE='[]' -s LIBRARY_DEPS_TO_AUTOEXPORT='[]' -s EXPORTED_FUNCTIONS='["add1"]' -s USE_SDL=0 -o simple.js -nostdlib 

But the generated output still contains characters for malloc, string conversion routines, etc.

I am also interested in the same process of creating a web assembly; that is, my real goal is to create a web assembly module containing only one function. Is this possible with emscripten?

+5
source share
4 answers

Emscripten is currently not suitable for this use case. Object files are a bitcode, and a link is used anytime, it wants to include some libraries and JS module code.

For WebAssembly, you can use clang from the top line of LLVM along with the s2wasm tool from Binaryen . Clang (optionally in combination with the LLVM llc ) can generate a .s assembly file that s2wasm can convert to wast (WebAssembly text format). You can convert this file to the wasm binary using the Binaryen wasm-as tool or the wast2wasm tool from WABT . This pipeline will turn any undefined links into functions from the C file into import imports, which you will need to manually connect to JavaScript functions. There are several examples of this floating around on the Internet (I found something by googling "webassembly clang s2wasm"). Also note that s2wasm does the layout of the s2wasm linear memory layout job, and it has several options that you want to know about; see his help output and his sources, for example. tool and linker code.

+4
source

You can use the -s ONLY_MY_CODE=1 option -s ONLY_MY_CODE=1 to get a file containing only your compiled code.

See the options that you can pass on to emscripten.

+2
source

You have 2 options for creating a standalone wasm module. Via emsdk or through llvm . Note that now emsdk generates code through the asm.js backend and does not natively support some i64 operations, such as multiplication.

emsdk

 emcc your_module.c -v -O3 -s WASM=1 -s SIDE_MODULE=1 -o your_module.wasm 

native llvm + binaryen

Please note that now you will need the latest llvm from trunk compiled with wasm support.

 clang -emit-llvm --target=wasm32 -O3 -c -o your_module.bc your_module.c llc -asm-verbose=false -o your_module.s your_module.bc s2wasm --emscripten-glue your_module.s > your_module.wast wasm-opt -O3 your_module.wast -o your_module.wasm 
+2
source

See WebAssembly Standalone , there is also a link to examples on this page.

 emcc simple.c -Os -s WASM=1 -s SIDE_MODULE=1 -o simple.wasm 

To run it in html / js, you must provide the imports object with imports.env={memoryBase, tableBase, memory, table} . But if you convert wasm file from wabt to wat format, you can strip the import section because you don't need either memory or a table, and then compile it back to wasm. Here is an example

+1
source

Source: https://habr.com/ru/post/1262805/


All Articles