Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I link a .so file that is a shared library? #16592

Open
jyh7a opened this issue Mar 25, 2022 · 8 comments
Open

How can I link a .so file that is a shared library? #16592

jyh7a opened this issue Mar 25, 2022 · 8 comments

Comments

@jyh7a
Copy link

jyh7a commented Mar 25, 2022

How can I link a .so file that is a shared library?

Hello.

  1. I want to convert tester.c to tester.js file.
    I need to link libMagicCrypto.so shared library to tester.c.

  2. I entered the following command.
    $ emcc tester.c -lMagicCrypto -L/home/jyh7a/webassembly -I/home/jyh7a/webassembly -ldl -o tester.js

But wasm-ld throws an unknown file type error and
emcc error also occurs. (It is attached below.)

2-1.
wasm-ld: error: unknown file type: /home/jyh7a/webassembly/libMagicCrypto.so

2-2.

emcc: error: '/home/jyh7a/emsdk/upstream/bin/wasm-ld -o tester.wasm /tmp/emscripten_temp_rtzaszvu/tester_0.o /home/jyh7a/webassembly/libMagicCrypto.so -L/home/jyh7a/webassembly -L/home/jyh7a/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten -lGL -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++ abi-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --strip-debug --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__stdio_exit --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init -- =stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max- memory=16777216 --global-base=1024' failed (returned 1)
  1. This is my folder structure. (All files in webassembly were delivered from other companies.)
webassembly
├── libMagicCrypto.so
├── libMagicCrypto.so.lic
├── mcapi.h
├── mcapi_error.h
├── mcapi_type.h
└── tester.c
  1. Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.7 (48a16209b1a0de5efd8112ce6430415730008d18)
clang version 15.0.0 (https://github.com/llvm/llvm-project fbce4a78035c32792b0a13cf1f169048b822c06b)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/jyh7a/emsdk/upstream/bi

5-1. Is it possible to dynamically link a shared library called .so ?
5-2. I wonder what is the cause of that error and how to fix it.
5-3. Please let me know if you need more information to solve the problem.

Thank you for your interest.

@ancientstraits
Copy link
Contributor

I think that libMagicCrypto.so is a desktop-compiled (ELF) library. You can check this by opening libMagicCrypto.so in a text editor and checking if it starts with ELF. When compiling Emscripten files, every library must be a WASM library, since browsers cannot run ELF files. This means that you should compile libMagicCrypto into a WASM file:

  1. Get the source code of the library.
  2. Find out what build system it uses.
  3. run em + that build system command:
    • If it is a make project, run emmake make.
    • If it is a ./configure && make project, run emconfigure ./configure && emmake make.
    • If it is a cmake project, run emcmake cmake && emmake make.

I cannot seem to find the source to libMagicCrypto, but if you know where it is, you probably can compile it. If you don't have the source, you might be out of luck.

@jyh7a
Copy link
Author

jyh7a commented Mar 28, 2022

@ancientstraits Thank you for your interest.

  1. As you said, I opened the .so file and checked it and saw that it was in ELF format.
    findELF

    You asked me what build system I used. Is it correct to ask what system you used to create a .so file?
    The libMagicCrypto.so file is the received file. Can I check which build system was used? Or should I ask the company that sent me the .so file?
    Currently, it is a bit difficult to ask the company that gave the .so file how it was built.

  2. I first assumed that it was built with make and tested the build. I entered the command below, but an error occurs.
    command
    $ em emmake make
    error
    em: command not found

    image

    command
    $ emcc emmake make
    error
    emcc: error: emmake: No such file or directory ("emmake" was expected to be an input file, based on the commandline arguments provided)

    image

    command
    $ emcc emmake make /home/jyh7a/webassembly/libMagicCrypto.so
    error
    em: command not found

    image

    I am getting this error, which part should I fix?
    3. run em + that build system command:
    Is it correct that em means emcc in ?

  3. I've never used make or cmake before, so I'm very confused.
    I am also studying emscripten recently.
    ( https://emscripten.org/docs/compiling/Building-Projects.html?highlight=emmake ) I am looking at this link, what document should I refer to to resolve the above error?

  4. To convert .so files to .wasm in the future
    Would it be better to install both make and cmake on my Ubuntu (Ubuntu 20.04.4 LTS)?
    Make and cmake are not installed yet.
    image

I would be very grateful if you could let me know if you have any information.

@ancientstraits
Copy link
Contributor

In order to compile the file, @jyh7a, you need to be in the directory of its source code. This is probably the root of a Git repository, and has a src directory containing files ending in .c. If you do have the source code of libMagicCrypto.so, then do ls to see which build files are there. emmake and emcmake don't use emcc, so you should just run emmake make or emcmake cmake without any emcc.

However, I think that you don't have the source code, since the company just sent you the ELF library with nothing else. I would see if their website has info on the library, and if there is a source code repository. If not, then you should contact them asking for the source code. Explain that the source code is needed in order to compile the library to WebAssembly to use it in a web browser.

Right now, I don't think it's possible to go from an ELF to WebAssembly, perhaps because ELF is made for a desktop, while WebAssembly is made for the web. So either you or the company should compile to wasm, or your wasm application will not be able to use libMagicCrypto.so.

@ancientstraits
Copy link
Contributor

@jyh7a I think it would be a good idea to have make and cmake installed on your computer with sudo apt install make cmake. Once your project gets large enough, you might need to write a Makefile or a CMakeLists.txt in order to automate the build process.

@jyh7a
Copy link
Author

jyh7a commented Mar 30, 2022

@ancientstraits Thank you very much for your answer.

I don't know how it was built because I only got the libMagicCrypto.so file from another company.
And the company that gave me the libMagicCrypto.so file doesn't convert it to a wasm file, so I guess I'll have to find another way.

I searched to see if I could use c files in nodejs.
I googled "how to get c function in nodejs" like this.

  1. I've seen answers telling you to use AddOn or FFI. If I do it this way, will the .so file be linked dynamically and used in the browser?

  2. If I use a compiler other than emscripten, is it possible to dynamically link the .so file and make it into a js file?

  3. Or is there a good way to recommend it?

Thanks for the always good info.
If you have any good ideas, I'd really appreciate it if you could let me know.

@ancientstraits
Copy link
Contributor

@jyh7a Even if you could use the C file in node.js (which is probably possible), node.js is still a server application, meaning you would have to run it on a server.

However, there might still be a small bit of hope. A decompiler turns binary into source code. Although there are not many C decompilers out there, you should research them and give some a try.

If even that does not work, then what you are trying to do with your constraints (use a library without source code in a web browser) is impossible. If the company asked you to do just that, you will have to tell them that it is impossible without the source code.

Also, you should look for extra information in the email in which the company shared the library with you. Does it reference any external names? Do those names lead to source code?

@jyh7a
Copy link
Author

jyh7a commented Apr 1, 2022

@ancientstraits Thank you so much for all the help!

Unfortunately, we don't have the c file that made the shared library.

I didn't know much about emscripten and ubuntu, but your help gave me a lot of information.

Currently, I am testing how to load java from node.js. (Link: https://www.geeksforgeeks.org/how-to-run-java-code-in-node-js/ )
Otherwise, I'm going to test it using ffi, ffi-napi.

If I succeed, I'll post another brief comment!
Thank you again for your help so far.

@Shaikh-Ubaid
Copy link

It seems probably I am having a similar use case. @jyh7a , please, could you possibly share if you found any way to link the shared.so library?

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

No branches or pull requests

3 participants