Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Linking static version of OpenSSL?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Dec 6, 2016 12:58 AM Last Post By: Angelo Soragna
Angelo Soragna

Posts: 3
Registered: 12/29/16
Linking static version of OpenSSL?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 29, 2016 8:10 AM
Hello

I'm using Delphi10.1 Berlin and attempting to statically link OpenSSL into an Android application.
So far I've been able to produce the static version of libcrypto.a and libssl.a from OpenSSL 1.0.1u tarball,
libs were built on OSX ElCapitan + XCode 7.3.1 + Android NDK r9d following some building tips found on https://wiki.openssl.org/index.php/Android

The static libs were finally copied to $(BDSLIB)\Android\release

I have included IdOpenSSLHeaders_Static in the use clause of a sample project, tweaked IdCompilerDefines.inc so that it would compile
statically for Android as per the following SO link: http://stackoverflow.com/questions/19695855/how-to-statically-link-indy-ssl-files

However when compiling a sample project the linker fails, listing several undefined references:
...
[DCC Error] E2597 C:\Users\.....\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: c:\\program files (x86)\\embarcadero\\studio\\18.0\\lib\\Android\\Release/libssl.a(s3_srvr.o): in function ssl3_get_cert_verify:s3_srvr.c(.text+0x3070): error: undefined reference to 'X509_certificate_type'
 
C:\Users\.....\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: c:\\program files (x86)\\embarcadero\\studio\\18.0\\lib\\Android\\Release/libssl.a(s3_clnt.o): in function ssl3_check_cert_and_algorithm:s3_clnt.c(.text+0x448c): error: undefined reference to 'X509_certificate_type'
 
C:\Users\.....\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: c:\\program files (x86)\\embarcadero\\studio\\18.0\\lib\\Android\\Release/libssl.a(s3_cbc.o): in function ssl3_cbc_digest_record:s3_cbc.c(.text+0x1410): error: undefined reference to 'MD5_Transform'
 
C:\Users\.....\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: c:\\program files (x86)\\embarcadero\\studio\\18.0\\lib\\Android\\Release/libssl.a(d1_lib.o): in function dtls1_new:d1_lib.c(.text+0x44): error: undefined reference to 'pqueue_new'
 
C:\Users\.....\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: c:\\program files (x86)\\embarcadero\\studio\\18.0\\lib\\Android\\Release/libssl.a(d1_lib.o): in function dtls1_new:d1_lib.c(.text+0x4c): error: undefined reference to 'pqueue_new'
...
...
...
C:\Users\.....\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: c:\\program files (x86)\\embarcadero\\studio\\18.0\\lib\\Android\\Release/libssl.a(tls_srp.o): in function SRP_Calc_A_param:tls_srp.c(.text+0xa8c): error: undefined reference to 'SRP_Calc_A'
...
...
...
[DCC Fatal Error] F2588 Linker error code: 1 ($00000001)
Failed
Elapsed time: 00:00:38.1


I'm not sure why it gives the above errors, as far as I can understand the static libraries were built with some missing feature or inconsistency, although the build process didn't actually raise any error and don't know exactly which settings I should configure.

I'm asking if somebody had ever attempted to statically link OpenSSL in an Android app or could share some hints about the building process or even share the static libs?

TIA

Angelo
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Linking static version of OpenSSL?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 29, 2016 11:17 AM   in response to: Angelo Soragna in response to: Angelo Soragna
Angelo wrote:

So far I've been able to produce the static version of libcrypto.a and
libssl.a from OpenSSL 1.0.1u tarball

Why 1.0.1 and not 1.0.2?

The static libs were finally copied to $(BDSLIB)\Android\release

But did you actually add the files to your project? It is not enough to
just put them in the IDE's lib folder.

However when compiling a sample project the linker fails, listing
several undefined references:

That has nothing to do with Indy, as it does not use the functions in question.
The errors are related to the lib files themselves. The linker is not able
to find various functions that libssl.a is statically linked to. Presumably
they reside in libcrypto.a, which is either being ignored by the linker,
or maybe it is the wrong version and does not contain the functions in question.

--
Remy Lebeau (TeamB)
Angelo Soragna

Posts: 3
Registered: 12/29/16
Re: Linking static version of OpenSSL?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 30, 2016 5:06 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

Why 1.0.1 and not 1.0.2?

No particular reason for that. I just didnt notice that 1.0.2 j is Long Term Support.
I built 1.0.2 and tried to link them, same result.

But did you actually add the files to your project? It is not enough to
just put them in the IDE's lib folder.

Yes, same result.
I even tried with $LINK directive, but it's actually windows platform specific, at least thats' what the compiler
warns about. However the outcome is the same.

http://docwiki.embarcadero.com/RADStudio/Berlin/en/OpenSSL shows that on iOS copying the .a libraries
would suffice and I thought it was the same on Android.


That has nothing to do with Indy, as it does not use the functions in question.
The errors are related to the lib files themselves. The linker is not able
to find various functions that libssl.a is statically linked to. Presumably
they reside in libcrypto.a, which is either being ignored by the linker,
or maybe it is the wrong version and does not contain the functions in question.

You're right, it's not related to Indy, I've just used the header files to import OpenSSL definitions into project.
Indeed those functions are in libcrypto.a and the lib actually contains them, I've checked against nm utility.

Perhaps linker must be fed with full path of the libraries when attempting static linking:

https://wiki.openssl.org/index.php/Android#Static_Library_Linking

In Delphi IDE I see the full command line for dccaarm.exe, where linker and libpath are provided:

..\dccaarm.exe <snip> --libpath:"<snip>." --linker:"<snip>windows\bin\arm-linux-androideabi-ld.exe"

Alas, could not find a way to provide linker with additional options, at least from Delphi IDE.

Invoking ld from command line would possibly allow that? I suspect it would need
a plenty of other options, not listed by dccaarm command line?

--
Angelo

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Linking static version of OpenSSL?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 30, 2016 9:05 AM   in response to: Angelo Soragna in response to: Angelo Soragna
Angelo wrote:

Indeed those functions are in libcrypto.a and the lib actually
contains them, I've checked against nm utility.

Did you verify that the function exports are decorated the way the linker
is expecting?

Alas, could not find a way to provide linker with additional options,
at least from Delphi IDE.

Project > Options > Delphi Compiler > Compiling > Other options > Additional
options to pass to the compiler

--
Remy Lebeau (TeamB)
Angelo Soragna

Posts: 3
Registered: 12/29/16
Re: Linking static version of OpenSSL?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 6, 2016 12:58 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Did you verify that the function exports are decorated the way the linker
is expecting?

Project > Options > Delphi Compiler > Compiling > Other options > Additional
options to pass to the compiler


Hi

the following worked for me:

* defined STATICLOAD_OPENSSL + had Indy rebuilt.

* OpenSSL 1.02j built on Linux. setenv-android.sh was setup for NDK r9c/r9d + arm-linux-androideabi-4.6

* copied .a libraries to .\lib\ folder of a project sample. Folder location should not really matter provided it matches the linker additional options below

* Linker options: Project > Options > Delphi Compiler > Linking > Options passed to LD linker: -L.\lib -lcrypto -L.\lib -lssl

I could run a project sample on android 5 and 4.3 unfortunately I'm currently unable to test it on android 6 and 7.

I 'm not clear about -fPIE -pie flags when building OpenSSL. PIE seems a mandatory requirement for Android 5+. Even if I build the library with/without those flags the Delphi project sample still links successfully and also runs without errors.

I haven't tried with latest Android NDKs

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02