2014-06-02 21:33:02 +00:00
|
|
|
// Package frame contains data structures and
|
|
|
|
// related functions for parsing and searching
|
|
|
|
// through Dwarf .debug_frame data.
|
|
|
|
package frame
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2014-06-25 19:14:29 +00:00
|
|
|
"io"
|
2014-06-02 21:33:02 +00:00
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
type parsefunc func(*parseContext) parsefunc
|
2014-06-06 21:05:26 +00:00
|
|
|
|
|
|
|
type parseContext struct {
|
|
|
|
Buf *bytes.Buffer
|
2014-06-25 19:14:29 +00:00
|
|
|
Entries FrameDescriptionEntries
|
2014-06-07 00:04:15 +00:00
|
|
|
Common *CommonInformationEntry
|
2014-06-25 19:14:29 +00:00
|
|
|
Frame *FrameDescriptionEntry
|
2014-06-06 21:05:26 +00:00
|
|
|
Length uint32
|
|
|
|
}
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-06 20:28:47 +00:00
|
|
|
// Parse takes in data (a byte slice) and returns a slice of
|
2014-06-02 21:33:02 +00:00
|
|
|
// CommonInformationEntry structures. Each CommonInformationEntry
|
2014-06-25 19:14:29 +00:00
|
|
|
// has a slice of FrameDescriptionEntry structures.
|
|
|
|
func Parse(data []byte) FrameDescriptionEntries {
|
2014-06-02 21:33:02 +00:00
|
|
|
var (
|
2014-06-25 19:14:29 +00:00
|
|
|
buf = bytes.NewBuffer(data)
|
|
|
|
pctx = &parseContext{Buf: buf}
|
2014-06-02 21:33:02 +00:00
|
|
|
)
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
for fn := parseLength; buf.Len() != 0; {
|
2014-06-25 19:14:29 +00:00
|
|
|
fn = fn(pctx)
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
return pctx.Entries
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:25:07 +00:00
|
|
|
// decodeULEB128 decodes an unsigned Little Endian Base 128
|
2014-06-02 21:33:02 +00:00
|
|
|
// represented number.
|
2014-06-07 00:24:32 +00:00
|
|
|
func decodeULEB128(buf *bytes.Buffer) (uint64, uint32) {
|
2014-06-02 21:33:02 +00:00
|
|
|
var (
|
|
|
|
result uint64
|
|
|
|
shift uint64
|
|
|
|
length uint32
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
2014-06-07 00:24:32 +00:00
|
|
|
b, err := buf.ReadByte()
|
2014-06-02 21:33:02 +00:00
|
|
|
if err != nil {
|
2014-06-09 22:19:44 +00:00
|
|
|
panic("Could not parse ULEB128 value")
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
length++
|
|
|
|
|
|
|
|
result |= uint64((uint(b) & 0x7f) << shift)
|
|
|
|
|
|
|
|
// If high order bit is 1.
|
|
|
|
if b&0x80 == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
shift += 7
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, length
|
|
|
|
}
|
|
|
|
|
2014-06-09 22:19:44 +00:00
|
|
|
// decodeSLEB128 decodes an signed Little Endian Base 128
|
|
|
|
// represented number.
|
|
|
|
func decodeSLEB128(buf *bytes.Buffer) (int64, uint32) {
|
|
|
|
var (
|
|
|
|
b byte
|
|
|
|
err error
|
|
|
|
result int64
|
|
|
|
shift uint64
|
|
|
|
length uint32
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
b, err = buf.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
panic("Could not parse SLEB128 value")
|
|
|
|
}
|
|
|
|
length++
|
|
|
|
|
|
|
|
result |= int64((int64(b) & 0x7f) << shift)
|
|
|
|
shift += 7
|
|
|
|
if b&0x80 == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shift < 8*uint64(length)) && (b&0x40 > 0) {
|
|
|
|
result |= -(1 << shift)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, length
|
|
|
|
}
|
|
|
|
|
2014-06-02 21:33:02 +00:00
|
|
|
func cieEntry(data []byte) bool {
|
|
|
|
return bytes.Equal(data, []byte{0xff, 0xff, 0xff, 0xff})
|
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
func parseLength(ctx *parseContext) parsefunc {
|
|
|
|
var fn parsefunc
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
binary.Read(ctx.Buf, binary.LittleEndian, &ctx.Length)
|
2014-06-25 19:14:29 +00:00
|
|
|
cieid := ctx.Buf.Next(4)
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
if cieEntry(cieid) {
|
2014-06-07 00:04:15 +00:00
|
|
|
ctx.Common = &CommonInformationEntry{Length: ctx.Length}
|
2014-06-25 19:14:29 +00:00
|
|
|
fn = parseVersion
|
|
|
|
} else {
|
|
|
|
ctx.Frame = &FrameDescriptionEntry{Length: ctx.Length, CIE: ctx.Common, AddressRange: &addrange{}}
|
|
|
|
ctx.Entries = append(ctx.Entries, ctx.Frame)
|
|
|
|
fn = parseInitialLocation
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
// Take off the length of the CIE id / CIE pointer.
|
|
|
|
ctx.Length -= 4
|
2014-06-06 21:05:26 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
return fn
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
func parseInitialLocation(ctx *parseContext) parsefunc {
|
|
|
|
binary.Read(ctx.Buf, binary.LittleEndian, &ctx.Frame.AddressRange.begin)
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
ctx.Length -= 8
|
|
|
|
|
|
|
|
return parseAddressRange
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
func parseAddressRange(ctx *parseContext) parsefunc {
|
|
|
|
binary.Read(ctx.Buf, binary.LittleEndian, &ctx.Frame.AddressRange.end)
|
|
|
|
|
|
|
|
ctx.Length -= 8
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
return parseFrameInstructions
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
func parseFrameInstructions(ctx *parseContext) parsefunc {
|
2014-06-07 00:04:15 +00:00
|
|
|
// The rest of this entry consists of the instructions
|
|
|
|
// so we can just grab all of the data from the buffer
|
|
|
|
// cursor to length.
|
|
|
|
var buf = make([]byte, ctx.Length)
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
io.ReadFull(ctx.Buf, buf)
|
2014-06-07 00:04:15 +00:00
|
|
|
ctx.Frame.Instructions = buf
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length = 0
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
return parseLength
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
func parseVersion(ctx *parseContext) parsefunc {
|
2014-06-07 00:04:15 +00:00
|
|
|
binary.Read(ctx.Buf, binary.LittleEndian, &ctx.Common.Version)
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length -= 1
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
return parseAugmentation
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
func parseAugmentation(ctx *parseContext) parsefunc {
|
2014-06-07 00:04:15 +00:00
|
|
|
var str, c = parseString(ctx.Buf)
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-07 00:04:15 +00:00
|
|
|
ctx.Common.Augmentation = str
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length -= c
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
return parseCodeAlignmentFactor
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
func parseCodeAlignmentFactor(ctx *parseContext) parsefunc {
|
2014-06-07 00:23:09 +00:00
|
|
|
var caf, c = decodeULEB128(ctx.Buf)
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-07 00:04:15 +00:00
|
|
|
ctx.Common.CodeAlignmentFactor = caf
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length -= c
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
return parseDataAlignmentFactor
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
func parseDataAlignmentFactor(ctx *parseContext) parsefunc {
|
2014-06-09 22:24:48 +00:00
|
|
|
var daf, c = decodeSLEB128(ctx.Buf)
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-07 00:04:15 +00:00
|
|
|
ctx.Common.DataAlignmentFactor = daf
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length -= c
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
return parseReturnAddressRegister
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
func parseReturnAddressRegister(ctx *parseContext) parsefunc {
|
|
|
|
reg, c := decodeULEB128(ctx.Buf)
|
|
|
|
ctx.Common.ReturnAddressRegister = uint8(reg)
|
|
|
|
ctx.Length -= c
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
return parseInitialInstructions
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
func parseInitialInstructions(ctx *parseContext) parsefunc {
|
2014-06-07 00:04:15 +00:00
|
|
|
// The rest of this entry consists of the instructions
|
|
|
|
// so we can just grab all of the data from the buffer
|
|
|
|
// cursor to length.
|
|
|
|
var buf = make([]byte, ctx.Length)
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
binary.Read(ctx.Buf, binary.LittleEndian, &buf)
|
2014-06-07 00:04:15 +00:00
|
|
|
ctx.Common.InitialInstructions = buf
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length = 0
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-25 19:14:29 +00:00
|
|
|
return parseLength
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseString(data *bytes.Buffer) (string, uint32) {
|
|
|
|
var (
|
|
|
|
size uint32
|
|
|
|
str []rune
|
|
|
|
strb []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
b, err := data.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
panic("parseString(): Could not read byte")
|
|
|
|
}
|
|
|
|
size++
|
|
|
|
|
|
|
|
if b == 0x0 {
|
|
|
|
if size == 1 {
|
|
|
|
return "", size
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
strb = append(strb, b)
|
|
|
|
|
|
|
|
if utf8.FullRune(strb) {
|
|
|
|
r, _ := utf8.DecodeRune(strb)
|
|
|
|
str = append(str, r)
|
|
|
|
size++
|
|
|
|
strb = strb[0:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(str), size
|
|
|
|
}
|