gitlab.com/Raven-IO/raven-delve@v1.22.4/pkg/proc/pclntab.go (about)

     1  package proc
     2  
     3  import (
     4  	"debug/elf"
     5  	"debug/macho"
     6  	"fmt"
     7  
     8  	"gitlab.com/Raven-IO/raven-delve/pkg/internal/gosym"
     9  )
    10  
    11  func readPcLnTableElf(exe *elf.File, path string) (*gosym.Table, uint64, error) {
    12  	// Default section label is .gopclntab
    13  	sectionLabel := ".gopclntab"
    14  
    15  	section := exe.Section(sectionLabel)
    16  	if section == nil {
    17  		// binary may be built with -pie
    18  		sectionLabel = ".data.rel.ro.gopclntab"
    19  		section = exe.Section(sectionLabel)
    20  		if section == nil {
    21  			return nil, 0, fmt.Errorf("could not read section .gopclntab")
    22  		}
    23  	}
    24  	tableData, err := section.Data()
    25  	if err != nil {
    26  		return nil, 0, fmt.Errorf("found section but could not read .gopclntab")
    27  	}
    28  
    29  	addr := exe.Section(".text").Addr
    30  	lineTable := gosym.NewLineTable(tableData, addr)
    31  	symTable, err := gosym.NewTable([]byte{}, lineTable)
    32  	if err != nil {
    33  		return nil, 0, fmt.Errorf("could not create symbol table from  %s ", path)
    34  	}
    35  	return symTable, section.Addr, nil
    36  }
    37  
    38  func readPcLnTableMacho(exe *macho.File, path string) (*gosym.Table, uint64, error) {
    39  	// Default section label is __gopclntab
    40  	sectionLabel := "__gopclntab"
    41  
    42  	section := exe.Section(sectionLabel)
    43  	if section == nil {
    44  		return nil, 0, fmt.Errorf("could not read section __gopclntab")
    45  	}
    46  	tableData, err := section.Data()
    47  	if err != nil {
    48  		return nil, 0, fmt.Errorf("found section but could not read __gopclntab")
    49  	}
    50  
    51  	addr := exe.Section("__text").Addr
    52  	lineTable := gosym.NewLineTable(tableData, addr)
    53  	symTable, err := gosym.NewTable([]byte{}, lineTable)
    54  	if err != nil {
    55  		return nil, 0, fmt.Errorf("could not create symbol table from  %s ", path)
    56  	}
    57  	return symTable, section.Addr, nil
    58  }