5.3.4 · HinglishBuild Systems & Toolchain

Dynamic - shared libraries — .so - .dll, dynamic linking, PIC

1,909 words9 min readRead in English

5.3.4 · Coding › Build Systems & Toolchain


Shared libraries KYO exist karte hain?

Teen wajahaat jis se dynamic linking jeet gayi:

  1. Disk + RAM ki bachat. Ek copy of libc (~2 MB) 200 processes ke beech share hoti hai 200 copies ki jagah. OS same physical pages ko har process mein map karta hai — lekin sirf read-only code pages, writable data nahi.
  2. Recompile kiye bina update. libssl.so mein security bug patch karo; har program agली launch pe wo pick kar leta hai. Static linking mein poori duniya ko relink karna padta.
  3. Plugin architecture. dlopen() / LoadLibrary() ek program ko woh code load karne dete hain jo compile time pe usse pata nahi tha.

Position-Independent Code (PIC) KYA hai?

PIC global data aur functions tak KAISE pahuncha hai?

Relative addressing internal jumps ke liye easy hai. Mushkil hissa hai external symbols (kisi doosri library ka function, ya global variable). Do tables isko solve karte hain:

Figure — Dynamic - shared libraries — .so  -  .dll, dynamic linking, PIC

Dynamic linking KAISE hota hai — step by step


Derivation: code page kyun constant rehna chahiye


Flashcards

Static aur dynamic linking mein ek-line difference kya hai?
Static library code ko executable mein copy karta hai; dynamic ek reference rakhta hai aur code ko run time pe alag .so/.dll se load karta hai.
Shared-library code position-independent kyun hona chahiye?
Taaki identical read-only code pages alag-alag processes mein alag virtual addresses pe map kiye ja sakein aur phir bhi correctly execute ho sakein, physical RAM sharing enable karte hue.
GCC/Clang mein PIC compile karne ka flag kya hai?
-fPIC (aur .so produce karne ke liye -shared).
GOT kya store karta hai aur yeh writable kyun hai?
External functions/data ke pointers (real addresses); yeh writable hai taaki dynamic loader load/run time pe correct per-process addresses patch kar sake.
PLT kya hai aur "lazy binding" kya hota hai?
Stubs ka ek table jo function ka real address uski pehli call pe resolve karta hai, GOT mein store karta hai, phir jump karta hai — baad ki calls resolution skip karti hain. Lazy = pehle use pe resolve karo, startup pe nahi.
PIC library mein per-process absolute address actually kahan rehta hai?
GOT mein (ek data page), code page mein kabhi nahi.
Windows pe GOT ka role kaun play karta hai?
Import Address Table (IAT), loader dwara patch hoti hai.
Dynamic linking startup cost ke bawajood poore system ke liye faster kyun ho sakti hai?
Shared code pages often already resident/cached hote hain kyunki doosre processes unhe use karte hain, RAM aur page-cache pressure bachaate hue.
Linux pe startup pe dynamic linking kaun perform karta hai?
Dynamic linker/loader ld-linux.so, executable ke interpreter ke roop mein named.
Link-time success run-time loading guarantee kyun nahi karta?
Link time sirf verify karta hai ki symbol exist karta hai aur dependency name record karta hai; run time mein search paths mein .so file ko physically dhundna zaroori hai.

Recall Feynman: ek 12-saal ke bacche ko explain karo

Socho 30 bacchon ko sab same recipe book chahiye. 30 baar photocopy karne ki jagah (kaagaz barbad = static linking), school library mein ek book rakhta hai aur har baccha bas yaad rakhta hai "library mein jaake dekh lo" (dynamic linking). Lekin bacchon ke classroom building ke alag-alag hisson mein hain, toh book "room 12 ke baad left lo" nahi keh sakti (yeh depend karta hai tum kahan ho). Isliye woh kehti hai "jahan tum khade ho wahan se 2 doors baad left lo" — directions jo kahin se bhi kaam karein. Woh self-relative directions hi PIC hain. Aur har baccha ek chhota sticky note (GOT) rakhta hai jisme exact page numbers sirf unke liye fill hote hain.


Connections

  • Static libraries — .a .lib — alternative; size, updatability, page cache mein trade-offs.
  • The Linker — symbol resolution & relocation — relocations woh hain jo GOT fill karte hain.
  • Virtual Memory & mmap — shared read-only pages woh mechanism hai jo RAM savings ke peeche hai.
  • ASLR & Security — PIC libraries (aur PIE executables) ki address randomization bhi enable karta hai.
  • dlopen and Plugin Architectures — compile time pe unknown code ka run-time loading.
  • ELF and PE file formatsDT_NEEDED, GOT/PLT sections vs Windows IAT.

Concept Map

copies code into

keeps only reference

resolved at

one copy shared in

enables

patched without relink

loaded via dlopen

requires

compiled with

uses only

works at any

allows read-only sharing of

Static linking .a .lib

Fat self-contained binary

Dynamic linking .so .dll

Shared library file

Load time or first call

Shared RAM pages

Disk and RAM savings

Update without recompiling

Plugin architecture

Position-Independent Code

-fPIC flag

Relative addressing

Load address