mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/objdump: works with windows pe executables now
Most code is copy from addr2line change 01dd67e5827f Update #7406 Fixes #7937 LGTM=iant R=golang-codereviews, iant, 0intro CC=golang-codereviews https://golang.org/cl/95090044
This commit is contained in:
parent
176041e4c6
commit
20aa947c56
2 changed files with 149 additions and 8 deletions
|
|
@ -170,18 +170,50 @@ func loadTables(f *os.File) (textStart uint64, textData, symtab, pclntab []byte,
|
||||||
textStart = uint64(sect.VirtualAddress)
|
textStart = uint64(sect.VirtualAddress)
|
||||||
textData, _ = sect.Data()
|
textData, _ = sect.Data()
|
||||||
}
|
}
|
||||||
if sect := obj.Section(".gosymtab"); sect != nil {
|
if pclntab, err = loadPETable(obj, "pclntab", "epclntab"); err != nil {
|
||||||
if symtab, err = sect.Data(); err != nil {
|
return 0, nil, nil, nil, err
|
||||||
return 0, nil, nil, nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if sect := obj.Section(".gopclntab"); sect != nil {
|
if symtab, err = loadPETable(obj, "symtab", "esymtab"); err != nil {
|
||||||
if pclntab, err = sect.Data(); err != nil {
|
return 0, nil, nil, nil, err
|
||||||
return 0, nil, nil, nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return textStart, textData, symtab, pclntab, nil
|
return textStart, textData, symtab, pclntab, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, nil, nil, nil, fmt.Errorf("unrecognized binary format")
|
return 0, nil, nil, nil, fmt.Errorf("unrecognized binary format")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func findPESymbol(f *pe.File, name string) (*pe.Symbol, error) {
|
||||||
|
for _, s := range f.Symbols {
|
||||||
|
if s.Name != name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if s.SectionNumber <= 0 {
|
||||||
|
return nil, fmt.Errorf("symbol %s: invalid section number %d", name, s.SectionNumber)
|
||||||
|
}
|
||||||
|
if len(f.Sections) < int(s.SectionNumber) {
|
||||||
|
return nil, fmt.Errorf("symbol %s: section number %d is larger than max %d", name, s.SectionNumber, len(f.Sections))
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("no %s symbol found", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadPETable(f *pe.File, sname, ename string) ([]byte, error) {
|
||||||
|
ssym, err := findPESymbol(f, sname)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
esym, err := findPESymbol(f, ename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if ssym.SectionNumber != esym.SectionNumber {
|
||||||
|
return nil, fmt.Errorf("%s and %s symbols must be in the same section", sname, ename)
|
||||||
|
}
|
||||||
|
sect := f.Sections[ssym.SectionNumber-1]
|
||||||
|
data, err := sect.Data()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return data[ssym.Value:esym.Value], nil
|
||||||
|
}
|
||||||
|
|
|
||||||
109
src/cmd/objdump/objdump_test.go
Normal file
109
src/cmd/objdump/objdump_test.go
Normal file
|
|
@ -0,0 +1,109 @@
|
||||||
|
// Copyright 2014 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 (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func loadSyms(t *testing.T) map[string]string {
|
||||||
|
cmd := exec.Command("go", "tool", "nm", os.Args[0])
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("go tool nm %v: %v\n%s", os.Args[0], err, string(out))
|
||||||
|
}
|
||||||
|
syms := make(map[string]string)
|
||||||
|
scanner := bufio.NewScanner(bytes.NewReader(out))
|
||||||
|
for scanner.Scan() {
|
||||||
|
f := strings.Fields(scanner.Text())
|
||||||
|
if len(f) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
syms[f[2]] = f[0]
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
t.Fatalf("error reading symbols: %v", err)
|
||||||
|
}
|
||||||
|
return syms
|
||||||
|
}
|
||||||
|
|
||||||
|
func runObjDump(t *testing.T, exepath, startaddr string) (path, lineno string) {
|
||||||
|
addr, err := strconv.ParseUint(startaddr, 16, 64)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("invalid start address %v: %v", startaddr, err)
|
||||||
|
}
|
||||||
|
endaddr := fmt.Sprintf("%x", addr+10)
|
||||||
|
cmd := exec.Command(exepath, os.Args[0], "0x"+startaddr, "0x"+endaddr)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("go tool objdump %v: %v\n%s", os.Args[0], err, string(out))
|
||||||
|
}
|
||||||
|
f := strings.Split(string(out), "\n")
|
||||||
|
if len(f) < 1 {
|
||||||
|
t.Fatal("objdump output must have at least one line")
|
||||||
|
}
|
||||||
|
pathAndLineNo := f[0]
|
||||||
|
f = strings.Split(pathAndLineNo, ":")
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
switch len(f) {
|
||||||
|
case 2:
|
||||||
|
return f[0], f[1]
|
||||||
|
case 3:
|
||||||
|
return f[0] + ":" + f[1], f[2]
|
||||||
|
default:
|
||||||
|
t.Fatalf("no line number found in %q", pathAndLineNo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(f) != 2 {
|
||||||
|
t.Fatalf("no line number found in %q", pathAndLineNo)
|
||||||
|
}
|
||||||
|
return f[0], f[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is line 75. The test depends on that.
|
||||||
|
func TestObjDump(t *testing.T) {
|
||||||
|
if runtime.GOOS == "plan9" {
|
||||||
|
t.Skip("skipping test; see http://golang.org/issue/7947")
|
||||||
|
}
|
||||||
|
syms := loadSyms(t)
|
||||||
|
|
||||||
|
tmpDir, err := ioutil.TempDir("", "TestObjDump")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("TempDir failed: ", err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
|
exepath := filepath.Join(tmpDir, "testobjdump.exe")
|
||||||
|
out, err := exec.Command("go", "build", "-o", exepath, "cmd/objdump").CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("go build -o %v cmd/objdump: %v\n%s", exepath, err, string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
srcPath, srcLineNo := runObjDump(t, exepath, syms["cmd/objdump.TestObjDump"])
|
||||||
|
fi1, err := os.Stat("objdump_test.go")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Stat failed: %v", err)
|
||||||
|
}
|
||||||
|
fi2, err := os.Stat(srcPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Stat failed: %v", err)
|
||||||
|
}
|
||||||
|
if !os.SameFile(fi1, fi2) {
|
||||||
|
t.Fatalf("objdump_test.go and %s are not same file", srcPath)
|
||||||
|
}
|
||||||
|
if srcLineNo != "76" {
|
||||||
|
t.Fatalf("line number = %v; want 76", srcLineNo)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue