![indirection redirection memory indirection redirection memory](https://electricalworkbook.com/wp-content/uploads/2018/08/c_pointers_start-300x230.png)
One of the key insights on which PIC relies is the offset between the text and data sections, known to the linker at link-time. Key insight #1 - offset between text and data sections
![indirection redirection memory indirection redirection memory](https://i.stack.imgur.com/HgAb3.png)
Moreover, having a writable text section (it must be kept writable, to allow the dynamic loader to perform the relocations) poses a security risk, making it easier to exploit the application.Īs we'll see in this article, PIC mostly mitigates these problems. Different applications won't be able to really share it. Therefore, for each application that loaded this shared library, it will have to be wholly placed in RAM again. But this is not possible with load-time relocation, since when using this technique the text section has to be modified at load-time to apply the relocations.
INDIRECTION REDIRECTION MEMORY CODE
If the text section (where the code is) of the shared library can only be loaded into memory once (and then mapped into the virtual memories of many processes), considerable amounts of RAM can be saved. Some common shared libraries are used by multiple applications. One of the main points of having shared libraries in the first place, is saving RAM. Second, the non-shareable text section problem, which is somewhat more serious.
INDIRECTION REDIRECTION MEMORY SOFTWARE
But if a complex piece of software loads multiple large shared libraries at start-up, and each shared library must first have its load-time relocations applied, these costs can build up and result in a noticeable delay in the start-up time of the application. You may think that the cost shouldn't be too large - after all, the loader doesn't have to scan through the whole text section - it should only look at the relocation entries.
![indirection redirection memory indirection redirection memory](https://bayanbox.ir/view/581244719208138556/virtual-memory.jpg)
If a shared library was linked with load-time relocation entries, it will take some time to actually perform these relocations when the application is loaded. Load-time relocation has a couple of problems: it takes time to perform, and it makes the text section of the library non-shareable.įirst, the performance problem. PIC, however, is much more popular nowadays, and is usually the recommended method of building shared libraries. The code for this post is available online.Īs we've seen in the previous article, load-time relocation is a fairly straightforward method, and it works. A future (hopefully much shorter)Īrticle will build upon the foundation of this one to explain how PIC is Implementing PIC on it is a bit trickier. Specifically because (unlike 圆4) it wasn't designed with PIC in mind, so It will explain only how PIC works on x86, picking this older architecture x86-64) in thisĪrticle, but as it grew longer and longer I decided it won't be practical. I originally planned to focus on both x86 and 圆4 (a.k.a. Here, I want to explain the second approach - PIC.
![indirection redirection memory indirection redirection memory](https://www.verywellmind.com/thmb/4gUwiGz1_sLdKuDsHUpJFZ0DHR4=/400x250/filters:no_upscale():max_bytes(150000):strip_icc()/GettyImages-1129860760-5c72f04046e0fb00014ef61a.jpg)
Load-time relocation was already covered. There are two main approaches to solve this problem in Linux ELF shared libraries: This creates a problem for the data and code references within the library, which should be somehow made to point to the correct memory locations. Briefly, when the linker creates a shared library, it doesn't know in advance where it might be loaded. I've described the need for special handling of shared libraries while loading them into the process's address space in a previous article.