macho/macho-go/pkg/ios/macho/dwarf.go
2023-05-31 16:17:03 +07:00

56 lines
1.2 KiB
Go

package macho
import (
"bytes"
"ios-wrapper/pkg/dwarf"
)
type DebuglineInfo struct {
debuglines []*dwarf.DebuglineParser
}
type LineInfo struct {
Line uint32
Directory string
File string
}
func (dinfo *DebuglineInfo) Find(address uint64) *LineInfo {
for _, debugline := range dinfo.debuglines {
found, line, directory, file := debugline.Find(address)
if found {
return &LineInfo{
line, directory, file,
}
}
}
return nil
}
// Parse the __debug_line into a list of DebugLine
//
// reference from Crystal dwarf parser
// https://github.com/crystal-lang/crystal/blob/421fa11bf05b11441b74bbeb2762b673f96fec3f/src/crystal/dwarf/line_numbers.cr
func (mc *MachoContext) DebugLineInfo() *DebuglineInfo {
debug_line_section := mc.FindSection("__debug_line")
if debug_line_section == nil {
return nil
}
start := uint64(debug_line_section.Offset())
end := start + debug_line_section.Size()
buf := bytes.NewBuffer(mc.buf[start:end])
debugLine := []*dwarf.DebuglineParser{}
for buf.Len() != 0 {
parser := &dwarf.DebuglineParser{}
parser.Parse(buf, mc.byteorder)
debugLine = append(debugLine, parser)
}
return &DebuglineInfo{
debugLine,
}
}