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-29 16:52:21 +00:00
|
|
|
|
|
|
|
"github.com/derekparker/dbg/dwarf/util"
|
2014-06-02 21:33:02 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-29 16:52:21 +00:00
|
|
|
var str, c = util.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-29 16:52:21 +00:00
|
|
|
var caf, c = util.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-29 16:52:21 +00:00
|
|
|
var daf, c = util.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 {
|
2014-06-29 16:52:21 +00:00
|
|
|
reg, c := util.DecodeULEB128(ctx.Buf)
|
2014-06-25 19:14:29 +00:00
|
|
|
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
|
|
|
}
|