-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
744 lines (650 loc) · 19 KB
/
parser.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
package goobj
import (
"bufio"
"errors"
"fmt"
"os"
"reflect"
)
const supportedGoObjVersion = 1
var magicHeader = []byte("\x00\x00go19ld")
var magicFooter = []byte("\xffgo19ld")
// File represents a go object file.
type File struct {
Symbols []Symbol
SymbolReferences []SymbolReference
DataBlock []byte
// the data block starts at this position of the object file
DataBlockPosition int64
}
// SymbolReference represents a symbol's name and its version.
type SymbolReference struct {
Name string
Version int64
}
// Symbol describes metadata associated with data block.
type Symbol struct {
IDIndex int64
Kind SymKind
Size int64
DupOK bool
Local bool
Typelink bool
GoTypeIndex int64
DataAddr DataAddr
Relocations []Relocation
// STEXT type has additional fields
stextFields *StextFields
}
// SymKind represents a type of symbol
type SymKind uint8
// taken from go1.10 cmd/internal/objabi
const (
// An otherwise invalid zero value for the type
Sxxx SymKind = iota
// Executable instructions
STEXT
// Read only static data
SRODATA
// Static data that does not contain any pointers
SNOPTRDATA
// Static data
SDATA
// Statically data that is initially all 0s
SBSS
// Statically data that is initially all 0s and does not contain pointers
SNOPTRBSS
// Thread-local data that is initially all 0s
STLSBSS
// Debugging data
SDWARFINFO
SDWARFRANGE
SDWARFLOC
)
func (kind SymKind) String() string {
switch kind {
case Sxxx:
return "INVALID"
case STEXT:
return "STEXT"
case SRODATA:
return "SRODATA"
case SNOPTRDATA:
return "SNOPTRDATA"
case SDATA:
return "SDATA"
case SBSS:
return "SBSS"
case SNOPTRBSS:
return "SNOPTRBSS"
case STLSBSS:
return "STLSBSS"
case SDWARFINFO:
return "SDWARFINFO"
case SDWARFRANGE:
return "SDWARFRANGE"
case SDWARFLOC:
return "SDWARFLOC"
default:
return "UNKNOWN"
}
}
// Relocation represents a symbol to be relocated and how to relocate it.
type Relocation struct {
Offset int64
Size int64
Type RelocType
Add int64
IDIndex int64
}
// RelocType describes a way to relocate a symbol.
type RelocType int32
// taken from go1.10 cmd/internal/objabi
const (
R_ADDR RelocType = 1 + iota
// R_ADDRPOWER relocates a pair of "D-form" instructions (instructions with 16-bit
// immediates in the low half of the instruction word), usually addis followed by
// another add or a load, inserting the "high adjusted" 16 bits of the address of
// the referenced symbol into the immediate field of the first instruction and the
// low 16 bits into that of the second instruction.
R_ADDRPOWER
// R_ADDRARM64 relocates an adrp, add pair to compute the address of the
// referenced symbol.
R_ADDRARM64
// R_ADDRMIPS (only used on mips/mips64) resolves to the low 16 bits of an external
// address, by encoding it into the instruction.
R_ADDRMIPS
// R_ADDROFF resolves to a 32-bit offset from the beginning of the section
// holding the data being relocated to the referenced symbol.
R_ADDROFF // 5
// R_WEAKADDROFF resolves just like R_ADDROFF but is a weak relocation.
// A weak relocation does not make the symbol it refers to reachable,
// and is only honored by the linker if the symbol is in some other way
// reachable.
R_WEAKADDROFF
R_SIZE
R_CALL // 8
R_CALLARM
R_CALLARM64
R_CALLIND // 11
R_CALLPOWER
// R_CALLMIPS (only used on mips64) resolves to non-PC-relative target address
// of a CALL (JAL) instruction, by encoding the address into the instruction.
R_CALLMIPS
R_CONST
R_PCREL // 15
// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
// thread-local symbol from the thread local base and is used to implement the
// "local exec" model for tls access (r.Sym is not set on intel platforms but is
// set to a TLS symbol -- runtime.tlsg -- in the linker when externally linking).
R_TLS_LE
// R_TLS_IE, used 386, amd64, and ARM resolves to the PC-relative offset to a GOT
// slot containing the offset from the thread-local symbol from the thread local
// base and is used to implemented the "initial exec" model for tls access (r.Sym
// is not set on intel platforms but is set to a TLS symbol -- runtime.tlsg -- in
// the linker when externally linking).
R_TLS_IE
R_GOTOFF
R_PLT0
R_PLT1
R_PLT2
R_USEFIELD
// R_USETYPE resolves to an *rtype, but no relocation is created. The
// linker uses this as a signal that the pointed-to type information
// should be linked into the final binary, even if there are no other
// direct references. (This is used for types reachable by reflection.)
R_USETYPE
// R_METHODOFF resolves to a 32-bit offset from the beginning of the section
// holding the data being relocated to the referenced symbol.
// It is a variant of R_ADDROFF used when linking from the uncommonType of a
// *rtype, and may be set to zero by the linker if it determines the method
// text is unreachable by the linked program.
R_METHODOFF // 24
R_POWER_TOC
R_GOTPCREL
// R_JMPMIPS (only used on mips64) resolves to non-PC-relative target address
// of a JMP instruction, by encoding the address into the instruction.
// The stack nosplit check ignores this since it is not a function call.
R_JMPMIPS
// R_DWARFSECREF resolves to the offset of the symbol from its section.
// Target of relocation must be size 4 (in current implementation).
R_DWARFSECREF // 28
// R_DWARFFILEREF resolves to an index into the DWARF .debug_line
// file table for the specified file symbol. Must be applied to an
// attribute of form DW_FORM_data4.
R_DWARFFILEREF // 29
// Platform dependent relocations. Architectures with fixed width instructions
// have the inherent issue that a 32-bit (or 64-bit!) displacement cannot be
// stuffed into a 32-bit instruction, so an address needs to be spread across
// several instructions, and in turn this requires a sequence of relocations, each
// updating a part of an instruction. This leads to relocation codes that are
// inherently processor specific.
// Arm64.
// Set a MOV[NZ] immediate field to bits [15:0] of the offset from the thread
// local base to the thread local variable defined by the referenced (thread
// local) symbol. Error if the offset does not fit into 16 bits.
R_ARM64_TLS_LE
// Relocates an ADRP; LD64 instruction sequence to load the offset between
// the thread local base and the thread local variable defined by the
// referenced (thread local) symbol from the GOT.
R_ARM64_TLS_IE
// R_ARM64_GOTPCREL relocates an adrp, ld64 pair to compute the address of the GOT
// slot of the referenced symbol.
R_ARM64_GOTPCREL
// PPC64.
// R_POWER_TLS_LE is used to implement the "local exec" model for tls
// access. It resolves to the offset of the thread-local symbol from the
// thread pointer (R13) and inserts this value into the low 16 bits of an
// instruction word.
R_POWER_TLS_LE
// R_POWER_TLS_IE is used to implement the "initial exec" model for tls access. It
// relocates a D-form, DS-form instruction sequence like R_ADDRPOWER_DS. It
// inserts to the offset of GOT slot for the thread-local symbol from the TOC (the
// GOT slot is filled by the dynamic linker with the offset of the thread-local
// symbol from the thread pointer (R13)).
R_POWER_TLS_IE
// R_POWER_TLS marks an X-form instruction such as "MOVD 0(R13)(R31*1), g" as
// accessing a particular thread-local symbol. It does not affect code generation
// but is used by the system linker when relaxing "initial exec" model code to
// "local exec" model code.
R_POWER_TLS
// R_ADDRPOWER_DS is similar to R_ADDRPOWER above, but assumes the second
// instruction is a "DS-form" instruction, which has an immediate field occupying
// bits [15:2] of the instruction word. Bits [15:2] of the address of the
// relocated symbol are inserted into this field; it is an error if the last two
// bits of the address are not 0.
R_ADDRPOWER_DS
// R_ADDRPOWER_PCREL relocates a D-form, DS-form instruction sequence like
// R_ADDRPOWER_DS but inserts the offset of the GOT slot for the referenced symbol
// from the TOC rather than the symbol's address.
R_ADDRPOWER_GOT
// R_ADDRPOWER_PCREL relocates two D-form instructions like R_ADDRPOWER, but
// inserts the displacement from the place being relocated to the address of the
// the relocated symbol instead of just its address.
R_ADDRPOWER_PCREL
// R_ADDRPOWER_TOCREL relocates two D-form instructions like R_ADDRPOWER, but
// inserts the offset from the TOC to the address of the relocated symbol
// rather than the symbol's address.
R_ADDRPOWER_TOCREL
// R_ADDRPOWER_TOCREL relocates a D-form, DS-form instruction sequence like
// R_ADDRPOWER_DS but inserts the offset from the TOC to the address of the the
// relocated symbol rather than the symbol's address.
R_ADDRPOWER_TOCREL_DS
// R_PCRELDBL relocates s390x 2-byte aligned PC-relative addresses.
// TODO(mundaym): remove once variants can be serialized - see issue 14218.
R_PCRELDBL
// R_ADDRMIPSU (only used on mips/mips64) resolves to the sign-adjusted "upper" 16
// bits (bit 16-31) of an external address, by encoding it into the instruction.
R_ADDRMIPSU
// R_ADDRMIPSTLS (only used on mips64) resolves to the low 16 bits of a TLS
// address (offset from thread pointer), by encoding it into the instruction.
R_ADDRMIPSTLS
// R_ADDRCUOFF resolves to a pointer-sized offset from the start of the
// symbol's DWARF compile unit.
R_ADDRCUOFF // 44
)
func (relocType RelocType) String() string {
switch relocType {
case R_ADDR:
return "R_ADDR"
case R_ADDRPOWER:
return "R_ADDRPOWER"
case R_ADDRARM64:
return "R_ADDRARM64"
case R_ADDRMIPS:
return "R_ADDRMIPS"
case R_ADDROFF:
return "R_ADDROFF"
case R_WEAKADDROFF:
return "R_WEAKADDROFF"
case R_SIZE:
return "R_SIZE"
case R_CALL:
return "R_CALL"
case R_CALLARM:
return "R_CALLARM"
case R_CALLARM64:
return "R_CALLARM64"
case R_CALLIND:
return "R_CALLIND"
case R_CALLPOWER:
return "R_CALLPOWER"
case R_CALLMIPS:
return "R_CALLMIPS"
case R_CONST:
return "R_CONST"
case R_PCREL:
return "R_PCREL"
case R_TLS_LE:
return "R_TLS_LE"
case R_TLS_IE:
return "R_TLS_IE"
case R_GOTOFF:
return "R_GOTOFF"
case R_PLT0:
return "R_PLT0"
case R_PLT1:
return "R_PLT1"
case R_PLT2:
return "R_PLT2"
case R_USEFIELD:
return "R_USEFIELD"
case R_USETYPE:
return "R_USETYPE"
case R_METHODOFF:
return "R_METHODOFF"
case R_POWER_TOC:
return "R_POWER_TOC"
case R_GOTPCREL:
return "R_GOTPCREL"
case R_JMPMIPS:
return "R_JMPMIPS"
case R_DWARFSECREF:
return "R_DWARFSECREF"
case R_DWARFFILEREF:
return "R_DWARFFILEREF"
case R_ARM64_TLS_LE:
return "R_ARM64_TLS_LE"
case R_ARM64_TLS_IE:
return "R_ARM64_TLS_IE"
case R_ARM64_GOTPCREL:
return "R_ARM64_GOTPCREL"
case R_POWER_TLS_LE:
return "R_POWER_TLS_LE"
case R_POWER_TLS_IE:
return "R_POWER_TLS_IE"
case R_POWER_TLS:
return "R_POWER_TLS"
case R_ADDRPOWER_DS:
return "R_ADDRPOWER_DS"
case R_ADDRPOWER_GOT:
return "R_ADDRPOWER_GOT"
case R_ADDRPOWER_PCREL:
return "R_ADDRPOWER_PCREL"
case R_ADDRPOWER_TOCREL:
return "R_ADDRPOWER_TOCREL"
case R_ADDRPOWER_TOCREL_DS:
return "R_ADDRPOWER_TOCREL_DS"
case R_PCRELDBL:
return "R_PCRELDBL"
case R_ADDRMIPSU:
return "R_ADDRMIPSU"
case R_ADDRMIPSTLS:
return "R_ADDRMIPSTLS"
case R_ADDRCUOFF:
return "R_ADDRCUOFF"
default:
return "Unknown"
}
}
// StextFields represents additional metadata STEXT-type symbol have.
type StextFields struct {
Args int64
Frame int64
Leaf bool
CFunc bool
TypeMethod bool
SharedFunc bool
NoSplit bool
Local []Local
// pcln table
PCSP DataAddr
PCFile DataAddr
PCLine DataAddr
PCInline DataAddr
PCData []DataAddr
FuncDataIndex []int64
FuncDataOffset []int64
FileIndex []int64
}
// Local represents a local variable including input args and output.
type Local struct {
AsymIndex int64
Offset int64
Type int64
GotypeIndex int64
}
// DataAddr represents a location of data block.
type DataAddr struct {
Size, Offset int64
}
// Parse parses a given go object file
func Parse(f *os.File) (*File, error) {
parser := newParser(bufio.NewReader(f))
if err := parser.skipHeader(); err != nil {
return nil, err
}
if err := parser.checkVersion(); err != nil {
return nil, err
}
if err := parser.skipDependencies(); err != nil {
return nil, err
}
if err := parser.parseReferences(); err != nil {
return nil, err
}
if err := parser.parseData(); err != nil {
return nil, err
}
if err := parser.parseSymbols(); err != nil {
return nil, err
}
return &parser.File, parser.skipFooter()
}
type parser struct {
reader readerWithCounter
// As a list of symbols are parsed, a symbol is associated with some region of the data block.
// associatedDataSize is the total size of those regions.
associatedDataSize int64
File
}
func newParser(raw *bufio.Reader) *parser {
return &parser{reader: readerWithCounter{raw: raw}}
}
func (p *parser) skipHeader() error {
buff := make([]byte, len(magicHeader))
_ = p.reader.read(buff)
if p.reader.err != nil {
return p.reader.err
}
for !reflect.DeepEqual(buff, magicHeader) {
b := p.reader.readByte()
if p.reader.err != nil {
return errors.New("magic header not found")
}
buff = append(buff[1:], b)
}
return nil
}
func (p *parser) checkVersion() error {
version := p.reader.readByte()
if p.reader.err != nil {
return p.reader.err
}
if version != 1 {
return fmt.Errorf("unexpected version: %d", version)
}
return nil
}
func (p *parser) skipDependencies() error {
for {
b := p.reader.readByte()
if p.reader.err != nil {
return p.reader.err
}
if b == 0 {
return nil
}
}
}
func (p *parser) parseReferences() error {
// the 1st reference is always empty.
p.SymbolReferences = append(p.SymbolReferences, SymbolReference{})
for {
b := p.reader.readByte()
if p.reader.err != nil {
return p.reader.err
}
if b == 0xff {
return nil
} else if b != 0xfe {
return fmt.Errorf("sanity check failed: %#x ", b)
}
if err := p.parseReference(); err != nil {
return err
}
}
}
func (p *parser) parseReference() error {
symbolName := p.reader.readString()
if p.reader.err != nil {
return p.reader.err
}
symbolVersion := p.reader.readVarint()
if p.reader.err != nil {
return p.reader.err
}
p.SymbolReferences = append(p.SymbolReferences, SymbolReference{symbolName, symbolVersion})
return nil
}
func (p *parser) parseData() error {
dataLength := p.reader.readVarint()
if p.reader.err != nil {
return p.reader.err
}
_ = p.reader.readVarint() // reloc
_ = p.reader.readVarint() // pcdata
_ = p.reader.readVarint() // automatics
_ = p.reader.readVarint() // funcdata
_ = p.reader.readVarint() // files
p.DataBlockPosition = p.reader.numReadBytes
p.DataBlock = make([]byte, dataLength)
numRead := 0
for numRead != int(dataLength) {
n := p.reader.read(p.DataBlock[numRead:])
if p.reader.err != nil {
return p.reader.err
}
numRead += n
}
return nil
}
func (p *parser) parseSymbols() error {
for {
b := p.reader.readByte()
if p.reader.err != nil {
return p.reader.err
}
if b == 0xff {
return nil
} else if b != 0xfe {
return fmt.Errorf("sanity check failed: %#x ", b)
}
if err := p.parseSymbol(); err != nil {
return err
}
}
}
func (p *parser) parseSymbol() error {
symbol := Symbol{}
symbol.Kind = SymKind(p.reader.readByte())
symbol.IDIndex = p.reader.readVarint()
flags := p.reader.readVarint()
symbol.DupOK = flags&0x1 != 0
symbol.Local = (flags>>1)&0x1 != 0
symbol.Typelink = (flags>>2)&0x1 != 0
symbol.Size = p.reader.readVarint()
symbol.GoTypeIndex = p.reader.readVarint()
dataSize := p.reader.readVarint()
symbol.DataAddr = DataAddr{Size: dataSize, Offset: p.associatedDataSize}
p.associatedDataSize += dataSize
numRelocs := p.reader.readVarint()
for i := 0; i < int(numRelocs); i++ {
reloc := Relocation{}
reloc.Offset = p.reader.readVarint()
reloc.Size = p.reader.readVarint()
reloc.Type = RelocType(p.reader.readVarint())
reloc.Add = p.reader.readVarint()
reloc.IDIndex = p.reader.readVarint()
symbol.Relocations = append(symbol.Relocations, reloc)
}
if symbol.Kind == STEXT {
if err := p.skipSTEXTFields(); err != nil {
return err
}
}
p.Symbols = append(p.Symbols, symbol)
return p.reader.err
}
func (p *parser) skipSTEXTFields() error {
_ = p.reader.readVarint() // Args
_ = p.reader.readVarint() // Frame
_ = p.reader.readVarint() // Flags
_ = p.reader.readVarint() // NoSplit
numLocals := p.reader.readVarint()
for i := 0; i < int(numLocals); i++ {
_ = p.reader.readVarint() // sym
_ = p.reader.readVarint() // offset
_ = p.reader.readVarint() // type
_ = p.reader.readVarint() // go type
}
pcspSize := p.reader.readVarint()
p.associatedDataSize += pcspSize
pcFileSize := p.reader.readVarint()
p.associatedDataSize += pcFileSize
pcLineSize := p.reader.readVarint()
p.associatedDataSize += pcLineSize
pcInlineSize := p.reader.readVarint()
p.associatedDataSize += pcInlineSize
numPCData := p.reader.readVarint()
for i := 0; i < int(numPCData); i++ {
pcDataSize := p.reader.readVarint()
p.associatedDataSize += pcDataSize
}
numFuncData := p.reader.readVarint()
for i := 0; i < int(numFuncData); i++ {
_ = p.reader.readVarint() // func data index
}
for i := 0; i < int(numFuncData); i++ {
_ = p.reader.readVarint() // func offset
}
numFiles := p.reader.readVarint()
for i := 0; i < int(numFiles); i++ {
_ = p.reader.readVarint() // file index
}
numInlineTrees := p.reader.readVarint()
for i := 0; i < int(numInlineTrees); i++ {
_ = p.reader.readVarint() // parent
_ = p.reader.readVarint() // file
_ = p.reader.readVarint() // line
_ = p.reader.readVarint() // func
}
return p.reader.err
}
func (p *parser) skipFooter() error {
buff := make([]byte, len(magicFooter))
_ = p.reader.read(buff)
if p.reader.err != nil {
return p.reader.err
}
if !reflect.DeepEqual(buff, magicFooter) {
return fmt.Errorf("invalid footer: %#x", buff)
}
return nil
}
// readerWithCounter is bufio.Reader which records the number of read bytes.
// When an error happens, it updates an error field rather than returning it, so that
// the error handling can be delayed. No read operation will be taken if the error field is not nil.
type readerWithCounter struct {
raw *bufio.Reader
numReadBytes int64
err error
}
func (r *readerWithCounter) readVarint() int64 {
var value uint64
var shift uint64
for {
b := r.readByte()
if r.err != nil {
return 0
}
value += uint64(b&0x7f) << shift
if (b>>7)&0x1 == 0 {
break
}
shift += 7
}
return zigzagDecode(value)
}
func (r *readerWithCounter) readString() string {
len := r.readVarint()
if r.err != nil {
return ""
}
buff := make([]byte, len)
numRead := 0
for numRead != int(len) {
n := r.read(buff[numRead:])
if r.err != nil {
return ""
}
numRead += n
}
return string(buff)
}
func (r *readerWithCounter) readByte() (b byte) {
if r.err != nil {
return
}
b, r.err = r.raw.ReadByte()
r.numReadBytes++
return
}
func (r *readerWithCounter) read(p []byte) (n int) {
if r.err != nil {
return
}
n, r.err = r.raw.Read(p)
r.numReadBytes += int64(n)
return
}