github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/cmd/objdump/plan9obj.go (about)

     1  // Copyright 2014 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  // Parsing of Plan 9 a.out executables.
     6  
     7  package main
     8  
     9  import (
    10  	"debug/plan9obj"
    11  	"os"
    12  	"sort"
    13  )
    14  
    15  var validSymType = map[rune]bool{
    16  	'T': true,
    17  	't': true,
    18  	'D': true,
    19  	'd': true,
    20  	'B': true,
    21  	'b': true,
    22  }
    23  
    24  func plan9Symbols(f *os.File) (syms []Sym, goarch string) {
    25  	p, err := plan9obj.NewFile(f)
    26  	if err != nil {
    27  		errorf("parsing %s: %v", f.Name(), err)
    28  		return
    29  	}
    30  
    31  	plan9Syms, err := p.Symbols()
    32  	if err != nil {
    33  		errorf("parsing %s: %v", f.Name(), err)
    34  		return
    35  	}
    36  
    37  	switch p.Magic {
    38  	case plan9obj.MagicAMD64:
    39  		goarch = "amd64"
    40  	case plan9obj.Magic386:
    41  		goarch = "386"
    42  	case plan9obj.MagicARM:
    43  		goarch = "arm"
    44  	}
    45  
    46  	// Build sorted list of addresses of all symbols.
    47  	// We infer the size of a symbol by looking at where the next symbol begins.
    48  	var addrs []uint64
    49  	for _, s := range plan9Syms {
    50  		if !validSymType[s.Type] {
    51  			continue
    52  		}
    53  		addrs = append(addrs, s.Value)
    54  	}
    55  	sort.Sort(uint64s(addrs))
    56  
    57  	for _, s := range plan9Syms {
    58  		if !validSymType[s.Type] {
    59  			continue
    60  		}
    61  		sym := Sym{Addr: s.Value, Name: s.Name, Code: rune(s.Type)}
    62  		i := sort.Search(len(addrs), func(x int) bool { return addrs[x] > s.Value })
    63  		if i < len(addrs) {
    64  			sym.Size = int64(addrs[i] - s.Value)
    65  		}
    66  		syms = append(syms, sym)
    67  	}
    68  
    69  	return
    70  }