nodejs / uvwasi

WASI syscall API built atop libuv
MIT License
226 stars 50 forks source link

Link error with gcc 11.2.1 #175

Open danbev opened 2 years ago

danbev commented 2 years ago

I'm seeing the following link error when building locally using the following commands:

$ mkdir build                                                                   
$ cd build                                                                      
$ cmake ..                                                                      
$ make                                                                          
[ 59%] Linking C shared library libuvwasi.so                                    
/usr/bin/ld: _deps/libuv-build/libuv_a.a(random.c.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
...                                                                             
collect2: error: ld returned 1 exit status                                      
make[2]: *** [CMakeFiles/uvwasi.dir/build.make:210: libuvwasi.so] Error 1          
make[1]: *** [CMakeFiles/Makefile2:196: CMakeFiles/uvwasi.dir/all] Error 2         
make: *** [Makefile:146: all] Error 2

This can be avoided around by setting CMAKE_POSITION_INDEPENDENT_CODE to True:

$ cmake -DCMAKE_POSITION_INDEPENDENT_CODE=True ..

This could also be set in CMakeLists.txt:

set(CMAKE_POSITION_INDEPENDENT_CODE ON)                                         

GCC version:

$ gcc --version
gcc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)

I'm not sure if this is an issue with my local environment, as the CI servers are obviously working as expected. But I wanted to create this issue just in case anyone else runs into it.

phated commented 1 year ago

@cjihrig Do we know if the shared library version of uvwasi works? I'm getting linking errors when trying to link uvwasi with wasm-micro-runtime (instead of compiling the sources together).

cjihrig commented 1 year ago

I'm not sure. I didn't work on nor use the shared library version. It was added in https://github.com/nodejs/uvwasi/commit/d8280df01378fb1cb6eb27032d09537dde5389c3. Maybe @Diatrus knows.

Diatrus commented 1 year ago

I'm not sure. I didn't work on nor use the shared library version. It was added in d8280df. Maybe @Diatrus knows.

I currently use the shared version of the uvwasi library when linking with nodejs, and I don't know that anything has changed linking-wise. I'd have to see the error myself. As for the link error above, the shared library should be compiled with -fPIC. If this was a mistake on my part by not adding that, I can look into a PR soon.

marcopiraccini commented 1 year ago

I can reproduce the issue with:

➜ gcc --version                             
gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

...will take a look

ApsarasX commented 1 year ago

I also encounter this problem.

My compilation environment information is as follows:

ApsarasX commented 1 year ago

Is there any progress for this issue?

cjihrig commented 1 year ago

@phated do you know if #210 helps with this at all?

phated commented 1 year ago

@cjihrig I'm uncertain. I think wamr is doing static linking and I don't know how to build their executable with dynamic linking. My PR definitely enables linking somehow from produced artifacts.

ApsarasX commented 1 year ago

@cjihrig I'm uncertain. I think wamr is doing static linking and I don't know how to build their executable with dynamic linking. My PR definitely enables linking somehow from produced artifacts.

@phated I compiled standalone uvwasi, rather than linking it with WAMR.

yagehu commented 9 months ago

You can work around it by adding -fPIC:

cmake ../.. -DBUILD_TESTING=ON -DCMAKE_C_FLAGS=-fPIC