github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/debug/dwarf/open.go (about)

     1  // Copyright 2009 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package dwarf provides access to DWARF debugging information loaded from
     6  // executable files, as defined in the DWARF 2.0 Standard at
     7  // http://dwarfstd.org/doc/dwarf-2.0.0.pdf
     8  package dwarf
     9  
    10  import "encoding/binary"
    11  
    12  // Data represents the DWARF debugging information
    13  // loaded from an executable file (for example, an ELF or Mach-O executable).
    14  type Data struct {
    15  	// raw data
    16  	abbrev   []byte
    17  	aranges  []byte
    18  	frame    []byte
    19  	info     []byte
    20  	line     []byte
    21  	pubnames []byte
    22  	ranges   []byte
    23  	str      []byte
    24  
    25  	// parsed data
    26  	abbrevCache map[uint32]abbrevTable
    27  	addrsize    int
    28  	order       binary.ByteOrder
    29  	typeCache   map[Offset]Type
    30  	unit        []unit
    31  }
    32  
    33  // New returns a new Data object initialized from the given parameters.
    34  // Rather than calling this function directly, clients should typically use
    35  // the DWARF method of the File type of the appropriate package debug/elf,
    36  // debug/macho, or debug/pe.
    37  //
    38  // The []byte arguments are the data from the corresponding debug section
    39  // in the object file; for example, for an ELF object, abbrev is the contents of
    40  // the ".debug_abbrev" section.
    41  func New(abbrev, aranges, frame, info, line, pubnames, ranges, str []byte) (*Data, error) {
    42  	d := &Data{
    43  		abbrev:      abbrev,
    44  		aranges:     aranges,
    45  		frame:       frame,
    46  		info:        info,
    47  		line:        line,
    48  		pubnames:    pubnames,
    49  		ranges:      ranges,
    50  		str:         str,
    51  		abbrevCache: make(map[uint32]abbrevTable),
    52  		typeCache:   make(map[Offset]Type),
    53  	}
    54  
    55  	// Sniff .debug_info to figure out byte order.
    56  	// bytes 4:6 are the version, a tiny 16-bit number (1, 2, 3).
    57  	if len(d.info) < 6 {
    58  		return nil, DecodeError{"info", Offset(len(d.info)), "too short"}
    59  	}
    60  	x, y := d.info[4], d.info[5]
    61  	switch {
    62  	case x == 0 && y == 0:
    63  		return nil, DecodeError{"info", 4, "unsupported version 0"}
    64  	case x == 0:
    65  		d.order = binary.BigEndian
    66  	case y == 0:
    67  		d.order = binary.LittleEndian
    68  	default:
    69  		return nil, DecodeError{"info", 4, "cannot determine byte order"}
    70  	}
    71  
    72  	u, err := d.parseUnits()
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	d.unit = u
    77  	return d, nil
    78  }