github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/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  	goarch = "386"
    38  
    39  	// Build sorted list of addresses of all symbols.
    40  	// We infer the size of a symbol by looking at where the next symbol begins.
    41  	var addrs []uint64
    42  	for _, s := range plan9Syms {
    43  		if !validSymType[s.Type] {
    44  			continue
    45  		}
    46  		addrs = append(addrs, s.Value)
    47  	}
    48  	sort.Sort(uint64s(addrs))
    49  
    50  	for _, s := range plan9Syms {
    51  		if !validSymType[s.Type] {
    52  			continue
    53  		}
    54  		sym := Sym{Addr: s.Value, Name: s.Name, Code: rune(s.Type)}
    55  		i := sort.Search(len(addrs), func(x int) bool { return addrs[x] > s.Value })
    56  		if i < len(addrs) {
    57  			sym.Size = int64(addrs[i] - s.Value)
    58  		}
    59  		syms = append(syms, sym)
    60  	}
    61  
    62  	return
    63  }