1. 程式人生 > >IAR ILINK 連結指令碼

IAR ILINK 連結指令碼

sections在地址空間中的存放是由ILINK連結器來實現的,而ILINK連結器是按照使用者在ICF檔案中的規定來放置sections的,所以理 解ICF檔案的內容尤其重要。
一個標準的ICF檔案可包括下面這些內容:
1.       可編址的儲存空間(memory)
2.       不同的儲存器地址區域(region)
3.       不同的地址塊(block)
4.       Section的初始化與否
5.       Section在儲存空間中的放置
下 面介紹了幾條ICF檔案中常見的指令,詳細內容請參考ILINK相關說明文件(EWARM_DevelopmentGuide.pdf):
1.    define [ exported ] symbol name = expr;
作用:   指定某個符號的值


引數:    exported 匯出該symbol,使其對可執行映象可用
name     --符號名
expr     --符號值
舉例:
define symbol RAM_START_ADDRESS = 0x40000000;
define symbol RAM_END_ADDRESS = 0x4000FFFF;    
2.    define memory name with size = expr [, unit-size];
作用:   定義一個可編址的儲存地址空間(memory)。

引數:    name     --memory的名稱
expr     --地址空間的大小
unit-size --expr的單位,可以是位(unitbitsize),預設是位元組(unitbytesize)
舉例:
define memory MEM with size = 4G;
3.    define region name = region-expr;
作用:    定義一個儲存地址區域(region)。一個區域可由一個或多個範圍組成,每個範圍內地址必須連續,但幾個範圍之間不必是連續的。
引數:    name region的名稱
region-expr memory:[from expr { to expr | size expr}],可以定義起止範圍,也可以定義起始地址和region的大小
舉例:
define region ROM = MEM:[from 0x0 size 0x10000];
define region ROM = MEM:[from 0x0 to 0xFFFF];


4.    define block name[ with param, param... ]
{
extended-selectors
};
作用:    定義一個地址塊(block);它可以是個空塊,比如棧、堆;也可以包含一系列sections。
引數:    name     block的名稱
param 可以是:     size = expr (塊的大小)
maximum size = expr (塊大小的上限)
alignment = expr (最小對齊位元組數)
fixed order (按照固定順序放置sections)
extended-selector [ first | last ] { section-selector | block name | overlay name }
first 最先存放
last 最後存放
section-selector [ section-attribute ][section-type][ section sectionname ][object filename ]

section-attribute--  [ ro  [ code | data ] | rw  [ code | data ] | zi ]

section-type – preinit_array|init_array
sectionname -- section的名稱
object filename 目標檔案的名稱
即可以按照section的屬性,名稱及其所在目標檔案的名稱這三個過濾條件中,任意選取一個條件,或選取多個條件進行組合,來圈定所要求的 sections。
name block或overlay的名稱
舉例:
define block HEAP with size = 0x1000, alignment = 4 { };
define block MYBLOCK1 = { section mysection1, section mysection2, readwrite };
define block MYBLOCK2 = { readonly object myfile2.o };
5.    initialize { by copy | manually } [ with param, param... ]
{
section-selectors
};
作用:    初始化sections。
引數:    by copy 在程式啟動時自動執行初始化。
manually 在程式啟動時不自動執行初始化。
param 可以是: packing = { none | compress1 | compress2 | auto }
copy routine = functionname
packing表示是否壓縮資料,預設是auto。
functionname表示是否使用自己的拷貝函式來取代預設函式。
section-selector 同上
舉例:
initialize by copy { rw };
6.    do not initialize
{
section-selectors
};
作用:    規定在程式啟動時不需要初始化的sections。一般用於__no_init宣告的變數段(.noinit)。
引數:    section-selector 同上
舉例:
do not initialize { .noinit };
7.    place at { address memory[: expr] | start of region_expr | end of region_expr }
{
extended-selectors
};
作用:    把一系列sections和blocks放置在某個具體的地址,或者一個region的開始或者結束處。
引數:    memory memory的名稱
expr 地址值,該地址必須在memory所定義的範圍內
region_expr region的名稱
extended-selector 同上
舉例:
place at start of ROM { section .cstart }; place at end of ROM { section .checksum }; place at address MEM:0x0 { section .intvec };
8.    place in region-expr
{
extended-selectors
};
作用:    把一系列sections和blocks放置在某個region中。sections和blocks將按任意順序放置。
引數:    region-expr region的名稱
extended-selector 同上
舉例:
place in ROM { readonly };         /* all readonly sections */
place in RAM { readwrite };         /* all readwrite sections */
place in RAM { block HEAP, block CSTACK, block IRQ_STACK };
place in ROM { section .text object myfile.o };     /* the .text section of myfile.o */
place in ROM { readonly object myfile.o };         /* all read-only sections of myfile.o */
place in ROM { readonly data object myfile.o };     /* all read-only data sections myfile.o */
FROM:http://blog.21ic.com/user1/5910/archives/2009/61982.html
IAR的ICF檔案中巨集給程式使用

如果想定在rom空間

icf中:

place at address mem:0x08090000 { readonly section .test };

C:中:

#pragma location = ".test"
const u32 uiData[512];// const 切不可省略

如果想定在ram空間

icf中:

place at address mem:0x08090000 { readwrite section .test };

C:中:

#pragma location = ".test"
u32 uiData[512];

可發現uiData的值為0x08090000

該法可方便通過ICF指定某變數的地址。

程式也可以。寫法類似.vector

如果用block的方。類似

define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };

C中:

#pragma language="extended"
#pragma segment="CSTACK"

ptr = __sfe( "CSTACK" );可得到CSTACK的高階地址+1

__sfe: Returns last address of segment.