Home | History | Annotate | Download | only in objfile
      1 // Copyright 2013 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 Go intermediate object files and archives.
      6 
      7 package objfile
      8 
      9 import (
     10 	"cmd/internal/goobj"
     11 	"fmt"
     12 	"os"
     13 )
     14 
     15 type goobjFile struct {
     16 	goobj *goobj.Package
     17 }
     18 
     19 func openGoobj(r *os.File) (rawFile, error) {
     20 	f, err := goobj.Parse(r, `""`)
     21 	if err != nil {
     22 		return nil, err
     23 	}
     24 	return &goobjFile{f}, nil
     25 }
     26 
     27 func goobjName(id goobj.SymID) string {
     28 	if id.Version == 0 {
     29 		return id.Name
     30 	}
     31 	return fmt.Sprintf("%s<%d>", id.Name, id.Version)
     32 }
     33 
     34 func (f *goobjFile) symbols() ([]Sym, error) {
     35 	seen := make(map[goobj.SymID]bool)
     36 
     37 	var syms []Sym
     38 	for _, s := range f.goobj.Syms {
     39 		seen[s.SymID] = true
     40 		sym := Sym{Addr: uint64(s.Data.Offset), Name: goobjName(s.SymID), Size: int64(s.Size), Type: s.Type.Name, Code: '?'}
     41 		switch s.Kind {
     42 		case goobj.STEXT, goobj.SELFRXSECT:
     43 			sym.Code = 'T'
     44 		case goobj.STYPE, goobj.SSTRING, goobj.SGOSTRING, goobj.SGOFUNC, goobj.SRODATA, goobj.SFUNCTAB, goobj.STYPELINK, goobj.SSYMTAB, goobj.SPCLNTAB, goobj.SELFROSECT:
     45 			sym.Code = 'R'
     46 		case goobj.SMACHOPLT, goobj.SELFSECT, goobj.SMACHO, goobj.SMACHOGOT, goobj.SNOPTRDATA, goobj.SINITARR, goobj.SDATA, goobj.SWINDOWS:
     47 			sym.Code = 'D'
     48 		case goobj.SBSS, goobj.SNOPTRBSS, goobj.STLSBSS:
     49 			sym.Code = 'B'
     50 		case goobj.SXREF, goobj.SMACHOSYMSTR, goobj.SMACHOSYMTAB, goobj.SMACHOINDIRECTPLT, goobj.SMACHOINDIRECTGOT, goobj.SFILE, goobj.SFILEPATH, goobj.SCONST, goobj.SDYNIMPORT, goobj.SHOSTOBJ:
     51 			sym.Code = 'X' // should not see
     52 		}
     53 		if s.Version != 0 {
     54 			sym.Code += 'a' - 'A'
     55 		}
     56 		syms = append(syms, sym)
     57 	}
     58 
     59 	for _, s := range f.goobj.Syms {
     60 		for _, r := range s.Reloc {
     61 			if !seen[r.Sym] {
     62 				seen[r.Sym] = true
     63 				sym := Sym{Name: goobjName(r.Sym), Code: 'U'}
     64 				if s.Version != 0 {
     65 					// should not happen but handle anyway
     66 					sym.Code = 'u'
     67 				}
     68 				syms = append(syms, sym)
     69 			}
     70 		}
     71 	}
     72 
     73 	return syms, nil
     74 }
     75 
     76 // pcln does not make sense for Go object files, because each
     77 // symbol has its own individual pcln table, so there is no global
     78 // space of addresses to map.
     79 func (f *goobjFile) pcln() (textStart uint64, symtab, pclntab []byte, err error) {
     80 	return 0, nil, nil, fmt.Errorf("pcln not available in go object file")
     81 }
     82 
     83 // text does not make sense for Go object files, because
     84 // each function has a separate section.
     85 func (f *goobjFile) text() (textStart uint64, text []byte, err error) {
     86 	return 0, nil, fmt.Errorf("text not available in go object file")
     87 }
     88 
     89 // goarch makes sense but is not exposed in debug/goobj's API,
     90 // and we don't need it yet for any users of internal/objfile.
     91 func (f *goobjFile) goarch() string {
     92 	return "GOARCH unimplemented for debug/goobj files"
     93 }
     94