// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.
/*Package dwarf provides access to DWARF debugging information loaded fromexecutable files, as defined in the DWARF 2.0 Standard athttp://dwarfstd.org/doc/dwarf-2.0.0.pdf.# SecurityThis package is not designed to be hardened against adversarial inputs, and isoutside the scope of https://go.dev/security/policy. In particular, only basicvalidation is done when parsing object files. As such, care should be taken whenparsing untrusted inputs, as parsing malformed files may consume significantresources, or cause panics.*/
package dwarfimport ()// Data represents the DWARF debugging information// loaded from an executable file (for example, an ELF or Mach-O executable).typeDatastruct {// raw data abbrev []byte aranges []byte frame []byte info []byte line []byte pubnames []byte ranges []byte str []byte// New sections added in DWARF 5. addr []byte lineStr []byte strOffsets []byte rngLists []byte// parsed data abbrevCache map[uint64]abbrevTable bigEndian bool order binary.ByteOrder typeCache map[Offset]Type typeSigs map[uint64]*typeUnit unit []unit}var errSegmentSelector = errors.New("non-zero segment_selector size not supported")// New returns a new [Data] object initialized from the given parameters.// Rather than calling this function directly, clients should typically use// the DWARF method of the File type of the appropriate package [debug/elf],// [debug/macho], or [debug/pe].//// The []byte arguments are the data from the corresponding debug section// in the object file; for example, for an ELF object, abbrev is the contents of// the ".debug_abbrev" section.func (, , , , , , , []byte) (*Data, error) { := &Data{abbrev: ,aranges: ,frame: ,info: ,line: ,pubnames: ,ranges: ,str: ,abbrevCache: make(map[uint64]abbrevTable),typeCache: make(map[Offset]Type),typeSigs: make(map[uint64]*typeUnit), }// Sniff .debug_info to figure out byte order. // 32-bit DWARF: 4 byte length, 2 byte version. // 64-bit DWARf: 4 bytes of 0xff, 8 byte length, 2 byte version.iflen(.info) < 6 {returnnil, DecodeError{"info", Offset(len(.info)), "too short"} } := 4if .info[0] == 0xff && .info[1] == 0xff && .info[2] == 0xff && .info[3] == 0xff {iflen(.info) < 14 {returnnil, DecodeError{"info", Offset(len(.info)), "too short"} } = 12 }// Fetch the version, a tiny 16-bit number (1, 2, 3, 4, 5). , := .info[], .info[+1]switch {case == 0 && == 0:returnnil, DecodeError{"info", 4, "unsupported version 0"}case == 0: .bigEndian = true .order = binary.BigEndiancase == 0: .bigEndian = false .order = binary.LittleEndiandefault:returnnil, DecodeError{"info", 4, "cannot determine byte order"} } , := .parseUnits()if != nil {returnnil, } .unit = return , nil}// AddTypes will add one .debug_types section to the DWARF data. A// typical object with DWARF version 4 debug info will have multiple// .debug_types sections. The name is used for error reporting only,// and serves to distinguish one .debug_types section from another.func ( *Data) ( string, []byte) error {return .parseTypes(, )}// AddSection adds another DWARF section by name. The name should be a// DWARF section name such as ".debug_addr", ".debug_str_offsets", and// so forth. This approach is used for new DWARF sections added in// DWARF 5 and later.func ( *Data) ( string, []byte) error {varerrorswitch {case".debug_addr": .addr = case".debug_line_str": .lineStr = case".debug_str_offsets": .strOffsets = case".debug_rnglists": .rngLists = }// Just ignore names that we don't yet support.return}
The pages are generated with Goldsv0.7.3. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.