-
Notifications
You must be signed in to change notification settings - Fork 3
/
das.go
168 lines (141 loc) · 2.77 KB
/
das.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* DAS - DisASsembler (not for MB)
*
* Copyright (C) 2017-2022 Namhyung Kim <[email protected]>
*
* Released under MIT license.
*/
package main
import (
"debug/elf"
"flag"
"fmt"
"log"
"os"
str "strings"
)
const (
DasVersion string = "v0.3"
)
const (
OPTYPE_OTHER = iota
OPTYPE_BRANCH
OPTYPE_RETURN
OPTYPE_INFO
)
type DasLine struct {
rawline string
offset uint64
optype int
opcode string // []uint8
mnemonic string
args string
local bool // for OPTYPE_BRANCH
target uint64 // for OPTYPE_BRANCH
indent int // for OPTYPE_INFO
comment string
}
type DasFunc struct {
name string
sym elf.Symbol
start uint64
sect bool
fold bool // for section
insn []*DasLine
}
type DasParser struct {
name string
file *os.File
elf *elf.File
ops DasArchOps
rodata int
strs []byte
comment string
hasFns bool
engine interface{}
}
type DasArchOps interface {
parsePLT()
parseInsn(interface{}, *elf.Symbol) *DasLine
describe(insn *DasLine) string
}
var (
funcs []*DasFunc
csect *DasFunc // current section
strs map[uint64]string // string table
)
var (
// command line options
capstone bool
objdump string
version bool
useInline bool
)
func init() {
flag.BoolVar(&capstone, "c", false, "Use capstone disassembler")
flag.StringVar(&objdump, "d", "objdump", "Path to objdump tool")
flag.BoolVar(&useInline, "i", false, "Include inline info")
flag.BoolVar(&version, "v", false, "Show version number")
}
func initDasParser(target string) *DasParser {
f, err := os.Open(target)
if err != nil {
log.Fatal(err)
}
e, err := elf.NewFile(f)
if err != nil {
log.Fatal(err)
}
return &DasParser{name: target, file: f, elf: e, comment: "#"}
}
func finishDasParser(p *DasParser) {
p.elf.Close()
p.file.Close()
}
func describeInsn(p *DasParser, insn *DasLine) string {
if p.ops != nil {
return fmt.Sprintf("%s: %s (%s)", "instruction",
str.ToUpper(insn.mnemonic), p.ops.describe(insn))
}
return "instruction: " + str.ToUpper(insn.mnemonic)
}
func main() {
flag.Parse()
args := flag.Args()
if version {
fmt.Printf("das (TUI disassembler): %s\n", DasVersion)
return
}
if len(args) < 1 {
log.Fatal("Usage: das <binary>")
}
target := args[0]
p := initDasParser(target)
if capstone {
prepareCapstone(p)
if p.ops == nil {
log.Fatal("Architecture not supported (yet)!")
}
parseCapstone(p)
} else {
setupArchOps(p)
initStrings(p)
initFuncList(p)
if !p.hasFns {
args := []string{"-d", "-C"}
if useInline {
args = append(args, "-l", "--inlines")
}
args = append(args, target)
cmd, r, err := runCommand("objdump", args...)
if err == nil {
parseObjdump(p, nil, r)
} else {
log.Fatal(err)
}
cmd.Wait()
}
}
ShowTUI(p)
finishDasParser(p)
}