xref: /aosp_15_r20/art/libnativeloader/README.md (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Workerlibnativeloader
2*795d594fSAndroid Build Coastguard Worker===============================================================================
3*795d594fSAndroid Build Coastguard Worker
4*795d594fSAndroid Build Coastguard WorkerOverview
5*795d594fSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
6*795d594fSAndroid Build Coastguard Workerlibnativeloader is responsible for loading native shared libraries (`*.so`
7*795d594fSAndroid Build Coastguard Workerfiles) inside the Android Runtime (ART). The native shared libraries could be
8*795d594fSAndroid Build Coastguard Workerapp-provided JNI libraries or public native libraries like `libc.so` provided
9*795d594fSAndroid Build Coastguard Workerby the platform.
10*795d594fSAndroid Build Coastguard Worker
11*795d594fSAndroid Build Coastguard WorkerThe most typical use case of this library is calling `System.loadLibrary(name)`.
12*795d594fSAndroid Build Coastguard WorkerWhen the method is called, the ART runtime delegates the call to this library
13*795d594fSAndroid Build Coastguard Workeralong with the reference to the classloader where the call was made. Then this
14*795d594fSAndroid Build Coastguard Workerlibrary finds the linker namespace (typically with the name `clns-` followed by
15*795d594fSAndroid Build Coastguard Workera number to make it unique) that is associated with the given classloader, and
16*795d594fSAndroid Build Coastguard Workertries to load the requested library from that namespace. The actual searching,
17*795d594fSAndroid Build Coastguard Workerloading, and linking of the library is performed by the dynamic linker.
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard WorkerThe linker namespace is created when an APK is loaded into the process, and is
20*795d594fSAndroid Build Coastguard Workerassociated with the classloader that loaded the APK. The linker namespace is
21*795d594fSAndroid Build Coastguard Workerconfigured so that only the JNI libraries embedded in the APK is accessible
22*795d594fSAndroid Build Coastguard Workerfrom the namespace, thus preventing an APK from loading JNI libraries of other
23*795d594fSAndroid Build Coastguard WorkerAPKs.
24*795d594fSAndroid Build Coastguard Worker
25*795d594fSAndroid Build Coastguard WorkerThe linker namespace is also configured differently depending on other
26*795d594fSAndroid Build Coastguard Workercharacteristics of the APK such as whether or not the APK is bundled with the
27*795d594fSAndroid Build Coastguard Workerplatform. In case of the unbundled, i.e., downloaded or updated APK, only the
28*795d594fSAndroid Build Coastguard Workerpublic native libraries that is listed in `/system/etc/public.libraries.txt`
29*795d594fSAndroid Build Coastguard Workerare available from the platform, whereas in case of the bundled, all libraries
30*795d594fSAndroid Build Coastguard Workerunder `/system/lib` are available (i.e. shared). In case when the unbundled
31*795d594fSAndroid Build Coastguard Workerapp is from `/vendor` or `/product` partition, the app is additionally provided
32*795d594fSAndroid Build Coastguard Workerwith the [VNDK-SP](https://source.android.com/devices/architecture/vndk#sp-hal)
33*795d594fSAndroid Build Coastguard Workerlibraries. As the platform is getting modularized with
34*795d594fSAndroid Build Coastguard Worker[APEX](https://android.googlesource.com/platform/system/apex/+/refs/heads/master/docs/README.md),
35*795d594fSAndroid Build Coastguard Workersome libraries are no longer provided from platform, but from the APEXes which
36*795d594fSAndroid Build Coastguard Workerhave their own linker namespaces. For example, ICU libraries `libicuuc.so` and
37*795d594fSAndroid Build Coastguard Worker`libicui18n.so` are from the I18n APEX.
38*795d594fSAndroid Build Coastguard Worker
39*795d594fSAndroid Build Coastguard WorkerThe list of public native libraries is not static. The default set of libraries
40*795d594fSAndroid Build Coastguard Workerare defined in AOSP, but partners can extend it to include their own libraries.
41*795d594fSAndroid Build Coastguard WorkerCurrently, following extensions are available:
42*795d594fSAndroid Build Coastguard Worker
43*795d594fSAndroid Build Coastguard Worker- `/vendor/etc/public.libraries.txt`: libraries in `/vendor/lib` that are
44*795d594fSAndroid Build Coastguard Workerspecific to the underlying SoC, e.g. GPU, DSP, etc.
45*795d594fSAndroid Build Coastguard Worker- `/{system|product}/etc/public.libraries-<companyname>.txt`: libraries in
46*795d594fSAndroid Build Coastguard Worker`/{system|product}/lib` that a device manufacturer has newly added. The
47*795d594fSAndroid Build Coastguard Workerlibraries should be named as `lib<name>.<companyname>.so` as in
48*795d594fSAndroid Build Coastguard Worker`libFoo.acme.so`.
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard WorkerNote that, due to the naming constraint requiring `.<companyname>.so` suffix, it
51*795d594fSAndroid Build Coastguard Workeris prohibited for a device manufacturer to expose an AOSP-defined private
52*795d594fSAndroid Build Coastguard Workerlibrary, e.g. libgui.so, libart.so, etc., to APKs.
53*795d594fSAndroid Build Coastguard Worker
54*795d594fSAndroid Build Coastguard WorkerLastly, libnativeloader is responsible for abstracting the two types of the
55*795d594fSAndroid Build Coastguard Workerdynamic linker interface: `libdl.so` and `libnativebridge.so`. The former is
56*795d594fSAndroid Build Coastguard Workerfor non-translated, e.g. ARM-on-ARM, libraries, while the latter is for
57*795d594fSAndroid Build Coastguard Workerloading libraries in a translated environment such as ARM-on-x86.
58*795d594fSAndroid Build Coastguard Worker
59*795d594fSAndroid Build Coastguard WorkerImplementation
60*795d594fSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
61*795d594fSAndroid Build Coastguard WorkerImplementation wise, libnativeloader consists of four parts:
62*795d594fSAndroid Build Coastguard Worker
63*795d594fSAndroid Build Coastguard Worker- `native_loader.cpp`
64*795d594fSAndroid Build Coastguard Worker- `library_namespaces.cpp`
65*795d594fSAndroid Build Coastguard Worker- `native_loader_namespace.cpp`
66*795d594fSAndroid Build Coastguard Worker- `public_libraries.cpp`
67*795d594fSAndroid Build Coastguard Worker
68*795d594fSAndroid Build Coastguard Worker`native_loader.cpp` implements the public interface of this library. It is just
69*795d594fSAndroid Build Coastguard Workera thin wrapper around `library_namespaces.cpp` and `native_loader_namespace.cpp`.
70*795d594fSAndroid Build Coastguard Worker
71*795d594fSAndroid Build Coastguard Worker`library_namespaces.cpp` implements the singleton class `LibraryNamespaces` which
72*795d594fSAndroid Build Coastguard Workeris a manager-like entity that is responsible for creating and configuring
73*795d594fSAndroid Build Coastguard Workerlinker namespaces and finding an already created linker namespace for a given
74*795d594fSAndroid Build Coastguard Workerclassloader.
75*795d594fSAndroid Build Coastguard Worker
76*795d594fSAndroid Build Coastguard Worker`native_loader_namespace.cpp` implements the class `NativeLoaderNamespace` that
77*795d594fSAndroid Build Coastguard Workermodels a linker namespace. Its main job is to abstract the two types of the
78*795d594fSAndroid Build Coastguard Workerdynamic linker interface so that other parts of this library do not have to know
79*795d594fSAndroid Build Coastguard Workerthe differences of the interfaces.
80*795d594fSAndroid Build Coastguard Worker
81*795d594fSAndroid Build Coastguard Worker`public_libraries.cpp` is responsible for reading `*.txt` files for the public
82*795d594fSAndroid Build Coastguard Workernative libraries from the various partitions. It can be considered as a part of
83*795d594fSAndroid Build Coastguard Worker`LibraryNamespaces` but is separated from it to hide the details of the parsing
84*795d594fSAndroid Build Coastguard Workerroutines.
85