Position-independent code When a program is in use in several different address spaces, the operating system can usually load the program at the same place in each of the address spaces in which it appears. This makes the linker’s job much easier, since it can bind all of the addresses in the program to fixed locations, and no relocation need be done at the time the program is loaded. Shared libraries complicate this situation considerably.In some simple shared library designs, each library is assigned a globally unique memory address either at system boot time or at the time the libraries are created.This puts the each library at a fixed address, but at the cost of creating a serious ottleneck to shared library administration, since the global list of library memory addresses has to be intained by the system manager.Furthermore, if a new version of a library appears that is larger than the revious version and doesn’t fit into the address space assigned, the entire set of shared libraries and, potentially, all of the programs that reference them, may need to be relinked. The alternative is to permit different programs to map a library to different places in the address space. This eases library administration, but the compiler, and linker, and program loader need to cooperate so that the library will work regardless of where in the address space the library appears. One simple approach is to include standard relocation information with the library, and when the library is mapped into each address space, the loader can fix up any relocatable addresses in the program to reflect the loaded addresses. Unfortunately, the process of fixing up involves writing into the library’s code and data, which means that the pages will no longer be shared, if they’re mapped COW, or the program will crash if the pages are mapped RO. To avoid this problem, shared libraries use Position Independent Code (PIC), code which will work regardless of where in memory it is loaded. All the code in shared libraries is usually PIC, so the code can be mapped read-only. Data pages still usually contain pointers which need relocation, but since data pages are mapped COW anyway, there’s little sharing lost. |