-
Notifications
You must be signed in to change notification settings - Fork 0
/
disasm.go
598 lines (525 loc) · 15.7 KB
/
disasm.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// https://cs.opensource.google/go/go/+/refs/tags/go1.17.2:src/cmd/internal/objfile/disasm.go;drc=refs%2Ftags%2Fgo1.17.2;l=43
package file
import (
"bufio"
"bytes"
"container/list"
"debug/gosym"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"text/tabwriter"
"golang.org/x/arch/arm/armasm"
"golang.org/x/arch/arm64/arm64asm"
"golang.org/x/arch/ppc64/ppc64asm"
"golang.org/x/arch/x86/x86asm"
)
// Added from cmd/internal/src
const FileSymPrefix = "gofile.."
// Disasm is a disassembler for a given File.
type Disasm struct {
syms []Symbol // symbols in file, sorted by address
pcln Liner // pcln table
text []byte // bytes of text segment (actual instructions)
textStart uint64 // start PC of text
textEnd uint64 // end PC of text
goarch string // GOARCH string
disasm disasmFunc // disassembler function for goarch
byteOrder binary.ByteOrder // byte order for goarch
gnulookup gnuFunc // Lookup GNU assembly
}
// Disasm returns a disassembler for the file f.
func (e *Entry) Disasm() (*Disasm, error) {
syms, err := e.Symbols()
if err != nil {
return nil, err
}
// We want to disassemble both known and unknown (imported) symbols
// TODO we could use this to derive import status?
dyns, err := e.DynamicSymbols()
if err != nil {
return nil, err
}
pcln, err := e.PCLineTable()
if err != nil {
return nil, err
}
syms = append(syms, dyns...)
textStart, textBytes, err := e.Text()
if err != nil {
return nil, err
}
goarch := e.GOARCH()
disasm := disasms[goarch]
gnulookup := gnuLookup[goarch]
byteOrder := byteOrders[goarch]
if disasm == nil || byteOrder == nil {
return nil, fmt.Errorf("unsupported architecture")
}
// Filter out section symbols, overwriting syms in place.
keep := syms[:0]
for _, sym := range syms {
switch sym.GetName() {
case "runtime.text", "text", "_text", "runtime.etext", "etext", "_etext":
// drop
default:
keep = append(keep, sym)
}
}
syms = keep
d := &Disasm{
syms: syms,
pcln: pcln,
text: textBytes,
textStart: textStart,
textEnd: textStart + uint64(len(textBytes)),
goarch: goarch,
disasm: disasm,
gnulookup: gnulookup,
byteOrder: byteOrder,
}
return d, nil
}
// lookup finds the symbol name containing addr.
func (d *Disasm) Lookup(addr uint64) (name string, base uint64) {
i := sort.Search(len(d.syms), func(i int) bool { return addr < d.syms[i].GetAddress() })
if i > 0 {
s := d.syms[i-1]
if s.GetAddress() != 0 && s.GetAddress() <= addr && addr < s.GetAddress()+uint64(s.GetSize()) {
return s.GetName(), s.GetAddress()
}
}
return "", 0
}
// base returns the final element in the path.
// It works on both Windows and Unix paths,
// regardless of host operating system.
func base(path string) string {
path = path[strings.LastIndex(path, "/")+1:]
path = path[strings.LastIndex(path, `\`)+1:]
return path
}
// CachedFile contains the content of a file split into lines.
type CachedFile struct {
FileName string
Lines [][]byte
}
// FileCache is a simple LRU cache of file contents.
type FileCache struct {
files *list.List
maxLen int
}
// NewFileCache returns a FileCache which can contain up to maxLen cached file contents.
func NewFileCache(maxLen int) *FileCache {
return &FileCache{
files: list.New(),
maxLen: maxLen,
}
}
// Line returns the source code line for the given file and line number.
// If the file is not already cached, reads it, inserts it into the cache,
// and removes the least recently used file if necessary.
// If the file is in cache, it is moved to the front of the list.
func (fc *FileCache) Line(filename string, line int) ([]byte, error) {
if filepath.Ext(filename) != ".go" {
return nil, nil
}
// Clean filenames returned by src.Pos.SymFilename()
// or src.PosBase.SymFilename() removing
// the leading src.FileSymPrefix.
filename = strings.TrimPrefix(filename, FileSymPrefix)
// Expand literal "$GOROOT" rewritten by obj.AbsFile()
filename = filepath.Clean(os.ExpandEnv(filename))
var cf *CachedFile
var e *list.Element
for e = fc.files.Front(); e != nil; e = e.Next() {
cf = e.Value.(*CachedFile)
if cf.FileName == filename {
break
}
}
if e == nil {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
cf = &CachedFile{
FileName: filename,
Lines: bytes.Split(content, []byte{'\n'}),
}
fc.files.PushFront(cf)
if fc.files.Len() >= fc.maxLen {
fc.files.Remove(fc.files.Back())
}
} else {
fc.files.MoveToFront(e)
}
// because //line directives can be out-of-range. (#36683)
if line-1 >= len(cf.Lines) || line-1 < 0 {
return nil, nil
}
return cf.Lines[line-1], nil
}
// Print prints a disassembly of the file to w.
// If filter is non-nil, the disassembly only includes functions with names matching filter.
// If printCode is true, the disassembly includs corresponding source lines.
// The disassembly only includes functions that overlap the range [start, end).
func (d *Disasm) Print(w io.Writer, filter *regexp.Regexp, start, end uint64, printCode bool, gnuAsm bool) {
if start < d.textStart {
start = d.textStart
}
if end > d.textEnd {
end = d.textEnd
}
printed := false
bw := bufio.NewWriter(w)
var fc *FileCache
if printCode {
fc = NewFileCache(8)
}
tw := tabwriter.NewWriter(bw, 18, 8, 1, '\t', tabwriter.StripEscape)
for _, sym := range d.syms {
symStart := sym.GetAddress()
symEnd := sym.GetAddress() + uint64(sym.GetSize())
relocs := sym.GetRelocations()
if sym.GetCode() != 'T' && sym.GetCode() != 't' ||
symStart < d.textStart ||
symEnd <= start || end <= symStart ||
filter != nil && !filter.MatchString(sym.GetName()) {
continue
}
if printed {
fmt.Fprintf(bw, "\n")
}
printed = true
file, _, _ := d.pcln.PCToLine(sym.GetAddress())
fmt.Fprintf(bw, "TEXT %s(SB) %s\n", sym.GetName(), file)
if symEnd > end {
symEnd = end
}
code := d.text[:end-d.textStart]
var lastFile string
var lastLine int
d.Decode(symStart, symEnd, relocs, gnuAsm, func(pc, size uint64, file string, line int, text string) {
i := pc - d.textStart
if printCode {
if file != lastFile || line != lastLine {
if srcLine, err := fc.Line(file, line); err == nil {
fmt.Fprintf(tw, "%s%s%s\n", []byte{tabwriter.Escape}, srcLine, []byte{tabwriter.Escape})
}
lastFile, lastLine = file, line
}
fmt.Fprintf(tw, " %#x\t", pc)
} else {
fmt.Fprintf(tw, " %s:%d\t%#x\t", base(file), line, pc)
}
if size%4 != 0 || d.goarch == "386" || d.goarch == "amd64" {
// Print instruction as bytes.
fmt.Fprintf(tw, "%x", code[i:i+size])
} else {
// Print instruction as 32-bit words.
for j := uint64(0); j < size; j += 4 {
if j > 0 {
fmt.Fprintf(tw, " ")
}
fmt.Fprintf(tw, "%08x", d.byteOrder.Uint32(code[i+j:]))
}
}
fmt.Fprintf(tw, "\t%s\t\n", text)
})
tw.Flush()
}
bw.Flush()
}
// GNUAssembly holds parsed assembly for symbols
type Instruction struct {
Text string
Source string
Assembly string
PCLine uint64
Size uint64
}
type GNUAssembly struct {
SymbolName string
File string
PCLine uint64
SrcLine string
Instructions []Instruction
}
type GNUAssemblyLookup map[string]GNUAssembly
// GetAssembly returns a full data structure of instructions to further parse
func (d *Disasm) GetGNUAssembly() GNUAssemblyLookup {
fc := NewFileCache(8)
asm := map[string]GNUAssembly{}
for _, sym := range d.syms {
symStart := sym.GetAddress()
symEnd := sym.GetAddress() + uint64(sym.GetSize())
relocs := sym.GetRelocations()
if sym.GetCode() != 'T' && sym.GetCode() != 't' ||
symStart < d.textStart ||
symEnd <= d.textStart ||
symEnd-d.textStart > uint64(len(d.text)) {
continue
}
file, _, _ := d.pcln.PCToLine(sym.GetAddress())
entry := GNUAssembly{SymbolName: sym.GetName(), File: file}
code := d.text[:symEnd-d.textStart]
var lastFile string
var lastLine int
instructions := d.DecodeGNUAssembly(symStart, symEnd, relocs, func(pc, size uint64, file string, line int, text string) Instruction {
i := pc - d.textStart
instruction := Instruction{PCLine: pc, Size: size}
if file != lastFile || line != lastLine {
if srcLine, err := fc.Line(file, line); err == nil {
instruction.Source = fmt.Sprintf("%s", srcLine)
}
lastFile, lastLine = file, line
}
if size%4 != 0 || d.goarch == "386" || d.goarch == "amd64" {
// Print instruction as bytes.
instruction.Text = fmt.Sprintf("%x", code[i:i+size])
} else {
// Print instruction as 32-bit words.
for j := uint64(0); j < size; j += 4 {
if j > 0 {
text += " "
}
instruction.Text = fmt.Sprintf("%08x", d.byteOrder.Uint32(code[i+j:]))
}
}
return instruction
})
entry.Instructions = append(entry.Instructions, instructions...)
asm[entry.SymbolName] = entry
}
return asm
}
// DecodeGNUAssembly disassembles the text segment range [start, end), calling f for each instruction.
func (d *Disasm) DecodeGNUAssembly(start, end uint64, relocs []Relocation, f func(pc, size uint64, file string, line int, text string) Instruction) []Instruction {
if start < d.textStart {
start = d.textStart
}
if end > d.textEnd {
end = d.textEnd
}
instructions := []Instruction{}
code := d.text[:end-d.textStart]
for pc := start; pc < end; {
i := pc - d.textStart
text, size := d.gnulookup(code[i:], pc, d.byteOrder)
file, line, _ := d.pcln.PCToLine(pc)
sep := "\t"
for len(relocs) > 0 && relocs[0].Address < i+uint64(size) {
text += sep + relocs[0].Stringer.String(pc-start)
sep = " "
relocs = relocs[1:]
}
instruction := f(pc, uint64(size), file, line, text)
instruction.Text = text
instructions = append(instructions, instruction)
pc += uint64(size)
}
return instructions
}
// Decode disassembles the text segment range [start, end), calling f for each instruction.
func (d *Disasm) Decode(start, end uint64, relocs []Relocation, gnuAsm bool, f func(pc, size uint64, file string, line int, text string)) {
if start < d.textStart {
start = d.textStart
}
if end > d.textEnd {
end = d.textEnd
}
code := d.text[:end-d.textStart]
lookup := d.Lookup
for pc := start; pc < end; {
i := pc - d.textStart
text, size := d.disasm(code[i:], pc, lookup, d.byteOrder, gnuAsm)
file, line, _ := d.pcln.PCToLine(pc)
sep := "\t"
for len(relocs) > 0 && relocs[0].Address < i+uint64(size) {
text += sep + relocs[0].Stringer.String(pc-start)
sep = " "
relocs = relocs[1:]
}
f(pc, uint64(size), file, line, text)
pc += uint64(size)
}
}
type lookupFunc = func(addr uint64) (sym string, base uint64)
type disasmFunc func(code []byte, pc uint64, lookup lookupFunc, ord binary.ByteOrder, _ bool) (text string, size int)
// Disasm Functions
func disasm_386(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder, gnuAsm bool) (string, int) {
return disasm_x86(code, pc, lookup, 32, gnuAsm)
}
func disasm_amd64(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder, gnuAsm bool) (string, int) {
return disasm_x86(code, pc, lookup, 64, gnuAsm)
}
func disasm_x86(code []byte, pc uint64, lookup lookupFunc, arch int, gnuAsm bool) (string, int) {
inst, err := x86asm.Decode(code, arch)
var text string
size := inst.Len
if err != nil || size == 0 || inst.Op == 0 {
size = 1
text = "?"
} else {
if gnuAsm {
text = fmt.Sprintf("%-36s // %s", x86asm.GoSyntax(inst, pc, lookup), x86asm.GNUSyntax(inst, pc, nil))
} else {
text = x86asm.GoSyntax(inst, pc, lookup)
}
}
return text, size
}
type textReader struct {
code []byte
pc uint64
}
func (r textReader) ReadAt(data []byte, off int64) (n int, err error) {
if off < 0 || uint64(off) < r.pc {
return 0, io.EOF
}
d := uint64(off) - r.pc
if d >= uint64(len(r.code)) {
return 0, io.EOF
}
n = copy(data, r.code[d:])
if n < len(data) {
err = io.ErrUnexpectedEOF
}
return
}
func disasm_arm(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder, gnuAsm bool) (string, int) {
inst, err := armasm.Decode(code, armasm.ModeARM)
var text string
size := inst.Len
if err != nil || size == 0 || inst.Op == 0 {
size = 4
text = "?"
} else if gnuAsm {
text = fmt.Sprintf("%-36s // %s", armasm.GoSyntax(inst, pc, lookup, textReader{code, pc}), armasm.GNUSyntax(inst))
} else {
text = armasm.GoSyntax(inst, pc, lookup, textReader{code, pc})
}
return text, size
}
func disasm_arm64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.ByteOrder, gnuAsm bool) (string, int) {
inst, err := arm64asm.Decode(code)
var text string
if err != nil || inst.Op == 0 {
text = "?"
} else if gnuAsm {
text = fmt.Sprintf("%-36s // %s", arm64asm.GoSyntax(inst, pc, lookup, textReader{code, pc}), arm64asm.GNUSyntax(inst))
} else {
text = arm64asm.GoSyntax(inst, pc, lookup, textReader{code, pc})
}
return text, 4
}
func disasm_ppc64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.ByteOrder, gnuAsm bool) (string, int) {
inst, err := ppc64asm.Decode(code, byteOrder)
var text string
size := inst.Len
if err != nil || size == 0 {
size = 4
text = "?"
} else {
if gnuAsm {
text = fmt.Sprintf("%-36s // %s", ppc64asm.GoSyntax(inst, pc, lookup), ppc64asm.GNUSyntax(inst, pc))
} else {
text = ppc64asm.GoSyntax(inst, pc, lookup)
}
}
return text, size
}
var disasms = map[string]disasmFunc{
"386": disasm_386,
"amd64": disasm_amd64,
"arm": disasm_arm,
"arm64": disasm_arm64,
"ppc64": disasm_ppc64,
"ppc64le": disasm_ppc64,
}
// GNU assembly lookup
type gnuFunc func(code []byte, pc uint64, ord binary.ByteOrder) (text string, size int)
func gnulookup_386(code []byte, pc uint64, _ binary.ByteOrder) (string, int) {
return gnulookup_x86(code, pc, 32)
}
func gnulookup_amd64(code []byte, pc uint64, _ binary.ByteOrder) (string, int) {
return gnulookup_x86(code, pc, 64)
}
func gnulookup_x86(code []byte, pc uint64, arch int) (string, int) {
inst, err := x86asm.Decode(code, arch)
var text string
size := inst.Len
if err != nil || size == 0 || inst.Op == 0 {
size = 1
text = "?"
} else {
text = fmt.Sprintf("%s", x86asm.GNUSyntax(inst, pc, nil))
}
return text, size
}
func gnulookup_arm(code []byte, pc uint64, _ binary.ByteOrder) (string, int) {
inst, err := armasm.Decode(code, armasm.ModeARM)
var text string
size := inst.Len
if err != nil || size == 0 || inst.Op == 0 {
size = 4
text = "?"
} else {
text = fmt.Sprintf("%s", armasm.GNUSyntax(inst))
}
return text, size
}
func gnulookup_arm64(code []byte, pc uint64, byteOrder binary.ByteOrder) (string, int) {
inst, err := arm64asm.Decode(code)
var text string
if err != nil || inst.Op == 0 {
text = "?"
} else {
text = fmt.Sprintf("%s", arm64asm.GNUSyntax(inst))
}
return text, 4
}
func gnulookup_ppc64(code []byte, pc uint64, byteOrder binary.ByteOrder) (string, int) {
inst, err := ppc64asm.Decode(code, byteOrder)
var text string
size := inst.Len
if err != nil || size == 0 {
size = 4
text = "?"
} else {
text = fmt.Sprintf("%s", ppc64asm.GNUSyntax(inst, pc))
}
return text, size
}
var gnuLookup = map[string]gnuFunc{
"386": gnulookup_386,
"amd64": gnulookup_amd64,
"arm": gnulookup_arm,
"arm64": gnulookup_arm64,
"ppc64": gnulookup_ppc64,
"ppc64le": gnulookup_ppc64,
}
var byteOrders = map[string]binary.ByteOrder{
"386": binary.LittleEndian,
"amd64": binary.LittleEndian,
"arm": binary.LittleEndian,
"arm64": binary.LittleEndian,
"ppc64": binary.BigEndian,
"ppc64le": binary.LittleEndian,
"s390x": binary.BigEndian,
}
type Liner interface {
// Given a pc, returns the corresponding file, line, and function data.
// If unknown, returns "",0,nil.
PCToLine(uint64) (string, int, *gosym.Func)
}