ldのメモ

2013/10/15binutils::ldimport
documentをベースに、テキトウに伸ばしていく予定。
とりあえずは斜め読みの抜粋。

参照資料
The GNU linker ld (Sourcery G++ Lite 2011.03-41) Version 2.20.51

"3 Linker Scripts"

'-T'で独自のリンカスクリプトを適用することができます。
'--verbose'を付与しておけば、デフォルトlinker scriptを見ることができます。

"3.1 Basic Linker Script Concepts"

::
VMA
virtual memory address.仮想メモリアドレス。実行時に参照するべきアドレス。\一般的にはRAMになる。
LMA
load memory address.読み出されるアドレス。実行前にここからVMAへ転送する。(独自のscriptを作るなら自力転送?)\一般的にはROMになるだろう。RAMにロードしておいて、startup routineが走ることもありえるかもね..
objdump -hでセクションを見ることができる。

"3.2 Linker Script Format"

Linker scriptはテキストファイル。
コメントは "/* 〜 */"で。 "//"は使えないぽい?

"3.3 Simple Linker Script Example"

 SECTIONS
 {
   . = 0x10000;
   .text : { *(.text) }
   . = 0x8000000;
   .data : { *(.data) }
   .bss : { *(.bss) }
 }

"3.4 Simple Linker Script Commands"

"3.4.1 Setting the Entry Point"
エントリーポイント指定。上から順番?
とりあえず"start"シンボルを作っておく。
  • the ‘-e’ entry command-line option;
  • the ENTRY(symbol ) command in a linker script;
  • the value of a target specific symbol, if it is defined; For many targets this is start, but PE and BeOS based systems for example check a list of possible entry symbols, matching the first one found.
  • the address of the first byte of the ‘.text’ section, if present;
  • The address 0.

"3.4.2 Commands Dealing with Files"
::
INCLUDE filename
INPUT(file, file, ...) INPUT(file file ...)
GROUP(file, file, ...) GROUP(file file ...)
AS_NEEDED(file, file, ...) AS_NEEDED(file file ...)
OUTPUT(filename)
SEARCH_DIR(path)
STARTUP(filename)
"3.4.3 Commands Dealing with Object File Formats"
::
OUTPUT_FORMAT(bfdname) OUTPUT_FORMAT(default, big, little)
TARGET(bfdname)

"3.4.4 Assign alias names to memory regions"
REGIONのaliasを定義して、ユースケースごとに切り替えられるようにできるっぽい。
とりあえずパス。
"3.4.5 Other Linker Script Commands"
::
ASSERT(exp, message )
式(exp)が非ゼロであることを確認する。ゼロ出会った場合、linkerはエラーを吐いて終了する。
EXTERN(symbol symbol ...)
symbolを強制的に未定義とします。(・・・まだ、嬉しさがわからない)
コマンドラインの'-u'オプション相当。
FORCE_COMMON_ALLOCATION
コマンドラインの'-d'オプション相当。relocatable output fileが与えられたとしても、common symbolに割り当てるようにします。
INHIBIT_COMMON_ALLOCATION
コマンドラインの'--no-define-common'相当。non-relocatable output fileが与えられたとしても、common symbolへのアドレス割り当てを省略するようにします。
INSERT [ AFTER | BEFORE ] output_section
複数のscriptを混ぜるときに使えそう?例示されているので後で見る。[ToDo]
NOCROSSREFS(section section ...)
output sectionを並べておくと、そのsection間でシンボル解決が出来なかった場合にエラーを吐いて止まる。特定セクションに関数を閉じ込めていることを確認するのに使える、か。
OUTPUT_ARCH(bfdarch)
出力形式をセットする。BFD libraryで使われるmachine architectureを入れる。

"3.5 Assigning Values to Symbols"

"3.5.1 Simple Assignments"
こんなのが使える。
 symbol = expression ;
 symbol += expression ;
 symbol -= expression ;
 symbol *= expression ;
 symbol /= expression ;
 symbol <<= expression ;
 symbol >>= expression ;
 symbol &= expression ;
 symbol |= expression ;
"3.5.4 Source Code Reference"
script中の変数は、Cの変数に"_"を付与したシンボルが対応する。
当該シンボルは、以下のexport宣言により参照できる。この変数が、
scriptで代入されたアドレスにマッピングされる、と見れば良いので、
&演算子によってアドレスを参照する。
extern int foo;


"3.6 SECTIONS Command"

 SECTIONS
 {
   sections-command
   sections-command
   ...
 }
  • an ENTRY command (see Section 3.4.1 [Entry command], page 39)
  • a symbol assignment (see Section 3.5 [Assignments], page 45)
  • an output section description
  • an overlay description
"3.6.1 Output Section Description"
output sectionの定義は以下のとおり。全部入り。
section [address] [(type)] :
 [AT(lma)]
 [ALIGN(section_align)]
 [SUBALIGN(subsection_align)]
 [constraint]
 {
  output-section-command
  output-section-command
  ...
 } [>region] [AT>lma_region] [:phdr :phdr ...] [=fillexp]
output-section-commandには、Input Sectionを並べることになる。。
オブジェクト名、シンボル名、セクション名で引っ掛けて、それをsectionにまとめる。
fillexp... 空きエリアをfillexpで埋める?3.6.5の"FILL(0x90909090)"で埋めるとか。

"3.6.8.2 Output Section LMA"
"3.6.9 Overlay Description"
section typeのOVERLAYではなく、OVERLAYというsection。
LMA/VMAが同一の場合。

"3.7 MEMORY Command"

 MEMORY
 {
   name [(attr )] : ORIGIN = origin, LENGTH = len
   ...
 }
nameを定義して、有効範囲を決める。regionとしてsection閉じカッコの後ろに並べる。

"3.10"あたりに、scriptで使える式・関数(マクロ?)の解説あり。