Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android – how to load shared library?

I’ve created simplest EXECUTABLE and SHARED_LIBRARY. SHARED_LIBRARY doesn’t get loaded without changing LD_LIBRARY_PATH:

# ./hello
./hello
link_image[1995]: failed to link ./hello
CANNOT LINK EXECUTABLE

# LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./hello
Hello, world!

All code below:

first.h

#ifndef FIRST_H
#define FIRST_H

extern int first(int x, int y);

#endif /* FIRST_H */

first.c

#include "first.h"

int first( int x, int y ) {
    return x + y;
}

hello.c

#include <stdio.h>
#include "first.h"

int main( int argc, char **argv ) {
    printf( "Hello, world!\n" );
    first( 1000, 24 );
    return 0;
}

Android.mk

include $(CLEAR_VARS)
LOCAL_MODULE    := first
LOCAL_SRC_FILES := first.c
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_SRC_FILES := hello.c
LOCAL_SHARED_LIBRARIES := first
LOCAL_LDFLAGS := -Wl,-rpath,. -Wl,-rpath,/data/data/testlib/lib
include $(BUILD_EXECUTABLE)

readelf --all hello

...
Dynamic section at offset 0xef4 contains 25 entries:
Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  INTERP         0x000154 0x00008154 0x00008154 0x00013 0x00013 R   0x1
      [Requesting program interpreter: /system/bin/linker]
...
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libfirst.so]
 0x00000001 (NEEDED)                     Shared library: [libstdc++.so]
 0x00000001 (NEEDED)                     Shared library: [libm.so]
 0x00000001 (NEEDED)                     Shared library: [libc.so]
 0x00000001 (NEEDED)                     Shared library: [libdl.so]
 0x0000000f (RPATH)                      Library rpath: [.:/data/data/testlib/lib]

RPATH is here, but linker doesn’t use it for some reason.

Apparently dynamic linker works just fine on Android (with LD_LIBRARY_PATH and it’s no different RPATH either)

What do I do wrong?

Am I missing something obvious?

RPATH has two directories in my example, (.:/data/data/testlib/lib), one (.) should be sufficient.

There is no Java in this example. It’s not used and not needed for the project.

Basically I’m looking for a standard way to load shared libraries from "my directory" without changing LD_LIBRARY_PATH (it’s not possible sometimes) or using wrapper to dlopen all required libs.

like image 626
Same old guy... Avatar asked Sep 28 '12 02:09

Same old guy...


1 Answers

The Bionic linker-loader (android/bionic/linker/linker.c in the AOSP source, if you want to check) seems to completely ignore RPATH in the ELF. It only considers LD_LIBRARY_PATH and a hardcoded array of "/vendor/lib" and "/system/lib".

This is based on a cursory scan of the code on the Ice Cream Sandwich tree I have checked out.

This would explain the behavior you are seeing.

like image 73
Yuriy Romanenko Avatar answered Oct 21 '22 08:10

Yuriy Romanenko