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
|
package dwarfbuilder
import (
"bytes"
"debug/dwarf"
"encoding/binary"
"github.com/go-delve/delve/pkg/dwarf/godwarf"
"github.com/go-delve/delve/pkg/dwarf/leb128"
)
// Form represents a DWARF form kind (see Figure 20, page 160 and following,
// DWARF v4)
type Form uint16
const (
DW_FORM_addr Form = 0x01 // address
DW_FORM_block2 Form = 0x03 // block
DW_FORM_block4 Form = 0x04 // block
DW_FORM_data2 Form = 0x05 // constant
DW_FORM_data4 Form = 0x06 // constant
DW_FORM_data8 Form = 0x07 // constant
DW_FORM_string Form = 0x08 // string
DW_FORM_block Form = 0x09 // block
DW_FORM_block1 Form = 0x0a // block
DW_FORM_data1 Form = 0x0b // constant
DW_FORM_flag Form = 0x0c // flag
DW_FORM_sdata Form = 0x0d // constant
DW_FORM_strp Form = 0x0e // string
DW_FORM_udata Form = 0x0f // constant
DW_FORM_ref_addr Form = 0x10 // reference
DW_FORM_ref1 Form = 0x11 // reference
DW_FORM_ref2 Form = 0x12 // reference
DW_FORM_ref4 Form = 0x13 // reference
DW_FORM_ref8 Form = 0x14 // reference
DW_FORM_ref_udata Form = 0x15 // reference
DW_FORM_indirect Form = 0x16 // (see Section 7.5.3)
DW_FORM_sec_offset Form = 0x17 // lineptr, loclistptr, macptr, rangelistptr
DW_FORM_exprloc Form = 0x18 // exprloc
DW_FORM_flag_present Form = 0x19 // flag
DW_FORM_ref_sig8 Form = 0x20 // reference
)
// Encoding represents a DWARF base type encoding (see section 7.8, page 168
// and following, DWARF v4).
type Encoding uint16
const (
DW_ATE_address Encoding = 0x01
DW_ATE_boolean Encoding = 0x02
DW_ATE_complex_float Encoding = 0x03
DW_ATE_float Encoding = 0x04
DW_ATE_signed Encoding = 0x05
DW_ATE_signed_char Encoding = 0x06
DW_ATE_unsigned Encoding = 0x07
DW_ATE_unsigned_char Encoding = 0x08
DW_ATE_imaginary_float Encoding = 0x09
DW_ATE_packed_decimal Encoding = 0x0a
DW_ATE_numeric_string Encoding = 0x0b
DW_ATE_edited Encoding = 0x0c
DW_ATE_signed_fixed Encoding = 0x0d
DW_ATE_unsigned_fixed Encoding = 0x0e
DW_ATE_decimal_float Encoding = 0x0f
DW_ATE_UTF Encoding = 0x10
DW_ATE_lo_user Encoding = 0x80
DW_ATE_hi_user Encoding = 0xff
)
// Address represents a machine address.
type Address uint64
type tagDescr struct {
tag dwarf.Tag
attr []dwarf.Attr
form []Form
children bool
}
type tagState struct {
off dwarf.Offset
tagDescr
}
// TagOpen starts a new DIE, call TagClose after adding all attributes and
// children elements.
func (b *Builder) TagOpen(tag dwarf.Tag, name string) dwarf.Offset {
if len(b.tagStack) > 0 {
b.tagStack[len(b.tagStack)-1].children = true
}
ts := &tagState{off: dwarf.Offset(b.info.Len())}
ts.tag = tag
b.info.WriteByte(0)
b.tagStack = append(b.tagStack, ts)
if name != "" {
b.Attr(dwarf.AttrName, name)
}
return ts.off
}
// SetHasChildren sets the current DIE as having children (even if none are added).
func (b *Builder) SetHasChildren() {
if len(b.tagStack) == 0 {
panic("NoChildren with no open tags")
}
b.tagStack[len(b.tagStack)-1].children = true
}
// TagClose closes the current DIE.
func (b *Builder) TagClose() {
if len(b.tagStack) == 0 {
panic("TagClose with no open tags")
}
tag := b.tagStack[len(b.tagStack)-1]
abbrev := b.abbrevFor(tag.tagDescr)
b.info.Bytes()[tag.off] = abbrev
if tag.children {
b.info.WriteByte(0)
}
b.tagStack = b.tagStack[:len(b.tagStack)-1]
}
// Attr adds an attribute to the current DIE.
func (b *Builder) Attr(attr dwarf.Attr, val interface{}) dwarf.Offset {
if len(b.tagStack) == 0 {
panic("Attr with no open tags")
}
off := dwarf.Offset(b.info.Len())
tag := b.tagStack[len(b.tagStack)-1]
if tag.children {
panic("Can't add attributes after adding children")
}
tag.attr = append(tag.attr, attr)
switch x := val.(type) {
case string:
tag.form = append(tag.form, DW_FORM_string)
b.info.WriteString(x)
b.info.WriteByte(0)
case uint8:
tag.form = append(tag.form, DW_FORM_data1)
binary.Write(&b.info, binary.LittleEndian, x)
case uint16:
tag.form = append(tag.form, DW_FORM_data2)
binary.Write(&b.info, binary.LittleEndian, x)
case uint64:
tag.form = append(tag.form, DW_FORM_data8)
binary.Write(&b.info, binary.LittleEndian, x)
case Address:
tag.form = append(tag.form, DW_FORM_addr)
binary.Write(&b.info, binary.LittleEndian, x)
case dwarf.Offset:
tag.form = append(tag.form, DW_FORM_ref_addr)
binary.Write(&b.info, binary.LittleEndian, x)
case []byte:
tag.form = append(tag.form, DW_FORM_block4)
binary.Write(&b.info, binary.LittleEndian, uint32(len(x)))
b.info.Write(x)
case []LocEntry:
tag.form = append(tag.form, DW_FORM_sec_offset)
binary.Write(&b.info, binary.LittleEndian, uint32(b.loc.Len()))
// base address
binary.Write(&b.loc, binary.LittleEndian, ^uint64(0))
binary.Write(&b.loc, binary.LittleEndian, uint64(0))
for _, locentry := range x {
binary.Write(&b.loc, binary.LittleEndian, locentry.Lowpc)
binary.Write(&b.loc, binary.LittleEndian, locentry.Highpc)
binary.Write(&b.loc, binary.LittleEndian, uint16(len(locentry.Loc)))
b.loc.Write(locentry.Loc)
}
// end of loclist
binary.Write(&b.loc, binary.LittleEndian, uint64(0))
binary.Write(&b.loc, binary.LittleEndian, uint64(0))
default:
panic("unknown value type")
}
return off
}
// PatchOffset writes the offset 'patch' at offset patchedOffset.
func (b *Builder) PatchOffset(patchedOffset, patch dwarf.Offset) {
infoBytes := b.info.Bytes()
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, patch)
copy(infoBytes[patchedOffset:], buf.Bytes())
}
func sameTagDescr(a, b tagDescr) bool {
if a.tag != b.tag {
return false
}
if len(a.attr) != len(b.attr) {
return false
}
if a.children != b.children {
return false
}
for i := range a.attr {
if a.attr[i] != b.attr[i] {
return false
}
if a.form[i] != b.form[i] {
return false
}
}
return true
}
// abbrevFor returns an abbrev for the given entry description. If no abbrev
// for tag already exist a new one is created.
func (b *Builder) abbrevFor(tag tagDescr) byte {
for abbrev, descr := range b.abbrevs {
if sameTagDescr(descr, tag) {
return byte(abbrev + 1)
}
}
b.abbrevs = append(b.abbrevs, tag)
return byte(len(b.abbrevs))
}
func (b *Builder) makeAbbrevTable() []byte {
var abbrev bytes.Buffer
for i := range b.abbrevs {
leb128.EncodeUnsigned(&abbrev, uint64(i+1))
leb128.EncodeUnsigned(&abbrev, uint64(b.abbrevs[i].tag))
if b.abbrevs[i].children {
abbrev.WriteByte(0x01)
} else {
abbrev.WriteByte(0x00)
}
for j := range b.abbrevs[i].attr {
leb128.EncodeUnsigned(&abbrev, uint64(b.abbrevs[i].attr[j]))
leb128.EncodeUnsigned(&abbrev, uint64(b.abbrevs[i].form[j]))
}
leb128.EncodeUnsigned(&abbrev, 0)
leb128.EncodeUnsigned(&abbrev, 0)
}
leb128.EncodeUnsigned(&abbrev, uint64(0))
return abbrev.Bytes()
}
func (b *Builder) AddCompileUnit(name string, lowPC uint64) dwarf.Offset {
r := b.TagOpen(dwarf.TagCompileUnit, name)
b.Attr(dwarf.AttrLowpc, lowPC)
return r
}
// AddSubprogram adds a subprogram declaration to debug_info, must call
// TagClose after adding all local variables and parameters.
// Will write an abbrev corresponding to a DW_TAG_subprogram, followed by a
// DW_AT_lowpc and a DW_AT_highpc.
func (b *Builder) AddSubprogram(fnname string, lowpc, highpc uint64) dwarf.Offset {
r := b.TagOpen(dwarf.TagSubprogram, fnname)
b.Attr(dwarf.AttrLowpc, Address(lowpc))
b.Attr(dwarf.AttrHighpc, Address(highpc))
return r
}
// AddVariable adds a new variable entry to debug_info.
// Will write a DW_TAG_variable, followed by a DW_AT_type and a
// DW_AT_location.
func (b *Builder) AddVariable(varname string, typ dwarf.Offset, loc interface{}) dwarf.Offset {
r := b.TagOpen(dwarf.TagVariable, varname)
b.Attr(dwarf.AttrType, typ)
b.Attr(dwarf.AttrLocation, loc)
b.TagClose()
return r
}
// AddBaseType adds a new base type entry to debug_info.
// Will write a DW_TAG_base_type, followed by a DW_AT_encoding and a
// DW_AT_byte_size.
func (b *Builder) AddBaseType(typename string, encoding Encoding, byteSz uint16) dwarf.Offset {
r := b.TagOpen(dwarf.TagBaseType, typename)
b.Attr(dwarf.AttrEncoding, uint16(encoding))
b.Attr(dwarf.AttrByteSize, byteSz)
b.TagClose()
return r
}
// AddStructType adds a new structure type to debug_info. Call TagClose to
// finish adding fields.
// Will write a DW_TAG_struct_type, followed by a DW_AT_byte_size.
func (b *Builder) AddStructType(typename string, byteSz uint16) dwarf.Offset {
r := b.TagOpen(dwarf.TagStructType, typename)
b.Attr(dwarf.AttrByteSize, byteSz)
return r
}
// AddMember adds a new member entry to debug_info.
// Writes a DW_TAG_member followed by DW_AT_type and DW_AT_data_member_loc.
func (b *Builder) AddMember(fieldname string, typ dwarf.Offset, memberLoc []byte) dwarf.Offset {
r := b.TagOpen(dwarf.TagMember, fieldname)
b.Attr(dwarf.AttrType, typ)
b.Attr(dwarf.AttrDataMemberLoc, memberLoc)
b.TagClose()
return r
}
// AddPointerType adds a new pointer type to debug_info.
func (b *Builder) AddPointerType(typename string, typ dwarf.Offset) dwarf.Offset {
r := b.TagOpen(dwarf.TagPointerType, typename)
b.Attr(dwarf.AttrType, typ)
b.Attr(godwarf.AttrGoKind, uint8(22))
b.TagClose()
return r
}
|