🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Linker Scripts, Startup Code, and the Reset Vector

Microcontrollers & ARMAssembly and MemoryđŸŸĸ Free Lesson

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

StepActionFailure if Skipped
1Load initial stack pointer from vector tableImmediate hard fault
2Branch to the reset handler addressProcessor never reaches C
3Enable FPU and set clock treeWrong clock or floating point faults
4Copy initialized data from flash to SRAMStatic initializers read flash garbage
5Zero the bss sectionStatic variables hold random values
6Call mainApplication 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

Copying a startup file from another project while keeping a different linker script is a classic source of corrupted initializers and random resets.

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

  1. Misidentifying the problem type — Always start by categorizing the problem
  2. Incorrect formula application — Verify the formula applies to your specific case
  3. Unit errors — Always check units are consistent throughout
  4. Sign errors — Pay careful attention to positive and negative values
  5. Rounding too early — Keep full precision until the final answer
  6. 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

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. 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

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids 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
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Microcontrollers & ARM Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement