openssl 的编译(linux、Ubuntu) 和 交叉编译(arm、Hi3531A)的问题分析、解决

目录

一、编译环境及准备材料

二、Ubuntu下编译openssl

三、交叉编译 openssl


一、编译环境及准备材料

1、编译环境:

1.1、Ubuntu环境 - ubuntu 14.04.1

$ uname -a
Linux ubuntu 4.4.0-128-generic #154~14.04.1-Ubuntu SMP Fri May 25 14:58:51 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

1.2、交叉编译器

$ arm-hisiv100nptl-linux-gcc -v
Using built-in specs.
Target: arm-hisiv100-linux-uclibcgnueabi
Configured with: ../gcc-4.4-2010q1/configure --host=i486-linux-gnu --build=i486-linux-gnu --target=arm-hisiv100-linux-uclibcgnueabi 
--prefix=/home/sying/uclibc_h3/hisiv100_finalnptl_src/hisiv100_src/install/arm-hisiv100-linux 
...
...
Thread model: posix
gcc version 4.4.1 (Hisilicon_v100(gcc4.4-290+uclibc_0.9.32.1+eabi+linuxpthread)) 

2、openssl 源码

openssl 官网源码下载

openssl有提供旧版本源码下载,我这里下载的是 openssl-1.1.0l.tar.gz

二、Ubuntu下编译openssl

在Ubuntu下编译,没有遇到报错的地方,只需要配置好输出目录即可。

将源码压缩包/home/samba/openssl/ 目录后,解压并创建 ssl_result_ubuntu 用来存放编译结果,进入源码目录执行 config 文件生成Makefile,用 --prefix 来指定openssl的安装目录,然后直接编译,过程如下:

ubuntu:/home/samba/openssl$ tar zxf openssl-1.1.0l.tar.gz 
ubuntu:/home/samba/openssl$ mkdir ssl_result_ubuntu
ubuntu:/home/samba/openssl$ cd openssl-1.1.0l/
ubuntu:/home/samba/openssl/openssl-1.1.0l$ ./config --prefix=/home/samba/openssl/ssl_result_ubuntu
ubuntu:/home/samba/openssl/openssl-1.1.0l$ make
ubuntu:/home/samba/openssl/openssl-1.1.0l$ make install
ubuntu:/home/samba/openssl/openssl-1.1.0l$ ls ../ssl_result_ubuntu/
bin  include  lib  share  ssl

三、交叉编译 openssl

1、配置、编译

交叉编译过程中出现比较多问题,问题集中在执行 config 生成 Makefile 的过程中,下面列出问题及解决方案。

将源码压缩包/home/samba/openssl/ 目录后,解压并创建 ssl_result_3531A 用来存放编译结果,进入源码目录执行 config 文件生成Makefile,用 --prefix 来指定 openssl 的安装目录,然后再使用 --cross-compile-prefix 来指定交叉编译工具前缀,接着编译出现错误,过程如下:

ubuntu:/home/samba/openssl$ tar zxf openssl-1.1.0l.tar.gz 
ubuntu:/home/samba/openssl$ mkdir ssl_result_3531A
ubuntu:/home/samba/openssl$ cd openssl-1.1.0l/
ubuntu:/home/samba/openssl/openssl-1.1.0l$ ./config --prefix=/home/samba/openssl/ssl_result_3531A --cross-compile-prefix=arm-hisiv100nptl-linux-
ubuntu:/home/samba/openssl/openssl-1.1.0l$ make
...
PIC -DOPENSSL_USE_NODELETE -c -o crypto/aes/aes-x86_64.o crypto/aes/aes-x86_64.s
crypto/aes/aes-x86_64.s: Assembler messages:
crypto/aes/aes-x86_64.s:2: Error: unrecognized symbol type ""
crypto/aes/aes-x86_64.s:3: Error: alignment too large: 15 assumed
crypto/aes/aes-x86_64.s:5: Error: bad instruction `xorl 0(%r15),%eax'
crypto/aes/aes-x86_64.s:6: Error: bad instruction `xorl 4(%r15),%ebx'
crypto/aes/aes-x86_64.s:7: Error: bad instruction `xorl 8(%r15),%ecx'
...

2、分析问题,加 no-asm 配置选项

问题分析:从错误打印看到,错误出现在编译 .s 文件(汇编文件)时,且后面还打印了汇编代码。原来 openssl 在编译时会默认使用汇编代码来加速编译过程,但只针对 x86平台,而 x86平台 的汇编代码和 arm平台 的汇编代码是不同的,所以会报错。

解决方案:执行 config 时,加上 no-asm 表示不使用汇编代码加速编译。

继续编译:重新生成 Makefiel,重新编译,出现错误,过程如下:

ubuntu:/home/samba/openssl/openssl-1.1.0l$ ./config no-asm --prefix=/home/samba/openssl/ssl_result_3531A --cross-compile-prefix=arm-hisiv100nptl-linux-
ubuntu:/home/samba/openssl/openssl-1.1.0l$ make clean && make 
省略无关打印...
F crypto/aes/aes_cbc.d.tmp -MT crypto/aes/aes_cbc.o -c -o crypto/aes/aes_cbc.o crypto/aes/aes_cbc.c
cc1: error: unrecognized command line option "-m64"
make[1]: *** [crypto/aes/aes_cbc.o] Error 1
省略无关打印...

3、分析问题,删除 Makefile 的 -m64

问题分析:错误打印表示 "-m64" 无法识别。查资料得知,-m64是x86 64位应用编译选项,m64选项设置int为32 bits及long指针为64 bits,为AMD的x86 64架构生成代码。所以,在arm平台无法支持。

解决方案:删除 Makefile 的两处 -m64,可以使用下面命令删除,也可以打开Makefile,搜索删除。

sed -i 's/-m64//' Makefile

继续编译:修改 Makefile 后,重新编译,出现错误如下:

./libcrypto.so: undefined reference to `getcontext'
./libcrypto.so: undefined reference to `setcontext'
./libcrypto.so: undefined reference to `makecontext'

4、分析问题,加 no-async 配置选项,编译通过

问题分析:查资料得知,这个错误是编译时缺少 ucontext 库(​ucontext提供的四个函数getcontext、setcontext、makecontext、swapcontext、可以在一个进程中实现用户级的线程切换)。海思3531A平台的交叉编译工具没有提供GNU C的 ucontext 库,所以出错。

解决方案:执行 config 时,加上 no-async,不使用 ucontext 库

继续编译:加上 no-async,重新生成Makefile,编译通过,过程如下:

ubuntu:/home/samba/openssl/openssl-1.1.0l$ ./config no-asm no-async --prefix=/home/samba/openssl/ssl_result_3531A --cross-compile-prefix=arm-hisiv100nptl-linux-
ubuntu:/home/samba/openssl/openssl-1.1.0l$ sed -i 's/-m64//' Makefile
ubuntu:/home/samba/openssl/openssl-1.1.0l$ make clean && make && make install
ubuntu:/home/samba/openssl/openssl-1.1.0l$ ls ../ssl_result_3531A/
bin  include  lib  share  ssl

到此,编译完成,在指定的安装目录下,生成了对应的头文件和库文件。

另外,关于 openssl 的配置选项 no-asm、no-async,在上文已经讲清楚了,而对其他配置选项感兴趣的可以查看 openssl编译参数选项

如果文章能解决你的问题,留个赞让我知道一下 ^_^

  • 13
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是在 Linux 上使用 OpenSSL 进行交叉编译的一般步骤: 1. 获取交叉编译工具链 您需要获取适用于您的目标体系结构的交叉编译工具链。这可以通过您的操作系统或第三方提供商进行获取。 2. 获取 OpenSSL 源代码 从 OpenSSL 官方网站下载最新版本的源代码,并将其解压缩到您的计算机上。 3. 配置 OpenSSL 在终端中导航到解压缩的 OpenSSL 目录,并运行以下命令: ``` ./Configure <options> --cross-compile-prefix=<prefix> ``` 其中,`<options>` 是您需要为 OpenSSL 配置指定的选项,以便在目标体系结构上进行编译。`<prefix>` 是您的交叉编译工具链的前缀。 例如,如果您的交叉编译工具链的前缀为 `arm-linux-gnueabi-`,您可以运行以下命令: ``` ./Configure linux-armv4 --cross-compile-prefix=arm-linux-gnueabi- ``` 4. 编译 OpenSSL 在终端中运行以下命令编译 OpenSSL: ``` make CC=<compiler> AR=<archiver> RANLIB=<ranlib> ``` 其中,`<compiler>` 是您的交叉编译工具链中的编译器,`<archiver>` 是您的交叉编译工具链中的静态库归档器,`<ranlib>` 是您的交叉编译工具链中的 ranlib 工具。 例如,如果您的交叉编译工具链的前缀为 `arm-linux-gnueabi-`,您可以运行以下命令: ``` make CC=arm-linux-gnueabi-gcc AR=arm-linux-gnueabi-ar RANLIB=arm-linux-gnueabi-ranlib ``` 5. 安装 OpenSSL 在终端中运行以下命令安装 OpenSSL: ``` make install DESTDIR=<install_dir> ``` 其中,`<install_dir>` 是您要安装 OpenSSL 的目录。 例如,如果您要将 OpenSSL 安装到 `/usr/local/openssl`,您可以运行以下命令: ``` make install DESTDIR=/usr/local/openssl ``` 完成这些步骤后,您应该能够在您的目标体系结构上运行 OpenSSL

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wkd_007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值