STM32F091启动文件中.size伪指令作用及语法解析
Understanding the
.size Directive in STM32 Cortex-M Startup Code Great question—digging into startup scripts and assembler directives is a key part of mastering embedded systems, so let’s break this down step by step.
What’s the .size Assembler Directive?
The .size directive is part of the GNU Assembler (GAS), the standard toolchain for most ARM Cortex-M targets including your STM32F091. Its core purpose is to record the memory size associated with a symbol in the object file.
Linkers, debuggers, and memory map tools use this information to:
- Correctly calculate how much RAM/Flash a function, variable, or section uses
- Generate accurate debug symbols (so your debugger knows not to treat a marker as a block of memory)
- Validate that your code and data fit within the MCU’s memory limits
Breaking Down .size __HeapBase, . - __HeapBase
Let’s unpack the syntax and context here, since this line is tied to how STM32 defines its heap region:
First, the key symbols:
__HeapBase: This is a linker-defined marker pointing to the start address of the heap (the RAM region used for dynamic allocation likemalloc()). It’s not a variable that takes up memory—it’s just a reference point..(the dot): In assembler/linker scripts, this represents the current location counter—think of it as the address the assembler is currently "working at".
The .size directive follows this format:
.size <symbol>, <size_value>
In your specific line:
- The
size_valueis calculated as. - __HeapBase. Since this line is placed immediately after__HeapBaseis defined, the current location counter (.) matches the address of__HeapBaseexactly. This means the size evaluates to 0. - We’re telling the assembler to mark
__HeapBaseas a zero-sized symbol. This is critical because__HeapBaseisn’t a block of memory—it’s just an address marker. Setting its size to 0 ensures tools like debuggers and linkers don’t misinterpret it as a memory-occupying variable, keeping your memory map and debug information accurate.
Quick Summary
.sizeis a GAS directive that records a symbol’s memory size for toolchain tools.- Your specific line marks
__HeapBase(the heap start marker) as a zero-sized symbol, correctly identifying it as an address reference rather than a variable that takes up space.
内容的提问来源于stack exchange,提问作者Gaurav Sharma
相关产品推荐
相关产品推荐

