unit-wasm icon indicating copy to clipboard operation
unit-wasm copied to clipboard

Error adding symbols: archive has no index

Open alexugthub opened this issue 1 year ago • 4 comments

When I try to build some of the examples with clang, using clang -o myapp.wasm luw-echo-request.c -lunit-wasm, I get this error:

/usr/bin/ld: /lib/x86_64-linux-gnu/libunit-wasm.a: error adding symbols: archive has no index; run ranlib to add one
clang: error: linker command failed with exit code 1 (use -v to see invocation)

alexugthub avatar Oct 19 '24 17:10 alexugthub

You will need a command more like

$ clang -fno-strict-aliasing --target=wasm32-wasi --sysroot=/usr/wasm32-wasi -I../../src/c/include -Wl,--no-entry,--export=__heap_base,--export=__data_end,--export=malloc,--export=free,--stack-first,-z,stack-size=$((8*1024*1024)) -mexec-model=reactor --rtlib=compiler-rt -o myapp.wasm luw-echo-request.c -L../../src/c -lunit-wasm

-I../../src/c/include is the location of the unit-wasm.h header file -L../../src/c is the location of the libunit-wasm.a static library

The actual command that compiles the luw-echo-request.c is

clang -O2 -Wall -Wextra -Wdeclaration-after-statement -Wvla -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wimplicit-function-declaration -Wimplicit-int -Wint-conversion -std=gnu11 -g -fno-common -fno-strict-aliasing --target=wasm32-wasi --sysroot=/usr/wasm32-wasi -I../../src/c/include -Wl,--no-entry,--export=__heap_base,--export=__data_end,--export=malloc,--export=free,--stack-first,-z,stack-size=$((8*1024*1024)) -mexec-model=reactor --rtlib=compiler-rt -o luw-echo-request.wasm luw-echo-request.c -L../../src/c -lunit-wasm

If you are able to build libunit-wasm and the C examples, i.e.

$ make examples

or

$ make V=1 examples

To see exactly what's being done.

Then you should be good to go with the above command...

ac000 avatar Oct 19 '24 18:10 ac000

Thanks, my final command was

clang -O2 -Wall -Wextra -Wdeclaration-after-statement -Wvla -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wimplicit-function-declaration -Wimplicit-int -Wint-conversion -std=gnu11 -g -fno-common -fno-strict-aliasing --target=wasm32-wasi --sysroot= -I../../src/c/include -Wl,--no-entry,--export=__heap_base,--export=__data_end,--export=malloc,--export=free,--stack-first,-z,stack-size=$((8*1024*1024)) -mexec-model=reactor --rtlib=compiler-rt -o luw-echo-request.wasm luw-echo-request.c -L../../src/c -lunit-wasm

I had to include another directory

alexugthub avatar Oct 20 '24 18:10 alexugthub

I copied the .wasm file to the nginx unit server and updated the configuration

"applications": {
     "wasm": {
        "type": "wasm-wasi-component",
        "component": "/var/www/component.wasm",
        "access": {
          "filesystem": [
            "/tmp/",
            "/var/tmp/"
          ]
        }
     }
  }

but now I get this error

0: failed to compile component 1: failed to parse WebAssembly module 2: attempted to parse a wasm module with a component parser

So, this .wasm file is a module, not a component. NGINX Unit recommends using a component, but even when I switch to module, I get

failed to start prototype "wasm"

alexugthub avatar Oct 21 '24 14:10 alexugthub

There are two WebAssembly related language modules.

wasm which supports WebAssembly modules, which is what this repository is for and wasm-wasi-component which as the name suggests is for WebAssembly components.

So here we're dealing with modules. For the luw-echo-request module, you'd need a config like (see https://github.com/nginx/unit-wasm?tab=readme-ov-file#using-with-unit)

        "luw-echo-request": {
            "type": "wasm",
            "module": "/path/to/unit-wasm/examples/c/luw-echo-request.wasm",
            "request_handler": "luw_request_handler",
            "malloc_handler": "luw_malloc_handler",
            "free_handler": "luw_free_handler",
            "module_init_handler": "luw_module_init_handler",
            "module_end_handler": "luw_module_end_handler"
        }

ac000 avatar Oct 21 '24 15:10 ac000