OS自作入門 3日目C関数コンパイルするときつけるオプション-Tとlinker script #osdevjp

30日でできる!OS自作入門(3日目)[Ubuntu16.04/NASM] - Qiita

↑を参考に。

linker scriptはここが大本っぽい 『30日でできる!OS自作入門』のメモ

まずgcc-Tオプションでhar.ld指定しているけど、Tオプションとは

-T script Use script as the linker script. This option is supported by most systems using the GNU linker. On some targets, such as bare-board targets without an operating system, the -T option may be required when linking to avoid references to undefined symbols. Using the GNU Compiler Collection (GCC): Link Options

リンカースクリプトはここ由来っぽい。ありがてえ。。 vanya.jp.net

ldのマニュアル Top (LD)

なるほどリンカーコマンド言語

ld accepts Linker Command Language files written in a superset of AT&T’s Link Editor Command Language syntax, to provide explicit and total control over the linking process.

ついでにマニュアル何ページか読む

LinkerScriptわからんのでコンセプト読んでみた

Basic Script Concepts (LD)

オブジェクトはsectionで出来ててobjdumpにhオプションつけると見れる

やってみた(途中まではる)

% objdump -h bin/mruby

bin/mruby:     ファイル形式 elf64-x86-64

セクション:
索引名          サイズ      VMA               LMA               File off  Algn
  0 .init         00000003  0000000000400120  0000000000400120  00000120  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .text         0006f8e0  0000000000400130  0000000000400130  00000130  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .fini         00000003  000000000046fa10  000000000046fa10  0006fa10  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  3 .rodata       00022578  000000000046fa20  000000000046fa20  0006fa20  2**5
                  CONTENTS, ALLOC, LOAD, READONLY, DATA

nmコマンドでシンボルとれるらしい

You can see the symbols in an object file by using the nm program, or by using the objdump program with the ‘-t’ option.

やってみる、お、見慣れた名前だ

% nm bin/mruby | tail
0000000000444bd0 t yyparse
00000000004896c0 r yypgoto
000000000047dac0 r yyr1
000000000047d860 r yyr2
000000000043fbd0 t yysyntax_error
0000000000483a60 r yytable
000000000048a860 r yytname
000000000048b280 r yytranslate
000000000043f490 t yywarn
000000000043f5e0 t yywarning_s

LinkerScriptのマニュアル読み進めていく、OUTPUT_FORMATでてきた!!!

The OUTPUT_FORMAT command names the BFD format to use for the output file (see BFD). Format Commands (LD)

objdump -iOUTPUT_FORMATで使えるフォーマットが取れるっぽい

You can use objdump -i (see objdump in The GNU Binary Utilities) to list all the formats available for your configuration. BFD (LD)

% objdump -i
BFD ヘッダファイルバージョン (GNU Binutils for Debian) 2.28
elf64-x86-64
 (header little endian, data little endian)
  i386
elf32-i386
 (header little endian, data little endian)
  i386
elf32-iamcu
 (header little endian, data little endian)
  iamcu
elf32-x86-64
 (header little endian, data little endian)
  i386
a.out-i386-linux
 (header little endian, data little endian)
  i386
pei-i386
 (header little endian, data little endian)
  i386
pei-x86-64
 (header little endian, data little endian)
  i386
elf64-l1om
 (header little endian, data little endian)
  l1om
elf64-k1om
 (header little endian, data little endian)
  k1om
elf64-little
 (header little endian, data little endian)
  i386
  l1om
  k1om
  iamcu
  plugin
elf64-big
 (header big endian, data big endian)
  i386
  l1om
  k1om
  iamcu
  plugin
elf32-little
 (header little endian, data little endian)
  i386
  l1om
  k1om
  iamcu
  plugin
elf32-big
 (header big endian, data big endian)
  i386
  l1om
  k1om
  iamcu
  plugin
pe-x86-64
 (header little endian, data little endian)
  i386
pe-bigobj-x86-64
 (header little endian, data little endian)
  i386
pe-i386
 (header little endian, data little endian)
  i386
plugin
 (header little endian, data little endian)
srec
 (header endianness unknown, data endianness unknown)
  i386
  l1om
  k1om
  iamcu
  plugin
symbolsrec
 (header endianness unknown, data endianness unknown)
  i386
  l1om
  k1om
  iamcu
  plugin
verilog
 (header endianness unknown, data endianness unknown)
  i386
  l1om
  k1om
  iamcu
  plugin
