Learn about Dynamic Links (6) -- relocate tables and Dynamic Links
Liu Zhi qingqing, Nanfeng smoked, phantom Qi Feng Yao Island, one day of the yellow cloud and white clouds, over there in the middle of the wheat waves, there are agricultural women smile Yin. Ask the garden if the peas are fat or not, and ask the bayberry if there are birds to steal. The roses haven't been too red for days. Mrs Mei is in town today and she has no news. -- Xu Zhimo summer field
Whether it is an executable file or so, as long as it depends on other so (. there are import symbols in the dynsym dynamic symbol table). In the compilation link stage, the addresses of these symbols are unknown, so they can only be relocated in the dynamic link stage.
Note: although the so compiled with PIC is called "address-independent code", it also needs to be relocated. Because for PIC's so, we just put the absolute address in the code into the GOT table of the Data Segment. Therefore, although the Code segment does not need to be relocated, the GOT table of the Data Segment needs to be relocated. (So is compiled with PIC to reuse the so code segment)
Take android liblog. so as an example. When memset is called in the code, it will actually jump to the memory indicated by memset_ptr.
The memory indicated by memset_ptr is defined in the GOT table:
So how to relocate the GOT table? Which memory address should each entry in the GOT table point? This information is described in the relocation table. For android, the relocation table is saved in. rel. dyn and. rel. plt.
From the table, we can see that the relocation items in. rel. dyn are mainly R_ARM_GLOB_DAT type, while those in. rel. plt are mainly R_ARM_JUMP_SLOT type relocation items. The former is used to relocate data references, and the latter is used to relocate function references. In addition, we also see the relocation of the R_ARM_RELATIVE type.
In the source code of android linker, two soinfo_relocate functions are called, namely. rel. dyn and. rel. plt, respectively:
In the source code of the soinfo_relocate function, we can see that for different types of relocation, the calculation of symbol addresses is also different.
In fact, for the R_ARM_GLOB_DAT and R_ARM_JUMP_SLOT relocation types, you only need to fill the symbol address with the corrected memory. The R_ARM_RELATIVE type looks special, and its role is to reset the base address (Rebasing ).
For example, the pointer p points to the static variable a, and the offset of the static variable a relative to the so base address is. During compilation, the base address of so is 0, and the value of p is. When so is loaded into the memory, the value of p needs to add a base address base of so in the memory. R_ARM_RELATIVE type relocation is used to do this.
For the relocation details of android linker and other relocation types, we will describe them later when writing the android linker source code analysis notes.
Learning Materials: programmer self-cultivation-links, loading and libraries