-
Notifications
You must be signed in to change notification settings - Fork 58
/
moduledata.go
134 lines (115 loc) · 2.74 KB
/
moduledata.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
// Copyright 2021 The GoRE Authors. All rights reserved.
// Use of this source code is governed by the license that
// can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io"
"os"
"github.com/cheynewallace/tabby"
"github.com/goretk/gore"
"github.com/spf13/cobra"
)
func init() {
modCMD := &cobra.Command{
Use: "moduledata path/to/file",
Args: cobra.ExactArgs(1),
Aliases: []string{"md"},
Short: "Display sections extracted from the moduledata structure.",
Run: func(cmd *cobra.Command, args []string) {
moduledata(args[0])
},
}
dumpCMD := &cobra.Command{
Use: "dump section path/to/file",
Args: cobra.ExactValidArgs(2),
Short: "Dump the contents of a section to standard out.",
Run: func(cmd *cobra.Command, args []string) {
dumpModuleSection(args[1], args[0])
},
}
modCMD.AddCommand(dumpCMD)
rootCmd.AddCommand(modCMD)
}
func dumpModuleSection(fp, section string) {
f, err := gore.Open(fp)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open the file: %s.\n", err)
return
}
defer f.Close()
md, err := f.Moduledata()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not retrieve the file's moduledata: %s.\n", err)
return
}
var sec gore.ModuleDataSection
switch section {
case "text":
sec = md.Text()
case "types":
sec = md.Types()
case "itablinks":
sec = md.ITabLinks()
case "pclntab":
sec = md.PCLNTab()
case "functab":
sec = md.FuncTab()
case "noptrdata":
sec = md.NoPtrData()
case "data":
sec = md.Data()
case "bss":
sec = md.Bss()
case "noptrbss":
sec = md.NoPtrBss()
default:
fmt.Fprintf(os.Stderr, "No known section with the name %s.\n", section)
return
}
data, err := sec.Data()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when getting the secton data: %s.\n", err)
return
}
if len(data) == 0 {
fmt.Fprintf(os.Stderr, "Section %s is empty.\n", section)
return
}
r := bytes.NewReader(data)
io.Copy(os.Stdout, r)
}
func moduledata(fp string) {
f, err := gore.Open(fp)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open the file: %s.\n", err)
return
}
defer f.Close()
md, err := f.Moduledata()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not retrieve the file's moduledata: %s.\n", err)
return
}
tab := tabby.New()
tab.AddHeader("Section", "Address", "Size")
sections := []struct {
name string
info gore.ModuleDataSection
}{
{"text", md.Text()},
{"types", md.Types()},
{"itablinks", md.ITabLinks()},
{"pclntab", md.PCLNTab()},
{"functab", md.FuncTab()},
{"noptrdata", md.NoPtrData()},
{"data", md.Data()},
{"bss", md.Bss()},
{"noptrbss", md.NoPtrBss()},
}
for _, v := range sections {
tab.AddLine(v.name, fmt.Sprintf("0x%x", v.info.Address), fmt.Sprintf("0x%x", v.info.Length))
}
tab.Print()
}