ここではELF形式を確認。
ELF形式のファイルを作るgccも無事インストールが終了できたので、ELF形式の実行ファイルを作成してreadelfコマンドを実行してみた。これをベースにコメントしてみる。
$readelf -a kzload.elf
ELF Header:
Magic: 7f 45 4c 46 01 02 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, big endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Hitachi H8/300
Version: 0x1
Entry point address: 0x100
Start of program headers: 52 (bytes into file)
Start of section headers: 1544 (bytes into file)
Flags: 0x810000
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 3
Size of section headers: 40 (bytes)
Number of section headers: 9
Section header string table index: 6
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .vectors PROGBITS 00000000 000094 000100 00 WA 0 0 4
[ 2] .text PROGBITS 00000100 000194 0003b0 00 AX 0 0 2
[ 3] .rodata PROGBITS 000004b0 000544 000071 01 AMS 0 0 1
[ 4] .data PROGBITS 00fffc20 0005b8 000010 00 WA 0 0 4
[ 5] .bss NOBITS 00fffc30 0005c8 000004 00 WA 0 0 2
[ 6] .shstrtab STRTAB 00000000 0005c8 00003d 00 0 0 1
[ 7] .symtab SYMTAB 00000000 000770 0004c0 10 8 30 4
[ 8] .strtab STRTAB 00000000 000c30 0001f9 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x000094 0x00000000 0x00000000 0x00100 0x00100 RW 0x1
LOAD 0x000194 0x00000100 0x00000100 0x00421 0x00421 R E 0x1
LOAD 0x0005b8 0x00fffc20 0x00000521 0x00010 0x00014 RW 0x1
Section to Segment mapping:
Segment Sections...
00 .vectors
01 .text .rodata
02 .data .bss
There is no dynamic segment in this file.
There are no relocations in this file.
There are no unwind sections in this file.
Symbol table '.symtab' contains 76 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
2: 00000100 0 SECTION LOCAL DEFAULT 2
3: 000004b0 0 SECTION LOCAL DEFAULT 3
4: 00fffc20 0 SECTION LOCAL DEFAULT 4
5: 00fffc30 0 SECTION LOCAL DEFAULT 5
6: 00000000 0 FILE LOCAL DEFAULT ABS vector.c
7: 0000010a 0 NOTYPE LOCAL DEFAULT 2 .L11
8: 00000000 0 FILE LOCAL DEFAULT ABS main.c
9: 00fffc22 2 OBJECT LOCAL DEFAULT 4 _static_data
10: 0000010c 148 NOTYPE LOCAL DEFAULT 2 _printval
11: 00fffc30 2 OBJECT LOCAL DEFAULT 5 _static_bss
12: 00000220 0 NOTYPE LOCAL DEFAULT 2 .L4
13: 00000000 0 FILE LOCAL DEFAULT ABS lib.c
14: 00000236 0 NOTYPE LOCAL DEFAULT 2 .L7
15: 0000022a 0 NOTYPE LOCAL DEFAULT 2 .L8
16: 00000258 0 NOTYPE LOCAL DEFAULT 2 .L15
17: 0000024a 0 NOTYPE LOCAL DEFAULT 2 .L16
18: 00000292 0 NOTYPE LOCAL DEFAULT 2 .L26
19: 0000028a 0 NOTYPE LOCAL DEFAULT 2 .L21
20: 00000284 0 NOTYPE LOCAL DEFAULT 2 .L22
21: 00000294 0 NOTYPE LOCAL DEFAULT 2 .L17
22: 00000270 0 NOTYPE LOCAL DEFAULT 2 .L27
23: 000002b0 0 NOTYPE LOCAL DEFAULT 2 .L34
24: 000002a6 0 NOTYPE LOCAL DEFAULT 2 .L35
25: 000002ce 0 NOTYPE LOCAL DEFAULT 2 .L38
26: 000002c0 0 NOTYPE LOCAL DEFAULT 2 .L37
27: 000002e4 0 NOTYPE LOCAL DEFAULT 2 .L53
28: 00000300 0 NOTYPE LOCAL DEFAULT 2 .L50
29: 000002fa 0 NOTYPE LOCAL DEFAULT 2 .L45
30: 000002f4 0 NOTYPE LOCAL DEFAULT 2 .L46
31: 00000302 0 NOTYPE LOCAL DEFAULT 2 .L41
32: 000002dc 0 NOTYPE LOCAL DEFAULT 2 .L55
33: 00000332 0 NOTYPE LOCAL DEFAULT 2 .L57
34: 0000032c 0 NOTYPE LOCAL DEFAULT 2 .L60
35: 00000326 0 NOTYPE LOCAL DEFAULT 2 .L61
36: 00000340 0 NOTYPE LOCAL DEFAULT 2 .L56
37: 0000033a 0 NOTYPE LOCAL DEFAULT 2 .L59
38: 0000033e 0 NOTYPE LOCAL DEFAULT 2 .L58
39: 00000318 0 NOTYPE LOCAL DEFAULT 2 .L63
40: 00000364 0 NOTYPE LOCAL DEFAULT 2 .L65
41: 00000390 0 NOTYPE LOCAL DEFAULT 2 .L71
42: 00000384 0 NOTYPE LOCAL DEFAULT 2 .L72
43: 000003be 0 NOTYPE LOCAL DEFAULT 2 .L86
44: 000003e2 0 NOTYPE LOCAL DEFAULT 2 .L75
45: 00000000 0 FILE LOCAL DEFAULT ABS serial.c
46: 00fffc24 12 OBJECT LOCAL DEFAULT 4 _regs
47: 00000482 0 NOTYPE LOCAL DEFAULT 2 .L4
48: 000002d6 50 NOTYPE GLOBAL DEFAULT 2 _strcmp
49: 00fffc32 2 OBJECT GLOBAL DEFAULT 5 _global_bss
50: 00000000 256 OBJECT GLOBAL DEFAULT 1 _vectors
51: 0000034a 46 NOTYPE GLOBAL DEFAULT 2 _putc
52: 000004b0 0 NOTYPE GLOBAL DEFAULT 2 _etext
53: 00000378 36 NOTYPE GLOBAL DEFAULT 2 _puts
54: 00fffc30 0 NOTYPE GLOBAL DEFAULT 5 _bss_start
55: 00000100 0 NOTYPE GLOBAL DEFAULT 2 _text_start
56: 00000436 36 NOTYPE GLOBAL DEFAULT 2 _serial_is_send_enable
57: 00fffc34 0 NOTYPE GLOBAL DEFAULT 5 _ebss
58: 0000023c 40 NOTYPE GLOBAL DEFAULT 2 _memcpy
59: 00000222 26 NOTYPE GLOBAL DEFAULT 2 _memset
60: 00000100 0 NOTYPE GLOBAL DEFAULT 2 _start
61: 00fffc20 0 NOTYPE GLOBAL DEFAULT 4 _data_start
62: 00000400 54 NOTYPE GLOBAL DEFAULT 2 _serial_init
63: 0000039c 100 NOTYPE GLOBAL DEFAULT 2 _putxval
64: 000002b8 30 NOTYPE GLOBAL DEFAULT 2 _strcpy
65: 00fffc20 2 OBJECT GLOBAL DEFAULT 4 _global_data
66: 00000264 58 NOTYPE GLOBAL DEFAULT 2 _memcmp
67: 00000308 66 NOTYPE GLOBAL DEFAULT 2 _strncmp
68: 000004b0 0 NOTYPE GLOBAL DEFAULT 3 _rodata_start
69: 00fffc30 0 NOTYPE GLOBAL DEFAULT 4 _edata
70: 00fffc34 0 NOTYPE GLOBAL DEFAULT ABS _end
71: 0000029e 26 NOTYPE GLOBAL DEFAULT 2 _strlen
72: 0000045a 86 NOTYPE GLOBAL DEFAULT 2 _serial_send_byte
73: 00ffff00 0 NOTYPE GLOBAL DEFAULT 5 _stack
74: 000001a0 130 NOTYPE GLOBAL DEFAULT 2 _main
75: 00000521 0 NOTYPE GLOBAL DEFAULT 3 _erodata
No version information found in this file.
■メモリの領域とセクション
ここで注目すべきところは以下の内容である。
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .vectors PROGBITS 00000000 000094 000100 00 WA 0 0 4
[ 2] .text PROGBITS 00000100 000194 0003b0 00 AX 0 0 2
[ 3] .rodata PROGBITS 000004b0 000544 000071 01 AMS 0 0 1
[ 4] .data PROGBITS 00fffc20 0005b8 000010 00 WA 0 0 4
[ 5] .bss NOBITS 00fffc30 0005c8 000004 00 WA 0 0 2
[ 6] .shstrtab STRTAB 00000000 0005c8 00003d 00 0 0 1
[ 7] .symtab SYMTAB 00000000 000770 0004c0 10 8 30 4
[ 8] .strtab STRTAB 00000000 000c30 0001f9 00 0 0 1
ELF形式は、ファイルの内部をセクションという単位で区切って管理している。
実はこのセクションがメモリの領域と対応しているということだ。
対応を以下に示ておきます。
[ 2] .text…テキスト領域に置かれるべき情報
[ 4] .data…データ領域に置かれるべき情報
[ 5] .bss…BSS領域に置かれるべき情報
一方、[ 1]/[ 3]/[ 6]/[ 7]/[ 8]の他の領域はどこに置かれるべきなのか?
もしかしてメモリの領域というのは3領域だけでなく他にもあるのかなどいろいろ浮かんできますが、とりあえずわからないので置いておきます。
ちなみにどんなセクションがあるか把握して意味まで確認できそうなありがたいサイト発見、ここ
■関数や変数の配置
readelfは他にもいろいろと情報が出しているようですので、もう少し覗いてみることにします。
こんどはここに注目。
Symbol table '.symtab' contains 76 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
2: 00000100 0 SECTION LOCAL DEFAULT 2
3: 000004b0 0 SECTION LOCAL DEFAULT 3
4: 00fffc20 0 SECTION LOCAL DEFAULT 4
5: 00fffc30 0 SECTION LOCAL DEFAULT 5
6: 00000000 0 FILE LOCAL DEFAULT ABS vector.c
7: 0000010a 0 NOTYPE LOCAL DEFAULT 2 .L11
8: 00000000 0 FILE LOCAL DEFAULT ABS main.c
9: 00fffc22 2 OBJECT LOCAL DEFAULT 4 _static_data
10: 0000010c 148 NOTYPE LOCAL DEFAULT 2 _printval
11: 00fffc30 2 OBJECT LOCAL DEFAULT 5 _static_bss
12: 00000220 0 NOTYPE LOCAL DEFAULT 2 .L4
13: 00000000 0 FILE LOCAL DEFAULT ABS lib.c
14: 00000236 0 NOTYPE LOCAL DEFAULT 2 .L7
15: 0000022a 0 NOTYPE LOCAL DEFAULT 2 .L8
16: 00000258 0 NOTYPE LOCAL DEFAULT 2 .L15
……………………………………………………
……………………………………………………
……………………………………………………
42: 00000384 0 NOTYPE LOCAL DEFAULT 2 .L72
43: 000003be 0 NOTYPE LOCAL DEFAULT 2 .L86
44: 000003e2 0 NOTYPE LOCAL DEFAULT 2 .L75
45: 00000000 0 FILE LOCAL DEFAULT ABS serial.c
46: 00fffc24 12 OBJECT LOCAL DEFAULT 4 _regs
47: 00000482 0 NOTYPE LOCAL DEFAULT 2 .L4
48: 000002d6 50 NOTYPE GLOBAL DEFAULT 2 _strcmp
49: 00fffc32 2 OBJECT GLOBAL DEFAULT 5 _global_bss
50: 00000000 256 OBJECT GLOBAL DEFAULT 1 _vectors
51: 0000034a 46 NOTYPE GLOBAL DEFAULT 2 _putc
52: 000004b0 0 NOTYPE GLOBAL DEFAULT 2 _etext
53: 00000378 36 NOTYPE GLOBAL DEFAULT 2 _puts
54: 00fffc30 0 NOTYPE GLOBAL DEFAULT 5 _bss_start
55: 00000100 0 NOTYPE GLOBAL DEFAULT 2 _text_start
56: 00000436 36 NOTYPE GLOBAL DEFAULT 2 _serial_is_send_enable
57: 00fffc34 0 NOTYPE GLOBAL DEFAULT 5 _ebss
58: 0000023c 40 NOTYPE GLOBAL DEFAULT 2 _memcpy
59: 00000222 26 NOTYPE GLOBAL DEFAULT 2 _memset
60: 00000100 0 NOTYPE GLOBAL DEFAULT 2 _start
61: 00fffc20 0 NOTYPE GLOBAL DEFAULT 4 _data_start
62: 00000400 54 NOTYPE GLOBAL DEFAULT 2 _serial_init
63: 0000039c 100 NOTYPE GLOBAL DEFAULT 2 _putxval
64: 000002b8 30 NOTYPE GLOBAL DEFAULT 2 _strcpy
65: 00fffc20 2 OBJECT GLOBAL DEFAULT 4 _global_data
66: 00000264 58 NOTYPE GLOBAL DEFAULT 2 _memcmp
67: 00000308 66 NOTYPE GLOBAL DEFAULT 2 _strncmp
68: 000004b0 0 NOTYPE GLOBAL DEFAULT 3 _rodata_start
69: 00fffc30 0 NOTYPE GLOBAL DEFAULT 4 _edata
70: 00fffc34 0 NOTYPE GLOBAL DEFAULT ABS _end
71: 0000029e 26 NOTYPE GLOBAL DEFAULT 2 _strlen
72: 0000045a 86 NOTYPE GLOBAL DEFAULT 2 _serial_send_byte
73: 00ffff00 0 NOTYPE GLOBAL DEFAULT 5 _stack
74: 000001a0 130 NOTYPE GLOBAL DEFAULT 2 _main
75: 00000521 0 NOTYPE GLOBAL DEFAULT 3 _erodata
ここでは、関数や変数の配置情報が含まれているようです。
変数ではここの行に注目。
46: 00fffc24 12 OBJECT LOCAL DEFAULT 4 _regs
serial.c中に書かれたregs配列変数は0x00fffc24に割り当てられることを意味しているようです。
そこは、.dataセクションに属しているのでデータ領域、ということはRAMに置かれるようです。
関数ではここの行に注目。
74: 000001a0 130 NOTYPE GLOBAL DEFAULT 2 _main
main.c中に書かれたmainは0x000001a0に割り当てられ、.textセクションに属しているのでテキスト領域、ということはROMに置かれるようです。
※ちなみに、regsの前に_がついていますが、次の説明が書いていありました。
リンカは関数名や変数名をシンボルとして扱います。関数や変数の実体はメモリ上にあるので、シンボルはそれらが存在している特定のアドレスに、便宜上名前をつけたものだそうです。
複数のファイルに定義した関数や変数はここで一括して管理されアドレスが振られています。
リンクを実施してELF形式の実行形式ファイルを作成するなかで複数のソースファイルに記述された内容が集められアドレスの配置まで決まっているようです。
ELF形式のファイルを作るgccも無事インストールが終了できたので、ELF形式の実行ファイルを作成してreadelfコマンドを実行してみた。これをベースにコメントしてみる。
$readelf -a kzload.elf
ELF Header:
Magic: 7f 45 4c 46 01 02 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, big endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Hitachi H8/300
Version: 0x1
Entry point address: 0x100
Start of program headers: 52 (bytes into file)
Start of section headers: 1544 (bytes into file)
Flags: 0x810000
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 3
Size of section headers: 40 (bytes)
Number of section headers: 9
Section header string table index: 6
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .vectors PROGBITS 00000000 000094 000100 00 WA 0 0 4
[ 2] .text PROGBITS 00000100 000194 0003b0 00 AX 0 0 2
[ 3] .rodata PROGBITS 000004b0 000544 000071 01 AMS 0 0 1
[ 4] .data PROGBITS 00fffc20 0005b8 000010 00 WA 0 0 4
[ 5] .bss NOBITS 00fffc30 0005c8 000004 00 WA 0 0 2
[ 6] .shstrtab STRTAB 00000000 0005c8 00003d 00 0 0 1
[ 7] .symtab SYMTAB 00000000 000770 0004c0 10 8 30 4
[ 8] .strtab STRTAB 00000000 000c30 0001f9 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x000094 0x00000000 0x00000000 0x00100 0x00100 RW 0x1
LOAD 0x000194 0x00000100 0x00000100 0x00421 0x00421 R E 0x1
LOAD 0x0005b8 0x00fffc20 0x00000521 0x00010 0x00014 RW 0x1
Section to Segment mapping:
Segment Sections...
00 .vectors
01 .text .rodata
02 .data .bss
There is no dynamic segment in this file.
There are no relocations in this file.
There are no unwind sections in this file.
Symbol table '.symtab' contains 76 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
2: 00000100 0 SECTION LOCAL DEFAULT 2
3: 000004b0 0 SECTION LOCAL DEFAULT 3
4: 00fffc20 0 SECTION LOCAL DEFAULT 4
5: 00fffc30 0 SECTION LOCAL DEFAULT 5
6: 00000000 0 FILE LOCAL DEFAULT ABS vector.c
7: 0000010a 0 NOTYPE LOCAL DEFAULT 2 .L11
8: 00000000 0 FILE LOCAL DEFAULT ABS main.c
9: 00fffc22 2 OBJECT LOCAL DEFAULT 4 _static_data
10: 0000010c 148 NOTYPE LOCAL DEFAULT 2 _printval
11: 00fffc30 2 OBJECT LOCAL DEFAULT 5 _static_bss
12: 00000220 0 NOTYPE LOCAL DEFAULT 2 .L4
13: 00000000 0 FILE LOCAL DEFAULT ABS lib.c
14: 00000236 0 NOTYPE LOCAL DEFAULT 2 .L7
15: 0000022a 0 NOTYPE LOCAL DEFAULT 2 .L8
16: 00000258 0 NOTYPE LOCAL DEFAULT 2 .L15
17: 0000024a 0 NOTYPE LOCAL DEFAULT 2 .L16
18: 00000292 0 NOTYPE LOCAL DEFAULT 2 .L26
19: 0000028a 0 NOTYPE LOCAL DEFAULT 2 .L21
20: 00000284 0 NOTYPE LOCAL DEFAULT 2 .L22
21: 00000294 0 NOTYPE LOCAL DEFAULT 2 .L17
22: 00000270 0 NOTYPE LOCAL DEFAULT 2 .L27
23: 000002b0 0 NOTYPE LOCAL DEFAULT 2 .L34
24: 000002a6 0 NOTYPE LOCAL DEFAULT 2 .L35
25: 000002ce 0 NOTYPE LOCAL DEFAULT 2 .L38
26: 000002c0 0 NOTYPE LOCAL DEFAULT 2 .L37
27: 000002e4 0 NOTYPE LOCAL DEFAULT 2 .L53
28: 00000300 0 NOTYPE LOCAL DEFAULT 2 .L50
29: 000002fa 0 NOTYPE LOCAL DEFAULT 2 .L45
30: 000002f4 0 NOTYPE LOCAL DEFAULT 2 .L46
31: 00000302 0 NOTYPE LOCAL DEFAULT 2 .L41
32: 000002dc 0 NOTYPE LOCAL DEFAULT 2 .L55
33: 00000332 0 NOTYPE LOCAL DEFAULT 2 .L57
34: 0000032c 0 NOTYPE LOCAL DEFAULT 2 .L60
35: 00000326 0 NOTYPE LOCAL DEFAULT 2 .L61
36: 00000340 0 NOTYPE LOCAL DEFAULT 2 .L56
37: 0000033a 0 NOTYPE LOCAL DEFAULT 2 .L59
38: 0000033e 0 NOTYPE LOCAL DEFAULT 2 .L58
39: 00000318 0 NOTYPE LOCAL DEFAULT 2 .L63
40: 00000364 0 NOTYPE LOCAL DEFAULT 2 .L65
41: 00000390 0 NOTYPE LOCAL DEFAULT 2 .L71
42: 00000384 0 NOTYPE LOCAL DEFAULT 2 .L72
43: 000003be 0 NOTYPE LOCAL DEFAULT 2 .L86
44: 000003e2 0 NOTYPE LOCAL DEFAULT 2 .L75
45: 00000000 0 FILE LOCAL DEFAULT ABS serial.c
46: 00fffc24 12 OBJECT LOCAL DEFAULT 4 _regs
47: 00000482 0 NOTYPE LOCAL DEFAULT 2 .L4
48: 000002d6 50 NOTYPE GLOBAL DEFAULT 2 _strcmp
49: 00fffc32 2 OBJECT GLOBAL DEFAULT 5 _global_bss
50: 00000000 256 OBJECT GLOBAL DEFAULT 1 _vectors
51: 0000034a 46 NOTYPE GLOBAL DEFAULT 2 _putc
52: 000004b0 0 NOTYPE GLOBAL DEFAULT 2 _etext
53: 00000378 36 NOTYPE GLOBAL DEFAULT 2 _puts
54: 00fffc30 0 NOTYPE GLOBAL DEFAULT 5 _bss_start
55: 00000100 0 NOTYPE GLOBAL DEFAULT 2 _text_start
56: 00000436 36 NOTYPE GLOBAL DEFAULT 2 _serial_is_send_enable
57: 00fffc34 0 NOTYPE GLOBAL DEFAULT 5 _ebss
58: 0000023c 40 NOTYPE GLOBAL DEFAULT 2 _memcpy
59: 00000222 26 NOTYPE GLOBAL DEFAULT 2 _memset
60: 00000100 0 NOTYPE GLOBAL DEFAULT 2 _start
61: 00fffc20 0 NOTYPE GLOBAL DEFAULT 4 _data_start
62: 00000400 54 NOTYPE GLOBAL DEFAULT 2 _serial_init
63: 0000039c 100 NOTYPE GLOBAL DEFAULT 2 _putxval
64: 000002b8 30 NOTYPE GLOBAL DEFAULT 2 _strcpy
65: 00fffc20 2 OBJECT GLOBAL DEFAULT 4 _global_data
66: 00000264 58 NOTYPE GLOBAL DEFAULT 2 _memcmp
67: 00000308 66 NOTYPE GLOBAL DEFAULT 2 _strncmp
68: 000004b0 0 NOTYPE GLOBAL DEFAULT 3 _rodata_start
69: 00fffc30 0 NOTYPE GLOBAL DEFAULT 4 _edata
70: 00fffc34 0 NOTYPE GLOBAL DEFAULT ABS _end
71: 0000029e 26 NOTYPE GLOBAL DEFAULT 2 _strlen
72: 0000045a 86 NOTYPE GLOBAL DEFAULT 2 _serial_send_byte
73: 00ffff00 0 NOTYPE GLOBAL DEFAULT 5 _stack
74: 000001a0 130 NOTYPE GLOBAL DEFAULT 2 _main
75: 00000521 0 NOTYPE GLOBAL DEFAULT 3 _erodata
No version information found in this file.
■メモリの領域とセクション
ここで注目すべきところは以下の内容である。
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .vectors PROGBITS 00000000 000094 000100 00 WA 0 0 4
[ 2] .text PROGBITS 00000100 000194 0003b0 00 AX 0 0 2
[ 3] .rodata PROGBITS 000004b0 000544 000071 01 AMS 0 0 1
[ 4] .data PROGBITS 00fffc20 0005b8 000010 00 WA 0 0 4
[ 5] .bss NOBITS 00fffc30 0005c8 000004 00 WA 0 0 2
[ 6] .shstrtab STRTAB 00000000 0005c8 00003d 00 0 0 1
[ 7] .symtab SYMTAB 00000000 000770 0004c0 10 8 30 4
[ 8] .strtab STRTAB 00000000 000c30 0001f9 00 0 0 1
ELF形式は、ファイルの内部をセクションという単位で区切って管理している。
実はこのセクションがメモリの領域と対応しているということだ。
対応を以下に示ておきます。
[ 2] .text…テキスト領域に置かれるべき情報
[ 4] .data…データ領域に置かれるべき情報
[ 5] .bss…BSS領域に置かれるべき情報
一方、[ 1]/[ 3]/[ 6]/[ 7]/[ 8]の他の領域はどこに置かれるべきなのか?
もしかしてメモリの領域というのは3領域だけでなく他にもあるのかなどいろいろ浮かんできますが、とりあえずわからないので置いておきます。
ちなみにどんなセクションがあるか把握して意味まで確認できそうなありがたいサイト発見、ここ
■関数や変数の配置
readelfは他にもいろいろと情報が出しているようですので、もう少し覗いてみることにします。
こんどはここに注目。
Symbol table '.symtab' contains 76 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
2: 00000100 0 SECTION LOCAL DEFAULT 2
3: 000004b0 0 SECTION LOCAL DEFAULT 3
4: 00fffc20 0 SECTION LOCAL DEFAULT 4
5: 00fffc30 0 SECTION LOCAL DEFAULT 5
6: 00000000 0 FILE LOCAL DEFAULT ABS vector.c
7: 0000010a 0 NOTYPE LOCAL DEFAULT 2 .L11
8: 00000000 0 FILE LOCAL DEFAULT ABS main.c
9: 00fffc22 2 OBJECT LOCAL DEFAULT 4 _static_data
10: 0000010c 148 NOTYPE LOCAL DEFAULT 2 _printval
11: 00fffc30 2 OBJECT LOCAL DEFAULT 5 _static_bss
12: 00000220 0 NOTYPE LOCAL DEFAULT 2 .L4
13: 00000000 0 FILE LOCAL DEFAULT ABS lib.c
14: 00000236 0 NOTYPE LOCAL DEFAULT 2 .L7
15: 0000022a 0 NOTYPE LOCAL DEFAULT 2 .L8
16: 00000258 0 NOTYPE LOCAL DEFAULT 2 .L15
……………………………………………………
……………………………………………………
……………………………………………………
42: 00000384 0 NOTYPE LOCAL DEFAULT 2 .L72
43: 000003be 0 NOTYPE LOCAL DEFAULT 2 .L86
44: 000003e2 0 NOTYPE LOCAL DEFAULT 2 .L75
45: 00000000 0 FILE LOCAL DEFAULT ABS serial.c
46: 00fffc24 12 OBJECT LOCAL DEFAULT 4 _regs
47: 00000482 0 NOTYPE LOCAL DEFAULT 2 .L4
48: 000002d6 50 NOTYPE GLOBAL DEFAULT 2 _strcmp
49: 00fffc32 2 OBJECT GLOBAL DEFAULT 5 _global_bss
50: 00000000 256 OBJECT GLOBAL DEFAULT 1 _vectors
51: 0000034a 46 NOTYPE GLOBAL DEFAULT 2 _putc
52: 000004b0 0 NOTYPE GLOBAL DEFAULT 2 _etext
53: 00000378 36 NOTYPE GLOBAL DEFAULT 2 _puts
54: 00fffc30 0 NOTYPE GLOBAL DEFAULT 5 _bss_start
55: 00000100 0 NOTYPE GLOBAL DEFAULT 2 _text_start
56: 00000436 36 NOTYPE GLOBAL DEFAULT 2 _serial_is_send_enable
57: 00fffc34 0 NOTYPE GLOBAL DEFAULT 5 _ebss
58: 0000023c 40 NOTYPE GLOBAL DEFAULT 2 _memcpy
59: 00000222 26 NOTYPE GLOBAL DEFAULT 2 _memset
60: 00000100 0 NOTYPE GLOBAL DEFAULT 2 _start
61: 00fffc20 0 NOTYPE GLOBAL DEFAULT 4 _data_start
62: 00000400 54 NOTYPE GLOBAL DEFAULT 2 _serial_init
63: 0000039c 100 NOTYPE GLOBAL DEFAULT 2 _putxval
64: 000002b8 30 NOTYPE GLOBAL DEFAULT 2 _strcpy
65: 00fffc20 2 OBJECT GLOBAL DEFAULT 4 _global_data
66: 00000264 58 NOTYPE GLOBAL DEFAULT 2 _memcmp
67: 00000308 66 NOTYPE GLOBAL DEFAULT 2 _strncmp
68: 000004b0 0 NOTYPE GLOBAL DEFAULT 3 _rodata_start
69: 00fffc30 0 NOTYPE GLOBAL DEFAULT 4 _edata
70: 00fffc34 0 NOTYPE GLOBAL DEFAULT ABS _end
71: 0000029e 26 NOTYPE GLOBAL DEFAULT 2 _strlen
72: 0000045a 86 NOTYPE GLOBAL DEFAULT 2 _serial_send_byte
73: 00ffff00 0 NOTYPE GLOBAL DEFAULT 5 _stack
74: 000001a0 130 NOTYPE GLOBAL DEFAULT 2 _main
75: 00000521 0 NOTYPE GLOBAL DEFAULT 3 _erodata
ここでは、関数や変数の配置情報が含まれているようです。
変数ではここの行に注目。
46: 00fffc24 12 OBJECT LOCAL DEFAULT 4 _regs
serial.c中に書かれたregs配列変数は0x00fffc24に割り当てられることを意味しているようです。
そこは、.dataセクションに属しているのでデータ領域、ということはRAMに置かれるようです。
関数ではここの行に注目。
74: 000001a0 130 NOTYPE GLOBAL DEFAULT 2 _main
main.c中に書かれたmainは0x000001a0に割り当てられ、.textセクションに属しているのでテキスト領域、ということはROMに置かれるようです。
※ちなみに、regsの前に_がついていますが、次の説明が書いていありました。
リンカは関数名や変数名をシンボルとして扱います。関数や変数の実体はメモリ上にあるので、シンボルはそれらが存在している特定のアドレスに、便宜上名前をつけたものだそうです。
複数のファイルに定義した関数や変数はここで一括して管理されアドレスが振られています。
リンクを実施してELF形式の実行形式ファイルを作成するなかで複数のソースファイルに記述された内容が集められアドレスの配置まで決まっているようです。