Keil中 L6406E: No space in execution regions with .ANY selector matching heap_4.o(.bss) 错误分析简述

部署运行你感兴趣的模型镜像

链接器错误 L6406E: No space in execution regions with .ANY selector matching heap_4.o(.bss) 表明内存分配已超出预期范围,通常与 全局/静态变量(.bss段)或堆(Heap)配置 有关。以下是逐步解决方案:


1. 理解错误原因

  • .bss 段:存储未初始化的全局变量和静态变量。
  • heap_4.o:通常与 FreeRTOS 的堆管理(Heap4)相关,Heap4 需要预先分配一块连续内存作为堆空间。
  • 错误本质:链接器无法为 .bss 段或堆分配足够的内存,可能原因包括:
    • 堆/栈配置过大,挤占其他内存区域。
    • 全局变量或静态变量过多(尤其是大型数组或结构体)。
    • 链接器脚本(.sct 或 .ld 文件)中内存区域划分不合理。

2. 快速检查与修复步骤

2.1 检查堆(Heap)和栈(Stack)配置
  • 定位内存配置
    • 在 FreeRTOS 中,堆大小通常在 FreeRTOSConfig.h 中通过 configTOTAL_HEAP_SIZE 定义。
    • 栈大小在任务创建时指定(如 xTaskCreate 的 usStackDepth 参数)。
  • 调整策略
    • 减少堆大小:如果堆分配过大(例如 configTOTAL_HEAP_SIZE 设置为 100KB 但实际只需 10KB),缩小它。
    • 减少栈大小:检查任务栈是否过度分配,使用内存分析工具(如 FreeRTOS 的 uxTaskGetStackHighWaterMark)优化栈使用。
2.2 优化全局变量和静态变量
  • 检查大型全局变量
    • 使用 grep 或 IDE 的全局搜索功能,定位代码中定义的大型数组或结构体(如 uint8_t buffer[1024*1024];)。
    • 优化建议
      • 将大型全局变量改为 static 局部变量(限制在文件作用域)。
      • 使用动态内存分配(如 malloc/free)替代静态分配。
      • 将数据存储到外部存储器(如 Flash/SD 卡)。
2.3 检查链接器脚本
  • 定位内存区域
    • 打开链接器脚本(如 *.sct 或 *.ld 文件),找到 RAM 或 IRAM 区域的定义。
    • 示例(ARMCC 链接器脚本片段):

      ld

      LR_RAM1 0x20000000 0x00010000 { ; 定义 RAM 区域
      ER_RAM1 0x20000000 0x00010000 { ; 执行区域
      *.o (RESET, +First)
      *(InRoot$$Sections)
      .ANY (+RO)
      .ANY (+RW +ZI)
      }
      }
  • 调整内存分配
    • 确保堆、栈和全局变量所在的内存区域有足够空间。
    • 如果使用分散加载(Scatter File),检查 .ANY 匹配规则是否合理。
2.4 分析内存映射文件(.map)
  • 生成 .map 文件
    • 在编译命令中添加 --map(ARMCC)或 -Wl,-Map=output.map(GCC),生成内存映射文件。
  • 定位占用空间大的段
    • 搜索 .bss 段和 heap_4.o 的内存占用:

      bash

      grep '\.bss' output.map | sort -k3 -nr # 按大小排序 .bss 段
      grep 'heap_4.o' output.map # 查看 heap_4.o 的内存分配
    • 如果 .bss 段占用过高,优化全局变量;如果堆空间不足,调整 configTOTAL_HEAP_SIZE
2.5 更换堆实现(可选)
  • 尝试其他堆管理方案
    • FreeRTOS 提供 5 种堆实现(Heap1~Heap5)。Heap4 需要连续内存块,而 Heap5 支持非连续内存。
    • 修改 FreeRTOSConfig.h
      #define configHEAP_TYPE 5 // 切换到 Heap5
    • 重新配置堆内存区域(需在链接器脚本中定义多个内存块)。

3. 示例:调整 FreeRTOS 堆和栈

假设原配置如下:

// FreeRTOSConfig.h
#define configTOTAL_HEAP_SIZE ( 64 * 1024 ) // 64KB 堆
#define configMINIMAL_STACK_SIZE ( 256 ) // 256 字栈(假设字长 4 字节 → 1KB)
  • 优化后
    #define configTOTAL_HEAP_SIZE ( 32 * 1024 ) // 减少到 32KB
    #define configMINIMAL_STACK_SIZE ( 128 ) // 减少到 128 字(0.5KB)

4. 总结

  1. 优先检查堆/栈配置,确保不过度分配。
  2. 优化全局变量,减少 .bss 段占用。
  3. 验证链接器脚本,确保内存区域划分合理。
  4. 通过 .map 文件定位问题,针对性优化。

当然,具体也可能由于以下原因,需要再分析。

  • 使用的编译器(ARMCC/GCC/IAR)。
  • 目标芯片型号和内存大小。
  • 链接器脚本关键片段和 .map 文件片段。

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

Rebuild started: Project: leican *** Using Compiler 'V5.06 update 7 (build 960)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' Rebuild target 'leican' assembling startup_stm32f407xx.s... compiling main.c... compiling stm32f4xx_hal_gpio.c... compiling stm32f4xx_hal_msp.c... compiling gpio.c... compiling stm32f4xx_hal_dma.c... compiling stm32f4xx_hal_flash_ramfunc.c... compiling stm32f4xx_it.c... compiling stm32f4xx_hal_flash.c... compiling stm32f4xx_hal_flash_ex.c... compiling stm32f4xx_hal_dma_ex.c... compiling stm32f4xx_hal_tim_ex.c... compiling stm32f4xx_hal_rcc_ex.c... compiling stm32f4xx_hal_rcc.c... compiling freetos_start.c... compiling croutine.c... compiling list.c... compiling event_groups.c... compiling queue.c... compiling stream_buffer.c... compiling stm32f4xx_hal_tim.c... compiling tasks.c... compiling heap_4.c... compiling timers.c... compiling port.c... compiling stm32f4xx_hal.c... compiling system_stm32f4xx.c... compiling usart.c... compiling sys.c... compiling stm32f4xx_hal_pwr_ex.c... compiling stm32f4xx_hal_pwr.c... compiling stm32f4xx_hal_exti.c... compiling stm32f4xx_hal_cortex.c... compiling stm32f4xx_hal_eth.c... compiling stm32f4xx_hal_uart.c... compiling delay.c... compiling sys_arch.c... compiling app_tcp_server.c... compiling lwip_comm.c... compiling ethernetif.c... compiling altcp_alloc.c... compiling altcp_tcp.c... compiling inet_chksum.c... compiling lwip_sys.c... compiling raw.c... compiling altcp.c... compiling def.c... compiling mem.c... compiling ip.c... compiling ethernet.c... compiling dns.c... compiling pbuf.c... compiling init.c... compiling netif.c... compiling memp.c... compiling autoip.c... compiling stats.c... compiling igmp.c... compiling acd.c... compiling ip4_addr.c... compiling icmp.c... compiling timeouts.c... compiling ip4_frag.c... compiling etharp.c... compiling ip4.c... compiling udp.c... compiling tcp_in.c... compiling tcp_out.c... compiling dhcp.c... compiling tcp.c... compiling master.c... compiling key.c... compiling 8512.c... compiling netifapi.c... compiling rs485.c... ..\Drivers\BSP\RS485\rs485.c(24): warning: #550-D: variable "err" was set but never used uint8_t err; ..\Drivers\BSP\RS485\rs485.c: 1 warning, 0 errors compiling err.c... compiling memory_management.c... compiling netbuf.c... compiling if_api.c... compiling netdb.c... compiling tcpip.c... compiling api_lib.c... compiling api_msg.c... compiling sockets.c... linking... .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching memp.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching heap_4.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching master.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching dns.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching tasks.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching startup_stm32f407xx.o(STACK). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching usart.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching etharp.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching rand.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching rs485.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching libspace.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching lwip_comm.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching memp.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching app_tcp_server.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching 8512.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching tasks.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching timers.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching dhcp.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching tcp_in.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching app_tcp_server.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching tcp.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching heap_4.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching freetos_start.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching ip.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching timers.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching tcp_in.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching port.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching dhcp.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching ip4_addr.o(.bss). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching tcpip.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching 8512.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching dns.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching timeouts.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching stm32f4xx_hal.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching netif.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching udp.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching usart.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching ip4_frag.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching system_stm32f4xx.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching delay.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching ethernetif.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching ip4.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching lwip_comm.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching etharp.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching rs485.o(.data). .\leican\leican.axf: Error: L6406E: No space in execution regions with .ANY selector matching startup_stm32f407xx.o(HEAP). .\leican\leican.axf: Error: L6407E: Sections of aggregate size 0x2e450 bytes could not fit into .ANY selector(s). Not enough information to list image symbols. Not enough information to list load addresses in the image map. Finished: 2 information, 0 warning and 47 error messages. ".\leican\leican.axf" - 47 Error(s), 1 Warning(s). Target not created.报错可能原因?
09-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值