2017-04-06 18:14:01 +00:00
|
|
|
package proc
|
|
|
|
|
|
|
|
import (
|
2017-05-29 13:20:01 +00:00
|
|
|
"debug/dwarf"
|
|
|
|
"debug/elf"
|
2017-04-06 18:14:01 +00:00
|
|
|
"debug/gosym"
|
2017-05-29 13:20:01 +00:00
|
|
|
"debug/macho"
|
2017-04-06 18:14:01 +00:00
|
|
|
"debug/pe"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/derekparker/delve/pkg/dwarf/frame"
|
2017-05-29 13:20:01 +00:00
|
|
|
"github.com/derekparker/delve/pkg/dwarf/godwarf"
|
2017-04-06 18:14:01 +00:00
|
|
|
"github.com/derekparker/delve/pkg/dwarf/line"
|
|
|
|
"github.com/derekparker/delve/pkg/dwarf/reader"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BinaryInfo struct {
|
|
|
|
lastModified time.Time // Time the executable of this process was last modified
|
|
|
|
|
2017-04-21 06:55:53 +00:00
|
|
|
GOOS string
|
2017-04-06 18:14:01 +00:00
|
|
|
closer io.Closer
|
|
|
|
|
|
|
|
// Maps package names to package paths, needed to lookup types inside DWARF info
|
|
|
|
packageMap map[string]string
|
|
|
|
|
2017-06-21 22:40:42 +00:00
|
|
|
Arch Arch
|
|
|
|
dwarf *dwarf.Data
|
|
|
|
frameEntries frame.FrameDescriptionEntries
|
|
|
|
lineInfo line.DebugLines
|
|
|
|
goSymTable *gosym.Table
|
|
|
|
types map[string]dwarf.Offset
|
2017-07-18 18:55:24 +00:00
|
|
|
packageVars map[string]dwarf.Offset
|
2017-06-21 22:40:42 +00:00
|
|
|
functions []functionDebugInfo
|
|
|
|
gStructOffset uint64
|
2017-04-06 18:14:01 +00:00
|
|
|
|
2017-05-29 13:20:01 +00:00
|
|
|
typeCache map[dwarf.Offset]godwarf.Type
|
|
|
|
|
2017-04-06 18:14:01 +00:00
|
|
|
loadModuleDataOnce sync.Once
|
|
|
|
moduleData []moduleData
|
|
|
|
nameOfRuntimeType map[uintptr]nameOfRuntimeTypeEntry
|
2017-08-15 06:21:24 +00:00
|
|
|
|
|
|
|
loadErrMu sync.Mutex
|
|
|
|
loadErr error
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var UnsupportedLinuxArchErr = errors.New("unsupported architecture - only linux/amd64 is supported")
|
|
|
|
var UnsupportedWindowsArchErr = errors.New("unsupported architecture of windows/386 - only windows/amd64 is supported")
|
|
|
|
var UnsupportedDarwinArchErr = errors.New("unsupported architecture - only darwin/amd64 is supported")
|
|
|
|
|
|
|
|
func NewBinaryInfo(goos, goarch string) BinaryInfo {
|
2017-05-29 13:20:01 +00:00
|
|
|
r := BinaryInfo{GOOS: goos, nameOfRuntimeType: make(map[uintptr]nameOfRuntimeTypeEntry), typeCache: make(map[dwarf.Offset]godwarf.Type)}
|
2017-04-06 18:14:01 +00:00
|
|
|
|
|
|
|
// TODO: find better way to determine proc arch (perhaps use executable file info)
|
|
|
|
switch goarch {
|
|
|
|
case "amd64":
|
2017-04-21 06:55:53 +00:00
|
|
|
r.Arch = AMD64Arch(goos)
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bininfo *BinaryInfo) LoadBinaryInfo(path string, wg *sync.WaitGroup) error {
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
bininfo.lastModified = fi.ModTime()
|
|
|
|
}
|
|
|
|
|
2017-04-21 06:55:53 +00:00
|
|
|
switch bininfo.GOOS {
|
2017-04-06 18:14:01 +00:00
|
|
|
case "linux":
|
|
|
|
return bininfo.LoadBinaryInfoElf(path, wg)
|
|
|
|
case "windows":
|
|
|
|
return bininfo.LoadBinaryInfoPE(path, wg)
|
|
|
|
case "darwin":
|
|
|
|
return bininfo.LoadBinaryInfoMacho(path, wg)
|
|
|
|
}
|
|
|
|
return errors.New("unsupported operating system")
|
|
|
|
}
|
|
|
|
|
2017-06-21 22:40:42 +00:00
|
|
|
// GStructOffset returns the offset of the G
|
|
|
|
// struct in thread local storage.
|
|
|
|
func (bi *BinaryInfo) GStructOffset() uint64 {
|
|
|
|
return bi.gStructOffset
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:14:01 +00:00
|
|
|
func (bi *BinaryInfo) LastModified() time.Time {
|
|
|
|
return bi.lastModified
|
|
|
|
}
|
|
|
|
|
|
|
|
// DwarfReader returns a reader for the dwarf data
|
|
|
|
func (bi *BinaryInfo) DwarfReader() *reader.Reader {
|
|
|
|
return reader.New(bi.dwarf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sources returns list of source files that comprise the debugged binary.
|
|
|
|
func (bi *BinaryInfo) Sources() map[string]*gosym.Obj {
|
|
|
|
return bi.goSymTable.Files
|
|
|
|
}
|
|
|
|
|
|
|
|
// Funcs returns list of functions present in the debugged program.
|
|
|
|
func (bi *BinaryInfo) Funcs() []gosym.Func {
|
|
|
|
return bi.goSymTable.Funcs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Types returns list of types present in the debugged program.
|
|
|
|
func (bi *BinaryInfo) Types() ([]string, error) {
|
|
|
|
types := make([]string, 0, len(bi.types))
|
|
|
|
for k := range bi.types {
|
|
|
|
types = append(types, k)
|
|
|
|
}
|
|
|
|
return types, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PCToLine converts an instruction address to a file/line/function.
|
|
|
|
func (bi *BinaryInfo) PCToLine(pc uint64) (string, int, *gosym.Func) {
|
|
|
|
return bi.goSymTable.PCToLine(pc)
|
|
|
|
}
|
|
|
|
|
2017-02-10 14:11:40 +00:00
|
|
|
// LineToPC converts a file:line into a memory address.
|
|
|
|
func (bi *BinaryInfo) LineToPC(filename string, lineno int) (pc uint64, fn *gosym.Func, err error) {
|
|
|
|
return bi.goSymTable.LineToPC(filename, lineno)
|
|
|
|
}
|
|
|
|
|
2017-04-21 06:55:53 +00:00
|
|
|
// PCToFunc returns the function containing the given PC address
|
|
|
|
func (bi *BinaryInfo) PCToFunc(pc uint64) *gosym.Func {
|
|
|
|
return bi.goSymTable.PCToFunc(pc)
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:14:01 +00:00
|
|
|
func (bi *BinaryInfo) Close() error {
|
|
|
|
return bi.closer.Close()
|
|
|
|
}
|
|
|
|
|
2017-08-15 06:21:24 +00:00
|
|
|
func (bi *BinaryInfo) setLoadError(fmtstr string, args ...interface{}) {
|
|
|
|
bi.loadErrMu.Lock()
|
|
|
|
bi.loadErr = fmt.Errorf(fmtstr, args...)
|
|
|
|
bi.loadErrMu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) LoadError() error {
|
|
|
|
return bi.loadErr
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:14:01 +00:00
|
|
|
// ELF ///////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) LoadBinaryInfoElf(path string, wg *sync.WaitGroup) error {
|
|
|
|
exe, err := os.OpenFile(path, 0, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bi.closer = exe
|
|
|
|
elfFile, err := elf.NewFile(exe)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if elfFile.Machine != elf.EM_X86_64 {
|
|
|
|
return UnsupportedLinuxArchErr
|
|
|
|
}
|
|
|
|
bi.dwarf, err = elfFile.DWARF()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-21 22:40:42 +00:00
|
|
|
wg.Add(5)
|
2017-04-06 18:14:01 +00:00
|
|
|
go bi.parseDebugFrameElf(elfFile, wg)
|
|
|
|
go bi.obtainGoSymbolsElf(elfFile, wg)
|
|
|
|
go bi.parseDebugLineInfoElf(elfFile, wg)
|
|
|
|
go bi.loadDebugInfoMaps(wg)
|
2017-06-21 22:40:42 +00:00
|
|
|
go bi.setGStructOffsetElf(elfFile, wg)
|
2017-04-06 18:14:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) parseDebugFrameElf(exe *elf.File, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
debugFrameSec := exe.Section(".debug_frame")
|
|
|
|
debugInfoSec := exe.Section(".debug_info")
|
|
|
|
|
|
|
|
if debugFrameSec != nil && debugInfoSec != nil {
|
|
|
|
debugFrame, err := exe.Section(".debug_frame").Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .debug_frame section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
dat, err := debugInfoSec.Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .debug_frame section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
bi.frameEntries = frame.Parse(debugFrame, frame.DwarfEndian(dat))
|
|
|
|
} else {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not find .debug_frame section in binary")
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) obtainGoSymbolsElf(exe *elf.File, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
var (
|
|
|
|
symdat []byte
|
|
|
|
pclndat []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if sec := exe.Section(".gosymtab"); sec != nil {
|
|
|
|
symdat, err = sec.Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .gosymtab section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sec := exe.Section(".gopclntab"); sec != nil {
|
|
|
|
pclndat, err = sec.Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .gopclntab section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pcln := gosym.NewLineTable(pclndat, exe.Section(".text").Addr)
|
|
|
|
tab, err := gosym.NewTable(symdat, pcln)
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get initialize line table: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bi.goSymTable = tab
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) parseDebugLineInfoElf(exe *elf.File, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
if sec := exe.Section(".debug_line"); sec != nil {
|
|
|
|
debugLine, err := exe.Section(".debug_line").Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .debug_line section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
bi.lineInfo = line.Parse(debugLine)
|
|
|
|
} else {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not find .debug_line section in binary")
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 22:40:42 +00:00
|
|
|
func (bi *BinaryInfo) setGStructOffsetElf(exe *elf.File, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
// This is a bit arcane. Essentially:
|
|
|
|
// - If the program is pure Go, it can do whatever it wants, and puts the G
|
|
|
|
// pointer at %fs-8.
|
|
|
|
// - Otherwise, Go asks the external linker to place the G pointer by
|
|
|
|
// emitting runtime.tlsg, a TLS symbol, which is relocated to the chosen
|
|
|
|
// offset in libc's TLS block.
|
|
|
|
symbols, err := exe.Symbols()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not parse ELF symbols: %v", err)
|
|
|
|
return
|
2017-06-21 22:40:42 +00:00
|
|
|
}
|
|
|
|
var tlsg *elf.Symbol
|
|
|
|
for _, symbol := range symbols {
|
|
|
|
if symbol.Name == "runtime.tlsg" {
|
|
|
|
s := symbol
|
|
|
|
tlsg = &s
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tlsg == nil {
|
|
|
|
bi.gStructOffset = ^uint64(8) + 1 // -8
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var tls *elf.Prog
|
|
|
|
for _, prog := range exe.Progs {
|
|
|
|
if prog.Type == elf.PT_TLS {
|
|
|
|
tls = prog
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The TLS register points to the end of the TLS block, which is
|
|
|
|
// tls.Memsz long. runtime.tlsg is an offset from the beginning of that block.
|
|
|
|
bi.gStructOffset = ^(tls.Memsz) + 1 + tlsg.Value // -tls.Memsz + tlsg.Value
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:14:01 +00:00
|
|
|
// PE ////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) LoadBinaryInfoPE(path string, wg *sync.WaitGroup) error {
|
|
|
|
peFile, closer, err := openExecutablePathPE(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bi.closer = closer
|
|
|
|
if peFile.Machine != pe.IMAGE_FILE_MACHINE_AMD64 {
|
|
|
|
return UnsupportedWindowsArchErr
|
|
|
|
}
|
2017-05-29 13:20:01 +00:00
|
|
|
bi.dwarf, err = peFile.DWARF()
|
2017-04-06 18:14:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(4)
|
|
|
|
go bi.parseDebugFramePE(peFile, wg)
|
|
|
|
go bi.obtainGoSymbolsPE(peFile, wg)
|
|
|
|
go bi.parseDebugLineInfoPE(peFile, wg)
|
|
|
|
go bi.loadDebugInfoMaps(wg)
|
2017-06-21 22:40:42 +00:00
|
|
|
|
|
|
|
// Use ArbitraryUserPointer (0x28) as pointer to pointer
|
|
|
|
// to G struct per:
|
|
|
|
// https://golang.org/src/runtime/cgo/gcc_windows_amd64.c
|
|
|
|
|
|
|
|
bi.gStructOffset = 0x28
|
2017-04-06 18:14:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func openExecutablePathPE(path string) (*pe.File, io.Closer, error) {
|
|
|
|
f, err := os.OpenFile(path, 0, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
peFile, err := pe.NewFile(f)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return peFile, f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) parseDebugFramePE(exe *pe.File, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
debugFrameSec := exe.Section(".debug_frame")
|
|
|
|
debugInfoSec := exe.Section(".debug_info")
|
|
|
|
|
|
|
|
if debugFrameSec != nil && debugInfoSec != nil {
|
|
|
|
debugFrame, err := debugFrameSec.Data()
|
|
|
|
if err != nil && uint32(len(debugFrame)) < debugFrameSec.Size {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .debug_frame section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
if 0 < debugFrameSec.VirtualSize && debugFrameSec.VirtualSize < debugFrameSec.Size {
|
|
|
|
debugFrame = debugFrame[:debugFrameSec.VirtualSize]
|
|
|
|
}
|
|
|
|
dat, err := debugInfoSec.Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .debug_info section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
bi.frameEntries = frame.Parse(debugFrame, frame.DwarfEndian(dat))
|
|
|
|
} else {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not find .debug_frame section in binary")
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 06:21:24 +00:00
|
|
|
func (bi *BinaryInfo) obtainGoSymbolsPE(exe *pe.File, wg *sync.WaitGroup) {
|
2017-04-06 18:14:01 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
_, symdat, pclndat, err := pclnPE(exe)
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get Go symbols: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pcln := gosym.NewLineTable(pclndat, uint64(exe.Section(".text").Offset))
|
|
|
|
tab, err := gosym.NewTable(symdat, pcln)
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get initialize line table: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.goSymTable = tab
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from https://golang.org/src/cmd/internal/objfile/pe.go
|
|
|
|
func findPESymbol(f *pe.File, name string) (*pe.Symbol, error) {
|
|
|
|
for _, s := range f.Symbols {
|
|
|
|
if s.Name != name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s.SectionNumber <= 0 {
|
|
|
|
return nil, fmt.Errorf("symbol %s: invalid section number %d", name, s.SectionNumber)
|
|
|
|
}
|
|
|
|
if len(f.Sections) < int(s.SectionNumber) {
|
|
|
|
return nil, fmt.Errorf("symbol %s: section number %d is larger than max %d", name, s.SectionNumber, len(f.Sections))
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no %s symbol found", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from https://golang.org/src/cmd/internal/objfile/pe.go
|
|
|
|
func loadPETable(f *pe.File, sname, ename string) ([]byte, error) {
|
|
|
|
ssym, err := findPESymbol(f, sname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
esym, err := findPESymbol(f, ename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ssym.SectionNumber != esym.SectionNumber {
|
|
|
|
return nil, fmt.Errorf("%s and %s symbols must be in the same section", sname, ename)
|
|
|
|
}
|
|
|
|
sect := f.Sections[ssym.SectionNumber-1]
|
|
|
|
data, err := sect.Data()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return data[ssym.Value:esym.Value], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from https://golang.org/src/cmd/internal/objfile/pe.go
|
|
|
|
func pclnPE(exe *pe.File) (textStart uint64, symtab, pclntab []byte, err error) {
|
|
|
|
var imageBase uint64
|
|
|
|
switch oh := exe.OptionalHeader.(type) {
|
|
|
|
case *pe.OptionalHeader32:
|
|
|
|
imageBase = uint64(oh.ImageBase)
|
|
|
|
case *pe.OptionalHeader64:
|
|
|
|
imageBase = oh.ImageBase
|
|
|
|
default:
|
|
|
|
return 0, nil, nil, fmt.Errorf("pe file format not recognized")
|
|
|
|
}
|
|
|
|
if sect := exe.Section(".text"); sect != nil {
|
|
|
|
textStart = imageBase + uint64(sect.VirtualAddress)
|
|
|
|
}
|
|
|
|
if pclntab, err = loadPETable(exe, "runtime.pclntab", "runtime.epclntab"); err != nil {
|
|
|
|
// We didn't find the symbols, so look for the names used in 1.3 and earlier.
|
|
|
|
// TODO: Remove code looking for the old symbols when we no longer care about 1.3.
|
|
|
|
var err2 error
|
|
|
|
if pclntab, err2 = loadPETable(exe, "pclntab", "epclntab"); err2 != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if symtab, err = loadPETable(exe, "runtime.symtab", "runtime.esymtab"); err != nil {
|
|
|
|
// Same as above.
|
|
|
|
var err2 error
|
|
|
|
if symtab, err2 = loadPETable(exe, "symtab", "esymtab"); err2 != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return textStart, symtab, pclntab, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) parseDebugLineInfoPE(exe *pe.File, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
if sec := exe.Section(".debug_line"); sec != nil {
|
|
|
|
debugLine, err := sec.Data()
|
|
|
|
if err != nil && uint32(len(debugLine)) < sec.Size {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .debug_line section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
if 0 < sec.VirtualSize && sec.VirtualSize < sec.Size {
|
|
|
|
debugLine = debugLine[:sec.VirtualSize]
|
|
|
|
}
|
|
|
|
bi.lineInfo = line.Parse(debugLine)
|
|
|
|
} else {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not find .debug_line section in binary")
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MACH-O ////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) LoadBinaryInfoMacho(path string, wg *sync.WaitGroup) error {
|
|
|
|
exe, err := macho.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bi.closer = exe
|
|
|
|
if exe.Cpu != macho.CpuAmd64 {
|
|
|
|
return UnsupportedDarwinArchErr
|
|
|
|
}
|
|
|
|
bi.dwarf, err = exe.DWARF()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(4)
|
|
|
|
go bi.parseDebugFrameMacho(exe, wg)
|
|
|
|
go bi.obtainGoSymbolsMacho(exe, wg)
|
|
|
|
go bi.parseDebugLineInfoMacho(exe, wg)
|
|
|
|
go bi.loadDebugInfoMaps(wg)
|
2017-06-21 22:40:42 +00:00
|
|
|
bi.gStructOffset = 0x8a0
|
2017-04-06 18:14:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) parseDebugFrameMacho(exe *macho.File, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
debugFrameSec := exe.Section("__debug_frame")
|
|
|
|
debugInfoSec := exe.Section("__debug_info")
|
|
|
|
|
|
|
|
if debugFrameSec != nil && debugInfoSec != nil {
|
|
|
|
debugFrame, err := exe.Section("__debug_frame").Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get __debug_frame section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
dat, err := debugInfoSec.Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .debug_info section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
bi.frameEntries = frame.Parse(debugFrame, frame.DwarfEndian(dat))
|
|
|
|
} else {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not find __debug_frame section in binary")
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) obtainGoSymbolsMacho(exe *macho.File, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
var (
|
|
|
|
symdat []byte
|
|
|
|
pclndat []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if sec := exe.Section("__gosymtab"); sec != nil {
|
|
|
|
symdat, err = sec.Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .gosymtab section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sec := exe.Section("__gopclntab"); sec != nil {
|
|
|
|
pclndat, err = sec.Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get .gopclntab section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pcln := gosym.NewLineTable(pclndat, exe.Section("__text").Addr)
|
|
|
|
tab, err := gosym.NewTable(symdat, pcln)
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get initialize line table: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bi.goSymTable = tab
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bi *BinaryInfo) parseDebugLineInfoMacho(exe *macho.File, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
if sec := exe.Section("__debug_line"); sec != nil {
|
|
|
|
debugLine, err := exe.Section("__debug_line").Data()
|
|
|
|
if err != nil {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not get __debug_line section: %v", err)
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
bi.lineInfo = line.Parse(debugLine)
|
|
|
|
} else {
|
2017-08-15 06:21:24 +00:00
|
|
|
bi.setLoadError("could not find __debug_line section in binary")
|
|
|
|
return
|
2017-04-06 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|