github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/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/objabi" 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 = objabi.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 = 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 = 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 == 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 = objabi.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 = 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 objabi.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 objabi.KindInt, 390 objabi.KindInt8, 391 objabi.KindInt16, 392 objabi.KindInt32, 393 objabi.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 objabi.KindUint, 399 objabi.KindUint8, 400 objabi.KindUint16, 401 objabi.KindUint32, 402 objabi.KindUint64, 403 objabi.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 objabi.KindFloat32, 409 objabi.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 objabi.KindComplex64, 415 objabi.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 objabi.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 objabi.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 objabi.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 objabi.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 objabi.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 objabi.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 objabi.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 objabi.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 objabi.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 for i := 0; i < nfields; i++ { 512 f := decodetypeStructFieldName(gotype, i) 513 s := decodetypeStructFieldType(gotype, i) 514 if f == "" { 515 f = s.Name[5:] // skip "type." 516 } 517 fld := newdie(ctxt, die, dwarf.DW_ABRV_STRUCTFIELD, f, 0) 518 newrefattr(fld, dwarf.DW_AT_type, defgotype(ctxt, s)) 519 offsetAnon := decodetypeStructFieldOffsAnon(ctxt.Arch, gotype, i) 520 newmemberoffsetattr(fld, int32(offsetAnon>>1)) 521 if offsetAnon&1 != 0 { // is embedded field 522 newattr(fld, dwarf.DW_AT_go_embedded_field, dwarf.DW_CLS_FLAG, 1, 0) 523 } 524 } 525 526 case objabi.KindUnsafePointer: 527 die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BARE_PTRTYPE, name, 0) 528 529 default: 530 Errorf(gotype, "dwarf: definition of unknown kind %d", kind) 531 die = newdie(ctxt, &dwtypes, dwarf.DW_ABRV_TYPEDECL, name, 0) 532 newrefattr(die, dwarf.DW_AT_type, mustFind(ctxt, "<unspecified>")) 533 } 534 535 newattr(die, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, int64(kind), 0) 536 537 if _, ok := prototypedies[gotype.Name]; ok { 538 prototypedies[gotype.Name] = die 539 } 540 541 return die 542 } 543 544 func nameFromDIESym(dwtype *Symbol) string { 545 return strings.TrimSuffix(dwtype.Name[len(dwarf.InfoPrefix):], "..def") 546 } 547 548 // Find or construct *T given T. 549 func defptrto(ctxt *Link, dwtype *Symbol) *Symbol { 550 ptrname := "*" + nameFromDIESym(dwtype) 551 die := find(ctxt, ptrname) 552 if die == nil { 553 pdie := newdie(ctxt, &dwtypes, dwarf.DW_ABRV_PTRTYPE, ptrname, 0) 554 newrefattr(pdie, dwarf.DW_AT_type, dwtype) 555 return dtolsym(pdie.Sym) 556 } 557 558 return die 559 } 560 561 // Copies src's children into dst. Copies attributes by value. 562 // DWAttr.data is copied as pointer only. If except is one of 563 // the top-level children, it will not be copied. 564 func copychildrenexcept(ctxt *Link, dst *dwarf.DWDie, src *dwarf.DWDie, except *dwarf.DWDie) { 565 for src = src.Child; src != nil; src = src.Link { 566 if src == except { 567 continue 568 } 569 c := newdie(ctxt, dst, src.Abbrev, getattr(src, dwarf.DW_AT_name).Data.(string), 0) 570 for a := src.Attr; a != nil; a = a.Link { 571 newattr(c, a.Atr, int(a.Cls), a.Value, a.Data) 572 } 573 copychildrenexcept(ctxt, c, src, nil) 574 } 575 576 reverselist(&dst.Child) 577 } 578 579 func copychildren(ctxt *Link, dst *dwarf.DWDie, src *dwarf.DWDie) { 580 copychildrenexcept(ctxt, dst, src, nil) 581 } 582 583 // Search children (assumed to have TAG_member) for the one named 584 // field and set its AT_type to dwtype 585 func substitutetype(structdie *dwarf.DWDie, field string, dwtype *Symbol) { 586 child := findchild(structdie, field) 587 if child == nil { 588 Exitf("dwarf substitutetype: %s does not have member %s", 589 getattr(structdie, dwarf.DW_AT_name).Data, field) 590 return 591 } 592 593 a := getattr(child, dwarf.DW_AT_type) 594 if a != nil { 595 a.Data = dwtype 596 } else { 597 newrefattr(child, dwarf.DW_AT_type, dwtype) 598 } 599 } 600 601 func findprotodie(ctxt *Link, name string) *dwarf.DWDie { 602 die, ok := prototypedies[name] 603 if ok && die == nil { 604 defgotype(ctxt, lookupOrDiag(ctxt, name)) 605 die = prototypedies[name] 606 } 607 return die 608 } 609 610 func synthesizestringtypes(ctxt *Link, die *dwarf.DWDie) { 611 prototype := walktypedef(findprotodie(ctxt, "type.runtime.stringStructDWARF")) 612 if prototype == nil { 613 return 614 } 615 616 for ; die != nil; die = die.Link { 617 if die.Abbrev != dwarf.DW_ABRV_STRINGTYPE { 618 continue 619 } 620 copychildren(ctxt, die, prototype) 621 } 622 } 623 624 func synthesizeslicetypes(ctxt *Link, die *dwarf.DWDie) { 625 prototype := walktypedef(findprotodie(ctxt, "type.runtime.slice")) 626 if prototype == nil { 627 return 628 } 629 630 for ; die != nil; die = die.Link { 631 if die.Abbrev != dwarf.DW_ABRV_SLICETYPE { 632 continue 633 } 634 copychildren(ctxt, die, prototype) 635 elem := getattr(die, dwarf.DW_AT_go_elem).Data.(*Symbol) 636 substitutetype(die, "array", defptrto(ctxt, elem)) 637 } 638 } 639 640 func mkinternaltypename(base string, arg1 string, arg2 string) string { 641 var buf string 642 643 if arg2 == "" { 644 buf = fmt.Sprintf("%s<%s>", base, arg1) 645 } else { 646 buf = fmt.Sprintf("%s<%s,%s>", base, arg1, arg2) 647 } 648 n := buf 649 return n 650 } 651 652 // synthesizemaptypes is way too closely married to runtime/hashmap.c 653 const ( 654 MaxKeySize = 128 655 MaxValSize = 128 656 BucketSize = 8 657 ) 658 659 func mkinternaltype(ctxt *Link, abbrev int, typename, keyname, valname string, f func(*dwarf.DWDie)) *Symbol { 660 name := mkinternaltypename(typename, keyname, valname) 661 symname := dwarf.InfoPrefix + name 662 s := ctxt.Syms.ROLookup(symname, 0) 663 if s != nil && s.Type == SDWARFINFO { 664 return s 665 } 666 die := newdie(ctxt, &dwtypes, abbrev, name, 0) 667 f(die) 668 return dtolsym(die.Sym) 669 } 670 671 func synthesizemaptypes(ctxt *Link, die *dwarf.DWDie) { 672 hash := walktypedef(findprotodie(ctxt, "type.runtime.hmap")) 673 bucket := walktypedef(findprotodie(ctxt, "type.runtime.bmap")) 674 675 if hash == nil { 676 return 677 } 678 679 for ; die != nil; die = die.Link { 680 if die.Abbrev != dwarf.DW_ABRV_MAPTYPE { 681 continue 682 } 683 gotype := getattr(die, dwarf.DW_AT_type).Data.(*Symbol) 684 keytype := decodetypeMapKey(gotype) 685 valtype := decodetypeMapValue(gotype) 686 keysize, valsize := decodetypeSize(ctxt.Arch, keytype), decodetypeSize(ctxt.Arch, valtype) 687 keytype, valtype = walksymtypedef(ctxt, defgotype(ctxt, keytype)), walksymtypedef(ctxt, defgotype(ctxt, valtype)) 688 689 // compute size info like hashmap.c does. 690 indirectKey, indirectVal := false, false 691 if keysize > MaxKeySize { 692 keysize = int64(SysArch.PtrSize) 693 indirectKey = true 694 } 695 if valsize > MaxValSize { 696 valsize = int64(SysArch.PtrSize) 697 indirectVal = true 698 } 699 700 // Construct type to represent an array of BucketSize keys 701 keyname := nameFromDIESym(keytype) 702 dwhks := mkinternaltype(ctxt, dwarf.DW_ABRV_ARRAYTYPE, "[]key", keyname, "", func(dwhk *dwarf.DWDie) { 703 newattr(dwhk, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, BucketSize*keysize, 0) 704 t := keytype 705 if indirectKey { 706 t = defptrto(ctxt, keytype) 707 } 708 newrefattr(dwhk, dwarf.DW_AT_type, t) 709 fld := newdie(ctxt, dwhk, dwarf.DW_ABRV_ARRAYRANGE, "size", 0) 710 newattr(fld, dwarf.DW_AT_count, dwarf.DW_CLS_CONSTANT, BucketSize, 0) 711 newrefattr(fld, dwarf.DW_AT_type, mustFind(ctxt, "uintptr")) 712 }) 713 714 // Construct type to represent an array of BucketSize values 715 valname := nameFromDIESym(valtype) 716 dwhvs := mkinternaltype(ctxt, dwarf.DW_ABRV_ARRAYTYPE, "[]val", valname, "", func(dwhv *dwarf.DWDie) { 717 newattr(dwhv, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, BucketSize*valsize, 0) 718 t := valtype 719 if indirectVal { 720 t = defptrto(ctxt, valtype) 721 } 722 newrefattr(dwhv, dwarf.DW_AT_type, t) 723 fld := newdie(ctxt, dwhv, dwarf.DW_ABRV_ARRAYRANGE, "size", 0) 724 newattr(fld, dwarf.DW_AT_count, dwarf.DW_CLS_CONSTANT, BucketSize, 0) 725 newrefattr(fld, dwarf.DW_AT_type, mustFind(ctxt, "uintptr")) 726 }) 727 728 // Construct bucket<K,V> 729 dwhbs := mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "bucket", keyname, valname, func(dwhb *dwarf.DWDie) { 730 // Copy over all fields except the field "data" from the generic 731 // bucket. "data" will be replaced with keys/values below. 732 copychildrenexcept(ctxt, dwhb, bucket, findchild(bucket, "data")) 733 734 fld := newdie(ctxt, dwhb, dwarf.DW_ABRV_STRUCTFIELD, "keys", 0) 735 newrefattr(fld, dwarf.DW_AT_type, dwhks) 736 newmemberoffsetattr(fld, BucketSize) 737 fld = newdie(ctxt, dwhb, dwarf.DW_ABRV_STRUCTFIELD, "values", 0) 738 newrefattr(fld, dwarf.DW_AT_type, dwhvs) 739 newmemberoffsetattr(fld, BucketSize+BucketSize*int32(keysize)) 740 fld = newdie(ctxt, dwhb, dwarf.DW_ABRV_STRUCTFIELD, "overflow", 0) 741 newrefattr(fld, dwarf.DW_AT_type, defptrto(ctxt, dtolsym(dwhb.Sym))) 742 newmemberoffsetattr(fld, BucketSize+BucketSize*(int32(keysize)+int32(valsize))) 743 if SysArch.RegSize > SysArch.PtrSize { 744 fld = newdie(ctxt, dwhb, dwarf.DW_ABRV_STRUCTFIELD, "pad", 0) 745 newrefattr(fld, dwarf.DW_AT_type, mustFind(ctxt, "uintptr")) 746 newmemberoffsetattr(fld, BucketSize+BucketSize*(int32(keysize)+int32(valsize))+int32(SysArch.PtrSize)) 747 } 748 749 newattr(dwhb, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, BucketSize+BucketSize*keysize+BucketSize*valsize+int64(SysArch.RegSize), 0) 750 }) 751 752 // Construct hash<K,V> 753 dwhs := mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "hash", keyname, valname, func(dwh *dwarf.DWDie) { 754 copychildren(ctxt, dwh, hash) 755 substitutetype(dwh, "buckets", defptrto(ctxt, dwhbs)) 756 substitutetype(dwh, "oldbuckets", defptrto(ctxt, dwhbs)) 757 newattr(dwh, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, getattr(hash, dwarf.DW_AT_byte_size).Value, nil) 758 }) 759 760 // make map type a pointer to hash<K,V> 761 newrefattr(die, dwarf.DW_AT_type, defptrto(ctxt, dwhs)) 762 } 763 } 764 765 func synthesizechantypes(ctxt *Link, die *dwarf.DWDie) { 766 sudog := walktypedef(findprotodie(ctxt, "type.runtime.sudog")) 767 waitq := walktypedef(findprotodie(ctxt, "type.runtime.waitq")) 768 hchan := walktypedef(findprotodie(ctxt, "type.runtime.hchan")) 769 if sudog == nil || waitq == nil || hchan == nil { 770 return 771 } 772 773 sudogsize := int(getattr(sudog, dwarf.DW_AT_byte_size).Value) 774 775 for ; die != nil; die = die.Link { 776 if die.Abbrev != dwarf.DW_ABRV_CHANTYPE { 777 continue 778 } 779 elemgotype := getattr(die, dwarf.DW_AT_type).Data.(*Symbol) 780 elemsize := decodetypeSize(ctxt.Arch, elemgotype) 781 elemname := elemgotype.Name[5:] 782 elemtype := walksymtypedef(ctxt, defgotype(ctxt, elemgotype)) 783 784 // sudog<T> 785 dwss := mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "sudog", elemname, "", func(dws *dwarf.DWDie) { 786 copychildren(ctxt, dws, sudog) 787 substitutetype(dws, "elem", elemtype) 788 if elemsize > 8 { 789 elemsize -= 8 790 } else { 791 elemsize = 0 792 } 793 newattr(dws, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, int64(sudogsize)+elemsize, nil) 794 }) 795 796 // waitq<T> 797 dwws := mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "waitq", elemname, "", func(dww *dwarf.DWDie) { 798 799 copychildren(ctxt, dww, waitq) 800 substitutetype(dww, "first", defptrto(ctxt, dwss)) 801 substitutetype(dww, "last", defptrto(ctxt, dwss)) 802 newattr(dww, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, getattr(waitq, dwarf.DW_AT_byte_size).Value, nil) 803 }) 804 805 // hchan<T> 806 dwhs := mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "hchan", elemname, "", func(dwh *dwarf.DWDie) { 807 copychildren(ctxt, dwh, hchan) 808 substitutetype(dwh, "recvq", dwws) 809 substitutetype(dwh, "sendq", dwws) 810 newattr(dwh, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, getattr(hchan, dwarf.DW_AT_byte_size).Value, nil) 811 }) 812 813 newrefattr(die, dwarf.DW_AT_type, defptrto(ctxt, dwhs)) 814 } 815 } 816 817 // For use with pass.c::genasmsym 818 func defdwsymb(ctxt *Link, sym *Symbol, s string, t SymbolType, v int64, gotype *Symbol) { 819 if strings.HasPrefix(s, "go.string.") { 820 return 821 } 822 if strings.HasPrefix(s, "runtime.gcbits.") { 823 return 824 } 825 826 if strings.HasPrefix(s, "type.") && s != "type.*" && !strings.HasPrefix(s, "type..") { 827 defgotype(ctxt, sym) 828 return 829 } 830 831 var dv *dwarf.DWDie 832 833 var dt *Symbol 834 switch t { 835 default: 836 return 837 838 case DataSym, BSSSym: 839 dv = newdie(ctxt, &dwglobals, dwarf.DW_ABRV_VARIABLE, s, int(sym.Version)) 840 newabslocexprattr(dv, v, sym) 841 if sym.Version == 0 { 842 newattr(dv, dwarf.DW_AT_external, dwarf.DW_CLS_FLAG, 1, 0) 843 } 844 fallthrough 845 846 case AutoSym, ParamSym: 847 dt = defgotype(ctxt, gotype) 848 } 849 850 if dv != nil { 851 newrefattr(dv, dwarf.DW_AT_type, dt) 852 } 853 } 854 855 func movetomodule(parent *dwarf.DWDie) { 856 die := dwroot.Child.Child 857 if die == nil { 858 dwroot.Child.Child = parent.Child 859 return 860 } 861 for die.Link != nil { 862 die = die.Link 863 } 864 die.Link = parent.Child 865 } 866 867 // If the pcln table contains runtime/runtime.go, use that to set gdbscript path. 868 func finddebugruntimepath(s *Symbol) { 869 if gdbscript != "" { 870 return 871 } 872 873 for i := range s.FuncInfo.File { 874 f := s.FuncInfo.File[i] 875 if i := strings.Index(f.Name, "runtime/runtime.go"); i >= 0 { 876 gdbscript = f.Name[:i] + "runtime/runtime-gdb.py" 877 break 878 } 879 } 880 } 881 882 /* 883 * Generate a sequence of opcodes that is as short as possible. 884 * See section 6.2.5 885 */ 886 const ( 887 LINE_BASE = -4 888 LINE_RANGE = 10 889 PC_RANGE = (255 - OPCODE_BASE) / LINE_RANGE 890 OPCODE_BASE = 10 891 ) 892 893 func putpclcdelta(linkctxt *Link, ctxt dwarf.Context, s *Symbol, deltaPC uint64, deltaLC int64) { 894 // Choose a special opcode that minimizes the number of bytes needed to 895 // encode the remaining PC delta and LC delta. 896 var opcode int64 897 if deltaLC < LINE_BASE { 898 if deltaPC >= PC_RANGE { 899 opcode = OPCODE_BASE + (LINE_RANGE * PC_RANGE) 900 } else { 901 opcode = OPCODE_BASE + (LINE_RANGE * int64(deltaPC)) 902 } 903 } else if deltaLC < LINE_BASE+LINE_RANGE { 904 if deltaPC >= PC_RANGE { 905 opcode = OPCODE_BASE + (deltaLC - LINE_BASE) + (LINE_RANGE * PC_RANGE) 906 if opcode > 255 { 907 opcode -= LINE_RANGE 908 } 909 } else { 910 opcode = OPCODE_BASE + (deltaLC - LINE_BASE) + (LINE_RANGE * int64(deltaPC)) 911 } 912 } else { 913 if deltaPC <= PC_RANGE { 914 opcode = OPCODE_BASE + (LINE_RANGE - 1) + (LINE_RANGE * int64(deltaPC)) 915 if opcode > 255 { 916 opcode = 255 917 } 918 } else { 919 // Use opcode 249 (pc+=23, lc+=5) or 255 (pc+=24, lc+=1). 920 // 921 // Let x=deltaPC-PC_RANGE. If we use opcode 255, x will be the remaining 922 // deltaPC that we need to encode separately before emitting 255. If we 923 // use opcode 249, we will need to encode x+1. If x+1 takes one more 924 // byte to encode than x, then we use opcode 255. 925 // 926 // In all other cases x and x+1 take the same number of bytes to encode, 927 // so we use opcode 249, which may save us a byte in encoding deltaLC, 928 // for similar reasons. 929 switch deltaPC - PC_RANGE { 930 // PC_RANGE is the largest deltaPC we can encode in one byte, using 931 // DW_LNS_const_add_pc. 932 // 933 // (1<<16)-1 is the largest deltaPC we can encode in three bytes, using 934 // DW_LNS_fixed_advance_pc. 935 // 936 // (1<<(7n))-1 is the largest deltaPC we can encode in n+1 bytes for 937 // n=1,3,4,5,..., using DW_LNS_advance_pc. 938 case PC_RANGE, (1 << 7) - 1, (1 << 16) - 1, (1 << 21) - 1, (1 << 28) - 1, 939 (1 << 35) - 1, (1 << 42) - 1, (1 << 49) - 1, (1 << 56) - 1, (1 << 63) - 1: 940 opcode = 255 941 default: 942 opcode = OPCODE_BASE + LINE_RANGE*PC_RANGE - 1 // 249 943 } 944 } 945 } 946 if opcode < OPCODE_BASE || opcode > 255 { 947 panic(fmt.Sprintf("produced invalid special opcode %d", opcode)) 948 } 949 950 // Subtract from deltaPC and deltaLC the amounts that the opcode will add. 951 deltaPC -= uint64((opcode - OPCODE_BASE) / LINE_RANGE) 952 deltaLC -= int64((opcode-OPCODE_BASE)%LINE_RANGE + LINE_BASE) 953 954 // Encode deltaPC. 955 if deltaPC != 0 { 956 if deltaPC <= PC_RANGE { 957 // Adjust the opcode so that we can use the 1-byte DW_LNS_const_add_pc 958 // instruction. 959 opcode -= LINE_RANGE * int64(PC_RANGE-deltaPC) 960 if opcode < OPCODE_BASE { 961 panic(fmt.Sprintf("produced invalid special opcode %d", opcode)) 962 } 963 Adduint8(linkctxt, s, dwarf.DW_LNS_const_add_pc) 964 } else if (1<<14) <= deltaPC && deltaPC < (1<<16) { 965 Adduint8(linkctxt, s, dwarf.DW_LNS_fixed_advance_pc) 966 Adduint16(linkctxt, s, uint16(deltaPC)) 967 } else { 968 Adduint8(linkctxt, s, dwarf.DW_LNS_advance_pc) 969 dwarf.Uleb128put(ctxt, s, int64(deltaPC)) 970 } 971 } 972 973 // Encode deltaLC. 974 if deltaLC != 0 { 975 Adduint8(linkctxt, s, dwarf.DW_LNS_advance_line) 976 dwarf.Sleb128put(ctxt, s, deltaLC) 977 } 978 979 // Output the special opcode. 980 Adduint8(linkctxt, s, uint8(opcode)) 981 } 982 983 /* 984 * Walk prog table, emit line program and build DIE tree. 985 */ 986 987 func getCompilationDir() string { 988 if dir, err := os.Getwd(); err == nil { 989 return dir 990 } 991 return "/" 992 } 993 994 func writelines(ctxt *Link, syms []*Symbol) ([]*Symbol, []*Symbol) { 995 var dwarfctxt dwarf.Context = dwctxt{ctxt} 996 if linesec == nil { 997 linesec = ctxt.Syms.Lookup(".debug_line", 0) 998 } 999 linesec.Type = SDWARFSECT 1000 linesec.R = linesec.R[:0] 1001 1002 ls := linesec 1003 syms = append(syms, ls) 1004 var funcs []*Symbol 1005 1006 unitstart := int64(-1) 1007 headerstart := int64(-1) 1008 headerend := int64(-1) 1009 epc := int64(0) 1010 var epcs *Symbol 1011 var dwinfo *dwarf.DWDie 1012 1013 lang := dwarf.DW_LANG_Go 1014 1015 s := ctxt.Textp[0] 1016 if ctxt.DynlinkingGo() && Headtype == objabi.Hdarwin { 1017 s = ctxt.Textp[1] // skip runtime.text 1018 } 1019 1020 dwinfo = newdie(ctxt, &dwroot, dwarf.DW_ABRV_COMPUNIT, "go", 0) 1021 newattr(dwinfo, dwarf.DW_AT_language, dwarf.DW_CLS_CONSTANT, int64(lang), 0) 1022 newattr(dwinfo, dwarf.DW_AT_stmt_list, dwarf.DW_CLS_PTR, 0, linesec) 1023 newattr(dwinfo, dwarf.DW_AT_low_pc, dwarf.DW_CLS_ADDRESS, s.Value, s) 1024 // OS X linker requires compilation dir or absolute path in comp unit name to output debug info. 1025 compDir := getCompilationDir() 1026 newattr(dwinfo, dwarf.DW_AT_comp_dir, dwarf.DW_CLS_STRING, int64(len(compDir)), compDir) 1027 1028 // Write .debug_line Line Number Program Header (sec 6.2.4) 1029 // Fields marked with (*) must be changed for 64-bit dwarf 1030 unitLengthOffset := ls.Size 1031 Adduint32(ctxt, ls, 0) // unit_length (*), filled in at end. 1032 unitstart = ls.Size 1033 Adduint16(ctxt, ls, 2) // dwarf version (appendix F) 1034 headerLengthOffset := ls.Size 1035 Adduint32(ctxt, ls, 0) // header_length (*), filled in at end. 1036 headerstart = ls.Size 1037 1038 // cpos == unitstart + 4 + 2 + 4 1039 Adduint8(ctxt, ls, 1) // minimum_instruction_length 1040 Adduint8(ctxt, ls, 1) // default_is_stmt 1041 Adduint8(ctxt, ls, LINE_BASE&0xFF) // line_base 1042 Adduint8(ctxt, ls, LINE_RANGE) // line_range 1043 Adduint8(ctxt, ls, OPCODE_BASE) // opcode_base 1044 Adduint8(ctxt, ls, 0) // standard_opcode_lengths[1] 1045 Adduint8(ctxt, ls, 1) // standard_opcode_lengths[2] 1046 Adduint8(ctxt, ls, 1) // standard_opcode_lengths[3] 1047 Adduint8(ctxt, ls, 1) // standard_opcode_lengths[4] 1048 Adduint8(ctxt, ls, 1) // standard_opcode_lengths[5] 1049 Adduint8(ctxt, ls, 0) // standard_opcode_lengths[6] 1050 Adduint8(ctxt, ls, 0) // standard_opcode_lengths[7] 1051 Adduint8(ctxt, ls, 0) // standard_opcode_lengths[8] 1052 Adduint8(ctxt, ls, 1) // standard_opcode_lengths[9] 1053 Adduint8(ctxt, ls, 0) // include_directories (empty) 1054 1055 for _, f := range ctxt.Filesyms { 1056 Addstring(ls, f.Name) 1057 Adduint8(ctxt, ls, 0) 1058 Adduint8(ctxt, ls, 0) 1059 Adduint8(ctxt, ls, 0) 1060 } 1061 1062 // 4 zeros: the string termination + 3 fields. 1063 Adduint8(ctxt, ls, 0) 1064 // terminate file_names. 1065 headerend = ls.Size 1066 1067 Adduint8(ctxt, ls, 0) // start extended opcode 1068 dwarf.Uleb128put(dwarfctxt, ls, 1+int64(SysArch.PtrSize)) 1069 Adduint8(ctxt, ls, dwarf.DW_LNE_set_address) 1070 1071 pc := s.Value 1072 line := 1 1073 file := 1 1074 Addaddr(ctxt, ls, s) 1075 1076 var pcfile Pciter 1077 var pcline Pciter 1078 for _, s := range ctxt.Textp { 1079 1080 epc = s.Value + s.Size 1081 epcs = s 1082 1083 dsym := ctxt.Syms.Lookup(dwarf.InfoPrefix+s.Name, int(s.Version)) 1084 dsym.Attr |= AttrHidden | AttrReachable 1085 dsym.Type = SDWARFINFO 1086 for _, r := range dsym.R { 1087 if r.Type == objabi.R_DWARFREF && r.Sym.Size == 0 { 1088 if Buildmode == BuildmodeShared { 1089 // These type symbols may not be present in BuildmodeShared. Skip. 1090 continue 1091 } 1092 n := nameFromDIESym(r.Sym) 1093 defgotype(ctxt, ctxt.Syms.Lookup("type."+n, 0)) 1094 } 1095 } 1096 funcs = append(funcs, dsym) 1097 1098 if s.FuncInfo == nil { 1099 continue 1100 } 1101 1102 finddebugruntimepath(s) 1103 1104 pciterinit(ctxt, &pcfile, &s.FuncInfo.Pcfile) 1105 pciterinit(ctxt, &pcline, &s.FuncInfo.Pcline) 1106 epc = pc 1107 for pcfile.done == 0 && pcline.done == 0 { 1108 if epc-s.Value >= int64(pcfile.nextpc) { 1109 pciternext(&pcfile) 1110 continue 1111 } 1112 1113 if epc-s.Value >= int64(pcline.nextpc) { 1114 pciternext(&pcline) 1115 continue 1116 } 1117 1118 if int32(file) != pcfile.value { 1119 Adduint8(ctxt, ls, dwarf.DW_LNS_set_file) 1120 dwarf.Uleb128put(dwarfctxt, ls, int64(pcfile.value)) 1121 file = int(pcfile.value) 1122 } 1123 1124 putpclcdelta(ctxt, dwarfctxt, ls, uint64(s.Value+int64(pcline.pc)-pc), int64(pcline.value)-int64(line)) 1125 1126 pc = s.Value + int64(pcline.pc) 1127 line = int(pcline.value) 1128 if pcfile.nextpc < pcline.nextpc { 1129 epc = int64(pcfile.nextpc) 1130 } else { 1131 epc = int64(pcline.nextpc) 1132 } 1133 epc += s.Value 1134 } 1135 } 1136 1137 Adduint8(ctxt, ls, 0) // start extended opcode 1138 dwarf.Uleb128put(dwarfctxt, ls, 1) 1139 Adduint8(ctxt, ls, dwarf.DW_LNE_end_sequence) 1140 1141 newattr(dwinfo, dwarf.DW_AT_high_pc, dwarf.DW_CLS_ADDRESS, epc+1, epcs) 1142 1143 setuint32(ctxt, ls, unitLengthOffset, uint32(ls.Size-unitstart)) 1144 setuint32(ctxt, ls, headerLengthOffset, uint32(headerend-headerstart)) 1145 1146 return syms, funcs 1147 } 1148 1149 /* 1150 * Emit .debug_frame 1151 */ 1152 const ( 1153 dataAlignmentFactor = -4 1154 ) 1155 1156 // appendPCDeltaCFA appends per-PC CFA deltas to b and returns the final slice. 1157 func appendPCDeltaCFA(b []byte, deltapc, cfa int64) []byte { 1158 b = append(b, dwarf.DW_CFA_def_cfa_offset_sf) 1159 b = dwarf.AppendSleb128(b, cfa/dataAlignmentFactor) 1160 1161 switch { 1162 case deltapc < 0x40: 1163 b = append(b, uint8(dwarf.DW_CFA_advance_loc+deltapc)) 1164 case deltapc < 0x100: 1165 b = append(b, dwarf.DW_CFA_advance_loc1) 1166 b = append(b, uint8(deltapc)) 1167 case deltapc < 0x10000: 1168 b = append(b, dwarf.DW_CFA_advance_loc2) 1169 b = Thearch.Append16(b, uint16(deltapc)) 1170 default: 1171 b = append(b, dwarf.DW_CFA_advance_loc4) 1172 b = Thearch.Append32(b, uint32(deltapc)) 1173 } 1174 return b 1175 } 1176 1177 func writeframes(ctxt *Link, syms []*Symbol) []*Symbol { 1178 var dwarfctxt dwarf.Context = dwctxt{ctxt} 1179 if framesec == nil { 1180 framesec = ctxt.Syms.Lookup(".debug_frame", 0) 1181 } 1182 framesec.Type = SDWARFSECT 1183 framesec.R = framesec.R[:0] 1184 fs := framesec 1185 syms = append(syms, fs) 1186 1187 // Emit the CIE, Section 6.4.1 1188 cieReserve := uint32(16) 1189 if haslinkregister(ctxt) { 1190 cieReserve = 32 1191 } 1192 Adduint32(ctxt, fs, cieReserve) // initial length, must be multiple of thearch.ptrsize 1193 Adduint32(ctxt, fs, 0xffffffff) // cid. 1194 Adduint8(ctxt, fs, 3) // dwarf version (appendix F) 1195 Adduint8(ctxt, fs, 0) // augmentation "" 1196 dwarf.Uleb128put(dwarfctxt, fs, 1) // code_alignment_factor 1197 dwarf.Sleb128put(dwarfctxt, fs, dataAlignmentFactor) // all CFI offset calculations include multiplication with this factor 1198 dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfreglr)) // return_address_register 1199 1200 Adduint8(ctxt, fs, dwarf.DW_CFA_def_cfa) // Set the current frame address.. 1201 dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfregsp)) // ...to use the value in the platform's SP register (defined in l.go)... 1202 if haslinkregister(ctxt) { 1203 dwarf.Uleb128put(dwarfctxt, fs, int64(0)) // ...plus a 0 offset. 1204 1205 Adduint8(ctxt, fs, dwarf.DW_CFA_same_value) // The platform's link register is unchanged during the prologue. 1206 dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfreglr)) 1207 1208 Adduint8(ctxt, fs, dwarf.DW_CFA_val_offset) // The previous value... 1209 dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfregsp)) // ...of the platform's SP register... 1210 dwarf.Uleb128put(dwarfctxt, fs, int64(0)) // ...is CFA+0. 1211 } else { 1212 dwarf.Uleb128put(dwarfctxt, fs, int64(SysArch.PtrSize)) // ...plus the word size (because the call instruction implicitly adds one word to the frame). 1213 1214 Adduint8(ctxt, fs, dwarf.DW_CFA_offset_extended) // The previous value... 1215 dwarf.Uleb128put(dwarfctxt, fs, int64(Thearch.Dwarfreglr)) // ...of the return address... 1216 dwarf.Uleb128put(dwarfctxt, fs, int64(-SysArch.PtrSize)/dataAlignmentFactor) // ...is saved at [CFA - (PtrSize/4)]. 1217 } 1218 1219 // 4 is to exclude the length field. 1220 pad := int64(cieReserve) + 4 - fs.Size 1221 1222 if pad < 0 { 1223 Exitf("dwarf: cieReserve too small by %d bytes.", -pad) 1224 } 1225 1226 Addbytes(fs, zeros[:pad]) 1227 1228 var deltaBuf []byte 1229 var pcsp Pciter 1230 for _, s := range ctxt.Textp { 1231 if s.FuncInfo == nil { 1232 continue 1233 } 1234 1235 // Emit a FDE, Section 6.4.1. 1236 // First build the section contents into a byte buffer. 1237 deltaBuf = deltaBuf[:0] 1238 for pciterinit(ctxt, &pcsp, &s.FuncInfo.Pcsp); pcsp.done == 0; pciternext(&pcsp) { 1239 nextpc := pcsp.nextpc 1240 1241 // pciterinit goes up to the end of the function, 1242 // but DWARF expects us to stop just before the end. 1243 if int64(nextpc) == s.Size { 1244 nextpc-- 1245 if nextpc < pcsp.pc { 1246 continue 1247 } 1248 } 1249 1250 if haslinkregister(ctxt) { 1251 // TODO(bryanpkc): This is imprecise. In general, the instruction 1252 // that stores the return address to the stack frame is not the 1253 // same one that allocates the frame. 1254 if pcsp.value > 0 { 1255 // The return address is preserved at (CFA-frame_size) 1256 // after a stack frame has been allocated. 1257 deltaBuf = append(deltaBuf, dwarf.DW_CFA_offset_extended_sf) 1258 deltaBuf = dwarf.AppendUleb128(deltaBuf, uint64(Thearch.Dwarfreglr)) 1259 deltaBuf = dwarf.AppendSleb128(deltaBuf, -int64(pcsp.value)/dataAlignmentFactor) 1260 } else { 1261 // The return address is restored into the link register 1262 // when a stack frame has been de-allocated. 1263 deltaBuf = append(deltaBuf, dwarf.DW_CFA_same_value) 1264 deltaBuf = dwarf.AppendUleb128(deltaBuf, uint64(Thearch.Dwarfreglr)) 1265 } 1266 deltaBuf = appendPCDeltaCFA(deltaBuf, int64(nextpc)-int64(pcsp.pc), int64(pcsp.value)) 1267 } else { 1268 deltaBuf = appendPCDeltaCFA(deltaBuf, int64(nextpc)-int64(pcsp.pc), int64(SysArch.PtrSize)+int64(pcsp.value)) 1269 } 1270 } 1271 pad := int(Rnd(int64(len(deltaBuf)), int64(SysArch.PtrSize))) - len(deltaBuf) 1272 deltaBuf = append(deltaBuf, zeros[:pad]...) 1273 1274 // Emit the FDE header, Section 6.4.1. 1275 // 4 bytes: length, must be multiple of thearch.ptrsize 1276 // 4 bytes: Pointer to the CIE above, at offset 0 1277 // ptrsize: initial location 1278 // ptrsize: address range 1279 Adduint32(ctxt, fs, uint32(4+2*SysArch.PtrSize+len(deltaBuf))) // length (excludes itself) 1280 if Linkmode == LinkExternal { 1281 adddwarfref(ctxt, fs, framesec, 4) 1282 } else { 1283 Adduint32(ctxt, fs, 0) // CIE offset 1284 } 1285 Addaddr(ctxt, fs, s) 1286 adduintxx(ctxt, fs, uint64(s.Size), SysArch.PtrSize) // address range 1287 Addbytes(fs, deltaBuf) 1288 } 1289 return syms 1290 } 1291 1292 /* 1293 * Walk DWarfDebugInfoEntries, and emit .debug_info 1294 */ 1295 const ( 1296 COMPUNITHEADERSIZE = 4 + 2 + 4 + 1 1297 ) 1298 1299 func writeinfo(ctxt *Link, syms []*Symbol, funcs []*Symbol) []*Symbol { 1300 if infosec == nil { 1301 infosec = ctxt.Syms.Lookup(".debug_info", 0) 1302 } 1303 infosec.R = infosec.R[:0] 1304 infosec.Type = SDWARFINFO 1305 infosec.Attr |= AttrReachable 1306 syms = append(syms, infosec) 1307 1308 if arangessec == nil { 1309 arangessec = ctxt.Syms.Lookup(".dwarfaranges", 0) 1310 } 1311 arangessec.R = arangessec.R[:0] 1312 1313 var dwarfctxt dwarf.Context = dwctxt{ctxt} 1314 1315 for compunit := dwroot.Child; compunit != nil; compunit = compunit.Link { 1316 s := dtolsym(compunit.Sym) 1317 1318 // Write .debug_info Compilation Unit Header (sec 7.5.1) 1319 // Fields marked with (*) must be changed for 64-bit dwarf 1320 // This must match COMPUNITHEADERSIZE above. 1321 Adduint32(ctxt, s, 0) // unit_length (*), will be filled in later. 1322 Adduint16(ctxt, s, 2) // dwarf version (appendix F) 1323 1324 // debug_abbrev_offset (*) 1325 adddwarfref(ctxt, s, abbrevsym, 4) 1326 1327 Adduint8(ctxt, s, uint8(SysArch.PtrSize)) // address_size 1328 1329 dwarf.Uleb128put(dwarfctxt, s, int64(compunit.Abbrev)) 1330 dwarf.PutAttrs(dwarfctxt, s, compunit.Abbrev, compunit.Attr) 1331 1332 cu := []*Symbol{s} 1333 if funcs != nil { 1334 cu = append(cu, funcs...) 1335 funcs = nil 1336 } 1337 cu = putdies(ctxt, dwarfctxt, cu, compunit.Child) 1338 var cusize int64 1339 for _, child := range cu { 1340 cusize += child.Size 1341 } 1342 cusize -= 4 // exclude the length field. 1343 setuint32(ctxt, s, 0, uint32(cusize)) 1344 newattr(compunit, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, cusize, 0) 1345 syms = append(syms, cu...) 1346 } 1347 return syms 1348 } 1349 1350 /* 1351 * Emit .debug_pubnames/_types. _info must have been written before, 1352 * because we need die->offs and infoo/infosize; 1353 */ 1354 func ispubname(die *dwarf.DWDie) bool { 1355 switch die.Abbrev { 1356 case dwarf.DW_ABRV_FUNCTION, dwarf.DW_ABRV_VARIABLE: 1357 a := getattr(die, dwarf.DW_AT_external) 1358 return a != nil && a.Value != 0 1359 } 1360 1361 return false 1362 } 1363 1364 func ispubtype(die *dwarf.DWDie) bool { 1365 return die.Abbrev >= dwarf.DW_ABRV_NULLTYPE 1366 } 1367 1368 func writepub(ctxt *Link, sname string, ispub func(*dwarf.DWDie) bool, syms []*Symbol) []*Symbol { 1369 s := ctxt.Syms.Lookup(sname, 0) 1370 s.Type = SDWARFSECT 1371 syms = append(syms, s) 1372 1373 for compunit := dwroot.Child; compunit != nil; compunit = compunit.Link { 1374 sectionstart := s.Size 1375 culength := uint32(getattr(compunit, dwarf.DW_AT_byte_size).Value) + 4 1376 1377 // Write .debug_pubnames/types Header (sec 6.1.1) 1378 Adduint32(ctxt, s, 0) // unit_length (*), will be filled in later. 1379 Adduint16(ctxt, s, 2) // dwarf version (appendix F) 1380 adddwarfref(ctxt, s, dtolsym(compunit.Sym), 4) // debug_info_offset (of the Comp unit Header) 1381 Adduint32(ctxt, s, culength) // debug_info_length 1382 1383 for die := compunit.Child; die != nil; die = die.Link { 1384 if !ispub(die) { 1385 continue 1386 } 1387 dwa := getattr(die, dwarf.DW_AT_name) 1388 name := dwa.Data.(string) 1389 if die.Sym == nil { 1390 fmt.Println("Missing sym for ", name) 1391 } 1392 adddwarfref(ctxt, s, dtolsym(die.Sym), 4) 1393 Addstring(s, name) 1394 } 1395 1396 Adduint32(ctxt, s, 0) 1397 1398 setuint32(ctxt, s, sectionstart, uint32(s.Size-sectionstart)-4) // exclude the length field. 1399 } 1400 1401 return syms 1402 } 1403 1404 /* 1405 * emit .debug_aranges. _info must have been written before, 1406 * because we need die->offs of dwarf.DW_globals. 1407 */ 1408 func writearanges(ctxt *Link, syms []*Symbol) []*Symbol { 1409 s := ctxt.Syms.Lookup(".debug_aranges", 0) 1410 s.Type = SDWARFSECT 1411 // The first tuple is aligned to a multiple of the size of a single tuple 1412 // (twice the size of an address) 1413 headersize := int(Rnd(4+2+4+1+1, int64(SysArch.PtrSize*2))) // don't count unit_length field itself 1414 1415 for compunit := dwroot.Child; compunit != nil; compunit = compunit.Link { 1416 b := getattr(compunit, dwarf.DW_AT_low_pc) 1417 if b == nil { 1418 continue 1419 } 1420 e := getattr(compunit, dwarf.DW_AT_high_pc) 1421 if e == nil { 1422 continue 1423 } 1424 1425 // Write .debug_aranges Header + entry (sec 6.1.2) 1426 unitlength := uint32(headersize) + 4*uint32(SysArch.PtrSize) - 4 1427 Adduint32(ctxt, s, unitlength) // unit_length (*) 1428 Adduint16(ctxt, s, 2) // dwarf version (appendix F) 1429 1430 adddwarfref(ctxt, s, dtolsym(compunit.Sym), 4) 1431 1432 Adduint8(ctxt, s, uint8(SysArch.PtrSize)) // address_size 1433 Adduint8(ctxt, s, 0) // segment_size 1434 padding := headersize - (4 + 2 + 4 + 1 + 1) 1435 for i := 0; i < padding; i++ { 1436 Adduint8(ctxt, s, 0) 1437 } 1438 1439 Addaddrplus(ctxt, s, b.Data.(*Symbol), b.Value-(b.Data.(*Symbol)).Value) 1440 adduintxx(ctxt, s, uint64(e.Value-b.Value), SysArch.PtrSize) 1441 adduintxx(ctxt, s, 0, SysArch.PtrSize) 1442 adduintxx(ctxt, s, 0, SysArch.PtrSize) 1443 } 1444 if s.Size > 0 { 1445 syms = append(syms, s) 1446 } 1447 return syms 1448 } 1449 1450 func writegdbscript(ctxt *Link, syms []*Symbol) []*Symbol { 1451 1452 if gdbscript != "" { 1453 s := ctxt.Syms.Lookup(".debug_gdb_scripts", 0) 1454 s.Type = SDWARFSECT 1455 syms = append(syms, s) 1456 Adduint8(ctxt, s, 1) // magic 1 byte? 1457 Addstring(s, gdbscript) 1458 } 1459 1460 return syms 1461 } 1462 1463 var prototypedies map[string]*dwarf.DWDie 1464 1465 /* 1466 * This is the main entry point for generating dwarf. After emitting 1467 * the mandatory debug_abbrev section, it calls writelines() to set up 1468 * the per-compilation unit part of the DIE tree, while simultaneously 1469 * emitting the debug_line section. When the final tree contains 1470 * forward references, it will write the debug_info section in 2 1471 * passes. 1472 * 1473 */ 1474 func dwarfgeneratedebugsyms(ctxt *Link) { 1475 if *FlagW { // disable dwarf 1476 return 1477 } 1478 if *FlagS && Headtype != objabi.Hdarwin { 1479 return 1480 } 1481 if Headtype == objabi.Hplan9 { 1482 return 1483 } 1484 1485 if Linkmode == LinkExternal { 1486 switch { 1487 case Iself: 1488 case Headtype == objabi.Hdarwin: 1489 case Headtype == objabi.Hwindows: 1490 default: 1491 return 1492 } 1493 } 1494 1495 if ctxt.Debugvlog != 0 { 1496 ctxt.Logf("%5.2f dwarf\n", Cputime()) 1497 } 1498 1499 // Forctxt.Diagnostic messages. 1500 newattr(&dwtypes, dwarf.DW_AT_name, dwarf.DW_CLS_STRING, int64(len("dwtypes")), "dwtypes") 1501 1502 // Some types that must exist to define other ones. 1503 newdie(ctxt, &dwtypes, dwarf.DW_ABRV_NULLTYPE, "<unspecified>", 0) 1504 1505 newdie(ctxt, &dwtypes, dwarf.DW_ABRV_NULLTYPE, "void", 0) 1506 newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BARE_PTRTYPE, "unsafe.Pointer", 0) 1507 1508 die := newdie(ctxt, &dwtypes, dwarf.DW_ABRV_BASETYPE, "uintptr", 0) // needed for array size 1509 newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_unsigned, 0) 1510 newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, int64(SysArch.PtrSize), 0) 1511 newattr(die, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, objabi.KindUintptr, 0) 1512 1513 // Prototypes needed for type synthesis. 1514 prototypedies = map[string]*dwarf.DWDie{ 1515 "type.runtime.stringStructDWARF": nil, 1516 "type.runtime.slice": nil, 1517 "type.runtime.hmap": nil, 1518 "type.runtime.bmap": nil, 1519 "type.runtime.sudog": nil, 1520 "type.runtime.waitq": nil, 1521 "type.runtime.hchan": nil, 1522 } 1523 1524 // Needed by the prettyprinter code for interface inspection. 1525 for _, typ := range []string{ 1526 "type.runtime._type", 1527 "type.runtime.arraytype", 1528 "type.runtime.chantype", 1529 "type.runtime.functype", 1530 "type.runtime.maptype", 1531 "type.runtime.ptrtype", 1532 "type.runtime.slicetype", 1533 "type.runtime.structtype", 1534 "type.runtime.interfacetype", 1535 "type.runtime.itab", 1536 "type.runtime.imethod"} { 1537 defgotype(ctxt, lookupOrDiag(ctxt, typ)) 1538 } 1539 1540 genasmsym(ctxt, defdwsymb) 1541 1542 syms := writeabbrev(ctxt, nil) 1543 syms, funcs := writelines(ctxt, syms) 1544 syms = writeframes(ctxt, syms) 1545 1546 synthesizestringtypes(ctxt, dwtypes.Child) 1547 synthesizeslicetypes(ctxt, dwtypes.Child) 1548 synthesizemaptypes(ctxt, dwtypes.Child) 1549 synthesizechantypes(ctxt, dwtypes.Child) 1550 1551 reversetree(&dwroot.Child) 1552 reversetree(&dwtypes.Child) 1553 reversetree(&dwglobals.Child) 1554 1555 movetomodule(&dwtypes) 1556 movetomodule(&dwglobals) 1557 1558 // Need to reorder symbols so SDWARFINFO is after all SDWARFSECT 1559 // (but we need to generate dies before writepub) 1560 infosyms := writeinfo(ctxt, nil, funcs) 1561 1562 syms = writepub(ctxt, ".debug_pubnames", ispubname, syms) 1563 syms = writepub(ctxt, ".debug_pubtypes", ispubtype, syms) 1564 syms = writearanges(ctxt, syms) 1565 syms = writegdbscript(ctxt, syms) 1566 syms = append(syms, infosyms...) 1567 dwarfp = syms 1568 } 1569 1570 /* 1571 * Elf. 1572 */ 1573 func dwarfaddshstrings(ctxt *Link, shstrtab *Symbol) { 1574 if *FlagW { // disable dwarf 1575 return 1576 } 1577 1578 Addstring(shstrtab, ".debug_abbrev") 1579 Addstring(shstrtab, ".debug_aranges") 1580 Addstring(shstrtab, ".debug_frame") 1581 Addstring(shstrtab, ".debug_info") 1582 Addstring(shstrtab, ".debug_line") 1583 Addstring(shstrtab, ".debug_pubnames") 1584 Addstring(shstrtab, ".debug_pubtypes") 1585 Addstring(shstrtab, ".debug_gdb_scripts") 1586 if Linkmode == LinkExternal { 1587 Addstring(shstrtab, elfRelType+".debug_info") 1588 Addstring(shstrtab, elfRelType+".debug_aranges") 1589 Addstring(shstrtab, elfRelType+".debug_line") 1590 Addstring(shstrtab, elfRelType+".debug_frame") 1591 Addstring(shstrtab, elfRelType+".debug_pubnames") 1592 Addstring(shstrtab, elfRelType+".debug_pubtypes") 1593 } 1594 } 1595 1596 // Add section symbols for DWARF debug info. This is called before 1597 // dwarfaddelfheaders. 1598 func dwarfaddelfsectionsyms(ctxt *Link) { 1599 if *FlagW { // disable dwarf 1600 return 1601 } 1602 if Linkmode != LinkExternal { 1603 return 1604 } 1605 sym := ctxt.Syms.Lookup(".debug_info", 0) 1606 putelfsectionsym(sym, sym.Sect.Elfsect.shnum) 1607 sym = ctxt.Syms.Lookup(".debug_abbrev", 0) 1608 putelfsectionsym(sym, sym.Sect.Elfsect.shnum) 1609 sym = ctxt.Syms.Lookup(".debug_line", 0) 1610 putelfsectionsym(sym, sym.Sect.Elfsect.shnum) 1611 sym = ctxt.Syms.Lookup(".debug_frame", 0) 1612 putelfsectionsym(sym, sym.Sect.Elfsect.shnum) 1613 } 1614 1615 /* 1616 * Windows PE 1617 */ 1618 func dwarfaddpeheaders(ctxt *Link) { 1619 if *FlagW { // disable dwarf 1620 return 1621 } 1622 for _, sect := range Segdwarf.Sections { 1623 h := newPEDWARFSection(ctxt, sect.Name, int64(sect.Length)) 1624 fileoff := sect.Vaddr - Segdwarf.Vaddr + Segdwarf.Fileoff 1625 if uint64(h.PointerToRawData) != fileoff { 1626 Exitf("%s.PointerToRawData = %#x, want %#x", sect.Name, h.PointerToRawData, fileoff) 1627 } 1628 } 1629 }