Learn about Dynamic Links (2) -- Address-independent code and Dynamic Links
Remove the part of the instruction that needs to be modified and place it in the Data zone to keep the instruction part unchanged. Each process can have a copy of the data part. This is the Position-independent Code (PIC). The advantage is that the commands are shared by multiple processes to save memory.
To implement PIC, you must solve the address location problem in the instruction. The address reference in the command can be divided:
1. function calls and variable access within the module;
2. function calls and variable access outside the module.
In 1st cases, since we all define in the same module and there is a certain relative position between them, we can solve the problem by calling the relative address.
In 2nd cases, for external access to the module, a global Offset Table (GOT) needs to be created in the data area to store the addresses of external variables or functions. Of course, the addresses in the GOT table need to be filled by the linker when the module is loaded and the addresses are relocated. In this way, you can first find the related items in the GOT table through the relative address for external symbol access in the command, and then obtain the specific address of the target variable or function.
In the Android system, there are two relocation tables for external variables and function references, so we call soinfo_relocate two consecutive times to relocate:
Finally, use "readelf-d xxx. so | grep TEXTREL "command to view xxx. so is PIC, because so does not contain any code segment to relocate the table, so there is no output.
Learning Materials: programmer self-cultivation-links, loading and libraries