github.com/FenixAra/go@v0.0.0-20170127160404-96ea0918e670/src/cmd/internal/obj/objfile.go (about)

     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  // Writing of Go object files.
     6  //
     7  // Originally, Go object files were Plan 9 object files, but no longer.
     8  // Now they are more like standard object files, in that each symbol is defined
     9  // by an associated memory image (bytes) and a list of relocations to apply
    10  // during linking. We do not (yet?) use a standard file format, however.
    11  // For now, the format is chosen to be as simple as possible to read and write.
    12  // It may change for reasons of efficiency, or we may even switch to a
    13  // standard file format if there are compelling benefits to doing so.
    14  // See golang.org/s/go13linker for more background.
    15  //
    16  // The file format is:
    17  //
    18  //	- magic header: "\x00\x00go17ld"
    19  //	- byte 1 - version number
    20  //	- sequence of strings giving dependencies (imported packages)
    21  //	- empty string (marks end of sequence)
    22  //	- sequence of symbol references used by the defined symbols
    23  //	- byte 0xff (marks end of sequence)
    24  //	- sequence of integer lengths:
    25  //		- total data length
    26  //		- total number of relocations
    27  //		- total number of pcdata
    28  //		- total number of automatics
    29  //		- total number of funcdata
    30  //		- total number of files
    31  //	- data, the content of the defined symbols
    32  //	- sequence of defined symbols
    33  //	- byte 0xff (marks end of sequence)
    34  //	- magic footer: "\xff\xffgo17ld"
    35  //
    36  // All integers are stored in a zigzag varint format.
    37  // See golang.org/s/go12symtab for a definition.
    38  //
    39  // Data blocks and strings are both stored as an integer
    40  // followed by that many bytes.
    41  //
    42  // A symbol reference is a string name followed by a version.
    43  //
    44  // A symbol points to other symbols using an index into the symbol
    45  // reference sequence. Index 0 corresponds to a nil LSym* pointer.
    46  // In the symbol layout described below "symref index" stands for this
    47  // index.
    48  //
    49  // Each symbol is laid out as the following fields (taken from LSym*):
    50  //
    51  //	- byte 0xfe (sanity check for synchronization)
    52  //	- type [int]
    53  //	- name & version [symref index]
    54  //	- flags [int]
    55  //		1<<0 dupok
    56  //		1<<1 local
    57  //		1<<2 add to typelink table
    58  //	- size [int]
    59  //	- gotype [symref index]
    60  //	- p [data block]
    61  //	- nr [int]
    62  //	- r [nr relocations, sorted by off]
    63  //
    64  // If type == STEXT, there are a few more fields:
    65  //
    66  //	- args [int]
    67  //	- locals [int]
    68  //	- nosplit [int]
    69  //	- flags [int]
    70  //		1<<0 leaf
    71  //		1<<1 C function
    72  //		1<<2 function may call reflect.Type.Method
    73  //	- nlocal [int]
    74  //	- local [nlocal automatics]
    75  //	- pcln [pcln table]
    76  //
    77  // Each relocation has the encoding:
    78  //
    79  //	- off [int]
    80  //	- siz [int]
    81  //	- type [int]
    82  //	- add [int]
    83  //	- sym [symref index]
    84  //
    85  // Each local has the encoding:
    86  //
    87  //	- asym [symref index]
    88  //	- offset [int]
    89  //	- type [int]
    90  //	- gotype [symref index]
    91  //
    92  // The pcln table has the encoding:
    93  //
    94  //	- pcsp [data block]
    95  //	- pcfile [data block]
    96  //	- pcline [data block]
    97  //	- npcdata [int]
    98  //	- pcdata [npcdata data blocks]
    99  //	- nfuncdata [int]
   100  //	- funcdata [nfuncdata symref index]
   101  //	- funcdatasym [nfuncdata ints]
   102  //	- nfile [int]
   103  //	- file [nfile symref index]
   104  //
   105  // The file layout and meaning of type integers are architecture-independent.
   106  //
   107  // TODO(rsc): The file format is good for a first pass but needs work.
   108  //	- There are SymID in the object file that should really just be strings.
   109  
   110  package obj
   111  
   112  import (
   113  	"bufio"
   114  	"cmd/internal/dwarf"
   115  	"cmd/internal/sys"
   116  	"fmt"
   117  	"log"
   118  	"path/filepath"
   119  	"sort"
   120  )
   121  
   122  // The Go and C compilers, and the assembler, call writeobj to write
   123  // out a Go object file. The linker does not call this; the linker
   124  // does not write out object files.
   125  func Writeobjdirect(ctxt *Link, b *bufio.Writer) {
   126  	Flushplist(ctxt)
   127  	WriteObjFile(ctxt, b)
   128  }
   129  
   130  // objWriter writes Go object files.
   131  type objWriter struct {
   132  	wr   *bufio.Writer
   133  	ctxt *Link
   134  	// Temporary buffer for zigzag int writing.
   135  	varintbuf [10]uint8
   136  
   137  	// Provide the the index of a symbol reference by symbol name.
   138  	// One map for versioned symbols and one for unversioned symbols.
   139  	// Used for deduplicating the symbol reference list.
   140  	refIdx  map[string]int
   141  	vrefIdx map[string]int
   142  
   143  	// Number of objects written of each type.
   144  	nRefs     int
   145  	nData     int
   146  	nReloc    int
   147  	nPcdata   int
   148  	nAutom    int
   149  	nFuncdata int
   150  	nFile     int
   151  }
   152  
   153  func (w *objWriter) addLengths(s *LSym) {
   154  	w.nData += len(s.P)
   155  	w.nReloc += len(s.R)
   156  
   157  	if s.Type != STEXT {
   158  		return
   159  	}
   160  
   161  	pc := s.Pcln
   162  
   163  	data := 0
   164  	data += len(pc.Pcsp.P)
   165  	data += len(pc.Pcfile.P)
   166  	data += len(pc.Pcline.P)
   167  	for i := 0; i < len(pc.Pcdata); i++ {
   168  		data += len(pc.Pcdata[i].P)
   169  	}
   170  
   171  	w.nData += data
   172  	w.nPcdata += len(pc.Pcdata)
   173  
   174  	autom := 0
   175  	for a := s.Autom; a != nil; a = a.Link {
   176  		autom++
   177  	}
   178  	w.nAutom += autom
   179  	w.nFuncdata += len(pc.Funcdataoff)
   180  	w.nFile += len(pc.File)
   181  }
   182  
   183  func (w *objWriter) writeLengths() {
   184  	w.writeInt(int64(w.nData))
   185  	w.writeInt(int64(w.nReloc))
   186  	w.writeInt(int64(w.nPcdata))
   187  	w.writeInt(int64(w.nAutom))
   188  	w.writeInt(int64(w.nFuncdata))
   189  	w.writeInt(int64(w.nFile))
   190  }
   191  
   192  func newObjWriter(ctxt *Link, b *bufio.Writer) *objWriter {
   193  	return &objWriter{
   194  		ctxt:    ctxt,
   195  		wr:      b,
   196  		vrefIdx: make(map[string]int),
   197  		refIdx:  make(map[string]int),
   198  	}
   199  }
   200  
   201  func WriteObjFile(ctxt *Link, b *bufio.Writer) {
   202  	w := newObjWriter(ctxt, b)
   203  
   204  	// Magic header
   205  	w.wr.WriteString("\x00\x00go17ld")
   206  
   207  	// Version
   208  	w.wr.WriteByte(1)
   209  
   210  	// Autolib
   211  	for _, pkg := range ctxt.Imports {
   212  		w.writeString(pkg)
   213  	}
   214  	w.writeString("")
   215  
   216  	// Symbol references
   217  	for _, s := range ctxt.Text {
   218  		w.writeRefs(s)
   219  		w.addLengths(s)
   220  	}
   221  	for _, s := range ctxt.Data {
   222  		w.writeRefs(s)
   223  		w.addLengths(s)
   224  	}
   225  	// End symbol references
   226  	w.wr.WriteByte(0xff)
   227  
   228  	// Lengths
   229  	w.writeLengths()
   230  
   231  	// Data block
   232  	for _, s := range ctxt.Text {
   233  		w.wr.Write(s.P)
   234  		pc := s.Pcln
   235  		w.wr.Write(pc.Pcsp.P)
   236  		w.wr.Write(pc.Pcfile.P)
   237  		w.wr.Write(pc.Pcline.P)
   238  		for i := 0; i < len(pc.Pcdata); i++ {
   239  			w.wr.Write(pc.Pcdata[i].P)
   240  		}
   241  	}
   242  	for _, s := range ctxt.Data {
   243  		w.wr.Write(s.P)
   244  	}
   245  
   246  	// Symbols
   247  	for _, s := range ctxt.Text {
   248  		w.writeSym(s)
   249  	}
   250  	for _, s := range ctxt.Data {
   251  		w.writeSym(s)
   252  	}
   253  
   254  	// Magic footer
   255  	w.wr.WriteString("\xff\xffgo17ld")
   256  }
   257  
   258  // Symbols are prefixed so their content doesn't get confused with the magic footer.
   259  const symPrefix = 0xfe
   260  
   261  func (w *objWriter) writeRef(s *LSym, isPath bool) {
   262  	if s == nil || s.RefIdx != 0 {
   263  		return
   264  	}
   265  	var m map[string]int
   266  	switch s.Version {
   267  	case 0:
   268  		m = w.refIdx
   269  	case 1:
   270  		m = w.vrefIdx
   271  	default:
   272  		log.Fatalf("%s: invalid version number %d", s.Name, s.Version)
   273  	}
   274  
   275  	idx := m[s.Name]
   276  	if idx != 0 {
   277  		s.RefIdx = idx
   278  		return
   279  	}
   280  	w.wr.WriteByte(symPrefix)
   281  	if isPath {
   282  		w.writeString(filepath.ToSlash(s.Name))
   283  	} else {
   284  		w.writeString(s.Name)
   285  	}
   286  	w.writeInt(int64(s.Version))
   287  	w.nRefs++
   288  	s.RefIdx = w.nRefs
   289  	m[s.Name] = w.nRefs
   290  }
   291  
   292  func (w *objWriter) writeRefs(s *LSym) {
   293  	w.writeRef(s, false)
   294  	w.writeRef(s.Gotype, false)
   295  	for i := range s.R {
   296  		w.writeRef(s.R[i].Sym, false)
   297  	}
   298  
   299  	if s.Type == STEXT {
   300  		for a := s.Autom; a != nil; a = a.Link {
   301  			w.writeRef(a.Asym, false)
   302  			w.writeRef(a.Gotype, false)
   303  		}
   304  		pc := s.Pcln
   305  		for _, d := range pc.Funcdata {
   306  			w.writeRef(d, false)
   307  		}
   308  		for _, f := range pc.File {
   309  			w.writeRef(f, true)
   310  		}
   311  	}
   312  }
   313  
   314  func (w *objWriter) writeSymDebug(s *LSym) {
   315  	ctxt := w.ctxt
   316  	fmt.Fprintf(ctxt.Bso, "%s ", s.Name)
   317  	if s.Version != 0 {
   318  		fmt.Fprintf(ctxt.Bso, "v=%d ", s.Version)
   319  	}
   320  	if s.Type != 0 {
   321  		fmt.Fprintf(ctxt.Bso, "t=%d ", s.Type)
   322  	}
   323  	if s.DuplicateOK() {
   324  		fmt.Fprintf(ctxt.Bso, "dupok ")
   325  	}
   326  	if s.CFunc() {
   327  		fmt.Fprintf(ctxt.Bso, "cfunc ")
   328  	}
   329  	if s.NoSplit() {
   330  		fmt.Fprintf(ctxt.Bso, "nosplit ")
   331  	}
   332  	fmt.Fprintf(ctxt.Bso, "size=%d", s.Size)
   333  	if s.Type == STEXT {
   334  		fmt.Fprintf(ctxt.Bso, " args=%#x locals=%#x", uint64(s.Args), uint64(s.Locals))
   335  		if s.Leaf() {
   336  			fmt.Fprintf(ctxt.Bso, " leaf")
   337  		}
   338  	}
   339  
   340  	fmt.Fprintf(ctxt.Bso, "\n")
   341  	for p := s.Text; p != nil; p = p.Link {
   342  		fmt.Fprintf(ctxt.Bso, "\t%#04x %v\n", uint(int(p.Pc)), p)
   343  	}
   344  	var c int
   345  	var j int
   346  	for i := 0; i < len(s.P); {
   347  		fmt.Fprintf(ctxt.Bso, "\t%#04x", uint(i))
   348  		for j = i; j < i+16 && j < len(s.P); j++ {
   349  			fmt.Fprintf(ctxt.Bso, " %02x", s.P[j])
   350  		}
   351  		for ; j < i+16; j++ {
   352  			fmt.Fprintf(ctxt.Bso, "   ")
   353  		}
   354  		fmt.Fprintf(ctxt.Bso, "  ")
   355  		for j = i; j < i+16 && j < len(s.P); j++ {
   356  			c = int(s.P[j])
   357  			if ' ' <= c && c <= 0x7e {
   358  				fmt.Fprintf(ctxt.Bso, "%c", c)
   359  			} else {
   360  				fmt.Fprintf(ctxt.Bso, ".")
   361  			}
   362  		}
   363  
   364  		fmt.Fprintf(ctxt.Bso, "\n")
   365  		i += 16
   366  	}
   367  
   368  	sort.Sort(relocByOff(s.R)) // generate stable output
   369  	for _, r := range s.R {
   370  		name := ""
   371  		if r.Sym != nil {
   372  			name = r.Sym.Name
   373  		} else if r.Type == R_TLS_LE {
   374  			name = "TLS"
   375  		}
   376  		if ctxt.Arch.InFamily(sys.ARM, sys.PPC64) {
   377  			fmt.Fprintf(ctxt.Bso, "\trel %d+%d t=%d %s+%x\n", int(r.Off), r.Siz, r.Type, name, uint64(r.Add))
   378  		} else {
   379  			fmt.Fprintf(ctxt.Bso, "\trel %d+%d t=%d %s+%d\n", int(r.Off), r.Siz, r.Type, name, r.Add)
   380  		}
   381  	}
   382  }
   383  
   384  func (w *objWriter) writeSym(s *LSym) {
   385  	ctxt := w.ctxt
   386  	if ctxt.Debugasm != 0 {
   387  		w.writeSymDebug(s)
   388  	}
   389  
   390  	w.wr.WriteByte(symPrefix)
   391  	w.writeInt(int64(s.Type))
   392  	w.writeRefIndex(s)
   393  	flags := int64(0)
   394  	if s.DuplicateOK() {
   395  		flags |= 1
   396  	}
   397  	if s.Local() {
   398  		flags |= 1 << 1
   399  	}
   400  	if s.MakeTypelink() {
   401  		flags |= 1 << 2
   402  	}
   403  	w.writeInt(flags)
   404  	w.writeInt(s.Size)
   405  	w.writeRefIndex(s.Gotype)
   406  	w.writeInt(int64(len(s.P)))
   407  
   408  	w.writeInt(int64(len(s.R)))
   409  	var r *Reloc
   410  	for i := 0; i < len(s.R); i++ {
   411  		r = &s.R[i]
   412  		w.writeInt(int64(r.Off))
   413  		w.writeInt(int64(r.Siz))
   414  		w.writeInt(int64(r.Type))
   415  		w.writeInt(r.Add)
   416  		w.writeRefIndex(r.Sym)
   417  	}
   418  
   419  	if s.Type != STEXT {
   420  		return
   421  	}
   422  
   423  	w.writeInt(int64(s.Args))
   424  	w.writeInt(int64(s.Locals))
   425  	if s.NoSplit() {
   426  		w.writeInt(1)
   427  	} else {
   428  		w.writeInt(0)
   429  	}
   430  	flags = int64(0)
   431  	if s.Leaf() {
   432  		flags |= 1
   433  	}
   434  	if s.CFunc() {
   435  		flags |= 1 << 1
   436  	}
   437  	if s.ReflectMethod() {
   438  		flags |= 1 << 2
   439  	}
   440  	w.writeInt(flags)
   441  	n := 0
   442  	for a := s.Autom; a != nil; a = a.Link {
   443  		n++
   444  	}
   445  	w.writeInt(int64(n))
   446  	for a := s.Autom; a != nil; a = a.Link {
   447  		w.writeRefIndex(a.Asym)
   448  		w.writeInt(int64(a.Aoffset))
   449  		if a.Name == NAME_AUTO {
   450  			w.writeInt(A_AUTO)
   451  		} else if a.Name == NAME_PARAM {
   452  			w.writeInt(A_PARAM)
   453  		} else {
   454  			log.Fatalf("%s: invalid local variable type %d", s.Name, a.Name)
   455  		}
   456  		w.writeRefIndex(a.Gotype)
   457  	}
   458  
   459  	pc := s.Pcln
   460  	w.writeInt(int64(len(pc.Pcsp.P)))
   461  	w.writeInt(int64(len(pc.Pcfile.P)))
   462  	w.writeInt(int64(len(pc.Pcline.P)))
   463  	w.writeInt(int64(len(pc.Pcdata)))
   464  	for i := 0; i < len(pc.Pcdata); i++ {
   465  		w.writeInt(int64(len(pc.Pcdata[i].P)))
   466  	}
   467  	w.writeInt(int64(len(pc.Funcdataoff)))
   468  	for i := 0; i < len(pc.Funcdataoff); i++ {
   469  		w.writeRefIndex(pc.Funcdata[i])
   470  	}
   471  	for i := 0; i < len(pc.Funcdataoff); i++ {
   472  		w.writeInt(pc.Funcdataoff[i])
   473  	}
   474  	w.writeInt(int64(len(pc.File)))
   475  	for _, f := range pc.File {
   476  		w.writeRefIndex(f)
   477  	}
   478  }
   479  
   480  func (w *objWriter) writeInt(sval int64) {
   481  	var v uint64
   482  	uv := (uint64(sval) << 1) ^ uint64(sval>>63)
   483  	p := w.varintbuf[:]
   484  	for v = uv; v >= 0x80; v >>= 7 {
   485  		p[0] = uint8(v | 0x80)
   486  		p = p[1:]
   487  	}
   488  	p[0] = uint8(v)
   489  	p = p[1:]
   490  	w.wr.Write(w.varintbuf[:len(w.varintbuf)-len(p)])
   491  }
   492  
   493  func (w *objWriter) writeString(s string) {
   494  	w.writeInt(int64(len(s)))
   495  	w.wr.WriteString(s)
   496  }
   497  
   498  func (w *objWriter) writeRefIndex(s *LSym) {
   499  	if s == nil {
   500  		w.writeInt(0)
   501  		return
   502  	}
   503  	if s.RefIdx == 0 {
   504  		log.Fatalln("writing an unreferenced symbol", s.Name)
   505  	}
   506  	w.writeInt(int64(s.RefIdx))
   507  }
   508  
   509  // relocByOff sorts relocations by their offsets.
   510  type relocByOff []Reloc
   511  
   512  func (x relocByOff) Len() int           { return len(x) }
   513  func (x relocByOff) Less(i, j int) bool { return x[i].Off < x[j].Off }
   514  func (x relocByOff) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
   515  
   516  // implement dwarf.Context
   517  type dwCtxt struct{ *Link }
   518  
   519  func (c dwCtxt) PtrSize() int {
   520  	return c.Arch.PtrSize
   521  }
   522  func (c dwCtxt) AddInt(s dwarf.Sym, size int, i int64) {
   523  	ls := s.(*LSym)
   524  	ls.WriteInt(c.Link, ls.Size, size, i)
   525  }
   526  func (c dwCtxt) AddBytes(s dwarf.Sym, b []byte) {
   527  	ls := s.(*LSym)
   528  	ls.WriteBytes(c.Link, ls.Size, b)
   529  }
   530  func (c dwCtxt) AddString(s dwarf.Sym, v string) {
   531  	ls := s.(*LSym)
   532  	ls.WriteString(c.Link, ls.Size, len(v), v)
   533  	ls.WriteInt(c.Link, ls.Size, 1, 0)
   534  }
   535  func (c dwCtxt) SymValue(s dwarf.Sym) int64 {
   536  	return 0
   537  }
   538  func (c dwCtxt) AddAddress(s dwarf.Sym, data interface{}, value int64) {
   539  	rsym := data.(*LSym)
   540  	ls := s.(*LSym)
   541  	size := c.PtrSize()
   542  	ls.WriteAddr(c.Link, ls.Size, size, rsym, value)
   543  }
   544  func (c dwCtxt) AddSectionOffset(s dwarf.Sym, size int, t interface{}, ofs int64) {
   545  	ls := s.(*LSym)
   546  	rsym := t.(*LSym)
   547  	ls.WriteAddr(c.Link, ls.Size, size, rsym, ofs)
   548  	r := &ls.R[len(ls.R)-1]
   549  	r.Type = R_DWARFREF
   550  }
   551  
   552  func gendwarf(ctxt *Link, text []*LSym) []*LSym {
   553  	dctxt := dwCtxt{ctxt}
   554  	var dw []*LSym
   555  
   556  	for _, s := range text {
   557  		dsym := Linklookup(ctxt, dwarf.InfoPrefix+s.Name, int(s.Version))
   558  		if dsym.Size != 0 {
   559  			continue
   560  		}
   561  		dw = append(dw, dsym)
   562  		dsym.Type = SDWARFINFO
   563  		dsym.Set(AttrDuplicateOK, s.DuplicateOK())
   564  		var vars dwarf.Var
   565  		var abbrev int
   566  		var offs int32
   567  		for a := s.Autom; a != nil; a = a.Link {
   568  			switch a.Name {
   569  			case NAME_AUTO:
   570  				abbrev = dwarf.DW_ABRV_AUTO
   571  				offs = a.Aoffset
   572  				if ctxt.FixedFrameSize() == 0 {
   573  					offs -= int32(ctxt.Arch.PtrSize)
   574  				}
   575  				if Framepointer_enabled(GOOS, GOARCH) {
   576  					offs -= int32(ctxt.Arch.PtrSize)
   577  				}
   578  
   579  			case NAME_PARAM:
   580  				abbrev = dwarf.DW_ABRV_PARAM
   581  				offs = a.Aoffset + int32(ctxt.FixedFrameSize())
   582  
   583  			default:
   584  				continue
   585  			}
   586  			typename := dwarf.InfoPrefix + a.Gotype.Name[len("type."):]
   587  			dwvar := &dwarf.Var{
   588  				Name:   a.Asym.Name,
   589  				Abbrev: abbrev,
   590  				Offset: int32(offs),
   591  				Type:   Linklookup(ctxt, typename, 0),
   592  			}
   593  			dws := &vars.Link
   594  			for ; *dws != nil; dws = &(*dws).Link {
   595  				if offs <= (*dws).Offset {
   596  					break
   597  				}
   598  			}
   599  			dwvar.Link = *dws
   600  			*dws = dwvar
   601  		}
   602  		dwarf.PutFunc(dctxt, dsym, s.Name, s.Version == 0, s, s.Size, vars.Link)
   603  	}
   604  	return dw
   605  }