2023-06-14 11:23:46 +00:00
|
|
|
package proc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"debug/elf"
|
2023-09-25 18:41:34 +00:00
|
|
|
"debug/macho"
|
2024-06-20 19:50:18 +00:00
|
|
|
"errors"
|
2023-06-14 11:23:46 +00:00
|
|
|
"fmt"
|
2023-11-03 09:00:49 +00:00
|
|
|
|
|
|
|
"github.com/go-delve/delve/pkg/internal/gosym"
|
2023-06-14 11:23:46 +00:00
|
|
|
)
|
|
|
|
|
2023-11-03 09:00:49 +00:00
|
|
|
func readPcLnTableElf(exe *elf.File, path string) (*gosym.Table, uint64, error) {
|
2023-06-14 11:23:46 +00:00
|
|
|
// Default section label is .gopclntab
|
|
|
|
sectionLabel := ".gopclntab"
|
|
|
|
|
|
|
|
section := exe.Section(sectionLabel)
|
|
|
|
if section == nil {
|
|
|
|
// binary may be built with -pie
|
2023-06-16 07:38:19 +00:00
|
|
|
sectionLabel = ".data.rel.ro.gopclntab"
|
2023-06-14 11:23:46 +00:00
|
|
|
section = exe.Section(sectionLabel)
|
|
|
|
if section == nil {
|
2024-06-20 19:50:18 +00:00
|
|
|
return nil, 0, errors.New("could not read section .gopclntab")
|
2023-06-14 11:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tableData, err := section.Data()
|
|
|
|
if err != nil {
|
2024-06-20 19:50:18 +00:00
|
|
|
return nil, 0, errors.New("found section but could not read .gopclntab")
|
2023-06-14 11:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr := exe.Section(".text").Addr
|
|
|
|
lineTable := gosym.NewLineTable(tableData, addr)
|
|
|
|
symTable, err := gosym.NewTable([]byte{}, lineTable)
|
|
|
|
if err != nil {
|
2023-11-03 09:00:49 +00:00
|
|
|
return nil, 0, fmt.Errorf("could not create symbol table from %s ", path)
|
2023-06-14 11:23:46 +00:00
|
|
|
}
|
2023-11-03 09:00:49 +00:00
|
|
|
return symTable, section.Addr, nil
|
2023-06-14 11:23:46 +00:00
|
|
|
}
|
2023-09-25 18:41:34 +00:00
|
|
|
|
2024-02-21 11:10:41 +00:00
|
|
|
func readPcLnTableMacho(exe *macho.File, path string) (*gosym.Table, uint64, error) {
|
2023-09-25 18:41:34 +00:00
|
|
|
// Default section label is __gopclntab
|
|
|
|
sectionLabel := "__gopclntab"
|
|
|
|
|
|
|
|
section := exe.Section(sectionLabel)
|
|
|
|
if section == nil {
|
2024-06-20 19:50:18 +00:00
|
|
|
return nil, 0, errors.New("could not read section __gopclntab")
|
2023-09-25 18:41:34 +00:00
|
|
|
}
|
|
|
|
tableData, err := section.Data()
|
|
|
|
if err != nil {
|
2024-06-20 19:50:18 +00:00
|
|
|
return nil, 0, errors.New("found section but could not read __gopclntab")
|
2023-09-25 18:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr := exe.Section("__text").Addr
|
|
|
|
lineTable := gosym.NewLineTable(tableData, addr)
|
|
|
|
symTable, err := gosym.NewTable([]byte{}, lineTable)
|
|
|
|
if err != nil {
|
2024-02-21 11:10:41 +00:00
|
|
|
return nil, 0, fmt.Errorf("could not create symbol table from %s ", path)
|
2023-09-25 18:41:34 +00:00
|
|
|
}
|
2024-02-21 11:10:41 +00:00
|
|
|
return symTable, section.Addr, nil
|
2023-09-25 18:41:34 +00:00
|
|
|
}
|