The Go Programming Language

Source file src/pkg/debug/dwarf/open.go

     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 (
    11		"encoding/binary"
    12		"os"
    13	)
    14	
    15	// Data represents the DWARF debugging information
    16	// loaded from an executable file (for example, an ELF or Mach-O executable).
    17	type Data struct {
    18		// raw data
    19		abbrev   []byte
    20		aranges  []byte
    21		frame    []byte
    22		info     []byte
    23		line     []byte
    24		pubnames []byte
    25		ranges   []byte
    26		str      []byte
    27	
    28		// parsed data
    29		abbrevCache map[uint32]abbrevTable
    30		addrsize    int
    31		order       binary.ByteOrder
    32		typeCache   map[Offset]Type
    33		unit        []unit
    34	}
    35	
    36	// New returns a new Data object initialized from the given parameters.
    37	// Clients should typically use [TODO(rsc): method to be named later] instead of calling
    38	// New directly.
    39	//
    40	// The []byte arguments are the data from the corresponding debug section
    41	// in the object file; for example, for an ELF object, abbrev is the contents of
    42	// the ".debug_abbrev" section.
    43	func New(abbrev, aranges, frame, info, line, pubnames, ranges, str []byte) (*Data, os.Error) {
    44		d := &Data{
    45			abbrev:      abbrev,
    46			aranges:     aranges,
    47			frame:       frame,
    48			info:        info,
    49			line:        line,
    50			pubnames:    pubnames,
    51			ranges:      ranges,
    52			str:         str,
    53			abbrevCache: make(map[uint32]abbrevTable),
    54			typeCache:   make(map[Offset]Type),
    55		}
    56	
    57		// Sniff .debug_info to figure out byte order.
    58		// bytes 4:6 are the version, a tiny 16-bit number (1, 2, 3).
    59		if len(d.info) < 6 {
    60			return nil, DecodeError{"info", Offset(len(d.info)), "too short"}
    61		}
    62		x, y := d.info[4], d.info[5]
    63		switch {
    64		case x == 0 && y == 0:
    65			return nil, DecodeError{"info", 4, "unsupported version 0"}
    66		case x == 0:
    67			d.order = binary.BigEndian
    68		case y == 0:
    69			d.order = binary.LittleEndian
    70		default:
    71			return nil, DecodeError{"info", 4, "cannot determine byte order"}
    72		}
    73	
    74		u, err := d.parseUnits()
    75		if err != nil {
    76			return nil, err
    77		}
    78		d.unit = u
    79		return d, nil
    80	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.