4 A custom dynamic linker for Android programs that adds a few interesting
5 features compared to /system/bin/linker:
7 - Support changing the library search path. The system linker, when used
8 inside Android applications, is limited to the value of LD_LIBRARY_PATH
9 at boot time, that only looks into system directories, not application
12 This linker allows the client to add application paths before the
13 default system ones, this has two benefits:
15 - Library dependencies are loaded automatically in the right order.
17 - Libraries from the application paths are favored over system ones.
18 This avoids conflicts when one of your application's libraries
19 has the same name than a system one (which happens randomly
20 on certain devices due to system application bundling).
22 (Note: The system linker issue above has been fixed in Android 4.3).
24 - Supports any number of shared libraries. On older Android platforms,
25 the system linker will refuse to load more than 64 or 128 libraries
26 in a single process (Note: Fixed in Android 4.3).
28 - Supports loading a library at an explicit (page-aligned) memory
29 address. The system linker always randomizes the address. Note that
30 this is generally a _bad_ idea for security reasons. Consider using
31 this only when using shared RELROs (see below).
33 - Supports loading a library from an explicit (page-aligned) file
34 offset. This can be useful to load a library directly from an .apk,
35 provided that it is uncompressed and at a page-aligned offset.
37 - Support sharing of RELRO sections. When two processes load the same
38 library at exactly the same address, the content of its RELRO section
39 is identical. By default, each instance uses private RAM pages to host
40 it, but it is possible to use a single ashmem region to share the same
43 See include/crazy_linker.h for the API and its documentation.
45 See LICENSE file for full licensing details (hint: BSD)
49 - Do not use this if you don't know what you're doing. Read the API
50 documentation first, and look at the test programs for usage examples.
52 - The crazy linker will always use the system linker to load NDK-exposed
53 system libraries (e.g. liblog.so and others). This avoids having two
54 instances of the same library in the same process, and correctly
55 resolving any symbols from system libraries.
57 - Any library loaded by the crazy linker, and which uses functions of
58 libdl.so will continue to work. However, calls to dlopen(), dlsym(),
59 et al will be redirected to the crazy linker's own wrappers.
61 This ensures that if a library is loaded by the crazy linker, any of
62 its dependencies will be loaded by it too.
64 - Libraries loaded with the crazy linker are visible to GDB, or Breakpad,
65 and stack unwinding / C++ exception propagation should just work.
71 You can't call the crazy_linker code directly from Java in your Android
72 application (it's a static library). You need to put it into your own
73 shared library, loaded with System.loadLibrary() instead (or alternatively,
74 inside your NativeActivity's shared library).
76 Also, libraries loaded with the crazy linker are not visible to the system
77 one. In practice, it means that lazy native method lookup will not work. I.e.:
79 The first time you call a native method like:
81 'mypackage.MyClass.myNativeMethod()'
83 The VM will look into existing native libraries with dlsym() for a
84 function symbol named like:
86 Java_mypackage_MyClass_myNativeMethod
88 This will not work if the symbol is inside a library loaded with the
91 To work-around this, register the native methods explicitely
92 in your JNI_OnLoad() by calling env->RegisterNatives() with the
93 appropriate parameters.
99 1/ Add the following to your module definition in your project's Android.mk:
101 LOCAL_STATIC_LIBRARIES := crazy_linker
103 2/ Also Include the top-level crazy_linker Android.mk, as in:
105 include /path/to/crazy_linker/Android.mk
107 3/ In your C or C++ source:
109 #include <crazy_linker.h>
111 Read the header for API documentation.
113 If your library implements native methods, it must explicitely register
114 them with env->RegisterNatives() before they become usable.
119 - Libraries loaded by the crazy linker are not automatically closed when
122 - dlopen() when called inside a library loaded by the crazy linker doesn't
123 support RTLD_MAIN or RTLD_NEXT.
128 If you modify this code, check your changes by running the test suite using:
131 tests/run-tests.sh crazy_linker
133 See DESIGN.TXT for an overview of the library's design.