IAR生成文件链接过程解析

  1. 项目编译时,IAR 的 C/C++ compiler 和 assembler,对工程的每个".c/.cpp/.asm"文件编译,分别生成一个.o的ELF可重定位目标文件。(内含的目标代码实际为机器码)

  2. 接着IAR linker对上步产生的 “.o”+“.icf”链接

  3. 最终产生可执行文件.out(.elf)。

  4. .bin和.hex文件是由.out文件转换而来的,先有.out文件再有.bin/hex文件。

(ELF:Executable and Linking Format 可执行和链接格式。ELF文件不同于我们常下载用的hex文件,Intel HEX文件常用来保存单片机或其他处理器的目标程序代码,是保存物理程序存储区的目标代码映像,而ELF文件不仅仅包括上述hex文件的信息,同时还包括符号表、链接信息、调试信息、源码信息等。这就是为什么我们可以将ELF文件转成hex文件,却不能将hex文件转成ELF文件。一些第三方调试软件或仿真软件需要使用ELF文件而不能使用hex或bin文件)

IAR的调试系统C-SPY会根据ELF文件产生进行JLIINK在线调试,因为该文件中包含了每个obj在内存中的绝对地址。而且对于函数而言,虽然其内部的局部变量在运行时的地址不是固定的,但由于程序经过固定程式的编译已经是确定性事件,所以C-SPY根据该文件的信息是可以预测的)

有两种设想(对于一个已经链接好了的最小单位镜像而言):

(1). 编译之后至链接之前:在镜像文件的头部设置标号,后面的bin引用地址的时候根据这个标号(基地址)偏移。这样,在linker链接的时候,就可以很方便的分配绝对地址了。

(2).在程序加载时,在镜像的头部设置一个变量,该变量可以动态改变[A1]。同样,其后的地址也以此偏移,这样就可以动态改变一个模块程序的存储器地址,实现动态加载、程序安装、管理了。

ELF文件格式参见《链接器与加载器》“Unix ELF格式”章节,或者wiki.

MAP文件格式分析如下

第一部分:

***PLACEMENT SUMMARY //位置总括

“A1”: place at 0x08000000 { ro section .intvec};----启动文件

“P1”: place in [from 0x08000000 to 0x0807ffff] { ro};-------代码与常量(.text、.rodata)

“P3”: place in [from 0x20000010 to 0x2000ffff] {

rw, block CSTACK, block HEAP };-----RAM区。规划.data 、.bss、栈、堆

Section Kind Address Size Object

“A1”:

.intvec ro code

“P1”

.text ro code

.rodata const

“P3”, part 1 of 3:

HEAP uninit

CSTACK uninit

.iar.dynexit uninit

“P3”, part 2 of 3:

.data inited

“P3”, part 3 of 3:

.bss zero

第二部分:

***INIT TABLE //启动过程中,变量的初始化

Address Size


Zero (__iar_zero_init3)------------------------------------.bss段

1destination range, total size 0x71c6:

0x2000362c 0x71c6

Copy/packbits (__iar_packbits_init3)--------------------------------.data段

1source range, total size 0x387 (84% of destination):

0x0801bcad 0x387

1destination range, total size 0x42e:

0x200031fc 0x42e

第三部分:

*** MODULE SUMMARY //模块概览

Module rocode ro data rw data

各个目标文件

(每个.c生成一个)只读代码常量变量

各个文件:

command line: [2]

动态库

dl7M_tl_if.a: [3]

rt7M_tl.a: [4]

shb_l.a: [5]

第四部分:

*** ENTRY LIST: 入口目录

Entry Address Size Type Object

实体名地址大小种类(Code or Data) Gb or Lc 目标文件

(函数/变量)

一、从目标文件到下载

二、目标文件的segment分配

三、程序的启动

1 When an application is started, thesystem startup code first performs hardware

initialization, such as initialization ofthe stack pointer to point at the end of the

predefined stack area:

2 Then, memories that should bezero-initialized are cleared, in other words, filled with

zeros:

3 For initialized data, data declared, forexample, like int i = 6; the initializers are

copied from ROM to RAM:

Figure 6: Initializing variables

4 Finally, the main function is called:

四、

类似于linux程序的编译过程以及SVN windows软件,IAR软件由工作区(windows外壳)和分散的模块组成(比如编译器 IarBuild、链接器ILink)组成。实质上,不用IAR提供的集成开发环境,使用那些命令行工具开发也是可行的。

IAR编译后,产生这几个目录.

(1)、Exe--------------execute 可执行文件包

/.bin:纯镜像文件

/.sim:flash loader 在烧flash时会用到*.sim文件

/.out:C-SPY JLINK在线调试用到的ELF/DWARF文件

以下摘自help:

Build considerations
When you build an application that will be downloaded to flash, specialconsideration is needed. Two output files must be generated. The first is theusual ELF/DWARF file (out) that provides the debugger with debug and symbolinformation. The second file is a simple-code file (filename extension sim)that will be opened and read by the flash loader when it downloads theapplication to flash memory.

The simple-code file must have the same path and name as the ELF/DWARF fileexcept for the filename extension. This file is automatically generated by thelinker.

(2).List

每个.c产生一个.lst及.s

/.lst list文件

/.s 汇编文件

(3)Obj

/.o 编译后生成的ELF可重定位目标文件

同时我做了相关的代码测试,首先新建一个工程,编译一个最简单的IO控制程序,生成的bin文件大小为512K,之后又做了一个相对contiki更复杂的工程文件,编译生成的bin文件大小仍为512K,打开二者生成的bin文件可以发现,文件固定大小为512K,但是有用的代码却不是这么多,剩余的部分被以所00填充,此时基本可以确定是工程本身设置的问题,所以从options开始对比排查,最终,确认了问题所在的原因,在link->Output栏,有一项output文件名称设置,先来看看IAR help对其的描述:

Output
The Output options determine the generated linker output.
Output filename
Sets the name of the ILINK output file. By default, the linker will use the project name with the filename extension out. To override the default name, specify an alternative name of the output file.
Note:
If you change the filename extension for linker ouput and want to use the output converter ielftool to convert the output, make sure ielftool will recognize the new filename extension. To achieve this, choose Tools>Filename Extension, select your toolchain, and click Edit. In the Filename Extension Overrides dialog box, select Output Converter and click Edit. In the Edit Filename Extensions dialog box, select Override and type the new filename extension and click OK. ielftool will now recognize the new filename extension.
Include debug information in output
Makes the linker generate an ELF output file including DWARF for debug information.

基本意思是说这个输出设置决定了生成的链接文件,默认的格式是.out格式的,如果你要改成其他格式,你必须得保证该格式的文件能够被ielftool文件识别,也就是上述所说的ELF文件,对比之下发现工程文件将此处设置成了.bin格式,按照help说明更改回默认的.out文件之后,生成的bin文件大小终于变为512K,THX,小问题,但是解决之路也不容易,总结一下大家共同学习,有说的不对的欢迎拍砖O(∩_∩)O哈哈~

版权声明:本文为博主原创文章,转载请附上博文链接!

原创文章 7 获赞 3 访问量 1万+

猜你喜欢

转载自blog.csdn.net/bingquan3333/article/details/84664862
今日推荐