-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (142 loc) · 3.91 KB
/
main.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
package main
import (
"os"
"github.com/sam-zheng/gobjdump/elf"
"github.com/spf13/cobra"
)
func main() {
cmd := &cobra.Command{
Use: "gobjdump <command> <file>",
Short: "ELF dumper for binaries built with Go",
Long: `gobjdump prints information specific to Go in an ELF executable built with Go. e.g.
* functions and files where they are defined
* pcsp/pcln of functions
* safe points of functions
* local/argument pointer map of functions`,
}
var function string
functionRequried := func(c *cobra.Command) {
c.Flags().StringVarP(&function, "function", "f", "", "function (required)")
c.MarkFlagRequired("function")
}
requireFile := func(cmd *cobra.Command, args []string) error {
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
return err
}
if _, err := os.Stat(args[0]); err != nil {
return err
}
return nil
}
doElfFile := func(f string, fn func(*elf.ELF_Info)) {
ef := elf.Open(f)
defer ef.Close()
fn(ef)
}
cmdPrintModule := &cobra.Command{
Use: "mod <file>",
Short: "print the module data layout",
Args: requireFile,
Run: func(cmd *cobra.Command, args []string) {
doElfFile(args[0], func(f *elf.ELF_Info) {
f.PrintModule(os.Stdout)
})
},
}
cmdPrintFuncs := &cobra.Command{
Use: "func <file>",
Short: "print functions grouped by files where they are defined",
Args: requireFile,
Run: func(cmd *cobra.Command, args []string) {
doElfFile(args[0], func(f *elf.ELF_Info) {
f.PrintFuncs(os.Stdout)
})
},
}
cmdPrintTypes := &cobra.Command{
Use: "type <file>",
Short: "print types",
Args: requireFile,
Run: func(cmd *cobra.Command, args []string) {
doElfFile(args[0], func(f *elf.ELF_Info) {
f.PrintTypes(os.Stdout)
})
},
}
cmdPrintPCSP := &cobra.Command{
Use: "pcsp <file>",
Short: "print pc->sp of a function",
Args: requireFile,
Run: func(cmd *cobra.Command, args []string) {
doElfFile(args[0], func(f *elf.ELF_Info) {
f.PrintPCSP(os.Stdout, function)
})
},
}
functionRequried(cmdPrintPCSP)
cmdPrintPCLN := &cobra.Command{
Use: "pcln <file>",
Short: "print pc->line No. of a function",
Args: requireFile,
Run: func(cmd *cobra.Command, args []string) {
doElfFile(args[0], func(f *elf.ELF_Info) {
f.PrintPCLN(os.Stdout, function)
})
},
}
functionRequried(cmdPrintPCLN)
cmdPrintSafePoints := &cobra.Command{
Use: "safe <file>",
Short: "print safe points of a function",
Args: requireFile,
Run: func(cmd *cobra.Command, args []string) {
doElfFile(args[0], func(f *elf.ELF_Info) {
f.PrintSafePoints(os.Stdout, function)
})
},
}
functionRequried(cmdPrintSafePoints)
cmdPrintArgPointerMap := &cobra.Command{
Use: "ap <file>",
Short: "print argument pointer map of a function",
Args: requireFile,
Run: func(cmd *cobra.Command, args []string) {
doElfFile(args[0], func(f *elf.ELF_Info) {
f.PrintArgPointerMap(os.Stdout, function)
})
},
}
functionRequried(cmdPrintArgPointerMap)
cmdPrintLocalPointerMap := &cobra.Command{
Use: "lp <file>",
Short: "print local pointer map of a function",
Args: requireFile,
Run: func(cmd *cobra.Command, args []string) {
doElfFile(args[0], func(f *elf.ELF_Info) {
f.PrintLocalPointerMap(os.Stdout, function)
})
},
}
functionRequried(cmdPrintLocalPointerMap)
cmdPrintStackObjs := &cobra.Command{
Use: "so <file>",
Short: "print stack objects of a function",
Args: requireFile,
Run: func(cmd *cobra.Command, args []string) {
doElfFile(args[0], func(f *elf.ELF_Info) {
f.PrintStackObjs(os.Stdout, function)
})
},
}
functionRequried(cmdPrintStackObjs)
cmd.AddCommand(cmdPrintModule)
cmd.AddCommand(cmdPrintFuncs)
cmd.AddCommand(cmdPrintTypes)
cmd.AddCommand(cmdPrintPCSP)
cmd.AddCommand(cmdPrintPCLN)
cmd.AddCommand(cmdPrintSafePoints)
cmd.AddCommand(cmdPrintArgPointerMap)
cmd.AddCommand(cmdPrintLocalPointerMap)
cmd.AddCommand(cmdPrintStackObjs)
cmd.Execute()
}