Question

I'm trying to build a dynamic library for android wrapper around libcrypto.a as described in

http://wiki.openssl.org/index.php/FIPS_Library_and_Android#Using_FIPS_OpenSSL_in_a_real_Application

I've produced the libcrypto.a as suggested on the page, but when I try to compile my wrapper.c I get a linker error, multiple definition of 'atexit'

Here's the command line I'm using to compile:

arm-linux-androideabi-gcc wrapper.c -fPIC -shared -I/usr/local/ssl/android-14/include -Wl,-Bstatic -lcrypto -L/usr/local/ssl/android-14/lib -o libwrapper.so --sysroot=/Users/scoleman/android-ndk-r9d/platforms/android-14/arch-arm -Wl, -Bdynamic

Here are the results:

/Users/scoleman/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: /Users/scoleman/android-ndk-r9d/platforms/android-14/arch-arm/usr/lib/libc.a(atexit.o): multiple definition of 'atexit'
/Users/scoleman/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: /Users/scoleman/android-ndk-r9d/platforms/android-14/arch-arm/usr/lib/crtbegin_so.o: previous definition here
/Users/scoleman/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
collect2: ld returned 1 exit status

Here's my wrapper.c:

#include <string.h>
#include <jni.h>

int MY_FIPS_mode()   {
   int mode = mode = FIPS_mode();
   return mode;
}
Was it helpful?

Solution

Here's the command line I'm using to compile:

arm-linux-androideabi-gcc wrapper.c -fPIC -shared -I/usr/local/ssl/android-14/include -Wl,-Bstatic -lcrypto -L/usr/local/ssl/android-14/lib -o libwrapper.so --sysroot=/Users/scoleman/android-ndk-r9d/platforms/android-14/arch-arm -Wl, -Bdynamic

I was able to duplicate the issue with your command line. The way I would approach this is (line breaks added for readability):

arm-linux-androideabi-gcc wrapper.c -fPIC -shared -o libwrapper.so
    --sysroot=.../android-ndk-r9d/platforms/android-14/arch-arm
    -I/usr/local/ssl/android-14/include
    /usr/local/ssl/android-14/lib/libssl.a
    /usr/local/ssl/android-14/lib/libcrypto.a
    -ldl

--sysroot will bring in the platform headers and libraries for Android 4.0 (API 14). That should be where atexit is defined. I think the -Bstatic and -Bdynamic might be complicating things since atexit was provided by both libc.a and crtbegin_so.o.

I avoid -Bstatic and -Bdynamic. When I want static linking, I specifically call out the full pathname of the static library, like /usr/local/ssl/android-14/lib/libcrypto.a. Remember, an archive is a collection of object files (*.o), so you can use it wherever you can use an object file.


Using the setenv-android.sh script from the page and your source, I was not able to duplicate:

$ . ./setenv-android.sh 
ANDROID_NDK_ROOT: /opt/android-ndk-r9
ANDROID_EABI: arm-linux-androideabi-4.6
ANDROID_API: android-14
ANDROID_SYSROOT: /opt/android-ndk-r9/platforms/android-14/arch-arm
ANDROID_TOOLCHAIN: /opt/android-ndk-r9/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin
CROSS_COMPILE: arm-linux-androideabi-
ANDROID_DEV: /opt/android-ndk-r9/platforms/android-14/arch-arm/usr

$ cat wrapper.c 
#include <string.h>
#include <jni.h>
#include <openssl/evp.h>

int MY_FIPS_mode()   {
    int mode = FIPS_mode();
    return mode;
}

$ arm-linux-androideabi-gcc wrapper.c -fPIC -shared -o libwrapper.so \
>     --sysroot=$ANDROID_SYSROOT \
>     -I/usr/local/ssl/android-14/include \
>     /usr/local/ssl/android-14/lib/libssl.a \
>     /usr/local/ssl/android-14/lib/libcrypto.a \
>     -ldl

$ ls
libwrapper.so       setenv-android.sh   wrapper.c

$ arm-linux-androideabi-readelf -h libwrapper.so 
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          244660 (bytes into file)
  Flags:                             0x5000000, Version5 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         7
  Size of section headers:           40 (bytes)
  Number of section headers:         34
  Section header string table index: 33

$ arm-linux-androideabi-nm -D libwrapper.so | grep MY_FIPS_mode
00009fa4 T MY_FIPS_mode
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top