DwarfDebug.cpp revision 1d5cc1db597a1a0f760b6d600819e4aef5aea26c
1//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains support for writing dwarf debug info into asm files. 11// 12//===----------------------------------------------------------------------===// 13#define DEBUG_TYPE "dwarfdebug" 14#include "DwarfDebug.h" 15#include "llvm/Module.h" 16#include "llvm/CodeGen/MachineFunction.h" 17#include "llvm/CodeGen/MachineModuleInfo.h" 18#include "llvm/MC/MCSection.h" 19#include "llvm/MC/MCStreamer.h" 20#include "llvm/MC/MCAsmInfo.h" 21#include "llvm/Target/TargetData.h" 22#include "llvm/Target/TargetFrameInfo.h" 23#include "llvm/Target/TargetLoweringObjectFile.h" 24#include "llvm/Target/TargetRegisterInfo.h" 25#include "llvm/ADT/StringExtras.h" 26#include "llvm/Support/Debug.h" 27#include "llvm/Support/ErrorHandling.h" 28#include "llvm/Support/Mangler.h" 29#include "llvm/Support/Timer.h" 30#include "llvm/System/Path.h" 31using namespace llvm; 32 33static TimerGroup &getDwarfTimerGroup() { 34 static TimerGroup DwarfTimerGroup("Dwarf Debugging"); 35 return DwarfTimerGroup; 36} 37 38//===----------------------------------------------------------------------===// 39 40/// Configuration values for initial hash set sizes (log2). 41/// 42static const unsigned InitAbbreviationsSetSize = 9; // log2(512) 43 44namespace llvm { 45 46//===----------------------------------------------------------------------===// 47/// CompileUnit - This dwarf writer support class manages information associate 48/// with a source file. 49class CompileUnit { 50 /// ID - File identifier for source. 51 /// 52 unsigned ID; 53 54 /// Die - Compile unit debug information entry. 55 /// 56 DIE *CUDie; 57 58 /// IndexTyDie - An anonymous type for index type. 59 DIE *IndexTyDie; 60 61 /// GVToDieMap - Tracks the mapping of unit level debug informaton 62 /// variables to debug information entries. 63 /// FIXME : Rename GVToDieMap -> NodeToDieMap 64 ValueMap<MDNode *, DIE *> GVToDieMap; 65 66 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton 67 /// descriptors to debug information entries using a DIEEntry proxy. 68 /// FIXME : Rename 69 ValueMap<MDNode *, DIEEntry *> GVToDIEEntryMap; 70 71 /// Globals - A map of globally visible named entities for this unit. 72 /// 73 StringMap<DIE*> Globals; 74 75 /// GlobalTypes - A map of globally visible types for this unit. 76 /// 77 StringMap<DIE*> GlobalTypes; 78 79public: 80 CompileUnit(unsigned I, DIE *D) 81 : ID(I), CUDie(D), IndexTyDie(0) {} 82 ~CompileUnit() { delete CUDie; delete IndexTyDie; } 83 84 // Accessors. 85 unsigned getID() const { return ID; } 86 DIE* getCUDie() const { return CUDie; } 87 const StringMap<DIE*> &getGlobals() const { return Globals; } 88 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; } 89 90 /// hasContent - Return true if this compile unit has something to write out. 91 /// 92 bool hasContent() const { return !CUDie->getChildren().empty(); } 93 94 /// addGlobal - Add a new global entity to the compile unit. 95 /// 96 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; } 97 98 /// addGlobalType - Add a new global type to the compile unit. 99 /// 100 void addGlobalType(const std::string &Name, DIE *Die) { 101 GlobalTypes[Name] = Die; 102 } 103 104 /// getDIE - Returns the debug information entry map slot for the 105 /// specified debug variable. 106 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); } 107 108 /// insertDIE - Insert DIE into the map. 109 void insertDIE(MDNode *N, DIE *D) { 110 GVToDieMap.insert(std::make_pair(N, D)); 111 } 112 113 /// getDIEEntry - Returns the debug information entry for the speciefied 114 /// debug variable. 115 DIEEntry *getDIEEntry(MDNode *N) { return GVToDIEEntryMap.lookup(N); } 116 117 /// insertDIEEntry - Insert debug information entry into the map. 118 void insertDIEEntry(MDNode *N, DIEEntry *E) { 119 GVToDIEEntryMap.insert(std::make_pair(N, E)); 120 } 121 122 /// addDie - Adds or interns the DIE to the compile unit. 123 /// 124 void addDie(DIE *Buffer) { 125 this->CUDie->addChild(Buffer); 126 } 127 128 // getIndexTyDie - Get an anonymous type for index type. 129 DIE *getIndexTyDie() { 130 return IndexTyDie; 131 } 132 133 // setIndexTyDie - Set D as anonymous type for index which can be reused 134 // later. 135 void setIndexTyDie(DIE *D) { 136 IndexTyDie = D; 137 } 138 139}; 140 141//===----------------------------------------------------------------------===// 142/// DbgVariable - This class is used to track local variable information. 143/// 144class DbgVariable { 145 DIVariable Var; // Variable Descriptor. 146 unsigned FrameIndex; // Variable frame index. 147 DbgVariable *AbstractVar; // Abstract variable for this variable. 148 DIE *TheDIE; 149public: 150 DbgVariable(DIVariable V, unsigned I) 151 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {} 152 153 // Accessors. 154 DIVariable getVariable() const { return Var; } 155 unsigned getFrameIndex() const { return FrameIndex; } 156 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; } 157 DbgVariable *getAbstractVariable() const { return AbstractVar; } 158 void setDIE(DIE *D) { TheDIE = D; } 159 DIE *getDIE() const { return TheDIE; } 160}; 161 162//===----------------------------------------------------------------------===// 163/// DbgScope - This class is used to track scope information. 164/// 165class DbgScope { 166 DbgScope *Parent; // Parent to this scope. 167 DIDescriptor Desc; // Debug info descriptor for scope. 168 WeakVH InlinedAtLocation; // Location at which scope is inlined. 169 bool AbstractScope; // Abstract Scope 170 unsigned StartLabelID; // Label ID of the beginning of scope. 171 unsigned EndLabelID; // Label ID of the end of scope. 172 const MachineInstr *LastInsn; // Last instruction of this scope. 173 const MachineInstr *FirstInsn; // First instruction of this scope. 174 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope. 175 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope. 176 177 // Private state for dump() 178 mutable unsigned IndentLevel; 179public: 180 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0) 181 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false), 182 StartLabelID(0), EndLabelID(0), 183 LastInsn(0), FirstInsn(0), IndentLevel(0) {} 184 virtual ~DbgScope(); 185 186 // Accessors. 187 DbgScope *getParent() const { return Parent; } 188 void setParent(DbgScope *P) { Parent = P; } 189 DIDescriptor getDesc() const { return Desc; } 190 MDNode *getInlinedAt() const { 191 return dyn_cast_or_null<MDNode>(InlinedAtLocation); 192 } 193 MDNode *getScopeNode() const { return Desc.getNode(); } 194 unsigned getStartLabelID() const { return StartLabelID; } 195 unsigned getEndLabelID() const { return EndLabelID; } 196 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; } 197 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; } 198 void setStartLabelID(unsigned S) { StartLabelID = S; } 199 void setEndLabelID(unsigned E) { EndLabelID = E; } 200 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; } 201 const MachineInstr *getLastInsn() { return LastInsn; } 202 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; } 203 void setAbstractScope() { AbstractScope = true; } 204 bool isAbstractScope() const { return AbstractScope; } 205 const MachineInstr *getFirstInsn() { return FirstInsn; } 206 207 /// addScope - Add a scope to the scope. 208 /// 209 void addScope(DbgScope *S) { Scopes.push_back(S); } 210 211 /// addVariable - Add a variable to the scope. 212 /// 213 void addVariable(DbgVariable *V) { Variables.push_back(V); } 214 215 void fixInstructionMarkers() { 216 assert (getFirstInsn() && "First instruction is missing!"); 217 if (getLastInsn()) 218 return; 219 220 // If a scope does not have an instruction to mark an end then use 221 // the end of last child scope. 222 SmallVector<DbgScope *, 4> &Scopes = getScopes(); 223 assert (!Scopes.empty() && "Inner most scope does not have last insn!"); 224 DbgScope *L = Scopes.back(); 225 if (!L->getLastInsn()) 226 L->fixInstructionMarkers(); 227 setLastInsn(L->getLastInsn()); 228 } 229 230#ifndef NDEBUG 231 void dump() const; 232#endif 233}; 234 235#ifndef NDEBUG 236void DbgScope::dump() const { 237 raw_ostream &err = errs(); 238 err.indent(IndentLevel); 239 MDNode *N = Desc.getNode(); 240 N->dump(); 241 err << " [" << StartLabelID << ", " << EndLabelID << "]\n"; 242 if (AbstractScope) 243 err << "Abstract Scope\n"; 244 245 IndentLevel += 2; 246 if (!Scopes.empty()) 247 err << "Children ...\n"; 248 for (unsigned i = 0, e = Scopes.size(); i != e; ++i) 249 if (Scopes[i] != this) 250 Scopes[i]->dump(); 251 252 IndentLevel -= 2; 253} 254#endif 255 256DbgScope::~DbgScope() { 257 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) 258 delete Scopes[i]; 259 for (unsigned j = 0, M = Variables.size(); j < M; ++j) 260 delete Variables[j]; 261} 262 263} // end llvm namespace 264 265DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T) 266 : Dwarf(OS, A, T, "dbg"), ModuleCU(0), 267 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(), 268 DIEValues(), StringPool(), 269 SectionSourceLines(), didInitial(false), shouldEmit(false), 270 CurrentFnDbgScope(0), DebugTimer(0) { 271 if (TimePassesIsEnabled) 272 DebugTimer = new Timer("Dwarf Debug Writer", 273 getDwarfTimerGroup()); 274} 275DwarfDebug::~DwarfDebug() { 276 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j) 277 delete DIEValues[j]; 278 279 delete DebugTimer; 280} 281 282/// assignAbbrevNumber - Define a unique number for the abbreviation. 283/// 284void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) { 285 // Profile the node so that we can make it unique. 286 FoldingSetNodeID ID; 287 Abbrev.Profile(ID); 288 289 // Check the set for priors. 290 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev); 291 292 // If it's newly added. 293 if (InSet == &Abbrev) { 294 // Add to abbreviation list. 295 Abbreviations.push_back(&Abbrev); 296 297 // Assign the vector position + 1 as its number. 298 Abbrev.setNumber(Abbreviations.size()); 299 } else { 300 // Assign existing abbreviation number. 301 Abbrev.setNumber(InSet->getNumber()); 302 } 303} 304 305/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug 306/// information entry. 307DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) { 308 DIEEntry *Value = new DIEEntry(Entry); 309 DIEValues.push_back(Value); 310 return Value; 311} 312 313/// addUInt - Add an unsigned integer attribute data and value. 314/// 315void DwarfDebug::addUInt(DIE *Die, unsigned Attribute, 316 unsigned Form, uint64_t Integer) { 317 if (!Form) Form = DIEInteger::BestForm(false, Integer); 318 DIEValue *Value = new DIEInteger(Integer); 319 DIEValues.push_back(Value); 320 Die->addValue(Attribute, Form, Value); 321} 322 323/// addSInt - Add an signed integer attribute data and value. 324/// 325void DwarfDebug::addSInt(DIE *Die, unsigned Attribute, 326 unsigned Form, int64_t Integer) { 327 if (!Form) Form = DIEInteger::BestForm(true, Integer); 328 DIEValue *Value = new DIEInteger(Integer); 329 DIEValues.push_back(Value); 330 Die->addValue(Attribute, Form, Value); 331} 332 333/// addString - Add a string attribute data and value. DIEString only 334/// keeps string reference. 335void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form, 336 const StringRef String) { 337 DIEValue *Value = new DIEString(String); 338 DIEValues.push_back(Value); 339 Die->addValue(Attribute, Form, Value); 340} 341 342/// addLabel - Add a Dwarf label attribute data and value. 343/// 344void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form, 345 const DWLabel &Label) { 346 DIEValue *Value = new DIEDwarfLabel(Label); 347 DIEValues.push_back(Value); 348 Die->addValue(Attribute, Form, Value); 349} 350 351/// addObjectLabel - Add an non-Dwarf label attribute data and value. 352/// 353void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form, 354 const std::string &Label) { 355 DIEValue *Value = new DIEObjectLabel(Label); 356 DIEValues.push_back(Value); 357 Die->addValue(Attribute, Form, Value); 358} 359 360/// addSectionOffset - Add a section offset label attribute data and value. 361/// 362void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form, 363 const DWLabel &Label, const DWLabel &Section, 364 bool isEH, bool useSet) { 365 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet); 366 DIEValues.push_back(Value); 367 Die->addValue(Attribute, Form, Value); 368} 369 370/// addDelta - Add a label delta attribute data and value. 371/// 372void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form, 373 const DWLabel &Hi, const DWLabel &Lo) { 374 DIEValue *Value = new DIEDelta(Hi, Lo); 375 DIEValues.push_back(Value); 376 Die->addValue(Attribute, Form, Value); 377} 378 379/// addBlock - Add block data. 380/// 381void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form, 382 DIEBlock *Block) { 383 Block->ComputeSize(TD); 384 DIEValues.push_back(Block); 385 Die->addValue(Attribute, Block->BestForm(), Block); 386} 387 388/// addSourceLine - Add location information to specified debug information 389/// entry. 390void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) { 391 // If there is no compile unit specified, don't add a line #. 392 if (V->getCompileUnit().isNull()) 393 return; 394 395 unsigned Line = V->getLineNumber(); 396 unsigned FileID = findCompileUnit(V->getCompileUnit()).getID(); 397 assert(FileID && "Invalid file id"); 398 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 399 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 400} 401 402/// addSourceLine - Add location information to specified debug information 403/// entry. 404void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) { 405 // If there is no compile unit specified, don't add a line #. 406 if (G->getCompileUnit().isNull()) 407 return; 408 409 unsigned Line = G->getLineNumber(); 410 unsigned FileID = findCompileUnit(G->getCompileUnit()).getID(); 411 assert(FileID && "Invalid file id"); 412 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 413 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 414} 415 416/// addSourceLine - Add location information to specified debug information 417/// entry. 418void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) { 419 // If there is no compile unit specified, don't add a line #. 420 if (SP->getCompileUnit().isNull()) 421 return; 422 // If the line number is 0, don't add it. 423 if (SP->getLineNumber() == 0) 424 return; 425 426 427 unsigned Line = SP->getLineNumber(); 428 unsigned FileID = findCompileUnit(SP->getCompileUnit()).getID(); 429 assert(FileID && "Invalid file id"); 430 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 431 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 432} 433 434/// addSourceLine - Add location information to specified debug information 435/// entry. 436void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) { 437 // If there is no compile unit specified, don't add a line #. 438 DICompileUnit CU = Ty->getCompileUnit(); 439 if (CU.isNull()) 440 return; 441 442 unsigned Line = Ty->getLineNumber(); 443 unsigned FileID = findCompileUnit(CU).getID(); 444 assert(FileID && "Invalid file id"); 445 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 446 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 447} 448 449/* Byref variables, in Blocks, are declared by the programmer as 450 "SomeType VarName;", but the compiler creates a 451 __Block_byref_x_VarName struct, and gives the variable VarName 452 either the struct, or a pointer to the struct, as its type. This 453 is necessary for various behind-the-scenes things the compiler 454 needs to do with by-reference variables in blocks. 455 456 However, as far as the original *programmer* is concerned, the 457 variable should still have type 'SomeType', as originally declared. 458 459 The following function dives into the __Block_byref_x_VarName 460 struct to find the original type of the variable. This will be 461 passed back to the code generating the type for the Debug 462 Information Entry for the variable 'VarName'. 'VarName' will then 463 have the original type 'SomeType' in its debug information. 464 465 The original type 'SomeType' will be the type of the field named 466 'VarName' inside the __Block_byref_x_VarName struct. 467 468 NOTE: In order for this to not completely fail on the debugger 469 side, the Debug Information Entry for the variable VarName needs to 470 have a DW_AT_location that tells the debugger how to unwind through 471 the pointers and __Block_byref_x_VarName struct to find the actual 472 value of the variable. The function addBlockByrefType does this. */ 473 474/// Find the type the programmer originally declared the variable to be 475/// and return that type. 476/// 477DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) { 478 479 DIType subType = Ty; 480 unsigned tag = Ty.getTag(); 481 482 if (tag == dwarf::DW_TAG_pointer_type) { 483 DIDerivedType DTy = DIDerivedType(Ty.getNode()); 484 subType = DTy.getTypeDerivedFrom(); 485 } 486 487 DICompositeType blockStruct = DICompositeType(subType.getNode()); 488 489 DIArray Elements = blockStruct.getTypeArray(); 490 491 if (Elements.isNull()) 492 return Ty; 493 494 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 495 DIDescriptor Element = Elements.getElement(i); 496 DIDerivedType DT = DIDerivedType(Element.getNode()); 497 if (Name == DT.getName()) 498 return (DT.getTypeDerivedFrom()); 499 } 500 501 return Ty; 502} 503 504/// addComplexAddress - Start with the address based on the location provided, 505/// and generate the DWARF information necessary to find the actual variable 506/// given the extra address information encoded in the DIVariable, starting from 507/// the starting location. Add the DWARF information to the die. 508/// 509void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die, 510 unsigned Attribute, 511 const MachineLocation &Location) { 512 const DIVariable &VD = DV->getVariable(); 513 DIType Ty = VD.getType(); 514 515 // Decode the original location, and use that as the start of the byref 516 // variable's location. 517 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false); 518 DIEBlock *Block = new DIEBlock(); 519 520 if (Location.isReg()) { 521 if (Reg < 32) { 522 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg); 523 } else { 524 Reg = Reg - dwarf::DW_OP_reg0; 525 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); 526 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); 527 } 528 } else { 529 if (Reg < 32) 530 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); 531 else { 532 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); 533 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); 534 } 535 536 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset()); 537 } 538 539 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) { 540 uint64_t Element = VD.getAddrElement(i); 541 542 if (Element == DIFactory::OpPlus) { 543 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 544 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i)); 545 } else if (Element == DIFactory::OpDeref) { 546 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 547 } else llvm_unreachable("unknown DIFactory Opcode"); 548 } 549 550 // Now attach the location information to the DIE. 551 addBlock(Die, Attribute, 0, Block); 552} 553 554/* Byref variables, in Blocks, are declared by the programmer as "SomeType 555 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and 556 gives the variable VarName either the struct, or a pointer to the struct, as 557 its type. This is necessary for various behind-the-scenes things the 558 compiler needs to do with by-reference variables in Blocks. 559 560 However, as far as the original *programmer* is concerned, the variable 561 should still have type 'SomeType', as originally declared. 562 563 The function getBlockByrefType dives into the __Block_byref_x_VarName 564 struct to find the original type of the variable, which is then assigned to 565 the variable's Debug Information Entry as its real type. So far, so good. 566 However now the debugger will expect the variable VarName to have the type 567 SomeType. So we need the location attribute for the variable to be an 568 expression that explains to the debugger how to navigate through the 569 pointers and struct to find the actual variable of type SomeType. 570 571 The following function does just that. We start by getting 572 the "normal" location for the variable. This will be the location 573 of either the struct __Block_byref_x_VarName or the pointer to the 574 struct __Block_byref_x_VarName. 575 576 The struct will look something like: 577 578 struct __Block_byref_x_VarName { 579 ... <various fields> 580 struct __Block_byref_x_VarName *forwarding; 581 ... <various other fields> 582 SomeType VarName; 583 ... <maybe more fields> 584 }; 585 586 If we are given the struct directly (as our starting point) we 587 need to tell the debugger to: 588 589 1). Add the offset of the forwarding field. 590 591 2). Follow that pointer to get the the real __Block_byref_x_VarName 592 struct to use (the real one may have been copied onto the heap). 593 594 3). Add the offset for the field VarName, to find the actual variable. 595 596 If we started with a pointer to the struct, then we need to 597 dereference that pointer first, before the other steps. 598 Translating this into DWARF ops, we will need to append the following 599 to the current location description for the variable: 600 601 DW_OP_deref -- optional, if we start with a pointer 602 DW_OP_plus_uconst <forward_fld_offset> 603 DW_OP_deref 604 DW_OP_plus_uconst <varName_fld_offset> 605 606 That is what this function does. */ 607 608/// addBlockByrefAddress - Start with the address based on the location 609/// provided, and generate the DWARF information necessary to find the 610/// actual Block variable (navigating the Block struct) based on the 611/// starting location. Add the DWARF information to the die. For 612/// more information, read large comment just above here. 613/// 614void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die, 615 unsigned Attribute, 616 const MachineLocation &Location) { 617 const DIVariable &VD = DV->getVariable(); 618 DIType Ty = VD.getType(); 619 DIType TmpTy = Ty; 620 unsigned Tag = Ty.getTag(); 621 bool isPointer = false; 622 623 StringRef varName = VD.getName(); 624 625 if (Tag == dwarf::DW_TAG_pointer_type) { 626 DIDerivedType DTy = DIDerivedType(Ty.getNode()); 627 TmpTy = DTy.getTypeDerivedFrom(); 628 isPointer = true; 629 } 630 631 DICompositeType blockStruct = DICompositeType(TmpTy.getNode()); 632 633 // Find the __forwarding field and the variable field in the __Block_byref 634 // struct. 635 DIArray Fields = blockStruct.getTypeArray(); 636 DIDescriptor varField = DIDescriptor(); 637 DIDescriptor forwardingField = DIDescriptor(); 638 639 640 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { 641 DIDescriptor Element = Fields.getElement(i); 642 DIDerivedType DT = DIDerivedType(Element.getNode()); 643 StringRef fieldName = DT.getName(); 644 if (fieldName == "__forwarding") 645 forwardingField = Element; 646 else if (fieldName == varName) 647 varField = Element; 648 } 649 650 assert(!varField.isNull() && "Can't find byref variable in Block struct"); 651 assert(!forwardingField.isNull() 652 && "Can't find forwarding field in Block struct"); 653 654 // Get the offsets for the forwarding field and the variable field. 655 unsigned int forwardingFieldOffset = 656 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3; 657 unsigned int varFieldOffset = 658 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3; 659 660 // Decode the original location, and use that as the start of the byref 661 // variable's location. 662 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false); 663 DIEBlock *Block = new DIEBlock(); 664 665 if (Location.isReg()) { 666 if (Reg < 32) 667 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg); 668 else { 669 Reg = Reg - dwarf::DW_OP_reg0; 670 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); 671 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); 672 } 673 } else { 674 if (Reg < 32) 675 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); 676 else { 677 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); 678 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); 679 } 680 681 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset()); 682 } 683 684 // If we started with a pointer to the __Block_byref... struct, then 685 // the first thing we need to do is dereference the pointer (DW_OP_deref). 686 if (isPointer) 687 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 688 689 // Next add the offset for the '__forwarding' field: 690 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in 691 // adding the offset if it's 0. 692 if (forwardingFieldOffset > 0) { 693 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 694 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset); 695 } 696 697 // Now dereference the __forwarding field to get to the real __Block_byref 698 // struct: DW_OP_deref. 699 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 700 701 // Now that we've got the real __Block_byref... struct, add the offset 702 // for the variable's field to get to the location of the actual variable: 703 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. 704 if (varFieldOffset > 0) { 705 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 706 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset); 707 } 708 709 // Now attach the location information to the DIE. 710 addBlock(Die, Attribute, 0, Block); 711} 712 713/// addAddress - Add an address attribute to a die based on the location 714/// provided. 715void DwarfDebug::addAddress(DIE *Die, unsigned Attribute, 716 const MachineLocation &Location) { 717 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false); 718 DIEBlock *Block = new DIEBlock(); 719 720 if (Location.isReg()) { 721 if (Reg < 32) { 722 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg); 723 } else { 724 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); 725 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); 726 } 727 } else { 728 if (Reg < 32) { 729 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); 730 } else { 731 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); 732 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); 733 } 734 735 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset()); 736 } 737 738 addBlock(Die, Attribute, 0, Block); 739} 740 741/// addType - Add a new type attribute to the specified entity. 742void DwarfDebug::addType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) { 743 if (Ty.isNull()) 744 return; 745 746 // Check for pre-existence. 747 DIEEntry *Entry = DW_Unit->getDIEEntry(Ty.getNode()); 748 749 // If it exists then use the existing value. 750 if (Entry) { 751 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry); 752 return; 753 } 754 755 // Set up proxy. 756 Entry = createDIEEntry(); 757 DW_Unit->insertDIEEntry(Ty.getNode(), Entry); 758 759 // Construct type. 760 DIE *Buffer = new DIE(dwarf::DW_TAG_base_type); 761 if (Ty.isBasicType()) 762 constructTypeDIE(DW_Unit, *Buffer, DIBasicType(Ty.getNode())); 763 else if (Ty.isCompositeType()) 764 constructTypeDIE(DW_Unit, *Buffer, DICompositeType(Ty.getNode())); 765 else { 766 assert(Ty.isDerivedType() && "Unknown kind of DIType"); 767 constructTypeDIE(DW_Unit, *Buffer, DIDerivedType(Ty.getNode())); 768 } 769 770 // Add debug information entry to entity and appropriate context. 771 DIE *Die = NULL; 772 DIDescriptor Context = Ty.getContext(); 773 if (!Context.isNull()) 774 Die = DW_Unit->getDIE(Context.getNode()); 775 776 if (Die) 777 Die->addChild(Buffer); 778 else 779 DW_Unit->addDie(Buffer); 780 Entry->setEntry(Buffer); 781 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry); 782} 783 784/// constructTypeDIE - Construct basic type die from DIBasicType. 785void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, 786 DIBasicType BTy) { 787 // Get core information. 788 StringRef Name = BTy.getName(); 789 Buffer.setTag(dwarf::DW_TAG_base_type); 790 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 791 BTy.getEncoding()); 792 793 // Add name if not anonymous or intermediate type. 794 if (!Name.empty()) 795 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 796 uint64_t Size = BTy.getSizeInBits() >> 3; 797 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 798} 799 800/// constructTypeDIE - Construct derived type die from DIDerivedType. 801void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, 802 DIDerivedType DTy) { 803 // Get core information. 804 StringRef Name = DTy.getName(); 805 uint64_t Size = DTy.getSizeInBits() >> 3; 806 unsigned Tag = DTy.getTag(); 807 808 // FIXME - Workaround for templates. 809 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type; 810 811 Buffer.setTag(Tag); 812 813 // Map to main type, void will not have a type. 814 DIType FromTy = DTy.getTypeDerivedFrom(); 815 addType(DW_Unit, &Buffer, FromTy); 816 817 // Add name if not anonymous or intermediate type. 818 if (!Name.empty()) 819 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 820 821 // Add size if non-zero (derived types might be zero-sized.) 822 if (Size) 823 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 824 825 // Add source line info if available and TyDesc is not a forward declaration. 826 if (!DTy.isForwardDecl()) 827 addSourceLine(&Buffer, &DTy); 828} 829 830/// constructTypeDIE - Construct type DIE from DICompositeType. 831void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, 832 DICompositeType CTy) { 833 // Get core information. 834 StringRef Name = CTy.getName(); 835 836 uint64_t Size = CTy.getSizeInBits() >> 3; 837 unsigned Tag = CTy.getTag(); 838 Buffer.setTag(Tag); 839 840 switch (Tag) { 841 case dwarf::DW_TAG_vector_type: 842 case dwarf::DW_TAG_array_type: 843 constructArrayTypeDIE(DW_Unit, Buffer, &CTy); 844 break; 845 case dwarf::DW_TAG_enumeration_type: { 846 DIArray Elements = CTy.getTypeArray(); 847 848 // Add enumerators to enumeration type. 849 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 850 DIE *ElemDie = NULL; 851 DIEnumerator Enum(Elements.getElement(i).getNode()); 852 if (!Enum.isNull()) { 853 ElemDie = constructEnumTypeDIE(DW_Unit, &Enum); 854 Buffer.addChild(ElemDie); 855 } 856 } 857 } 858 break; 859 case dwarf::DW_TAG_subroutine_type: { 860 // Add return type. 861 DIArray Elements = CTy.getTypeArray(); 862 DIDescriptor RTy = Elements.getElement(0); 863 addType(DW_Unit, &Buffer, DIType(RTy.getNode())); 864 865 // Add prototype flag. 866 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1); 867 868 // Add arguments. 869 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) { 870 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); 871 DIDescriptor Ty = Elements.getElement(i); 872 addType(DW_Unit, Arg, DIType(Ty.getNode())); 873 Buffer.addChild(Arg); 874 } 875 } 876 break; 877 case dwarf::DW_TAG_structure_type: 878 case dwarf::DW_TAG_union_type: 879 case dwarf::DW_TAG_class_type: { 880 // Add elements to structure type. 881 DIArray Elements = CTy.getTypeArray(); 882 883 // A forward struct declared type may not have elements available. 884 if (Elements.isNull()) 885 break; 886 887 // Add elements to structure type. 888 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 889 DIDescriptor Element = Elements.getElement(i); 890 if (Element.isNull()) 891 continue; 892 DIE *ElemDie = NULL; 893 if (Element.getTag() == dwarf::DW_TAG_subprogram) 894 ElemDie = createMemberSubprogramDIE(DW_Unit, 895 DISubprogram(Element.getNode())); 896 else 897 ElemDie = createMemberDIE(DW_Unit, 898 DIDerivedType(Element.getNode())); 899 Buffer.addChild(ElemDie); 900 } 901 902 if (CTy.isAppleBlockExtension()) 903 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1); 904 905 unsigned RLang = CTy.getRunTimeLang(); 906 if (RLang) 907 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, 908 dwarf::DW_FORM_data1, RLang); 909 break; 910 } 911 default: 912 break; 913 } 914 915 // Add name if not anonymous or intermediate type. 916 if (!Name.empty()) 917 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 918 919 if (Tag == dwarf::DW_TAG_enumeration_type || 920 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) { 921 // Add size if non-zero (derived types might be zero-sized.) 922 if (Size) 923 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 924 else { 925 // Add zero size if it is not a forward declaration. 926 if (CTy.isForwardDecl()) 927 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); 928 else 929 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0); 930 } 931 932 // Add source line info if available. 933 if (!CTy.isForwardDecl()) 934 addSourceLine(&Buffer, &CTy); 935 } 936} 937 938/// constructSubrangeDIE - Construct subrange DIE from DISubrange. 939void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){ 940 int64_t L = SR.getLo(); 941 int64_t H = SR.getHi(); 942 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type); 943 944 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy); 945 if (L) 946 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L); 947 if (H) 948 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H); 949 950 Buffer.addChild(DW_Subrange); 951} 952 953/// constructArrayTypeDIE - Construct array type DIE from DICompositeType. 954void DwarfDebug::constructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, 955 DICompositeType *CTy) { 956 Buffer.setTag(dwarf::DW_TAG_array_type); 957 if (CTy->getTag() == dwarf::DW_TAG_vector_type) 958 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1); 959 960 // Emit derived type. 961 addType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom()); 962 DIArray Elements = CTy->getTypeArray(); 963 964 // Get an anonymous type for index type. 965 DIE *IdxTy = DW_Unit->getIndexTyDie(); 966 if (!IdxTy) { 967 // Construct an anonymous type for index type. 968 IdxTy = new DIE(dwarf::DW_TAG_base_type); 969 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t)); 970 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 971 dwarf::DW_ATE_signed); 972 DW_Unit->addDie(IdxTy); 973 DW_Unit->setIndexTyDie(IdxTy); 974 } 975 976 // Add subranges to array type. 977 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 978 DIDescriptor Element = Elements.getElement(i); 979 if (Element.getTag() == dwarf::DW_TAG_subrange_type) 980 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy); 981 } 982} 983 984/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator. 985DIE *DwarfDebug::constructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) { 986 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator); 987 StringRef Name = ETy->getName(); 988 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 989 int64_t Value = ETy->getEnumValue(); 990 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value); 991 return Enumerator; 992} 993 994/// createGlobalVariableDIE - Create new DIE using GV. 995DIE *DwarfDebug::createGlobalVariableDIE(CompileUnit *DW_Unit, 996 const DIGlobalVariable &GV) { 997 // If the global variable was optmized out then no need to create debug info 998 // entry. 999 if (!GV.getGlobal()) return NULL; 1000 if (GV.getDisplayName().empty()) return NULL; 1001 1002 DIE *GVDie = new DIE(dwarf::DW_TAG_variable); 1003 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, 1004 GV.getDisplayName()); 1005 1006 StringRef LinkageName = GV.getLinkageName(); 1007 if (!LinkageName.empty()) { 1008 // Skip special LLVM prefix that is used to inform the asm printer to not 1009 // emit usual symbol prefix before the symbol name. This happens for 1010 // Objective-C symbol names and symbol whose name is replaced using GCC's 1011 // __asm__ attribute. 1012 if (LinkageName[0] == 1) 1013 LinkageName = LinkageName.substr(1); 1014 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string, 1015 LinkageName); 1016 } 1017 addType(DW_Unit, GVDie, GV.getType()); 1018 if (!GV.isLocalToUnit()) 1019 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1); 1020 addSourceLine(GVDie, &GV); 1021 1022 // Add address. 1023 DIEBlock *Block = new DIEBlock(); 1024 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); 1025 addObjectLabel(Block, 0, dwarf::DW_FORM_udata, 1026 Asm->Mang->getMangledName(GV.getGlobal())); 1027 addBlock(GVDie, dwarf::DW_AT_location, 0, Block); 1028 1029 return GVDie; 1030} 1031 1032/// createMemberDIE - Create new member DIE. 1033DIE *DwarfDebug::createMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){ 1034 DIE *MemberDie = new DIE(DT.getTag()); 1035 StringRef Name = DT.getName(); 1036 if (!Name.empty()) 1037 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 1038 1039 addType(DW_Unit, MemberDie, DT.getTypeDerivedFrom()); 1040 1041 addSourceLine(MemberDie, &DT); 1042 1043 DIEBlock *MemLocationDie = new DIEBlock(); 1044 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 1045 1046 uint64_t Size = DT.getSizeInBits(); 1047 uint64_t FieldSize = DT.getOriginalTypeSize(); 1048 1049 if (Size != FieldSize) { 1050 // Handle bitfield. 1051 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3); 1052 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits()); 1053 1054 uint64_t Offset = DT.getOffsetInBits(); 1055 uint64_t FieldOffset = Offset; 1056 uint64_t AlignMask = ~(DT.getAlignInBits() - 1); 1057 uint64_t HiMark = (Offset + FieldSize) & AlignMask; 1058 FieldOffset = (HiMark - FieldSize); 1059 Offset -= FieldOffset; 1060 1061 // Maybe we need to work from the other end. 1062 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size); 1063 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset); 1064 1065 // Here WD_AT_data_member_location points to the anonymous 1066 // field that includes this bit field. 1067 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3); 1068 1069 } else 1070 // This is not a bitfield. 1071 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3); 1072 1073 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie); 1074 1075 if (DT.isProtected()) 1076 addUInt(MemberDie, dwarf::DW_AT_accessibility, 0, 1077 dwarf::DW_ACCESS_protected); 1078 else if (DT.isPrivate()) 1079 addUInt(MemberDie, dwarf::DW_AT_accessibility, 0, 1080 dwarf::DW_ACCESS_private); 1081 1082 return MemberDie; 1083} 1084 1085/// createRawSubprogramDIE - Create new partially incomplete DIE. This is 1086/// a helper routine used by createMemberSubprogramDIE and 1087/// createSubprogramDIE. 1088DIE *DwarfDebug::createRawSubprogramDIE(CompileUnit *DW_Unit, 1089 const DISubprogram &SP) { 1090 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram); 1091 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName()); 1092 1093 StringRef LinkageName = SP.getLinkageName(); 1094 if (!LinkageName.empty()) { 1095 // Skip special LLVM prefix that is used to inform the asm printer to not 1096 // emit usual symbol prefix before the symbol name. This happens for 1097 // Objective-C symbol names and symbol whose name is replaced using GCC's 1098 // __asm__ attribute. 1099 if (LinkageName[0] == 1) 1100 LinkageName = LinkageName.substr(1); 1101 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string, 1102 LinkageName); 1103 } 1104 addSourceLine(SPDie, &SP); 1105 1106 // Add prototyped tag, if C or ObjC. 1107 unsigned Lang = SP.getCompileUnit().getLanguage(); 1108 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 || 1109 Lang == dwarf::DW_LANG_ObjC) 1110 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1); 1111 1112 // Add Return Type. 1113 DICompositeType SPTy = SP.getType(); 1114 DIArray Args = SPTy.getTypeArray(); 1115 unsigned SPTag = SPTy.getTag(); 1116 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type) 1117 addType(DW_Unit, SPDie, SPTy); 1118 else 1119 addType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode())); 1120 1121 return SPDie; 1122} 1123 1124/// createMemberSubprogramDIE - Create new member DIE using SP. This routine 1125/// always returns a die with DW_AT_declaration attribute. 1126DIE *DwarfDebug::createMemberSubprogramDIE(CompileUnit *DW_Unit, 1127 const DISubprogram &SP) { 1128 DIE *SPDie = ModuleCU->getDIE(SP.getNode()); 1129 if (!SPDie) 1130 SPDie = createSubprogramDIE(DW_Unit, SP); 1131 1132 // If SPDie has DW_AT_declaration then reuse it. 1133 if (!SP.isDefinition()) 1134 return SPDie; 1135 1136 // Otherwise create new DIE for the declaration. First push definition 1137 // DIE at the top level. 1138 if (TopLevelDIEs.insert(SPDie)) 1139 TopLevelDIEsVector.push_back(SPDie); 1140 1141 SPDie = createRawSubprogramDIE(DW_Unit, SP); 1142 1143 // Add arguments. 1144 DICompositeType SPTy = SP.getType(); 1145 DIArray Args = SPTy.getTypeArray(); 1146 unsigned SPTag = SPTy.getTag(); 1147 if (SPTag == dwarf::DW_TAG_subroutine_type) 1148 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { 1149 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); 1150 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode())); 1151 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ?? 1152 SPDie->addChild(Arg); 1153 } 1154 1155 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); 1156 return SPDie; 1157} 1158 1159/// createSubprogramDIE - Create new DIE using SP. 1160DIE *DwarfDebug::createSubprogramDIE(CompileUnit *DW_Unit, 1161 const DISubprogram &SP) { 1162 DIE *SPDie = ModuleCU->getDIE(SP.getNode()); 1163 if (SPDie) 1164 return SPDie; 1165 1166 SPDie = createRawSubprogramDIE(DW_Unit, SP); 1167 1168 if (!SP.isDefinition()) { 1169 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); 1170 1171 // Add arguments. Do not add arguments for subprogram definition. They will 1172 // be handled while processing variables. 1173 DICompositeType SPTy = SP.getType(); 1174 DIArray Args = SPTy.getTypeArray(); 1175 unsigned SPTag = SPTy.getTag(); 1176 1177 if (SPTag == dwarf::DW_TAG_subroutine_type) 1178 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { 1179 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); 1180 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode())); 1181 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ?? 1182 SPDie->addChild(Arg); 1183 } 1184 } 1185 1186 // DW_TAG_inlined_subroutine may refer to this DIE. 1187 DW_Unit->insertDIE(SP.getNode(), SPDie); 1188 return SPDie; 1189} 1190 1191/// findCompileUnit - Get the compile unit for the given descriptor. 1192/// 1193CompileUnit &DwarfDebug::findCompileUnit(DICompileUnit Unit) const { 1194 DenseMap<Value *, CompileUnit *>::const_iterator I = 1195 CompileUnitMap.find(Unit.getNode()); 1196 assert(I != CompileUnitMap.end() && "Missing compile unit."); 1197 return *I->second; 1198} 1199 1200/// createDbgScopeVariable - Create a new scope variable. 1201/// 1202DIE *DwarfDebug::createDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) { 1203 // Get the descriptor. 1204 const DIVariable &VD = DV->getVariable(); 1205 StringRef Name = VD.getName(); 1206 if (Name.empty()) 1207 return NULL; 1208 1209 // Translate tag to proper Dwarf tag. The result variable is dropped for 1210 // now. 1211 unsigned Tag; 1212 switch (VD.getTag()) { 1213 case dwarf::DW_TAG_return_variable: 1214 return NULL; 1215 case dwarf::DW_TAG_arg_variable: 1216 Tag = dwarf::DW_TAG_formal_parameter; 1217 break; 1218 case dwarf::DW_TAG_auto_variable: // fall thru 1219 default: 1220 Tag = dwarf::DW_TAG_variable; 1221 break; 1222 } 1223 1224 // Define variable debug information entry. 1225 DIE *VariableDie = new DIE(Tag); 1226 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 1227 1228 // Add source line info if available. 1229 addSourceLine(VariableDie, &VD); 1230 1231 // Add variable type. 1232 // FIXME: isBlockByrefVariable should be reformulated in terms of complex 1233 // addresses instead. 1234 if (VD.isBlockByrefVariable()) 1235 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name)); 1236 else 1237 addType(Unit, VariableDie, VD.getType()); 1238 1239 // Add variable address. 1240 // Variables for abstract instances of inlined functions don't get a 1241 // location. 1242 MachineLocation Location; 1243 unsigned FrameReg; 1244 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg); 1245 Location.set(FrameReg, Offset); 1246 1247 1248 if (VD.hasComplexAddress()) 1249 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location); 1250 else if (VD.isBlockByrefVariable()) 1251 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location); 1252 else 1253 addAddress(VariableDie, dwarf::DW_AT_location, Location); 1254 1255 return VariableDie; 1256} 1257 1258/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction. 1259/// Initialize scope and update scope hierarchy. 1260DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI, 1261 MDNode *InlinedAt) { 1262 assert (N && "Invalid Scope encoding!"); 1263 assert (MI && "Missing machine instruction!"); 1264 bool GetConcreteScope = (MI && InlinedAt); 1265 1266 DbgScope *NScope = NULL; 1267 1268 if (InlinedAt) 1269 NScope = DbgScopeMap.lookup(InlinedAt); 1270 else 1271 NScope = DbgScopeMap.lookup(N); 1272 assert (NScope && "Unable to find working scope!"); 1273 1274 if (NScope->getFirstInsn()) 1275 return NScope; 1276 1277 DbgScope *Parent = NULL; 1278 if (GetConcreteScope) { 1279 DILocation IL(InlinedAt); 1280 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI, 1281 IL.getOrigLocation().getNode()); 1282 assert (Parent && "Unable to find Parent scope!"); 1283 NScope->setParent(Parent); 1284 Parent->addScope(NScope); 1285 } else if (DIDescriptor(N).isLexicalBlock()) { 1286 DILexicalBlock DB(N); 1287 if (!DB.getContext().isNull()) { 1288 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt); 1289 NScope->setParent(Parent); 1290 Parent->addScope(NScope); 1291 } 1292 } 1293 1294 NScope->setFirstInsn(MI); 1295 1296 if (!Parent && !InlinedAt) { 1297 StringRef SPName = DISubprogram(N).getLinkageName(); 1298 if (SPName == MF->getFunction()->getName()) 1299 CurrentFnDbgScope = NScope; 1300 } 1301 1302 if (GetConcreteScope) { 1303 ConcreteScopes[InlinedAt] = NScope; 1304 getOrCreateAbstractScope(N); 1305 } 1306 1307 return NScope; 1308} 1309 1310DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) { 1311 assert (N && "Invalid Scope encoding!"); 1312 1313 DbgScope *AScope = AbstractScopes.lookup(N); 1314 if (AScope) 1315 return AScope; 1316 1317 DbgScope *Parent = NULL; 1318 1319 DIDescriptor Scope(N); 1320 if (Scope.isLexicalBlock()) { 1321 DILexicalBlock DB(N); 1322 DIDescriptor ParentDesc = DB.getContext(); 1323 if (!ParentDesc.isNull()) 1324 Parent = getOrCreateAbstractScope(ParentDesc.getNode()); 1325 } 1326 1327 AScope = new DbgScope(Parent, DIDescriptor(N), NULL); 1328 1329 if (Parent) 1330 Parent->addScope(AScope); 1331 AScope->setAbstractScope(); 1332 AbstractScopes[N] = AScope; 1333 if (DIDescriptor(N).isSubprogram()) 1334 AbstractScopesList.push_back(AScope); 1335 return AScope; 1336} 1337 1338/// updateSubprogramScopeDIE - Find DIE for the given subprogram and 1339/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes. 1340/// If there are global variables in this scope then create and insert 1341/// DIEs for these variables. 1342DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) { 1343 1344 DIE *SPDie = ModuleCU->getDIE(SPNode); 1345 assert (SPDie && "Unable to find subprogram DIE!"); 1346 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 1347 DWLabel("func_begin", SubprogramCount)); 1348 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, 1349 DWLabel("func_end", SubprogramCount)); 1350 MachineLocation Location(RI->getFrameRegister(*MF)); 1351 addAddress(SPDie, dwarf::DW_AT_frame_base, Location); 1352 1353 if (!DISubprogram(SPNode).isLocalToUnit()) 1354 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1); 1355 1356 // If there are global variables at this scope then add their dies. 1357 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(), 1358 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) { 1359 MDNode *N = dyn_cast_or_null<MDNode>(*SGI); 1360 if (!N) continue; 1361 DIGlobalVariable GV(N); 1362 if (GV.getContext().getNode() == SPNode) { 1363 DIE *ScopedGVDie = createGlobalVariableDIE(ModuleCU, GV); 1364 if (ScopedGVDie) 1365 SPDie->addChild(ScopedGVDie); 1366 } 1367 } 1368 1369 return SPDie; 1370} 1371 1372/// constructLexicalScope - Construct new DW_TAG_lexical_block 1373/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels. 1374DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) { 1375 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID()); 1376 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID()); 1377 1378 // Ignore empty scopes. 1379 if (StartID == EndID && StartID != 0) 1380 return NULL; 1381 1382 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block); 1383 if (Scope->isAbstractScope()) 1384 return ScopeDIE; 1385 1386 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 1387 StartID ? 1388 DWLabel("label", StartID) 1389 : DWLabel("func_begin", SubprogramCount)); 1390 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, 1391 EndID ? 1392 DWLabel("label", EndID) 1393 : DWLabel("func_end", SubprogramCount)); 1394 1395 1396 1397 return ScopeDIE; 1398} 1399 1400/// constructInlinedScopeDIE - This scope represents inlined body of 1401/// a function. Construct DIE to represent this concrete inlined copy 1402/// of the function. 1403DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) { 1404 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID()); 1405 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID()); 1406 assert (StartID && "Invalid starting label for an inlined scope!"); 1407 assert (EndID && "Invalid end label for an inlined scope!"); 1408 // Ignore empty scopes. 1409 if (StartID == EndID && StartID != 0) 1410 return NULL; 1411 1412 DIScope DS(Scope->getScopeNode()); 1413 if (DS.isNull()) 1414 return NULL; 1415 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine); 1416 1417 DISubprogram InlinedSP = getDISubprogram(DS.getNode()); 1418 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode()); 1419 assert (OriginDIE && "Unable to find Origin DIE!"); 1420 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, 1421 dwarf::DW_FORM_ref4, OriginDIE); 1422 1423 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 1424 DWLabel("label", StartID)); 1425 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, 1426 DWLabel("label", EndID)); 1427 1428 InlinedSubprogramDIEs.insert(OriginDIE); 1429 1430 // Track the start label for this inlined function. 1431 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator 1432 I = InlineInfo.find(InlinedSP.getNode()); 1433 1434 if (I == InlineInfo.end()) { 1435 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID, 1436 ScopeDIE)); 1437 InlinedSPNodes.push_back(InlinedSP.getNode()); 1438 } else 1439 I->second.push_back(std::make_pair(StartID, ScopeDIE)); 1440 1441 StringPool.insert(InlinedSP.getName()); 1442 StringPool.insert(InlinedSP.getLinkageName()); 1443 DILocation DL(Scope->getInlinedAt()); 1444 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID()); 1445 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber()); 1446 1447 return ScopeDIE; 1448} 1449 1450 1451/// constructVariableDIE - Construct a DIE for the given DbgVariable. 1452DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, 1453 DbgScope *Scope, CompileUnit *Unit) { 1454 // Get the descriptor. 1455 const DIVariable &VD = DV->getVariable(); 1456 StringRef Name = VD.getName(); 1457 if (Name.empty()) 1458 return NULL; 1459 1460 // Translate tag to proper Dwarf tag. The result variable is dropped for 1461 // now. 1462 unsigned Tag; 1463 switch (VD.getTag()) { 1464 case dwarf::DW_TAG_return_variable: 1465 return NULL; 1466 case dwarf::DW_TAG_arg_variable: 1467 Tag = dwarf::DW_TAG_formal_parameter; 1468 break; 1469 case dwarf::DW_TAG_auto_variable: // fall thru 1470 default: 1471 Tag = dwarf::DW_TAG_variable; 1472 break; 1473 } 1474 1475 // Define variable debug information entry. 1476 DIE *VariableDie = new DIE(Tag); 1477 1478 1479 DIE *AbsDIE = NULL; 1480 if (DbgVariable *AV = DV->getAbstractVariable()) 1481 AbsDIE = AV->getDIE(); 1482 1483 if (AbsDIE) { 1484 DIScope DS(Scope->getScopeNode()); 1485 DISubprogram InlinedSP = getDISubprogram(DS.getNode()); 1486 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode()); 1487 (void) OriginSPDIE; 1488 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!"); 1489 DIE *AbsDIE = DV->getAbstractVariable()->getDIE(); 1490 assert (AbsDIE && "Unable to find Origin DIE for the Variable!"); 1491 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, 1492 dwarf::DW_FORM_ref4, AbsDIE); 1493 } 1494 else { 1495 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 1496 addSourceLine(VariableDie, &VD); 1497 1498 // Add variable type. 1499 // FIXME: isBlockByrefVariable should be reformulated in terms of complex 1500 // addresses instead. 1501 if (VD.isBlockByrefVariable()) 1502 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name)); 1503 else 1504 addType(Unit, VariableDie, VD.getType()); 1505 } 1506 1507 // Add variable address. 1508 if (!Scope->isAbstractScope()) { 1509 MachineLocation Location; 1510 unsigned FrameReg; 1511 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg); 1512 Location.set(FrameReg, Offset); 1513 1514 if (VD.hasComplexAddress()) 1515 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location); 1516 else if (VD.isBlockByrefVariable()) 1517 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location); 1518 else 1519 addAddress(VariableDie, dwarf::DW_AT_location, Location); 1520 } 1521 DV->setDIE(VariableDie); 1522 return VariableDie; 1523 1524} 1525 1526void DwarfDebug::addPubTypes(DISubprogram SP) { 1527 DICompositeType SPTy = SP.getType(); 1528 unsigned SPTag = SPTy.getTag(); 1529 if (SPTag != dwarf::DW_TAG_subroutine_type) 1530 return; 1531 1532 DIArray Args = SPTy.getTypeArray(); 1533 if (Args.isNull()) 1534 return; 1535 1536 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) { 1537 DIType ATy(Args.getElement(i).getNode()); 1538 if (ATy.isNull()) 1539 continue; 1540 DICompositeType CATy = getDICompositeType(ATy); 1541 if (!CATy.isNull() && !CATy.getName().empty()) { 1542 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode())) 1543 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry()); 1544 } 1545 } 1546} 1547 1548/// constructScopeDIE - Construct a DIE for this scope. 1549DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) { 1550 if (!Scope) 1551 return NULL; 1552 DIScope DS(Scope->getScopeNode()); 1553 if (DS.isNull()) 1554 return NULL; 1555 1556 DIE *ScopeDIE = NULL; 1557 if (Scope->getInlinedAt()) 1558 ScopeDIE = constructInlinedScopeDIE(Scope); 1559 else if (DS.isSubprogram()) { 1560 if (Scope->isAbstractScope()) 1561 ScopeDIE = ModuleCU->getDIE(DS.getNode()); 1562 else 1563 ScopeDIE = updateSubprogramScopeDIE(DS.getNode()); 1564 } 1565 else { 1566 ScopeDIE = constructLexicalScopeDIE(Scope); 1567 if (!ScopeDIE) return NULL; 1568 } 1569 1570 // Add variables to scope. 1571 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables(); 1572 for (unsigned i = 0, N = Variables.size(); i < N; ++i) { 1573 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope, ModuleCU); 1574 if (VariableDIE) 1575 ScopeDIE->addChild(VariableDIE); 1576 } 1577 1578 // Add nested scopes. 1579 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes(); 1580 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) { 1581 // Define the Scope debug information entry. 1582 DIE *NestedDIE = constructScopeDIE(Scopes[j]); 1583 if (NestedDIE) 1584 ScopeDIE->addChild(NestedDIE); 1585 } 1586 1587 if (DS.isSubprogram()) 1588 addPubTypes(DISubprogram(DS.getNode())); 1589 1590 return ScopeDIE; 1591} 1592 1593/// GetOrCreateSourceID - Look up the source id with the given directory and 1594/// source file names. If none currently exists, create a new id and insert it 1595/// in the SourceIds map. This can update DirectoryNames and SourceFileNames 1596/// maps as well. 1597unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) { 1598 unsigned DId; 1599 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName); 1600 if (DI != DirectoryIdMap.end()) { 1601 DId = DI->getValue(); 1602 } else { 1603 DId = DirectoryNames.size() + 1; 1604 DirectoryIdMap[DirName] = DId; 1605 DirectoryNames.push_back(DirName); 1606 } 1607 1608 unsigned FId; 1609 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName); 1610 if (FI != SourceFileIdMap.end()) { 1611 FId = FI->getValue(); 1612 } else { 1613 FId = SourceFileNames.size() + 1; 1614 SourceFileIdMap[FileName] = FId; 1615 SourceFileNames.push_back(FileName); 1616 } 1617 1618 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI = 1619 SourceIdMap.find(std::make_pair(DId, FId)); 1620 if (SI != SourceIdMap.end()) 1621 return SI->second; 1622 1623 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0. 1624 SourceIdMap[std::make_pair(DId, FId)] = SrcId; 1625 SourceIds.push_back(std::make_pair(DId, FId)); 1626 1627 return SrcId; 1628} 1629 1630void DwarfDebug::constructCompileUnit(MDNode *N) { 1631 DICompileUnit DIUnit(N); 1632 StringRef FN = DIUnit.getFilename(); 1633 StringRef Dir = DIUnit.getDirectory(); 1634 unsigned ID = GetOrCreateSourceID(Dir, FN); 1635 1636 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit); 1637 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 1638 DWLabel("section_line", 0), DWLabel("section_line", 0), 1639 false); 1640 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string, 1641 DIUnit.getProducer()); 1642 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1, 1643 DIUnit.getLanguage()); 1644 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN); 1645 1646 if (!Dir.empty()) 1647 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir); 1648 if (DIUnit.isOptimized()) 1649 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1); 1650 1651 StringRef Flags = DIUnit.getFlags(); 1652 if (!Flags.empty()) 1653 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags); 1654 1655 unsigned RVer = DIUnit.getRunTimeVersion(); 1656 if (RVer) 1657 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, 1658 dwarf::DW_FORM_data1, RVer); 1659 1660 CompileUnit *Unit = new CompileUnit(ID, Die); 1661 if (!ModuleCU && DIUnit.isMain()) { 1662 // Use first compile unit marked as isMain as the compile unit 1663 // for this module. 1664 ModuleCU = Unit; 1665 } 1666 1667 CompileUnitMap[DIUnit.getNode()] = Unit; 1668 CompileUnits.push_back(Unit); 1669} 1670 1671void DwarfDebug::constructGlobalVariableDIE(MDNode *N) { 1672 DIGlobalVariable DI_GV(N); 1673 1674 // If debug information is malformed then ignore it. 1675 if (DI_GV.Verify() == false) 1676 return; 1677 1678 // Check for pre-existence. 1679 if (ModuleCU->getDIE(DI_GV.getNode())) 1680 return; 1681 1682 DIE *VariableDie = createGlobalVariableDIE(ModuleCU, DI_GV); 1683 1684 // Add to map. 1685 ModuleCU->insertDIE(N, VariableDie); 1686 1687 // Add to context owner. 1688 if (TopLevelDIEs.insert(VariableDie)) 1689 TopLevelDIEsVector.push_back(VariableDie); 1690 1691 // Expose as global. FIXME - need to check external flag. 1692 ModuleCU->addGlobal(DI_GV.getName(), VariableDie); 1693 1694 DIType GTy = DI_GV.getType(); 1695 if (GTy.isCompositeType() && !GTy.getName().empty()) { 1696 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode()); 1697 assert (Entry && "Missing global type!"); 1698 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry()); 1699 } 1700 return; 1701} 1702 1703void DwarfDebug::constructSubprogramDIE(MDNode *N) { 1704 DISubprogram SP(N); 1705 1706 // Check for pre-existence. 1707 if (ModuleCU->getDIE(N)) 1708 return; 1709 1710 if (!SP.isDefinition()) 1711 // This is a method declaration which will be handled while constructing 1712 // class type. 1713 return; 1714 1715 DIE *SubprogramDie = createSubprogramDIE(ModuleCU, SP); 1716 1717 // Add to map. 1718 ModuleCU->insertDIE(N, SubprogramDie); 1719 1720 // Add to context owner. 1721 if (SP.getContext().getNode() == SP.getCompileUnit().getNode()) 1722 if (TopLevelDIEs.insert(SubprogramDie)) 1723 TopLevelDIEsVector.push_back(SubprogramDie); 1724 1725 // Expose as global. 1726 ModuleCU->addGlobal(SP.getName(), SubprogramDie); 1727 1728 return; 1729} 1730 1731/// beginModule - Emit all Dwarf sections that should come prior to the 1732/// content. Create global DIEs and emit initial debug info sections. 1733/// This is inovked by the target AsmPrinter. 1734void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) { 1735 this->M = M; 1736 1737 if (TimePassesIsEnabled) 1738 DebugTimer->startTimer(); 1739 1740 if (!MAI->doesSupportDebugInformation()) 1741 return; 1742 1743 DebugInfoFinder DbgFinder; 1744 DbgFinder.processModule(*M); 1745 1746 // Create all the compile unit DIEs. 1747 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(), 1748 E = DbgFinder.compile_unit_end(); I != E; ++I) 1749 constructCompileUnit(*I); 1750 1751 if (CompileUnits.empty()) { 1752 if (TimePassesIsEnabled) 1753 DebugTimer->stopTimer(); 1754 1755 return; 1756 } 1757 1758 // If main compile unit for this module is not seen than randomly 1759 // select first compile unit. 1760 if (!ModuleCU) 1761 ModuleCU = CompileUnits[0]; 1762 1763 // Create DIEs for each of the externally visible global variables. 1764 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(), 1765 E = DbgFinder.global_variable_end(); I != E; ++I) { 1766 DIGlobalVariable GV(*I); 1767 if (GV.getContext().getNode() != GV.getCompileUnit().getNode()) 1768 ScopedGVs.push_back(*I); 1769 else 1770 constructGlobalVariableDIE(*I); 1771 } 1772 1773 // Create DIEs for each subprogram. 1774 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(), 1775 E = DbgFinder.subprogram_end(); I != E; ++I) 1776 constructSubprogramDIE(*I); 1777 1778 MMI = mmi; 1779 shouldEmit = true; 1780 MMI->setDebugInfoAvailability(true); 1781 1782 // Prime section data. 1783 SectionMap.insert(Asm->getObjFileLowering().getTextSection()); 1784 1785 // Print out .file directives to specify files for .loc directives. These are 1786 // printed out early so that they precede any .loc directives. 1787 if (MAI->hasDotLocAndDotFile()) { 1788 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) { 1789 // Remember source id starts at 1. 1790 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i); 1791 sys::Path FullPath(getSourceDirectoryName(Id.first)); 1792 bool AppendOk = 1793 FullPath.appendComponent(getSourceFileName(Id.second)); 1794 assert(AppendOk && "Could not append filename to directory!"); 1795 AppendOk = false; 1796 Asm->EmitFile(i, FullPath.str()); 1797 Asm->EOL(); 1798 } 1799 } 1800 1801 // Emit initial sections 1802 emitInitial(); 1803 1804 if (TimePassesIsEnabled) 1805 DebugTimer->stopTimer(); 1806} 1807 1808/// endModule - Emit all Dwarf sections that should come after the content. 1809/// 1810void DwarfDebug::endModule() { 1811 if (!ModuleCU) 1812 return; 1813 1814 if (TimePassesIsEnabled) 1815 DebugTimer->startTimer(); 1816 1817 // Attach DW_AT_inline attribute with inlined subprogram DIEs. 1818 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(), 1819 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) { 1820 DIE *ISP = *AI; 1821 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined); 1822 } 1823 1824 // Insert top level DIEs. 1825 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(), 1826 TE = TopLevelDIEsVector.end(); TI != TE; ++TI) 1827 ModuleCU->getCUDie()->addChild(*TI); 1828 1829 // Standard sections final addresses. 1830 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection()); 1831 EmitLabel("text_end", 0); 1832 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection()); 1833 EmitLabel("data_end", 0); 1834 1835 // End text sections. 1836 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) { 1837 Asm->OutStreamer.SwitchSection(SectionMap[i]); 1838 EmitLabel("section_end", i); 1839 } 1840 1841 // Emit common frame information. 1842 emitCommonDebugFrame(); 1843 1844 // Emit function debug frame information 1845 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(), 1846 E = DebugFrames.end(); I != E; ++I) 1847 emitFunctionDebugFrame(*I); 1848 1849 // Compute DIE offsets and sizes. 1850 computeSizeAndOffsets(); 1851 1852 // Emit all the DIEs into a debug info section 1853 emitDebugInfo(); 1854 1855 // Corresponding abbreviations into a abbrev section. 1856 emitAbbreviations(); 1857 1858 // Emit source line correspondence into a debug line section. 1859 emitDebugLines(); 1860 1861 // Emit info into a debug pubnames section. 1862 emitDebugPubNames(); 1863 1864 // Emit info into a debug pubtypes section. 1865 emitDebugPubTypes(); 1866 1867 // Emit info into a debug str section. 1868 emitDebugStr(); 1869 1870 // Emit info into a debug loc section. 1871 emitDebugLoc(); 1872 1873 // Emit info into a debug aranges section. 1874 EmitDebugARanges(); 1875 1876 // Emit info into a debug ranges section. 1877 emitDebugRanges(); 1878 1879 // Emit info into a debug macinfo section. 1880 emitDebugMacInfo(); 1881 1882 // Emit inline info. 1883 emitDebugInlineInfo(); 1884 1885 if (TimePassesIsEnabled) 1886 DebugTimer->stopTimer(); 1887} 1888 1889/// findAbstractVariable - Find abstract variable, if any, associated with Var. 1890DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var, 1891 unsigned FrameIdx, 1892 DILocation &ScopeLoc) { 1893 1894 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode()); 1895 if (AbsDbgVariable) 1896 return AbsDbgVariable; 1897 1898 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode()); 1899 if (!Scope) 1900 return NULL; 1901 1902 AbsDbgVariable = new DbgVariable(Var, FrameIdx); 1903 Scope->addVariable(AbsDbgVariable); 1904 AbstractVariables[Var.getNode()] = AbsDbgVariable; 1905 return AbsDbgVariable; 1906} 1907 1908/// collectVariableInfo - Populate DbgScope entries with variables' info. 1909void DwarfDebug::collectVariableInfo() { 1910 if (!MMI) return; 1911 1912 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo(); 1913 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(), 1914 VE = VMap.end(); VI != VE; ++VI) { 1915 MetadataBase *MB = VI->first; 1916 MDNode *Var = dyn_cast_or_null<MDNode>(MB); 1917 if (!Var) continue; 1918 DIVariable DV (Var); 1919 std::pair< unsigned, MDNode *> VP = VI->second; 1920 DILocation ScopeLoc(VP.second); 1921 1922 DbgScope *Scope = 1923 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode()); 1924 if (!Scope) 1925 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode()); 1926 // If variable scope is not found then skip this variable. 1927 if (!Scope) 1928 continue; 1929 1930 DbgVariable *RegVar = new DbgVariable(DV, VP.first); 1931 Scope->addVariable(RegVar); 1932 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, 1933 ScopeLoc)) 1934 RegVar->setAbstractVariable(AbsDbgVariable); 1935 } 1936} 1937 1938/// beginScope - Process beginning of a scope starting at Label. 1939void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) { 1940 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI); 1941 if (I == DbgScopeBeginMap.end()) 1942 return; 1943 ScopeVector &SD = I->second; 1944 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end(); 1945 SDI != SDE; ++SDI) 1946 (*SDI)->setStartLabelID(Label); 1947} 1948 1949/// endScope - Process end of a scope. 1950void DwarfDebug::endScope(const MachineInstr *MI) { 1951 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI); 1952 if (I == DbgScopeEndMap.end()) 1953 return; 1954 1955 unsigned Label = MMI->NextLabelID(); 1956 Asm->printLabel(Label); 1957 1958 SmallVector<DbgScope *, 2> &SD = I->second; 1959 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end(); 1960 SDI != SDE; ++SDI) 1961 (*SDI)->setEndLabelID(Label); 1962 return; 1963} 1964 1965/// createDbgScope - Create DbgScope for the scope. 1966void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) { 1967 1968 if (!InlinedAt) { 1969 DbgScope *WScope = DbgScopeMap.lookup(Scope); 1970 if (WScope) 1971 return; 1972 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL); 1973 DbgScopeMap.insert(std::make_pair(Scope, WScope)); 1974 if (DIDescriptor(Scope).isLexicalBlock()) 1975 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL); 1976 return; 1977 } 1978 1979 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt); 1980 if (WScope) 1981 return; 1982 1983 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt); 1984 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope)); 1985 DILocation DL(InlinedAt); 1986 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode()); 1987} 1988 1989/// extractScopeInformation - Scan machine instructions in this function 1990/// and collect DbgScopes. Return true, if atleast one scope was found. 1991bool DwarfDebug::extractScopeInformation(MachineFunction *MF) { 1992 // If scope information was extracted using .dbg intrinsics then there is not 1993 // any need to extract these information by scanning each instruction. 1994 if (!DbgScopeMap.empty()) 1995 return false; 1996 1997 // Scan each instruction and create scopes. First build working set of scopes. 1998 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); 1999 I != E; ++I) { 2000 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); 2001 II != IE; ++II) { 2002 const MachineInstr *MInsn = II; 2003 DebugLoc DL = MInsn->getDebugLoc(); 2004 if (DL.isUnknown()) continue; 2005 DebugLocTuple DLT = MF->getDebugLocTuple(DL); 2006 if (!DLT.Scope) continue; 2007 // There is no need to create another DIE for compile unit. For all 2008 // other scopes, create one DbgScope now. This will be translated 2009 // into a scope DIE at the end. 2010 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue; 2011 createDbgScope(DLT.Scope, DLT.InlinedAtLoc); 2012 } 2013 } 2014 2015 2016 // Build scope hierarchy using working set of scopes. 2017 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); 2018 I != E; ++I) { 2019 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); 2020 II != IE; ++II) { 2021 const MachineInstr *MInsn = II; 2022 DebugLoc DL = MInsn->getDebugLoc(); 2023 if (DL.isUnknown()) continue; 2024 DebugLocTuple DLT = MF->getDebugLocTuple(DL); 2025 if (!DLT.Scope) continue; 2026 // There is no need to create another DIE for compile unit. For all 2027 // other scopes, create one DbgScope now. This will be translated 2028 // into a scope DIE at the end. 2029 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue; 2030 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc); 2031 Scope->setLastInsn(MInsn); 2032 } 2033 } 2034 2035 // If a scope's last instruction is not set then use its child scope's 2036 // last instruction as this scope's last instrunction. 2037 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(), 2038 DE = DbgScopeMap.end(); DI != DE; ++DI) { 2039 if (DI->second->isAbstractScope()) 2040 continue; 2041 assert (DI->second->getFirstInsn() && "Invalid first instruction!"); 2042 DI->second->fixInstructionMarkers(); 2043 assert (DI->second->getLastInsn() && "Invalid last instruction!"); 2044 } 2045 2046 // Each scope has first instruction and last instruction to mark beginning 2047 // and end of a scope respectively. Create an inverse map that list scopes 2048 // starts (and ends) with an instruction. One instruction may start (or end) 2049 // multiple scopes. 2050 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(), 2051 DE = DbgScopeMap.end(); DI != DE; ++DI) { 2052 DbgScope *S = DI->second; 2053 if (S->isAbstractScope()) 2054 continue; 2055 const MachineInstr *MI = S->getFirstInsn(); 2056 assert (MI && "DbgScope does not have first instruction!"); 2057 2058 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI); 2059 if (IDI != DbgScopeBeginMap.end()) 2060 IDI->second.push_back(S); 2061 else 2062 DbgScopeBeginMap[MI].push_back(S); 2063 2064 MI = S->getLastInsn(); 2065 assert (MI && "DbgScope does not have last instruction!"); 2066 IDI = DbgScopeEndMap.find(MI); 2067 if (IDI != DbgScopeEndMap.end()) 2068 IDI->second.push_back(S); 2069 else 2070 DbgScopeEndMap[MI].push_back(S); 2071 } 2072 2073 return !DbgScopeMap.empty(); 2074} 2075 2076/// beginFunction - Gather pre-function debug information. Assumes being 2077/// emitted immediately after the function entry point. 2078void DwarfDebug::beginFunction(MachineFunction *MF) { 2079 this->MF = MF; 2080 2081 if (!ShouldEmitDwarfDebug()) return; 2082 2083 if (TimePassesIsEnabled) 2084 DebugTimer->startTimer(); 2085 2086 if (!extractScopeInformation(MF)) 2087 return; 2088 2089 collectVariableInfo(); 2090 2091 // Begin accumulating function debug information. 2092 MMI->BeginFunction(MF); 2093 2094 // Assumes in correct section after the entry point. 2095 EmitLabel("func_begin", ++SubprogramCount); 2096 2097 // Emit label for the implicitly defined dbg.stoppoint at the start of the 2098 // function. 2099 DebugLoc FDL = MF->getDefaultDebugLoc(); 2100 if (!FDL.isUnknown()) { 2101 DebugLocTuple DLT = MF->getDebugLocTuple(FDL); 2102 unsigned LabelID = 0; 2103 DISubprogram SP = getDISubprogram(DLT.Scope); 2104 if (!SP.isNull()) 2105 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope); 2106 else 2107 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope); 2108 Asm->printLabel(LabelID); 2109 O << '\n'; 2110 } 2111 if (TimePassesIsEnabled) 2112 DebugTimer->stopTimer(); 2113} 2114 2115/// endFunction - Gather and emit post-function debug information. 2116/// 2117void DwarfDebug::endFunction(MachineFunction *MF) { 2118 if (!ShouldEmitDwarfDebug()) return; 2119 2120 if (TimePassesIsEnabled) 2121 DebugTimer->startTimer(); 2122 2123 if (DbgScopeMap.empty()) 2124 return; 2125 2126 // Define end label for subprogram. 2127 EmitLabel("func_end", SubprogramCount); 2128 2129 // Get function line info. 2130 if (!Lines.empty()) { 2131 // Get section line info. 2132 unsigned ID = SectionMap.insert(Asm->getCurrentSection()); 2133 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID); 2134 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1]; 2135 // Append the function info to section info. 2136 SectionLineInfos.insert(SectionLineInfos.end(), 2137 Lines.begin(), Lines.end()); 2138 } 2139 2140 // Construct abstract scopes. 2141 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(), 2142 AE = AbstractScopesList.end(); AI != AE; ++AI) 2143 constructScopeDIE(*AI); 2144 2145 constructScopeDIE(CurrentFnDbgScope); 2146 2147 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount, 2148 MMI->getFrameMoves())); 2149 2150 // Clear debug info 2151 CurrentFnDbgScope = NULL; 2152 DbgScopeMap.clear(); 2153 DbgScopeBeginMap.clear(); 2154 DbgScopeEndMap.clear(); 2155 ConcreteScopes.clear(); 2156 AbstractScopesList.clear(); 2157 2158 Lines.clear(); 2159 2160 if (TimePassesIsEnabled) 2161 DebugTimer->stopTimer(); 2162} 2163 2164/// recordSourceLine - Records location information and associates it with a 2165/// label. Returns a unique label ID used to generate a label and provide 2166/// correspondence to the source line list. 2167unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, 2168 MDNode *S) { 2169 if (!MMI) 2170 return 0; 2171 2172 if (TimePassesIsEnabled) 2173 DebugTimer->startTimer(); 2174 2175 StringRef Dir; 2176 StringRef Fn; 2177 2178 DIDescriptor Scope(S); 2179 if (Scope.isCompileUnit()) { 2180 DICompileUnit CU(S); 2181 Dir = CU.getDirectory(); 2182 Fn = CU.getFilename(); 2183 } else if (Scope.isSubprogram()) { 2184 DISubprogram SP(S); 2185 Dir = SP.getDirectory(); 2186 Fn = SP.getFilename(); 2187 } else if (Scope.isLexicalBlock()) { 2188 DILexicalBlock DB(S); 2189 Dir = DB.getDirectory(); 2190 Fn = DB.getFilename(); 2191 } else 2192 assert (0 && "Unexpected scope info"); 2193 2194 unsigned Src = GetOrCreateSourceID(Dir, Fn); 2195 unsigned ID = MMI->NextLabelID(); 2196 Lines.push_back(SrcLineInfo(Line, Col, Src, ID)); 2197 2198 if (TimePassesIsEnabled) 2199 DebugTimer->stopTimer(); 2200 2201 return ID; 2202} 2203 2204/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be 2205/// timed. Look up the source id with the given directory and source file 2206/// names. If none currently exists, create a new id and insert it in the 2207/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as 2208/// well. 2209unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName, 2210 const std::string &FileName) { 2211 if (TimePassesIsEnabled) 2212 DebugTimer->startTimer(); 2213 2214 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str()); 2215 2216 if (TimePassesIsEnabled) 2217 DebugTimer->stopTimer(); 2218 2219 return SrcId; 2220} 2221 2222//===----------------------------------------------------------------------===// 2223// Emit Methods 2224//===----------------------------------------------------------------------===// 2225 2226/// computeSizeAndOffset - Compute the size and offset of a DIE. 2227/// 2228unsigned 2229DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) { 2230 // Get the children. 2231 const std::vector<DIE *> &Children = Die->getChildren(); 2232 2233 // If not last sibling and has children then add sibling offset attribute. 2234 if (!Last && !Children.empty()) Die->addSiblingOffset(); 2235 2236 // Record the abbreviation. 2237 assignAbbrevNumber(Die->getAbbrev()); 2238 2239 // Get the abbreviation for this DIE. 2240 unsigned AbbrevNumber = Die->getAbbrevNumber(); 2241 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1]; 2242 2243 // Set DIE offset 2244 Die->setOffset(Offset); 2245 2246 // Start the size with the size of abbreviation code. 2247 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber); 2248 2249 const SmallVector<DIEValue*, 32> &Values = Die->getValues(); 2250 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData(); 2251 2252 // Size the DIE attribute values. 2253 for (unsigned i = 0, N = Values.size(); i < N; ++i) 2254 // Size attribute value. 2255 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm()); 2256 2257 // Size the DIE children if any. 2258 if (!Children.empty()) { 2259 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes && 2260 "Children flag not set"); 2261 2262 for (unsigned j = 0, M = Children.size(); j < M; ++j) 2263 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M); 2264 2265 // End of children marker. 2266 Offset += sizeof(int8_t); 2267 } 2268 2269 Die->setSize(Offset - Die->getOffset()); 2270 return Offset; 2271} 2272 2273/// computeSizeAndOffsets - Compute the size and offset of all the DIEs. 2274/// 2275void DwarfDebug::computeSizeAndOffsets() { 2276 // Compute size of compile unit header. 2277 static unsigned Offset = 2278 sizeof(int32_t) + // Length of Compilation Unit Info 2279 sizeof(int16_t) + // DWARF version number 2280 sizeof(int32_t) + // Offset Into Abbrev. Section 2281 sizeof(int8_t); // Pointer Size (in bytes) 2282 2283 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true); 2284 CompileUnitOffsets[ModuleCU] = 0; 2285} 2286 2287/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc 2288/// tools to recognize the object file contains Dwarf information. 2289void DwarfDebug::emitInitial() { 2290 // Check to see if we already emitted intial headers. 2291 if (didInitial) return; 2292 didInitial = true; 2293 2294 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 2295 2296 // Dwarf sections base addresses. 2297 if (MAI->doesDwarfRequireFrameSection()) { 2298 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection()); 2299 EmitLabel("section_debug_frame", 0); 2300 } 2301 2302 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection()); 2303 EmitLabel("section_info", 0); 2304 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection()); 2305 EmitLabel("section_abbrev", 0); 2306 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection()); 2307 EmitLabel("section_aranges", 0); 2308 2309 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) { 2310 Asm->OutStreamer.SwitchSection(LineInfoDirective); 2311 EmitLabel("section_macinfo", 0); 2312 } 2313 2314 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection()); 2315 EmitLabel("section_line", 0); 2316 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection()); 2317 EmitLabel("section_loc", 0); 2318 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection()); 2319 EmitLabel("section_pubnames", 0); 2320 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection()); 2321 EmitLabel("section_pubtypes", 0); 2322 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection()); 2323 EmitLabel("section_str", 0); 2324 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection()); 2325 EmitLabel("section_ranges", 0); 2326 2327 Asm->OutStreamer.SwitchSection(TLOF.getTextSection()); 2328 EmitLabel("text_begin", 0); 2329 Asm->OutStreamer.SwitchSection(TLOF.getDataSection()); 2330 EmitLabel("data_begin", 0); 2331} 2332 2333/// emitDIE - Recusively Emits a debug information entry. 2334/// 2335void DwarfDebug::emitDIE(DIE *Die) { 2336 // Get the abbreviation for this DIE. 2337 unsigned AbbrevNumber = Die->getAbbrevNumber(); 2338 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1]; 2339 2340 Asm->EOL(); 2341 2342 // Emit the code (index) for the abbreviation. 2343 Asm->EmitULEB128Bytes(AbbrevNumber); 2344 2345 if (Asm->isVerbose()) 2346 Asm->EOL(std::string("Abbrev [" + 2347 utostr(AbbrevNumber) + 2348 "] 0x" + utohexstr(Die->getOffset()) + 2349 ":0x" + utohexstr(Die->getSize()) + " " + 2350 dwarf::TagString(Abbrev->getTag()))); 2351 else 2352 Asm->EOL(); 2353 2354 SmallVector<DIEValue*, 32> &Values = Die->getValues(); 2355 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData(); 2356 2357 // Emit the DIE attribute values. 2358 for (unsigned i = 0, N = Values.size(); i < N; ++i) { 2359 unsigned Attr = AbbrevData[i].getAttribute(); 2360 unsigned Form = AbbrevData[i].getForm(); 2361 assert(Form && "Too many attributes for DIE (check abbreviation)"); 2362 2363 switch (Attr) { 2364 case dwarf::DW_AT_sibling: 2365 Asm->EmitInt32(Die->getSiblingOffset()); 2366 break; 2367 case dwarf::DW_AT_abstract_origin: { 2368 DIEEntry *E = cast<DIEEntry>(Values[i]); 2369 DIE *Origin = E->getEntry(); 2370 unsigned Addr = Origin->getOffset(); 2371 Asm->EmitInt32(Addr); 2372 break; 2373 } 2374 default: 2375 // Emit an attribute using the defined form. 2376 Values[i]->EmitValue(this, Form); 2377 break; 2378 } 2379 2380 Asm->EOL(dwarf::AttributeString(Attr)); 2381 } 2382 2383 // Emit the DIE children if any. 2384 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) { 2385 const std::vector<DIE *> &Children = Die->getChildren(); 2386 2387 for (unsigned j = 0, M = Children.size(); j < M; ++j) 2388 emitDIE(Children[j]); 2389 2390 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark"); 2391 } 2392} 2393 2394/// emitDebugInfo / emitDebugInfoPerCU - Emit the debug info section. 2395/// 2396void DwarfDebug::emitDebugInfoPerCU(CompileUnit *Unit) { 2397 DIE *Die = Unit->getCUDie(); 2398 2399 // Emit the compile units header. 2400 EmitLabel("info_begin", Unit->getID()); 2401 2402 // Emit size of content not including length itself 2403 unsigned ContentSize = Die->getSize() + 2404 sizeof(int16_t) + // DWARF version number 2405 sizeof(int32_t) + // Offset Into Abbrev. Section 2406 sizeof(int8_t) + // Pointer Size (in bytes) 2407 sizeof(int32_t); // FIXME - extra pad for gdb bug. 2408 2409 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info"); 2410 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number"); 2411 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false); 2412 Asm->EOL("Offset Into Abbrev. Section"); 2413 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)"); 2414 2415 emitDIE(Die); 2416 // FIXME - extra padding for gdb bug. 2417 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); 2418 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); 2419 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); 2420 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); 2421 EmitLabel("info_end", Unit->getID()); 2422 2423 Asm->EOL(); 2424} 2425 2426void DwarfDebug::emitDebugInfo() { 2427 // Start debug info section. 2428 Asm->OutStreamer.SwitchSection( 2429 Asm->getObjFileLowering().getDwarfInfoSection()); 2430 2431 emitDebugInfoPerCU(ModuleCU); 2432} 2433 2434/// emitAbbreviations - Emit the abbreviation section. 2435/// 2436void DwarfDebug::emitAbbreviations() const { 2437 // Check to see if it is worth the effort. 2438 if (!Abbreviations.empty()) { 2439 // Start the debug abbrev section. 2440 Asm->OutStreamer.SwitchSection( 2441 Asm->getObjFileLowering().getDwarfAbbrevSection()); 2442 2443 EmitLabel("abbrev_begin", 0); 2444 2445 // For each abbrevation. 2446 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) { 2447 // Get abbreviation data 2448 const DIEAbbrev *Abbrev = Abbreviations[i]; 2449 2450 // Emit the abbrevations code (base 1 index.) 2451 Asm->EmitULEB128Bytes(Abbrev->getNumber()); 2452 Asm->EOL("Abbreviation Code"); 2453 2454 // Emit the abbreviations data. 2455 Abbrev->Emit(Asm); 2456 2457 Asm->EOL(); 2458 } 2459 2460 // Mark end of abbreviations. 2461 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)"); 2462 2463 EmitLabel("abbrev_end", 0); 2464 Asm->EOL(); 2465 } 2466} 2467 2468/// emitEndOfLineMatrix - Emit the last address of the section and the end of 2469/// the line matrix. 2470/// 2471void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) { 2472 // Define last address of section. 2473 Asm->EmitInt8(0); Asm->EOL("Extended Op"); 2474 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size"); 2475 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address"); 2476 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label"); 2477 2478 // Mark end of matrix. 2479 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence"); 2480 Asm->EmitULEB128Bytes(1); Asm->EOL(); 2481 Asm->EmitInt8(1); Asm->EOL(); 2482} 2483 2484/// emitDebugLines - Emit source line information. 2485/// 2486void DwarfDebug::emitDebugLines() { 2487 // If the target is using .loc/.file, the assembler will be emitting the 2488 // .debug_line table automatically. 2489 if (MAI->hasDotLocAndDotFile()) 2490 return; 2491 2492 // Minimum line delta, thus ranging from -10..(255-10). 2493 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1); 2494 // Maximum line delta, thus ranging from -10..(255-10). 2495 const int MaxLineDelta = 255 + MinLineDelta; 2496 2497 // Start the dwarf line section. 2498 Asm->OutStreamer.SwitchSection( 2499 Asm->getObjFileLowering().getDwarfLineSection()); 2500 2501 // Construct the section header. 2502 EmitDifference("line_end", 0, "line_begin", 0, true); 2503 Asm->EOL("Length of Source Line Info"); 2504 EmitLabel("line_begin", 0); 2505 2506 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number"); 2507 2508 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true); 2509 Asm->EOL("Prolog Length"); 2510 EmitLabel("line_prolog_begin", 0); 2511 2512 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length"); 2513 2514 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag"); 2515 2516 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)"); 2517 2518 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)"); 2519 2520 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base"); 2521 2522 // Line number standard opcode encodings argument count 2523 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count"); 2524 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count"); 2525 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count"); 2526 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count"); 2527 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count"); 2528 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count"); 2529 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count"); 2530 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count"); 2531 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count"); 2532 2533 // Emit directories. 2534 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) { 2535 Asm->EmitString(getSourceDirectoryName(DI)); 2536 Asm->EOL("Directory"); 2537 } 2538 2539 Asm->EmitInt8(0); Asm->EOL("End of directories"); 2540 2541 // Emit files. 2542 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) { 2543 // Remember source id starts at 1. 2544 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI); 2545 Asm->EmitString(getSourceFileName(Id.second)); 2546 Asm->EOL("Source"); 2547 Asm->EmitULEB128Bytes(Id.first); 2548 Asm->EOL("Directory #"); 2549 Asm->EmitULEB128Bytes(0); 2550 Asm->EOL("Mod date"); 2551 Asm->EmitULEB128Bytes(0); 2552 Asm->EOL("File size"); 2553 } 2554 2555 Asm->EmitInt8(0); Asm->EOL("End of files"); 2556 2557 EmitLabel("line_prolog_end", 0); 2558 2559 // A sequence for each text section. 2560 unsigned SecSrcLinesSize = SectionSourceLines.size(); 2561 2562 for (unsigned j = 0; j < SecSrcLinesSize; ++j) { 2563 // Isolate current sections line info. 2564 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j]; 2565 2566 /*if (Asm->isVerbose()) { 2567 const MCSection *S = SectionMap[j + 1]; 2568 O << '\t' << MAI->getCommentString() << " Section" 2569 << S->getName() << '\n'; 2570 }*/ 2571 Asm->EOL(); 2572 2573 // Dwarf assumes we start with first line of first source file. 2574 unsigned Source = 1; 2575 unsigned Line = 1; 2576 2577 // Construct rows of the address, source, line, column matrix. 2578 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) { 2579 const SrcLineInfo &LineInfo = LineInfos[i]; 2580 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID()); 2581 if (!LabelID) continue; 2582 2583 if (LineInfo.getLine() == 0) continue; 2584 2585 if (!Asm->isVerbose()) 2586 Asm->EOL(); 2587 else { 2588 std::pair<unsigned, unsigned> SourceID = 2589 getSourceDirectoryAndFileIds(LineInfo.getSourceID()); 2590 O << '\t' << MAI->getCommentString() << ' ' 2591 << getSourceDirectoryName(SourceID.first) << ' ' 2592 << getSourceFileName(SourceID.second) 2593 <<" :" << utostr_32(LineInfo.getLine()) << '\n'; 2594 } 2595 2596 // Define the line address. 2597 Asm->EmitInt8(0); Asm->EOL("Extended Op"); 2598 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size"); 2599 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address"); 2600 EmitReference("label", LabelID); Asm->EOL("Location label"); 2601 2602 // If change of source, then switch to the new source. 2603 if (Source != LineInfo.getSourceID()) { 2604 Source = LineInfo.getSourceID(); 2605 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file"); 2606 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source"); 2607 } 2608 2609 // If change of line. 2610 if (Line != LineInfo.getLine()) { 2611 // Determine offset. 2612 int Offset = LineInfo.getLine() - Line; 2613 int Delta = Offset - MinLineDelta; 2614 2615 // Update line. 2616 Line = LineInfo.getLine(); 2617 2618 // If delta is small enough and in range... 2619 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) { 2620 // ... then use fast opcode. 2621 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta"); 2622 } else { 2623 // ... otherwise use long hand. 2624 Asm->EmitInt8(dwarf::DW_LNS_advance_line); 2625 Asm->EOL("DW_LNS_advance_line"); 2626 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset"); 2627 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy"); 2628 } 2629 } else { 2630 // Copy the previous row (different address or source) 2631 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy"); 2632 } 2633 } 2634 2635 emitEndOfLineMatrix(j + 1); 2636 } 2637 2638 if (SecSrcLinesSize == 0) 2639 // Because we're emitting a debug_line section, we still need a line 2640 // table. The linker and friends expect it to exist. If there's nothing to 2641 // put into it, emit an empty table. 2642 emitEndOfLineMatrix(1); 2643 2644 EmitLabel("line_end", 0); 2645 Asm->EOL(); 2646} 2647 2648/// emitCommonDebugFrame - Emit common frame info into a debug frame section. 2649/// 2650void DwarfDebug::emitCommonDebugFrame() { 2651 if (!MAI->doesDwarfRequireFrameSection()) 2652 return; 2653 2654 int stackGrowth = 2655 Asm->TM.getFrameInfo()->getStackGrowthDirection() == 2656 TargetFrameInfo::StackGrowsUp ? 2657 TD->getPointerSize() : -TD->getPointerSize(); 2658 2659 // Start the dwarf frame section. 2660 Asm->OutStreamer.SwitchSection( 2661 Asm->getObjFileLowering().getDwarfFrameSection()); 2662 2663 EmitLabel("debug_frame_common", 0); 2664 EmitDifference("debug_frame_common_end", 0, 2665 "debug_frame_common_begin", 0, true); 2666 Asm->EOL("Length of Common Information Entry"); 2667 2668 EmitLabel("debug_frame_common_begin", 0); 2669 Asm->EmitInt32((int)dwarf::DW_CIE_ID); 2670 Asm->EOL("CIE Identifier Tag"); 2671 Asm->EmitInt8(dwarf::DW_CIE_VERSION); 2672 Asm->EOL("CIE Version"); 2673 Asm->EmitString(""); 2674 Asm->EOL("CIE Augmentation"); 2675 Asm->EmitULEB128Bytes(1); 2676 Asm->EOL("CIE Code Alignment Factor"); 2677 Asm->EmitSLEB128Bytes(stackGrowth); 2678 Asm->EOL("CIE Data Alignment Factor"); 2679 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false)); 2680 Asm->EOL("CIE RA Column"); 2681 2682 std::vector<MachineMove> Moves; 2683 RI->getInitialFrameState(Moves); 2684 2685 EmitFrameMoves(NULL, 0, Moves, false); 2686 2687 Asm->EmitAlignment(2, 0, 0, false); 2688 EmitLabel("debug_frame_common_end", 0); 2689 2690 Asm->EOL(); 2691} 2692 2693/// emitFunctionDebugFrame - Emit per function frame info into a debug frame 2694/// section. 2695void 2696DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){ 2697 if (!MAI->doesDwarfRequireFrameSection()) 2698 return; 2699 2700 // Start the dwarf frame section. 2701 Asm->OutStreamer.SwitchSection( 2702 Asm->getObjFileLowering().getDwarfFrameSection()); 2703 2704 EmitDifference("debug_frame_end", DebugFrameInfo.Number, 2705 "debug_frame_begin", DebugFrameInfo.Number, true); 2706 Asm->EOL("Length of Frame Information Entry"); 2707 2708 EmitLabel("debug_frame_begin", DebugFrameInfo.Number); 2709 2710 EmitSectionOffset("debug_frame_common", "section_debug_frame", 2711 0, 0, true, false); 2712 Asm->EOL("FDE CIE offset"); 2713 2714 EmitReference("func_begin", DebugFrameInfo.Number); 2715 Asm->EOL("FDE initial location"); 2716 EmitDifference("func_end", DebugFrameInfo.Number, 2717 "func_begin", DebugFrameInfo.Number); 2718 Asm->EOL("FDE address range"); 2719 2720 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, 2721 false); 2722 2723 Asm->EmitAlignment(2, 0, 0, false); 2724 EmitLabel("debug_frame_end", DebugFrameInfo.Number); 2725 2726 Asm->EOL(); 2727} 2728 2729void DwarfDebug::emitDebugPubNamesPerCU(CompileUnit *Unit) { 2730 EmitDifference("pubnames_end", Unit->getID(), 2731 "pubnames_begin", Unit->getID(), true); 2732 Asm->EOL("Length of Public Names Info"); 2733 2734 EmitLabel("pubnames_begin", Unit->getID()); 2735 2736 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version"); 2737 2738 EmitSectionOffset("info_begin", "section_info", 2739 Unit->getID(), 0, true, false); 2740 Asm->EOL("Offset of Compilation Unit Info"); 2741 2742 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(), 2743 true); 2744 Asm->EOL("Compilation Unit Length"); 2745 2746 const StringMap<DIE*> &Globals = Unit->getGlobals(); 2747 for (StringMap<DIE*>::const_iterator 2748 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) { 2749 const char *Name = GI->getKeyData(); 2750 DIE * Entity = GI->second; 2751 2752 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset"); 2753 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name"); 2754 } 2755 2756 Asm->EmitInt32(0); Asm->EOL("End Mark"); 2757 EmitLabel("pubnames_end", Unit->getID()); 2758 2759 Asm->EOL(); 2760} 2761 2762/// emitDebugPubNames - Emit visible names into a debug pubnames section. 2763/// 2764void DwarfDebug::emitDebugPubNames() { 2765 // Start the dwarf pubnames section. 2766 Asm->OutStreamer.SwitchSection( 2767 Asm->getObjFileLowering().getDwarfPubNamesSection()); 2768 2769 emitDebugPubNamesPerCU(ModuleCU); 2770} 2771 2772void DwarfDebug::emitDebugPubTypes() { 2773 // Start the dwarf pubnames section. 2774 Asm->OutStreamer.SwitchSection( 2775 Asm->getObjFileLowering().getDwarfPubTypesSection()); 2776 EmitDifference("pubtypes_end", ModuleCU->getID(), 2777 "pubtypes_begin", ModuleCU->getID(), true); 2778 Asm->EOL("Length of Public Types Info"); 2779 2780 EmitLabel("pubtypes_begin", ModuleCU->getID()); 2781 2782 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version"); 2783 2784 EmitSectionOffset("info_begin", "section_info", 2785 ModuleCU->getID(), 0, true, false); 2786 Asm->EOL("Offset of Compilation ModuleCU Info"); 2787 2788 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(), 2789 true); 2790 Asm->EOL("Compilation ModuleCU Length"); 2791 2792 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes(); 2793 for (StringMap<DIE*>::const_iterator 2794 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) { 2795 const char *Name = GI->getKeyData(); 2796 DIE * Entity = GI->second; 2797 2798 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset"); 2799 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name"); 2800 } 2801 2802 Asm->EmitInt32(0); Asm->EOL("End Mark"); 2803 EmitLabel("pubtypes_end", ModuleCU->getID()); 2804 2805 Asm->EOL(); 2806} 2807 2808/// emitDebugStr - Emit visible names into a debug str section. 2809/// 2810void DwarfDebug::emitDebugStr() { 2811 // Check to see if it is worth the effort. 2812 if (!StringPool.empty()) { 2813 // Start the dwarf str section. 2814 Asm->OutStreamer.SwitchSection( 2815 Asm->getObjFileLowering().getDwarfStrSection()); 2816 2817 // For each of strings in the string pool. 2818 for (unsigned StringID = 1, N = StringPool.size(); 2819 StringID <= N; ++StringID) { 2820 // Emit a label for reference from debug information entries. 2821 EmitLabel("string", StringID); 2822 2823 // Emit the string itself. 2824 const std::string &String = StringPool[StringID]; 2825 Asm->EmitString(String); Asm->EOL(); 2826 } 2827 2828 Asm->EOL(); 2829 } 2830} 2831 2832/// emitDebugLoc - Emit visible names into a debug loc section. 2833/// 2834void DwarfDebug::emitDebugLoc() { 2835 // Start the dwarf loc section. 2836 Asm->OutStreamer.SwitchSection( 2837 Asm->getObjFileLowering().getDwarfLocSection()); 2838 Asm->EOL(); 2839} 2840 2841/// EmitDebugARanges - Emit visible names into a debug aranges section. 2842/// 2843void DwarfDebug::EmitDebugARanges() { 2844 // Start the dwarf aranges section. 2845 Asm->OutStreamer.SwitchSection( 2846 Asm->getObjFileLowering().getDwarfARangesSection()); 2847 2848 // FIXME - Mock up 2849#if 0 2850 CompileUnit *Unit = GetBaseCompileUnit(); 2851 2852 // Don't include size of length 2853 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info"); 2854 2855 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version"); 2856 2857 EmitReference("info_begin", Unit->getID()); 2858 Asm->EOL("Offset of Compilation Unit Info"); 2859 2860 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address"); 2861 2862 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor"); 2863 2864 Asm->EmitInt16(0); Asm->EOL("Pad (1)"); 2865 Asm->EmitInt16(0); Asm->EOL("Pad (2)"); 2866 2867 // Range 1 2868 EmitReference("text_begin", 0); Asm->EOL("Address"); 2869 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length"); 2870 2871 Asm->EmitInt32(0); Asm->EOL("EOM (1)"); 2872 Asm->EmitInt32(0); Asm->EOL("EOM (2)"); 2873#endif 2874 2875 Asm->EOL(); 2876} 2877 2878/// emitDebugRanges - Emit visible names into a debug ranges section. 2879/// 2880void DwarfDebug::emitDebugRanges() { 2881 // Start the dwarf ranges section. 2882 Asm->OutStreamer.SwitchSection( 2883 Asm->getObjFileLowering().getDwarfRangesSection()); 2884 Asm->EOL(); 2885} 2886 2887/// emitDebugMacInfo - Emit visible names into a debug macinfo section. 2888/// 2889void DwarfDebug::emitDebugMacInfo() { 2890 if (const MCSection *LineInfo = 2891 Asm->getObjFileLowering().getDwarfMacroInfoSection()) { 2892 // Start the dwarf macinfo section. 2893 Asm->OutStreamer.SwitchSection(LineInfo); 2894 Asm->EOL(); 2895 } 2896} 2897 2898/// emitDebugInlineInfo - Emit inline info using following format. 2899/// Section Header: 2900/// 1. length of section 2901/// 2. Dwarf version number 2902/// 3. address size. 2903/// 2904/// Entries (one "entry" for each function that was inlined): 2905/// 2906/// 1. offset into __debug_str section for MIPS linkage name, if exists; 2907/// otherwise offset into __debug_str for regular function name. 2908/// 2. offset into __debug_str section for regular function name. 2909/// 3. an unsigned LEB128 number indicating the number of distinct inlining 2910/// instances for the function. 2911/// 2912/// The rest of the entry consists of a {die_offset, low_pc} pair for each 2913/// inlined instance; the die_offset points to the inlined_subroutine die in the 2914/// __debug_info section, and the low_pc is the starting address for the 2915/// inlining instance. 2916void DwarfDebug::emitDebugInlineInfo() { 2917 if (!MAI->doesDwarfUsesInlineInfoSection()) 2918 return; 2919 2920 if (!ModuleCU) 2921 return; 2922 2923 Asm->OutStreamer.SwitchSection( 2924 Asm->getObjFileLowering().getDwarfDebugInlineSection()); 2925 Asm->EOL(); 2926 EmitDifference("debug_inlined_end", 1, 2927 "debug_inlined_begin", 1, true); 2928 Asm->EOL("Length of Debug Inlined Information Entry"); 2929 2930 EmitLabel("debug_inlined_begin", 1); 2931 2932 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version"); 2933 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)"); 2934 2935 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(), 2936 E = InlinedSPNodes.end(); I != E; ++I) { 2937 2938// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator 2939 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) { 2940 MDNode *Node = *I; 2941 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II 2942 = InlineInfo.find(Node); 2943 SmallVector<InlineInfoLabels, 4> &Labels = II->second; 2944 DISubprogram SP(Node); 2945 StringRef LName = SP.getLinkageName(); 2946 StringRef Name = SP.getName(); 2947 2948 if (LName.empty()) 2949 Asm->EmitString(Name); 2950 else { 2951 // Skip special LLVM prefix that is used to inform the asm printer to not 2952 // emit usual symbol prefix before the symbol name. This happens for 2953 // Objective-C symbol names and symbol whose name is replaced using GCC's 2954 // __asm__ attribute. 2955 if (LName[0] == 1) 2956 LName = LName.substr(1); 2957// Asm->EmitString(LName); 2958 EmitSectionOffset("string", "section_str", 2959 StringPool.idFor(LName), false, true); 2960 2961 } 2962 Asm->EOL("MIPS linkage name"); 2963// Asm->EmitString(Name); 2964 EmitSectionOffset("string", "section_str", 2965 StringPool.idFor(Name), false, true); 2966 Asm->EOL("Function name"); 2967 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count"); 2968 2969 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(), 2970 LE = Labels.end(); LI != LE; ++LI) { 2971 DIE *SP = LI->second; 2972 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset"); 2973 2974 if (TD->getPointerSize() == sizeof(int32_t)) 2975 O << MAI->getData32bitsDirective(); 2976 else 2977 O << MAI->getData64bitsDirective(); 2978 2979 PrintLabelName("label", LI->first); Asm->EOL("low_pc"); 2980 } 2981 } 2982 2983 EmitLabel("debug_inlined_end", 1); 2984 Asm->EOL(); 2985} 2986