tekhex
 (header endianness unknown, data endianness unknown)
  i386
  l1om
  k1om
  iamcu
  plugin
binary
 (header endianness unknown, data endianness unknown)
  i386
  l1om
  k1om
  iamcu
  plugin
ihex
 (header endianness unknown, data endianness unknown)
  i386
  l1om
  k1om
  iamcu
  plugin

         elf64-x86-64 elf32-i386 elf32-iamcu elf32-x86-64 a.out-i386-linux 
    i386 elf64-x86-64 elf32-i386 ----------- elf32-x86-64 a.out-i386-linux
    l1om ------------ ---------- ----------- ------------ ----------------
    k1om ------------ ---------- ----------- ------------ ----------------
   iamcu ------------ ---------- elf32-iamcu ------------ ----------------
  plugin ------------ ---------- ----------- ------------ ----------------

         pei-i386 pei-x86-64 elf64-l1om elf64-k1om elf64-little elf64-big 
    i386 pei-i386 pei-x86-64 ---------- ---------- elf64-little elf64-big
    l1om -------- ---------- elf64-l1om ---------- elf64-little elf64-big
    k1om -------- ---------- ---------- elf64-k1om elf64-little elf64-big
   iamcu -------- ---------- ---------- ---------- elf64-little elf64-big
  plugin -------- ---------- ---------- ---------- elf64-little elf64-big

         elf32-little elf32-big pe-x86-64 pe-bigobj-x86-64 pe-i386 plugin srec 
    i386 elf32-little elf32-big pe-x86-64 pe-bigobj-x86-64 pe-i386 ------ srec
    l1om elf32-little elf32-big --------- ---------------- ------- ------ srec
    k1om elf32-little elf32-big --------- ---------------- ------- ------ srec
   iamcu elf32-little elf32-big --------- ---------------- ------- ------ srec
  plugin elf32-little elf32-big --------- ---------------- ------- ------ srec

         symbolsrec verilog tekhex binary ihex 
    i386 symbolsrec verilog tekhex binary ihex
    l1om symbolsrec verilog tekhex binary ihex
    k1om symbolsrec verilog tekhex binary ihex
   iamcu symbolsrec verilog tekhex binary ihex
  plugin symbolsrec verilog tekhex binary ihex

SECTIONSはこれ

SECTIONS (LD)

文法

SECTIONS
{
  sections-command
  sections-command
  …
}

.headとかの記法はこれ

Output Section Description (LD)

The full description of an output section looks like this:

section [address] [(type)] :
  [AT(lma)]
  [ALIGN(section_align) | ALIGN_WITH_INPUT]
  [SUBALIGN(subsection_align)]
  [constraint]
  {
    output-section-command
    output-section-command
    …
  } [>region] [AT>lma_region] [:phdr :phdr …] [=fillexp] [,]

LONGとかはこれ

Output Section Data (LD)

ADDR, SIZEOF, LOADADDRは組み込み関数 Builtin Functions (LD)

ATはキーワード

The AT keyword takes an expression as an argument. This specifies the exact load address of the section. Output Section LMA (LD)

というかさっきのSECTIONSの構文にちゃんと登場しとる

このhar.ldざっとみたかんじ

30日でできる!OS自作入門(3日目)[Ubuntu16.04/NASM] - Qiita

3つセクションがある

  • .headセクション
  • .textセクション
  • .dataセクション

はじまりの位置

  • .headセクションは0x00からはじまる。
  • .dataセクションは0x31000000から

VMA/LMAの対応はこのページの疑似コードが分かりやすい、ROMからコピー

Output Section LMA (LD)

*(.data)は全ての入力ファイルにマッチするので*、そのなかの.dataセクション全てを表してるっぽい

Input Section Wildcards (LD)

DISCARDはとくべつで、ここにマッチしたセクションは破棄されるっぽい Output Section Discarding (LD)

つまり.eh_frameのセクションが捨てられる

eh_frameはerror handling frameの略なのかな、C++の例外の解決に使われるよう

.rodataは定数が入るっぽい

.bssは0初期化されてる値、なるほど

BSSはBlock Started by Symbolというアセンブラの疑似命令に由来する。 BSSはBlock Started by Symbolというアセンブラの疑似命令に由来する。

一応命令一通りマニュアルみてみたけど、自分で書けるかというとびみょうそう。

このページすごいべんりだ... 『30日でできる!OS自作入門』のメモ

いいなと思ったらKyashでお金を下さい
20191128011151
GitHubスポンサーも受け付けています
https://github.com/sponsors/hanachin/