10.5 リンカ(linker)

  2.ldによる実行可能モジュール生成の実際
 asが生成したオブジェクトコード(hello.o)を用いて、ldが実行可能モジュールの生成を行います。実行可能モジュール(objdumpを用いて逆アセンブルしたもの)を以下に示します。
<hello.o>
   ↓
 [リンク、実行可能コード生成:ld(collect2)]
   ↓
<hello>(「objdump -d hello」で表示)

 hello:     file format elf32-i386

 Disassembly of section .init:

 08048230 <_init>:
  8048230:       55                      push   %ebp
                   :
                   :
  804824e:       89 f6                   mov    %esi,%esi
 Disassembly of section .plt:

 08048250 <.plt>:
  8048250:       ff 35 e4 92 04 08       pushl  0x80492e4
                   :
                   :
  804827b:       e9 d0 ff ff ff          jmp    8048250 <_init+0x20>
 Disassembly of section .text:

 08048280 <_start>:
  8048280:       31 ed                   xor    %ebp,%ebp
                   :
                   :
  80482a3:       90                      nop

 080482a4 <main>:
  80482a4:       55                      push   %ebp
  80482a5:       89 e5                   mov    %esp,%ebp
  80482a7:       68 cc 82 04 08          push   $0x80482cc
  80482ac:       e8 bf ff ff ff          call   8048270 <_init+0x40>
  80482b1:       c9                      leave
  80482b2:       c3                      ret
 Disassembly of section .fini:

 080482b4 <_fini>:
  80482b4:       55                      push   %ebp
                   :
                   :
  80482be:       81 c3 23 10 00 00       add    $0x1023,%ebx
 前述の再配置可能オブジェクトファイルのなかで、push $0x0や<main+0x9>などと相対番地指定されていた箇所が、リンカによってモジュール中でのアロケーションが決定され、それぞれ、push $0x80482cc、<_init+0x40>などと配置が決定されていることがわかります。

 前者のpushすべきメモリ領域は、ターゲット上で実行可能モジュールがロードされた後に領域確保され、絶対位置が決定されて確定します。後者の<_init+0x40>なる場所も、ターゲット上で実行可能モジュールがロードされた時点で、オペレーティングシステムからプログラム(コード)領域から.initの絶対位置が決定されて確定します。