Position Independent Code

Position Independent Code


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:-

  1. Inefficient use of the address space. (Space will be allocated even if we are not using library.)
  2. Difficult to manage.(We have to always make sure that none of the space or chunk of memory given to the shared library overlapped).
  3. Every time when we do changes inside the library we have to put an extra effort to make shared library fit in its address space. If it does not fit we have to find space for it.
  4. When new library is created we have to find a new chunk of memory for it.
  5. We cant avoid internal and external fragementation here.


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.

No alt text provided for this image

Thank for information ??

要查看或添加评论,请登录

abhinav Ashok kumar的更多文章

  • Exploring the Mathematical Foundations of Data Structures and Algorithms

    Exploring the Mathematical Foundations of Data Structures and Algorithms

    ?? Read the Full Guide Here Subscribe to compilersutra for getting the latest update https://www.compilersutra.

    1 条评论
  • Parallel programming Evolution

    Parallel programming Evolution

    Parallel programming has revolutionized how we leverage modern computing power! From instruction-level parallelism…

  • LLVM vs. GCC: A Comprehensive Comparison

    LLVM vs. GCC: A Comprehensive Comparison

    When it comes to compiling C, C++, and other languages, LLVM and GCC are two of the most widely used compiler…

  • Exploring TVM for Beginners: A Must-Read Guide for Compiler Enthusiasts

    Exploring TVM for Beginners: A Must-Read Guide for Compiler Enthusiasts

    For those diving into machine learning compilers, TVM is a powerful tool that optimizes deep learning models for…

  • Optimizing LLVM Passes: Understanding Pass Execution Time

    Optimizing LLVM Passes: Understanding Pass Execution Time

    Optimizing LLVM passes is crucial for improving performance and efficiency for compiler engineers. A key aspect of this…

  • CPP MCQ Stack

    CPP MCQ Stack

    Welcome to Compiler Sutra — the place to be if you want to improve at C++ and compilers! Link :…

    1 条评论
  • Disabling LLVM Pass

    Disabling LLVM Pass

    ?? Disabling an LLVM Pass for Custom Compiler Modifications ?? LLVM is at the core of many modern compilers, and its…

    1 条评论
  • How LLVM Solve Traditional Compiler Problem m*n

    How LLVM Solve Traditional Compiler Problem m*n

    LLVM (Low-Level Virtual Machine) is a compiler framework that helps compiler developers to transform and build…

  • Pass In LLVM To Count the Number of Instructions in It

    Pass In LLVM To Count the Number of Instructions in It

    You can read the full tutorial here: Read the Full Tutorial This tutorial explores FunctionCount.cpp, a practical…

  • Unlocking C++11 part 2

    Unlocking C++11 part 2

    Hello, Tech Enthusiasts Here is the link for the Unlocking C++11 Part 1 The C++11 standard has transformed how we write…

    1 条评论

社区洞察

其他会员也浏览了