Cross Compiler主要在資源較豐富的電腦上執行,而編譯出能在別的電腦上執行的目的碼(Object Code)。
object code(目的碼):
指電腦科學中編譯器或組譯器處理原始碼後所生成的代碼,它一般由機械碼或接近於機器語言的代碼組成。
object file(目的檔)
即存放目的碼的電腦檔案,它常被稱作二進制檔案(binaries)。
Executable Format 主要有:
現在Linux和Windows上的可執行檔,基本上是基於COFF格式演變而來。
ELF 標準中將使用 ELF 格式的檔案分成:
ELF file type | Example |
---|---|
Relocatable File | Linux 的 .o、Windows 的 .obj |
Executable File | /bin/bash、Windows 的 .exe |
Shared Object File | Linux 的 .so、Windows 的 .dll |
Core Dump File | Linux 的 core dump |
ELF 檔是由 header、一堆 section 及一堆 table 組成的,各 table 也是 section。
透過readelf / objdump 工具
可用 readelf -h XXX.elf ,觀察描述整個 ELF 檔的屬性。
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: EXEC (Executable file)
Machine: Moxie
Version: 0x1
Entry point address: 0x1000
Start of program headers: 52 (bytes into file)
Start of section headers: 38760 (bytes into file)
Flags: 0x0
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: 22
Section header string table index: 21
typedef struct
{
unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
Elf64_Half e_type; /* Object file type */
Elf64_Half e_machine; /* Architecture */
Elf64_Word e_version; /* Object file version */
Elf64_Addr e_entry; /* Entry point virtual address */
Elf64_Off e_phoff; /* Program header table file offset */
Elf64_Off e_shoff; /* Section header table file offset */
Elf64_Word e_flags; /* Processor-specific flags */
Elf64_Half e_ehsize; /* ELF header size in bytes */
Elf64_Half e_phentsize; /* Program header table entry size */
Elf64_Half e_phnum; /* Program header table entry count */
Elf64_Half e_shentsize; /* Section header table entry size */
Elf64_Half e_shnum; /* Section header table entry count */
Elf64_Half e_shstrndx; /* Section header string table index */
} Elf64_Ehdr;
可用 readelf -S XXX.elf ,觀察各section的屬性
There are 22 section headers, starting at offset 0x9768:
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 00001000 000094 000a2c 00 AX 0 0 2
[ 2] .init PROGBITS 00001a2c 000ac0 00000e 00 AX 0 0 2
[ 3] .fini PROGBITS 00001a3a 000ace 000008 00 AX 0 0 2
[ 4] .rodata PROGBITS 00001a44 000ad8 00010c 00 A 0 0 4
[ 5] .data PROGBITS 00001c50 000be4 000428 00 WA 0 0 4
[ 6] .eh_frame PROGBITS 00002078 00100c 000004 00 WA 0 0 4
[ 7] .ctors PROGBITS 0000207c 001010 000008 00 WA 0 0 4
[ 8] .dtors PROGBITS 00002084 001018 000008 00 WA 0 0 4
[ 9] .bss NOBITS 0000208c 001020 000094 00 WA 0 0 4
[10] .comment PROGBITS 00000000 001020 00003b 01 MS 0 0 1
[11] .debug_aranges PROGBITS 00000000 00105b 000150 00 0 0 1
[12] .debug_info PROGBITS 00000000 0011ab 003d87 00 0 0 1
[13] .debug_abbrev PROGBITS 00000000 004f32 000f8a 00 0 0 1
[14] .debug_line PROGBITS 00000000 005ebc 0014ef 00 0 0 1
[15] .debug_frame PROGBITS 00000000 0073ac 0002fc 00 0 0 4
[16] .debug_str PROGBITS 00000000 0076a8 000a92 01 MS 0 0 1
[17] .debug_loc PROGBITS 00000000 00813a 000d3d 00 0 0 1
[18] .debug_ranges PROGBITS 00000000 008e77 0000a8 00 0 0 1
[19] .symtab SYMTAB 00000000 008f20 000540 10 20 54 4
[20] .strtab STRTAB 00000000 009460 00023e 00 0 0 1
[21] .shstrtab STRTAB 00000000 00969e 0000c7 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
typedef struct
{
Elf64_Word st_name; /* Symbol name (string tbl index) */
unsigned char st_info; /* Symbol type and binding */
unsigned char st_other; /* Symbol visibility */
Elf64_Section st_shndx; /* Section index */
Elf64_Addr st_value; /* Symbol value */
Elf64_Xword st_size; /* Symbol size */
} Elf64_Sym;
依照 隔離執行環境的建構與應用 的步驟觀察 tests/sha256
$ src/sandbox -e tests/sha256 -g 9999
$ moxie-none-moxiebox-gdb -q tests/sha256
終端機 B 進入 remote GDB 後透過 compare-sections 來尋找對應的 sections 位置
(gdb) compare-sections .text
Section .text, range 0x1000 -- 0x1a2c: matched.
(gdb) compare-sections .data
Section .data, range 0x1c50 -- 0x2078: matched.
或者透過 info files 印出所有 sections 的位置
(gdb) info files
Symbols from "/home/uscca22/sysprog/homework_team/moxiebox/tests/sha256".
Remote serial target in gdb-specific protocol:
Debugging a target over a serial line.
While running this, GDB does not access memory from...
Local exec file:
`/home/uscca22/sysprog/homework_team/moxiebox/tests/sha256', file type elf32-littlemoxie.
Entry point: 0x1000
0x00001000 - 0x00001a2c is .text
0x00001a2c - 0x00001a3a is .init
0x00001a3a - 0x00001a42 is .fini
0x00001a44 - 0x00001b50 is .rodata
0x00001c50 - 0x00002078 is .data
0x00002078 - 0x0000207c is .eh_frame
0x0000207c - 0x00002084 is .ctors
0x00002084 - 0x0000208c is .dtors
0x0000208c - 0x00002120 is .bss
透過 Examining memory 指令搭配剛剛找到的位置來檢驗 .text 和 .data section 內容
(gdb)x/nfu addr
x : Examining memory
n : Repeat count
f : Display format
u : Unit size
.text section 存放指令,因此 display format 選擇 instruction format
(gdb) x/5i 0x00001000
0x1000 <__start>: xor $fp, $fp
0x1002 <__start+2>: gsr $sp, 0x7
0x1004 <__start+4>: ldi.l $r0, 0x208c
0x100a <__start+10>: xor $r1, $r1
0x100c <__start+12>: ldi.l $r2, 0x2120
(gdb)
0x1012 <__start+18>: sub $r2, $r0
0x1014 <__start+20>: jsra 0x14b0 <memset>
0x101a <__start+26>: gsr $r0, 0x6
0x101c <__start+28>: sta.l 0x211c, $r0
0x1022 <__start+34>: xor $r0, $r0
(gdb)
0x1024 <__start+36>: xor $r1, $r1
0x1026 <__start+38>: jsra 0x1a2c <_init>
0x102c <__start+44>: ldi.l $r0, 0x1a3a
0x1032 <__start+50>: jsra 0x116c <atexit>
0x1038 <__start+56>: jsra 0x13e0 <main>
...
...
...
.data section 存放不同大小的資料, display format 使用 instruction format 沒有太大的意義,因此選擇以十六進制顯示
(gdb) x/5xh 0x1c50
0x1c50 <_impure_ptr>: 0x1c54 0x0000 0x0000 0x0000 0x1f40
(gdb)
0x1c5a <impure_data+6>: 0x0000 0x1fa8 0x0000 0x2010 0x0000
(gdb)
0x1c64 <impure_data+16>: 0x0000 0x0000 0x0000 0x0000 0x0000
(gdb)
0x1c6e <impure_data+26>: 0x0000 0x0000 0x0000 0x0000 0x0000
(gdb)
0x1c78 <impure_data+36>: 0x0000 0x0000 0x0000 0x0000 0x0000
...
...
...
mmap
系統呼叫.text
和 .data
section 內容呢?step
是如何透過 GDB stub 傳遞到 moxiebox 裡頭呢?兩邊的通訊協定又為何?
src/sandbox.cc
檔案內容和 GDB Remote Serial Protocol