github.com/undoio/delve@v1.9.0/pkg/dwarf/dwarfbuilder/builder.go (about)

     1  // Package dwarfbuilder provides a way to build DWARF sections with
     2  // arbitrary contents.
     3  package dwarfbuilder
     4  
     5  import (
     6  	"bytes"
     7  	"debug/dwarf"
     8  	"encoding/binary"
     9  	"fmt"
    10  )
    11  
    12  // Builder dwarf builder
    13  type Builder struct {
    14  	info     bytes.Buffer
    15  	loc      bytes.Buffer
    16  	abbrevs  []tagDescr
    17  	tagStack []*tagState
    18  }
    19  
    20  // New creates a new DWARF builder.
    21  func New() *Builder {
    22  	b := &Builder{}
    23  
    24  	b.info.Write([]byte{
    25  		0x0, 0x0, 0x0, 0x0, // length
    26  		0x4, 0x0, // version
    27  		0x0, 0x0, 0x0, 0x0, // debug_abbrev_offset
    28  		0x8, // address_size
    29  	})
    30  
    31  	b.TagOpen(dwarf.TagCompileUnit, "go")
    32  	b.Attr(dwarf.AttrLanguage, uint8(22))
    33  
    34  	return b
    35  }
    36  
    37  // Build closes b and returns all the dwarf sections.
    38  func (b *Builder) Build() (abbrev, aranges, frame, info, line, pubnames, ranges, str, loc []byte, err error) {
    39  	b.TagClose()
    40  
    41  	if len(b.tagStack) > 0 {
    42  		err = fmt.Errorf("unbalanced TagOpen/TagClose %d", len(b.tagStack))
    43  		return
    44  	}
    45  
    46  	abbrev = b.makeAbbrevTable()
    47  	info = b.info.Bytes()
    48  	binary.LittleEndian.PutUint32(info, uint32(len(info)-4))
    49  	loc = b.loc.Bytes()
    50  
    51  	return
    52  }