github.com/euank/go@v0.0.0-20160829210321-495514729181/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(c.linkctxt, ls, b)
    40  }
    41  func (c dwctxt) AddString(s dwarf.Sym, v string) {
    42  	Addstring(c.linkctxt, 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  		c.linkctxt.Diag("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 := Linklookup(ctxt, ".debug_abbrev", 0)
    86  	s.Type = obj.SDWARFSECT
    87  	abbrevsym = s
    88  	Addbytes(ctxt, 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 := Linklookup(ctxt, 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 := Linkrlookup(ctxt, 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 := Linkrlookup(ctxt, 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  		ctxt.Diag("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 := Linkrlookup(ctxt, 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  		ctxt.Diag("dwarf: bad def in dotypedef")
   339  	}
   340  
   341  	sym := Linklookup(ctxt, 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  		ctxt.Diag("dwarf: type name doesn't start with \"type.\": %s", gotype.Name)
   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  		ctxt.Diag("dwarf: definition of unknown kind %d: %s", kind, gotype.Name)
   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 := Linkrlookup(ctxt, 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 int, v int64, size int64, ver int, 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 'd', 'b', 'D', 'B':
   838  		dv = newdie(ctxt, &dwglobals, dwarf.DW_ABRV_VARIABLE, s, ver)
   839  		newabslocexprattr(dv, v, sym)
   840  		if ver == 0 {
   841  			newattr(dv, dwarf.DW_AT_external, dwarf.DW_CLS_FLAG, 1, 0)
   842  		}
   843  		fallthrough
   844  
   845  	case 'a', 'p':
   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 short opcodes when possible, long ones when necessary.
   883   * See section 6.2.5
   884   */
   885  const (
   886  	LINE_BASE   = -1
   887  	LINE_RANGE  = 4
   888  	OPCODE_BASE = 10
   889  )
   890  
   891  func putpclcdelta(linkctxt *Link, ctxt dwarf.Context, s *Symbol, deltaPC int64, deltaLC int64) {
   892  	if LINE_BASE <= deltaLC && deltaLC < LINE_BASE+LINE_RANGE {
   893  		var opcode int64 = OPCODE_BASE + (deltaLC - LINE_BASE) + (LINE_RANGE * deltaPC)
   894  		if OPCODE_BASE <= opcode && opcode < 256 {
   895  			Adduint8(linkctxt, s, uint8(opcode))
   896  			return
   897  		}
   898  	}
   899  
   900  	if deltaPC != 0 {
   901  		Adduint8(linkctxt, s, dwarf.DW_LNS_advance_pc)
   902  		dwarf.Sleb128put(ctxt, s, deltaPC)
   903  	}
   904  
   905  	Adduint8(linkctxt, s, dwarf.DW_LNS_advance_line)
   906  	dwarf.Sleb128put(ctxt, s, deltaLC)
   907  	Adduint8(linkctxt, s, dwarf.DW_LNS_copy)
   908  }
   909  
   910  /*
   911   * Walk prog table, emit line program and build DIE tree.
   912   */
   913  
   914  func getCompilationDir() string {
   915  	if dir, err := os.Getwd(); err == nil {
   916  		return dir
   917  	}
   918  	return "/"
   919  }
   920  
   921  func writelines(ctxt *Link, syms []*Symbol) ([]*Symbol, []*Symbol) {
   922  	var dwarfctxt dwarf.Context = dwctxt{ctxt}
   923  	if linesec == nil {
   924  		linesec = Linklookup(ctxt, ".debug_line", 0)
   925  	}
   926  	linesec.Type = obj.SDWARFSECT
   927  	linesec.R = linesec.R[:0]
   928  
   929  	ls := linesec
   930  	syms = append(syms, ls)
   931  	var funcs []*Symbol
   932  
   933  	unitstart := int64(-1)
   934  	headerstart := int64(-1)
   935  	headerend := int64(-1)
   936  	epc := int64(0)
   937  	var epcs *Symbol
   938  	var dwinfo *dwarf.DWDie
   939  
   940  	lang := dwarf.DW_LANG_Go
   941  
   942  	s := ctxt.Textp[0]
   943  
   944  	dwinfo = newdie(ctxt, &dwroot, dwarf.DW_ABRV_COMPUNIT, "go", 0)
   945  	newattr(dwinfo, dwarf.DW_AT_language, dwarf.DW_CLS_CONSTANT, int64(lang), 0)
   946  	newattr(dwinfo, dwarf.DW_AT_stmt_list, dwarf.DW_CLS_PTR, 0, linesec)
   947  	newattr(dwinfo, dwarf.DW_AT_low_pc, dwarf.DW_CLS_ADDRESS, s.Value, s)
   948  	// OS X linker requires compilation dir or absolute path in comp unit name to output debug info.
   949  	compDir := getCompilationDir()
   950  	newattr(dwinfo, dwarf.DW_AT_comp_dir, dwarf.DW_CLS_STRING, int64(len(compDir)), compDir)
   951  
   952  	// Write .debug_line Line Number Program Header (sec 6.2.4)
   953  	// Fields marked with (*) must be changed for 64-bit dwarf
   954  	unitLengthOffset := ls.Size
   955  	Adduint32(ctxt, ls, 0) // unit_length (*), filled in at end.
   956  	unitstart = ls.Size
   957  	Adduint16(ctxt, ls, 2) // dwarf version (appendix F)
   958  	headerLengthOffset := ls.Size
   959  	Adduint32(ctxt, ls, 0) // header_length (*), filled in at end.
   960  	headerstart = ls.Size
   961  
   962  	// cpos == unitstart + 4 + 2 + 4
   963  	Adduint8(ctxt, ls, 1)              // minimum_instruction_length
   964  	Adduint8(ctxt, ls, 1)              // default_is_stmt
   965  	Adduint8(ctxt, ls, LINE_BASE&0xFF) // line_base
   966  	Adduint8(ctxt, ls, LINE_RANGE)     // line_range
   967  	Adduint8(ctxt, ls, OPCODE_BASE)    // opcode_base
   968  	Adduint8(ctxt, ls, 0)              // standard_opcode_lengths[1]
   969  	Adduint8(ctxt, ls, 1)              // standard_opcode_lengths[2]
   970  	Adduint8(ctxt, ls, 1)              // standard_opcode_lengths[3]
   971  	Adduint8(ctxt, ls, 1)              // standard_opcode_lengths[4]
   972  	Adduint8(ctxt, ls, 1)              // standard_opcode_lengths[5]
   973  	Adduint8(ctxt, ls, 0)              // standard_opcode_lengths[6]
   974  	Adduint8(ctxt, ls, 0)              // standard_opcode_lengths[7]
   975  	Adduint8(ctxt, ls, 0)              // standard_opcode_lengths[8]
   976  	Adduint8(ctxt, ls, 1)              // standard_opcode_lengths[9]
   977  	Adduint8(ctxt, ls, 0)              // include_directories  (empty)
   978  
   979  	for _, f := range ctxt.Filesyms {
   980  		Addstring(ctxt, ls, f.Name)
   981  		Adduint8(ctxt, ls, 0)
   982  		Adduint8(ctxt, ls, 0)
   983  		Adduint8(ctxt, ls, 0)
   984  	}
   985  
   986  	// 4 zeros: the string termination + 3 fields.
   987  	Adduint8(ctxt, ls, 0)
   988  	// terminate file_names.
   989  	headerend = ls.Size
   990  
   991  	Adduint8(ctxt, ls, 0) // start extended opcode
   992  	dwarf.Uleb128put(dwarfctxt, ls, 1+int64(SysArch.PtrSize))
   993  	Adduint8(ctxt, ls, dwarf.DW_LNE_set_address)
   994  
   995  	pc := s.Value
   996  	line := 1
   997  	file := 1
   998  	Addaddr(ctxt, ls, s)
   999  
  1000  	var pcfile Pciter
  1001  	var pcline Pciter
  1002  	for _, ctxt.Cursym = range ctxt.Textp {
  1003  		s := ctxt.Cursym
  1004  
  1005  		epc = s.Value + s.Size
  1006  		epcs = s
  1007  
  1008  		dsym := Linklookup(ctxt, dwarf.InfoPrefix+s.Name, int(s.Version))
  1009  		dsym.Attr |= AttrHidden
  1010  		dsym.Type = obj.SDWARFINFO
  1011  		for _, r := range dsym.R {
  1012  			if r.Type == obj.R_DWARFREF && r.Sym.Size == 0 {
  1013  				if Buildmode == BuildmodeShared {
  1014  					// These type symbols may not be present in BuildmodeShared. Skip.
  1015  					continue
  1016  				}
  1017  				n := nameFromDIESym(r.Sym)
  1018  				defgotype(ctxt, Linklookup(ctxt, "type."+n, 0))
  1019  			}
  1020  		}
  1021  		funcs = append(funcs, dsym)
  1022  
  1023  		if s.FuncInfo == nil {
  1024  			continue
  1025  		}
  1026  
  1027  		finddebugruntimepath(s)
  1028  
  1029  		pciterinit(ctxt, &pcfile, &s.FuncInfo.Pcfile)
  1030  		pciterinit(ctxt, &pcline, &s.FuncInfo.Pcline)
  1031  		epc = pc
  1032  		for pcfile.done == 0 && pcline.done == 0 {
  1033  			if epc-s.Value >= int64(pcfile.nextpc) {
  1034  				pciternext(&pcfile)
  1035  				continue
  1036  			}
  1037  
  1038  			if epc-s.Value >= int64(pcline.nextpc) {
  1039  				pciternext(&pcline)
  1040  				continue
  1041  			}
  1042  
  1043  			if int32(file) != pcfile.value {
  1044  				Adduint8(ctxt, ls, dwarf.DW_LNS_set_file)
  1045  				dwarf.Uleb128put(dwarfctxt, ls, int64(pcfile.value))
  1046  				file = int(pcfile.value)
  1047  			}
  1048  
  1049  			putpclcdelta(ctxt, dwarfctxt, ls, s.Value+int64(pcline.pc)-pc, int64(pcline.value)-int64(line))
  1050  
  1051  			pc = s.Value + int64(pcline.pc)
  1052  			line = int(pcline.value)
  1053  			if pcfile.nextpc < pcline.nextpc {
  1054  				epc = int64(pcfile.nextpc)
  1055  			} else {
  1056  				epc = int64(pcline.nextpc)
  1057  			}
  1058  			epc += s.Value
  1059  		}
  1060  	}
  1061  
  1062  	Adduint8(ctxt, ls, 0) // start extended opcode
  1063  	dwarf.Uleb128put(dwarfctxt, ls, 1)
  1064  	Adduint8(ctxt, ls, dwarf.DW_LNE_end_sequence)
  1065  
  1066  	newattr(dwinfo, dwarf.DW_AT_high_pc, dwarf.DW_CLS_ADDRESS, epc+1, epcs)
  1067  
  1068  	setuint32(ctxt, ls, unitLengthOffset, uint32(ls.Size-unitstart))
  1069  	setuint32(ctxt, ls, headerLengthOffset, uint32(headerend-headerstart))
  1070  
  1071  	return syms, funcs
  1072  }
  1073  
  1074  /*
  1075   *  Emit .debug_frame
  1076   */
  1077  const (
  1078  	dataAlignmentFactor = -4
  1079  )
  1080  
  1081  // appendPCDeltaCFA appends per-PC CFA deltas to b and returns the final slice.
  1082  func appendPCDeltaCFA(b []byte, deltapc, cfa int64) []byte {
  1083  	b = append(b, dwarf.DW_CFA_def_cfa_offset_sf)
  1084  	b = dwarf.AppendSleb128(b, cfa/dataAlignmentFactor)
  1085  
  1086  	switch {
  1087  	case deltapc < 0x40:
  1088  		b = append(b, uint8(dwarf.DW_CFA_advance_loc+deltapc))
  1089  	case deltapc < 0x100:
  1090  		b = append(b, dwarf.DW_CFA_advance_loc1)
  1091  		b = append(b, uint8(deltapc))
  1092  	case deltapc < 0x10000:
  1093  		b = append(b, dwarf.DW_CFA_advance_loc2)
  1094  		b = Thearch.Append16(b, uint16(deltapc))
  1095  	default:
  1096  		b = append(b, dwarf.DW_CFA_advance_loc4)
  1097  		b = Thearch.Append32(b, uint32(deltapc))
  1098  	}
  1099  	return b
  1100  }
  1101  
  1102  func writeframes(ctxt *Link, syms []*Symbol) []*Symbol {
  1103  	var dwarfctxt dwarf.Context = dwctxt{ctxt}
  1104  	if framesec == nil {
  1105  		framesec = Linklookup(ctxt, ".debug_frame", 0)
  1106  	}
  1107  	framesec.Type = obj.SDWARFSECT
  1108  	framesec.R = framesec.R[:0]
  1109  	fs := framesec
  1110  	syms = append(syms, fs)
  1111  
  1112  	// Emit the CIE, Section 6.4.1
  1113  	cieReserve := uint32(16)
  1114  	if haslinkregister(ctxt) {
  1115  		cieReserve = 32
  1116  	}
  1117  	Adduint32(ctxt, fs, cieReserve)                            // initial length, must be multiple of thearch.ptrsize
  1118  	Adduint32(ctxt, fs, 0xffffffff)                            // cid.
  1119  	Adduint8(ctxt, fs, 3)                                      // dwarf version (appendix F)
  1120  	Adduint8(ctxt, fs, 0)                                      // augmentation ""
  1121  	dwarf.Uleb128put(dwarfctxt, fs, 1)                         // code_alignment_factor
  1122  	dwarf.Sleb128put(dwarfctxt, fs, dataAlignmentFactor)       // all CFI offset calculations include multiplication with this factor
  1123  	dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfreglr)) // return_address_register
  1124  
  1125  	Adduint8(ctxt, fs, dwarf.DW_CFA_def_cfa)                   // Set the current frame address..
  1126  	dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfregsp)) // ...to use the value in the platform's SP register (defined in l.go)...
  1127  	if haslinkregister(ctxt) {
  1128  		dwarf.Uleb128put(dwarfctxt, fs, int64(0)) // ...plus a 0 offset.
  1129  
  1130  		Adduint8(ctxt, fs, dwarf.DW_CFA_same_value) // The platform's link register is unchanged during the prologue.
  1131  		dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfreglr))
  1132  
  1133  		Adduint8(ctxt, fs, dwarf.DW_CFA_val_offset)                // The previous value...
  1134  		dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfregsp)) // ...of the platform's SP register...
  1135  		dwarf.Uleb128put(dwarfctxt, fs, int64(0))                  // ...is CFA+0.
  1136  	} else {
  1137  		dwarf.Uleb128put(dwarfctxt, fs, int64(SysArch.PtrSize)) // ...plus the word size (because the call instruction implicitly adds one word to the frame).
  1138  
  1139  		Adduint8(ctxt, fs, dwarf.DW_CFA_offset_extended)                             // The previous value...
  1140  		dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfreglr))                   // ...of the return address...
  1141  		dwarf.Uleb128put(dwarfctxt, fs, int64(-SysArch.PtrSize)/dataAlignmentFactor) // ...is saved at [CFA - (PtrSize/4)].
  1142  	}
  1143  
  1144  	// 4 is to exclude the length field.
  1145  	pad := int64(cieReserve) + 4 - fs.Size
  1146  
  1147  	if pad < 0 {
  1148  		Exitf("dwarf: cieReserve too small by %d bytes.", -pad)
  1149  	}
  1150  
  1151  	Addbytes(ctxt, fs, zeros[:pad])
  1152  
  1153  	var deltaBuf []byte
  1154  	var pcsp Pciter
  1155  	for _, ctxt.Cursym = range ctxt.Textp {
  1156  		s := ctxt.Cursym
  1157  		if s.FuncInfo == nil {
  1158  			continue
  1159  		}
  1160  
  1161  		// Emit a FDE, Section 6.4.1.
  1162  		// First build the section contents into a byte buffer.
  1163  		deltaBuf = deltaBuf[:0]
  1164  		for pciterinit(ctxt, &pcsp, &s.FuncInfo.Pcsp); pcsp.done == 0; pciternext(&pcsp) {
  1165  			nextpc := pcsp.nextpc
  1166  
  1167  			// pciterinit goes up to the end of the function,
  1168  			// but DWARF expects us to stop just before the end.
  1169  			if int64(nextpc) == s.Size {
  1170  				nextpc--
  1171  				if nextpc < pcsp.pc {
  1172  					continue
  1173  				}
  1174  			}
  1175  
  1176  			if haslinkregister(ctxt) {
  1177  				// TODO(bryanpkc): This is imprecise. In general, the instruction
  1178  				// that stores the return address to the stack frame is not the
  1179  				// same one that allocates the frame.
  1180  				if pcsp.value > 0 {
  1181  					// The return address is preserved at (CFA-frame_size)
  1182  					// after a stack frame has been allocated.
  1183  					deltaBuf = append(deltaBuf, dwarf.DW_CFA_offset_extended_sf)
  1184  					deltaBuf = dwarf.AppendUleb128(deltaBuf, uint64(Thearch.Dwarfreglr))
  1185  					deltaBuf = dwarf.AppendSleb128(deltaBuf, -int64(pcsp.value)/dataAlignmentFactor)
  1186  				} else {
  1187  					// The return address is restored into the link register
  1188  					// when a stack frame has been de-allocated.
  1189  					deltaBuf = append(deltaBuf, dwarf.DW_CFA_same_value)
  1190  					deltaBuf = dwarf.AppendUleb128(deltaBuf, uint64(Thearch.Dwarfreglr))
  1191  				}
  1192  				deltaBuf = appendPCDeltaCFA(deltaBuf, int64(nextpc)-int64(pcsp.pc), int64(pcsp.value))
  1193  			} else {
  1194  				deltaBuf = appendPCDeltaCFA(deltaBuf, int64(nextpc)-int64(pcsp.pc), int64(SysArch.PtrSize)+int64(pcsp.value))
  1195  			}
  1196  		}
  1197  		pad := int(Rnd(int64(len(deltaBuf)), int64(SysArch.PtrSize))) - len(deltaBuf)
  1198  		deltaBuf = append(deltaBuf, zeros[:pad]...)
  1199  
  1200  		// Emit the FDE header, Section 6.4.1.
  1201  		//	4 bytes: length, must be multiple of thearch.ptrsize
  1202  		//	4 bytes: Pointer to the CIE above, at offset 0
  1203  		//	ptrsize: initial location
  1204  		//	ptrsize: address range
  1205  		Adduint32(ctxt, fs, uint32(4+2*SysArch.PtrSize+len(deltaBuf))) // length (excludes itself)
  1206  		if Linkmode == LinkExternal {
  1207  			adddwarfref(ctxt, fs, framesec, 4)
  1208  		} else {
  1209  			Adduint32(ctxt, fs, 0) // CIE offset
  1210  		}
  1211  		Addaddr(ctxt, fs, s)
  1212  		adduintxx(ctxt, fs, uint64(s.Size), SysArch.PtrSize) // address range
  1213  		Addbytes(ctxt, fs, deltaBuf)
  1214  	}
  1215  	return syms
  1216  }
  1217  
  1218  /*
  1219   *  Walk DWarfDebugInfoEntries, and emit .debug_info
  1220   */
  1221  const (
  1222  	COMPUNITHEADERSIZE = 4 + 2 + 4 + 1
  1223  )
  1224  
  1225  func writeinfo(ctxt *Link, syms []*Symbol, funcs []*Symbol) []*Symbol {
  1226  	if infosec == nil {
  1227  		infosec = Linklookup(ctxt, ".debug_info", 0)
  1228  	}
  1229  	infosec.R = infosec.R[:0]
  1230  	infosec.Type = obj.SDWARFINFO
  1231  	infosec.Attr |= AttrReachable
  1232  	syms = append(syms, infosec)
  1233  
  1234  	if arangessec == nil {
  1235  		arangessec = Linklookup(ctxt, ".dwarfaranges", 0)
  1236  	}
  1237  	arangessec.R = arangessec.R[:0]
  1238  
  1239  	var dwarfctxt dwarf.Context = dwctxt{ctxt}
  1240  
  1241  	for compunit := dwroot.Child; compunit != nil; compunit = compunit.Link {
  1242  		s := dtolsym(compunit.Sym)
  1243  
  1244  		// Write .debug_info Compilation Unit Header (sec 7.5.1)
  1245  		// Fields marked with (*) must be changed for 64-bit dwarf
  1246  		// This must match COMPUNITHEADERSIZE above.
  1247  		Adduint32(ctxt, s, 0) // unit_length (*), will be filled in later.
  1248  		Adduint16(ctxt, s, 2) // dwarf version (appendix F)
  1249  
  1250  		// debug_abbrev_offset (*)
  1251  		adddwarfref(ctxt, s, abbrevsym, 4)
  1252  
  1253  		Adduint8(ctxt, s, uint8(SysArch.PtrSize)) // address_size
  1254  
  1255  		dwarf.Uleb128put(dwarfctxt, s, int64(compunit.Abbrev))
  1256  		dwarf.PutAttrs(dwarfctxt, s, compunit.Abbrev, compunit.Attr)
  1257  
  1258  		cu := []*Symbol{s}
  1259  		if funcs != nil {
  1260  			cu = append(cu, funcs...)
  1261  			funcs = nil
  1262  		}
  1263  		cu = putdies(ctxt, dwarfctxt, cu, compunit.Child)
  1264  		var cusize int64
  1265  		for _, child := range cu {
  1266  			cusize += child.Size
  1267  		}
  1268  		cusize -= 4 // exclude the length field.
  1269  		setuint32(ctxt, s, 0, uint32(cusize))
  1270  		newattr(compunit, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, cusize, 0)
  1271  		syms = append(syms, cu...)
  1272  	}
  1273  	return syms
  1274  }
  1275  
  1276  /*
  1277   *  Emit .debug_pubnames/_types.  _info must have been written before,
  1278   *  because we need die->offs and infoo/infosize;
  1279   */
  1280  func ispubname(die *dwarf.DWDie) bool {
  1281  	switch die.Abbrev {
  1282  	case dwarf.DW_ABRV_FUNCTION, dwarf.DW_ABRV_VARIABLE:
  1283  		a := getattr(die, dwarf.DW_AT_external)
  1284  		return a != nil && a.Value != 0
  1285  	}
  1286  
  1287  	return false
  1288  }
  1289  
  1290  func ispubtype(die *dwarf.DWDie) bool {
  1291  	return die.Abbrev >= dwarf.DW_ABRV_NULLTYPE
  1292  }
  1293  
  1294  func writepub(ctxt *Link, sname string, ispub func(*dwarf.DWDie) bool, syms []*Symbol) []*Symbol {
  1295  	s := Linklookup(ctxt, sname, 0)
  1296  	s.Type = obj.SDWARFSECT
  1297  	syms = append(syms, s)
  1298  
  1299  	for compunit := dwroot.Child; compunit != nil; compunit = compunit.Link {
  1300  		sectionstart := s.Size
  1301  		culength := uint32(getattr(compunit, dwarf.DW_AT_byte_size).Value) + 4
  1302  
  1303  		// Write .debug_pubnames/types	Header (sec 6.1.1)
  1304  		Adduint32(ctxt, s, 0)                          // unit_length (*), will be filled in later.
  1305  		Adduint16(ctxt, s, 2)                          // dwarf version (appendix F)
  1306  		adddwarfref(ctxt, s, dtolsym(compunit.Sym), 4) // debug_info_offset (of the Comp unit Header)
  1307  		Adduint32(ctxt, s, culength)                   // debug_info_length
  1308  
  1309  		for die := compunit.Child; die != nil; die = die.Link {
  1310  			if !ispub(die) {
  1311  				continue
  1312  			}
  1313  			dwa := getattr(die, dwarf.DW_AT_name)
  1314  			name := dwa.Data.(string)
  1315  			if die.Sym == nil {
  1316  				fmt.Println("Missing sym for ", name)
  1317  			}
  1318  			adddwarfref(ctxt, s, dtolsym(die.Sym), 4)
  1319  			Addstring(ctxt, s, name)
  1320  		}
  1321  
  1322  		Adduint32(ctxt, s, 0)
  1323  
  1324  		setuint32(ctxt, s, sectionstart, uint32(s.Size-sectionstart)-4) // exclude the length field.
  1325  	}
  1326  
  1327  	return syms
  1328  }
  1329  
  1330  /*
  1331   *  emit .debug_aranges.  _info must have been written before,
  1332   *  because we need die->offs of dwarf.DW_globals.
  1333   */
  1334  func writearanges(ctxt *Link, syms []*Symbol) []*Symbol {
  1335  	s := Linklookup(ctxt, ".debug_aranges", 0)
  1336  	s.Type = obj.SDWARFSECT
  1337  	// The first tuple is aligned to a multiple of the size of a single tuple
  1338  	// (twice the size of an address)
  1339  	headersize := int(Rnd(4+2+4+1+1, int64(SysArch.PtrSize*2))) // don't count unit_length field itself
  1340  
  1341  	for compunit := dwroot.Child; compunit != nil; compunit = compunit.Link {
  1342  		b := getattr(compunit, dwarf.DW_AT_low_pc)
  1343  		if b == nil {
  1344  			continue
  1345  		}
  1346  		e := getattr(compunit, dwarf.DW_AT_high_pc)
  1347  		if e == nil {
  1348  			continue
  1349  		}
  1350  
  1351  		// Write .debug_aranges	 Header + entry	 (sec 6.1.2)
  1352  		unitlength := uint32(headersize) + 4*uint32(SysArch.PtrSize) - 4
  1353  		Adduint32(ctxt, s, unitlength) // unit_length (*)
  1354  		Adduint16(ctxt, s, 2)          // dwarf version (appendix F)
  1355  
  1356  		adddwarfref(ctxt, s, dtolsym(compunit.Sym), 4)
  1357  
  1358  		Adduint8(ctxt, s, uint8(SysArch.PtrSize)) // address_size
  1359  		Adduint8(ctxt, s, 0)                      // segment_size
  1360  		padding := headersize - (4 + 2 + 4 + 1 + 1)
  1361  		for i := 0; i < padding; i++ {
  1362  			Adduint8(ctxt, s, 0)
  1363  		}
  1364  
  1365  		Addaddrplus(ctxt, s, b.Data.(*Symbol), b.Value-(b.Data.(*Symbol)).Value)
  1366  		adduintxx(ctxt, s, uint64(e.Value-b.Value), SysArch.PtrSize)
  1367  		adduintxx(ctxt, s, 0, SysArch.PtrSize)
  1368  		adduintxx(ctxt, s, 0, SysArch.PtrSize)
  1369  	}
  1370  	if s.Size > 0 {
  1371  		syms = append(syms, s)
  1372  	}
  1373  	return syms
  1374  }
  1375  
  1376  func writegdbscript(ctxt *Link, syms []*Symbol) []*Symbol {
  1377  
  1378  	if gdbscript != "" {
  1379  		s := Linklookup(ctxt, ".debug_gdb_scripts", 0)
  1380  		s.Type = obj.SDWARFSECT
  1381  		syms = append(syms, s)
  1382  		Adduint8(ctxt, s, 1) // magic 1 byte?
  1383  		Addstring(ctxt, s, gdbscript)
  1384  	}
  1385  
  1386  	return syms
  1387  }
  1388  
  1389  var prototypedies map[string]*dwarf.DWDie
  1390  
  1391  /*
  1392   * This is the main entry point for generating dwarf.  After emitting
  1393   * the mandatory debug_abbrev section, it calls writelines() to set up
  1394   * the per-compilation unit part of the DIE tree, while simultaneously
  1395   * emitting the debug_line section.  When the final tree contains
  1396   * forward references, it will write the debug_info section in 2
  1397   * passes.
  1398   *
  1399   */
  1400  func dwarfgeneratedebugsyms(ctxt *Link) {
  1401  	if *FlagW { // disable dwarf
  1402  		return
  1403  	}
  1404  	if *FlagS && HEADTYPE != obj.Hdarwin {
  1405  		return
  1406  	}
  1407  	if HEADTYPE == obj.Hplan9 {
  1408  		return
  1409  	}
  1410  
  1411  	if Linkmode == LinkExternal {
  1412  		if !Iself && HEADTYPE != obj.Hdarwin {
  1413  			return
  1414  		}
  1415  	}
  1416  
  1417  	if ctxt.Debugvlog != 0 {
  1418  		ctxt.Logf("%5.2f dwarf\n", obj.Cputime())
  1419  	}
  1420  
  1421  	// Forctxt.Diagnostic messages.
  1422  	newattr(&dwtypes, dwarf.DW_AT_name, dwarf.DW_CLS_STRING, int64(len("dwtypes")), "dwtypes")
  1423  
  1424  	// Some types that must exist to define other ones.
  1425  	newdie(ctxt, &dwtypes, dwarf.DW_ABRV_NULLTYPE, "<unspecified>", 0)
  1426  
  1427  	newdie(ctxt, &dwtypes, dwarf.DW_ABRV_NULLTYPE, "void", 0)
  1428  	newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BARE_PTRTYPE, "unsafe.Pointer", 0)
  1429  
  1430  	die := newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BASETYPE, "uintptr", 0) // needed for array size
  1431  	newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_unsigned, 0)
  1432  	newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, int64(SysArch.PtrSize), 0)
  1433  	newattr(die, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, obj.KindUintptr, 0)
  1434  
  1435  	// Prototypes needed for type synthesis.
  1436  	prototypedies = map[string]*dwarf.DWDie{
  1437  		"type.runtime.stringStructDWARF": nil,
  1438  		"type.runtime.slice":             nil,
  1439  		"type.runtime.hmap":              nil,
  1440  		"type.runtime.bmap":              nil,
  1441  		"type.runtime.sudog":             nil,
  1442  		"type.runtime.waitq":             nil,
  1443  		"type.runtime.hchan":             nil,
  1444  	}
  1445  
  1446  	// Needed by the prettyprinter code for interface inspection.
  1447  	defgotype(ctxt, lookupOrDiag(ctxt, "type.runtime._type"))
  1448  
  1449  	defgotype(ctxt, lookupOrDiag(ctxt, "type.runtime.interfacetype"))
  1450  	defgotype(ctxt, lookupOrDiag(ctxt, "type.runtime.itab"))
  1451  
  1452  	genasmsym(ctxt, defdwsymb)
  1453  
  1454  	syms := writeabbrev(ctxt, nil)
  1455  	syms, funcs := writelines(ctxt, syms)
  1456  	syms = writeframes(ctxt, syms)
  1457  
  1458  	synthesizestringtypes(ctxt, dwtypes.Child)
  1459  	synthesizeslicetypes(ctxt, dwtypes.Child)
  1460  	synthesizemaptypes(ctxt, dwtypes.Child)
  1461  	synthesizechantypes(ctxt, dwtypes.Child)
  1462  
  1463  	reversetree(&dwroot.Child)
  1464  	reversetree(&dwtypes.Child)
  1465  	reversetree(&dwglobals.Child)
  1466  
  1467  	movetomodule(&dwtypes)
  1468  	movetomodule(&dwglobals)
  1469  
  1470  	// Need to reorder symbols so SDWARFINFO is after all SDWARFSECT
  1471  	// (but we need to generate dies before writepub)
  1472  	infosyms := writeinfo(ctxt, nil, funcs)
  1473  
  1474  	syms = writepub(ctxt, ".debug_pubnames", ispubname, syms)
  1475  	syms = writepub(ctxt, ".debug_pubtypes", ispubtype, syms)
  1476  	syms = writearanges(ctxt, syms)
  1477  	syms = writegdbscript(ctxt, syms)
  1478  	syms = append(syms, infosyms...)
  1479  	dwarfp = syms
  1480  }
  1481  
  1482  /*
  1483   *  Elf.
  1484   */
  1485  func dwarfaddshstrings(ctxt *Link, shstrtab *Symbol) {
  1486  	if *FlagW { // disable dwarf
  1487  		return
  1488  	}
  1489  
  1490  	Addstring(ctxt, shstrtab, ".debug_abbrev")
  1491  	Addstring(ctxt, shstrtab, ".debug_aranges")
  1492  	Addstring(ctxt, shstrtab, ".debug_frame")
  1493  	Addstring(ctxt, shstrtab, ".debug_info")
  1494  	Addstring(ctxt, shstrtab, ".debug_line")
  1495  	Addstring(ctxt, shstrtab, ".debug_pubnames")
  1496  	Addstring(ctxt, shstrtab, ".debug_pubtypes")
  1497  	Addstring(ctxt, shstrtab, ".debug_gdb_scripts")
  1498  	if Linkmode == LinkExternal {
  1499  		Addstring(ctxt, shstrtab, elfRelType+".debug_info")
  1500  		Addstring(ctxt, shstrtab, elfRelType+".debug_aranges")
  1501  		Addstring(ctxt, shstrtab, elfRelType+".debug_line")
  1502  		Addstring(ctxt, shstrtab, elfRelType+".debug_frame")
  1503  		Addstring(ctxt, shstrtab, elfRelType+".debug_pubnames")
  1504  		Addstring(ctxt, shstrtab, elfRelType+".debug_pubtypes")
  1505  	}
  1506  }
  1507  
  1508  // Add section symbols for DWARF debug info.  This is called before
  1509  // dwarfaddelfheaders.
  1510  func dwarfaddelfsectionsyms(ctxt *Link) {
  1511  	if *FlagW { // disable dwarf
  1512  		return
  1513  	}
  1514  	if Linkmode != LinkExternal {
  1515  		return
  1516  	}
  1517  	sym := Linklookup(ctxt, ".debug_info", 0)
  1518  	putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
  1519  	sym = Linklookup(ctxt, ".debug_abbrev", 0)
  1520  	putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
  1521  	sym = Linklookup(ctxt, ".debug_line", 0)
  1522  	putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
  1523  	sym = Linklookup(ctxt, ".debug_frame", 0)
  1524  	putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
  1525  }
  1526  
  1527  /*
  1528   * Windows PE
  1529   */
  1530  func dwarfaddpeheaders(ctxt *Link) {
  1531  	if *FlagW { // disable dwarf
  1532  		return
  1533  	}
  1534  	for sect := Segdwarf.Sect; sect != nil; sect = sect.Next {
  1535  		h := newPEDWARFSection(ctxt, sect.Name, int64(sect.Length))
  1536  		fileoff := sect.Vaddr - Segdwarf.Vaddr + Segdwarf.Fileoff
  1537  		if uint64(h.PointerToRawData) != fileoff {
  1538  			ctxt.Diag("%s.PointerToRawData = %#x, want %#x", sect.Name, h.PointerToRawData, fileoff)
  1539  			errorexit()
  1540  		}
  1541  	}
  1542  }