Position Independent Code
abhinav Ashok kumar
Curating Insights & Innovating in GPU Compiler | Performance Analyst at Qualcomm | LLVM Contributor | Maintain News Letter | AI/ML in Compiler
We all are aware about the concept that the shared library can be used by multiple running processes at same time. But how multiple processes shared a single copy of the shared library?
One way is that,
Assign some customize chunk of the memory to each shared library.
But if you think a little this approach will leads to serious problem. Some of the problem which i can think off is:-
To avoid all these problem modern system use the code Segement. Modern system compile the code system of the shared library or modules so that this can be load and run from anywhere from the memory without modify by the linker . Ofcourse if library is created by us we have to give the path of the place where library is their to the loaders.
With this method single copy of the shared library's code segmment is shared between infinite number of the process.
领英推荐
Note:- Here each process will get its own copy of the read/write data segment
Position Independent Code(PIC) as name suggest is a code which does not depend on position. At these point question arises
Position of What ?
Position of the location of loading the code.
PIC can be defined as a code that can be loaded without worrying about any relocation.
We can use directly use -fPIC option in gcc to create PIC. Shared library must always use this compilation option.
linux>gcc -fPIC source.c -o out
On x86-64 machine,
their is no special treatment required for the references of symbol in same executable to be PIC.
But reference to the external symbol (function , global variables) requires some special treatment which we will discuss it in next week.
Thank for information ??