Source file src/cmd/internal/obj/pcln.go

     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 obj
     6  
     7  import (
     8  	"cmd/internal/goobj"
     9  	"cmd/internal/objabi"
    10  	"encoding/binary"
    11  	"fmt"
    12  	"log"
    13  )
    14  
    15  // funcpctab writes to dst a pc-value table mapping the code in func to the values
    16  // returned by valfunc parameterized by arg. The invocation of valfunc to update the
    17  // current value is, for each p,
    18  //
    19  //	sym = valfunc(func, p, 0, arg);
    20  //	record sym.P as value at p->pc;
    21  //	sym = valfunc(func, p, 1, arg);
    22  //
    23  // where func is the function, val is the current value, p is the instruction being
    24  // considered, and arg can be used to further parameterize valfunc.
    25  func funcpctab(ctxt *Link, func_ *LSym, desc string, valfunc func(*Link, *LSym, int32, *Prog, int32, any) int32, arg any) *LSym {
    26  	dbg := desc == ctxt.Debugpcln
    27  	dst := []byte{}
    28  	sym := &LSym{
    29  		Type:      objabi.SRODATA,
    30  		Attribute: AttrContentAddressable | AttrPcdata,
    31  		Align:     1,
    32  	}
    33  
    34  	if dbg {
    35  		ctxt.Logf("funcpctab %s [valfunc=%s]\n", func_.Name, desc)
    36  	}
    37  
    38  	val := int32(-1)
    39  	oldval := val
    40  	fn := func_.Func()
    41  	if fn.Text == nil {
    42  		// Return the empty symbol we've built so far.
    43  		return sym
    44  	}
    45  
    46  	pc := fn.Text.Pc
    47  
    48  	if dbg {
    49  		ctxt.Logf("%6x %6d %v\n", uint64(pc), val, fn.Text)
    50  	}
    51  
    52  	buf := make([]byte, binary.MaxVarintLen32)
    53  	started := false
    54  	for p := fn.Text; p != nil; p = p.Link {
    55  		// Update val. If it's not changing, keep going.
    56  		val = valfunc(ctxt, func_, val, p, 0, arg)
    57  
    58  		if val == oldval && started {
    59  			val = valfunc(ctxt, func_, val, p, 1, arg)
    60  			if dbg {
    61  				ctxt.Logf("%6x %6s %v\n", uint64(p.Pc), "", p)
    62  			}
    63  			continue
    64  		}
    65  
    66  		// If the pc of the next instruction is the same as the
    67  		// pc of this instruction, this instruction is not a real
    68  		// instruction. Keep going, so that we only emit a delta
    69  		// for a true instruction boundary in the program.
    70  		if p.Link != nil && p.Link.Pc == p.Pc {
    71  			val = valfunc(ctxt, func_, val, p, 1, arg)
    72  			if dbg {
    73  				ctxt.Logf("%6x %6s %v\n", uint64(p.Pc), "", p)
    74  			}
    75  			continue
    76  		}
    77  
    78  		// The table is a sequence of (value, pc) pairs, where each
    79  		// pair states that the given value is in effect from the current position
    80  		// up to the given pc, which becomes the new current position.
    81  		// To generate the table as we scan over the program instructions,
    82  		// we emit a "(value" when pc == func->value, and then
    83  		// each time we observe a change in value we emit ", pc) (value".
    84  		// When the scan is over, we emit the closing ", pc)".
    85  		//
    86  		// The table is delta-encoded. The value deltas are signed and
    87  		// transmitted in zig-zag form, where a complement bit is placed in bit 0,
    88  		// and the pc deltas are unsigned. Both kinds of deltas are sent
    89  		// as variable-length little-endian base-128 integers,
    90  		// where the 0x80 bit indicates that the integer continues.
    91  
    92  		if dbg {
    93  			ctxt.Logf("%6x %6d %v\n", uint64(p.Pc), val, p)
    94  		}
    95  
    96  		if started {
    97  			pcdelta := (p.Pc - pc) / int64(ctxt.Arch.MinLC)
    98  			n := binary.PutUvarint(buf, uint64(pcdelta))
    99  			dst = append(dst, buf[:n]...)
   100  			pc = p.Pc
   101  		}
   102  
   103  		delta := val - oldval
   104  		n := binary.PutVarint(buf, int64(delta))
   105  		dst = append(dst, buf[:n]...)
   106  		oldval = val
   107  		started = true
   108  		val = valfunc(ctxt, func_, val, p, 1, arg)
   109  	}
   110  
   111  	if started {
   112  		if dbg {
   113  			ctxt.Logf("%6x done\n", uint64(fn.Text.Pc+func_.Size))
   114  		}
   115  		v := (func_.Size - pc) / int64(ctxt.Arch.MinLC)
   116  		if v < 0 {
   117  			ctxt.Diag("negative pc offset: %v", v)
   118  		}
   119  		n := binary.PutUvarint(buf, uint64(v))
   120  		dst = append(dst, buf[:n]...)
   121  		// add terminating varint-encoded 0, which is just 0
   122  		dst = append(dst, 0)
   123  	}
   124  
   125  	if dbg {
   126  		ctxt.Logf("wrote %d bytes to %p\n", len(dst), dst)
   127  		for _, p := range dst {
   128  			ctxt.Logf(" %02x", p)
   129  		}
   130  		ctxt.Logf("\n")
   131  	}
   132  
   133  	sym.Size = int64(len(dst))
   134  	sym.P = dst
   135  	return sym
   136  }
   137  
   138  // pctofileline computes either the file number (arg == 0)
   139  // or the line number (arg == 1) to use at p.
   140  // Because p.Pos applies to p, phase == 0 (before p)
   141  // takes care of the update.
   142  func pctofileline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg any) int32 {
   143  	if p.As == ATEXT || p.As == ANOP || p.Pos.Line() == 0 || phase == 1 {
   144  		return oldval
   145  	}
   146  	f, l := ctxt.getFileIndexAndLine(p.Pos)
   147  	if arg == nil {
   148  		return l
   149  	}
   150  	pcln := arg.(*Pcln)
   151  	pcln.UsedFiles[goobj.CUFileIndex(f)] = struct{}{}
   152  	return int32(f)
   153  }
   154  
   155  // pcinlineState holds the state used to create a function's inlining
   156  // tree and the PC-value table that maps PCs to nodes in that tree.
   157  type pcinlineState struct {
   158  	globalToLocal map[int]int
   159  	localTree     InlTree
   160  }
   161  
   162  // addBranch adds a branch from the global inlining tree in ctxt to
   163  // the function's local inlining tree, returning the index in the local tree.
   164  func (s *pcinlineState) addBranch(ctxt *Link, globalIndex int) int {
   165  	if globalIndex < 0 {
   166  		return -1
   167  	}
   168  
   169  	localIndex, ok := s.globalToLocal[globalIndex]
   170  	if ok {
   171  		return localIndex
   172  	}
   173  
   174  	// Since tracebacks don't include column information, we could
   175  	// use one node for multiple calls of the same function on the
   176  	// same line (e.g., f(x) + f(y)). For now, we use one node for
   177  	// each inlined call.
   178  	call := ctxt.InlTree.nodes[globalIndex]
   179  	call.Parent = s.addBranch(ctxt, call.Parent)
   180  	localIndex = len(s.localTree.nodes)
   181  	s.localTree.nodes = append(s.localTree.nodes, call)
   182  	s.globalToLocal[globalIndex] = localIndex
   183  	return localIndex
   184  }
   185  
   186  func (s *pcinlineState) setParentPC(ctxt *Link, globalIndex int, pc int32) {
   187  	localIndex, ok := s.globalToLocal[globalIndex]
   188  	if !ok {
   189  		// We know where to unwind to when we need to unwind a body identified
   190  		// by globalIndex. But there may be no instructions generated by that
   191  		// body (it's empty, or its instructions were CSEd with other things, etc.).
   192  		// In that case, we don't need an unwind entry.
   193  		// TODO: is this really right? Seems to happen a whole lot...
   194  		return
   195  	}
   196  	s.localTree.setParentPC(localIndex, pc)
   197  }
   198  
   199  // pctoinline computes the index into the local inlining tree to use at p.
   200  // If p is not the result of inlining, pctoinline returns -1. Because p.Pos
   201  // applies to p, phase == 0 (before p) takes care of the update.
   202  func (s *pcinlineState) pctoinline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg any) int32 {
   203  	if phase == 1 {
   204  		return oldval
   205  	}
   206  
   207  	posBase := ctxt.PosTable.Pos(p.Pos).Base()
   208  	if posBase == nil {
   209  		return -1
   210  	}
   211  
   212  	globalIndex := posBase.InliningIndex()
   213  	if globalIndex < 0 {
   214  		return -1
   215  	}
   216  
   217  	if s.globalToLocal == nil {
   218  		s.globalToLocal = make(map[int]int)
   219  	}
   220  
   221  	return int32(s.addBranch(ctxt, globalIndex))
   222  }
   223  
   224  // pctospadj computes the sp adjustment in effect.
   225  // It is oldval plus any adjustment made by p itself.
   226  // The adjustment by p takes effect only after p, so we
   227  // apply the change during phase == 1.
   228  func pctospadj(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg any) int32 {
   229  	if oldval == -1 { // starting
   230  		oldval = 0
   231  	}
   232  	if phase == 0 {
   233  		return oldval
   234  	}
   235  	if oldval+p.Spadj < -10000 || oldval+p.Spadj > 1100000000 {
   236  		ctxt.Diag("overflow in spadj: %d + %d = %d", oldval, p.Spadj, oldval+p.Spadj)
   237  		ctxt.DiagFlush()
   238  		log.Fatalf("bad code")
   239  	}
   240  
   241  	return oldval + p.Spadj
   242  }
   243  
   244  // pctopcdata computes the pcdata value in effect at p.
   245  // A PCDATA instruction sets the value in effect at future
   246  // non-PCDATA instructions.
   247  // Since PCDATA instructions have no width in the final code,
   248  // it does not matter which phase we use for the update.
   249  func pctopcdata(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg any) int32 {
   250  	if phase == 0 || p.As != APCDATA || p.From.Offset != int64(arg.(uint32)) {
   251  		return oldval
   252  	}
   253  	if int64(int32(p.To.Offset)) != p.To.Offset {
   254  		ctxt.Diag("overflow in PCDATA instruction: %v", p)
   255  		ctxt.DiagFlush()
   256  		log.Fatalf("bad code")
   257  	}
   258  
   259  	return int32(p.To.Offset)
   260  }
   261  
   262  func linkpcln(ctxt *Link, cursym *LSym) {
   263  	pcln := &cursym.Func().Pcln
   264  	pcln.UsedFiles = make(map[goobj.CUFileIndex]struct{})
   265  
   266  	npcdata := 0
   267  	nfuncdata := 0
   268  	for p := cursym.Func().Text; p != nil; p = p.Link {
   269  		// Find the highest ID of any used PCDATA table. This ignores PCDATA table
   270  		// that consist entirely of "-1", since that's the assumed default value.
   271  		//   From.Offset is table ID
   272  		//   To.Offset is data
   273  		if p.As == APCDATA && p.From.Offset >= int64(npcdata) && p.To.Offset != -1 { // ignore -1 as we start at -1, if we only see -1, nothing changed
   274  			npcdata = int(p.From.Offset + 1)
   275  		}
   276  		// Find the highest ID of any FUNCDATA table.
   277  		//   From.Offset is table ID
   278  		if p.As == AFUNCDATA && p.From.Offset >= int64(nfuncdata) {
   279  			nfuncdata = int(p.From.Offset + 1)
   280  		}
   281  	}
   282  
   283  	pcln.Pcdata = make([]*LSym, npcdata)
   284  	pcln.Funcdata = make([]*LSym, nfuncdata)
   285  
   286  	pcln.Pcsp = funcpctab(ctxt, cursym, "pctospadj", pctospadj, nil)
   287  	pcln.Pcfile = funcpctab(ctxt, cursym, "pctofile", pctofileline, pcln)
   288  	pcln.Pcline = funcpctab(ctxt, cursym, "pctoline", pctofileline, nil)
   289  
   290  	// Check that all the Progs used as inline markers are still reachable.
   291  	// See issue #40473.
   292  	fn := cursym.Func()
   293  	inlMarkProgs := make(map[*Prog]struct{}, len(fn.InlMarks))
   294  	for _, inlMark := range fn.InlMarks {
   295  		inlMarkProgs[inlMark.p] = struct{}{}
   296  	}
   297  	for p := fn.Text; p != nil; p = p.Link {
   298  		delete(inlMarkProgs, p)
   299  	}
   300  	if len(inlMarkProgs) > 0 {
   301  		ctxt.Diag("one or more instructions used as inline markers are no longer reachable")
   302  	}
   303  
   304  	pcinlineState := new(pcinlineState)
   305  	pcln.Pcinline = funcpctab(ctxt, cursym, "pctoinline", pcinlineState.pctoinline, nil)
   306  	for _, inlMark := range fn.InlMarks {
   307  		pcinlineState.setParentPC(ctxt, int(inlMark.id), int32(inlMark.p.Pc))
   308  	}
   309  	pcln.InlTree = pcinlineState.localTree
   310  	if ctxt.Debugpcln == "pctoinline" && len(pcln.InlTree.nodes) > 0 {
   311  		ctxt.Logf("-- inlining tree for %s:\n", cursym)
   312  		dumpInlTree(ctxt, pcln.InlTree)
   313  		ctxt.Logf("--\n")
   314  	}
   315  
   316  	// tabulate which pc and func data we have.
   317  	havepc := make([]uint32, (npcdata+31)/32)
   318  	havefunc := make([]uint32, (nfuncdata+31)/32)
   319  	for p := fn.Text; p != nil; p = p.Link {
   320  		if p.As == AFUNCDATA {
   321  			if (havefunc[p.From.Offset/32]>>uint64(p.From.Offset%32))&1 != 0 {
   322  				ctxt.Diag("multiple definitions for FUNCDATA $%d", p.From.Offset)
   323  			}
   324  			havefunc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32)
   325  		}
   326  
   327  		if p.As == APCDATA && p.To.Offset != -1 {
   328  			havepc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32)
   329  		}
   330  	}
   331  
   332  	// pcdata.
   333  	for i := 0; i < npcdata; i++ {
   334  		if (havepc[i/32]>>uint(i%32))&1 == 0 {
   335  			// use an empty symbol.
   336  			pcln.Pcdata[i] = &LSym{
   337  				Type:      objabi.SRODATA,
   338  				Attribute: AttrContentAddressable | AttrPcdata,
   339  				Align:     1,
   340  			}
   341  		} else {
   342  			pcln.Pcdata[i] = funcpctab(ctxt, cursym, "pctopcdata", pctopcdata, any(uint32(i)))
   343  		}
   344  	}
   345  
   346  	// funcdata
   347  	if nfuncdata > 0 {
   348  		for p := fn.Text; p != nil; p = p.Link {
   349  			if p.As != AFUNCDATA {
   350  				continue
   351  			}
   352  			i := int(p.From.Offset)
   353  			if p.To.Type != TYPE_MEM || p.To.Offset != 0 {
   354  				panic(fmt.Sprintf("bad funcdata: %v", p))
   355  			}
   356  			pcln.Funcdata[i] = p.To.Sym
   357  		}
   358  	}
   359  }
   360  
   361  // PCIter iterates over encoded pcdata tables.
   362  type PCIter struct {
   363  	p       []byte
   364  	PC      uint32
   365  	NextPC  uint32
   366  	PCScale uint32
   367  	Value   int32
   368  	start   bool
   369  	Done    bool
   370  }
   371  
   372  // NewPCIter creates a PCIter with a scale factor for the PC step size.
   373  func NewPCIter(pcScale uint32) *PCIter {
   374  	it := new(PCIter)
   375  	it.PCScale = pcScale
   376  	return it
   377  }
   378  
   379  // Next advances it to the Next pc.
   380  func (it *PCIter) Next() {
   381  	it.PC = it.NextPC
   382  	if it.Done {
   383  		return
   384  	}
   385  	if len(it.p) == 0 {
   386  		it.Done = true
   387  		return
   388  	}
   389  
   390  	// Value delta
   391  	val, n := binary.Varint(it.p)
   392  	if n <= 0 {
   393  		log.Fatalf("bad Value varint in pciterNext: read %v", n)
   394  	}
   395  	it.p = it.p[n:]
   396  
   397  	if val == 0 && !it.start {
   398  		it.Done = true
   399  		return
   400  	}
   401  
   402  	it.start = false
   403  	it.Value += int32(val)
   404  
   405  	// pc delta
   406  	pc, n := binary.Uvarint(it.p)
   407  	if n <= 0 {
   408  		log.Fatalf("bad pc varint in pciterNext: read %v", n)
   409  	}
   410  	it.p = it.p[n:]
   411  
   412  	it.NextPC = it.PC + uint32(pc)*it.PCScale
   413  }
   414  
   415  // init prepares it to iterate over p,
   416  // and advances it to the first pc.
   417  func (it *PCIter) Init(p []byte) {
   418  	it.p = p
   419  	it.PC = 0
   420  	it.NextPC = 0
   421  	it.Value = -1
   422  	it.start = true
   423  	it.Done = false
   424  	it.Next()
   425  }
   426  

View as plain text