ELF文件是一种格式,我们使用gcc编译出来的.o文件,和链接后生成的.out文件一般都是ELF格式的文件。
Bin文件一般指的是连接器ld -b binary链接出来的或者用objcopy -b抽出来的文件。
ELF和Bin文件都是二进制文件(binary file)。
5.1 How It Works: An Outline of BFD
===================================
When an object file(编译器编译出来没有链接的文件叫做object file.) is opened, BFD subroutines automatically determine
the format of the input object file. They then build a descriptor in
memory with pointers to routines that will be used to access elements of
the object file's data structures.
As different information from the object files is required, BFD
reads from different sections of the file and processes them. For
example, a very common operation for the linker is processing symbol
tables. Each BFD back end provides a routine for converting between
the object file's representation of symbols and an internal canonical
format. When the linker asks for the symbol table of an object file, it
calls through a memory pointer to the routine from the relevant BFD
back end which reads and converts the table into a canonical form. The
linker then operates upon the canonical form. When the link is finished
and the linker writes the output file's symbol table, another BFD back
end routine is called to take the newly created symbol table and
convert it into the chosen output format.
ld manual中ld -b选项的解释为
`-b INPUT-FORMAT'
`--format=INPUT-FORMAT'
`ld' may be configured to support more than one kind of object
file. If your `ld' is configured this way, you can use the `-b'
option to specify the binary format for input object files that
follow this option on the command line. Even when `ld' is
configured to support alternative object formats, you don't
usually need to specify this, as `ld' should be configured to
expect as a default input format the most usual format on each
machine. INPUT-FORMAT is a text string, the name of a particular
format supported by the BFD libraries. (You can list the
available binary formats with `objdump -i'.) *Note BFD::.
使用objdump -i可以列出ld连接器支持的所有output格式:
BFD header file version version 2.19.51.0.14-34.fc12 20090722
elf32-i386
(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
elf64-x86-64
(header little endian, data little endian)
i386
elf64-little
(header little endian, data little endian)
i386
elf64-big
(header big endian, data big endian)
i386
elf32-little
(header little endian, data little endian)
i386
elf32-big
(header big endian, data big endian)
i386
srec
(header endianness unknown, data endianness unknown)
i386
symbolsrec
(header endianness unknown, data endianness unknown)
i386
verilog
(header endianness unknown, data endianness unknown)
i386
tekhex
(header endianness unknown, data endianness unknown)
i386
binary
(header endianness unknown, data endianness unknown)
i386
ihex
(header endianness unknown, data endianness unknown)
i386
trad-core
(header endianness unknown, data endianness unknown)
elf32-i386 a.out-i386-linux pei-i386 elf64-x86-64 elf64-little
i386 elf32-i386 a.out-i386-linux pei-i386 elf64-x86-64 elf64-little
elf64-big elf32-little elf32-big srec symbolsrec verilog tekhex
i386 elf64-big elf32-little elf32-big srec symbolsrec verilog tekhex
binary ihex trad-core
i386 binary ihex ---------
可见ld的output格式有的是ELF格式,有的不是ELF格式,例如常见的binary,即.bin文件。ELF格式的文件可以使用readelf和objdump来分析,binary不能使用这两个工具分析。
链接后的ELF文件比binary多很多的信息,所以方便用来调试。
我们使用readelf -a 列出boot.o的信息:
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: REL (Relocatable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 672 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 0 (bytes)
Number of program headers: 0
Size of section headers: 40 (bytes)
Number of section headers: 11
Section header string table index: 8
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 00000000 000034 00006a 00 AX 0 0 4
[ 2] .rel.text REL 00000000 0005cc 000028 08 9 1 4
[ 3] .data PROGBITS 00000000 0000a0 000000 00 WA 0 0 4
[ 4] .bss NOBITS 00000000 0000a0 000000 00 WA 0 0 4
[ 5] .stab PROGBITS 00000000 0000a0 00018c 0c 7 0 4
[ 6] .rel.stab REL 00000000 0005f4 000100 08 9 5 4
[ 7] .stabstr STRTAB 00000000 00022c 00002f 00 0 0 1
[ 8] .shstrtab STRTAB 00000000 00025b 000043 00 0 0 1
[ 9] .symtab SYMTAB 00000000 000458 000110 10 10 15 4
[10] .strtab STRTAB 00000000 000568 000064 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)
There are no section groups in this file.
There are no program headers in this file.
Relocation section '.rel.text' at offset 0x5cc contains 5 entries:
Offset Info Type Sym.Value Sym. Name
00000021 00000114 R_386_16 00000000 .text
0000002e 00000114 R_386_16 00000000 .text
00000041 00000f01 R_386_32 00000000 start
00000046 00001002 R_386_PC32 00000000 bootmain
00000066 00000101 R_386_32 00000000 .text
Relocation section '.rel.stab' at offset 0x5f4 contains 32 entries:
Offset Info Type Sym.Value Sym. Name
00000014 00000101 R_386_32 00000000 .text
00000020 00000101 R_386_32 00000000 .text
0000002c 00000101 R_386_32 00000000 .text
00000038 00000101 R_386_32 00000000 .text
00000044 00000101 R_386_32 00000000 .text
00000050 00000101 R_386_32 00000000 .text
0000005c 00000101 R_386_32 00000000 .text
00000068 00000101 R_386_32 00000000 .text
00000074 00000101 R_386_32 00000000 .text
00000080 00000101 R_386_32 00000000 .text
0000008c 00000101 R_386_32 00000000 .text
00000098 00000101 R_386_32 00000000 .text
000000a4 00000101 R_386_32 00000000 .text
000000b0 00000101 R_386_32 00000000 .text
000000bc 00000101 R_386_32 00000000 .text
000000c8 00000101 R_386_32 00000000 .text
000000d4 00000101 R_386_32 00000000 .text
000000e0 00000101 R_386_32 00000000 .text
000000ec 00000101 R_386_32 00000000 .text
000000f8 00000101 R_386_32 00000000 .text
00000104 00000101 R_386_32 00000000 .text
00000110 00000101 R_386_32 00000000 .text
0000011c 00000101 R_386_32 00000000 .text
00000128 00000101 R_386_32 00000000 .text
00000134 00000101 R_386_32 00000000 .text
00000140 00000101 R_386_32 00000000 .text
0000014c 00000101 R_386_32 00000000 .text
00000158 00000101 R_386_32 00000000 .text
00000164 00000101 R_386_32 00000000 .text
00000170 00000101 R_386_32 00000000 .text
0000017c 00000101 R_386_32 00000000 .text
00000188 00000101 R_386_32 00000000 .text
There are no unwind sections in this file.
Symbol table '.symtab' contains 17 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
2: 00000000 0 SECTION LOCAL DEFAULT 3
3: 00000000 0 SECTION LOCAL DEFAULT 4
4: 00000008 0 NOTYPE LOCAL DEFAULT ABS PROT_MODE_CSEG
5: 00000010 0 NOTYPE LOCAL DEFAULT ABS PROT_MODE_DSEG
6: 00000001 0 NOTYPE LOCAL DEFAULT ABS CR0_PE_ON
7: 0000000a 0 NOTYPE LOCAL DEFAULT 1 seta20.1
8: 00000014 0 NOTYPE LOCAL DEFAULT 1 seta20.2
9: 00000064 0 NOTYPE LOCAL DEFAULT 1 gdtdesc
10: 00000032 0 NOTYPE LOCAL DEFAULT 1 protcseg
11: 0000004a 0 NOTYPE LOCAL DEFAULT 1 spin
12: 0000004c 0 NOTYPE LOCAL DEFAULT 1 gdt
13: 00000000 0 SECTION LOCAL DEFAULT 5
14: 00000000 0 SECTION LOCAL DEFAULT 7
15: 00000000 0 NOTYPE GLOBAL DEFAULT 1 start
16: 00000000 0 NOTYPE GLOBAL DEFAULT UND bootmain
Bin文件一般指的是连接器ld -b binary链接出来的或者用objcopy -b抽出来的文件。
ELF和Bin文件都是二进制文件(binary file)。
X86对于这两种可执行文件都是可以直接运行的。
这里have some problem, the bin can't be read by the objdump, but in JOS, the boot.s and main.c can read the kernel bin according to the ELF format. why?
5.1 How It Works: An Outline of BFD
===================================
When an object file(编译器编译出来没有链接的文件叫做object file.) is opened, BFD subroutines automatically determine
the format of the input object file. They then build a descriptor in
memory with pointers to routines that will be used to access elements of
the object file's data structures.
As different information from the object files is required, BFD
reads from different sections of the file and processes them. For
example, a very common operation for the linker is processing symbol
tables. Each BFD back end provides a routine for converting between
the object file's representation of symbols and an internal canonical
format. When the linker asks for the symbol table of an object file, it
calls through a memory pointer to the routine from the relevant BFD
back end which reads and converts the table into a canonical form. The
linker then operates upon the canonical form. When the link is finished
and the linker writes the output file's symbol table, another BFD back
end routine is called to take the newly created symbol table and
convert it into the chosen output format.
ld manual中ld -b选项的解释为
`-b INPUT-FORMAT'
`--format=INPUT-FORMAT'
`ld' may be configured to support more than one kind of object
file. If your `ld' is configured this way, you can use the `-b'
option to specify the binary format for input object files that
follow this option on the command line. Even when `ld' is
configured to support alternative object formats, you don't
usually need to specify this, as `ld' should be configured to
expect as a default input format the most usual format on each
machine. INPUT-FORMAT is a text string, the name of a particular
format supported by the BFD libraries. (You can list the
available binary formats with `objdump -i'.) *Note BFD::.
使用objdump -i可以列出ld连接器支持的所有output格式:
BFD header file version version 2.19.51.0.14-34.fc12 20090722
elf32-i386
(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
elf64-x86-64
(header little endian, data little endian)
i386
elf64-little
(header little endian, data little endian)
i386
elf64-big
(header big endian, data big endian)
i386
elf32-little
(header little endian, data little endian)
i386
elf32-big
(header big endian, data big endian)
i386
srec
(header endianness unknown, data endianness unknown)
i386
symbolsrec
(header endianness unknown, data endianness unknown)
i386
verilog
(header endianness unknown, data endianness unknown)
i386
tekhex
(header endianness unknown, data endianness unknown)
i386
binary
(header endianness unknown, data endianness unknown)
i386
ihex
(header endianness unknown, data endianness unknown)
i386
trad-core
(header endianness unknown, data endianness unknown)
elf32-i386 a.out-i386-linux pei-i386 elf64-x86-64 elf64-little
i386 elf32-i386 a.out-i386-linux pei-i386 elf64-x86-64 elf64-little
elf64-big elf32-little elf32-big srec symbolsrec verilog tekhex
i386 elf64-big elf32-little elf32-big srec symbolsrec verilog tekhex
binary ihex trad-core
i386 binary ihex ---------
可见ld的output格式有的是ELF格式,有的不是ELF格式,例如常见的binary,即.bin文件。ELF格式的文件可以使用readelf和objdump来分析,binary不能使用这两个工具分析。
链接后的ELF文件比binary多很多的信息,所以方便用来调试。
我们使用readelf -a 列出boot.o的信息:
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: REL (Relocatable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 672 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 0 (bytes)
Number of program headers: 0
Size of section headers: 40 (bytes)
Number of section headers: 11
Section header string table index: 8
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 00000000 000034 00006a 00 AX 0 0 4
[ 2] .rel.text REL 00000000 0005cc 000028 08 9 1 4
[ 3] .data PROGBITS 00000000 0000a0 000000 00 WA 0 0 4
[ 4] .bss NOBITS 00000000 0000a0 000000 00 WA 0 0 4
[ 5] .stab PROGBITS 00000000 0000a0 00018c 0c 7 0 4
[ 6] .rel.stab REL 00000000 0005f4 000100 08 9 5 4
[ 7] .stabstr STRTAB 00000000 00022c 00002f 00 0 0 1
[ 8] .shstrtab STRTAB 00000000 00025b 000043 00 0 0 1
[ 9] .symtab SYMTAB 00000000 000458 000110 10 10 15 4
[10] .strtab STRTAB 00000000 000568 000064 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)
There are no section groups in this file.
There are no program headers in this file.
Relocation section '.rel.text' at offset 0x5cc contains 5 entries:
Offset Info Type Sym.Value Sym. Name
00000021 00000114 R_386_16 00000000 .text
0000002e 00000114 R_386_16 00000000 .text
00000041 00000f01 R_386_32 00000000 start
00000046 00001002 R_386_PC32 00000000 bootmain
00000066 00000101 R_386_32 00000000 .text
Relocation section '.rel.stab' at offset 0x5f4 contains 32 entries:
Offset Info Type Sym.Value Sym. Name
00000014 00000101 R_386_32 00000000 .text
00000020 00000101 R_386_32 00000000 .text
0000002c 00000101 R_386_32 00000000 .text
00000038 00000101 R_386_32 00000000 .text
00000044 00000101 R_386_32 00000000 .text
00000050 00000101 R_386_32 00000000 .text
0000005c 00000101 R_386_32 00000000 .text
00000068 00000101 R_386_32 00000000 .text
00000074 00000101 R_386_32 00000000 .text
00000080 00000101 R_386_32 00000000 .text
0000008c 00000101 R_386_32 00000000 .text
00000098 00000101 R_386_32 00000000 .text
000000a4 00000101 R_386_32 00000000 .text
000000b0 00000101 R_386_32 00000000 .text
000000bc 00000101 R_386_32 00000000 .text
000000c8 00000101 R_386_32 00000000 .text
000000d4 00000101 R_386_32 00000000 .text
000000e0 00000101 R_386_32 00000000 .text
000000ec 00000101 R_386_32 00000000 .text
000000f8 00000101 R_386_32 00000000 .text
00000104 00000101 R_386_32 00000000 .text
00000110 00000101 R_386_32 00000000 .text
0000011c 00000101 R_386_32 00000000 .text
00000128 00000101 R_386_32 00000000 .text
00000134 00000101 R_386_32 00000000 .text
00000140 00000101 R_386_32 00000000 .text
0000014c 00000101 R_386_32 00000000 .text
00000158 00000101 R_386_32 00000000 .text
00000164 00000101 R_386_32 00000000 .text
00000170 00000101 R_386_32 00000000 .text
0000017c 00000101 R_386_32 00000000 .text
00000188 00000101 R_386_32 00000000 .text
There are no unwind sections in this file.
Symbol table '.symtab' contains 17 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
2: 00000000 0 SECTION LOCAL DEFAULT 3
3: 00000000 0 SECTION LOCAL DEFAULT 4
4: 00000008 0 NOTYPE LOCAL DEFAULT ABS PROT_MODE_CSEG
5: 00000010 0 NOTYPE LOCAL DEFAULT ABS PROT_MODE_DSEG
6: 00000001 0 NOTYPE LOCAL DEFAULT ABS CR0_PE_ON
7: 0000000a 0 NOTYPE LOCAL DEFAULT 1 seta20.1
8: 00000014 0 NOTYPE LOCAL DEFAULT 1 seta20.2
9: 00000064 0 NOTYPE LOCAL DEFAULT 1 gdtdesc
10: 00000032 0 NOTYPE LOCAL DEFAULT 1 protcseg
11: 0000004a 0 NOTYPE LOCAL DEFAULT 1 spin
12: 0000004c 0 NOTYPE LOCAL DEFAULT 1 gdt
13: 00000000 0 SECTION LOCAL DEFAULT 5
14: 00000000 0 SECTION LOCAL DEFAULT 7
15: 00000000 0 NOTYPE GLOBAL DEFAULT 1 start
16: 00000000 0 NOTYPE GLOBAL DEFAULT UND bootmain