-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathobjdump.go
190 lines (173 loc) · 4.65 KB
/
objdump.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"os"
"github.com/fatih/color"
"github.com/nokia/ntt/k3/t3xf"
"github.com/nokia/ntt/k3/t3xf/opcode"
"github.com/spf13/cobra"
)
type t3xfDumper interface {
printInstruction(int, []byte, opcode.Opcode, interface{})
acquireRefs(int, int)
}
type t3xfAsmDump struct {
indent int
lineNo int
offsetToLineMap map[int]int
}
type t3xfObjDump struct {
indent int
}
func NewT3xfObjDump() *t3xfObjDump {
return &t3xfObjDump{indent: 0}
}
func NewT3xfAsmDump() *t3xfAsmDump {
return &t3xfAsmDump{indent: 0, lineNo: 1, offsetToLineMap: make(map[int]int)}
}
var (
ObjdumpCommand = &cobra.Command{
Use: "objdump",
Short: "Display information from T3XF object",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
b, err := os.ReadFile(args[0])
if err != nil {
return err
}
offset := 0
var dumper t3xfDumper
if !asT3xfAsm {
dumper = NewT3xfObjDump()
} else {
dumper = NewT3xfAsmDump()
lineNo := 1
for offset < len(b) {
n, _, _ := t3xf.Decode(b[offset:])
dumper.acquireRefs(offset, lineNo)
lineNo++
offset += n
}
offset = 0
}
for offset < len(b) {
n, op, arg := t3xf.Decode(b[offset:])
dumper.printInstruction(offset, b, op, arg)
fmt.Fprintf(w, "\n")
offset += n
}
w.Flush()
return nil
},
}
line = color.New(color.FgWhite, color.Faint)
literal = color.New()
asT3xfAsm bool
)
const INDENT_OFFSET = 4
func init() {
ObjdumpCommand.Flags().BoolVarP(&asT3xfAsm, "t3xfasm", "a", false, "output as t3xf assembler (suitable as input for 'ntt t3xfasm')")
}
func (asmDump *t3xfAsmDump) acquireRefs(offset int, asmLineNo int) {
asmDump.offsetToLineMap[offset] = asmLineNo
}
func (asmDump *t3xfAsmDump) printInstruction(offset int, b []byte, op opcode.Opcode, arg interface{}) {
asmDump.lineNo++
switch op {
case opcode.SCAN, opcode.ISCAN:
line.Fprintf(w, "%*sscan", asmDump.indent, "")
asmDump.indent += INDENT_OFFSET
case opcode.BLOCK:
asmDump.indent -= INDENT_OFFSET
line.Fprintf(w, "%*sblock", asmDump.indent, "")
case opcode.LINE:
line.Fprintf(w, "%*s=%d", asmDump.indent, "", arg)
case opcode.NATLONG:
line.Fprintf(w, "%*snatlong ", asmDump.indent, "")
asmDump.printArgument(arg)
case opcode.ISTR:
line.Fprintf(w, "%*sistr ", asmDump.indent, "")
asmDump.printArgument(arg)
case opcode.FSTR:
line.Fprintf(w, "%*sfstr ", asmDump.indent, "")
asmDump.printArgument(arg)
case opcode.IEEE754DP:
line.Fprintf(w, "%*sieee754dp ", asmDump.indent, "")
asmDump.printArgument(arg)
case opcode.REF:
line.Fprintf(w, "%*s", asmDump.indent, "")
asmDump.printArgument(arg)
case opcode.UTF8:
literal.Fprintf(w, "%*sutf8 %q", asmDump.indent, "", arg.(string))
default:
fmt.Fprintf(w, "%*s%s", asmDump.indent, "", op.String())
if arg != nil {
fmt.Fprintf(w, " ")
asmDump.printArgument(arg)
}
}
}
func (*t3xfObjDump) acquireRefs(int, int) {
}
func (objDump *t3xfObjDump) printInstruction(offset int, b []byte, op opcode.Opcode, arg interface{}) {
switch op {
case opcode.SCAN, opcode.ISCAN:
objDump.printOffset(offset)
line.Fprintf(w, "scan")
objDump.indent += INDENT_OFFSET
case opcode.BLOCK:
objDump.indent -= INDENT_OFFSET
objDump.printOffset(offset)
line.Fprintf(w, "block")
case opcode.LINE:
line.Fprintf(w, " %*s=%d", objDump.indent, "", arg)
case opcode.REF, opcode.IEEE754DP, opcode.NATLONG, opcode.ISTR, opcode.FSTR:
objDump.printOffset(offset)
objDump.printArgument(arg)
case opcode.UTF8:
objDump.printOffset(offset)
literal.Fprintf(w, "%q", arg.(string))
default:
objDump.printOffset(offset)
fmt.Fprintf(w, "%s", op.String())
if arg != nil {
fmt.Fprintf(w, " ")
objDump.printArgument(arg)
}
}
}
func (objDump *t3xfObjDump) printOffset(offset int) {
line.Fprintf(w, "%08x: %*s", offset, objDump.indent, "")
}
func (objDump *t3xfObjDump) printArgument(arg interface{}) {
switch arg := arg.(type) {
case int:
literal.Fprintf(w, "%d", arg)
case float64:
literal.Fprintf(w, "%f", arg)
case string:
literal.Fprintf(w, "%s", arg)
case t3xf.Reference:
bold.Fprintf(w, "@%08x", arg)
case *t3xf.String:
literal.Fprintf(w, "0x%02x (len=%d)", arg.Bytes(), arg.Len())
default:
fmt.Fprintf(w, "%+v", arg)
}
}
func (asmDump *t3xfAsmDump) printArgument(arg interface{}) {
switch arg := arg.(type) {
case int:
literal.Fprintf(w, "%d", arg)
case float64:
literal.Fprintf(w, "%g", arg)
case string:
literal.Fprintf(w, "\"%s\"", arg)
case t3xf.Reference:
bold.Fprintf(w, "@%d", asmDump.offsetToLineMap[int(arg)])
case *t3xf.String:
literal.Fprintf(w, "0x%02x (len=%d)", arg.Bytes(), arg.Len())
default:
fmt.Fprintf(w, "%+v", arg)
}
}