-
Notifications
You must be signed in to change notification settings - Fork 27
/
objdump.go
87 lines (80 loc) · 1.87 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
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"
)
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
}
indent = 0 // TODO(5nord): clean up global variables (sorry).
offset := 0
for offset < len(b) {
n, op, arg := t3xf.Decode(b[offset:])
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()
)
func printInstruction(offset int, b []byte, op opcode.Opcode, arg interface{}) {
switch op {
case opcode.SCAN:
printOffset(offset)
line.Fprintf(w, "scan")
indent += 4
case opcode.BLOCK:
indent -= 4
printOffset(offset)
line.Fprintf(w, "block")
case opcode.LINE:
line.Fprintf(w, " %*s=%d", indent, "", arg)
case opcode.REF, opcode.IEEE754DP, opcode.NATLONG, opcode.ISTR, opcode.FSTR:
printOffset(offset)
printArgument(arg)
case opcode.UTF8:
printOffset(offset)
literal.Fprintf(w, "%q", arg.(string))
default:
printOffset(offset)
fmt.Fprintf(w, "%s", op.String())
if arg != nil {
fmt.Fprintf(w, " ")
printArgument(arg)
}
}
}
func printOffset(offset int) {
line.Fprintf(w, "%08x: %*s", offset, indent, "")
}
func 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)
}
}