-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.odin
148 lines (102 loc) · 2.51 KB
/
main.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package binary
import "core:fmt"
import "core:strings"
import "core:os"
import bfd "./odin-bfd"
dis_mode :: enum {
LINEAR_DIS,
RECURSIVE_DIS,
SINGLE_FUNC_DIS
}
print_usage :: proc() {
fmt.println(os.args[0], "<bin> <mode>")
fmt.println("mode: [-l|-r|-f <name>]")
fmt.println("-l: linear disassembler")
fmt.println("-r: recursive disassembler")
fmt.println("-f: disassemble single function")
}
main :: proc() {
if len(os.args) != 3 && len(os.args) != 4 {
print_usage()
return
}
mode : dis_mode
if os.args[2] == "-l" {
mode = .LINEAR_DIS
}
else if os.args[2] == "-r" {
mode = .RECURSIVE_DIS
}
else if os.args[2] == "-f" {
mode = .SINGLE_FUNC_DIS
}
else {
print_usage()
return
}
// Load binary, then do disassembly
bin : Binary
ok : bool
b := open_bfd(os.args[1])
if (b == nil) {
return
}
bin.filename = strings.clone_from_cstring(b.filename)
fmt.println("Filename = ", bin.filename)
bin.entry = bfd.bfd_get_start_address(b)
fmt.println("Start = ", bin.entry)
bin.type_str = strings.clone_from_cstring(b.xvec.name)
fmt.println("Type = ", bin.type_str)
ok = set_flavour(&bin, b)
if !ok {
return
}
fmt.println("Flavour = ", bin.type)
bfd_info := bfd.bfd_get_arch_info(b)
bin.arch_str = strings.clone_from_cstring(bfd_info.printable_name)
fmt.println(bin.arch_str)
ok = set_arch(&bin, bfd_info)
if !ok {
return
}
fmt.println("Arch = ", bin.arch, bin.bits)
load_symbols_bfd(b, &bin)
load_dynsym_bfd(b, &bin)
ret := load_sections_bfd(b, &bin)
if ret < 0 {
fmt.println("Failed to load sections")
return
}
fmt.println("Binary loaded")
n_secs := len(bin.sections)
code : string
i : int
for i = 0; i < n_secs; i+=1 {
if bin.sections[i].type == section_type.SEC_TYPE_CODE {
code = "CODE"
}
else {
code = "DATA"
}
fmt.printf("0x%016x %d %s %s\n", bin.sections[i].vma, bin.sections[i].size, bin.sections[i].name, code)
}
n_syms := len(bin.symbols)
for i = 0; i < n_syms; i+=1 {
if bin.symbols[i].type == .SYM_TYPE_FUNC {
code = "FUNC"
}
else {
code = ""
}
fmt.printf("%s 0x%016x %s\n", bin.symbols[i].name, bin.symbols[i].addr, code)
}
// Loaded binary, now do disassembly
switch mode {
case .LINEAR_DIS:
disasm_bin(&bin)
case .RECURSIVE_DIS:
recursive_disasm_bin(&bin)
case .SINGLE_FUNC_DIS:
disasm_func(&bin, os.args[3])
}
}