代码之家  ›  专栏  ›  技术社区  ›  Petr Skocik

在openssl中使用libc时修复动态链接器错误

  •  1
  • Petr Skocik  · 技术社区  · 6 年前

    这里有一个简单的hello world sha1哈希器,它使用openssl库。

    #include <openssl/sha.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        system("printf '%s' 'hello world' | sha1sum");
    
        unsigned char digest[SHA_DIGEST_LENGTH];
        char digest_pr[(SHA_DIGEST_LENGTH)*2+1];
        SHA_CTX ctx;
        if(!SHA1_Init(&ctx)) return 1;
    #define STR_STRLEN(A) A, (sizeof(A)/sizeof(*(A))-1)
        if(!SHA1_Update(&ctx,STR_STRLEN("hello"))) return EXIT_FAILURE;
        if(!SHA1_Update(&ctx,STR_STRLEN(" world"))) return EXIT_FAILURE;
        if(!SHA1_Final(digest,&ctx)) return EXIT_FAILURE;
        #define DIGITS "0123456789abcdef"
        for(size_t i=0;i<sizeof(digest);i++){
            digest_pr[i*2+0]=DIGITS[digest[i]/16];
            digest_pr[i*2+1]=DIGITS[digest[i]%16];
        }
        digest_pr[(SHA_DIGEST_LENGTH)*2]='\0';
        puts(digest_pr);
    }
    

    在安装了libssl dev的Mint/Ubuntu上,我可以编译并链接它 $CC sha.c (CC是gcc、tcc或clang中的一个)然后成功地运行它,但是这不适用于musl,所以我获取了openssl源代码( git clone https://github.com/openssl/openssl ),配置为 ./config --prefix=/usr/local/musl ,构建并安装它,现在 musl-gcc sha.c -lcrypto works 但是跑步 LD_LIBRARY_PATH=/usr/local/musl/lib a.out 让我明白:

    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: __fprintf_chk: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: makecontext: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: setcontext: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: __register_atfork: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: __memcpy_chk: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: __strcat_chk: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: secure_getenv: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: __vfprintf_chk: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: __syslog_chk: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: __memset_chk: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: __fread_chk: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: getcontext: symbol not found
    Error relocating /usr/local/musl/lib/libcrypto.so.1.1: __sprintf_chk: symbol not found
    

    是什么引起的?我该怎么解决?

    0 回复  |  直到 6 年前