繁体   English   中英

使用带有 openssl 的 libc 时修复动态 linker 错误

[英]Fixing dynamic linker errors when using libc with openssl

这是一个使用 openssl 库的简单 hello world sha1-hasher。

#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 so I grabbed the openssl source ( git clone https://github.com/openssl/openssl ), configured it with ./config --prefix=/usr/local/musl , built it and installed it and now 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

是什么原因造成的,我该如何解决?

您的 OpenSSL 似乎不是针对 musl-libc 构建的

The musl-libc has its own dynamic linker (see https://wiki.musl-libc.org/faq.html ), we could create a soft link for the musl dynamic linker. -syslibdir是动态库所在的目录,例如ld-musl-x86_64.so.1 ,请参阅https://wiki.musl-libc.org/getting-started.html

sudo ln -sf <YOUR-MUSL-LIBC-syslibdir/ld-musl-x86_64.so.1> /usr/bin/musl-ldd  

然后你可以看到 openssl 是否是针对 musl-libc 构建的。 当我使用 glibc 构建 OpenSSL 时,它显示以下错误

$ musl-ldd <YOUR-OPENSSL-SRC-musl-ldd ./libcrypto.so.1.1> 
        musl-ldd (0x7fcd5a749000)
        libdl.so.2 => musl-ldd (0x7fcd5a749000)
        libpthread.so.0 => musl-ldd (0x7fcd5a749000)
        libc.so.6 => musl-ldd (0x7fcd5a749000)
Error relocating ./libcrypto.so.1.1: makecontext: symbol not found
Error relocating ./libcrypto.so.1.1: setcontext: symbol not found
Error relocating ./libcrypto.so.1.1: __register_atfork: symbol not found
Error relocating ./libcrypto.so.1.1: getcontext: symbol not found

并且 glib 动态 linker 工作正常,

$ ldd ./libcrypto.so.1.1
        linux-vdso.so.1 (0x00007ffd395a6000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff6e6e64000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff6e6e41000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff6e6c4f000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ff6e71d7000)

要使用 musl-libc 构建 OpenSSL,我们还必须指定 linux 标头的位置以避免<linux/mman.h>之类的错误

I only have attempted to build OpenSSL using Clang and Musl-libc, here is the clang wrapper I used https://gist.github.com/randoruf/d1aa4e8acb0a852addcd2b84fc003719 .

但是问题仍然很少,似乎 OpenSSL 中的make testlld有一些依赖。 我发现一些测试不会通过(例如test_internal_ctype ),不知道如何修复它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM