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"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
type parsefunc func(*parseContext) (parsefunc, *parseContext)
|
|
|
|
|
|
|
|
type parseContext struct {
|
|
|
|
Buf *bytes.Buffer
|
|
|
|
Entries CommonEntries
|
2014-06-07 00:04:15 +00:00
|
|
|
Common *CommonInformationEntry
|
|
|
|
Frame *FrameDescriptorEntry
|
2014-06-06 21:05:26 +00:00
|
|
|
Length uint32
|
|
|
|
}
|
2014-06-02 21:33:02 +00:00
|
|
|
|
|
|
|
type CommonEntries []*CommonInformationEntry
|
|
|
|
|
|
|
|
// Represents a Common Information Entry in
|
|
|
|
// the Dwarf .debug_frame section.
|
|
|
|
type CommonInformationEntry struct {
|
|
|
|
Length uint32
|
|
|
|
CIE_id uint32
|
|
|
|
Version uint8
|
|
|
|
Augmentation string
|
|
|
|
CodeAlignmentFactor uint64
|
|
|
|
DataAlignmentFactor uint64
|
|
|
|
ReturnAddressRegister byte
|
|
|
|
InitialInstructions []byte
|
|
|
|
FrameDescriptorEntries []*FrameDescriptorEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Represents a Frame Descriptor Entry in the
|
|
|
|
// Dwarf .debug_frame section.
|
|
|
|
type FrameDescriptorEntry struct {
|
|
|
|
Length uint32
|
|
|
|
CIE_pointer *CommonInformationEntry
|
|
|
|
InitialLocation uint64
|
|
|
|
AddressRange uint64
|
|
|
|
Instructions []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
DW_CFA_advance_loc = (0x1 << 6) // High 2 bits: 0x1, low 6: delta
|
|
|
|
DW_CFA_offset = (0x2 << 6) // High 2 bits: 0x2, low 6: register
|
|
|
|
DW_CFA_restore = (0x3 << 6) // High 2 bits: 0x3, low 6: register
|
|
|
|
DW_CFA_nop = 0x0 // No ops
|
|
|
|
DW_CFA_set_loc = 0x1 // op1: address
|
|
|
|
DW_CFA_advance_loc1 = iota // op1: 1-bytes delta
|
|
|
|
DW_CFA_advance_loc2 // op1: 2-byte delta
|
|
|
|
DW_CFA_advance_loc4 // op1: 4-byte delta
|
|
|
|
DW_CFA_offset_extended // op1: ULEB128 register, op2: ULEB128 offset
|
|
|
|
DW_CFA_restore_extended // op1: ULEB128 register
|
|
|
|
DW_CFA_undefined // op1: ULEB128 register
|
|
|
|
DW_CFA_same_value // op1: ULEB128 register
|
|
|
|
DW_CFA_register // op1: ULEB128 register, op2: ULEB128 register
|
|
|
|
DW_CFA_remember_state // No ops
|
|
|
|
DW_CFA_restore_state // No ops
|
|
|
|
DW_CFA_def_cfa // op1: ULEB128 register, op2: ULEB128 offset
|
|
|
|
DW_CFA_def_cfa_register // op1: ULEB128 register
|
|
|
|
DW_CFA_def_cfa_offset // op1: ULEB128 offset
|
|
|
|
DW_CFA_def_cfa_expression // op1: BLOCK
|
|
|
|
DW_CFA_expression // op1: ULEB128 register, op2: BLOCK
|
|
|
|
DW_CFA_offset_extended_sf // op1: ULEB128 register, op2: SLEB128 offset
|
|
|
|
DW_CFA_def_cfa_sf // op1: ULEB128 register, op2: SLEB128 offset
|
|
|
|
DW_CFA_def_cfa_offset_sf // op1: SLEB128 offset
|
|
|
|
DW_CFA_val_offset // op1: ULEB128, op2: ULEB128
|
|
|
|
DW_CFA_val_offset_sf // op1: ULEB128, op2: SLEB128
|
|
|
|
DW_CFA_val_expression // op1: ULEB128, op2: BLOCK
|
|
|
|
DW_CFA_lo_user = 0x1c // op1: BLOCK
|
|
|
|
DW_CFA_hi_user = 0x3f // op1: ULEB128 register, op2: BLOCK
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
// has a slice of FrameDescriptorEntry structures.
|
|
|
|
func Parse(data []byte) CommonEntries {
|
|
|
|
var (
|
|
|
|
length uint32
|
|
|
|
entries CommonEntries
|
2014-06-06 21:05:26 +00:00
|
|
|
buf = bytes.NewBuffer(data)
|
|
|
|
pctx = &parseContext{Buf: buf, Entries: entries, Length: length}
|
2014-06-02 21:33:02 +00:00
|
|
|
)
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
for fn := parseLength; buf.Len() != 0; {
|
|
|
|
fn, pctx = 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 {
|
|
|
|
panic("Could not parse LEB128 value")
|
|
|
|
}
|
|
|
|
length++
|
|
|
|
|
|
|
|
result |= uint64((uint(b) & 0x7f) << shift)
|
|
|
|
|
|
|
|
// If high order bit is 1.
|
|
|
|
if b&0x80 == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
shift += 7
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, length
|
|
|
|
}
|
|
|
|
|
|
|
|
func cieEntry(data []byte) bool {
|
|
|
|
return bytes.Equal(data, []byte{0xff, 0xff, 0xff, 0xff})
|
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseLength(ctx *parseContext) (parsefunc, *parseContext) {
|
|
|
|
binary.Read(ctx.Buf, binary.LittleEndian, &ctx.Length)
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
if cieEntry(ctx.Buf.Bytes()[0:4]) {
|
2014-06-07 00:04:15 +00:00
|
|
|
ctx.Common = &CommonInformationEntry{Length: ctx.Length}
|
|
|
|
ctx.Entries = append(ctx.Entries, ctx.Common)
|
2014-06-06 21:05:26 +00:00
|
|
|
return parseCIEID, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:04:15 +00:00
|
|
|
ctx.Frame = &FrameDescriptorEntry{Length: ctx.Length, CIE_pointer: ctx.Common}
|
|
|
|
ctx.Common.FrameDescriptorEntries = append(ctx.Common.FrameDescriptorEntries, ctx.Frame)
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
// We aren't reading the CIE pointer from this section so just move the cursor past it.
|
|
|
|
ctx.Buf.Next(4)
|
|
|
|
|
|
|
|
return parseInitialLocation, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseInitialLocation(ctx *parseContext) (parsefunc, *parseContext) {
|
2014-06-07 00:04:15 +00:00
|
|
|
binary.Read(ctx.Buf, binary.LittleEndian, &ctx.Frame.InitialLocation)
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length -= 4
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
return parseAddressRange, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseAddressRange(ctx *parseContext) (parsefunc, *parseContext) {
|
2014-06-07 00:04:15 +00:00
|
|
|
binary.Read(ctx.Buf, binary.LittleEndian, &ctx.Frame.AddressRange)
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length -= 4
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
return parseFrameInstructions, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseFrameInstructions(ctx *parseContext) (parsefunc, *parseContext) {
|
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.Frame.Instructions = buf
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length = 0
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
return parseLength, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseCIEID(ctx *parseContext) (parsefunc, *parseContext) {
|
2014-06-07 00:04:15 +00:00
|
|
|
binary.Read(ctx.Buf, binary.LittleEndian, &ctx.Common.CIE_id)
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length -= 4
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
return parseVersion, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseVersion(ctx *parseContext) (parsefunc, *parseContext) {
|
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-06 21:05:26 +00:00
|
|
|
return parseAugmentation, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseAugmentation(ctx *parseContext) (parsefunc, *parseContext) {
|
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
|
|
|
|
|
|
|
|
return parseCodeAlignmentFactor, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseCodeAlignmentFactor(ctx *parseContext) (parsefunc, *parseContext) {
|
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-06 21:05:26 +00:00
|
|
|
return parseDataAlignmentFactor, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseDataAlignmentFactor(ctx *parseContext) (parsefunc, *parseContext) {
|
2014-06-07 00:23:09 +00:00
|
|
|
var daf, c = decodeULEB128(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-06 21:05:26 +00:00
|
|
|
return parseReturnAddressRegister, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseReturnAddressRegister(ctx *parseContext) (parsefunc, *parseContext) {
|
2014-06-07 00:04:15 +00:00
|
|
|
binary.Read(ctx.Buf, binary.LittleEndian, &ctx.Common.ReturnAddressRegister)
|
2014-06-06 21:05:26 +00:00
|
|
|
ctx.Length -= 1
|
2014-06-02 21:33:02 +00:00
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
return parseInitialInstructions, ctx
|
2014-06-02 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 21:05:26 +00:00
|
|
|
func parseInitialInstructions(ctx *parseContext) (parsefunc, *parseContext) {
|
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-06 21:05:26 +00:00
|
|
|
return parseLength, ctx
|
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
|
|
|
|
}
|