github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/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 dupok
    56  //	- size [int]
    57  //	- gotype [symref index]
    58  //	- p [data block]
    59  //	- nr [int]
    60  //	- r [nr relocations, sorted by off]
    61  //
    62  // If type == STEXT, there are a few more fields:
    63  //
    64  //	- args [int]
    65  //	- locals [int]
    66  //	- nosplit [int]
    67  //	- flags [int]
    68  //		1<<0 leaf
    69  //		1<<1 C function
    70  //		1<<2 function may call reflect.Type.Method
    71  //	- nlocal [int]
    72  //	- local [nlocal automatics]
    73  //	- pcln [pcln table]
    74  //
    75  // Each relocation has the encoding:
    76  //
    77  //	- off [int]
    78  //	- siz [int]
    79  //	- type [int]
    80  //	- add [int]
    81  //	- sym [symref index]
    82  //
    83  // Each local has the encoding:
    84  //
    85  //	- asym [symref index]
    86  //	- offset [int]
    87  //	- type [int]
    88  //	- gotype [symref index]
    89  //
    90  // The pcln table has the encoding:
    91  //
    92  //	- pcsp [data block]
    93  //	- pcfile [data block]
    94  //	- pcline [data block]
    95  //	- npcdata [int]
    96  //	- pcdata [npcdata data blocks]
    97  //	- nfuncdata [int]
    98  //	- funcdata [nfuncdata symref index]
    99  //	- funcdatasym [nfuncdata ints]
   100  //	- nfile [int]
   101  //	- file [nfile symref index]
   102  //
   103  // The file layout and meaning of type integers are architecture-independent.
   104  //
   105  // TODO(rsc): The file format is good for a first pass but needs work.
   106  //	- There are SymID in the object file that should really just be strings.
   107  
   108  package obj
   109  
   110  import (
   111  	"bufio"
   112  	"cmd/internal/sys"
   113  	"fmt"
   114  	"log"
   115  	"path/filepath"
   116  	"sort"
   117  )
   118  
   119  // The Go and C compilers, and the assembler, call writeobj to write
   120  // out a Go object file. The linker does not call this; the linker
   121  // does not write out object files.
   122  func Writeobjdirect(ctxt *Link, b *bufio.Writer) {
   123  	Flushplist(ctxt)
   124  	WriteObjFile(ctxt, b)
   125  }
   126  
   127  // objWriter writes Go object files.
   128  type objWriter struct {
   129  	wr   *bufio.Writer
   130  	ctxt *Link
   131  	// Temporary buffer for zigzag int writing.
   132  	varintbuf [10]uint8
   133  
   134  	// Provide the the index of a symbol reference by symbol name.
   135  	// One map for versioned symbols and one for unversioned symbols.
   136  	// Used for deduplicating the symbol reference list.
   137  	refIdx  map[string]int
   138  	vrefIdx map[string]int
   139  
   140  	// Number of objects written of each type.
   141  	nRefs     int
   142  	nData     int
   143  	nReloc    int
   144  	nPcdata   int
   145  	nAutom    int
   146  	nFuncdata int
   147  	nFile     int
   148  }
   149  
   150  func (w *objWriter) addLengths(s *LSym) {
   151  	w.nData += len(s.P)
   152  	w.nReloc += len(s.R)
   153  
   154  	if s.Type != STEXT {
   155  		return
   156  	}
   157  
   158  	pc := s.Pcln
   159  
   160  	data := 0
   161  	data += len(pc.Pcsp.P)
   162  	data += len(pc.Pcfile.P)
   163  	data += len(pc.Pcline.P)
   164  	for i := 0; i < len(pc.Pcdata); i++ {
   165  		data += len(pc.Pcdata[i].P)
   166  	}
   167  
   168  	w.nData += data
   169  	w.nPcdata += len(pc.Pcdata)
   170  
   171  	autom := 0
   172  	for a := s.Autom; a != nil; a = a.Link {
   173  		autom++
   174  	}
   175  	w.nAutom += autom
   176  	w.nFuncdata += len(pc.Funcdataoff)
   177  	w.nFile += len(pc.File)
   178  }
   179  
   180  func (w *objWriter) writeLengths() {
   181  	w.writeInt(int64(w.nData))
   182  	w.writeInt(int64(w.nReloc))
   183  	w.writeInt(int64(w.nPcdata))
   184  	w.writeInt(int64(w.nAutom))
   185  	w.writeInt(int64(w.nFuncdata))
   186  	w.writeInt(int64(w.nFile))
   187  }
   188  
   189  func newObjWriter(ctxt *Link, b *bufio.Writer) *objWriter {
   190  	return &objWriter{
   191  		ctxt:    ctxt,
   192  		wr:      b,
   193  		vrefIdx: make(map[string]int),
   194  		refIdx:  make(map[string]int),
   195  	}
   196  }
   197  
   198  func WriteObjFile(ctxt *Link, b *bufio.Writer) {
   199  	w := newObjWriter(ctxt, b)
   200  
   201  	// Magic header
   202  	w.wr.WriteString("\x00\x00go17ld")
   203  
   204  	// Version
   205  	w.wr.WriteByte(1)
   206  
   207  	// Autolib
   208  	for _, pkg := range ctxt.Imports {
   209  		w.writeString(pkg)
   210  	}
   211  	w.writeString("")
   212  
   213  	// Symbol references
   214  	for _, s := range ctxt.Text {
   215  		w.writeRefs(s)
   216  		w.addLengths(s)
   217  	}
   218  	for _, s := range ctxt.Data {
   219  		w.writeRefs(s)
   220  		w.addLengths(s)
   221  	}
   222  	// End symbol references
   223  	w.wr.WriteByte(0xff)
   224  
   225  	// Lengths
   226  	w.writeLengths()
   227  
   228  	// Data block
   229  	for _, s := range ctxt.Text {
   230  		w.wr.Write(s.P)
   231  		pc := s.Pcln
   232  		w.wr.Write(pc.Pcsp.P)
   233  		w.wr.Write(pc.Pcfile.P)
   234  		w.wr.Write(pc.Pcline.P)
   235  		for i := 0; i < len(pc.Pcdata); i++ {
   236  			w.wr.Write(pc.Pcdata[i].P)
   237  		}
   238  	}
   239  	for _, s := range ctxt.Data {
   240  		w.wr.Write(s.P)
   241  	}
   242  
   243  	// Symbols
   244  	for _, s := range ctxt.Text {
   245  		w.writeSym(s)
   246  	}
   247  	for _, s := range ctxt.Data {
   248  		w.writeSym(s)
   249  	}
   250  
   251  	// Magic footer
   252  	w.wr.WriteString("\xff\xffgo17ld")
   253  }
   254  
   255  // Symbols are prefixed so their content doesn't get confused with the magic footer.
   256  const symPrefix = 0xfe
   257  
   258  func (w *objWriter) writeRef(s *LSym, isPath bool) {
   259  	if s == nil || s.RefIdx != 0 {
   260  		return
   261  	}
   262  	var m map[string]int
   263  	switch s.Version {
   264  	case 0:
   265  		m = w.refIdx
   266  	case 1:
   267  		m = w.vrefIdx
   268  	default:
   269  		log.Fatalf("%s: invalid version number %d", s.Name, s.Version)
   270  	}
   271  
   272  	idx := m[s.Name]
   273  	if idx != 0 {
   274  		s.RefIdx = idx
   275  		return
   276  	}
   277  	w.wr.WriteByte(symPrefix)
   278  	if isPath {
   279  		w.writeString(filepath.ToSlash(s.Name))
   280  	} else {
   281  		w.writeString(s.Name)
   282  	}
   283  	w.writeInt(int64(s.Version))
   284  	w.nRefs++
   285  	s.RefIdx = w.nRefs
   286  	m[s.Name] = w.nRefs
   287  }
   288  
   289  func (w *objWriter) writeRefs(s *LSym) {
   290  	w.writeRef(s, false)
   291  	w.writeRef(s.Gotype, false)
   292  	for i := range s.R {
   293  		w.writeRef(s.R[i].Sym, false)
   294  	}
   295  
   296  	if s.Type == STEXT {
   297  		for a := s.Autom; a != nil; a = a.Link {
   298  			w.writeRef(a.Asym, false)
   299  			w.writeRef(a.Gotype, false)
   300  		}
   301  		pc := s.Pcln
   302  		for _, d := range pc.Funcdata {
   303  			w.writeRef(d, false)
   304  		}
   305  		for _, f := range pc.File {
   306  			w.writeRef(f, true)
   307  		}
   308  	}
   309  }
   310  
   311  func (w *objWriter) writeSymDebug(s *LSym) {
   312  	ctxt := w.ctxt
   313  	fmt.Fprintf(ctxt.Bso, "%s ", s.Name)
   314  	if s.Version != 0 {
   315  		fmt.Fprintf(ctxt.Bso, "v=%d ", s.Version)
   316  	}
   317  	if s.Type != 0 {
   318  		fmt.Fprintf(ctxt.Bso, "t=%d ", s.Type)
   319  	}
   320  	if s.Dupok {
   321  		fmt.Fprintf(ctxt.Bso, "dupok ")
   322  	}
   323  	if s.Cfunc {
   324  		fmt.Fprintf(ctxt.Bso, "cfunc ")
   325  	}
   326  	if s.Nosplit {
   327  		fmt.Fprintf(ctxt.Bso, "nosplit ")
   328  	}
   329  	fmt.Fprintf(ctxt.Bso, "size=%d", s.Size)
   330  	if s.Type == STEXT {
   331  		fmt.Fprintf(ctxt.Bso, " args=%#x locals=%#x", uint64(s.Args), uint64(s.Locals))
   332  		if s.Leaf {
   333  			fmt.Fprintf(ctxt.Bso, " leaf")
   334  		}
   335  	}
   336  
   337  	fmt.Fprintf(ctxt.Bso, "\n")
   338  	for p := s.Text; p != nil; p = p.Link {
   339  		fmt.Fprintf(ctxt.Bso, "\t%#04x %v\n", uint(int(p.Pc)), p)
   340  	}
   341  	var c int
   342  	var j int
   343  	for i := 0; i < len(s.P); {
   344  		fmt.Fprintf(ctxt.Bso, "\t%#04x", uint(i))
   345  		for j = i; j < i+16 && j < len(s.P); j++ {
   346  			fmt.Fprintf(ctxt.Bso, " %02x", s.P[j])
   347  		}
   348  		for ; j < i+16; j++ {
   349  			fmt.Fprintf(ctxt.Bso, "   ")
   350  		}
   351  		fmt.Fprintf(ctxt.Bso, "  ")
   352  		for j = i; j < i+16 && j < len(s.P); j++ {
   353  			c = int(s.P[j])
   354  			if ' ' <= c && c <= 0x7e {
   355  				fmt.Fprintf(ctxt.Bso, "%c", c)
   356  			} else {
   357  				fmt.Fprintf(ctxt.Bso, ".")
   358  			}
   359  		}
   360  
   361  		fmt.Fprintf(ctxt.Bso, "\n")
   362  		i += 16
   363  	}
   364  
   365  	sort.Sort(relocByOff(s.R)) // generate stable output
   366  	for _, r := range s.R {
   367  		name := ""
   368  		if r.Sym != nil {
   369  			name = r.Sym.Name
   370  		} else if r.Type == R_TLS_LE {
   371  			name = "TLS"
   372  		}
   373  		if ctxt.Arch.InFamily(sys.ARM, sys.PPC64) {
   374  			fmt.Fprintf(ctxt.Bso, "\trel %d+%d t=%d %s+%x\n", int(r.Off), r.Siz, r.Type, name, uint64(r.Add))
   375  		} else {
   376  			fmt.Fprintf(ctxt.Bso, "\trel %d+%d t=%d %s+%d\n", int(r.Off), r.Siz, r.Type, name, r.Add)
   377  		}
   378  	}
   379  }
   380  
   381  func (w *objWriter) writeSym(s *LSym) {
   382  	ctxt := w.ctxt
   383  	if ctxt.Debugasm != 0 {
   384  		w.writeSymDebug(s)
   385  	}
   386  
   387  	w.wr.WriteByte(symPrefix)
   388  	w.writeInt(int64(s.Type))
   389  	w.writeRefIndex(s)
   390  	flags := int64(0)
   391  	if s.Dupok {
   392  		flags |= 1
   393  	}
   394  	if s.Local {
   395  		flags |= 1 << 1
   396  	}
   397  	w.writeInt(flags)
   398  	w.writeInt(s.Size)
   399  	w.writeRefIndex(s.Gotype)
   400  	w.writeInt(int64(len(s.P)))
   401  
   402  	w.writeInt(int64(len(s.R)))
   403  	var r *Reloc
   404  	for i := 0; i < len(s.R); i++ {
   405  		r = &s.R[i]
   406  		w.writeInt(int64(r.Off))
   407  		w.writeInt(int64(r.Siz))
   408  		w.writeInt(int64(r.Type))
   409  		w.writeInt(r.Add)
   410  		w.writeRefIndex(r.Sym)
   411  	}
   412  
   413  	if s.Type != STEXT {
   414  		return
   415  	}
   416  
   417  	w.writeInt(int64(s.Args))
   418  	w.writeInt(int64(s.Locals))
   419  	if s.Nosplit {
   420  		w.writeInt(1)
   421  	} else {
   422  		w.writeInt(0)
   423  	}
   424  	flags = int64(0)
   425  	if s.Leaf {
   426  		flags |= 1
   427  	}
   428  	if s.Cfunc {
   429  		flags |= 1 << 1
   430  	}
   431  	if s.ReflectMethod {
   432  		flags |= 1 << 2
   433  	}
   434  	w.writeInt(flags)
   435  	n := 0
   436  	for a := s.Autom; a != nil; a = a.Link {
   437  		n++
   438  	}
   439  	w.writeInt(int64(n))
   440  	for a := s.Autom; a != nil; a = a.Link {
   441  		w.writeRefIndex(a.Asym)
   442  		w.writeInt(int64(a.Aoffset))
   443  		if a.Name == NAME_AUTO {
   444  			w.writeInt(A_AUTO)
   445  		} else if a.Name == NAME_PARAM {
   446  			w.writeInt(A_PARAM)
   447  		} else {
   448  			log.Fatalf("%s: invalid local variable type %d", s.Name, a.Name)
   449  		}
   450  		w.writeRefIndex(a.Gotype)
   451  	}
   452  
   453  	pc := s.Pcln
   454  	w.writeInt(int64(len(pc.Pcsp.P)))
   455  	w.writeInt(int64(len(pc.Pcfile.P)))
   456  	w.writeInt(int64(len(pc.Pcline.P)))
   457  	w.writeInt(int64(len(pc.Pcdata)))
   458  	for i := 0; i < len(pc.Pcdata); i++ {
   459  		w.writeInt(int64(len(pc.Pcdata[i].P)))
   460  	}
   461  	w.writeInt(int64(len(pc.Funcdataoff)))
   462  	for i := 0; i < len(pc.Funcdataoff); i++ {
   463  		w.writeRefIndex(pc.Funcdata[i])
   464  	}
   465  	for i := 0; i < len(pc.Funcdataoff); i++ {
   466  		w.writeInt(pc.Funcdataoff[i])
   467  	}
   468  	w.writeInt(int64(len(pc.File)))
   469  	for _, f := range pc.File {
   470  		w.writeRefIndex(f)
   471  	}
   472  }
   473  
   474  func (w *objWriter) writeInt(sval int64) {
   475  	var v uint64
   476  	uv := (uint64(sval) << 1) ^ uint64(sval>>63)
   477  	p := w.varintbuf[:]
   478  	for v = uv; v >= 0x80; v >>= 7 {
   479  		p[0] = uint8(v | 0x80)
   480  		p = p[1:]
   481  	}
   482  	p[0] = uint8(v)
   483  	p = p[1:]
   484  	w.wr.Write(w.varintbuf[:len(w.varintbuf)-len(p)])
   485  }
   486  
   487  func (w *objWriter) writeString(s string) {
   488  	w.writeInt(int64(len(s)))
   489  	w.wr.WriteString(s)
   490  }
   491  
   492  func (w *objWriter) writeRefIndex(s *LSym) {
   493  	if s == nil {
   494  		w.writeInt(0)
   495  		return
   496  	}
   497  	if s.RefIdx == 0 {
   498  		log.Fatalln("writing an unreferenced symbol", s.Name)
   499  	}
   500  	w.writeInt(int64(s.RefIdx))
   501  }
   502  
   503  // relocByOff sorts relocations by their offsets.
   504  type relocByOff []Reloc
   505  
   506  func (x relocByOff) Len() int           { return len(x) }
   507  func (x relocByOff) Less(i, j int) bool { return x[i].Off < x[j].Off }
   508  func (x relocByOff) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }