LLVM API Documentation
00001 //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains support for constructing a dwarf compile unit. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "DwarfUnit.h" 00015 #include "DwarfAccelTable.h" 00016 #include "DwarfDebug.h" 00017 #include "llvm/ADT/APFloat.h" 00018 #include "llvm/IR/Constants.h" 00019 #include "llvm/IR/DIBuilder.h" 00020 #include "llvm/IR/DataLayout.h" 00021 #include "llvm/IR/GlobalVariable.h" 00022 #include "llvm/IR/Instructions.h" 00023 #include "llvm/IR/Mangler.h" 00024 #include "llvm/MC/MCAsmInfo.h" 00025 #include "llvm/MC/MCContext.h" 00026 #include "llvm/MC/MCSection.h" 00027 #include "llvm/MC/MCStreamer.h" 00028 #include "llvm/Support/CommandLine.h" 00029 #include "llvm/Target/TargetFrameLowering.h" 00030 #include "llvm/Target/TargetLoweringObjectFile.h" 00031 #include "llvm/Target/TargetMachine.h" 00032 #include "llvm/Target/TargetRegisterInfo.h" 00033 #include "llvm/Target/TargetSubtargetInfo.h" 00034 00035 using namespace llvm; 00036 00037 #define DEBUG_TYPE "dwarfdebug" 00038 00039 static cl::opt<bool> 00040 GenerateDwarfTypeUnits("generate-type-units", cl::Hidden, 00041 cl::desc("Generate DWARF4 type units."), 00042 cl::init(false)); 00043 00044 /// Unit - Unit constructor. 00045 DwarfUnit::DwarfUnit(unsigned UID, dwarf::Tag UnitTag, DICompileUnit Node, 00046 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU) 00047 : UniqueID(UID), CUNode(Node), UnitDie(UnitTag), DebugInfoOffset(0), Asm(A), 00048 DD(DW), DU(DWU), IndexTyDie(nullptr), Section(nullptr), 00049 Skeleton(nullptr) { 00050 assert(UnitTag == dwarf::DW_TAG_compile_unit || 00051 UnitTag == dwarf::DW_TAG_type_unit); 00052 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1); 00053 } 00054 00055 DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DICompileUnit Node, 00056 AsmPrinter *A, DwarfDebug *DW, 00057 DwarfFile *DWU) 00058 : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU) { 00059 insertDIE(Node, &getUnitDie()); 00060 } 00061 00062 DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DwarfCompileUnit &CU, AsmPrinter *A, 00063 DwarfDebug *DW, DwarfFile *DWU, 00064 MCDwarfDwoLineTable *SplitLineTable) 00065 : DwarfUnit(UID, dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU), 00066 CU(CU), SplitLineTable(SplitLineTable) { 00067 if (SplitLineTable) 00068 addSectionOffset(UnitDie, dwarf::DW_AT_stmt_list, 0); 00069 } 00070 00071 /// ~Unit - Destructor for compile unit. 00072 DwarfUnit::~DwarfUnit() { 00073 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) 00074 DIEBlocks[j]->~DIEBlock(); 00075 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j) 00076 DIELocs[j]->~DIELoc(); 00077 } 00078 00079 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug 00080 /// information entry. 00081 DIEEntry *DwarfUnit::createDIEEntry(DIE &Entry) { 00082 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry); 00083 return Value; 00084 } 00085 00086 /// getDefaultLowerBound - Return the default lower bound for an array. If the 00087 /// DWARF version doesn't handle the language, return -1. 00088 int64_t DwarfUnit::getDefaultLowerBound() const { 00089 switch (getLanguage()) { 00090 default: 00091 break; 00092 00093 case dwarf::DW_LANG_C89: 00094 case dwarf::DW_LANG_C99: 00095 case dwarf::DW_LANG_C: 00096 case dwarf::DW_LANG_C_plus_plus: 00097 case dwarf::DW_LANG_ObjC: 00098 case dwarf::DW_LANG_ObjC_plus_plus: 00099 return 0; 00100 00101 case dwarf::DW_LANG_Fortran77: 00102 case dwarf::DW_LANG_Fortran90: 00103 case dwarf::DW_LANG_Fortran95: 00104 return 1; 00105 00106 // The languages below have valid values only if the DWARF version >= 4. 00107 case dwarf::DW_LANG_Java: 00108 case dwarf::DW_LANG_Python: 00109 case dwarf::DW_LANG_UPC: 00110 case dwarf::DW_LANG_D: 00111 if (dwarf::DWARF_VERSION >= 4) 00112 return 0; 00113 break; 00114 00115 case dwarf::DW_LANG_Ada83: 00116 case dwarf::DW_LANG_Ada95: 00117 case dwarf::DW_LANG_Cobol74: 00118 case dwarf::DW_LANG_Cobol85: 00119 case dwarf::DW_LANG_Modula2: 00120 case dwarf::DW_LANG_Pascal83: 00121 case dwarf::DW_LANG_PLI: 00122 if (dwarf::DWARF_VERSION >= 4) 00123 return 1; 00124 break; 00125 } 00126 00127 return -1; 00128 } 00129 00130 /// Check whether the DIE for this MDNode can be shared across CUs. 00131 static bool isShareableAcrossCUs(DIDescriptor D) { 00132 // When the MDNode can be part of the type system, the DIE can be shared 00133 // across CUs. 00134 // Combining type units and cross-CU DIE sharing is lower value (since 00135 // cross-CU DIE sharing is used in LTO and removes type redundancy at that 00136 // level already) but may be implementable for some value in projects 00137 // building multiple independent libraries with LTO and then linking those 00138 // together. 00139 return (D.isType() || 00140 (D.isSubprogram() && !DISubprogram(D).isDefinition())) && 00141 !GenerateDwarfTypeUnits; 00142 } 00143 00144 /// getDIE - Returns the debug information entry map slot for the 00145 /// specified debug variable. We delegate the request to DwarfDebug 00146 /// when the DIE for this MDNode can be shared across CUs. The mappings 00147 /// will be kept in DwarfDebug for shareable DIEs. 00148 DIE *DwarfUnit::getDIE(DIDescriptor D) const { 00149 if (isShareableAcrossCUs(D)) 00150 return DD->getDIE(D); 00151 return MDNodeToDieMap.lookup(D); 00152 } 00153 00154 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug 00155 /// when the DIE for this MDNode can be shared across CUs. The mappings 00156 /// will be kept in DwarfDebug for shareable DIEs. 00157 void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) { 00158 if (isShareableAcrossCUs(Desc)) { 00159 DD->insertDIE(Desc, D); 00160 return; 00161 } 00162 MDNodeToDieMap.insert(std::make_pair(Desc, D)); 00163 } 00164 00165 /// addFlag - Add a flag that is true. 00166 void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) { 00167 if (DD->getDwarfVersion() >= 4) 00168 Die.addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne); 00169 else 00170 Die.addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne); 00171 } 00172 00173 /// addUInt - Add an unsigned integer attribute data and value. 00174 /// 00175 void DwarfUnit::addUInt(DIE &Die, dwarf::Attribute Attribute, 00176 Optional<dwarf::Form> Form, uint64_t Integer) { 00177 if (!Form) 00178 Form = DIEInteger::BestForm(false, Integer); 00179 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator) 00180 DIEInteger(Integer); 00181 Die.addValue(Attribute, *Form, Value); 00182 } 00183 00184 void DwarfUnit::addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer) { 00185 addUInt(Block, (dwarf::Attribute)0, Form, Integer); 00186 } 00187 00188 /// addSInt - Add an signed integer attribute data and value. 00189 /// 00190 void DwarfUnit::addSInt(DIE &Die, dwarf::Attribute Attribute, 00191 Optional<dwarf::Form> Form, int64_t Integer) { 00192 if (!Form) 00193 Form = DIEInteger::BestForm(true, Integer); 00194 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); 00195 Die.addValue(Attribute, *Form, Value); 00196 } 00197 00198 void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form, 00199 int64_t Integer) { 00200 addSInt(Die, (dwarf::Attribute)0, Form, Integer); 00201 } 00202 00203 /// addString - Add a string attribute data and value. We always emit a 00204 /// reference to the string pool instead of immediate strings so that DIEs have 00205 /// more predictable sizes. In the case of split dwarf we emit an index 00206 /// into another table which gets us the static offset into the string 00207 /// table. 00208 void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute, 00209 StringRef String) { 00210 00211 if (!DD->useSplitDwarf()) 00212 return addLocalString(Die, Attribute, String); 00213 00214 unsigned idx = DU->getStringPool().getIndex(*Asm, String); 00215 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); 00216 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String); 00217 Die.addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str); 00218 } 00219 00220 /// addLocalString - Add a string attribute data and value. This is guaranteed 00221 /// to be in the local string pool instead of indirected. 00222 void DwarfUnit::addLocalString(DIE &Die, dwarf::Attribute Attribute, 00223 StringRef String) { 00224 MCSymbol *Symb = DU->getStringPool().getSymbol(*Asm, String); 00225 DIEValue *Value; 00226 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 00227 Value = new (DIEValueAllocator) DIELabel(Symb); 00228 else 00229 Value = new (DIEValueAllocator) DIEDelta(Symb, DD->getDebugStrSym()); 00230 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String); 00231 Die.addValue(Attribute, dwarf::DW_FORM_strp, Str); 00232 } 00233 00234 /// addExpr - Add a Dwarf expression attribute data and value. 00235 /// 00236 void DwarfUnit::addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr) { 00237 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr); 00238 Die.addValue((dwarf::Attribute)0, Form, Value); 00239 } 00240 00241 /// addLocationList - Add a Dwarf loclistptr attribute data and value. 00242 /// 00243 void DwarfUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute, 00244 unsigned Index) { 00245 DIEValue *Value = new (DIEValueAllocator) DIELocList(Index); 00246 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 00247 : dwarf::DW_FORM_data4; 00248 Die.addValue(Attribute, Form, Value); 00249 } 00250 00251 /// addLabel - Add a Dwarf label attribute data and value. 00252 /// 00253 void DwarfUnit::addLabel(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form, 00254 const MCSymbol *Label) { 00255 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); 00256 Die.addValue(Attribute, Form, Value); 00257 } 00258 00259 void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) { 00260 addLabel(Die, (dwarf::Attribute)0, Form, Label); 00261 } 00262 00263 /// addSectionLabel - Add a Dwarf section label attribute data and value. 00264 /// 00265 void DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 00266 const MCSymbol *Label) { 00267 if (DD->getDwarfVersion() >= 4) 00268 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label); 00269 else 00270 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label); 00271 } 00272 00273 /// addSectionOffset - Add an offset into a section attribute data and value. 00274 /// 00275 void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute, 00276 uint64_t Integer) { 00277 if (DD->getDwarfVersion() >= 4) 00278 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer); 00279 else 00280 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer); 00281 } 00282 00283 /// addLabelAddress - Add a dwarf label attribute data and value using 00284 /// DW_FORM_addr or DW_FORM_GNU_addr_index. 00285 /// 00286 void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 00287 const MCSymbol *Label) { 00288 00289 // Don't use the address pool in non-fission or in the skeleton unit itself. 00290 // FIXME: Once GDB supports this, it's probably worthwhile using the address 00291 // pool from the skeleton - maybe even in non-fission (possibly fewer 00292 // relocations by sharing them in the pool, but we have other ideas about how 00293 // to reduce the number of relocations as well/instead). 00294 if (!DD->useSplitDwarf() || !Skeleton) 00295 return addLocalLabelAddress(Die, Attribute, Label); 00296 00297 if (Label) 00298 DD->addArangeLabel(SymbolCU(this, Label)); 00299 00300 unsigned idx = DD->getAddressPool().getIndex(Label); 00301 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); 00302 Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value); 00303 } 00304 00305 void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, 00306 dwarf::Attribute Attribute, 00307 const MCSymbol *Label) { 00308 if (Label) 00309 DD->addArangeLabel(SymbolCU(this, Label)); 00310 00311 Die.addValue(Attribute, dwarf::DW_FORM_addr, 00312 Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label) 00313 : new (DIEValueAllocator) DIEInteger(0)); 00314 } 00315 00316 unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) { 00317 // If we print assembly, we can't separate .file entries according to 00318 // compile units. Thus all files will belong to the default compile unit. 00319 00320 // FIXME: add a better feature test than hasRawTextSupport. Even better, 00321 // extend .file to support this. 00322 return Asm->OutStreamer.EmitDwarfFileDirective( 00323 0, DirName, FileName, 00324 Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID()); 00325 } 00326 00327 unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) { 00328 return SplitLineTable ? SplitLineTable->getFile(DirName, FileName) 00329 : getCU().getOrCreateSourceID(FileName, DirName); 00330 } 00331 00332 /// addOpAddress - Add a dwarf op address data and value using the 00333 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index. 00334 /// 00335 void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) { 00336 if (!DD->useSplitDwarf()) { 00337 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); 00338 addLabel(Die, dwarf::DW_FORM_udata, Sym); 00339 } else { 00340 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index); 00341 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, 00342 DD->getAddressPool().getIndex(Sym)); 00343 } 00344 } 00345 00346 /// addSectionDelta - Add a section label delta attribute data and value. 00347 /// 00348 void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 00349 const MCSymbol *Hi, const MCSymbol *Lo) { 00350 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); 00351 Die.addValue(Attribute, DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 00352 : dwarf::DW_FORM_data4, 00353 Value); 00354 } 00355 00356 void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute, 00357 const MCSymbol *Hi, const MCSymbol *Lo) { 00358 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); 00359 Die.addValue(Attribute, dwarf::DW_FORM_data4, Value); 00360 } 00361 00362 /// addDIEEntry - Add a DIE attribute data and value. 00363 /// 00364 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) { 00365 addDIEEntry(Die, Attribute, createDIEEntry(Entry)); 00366 } 00367 00368 void DwarfUnit::addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type) { 00369 // Flag the type unit reference as a declaration so that if it contains 00370 // members (implicit special members, static data member definitions, member 00371 // declarations for definitions in this CU, etc) consumers don't get confused 00372 // and think this is a full definition. 00373 addFlag(Die, dwarf::DW_AT_declaration); 00374 00375 Die.addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8, 00376 new (DIEValueAllocator) DIETypeSignature(Type)); 00377 } 00378 00379 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, 00380 DIEEntry *Entry) { 00381 const DIE *DieCU = Die.getUnitOrNull(); 00382 const DIE *EntryCU = Entry->getEntry().getUnitOrNull(); 00383 if (!DieCU) 00384 // We assume that Die belongs to this CU, if it is not linked to any CU yet. 00385 DieCU = &getUnitDie(); 00386 if (!EntryCU) 00387 EntryCU = &getUnitDie(); 00388 Die.addValue(Attribute, 00389 EntryCU == DieCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr, 00390 Entry); 00391 } 00392 00393 /// Create a DIE with the given Tag, add the DIE to its parent, and 00394 /// call insertDIE if MD is not null. 00395 DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) { 00396 assert(Tag != dwarf::DW_TAG_auto_variable && 00397 Tag != dwarf::DW_TAG_arg_variable); 00398 Parent.addChild(make_unique<DIE>((dwarf::Tag)Tag)); 00399 DIE &Die = *Parent.getChildren().back(); 00400 if (N) 00401 insertDIE(N, &Die); 00402 return Die; 00403 } 00404 00405 /// addBlock - Add block data. 00406 /// 00407 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) { 00408 Loc->ComputeSize(Asm); 00409 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on. 00410 Die.addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc); 00411 } 00412 00413 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, 00414 DIEBlock *Block) { 00415 Block->ComputeSize(Asm); 00416 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. 00417 Die.addValue(Attribute, Block->BestForm(), Block); 00418 } 00419 00420 /// addSourceLine - Add location information to specified debug information 00421 /// entry. 00422 void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, StringRef File, 00423 StringRef Directory) { 00424 if (Line == 0) 00425 return; 00426 00427 unsigned FileID = getOrCreateSourceID(File, Directory); 00428 assert(FileID && "Invalid file id"); 00429 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); 00430 addUInt(Die, dwarf::DW_AT_decl_line, None, Line); 00431 } 00432 00433 /// addSourceLine - Add location information to specified debug information 00434 /// entry. 00435 void DwarfUnit::addSourceLine(DIE &Die, DIVariable V) { 00436 assert(V.isVariable()); 00437 00438 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(), 00439 V.getContext().getDirectory()); 00440 } 00441 00442 /// addSourceLine - Add location information to specified debug information 00443 /// entry. 00444 void DwarfUnit::addSourceLine(DIE &Die, DIGlobalVariable G) { 00445 assert(G.isGlobalVariable()); 00446 00447 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory()); 00448 } 00449 00450 /// addSourceLine - Add location information to specified debug information 00451 /// entry. 00452 void DwarfUnit::addSourceLine(DIE &Die, DISubprogram SP) { 00453 assert(SP.isSubprogram()); 00454 00455 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory()); 00456 } 00457 00458 /// addSourceLine - Add location information to specified debug information 00459 /// entry. 00460 void DwarfUnit::addSourceLine(DIE &Die, DIType Ty) { 00461 assert(Ty.isType()); 00462 00463 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory()); 00464 } 00465 00466 /// addSourceLine - Add location information to specified debug information 00467 /// entry. 00468 void DwarfUnit::addSourceLine(DIE &Die, DIObjCProperty Ty) { 00469 assert(Ty.isObjCProperty()); 00470 00471 DIFile File = Ty.getFile(); 00472 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(), 00473 File.getDirectory()); 00474 } 00475 00476 /// addSourceLine - Add location information to specified debug information 00477 /// entry. 00478 void DwarfUnit::addSourceLine(DIE &Die, DINameSpace NS) { 00479 assert(NS.Verify()); 00480 00481 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory()); 00482 } 00483 00484 /// addVariableAddress - Add DW_AT_location attribute for a 00485 /// DbgVariable based on provided MachineLocation. 00486 void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE &Die, 00487 MachineLocation Location) { 00488 if (DV.variableHasComplexAddress()) 00489 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); 00490 else if (DV.isBlockByrefVariable()) 00491 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); 00492 else 00493 addAddress(Die, dwarf::DW_AT_location, Location, 00494 DV.getVariable().isIndirect()); 00495 } 00496 00497 /// addRegisterOp - Add register operand. 00498 // FIXME: Ideally, this would share the implementation with 00499 // AsmPrinter::EmitDwarfRegOpPiece. 00500 void DwarfUnit::addRegisterOpPiece(DIELoc &TheDie, unsigned Reg, 00501 unsigned SizeInBits, unsigned OffsetInBits) { 00502 const TargetRegisterInfo *RI = Asm->TM.getSubtargetImpl()->getRegisterInfo(); 00503 int DWReg = RI->getDwarfRegNum(Reg, false); 00504 bool isSubRegister = DWReg < 0; 00505 00506 unsigned Idx = 0; 00507 00508 // Go up the super-register chain until we hit a valid dwarf register number. 00509 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) { 00510 DWReg = RI->getDwarfRegNum(*SR, false); 00511 if (DWReg >= 0) 00512 Idx = RI->getSubRegIndex(*SR, Reg); 00513 } 00514 00515 if (DWReg < 0) { 00516 DEBUG(dbgs() << "Invalid Dwarf register number.\n"); 00517 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop); 00518 return; 00519 } 00520 00521 // Emit register. 00522 if (DWReg < 32) 00523 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg); 00524 else { 00525 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); 00526 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); 00527 } 00528 00529 // Emit mask. 00530 bool isPiece = SizeInBits > 0; 00531 if (isSubRegister || isPiece) { 00532 const unsigned SizeOfByte = 8; 00533 unsigned RegSizeInBits = RI->getSubRegIdxSize(Idx); 00534 unsigned RegOffsetInBits = RI->getSubRegIdxOffset(Idx); 00535 unsigned PieceSizeInBits = std::max(SizeInBits, RegSizeInBits); 00536 unsigned PieceOffsetInBits = OffsetInBits ? OffsetInBits : RegOffsetInBits; 00537 assert(RegSizeInBits >= SizeInBits && "register smaller than value"); 00538 00539 if (RegOffsetInBits != PieceOffsetInBits) { 00540 // Manually shift the value into place, since the DW_OP_piece 00541 // describes the part of the variable, not the position of the 00542 // subregister. 00543 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 00544 addUInt(TheDie, dwarf::DW_FORM_data1, RegOffsetInBits); 00545 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_shr); 00546 } 00547 00548 if (PieceOffsetInBits > 0 || PieceSizeInBits % SizeOfByte) { 00549 assert(PieceSizeInBits > 0 && "piece has zero size"); 00550 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece); 00551 addUInt(TheDie, dwarf::DW_FORM_data1, PieceSizeInBits); 00552 addUInt(TheDie, dwarf::DW_FORM_data1, PieceOffsetInBits); 00553 } else { 00554 assert(PieceSizeInBits > 0 && "piece has zero size"); 00555 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece); 00556 addUInt(TheDie, dwarf::DW_FORM_data1, PieceSizeInBits/SizeOfByte); 00557 } 00558 } 00559 } 00560 00561 /// addRegisterOffset - Add register offset. 00562 void DwarfUnit::addRegisterOffset(DIELoc &TheDie, unsigned Reg, 00563 int64_t Offset) { 00564 const TargetRegisterInfo *RI = Asm->TM.getSubtargetImpl()->getRegisterInfo(); 00565 unsigned DWReg = RI->getDwarfRegNum(Reg, false); 00566 const TargetRegisterInfo *TRI = Asm->TM.getSubtargetImpl()->getRegisterInfo(); 00567 if (Reg == TRI->getFrameRegister(*Asm->MF)) 00568 // If variable offset is based in frame register then use fbreg. 00569 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg); 00570 else if (DWReg < 32) 00571 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg); 00572 else { 00573 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); 00574 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); 00575 } 00576 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset); 00577 } 00578 00579 /// addAddress - Add an address attribute to a die based on the location 00580 /// provided. 00581 void DwarfUnit::addAddress(DIE &Die, dwarf::Attribute Attribute, 00582 const MachineLocation &Location, bool Indirect) { 00583 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 00584 00585 if (Location.isReg() && !Indirect) 00586 addRegisterOpPiece(*Loc, Location.getReg()); 00587 else { 00588 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset()); 00589 if (Indirect && !Location.isReg()) { 00590 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 00591 } 00592 } 00593 00594 // Now attach the location information to the DIE. 00595 addBlock(Die, Attribute, Loc); 00596 } 00597 00598 /// addComplexAddress - Start with the address based on the location provided, 00599 /// and generate the DWARF information necessary to find the actual variable 00600 /// given the extra address information encoded in the DbgVariable, starting 00601 /// from the starting location. Add the DWARF information to the die. 00602 /// 00603 void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE &Die, 00604 dwarf::Attribute Attribute, 00605 const MachineLocation &Location) { 00606 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 00607 unsigned N = DV.getNumAddrElements(); 00608 unsigned i = 0; 00609 if (Location.isReg()) { 00610 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) { 00611 // If first address element is OpPlus then emit 00612 // DW_OP_breg + Offset instead of DW_OP_reg + Offset. 00613 addRegisterOffset(*Loc, Location.getReg(), DV.getAddrElement(1)); 00614 i = 2; 00615 } else if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpDeref) { 00616 addRegisterOpPiece(*Loc, Location.getReg(), 00617 DV.getVariable().getPieceSize(), 00618 DV.getVariable().getPieceOffset()); 00619 i = 3; 00620 } else 00621 addRegisterOpPiece(*Loc, Location.getReg()); 00622 } else 00623 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset()); 00624 00625 for (; i < N; ++i) { 00626 uint64_t Element = DV.getAddrElement(i); 00627 if (Element == DIBuilder::OpPlus) { 00628 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 00629 addUInt(*Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i)); 00630 00631 } else if (Element == DIBuilder::OpDeref) { 00632 if (!Location.isReg()) 00633 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 00634 00635 } else if (Element == DIBuilder::OpPiece) { 00636 const unsigned SizeOfByte = 8; 00637 unsigned PieceOffsetInBits = DV.getAddrElement(++i)*SizeOfByte; 00638 unsigned PieceSizeInBits = DV.getAddrElement(++i)*SizeOfByte; 00639 // Emit DW_OP_bit_piece Size Offset. 00640 assert(PieceSizeInBits > 0 && "piece has zero size"); 00641 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece); 00642 addUInt(*Loc, dwarf::DW_FORM_udata, PieceSizeInBits); 00643 addUInt(*Loc, dwarf::DW_FORM_udata, PieceOffsetInBits); 00644 00645 } else 00646 llvm_unreachable("unknown DIBuilder Opcode"); 00647 } 00648 00649 // Now attach the location information to the DIE. 00650 addBlock(Die, Attribute, Loc); 00651 } 00652 00653 /* Byref variables, in Blocks, are declared by the programmer as "SomeType 00654 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and 00655 gives the variable VarName either the struct, or a pointer to the struct, as 00656 its type. This is necessary for various behind-the-scenes things the 00657 compiler needs to do with by-reference variables in Blocks. 00658 00659 However, as far as the original *programmer* is concerned, the variable 00660 should still have type 'SomeType', as originally declared. 00661 00662 The function getBlockByrefType dives into the __Block_byref_x_VarName 00663 struct to find the original type of the variable, which is then assigned to 00664 the variable's Debug Information Entry as its real type. So far, so good. 00665 However now the debugger will expect the variable VarName to have the type 00666 SomeType. So we need the location attribute for the variable to be an 00667 expression that explains to the debugger how to navigate through the 00668 pointers and struct to find the actual variable of type SomeType. 00669 00670 The following function does just that. We start by getting 00671 the "normal" location for the variable. This will be the location 00672 of either the struct __Block_byref_x_VarName or the pointer to the 00673 struct __Block_byref_x_VarName. 00674 00675 The struct will look something like: 00676 00677 struct __Block_byref_x_VarName { 00678 ... <various fields> 00679 struct __Block_byref_x_VarName *forwarding; 00680 ... <various other fields> 00681 SomeType VarName; 00682 ... <maybe more fields> 00683 }; 00684 00685 If we are given the struct directly (as our starting point) we 00686 need to tell the debugger to: 00687 00688 1). Add the offset of the forwarding field. 00689 00690 2). Follow that pointer to get the real __Block_byref_x_VarName 00691 struct to use (the real one may have been copied onto the heap). 00692 00693 3). Add the offset for the field VarName, to find the actual variable. 00694 00695 If we started with a pointer to the struct, then we need to 00696 dereference that pointer first, before the other steps. 00697 Translating this into DWARF ops, we will need to append the following 00698 to the current location description for the variable: 00699 00700 DW_OP_deref -- optional, if we start with a pointer 00701 DW_OP_plus_uconst <forward_fld_offset> 00702 DW_OP_deref 00703 DW_OP_plus_uconst <varName_fld_offset> 00704 00705 That is what this function does. */ 00706 00707 /// addBlockByrefAddress - Start with the address based on the location 00708 /// provided, and generate the DWARF information necessary to find the 00709 /// actual Block variable (navigating the Block struct) based on the 00710 /// starting location. Add the DWARF information to the die. For 00711 /// more information, read large comment just above here. 00712 /// 00713 void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE &Die, 00714 dwarf::Attribute Attribute, 00715 const MachineLocation &Location) { 00716 DIType Ty = DV.getType(); 00717 DIType TmpTy = Ty; 00718 uint16_t Tag = Ty.getTag(); 00719 bool isPointer = false; 00720 00721 StringRef varName = DV.getName(); 00722 00723 if (Tag == dwarf::DW_TAG_pointer_type) { 00724 DIDerivedType DTy(Ty); 00725 TmpTy = resolve(DTy.getTypeDerivedFrom()); 00726 isPointer = true; 00727 } 00728 00729 DICompositeType blockStruct(TmpTy); 00730 00731 // Find the __forwarding field and the variable field in the __Block_byref 00732 // struct. 00733 DIArray Fields = blockStruct.getElements(); 00734 DIDerivedType varField; 00735 DIDerivedType forwardingField; 00736 00737 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { 00738 DIDerivedType DT(Fields.getElement(i)); 00739 StringRef fieldName = DT.getName(); 00740 if (fieldName == "__forwarding") 00741 forwardingField = DT; 00742 else if (fieldName == varName) 00743 varField = DT; 00744 } 00745 00746 // Get the offsets for the forwarding field and the variable field. 00747 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3; 00748 unsigned varFieldOffset = varField.getOffsetInBits() >> 2; 00749 00750 // Decode the original location, and use that as the start of the byref 00751 // variable's location. 00752 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 00753 00754 if (Location.isReg()) 00755 addRegisterOpPiece(*Loc, Location.getReg()); 00756 else 00757 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset()); 00758 00759 // If we started with a pointer to the __Block_byref... struct, then 00760 // the first thing we need to do is dereference the pointer (DW_OP_deref). 00761 if (isPointer) 00762 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 00763 00764 // Next add the offset for the '__forwarding' field: 00765 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in 00766 // adding the offset if it's 0. 00767 if (forwardingFieldOffset > 0) { 00768 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 00769 addUInt(*Loc, dwarf::DW_FORM_udata, forwardingFieldOffset); 00770 } 00771 00772 // Now dereference the __forwarding field to get to the real __Block_byref 00773 // struct: DW_OP_deref. 00774 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 00775 00776 // Now that we've got the real __Block_byref... struct, add the offset 00777 // for the variable's field to get to the location of the actual variable: 00778 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. 00779 if (varFieldOffset > 0) { 00780 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 00781 addUInt(*Loc, dwarf::DW_FORM_udata, varFieldOffset); 00782 } 00783 00784 // Now attach the location information to the DIE. 00785 addBlock(Die, Attribute, Loc); 00786 } 00787 00788 /// Return true if type encoding is unsigned. 00789 static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) { 00790 DIDerivedType DTy(Ty); 00791 if (DTy.isDerivedType()) { 00792 dwarf::Tag T = (dwarf::Tag)Ty.getTag(); 00793 // Encode pointer constants as unsigned bytes. This is used at least for 00794 // null pointer constant emission. 00795 // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed 00796 // here, but accept them for now due to a bug in SROA producing bogus 00797 // dbg.values. 00798 if (T == dwarf::DW_TAG_pointer_type || 00799 T == dwarf::DW_TAG_ptr_to_member_type || 00800 T == dwarf::DW_TAG_reference_type || 00801 T == dwarf::DW_TAG_rvalue_reference_type) 00802 return true; 00803 assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type || 00804 T == dwarf::DW_TAG_volatile_type || 00805 T == dwarf::DW_TAG_restrict_type || 00806 T == dwarf::DW_TAG_enumeration_type); 00807 if (DITypeRef Deriv = DTy.getTypeDerivedFrom()) 00808 return isUnsignedDIType(DD, DD->resolve(Deriv)); 00809 // FIXME: Enums without a fixed underlying type have unknown signedness 00810 // here, leading to incorrectly emitted constants. 00811 assert(DTy.getTag() == dwarf::DW_TAG_enumeration_type); 00812 return false; 00813 } 00814 00815 DIBasicType BTy(Ty); 00816 assert(BTy.isBasicType()); 00817 unsigned Encoding = BTy.getEncoding(); 00818 assert((Encoding == dwarf::DW_ATE_unsigned || 00819 Encoding == dwarf::DW_ATE_unsigned_char || 00820 Encoding == dwarf::DW_ATE_signed || 00821 Encoding == dwarf::DW_ATE_signed_char || 00822 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean) && 00823 "Unsupported encoding"); 00824 return (Encoding == dwarf::DW_ATE_unsigned || 00825 Encoding == dwarf::DW_ATE_unsigned_char || 00826 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean); 00827 } 00828 00829 /// If this type is derived from a base type then return base type size. 00830 static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) { 00831 unsigned Tag = Ty.getTag(); 00832 00833 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && 00834 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && 00835 Tag != dwarf::DW_TAG_restrict_type) 00836 return Ty.getSizeInBits(); 00837 00838 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom()); 00839 00840 // If this type is not derived from any type or the type is a declaration then 00841 // take conservative approach. 00842 if (!BaseType.isValid() || BaseType.isForwardDecl()) 00843 return Ty.getSizeInBits(); 00844 00845 // If this is a derived type, go ahead and get the base type, unless it's a 00846 // reference then it's just the size of the field. Pointer types have no need 00847 // of this since they're a different type of qualification on the type. 00848 if (BaseType.getTag() == dwarf::DW_TAG_reference_type || 00849 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type) 00850 return Ty.getSizeInBits(); 00851 00852 if (BaseType.isDerivedType()) 00853 return getBaseTypeSize(DD, DIDerivedType(BaseType)); 00854 00855 return BaseType.getSizeInBits(); 00856 } 00857 00858 /// addConstantFPValue - Add constant value entry in variable DIE. 00859 void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) { 00860 assert(MO.isFPImm() && "Invalid machine operand!"); 00861 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 00862 APFloat FPImm = MO.getFPImm()->getValueAPF(); 00863 00864 // Get the raw data form of the floating point. 00865 const APInt FltVal = FPImm.bitcastToAPInt(); 00866 const char *FltPtr = (const char *)FltVal.getRawData(); 00867 00868 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. 00869 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 00870 int Incr = (LittleEndian ? 1 : -1); 00871 int Start = (LittleEndian ? 0 : NumBytes - 1); 00872 int Stop = (LittleEndian ? NumBytes : -1); 00873 00874 // Output the constant to DWARF one byte at a time. 00875 for (; Start != Stop; Start += Incr) 00876 addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]); 00877 00878 addBlock(Die, dwarf::DW_AT_const_value, Block); 00879 } 00880 00881 /// addConstantFPValue - Add constant value entry in variable DIE. 00882 void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) { 00883 // Pass this down to addConstantValue as an unsigned bag of bits. 00884 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true); 00885 } 00886 00887 /// addConstantValue - Add constant value entry in variable DIE. 00888 void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI, DIType Ty) { 00889 addConstantValue(Die, CI->getValue(), Ty); 00890 } 00891 00892 /// addConstantValue - Add constant value entry in variable DIE. 00893 void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO, 00894 DIType Ty) { 00895 assert(MO.isImm() && "Invalid machine operand!"); 00896 00897 addConstantValue(Die, isUnsignedDIType(DD, Ty), MO.getImm()); 00898 } 00899 00900 void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) { 00901 // FIXME: This is a bit conservative/simple - it emits negative values always 00902 // sign extended to 64 bits rather than minimizing the number of bytes. 00903 addUInt(Die, dwarf::DW_AT_const_value, 00904 Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val); 00905 } 00906 00907 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, DIType Ty) { 00908 addConstantValue(Die, Val, isUnsignedDIType(DD, Ty)); 00909 } 00910 00911 // addConstantValue - Add constant value entry in variable DIE. 00912 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) { 00913 unsigned CIBitWidth = Val.getBitWidth(); 00914 if (CIBitWidth <= 64) { 00915 addConstantValue(Die, Unsigned, 00916 Unsigned ? Val.getZExtValue() : Val.getSExtValue()); 00917 return; 00918 } 00919 00920 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 00921 00922 // Get the raw data form of the large APInt. 00923 const uint64_t *Ptr64 = Val.getRawData(); 00924 00925 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. 00926 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 00927 00928 // Output the constant to DWARF one byte at a time. 00929 for (int i = 0; i < NumBytes; i++) { 00930 uint8_t c; 00931 if (LittleEndian) 00932 c = Ptr64[i / 8] >> (8 * (i & 7)); 00933 else 00934 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7)); 00935 addUInt(*Block, dwarf::DW_FORM_data1, c); 00936 } 00937 00938 addBlock(Die, dwarf::DW_AT_const_value, Block); 00939 } 00940 00941 /// addTemplateParams - Add template parameters into buffer. 00942 void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) { 00943 // Add template parameters. 00944 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) { 00945 DIDescriptor Element = TParams.getElement(i); 00946 if (Element.isTemplateTypeParameter()) 00947 constructTemplateTypeParameterDIE(Buffer, 00948 DITemplateTypeParameter(Element)); 00949 else if (Element.isTemplateValueParameter()) 00950 constructTemplateValueParameterDIE(Buffer, 00951 DITemplateValueParameter(Element)); 00952 } 00953 } 00954 00955 /// getOrCreateContextDIE - Get context owner's DIE. 00956 DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) { 00957 if (!Context || Context.isFile()) 00958 return &getUnitDie(); 00959 if (Context.isType()) 00960 return getOrCreateTypeDIE(DIType(Context)); 00961 if (Context.isNameSpace()) 00962 return getOrCreateNameSpace(DINameSpace(Context)); 00963 if (Context.isSubprogram()) 00964 return getOrCreateSubprogramDIE(DISubprogram(Context)); 00965 return getDIE(Context); 00966 } 00967 00968 DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) { 00969 DIScope Context = resolve(Ty.getContext()); 00970 DIE *ContextDIE = getOrCreateContextDIE(Context); 00971 00972 if (DIE *TyDIE = getDIE(Ty)) 00973 return TyDIE; 00974 00975 // Create new type. 00976 DIE &TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty); 00977 00978 constructTypeDIE(TyDIE, Ty); 00979 00980 updateAcceleratorTables(Context, Ty, TyDIE); 00981 return &TyDIE; 00982 } 00983 00984 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the 00985 /// given DIType. 00986 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) { 00987 if (!TyNode) 00988 return nullptr; 00989 00990 DIType Ty(TyNode); 00991 assert(Ty.isType()); 00992 assert(Ty == resolve(Ty.getRef()) && 00993 "type was not uniqued, possible ODR violation."); 00994 00995 // DW_TAG_restrict_type is not supported in DWARF2 00996 if (Ty.getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2) 00997 return getOrCreateTypeDIE(resolve(DIDerivedType(Ty).getTypeDerivedFrom())); 00998 00999 // Construct the context before querying for the existence of the DIE in case 01000 // such construction creates the DIE. 01001 DIScope Context = resolve(Ty.getContext()); 01002 DIE *ContextDIE = getOrCreateContextDIE(Context); 01003 assert(ContextDIE); 01004 01005 if (DIE *TyDIE = getDIE(Ty)) 01006 return TyDIE; 01007 01008 // Create new type. 01009 DIE &TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty); 01010 01011 updateAcceleratorTables(Context, Ty, TyDIE); 01012 01013 if (Ty.isBasicType()) 01014 constructTypeDIE(TyDIE, DIBasicType(Ty)); 01015 else if (Ty.isCompositeType()) { 01016 DICompositeType CTy(Ty); 01017 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl()) 01018 if (MDString *TypeId = CTy.getIdentifier()) { 01019 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy); 01020 // Skip updating the accelerator tables since this is not the full type. 01021 return &TyDIE; 01022 } 01023 constructTypeDIE(TyDIE, CTy); 01024 } else { 01025 assert(Ty.isDerivedType() && "Unknown kind of DIType"); 01026 constructTypeDIE(TyDIE, DIDerivedType(Ty)); 01027 } 01028 01029 return &TyDIE; 01030 } 01031 01032 void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty, 01033 const DIE &TyDIE) { 01034 if (!Ty.getName().empty() && !Ty.isForwardDecl()) { 01035 bool IsImplementation = 0; 01036 if (Ty.isCompositeType()) { 01037 DICompositeType CT(Ty); 01038 // A runtime language of 0 actually means C/C++ and that any 01039 // non-negative value is some version of Objective-C/C++. 01040 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete(); 01041 } 01042 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0; 01043 DD->addAccelType(Ty.getName(), TyDIE, Flags); 01044 01045 if ((!Context || Context.isCompileUnit() || Context.isFile() || 01046 Context.isNameSpace()) && 01047 getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) 01048 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = 01049 &TyDIE; 01050 } 01051 } 01052 01053 /// addType - Add a new type attribute to the specified entity. 01054 void DwarfUnit::addType(DIE &Entity, DIType Ty, dwarf::Attribute Attribute) { 01055 assert(Ty && "Trying to add a type that doesn't exist?"); 01056 01057 // Check for pre-existence. 01058 DIEEntry *Entry = getDIEEntry(Ty); 01059 // If it exists then use the existing value. 01060 if (Entry) { 01061 addDIEEntry(Entity, Attribute, Entry); 01062 return; 01063 } 01064 01065 // Construct type. 01066 DIE *Buffer = getOrCreateTypeDIE(Ty); 01067 01068 // Set up proxy. 01069 Entry = createDIEEntry(*Buffer); 01070 insertDIEEntry(Ty, Entry); 01071 addDIEEntry(Entity, Attribute, Entry); 01072 } 01073 01074 /// addGlobalName - Add a new global name to the compile unit. 01075 void DwarfUnit::addGlobalName(StringRef Name, DIE &Die, DIScope Context) { 01076 if (getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly) 01077 return; 01078 std::string FullName = getParentContextString(Context) + Name.str(); 01079 GlobalNames[FullName] = &Die; 01080 } 01081 01082 /// getParentContextString - Walks the metadata parent chain in a language 01083 /// specific manner (using the compile unit language) and returns 01084 /// it as a string. This is done at the metadata level because DIEs may 01085 /// not currently have been added to the parent context and walking the 01086 /// DIEs looking for names is more expensive than walking the metadata. 01087 std::string DwarfUnit::getParentContextString(DIScope Context) const { 01088 if (!Context) 01089 return ""; 01090 01091 // FIXME: Decide whether to implement this for non-C++ languages. 01092 if (getLanguage() != dwarf::DW_LANG_C_plus_plus) 01093 return ""; 01094 01095 std::string CS; 01096 SmallVector<DIScope, 1> Parents; 01097 while (!Context.isCompileUnit()) { 01098 Parents.push_back(Context); 01099 if (Context.getContext()) 01100 Context = resolve(Context.getContext()); 01101 else 01102 // Structure, etc types will have a NULL context if they're at the top 01103 // level. 01104 break; 01105 } 01106 01107 // Reverse iterate over our list to go from the outermost construct to the 01108 // innermost. 01109 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(), 01110 E = Parents.rend(); 01111 I != E; ++I) { 01112 DIScope Ctx = *I; 01113 StringRef Name = Ctx.getName(); 01114 if (Name.empty() && Ctx.isNameSpace()) 01115 Name = "(anonymous namespace)"; 01116 if (!Name.empty()) { 01117 CS += Name; 01118 CS += "::"; 01119 } 01120 } 01121 return CS; 01122 } 01123 01124 /// constructTypeDIE - Construct basic type die from DIBasicType. 01125 void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) { 01126 // Get core information. 01127 StringRef Name = BTy.getName(); 01128 // Add name if not anonymous or intermediate type. 01129 if (!Name.empty()) 01130 addString(Buffer, dwarf::DW_AT_name, Name); 01131 01132 // An unspecified type only has a name attribute. 01133 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) 01134 return; 01135 01136 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 01137 BTy.getEncoding()); 01138 01139 uint64_t Size = BTy.getSizeInBits() >> 3; 01140 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 01141 } 01142 01143 /// constructTypeDIE - Construct derived type die from DIDerivedType. 01144 void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) { 01145 // Get core information. 01146 StringRef Name = DTy.getName(); 01147 uint64_t Size = DTy.getSizeInBits() >> 3; 01148 uint16_t Tag = Buffer.getTag(); 01149 01150 // Map to main type, void will not have a type. 01151 DIType FromTy = resolve(DTy.getTypeDerivedFrom()); 01152 if (FromTy) 01153 addType(Buffer, FromTy); 01154 01155 // Add name if not anonymous or intermediate type. 01156 if (!Name.empty()) 01157 addString(Buffer, dwarf::DW_AT_name, Name); 01158 01159 // Add size if non-zero (derived types might be zero-sized.) 01160 if (Size && Tag != dwarf::DW_TAG_pointer_type) 01161 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 01162 01163 if (Tag == dwarf::DW_TAG_ptr_to_member_type) 01164 addDIEEntry(Buffer, dwarf::DW_AT_containing_type, 01165 *getOrCreateTypeDIE(resolve(DTy.getClassType()))); 01166 // Add source line info if available and TyDesc is not a forward declaration. 01167 if (!DTy.isForwardDecl()) 01168 addSourceLine(Buffer, DTy); 01169 } 01170 01171 /// constructSubprogramArguments - Construct function argument DIEs. 01172 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeArray Args) { 01173 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { 01174 DIType Ty = resolve(Args.getElement(i)); 01175 if (!Ty) { 01176 assert(i == N-1 && "Unspecified parameter must be the last argument"); 01177 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer); 01178 } else { 01179 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer); 01180 addType(Arg, DIType(Ty)); 01181 if (DIType(Ty).isArtificial()) 01182 addFlag(Arg, dwarf::DW_AT_artificial); 01183 } 01184 } 01185 } 01186 01187 /// constructTypeDIE - Construct type DIE from DICompositeType. 01188 void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) { 01189 // Add name if not anonymous or intermediate type. 01190 StringRef Name = CTy.getName(); 01191 01192 uint64_t Size = CTy.getSizeInBits() >> 3; 01193 uint16_t Tag = Buffer.getTag(); 01194 01195 switch (Tag) { 01196 case dwarf::DW_TAG_array_type: 01197 constructArrayTypeDIE(Buffer, CTy); 01198 break; 01199 case dwarf::DW_TAG_enumeration_type: 01200 constructEnumTypeDIE(Buffer, CTy); 01201 break; 01202 case dwarf::DW_TAG_subroutine_type: { 01203 // Add return type. A void return won't have a type. 01204 DITypeArray Elements = DISubroutineType(CTy).getTypeArray(); 01205 DIType RTy(resolve(Elements.getElement(0))); 01206 if (RTy) 01207 addType(Buffer, RTy); 01208 01209 bool isPrototyped = true; 01210 if (Elements.getNumElements() == 2 && 01211 !Elements.getElement(1)) 01212 isPrototyped = false; 01213 01214 constructSubprogramArguments(Buffer, Elements); 01215 01216 // Add prototype flag if we're dealing with a C language and the 01217 // function has been prototyped. 01218 uint16_t Language = getLanguage(); 01219 if (isPrototyped && 01220 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 01221 Language == dwarf::DW_LANG_ObjC)) 01222 addFlag(Buffer, dwarf::DW_AT_prototyped); 01223 01224 if (CTy.isLValueReference()) 01225 addFlag(Buffer, dwarf::DW_AT_reference); 01226 01227 if (CTy.isRValueReference()) 01228 addFlag(Buffer, dwarf::DW_AT_rvalue_reference); 01229 } break; 01230 case dwarf::DW_TAG_structure_type: 01231 case dwarf::DW_TAG_union_type: 01232 case dwarf::DW_TAG_class_type: { 01233 // Add elements to structure type. 01234 DIArray Elements = CTy.getElements(); 01235 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 01236 DIDescriptor Element = Elements.getElement(i); 01237 if (Element.isSubprogram()) 01238 getOrCreateSubprogramDIE(DISubprogram(Element)); 01239 else if (Element.isDerivedType()) { 01240 DIDerivedType DDTy(Element); 01241 if (DDTy.getTag() == dwarf::DW_TAG_friend) { 01242 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer); 01243 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()), 01244 dwarf::DW_AT_friend); 01245 } else if (DDTy.isStaticMember()) { 01246 getOrCreateStaticMemberDIE(DDTy); 01247 } else { 01248 constructMemberDIE(Buffer, DDTy); 01249 } 01250 } else if (Element.isObjCProperty()) { 01251 DIObjCProperty Property(Element); 01252 DIE &ElemDie = createAndAddDIE(Property.getTag(), Buffer); 01253 StringRef PropertyName = Property.getObjCPropertyName(); 01254 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); 01255 if (Property.getType()) 01256 addType(ElemDie, Property.getType()); 01257 addSourceLine(ElemDie, Property); 01258 StringRef GetterName = Property.getObjCPropertyGetterName(); 01259 if (!GetterName.empty()) 01260 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); 01261 StringRef SetterName = Property.getObjCPropertySetterName(); 01262 if (!SetterName.empty()) 01263 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); 01264 unsigned PropertyAttributes = 0; 01265 if (Property.isReadOnlyObjCProperty()) 01266 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly; 01267 if (Property.isReadWriteObjCProperty()) 01268 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite; 01269 if (Property.isAssignObjCProperty()) 01270 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign; 01271 if (Property.isRetainObjCProperty()) 01272 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain; 01273 if (Property.isCopyObjCProperty()) 01274 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy; 01275 if (Property.isNonAtomicObjCProperty()) 01276 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic; 01277 if (PropertyAttributes) 01278 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None, 01279 PropertyAttributes); 01280 01281 DIEEntry *Entry = getDIEEntry(Element); 01282 if (!Entry) { 01283 Entry = createDIEEntry(ElemDie); 01284 insertDIEEntry(Element, Entry); 01285 } 01286 } else 01287 continue; 01288 } 01289 01290 if (CTy.isAppleBlockExtension()) 01291 addFlag(Buffer, dwarf::DW_AT_APPLE_block); 01292 01293 DICompositeType ContainingType(resolve(CTy.getContainingType())); 01294 if (ContainingType) 01295 addDIEEntry(Buffer, dwarf::DW_AT_containing_type, 01296 *getOrCreateTypeDIE(ContainingType)); 01297 01298 if (CTy.isObjcClassComplete()) 01299 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type); 01300 01301 // Add template parameters to a class, structure or union types. 01302 // FIXME: The support isn't in the metadata for this yet. 01303 if (Tag == dwarf::DW_TAG_class_type || 01304 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) 01305 addTemplateParams(Buffer, CTy.getTemplateParams()); 01306 01307 break; 01308 } 01309 default: 01310 break; 01311 } 01312 01313 // Add name if not anonymous or intermediate type. 01314 if (!Name.empty()) 01315 addString(Buffer, dwarf::DW_AT_name, Name); 01316 01317 if (Tag == dwarf::DW_TAG_enumeration_type || 01318 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type || 01319 Tag == dwarf::DW_TAG_union_type) { 01320 // Add size if non-zero (derived types might be zero-sized.) 01321 // TODO: Do we care about size for enum forward declarations? 01322 if (Size) 01323 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 01324 else if (!CTy.isForwardDecl()) 01325 // Add zero size if it is not a forward declaration. 01326 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0); 01327 01328 // If we're a forward decl, say so. 01329 if (CTy.isForwardDecl()) 01330 addFlag(Buffer, dwarf::DW_AT_declaration); 01331 01332 // Add source line info if available. 01333 if (!CTy.isForwardDecl()) 01334 addSourceLine(Buffer, CTy); 01335 01336 // No harm in adding the runtime language to the declaration. 01337 unsigned RLang = CTy.getRunTimeLang(); 01338 if (RLang) 01339 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1, 01340 RLang); 01341 } 01342 } 01343 01344 /// constructTemplateTypeParameterDIE - Construct new DIE for the given 01345 /// DITemplateTypeParameter. 01346 void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer, 01347 DITemplateTypeParameter TP) { 01348 DIE &ParamDIE = 01349 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer); 01350 // Add the type if it exists, it could be void and therefore no type. 01351 if (TP.getType()) 01352 addType(ParamDIE, resolve(TP.getType())); 01353 if (!TP.getName().empty()) 01354 addString(ParamDIE, dwarf::DW_AT_name, TP.getName()); 01355 } 01356 01357 /// constructTemplateValueParameterDIE - Construct new DIE for the given 01358 /// DITemplateValueParameter. 01359 void 01360 DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer, 01361 DITemplateValueParameter VP) { 01362 DIE &ParamDIE = createAndAddDIE(VP.getTag(), Buffer); 01363 01364 // Add the type if there is one, template template and template parameter 01365 // packs will not have a type. 01366 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter) 01367 addType(ParamDIE, resolve(VP.getType())); 01368 if (!VP.getName().empty()) 01369 addString(ParamDIE, dwarf::DW_AT_name, VP.getName()); 01370 if (Value *Val = VP.getValue()) { 01371 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) 01372 addConstantValue(ParamDIE, CI, resolve(VP.getType())); 01373 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) { 01374 // For declaration non-type template parameters (such as global values and 01375 // functions) 01376 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 01377 addOpAddress(*Loc, Asm->getSymbol(GV)); 01378 // Emit DW_OP_stack_value to use the address as the immediate value of the 01379 // parameter, rather than a pointer to it. 01380 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); 01381 addBlock(ParamDIE, dwarf::DW_AT_location, Loc); 01382 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) { 01383 assert(isa<MDString>(Val)); 01384 addString(ParamDIE, dwarf::DW_AT_GNU_template_name, 01385 cast<MDString>(Val)->getString()); 01386 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) { 01387 assert(isa<MDNode>(Val)); 01388 DIArray A(cast<MDNode>(Val)); 01389 addTemplateParams(ParamDIE, A); 01390 } 01391 } 01392 } 01393 01394 /// getOrCreateNameSpace - Create a DIE for DINameSpace. 01395 DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) { 01396 // Construct the context before querying for the existence of the DIE in case 01397 // such construction creates the DIE. 01398 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext()); 01399 01400 if (DIE *NDie = getDIE(NS)) 01401 return NDie; 01402 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS); 01403 01404 StringRef Name = NS.getName(); 01405 if (!Name.empty()) 01406 addString(NDie, dwarf::DW_AT_name, NS.getName()); 01407 else 01408 Name = "(anonymous namespace)"; 01409 DD->addAccelNamespace(Name, NDie); 01410 addGlobalName(Name, NDie, NS.getContext()); 01411 addSourceLine(NDie, NS); 01412 return &NDie; 01413 } 01414 01415 /// getOrCreateSubprogramDIE - Create new DIE using SP. 01416 DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) { 01417 // Construct the context before querying for the existence of the DIE in case 01418 // such construction creates the DIE (as is the case for member function 01419 // declarations). 01420 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext())); 01421 01422 if (DIE *SPDie = getDIE(SP)) 01423 return SPDie; 01424 01425 if (DISubprogram SPDecl = SP.getFunctionDeclaration()) { 01426 // Add subprogram definitions to the CU die directly. 01427 ContextDIE = &getUnitDie(); 01428 // Build the decl now to ensure it precedes the definition. 01429 getOrCreateSubprogramDIE(SPDecl); 01430 } 01431 01432 // DW_TAG_inlined_subroutine may refer to this DIE. 01433 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP); 01434 01435 // Stop here and fill this in later, depending on whether or not this 01436 // subprogram turns out to have inlined instances or not. 01437 if (SP.isDefinition()) 01438 return &SPDie; 01439 01440 applySubprogramAttributes(SP, SPDie); 01441 return &SPDie; 01442 } 01443 01444 void DwarfUnit::applySubprogramAttributesToDefinition(DISubprogram SP, DIE &SPDie) { 01445 DISubprogram SPDecl = SP.getFunctionDeclaration(); 01446 DIScope Context = resolve(SPDecl ? SPDecl.getContext() : SP.getContext()); 01447 applySubprogramAttributes(SP, SPDie); 01448 addGlobalName(SP.getName(), SPDie, Context); 01449 } 01450 01451 void DwarfUnit::applySubprogramAttributes(DISubprogram SP, DIE &SPDie) { 01452 DIE *DeclDie = nullptr; 01453 StringRef DeclLinkageName; 01454 if (DISubprogram SPDecl = SP.getFunctionDeclaration()) { 01455 DeclDie = getDIE(SPDecl); 01456 assert(DeclDie && "This DIE should've already been constructed when the " 01457 "definition DIE was created in " 01458 "getOrCreateSubprogramDIE"); 01459 DeclLinkageName = SPDecl.getLinkageName(); 01460 } 01461 01462 // Add function template parameters. 01463 addTemplateParams(SPDie, SP.getTemplateParams()); 01464 01465 // Add the linkage name if we have one and it isn't in the Decl. 01466 StringRef LinkageName = SP.getLinkageName(); 01467 assert(((LinkageName.empty() || DeclLinkageName.empty()) || 01468 LinkageName == DeclLinkageName) && 01469 "decl has a linkage name and it is different"); 01470 if (!LinkageName.empty() && DeclLinkageName.empty()) 01471 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, 01472 GlobalValue::getRealLinkageName(LinkageName)); 01473 01474 if (DeclDie) { 01475 // Refer to the function declaration where all the other attributes will be 01476 // found. 01477 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie); 01478 return; 01479 } 01480 01481 // Constructors and operators for anonymous aggregates do not have names. 01482 if (!SP.getName().empty()) 01483 addString(SPDie, dwarf::DW_AT_name, SP.getName()); 01484 01485 addSourceLine(SPDie, SP); 01486 01487 // Add the prototype if we have a prototype and we have a C like 01488 // language. 01489 uint16_t Language = getLanguage(); 01490 if (SP.isPrototyped() && 01491 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 01492 Language == dwarf::DW_LANG_ObjC)) 01493 addFlag(SPDie, dwarf::DW_AT_prototyped); 01494 01495 DISubroutineType SPTy = SP.getType(); 01496 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type && 01497 "the type of a subprogram should be a subroutine"); 01498 01499 DITypeArray Args = SPTy.getTypeArray(); 01500 // Add a return type. If this is a type like a C/C++ void type we don't add a 01501 // return type. 01502 if (resolve(Args.getElement(0))) 01503 addType(SPDie, DIType(resolve(Args.getElement(0)))); 01504 01505 unsigned VK = SP.getVirtuality(); 01506 if (VK) { 01507 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); 01508 DIELoc *Block = getDIELoc(); 01509 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 01510 addUInt(*Block, dwarf::DW_FORM_udata, SP.getVirtualIndex()); 01511 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block); 01512 ContainingTypeMap.insert( 01513 std::make_pair(&SPDie, resolve(SP.getContainingType()))); 01514 } 01515 01516 if (!SP.isDefinition()) { 01517 addFlag(SPDie, dwarf::DW_AT_declaration); 01518 01519 // Add arguments. Do not add arguments for subprogram definition. They will 01520 // be handled while processing variables. 01521 constructSubprogramArguments(SPDie, Args); 01522 } 01523 01524 if (SP.isArtificial()) 01525 addFlag(SPDie, dwarf::DW_AT_artificial); 01526 01527 if (!SP.isLocalToUnit()) 01528 addFlag(SPDie, dwarf::DW_AT_external); 01529 01530 if (SP.isOptimized()) 01531 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); 01532 01533 if (unsigned isa = Asm->getISAEncoding()) { 01534 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); 01535 } 01536 01537 if (SP.isLValueReference()) 01538 addFlag(SPDie, dwarf::DW_AT_reference); 01539 01540 if (SP.isRValueReference()) 01541 addFlag(SPDie, dwarf::DW_AT_rvalue_reference); 01542 01543 if (SP.isProtected()) 01544 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 01545 dwarf::DW_ACCESS_protected); 01546 else if (SP.isPrivate()) 01547 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 01548 dwarf::DW_ACCESS_private); 01549 else if (SP.isPublic()) 01550 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 01551 dwarf::DW_ACCESS_public); 01552 01553 if (SP.isExplicit()) 01554 addFlag(SPDie, dwarf::DW_AT_explicit); 01555 } 01556 01557 void DwarfUnit::applyVariableAttributes(const DbgVariable &Var, 01558 DIE &VariableDie) { 01559 StringRef Name = Var.getName(); 01560 if (!Name.empty()) 01561 addString(VariableDie, dwarf::DW_AT_name, Name); 01562 addSourceLine(VariableDie, Var.getVariable()); 01563 addType(VariableDie, Var.getType()); 01564 if (Var.isArtificial()) 01565 addFlag(VariableDie, dwarf::DW_AT_artificial); 01566 } 01567 01568 // Return const expression if value is a GEP to access merged global 01569 // constant. e.g. 01570 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) 01571 static const ConstantExpr *getMergedGlobalExpr(const Value *V) { 01572 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V); 01573 if (!CE || CE->getNumOperands() != 3 || 01574 CE->getOpcode() != Instruction::GetElementPtr) 01575 return nullptr; 01576 01577 // First operand points to a global struct. 01578 Value *Ptr = CE->getOperand(0); 01579 if (!isa<GlobalValue>(Ptr) || 01580 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType())) 01581 return nullptr; 01582 01583 // Second operand is zero. 01584 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1)); 01585 if (!CI || !CI->isZero()) 01586 return nullptr; 01587 01588 // Third operand is offset. 01589 if (!isa<ConstantInt>(CE->getOperand(2))) 01590 return nullptr; 01591 01592 return CE; 01593 } 01594 01595 /// createGlobalVariableDIE - create global variable DIE. 01596 void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) { 01597 // Check for pre-existence. 01598 if (getDIE(GV)) 01599 return; 01600 01601 assert(GV.isGlobalVariable()); 01602 01603 DIScope GVContext = GV.getContext(); 01604 DIType GTy = DD->resolve(GV.getType()); 01605 01606 // If this is a static data member definition, some attributes belong 01607 // to the declaration DIE. 01608 DIE *VariableDIE = nullptr; 01609 bool IsStaticMember = false; 01610 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration(); 01611 if (SDMDecl.Verify()) { 01612 assert(SDMDecl.isStaticMember() && "Expected static member decl"); 01613 // We need the declaration DIE that is in the static member's class. 01614 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl); 01615 IsStaticMember = true; 01616 } 01617 01618 // If this is not a static data member definition, create the variable 01619 // DIE and add the initial set of attributes to it. 01620 if (!VariableDIE) { 01621 // Construct the context before querying for the existence of the DIE in 01622 // case such construction creates the DIE. 01623 DIE *ContextDIE = getOrCreateContextDIE(GVContext); 01624 01625 // Add to map. 01626 VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV); 01627 01628 // Add name and type. 01629 addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); 01630 addType(*VariableDIE, GTy); 01631 01632 // Add scoping info. 01633 if (!GV.isLocalToUnit()) 01634 addFlag(*VariableDIE, dwarf::DW_AT_external); 01635 01636 // Add line number info. 01637 addSourceLine(*VariableDIE, GV); 01638 } 01639 01640 // Add location. 01641 bool addToAccelTable = false; 01642 DIE *VariableSpecDIE = nullptr; 01643 bool isGlobalVariable = GV.getGlobal() != nullptr; 01644 if (isGlobalVariable) { 01645 addToAccelTable = true; 01646 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 01647 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal()); 01648 if (GV.getGlobal()->isThreadLocal()) { 01649 // FIXME: Make this work with -gsplit-dwarf. 01650 unsigned PointerSize = Asm->getDataLayout().getPointerSize(); 01651 assert((PointerSize == 4 || PointerSize == 8) && 01652 "Add support for other sizes if necessary"); 01653 // Based on GCC's support for TLS: 01654 if (!DD->useSplitDwarf()) { 01655 // 1) Start with a constNu of the appropriate pointer size 01656 addUInt(*Loc, dwarf::DW_FORM_data1, 01657 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u); 01658 // 2) containing the (relocated) offset of the TLS variable 01659 // within the module's TLS block. 01660 addExpr(*Loc, dwarf::DW_FORM_udata, 01661 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); 01662 } else { 01663 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); 01664 addUInt(*Loc, dwarf::DW_FORM_udata, 01665 DD->getAddressPool().getIndex(Sym, /* TLS */ true)); 01666 } 01667 // 3) followed by a custom OP to make the debugger do a TLS lookup. 01668 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address); 01669 } else { 01670 DD->addArangeLabel(SymbolCU(this, Sym)); 01671 addOpAddress(*Loc, Sym); 01672 } 01673 // Do not create specification DIE if context is either compile unit 01674 // or a subprogram. 01675 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() && 01676 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) { 01677 // Create specification DIE. 01678 VariableSpecDIE = &createAndAddDIE(dwarf::DW_TAG_variable, UnitDie); 01679 addDIEEntry(*VariableSpecDIE, dwarf::DW_AT_specification, *VariableDIE); 01680 addBlock(*VariableSpecDIE, dwarf::DW_AT_location, Loc); 01681 // A static member's declaration is already flagged as such. 01682 if (!SDMDecl.Verify()) 01683 addFlag(*VariableDIE, dwarf::DW_AT_declaration); 01684 } else { 01685 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); 01686 } 01687 // Add the linkage name. 01688 StringRef LinkageName = GV.getLinkageName(); 01689 if (!LinkageName.empty()) 01690 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include: 01691 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and 01692 // TAG_variable. 01693 addString(IsStaticMember && VariableSpecDIE ? *VariableSpecDIE 01694 : *VariableDIE, 01695 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name 01696 : dwarf::DW_AT_MIPS_linkage_name, 01697 GlobalValue::getRealLinkageName(LinkageName)); 01698 } else if (const ConstantInt *CI = 01699 dyn_cast_or_null<ConstantInt>(GV.getConstant())) { 01700 // AT_const_value was added when the static member was created. To avoid 01701 // emitting AT_const_value multiple times, we only add AT_const_value when 01702 // it is not a static member. 01703 if (!IsStaticMember) 01704 addConstantValue(*VariableDIE, CI, GTy); 01705 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) { 01706 addToAccelTable = true; 01707 // GV is a merged global. 01708 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 01709 Value *Ptr = CE->getOperand(0); 01710 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr)); 01711 DD->addArangeLabel(SymbolCU(this, Sym)); 01712 addOpAddress(*Loc, Sym); 01713 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 01714 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end()); 01715 addUInt(*Loc, dwarf::DW_FORM_udata, 01716 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); 01717 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 01718 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); 01719 } 01720 01721 if (addToAccelTable) { 01722 DIE &AddrDIE = VariableSpecDIE ? *VariableSpecDIE : *VariableDIE; 01723 DD->addAccelName(GV.getName(), AddrDIE); 01724 01725 // If the linkage name is different than the name, go ahead and output 01726 // that as well into the name table. 01727 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) 01728 DD->addAccelName(GV.getLinkageName(), AddrDIE); 01729 } 01730 01731 addGlobalName(GV.getName(), VariableSpecDIE ? *VariableSpecDIE : *VariableDIE, 01732 GV.getContext()); 01733 } 01734 01735 /// constructSubrangeDIE - Construct subrange DIE from DISubrange. 01736 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) { 01737 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer); 01738 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy); 01739 01740 // The LowerBound value defines the lower bounds which is typically zero for 01741 // C/C++. The Count value is the number of elements. Values are 64 bit. If 01742 // Count == -1 then the array is unbounded and we do not emit 01743 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and 01744 // Count == 0, then the array has zero elements in which case we do not emit 01745 // an upper bound. 01746 int64_t LowerBound = SR.getLo(); 01747 int64_t DefaultLowerBound = getDefaultLowerBound(); 01748 int64_t Count = SR.getCount(); 01749 01750 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound) 01751 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound); 01752 01753 if (Count != -1 && Count != 0) 01754 // FIXME: An unbounded array should reference the expression that defines 01755 // the array. 01756 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None, 01757 LowerBound + Count - 1); 01758 } 01759 01760 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. 01761 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) { 01762 if (CTy.isVector()) 01763 addFlag(Buffer, dwarf::DW_AT_GNU_vector); 01764 01765 // Emit the element type. 01766 addType(Buffer, resolve(CTy.getTypeDerivedFrom())); 01767 01768 // Get an anonymous type for index type. 01769 // FIXME: This type should be passed down from the front end 01770 // as different languages may have different sizes for indexes. 01771 DIE *IdxTy = getIndexTyDie(); 01772 if (!IdxTy) { 01773 // Construct an integer type to use for indexes. 01774 IdxTy = &createAndAddDIE(dwarf::DW_TAG_base_type, UnitDie); 01775 addString(*IdxTy, dwarf::DW_AT_name, "sizetype"); 01776 addUInt(*IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int64_t)); 01777 addUInt(*IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 01778 dwarf::DW_ATE_unsigned); 01779 setIndexTyDie(IdxTy); 01780 } 01781 01782 // Add subranges to array type. 01783 DIArray Elements = CTy.getElements(); 01784 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 01785 DIDescriptor Element = Elements.getElement(i); 01786 if (Element.getTag() == dwarf::DW_TAG_subrange_type) 01787 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy); 01788 } 01789 } 01790 01791 /// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType. 01792 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) { 01793 DIArray Elements = CTy.getElements(); 01794 01795 // Add enumerators to enumeration type. 01796 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 01797 DIEnumerator Enum(Elements.getElement(i)); 01798 if (Enum.isEnumerator()) { 01799 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer); 01800 StringRef Name = Enum.getName(); 01801 addString(Enumerator, dwarf::DW_AT_name, Name); 01802 int64_t Value = Enum.getEnumValue(); 01803 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, 01804 Value); 01805 } 01806 } 01807 DIType DTy = resolve(CTy.getTypeDerivedFrom()); 01808 if (DTy) { 01809 addType(Buffer, DTy); 01810 addFlag(Buffer, dwarf::DW_AT_enum_class); 01811 } 01812 } 01813 01814 /// constructContainingTypeDIEs - Construct DIEs for types that contain 01815 /// vtables. 01816 void DwarfUnit::constructContainingTypeDIEs() { 01817 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(), 01818 CE = ContainingTypeMap.end(); 01819 CI != CE; ++CI) { 01820 DIE &SPDie = *CI->first; 01821 DIDescriptor D(CI->second); 01822 if (!D) 01823 continue; 01824 DIE *NDie = getDIE(D); 01825 if (!NDie) 01826 continue; 01827 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie); 01828 } 01829 } 01830 01831 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 01832 std::unique_ptr<DIE> DwarfUnit::constructVariableDIE(DbgVariable &DV, 01833 bool Abstract) { 01834 auto D = constructVariableDIEImpl(DV, Abstract); 01835 DV.setDIE(*D); 01836 return D; 01837 } 01838 01839 std::unique_ptr<DIE> DwarfUnit::constructVariableDIEImpl(const DbgVariable &DV, 01840 bool Abstract) { 01841 // Define variable debug information entry. 01842 auto VariableDie = make_unique<DIE>(DV.getTag()); 01843 01844 if (Abstract) { 01845 applyVariableAttributes(DV, *VariableDie); 01846 return VariableDie; 01847 } 01848 01849 // Add variable address. 01850 01851 unsigned Offset = DV.getDotDebugLocOffset(); 01852 if (Offset != ~0U) { 01853 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset); 01854 return VariableDie; 01855 } 01856 01857 // Check if variable is described by a DBG_VALUE instruction. 01858 if (const MachineInstr *DVInsn = DV.getMInsn()) { 01859 assert(DVInsn->getNumOperands() == 3); 01860 if (DVInsn->getOperand(0).isReg()) { 01861 const MachineOperand RegOp = DVInsn->getOperand(0); 01862 // If the second operand is an immediate, this is an indirect value. 01863 if (DVInsn->getOperand(1).isImm()) { 01864 MachineLocation Location(RegOp.getReg(), 01865 DVInsn->getOperand(1).getImm()); 01866 addVariableAddress(DV, *VariableDie, Location); 01867 } else if (RegOp.getReg()) 01868 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg())); 01869 } else if (DVInsn->getOperand(0).isImm()) 01870 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType()); 01871 else if (DVInsn->getOperand(0).isFPImm()) 01872 addConstantFPValue(*VariableDie, DVInsn->getOperand(0)); 01873 else if (DVInsn->getOperand(0).isCImm()) 01874 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(), 01875 DV.getType()); 01876 01877 return VariableDie; 01878 } 01879 01880 // .. else use frame index. 01881 int FI = DV.getFrameIndex(); 01882 if (FI != ~0) { 01883 unsigned FrameReg = 0; 01884 const TargetFrameLowering *TFI = 01885 Asm->TM.getSubtargetImpl()->getFrameLowering(); 01886 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); 01887 MachineLocation Location(FrameReg, Offset); 01888 addVariableAddress(DV, *VariableDie, Location); 01889 } 01890 01891 return VariableDie; 01892 } 01893 01894 /// constructMemberDIE - Construct member DIE from DIDerivedType. 01895 void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) { 01896 DIE &MemberDie = createAndAddDIE(DT.getTag(), Buffer); 01897 StringRef Name = DT.getName(); 01898 if (!Name.empty()) 01899 addString(MemberDie, dwarf::DW_AT_name, Name); 01900 01901 addType(MemberDie, resolve(DT.getTypeDerivedFrom())); 01902 01903 addSourceLine(MemberDie, DT); 01904 01905 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) { 01906 01907 // For C++, virtual base classes are not at fixed offset. Use following 01908 // expression to extract appropriate offset from vtable. 01909 // BaseAddr = ObAddr + *((*ObAddr) - Offset) 01910 01911 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc(); 01912 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); 01913 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 01914 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 01915 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits()); 01916 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); 01917 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 01918 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 01919 01920 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie); 01921 } else { 01922 uint64_t Size = DT.getSizeInBits(); 01923 uint64_t FieldSize = getBaseTypeSize(DD, DT); 01924 uint64_t OffsetInBytes; 01925 01926 if (Size != FieldSize) { 01927 // Handle bitfield, assume bytes are 8 bits. 01928 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8); 01929 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size); 01930 01931 uint64_t Offset = DT.getOffsetInBits(); 01932 uint64_t AlignMask = ~(DT.getAlignInBits() - 1); 01933 uint64_t HiMark = (Offset + FieldSize) & AlignMask; 01934 uint64_t FieldOffset = (HiMark - FieldSize); 01935 Offset -= FieldOffset; 01936 01937 // Maybe we need to work from the other end. 01938 if (Asm->getDataLayout().isLittleEndian()) 01939 Offset = FieldSize - (Offset + Size); 01940 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset); 01941 01942 // Here DW_AT_data_member_location points to the anonymous 01943 // field that includes this bit field. 01944 OffsetInBytes = FieldOffset >> 3; 01945 } else 01946 // This is not a bitfield. 01947 OffsetInBytes = DT.getOffsetInBits() >> 3; 01948 01949 if (DD->getDwarfVersion() <= 2) { 01950 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc(); 01951 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 01952 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes); 01953 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie); 01954 } else 01955 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, 01956 OffsetInBytes); 01957 } 01958 01959 if (DT.isProtected()) 01960 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 01961 dwarf::DW_ACCESS_protected); 01962 else if (DT.isPrivate()) 01963 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 01964 dwarf::DW_ACCESS_private); 01965 // Otherwise C++ member and base classes are considered public. 01966 else if (DT.isPublic()) 01967 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 01968 dwarf::DW_ACCESS_public); 01969 if (DT.isVirtual()) 01970 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, 01971 dwarf::DW_VIRTUALITY_virtual); 01972 01973 // Objective-C properties. 01974 if (MDNode *PNode = DT.getObjCProperty()) 01975 if (DIEEntry *PropertyDie = getDIEEntry(PNode)) 01976 MemberDie.addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4, 01977 PropertyDie); 01978 01979 if (DT.isArtificial()) 01980 addFlag(MemberDie, dwarf::DW_AT_artificial); 01981 } 01982 01983 /// getOrCreateStaticMemberDIE - Create new DIE for C++ static member. 01984 DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) { 01985 if (!DT.Verify()) 01986 return nullptr; 01987 01988 // Construct the context before querying for the existence of the DIE in case 01989 // such construction creates the DIE. 01990 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext())); 01991 assert(dwarf::isType(ContextDIE->getTag()) && 01992 "Static member should belong to a type."); 01993 01994 if (DIE *StaticMemberDIE = getDIE(DT)) 01995 return StaticMemberDIE; 01996 01997 DIE &StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT); 01998 01999 DIType Ty = resolve(DT.getTypeDerivedFrom()); 02000 02001 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName()); 02002 addType(StaticMemberDIE, Ty); 02003 addSourceLine(StaticMemberDIE, DT); 02004 addFlag(StaticMemberDIE, dwarf::DW_AT_external); 02005 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration); 02006 02007 // FIXME: We could omit private if the parent is a class_type, and 02008 // public if the parent is something else. 02009 if (DT.isProtected()) 02010 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 02011 dwarf::DW_ACCESS_protected); 02012 else if (DT.isPrivate()) 02013 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 02014 dwarf::DW_ACCESS_private); 02015 else if (DT.isPublic()) 02016 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 02017 dwarf::DW_ACCESS_public); 02018 02019 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant())) 02020 addConstantValue(StaticMemberDIE, CI, Ty); 02021 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant())) 02022 addConstantFPValue(StaticMemberDIE, CFP); 02023 02024 return &StaticMemberDIE; 02025 } 02026 02027 void DwarfUnit::emitHeader(const MCSymbol *ASectionSym) const { 02028 Asm->OutStreamer.AddComment("DWARF version number"); 02029 Asm->EmitInt16(DD->getDwarfVersion()); 02030 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section"); 02031 // We share one abbreviations table across all units so it's always at the 02032 // start of the section. Use a relocatable offset where needed to ensure 02033 // linking doesn't invalidate that offset. 02034 if (ASectionSym) 02035 Asm->EmitSectionOffset(ASectionSym, ASectionSym); 02036 else 02037 // Use a constant value when no symbol is provided. 02038 Asm->EmitInt32(0); 02039 Asm->OutStreamer.AddComment("Address Size (in bytes)"); 02040 Asm->EmitInt8(Asm->getDataLayout().getPointerSize()); 02041 } 02042 02043 void DwarfCompileUnit::addRange(RangeSpan Range) { 02044 // Only add a range for this unit if we're emitting full debug. 02045 if (getCUNode().getEmissionKind() == DIBuilder::FullDebug) { 02046 bool SameAsPrevCU = this == DD->getPrevCU(); 02047 DD->setPrevCU(this); 02048 // If we have no current ranges just add the range and return, otherwise, 02049 // check the current section and CU against the previous section and CU we 02050 // emitted into and the subprogram was contained within. If these are the 02051 // same then extend our current range, otherwise add this as a new range. 02052 if (CURanges.empty() || !SameAsPrevCU || 02053 (&CURanges.back().getEnd()->getSection() != 02054 &Range.getEnd()->getSection())) { 02055 CURanges.push_back(Range); 02056 return; 02057 } 02058 02059 CURanges.back().setEnd(Range.getEnd()); 02060 } 02061 } 02062 02063 void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) { 02064 // Define start line table label for each Compile Unit. 02065 MCSymbol *LineTableStartSym = 02066 Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID()); 02067 02068 stmtListIndex = UnitDie.getValues().size(); 02069 02070 // DW_AT_stmt_list is a offset of line number information for this 02071 // compile unit in debug_line section. For split dwarf this is 02072 // left in the skeleton CU and so not included. 02073 // The line table entries are not always emitted in assembly, so it 02074 // is not okay to use line_table_start here. 02075 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 02076 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym); 02077 else 02078 addSectionDelta(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym, 02079 DwarfLineSectionSym); 02080 } 02081 02082 void DwarfCompileUnit::applyStmtList(DIE &D) { 02083 D.addValue(dwarf::DW_AT_stmt_list, 02084 UnitDie.getAbbrev().getData()[stmtListIndex].getForm(), 02085 UnitDie.getValues()[stmtListIndex]); 02086 } 02087 02088 void DwarfTypeUnit::emitHeader(const MCSymbol *ASectionSym) const { 02089 DwarfUnit::emitHeader(ASectionSym); 02090 Asm->OutStreamer.AddComment("Type Signature"); 02091 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature)); 02092 Asm->OutStreamer.AddComment("Type DIE Offset"); 02093 // In a skeleton type unit there is no type DIE so emit a zero offset. 02094 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0, 02095 sizeof(Ty->getOffset())); 02096 } 02097 02098 void DwarfTypeUnit::initSection(const MCSection *Section) { 02099 assert(!this->Section); 02100 this->Section = Section; 02101 // Since each type unit is contained in its own COMDAT section, the begin 02102 // label and the section label are the same. Using the begin label emission in 02103 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but 02104 // the only other alternative of lazily constructing start-of-section labels 02105 // and storing a mapping in DwarfDebug (or AsmPrinter). 02106 this->SectionSym = this->LabelBegin = 02107 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID()); 02108 this->LabelEnd = 02109 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID()); 02110 }