-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsym.go
283 lines (211 loc) · 5.24 KB
/
gsym.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package gogsym
import (
"encoding/hex"
"errors"
"fmt"
"io"
"sort"
"github.com/chimehq/binarycursor"
)
const GSYM_MAGIC uint32 = 0x4753594d
const GSYM_CIGAM uint32 = 0x4d595347
const GSYM_MAX_UUID_SIZE = 20
const GSYM_HEADER_SIZE = 28 + GSYM_MAX_UUID_SIZE
var ErrUnsupportedVersion = errors.New("Unsupported Version")
var ErrAddressOutOfRange = errors.New("Address out of range")
var ErrUUIDSizeOutOfRange = errors.New("UUID size out of range")
var ErrAddressSizeOutOfrange = errors.New("Address size out of range")
var ErrAddressNotFound = errors.New("Address not found")
type Header struct {
Magic uint32
Version uint16
AddrOffSize uint8
UUIDSize uint8
BaseAddress uint64
NumAddresses uint32
StrtabOffset uint32
StrtabSize uint32
UUID [GSYM_MAX_UUID_SIZE]byte
}
func (h Header) Size() int64 {
return int64(GSYM_HEADER_SIZE)
}
func newHeader(bc binarycursor.BinaryCursor) (Header, error) {
h := Header{}
var err error
h.Magic, err = bc.ReadUint32()
if err != nil {
return h, err
}
if h.Magic == GSYM_CIGAM {
bc.FlipOrder()
}
h.Version, err = bc.ReadUint16()
if err != nil {
return h, err
}
if h.Version != uint16(1) {
return h, ErrUnsupportedVersion
}
h.AddrOffSize, err = bc.ReadUint8()
if err != nil {
return h, err
}
h.UUIDSize, err = bc.ReadUint8()
if err != nil {
return h, err
}
if h.UUIDSize > GSYM_MAX_UUID_SIZE {
return h, ErrUUIDSizeOutOfRange
}
h.BaseAddress, err = bc.ReadUint64()
if err != nil {
return h, err
}
h.NumAddresses, err = bc.ReadUint32()
if err != nil {
return h, err
}
h.StrtabOffset, err = bc.ReadUint32()
if err != nil {
return h, err
}
h.StrtabSize, err = bc.ReadUint32()
if err != nil {
return h, err
}
n, err := bc.Read(h.UUID[0:h.UUIDSize])
if n != int(h.UUIDSize) {
return h, fmt.Errorf("Expected %d UUIDS bytes, got %d", h.UUIDSize, n)
}
return h, nil
}
func (h Header) UUIDBytes() []byte {
return h.UUID[0:h.UUIDSize]
}
func (h Header) UUIDString() string {
return hex.EncodeToString(h.UUIDBytes())
}
type Gsym struct {
readerAt io.ReaderAt
cursor binarycursor.BinaryCursor
Header Header
}
func NewGsymWithReader(r io.ReaderAt) (Gsym, error) {
bc := binarycursor.NewBinaryReaderAtCursor(r, 0)
g := Gsym{
readerAt: r,
cursor: bc,
Header: Header{},
}
header, err := newHeader(bc)
if err != nil {
return g, err
}
g.Header = header
return g, nil
}
func (g Gsym) cursorAt(offset int64) binarycursor.BinaryCursor {
c := binarycursor.NewBinaryReaderAtCursor(g.readerAt, offset)
c.SetOrder(g.cursor.Order())
return c
}
func (g Gsym) AddressTableOffset() int64 {
return int64(g.Header.Size())
}
func (g Gsym) ReadAddressEntry(idx int) (uint64, error) {
offset := int64(idx)*int64(g.Header.AddrOffSize) + g.AddressTableOffset()
cursor := g.cursorAt(offset)
switch g.Header.AddrOffSize {
case 1:
v8, err := cursor.ReadUint8()
return uint64(v8), err
case 2:
v16, err := cursor.ReadUint16()
return uint64(v16), err
case 4:
v32, err := cursor.ReadUint32()
return uint64(v32), err
case 8:
return cursor.ReadUint64()
}
return uint64(0), ErrAddressSizeOutOfrange
}
func (g Gsym) GetTextRelativeAddressIndex(addr uint64) (int, error) {
return g.GetAddressIndex(addr + g.Header.BaseAddress)
}
func (g Gsym) GetAddressIndex(addr uint64) (int, error) {
if addr < g.Header.BaseAddress {
return 0, ErrAddressOutOfRange
}
relAddr := addr - g.Header.BaseAddress
count := int(g.Header.NumAddresses)
idx := sort.Search(count, func(i int) bool {
entryAddr, _ := g.ReadAddressEntry(i)
return entryAddr >= relAddr
})
entryAddr, err := g.ReadAddressEntry(idx)
if err != nil {
return 0, err
}
if idx == 0 && relAddr < entryAddr {
return 0, ErrAddressNotFound
}
if idx == count || relAddr < entryAddr {
idx -= 1
}
return idx, nil
}
func (g Gsym) AddressInfoTableOffset() int64 {
addrTableSize := int64(g.Header.NumAddresses) * int64(g.Header.AddrOffSize)
return g.AddressTableOffset() + addrTableSize
}
func (g Gsym) GetAddressInfoOffset(index int) (int64, error) {
offset := g.AddressInfoTableOffset() + int64(index*4)
c := g.cursorAt(offset)
value, err := c.ReadUint32()
return int64(value), err
}
func (g Gsym) GetString(offset int64) (string, error) {
strOffset := int64(g.Header.StrtabOffset) + offset
c := g.cursorAt(strOffset)
return c.ReadNullTerminatedUTF8String()
}
type FileEntry struct {
DirStrOffset uint32
BaseStrOffset uint32
}
func (g Gsym) GetFileEntry(index uint32) (FileEntry, error) {
offset := g.AddressInfoTableOffset() + int64(g.Header.NumAddresses*4)
// offset: uint32 count
// offset + 4: uint32(0), uint32(0)
// and, every entry is 2 uint32s
offset += 4 + int64(index)*4*2
c := g.cursorAt(offset)
entry := FileEntry{}
var err error = nil
entry.DirStrOffset, err = c.ReadUint32()
if err != nil {
return entry, err
}
entry.BaseStrOffset, err = c.ReadUint32()
return entry, err
}
func (g Gsym) GetFile(index uint32) (string, error) {
if index == 0 {
return "", nil
}
entry, err := g.GetFileEntry(index)
if err != nil {
return "", err
}
dir, err := g.GetString(int64(entry.DirStrOffset))
if err != nil {
return "", err
}
base, err := g.GetString(int64(entry.BaseStrOffset))
if err != nil {
return "", err
}
return dir + "/" + base, nil
}