Shared Libraries in Android.

846 views
Skip to first unread message

F H

unread,
Jan 6, 2009, 10:30:31 AM1/6/09
to android-...@googlegroups.com
I'm building a shared library using a separate build environment to Android, though I'm using the Android tools and bionic etc - and as far as I can tell the same build options used when a shared library is built in the Android environment.  I then copy this library into the out/....../system/lib directory before building Android - I can confirm that the library is added to the image when running the emulator and looking at whats in /system/lib using adb shell.
 
The issue though is that an JNI interface which invokes dlopen on the named library fails to find it (though it will find and load other .so files in the system/lib directory). The subsequent error reported is "Cannot find Library" (though it's visible from adb shell).
 
Any Ideas what step may be missing? does dlopen only open certain designated libraries?
 

David Turner

unread,
Jan 6, 2009, 10:34:25 AM1/6/09
to android-...@googlegroups.com
dlopen() doesn't check the names of the libraries that it tries to open. there is probably something weird in the way you
generate your shared library.

just another reason to use the Android toolchain to build these things.

F H

unread,
Jan 6, 2009, 10:54:00 AM1/6/09
to android-...@googlegroups.com
Thanks David,
 
I suspect as much. Problem is I have two big build systems each with their own sets of vairiables etc.  So provided the library is just built correctly then what I'm doing ought to just work (i.e. there isn't some weird post-processing stage in the Android build for example that post-processes the shared libraries in some way or builds some kind of index file for accessing the libraries?).

David Turner

unread,
Jan 6, 2009, 3:37:31 PM1/6/09
to android-...@googlegroups.com
Android uses a custom prelinker tool to reduce the size of the system libraries.

on the other hand, the dynamic linker should (in theory) be able to deal with non-prelinked libraries.
in practice, our linker is known to not implement certain ELF relocations that are generated by other
toolchains. I suspect this may be one of the problem you're facing.

can you provide a tiny shared library file to download somewhere that I could look at
(one that doesn't link obviously)

leemgs

unread,
Jan 6, 2009, 7:12:14 PM1/6/09
to android-...@googlegroups.com
Hi~ Davide Turner.
Thank you for technical information of android prelink.

F H ~ . I have one idea for you.
If you are interested in prelink knowledge by Jakub Jelinek,
I recommend that you will read http://people.redhat.com/jakub/prelink.pdf.

and, If you are want to modify android source for custom your toolchain,
Analize prelink related source like a belows.

./build/core/prelink-linux-arm.map
./build/core/shared_library.mk
./bionic/linker/linker.c.
./build/tools/apriori/prelinkmap.c (prelinkmap.h)

1. ./build/core/prelink-linux-arm.map

-------------------------------------
# 0xC0000000 - 0xFFFFFFFF Kernel
# 0xB0100000 - 0xBFFFFFFF Thread 0 Stack
# 0xB0000000 - 0xB00FFFFF Linker
# 0xA0000000 - 0xBFFFFFFF Prelinked System Libraries
# 0x90000000 - 0x9FFFFFFF Prelinked App Libraries
# 0x80000000 - 0x8FFFFFFF Non-prelinked Libraries
# 0x40000000 - 0x7FFFFFFF mmap'd stuff
# 0x10000000 - 0x3FFFFFFF Thread Stacks
# 0x00000000 - 0x0FFFFFFF .text / .data / heap

-------------------------------------


2. ./build/core/shared_library.mk
Standard rules for building a normal shared library.
Additional inputs from base_rules.make:None.
LOCAL_MODULE_SUFFIX will be set for you
------------------------------
ifeq ($(strip $(LOCAL_PRELINK_MODULE)),)
LOCAL_PRELINK_MODULE := $(strip $(TARGET_PRELINK_MODULE))
endif
------------------------------


3. ./bionic/linker/linker.c
---------------------------
>>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<

Do NOT use malloc() and friends or pthread_*() code here.
Don't use printf() either; it's caused mysterious memory
corruption in the past.
The linker runs before we bring up libc and it's easiest
to make sure it does not depend on any complex libc features

open issues / todo:

- should we do anything special for STB_WEAK symbols?
- are we doing everything we should for ARM_COPY relocations?
- cleaner error reporting
- configuration for paths (LD_LIBRARY_PATH?)
- after linking, set as much stuff as possible to READONLY
and NOEXEC
- linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
headers provide versions that are negative...
- allocate space for soinfo structs dynamically instead of
having a hard limit (64)

features to add someday:

- dlopen() and friends
---------------------------


4. ./build/tools/apriori/prelinkmap.c
-----------------------------------------------------
These values limit the address range within which we prelinked libraries
reside. The limit is not set in stone, but should be observed in the
prelink map, or the prelink step will fail.

#define PRELINK_MIN 0x90000000
#define PRELINK_MAX 0xB0000000
-----------------------------------------------------

2009/1/7 David Turner <di...@android.com>:

Reply all
Reply to author
Forward
0 new messages