github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/dwarf/godwarf/addr.go (about)

     1  package godwarf
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"errors"
     7  
     8  	"github.com/go-delve/delve/pkg/dwarf"
     9  )
    10  
    11  // DebugAddrSection represents the debug_addr section of DWARFv5.
    12  // See DWARFv5 section 7.27 page 241 and following.
    13  type DebugAddrSection struct {
    14  	byteOrder binary.ByteOrder
    15  	ptrSz     int
    16  	data      []byte
    17  }
    18  
    19  // ParseAddr parses the header of a debug_addr section.
    20  func ParseAddr(data []byte) *DebugAddrSection {
    21  	if len(data) == 0 {
    22  		return nil
    23  	}
    24  	r := &DebugAddrSection{data: data}
    25  	_, dwarf64, _, byteOrder := dwarf.ReadDwarfLengthVersion(data)
    26  	r.byteOrder = byteOrder
    27  	data = data[6:]
    28  	if dwarf64 {
    29  		data = data[8:]
    30  	}
    31  
    32  	addrSz := data[0]
    33  	segSelSz := data[1]
    34  	r.ptrSz = int(addrSz + segSelSz)
    35  
    36  	return r
    37  }
    38  
    39  // GetSubsection returns the subsection of debug_addr starting at addrBase
    40  func (addr *DebugAddrSection) GetSubsection(addrBase uint64) *DebugAddr {
    41  	if addr == nil {
    42  		return nil
    43  	}
    44  	return &DebugAddr{DebugAddrSection: addr, addrBase: addrBase}
    45  }
    46  
    47  // DebugAddr represents a subsection of the debug_addr section with a specific base address
    48  type DebugAddr struct {
    49  	*DebugAddrSection
    50  	addrBase uint64
    51  }
    52  
    53  // Get returns the address at index idx starting from addrBase.
    54  func (addr *DebugAddr) Get(idx uint64) (uint64, error) {
    55  	if addr == nil || addr.DebugAddrSection == nil {
    56  		return 0, errors.New("debug_addr section not present")
    57  	}
    58  	off := idx*uint64(addr.ptrSz) + addr.addrBase
    59  	return dwarf.ReadUintRaw(bytes.NewReader(addr.data[off:]), addr.byteOrder, addr.ptrSz)
    60  }