github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/src/cmd/link/internal/ld/dwarf.go (about)

     1  // Copyright 2010 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  // TODO/NICETOHAVE:
     6  //   - eliminate DW_CLS_ if not used
     7  //   - package info in compilation units
     8  //   - assign global variables and types to their packages
     9  //   - gdb uses c syntax, meaning clumsy quoting is needed for go identifiers. eg
    10  //     ptype struct '[]uint8' and qualifiers need to be quoted away
    11  //   - lexical scoping is lost, so gdb gets confused as to which 'main.i' you mean.
    12  //   - file:line info for variables
    13  //   - make strings a typedef so prettyprinters can see the underlying string type
    14  
    15  package ld
    16  
    17  import (
    18  	"cmd/internal/dwarf"
    19  	"cmd/internal/obj"
    20  	"fmt"
    21  	"log"
    22  	"os"
    23  	"strings"
    24  )
    25  
    26  type dwctxt struct {
    27  	linkctxt *Link
    28  }
    29  
    30  func (c dwctxt) PtrSize() int {
    31  	return SysArch.PtrSize
    32  }
    33  func (c dwctxt) AddInt(s dwarf.Sym, size int, i int64) {
    34  	ls := s.(*Symbol)
    35  	adduintxx(c.linkctxt, ls, uint64(i), size)
    36  }
    37  func (c dwctxt) AddBytes(s dwarf.Sym, b []byte) {
    38  	ls := s.(*Symbol)
    39  	Addbytes(ls, b)
    40  }
    41  func (c dwctxt) AddString(s dwarf.Sym, v string) {
    42  	Addstring(s.(*Symbol), v)
    43  }
    44  func (c dwctxt) SymValue(s dwarf.Sym) int64 {
    45  	return s.(*Symbol).Value
    46  }
    47  
    48  func (c dwctxt) AddAddress(s dwarf.Sym, data interface{}, value int64) {
    49  	if value != 0 {
    50  		value -= (data.(*Symbol)).Value
    51  	}
    52  	Addaddrplus(c.linkctxt, s.(*Symbol), data.(*Symbol), value)
    53  }
    54  
    55  func (c dwctxt) AddSectionOffset(s dwarf.Sym, size int, t interface{}, ofs int64) {
    56  	ls := s.(*Symbol)
    57  	switch size {
    58  	default:
    59  		Errorf(ls, "invalid size %d in adddwarfref\n", size)
    60  		fallthrough
    61  	case SysArch.PtrSize:
    62  		Addaddr(c.linkctxt, ls, t.(*Symbol))
    63  	case 4:
    64  		addaddrplus4(c.linkctxt, ls, t.(*Symbol), 0)
    65  	}
    66  	r := &ls.R[len(ls.R)-1]
    67  	r.Type = obj.R_DWARFREF
    68  	r.Add = ofs
    69  }
    70  
    71  /*
    72   * Offsets and sizes of the debug_* sections in the cout file.
    73   */
    74  var abbrevsym *Symbol
    75  var arangessec *Symbol
    76  var framesec *Symbol
    77  var infosec *Symbol
    78  var linesec *Symbol
    79  
    80  var gdbscript string
    81  
    82  var dwarfp []*Symbol
    83  
    84  func writeabbrev(ctxt *Link, syms []*Symbol) []*Symbol {
    85  	s := ctxt.Syms.Lookup(".debug_abbrev", 0)
    86  	s.Type = obj.SDWARFSECT
    87  	abbrevsym = s
    88  	Addbytes(s, dwarf.GetAbbrev())
    89  	return append(syms, s)
    90  }
    91  
    92  /*
    93   * Root DIEs for compilation units, types and global variables.
    94   */
    95  var dwroot dwarf.DWDie
    96  
    97  var dwtypes dwarf.DWDie
    98  
    99  var dwglobals dwarf.DWDie
   100  
   101  func newattr(die *dwarf.DWDie, attr uint16, cls int, value int64, data interface{}) *dwarf.DWAttr {
   102  	a := new(dwarf.DWAttr)
   103  	a.Link = die.Attr
   104  	die.Attr = a
   105  	a.Atr = attr
   106  	a.Cls = uint8(cls)
   107  	a.Value = value
   108  	a.Data = data
   109  	return a
   110  }
   111  
   112  // Each DIE (except the root ones) has at least 1 attribute: its
   113  // name. getattr moves the desired one to the front so
   114  // frequently searched ones are found faster.
   115  func getattr(die *dwarf.DWDie, attr uint16) *dwarf.DWAttr {
   116  	if die.Attr.Atr == attr {
   117  		return die.Attr
   118  	}
   119  
   120  	a := die.Attr
   121  	b := a.Link
   122  	for b != nil {
   123  		if b.Atr == attr {
   124  			a.Link = b.Link
   125  			b.Link = die.Attr
   126  			die.Attr = b
   127  			return b
   128  		}
   129  
   130  		a = b
   131  		b = b.Link
   132  	}
   133  
   134  	return nil
   135  }
   136  
   137  // Every DIE has at least a AT_name attribute (but it will only be
   138  // written out if it is listed in the abbrev).
   139  func newdie(ctxt *Link, parent *dwarf.DWDie, abbrev int, name string, version int) *dwarf.DWDie {
   140  	die := new(dwarf.DWDie)
   141  	die.Abbrev = abbrev
   142  	die.Link = parent.Child
   143  	parent.Child = die
   144  
   145  	newattr(die, dwarf.DW_AT_name, dwarf.DW_CLS_STRING, int64(len(name)), name)
   146  
   147  	if name != "" && (abbrev <= dwarf.DW_ABRV_VARIABLE || abbrev >= dwarf.DW_ABRV_NULLTYPE) {
   148  		if abbrev != dwarf.DW_ABRV_VARIABLE || version == 0 {
   149  			sym := ctxt.Syms.Lookup(dwarf.InfoPrefix+name, version)
   150  			sym.Attr |= AttrHidden
   151  			sym.Type = obj.SDWARFINFO
   152  			die.Sym = sym
   153  		}
   154  	}
   155  
   156  	return die
   157  }
   158  
   159  func walktypedef(die *dwarf.DWDie) *dwarf.DWDie {
   160  	if die == nil {
   161  		return nil
   162  	}
   163  	// Resolve typedef if present.
   164  	if die.Abbrev == dwarf.DW_ABRV_TYPEDECL {
   165  		for attr := die.Attr; attr != nil; attr = attr.Link {
   166  			if attr.Atr == dwarf.DW_AT_type && attr.Cls == dwarf.DW_CLS_REFERENCE && attr.Data != nil {
   167  				return attr.Data.(*dwarf.DWDie)
   168  			}
   169  		}
   170  	}
   171  
   172  	return die
   173  }
   174  
   175  func walksymtypedef(ctxt *Link, s *Symbol) *Symbol {
   176  	if t := ctxt.Syms.ROLookup(s.Name+"..def", int(s.Version)); t != nil {
   177  		return t
   178  	}
   179  	return s
   180  }
   181  
   182  // Find child by AT_name using hashtable if available or linear scan
   183  // if not.
   184  func findchild(die *dwarf.DWDie, name string) *dwarf.DWDie {
   185  	var prev *dwarf.DWDie
   186  	for ; die != prev; prev, die = die, walktypedef(die) {
   187  		for a := die.Child; a != nil; a = a.Link {
   188  			if name == getattr(a, dwarf.DW_AT_name).Data {
   189  				return a
   190  			}
   191  		}
   192  		continue
   193  	}
   194  	return nil
   195  }
   196  
   197  // Used to avoid string allocation when looking up dwarf symbols
   198  var prefixBuf = []byte(dwarf.InfoPrefix)
   199  
   200  func find(ctxt *Link, name string) *Symbol {
   201  	n := append(prefixBuf, name...)
   202  	// The string allocation below is optimized away because it is only used in a map lookup.
   203  	s := ctxt.Syms.ROLookup(string(n), 0)
   204  	prefixBuf = n[:len(dwarf.InfoPrefix)]
   205  	if s != nil && s.Type == obj.SDWARFINFO {
   206  		return s
   207  	}
   208  	return nil
   209  }
   210  
   211  func mustFind(ctxt *Link, name string) *Symbol {
   212  	r := find(ctxt, name)
   213  	if r == nil {
   214  		Exitf("dwarf find: cannot find %s", name)
   215  	}
   216  	return r
   217  }
   218  
   219  func adddwarfref(ctxt *Link, s *Symbol, t *Symbol, size int) int64 {
   220  	var result int64
   221  	switch size {
   222  	default:
   223  		Errorf(s, "invalid size %d in adddwarfref\n", size)
   224  		fallthrough
   225  	case SysArch.PtrSize:
   226  		result = Addaddr(ctxt, s, t)
   227  	case 4:
   228  		result = addaddrplus4(ctxt, s, t, 0)
   229  	}
   230  	r := &s.R[len(s.R)-1]
   231  	r.Type = obj.R_DWARFREF
   232  	return result
   233  }
   234  
   235  func newrefattr(die *dwarf.DWDie, attr uint16, ref *Symbol) *dwarf.DWAttr {
   236  	if ref == nil {
   237  		return nil
   238  	}
   239  	return newattr(die, attr, dwarf.DW_CLS_REFERENCE, 0, ref)
   240  }
   241  
   242  func putdies(linkctxt *Link, ctxt dwarf.Context, syms []*Symbol, die *dwarf.DWDie) []*Symbol {
   243  	for ; die != nil; die = die.Link {
   244  		syms = putdie(linkctxt, ctxt, syms, die)
   245  	}
   246  	Adduint8(linkctxt, syms[len(syms)-1], 0)
   247  
   248  	return syms
   249  }
   250  
   251  func dtolsym(s dwarf.Sym) *Symbol {
   252  	if s == nil {
   253  		return nil
   254  	}
   255  	return s.(*Symbol)
   256  }
   257  
   258  func putdie(linkctxt *Link, ctxt dwarf.Context, syms []*Symbol, die *dwarf.DWDie) []*Symbol {
   259  	s := dtolsym(die.Sym)
   260  	if s == nil {
   261  		s = syms[len(syms)-1]
   262  	} else {
   263  		if s.Attr.OnList() {
   264  			log.Fatalf("symbol %s listed multiple times", s.Name)
   265  		}
   266  		s.Attr |= AttrOnList
   267  		syms = append(syms, s)
   268  	}
   269  	dwarf.Uleb128put(ctxt, s, int64(die.Abbrev))
   270  	dwarf.PutAttrs(ctxt, s, die.Abbrev, die.Attr)
   271  	if dwarf.HasChildren(die) {
   272  		return putdies(linkctxt, ctxt, syms, die.Child)
   273  	}
   274  	return syms
   275  }
   276  
   277  func reverselist(list **dwarf.DWDie) {
   278  	curr := *list
   279  	var prev *dwarf.DWDie
   280  	for curr != nil {
   281  		var next *dwarf.DWDie = curr.Link
   282  		curr.Link = prev
   283  		prev = curr
   284  		curr = next
   285  	}
   286  
   287  	*list = prev
   288  }
   289  
   290  func reversetree(list **dwarf.DWDie) {
   291  	reverselist(list)
   292  	for die := *list; die != nil; die = die.Link {
   293  		if dwarf.HasChildren(die) {
   294  			reversetree(&die.Child)
   295  		}
   296  	}
   297  }
   298  
   299  func newmemberoffsetattr(die *dwarf.DWDie, offs int32) {
   300  	var block [20]byte
   301  	b := append(block[:0], dwarf.DW_OP_plus_uconst)
   302  	b = dwarf.AppendUleb128(b, uint64(offs))
   303  	newattr(die, dwarf.DW_AT_data_member_location, dwarf.DW_CLS_BLOCK, int64(len(b)), b)
   304  }
   305  
   306  // GDB doesn't like FORM_addr for AT_location, so emit a
   307  // location expression that evals to a const.
   308  func newabslocexprattr(die *dwarf.DWDie, addr int64, sym *Symbol) {
   309  	newattr(die, dwarf.DW_AT_location, dwarf.DW_CLS_ADDRESS, addr, sym)
   310  	// below
   311  }
   312  
   313  // Lookup predefined types
   314  func lookupOrDiag(ctxt *Link, n string) *Symbol {
   315  	s := ctxt.Syms.ROLookup(n, 0)
   316  	if s == nil || s.Size == 0 {
   317  		Exitf("dwarf: missing type: %s", n)
   318  	}
   319  
   320  	return s
   321  }
   322  
   323  func dotypedef(ctxt *Link, parent *dwarf.DWDie, name string, def *dwarf.DWDie) {
   324  	// Only emit typedefs for real names.
   325  	if strings.HasPrefix(name, "map[") {
   326  		return
   327  	}
   328  	if strings.HasPrefix(name, "struct {") {
   329  		return
   330  	}
   331  	if strings.HasPrefix(name, "chan ") {
   332  		return
   333  	}
   334  	if name[0] == '[' || name[0] == '*' {
   335  		return
   336  	}
   337  	if def == nil {
   338  		Errorf(nil, "dwarf: bad def in dotypedef")
   339  	}
   340  
   341  	sym := ctxt.Syms.Lookup(dtolsym(def.Sym).Name+"..def", 0)
   342  	sym.Attr |= AttrHidden
   343  	sym.Type = obj.SDWARFINFO
   344  	def.Sym = sym
   345  
   346  	// The typedef entry must be created after the def,
   347  	// so that future lookups will find the typedef instead
   348  	// of the real definition. This hooks the typedef into any
   349  	// circular definition loops, so that gdb can understand them.
   350  	die := newdie(ctxt, parent, dwarf.DW_ABRV_TYPEDECL, name, 0)
   351  
   352  	newrefattr(die, dwarf.DW_AT_type, sym)
   353  }
   354  
   355  // Define gotype, for composite ones recurse into constituents.
   356  func defgotype(ctxt *Link, gotype *Symbol) *Symbol {
   357  	if gotype == nil {
   358  		return mustFind(ctxt, "<unspecified>")
   359  	}
   360  
   361  	if !strings.HasPrefix(gotype.Name, "type.") {
   362  		Errorf(gotype, "dwarf: type name doesn't start with \"type.\"")
   363  		return mustFind(ctxt, "<unspecified>")
   364  	}
   365  
   366  	name := gotype.Name[5:] // could also decode from Type.string
   367  
   368  	sdie := find(ctxt, name)
   369  
   370  	if sdie != nil {
   371  		return sdie
   372  	}
   373  
   374  	return newtype(ctxt, gotype).Sym.(*Symbol)
   375  }
   376  
   377  func newtype(ctxt *Link, gotype *Symbol) *dwarf.DWDie {
   378  	name := gotype.Name[5:] // could also decode from Type.string
   379  	kind := decodetypeKind(gotype)
   380  	bytesize := decodetypeSize(ctxt.Arch, gotype)
   381  
   382  	var die *dwarf.DWDie
   383  	switch kind {
   384  	case obj.KindBool:
   385  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BASETYPE, name, 0)
   386  		newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_boolean, 0)
   387  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   388  
   389  	case obj.KindInt,
   390  		obj.KindInt8,
   391  		obj.KindInt16,
   392  		obj.KindInt32,
   393  		obj.KindInt64:
   394  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BASETYPE, name, 0)
   395  		newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_signed, 0)
   396  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   397  
   398  	case obj.KindUint,
   399  		obj.KindUint8,
   400  		obj.KindUint16,
   401  		obj.KindUint32,
   402  		obj.KindUint64,
   403  		obj.KindUintptr:
   404  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BASETYPE, name, 0)
   405  		newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_unsigned, 0)
   406  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   407  
   408  	case obj.KindFloat32,
   409  		obj.KindFloat64:
   410  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BASETYPE, name, 0)
   411  		newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_float, 0)
   412  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   413  
   414  	case obj.KindComplex64,
   415  		obj.KindComplex128:
   416  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BASETYPE, name, 0)
   417  		newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_complex_float, 0)
   418  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   419  
   420  	case obj.KindArray:
   421  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_ARRAYTYPE, name, 0)
   422  		dotypedef(ctxt, &dwtypes, name, die)
   423  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   424  		s := decodetypeArrayElem(gotype)
   425  		newrefattr(die, dwarf.DW_AT_type, defgotype(ctxt, s))
   426  		fld := newdie(ctxt, die, dwarf.DW_ABRV_ARRAYRANGE, "range", 0)
   427  
   428  		// use actual length not upper bound; correct for 0-length arrays.
   429  		newattr(fld, dwarf.DW_AT_count, dwarf.DW_CLS_CONSTANT, decodetypeArrayLen(ctxt.Arch, gotype), 0)
   430  
   431  		newrefattr(fld, dwarf.DW_AT_type, mustFind(ctxt, "uintptr"))
   432  
   433  	case obj.KindChan:
   434  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_CHANTYPE, name, 0)
   435  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   436  		s := decodetypeChanElem(gotype)
   437  		newrefattr(die, dwarf.DW_AT_go_elem, defgotype(ctxt, s))
   438  		// Save elem type for synthesizechantypes. We could synthesize here
   439  		// but that would change the order of DIEs we output.
   440  		newrefattr(die, dwarf.DW_AT_type, s)
   441  
   442  	case obj.KindFunc:
   443  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_FUNCTYPE, name, 0)
   444  		dotypedef(ctxt, &dwtypes, name, die)
   445  		newrefattr(die, dwarf.DW_AT_type, mustFind(ctxt, "void"))
   446  		nfields := decodetypeFuncInCount(ctxt.Arch, gotype)
   447  		var fld *dwarf.DWDie
   448  		var s *Symbol
   449  		for i := 0; i < nfields; i++ {
   450  			s = decodetypeFuncInType(gotype, i)
   451  			fld = newdie(ctxt, die, dwarf.DW_ABRV_FUNCTYPEPARAM, s.Name[5:], 0)
   452  			newrefattr(fld, dwarf.DW_AT_type, defgotype(ctxt, s))
   453  		}
   454  
   455  		if decodetypeFuncDotdotdot(ctxt.Arch, gotype) {
   456  			newdie(ctxt, die, dwarf.DW_ABRV_DOTDOTDOT, "...", 0)
   457  		}
   458  		nfields = decodetypeFuncOutCount(ctxt.Arch, gotype)
   459  		for i := 0; i < nfields; i++ {
   460  			s = decodetypeFuncOutType(ctxt.Arch, gotype, i)
   461  			fld = newdie(ctxt, die, dwarf.DW_ABRV_FUNCTYPEPARAM, s.Name[5:], 0)
   462  			newrefattr(fld, dwarf.DW_AT_type, defptrto(ctxt, defgotype(ctxt, s)))
   463  		}
   464  
   465  	case obj.KindInterface:
   466  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_IFACETYPE, name, 0)
   467  		dotypedef(ctxt, &dwtypes, name, die)
   468  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   469  		nfields := int(decodetypeIfaceMethodCount(ctxt.Arch, gotype))
   470  		var s *Symbol
   471  		if nfields == 0 {
   472  			s = lookupOrDiag(ctxt, "type.runtime.eface")
   473  		} else {
   474  			s = lookupOrDiag(ctxt, "type.runtime.iface")
   475  		}
   476  		newrefattr(die, dwarf.DW_AT_type, defgotype(ctxt, s))
   477  
   478  	case obj.KindMap:
   479  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_MAPTYPE, name, 0)
   480  		s := decodetypeMapKey(gotype)
   481  		newrefattr(die, dwarf.DW_AT_go_key, defgotype(ctxt, s))
   482  		s = decodetypeMapValue(gotype)
   483  		newrefattr(die, dwarf.DW_AT_go_elem, defgotype(ctxt, s))
   484  		// Save gotype for use in synthesizemaptypes. We could synthesize here,
   485  		// but that would change the order of the DIEs.
   486  		newrefattr(die, dwarf.DW_AT_type, gotype)
   487  
   488  	case obj.KindPtr:
   489  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_PTRTYPE, name, 0)
   490  		dotypedef(ctxt, &dwtypes, name, die)
   491  		s := decodetypePtrElem(gotype)
   492  		newrefattr(die, dwarf.DW_AT_type, defgotype(ctxt, s))
   493  
   494  	case obj.KindSlice:
   495  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_SLICETYPE, name, 0)
   496  		dotypedef(ctxt, &dwtypes, name, die)
   497  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   498  		s := decodetypeArrayElem(gotype)
   499  		elem := defgotype(ctxt, s)
   500  		newrefattr(die, dwarf.DW_AT_go_elem, elem)
   501  
   502  	case obj.KindString:
   503  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_STRINGTYPE, name, 0)
   504  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   505  
   506  	case obj.KindStruct:
   507  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_STRUCTTYPE, name, 0)
   508  		dotypedef(ctxt, &dwtypes, name, die)
   509  		newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
   510  		nfields := decodetypeStructFieldCount(ctxt.Arch, gotype)
   511  		var f string
   512  		var fld *dwarf.DWDie
   513  		var s *Symbol
   514  		for i := 0; i < nfields; i++ {
   515  			f = decodetypeStructFieldName(gotype, i)
   516  			s = decodetypeStructFieldType(gotype, i)
   517  			if f == "" {
   518  				f = s.Name[5:] // skip "type."
   519  			}
   520  			fld = newdie(ctxt, die, dwarf.DW_ABRV_STRUCTFIELD, f, 0)
   521  			newrefattr(fld, dwarf.DW_AT_type, defgotype(ctxt, s))
   522  			newmemberoffsetattr(fld, int32(decodetypeStructFieldOffs(ctxt.Arch, gotype, i)))
   523  		}
   524  
   525  	case obj.KindUnsafePointer:
   526  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BARE_PTRTYPE, name, 0)
   527  
   528  	default:
   529  		Errorf(gotype, "dwarf: definition of unknown kind %d", kind)
   530  		die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_TYPEDECL, name, 0)
   531  		newrefattr(die, dwarf.DW_AT_type, mustFind(ctxt, "<unspecified>"))
   532  	}
   533  
   534  	newattr(die, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, int64(kind), 0)
   535  
   536  	if _, ok := prototypedies[gotype.Name]; ok {
   537  		prototypedies[gotype.Name] = die
   538  	}
   539  
   540  	return die
   541  }
   542  
   543  func nameFromDIESym(dwtype *Symbol) string {
   544  	return strings.TrimSuffix(dwtype.Name[len(dwarf.InfoPrefix):], "..def")
   545  }
   546  
   547  // Find or construct *T given T.
   548  func defptrto(ctxt *Link, dwtype *Symbol) *Symbol {
   549  	ptrname := "*" + nameFromDIESym(dwtype)
   550  	die := find(ctxt, ptrname)
   551  	if die == nil {
   552  		pdie := newdie(ctxt, &dwtypes, dwarf.DW_ABRV_PTRTYPE, ptrname, 0)
   553  		newrefattr(pdie, dwarf.DW_AT_type, dwtype)
   554  		return dtolsym(pdie.Sym)
   555  	}
   556  
   557  	return die
   558  }
   559  
   560  // Copies src's children into dst. Copies attributes by value.
   561  // DWAttr.data is copied as pointer only. If except is one of
   562  // the top-level children, it will not be copied.
   563  func copychildrenexcept(ctxt *Link, dst *dwarf.DWDie, src *dwarf.DWDie, except *dwarf.DWDie) {
   564  	for src = src.Child; src != nil; src = src.Link {
   565  		if src == except {
   566  			continue
   567  		}
   568  		c := newdie(ctxt, dst, src.Abbrev, getattr(src, dwarf.DW_AT_name).Data.(string), 0)
   569  		for a := src.Attr; a != nil; a = a.Link {
   570  			newattr(c, a.Atr, int(a.Cls), a.Value, a.Data)
   571  		}
   572  		copychildrenexcept(ctxt, c, src, nil)
   573  	}
   574  
   575  	reverselist(&dst.Child)
   576  }
   577  
   578  func copychildren(ctxt *Link, dst *dwarf.DWDie, src *dwarf.DWDie) {
   579  	copychildrenexcept(ctxt, dst, src, nil)
   580  }
   581  
   582  // Search children (assumed to have TAG_member) for the one named
   583  // field and set its AT_type to dwtype
   584  func substitutetype(structdie *dwarf.DWDie, field string, dwtype *Symbol) {
   585  	child := findchild(structdie, field)
   586  	if child == nil {
   587  		Exitf("dwarf substitutetype: %s does not have member %s",
   588  			getattr(structdie, dwarf.DW_AT_name).Data, field)
   589  		return
   590  	}
   591  
   592  	a := getattr(child, dwarf.DW_AT_type)
   593  	if a != nil {
   594  		a.Data = dwtype
   595  	} else {
   596  		newrefattr(child, dwarf.DW_AT_type, dwtype)
   597  	}
   598  }
   599  
   600  func findprotodie(ctxt *Link, name string) *dwarf.DWDie {
   601  	die, ok := prototypedies[name]
   602  	if ok && die == nil {
   603  		defgotype(ctxt, lookupOrDiag(ctxt, name))
   604  		die = prototypedies[name]
   605  	}
   606  	return die
   607  }
   608  
   609  func synthesizestringtypes(ctxt *Link, die *dwarf.DWDie) {
   610  	prototype := walktypedef(findprotodie(ctxt, "type.runtime.stringStructDWARF"))
   611  	if prototype == nil {
   612  		return
   613  	}
   614  
   615  	for ; die != nil; die = die.Link {
   616  		if die.Abbrev != dwarf.DW_ABRV_STRINGTYPE {
   617  			continue
   618  		}
   619  		copychildren(ctxt, die, prototype)
   620  	}
   621  }
   622  
   623  func synthesizeslicetypes(ctxt *Link, die *dwarf.DWDie) {
   624  	prototype := walktypedef(findprotodie(ctxt, "type.runtime.slice"))
   625  	if prototype == nil {
   626  		return
   627  	}
   628  
   629  	for ; die != nil; die = die.Link {
   630  		if die.Abbrev != dwarf.DW_ABRV_SLICETYPE {
   631  			continue
   632  		}
   633  		copychildren(ctxt, die, prototype)
   634  		elem := getattr(die, dwarf.DW_AT_go_elem).Data.(*Symbol)
   635  		substitutetype(die, "array", defptrto(ctxt, elem))
   636  	}
   637  }
   638  
   639  func mkinternaltypename(base string, arg1 string, arg2 string) string {
   640  	var buf string
   641  
   642  	if arg2 == "" {
   643  		buf = fmt.Sprintf("%s<%s>", base, arg1)
   644  	} else {
   645  		buf = fmt.Sprintf("%s<%s,%s>", base, arg1, arg2)
   646  	}
   647  	n := buf
   648  	return n
   649  }
   650  
   651  // synthesizemaptypes is way too closely married to runtime/hashmap.c
   652  const (
   653  	MaxKeySize = 128
   654  	MaxValSize = 128
   655  	BucketSize = 8
   656  )
   657  
   658  func mkinternaltype(ctxt *Link, abbrev int, typename, keyname, valname string, f func(*dwarf.DWDie)) *Symbol {
   659  	name := mkinternaltypename(typename, keyname, valname)
   660  	symname := dwarf.InfoPrefix + name
   661  	s := ctxt.Syms.ROLookup(symname, 0)
   662  	if s != nil && s.Type == obj.SDWARFINFO {
   663  		return s
   664  	}
   665  	die := newdie(ctxt, &dwtypes, abbrev, name, 0)
   666  	f(die)
   667  	return dtolsym(die.Sym)
   668  }
   669  
   670  func synthesizemaptypes(ctxt *Link, die *dwarf.DWDie) {
   671  	hash := walktypedef(findprotodie(ctxt, "type.runtime.hmap"))
   672  	bucket := walktypedef(findprotodie(ctxt, "type.runtime.bmap"))
   673  
   674  	if hash == nil {
   675  		return
   676  	}
   677  
   678  	for ; die != nil; die = die.Link {
   679  		if die.Abbrev != dwarf.DW_ABRV_MAPTYPE {
   680  			continue
   681  		}
   682  		gotype := getattr(die, dwarf.DW_AT_type).Data.(*Symbol)
   683  		keytype := decodetypeMapKey(gotype)
   684  		valtype := decodetypeMapValue(gotype)
   685  		keysize, valsize := decodetypeSize(ctxt.Arch, keytype), decodetypeSize(ctxt.Arch, valtype)
   686  		keytype, valtype = walksymtypedef(ctxt, defgotype(ctxt, keytype)), walksymtypedef(ctxt, defgotype(ctxt, valtype))
   687  
   688  		// compute size info like hashmap.c does.
   689  		indirectKey, indirectVal := false, false
   690  		if keysize > MaxKeySize {
   691  			keysize = int64(SysArch.PtrSize)
   692  			indirectKey = true
   693  		}
   694  		if valsize > MaxValSize {
   695  			valsize = int64(SysArch.PtrSize)
   696  			indirectVal = true
   697  		}
   698  
   699  		// Construct type to represent an array of BucketSize keys
   700  		keyname := nameFromDIESym(keytype)
   701  		dwhks := mkinternaltype(ctxt, dwarf.DW_ABRV_ARRAYTYPE, "[]key", keyname, "", func(dwhk *dwarf.DWDie) {
   702  			newattr(dwhk, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, BucketSize*keysize, 0)
   703  			t := keytype
   704  			if indirectKey {
   705  				t = defptrto(ctxt, keytype)
   706  			}
   707  			newrefattr(dwhk, dwarf.DW_AT_type, t)
   708  			fld := newdie(ctxt, dwhk, dwarf.DW_ABRV_ARRAYRANGE, "size", 0)
   709  			newattr(fld, dwarf.DW_AT_count, dwarf.DW_CLS_CONSTANT, BucketSize, 0)
   710  			newrefattr(fld, dwarf.DW_AT_type, mustFind(ctxt, "uintptr"))
   711  		})
   712  
   713  		// Construct type to represent an array of BucketSize values
   714  		valname := nameFromDIESym(valtype)
   715  		dwhvs := mkinternaltype(ctxt, dwarf.DW_ABRV_ARRAYTYPE, "[]val", valname, "", func(dwhv *dwarf.DWDie) {
   716  			newattr(dwhv, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, BucketSize*valsize, 0)
   717  			t := valtype
   718  			if indirectVal {
   719  				t = defptrto(ctxt, valtype)
   720  			}
   721  			newrefattr(dwhv, dwarf.DW_AT_type, t)
   722  			fld := newdie(ctxt, dwhv, dwarf.DW_ABRV_ARRAYRANGE, "size", 0)
   723  			newattr(fld, dwarf.DW_AT_count, dwarf.DW_CLS_CONSTANT, BucketSize, 0)
   724  			newrefattr(fld, dwarf.DW_AT_type, mustFind(ctxt, "uintptr"))
   725  		})
   726  
   727  		// Construct bucket<K,V>
   728  		dwhbs := mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "bucket", keyname, valname, func(dwhb *dwarf.DWDie) {
   729  			// Copy over all fields except the field "data" from the generic
   730  			// bucket. "data" will be replaced with keys/values below.
   731  			copychildrenexcept(ctxt, dwhb, bucket, findchild(bucket, "data"))
   732  
   733  			fld := newdie(ctxt, dwhb, dwarf.DW_ABRV_STRUCTFIELD, "keys", 0)
   734  			newrefattr(fld, dwarf.DW_AT_type, dwhks)
   735  			newmemberoffsetattr(fld, BucketSize)
   736  			fld = newdie(ctxt, dwhb, dwarf.DW_ABRV_STRUCTFIELD, "values", 0)
   737  			newrefattr(fld, dwarf.DW_AT_type, dwhvs)
   738  			newmemberoffsetattr(fld, BucketSize+BucketSize*int32(keysize))
   739  			fld = newdie(ctxt, dwhb, dwarf.DW_ABRV_STRUCTFIELD, "overflow", 0)
   740  			newrefattr(fld, dwarf.DW_AT_type, defptrto(ctxt, dtolsym(dwhb.Sym)))
   741  			newmemberoffsetattr(fld, BucketSize+BucketSize*(int32(keysize)+int32(valsize)))
   742  			if SysArch.RegSize > SysArch.PtrSize {
   743  				fld = newdie(ctxt, dwhb, dwarf.DW_ABRV_STRUCTFIELD, "pad", 0)
   744  				newrefattr(fld, dwarf.DW_AT_type, mustFind(ctxt, "uintptr"))
   745  				newmemberoffsetattr(fld, BucketSize+BucketSize*(int32(keysize)+int32(valsize))+int32(SysArch.PtrSize))
   746  			}
   747  
   748  			newattr(dwhb, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, BucketSize+BucketSize*keysize+BucketSize*valsize+int64(SysArch.RegSize), 0)
   749  		})
   750  
   751  		// Construct hash<K,V>
   752  		dwhs := mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "hash", keyname, valname, func(dwh *dwarf.DWDie) {
   753  			copychildren(ctxt, dwh, hash)
   754  			substitutetype(dwh, "buckets", defptrto(ctxt, dwhbs))
   755  			substitutetype(dwh, "oldbuckets", defptrto(ctxt, dwhbs))
   756  			newattr(dwh, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, getattr(hash, dwarf.DW_AT_byte_size).Value, nil)
   757  		})
   758  
   759  		// make map type a pointer to hash<K,V>
   760  		newrefattr(die, dwarf.DW_AT_type, defptrto(ctxt, dwhs))
   761  	}
   762  }
   763  
   764  func synthesizechantypes(ctxt *Link, die *dwarf.DWDie) {
   765  	sudog := walktypedef(findprotodie(ctxt, "type.runtime.sudog"))
   766  	waitq := walktypedef(findprotodie(ctxt, "type.runtime.waitq"))
   767  	hchan := walktypedef(findprotodie(ctxt, "type.runtime.hchan"))
   768  	if sudog == nil || waitq == nil || hchan == nil {
   769  		return
   770  	}
   771  
   772  	sudogsize := int(getattr(sudog, dwarf.DW_AT_byte_size).Value)
   773  
   774  	for ; die != nil; die = die.Link {
   775  		if die.Abbrev != dwarf.DW_ABRV_CHANTYPE {
   776  			continue
   777  		}
   778  		elemgotype := getattr(die, dwarf.DW_AT_type).Data.(*Symbol)
   779  		elemsize := decodetypeSize(ctxt.Arch, elemgotype)
   780  		elemname := elemgotype.Name[5:]
   781  		elemtype := walksymtypedef(ctxt, defgotype(ctxt, elemgotype))
   782  
   783  		// sudog<T>
   784  		dwss := mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "sudog", elemname, "", func(dws *dwarf.DWDie) {
   785  			copychildren(ctxt, dws, sudog)
   786  			substitutetype(dws, "elem", elemtype)
   787  			if elemsize > 8 {
   788  				elemsize -= 8
   789  			} else {
   790  				elemsize = 0
   791  			}
   792  			newattr(dws, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, int64(sudogsize)+elemsize, nil)
   793  		})
   794  
   795  		// waitq<T>
   796  		dwws := mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "waitq", elemname, "", func(dww *dwarf.DWDie) {
   797  
   798  			copychildren(ctxt, dww, waitq)
   799  			substitutetype(dww, "first", defptrto(ctxt, dwss))
   800  			substitutetype(dww, "last", defptrto(ctxt, dwss))
   801  			newattr(dww, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, getattr(waitq, dwarf.DW_AT_byte_size).Value, nil)
   802  		})
   803  
   804  		// hchan<T>
   805  		dwhs := mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "hchan", elemname, "", func(dwh *dwarf.DWDie) {
   806  			copychildren(ctxt, dwh, hchan)
   807  			substitutetype(dwh, "recvq", dwws)
   808  			substitutetype(dwh, "sendq", dwws)
   809  			newattr(dwh, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, getattr(hchan, dwarf.DW_AT_byte_size).Value, nil)
   810  		})
   811  
   812  		newrefattr(die, dwarf.DW_AT_type, defptrto(ctxt, dwhs))
   813  	}
   814  }
   815  
   816  // For use with pass.c::genasmsym
   817  func defdwsymb(ctxt *Link, sym *Symbol, s string, t SymbolType, v int64, gotype *Symbol) {
   818  	if strings.HasPrefix(s, "go.string.") {
   819  		return
   820  	}
   821  	if strings.HasPrefix(s, "runtime.gcbits.") {
   822  		return
   823  	}
   824  
   825  	if strings.HasPrefix(s, "type.") && s != "type.*" && !strings.HasPrefix(s, "type..") {
   826  		defgotype(ctxt, sym)
   827  		return
   828  	}
   829  
   830  	var dv *dwarf.DWDie
   831  
   832  	var dt *Symbol
   833  	switch t {
   834  	default:
   835  		return
   836  
   837  	case DataSym, BSSSym:
   838  		dv = newdie(ctxt, &dwglobals, dwarf.DW_ABRV_VARIABLE, s, int(sym.Version))
   839  		newabslocexprattr(dv, v, sym)
   840  		if sym.Version == 0 {
   841  			newattr(dv, dwarf.DW_AT_external, dwarf.DW_CLS_FLAG, 1, 0)
   842  		}
   843  		fallthrough
   844  
   845  	case AutoSym, ParamSym:
   846  		dt = defgotype(ctxt, gotype)
   847  	}
   848  
   849  	if dv != nil {
   850  		newrefattr(dv, dwarf.DW_AT_type, dt)
   851  	}
   852  }
   853  
   854  func movetomodule(parent *dwarf.DWDie) {
   855  	die := dwroot.Child.Child
   856  	if die == nil {
   857  		dwroot.Child.Child = parent.Child
   858  		return
   859  	}
   860  	for die.Link != nil {
   861  		die = die.Link
   862  	}
   863  	die.Link = parent.Child
   864  }
   865  
   866  // If the pcln table contains runtime/runtime.go, use that to set gdbscript path.
   867  func finddebugruntimepath(s *Symbol) {
   868  	if gdbscript != "" {
   869  		return
   870  	}
   871  
   872  	for i := range s.FuncInfo.File {
   873  		f := s.FuncInfo.File[i]
   874  		if i := strings.Index(f.Name, "runtime/runtime.go"); i >= 0 {
   875  			gdbscript = f.Name[:i] + "runtime/runtime-gdb.py"
   876  			break
   877  		}
   878  	}
   879  }
   880  
   881  /*
   882   * Generate a sequence of opcodes that is as short as possible.
   883   * See section 6.2.5
   884   */
   885  const (
   886  	LINE_BASE   = -4
   887  	LINE_RANGE  = 10
   888  	PC_RANGE    = (255 - OPCODE_BASE) / LINE_RANGE
   889  	OPCODE_BASE = 10
   890  )
   891  
   892  func putpclcdelta(linkctxt *Link, ctxt dwarf.Context, s *Symbol, deltaPC uint64, deltaLC int64) {
   893  	// Choose a special opcode that minimizes the number of bytes needed to
   894  	// encode the remaining PC delta and LC delta.
   895  	var opcode int64
   896  	if deltaLC < LINE_BASE {
   897  		if deltaPC >= PC_RANGE {
   898  			opcode = OPCODE_BASE + (LINE_RANGE * PC_RANGE)
   899  		} else {
   900  			opcode = OPCODE_BASE + (LINE_RANGE * int64(deltaPC))
   901  		}
   902  	} else if deltaLC < LINE_BASE+LINE_RANGE {
   903  		if deltaPC >= PC_RANGE {
   904  			opcode = OPCODE_BASE + (deltaLC - LINE_BASE) + (LINE_RANGE * PC_RANGE)
   905  			if opcode > 255 {
   906  				opcode -= LINE_RANGE
   907  			}
   908  		} else {
   909  			opcode = OPCODE_BASE + (deltaLC - LINE_BASE) + (LINE_RANGE * int64(deltaPC))
   910  		}
   911  	} else {
   912  		if deltaPC <= PC_RANGE {
   913  			opcode = OPCODE_BASE + (LINE_RANGE - 1) + (LINE_RANGE * int64(deltaPC))
   914  			if opcode > 255 {
   915  				opcode = 255
   916  			}
   917  		} else {
   918  			// Use opcode 249 (pc+=23, lc+=5) or 255 (pc+=24, lc+=1).
   919  			//
   920  			// Let x=deltaPC-PC_RANGE.  If we use opcode 255, x will be the remaining
   921  			// deltaPC that we need to encode separately before emitting 255.  If we
   922  			// use opcode 249, we will need to encode x+1.  If x+1 takes one more
   923  			// byte to encode than x, then we use opcode 255.
   924  			//
   925  			// In all other cases x and x+1 take the same number of bytes to encode,
   926  			// so we use opcode 249, which may save us a byte in encoding deltaLC,
   927  			// for similar reasons.
   928  			switch deltaPC - PC_RANGE {
   929  			// PC_RANGE is the largest deltaPC we can encode in one byte, using
   930  			// DW_LNS_const_add_pc.
   931  			//
   932  			// (1<<16)-1 is the largest deltaPC we can encode in three bytes, using
   933  			// DW_LNS_fixed_advance_pc.
   934  			//
   935  			// (1<<(7n))-1 is the largest deltaPC we can encode in n+1 bytes for
   936  			// n=1,3,4,5,..., using DW_LNS_advance_pc.
   937  			case PC_RANGE, (1 << 7) - 1, (1 << 16) - 1, (1 << 21) - 1, (1 << 28) - 1,
   938  				(1 << 35) - 1, (1 << 42) - 1, (1 << 49) - 1, (1 << 56) - 1, (1 << 63) - 1:
   939  				opcode = 255
   940  			default:
   941  				opcode = OPCODE_BASE + LINE_RANGE*PC_RANGE - 1 // 249
   942  			}
   943  		}
   944  	}
   945  	if opcode < OPCODE_BASE || opcode > 255 {
   946  		panic(fmt.Sprintf("produced invalid special opcode %d", opcode))
   947  	}
   948  
   949  	// Subtract from deltaPC and deltaLC the amounts that the opcode will add.
   950  	deltaPC -= uint64((opcode - OPCODE_BASE) / LINE_RANGE)
   951  	deltaLC -= int64((opcode-OPCODE_BASE)%LINE_RANGE + LINE_BASE)
   952  
   953  	// Encode deltaPC.
   954  	if deltaPC != 0 {
   955  		if deltaPC <= PC_RANGE {
   956  			// Adjust the opcode so that we can use the 1-byte DW_LNS_const_add_pc
   957  			// instruction.
   958  			opcode -= LINE_RANGE * int64(PC_RANGE-deltaPC)
   959  			if opcode < OPCODE_BASE {
   960  				panic(fmt.Sprintf("produced invalid special opcode %d", opcode))
   961  			}
   962  			Adduint8(linkctxt, s, dwarf.DW_LNS_const_add_pc)
   963  		} else if (1<<14) <= deltaPC && deltaPC < (1<<16) {
   964  			Adduint8(linkctxt, s, dwarf.DW_LNS_fixed_advance_pc)
   965  			Adduint16(linkctxt, s, uint16(deltaPC))
   966  		} else {
   967  			Adduint8(linkctxt, s, dwarf.DW_LNS_advance_pc)
   968  			dwarf.Uleb128put(ctxt, s, int64(deltaPC))
   969  		}
   970  	}
   971  
   972  	// Encode deltaLC.
   973  	if deltaLC != 0 {
   974  		Adduint8(linkctxt, s, dwarf.DW_LNS_advance_line)
   975  		dwarf.Sleb128put(ctxt, s, deltaLC)
   976  	}
   977  
   978  	// Output the special opcode.
   979  	Adduint8(linkctxt, s, uint8(opcode))
   980  }
   981  
   982  /*
   983   * Walk prog table, emit line program and build DIE tree.
   984   */
   985  
   986  func getCompilationDir() string {
   987  	if dir, err := os.Getwd(); err == nil {
   988  		return dir
   989  	}
   990  	return "/"
   991  }
   992  
   993  func writelines(ctxt *Link, syms []*Symbol) ([]*Symbol, []*Symbol) {
   994  	var dwarfctxt dwarf.Context = dwctxt{ctxt}
   995  	if linesec == nil {
   996  		linesec = ctxt.Syms.Lookup(".debug_line", 0)
   997  	}
   998  	linesec.Type = obj.SDWARFSECT
   999  	linesec.R = linesec.R[:0]
  1000  
  1001  	ls := linesec
  1002  	syms = append(syms, ls)
  1003  	var funcs []*Symbol
  1004  
  1005  	unitstart := int64(-1)
  1006  	headerstart := int64(-1)
  1007  	headerend := int64(-1)
  1008  	epc := int64(0)
  1009  	var epcs *Symbol
  1010  	var dwinfo *dwarf.DWDie
  1011  
  1012  	lang := dwarf.DW_LANG_Go
  1013  
  1014  	s := ctxt.Textp[0]
  1015  	if ctxt.DynlinkingGo() && Headtype == obj.Hdarwin {
  1016  		s = ctxt.Textp[1] // skip runtime.text
  1017  	}
  1018  
  1019  	dwinfo = newdie(ctxt, &dwroot, dwarf.DW_ABRV_COMPUNIT, "go", 0)
  1020  	newattr(dwinfo, dwarf.DW_AT_language, dwarf.DW_CLS_CONSTANT, int64(lang), 0)
  1021  	newattr(dwinfo, dwarf.DW_AT_stmt_list, dwarf.DW_CLS_PTR, 0, linesec)
  1022  	newattr(dwinfo, dwarf.DW_AT_low_pc, dwarf.DW_CLS_ADDRESS, s.Value, s)
  1023  	// OS X linker requires compilation dir or absolute path in comp unit name to output debug info.
  1024  	compDir := getCompilationDir()
  1025  	newattr(dwinfo, dwarf.DW_AT_comp_dir, dwarf.DW_CLS_STRING, int64(len(compDir)), compDir)
  1026  
  1027  	// Write .debug_line Line Number Program Header (sec 6.2.4)
  1028  	// Fields marked with (*) must be changed for 64-bit dwarf
  1029  	unitLengthOffset := ls.Size
  1030  	Adduint32(ctxt, ls, 0) // unit_length (*), filled in at end.
  1031  	unitstart = ls.Size
  1032  	Adduint16(ctxt, ls, 2) // dwarf version (appendix F)
  1033  	headerLengthOffset := ls.Size
  1034  	Adduint32(ctxt, ls, 0) // header_length (*), filled in at end.
  1035  	headerstart = ls.Size
  1036  
  1037  	// cpos == unitstart + 4 + 2 + 4
  1038  	Adduint8(ctxt, ls, 1)              // minimum_instruction_length
  1039  	Adduint8(ctxt, ls, 1)              // default_is_stmt
  1040  	Adduint8(ctxt, ls, LINE_BASE&0xFF) // line_base
  1041  	Adduint8(ctxt, ls, LINE_RANGE)     // line_range
  1042  	Adduint8(ctxt, ls, OPCODE_BASE)    // opcode_base
  1043  	Adduint8(ctxt, ls, 0)              // standard_opcode_lengths[1]
  1044  	Adduint8(ctxt, ls, 1)              // standard_opcode_lengths[2]
  1045  	Adduint8(ctxt, ls, 1)              // standard_opcode_lengths[3]
  1046  	Adduint8(ctxt, ls, 1)              // standard_opcode_lengths[4]
  1047  	Adduint8(ctxt, ls, 1)              // standard_opcode_lengths[5]
  1048  	Adduint8(ctxt, ls, 0)              // standard_opcode_lengths[6]
  1049  	Adduint8(ctxt, ls, 0)              // standard_opcode_lengths[7]
  1050  	Adduint8(ctxt, ls, 0)              // standard_opcode_lengths[8]
  1051  	Adduint8(ctxt, ls, 1)              // standard_opcode_lengths[9]
  1052  	Adduint8(ctxt, ls, 0)              // include_directories  (empty)
  1053  
  1054  	for _, f := range ctxt.Filesyms {
  1055  		Addstring(ls, f.Name)
  1056  		Adduint8(ctxt, ls, 0)
  1057  		Adduint8(ctxt, ls, 0)
  1058  		Adduint8(ctxt, ls, 0)
  1059  	}
  1060  
  1061  	// 4 zeros: the string termination + 3 fields.
  1062  	Adduint8(ctxt, ls, 0)
  1063  	// terminate file_names.
  1064  	headerend = ls.Size
  1065  
  1066  	Adduint8(ctxt, ls, 0) // start extended opcode
  1067  	dwarf.Uleb128put(dwarfctxt, ls, 1+int64(SysArch.PtrSize))
  1068  	Adduint8(ctxt, ls, dwarf.DW_LNE_set_address)
  1069  
  1070  	pc := s.Value
  1071  	line := 1
  1072  	file := 1
  1073  	Addaddr(ctxt, ls, s)
  1074  
  1075  	var pcfile Pciter
  1076  	var pcline Pciter
  1077  	for _, s := range ctxt.Textp {
  1078  
  1079  		epc = s.Value + s.Size
  1080  		epcs = s
  1081  
  1082  		dsym := ctxt.Syms.Lookup(dwarf.InfoPrefix+s.Name, int(s.Version))
  1083  		dsym.Attr |= AttrHidden
  1084  		dsym.Type = obj.SDWARFINFO
  1085  		for _, r := range dsym.R {
  1086  			if r.Type == obj.R_DWARFREF && r.Sym.Size == 0 {
  1087  				if Buildmode == BuildmodeShared {
  1088  					// These type symbols may not be present in BuildmodeShared. Skip.
  1089  					continue
  1090  				}
  1091  				n := nameFromDIESym(r.Sym)
  1092  				defgotype(ctxt, ctxt.Syms.Lookup("type."+n, 0))
  1093  			}
  1094  		}
  1095  		funcs = append(funcs, dsym)
  1096  
  1097  		if s.FuncInfo == nil {
  1098  			continue
  1099  		}
  1100  
  1101  		finddebugruntimepath(s)
  1102  
  1103  		pciterinit(ctxt, &pcfile, &s.FuncInfo.Pcfile)
  1104  		pciterinit(ctxt, &pcline, &s.FuncInfo.Pcline)
  1105  		epc = pc
  1106  		for pcfile.done == 0 && pcline.done == 0 {
  1107  			if epc-s.Value >= int64(pcfile.nextpc) {
  1108  				pciternext(&pcfile)
  1109  				continue
  1110  			}
  1111  
  1112  			if epc-s.Value >= int64(pcline.nextpc) {
  1113  				pciternext(&pcline)
  1114  				continue
  1115  			}
  1116  
  1117  			if int32(file) != pcfile.value {
  1118  				Adduint8(ctxt, ls, dwarf.DW_LNS_set_file)
  1119  				dwarf.Uleb128put(dwarfctxt, ls, int64(pcfile.value))
  1120  				file = int(pcfile.value)
  1121  			}
  1122  
  1123  			putpclcdelta(ctxt, dwarfctxt, ls, uint64(s.Value+int64(pcline.pc)-pc), int64(pcline.value)-int64(line))
  1124  
  1125  			pc = s.Value + int64(pcline.pc)
  1126  			line = int(pcline.value)
  1127  			if pcfile.nextpc < pcline.nextpc {
  1128  				epc = int64(pcfile.nextpc)
  1129  			} else {
  1130  				epc = int64(pcline.nextpc)
  1131  			}
  1132  			epc += s.Value
  1133  		}
  1134  	}
  1135  
  1136  	Adduint8(ctxt, ls, 0) // start extended opcode
  1137  	dwarf.Uleb128put(dwarfctxt, ls, 1)
  1138  	Adduint8(ctxt, ls, dwarf.DW_LNE_end_sequence)
  1139  
  1140  	newattr(dwinfo, dwarf.DW_AT_high_pc, dwarf.DW_CLS_ADDRESS, epc+1, epcs)
  1141  
  1142  	setuint32(ctxt, ls, unitLengthOffset, uint32(ls.Size-unitstart))
  1143  	setuint32(ctxt, ls, headerLengthOffset, uint32(headerend-headerstart))
  1144  
  1145  	return syms, funcs
  1146  }
  1147  
  1148  /*
  1149   *  Emit .debug_frame
  1150   */
  1151  const (
  1152  	dataAlignmentFactor = -4
  1153  )
  1154  
  1155  // appendPCDeltaCFA appends per-PC CFA deltas to b and returns the final slice.
  1156  func appendPCDeltaCFA(b []byte, deltapc, cfa int64) []byte {
  1157  	b = append(b, dwarf.DW_CFA_def_cfa_offset_sf)
  1158  	b = dwarf.AppendSleb128(b, cfa/dataAlignmentFactor)
  1159  
  1160  	switch {
  1161  	case deltapc < 0x40:
  1162  		b = append(b, uint8(dwarf.DW_CFA_advance_loc+deltapc))
  1163  	case deltapc < 0x100:
  1164  		b = append(b, dwarf.DW_CFA_advance_loc1)
  1165  		b = append(b, uint8(deltapc))
  1166  	case deltapc < 0x10000:
  1167  		b = append(b, dwarf.DW_CFA_advance_loc2)
  1168  		b = Thearch.Append16(b, uint16(deltapc))
  1169  	default:
  1170  		b = append(b, dwarf.DW_CFA_advance_loc4)
  1171  		b = Thearch.Append32(b, uint32(deltapc))
  1172  	}
  1173  	return b
  1174  }
  1175  
  1176  func writeframes(ctxt *Link, syms []*Symbol) []*Symbol {
  1177  	var dwarfctxt dwarf.Context = dwctxt{ctxt}
  1178  	if framesec == nil {
  1179  		framesec = ctxt.Syms.Lookup(".debug_frame", 0)
  1180  	}
  1181  	framesec.Type = obj.SDWARFSECT
  1182  	framesec.R = framesec.R[:0]
  1183  	fs := framesec
  1184  	syms = append(syms, fs)
  1185  
  1186  	// Emit the CIE, Section 6.4.1
  1187  	cieReserve := uint32(16)
  1188  	if haslinkregister(ctxt) {
  1189  		cieReserve = 32
  1190  	}
  1191  	Adduint32(ctxt, fs, cieReserve)                            // initial length, must be multiple of thearch.ptrsize
  1192  	Adduint32(ctxt, fs, 0xffffffff)                            // cid.
  1193  	Adduint8(ctxt, fs, 3)                                      // dwarf version (appendix F)
  1194  	Adduint8(ctxt, fs, 0)                                      // augmentation ""
  1195  	dwarf.Uleb128put(dwarfctxt, fs, 1)                         // code_alignment_factor
  1196  	dwarf.Sleb128put(dwarfctxt, fs, dataAlignmentFactor)       // all CFI offset calculations include multiplication with this factor
  1197  	dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfreglr)) // return_address_register
  1198  
  1199  	Adduint8(ctxt, fs, dwarf.DW_CFA_def_cfa)                   // Set the current frame address..
  1200  	dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfregsp)) // ...to use the value in the platform's SP register (defined in l.go)...
  1201  	if haslinkregister(ctxt) {
  1202  		dwarf.Uleb128put(dwarfctxt, fs, int64(0)) // ...plus a 0 offset.
  1203  
  1204  		Adduint8(ctxt, fs, dwarf.DW_CFA_same_value) // The platform's link register is unchanged during the prologue.
  1205  		dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfreglr))
  1206  
  1207  		Adduint8(ctxt, fs, dwarf.DW_CFA_val_offset)                // The previous value...
  1208  		dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfregsp)) // ...of the platform's SP register...
  1209  		dwarf.Uleb128put(dwarfctxt, fs, int64(0))                  // ...is CFA+0.
  1210  	} else {
  1211  		dwarf.Uleb128put(dwarfctxt, fs, int64(SysArch.PtrSize)) // ...plus the word size (because the call instruction implicitly adds one word to the frame).
  1212  
  1213  		Adduint8(ctxt, fs, dwarf.DW_CFA_offset_extended)                             // The previous value...
  1214  		dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfreglr))                   // ...of the return address...
  1215  		dwarf.Uleb128put(dwarfctxt, fs, int64(-SysArch.PtrSize)/dataAlignmentFactor) // ...is saved at [CFA - (PtrSize/4)].
  1216  	}
  1217  
  1218  	// 4 is to exclude the length field.
  1219  	pad := int64(cieReserve) + 4 - fs.Size
  1220  
  1221  	if pad < 0 {
  1222  		Exitf("dwarf: cieReserve too small by %d bytes.", -pad)
  1223  	}
  1224  
  1225  	Addbytes(fs, zeros[:pad])
  1226  
  1227  	var deltaBuf []byte
  1228  	var pcsp Pciter
  1229  	for _, s := range ctxt.Textp {
  1230  		if s.FuncInfo == nil {
  1231  			continue
  1232  		}
  1233  
  1234  		// Emit a FDE, Section 6.4.1.
  1235  		// First build the section contents into a byte buffer.
  1236  		deltaBuf = deltaBuf[:0]
  1237  		for pciterinit(ctxt, &pcsp, &s.FuncInfo.Pcsp); pcsp.done == 0; pciternext(&pcsp) {
  1238  			nextpc := pcsp.nextpc
  1239  
  1240  			// pciterinit goes up to the end of the function,
  1241  			// but DWARF expects us to stop just before the end.
  1242  			if int64(nextpc) == s.Size {
  1243  				nextpc--
  1244  				if nextpc < pcsp.pc {
  1245  					continue
  1246  				}
  1247  			}
  1248  
  1249  			if haslinkregister(ctxt) {
  1250  				// TODO(bryanpkc): This is imprecise. In general, the instruction
  1251  				// that stores the return address to the stack frame is not the
  1252  				// same one that allocates the frame.
  1253  				if pcsp.value > 0 {
  1254  					// The return address is preserved at (CFA-frame_size)
  1255  					// after a stack frame has been allocated.
  1256  					deltaBuf = append(deltaBuf, dwarf.DW_CFA_offset_extended_sf)
  1257  					deltaBuf = dwarf.AppendUleb128(deltaBuf, uint64(Thearch.Dwarfreglr))
  1258  					deltaBuf = dwarf.AppendSleb128(deltaBuf, -int64(pcsp.value)/dataAlignmentFactor)
  1259  				} else {
  1260  					// The return address is restored into the link register
  1261  					// when a stack frame has been de-allocated.
  1262  					deltaBuf = append(deltaBuf, dwarf.DW_CFA_same_value)
  1263  					deltaBuf = dwarf.AppendUleb128(deltaBuf, uint64(Thearch.Dwarfreglr))
  1264  				}
  1265  				deltaBuf = appendPCDeltaCFA(deltaBuf, int64(nextpc)-int64(pcsp.pc), int64(pcsp.value))
  1266  			} else {
  1267  				deltaBuf = appendPCDeltaCFA(deltaBuf, int64(nextpc)-int64(pcsp.pc), int64(SysArch.PtrSize)+int64(pcsp.value))
  1268  			}
  1269  		}
  1270  		pad := int(Rnd(int64(len(deltaBuf)), int64(SysArch.PtrSize))) - len(deltaBuf)
  1271  		deltaBuf = append(deltaBuf, zeros[:pad]...)
  1272  
  1273  		// Emit the FDE header, Section 6.4.1.
  1274  		//	4 bytes: length, must be multiple of thearch.ptrsize
  1275  		//	4 bytes: Pointer to the CIE above, at offset 0
  1276  		//	ptrsize: initial location
  1277  		//	ptrsize: address range
  1278  		Adduint32(ctxt, fs, uint32(4+2*SysArch.PtrSize+len(deltaBuf))) // length (excludes itself)
  1279  		if Linkmode == LinkExternal {
  1280  			adddwarfref(ctxt, fs, framesec, 4)
  1281  		} else {
  1282  			Adduint32(ctxt, fs, 0) // CIE offset
  1283  		}
  1284  		Addaddr(ctxt, fs, s)
  1285  		adduintxx(ctxt, fs, uint64(s.Size), SysArch.PtrSize) // address range
  1286  		Addbytes(fs, deltaBuf)
  1287  	}
  1288  	return syms
  1289  }
  1290  
  1291  /*
  1292   *  Walk DWarfDebugInfoEntries, and emit .debug_info
  1293   */
  1294  const (
  1295  	COMPUNITHEADERSIZE = 4 + 2 + 4 + 1
  1296  )
  1297  
  1298  func writeinfo(ctxt *Link, syms []*Symbol, funcs []*Symbol) []*Symbol {
  1299  	if infosec == nil {
  1300  		infosec = ctxt.Syms.Lookup(".debug_info", 0)
  1301  	}
  1302  	infosec.R = infosec.R[:0]
  1303  	infosec.Type = obj.SDWARFINFO
  1304  	infosec.Attr |= AttrReachable
  1305  	syms = append(syms, infosec)
  1306  
  1307  	if arangessec == nil {
  1308  		arangessec = ctxt.Syms.Lookup(".dwarfaranges", 0)
  1309  	}
  1310  	arangessec.R = arangessec.R[:0]
  1311  
  1312  	var dwarfctxt dwarf.Context = dwctxt{ctxt}
  1313  
  1314  	for compunit := dwroot.Child; compunit != nil; compunit = compunit.Link {
  1315  		s := dtolsym(compunit.Sym)
  1316  
  1317  		// Write .debug_info Compilation Unit Header (sec 7.5.1)
  1318  		// Fields marked with (*) must be changed for 64-bit dwarf
  1319  		// This must match COMPUNITHEADERSIZE above.
  1320  		Adduint32(ctxt, s, 0) // unit_length (*), will be filled in later.
  1321  		Adduint16(ctxt, s, 2) // dwarf version (appendix F)
  1322  
  1323  		// debug_abbrev_offset (*)
  1324  		adddwarfref(ctxt, s, abbrevsym, 4)
  1325  
  1326  		Adduint8(ctxt, s, uint8(SysArch.PtrSize)) // address_size
  1327  
  1328  		dwarf.Uleb128put(dwarfctxt, s, int64(compunit.Abbrev))
  1329  		dwarf.PutAttrs(dwarfctxt, s, compunit.Abbrev, compunit.Attr)
  1330  
  1331  		cu := []*Symbol{s}
  1332  		if funcs != nil {
  1333  			cu = append(cu, funcs...)
  1334  			funcs = nil
  1335  		}
  1336  		cu = putdies(ctxt, dwarfctxt, cu, compunit.Child)
  1337  		var cusize int64
  1338  		for _, child := range cu {
  1339  			cusize += child.Size
  1340  		}
  1341  		cusize -= 4 // exclude the length field.
  1342  		setuint32(ctxt, s, 0, uint32(cusize))
  1343  		newattr(compunit, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, cusize, 0)
  1344  		syms = append(syms, cu...)
  1345  	}
  1346  	return syms
  1347  }
  1348  
  1349  /*
  1350   *  Emit .debug_pubnames/_types.  _info must have been written before,
  1351   *  because we need die->offs and infoo/infosize;
  1352   */
  1353  func ispubname(die *dwarf.DWDie) bool {
  1354  	switch die.Abbrev {
  1355  	case dwarf.DW_ABRV_FUNCTION, dwarf.DW_ABRV_VARIABLE:
  1356  		a := getattr(die, dwarf.DW_AT_external)
  1357  		return a != nil && a.Value != 0
  1358  	}
  1359  
  1360  	return false
  1361  }
  1362  
  1363  func ispubtype(die *dwarf.DWDie) bool {
  1364  	return die.Abbrev >= dwarf.DW_ABRV_NULLTYPE
  1365  }
  1366  
  1367  func writepub(ctxt *Link, sname string, ispub func(*dwarf.DWDie) bool, syms []*Symbol) []*Symbol {
  1368  	s := ctxt.Syms.Lookup(sname, 0)
  1369  	s.Type = obj.SDWARFSECT
  1370  	syms = append(syms, s)
  1371  
  1372  	for compunit := dwroot.Child; compunit != nil; compunit = compunit.Link {
  1373  		sectionstart := s.Size
  1374  		culength := uint32(getattr(compunit, dwarf.DW_AT_byte_size).Value) + 4
  1375  
  1376  		// Write .debug_pubnames/types	Header (sec 6.1.1)
  1377  		Adduint32(ctxt, s, 0)                          // unit_length (*), will be filled in later.
  1378  		Adduint16(ctxt, s, 2)                          // dwarf version (appendix F)
  1379  		adddwarfref(ctxt, s, dtolsym(compunit.Sym), 4) // debug_info_offset (of the Comp unit Header)
  1380  		Adduint32(ctxt, s, culength)                   // debug_info_length
  1381  
  1382  		for die := compunit.Child; die != nil; die = die.Link {
  1383  			if !ispub(die) {
  1384  				continue
  1385  			}
  1386  			dwa := getattr(die, dwarf.DW_AT_name)
  1387  			name := dwa.Data.(string)
  1388  			if die.Sym == nil {
  1389  				fmt.Println("Missing sym for ", name)
  1390  			}
  1391  			adddwarfref(ctxt, s, dtolsym(die.Sym), 4)
  1392  			Addstring(s, name)
  1393  		}
  1394  
  1395  		Adduint32(ctxt, s, 0)
  1396  
  1397  		setuint32(ctxt, s, sectionstart, uint32(s.Size-sectionstart)-4) // exclude the length field.
  1398  	}
  1399  
  1400  	return syms
  1401  }
  1402  
  1403  /*
  1404   *  emit .debug_aranges.  _info must have been written before,
  1405   *  because we need die->offs of dwarf.DW_globals.
  1406   */
  1407  func writearanges(ctxt *Link, syms []*Symbol) []*Symbol {
  1408  	s := ctxt.Syms.Lookup(".debug_aranges", 0)
  1409  	s.Type = obj.SDWARFSECT
  1410  	// The first tuple is aligned to a multiple of the size of a single tuple
  1411  	// (twice the size of an address)
  1412  	headersize := int(Rnd(4+2+4+1+1, int64(SysArch.PtrSize*2))) // don't count unit_length field itself
  1413  
  1414  	for compunit := dwroot.Child; compunit != nil; compunit = compunit.Link {
  1415  		b := getattr(compunit, dwarf.DW_AT_low_pc)
  1416  		if b == nil {
  1417  			continue
  1418  		}
  1419  		e := getattr(compunit, dwarf.DW_AT_high_pc)
  1420  		if e == nil {
  1421  			continue
  1422  		}
  1423  
  1424  		// Write .debug_aranges	 Header + entry	 (sec 6.1.2)
  1425  		unitlength := uint32(headersize) + 4*uint32(SysArch.PtrSize) - 4
  1426  		Adduint32(ctxt, s, unitlength) // unit_length (*)
  1427  		Adduint16(ctxt, s, 2)          // dwarf version (appendix F)
  1428  
  1429  		adddwarfref(ctxt, s, dtolsym(compunit.Sym), 4)
  1430  
  1431  		Adduint8(ctxt, s, uint8(SysArch.PtrSize)) // address_size
  1432  		Adduint8(ctxt, s, 0)                      // segment_size
  1433  		padding := headersize - (4 + 2 + 4 + 1 + 1)
  1434  		for i := 0; i < padding; i++ {
  1435  			Adduint8(ctxt, s, 0)
  1436  		}
  1437  
  1438  		Addaddrplus(ctxt, s, b.Data.(*Symbol), b.Value-(b.Data.(*Symbol)).Value)
  1439  		adduintxx(ctxt, s, uint64(e.Value-b.Value), SysArch.PtrSize)
  1440  		adduintxx(ctxt, s, 0, SysArch.PtrSize)
  1441  		adduintxx(ctxt, s, 0, SysArch.PtrSize)
  1442  	}
  1443  	if s.Size > 0 {
  1444  		syms = append(syms, s)
  1445  	}
  1446  	return syms
  1447  }
  1448  
  1449  func writegdbscript(ctxt *Link, syms []*Symbol) []*Symbol {
  1450  
  1451  	if gdbscript != "" {
  1452  		s := ctxt.Syms.Lookup(".debug_gdb_scripts", 0)
  1453  		s.Type = obj.SDWARFSECT
  1454  		syms = append(syms, s)
  1455  		Adduint8(ctxt, s, 1) // magic 1 byte?
  1456  		Addstring(s, gdbscript)
  1457  	}
  1458  
  1459  	return syms
  1460  }
  1461  
  1462  var prototypedies map[string]*dwarf.DWDie
  1463  
  1464  /*
  1465   * This is the main entry point for generating dwarf.  After emitting
  1466   * the mandatory debug_abbrev section, it calls writelines() to set up
  1467   * the per-compilation unit part of the DIE tree, while simultaneously
  1468   * emitting the debug_line section.  When the final tree contains
  1469   * forward references, it will write the debug_info section in 2
  1470   * passes.
  1471   *
  1472   */
  1473  func dwarfgeneratedebugsyms(ctxt *Link) {
  1474  	if *FlagW { // disable dwarf
  1475  		return
  1476  	}
  1477  	if *FlagS && Headtype != obj.Hdarwin {
  1478  		return
  1479  	}
  1480  	if Headtype == obj.Hplan9 {
  1481  		return
  1482  	}
  1483  
  1484  	if Linkmode == LinkExternal {
  1485  		if !Iself && Headtype != obj.Hdarwin {
  1486  			return
  1487  		}
  1488  	}
  1489  
  1490  	if ctxt.Debugvlog != 0 {
  1491  		ctxt.Logf("%5.2f dwarf\n", obj.Cputime())
  1492  	}
  1493  
  1494  	// Forctxt.Diagnostic messages.
  1495  	newattr(&dwtypes, dwarf.DW_AT_name, dwarf.DW_CLS_STRING, int64(len("dwtypes")), "dwtypes")
  1496  
  1497  	// Some types that must exist to define other ones.
  1498  	newdie(ctxt, &dwtypes, dwarf.DW_ABRV_NULLTYPE, "<unspecified>", 0)
  1499  
  1500  	newdie(ctxt, &dwtypes, dwarf.DW_ABRV_NULLTYPE, "void", 0)
  1501  	newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BARE_PTRTYPE, "unsafe.Pointer", 0)
  1502  
  1503  	die := newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BASETYPE, "uintptr", 0) // needed for array size
  1504  	newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_unsigned, 0)
  1505  	newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, int64(SysArch.PtrSize), 0)
  1506  	newattr(die, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, obj.KindUintptr, 0)
  1507  
  1508  	// Prototypes needed for type synthesis.
  1509  	prototypedies = map[string]*dwarf.DWDie{
  1510  		"type.runtime.stringStructDWARF": nil,
  1511  		"type.runtime.slice":             nil,
  1512  		"type.runtime.hmap":              nil,
  1513  		"type.runtime.bmap":              nil,
  1514  		"type.runtime.sudog":             nil,
  1515  		"type.runtime.waitq":             nil,
  1516  		"type.runtime.hchan":             nil,
  1517  	}
  1518  
  1519  	// Needed by the prettyprinter code for interface inspection.
  1520  	defgotype(ctxt, lookupOrDiag(ctxt, "type.runtime._type"))
  1521  
  1522  	defgotype(ctxt, lookupOrDiag(ctxt, "type.runtime.interfacetype"))
  1523  	defgotype(ctxt, lookupOrDiag(ctxt, "type.runtime.itab"))
  1524  
  1525  	genasmsym(ctxt, defdwsymb)
  1526  
  1527  	syms := writeabbrev(ctxt, nil)
  1528  	syms, funcs := writelines(ctxt, syms)
  1529  	syms = writeframes(ctxt, syms)
  1530  
  1531  	synthesizestringtypes(ctxt, dwtypes.Child)
  1532  	synthesizeslicetypes(ctxt, dwtypes.Child)
  1533  	synthesizemaptypes(ctxt, dwtypes.Child)
  1534  	synthesizechantypes(ctxt, dwtypes.Child)
  1535  
  1536  	reversetree(&dwroot.Child)
  1537  	reversetree(&dwtypes.Child)
  1538  	reversetree(&dwglobals.Child)
  1539  
  1540  	movetomodule(&dwtypes)
  1541  	movetomodule(&dwglobals)
  1542  
  1543  	// Need to reorder symbols so SDWARFINFO is after all SDWARFSECT
  1544  	// (but we need to generate dies before writepub)
  1545  	infosyms := writeinfo(ctxt, nil, funcs)
  1546  
  1547  	syms = writepub(ctxt, ".debug_pubnames", ispubname, syms)
  1548  	syms = writepub(ctxt, ".debug_pubtypes", ispubtype, syms)
  1549  	syms = writearanges(ctxt, syms)
  1550  	syms = writegdbscript(ctxt, syms)
  1551  	syms = append(syms, infosyms...)
  1552  	dwarfp = syms
  1553  }
  1554  
  1555  /*
  1556   *  Elf.
  1557   */
  1558  func dwarfaddshstrings(ctxt *Link, shstrtab *Symbol) {
  1559  	if *FlagW { // disable dwarf
  1560  		return
  1561  	}
  1562  
  1563  	Addstring(shstrtab, ".debug_abbrev")
  1564  	Addstring(shstrtab, ".debug_aranges")
  1565  	Addstring(shstrtab, ".debug_frame")
  1566  	Addstring(shstrtab, ".debug_info")
  1567  	Addstring(shstrtab, ".debug_line")
  1568  	Addstring(shstrtab, ".debug_pubnames")
  1569  	Addstring(shstrtab, ".debug_pubtypes")
  1570  	Addstring(shstrtab, ".debug_gdb_scripts")
  1571  	if Linkmode == LinkExternal {
  1572  		Addstring(shstrtab, elfRelType+".debug_info")
  1573  		Addstring(shstrtab, elfRelType+".debug_aranges")
  1574  		Addstring(shstrtab, elfRelType+".debug_line")
  1575  		Addstring(shstrtab, elfRelType+".debug_frame")
  1576  		Addstring(shstrtab, elfRelType+".debug_pubnames")
  1577  		Addstring(shstrtab, elfRelType+".debug_pubtypes")
  1578  	}
  1579  }
  1580  
  1581  // Add section symbols for DWARF debug info.  This is called before
  1582  // dwarfaddelfheaders.
  1583  func dwarfaddelfsectionsyms(ctxt *Link) {
  1584  	if *FlagW { // disable dwarf
  1585  		return
  1586  	}
  1587  	if Linkmode != LinkExternal {
  1588  		return
  1589  	}
  1590  	sym := ctxt.Syms.Lookup(".debug_info", 0)
  1591  	putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
  1592  	sym = ctxt.Syms.Lookup(".debug_abbrev", 0)
  1593  	putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
  1594  	sym = ctxt.Syms.Lookup(".debug_line", 0)
  1595  	putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
  1596  	sym = ctxt.Syms.Lookup(".debug_frame", 0)
  1597  	putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
  1598  }
  1599  
  1600  /*
  1601   * Windows PE
  1602   */
  1603  func dwarfaddpeheaders(ctxt *Link) {
  1604  	if *FlagW { // disable dwarf
  1605  		return
  1606  	}
  1607  	for sect := Segdwarf.Sect; sect != nil; sect = sect.Next {
  1608  		h := newPEDWARFSection(ctxt, sect.Name, int64(sect.Length))
  1609  		fileoff := sect.Vaddr - Segdwarf.Vaddr + Segdwarf.Fileoff
  1610  		if uint64(h.PointerToRawData) != fileoff {
  1611  			Exitf("%s.PointerToRawData = %#x, want %#x", sect.Name, h.PointerToRawData, fileoff)
  1612  		}
  1613  	}
  1614  }