Source file src/cmd/compile/internal/dwarfgen/dwarf.go

     1  // Copyright 2011 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 dwarfgen
     6  
     7  import (
     8  	"bytes"
     9  	"flag"
    10  	"fmt"
    11  	"internal/buildcfg"
    12  	"sort"
    13  
    14  	"cmd/compile/internal/base"
    15  	"cmd/compile/internal/ir"
    16  	"cmd/compile/internal/reflectdata"
    17  	"cmd/compile/internal/ssa"
    18  	"cmd/compile/internal/ssagen"
    19  	"cmd/compile/internal/typecheck"
    20  	"cmd/compile/internal/types"
    21  	"cmd/internal/dwarf"
    22  	"cmd/internal/obj"
    23  	"cmd/internal/objabi"
    24  	"cmd/internal/src"
    25  )
    26  
    27  func Info(fnsym *obj.LSym, infosym *obj.LSym, curfn obj.Func) (scopes []dwarf.Scope, inlcalls dwarf.InlCalls) {
    28  	fn := curfn.(*ir.Func)
    29  
    30  	if fn.Nname != nil {
    31  		expect := fn.Linksym()
    32  		if fnsym.ABI() == obj.ABI0 {
    33  			expect = fn.LinksymABI(obj.ABI0)
    34  		}
    35  		if fnsym != expect {
    36  			base.Fatalf("unexpected fnsym: %v != %v", fnsym, expect)
    37  		}
    38  	}
    39  
    40  	// Back when there were two different *Funcs for a function, this code
    41  	// was not consistent about whether a particular *Node being processed
    42  	// was an ODCLFUNC or ONAME node. Partly this is because inlined function
    43  	// bodies have no ODCLFUNC node, which was it's own inconsistency.
    44  	// In any event, the handling of the two different nodes for DWARF purposes
    45  	// was subtly different, likely in unintended ways. CL 272253 merged the
    46  	// two nodes' Func fields, so that code sees the same *Func whether it is
    47  	// holding the ODCLFUNC or the ONAME. This resulted in changes in the
    48  	// DWARF output. To preserve the existing DWARF output and leave an
    49  	// intentional change for a future CL, this code does the following when
    50  	// fn.Op == ONAME:
    51  	//
    52  	// 1. Disallow use of createComplexVars in createDwarfVars.
    53  	//    It was not possible to reach that code for an ONAME before,
    54  	//    because the DebugInfo was set only on the ODCLFUNC Func.
    55  	//    Calling into it in the ONAME case causes an index out of bounds panic.
    56  	//
    57  	// 2. Do not populate apdecls. fn.Func.Dcl was in the ODCLFUNC Func,
    58  	//    not the ONAME Func. Populating apdecls for the ONAME case results
    59  	//    in selected being populated after createSimpleVars is called in
    60  	//    createDwarfVars, and then that causes the loop to skip all the entries
    61  	//    in dcl, meaning that the RecordAutoType calls don't happen.
    62  	//
    63  	// These two adjustments keep toolstash -cmp working for now.
    64  	// Deciding the right answer is, as they say, future work.
    65  	//
    66  	// We can tell the difference between the old ODCLFUNC and ONAME
    67  	// cases by looking at the infosym.Name. If it's empty, DebugInfo is
    68  	// being called from (*obj.Link).populateDWARF, which used to use
    69  	// the ODCLFUNC. If it's non-empty (the name will end in $abstract),
    70  	// DebugInfo is being called from (*obj.Link).DwarfAbstractFunc,
    71  	// which used to use the ONAME form.
    72  	isODCLFUNC := infosym.Name == ""
    73  
    74  	var apdecls []*ir.Name
    75  	// Populate decls for fn.
    76  	if isODCLFUNC {
    77  		for _, n := range fn.Dcl {
    78  			if n.Op() != ir.ONAME { // might be OTYPE or OLITERAL
    79  				continue
    80  			}
    81  			switch n.Class {
    82  			case ir.PAUTO:
    83  				if !n.Used() {
    84  					// Text == nil -> generating abstract function
    85  					if fnsym.Func().Text != nil {
    86  						base.Fatalf("debuginfo unused node (AllocFrame should truncate fn.Func.Dcl)")
    87  					}
    88  					continue
    89  				}
    90  			case ir.PPARAM, ir.PPARAMOUT:
    91  			default:
    92  				continue
    93  			}
    94  			apdecls = append(apdecls, n)
    95  			if n.Type().Kind() == types.TSSA {
    96  				// Can happen for TypeInt128 types. This only happens for
    97  				// spill locations, so not a huge deal.
    98  				continue
    99  			}
   100  			fnsym.Func().RecordAutoType(reflectdata.TypeLinksym(n.Type()))
   101  		}
   102  	}
   103  
   104  	var closureVars map[*ir.Name]int64
   105  	if fn.Needctxt() {
   106  		closureVars = make(map[*ir.Name]int64)
   107  		csiter := typecheck.NewClosureStructIter(fn.ClosureVars)
   108  		for {
   109  			n, _, offset := csiter.Next()
   110  			if n == nil {
   111  				break
   112  			}
   113  			closureVars[n] = offset
   114  			if n.Heapaddr != nil {
   115  				closureVars[n.Heapaddr] = offset
   116  			}
   117  		}
   118  	}
   119  
   120  	decls, dwarfVars := createDwarfVars(fnsym, isODCLFUNC, fn, apdecls, closureVars)
   121  
   122  	// For each type referenced by the functions auto vars but not
   123  	// already referenced by a dwarf var, attach an R_USETYPE relocation to
   124  	// the function symbol to insure that the type included in DWARF
   125  	// processing during linking.
   126  	typesyms := []*obj.LSym{}
   127  	for t := range fnsym.Func().Autot {
   128  		typesyms = append(typesyms, t)
   129  	}
   130  	sort.Sort(obj.BySymName(typesyms))
   131  	for _, sym := range typesyms {
   132  		r := obj.Addrel(infosym)
   133  		r.Sym = sym
   134  		r.Type = objabi.R_USETYPE
   135  	}
   136  	fnsym.Func().Autot = nil
   137  
   138  	var varScopes []ir.ScopeID
   139  	for _, decl := range decls {
   140  		pos := declPos(decl)
   141  		varScopes = append(varScopes, findScope(fn.Marks, pos))
   142  	}
   143  
   144  	scopes = assembleScopes(fnsym, fn, dwarfVars, varScopes)
   145  	if base.Flag.GenDwarfInl > 0 {
   146  		inlcalls = assembleInlines(fnsym, dwarfVars)
   147  	}
   148  	return scopes, inlcalls
   149  }
   150  
   151  func declPos(decl *ir.Name) src.XPos {
   152  	return decl.Canonical().Pos()
   153  }
   154  
   155  // createDwarfVars process fn, returning a list of DWARF variables and the
   156  // Nodes they represent.
   157  func createDwarfVars(fnsym *obj.LSym, complexOK bool, fn *ir.Func, apDecls []*ir.Name, closureVars map[*ir.Name]int64) ([]*ir.Name, []*dwarf.Var) {
   158  	// Collect a raw list of DWARF vars.
   159  	var vars []*dwarf.Var
   160  	var decls []*ir.Name
   161  	var selected ir.NameSet
   162  
   163  	if base.Ctxt.Flag_locationlists && base.Ctxt.Flag_optimize && fn.DebugInfo != nil && complexOK {
   164  		decls, vars, selected = createComplexVars(fnsym, fn, closureVars)
   165  	} else if fn.ABI == obj.ABIInternal && base.Flag.N != 0 && complexOK {
   166  		decls, vars, selected = createABIVars(fnsym, fn, apDecls, closureVars)
   167  	} else {
   168  		decls, vars, selected = createSimpleVars(fnsym, apDecls, closureVars)
   169  	}
   170  	if fn.DebugInfo != nil {
   171  		// Recover zero sized variables eliminated by the stackframe pass
   172  		for _, n := range fn.DebugInfo.(*ssa.FuncDebug).OptDcl {
   173  			if n.Class != ir.PAUTO {
   174  				continue
   175  			}
   176  			types.CalcSize(n.Type())
   177  			if n.Type().Size() == 0 {
   178  				decls = append(decls, n)
   179  				vars = append(vars, createSimpleVar(fnsym, n, closureVars))
   180  				vars[len(vars)-1].StackOffset = 0
   181  				fnsym.Func().RecordAutoType(reflectdata.TypeLinksym(n.Type()))
   182  			}
   183  		}
   184  	}
   185  
   186  	dcl := apDecls
   187  	if fnsym.WasInlined() {
   188  		dcl = preInliningDcls(fnsym)
   189  	} else {
   190  		// The backend's stackframe pass prunes away entries from the
   191  		// fn's Dcl list, including PARAMOUT nodes that correspond to
   192  		// output params passed in registers. Add back in these
   193  		// entries here so that we can process them properly during
   194  		// DWARF-gen. See issue 48573 for more details.
   195  		debugInfo := fn.DebugInfo.(*ssa.FuncDebug)
   196  		for _, n := range debugInfo.RegOutputParams {
   197  			if n.Class != ir.PPARAMOUT || !n.IsOutputParamInRegisters() {
   198  				panic("invalid ir.Name on debugInfo.RegOutputParams list")
   199  			}
   200  			dcl = append(dcl, n)
   201  		}
   202  	}
   203  
   204  	// If optimization is enabled, the list above will typically be
   205  	// missing some of the original pre-optimization variables in the
   206  	// function (they may have been promoted to registers, folded into
   207  	// constants, dead-coded away, etc).  Input arguments not eligible
   208  	// for SSA optimization are also missing.  Here we add back in entries
   209  	// for selected missing vars. Note that the recipe below creates a
   210  	// conservative location. The idea here is that we want to
   211  	// communicate to the user that "yes, there is a variable named X
   212  	// in this function, but no, I don't have enough information to
   213  	// reliably report its contents."
   214  	// For non-SSA-able arguments, however, the correct information
   215  	// is known -- they have a single home on the stack.
   216  	for _, n := range dcl {
   217  		if selected.Has(n) {
   218  			continue
   219  		}
   220  		c := n.Sym().Name[0]
   221  		if c == '.' || n.Type().IsUntyped() {
   222  			continue
   223  		}
   224  		if n.Class == ir.PPARAM && !ssa.CanSSA(n.Type()) {
   225  			// SSA-able args get location lists, and may move in and
   226  			// out of registers, so those are handled elsewhere.
   227  			// Autos and named output params seem to get handled
   228  			// with VARDEF, which creates location lists.
   229  			// Args not of SSA-able type are treated here; they
   230  			// are homed on the stack in a single place for the
   231  			// entire call.
   232  			vars = append(vars, createSimpleVar(fnsym, n, closureVars))
   233  			decls = append(decls, n)
   234  			continue
   235  		}
   236  		typename := dwarf.InfoPrefix + types.TypeSymName(n.Type())
   237  		decls = append(decls, n)
   238  		tag := dwarf.DW_TAG_variable
   239  		isReturnValue := (n.Class == ir.PPARAMOUT)
   240  		if n.Class == ir.PPARAM || n.Class == ir.PPARAMOUT {
   241  			tag = dwarf.DW_TAG_formal_parameter
   242  		}
   243  		if n.Esc() == ir.EscHeap {
   244  			// The variable in question has been promoted to the heap.
   245  			// Its address is in n.Heapaddr.
   246  			// TODO(thanm): generate a better location expression
   247  		}
   248  		inlIndex := 0
   249  		if base.Flag.GenDwarfInl > 1 {
   250  			if n.InlFormal() || n.InlLocal() {
   251  				inlIndex = posInlIndex(n.Pos()) + 1
   252  				if n.InlFormal() {
   253  					tag = dwarf.DW_TAG_formal_parameter
   254  				}
   255  			}
   256  		}
   257  		declpos := base.Ctxt.InnermostPos(n.Pos())
   258  		vars = append(vars, &dwarf.Var{
   259  			Name:          n.Sym().Name,
   260  			IsReturnValue: isReturnValue,
   261  			Tag:           tag,
   262  			WithLoclist:   true,
   263  			StackOffset:   int32(n.FrameOffset()),
   264  			Type:          base.Ctxt.Lookup(typename),
   265  			DeclFile:      declpos.RelFilename(),
   266  			DeclLine:      declpos.RelLine(),
   267  			DeclCol:       declpos.RelCol(),
   268  			InlIndex:      int32(inlIndex),
   269  			ChildIndex:    -1,
   270  			DictIndex:     n.DictIndex,
   271  			ClosureOffset: closureOffset(n, closureVars),
   272  		})
   273  		// Record go type of to insure that it gets emitted by the linker.
   274  		fnsym.Func().RecordAutoType(reflectdata.TypeLinksym(n.Type()))
   275  	}
   276  
   277  	// Sort decls and vars.
   278  	sortDeclsAndVars(fn, decls, vars)
   279  
   280  	return decls, vars
   281  }
   282  
   283  // sortDeclsAndVars sorts the decl and dwarf var lists according to
   284  // parameter declaration order, so as to insure that when a subprogram
   285  // DIE is emitted, its parameter children appear in declaration order.
   286  // Prior to the advent of the register ABI, sorting by frame offset
   287  // would achieve this; with the register we now need to go back to the
   288  // original function signature.
   289  func sortDeclsAndVars(fn *ir.Func, decls []*ir.Name, vars []*dwarf.Var) {
   290  	paramOrder := make(map[*ir.Name]int)
   291  	idx := 1
   292  	for _, f := range fn.Type().RecvParamsResults() {
   293  		if n, ok := f.Nname.(*ir.Name); ok {
   294  			paramOrder[n] = idx
   295  			idx++
   296  		}
   297  	}
   298  	sort.Stable(varsAndDecls{decls, vars, paramOrder})
   299  }
   300  
   301  type varsAndDecls struct {
   302  	decls      []*ir.Name
   303  	vars       []*dwarf.Var
   304  	paramOrder map[*ir.Name]int
   305  }
   306  
   307  func (v varsAndDecls) Len() int {
   308  	return len(v.decls)
   309  }
   310  
   311  func (v varsAndDecls) Less(i, j int) bool {
   312  	nameLT := func(ni, nj *ir.Name) bool {
   313  		oi, foundi := v.paramOrder[ni]
   314  		oj, foundj := v.paramOrder[nj]
   315  		if foundi {
   316  			if foundj {
   317  				return oi < oj
   318  			} else {
   319  				return true
   320  			}
   321  		}
   322  		return false
   323  	}
   324  	return nameLT(v.decls[i], v.decls[j])
   325  }
   326  
   327  func (v varsAndDecls) Swap(i, j int) {
   328  	v.vars[i], v.vars[j] = v.vars[j], v.vars[i]
   329  	v.decls[i], v.decls[j] = v.decls[j], v.decls[i]
   330  }
   331  
   332  // Given a function that was inlined at some point during the
   333  // compilation, return a sorted list of nodes corresponding to the
   334  // autos/locals in that function prior to inlining. If this is a
   335  // function that is not local to the package being compiled, then the
   336  // names of the variables may have been "versioned" to avoid conflicts
   337  // with local vars; disregard this versioning when sorting.
   338  func preInliningDcls(fnsym *obj.LSym) []*ir.Name {
   339  	fn := base.Ctxt.DwFixups.GetPrecursorFunc(fnsym).(*ir.Func)
   340  	var rdcl []*ir.Name
   341  	for _, n := range fn.Inl.Dcl {
   342  		c := n.Sym().Name[0]
   343  		// Avoid reporting "_" parameters, since if there are more than
   344  		// one, it can result in a collision later on, as in #23179.
   345  		if n.Sym().Name == "_" || c == '.' || n.Type().IsUntyped() {
   346  			continue
   347  		}
   348  		rdcl = append(rdcl, n)
   349  	}
   350  	return rdcl
   351  }
   352  
   353  // createSimpleVars creates a DWARF entry for every variable declared in the
   354  // function, claiming that they are permanently on the stack.
   355  func createSimpleVars(fnsym *obj.LSym, apDecls []*ir.Name, closureVars map[*ir.Name]int64) ([]*ir.Name, []*dwarf.Var, ir.NameSet) {
   356  	var vars []*dwarf.Var
   357  	var decls []*ir.Name
   358  	var selected ir.NameSet
   359  	for _, n := range apDecls {
   360  		if ir.IsAutoTmp(n) {
   361  			continue
   362  		}
   363  
   364  		decls = append(decls, n)
   365  		vars = append(vars, createSimpleVar(fnsym, n, closureVars))
   366  		selected.Add(n)
   367  	}
   368  	return decls, vars, selected
   369  }
   370  
   371  func createSimpleVar(fnsym *obj.LSym, n *ir.Name, closureVars map[*ir.Name]int64) *dwarf.Var {
   372  	var tag int
   373  	var offs int64
   374  
   375  	localAutoOffset := func() int64 {
   376  		offs = n.FrameOffset()
   377  		if base.Ctxt.Arch.FixedFrameSize == 0 {
   378  			offs -= int64(types.PtrSize)
   379  		}
   380  		if buildcfg.FramePointerEnabled {
   381  			offs -= int64(types.PtrSize)
   382  		}
   383  		return offs
   384  	}
   385  
   386  	switch n.Class {
   387  	case ir.PAUTO:
   388  		offs = localAutoOffset()
   389  		tag = dwarf.DW_TAG_variable
   390  	case ir.PPARAM, ir.PPARAMOUT:
   391  		tag = dwarf.DW_TAG_formal_parameter
   392  		if n.IsOutputParamInRegisters() {
   393  			offs = localAutoOffset()
   394  		} else {
   395  			offs = n.FrameOffset() + base.Ctxt.Arch.FixedFrameSize
   396  		}
   397  
   398  	default:
   399  		base.Fatalf("createSimpleVar unexpected class %v for node %v", n.Class, n)
   400  	}
   401  
   402  	typename := dwarf.InfoPrefix + types.TypeSymName(n.Type())
   403  	delete(fnsym.Func().Autot, reflectdata.TypeLinksym(n.Type()))
   404  	inlIndex := 0
   405  	if base.Flag.GenDwarfInl > 1 {
   406  		if n.InlFormal() || n.InlLocal() {
   407  			inlIndex = posInlIndex(n.Pos()) + 1
   408  			if n.InlFormal() {
   409  				tag = dwarf.DW_TAG_formal_parameter
   410  			}
   411  		}
   412  	}
   413  	declpos := base.Ctxt.InnermostPos(declPos(n))
   414  	return &dwarf.Var{
   415  		Name:          n.Sym().Name,
   416  		IsReturnValue: n.Class == ir.PPARAMOUT,
   417  		IsInlFormal:   n.InlFormal(),
   418  		Tag:           tag,
   419  		StackOffset:   int32(offs),
   420  		Type:          base.Ctxt.Lookup(typename),
   421  		DeclFile:      declpos.RelFilename(),
   422  		DeclLine:      declpos.RelLine(),
   423  		DeclCol:       declpos.RelCol(),
   424  		InlIndex:      int32(inlIndex),
   425  		ChildIndex:    -1,
   426  		DictIndex:     n.DictIndex,
   427  		ClosureOffset: closureOffset(n, closureVars),
   428  	}
   429  }
   430  
   431  // createABIVars creates DWARF variables for functions in which the
   432  // register ABI is enabled but optimization is turned off. It uses a
   433  // hybrid approach in which register-resident input params are
   434  // captured with location lists, and all other vars use the "simple"
   435  // strategy.
   436  func createABIVars(fnsym *obj.LSym, fn *ir.Func, apDecls []*ir.Name, closureVars map[*ir.Name]int64) ([]*ir.Name, []*dwarf.Var, ir.NameSet) {
   437  
   438  	// Invoke createComplexVars to generate dwarf vars for input parameters
   439  	// that are register-allocated according to the ABI rules.
   440  	decls, vars, selected := createComplexVars(fnsym, fn, closureVars)
   441  
   442  	// Now fill in the remainder of the variables: input parameters
   443  	// that are not register-resident, output parameters, and local
   444  	// variables.
   445  	for _, n := range apDecls {
   446  		if ir.IsAutoTmp(n) {
   447  			continue
   448  		}
   449  		if _, ok := selected[n]; ok {
   450  			// already handled
   451  			continue
   452  		}
   453  
   454  		decls = append(decls, n)
   455  		vars = append(vars, createSimpleVar(fnsym, n, closureVars))
   456  		selected.Add(n)
   457  	}
   458  
   459  	return decls, vars, selected
   460  }
   461  
   462  // createComplexVars creates recomposed DWARF vars with location lists,
   463  // suitable for describing optimized code.
   464  func createComplexVars(fnsym *obj.LSym, fn *ir.Func, closureVars map[*ir.Name]int64) ([]*ir.Name, []*dwarf.Var, ir.NameSet) {
   465  	debugInfo := fn.DebugInfo.(*ssa.FuncDebug)
   466  
   467  	// Produce a DWARF variable entry for each user variable.
   468  	var decls []*ir.Name
   469  	var vars []*dwarf.Var
   470  	var ssaVars ir.NameSet
   471  
   472  	for varID, dvar := range debugInfo.Vars {
   473  		n := dvar
   474  		ssaVars.Add(n)
   475  		for _, slot := range debugInfo.VarSlots[varID] {
   476  			ssaVars.Add(debugInfo.Slots[slot].N)
   477  		}
   478  
   479  		if dvar := createComplexVar(fnsym, fn, ssa.VarID(varID), closureVars); dvar != nil {
   480  			decls = append(decls, n)
   481  			vars = append(vars, dvar)
   482  		}
   483  	}
   484  
   485  	return decls, vars, ssaVars
   486  }
   487  
   488  // createComplexVar builds a single DWARF variable entry and location list.
   489  func createComplexVar(fnsym *obj.LSym, fn *ir.Func, varID ssa.VarID, closureVars map[*ir.Name]int64) *dwarf.Var {
   490  	debug := fn.DebugInfo.(*ssa.FuncDebug)
   491  	n := debug.Vars[varID]
   492  
   493  	var tag int
   494  	switch n.Class {
   495  	case ir.PAUTO:
   496  		tag = dwarf.DW_TAG_variable
   497  	case ir.PPARAM, ir.PPARAMOUT:
   498  		tag = dwarf.DW_TAG_formal_parameter
   499  	default:
   500  		return nil
   501  	}
   502  
   503  	gotype := reflectdata.TypeLinksym(n.Type())
   504  	delete(fnsym.Func().Autot, gotype)
   505  	typename := dwarf.InfoPrefix + gotype.Name[len("type:"):]
   506  	inlIndex := 0
   507  	if base.Flag.GenDwarfInl > 1 {
   508  		if n.InlFormal() || n.InlLocal() {
   509  			inlIndex = posInlIndex(n.Pos()) + 1
   510  			if n.InlFormal() {
   511  				tag = dwarf.DW_TAG_formal_parameter
   512  			}
   513  		}
   514  	}
   515  	declpos := base.Ctxt.InnermostPos(n.Pos())
   516  	dvar := &dwarf.Var{
   517  		Name:          n.Sym().Name,
   518  		IsReturnValue: n.Class == ir.PPARAMOUT,
   519  		IsInlFormal:   n.InlFormal(),
   520  		Tag:           tag,
   521  		WithLoclist:   true,
   522  		Type:          base.Ctxt.Lookup(typename),
   523  		// The stack offset is used as a sorting key, so for decomposed
   524  		// variables just give it the first one. It's not used otherwise.
   525  		// This won't work well if the first slot hasn't been assigned a stack
   526  		// location, but it's not obvious how to do better.
   527  		StackOffset:   ssagen.StackOffset(debug.Slots[debug.VarSlots[varID][0]]),
   528  		DeclFile:      declpos.RelFilename(),
   529  		DeclLine:      declpos.RelLine(),
   530  		DeclCol:       declpos.RelCol(),
   531  		InlIndex:      int32(inlIndex),
   532  		ChildIndex:    -1,
   533  		DictIndex:     n.DictIndex,
   534  		ClosureOffset: closureOffset(n, closureVars),
   535  	}
   536  	list := debug.LocationLists[varID]
   537  	if len(list) != 0 {
   538  		dvar.PutLocationList = func(listSym, startPC dwarf.Sym) {
   539  			debug.PutLocationList(list, base.Ctxt, listSym.(*obj.LSym), startPC.(*obj.LSym))
   540  		}
   541  	}
   542  	return dvar
   543  }
   544  
   545  // RecordFlags records the specified command-line flags to be placed
   546  // in the DWARF info.
   547  func RecordFlags(flags ...string) {
   548  	if base.Ctxt.Pkgpath == "" {
   549  		panic("missing pkgpath")
   550  	}
   551  
   552  	type BoolFlag interface {
   553  		IsBoolFlag() bool
   554  	}
   555  	type CountFlag interface {
   556  		IsCountFlag() bool
   557  	}
   558  	var cmd bytes.Buffer
   559  	for _, name := range flags {
   560  		f := flag.Lookup(name)
   561  		if f == nil {
   562  			continue
   563  		}
   564  		getter := f.Value.(flag.Getter)
   565  		if getter.String() == f.DefValue {
   566  			// Flag has default value, so omit it.
   567  			continue
   568  		}
   569  		if bf, ok := f.Value.(BoolFlag); ok && bf.IsBoolFlag() {
   570  			val, ok := getter.Get().(bool)
   571  			if ok && val {
   572  				fmt.Fprintf(&cmd, " -%s", f.Name)
   573  				continue
   574  			}
   575  		}
   576  		if cf, ok := f.Value.(CountFlag); ok && cf.IsCountFlag() {
   577  			val, ok := getter.Get().(int)
   578  			if ok && val == 1 {
   579  				fmt.Fprintf(&cmd, " -%s", f.Name)
   580  				continue
   581  			}
   582  		}
   583  		fmt.Fprintf(&cmd, " -%s=%v", f.Name, getter.Get())
   584  	}
   585  
   586  	// Adds flag to producer string signaling whether regabi is turned on or
   587  	// off.
   588  	// Once regabi is turned on across the board and the relative GOEXPERIMENT
   589  	// knobs no longer exist this code should be removed.
   590  	if buildcfg.Experiment.RegabiArgs {
   591  		cmd.Write([]byte(" regabi"))
   592  	}
   593  
   594  	if cmd.Len() == 0 {
   595  		return
   596  	}
   597  	s := base.Ctxt.Lookup(dwarf.CUInfoPrefix + "producer." + base.Ctxt.Pkgpath)
   598  	s.Type = objabi.SDWARFCUINFO
   599  	// Sometimes (for example when building tests) we can link
   600  	// together two package main archives. So allow dups.
   601  	s.Set(obj.AttrDuplicateOK, true)
   602  	base.Ctxt.Data = append(base.Ctxt.Data, s)
   603  	s.P = cmd.Bytes()[1:]
   604  }
   605  
   606  // RecordPackageName records the name of the package being
   607  // compiled, so that the linker can save it in the compile unit's DIE.
   608  func RecordPackageName() {
   609  	s := base.Ctxt.Lookup(dwarf.CUInfoPrefix + "packagename." + base.Ctxt.Pkgpath)
   610  	s.Type = objabi.SDWARFCUINFO
   611  	// Sometimes (for example when building tests) we can link
   612  	// together two package main archives. So allow dups.
   613  	s.Set(obj.AttrDuplicateOK, true)
   614  	base.Ctxt.Data = append(base.Ctxt.Data, s)
   615  	s.P = []byte(types.LocalPkg.Name)
   616  }
   617  
   618  func closureOffset(n *ir.Name, closureVars map[*ir.Name]int64) int64 {
   619  	return closureVars[n]
   620  }
   621  

View as plain text