1 Star 0 Fork 0

runzhe.wrz/dwarf2json

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dwarf.go 2.51 KB
一键复制 编辑 原始数据 按行查看 历史
// 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 main
import (
"bytes"
"compress/zlib"
"debug/dwarf"
"debug/elf"
"encoding/binary"
"fmt"
"io"
"strings"
)
// DWARF is a copy of Go's debug/elf func (f *File) DWARF().
// it has been forked here to prevent relocations from being applied
// prior to DWARF .debug_info parsing.
//
// See #15 for more details.
func DWARF(f *elf.File) (*dwarf.Data, error) {
dwarfSuffix := func(s *elf.Section) string {
switch {
case strings.HasPrefix(s.Name, ".debug_"):
return s.Name[7:]
case strings.HasPrefix(s.Name, ".zdebug_"):
return s.Name[8:]
default:
return ""
}
}
// sectionData gets the data for s, checks its size, and
// applies any applicable relations.
sectionData := func(i int, s *elf.Section) ([]byte, error) {
b, err := s.Data()
if err != nil && uint64(len(b)) < s.Size {
return nil, err
}
if len(b) >= 12 && string(b[:4]) == "ZLIB" {
dlen := binary.BigEndian.Uint64(b[4:12])
dbuf := make([]byte, dlen)
r, err := zlib.NewReader(bytes.NewBuffer(b[12:]))
if err != nil {
return nil, err
}
if _, err := io.ReadFull(r, dbuf); err != nil {
return nil, err
}
if err := r.Close(); err != nil {
return nil, err
}
b = dbuf
}
// NOTE: removed relocations from original code here
return b, nil
}
// There are many DWARf sections, but these are the ones
// the debug/dwarf package started with.
var dat = map[string][]byte{"abbrev": nil, "info": nil, "str": nil, "line": nil, "ranges": nil}
for i, s := range f.Sections {
suffix := dwarfSuffix(s)
if suffix == "" {
continue
}
if _, ok := dat[suffix]; !ok {
continue
}
b, err := sectionData(i, s)
if err != nil {
return nil, err
}
dat[suffix] = b
}
d, err := dwarf.New(dat["abbrev"], nil, nil, dat["info"], dat["line"], nil, dat["ranges"], dat["str"])
if err != nil {
return nil, err
}
// Look for DWARF4 .debug_types sections and DWARF5 sections.
for i, s := range f.Sections {
suffix := dwarfSuffix(s)
if suffix == "" {
continue
}
if _, ok := dat[suffix]; ok {
// Already handled.
continue
}
b, err := sectionData(i, s)
if err != nil {
return nil, err
}
if suffix == "types" {
if err := d.AddTypes(fmt.Sprintf("types-%d", i), b); err != nil {
return nil, err
}
} else {
if err := d.AddSection(".debug_"+suffix, b); err != nil {
return nil, err
}
}
}
return d, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/runzhe-wrz/dwarf2json.git
git@gitee.com:runzhe-wrz/dwarf2json.git
runzhe-wrz
dwarf2json
dwarf2json
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385