hudiekaxp

[IAR]如何在代码中获取目标文件大小

0
阅读(5824)

        相信大家在Keil中有用过 Image$$Limit的方式来获取目标文件各个段的大小,以及整个目标文件的大小,IAR有没有类似的功能呢?答案是肯定的。

        如果大家看过赛 【原创】从零入手Kinetis系统开发(四)之启动代码分析这篇文章,就会知道,common_startup(void)这个函数有很多对各个段的操作:


   /*清零零初始化数据区*/
    /* Get the addresses for the .bss section (zero-initialized data) */
    uint8* bss_start = __section_begin(".bss");
    uint8* bss_end = __section_end(".bss");
   /*下面就是复制初始化数据区到RAM了*/
    /* Get the addresses for the .data section (initialized data section) */
    uint8* data_ram = __section_begin(".data");
    uint8* data_rom = __section_begin(".data_init");
    uint8* data_rom_end = __section_end(".data_init");
聪明的读者应该已经知道了,__section_size正是我们获取文件大小的法宝:


数好像已经对上了,欧耶。别着急,可能还有其他故事呢。

       咱们来看看实际的bin文件大小

Opening data file [D:\YouLong\YL-K26Z_demo_IAR\Examples\SPI\Demo_SPI_Master\IAR\Debug\Exe\Kinetis_L_Demo.bin] ...
 - Data file opened successfully (4840 bytes, 1 range, CRC = 0xAF6EE9B1)

怎么会这样,说好的3820呢?怎么会大这么多呢?

问题出在text + intvec并不完全,他还缺少了const类型的全局变量。从MAP文件可以看出:

    .rodata          const    0x000012e0    0x0  copy_init3.o [4]
  .iar.init_table    const    0x000012e0   0x24  - Linker created -
  Initializer bytes  ro data  0x00001304   0x1c  <for .data-1>
                            - 0x00001320  0xf10

Region$$Table$$Base    0x000012e0          --   Gb  - Linker created -
Region$$Table$$Limit   0x00001304          --   Gb  - Linker created -

Region$$Table$$Limit 是Initializer bytes的起始,它加上.data段的大小,刚好就是bin文件的大小:


Opening data file [D:\YouLong\YL-K26Z_demo_IAR\Examples\SPI\Demo_SPI_Master\IAR\Debug\Exe\Kinetis_L_Demo.bin] ...
 - Data file opened successfully (4896 bytes, 1 range, CRC = 0x4F8EB03C)