-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapfile.go
415 lines (392 loc) · 10.7 KB
/
mapfile.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Package mapfile provides access to symbol map files (MAP file format)
// produced by linkers.
package mapfile
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/mewkiz/pkg/term"
"github.com/pkg/errors"
)
var (
// dbg is a logger with the "pdb:" prefix which logs debug messages to standard
// error.
dbg = log.New(os.Stderr, term.CyanBold("pdb:")+" ", 0)
// warn is a logger with the "pdb:" prefix which logs warning messages to
// standard error.
warn = log.New(os.Stderr, term.RedBold("pdb:")+" ", 0)
)
// Note: this package supports the MAP file format produced by Visual Studio
// Code. Future versions of mapfile may include support for the MAP file format
// produced by GCC.
// Map is a symbol map file.
type Map struct {
// Name of linker output.
Name string
// Link date.
Date time.Time
// Base address (preferred load address).
BaseAddr uint64
// Segment relative offset to entry point.
Entry SegmentOffset
// Sections.
Sects []*Section
// Symbols.
Syms []*Symbol
}
// ParseString parses the given symbol map file, reading from s.
func ParseString(s string) (*Map, error) {
r := strings.NewReader(s)
return Parse(r)
}
// ParseBytes parses the given symbol map file, reading from buf.
func ParseBytes(buf []byte) (*Map, error) {
r := bytes.NewReader(buf)
return Parse(r)
}
// ParseFile parses the given symbol map file, reading from mapPath.
func ParseFile(mapPath string) (*Map, error) {
buf, err := ioutil.ReadFile(mapPath)
if err != nil {
return nil, errors.WithStack(err)
}
return ParseBytes(buf)
}
// Parse parses the given symbol map file, reading from r.
func Parse(r io.Reader) (*Map, error) {
// Read lines.
lines, err := readLines(r)
if err != nil {
return nil, errors.WithStack(err)
}
// Example contents of foo.MAP file:
//
// FOO
//
// Timestamp is 5e97f112 (Wed Apr 15 22:45:54 2020)
//
// Preferred load address is 00400000
//
// Start Length Name Class
// 0001:00000000 001012c6H .text CODE
// 0002:00000000 00007c18H .rdata DATA
// ...
//
// Address Publics by Value Rva+Base Lib:Object
//
// 0001:00000000 ?bar@@YIXH@Z 00401000 f baz.obj
// 0002:00000058 ?qux@@3PBDB 00503058 baz.obj
// 0003:00000040 ?fob@@3PADA 0050b040 baz.obj
// 0004:00000000 __IMPORT_DESCRIPTOR_KERNEL32 00731000 kernel32:KERNEL32.dll
// ...
//
// entry point at 0001:000f0290
//
// Static symbols
//
// 0001:000dc1c2 ?quux@@YIXXZ 004dd1c2 f quuz.obj
//
// FIXUPS: 101506 21 13 21 15 21 39f 5f 114 211 10 17 9 4a 32 61 64 33 30
// ...
m := &Map{}
for i := 0; i < len(lines); i++ {
line := lines[i]
switch {
// Name of linker output.
case i == 0:
m.Name = line // first line is linker output name.
// Link date.
case strings.HasPrefix(line, "Timestamp is "):
// Timestamp is 5e97f112 (Wed Apr 15 22:45:54 2020)
rawDate := line[len("Timestamp is 5e97f112 (") : len(line)-len(")")]
date, err := time.Parse(time.ANSIC, rawDate)
if err != nil {
return nil, errors.WithStack(err)
}
m.Date = date
// Base address.
case strings.HasPrefix(line, "Preferred load address is "):
// Preferred load address is 00400000
rawBaseAddr := line[len("Preferred load address is "):]
baseAddr, err := strconv.ParseUint(rawBaseAddr, 16, 64)
if err != nil {
return nil, errors.WithStack(err)
}
m.BaseAddr = baseAddr
// List of sections.
// Start Length Name Class
case hasFields(line, []string{"Start", "Length", "Name", "Class"}):
sects, n, err := parseSections(lines[i:])
if err != nil {
return nil, errors.WithStack(err)
}
m.Sects = append(m.Sects, sects...)
i += n - 1
// List of symbols.
// Address Publics by Value Rva+Base Lib:Object
case hasFields(line, []string{"Address", "Publics", "by", "Value", "Rva+Base", "Lib:Object"}):
fallthrough
case strings.HasPrefix(line, "Static symbols"):
syms, n, err := parseSymbols(lines[i:])
if err != nil {
return nil, errors.WithStack(err)
}
m.Syms = append(m.Syms, syms...)
i += n - 1
// Entry point.
case strings.HasPrefix(line, "entry point at"):
// entry point at 0001:000f0290
rawEntry := strings.TrimSpace(strings.TrimPrefix(line, "entry point at"))
entry, err := parseSegmentOffset(rawEntry)
if err != nil {
return nil, errors.WithStack(err)
}
m.Entry = entry
case strings.HasPrefix(line, "FIXUPS:"):
// ignore.
case len(line) == 0:
// skip empty lines.
default:
warn.Printf("support for line prefix %q not yet implemented", line)
}
}
return m, nil
}
// hasFields reports whether the given line contains the specified fields, as
// separated by whitespace.
func hasFields(line string, fields []string) bool {
got := strings.Fields(line)
if len(fields) != len(got) {
return false
}
for i := range fields {
want := fields[i]
if want != got[i] {
return false
}
}
return true
}
// parseSections parses a list of sections from the given lines, terminated by a
// blank line.
func parseSections(lines []string) (sects []*Section, n int, err error) {
// Skip header.
n++
// Parse list of sections.
for ; n < len(lines); n++ {
line := lines[n]
if len(line) == 0 {
// End of sections list reached.
break
}
// 0001:00000000 001012c6H .text CODE
sect, err := parseSection(line)
if err != nil {
return nil, n, errors.WithStack(err)
}
sects = append(sects, sect)
}
return sects, n, nil
}
// parseSymbols parses a list of symbols from the given lines, terminated by a
// blank line.
func parseSymbols(lines []string) (syms []*Symbol, n int, err error) {
// Parse header.
header := lines[n]
isStatic := strings.HasPrefix(header, "Static symbols")
n++
// Parse empty line between header and list of symbols.
emptyLine := lines[n]
if len(emptyLine) != 0 {
return nil, n, errors.Errorf("unexpected line between header and list of symbols; expected empty line, got %q", emptyLine)
}
n++
// Parse list of symbols.
for ; n < len(lines); n++ {
line := lines[n]
if len(line) == 0 {
// End of symbols list reached.
break
}
// 0001:00000000 ?bar@@YIXH@Z 00401000 f baz.obj
sym, err := parseSymbol(line)
if err != nil {
return nil, n, errors.WithStack(err)
}
sym.IsStatic = isStatic
syms = append(syms, sym)
}
return syms, n, nil
}
// Section tracks section linkage information.
type Section struct {
// Section name.
Name string
// Segment relative offset to start of section.
Start SegmentOffset
// Size of section in bytes.
Size int
// Section type (code or data).
Type SectionType
}
// parseSection parses the string representation of the given section.
func parseSection(s string) (*Section, error) {
// Example:
//
// 0001:00000000 001012c6H .text CODE
fields := strings.Fields(s)
sect := &Section{}
// Start of section (offset relative to segment).
//
// 0001:00000000
rawStart := fields[0]
start, err := parseSegmentOffset(rawStart)
if err != nil {
return nil, errors.WithStack(err)
}
sect.Start = start
// Size in bytes.
//
// 001012c6H
rawSize := strings.TrimSuffix(fields[1], "H")
size, err := strconv.ParseUint(rawSize, 16, 64)
if err != nil {
return nil, errors.WithStack(err)
}
sect.Size = int(size)
// Section name.
//
// .text
sect.Name = fields[2]
// Section type.
//
// CODE
sect.Type = SectionTypeFromString(fields[3])
return sect, nil
}
//go:generate stringer -linecomment -type SectionType
//go:generate string2enum -samepkg -linecomment -type SectionType
// SectionType specifies the type of a section (code or data).
type SectionType uint8
// Section types.
const (
SectionTypeCode SectionType = iota + 1 // CODE
SectionTypeData // DATA
)
// Symbol is a symbol with linker information.
type Symbol struct {
// Demangled name of symbol.
Name string
// Mangled name of symbol (e.g. "_WinMain@16").
MangledName string
// Virtual address of symbol (relative virtual address + base address).
Addr uint64
// Segment relative offset to start of symbol.
Start SegmentOffset
// File name of object containing symbol ([libname:]filename).
ObjectName string
// Symbols is a function.
IsFunc bool
// Symbols is static.
IsStatic bool
}
// parseSymbol parses the string representation of the given symbol.
func parseSymbol(s string) (*Symbol, error) {
// Example:
//
// 0001:00000000 ?bar@@YIXH@Z 00401000 f baz.obj
sym := &Symbol{}
fields := strings.Fields(s)
// Start of symbol (offset relative to segment).
//
// 0001:00000000
rawStart := fields[0]
start, err := parseSegmentOffset(rawStart)
if err != nil {
return nil, errors.WithStack(err)
}
sym.Start = start
// Symbol name.
//
// ?bar@@YIXH@Z
sym.MangledName = fields[1]
// TODO: demangle symbol name.
// Address of symbol.
//
// 00401000
rawAddr := fields[2]
addr, err := strconv.ParseUint(rawAddr, 16, 64)
if err != nil {
return nil, errors.WithStack(err)
}
sym.Addr = addr
// (optional) Symbol type.
//
// f
if len(fields) == 5 {
rawSymbolType := fields[3]
switch rawSymbolType {
case "f":
sym.IsFunc = true
default:
panic(fmt.Errorf("support for symbol type %q not yet implemented", rawSymbolType))
}
}
// Object name.
//
// baz.obj
sym.ObjectName = fields[len(fields)-1]
return sym, nil
}
// SegmentOffset specifies a segment relative offset.
type SegmentOffset struct {
// Segment number.
SegNum int
// Offset in bytes from start of segment.
Offset uint64
}
// parseSegmentOffset parses the string representation of the given segment
// offset.
func parseSegmentOffset(s string) (SegmentOffset, error) {
// Example:
//
// 0001:00093247
var segOffset SegmentOffset
// Segment number.
parts := strings.Split(s, ":")
rawSegNum := parts[0]
segNum, err := strconv.ParseUint(rawSegNum, 16, 64)
if err != nil {
return SegmentOffset{}, errors.WithStack(err)
}
segOffset.SegNum = int(segNum)
// Offset in bytes from start of segment.
rawOffset := parts[1]
offset, err := strconv.ParseUint(rawOffset, 16, 64)
if err != nil {
return SegmentOffset{}, errors.WithStack(err)
}
segOffset.Offset = offset
return segOffset, nil
}
// readLines reads and returns the lines of r, trimming spaces of each line.
func readLines(r io.Reader) ([]string, error) {
s := bufio.NewScanner(r)
var lines []string
for s.Scan() {
line := s.Text()
line = strings.TrimSpace(line)
lines = append(lines, line)
}
if err := s.Err(); err != nil {
return nil, errors.WithStack(err)
}
return lines, nil
}