blog 文章

2017/06/01

產生一個 elf object file, 讓 gcc link 出 elf 執行檔案

op2.c
1 //#include <stdio.h>
2
3 int main()
4 {
5   printf("1+2 = %d\n", 1+2);
6 }

op2.S 由 simple_compier 產生。

op2.S
 1 .section .rodata
 2 LC1: 
 3     .string "1+2 = %d\n"
 4 .text
 5 .global main
 6 .type main, @function
 7 main:
 8 pushl %ebp
 9 movl %esp, %ebp
10 subl $0, %esp # reserve local variable
11 movl $1, %eax
12 ADD $2, %eax
13 pushl %eax
14 popl %eax
15 pushl %eax
16 pushl $LC1
17 call printf
18 addl $8, %esp
19 leave
20 ret
21 
22 xyz:

奮戰幾天後, op2.o.elf 由 simple_as 將 op2.S 轉成 op2.o

op2.o.elf
 1 ELF Header:
 2   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
 3   Class:                             ELF32
 4   Data:                              2's complement, little endian
 5   Version:                           1 (current)
 6   OS/ABI:                            UNIX - System V
 7   ABI Version:                       0
 8   Type:                              REL (Relocatable file)
 9   Machine:                           Intel 80386
10   Version:                           0x1
11   Entry point address:               0x0
12   Start of program headers:          0 (bytes into file)
13   Start of section headers:          337 (bytes into file)
14   Flags:                             0x0
15   Size of this header:               52 (bytes)
16   Size of program headers:           0 (bytes)
17   Number of program headers:         0
18   Size of section headers:           40 (bytes)
19   Number of section headers:         9
20   Section header string table index: 5
21 
22 Section Headers:
23   [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
24   [ 0]                   NULL            00000000 000000 000000 00      0   0  0
25   [ 1] .bss              NOBITS          00000000 000034 000000 00  WA  0   0  1
26   [ 2] .data             PROGBITS        00000000 000034 000000 00  WA  0   0  1
27   [ 3] .rel.text         REL             00000000 000034 000010 08   I  7   8  4
28   [ 4] .rodata           PROGBITS        00000000 000044 00000a 00      0   0  0
29   [ 5] .shstrtab         STRTAB          00000000 00004e 00003e 00      0   0  0
30   [ 6] .strtab           STRTAB          00000000 00008c 000015 00      0   0  0
31   [ 7] .symtab           SYMTAB          00000000 0000a1 000090 10      6   7  0
32   [ 8] .text             PROGBITS        00000000 000131 000020 00  AX  0   0  0
33 Key to Flags:
34   W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
35   L (link order), O (extra OS processing required), G (group), T (TLS),
36   C (compressed), x (unknown), o (OS specific), E (exclude),
37   p (processor specific)
38 
39 There are no section groups in this file.
40 
41 There are no program headers in this file.
42 
43 Relocation section '.rel.text' at offset 0x34 contains 2 entries:
44  Offset     Info    Type            Sym.Value  Sym. Name
45 00000012  00000301 R_386_32          00000000   .rodata
46 00000017  00000802 R_386_PC32        00000000   printf
47 
48 The decoding of unwind sections for machine type Intel 80386 is not currently supported.
49 
50 Symbol table '.symtab' contains 9 entries:
51    Num:    Value  Size Type    Bind   Vis      Ndx Name
52      0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
53      1: 00000000     0 SECTION LOCAL  DEFAULT    1 
54      2: 00000000     0 SECTION LOCAL  DEFAULT    2 
55      3: 00000000     0 SECTION LOCAL  DEFAULT    4 
56      4: 00000000     0 SECTION LOCAL  DEFAULT    8 
57      5: 00000000     0 NOTYPE  LOCAL  DEFAULT    4 LC1
58      6: 00000000     0 NOTYPE  LOCAL  DEFAULT    8 xyz
59      7: 00000000     0 FUNC    GLOBAL DEFAULT    8 main
60      8: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND printf
61 
62 No version information found in this file.

儘管有些部份是 hard code, 但已經可以讓 gcc link 成 elf 執行檔。 執行後正常輸出
descent@debian64:test_pattern$ ./op2 
1+2 = 3

單純印出 hello world 比較簡單, 所以用 1+2 來測試。真是太爽了。

沒有留言:

張貼留言