静态编译 openssl 二进制文件

问题描述 投票:0回答:6

从源 tarball 构建时由 config 和 make 命令生成的 openssl 二进制文件动态链接到这些库:

    linux-vdso.so.1 =>  (0x00007fffa75fe000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff7f79ab000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff7f75e2000)
    /lib64/ld-linux-x86-64.so.2 (0x00007ff7f7bd2000)

我的猜测是,如果我可以静态链接到 lib gcc,对其他共享库的依赖也会消失。

问题是如何让配置脚本生成静态链接的二进制

在 Windows 上构建的过程也相同吗?

openssl
6个回答
5
投票

对我有用的是将

-static
--static
传递到
./config
步骤。
--no-shared
似乎记录在 INSTALL 中,但导致构建失败。
-static
本身也会导致构建失败。


./config --static -static


3
投票

获取来源。我使用 git 因为我发现它更容易,但是下载源 tar.gz 也可以:

$ git clone git://git.openssl.org/openssl.git
Cloning into 'openssl'...
remote: Counting objects: 394745, done.
remote: Compressing objects: 100% (102341/102341), done.
remote: Total 394745 (delta 288534), reused 387444 (delta 281591)
Receiving objects: 100% (394745/394745), 92.39 MiB | 911.00 KiB/s, done.
Resolving deltas: 100% (288534/288534), done.
Updating files: 100% (24047/24047), done.

检查远程分支 (

git branch -r
) 或标签 (
git tag
) 并选择要构建的版本。我用的是最新的1.1.1j:

$ cd openssl
$ git checkout OpenSSL_1_1_1j
Note: switching to 'OpenSSL_1_1_1j'.
...
HEAD is now at 52c587d60b Prepare for 1.1.1j release

使用

./config
参数运行
-static

$ ./config -static
Operating system: x86_64-whatever-linux2
Configuring OpenSSL version 1.1.1j (0x101010afL) for linux-x86_64
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                     ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL file first)         ***
***                                                                ***
**********************************************************************

我从

-static
 文件中获得了这个 
INSTALL
参数:

    -Dxxx, -Ixxx, -Wp, -lxxx, -Lxxx, -Wl, -rpath, -R, -framework, -static
                     These system specific options will be recognised and
                     passed through to the compiler to allow you to define
                     preprocessor symbols, specify additional libraries, library
                     directories or other compiler options. It might be worth
                     noting that some compilers generate code specifically for
                     processor the compiler currently executes on. This is not
                     necessarily what you might have in mind, since it might be
                     unsuitable for execution on other, typically older,
                     processor. Consult your compiler documentation.

编译:

$ make -j`nproc`
...

检查它是否是静态二进制文件:

$ ldd apps/openssl
    not a dynamic executable
$ file apps/openssl
apps/openssl: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=286e4615c57e3c21b8e566eb2a046353fe2308c0, for GNU/Linux 3.2.0, with debug_info, not stripped

无需手动编辑Makefile。 不幸的是我不知道如何在 Windows 上执行此操作。


2
投票

我无法使上述解决方案发挥作用。链接器抛出有关 _dlopen 未定义的错误。

我在配置行中添加了 no-shared 选项,这构建的 openssl 静态链接到 openssl 库。

仍然依赖libsocket.so.2、linnsl.so、libz.so和libc.so.1


2
投票

对于 Windows,我成功地使用了这个序列

  • 克隆 OpenSSL_1_1_1-静态分支
  • 按照 Windows 说明安装 PerlNetwide Assembler (NASM);将这些 exe 添加到 PATH 中
  • 使用Visual Studio 2017命令提示符; cd openssl
  • perl Configure VC-WIN32 /MT
    • 注意:-static 对于 Windows 无效
  • 编辑makefile,找到'/MD'标志并将其删除(将使用/MT)
  • nmake
    • nmake test
    • nmake install

然后将库 libcrypto_static.lib、libssl_static.lib 链接到您的程序,该程序也必须使用 /MT 进行编译(/MTd 用于调试)。

其他 Visual Studio 编译器版本应该同样工作。

注意:根据使用情况,可能需要从 makefile 中删除标志 -D"OPENSSL_USE_APPLINK" 并重新编译静态库。


1
投票

我在寻找同样的东西时发现了这篇文章。我不知道配置脚本执行此操作的正确语法,但这就是我实现它的方法。

cd /tmp
wget http://www.openssl.org/source/openssl-1.0.1e.tar.gz
tar -zxvf openssl-1.0.1e.tar.gz
cd openssl-1.0.1e
./config

然后我将“-static -static-libgcc”添加到 openssl-1.0.1e/Makefile 的 CFLAG 行(注意这是在我运行 ./config 之后)。然后我像平常一样构建它。

make INSTALL_PREFIX=/tmp/package-root install

现在是静态编译的

$ ldd /tmp/package-root/usr/local/ssl/bin/openssl
        not a dynamic executable

0
投票

OpenSSL 编译和安装说明建议使用

Configure
而不是
config
,因为它们之间的区别在于Configure可以正确处理host-arch-compiler三元组,而config则不能。

INSTALL.md
指示同时使用标志
-no-shared
-no-pinshared
来启用静态库编译:

./Configure -no-shared -no-pinshared

© www.soinside.com 2019 - 2024. All rights reserved.