Exercises — Linker scripts — memory regions, sections (.text, .data, .bss)
5.5.17 · D4· Coding › Embedded Systems & Real-Time Software › Linker scripts — memory regions, sections (.text, .data, .bss)
Poore page mein yahi chip use hogi (jab tak koi problem override na kare):
jahaan matlab ORIGIN (pehle byte ka address) aur matlab LENGTH (region mein kitne bytes hain). bytes.
Problem 1 se pehle jo vocabulary chahiye
Har solution mein do terms aate hain. Inhe ek baar, ek picture ke saath, pehle se samjho — taaki koi bhi symbol use hone se pehle explain ho jaye.
Figure 1 mein do memory regions dikhaye gaye hain aur cursor . kaise dono mein se walk karta hai sections place karte hue.

Figure 1 aise padho: left column Flash hai (non-volatile), right RAM hai (power-off pe wipe ho jaata hai). Cyan cursor arrow labeled . har region ke ORIGIN se top pe shuru hota hai aur sections place hote hue neeche jaata hai. Dhyan do .data do baar aata hai — Flash mein ek amber block (uske stored initial values, LMA) aur RAM mein ek amber block (jahaan yeh run karta hai, VMA) — amber "copy at boot" arrow se jude hue. .bss (cyan, sirf RAM) ka koi Flash twin nahi hai; yeh simply zero kar diya jaata hai. Yeh ek picture poore page ka miniature hai; neeche ke har exercise is page ka ek slice hai.
Level 1 — Recognition
Exercise 1.1 (L1)
Har declaration ke liye, batao yeh kis section mein jaata hai: .text, .rodata, .data, ya .bss.
- (a)
int counter = 42;(global) - (b)
int total;(global) - (c)
const char msg[] = "hi";(global) - (d)
void loop(void) { ... } - (e)
static int cache = 0;(global)
Recall Solution 1.1
- (a)
.data— non-zero value se initialized, toh value ko store karna padega (Flash mein) aur RAM mein copy karna padega. - (b)
.bss— koi initializer nahi wala global implicitly zero hota hai, toh yeh.bssmein jaata hai (sirf uski size record hoti hai). - (c)
.rodata—constdata read-only hai, Flash mein rakha jaata hai. - (d)
.text— machine code hamesha Flash mein rehta hai aur wahan se execute hota hai. - (e)
.bss— yeh trap hai. Yeh0se initialized hai, aur zero-initialized data.bssmein jaata hai,.datamein nahi (Flash mein zeros store karna wasteful hoga). Neeche mistake dekho.
Exercise 1.2 (L1)
Har linker-script keyword ko match karo jo woh return karta hai:
ADDR(.data), LOADADDR(.data), SIZEOF(.data).
Recall Solution 1.2
ADDR(.data)→ VMA (run address, RAM mein).LOADADDR(.data)→ LMA (load address, Flash mein, jahaan init values physically baithe hain).SIZEOF(.data)→.datamein kitne bytes hain.
Parent se memory hook: Data Doubles — do addresses. Figure 1 mein yeh do amber blocks hain.
Level 2 — Application
Exercise 2.1 (L2)
Ek program mein hai: .text bytes, .rodata bytes, .data bytes, .bss bytes.
- (a) Flash
.binimage kitne bytes occupy karta hai? - (b) Static data kitne bytes RAM use karta hai (stack/heap ignore karo)?
Recall Solution 2.1
(a) Flash image. Image mein physically sab kuch hona chahiye jo power-off survive kare: code, read-only constants, aur .data ke initial values (uski LMA copy — Figure 1 mein amber Flash block). .bss Flash mein kuch nahi contribute karta.
(b) RAM. Static RAM mein .data (copy hone ke baad) plus .bss hota hai:
Note karo .data dono budgets mein count hota hai — uske do ghar hain, exactly LMA/VMA idea.
Exercise 2.2 (L2)
Given layout .text phir .data Flash mein placed (dono AT> FLASH), .text = 0x2FEC bytes se shuru:
- (a)
LOADADDR(.data)kya hai? - (b) Agar
.data0x200bytes ka hai, toh Flash address kya hai.datake baad ka pehla free byte?
Recall Solution 2.2
Location counter . yaad karo (upar define kiya): yeh cursor hai jo region ke ORIGIN se shuru hota hai aur har section ki size ke barabar aage badhta hai. Flash mein yeh se shuru hota hai.
(a) .text (0x2FEC bytes) place hone ke baad, cursor 0x2FEC se aage badh gaya, toh .data wahan load hota hai:
(b) .data (0x200 bytes) ke baad cursor phir aage badhta hai:
Level 3 — Analysis
Exercise 3.1 (L3)
Parent note se startup copy loop:
for (uint32_t *p=&_sidata, *q=&_sdata; q < &_edata; ) *q++ = *p++;Symbols hain: _sidata = LMA of .data, _sdata = VMA start, _edata = VMA end. Maano _sdata = 0x20000000, _edata = 0x20000010, _sidata = 0x08002FEC.
- (a) Yeh loop kitne 32-bit words copy karta hai?
- (b) Kaunse source (Flash) addresses read hote hain?
Figure 2 is loop ko ek mirror ki tarah draw karta hai: left mein Flash source block, right mein RAM destination, aur har word ke liye ek arrow.

Recall Solution 3.1
Pointers uint32_t* hain, toh q++ har iteration mein 4 bytes advance karta hai (Figure 2 mein ek arrow per word).
(a) RAM mein byte span bytes. Words:
(b) Flash pointer p 0x08002FEC se shuru hota hai aur 4 se step karta hai:
(char reads, 0x20000000, 04, 08, 0C pe char writes se match karte hain — Figure 2 mein char arrows).
Exercise 3.2 (L3)
Ek student .isr_vector ke around se KEEP(...) hata deta hai aur linker garbage collection (--gc-sections) enable karta hai. Board reset pe hard-fault karta hai. Cause aur effect ki chain explain karo. (.isr_vector is page ke upar define kiya gaya tha; deep dive Vector table & interrupt handling aur Startup code & Reset_Handler.)
Recall Solution 3.2
- Garbage collection koi bhi section delete kar deta hai jo kisi symbol ke dwara referenced nahi hai — Flash bachane ke liye.
.isr_vectorsection (vector table) C mein kabhi naam se call nahi hota — CPU ise hardware convention se Flash ke base se padhta hai. Linker ko koi reference nahi dikhta.- Toh GC
.isr_vectordrop kar deta hai. Ab Flash ke pehle bytes woh section hai jo wahan aakar baith gaya (ya padding). - Reset pe CPU initial stack pointer aur Reset_Handler address (ab missing) vector table se load karna chahta hai → garbage addresses → immediate hard fault.
KEEP()linker ko kehta hai "pretend karo yeh referenced hai; ise kabhi collect mat karo."
Level 4 — Synthesis
Build karne se pehle ALIGN ke baare mein ek baat
Exercise 4.1 (L4)
Is page ke upar wale chip ke liye ek complete, minimal linker script likho jo:
- vector table pehle rakhta hai, phir
.text, phir.rodataalag-alag named output sections ke roop mein Flash mein; .dataRAM mein run karta hai lekin Flash se load hota hai;.bssRAM mein hai;- sabhi section starts/ends 4-byte aligned hain;
- startup code ke liye
_sidata, _sdata, _edata, _sbss, _ebssexport karta hai.
Recall Solution 4.1
"vector table, phir .text, phir .rodata" ko distinct, ordered sections ke roop mein honour karne ke liye, hum har ek ko uska apna output section dete hain .rodata ko .text mein fold karne ke bajaye. File mein unka order wahi order hai jisme woh Flash mein place hote hain.
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 256K
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 64K
}
SECTIONS
{
.isr_vector : {
. = ALIGN(4);
KEEP(*(.isr_vector)) /* vector table FIRST; KEEP survives GC */
. = ALIGN(4);
} > FLASH
.text : {
. = ALIGN(4);
*(.text*) /* machine code */
. = ALIGN(4);
} > FLASH
.rodata : {
. = ALIGN(4);
*(.rodata*) /* read-only constants, kept separate */
. = ALIGN(4);
} > FLASH
_sidata = LOADADDR(.data); /* Flash source of init values (LMA) */
.data : {
. = ALIGN(4);
_sdata = .; /* RAM run start (VMA) */
*(.data*)
. = ALIGN(4);
_edata = .; /* RAM run end */
} > RAM AT> FLASH /* VMA in RAM, LMA in Flash */
.bss : {
. = ALIGN(4);
_sbss = .;
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .;
} > RAM /* no AT>: nothing to load, just zeroed */
}Har choice justified: .isr_vector, .text, .rodata teen alag output sections hain requested order mein (prompt se exactly match); KEEP table ko protect karta hai (Ex 3.2); ALIGN(4) har boundary ko word-aligned rakhta hai taaki copy loop legal ho; AT> FLASH .data ko do addresses deta hai (Ex 2.2); .bss ka koi AT> nahi hai kyunki yeh boot pe zero hota hai, copy nahi hota.
Exercise 4.2 (L4)
Upar wale script ko use karte hue aur assume karte hue .isr_vector+.text+.rodata total bytes (already 4-byte aligned), aur .data bytes:
- (a)
_sidata,_sdata,_edatacompute karo. - (b) Confirm karo ki Flash image 256K mein fit hoti hai.
Recall Solution 4.2
(a)
_sidata = LOADADDR(.data)= teen code sections ke baad Flash cursor = ._sdata = ADDR(.data)=.datake start pe RAM cursor..dataRAM mein pehla hai, toh yeh ke barabar hai._edata: kyun_edata = _sdata + 0x40? Script mein_sdata.se*(.data*)se pehle stamp hota hai aur_edatabaad mein stamp hota hai, toh_edata - _sdataexactly woh bytes hain jitne cursor ne advance kiye — woh haiSIZEOF(.data) = 0x40. Isliye:
(b) Flash image bytes. Region size bytes. Kyunki , yeh comfortably fit hoti hai.
Level 5 — Mastery
Exercise 5.1 (L5)
Ek design ko RAM mein chahiye: .data = 8K, .bss = 20K, plus 8K stack aur 16K heap. RAM 64K hai. Saath mein RAM mein 4K DMA buffer reserve karne ki requirement bhi hai.
- (a) Kya sab fit hota hai? Arithmetic dikhao.
- (b) Kitne bytes bacha rahe hain?
- (c) Engineer heap ko double karke 32K karna chahta hai. Ab kya hoga? Ek aisa sabse sasta fix suggest karo jo saari functionality rakhta ho. (Dekho Stack vs Heap in embedded systems aur Flash vs RAM tradeoffs.)
Recall Solution 5.1
(a) Total RAM demand: , toh yeh fit hota hai. (b) Bacha hua: (c) Heap double karne se add hota hai, → 8K se overflow. Linker "region RAM overflowed" error emit karega. Sabse saste fixes jo functionality rakhte hain:
- Jo data genuinely constant hai lekin galti se RAM mein hai, use
.rodatamein (Flash mein) move karo — bina kisi cost ke RAM free hoga. - Ya, agar 32K heap genuinely required hai, koi software trick RAM nahi banati: tumhe zyada RAM wala chip lena padega. Lekin sabse sasta software-only move hai 8K slack reclaim karna + agar measurements allow karen toh over-provisioned stack/DMA buffer shrink karna. Koi free lunch nahi hai — RAM ek hard physical limit hai ().
Exercise 5.2 (L5)
Prove karo, words mein address arithmetic ke saath, kyun correct boot ke liye yeh invariant hold karna chahiye, aur exact word count do jo copy loop perform karta hai exported symbols ke terms mein:
Recall Solution 5.2
Kyun invariant hold karta hai. .data ek contiguous block occupy karta hai. RAM mein yeh _sdata … _edata span karta hai, toh uski size hai. Flash mein wahi same bytes _sidata se start hoke mirror hote hain (Figure 1 mein left–right amber twins, Figure 2 mein mirror). Kyunki copy byte-for-byte hai, Flash mirror mein bilkul same number of bytes hone chahiye, yaani _sidata se _sidata + (\_edata - \_sdata) tak. Agar yeh dono sizes disagree karti, toh ya kuch init values kabhi copy na hote ya loop mirror ke baad padh leta. Linker equality guarantee karta hai kyunki dono same SIZEOF(.data) se compute hoti hain.
Word count. uint32_t (4-byte) pointers ke saath:
assuming .data 4-byte aligned hai aur size 4 ka multiple hai (Ex 4.1 mein ALIGN(4) directives guarantee karte hain). Ex 4.2 ke numbers ke liye, words.
Recall Jaane se pehle ek-line self-test
Flash image mein .text + .rodata + .data count hota hai; RAM mein .data + .bss + stack + heap count hota hai. .data dono mein aata hai kyunki uske do addresses hain (LMA in Flash, VMA in RAM).
Flashcards
Kya static int cache = 0; .data mein jaata hai ya .bss mein?
.bss mein — zero initializer ka matlab same hai koi initializer nahi; sirf non-zero initializers .data force karte hain.Kya .data Flash image size mein count hota hai?
.bin mein real bytes hain.Ek uint32_t* copy loop 16-byte .data region pe kitne iterations run karta hai?
q++ 4 bytes advance karta hai, toh 16/4 = 4 words..isr_vector ke around se KEEP hatane pe hard fault kyun aata hai?
Linker location counter . kya karta hai?
ALIGN(4) kya karta hai aur .data ke liye kyun chahiye?
. ko 4 ke next multiple tak bump karta hai taaki word-wise copy loop legally aligned addresses read/write kare.