// Adapted from src/debug/pe/file.go: pe.(*File).DWARF() func dwarfFromPE(f *pe.File) (*dwarf.Data, error) { // There are many other DWARF sections, but these // are the ones the debug/dwarf package uses. // Don't bother loading others. var names = [...]string{"abbrev", "info", "line", "str"} var dat [len(names)][]byte for i, name := range names { name = ".debug_" + name s := f.Section(name) if s == nil { continue } b, err := s.Data() if err != nil && uint32(len(b)) < s.Size { return nil, err } if 0 < s.VirtualSize && s.VirtualSize < s.Size { b = b[:s.VirtualSize] } dat[i] = b } abbrev, info, line, str := dat[0], dat[1], dat[2], dat[3] return dwarf.New(abbrev, nil, nil, info, line, nil, nil, str) }
// DWARF returns the DWARF debug information for the Mach-O file. func (f *File) DWARF() (*dwarf.Data, error) { // There are many other DWARF sections, but these // are the required ones, and the debug/dwarf package // does not use the others, so don't bother loading them. var names = [...]string{"abbrev", "frame", "info", "line", "str"} var dat [len(names)][]byte for i, name := range names { name = "__debug_" + name s := f.Section(name) if s == nil { continue } b, err := s.Data() if err != nil && uint64(len(b)) < s.Size { return nil, err } dat[i] = b } abbrev, frame, info, line, str := dat[0], dat[1], dat[2], dat[3], dat[4] return dwarf.New(abbrev, nil, frame, info, line, nil, nil, str) }
func (f *File) DWARF() (*dwarf.Data, error) { // There are many other DWARF sections, but these // are the required ones, and the debug/dwarf package // does not use the others, so don't bother loading them. // r: added line. var names = [...]string{"abbrev", "frame", "info", "line", "str"} var dat [len(names)][]byte for i, name := range names { name = ".debug_" + name s := f.Section(name) if s == nil { continue } b, err := s.Data() if err != nil && uint64(len(b)) < s.Size { return nil, err } dat[i] = b } // If there's a relocation table for .debug_info, we have to process it // now otherwise the data in .debug_info is invalid for x86-64 objects. rela := f.Section(".rela.debug_info") if rela != nil && rela.Type == SHT_RELA && f.Machine == EM_X86_64 { data, err := rela.Data() if err != nil { return nil, err } err = f.applyRelocations(dat[2], data) if err != nil { return nil, err } } abbrev, frame, info, line, str := dat[0], dat[1], dat[2], dat[3], dat[4] d, err := dwarf.New(abbrev, nil, frame, info, line, nil, nil, str) if err != nil { return nil, err } // Look for DWARF4 .debug_types sections. for i, s := range f.Sections { if s.Name == ".debug_types" { b, err := s.Data() if err != nil && uint64(len(b)) < s.Size { return nil, err } for _, r := range f.Sections { if r.Type != SHT_RELA && r.Type != SHT_REL { continue } if int(r.Info) != i { continue } rd, err := r.Data() if err != nil { return nil, err } err = f.applyRelocations(b, rd) if err != nil { return nil, err } } err = d.AddTypes(fmt.Sprintf("types-%d", i), b) if err != nil { return nil, err } } } return d, nil }