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哈哈~

版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章