5.5.17 · D1 · HinglishEmbedded Systems & Real-Time Software

FoundationsLinker scripts — memory regions, sections (.text, .data, .bss)

2,429 words11 min read↑ Read in English

5.5.17 · D1 · Coding › Embedded Systems & Real-Time Software › Linker scripts — memory regions, sections (.text, .data, .bs

Parent note padhne se pehle tumhe ek choti si vocabulary chahiye. Neeche, har symbol aur term plain words mein define ki gayi hai, picture ki tarah draw ki gayi hai, aur justify kiya gaya hai ki kyun topic uske bina aage nahi badh sakta. Upar se neeche padho — har item uske upar waale pe lean karta hai.


1. Memory ek numbered boxes ki row ke roop mein

Sab kuch yahan se shuru hota hai. Memory koi mysterious cloud nahi hai — yeh tiny boxes ki ek lambi line hai, har box mein ek byte (8 bits) hoti hai, aur har box ka ek number hota hai jise uska address kehte hain.

Figure — Linker scripts — memory regions, sections (.text, .data, .bss)
  • 0x prefix ka matlab hai "aane waale digits hex hain."
  • Hex digits 0 1 2 3 4 5 6 7 8 9 A B C D E F hote hain (A = ten, F = fifteen).
  • Har address ek byte point karta hai; uske baad wala box address + 1 hai.

Topic ko yeh kyun chahiye: poora linker script addresses ki ek list hai — ORIGIN = 0x08000000 ka literal matlab hai "Flash ka pehla box hex mein eight-oh-million number par hai." Addresses ke bina koi map nahi hai.


2. Do tarah ki memory: Flash aur RAM

Boxes ki yeh line actually do physically alag chips mein split hoti hai, aur yeh ek attic aur ek desk ki tarah behave karti hain.

Figure — Linker scripts — memory regions, sections (.text, .data, .bss)

Key words jo tumhare paas hone chahiye:

  • Volatile yahan ka matlab hai "bina power ke contents kho dena" (ek memory property). Note: yeh C keyword volatile se alag hai jo Volatile keyword & memory-mapped IO mein hai — same word, alag meaning. Inhe confuse mat karo.
  • Non-volatile = contents hamesha rakhta hai.

3. ORIGIN aur LENGTH — memory kahan se shuru hoti hai aur kitni badi hai

Ek region poori tarah do numbers se describe hoti hai.

Topic ko yeh kyun chahiye: har linker script mein MEMORY { FLASH : ORIGIN=..., LENGTH=... } block literally yahi do numbers hain per region. Agar tumhara data se zyada ho jaaye, linker "region overflowed" error throw karta hai — yeh check tabhi possible hai jab tumne use bataya ho.


4. Location counter . — ek moving cursor

Jab linker kisi region ko fill karta hai, toh woh sections ko ek ke baad ek shelf par books ki tarah rakhta hai. Woh track karta hai ki abhi kahan hai ek single special symbol se: the dot.

Figure — Linker scripts — memory regions, sections (.text, .data, .bss)

Topic ko yeh kyun chahiye: _sdata = .; ... *(.data*) ... _edata = .; jaisi lines cursor ko data place karne se pehle aur baad mein capture karti hain, toh difference exactly .data ka size hota hai.


5. Symbols — woh names jo linker addresses se jodta hai

Parent mein har jagah use hone wala naming pattern (abhi seekh lo):

  • _s... = kisi region ka start (jaise _sdata = .data ka start).
  • _e... = kisi region ka end (jaise _edata = .data ka end).
  • _si... = init values ka start (jaise _sidata = jahan .data ki initial values Flash mein baithi hain).

Topic ko yeh kyun chahiye: startup code "Flash se RAM copy karo" bina numbers ke nahi keh sakta. Linker script _sidata, _sdata, _edata ko woh numbers ke roop mein export karta hai, aur copy loop _sdata se _edata tak chalti hai. Symbols map aur running code ke beech bridge hain — Startup code & Reset_Handler dekho.


6. LMA vs VMA — do-address idea

Yeh single hardest concept hai jo parent use karta hai, toh hum ise dheere build karte hain.

Figure — Linker scripts — memory regions, sections (.text, .data, .bss)

Kuch data ko do alag waqt par do jagah rehna hota hai:

  • Aaraam mein (power off): int x = 5; ki starting value 5 survive karni chahiye, toh woh Flash mein store hoti hai.
  • Run karte waqt: x changeable hona chahiye, toh use RAM mein rehna chahiye.

Topic ko yeh kyun chahiye: AT> FLASH directive exist hi isliye karta hai taaki LMA ko VMA se alag set kiya ja sake. Agar "ek section, do addresses" tumhare dimaag mein nahi baitha, toh AT> nonsense lagega.


7. Sections — labelled bundles of stuff

Char sections jo tumhe zaroori pehchaanni chahiye (poori detail parent mein hai):

  • .text ::: machine code → Flash.
  • .rodata ::: read-only constants → Flash.
  • .data ::: initialized variables → RAM run, Flash load (do-address wala case).
  • .bss ::: zero-initialized variables → sirf RAM, koi Flash bytes nahi, boot par bas zero kiya jaata hai.

Do aur jinse tum miloge: .stack aur .heap runtime par RAM mein rehte hain — Stack vs Heap in embedded systems dekho.


8. Vector table — woh pehli cheez jo CPU padhta hai

Topic ko yeh kyun chahiye: linker script ko vector table ko .text mein sabse pehle force karna chahiye aur use kabhi delete nahi hone dena (yahi KEEP(...) guarantee karta hai). Agar woh move ho ya gayab ho, chip power-up par garbage mein jump karta hai.


Yeh foundations topic ko kaise feed karte hain

Address (hex box number)

Flash and RAM regions

ORIGIN and LENGTH

Location counter dot

Symbols name addresses

LMA vs VMA two addresses

Sections text data bss

Vector table placement

Linker script topic 5.5.17

Upar se neeche padho: addresses tumhe regions dete hain; regions aur moving cursor tumhe computed placement dete hain; computed placement aur symbols startup code ko cheezein dhundhne dete hain; do-address idea .data explain karta hai; sections aur vector table map finish karte hain.


Equipment checklist

Khud test karo — right side cover karo aur reveal karne se pehle jawab do.

Kisi number par 0x prefix ka matlab kya hai, aur addresses ke liye hex kyun use hota hai?
Yeh hexadecimal (base-16) number mark karta hai; har hex digit = 4 bits, toh addresses chip ke binary pe cleanly map karte hain.
Kaun si memory power-off par sab bhool jaati hai, aur kaun si rakhti hai?
RAM volatile hai (bhool jaati hai); Flash non-volatile hai (rakhti hai).
ORIGIN = 0x08000000, LENGTH = 256K diya ho, toh last usable address kya hai?
0x0803FFFF (kyunki bytes, last box ).
Linker script mein . (dot) symbol ka matlab kya hai?
Location counter — linker ka current address cursor, jo sections place hone ke saath advance karta hai.
_sdata jaisa symbol kya hota hai?
Ek naam jise linker ek real address se bind karta hai, taaki startup code us box number ko refer kar sake.
VMA aur LMA mein kya fark hai?
VMA = run address (jahan data run hote waqt rehta hai, .data ke liye RAM); LMA = load address (jahan uske initial bytes aaraam mein store hote hain, Flash).
.text ka VMA = LMA kyun hota hai lekin .data ka nahi?
Code seedha Flash se execute hota hai isliye ek address chahiye; .data non-volatile (Flash) store hona chahiye yet writable (RAM) bhi hona chahiye, isliye use do chahiye.
.bss Flash binary mein koi bytes kyun store nahi karta?
Yeh zero-initialized hai — sirf uska size record hota hai, aur startup code boot par RAM mein zeros likhta hai.
Vector table Flash mein pehle kyun hona chahiye aur KEEP mein kyun wrap hona chahiye?
CPU reset par stack pointer aur boot code dhundhne ke liye Flash ke bilkul pehle boxes padhta hai; KEEP garbage collection ko use delete karne se rokta hai.