Loading and dynamic link process of Android native library
Loading process of native library
We start with a simple NDK demo.
Previous work of Android linker linker
Let's start with system. Loadlibrary ().
Let's look at loadlibrary0 ()
The parameter loader is an Android application class loader, which is an object of PathClassLoader type and inherits from the basedexclassloader object. Let's look at the findlibrary () method of basedexclassloader.
Let's look at the findlibrary () method of dexpathlist
Go back to loadlibrary0 (). With the full path name of the dynamic library, you can load the library. Let's look at doload ().
Nativeload() finally calls loadnativelibrary(), which is analyzed directly below.
For JNI registration, it will not be discussed here. Let's look at the implementation of opennativelibrary ().
Let's look at Android_ dlopen_ Implementation of ext()
The next step is the work of the Android linker linker.
Loading of Android linker linker
Let's start with do_ Dlopen() starts parsing.
find_ Library() parameter translated_ When name is not empty, call find directly_ Libraries (), which is the key function of loading links, let's see its implementation.
find_ The loading of dynamic libraries in Libraries () can be divided into two parts
Let's start with find_ library_ Internal() starts the analysis.
Load is analyzed below_ library()
Let's look at another load_ Implementation of library()
Let's analyze the reading process of ELF file header and segment information, that is, loadtask's read(), which directly calls elfleader's read() method.
In the actual address calculation, use si_ - > load_ Bias, do not use si_ - > base。
Let's look at the load () method of elfleader
Reserveaddressspace(): keep the virtual address space as dynamic library (load address randomization) loadsegments(): load loadable segments in ELF file findphd(): ensure that the program header table is included in a loadable segment
The loading of the dynamic library has been completed. Let's see the link process.
Dynamic linking process of native library
Pre link
Let's look at prelink_ image()
link
Link is mainly used to complete symbol relocation. Here is link_ Image() start profiling
Next, take the relocation of function references as an example to analyze the relocate () method
reference resources
Tool Interface Standard (TIS) Executable and Linking Format (ELF) Specification
summary
The above is the loading and dynamic linking process of Android native library introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!