返回提示词目录第 1421 页

分析并缩减固件存储与内存占用

依据链接映射和体积报告定位资源大户,提出编译与代码优化方案,并列出节省估算和硬件检查。

完整提示词

<context>
The user's firmware no longer fits, or has no room for the next feature or an over-the-air update slot. MCU: {{mcu}}. Flash holds `.text`, `.rodata` and the initial values of `.data`; RAM holds `.data`, `.bss`, heap and stacks. The usual big wins are not clever code changes but things pulled in by accident: `printf` with floating point support, C++ exceptions, RTTI and iostream, the full newlib instead of newlib-nano, software floating point routines because one `float` or `double` crept in, unused vendor HAL modules, large lookup tables or fonts copied into RAM because they were not `const`, and debug strings and asserts left in release builds.

The trap is shrinking size and breaking timing or behaviour: `-Os` and LTO can change timing of busy-wait loops, inline differently in interrupt handlers, and expose undefined behaviour; stack sizes cut without measuring cause rare crashes in the field.
</context>

<task>
<map_or_size_report>
{{map_or_size_report}}
</map_or_size_report>

1. Summarise the totals: flash used and free, RAM used and free (static), and the share of each section. Note whether heap and stacks are reserved statically.
2. List the 15-20 largest symbols and the libraries they come from (application, vendor HAL, RTOS, C library, compiler runtime such as soft-float or division helpers). Group by origin and give each group's bytes.
3. Identify accidental inclusions and their likely trigger: printf or scanf family with float support, `malloc` pulling in the allocator, exceptions and unwinding tables, RTTI, static constructors, double-precision maths in single-precision code, `sprintf` used for one number, assert strings with file names.
4. Propose quick wins with expected bytes saved (ranges): `-Os` or `-Oz` where the compiler supports it, `-ffunction-sections -fdata-sections` with `--gc-sections`, LTO, newlib-nano (`--specs=nano.specs`) and dropping `_printf_float` if not needed, `-fno-exceptions -fno-rtti` for C++, `-fsingle-precision-constant` or explicit `f` suffixes, removing unused HAL modules, compiling out logs and asserts in release.
5. Propose code changes for what remains: mark tables `const` so they stay in flash, smaller types and bitfields for large arrays of structs, replace a heavy library call with a small purpose-built one, deduplicate strings, compress large assets, move rarely used code to a bootloader-shared or external flash region if the hardware supports it.
6. RAM and stack: find the largest `.bss` and `.data` objects, check buffer sizes against real need, and size each task or main stack from measured high-water marks (stack painting, the RTOS's stack high-water API, or `-fstack-usage` and call-graph analysis) plus a stated margin (for example 20-25%).
7. List risks and checks for each change: timing-sensitive code, interrupt latency, behaviour changes under LTO, and the regression tests or hardware checks to run.

If the input lacks symbol-level sizes, give the exact command to produce them for the toolchain and stop.
</task>

<constraints>
- Every saving is an estimate until rebuilt; give ranges and say which are typical rather than measured.
- Never reduce a stack or buffer without measured usage and a margin.
- Do not invent symbol names or sizes not in the report.
- Do not recommend removing safety checks (watchdog, bounds checks on external input) to save bytes.
{{> guardrails/scope-discipline}}
</constraints>

<output_format>
## Where the bytes go
Table: section | bytes | share | notes. Then a table: symbol or group | origin | bytes.
## Quick wins
Table: change | flag or setting | estimated bytes saved | risk.
## Code changes
Bullets with snippets where useful.
## RAM and stack
Table: object or stack | current bytes | measured or estimated need | proposed.
## Risks and checks
Bullets.
</output_format>

可替换内容

  • {{mcu}}
  • {{map_or_size_report}}
  • {{> guardrails/scope-discipline}}

分类与标签

编程开发、嵌入式开发、固件体积优化、内存分析、栈空间测量

前往 AI 图片创作