Commit 589ea936 authored by Hiroshi Ioka's avatar Hiroshi Ioka Committed by Ian Lance Taylor

cmd/nm: handle cgo archive

This CL also make cmd/nm accept PE object file.

Fixes #21706

Change-Id: I4a528b7d53da1082e61523ebeba02c4c514a43a7
Reviewed-on: https://go-review.googlesource.com/64890
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 6a537c1d
...@@ -133,7 +133,12 @@ type Package struct { ...@@ -133,7 +133,12 @@ type Package struct {
Syms []*Sym // symbols defined by this package Syms []*Sym // symbols defined by this package
MaxVersion int // maximum Version in any SymID in Syms MaxVersion int // maximum Version in any SymID in Syms
Arch string // architecture Arch string // architecture
Native []io.ReaderAt // native object data (e.g. ELF) Native []*NativeReader // native object data (e.g. ELF)
}
type NativeReader struct {
Name string
io.ReaderAt
} }
var ( var (
...@@ -439,7 +444,10 @@ func (r *objReader) parseArchive() error { ...@@ -439,7 +444,10 @@ func (r *objReader) parseArchive() error {
return fmt.Errorf("parsing archive member %q: %v", name, err) return fmt.Errorf("parsing archive member %q: %v", name, err)
} }
} else { } else {
r.p.Native = append(r.p.Native, io.NewSectionReader(r.f, r.offset, size)) r.p.Native = append(r.p.Native, &NativeReader{
Name: name,
ReaderAt: io.NewSectionReader(r.f, r.offset, size),
})
} }
r.skip(r.limit - r.offset) r.skip(r.limit - r.offset)
......
...@@ -40,23 +40,23 @@ type Disasm struct { ...@@ -40,23 +40,23 @@ type Disasm struct {
} }
// Disasm returns a disassembler for the file f. // Disasm returns a disassembler for the file f.
func (f *File) Disasm() (*Disasm, error) { func (e *Entry) Disasm() (*Disasm, error) {
syms, err := f.Symbols() syms, err := e.Symbols()
if err != nil { if err != nil {
return nil, err return nil, err
} }
pcln, err := f.PCLineTable() pcln, err := e.PCLineTable()
if err != nil { if err != nil {
return nil, err return nil, err
} }
textStart, textBytes, err := f.Text() textStart, textBytes, err := e.Text()
if err != nil { if err != nil {
return nil, err return nil, err
} }
goarch := f.GOARCH() goarch := e.GOARCH()
disasm := disasms[goarch] disasm := disasms[goarch]
byteOrder := byteOrders[goarch] byteOrder := byteOrders[goarch]
if disasm == nil || byteOrder == nil { if disasm == nil || byteOrder == nil {
......
...@@ -11,14 +11,14 @@ import ( ...@@ -11,14 +11,14 @@ import (
"debug/elf" "debug/elf"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"os" "io"
) )
type elfFile struct { type elfFile struct {
elf *elf.File elf *elf.File
} }
func openElf(r *os.File) (rawFile, error) { func openElf(r io.ReaderAt) (rawFile, error) {
f, err := elf.NewFile(r) f, err := elf.NewFile(r)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -22,12 +22,33 @@ type goobjFile struct { ...@@ -22,12 +22,33 @@ type goobjFile struct {
f *os.File // the underlying .o or .a file f *os.File // the underlying .o or .a file
} }
func openGoobj(r *os.File) (rawFile, error) { func openGoFile(r *os.File) (*File, error) {
f, err := goobj.Parse(r, `""`) f, err := goobj.Parse(r, `""`)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &goobjFile{goobj: f, f: r}, nil rf := &goobjFile{goobj: f, f: r}
if len(f.Native) == 0 {
return &File{r, []*Entry{&Entry{raw: rf}}}, nil
}
entries := make([]*Entry, len(f.Native)+1)
entries[0] = &Entry{
raw: rf,
}
L:
for i, nr := range f.Native {
for _, try := range openers {
if raw, err := try(nr); err == nil {
entries[i+1] = &Entry{
name: nr.Name,
raw: raw,
}
continue L
}
}
return nil, fmt.Errorf("open %s: unrecognized archive member %s", r.Name(), nr.Name)
}
return &File{r, entries}, nil
} }
func goobjName(id goobj.SymID) string { func goobjName(id goobj.SymID) string {
......
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
"debug/dwarf" "debug/dwarf"
"debug/macho" "debug/macho"
"fmt" "fmt"
"os" "io"
"sort" "sort"
) )
...@@ -20,7 +20,7 @@ type machoFile struct { ...@@ -20,7 +20,7 @@ type machoFile struct {
macho *macho.File macho *macho.File
} }
func openMacho(r *os.File) (rawFile, error) { func openMacho(r io.ReaderAt) (rawFile, error) {
f, err := macho.NewFile(r) f, err := macho.NewFile(r)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"debug/dwarf" "debug/dwarf"
"debug/gosym" "debug/gosym"
"fmt" "fmt"
"io"
"os" "os"
"sort" "sort"
) )
...@@ -25,6 +26,11 @@ type rawFile interface { ...@@ -25,6 +26,11 @@ type rawFile interface {
// A File is an opened executable file. // A File is an opened executable file.
type File struct { type File struct {
r *os.File r *os.File
entries []*Entry
}
type Entry struct {
name string
raw rawFile raw rawFile
} }
...@@ -50,9 +56,8 @@ type RelocStringer interface { ...@@ -50,9 +56,8 @@ type RelocStringer interface {
String(insnOffset uint64) string String(insnOffset uint64) string
} }
var openers = []func(*os.File) (rawFile, error){ var openers = []func(io.ReaderAt) (rawFile, error){
openElf, openElf,
openGoobj,
openMacho, openMacho,
openPE, openPE,
openPlan9, openPlan9,
...@@ -65,9 +70,12 @@ func Open(name string) (*File, error) { ...@@ -65,9 +70,12 @@ func Open(name string) (*File, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if f, err := openGoFile(r); err == nil {
return f, nil
}
for _, try := range openers { for _, try := range openers {
if raw, err := try(r); err == nil { if raw, err := try(r); err == nil {
return &File{r, raw}, nil return &File{r, []*Entry{&Entry{raw: raw}}}, nil
} }
} }
r.Close() r.Close()
...@@ -78,8 +86,44 @@ func (f *File) Close() error { ...@@ -78,8 +86,44 @@ func (f *File) Close() error {
return f.r.Close() return f.r.Close()
} }
func (f *File) Entries() []*Entry {
return f.entries
}
func (f *File) Symbols() ([]Sym, error) { func (f *File) Symbols() ([]Sym, error) {
syms, err := f.raw.symbols() return f.entries[0].Symbols()
}
func (f *File) PCLineTable() (Liner, error) {
return f.entries[0].PCLineTable()
}
func (f *File) Text() (uint64, []byte, error) {
return f.entries[0].Text()
}
func (f *File) GOARCH() string {
return f.entries[0].GOARCH()
}
func (f *File) LoadAddress() (uint64, error) {
return f.entries[0].LoadAddress()
}
func (f *File) DWARF() (*dwarf.Data, error) {
return f.entries[0].DWARF()
}
func (f *File) Disasm() (*Disasm, error) {
return f.entries[0].Disasm()
}
func (e *Entry) Name() string {
return e.name
}
func (e *Entry) Symbols() ([]Sym, error) {
syms, err := e.raw.symbols()
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -93,37 +137,37 @@ func (x byAddr) Less(i, j int) bool { return x[i].Addr < x[j].Addr } ...@@ -93,37 +137,37 @@ func (x byAddr) Less(i, j int) bool { return x[i].Addr < x[j].Addr }
func (x byAddr) Len() int { return len(x) } func (x byAddr) Len() int { return len(x) }
func (x byAddr) Swap(i, j int) { x[i], x[j] = x[j], x[i] } func (x byAddr) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (f *File) PCLineTable() (Liner, error) { func (e *Entry) PCLineTable() (Liner, error) {
// If the raw file implements Liner directly, use that. // If the raw file implements Liner directly, use that.
// Currently, only Go intermediate objects and archives (goobj) use this path. // Currently, only Go intermediate objects and archives (goobj) use this path.
if pcln, ok := f.raw.(Liner); ok { if pcln, ok := e.raw.(Liner); ok {
return pcln, nil return pcln, nil
} }
// Otherwise, read the pcln tables and build a Liner out of that. // Otherwise, read the pcln tables and build a Liner out of that.
textStart, symtab, pclntab, err := f.raw.pcln() textStart, symtab, pclntab, err := e.raw.pcln()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return gosym.NewTable(symtab, gosym.NewLineTable(pclntab, textStart)) return gosym.NewTable(symtab, gosym.NewLineTable(pclntab, textStart))
} }
func (f *File) Text() (uint64, []byte, error) { func (e *Entry) Text() (uint64, []byte, error) {
return f.raw.text() return e.raw.text()
} }
func (f *File) GOARCH() string { func (e *Entry) GOARCH() string {
return f.raw.goarch() return e.raw.goarch()
} }
// LoadAddress returns the expected load address of the file. // LoadAddress returns the expected load address of the file.
// This differs from the actual load address for a position-independent // This differs from the actual load address for a position-independent
// executable. // executable.
func (f *File) LoadAddress() (uint64, error) { func (e *Entry) LoadAddress() (uint64, error) {
return f.raw.loadAddress() return e.raw.loadAddress()
} }
// DWARF returns DWARF debug data for the file, if any. // DWARF returns DWARF debug data for the file, if any.
// This is for cmd/pprof to locate cgo functions. // This is for cmd/pprof to locate cgo functions.
func (f *File) DWARF() (*dwarf.Data, error) { func (e *Entry) DWARF() (*dwarf.Data, error) {
return f.raw.dwarf() return e.raw.dwarf()
} }
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
"debug/dwarf" "debug/dwarf"
"debug/pe" "debug/pe"
"fmt" "fmt"
"os" "io"
"sort" "sort"
) )
...@@ -18,17 +18,11 @@ type peFile struct { ...@@ -18,17 +18,11 @@ type peFile struct {
pe *pe.File pe *pe.File
} }
func openPE(r *os.File) (rawFile, error) { func openPE(r io.ReaderAt) (rawFile, error) {
f, err := pe.NewFile(r) f, err := pe.NewFile(r)
if err != nil { if err != nil {
return nil, err return nil, err
} }
switch f.OptionalHeader.(type) {
case *pe.OptionalHeader32, *pe.OptionalHeader64:
// ok
default:
return nil, fmt.Errorf("unrecognized PE format")
}
return &peFile{f}, nil return &peFile{f}, nil
} }
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
"debug/plan9obj" "debug/plan9obj"
"errors" "errors"
"fmt" "fmt"
"os" "io"
"sort" "sort"
) )
...@@ -28,7 +28,7 @@ type plan9File struct { ...@@ -28,7 +28,7 @@ type plan9File struct {
plan9 *plan9obj.File plan9 *plan9obj.File
} }
func openPlan9(r *os.File) (rawFile, error) { func openPlan9(r io.ReaderAt) (rawFile, error) {
f, err := plan9obj.NewFile(r) f, err := plan9obj.NewFile(r)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -106,7 +106,12 @@ func nm(file string) { ...@@ -106,7 +106,12 @@ func nm(file string) {
} }
defer f.Close() defer f.Close()
syms, err := f.Symbols() w := bufio.NewWriter(os.Stdout)
entries := f.Entries()
for _, e := range entries {
syms, err := e.Symbols()
if err != nil { if err != nil {
errorf("reading %s: %v", file, err) errorf("reading %s: %v", file, err)
} }
...@@ -123,9 +128,15 @@ func nm(file string) { ...@@ -123,9 +128,15 @@ func nm(file string) {
sort.Slice(syms, func(i, j int) bool { return syms[i].Size > syms[j].Size }) sort.Slice(syms, func(i, j int) bool { return syms[i].Size > syms[j].Size })
} }
w := bufio.NewWriter(os.Stdout)
for _, sym := range syms { for _, sym := range syms {
if filePrefix { if len(entries) > 1 {
name := e.Name()
if name == "" {
fmt.Fprintf(w, "%s(%s):\t", file, "_go_.o")
} else {
fmt.Fprintf(w, "%s(%s):\t", file, name)
}
} else if filePrefix {
fmt.Fprintf(w, "%s:\t", file) fmt.Fprintf(w, "%s:\t", file)
} }
if sym.Code == 'U' { if sym.Code == 'U' {
...@@ -142,5 +153,7 @@ func nm(file string) { ...@@ -142,5 +153,7 @@ func nm(file string) {
} }
fmt.Fprintf(w, "\n") fmt.Fprintf(w, "\n")
} }
}
w.Flush() w.Flush()
} }
...@@ -34,3 +34,14 @@ func TestInternalLinkerCgoExec(t *testing.T) { ...@@ -34,3 +34,14 @@ func TestInternalLinkerCgoExec(t *testing.T) {
func TestExternalLinkerCgoExec(t *testing.T) { func TestExternalLinkerCgoExec(t *testing.T) {
testGoExec(t, true, true) testGoExec(t, true, true)
} }
func TestCgoLib(t *testing.T) {
if runtime.GOARCH == "arm" {
switch runtime.GOOS {
case "darwin", "android", "nacl":
default:
t.Skip("skip test due to #19811")
}
}
testGoLib(t, true)
}
...@@ -161,7 +161,7 @@ func TestGoExec(t *testing.T) { ...@@ -161,7 +161,7 @@ func TestGoExec(t *testing.T) {
testGoExec(t, false, false) testGoExec(t, false, false)
} }
func testGoLib(t *testing.T) { func testGoLib(t *testing.T, iscgo bool) {
tmpdir, err := ioutil.TempDir("", "TestGoLib") tmpdir, err := ioutil.TempDir("", "TestGoLib")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -180,7 +180,7 @@ func testGoLib(t *testing.T) { ...@@ -180,7 +180,7 @@ func testGoLib(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = template.Must(template.New("mylib").Parse(testlib)).Execute(file, nil) err = template.Must(template.New("mylib").Parse(testlib)).Execute(file, iscgo)
if e := file.Close(); err == nil { if e := file.Close(); err == nil {
err = e err = e
} }
...@@ -212,23 +212,46 @@ func testGoLib(t *testing.T) { ...@@ -212,23 +212,46 @@ func testGoLib(t *testing.T) {
type symType struct { type symType struct {
Type string Type string
Name string Name string
CSym bool
Found bool Found bool
} }
var syms = []symType{ var syms = []symType{
{"B", "%22%22.Testdata", false}, {"B", "%22%22.Testdata", false, false},
{"T", "%22%22.Testfunc", false}, {"T", "%22%22.Testfunc", false, false},
}
if iscgo {
syms = append(syms, symType{"B", "%22%22.TestCgodata", false, false})
syms = append(syms, symType{"T", "%22%22.TestCgofunc", false, false})
if runtime.GOOS == "darwin" || (runtime.GOOS == "windows" && runtime.GOARCH == "386") {
syms = append(syms, symType{"D", "_cgodata", true, false})
syms = append(syms, symType{"T", "_cgofunc", true, false})
} else {
syms = append(syms, symType{"D", "cgodata", true, false})
syms = append(syms, symType{"T", "cgofunc", true, false})
}
} }
scanner := bufio.NewScanner(bytes.NewBuffer(out)) scanner := bufio.NewScanner(bytes.NewBuffer(out))
for scanner.Scan() { for scanner.Scan() {
f := strings.Fields(scanner.Text()) f := strings.Fields(scanner.Text())
var typ, name string
var csym bool
if iscgo {
if len(f) < 4 {
continue
}
csym = !strings.Contains(f[0], "_go_.o")
typ = f[2]
name = f[3]
} else {
if len(f) < 3 { if len(f) < 3 {
continue continue
} }
typ := f[1] typ = f[1]
name := f[2] name = f[2]
}
for i := range syms { for i := range syms {
sym := &syms[i] sym := &syms[i]
if sym.Type == typ && sym.Name == name { if sym.Type == typ && sym.Name == name && sym.CSym == csym {
if sym.Found { if sym.Found {
t.Fatalf("duplicate symbol %s %s", sym.Type, sym.Name) t.Fatalf("duplicate symbol %s %s", sym.Type, sym.Name)
} }
...@@ -248,7 +271,7 @@ func testGoLib(t *testing.T) { ...@@ -248,7 +271,7 @@ func testGoLib(t *testing.T) {
} }
func TestGoLib(t *testing.T) { func TestGoLib(t *testing.T) {
testGoLib(t) testGoLib(t, false)
} }
const testexec = ` const testexec = `
...@@ -274,6 +297,18 @@ func testfunc() { ...@@ -274,6 +297,18 @@ func testfunc() {
const testlib = ` const testlib = `
package mylib package mylib
{{if .}}
// int cgodata = 5;
// void cgofunc(void) {}
import "C"
var TestCgodata = C.cgodata
func TestCgofunc() {
C.cgofunc()
}
{{end}}
var Testdata uint32 var Testdata uint32
func Testfunc() {} func Testfunc() {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment