...
Run Format

Source file src/cmd/link/internal/ld/pcln.go

Documentation: cmd/link/internal/ld

     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  package ld
     6  
     7  import (
     8  	"cmd/internal/objabi"
     9  	"cmd/internal/src"
    10  	"cmd/link/internal/sym"
    11  	"log"
    12  	"os"
    13  	"path/filepath"
    14  	"strings"
    15  )
    16  
    17  // iteration over encoded pcdata tables.
    18  
    19  func getvarint(pp *[]byte) uint32 {
    20  	v := uint32(0)
    21  	p := *pp
    22  	for shift := 0; ; shift += 7 {
    23  		v |= uint32(p[0]&0x7F) << uint(shift)
    24  		tmp4 := p
    25  		p = p[1:]
    26  		if tmp4[0]&0x80 == 0 {
    27  			break
    28  		}
    29  	}
    30  
    31  	*pp = p
    32  	return v
    33  }
    34  
    35  func pciternext(it *Pciter) {
    36  	it.pc = it.nextpc
    37  	if it.done != 0 {
    38  		return
    39  	}
    40  	if -cap(it.p) >= -cap(it.d.P[len(it.d.P):]) {
    41  		it.done = 1
    42  		return
    43  	}
    44  
    45  	// value delta
    46  	v := getvarint(&it.p)
    47  
    48  	if v == 0 && it.start == 0 {
    49  		it.done = 1
    50  		return
    51  	}
    52  
    53  	it.start = 0
    54  	dv := int32(v>>1) ^ (int32(v<<31) >> 31)
    55  	it.value += dv
    56  
    57  	// pc delta
    58  	v = getvarint(&it.p)
    59  
    60  	it.nextpc = it.pc + v*it.pcscale
    61  }
    62  
    63  func pciterinit(ctxt *Link, it *Pciter, d *sym.Pcdata) {
    64  	it.d = *d
    65  	it.p = it.d.P
    66  	it.pc = 0
    67  	it.nextpc = 0
    68  	it.value = -1
    69  	it.start = 1
    70  	it.done = 0
    71  	it.pcscale = uint32(ctxt.Arch.MinLC)
    72  	pciternext(it)
    73  }
    74  
    75  func addvarint(d *sym.Pcdata, val uint32) {
    76  	n := int32(0)
    77  	for v := val; v >= 0x80; v >>= 7 {
    78  		n++
    79  	}
    80  	n++
    81  
    82  	old := len(d.P)
    83  	for cap(d.P) < len(d.P)+int(n) {
    84  		d.P = append(d.P[:cap(d.P)], 0)
    85  	}
    86  	d.P = d.P[:old+int(n)]
    87  
    88  	p := d.P[old:]
    89  	var v uint32
    90  	for v = val; v >= 0x80; v >>= 7 {
    91  		p[0] = byte(v | 0x80)
    92  		p = p[1:]
    93  	}
    94  	p[0] = byte(v)
    95  }
    96  
    97  func addpctab(ctxt *Link, ftab *sym.Symbol, off int32, d *sym.Pcdata) int32 {
    98  	var start int32
    99  	if len(d.P) > 0 {
   100  		start = int32(len(ftab.P))
   101  		ftab.AddBytes(d.P)
   102  	}
   103  	return int32(ftab.SetUint32(ctxt.Arch, int64(off), uint32(start)))
   104  }
   105  
   106  func ftabaddstring(ctxt *Link, ftab *sym.Symbol, s string) int32 {
   107  	n := int32(len(s)) + 1
   108  	start := int32(len(ftab.P))
   109  	ftab.Grow(int64(start) + int64(n) + 1)
   110  	copy(ftab.P[start:], s)
   111  	return start
   112  }
   113  
   114  // numberfile assigns a file number to the file if it hasn't been assigned already.
   115  func numberfile(ctxt *Link, file *sym.Symbol) {
   116  	if file.Type != sym.SFILEPATH {
   117  		ctxt.Filesyms = append(ctxt.Filesyms, file)
   118  		file.Value = int64(len(ctxt.Filesyms))
   119  		file.Type = sym.SFILEPATH
   120  		path := file.Name[len(src.FileSymPrefix):]
   121  		file.Name = expandGoroot(path)
   122  	}
   123  }
   124  
   125  func renumberfiles(ctxt *Link, files []*sym.Symbol, d *sym.Pcdata) {
   126  	// Give files numbers.
   127  	for _, f := range files {
   128  		numberfile(ctxt, f)
   129  	}
   130  
   131  	newval := int32(-1)
   132  	var out sym.Pcdata
   133  	var it Pciter
   134  	for pciterinit(ctxt, &it, d); it.done == 0; pciternext(&it) {
   135  		// value delta
   136  		oldval := it.value
   137  
   138  		var val int32
   139  		if oldval == -1 {
   140  			val = -1
   141  		} else {
   142  			if oldval < 0 || oldval >= int32(len(files)) {
   143  				log.Fatalf("bad pcdata %d", oldval)
   144  			}
   145  			val = int32(files[oldval].Value)
   146  		}
   147  
   148  		dv := val - newval
   149  		newval = val
   150  		v := (uint32(dv) << 1) ^ uint32(dv>>31)
   151  		addvarint(&out, v)
   152  
   153  		// pc delta
   154  		addvarint(&out, (it.nextpc-it.pc)/it.pcscale)
   155  	}
   156  
   157  	// terminating value delta
   158  	addvarint(&out, 0)
   159  
   160  	*d = out
   161  }
   162  
   163  // onlycsymbol reports whether this is a symbol that is referenced by C code.
   164  func onlycsymbol(s *sym.Symbol) bool {
   165  	switch s.Name {
   166  	case "_cgo_topofstack", "_cgo_panic", "crosscall2":
   167  		return true
   168  	}
   169  	if strings.HasPrefix(s.Name, "_cgoexp_") {
   170  		return true
   171  	}
   172  	return false
   173  }
   174  
   175  func emitPcln(ctxt *Link, s *sym.Symbol) bool {
   176  	if s == nil {
   177  		return true
   178  	}
   179  	if ctxt.BuildMode == BuildModePlugin && ctxt.HeadType == objabi.Hdarwin && onlycsymbol(s) {
   180  		return false
   181  	}
   182  	// We want to generate func table entries only for the "lowest level" symbols,
   183  	// not containers of subsymbols.
   184  	if s.Attr.Container() {
   185  		return true
   186  	}
   187  	return true
   188  }
   189  
   190  // pclntab initializes the pclntab symbol with
   191  // runtime function and file name information.
   192  
   193  var pclntabZpcln sym.FuncInfo
   194  
   195  // These variables are used to initialize runtime.firstmoduledata, see symtab.go:symtab.
   196  var pclntabNfunc int32
   197  var pclntabFiletabOffset int32
   198  var pclntabPclntabOffset int32
   199  var pclntabFirstFunc *sym.Symbol
   200  var pclntabLastFunc *sym.Symbol
   201  
   202  func (ctxt *Link) pclntab() {
   203  	funcdataBytes := int64(0)
   204  	ftab := ctxt.Syms.Lookup("runtime.pclntab", 0)
   205  	ftab.Type = sym.SPCLNTAB
   206  	ftab.Attr |= sym.AttrReachable
   207  
   208  	// See golang.org/s/go12symtab for the format. Briefly:
   209  	//	8-byte header
   210  	//	nfunc [thearch.ptrsize bytes]
   211  	//	function table, alternating PC and offset to func struct [each entry thearch.ptrsize bytes]
   212  	//	end PC [thearch.ptrsize bytes]
   213  	//	offset to file table [4 bytes]
   214  	nfunc := int32(0)
   215  
   216  	// Find container symbols and mark them as such.
   217  	for _, s := range ctxt.Textp {
   218  		if s.Outer != nil {
   219  			s.Outer.Attr |= sym.AttrContainer
   220  		}
   221  	}
   222  
   223  	for _, s := range ctxt.Textp {
   224  		if emitPcln(ctxt, s) {
   225  			nfunc++
   226  		}
   227  	}
   228  
   229  	pclntabNfunc = nfunc
   230  	ftab.Grow(8 + int64(ctxt.Arch.PtrSize) + int64(nfunc)*2*int64(ctxt.Arch.PtrSize) + int64(ctxt.Arch.PtrSize) + 4)
   231  	ftab.SetUint32(ctxt.Arch, 0, 0xfffffffb)
   232  	ftab.SetUint8(ctxt.Arch, 6, uint8(ctxt.Arch.MinLC))
   233  	ftab.SetUint8(ctxt.Arch, 7, uint8(ctxt.Arch.PtrSize))
   234  	ftab.SetUint(ctxt.Arch, 8, uint64(nfunc))
   235  	pclntabPclntabOffset = int32(8 + ctxt.Arch.PtrSize)
   236  
   237  	funcnameoff := make(map[string]int32)
   238  	nameToOffset := func(name string) int32 {
   239  		nameoff, ok := funcnameoff[name]
   240  		if !ok {
   241  			nameoff = ftabaddstring(ctxt, ftab, name)
   242  			funcnameoff[name] = nameoff
   243  		}
   244  		return nameoff
   245  	}
   246  
   247  	nfunc = 0
   248  	var last *sym.Symbol
   249  	for _, s := range ctxt.Textp {
   250  		last = s
   251  		if !emitPcln(ctxt, s) {
   252  			continue
   253  		}
   254  		pcln := s.FuncInfo
   255  		if pcln == nil {
   256  			pcln = &pclntabZpcln
   257  		}
   258  
   259  		if pclntabFirstFunc == nil {
   260  			pclntabFirstFunc = s
   261  		}
   262  
   263  		if len(pcln.InlTree) > 0 {
   264  			if len(pcln.Pcdata) <= objabi.PCDATA_InlTreeIndex {
   265  				// Create inlining pcdata table.
   266  				pcdata := make([]sym.Pcdata, objabi.PCDATA_InlTreeIndex+1)
   267  				copy(pcdata, pcln.Pcdata)
   268  				pcln.Pcdata = pcdata
   269  			}
   270  
   271  			if len(pcln.Funcdataoff) <= objabi.FUNCDATA_InlTree {
   272  				// Create inline tree funcdata.
   273  				funcdata := make([]*sym.Symbol, objabi.FUNCDATA_InlTree+1)
   274  				funcdataoff := make([]int64, objabi.FUNCDATA_InlTree+1)
   275  				copy(funcdata, pcln.Funcdata)
   276  				copy(funcdataoff, pcln.Funcdataoff)
   277  				pcln.Funcdata = funcdata
   278  				pcln.Funcdataoff = funcdataoff
   279  			}
   280  		}
   281  
   282  		funcstart := int32(len(ftab.P))
   283  		funcstart += int32(-len(ftab.P)) & (int32(ctxt.Arch.PtrSize) - 1)
   284  
   285  		ftab.SetAddr(ctxt.Arch, 8+int64(ctxt.Arch.PtrSize)+int64(nfunc)*2*int64(ctxt.Arch.PtrSize), s)
   286  		ftab.SetUint(ctxt.Arch, 8+int64(ctxt.Arch.PtrSize)+int64(nfunc)*2*int64(ctxt.Arch.PtrSize)+int64(ctxt.Arch.PtrSize), uint64(funcstart))
   287  
   288  		// Write runtime._func. Keep in sync with ../../../../runtime/runtime2.go:/_func
   289  		// and package debug/gosym.
   290  
   291  		// fixed size of struct, checked below
   292  		off := funcstart
   293  
   294  		end := funcstart + int32(ctxt.Arch.PtrSize) + 3*4 + 5*4 + int32(len(pcln.Pcdata))*4 + int32(len(pcln.Funcdata))*int32(ctxt.Arch.PtrSize)
   295  		if len(pcln.Funcdata) > 0 && (end&int32(ctxt.Arch.PtrSize-1) != 0) {
   296  			end += 4
   297  		}
   298  		ftab.Grow(int64(end))
   299  
   300  		// entry uintptr
   301  		off = int32(ftab.SetAddr(ctxt.Arch, int64(off), s))
   302  
   303  		// name int32
   304  		nameoff := nameToOffset(s.Name)
   305  		off = int32(ftab.SetUint32(ctxt.Arch, int64(off), uint32(nameoff)))
   306  
   307  		// args int32
   308  		// TODO: Move into funcinfo.
   309  		args := uint32(0)
   310  		if s.FuncInfo != nil {
   311  			args = uint32(s.FuncInfo.Args)
   312  		}
   313  		off = int32(ftab.SetUint32(ctxt.Arch, int64(off), args))
   314  
   315  		// funcID uint32
   316  		funcID := objabi.FuncID_normal
   317  		switch s.Name {
   318  		case "runtime.main":
   319  			funcID = objabi.FuncID_runtime_main
   320  		case "runtime.goexit":
   321  			funcID = objabi.FuncID_goexit
   322  		case "runtime.jmpdefer":
   323  			funcID = objabi.FuncID_jmpdefer
   324  		case "runtime.mcall":
   325  			funcID = objabi.FuncID_mcall
   326  		case "runtime.morestack":
   327  			funcID = objabi.FuncID_morestack
   328  		case "runtime.mstart":
   329  			funcID = objabi.FuncID_mstart
   330  		case "runtime.rt0_go":
   331  			funcID = objabi.FuncID_rt0_go
   332  		case "runtime.asmcgocall":
   333  			funcID = objabi.FuncID_asmcgocall
   334  		case "runtime.sigpanic":
   335  			funcID = objabi.FuncID_sigpanic
   336  		case "runtime.runfinq":
   337  			funcID = objabi.FuncID_runfinq
   338  		case "runtime.gcBgMarkWorker":
   339  			funcID = objabi.FuncID_gcBgMarkWorker
   340  		case "runtime.systemstack_switch":
   341  			funcID = objabi.FuncID_systemstack_switch
   342  		case "runtime.systemstack":
   343  			funcID = objabi.FuncID_systemstack
   344  		case "runtime.cgocallback_gofunc":
   345  			funcID = objabi.FuncID_cgocallback_gofunc
   346  		case "runtime.gogo":
   347  			funcID = objabi.FuncID_gogo
   348  		case "runtime.externalthreadhandler":
   349  			funcID = objabi.FuncID_externalthreadhandler
   350  		case "runtime.debugCallV1":
   351  			funcID = objabi.FuncID_debugCallV1
   352  		}
   353  		off = int32(ftab.SetUint32(ctxt.Arch, int64(off), uint32(funcID)))
   354  
   355  		if pcln != &pclntabZpcln {
   356  			renumberfiles(ctxt, pcln.File, &pcln.Pcfile)
   357  			if false {
   358  				// Sanity check the new numbering
   359  				var it Pciter
   360  				for pciterinit(ctxt, &it, &pcln.Pcfile); it.done == 0; pciternext(&it) {
   361  					if it.value < 1 || it.value > int32(len(ctxt.Filesyms)) {
   362  						Errorf(s, "bad file number in pcfile: %d not in range [1, %d]\n", it.value, len(ctxt.Filesyms))
   363  						errorexit()
   364  					}
   365  				}
   366  			}
   367  		}
   368  
   369  		if len(pcln.InlTree) > 0 {
   370  			inlTreeSym := ctxt.Syms.Lookup("inltree."+s.Name, 0)
   371  			inlTreeSym.Type = sym.SRODATA
   372  			inlTreeSym.Attr |= sym.AttrReachable | sym.AttrDuplicateOK
   373  
   374  			for i, call := range pcln.InlTree {
   375  				// Usually, call.File is already numbered since the file
   376  				// shows up in the Pcfile table. However, two inlined calls
   377  				// might overlap exactly so that only the innermost file
   378  				// appears in the Pcfile table. In that case, this assigns
   379  				// the outer file a number.
   380  				numberfile(ctxt, call.File)
   381  				nameoff := nameToOffset(call.Func.Name)
   382  
   383  				inlTreeSym.SetUint32(ctxt.Arch, int64(i*16+0), uint32(call.Parent))
   384  				inlTreeSym.SetUint32(ctxt.Arch, int64(i*16+4), uint32(call.File.Value))
   385  				inlTreeSym.SetUint32(ctxt.Arch, int64(i*16+8), uint32(call.Line))
   386  				inlTreeSym.SetUint32(ctxt.Arch, int64(i*16+12), uint32(nameoff))
   387  			}
   388  
   389  			pcln.Funcdata[objabi.FUNCDATA_InlTree] = inlTreeSym
   390  			pcln.Pcdata[objabi.PCDATA_InlTreeIndex] = pcln.Pcinline
   391  		}
   392  
   393  		// pcdata
   394  		off = addpctab(ctxt, ftab, off, &pcln.Pcsp)
   395  
   396  		off = addpctab(ctxt, ftab, off, &pcln.Pcfile)
   397  		off = addpctab(ctxt, ftab, off, &pcln.Pcline)
   398  		off = int32(ftab.SetUint32(ctxt.Arch, int64(off), uint32(len(pcln.Pcdata))))
   399  		off = int32(ftab.SetUint32(ctxt.Arch, int64(off), uint32(len(pcln.Funcdata))))
   400  		for i := range pcln.Pcdata {
   401  			off = addpctab(ctxt, ftab, off, &pcln.Pcdata[i])
   402  		}
   403  
   404  		// funcdata, must be pointer-aligned and we're only int32-aligned.
   405  		// Missing funcdata will be 0 (nil pointer).
   406  		if len(pcln.Funcdata) > 0 {
   407  			if off&int32(ctxt.Arch.PtrSize-1) != 0 {
   408  				off += 4
   409  			}
   410  			for i := range pcln.Funcdata {
   411  				if pcln.Funcdata[i] == nil {
   412  					ftab.SetUint(ctxt.Arch, int64(off)+int64(ctxt.Arch.PtrSize)*int64(i), uint64(pcln.Funcdataoff[i]))
   413  				} else {
   414  					// TODO: Dedup.
   415  					funcdataBytes += pcln.Funcdata[i].Size
   416  
   417  					ftab.SetAddrPlus(ctxt.Arch, int64(off)+int64(ctxt.Arch.PtrSize)*int64(i), pcln.Funcdata[i], pcln.Funcdataoff[i])
   418  				}
   419  			}
   420  
   421  			off += int32(len(pcln.Funcdata)) * int32(ctxt.Arch.PtrSize)
   422  		}
   423  
   424  		if off != end {
   425  			Errorf(s, "bad math in functab: funcstart=%d off=%d but end=%d (npcdata=%d nfuncdata=%d ptrsize=%d)", funcstart, off, end, len(pcln.Pcdata), len(pcln.Funcdata), ctxt.Arch.PtrSize)
   426  			errorexit()
   427  		}
   428  
   429  		nfunc++
   430  	}
   431  
   432  	pclntabLastFunc = last
   433  	// Final entry of table is just end pc.
   434  	ftab.SetAddrPlus(ctxt.Arch, 8+int64(ctxt.Arch.PtrSize)+int64(nfunc)*2*int64(ctxt.Arch.PtrSize), last, last.Size)
   435  
   436  	// Start file table.
   437  	start := int32(len(ftab.P))
   438  
   439  	start += int32(-len(ftab.P)) & (int32(ctxt.Arch.PtrSize) - 1)
   440  	pclntabFiletabOffset = start
   441  	ftab.SetUint32(ctxt.Arch, 8+int64(ctxt.Arch.PtrSize)+int64(nfunc)*2*int64(ctxt.Arch.PtrSize)+int64(ctxt.Arch.PtrSize), uint32(start))
   442  
   443  	ftab.Grow(int64(start) + (int64(len(ctxt.Filesyms))+1)*4)
   444  	ftab.SetUint32(ctxt.Arch, int64(start), uint32(len(ctxt.Filesyms)+1))
   445  	for i := len(ctxt.Filesyms) - 1; i >= 0; i-- {
   446  		s := ctxt.Filesyms[i]
   447  		ftab.SetUint32(ctxt.Arch, int64(start)+s.Value*4, uint32(ftabaddstring(ctxt, ftab, s.Name)))
   448  	}
   449  
   450  	ftab.Size = int64(len(ftab.P))
   451  
   452  	if ctxt.Debugvlog != 0 {
   453  		ctxt.Logf("%5.2f pclntab=%d bytes, funcdata total %d bytes\n", Cputime(), ftab.Size, funcdataBytes)
   454  	}
   455  }
   456  
   457  func gorootFinal() string {
   458  	root := objabi.GOROOT
   459  	if final := os.Getenv("GOROOT_FINAL"); final != "" {
   460  		root = final
   461  	}
   462  	return root
   463  }
   464  
   465  func expandGoroot(s string) string {
   466  	const n = len("$GOROOT")
   467  	if len(s) >= n+1 && s[:n] == "$GOROOT" && (s[n] == '/' || s[n] == '\\') {
   468  		return filepath.ToSlash(filepath.Join(gorootFinal(), s[n:]))
   469  	}
   470  	return s
   471  }
   472  
   473  const (
   474  	BUCKETSIZE    = 256 * MINFUNC
   475  	SUBBUCKETS    = 16
   476  	SUBBUCKETSIZE = BUCKETSIZE / SUBBUCKETS
   477  	NOIDX         = 0x7fffffff
   478  )
   479  
   480  // findfunctab generates a lookup table to quickly find the containing
   481  // function for a pc. See src/runtime/symtab.go:findfunc for details.
   482  func (ctxt *Link) findfunctab() {
   483  	t := ctxt.Syms.Lookup("runtime.findfunctab", 0)
   484  	t.Type = sym.SRODATA
   485  	t.Attr |= sym.AttrReachable
   486  	t.Attr |= sym.AttrLocal
   487  
   488  	// find min and max address
   489  	min := ctxt.Textp[0].Value
   490  	max := int64(0)
   491  	for _, s := range ctxt.Textp {
   492  		max = s.Value + s.Size
   493  	}
   494  
   495  	// for each subbucket, compute the minimum of all symbol indexes
   496  	// that map to that subbucket.
   497  	n := int32((max - min + SUBBUCKETSIZE - 1) / SUBBUCKETSIZE)
   498  
   499  	indexes := make([]int32, n)
   500  	for i := int32(0); i < n; i++ {
   501  		indexes[i] = NOIDX
   502  	}
   503  	idx := int32(0)
   504  	for i, s := range ctxt.Textp {
   505  		if !emitPcln(ctxt, s) {
   506  			continue
   507  		}
   508  		p := s.Value
   509  		var e *sym.Symbol
   510  		i++
   511  		if i < len(ctxt.Textp) {
   512  			e = ctxt.Textp[i]
   513  		}
   514  		for !emitPcln(ctxt, e) && i < len(ctxt.Textp) {
   515  			e = ctxt.Textp[i]
   516  			i++
   517  		}
   518  		q := max
   519  		if e != nil {
   520  			q = e.Value
   521  		}
   522  
   523  		//print("%d: [%lld %lld] %s\n", idx, p, q, s->name);
   524  		for ; p < q; p += SUBBUCKETSIZE {
   525  			i = int((p - min) / SUBBUCKETSIZE)
   526  			if indexes[i] > idx {
   527  				indexes[i] = idx
   528  			}
   529  		}
   530  
   531  		i = int((q - 1 - min) / SUBBUCKETSIZE)
   532  		if indexes[i] > idx {
   533  			indexes[i] = idx
   534  		}
   535  		idx++
   536  	}
   537  
   538  	// allocate table
   539  	nbuckets := int32((max - min + BUCKETSIZE - 1) / BUCKETSIZE)
   540  
   541  	t.Grow(4*int64(nbuckets) + int64(n))
   542  
   543  	// fill in table
   544  	for i := int32(0); i < nbuckets; i++ {
   545  		base := indexes[i*SUBBUCKETS]
   546  		if base == NOIDX {
   547  			Errorf(nil, "hole in findfunctab")
   548  		}
   549  		t.SetUint32(ctxt.Arch, int64(i)*(4+SUBBUCKETS), uint32(base))
   550  		for j := int32(0); j < SUBBUCKETS && i*SUBBUCKETS+j < n; j++ {
   551  			idx = indexes[i*SUBBUCKETS+j]
   552  			if idx == NOIDX {
   553  				Errorf(nil, "hole in findfunctab")
   554  			}
   555  			if idx-base >= 256 {
   556  				Errorf(nil, "too many functions in a findfunc bucket! %d/%d %d %d", i, nbuckets, j, idx-base)
   557  			}
   558  
   559  			t.SetUint8(ctxt.Arch, int64(i)*(4+SUBBUCKETS)+4+int64(j), uint8(idx-base))
   560  		}
   561  	}
   562  }
   563  

View as plain text