4

I'm trying to compile some C code that uses OpenSSL with emscripten, but I get unresolved symbol warnings like:

warning: unresolved symbol: SHA256_Init
warning: unresolved symbol: SHA256_Final
warning: unresolved symbol: SHA256_Update

I compiled the code using this command:

emcc SHA256.c -lssl -lcrypto -L /usr/local/openssl-1.0.2k/lib/ -I /usr/local/openssl-1.0.2k/include -s WASM=1 -o SHA256.html --emrun

With the following source code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>


void sha256(char *string, char outputBuffer[65])
{
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, string, strlen(string));
    SHA256_Final(hash, &sha256);
    int i = 0;
    for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
    {
        sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
    }
    outputBuffer[64] = 0;
}


int main (void)
{
    static unsigned char buffer[65];
    sha256("string", buffer);
    printf("%s\n", buffer);

    return 0;
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Gemscripten
  • 41
  • 1
  • 2
  • Pay particular attention to @mrduclaw's answer and where the OpenSSL libraries are located in the link command. – jww Sep 01 '17 at 03:15
  • The problem is that you are expecting to link to the system ssl and crypto libraries using (-lssl -lcrypto). These are not available in emscripten directly. You would need the source code for these libraries and compile them with emscripten to be able to successfully run your code. @jww This is not a duplicate, rather a misunderstanding of how emscripten works – Tarun Gehlaut Sep 02 '17 at 09:59
  • @Tarun - Ack, thanks, reopened. Sorry about that. For the record, `emcc SHA256.c -lssl -lcrypto ...` looks wrong. When using traditional compilers, the equation is `emcc SHA256.c ... -lssl -lcrypto`. The libraries come last, not first. That's because `ld` is a single pass linker. – jww Sep 02 '17 at 10:43
  • @Gemscripten, did you managed to compile your code? Please share. – Shlomi Schwartz Apr 12 '18 at 13:32
  • @Gemscripten were you able to link your code, I am facing the same problem i.e. https://stackoverflow.com/questions/52327290/linking-openssl-with-webassembly. – Tito Sep 17 '18 at 12:34

1 Answers1

1

Compile the two libraries : crypto and openssl with emscripten, rather than using the system versions.

Use the generated .bc files for these libraries as linker arguments to the em++ command and it should work.

Tarun Gehlaut
  • 1,292
  • 10
  • 16