resource-optimization-lowend

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Resource Optimization (Low-End Systems)

资源优化(低端系统)

Purpose

目标

Guide agents through flash and RAM optimization on constrained devices: compiler size flags, linker map analysis, stack usage measurement, dead code elimination, and size-vs-speed tradeoffs — for bare-metal and small RTOS images.
指导 Agent 在资源受限设备上进行 flash 和 RAM 优化:编译器体积优化标志、linker map 分析、栈使用量测量、无用代码消除,以及体积与速度之间的权衡——适用于裸机和小型 RTOS 镜像。

When to Use

使用场景

  • Firmware exceeds flash budget
  • Stack overflow in production only
  • Choosing
    -Os
    vs
    -O2
    on MCU
  • Finding unexpected
    .rodata
    or
    .bss
    growth
  • 固件超出 flash 预算
  • 栈溢出仅在生产环境中发生
  • 在 MCU 上选择
    -Os
    与
    -O2
  • 发现异常的
    .rodata
    或
    .bss
    增长

Workflow

工作流

1. Size measurement toolchain

1. 体积测量工具链

bash
arm-none-eabi-size -A firmware.elf
arm-none-eabi-objdump -h firmware.elf
nm --size-sort -S firmware.elf | tail -20
bash
arm-none-eabi-size -A firmware.elf
arm-none-eabi-objdump -h firmware.elf
nm --size-sort -S firmware.elf | tail -20

2. Linker map file

2. Linker map 文件

ld
/* linker.ld */
OUTPUT_FORMAT("elf32-littlearm")
ENTRY(Reset_Handler)

SECTIONS
{
  /* ... */
}

/* Generate map */
/* gcc ... -Wl,-Map=firmware.map */
bash
grep -E '\.text|\.rodata|\.data|\.bss' firmware.map | head
Identify largest symbols and unexpected library pull-in.
ld
/* linker.ld */
OUTPUT_FORMAT("elf32-littlearm")
ENTRY(Reset_Handler)

SECTIONS
{
  /* ... */
}

/* Generate map */
/* gcc ... -Wl,-Map=firmware.map */
bash
grep -E '\.text|\.rodata|\.data|\.bss' firmware.map | head
找出最大的符号和意外引入的库。

3. Compiler flags

3. 编译器标志

FlagEffect
-Os
Size-first optimization
-ffunction-sections -fdata-sections
Per-symbol sections
-Wl,--gc-sections
Drop unused sections
-flto
Cross-TU dead code elimination
-specs=nano.specs
Smaller newlib (GCC ARM)
makefile
CFLAGS += -Os -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections -Wl,--print-memory-usage
标志效果
-Os
优先优化体积
-ffunction-sections -fdata-sections
按符号划分段
-Wl,--gc-sections
丢弃未使用的段
-flto
跨 TU 消除无用代码
-specs=nano.specs
更小的 newlib(GCC ARM)
makefile
CFLAGS += -Os -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections -Wl,--print-memory-usage

4. Stack analysis

4. 栈分析

bash
undefined
bash
undefined

Static (if built with -fstack-usage)

Static (if built with -fstack-usage)

find . -name '*.su' -exec cat {} ;
find . -name '*.su' -exec cat {} ;

Linker stack symbol

Linker stack symbol

grep _estack firmware.map

Runtime: fill stack with pattern (`0xDEADBEEF`), run tests, scan high-water mark. FreeRTOS: `uxTaskGetStackHighWaterMark`.
grep _estack firmware.map

运行时:使用模式(`0xDEADBEEF`)填充栈,运行测试并扫描 high-water mark。FreeRTOS:`uxTaskGetStackHighWaterMark`。

5. RAM categories

5. RAM 类别

SectionTactic
.bss
Shrink buffers; pool allocators
.data
Move const to flash (
const
→
.rodata
)
HeapAvoid malloc; fixed pools
StackReduce nesting; smaller ISR stacks
段策略
.bss
缩小缓冲区;使用池分配器
.data
将 const 移至 flash(
const
→
.rodata
)
Heap避免 malloc;使用固定内存池
Stack减少嵌套;缩小 ISR 栈

6. Size vs speed decision

6. 体积与速度的决策

Hot path in ISR or 1 kHz loop?
├── Yes → -O2 for that file (#pragma GCC optimize)
└── No  → -Os globally
Hot path in ISR or 1 kHz loop?
├── Yes → -O2 for that file (#pragma GCC optimize)
└── No  → -Os globally

7. Agent usage

7. Agent 使用方式

/resource-optimization-lowend Find top 10 flash consumers in firmware.map and suggest -gc-sections fixes
/resource-optimization-lowend Find top 10 flash consumers in firmware.map and suggest -gc-sections fixes

Common Problems

常见问题

SymptomCauseFix
printf pulls 20+ KBFull newlib printf
_write
retarget; tiny printf
--gc-sections
broke IRQ
Section collected
KEEP()
in linker script
Stack overflow lateDeep call + IRQ nestMeasure HW stack; increase
_estack
RAM zero but big ELF
.data
not loaded
Check VMA/LMA
LTO link failMixed compiler versionsSame GCC for all objects
症状原因修复方案
printf 引入 20+ KB完整的 newlib printf重定向
_write
;使用 tiny printf
--gc-sections
导致 IRQ 失效
段被回收在 linker script 中使用
KEEP()
栈在后期才溢出调用层级过深 + IRQ 嵌套测量硬件栈;增大
_estack
RAM 为零但 ELF 很大
.data
未加载
检查 VMA/LMA
LTO 链接失败编译器版本混用所有目标文件使用同一 GCC

Related Skills

相关 Skill

  • skills/embedded/linker-scripts
    — MEMORY/Sections layout
  • skills/baremetal/baremetal-startup
    — stack symbol
  • skills/baremetal/low-power-embedded
    — RAM retention
  • skills/compilers/gcc
    — optimization flags
  • skills/rust/rust-no-std
    — embedded Rust size patterns
  • skills/embedded/linker-scripts
    — MEMORY/Sections 布局
  • skills/baremetal/baremetal-startup
    — 栈符号
  • skills/baremetal/low-power-embedded
    — RAM 保留
  • skills/compilers/gcc
    — 优化标志
  • skills/rust/rust-no-std
    — 嵌入式 Rust 体积优化模式