Linker Scripts, Startup Code, and the Reset Vector
What You'll Learn
- 1Explain the reset sequence on a Cortex-M part
- 2Place vector table and sections with linker control
- 3Write startup code that copies data and clears bss
- 4Diagnose crashes caused by bad memory placement
Before main runs, a chain of small but critical steps must place code and data correctly. The vector table lives at a fixed address and its first word is the initial stack pointer while the second word is the reset handler address. On reset the core loads both, branches to the handler, and from there the C runtime initializes the data and bss sections. The linker script decides where everything lands, so a mismatch between script and startup code produces the most confusing boot failures in embedded work.
The Reset Sequence
Reset loads the main stack pointer from vector table offset zero and the program counter from offset four, both taken from the boot alias region. The reset handler then enables the FPU if present, calls the vendor clock initialization, copies initialized data from flash to SRAM, and zeroes the bss region. Only then does it branch into main.
If the stack pointer word is garbage the core cannot even begin execution, which is why an empty or wrong vector table yields a hard fault with no obvious cause. Similarly, skipping the bss clear leaves static variables with random values that only appear on the first run of a rarely used feature.
Vector Table Layout
The vector table is an array of addresses where the first two entries are special and the rest map to exception handlers in priority order. Cortex-M3 and later allow the vector table to be relocated by writing the vector table offset register, which is how a bootloader hands control to an application at a different flash address. Weak default handlers let you override only the interrupts you actually service.
Alignment requirements are strict: the table must be aligned to its size rounded up to a power of two. A misaligned table offset register write is ignored, and the device keeps dispatching to the wrong entries, a bug that looks like random peripheral interrupts.
Linker Script Responsibilities
A linker script divides flash and RAM into named sections, assigns the vector table first in flash, and separates initialized data that must be copied from zero initialized data that only needs clearing. It also provides symbols for the heap and stack boundaries and checks that nothing overflows its region. The map file it produces is the primary tool for size analysis.
Modern build systems generate part of this automatically, but the principles remain: each input section lands at a chosen address, the total must fit, and startup code must agree with the placement. When a variable ends up in the wrong region, power or boot behavior changes in ways that look like hardware faults.
Startup Sequence Order
| Step | Action | Failure if Skipped |
|---|---|---|
| 1 | Load initial stack pointer from vector table | Immediate hard fault |
| 2 | Branch to the reset handler address | Processor never reaches C |
| 3 | Enable FPU and set clock tree | Wrong clock or floating point faults |
| 4 | Copy initialized data from flash to SRAM | Static initializers read flash garbage |
| 5 | Zero the bss section | Static variables hold random values |
| 6 | Call main | Application never starts |
Real-World Examples
- Place the vector table â Put the vector section at the flash base so the boot alias points to valid entries with no relocation.
- Relocate for a bootloader â Write the vector table offset register to the application base and set the stack pointer before jumping.
- Read the map file â Confirm the largest functions and check that .data plus .bss plus stack fit inside SRAM.
- Catch a weak handler â Override the default handler for one interrupt to confirm the vector and clock enable for that peripheral.
đStartup Must Match the Linker Script
Common Mistakes
Forgetting to enable the FPU before using float
Fix: Floating point instructions fault unless the register that powers the coprocessor is enabled early in startup.
Letting the stack collide with the heap
Fix: Without linker checks, .data and .bss growth silently overwrites the stack and fails only under load.
Ignoring vector table alignment
Fix: A misaligned vector offset is rejected and interrupts dispatch to the wrong handlers.
Key Takeaways
- Reset loads the initial stack pointer and the reset handler from the vector table.
- Startup copies .data, clears .bss, then calls main.
- The linker script controls placement and reports overflow in the map file.
- Relocating the vector table enables bootloader and application schemes.
Frequently Asked Questions
Why does my program run under the debugger but not on power up?
The debugger may set clocks, load sections, or start at main, hiding a startup or boot pin configuration problem.
What is the difference between data and bss?
Data holds initialized statics copied from flash at boot, while bss holds zero initialized statics that only need clearing.
Can I run code directly from SRAM?
Yes, place the sections in the SRAM region and set the boot option accordingly, which is common for fast RAM resident kernels.
Interview Questions
Q1: Walk through what happens between reset and main.
Load MSP from vector offset zero, load the reset handler from offset four, enable the FPU, run clock setup, copy .data, zero .bss, then branch to main.
Q2: How does a bootloader start an application?
It programs the application vector table, writes the vector table offset register to point at it, sets the main stack pointer, and branches to the application reset handler.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions