Skip to content

fix: ELF files too large when addresses not contiguous#6

Merged
github0null merged 2 commits intogithub0null:masterfrom
Deadline039:fix-elf-large
Jan 1, 2026
Merged

fix: ELF files too large when addresses not contiguous#6
github0null merged 2 commits intogithub0null:masterfrom
Deadline039:fix-elf-large

Conversation

@Deadline039
Copy link
Contributor

@Deadline039 Deadline039 commented Dec 9, 2025

当定义多个加载域后,地址不连续,会导致ELF中填充大量数据,ELF文件巨大:
big_elf

原因是将其他加载域的section全部放在ENTRY section导致的,因此只要把对应加载域的section用objcopy到对应的section即可。

测试文件axf的elf信息如下(为了避免空间爆掉,测试时EXT_FLASH起始地址为0x08900000):

fromelf -r xxx.axf
========================================================================

** ELF Header Information

    File Name: xxx.axf
...
========================================================================
** Program header #0 (PT_LOAD) [PF_X + PF_W + PF_R + PF_ARM_ENTRY]
    Size : 220336 bytes (146592 bytes in file)
    Virtual address: 0x08000000 (Alignment 16)
====================================
** Program header #1 (PT_LOAD) [PF_R]
    Size : 308028 bytes
    Virtual address: 0x08900000 (Alignment 4)
========================================================================
** Section #1 'ER_IROM1' (SHT_PROGBITS) [SHF_ALLOC + SHF_EXECINSTR]
    Size   : 146540 bytes (alignment 16)
    Address: 0x08000000

** Section #2 'RW_IRAM1' (SHT_PROGBITS) [SHF_ALLOC + SHF_WRITE]
    Size   : 48 bytes (alignment 8)
    Address: 0x20000000

** Section #3 'RW_IRAM1' (SHT_NOBITS) [SHF_ALLOC + SHF_WRITE]
    Size   : 73744 bytes (alignment 8)
    Address: 0x20000030

** Section #4 'EXT_FLASH' (SHT_PROGBITS) [SHF_ALLOC]
    Size   : 308028 bytes (alignment 4)
    Address: 0x08900000
...

修改前的elf信息如下:

fromelf -r xxx.elf
"xxx.elf": Warning: Q0479W: In ELF file: Section/segment mapping in SHT_NOTE section #10 contains 4 entries, but there are 3 SHF_ALLOC sections
========================================================================
** ELF Header Information
    File Name: xxx.elf
...
========================================================================
** Program header #0 (PT_LOAD) [PF_X + PF_W + PF_R + PF_ARM_ENTRY]
    Size : 9745212 bytes
    Virtual address: 0x08000000 (Alignment 16)
====================================
** Program header #1 (PT_LOAD) [PF_R]
    Size : 0 bytes
    Virtual address: 0x08900000 (Alignment 4)
========================================================================
** Section #1 'ER_IROM1' (SHT_PROGBITS) [SHF_ALLOC + SHF_EXECINSTR]
    Size   : 9745212 bytes (alignment 16)
    Address: 0x08000000

** Section #2 'RW_IRAM1' (SHT_PROGBITS) [SHF_ALLOC + SHF_WRITE]
    Size   : 48 bytes (alignment 8)
    Address: 0x20000000

** Section #3 'RW_IRAM1' (SHT_NOBITS) [SHF_ALLOC + SHF_WRITE]
    Size   : 73744 bytes (alignment 8)
    Address: 0x20000030
...

修改前EXT_FLASH的大小为0字节,根据axf的信息,0x08900000(EXT_FLASH Start Addr) - 0x08000000 (ER_IROM1 Start Addr) + 330028(EXT_FLASH Size) = 9745212,是可以对的上ER_IROM1的大小的,这证明EXT_FLASH全部被塞到了ER_IROM1中。

如果是STM32 QSPI的地址(0x90000000),那么文件体积会达到2G多!而且其中绝大多数都是无用的空数据。

修改后的elf信息如下:

fromelf -r xxx.elf
========================================================================
** ELF Header Information
    File Name: xxx.elf
...
========================================================================
** Program header #0 (PT_LOAD) [PF_X + PF_W + PF_R + PF_ARM_ENTRY]
    Size : 146608 bytes
    Virtual address: 0x08000000 (Alignment 16)
====================================
** Program header #1 (PT_LOAD) [PF_R]
    Size : 330028 bytes
    Virtual address: 0x08900000 (Alignment 4)
========================================================================
** Section #1 'ER_IROM1' (SHT_PROGBITS) [SHF_ALLOC + SHF_EXECINSTR]
    Size   : 146608 bytes (alignment 16)
    Address: 0x08000000

** Section #2 'RW_IRAM1' (SHT_PROGBITS) [SHF_ALLOC + SHF_WRITE]
    Size   : 48 bytes (alignment 8)
    Address: 0x20000000

** Section #3 'RW_IRAM1' (SHT_NOBITS) [SHF_ALLOC + SHF_WRITE]
    Size   : 73744 bytes (alignment 8)
    Address: 0x20000030

** Section #4 'EXT_FLASH' (SHT_PROGBITS) [SHF_ALLOC]
    Size   : 330028 bytes (alignment 4)
    Address: 0x08900000
...

fromelf如果不合并bin,会将bin按照section名称在输出文件下建立bin文件,按照文件名copy就可以了。

调试前,gdb server会下载其他Flash:
debug

且调试时RW段不会变成0xFF:
debug2

@github0null
Copy link
Owner

好的,感谢您的贡献。

@github0null github0null merged commit 4914e99 into github0null:master Jan 1, 2026
@Deadline039 Deadline039 deleted the fix-elf-large branch February 9, 2026 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants