github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/cmd/nm/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 func plan9Symbols(f *os.File) []Sym { 16 p, err := plan9obj.NewFile(f) 17 if err != nil { 18 errorf("parsing %s: %v", f.Name(), err) 19 return nil 20 } 21 22 plan9Syms, err := p.Symbols() 23 if err != nil { 24 errorf("parsing %s: %v", f.Name(), err) 25 return nil 26 } 27 28 // Build sorted list of addresses of all symbols. 29 // We infer the size of a symbol by looking at where the next symbol begins. 30 var addrs []uint64 31 for _, s := range plan9Syms { 32 addrs = append(addrs, s.Value) 33 } 34 sort.Sort(uint64s(addrs)) 35 36 var syms []Sym 37 38 for _, s := range plan9Syms { 39 sym := Sym{Addr: s.Value, Name: s.Name, Code: rune(s.Type)} 40 i := sort.Search(len(addrs), func(x int) bool { return addrs[x] > s.Value }) 41 if i < len(addrs) { 42 sym.Size = int64(addrs[i] - s.Value) 43 } 44 syms = append(syms, sym) 45 } 46 47 return syms 48 }