DwarfUnit.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
  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 constructing a dwarf compile unit.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "DwarfUnit.h"
  14. #include "DwarfAccelTable.h"
  15. #include "DwarfCompileUnit.h"
  16. #include "DwarfDebug.h"
  17. #include "DwarfExpression.h"
  18. #include "llvm/ADT/APFloat.h"
  19. #include "llvm/IR/Constants.h"
  20. #include "llvm/IR/DIBuilder.h"
  21. #include "llvm/IR/DataLayout.h"
  22. #include "llvm/IR/GlobalVariable.h"
  23. #include "llvm/IR/Instructions.h"
  24. #include "llvm/IR/Mangler.h"
  25. #include "llvm/MC/MCAsmInfo.h"
  26. #include "llvm/MC/MCContext.h"
  27. #include "llvm/MC/MCSection.h"
  28. #include "llvm/MC/MCStreamer.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Target/TargetFrameLowering.h"
  31. #include "llvm/Target/TargetLoweringObjectFile.h"
  32. #include "llvm/Target/TargetMachine.h"
  33. #include "llvm/Target/TargetRegisterInfo.h"
  34. #include "llvm/Target/TargetSubtargetInfo.h"
  35. using namespace llvm;
  36. #define DEBUG_TYPE "dwarfdebug"
  37. static cl::opt<bool>
  38. GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
  39. cl::desc("Generate DWARF4 type units."),
  40. cl::init(false));
  41. void DIEDwarfExpression::EmitOp(uint8_t Op, const char* Comment) {
  42. DU.addUInt(DIE, dwarf::DW_FORM_data1, Op);
  43. }
  44. void DIEDwarfExpression::EmitSigned(int Value) {
  45. DU.addSInt(DIE, dwarf::DW_FORM_sdata, Value);
  46. }
  47. void DIEDwarfExpression::EmitUnsigned(unsigned Value) {
  48. DU.addUInt(DIE, dwarf::DW_FORM_udata, Value);
  49. }
  50. bool DIEDwarfExpression::isFrameRegister(unsigned MachineReg) {
  51. return MachineReg == getTRI()->getFrameRegister(*AP.MF);
  52. }
  53. /// Unit - Unit constructor.
  54. DwarfUnit::DwarfUnit(unsigned UID, dwarf::Tag UnitTag, DICompileUnit Node,
  55. AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
  56. : UniqueID(UID), CUNode(Node), UnitDie(UnitTag), DebugInfoOffset(0), Asm(A),
  57. DD(DW), DU(DWU), IndexTyDie(nullptr), Section(nullptr) {
  58. assert(UnitTag == dwarf::DW_TAG_compile_unit ||
  59. UnitTag == dwarf::DW_TAG_type_unit);
  60. DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
  61. }
  62. DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DwarfCompileUnit &CU, AsmPrinter *A,
  63. DwarfDebug *DW, DwarfFile *DWU,
  64. MCDwarfDwoLineTable *SplitLineTable)
  65. : DwarfUnit(UID, dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU),
  66. CU(CU), SplitLineTable(SplitLineTable) {
  67. if (SplitLineTable)
  68. addSectionOffset(UnitDie, dwarf::DW_AT_stmt_list, 0);
  69. }
  70. /// ~Unit - Destructor for compile unit.
  71. DwarfUnit::~DwarfUnit() {
  72. for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
  73. DIEBlocks[j]->~DIEBlock();
  74. for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
  75. DIELocs[j]->~DIELoc();
  76. }
  77. /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
  78. /// information entry.
  79. DIEEntry *DwarfUnit::createDIEEntry(DIE &Entry) {
  80. DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
  81. return Value;
  82. }
  83. /// getDefaultLowerBound - Return the default lower bound for an array. If the
  84. /// DWARF version doesn't handle the language, return -1.
  85. int64_t DwarfUnit::getDefaultLowerBound() const {
  86. switch (getLanguage()) {
  87. default:
  88. break;
  89. case dwarf::DW_LANG_C89:
  90. case dwarf::DW_LANG_C99:
  91. case dwarf::DW_LANG_C:
  92. case dwarf::DW_LANG_C_plus_plus:
  93. case dwarf::DW_LANG_ObjC:
  94. case dwarf::DW_LANG_ObjC_plus_plus:
  95. return 0;
  96. case dwarf::DW_LANG_Fortran77:
  97. case dwarf::DW_LANG_Fortran90:
  98. case dwarf::DW_LANG_Fortran95:
  99. return 1;
  100. // The languages below have valid values only if the DWARF version >= 4.
  101. case dwarf::DW_LANG_Java:
  102. case dwarf::DW_LANG_Python:
  103. case dwarf::DW_LANG_UPC:
  104. case dwarf::DW_LANG_D:
  105. if (dwarf::DWARF_VERSION >= 4)
  106. return 0;
  107. break;
  108. case dwarf::DW_LANG_Ada83:
  109. case dwarf::DW_LANG_Ada95:
  110. case dwarf::DW_LANG_Cobol74:
  111. case dwarf::DW_LANG_Cobol85:
  112. case dwarf::DW_LANG_Modula2:
  113. case dwarf::DW_LANG_Pascal83:
  114. case dwarf::DW_LANG_PLI:
  115. if (dwarf::DWARF_VERSION >= 4)
  116. return 1;
  117. break;
  118. }
  119. return -1;
  120. }
  121. /// Check whether the DIE for this MDNode can be shared across CUs.
  122. static bool isShareableAcrossCUs(DIDescriptor D) {
  123. // When the MDNode can be part of the type system, the DIE can be shared
  124. // across CUs.
  125. // Combining type units and cross-CU DIE sharing is lower value (since
  126. // cross-CU DIE sharing is used in LTO and removes type redundancy at that
  127. // level already) but may be implementable for some value in projects
  128. // building multiple independent libraries with LTO and then linking those
  129. // together.
  130. return (D.isType() ||
  131. (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
  132. !GenerateDwarfTypeUnits;
  133. }
  134. /// getDIE - Returns the debug information entry map slot for the
  135. /// specified debug variable. We delegate the request to DwarfDebug
  136. /// when the DIE for this MDNode can be shared across CUs. The mappings
  137. /// will be kept in DwarfDebug for shareable DIEs.
  138. DIE *DwarfUnit::getDIE(DIDescriptor D) const {
  139. if (isShareableAcrossCUs(D))
  140. return DU->getDIE(D);
  141. return MDNodeToDieMap.lookup(D);
  142. }
  143. /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
  144. /// when the DIE for this MDNode can be shared across CUs. The mappings
  145. /// will be kept in DwarfDebug for shareable DIEs.
  146. void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
  147. if (isShareableAcrossCUs(Desc)) {
  148. DU->insertDIE(Desc, D);
  149. return;
  150. }
  151. MDNodeToDieMap.insert(std::make_pair(Desc, D));
  152. }
  153. /// addFlag - Add a flag that is true.
  154. void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
  155. if (DD->getDwarfVersion() >= 4)
  156. Die.addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
  157. else
  158. Die.addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
  159. }
  160. /// addUInt - Add an unsigned integer attribute data and value.
  161. ///
  162. void DwarfUnit::addUInt(DIE &Die, dwarf::Attribute Attribute,
  163. Optional<dwarf::Form> Form, uint64_t Integer) {
  164. if (!Form)
  165. Form = DIEInteger::BestForm(false, Integer);
  166. DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
  167. DIEInteger(Integer);
  168. Die.addValue(Attribute, *Form, Value);
  169. }
  170. void DwarfUnit::addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer) {
  171. addUInt(Block, (dwarf::Attribute)0, Form, Integer);
  172. }
  173. /// addSInt - Add an signed integer attribute data and value.
  174. ///
  175. void DwarfUnit::addSInt(DIE &Die, dwarf::Attribute Attribute,
  176. Optional<dwarf::Form> Form, int64_t Integer) {
  177. if (!Form)
  178. Form = DIEInteger::BestForm(true, Integer);
  179. DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
  180. Die.addValue(Attribute, *Form, Value);
  181. }
  182. void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form,
  183. int64_t Integer) {
  184. addSInt(Die, (dwarf::Attribute)0, Form, Integer);
  185. }
  186. /// addString - Add a string attribute data and value. We always emit a
  187. /// reference to the string pool instead of immediate strings so that DIEs have
  188. /// more predictable sizes. In the case of split dwarf we emit an index
  189. /// into another table which gets us the static offset into the string
  190. /// table.
  191. void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
  192. StringRef String) {
  193. if (!isDwoUnit())
  194. return addLocalString(Die, Attribute, String);
  195. addIndexedString(Die, Attribute, String);
  196. }
  197. void DwarfUnit::addIndexedString(DIE &Die, dwarf::Attribute Attribute,
  198. StringRef String) {
  199. unsigned idx = DU->getStringPool().getIndex(*Asm, String);
  200. DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
  201. DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
  202. Die.addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
  203. }
  204. /// addLocalString - Add a string attribute data and value. This is guaranteed
  205. /// to be in the local string pool instead of indirected.
  206. void DwarfUnit::addLocalString(DIE &Die, dwarf::Attribute Attribute,
  207. StringRef String) {
  208. MCSymbol *Symb = DU->getStringPool().getSymbol(*Asm, String);
  209. DIEValue *Value;
  210. if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
  211. Value = new (DIEValueAllocator) DIELabel(Symb);
  212. else
  213. Value = new (DIEValueAllocator) DIEDelta(Symb, DD->getDebugStrSym());
  214. DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
  215. Die.addValue(Attribute, dwarf::DW_FORM_strp, Str);
  216. }
  217. /// addLabel - Add a Dwarf label attribute data and value.
  218. ///
  219. void DwarfUnit::addLabel(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
  220. const MCSymbol *Label) {
  221. DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
  222. Die.addValue(Attribute, Form, Value);
  223. }
  224. void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
  225. addLabel(Die, (dwarf::Attribute)0, Form, Label);
  226. }
  227. /// addSectionOffset - Add an offset into a section attribute data and value.
  228. ///
  229. void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
  230. uint64_t Integer) {
  231. if (DD->getDwarfVersion() >= 4)
  232. addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
  233. else
  234. addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
  235. }
  236. unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
  237. return SplitLineTable ? SplitLineTable->getFile(DirName, FileName)
  238. : getCU().getOrCreateSourceID(FileName, DirName);
  239. }
  240. /// addOpAddress - Add a dwarf op address data and value using the
  241. /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
  242. ///
  243. void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
  244. if (!DD->useSplitDwarf()) {
  245. addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
  246. addLabel(Die, dwarf::DW_FORM_udata, Sym);
  247. } else {
  248. addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
  249. addUInt(Die, dwarf::DW_FORM_GNU_addr_index,
  250. DD->getAddressPool().getIndex(Sym));
  251. }
  252. }
  253. void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute,
  254. const MCSymbol *Hi, const MCSymbol *Lo) {
  255. DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
  256. Die.addValue(Attribute, dwarf::DW_FORM_data4, Value);
  257. }
  258. /// addDIEEntry - Add a DIE attribute data and value.
  259. ///
  260. void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) {
  261. addDIEEntry(Die, Attribute, createDIEEntry(Entry));
  262. }
  263. void DwarfUnit::addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type) {
  264. // Flag the type unit reference as a declaration so that if it contains
  265. // members (implicit special members, static data member definitions, member
  266. // declarations for definitions in this CU, etc) consumers don't get confused
  267. // and think this is a full definition.
  268. addFlag(Die, dwarf::DW_AT_declaration);
  269. Die.addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
  270. new (DIEValueAllocator) DIETypeSignature(Type));
  271. }
  272. void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
  273. DIEEntry *Entry) {
  274. const DIE *DieCU = Die.getUnitOrNull();
  275. const DIE *EntryCU = Entry->getEntry().getUnitOrNull();
  276. if (!DieCU)
  277. // We assume that Die belongs to this CU, if it is not linked to any CU yet.
  278. DieCU = &getUnitDie();
  279. if (!EntryCU)
  280. EntryCU = &getUnitDie();
  281. Die.addValue(Attribute,
  282. EntryCU == DieCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
  283. Entry);
  284. }
  285. /// Create a DIE with the given Tag, add the DIE to its parent, and
  286. /// call insertDIE if MD is not null.
  287. DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
  288. assert(Tag != dwarf::DW_TAG_auto_variable &&
  289. Tag != dwarf::DW_TAG_arg_variable);
  290. Parent.addChild(make_unique<DIE>((dwarf::Tag)Tag));
  291. DIE &Die = *Parent.getChildren().back();
  292. if (N)
  293. insertDIE(N, &Die);
  294. return Die;
  295. }
  296. /// addBlock - Add block data.
  297. ///
  298. void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
  299. Loc->ComputeSize(Asm);
  300. DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
  301. Die.addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
  302. }
  303. void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
  304. DIEBlock *Block) {
  305. Block->ComputeSize(Asm);
  306. DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
  307. Die.addValue(Attribute, Block->BestForm(), Block);
  308. }
  309. /// addSourceLine - Add location information to specified debug information
  310. /// entry.
  311. void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, StringRef File,
  312. StringRef Directory) {
  313. if (Line == 0)
  314. return;
  315. unsigned FileID = getOrCreateSourceID(File, Directory);
  316. assert(FileID && "Invalid file id");
  317. addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
  318. addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
  319. }
  320. /// addSourceLine - Add location information to specified debug information
  321. /// entry.
  322. void DwarfUnit::addSourceLine(DIE &Die, DIVariable V) {
  323. assert(V.isVariable());
  324. addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
  325. V.getContext().getDirectory());
  326. }
  327. /// addSourceLine - Add location information to specified debug information
  328. /// entry.
  329. void DwarfUnit::addSourceLine(DIE &Die, DIGlobalVariable G) {
  330. assert(G.isGlobalVariable());
  331. addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
  332. }
  333. /// addSourceLine - Add location information to specified debug information
  334. /// entry.
  335. void DwarfUnit::addSourceLine(DIE &Die, DISubprogram SP) {
  336. assert(SP.isSubprogram());
  337. addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
  338. }
  339. /// addSourceLine - Add location information to specified debug information
  340. /// entry.
  341. void DwarfUnit::addSourceLine(DIE &Die, DIType Ty) {
  342. assert(Ty.isType());
  343. addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
  344. }
  345. /// addSourceLine - Add location information to specified debug information
  346. /// entry.
  347. void DwarfUnit::addSourceLine(DIE &Die, DIObjCProperty Ty) {
  348. assert(Ty.isObjCProperty());
  349. DIFile File = Ty.getFile();
  350. addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
  351. File.getDirectory());
  352. }
  353. /// addSourceLine - Add location information to specified debug information
  354. /// entry.
  355. void DwarfUnit::addSourceLine(DIE &Die, DINameSpace NS) {
  356. assert(NS.Verify());
  357. addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
  358. }
  359. /// addRegisterOp - Add register operand.
  360. bool DwarfUnit::addRegisterOpPiece(DIELoc &TheDie, unsigned Reg,
  361. unsigned SizeInBits, unsigned OffsetInBits) {
  362. DIEDwarfExpression Expr(*Asm, *this, TheDie);
  363. Expr.AddMachineRegPiece(Reg, SizeInBits, OffsetInBits);
  364. return true;
  365. }
  366. /// addRegisterOffset - Add register offset.
  367. bool DwarfUnit::addRegisterOffset(DIELoc &TheDie, unsigned Reg,
  368. int64_t Offset) {
  369. DIEDwarfExpression Expr(*Asm, *this, TheDie);
  370. return Expr.AddMachineRegIndirect(Reg, Offset);
  371. }
  372. /* Byref variables, in Blocks, are declared by the programmer as "SomeType
  373. VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
  374. gives the variable VarName either the struct, or a pointer to the struct, as
  375. its type. This is necessary for various behind-the-scenes things the
  376. compiler needs to do with by-reference variables in Blocks.
  377. However, as far as the original *programmer* is concerned, the variable
  378. should still have type 'SomeType', as originally declared.
  379. The function getBlockByrefType dives into the __Block_byref_x_VarName
  380. struct to find the original type of the variable, which is then assigned to
  381. the variable's Debug Information Entry as its real type. So far, so good.
  382. However now the debugger will expect the variable VarName to have the type
  383. SomeType. So we need the location attribute for the variable to be an
  384. expression that explains to the debugger how to navigate through the
  385. pointers and struct to find the actual variable of type SomeType.
  386. The following function does just that. We start by getting
  387. the "normal" location for the variable. This will be the location
  388. of either the struct __Block_byref_x_VarName or the pointer to the
  389. struct __Block_byref_x_VarName.
  390. The struct will look something like:
  391. struct __Block_byref_x_VarName {
  392. ... <various fields>
  393. struct __Block_byref_x_VarName *forwarding;
  394. ... <various other fields>
  395. SomeType VarName;
  396. ... <maybe more fields>
  397. };
  398. If we are given the struct directly (as our starting point) we
  399. need to tell the debugger to:
  400. 1). Add the offset of the forwarding field.
  401. 2). Follow that pointer to get the real __Block_byref_x_VarName
  402. struct to use (the real one may have been copied onto the heap).
  403. 3). Add the offset for the field VarName, to find the actual variable.
  404. If we started with a pointer to the struct, then we need to
  405. dereference that pointer first, before the other steps.
  406. Translating this into DWARF ops, we will need to append the following
  407. to the current location description for the variable:
  408. DW_OP_deref -- optional, if we start with a pointer
  409. DW_OP_plus_uconst <forward_fld_offset>
  410. DW_OP_deref
  411. DW_OP_plus_uconst <varName_fld_offset>
  412. That is what this function does. */
  413. /// addBlockByrefAddress - Start with the address based on the location
  414. /// provided, and generate the DWARF information necessary to find the
  415. /// actual Block variable (navigating the Block struct) based on the
  416. /// starting location. Add the DWARF information to the die. For
  417. /// more information, read large comment just above here.
  418. ///
  419. void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
  420. dwarf::Attribute Attribute,
  421. const MachineLocation &Location) {
  422. DIType Ty = DV.getType();
  423. DIType TmpTy = Ty;
  424. uint16_t Tag = Ty.getTag();
  425. bool isPointer = false;
  426. StringRef varName = DV.getName();
  427. if (Tag == dwarf::DW_TAG_pointer_type) {
  428. DIDerivedType DTy(Ty);
  429. TmpTy = resolve(DTy.getTypeDerivedFrom());
  430. isPointer = true;
  431. }
  432. DICompositeType blockStruct(TmpTy);
  433. // Find the __forwarding field and the variable field in the __Block_byref
  434. // struct.
  435. DIArray Fields = blockStruct.getElements();
  436. DIDerivedType varField;
  437. DIDerivedType forwardingField;
  438. for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
  439. DIDerivedType DT(Fields.getElement(i));
  440. StringRef fieldName = DT.getName();
  441. if (fieldName == "__forwarding")
  442. forwardingField = DT;
  443. else if (fieldName == varName)
  444. varField = DT;
  445. }
  446. // Get the offsets for the forwarding field and the variable field.
  447. unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
  448. unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
  449. // Decode the original location, and use that as the start of the byref
  450. // variable's location.
  451. DIELoc *Loc = new (DIEValueAllocator) DIELoc();
  452. bool validReg;
  453. if (Location.isReg())
  454. validReg = addRegisterOpPiece(*Loc, Location.getReg());
  455. else
  456. validReg = addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
  457. if (!validReg)
  458. return;
  459. // If we started with a pointer to the __Block_byref... struct, then
  460. // the first thing we need to do is dereference the pointer (DW_OP_deref).
  461. if (isPointer)
  462. addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
  463. // Next add the offset for the '__forwarding' field:
  464. // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
  465. // adding the offset if it's 0.
  466. if (forwardingFieldOffset > 0) {
  467. addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
  468. addUInt(*Loc, dwarf::DW_FORM_udata, forwardingFieldOffset);
  469. }
  470. // Now dereference the __forwarding field to get to the real __Block_byref
  471. // struct: DW_OP_deref.
  472. addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
  473. // Now that we've got the real __Block_byref... struct, add the offset
  474. // for the variable's field to get to the location of the actual variable:
  475. // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
  476. if (varFieldOffset > 0) {
  477. addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
  478. addUInt(*Loc, dwarf::DW_FORM_udata, varFieldOffset);
  479. }
  480. // Now attach the location information to the DIE.
  481. addBlock(Die, Attribute, Loc);
  482. }
  483. /// Return true if type encoding is unsigned.
  484. static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
  485. DIDerivedType DTy(Ty);
  486. if (DTy.isDerivedType()) {
  487. dwarf::Tag T = (dwarf::Tag)Ty.getTag();
  488. // Encode pointer constants as unsigned bytes. This is used at least for
  489. // null pointer constant emission.
  490. // (Pieces of) aggregate types that get hacked apart by SROA may also be
  491. // represented by a constant. Encode them as unsigned bytes.
  492. // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
  493. // here, but accept them for now due to a bug in SROA producing bogus
  494. // dbg.values.
  495. if (T == dwarf::DW_TAG_array_type ||
  496. T == dwarf::DW_TAG_class_type ||
  497. T == dwarf::DW_TAG_pointer_type ||
  498. T == dwarf::DW_TAG_ptr_to_member_type ||
  499. T == dwarf::DW_TAG_reference_type ||
  500. T == dwarf::DW_TAG_rvalue_reference_type ||
  501. T == dwarf::DW_TAG_structure_type)
  502. return true;
  503. assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
  504. T == dwarf::DW_TAG_volatile_type ||
  505. T == dwarf::DW_TAG_restrict_type ||
  506. T == dwarf::DW_TAG_enumeration_type);
  507. if (DITypeRef Deriv = DTy.getTypeDerivedFrom())
  508. return isUnsignedDIType(DD, DD->resolve(Deriv));
  509. // FIXME: Enums without a fixed underlying type have unknown signedness
  510. // here, leading to incorrectly emitted constants.
  511. assert(DTy.getTag() == dwarf::DW_TAG_enumeration_type);
  512. return false;
  513. }
  514. DIBasicType BTy(Ty);
  515. assert(BTy.isBasicType());
  516. unsigned Encoding = BTy.getEncoding();
  517. assert((Encoding == dwarf::DW_ATE_unsigned ||
  518. Encoding == dwarf::DW_ATE_unsigned_char ||
  519. Encoding == dwarf::DW_ATE_signed ||
  520. Encoding == dwarf::DW_ATE_signed_char ||
  521. Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
  522. (Ty.getTag() == dwarf::DW_TAG_unspecified_type &&
  523. Ty.getName() == "decltype(nullptr)")) &&
  524. "Unsupported encoding");
  525. return (Encoding == dwarf::DW_ATE_unsigned ||
  526. Encoding == dwarf::DW_ATE_unsigned_char ||
  527. Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
  528. Ty.getTag() == dwarf::DW_TAG_unspecified_type);
  529. }
  530. /// If this type is derived from a base type then return base type size.
  531. static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
  532. unsigned Tag = Ty.getTag();
  533. if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
  534. Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
  535. Tag != dwarf::DW_TAG_restrict_type)
  536. return Ty.getSizeInBits();
  537. DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
  538. // If this type is not derived from any type or the type is a declaration then
  539. // take conservative approach.
  540. if (!BaseType.isValid() || BaseType.isForwardDecl())
  541. return Ty.getSizeInBits();
  542. // If this is a derived type, go ahead and get the base type, unless it's a
  543. // reference then it's just the size of the field. Pointer types have no need
  544. // of this since they're a different type of qualification on the type.
  545. if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
  546. BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
  547. return Ty.getSizeInBits();
  548. if (BaseType.isDerivedType())
  549. return getBaseTypeSize(DD, DIDerivedType(BaseType));
  550. return BaseType.getSizeInBits();
  551. }
  552. /// addConstantFPValue - Add constant value entry in variable DIE.
  553. void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) {
  554. assert(MO.isFPImm() && "Invalid machine operand!");
  555. DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
  556. APFloat FPImm = MO.getFPImm()->getValueAPF();
  557. // Get the raw data form of the floating point.
  558. const APInt FltVal = FPImm.bitcastToAPInt();
  559. const char *FltPtr = (const char *)FltVal.getRawData();
  560. int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
  561. bool LittleEndian = Asm->getDataLayout().isLittleEndian();
  562. int Incr = (LittleEndian ? 1 : -1);
  563. int Start = (LittleEndian ? 0 : NumBytes - 1);
  564. int Stop = (LittleEndian ? NumBytes : -1);
  565. // Output the constant to DWARF one byte at a time.
  566. for (; Start != Stop; Start += Incr)
  567. addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
  568. addBlock(Die, dwarf::DW_AT_const_value, Block);
  569. }
  570. /// addConstantFPValue - Add constant value entry in variable DIE.
  571. void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
  572. // Pass this down to addConstantValue as an unsigned bag of bits.
  573. addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
  574. }
  575. /// addConstantValue - Add constant value entry in variable DIE.
  576. void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI, DIType Ty) {
  577. addConstantValue(Die, CI->getValue(), Ty);
  578. }
  579. /// addConstantValue - Add constant value entry in variable DIE.
  580. void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO,
  581. DIType Ty) {
  582. assert(MO.isImm() && "Invalid machine operand!");
  583. addConstantValue(Die, isUnsignedDIType(DD, Ty), MO.getImm());
  584. }
  585. void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
  586. // FIXME: This is a bit conservative/simple - it emits negative values always
  587. // sign extended to 64 bits rather than minimizing the number of bytes.
  588. addUInt(Die, dwarf::DW_AT_const_value,
  589. Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
  590. }
  591. void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, DIType Ty) {
  592. addConstantValue(Die, Val, isUnsignedDIType(DD, Ty));
  593. }
  594. // addConstantValue - Add constant value entry in variable DIE.
  595. void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
  596. unsigned CIBitWidth = Val.getBitWidth();
  597. if (CIBitWidth <= 64) {
  598. addConstantValue(Die, Unsigned,
  599. Unsigned ? Val.getZExtValue() : Val.getSExtValue());
  600. return;
  601. }
  602. DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
  603. // Get the raw data form of the large APInt.
  604. const uint64_t *Ptr64 = Val.getRawData();
  605. int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
  606. bool LittleEndian = Asm->getDataLayout().isLittleEndian();
  607. // Output the constant to DWARF one byte at a time.
  608. for (int i = 0; i < NumBytes; i++) {
  609. uint8_t c;
  610. if (LittleEndian)
  611. c = Ptr64[i / 8] >> (8 * (i & 7));
  612. else
  613. c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
  614. addUInt(*Block, dwarf::DW_FORM_data1, c);
  615. }
  616. addBlock(Die, dwarf::DW_AT_const_value, Block);
  617. }
  618. /// addTemplateParams - Add template parameters into buffer.
  619. void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
  620. // Add template parameters.
  621. for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
  622. DIDescriptor Element = TParams.getElement(i);
  623. if (Element.isTemplateTypeParameter())
  624. constructTemplateTypeParameterDIE(Buffer,
  625. DITemplateTypeParameter(Element));
  626. else if (Element.isTemplateValueParameter())
  627. constructTemplateValueParameterDIE(Buffer,
  628. DITemplateValueParameter(Element));
  629. }
  630. }
  631. /// getOrCreateContextDIE - Get context owner's DIE.
  632. DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
  633. if (!Context || Context.isFile())
  634. return &getUnitDie();
  635. if (Context.isType())
  636. return getOrCreateTypeDIE(DIType(Context));
  637. if (Context.isNameSpace())
  638. return getOrCreateNameSpace(DINameSpace(Context));
  639. if (Context.isSubprogram())
  640. return getOrCreateSubprogramDIE(DISubprogram(Context));
  641. return getDIE(Context);
  642. }
  643. DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
  644. DIScope Context = resolve(Ty.getContext());
  645. DIE *ContextDIE = getOrCreateContextDIE(Context);
  646. if (DIE *TyDIE = getDIE(Ty))
  647. return TyDIE;
  648. // Create new type.
  649. DIE &TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
  650. constructTypeDIE(TyDIE, Ty);
  651. updateAcceleratorTables(Context, Ty, TyDIE);
  652. return &TyDIE;
  653. }
  654. /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
  655. /// given DIType.
  656. DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
  657. if (!TyNode)
  658. return nullptr;
  659. DIType Ty(TyNode);
  660. assert(Ty.isType());
  661. assert(Ty == resolve(Ty.getRef()) &&
  662. "type was not uniqued, possible ODR violation.");
  663. // DW_TAG_restrict_type is not supported in DWARF2
  664. if (Ty.getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
  665. return getOrCreateTypeDIE(resolve(DIDerivedType(Ty).getTypeDerivedFrom()));
  666. // Construct the context before querying for the existence of the DIE in case
  667. // such construction creates the DIE.
  668. DIScope Context = resolve(Ty.getContext());
  669. DIE *ContextDIE = getOrCreateContextDIE(Context);
  670. assert(ContextDIE);
  671. if (DIE *TyDIE = getDIE(Ty))
  672. return TyDIE;
  673. // Create new type.
  674. DIE &TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
  675. updateAcceleratorTables(Context, Ty, TyDIE);
  676. if (Ty.isBasicType())
  677. constructTypeDIE(TyDIE, DIBasicType(Ty));
  678. else if (Ty.isCompositeType()) {
  679. DICompositeType CTy(Ty);
  680. if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
  681. if (MDString *TypeId = CTy.getIdentifier()) {
  682. DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
  683. // Skip updating the accelerator tables since this is not the full type.
  684. return &TyDIE;
  685. }
  686. constructTypeDIE(TyDIE, CTy);
  687. } else {
  688. assert(Ty.isDerivedType() && "Unknown kind of DIType");
  689. constructTypeDIE(TyDIE, DIDerivedType(Ty));
  690. }
  691. return &TyDIE;
  692. }
  693. void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
  694. const DIE &TyDIE) {
  695. if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
  696. bool IsImplementation = 0;
  697. if (Ty.isCompositeType()) {
  698. DICompositeType CT(Ty);
  699. // A runtime language of 0 actually means C/C++ and that any
  700. // non-negative value is some version of Objective-C/C++.
  701. IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
  702. }
  703. unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
  704. DD->addAccelType(Ty.getName(), TyDIE, Flags);
  705. if (!Context || Context.isCompileUnit() || Context.isFile() ||
  706. Context.isNameSpace())
  707. addGlobalType(Ty, TyDIE, Context);
  708. }
  709. }
  710. /// addType - Add a new type attribute to the specified entity.
  711. void DwarfUnit::addType(DIE &Entity, DIType Ty, dwarf::Attribute Attribute) {
  712. assert(Ty && "Trying to add a type that doesn't exist?");
  713. // Check for pre-existence.
  714. DIEEntry *Entry = getDIEEntry(Ty);
  715. // If it exists then use the existing value.
  716. if (Entry) {
  717. addDIEEntry(Entity, Attribute, Entry);
  718. return;
  719. }
  720. // Construct type.
  721. DIE *Buffer = getOrCreateTypeDIE(Ty);
  722. // Set up proxy.
  723. Entry = createDIEEntry(*Buffer);
  724. insertDIEEntry(Ty, Entry);
  725. addDIEEntry(Entity, Attribute, Entry);
  726. }
  727. /// getParentContextString - Walks the metadata parent chain in a language
  728. /// specific manner (using the compile unit language) and returns
  729. /// it as a string. This is done at the metadata level because DIEs may
  730. /// not currently have been added to the parent context and walking the
  731. /// DIEs looking for names is more expensive than walking the metadata.
  732. std::string DwarfUnit::getParentContextString(DIScope Context) const {
  733. if (!Context)
  734. return "";
  735. // FIXME: Decide whether to implement this for non-C++ languages.
  736. if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
  737. return "";
  738. std::string CS;
  739. SmallVector<DIScope, 1> Parents;
  740. while (!Context.isCompileUnit()) {
  741. Parents.push_back(Context);
  742. if (Context.getContext())
  743. Context = resolve(Context.getContext());
  744. else
  745. // Structure, etc types will have a NULL context if they're at the top
  746. // level.
  747. break;
  748. }
  749. // Reverse iterate over our list to go from the outermost construct to the
  750. // innermost.
  751. for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
  752. E = Parents.rend();
  753. I != E; ++I) {
  754. DIScope Ctx = *I;
  755. StringRef Name = Ctx.getName();
  756. if (Name.empty() && Ctx.isNameSpace())
  757. Name = "(anonymous namespace)";
  758. if (!Name.empty()) {
  759. CS += Name;
  760. CS += "::";
  761. }
  762. }
  763. return CS;
  764. }
  765. /// constructTypeDIE - Construct basic type die from DIBasicType.
  766. void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
  767. // Get core information.
  768. StringRef Name = BTy.getName();
  769. // Add name if not anonymous or intermediate type.
  770. if (!Name.empty())
  771. addString(Buffer, dwarf::DW_AT_name, Name);
  772. // An unspecified type only has a name attribute.
  773. if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
  774. return;
  775. addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
  776. BTy.getEncoding());
  777. uint64_t Size = BTy.getSizeInBits() >> 3;
  778. addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
  779. }
  780. /// constructTypeDIE - Construct derived type die from DIDerivedType.
  781. void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
  782. // Get core information.
  783. StringRef Name = DTy.getName();
  784. uint64_t Size = DTy.getSizeInBits() >> 3;
  785. uint16_t Tag = Buffer.getTag();
  786. // Map to main type, void will not have a type.
  787. DIType FromTy = resolve(DTy.getTypeDerivedFrom());
  788. if (FromTy)
  789. addType(Buffer, FromTy);
  790. // Add name if not anonymous or intermediate type.
  791. if (!Name.empty())
  792. addString(Buffer, dwarf::DW_AT_name, Name);
  793. // Add size if non-zero (derived types might be zero-sized.)
  794. if (Size && Tag != dwarf::DW_TAG_pointer_type
  795. && Tag != dwarf::DW_TAG_ptr_to_member_type)
  796. addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
  797. if (Tag == dwarf::DW_TAG_ptr_to_member_type)
  798. addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
  799. *getOrCreateTypeDIE(resolve(DTy.getClassType())));
  800. // Add source line info if available and TyDesc is not a forward declaration.
  801. if (!DTy.isForwardDecl())
  802. addSourceLine(Buffer, DTy);
  803. }
  804. /// constructSubprogramArguments - Construct function argument DIEs.
  805. void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeArray Args) {
  806. for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
  807. DIType Ty = resolve(Args.getElement(i));
  808. if (!Ty) {
  809. assert(i == N-1 && "Unspecified parameter must be the last argument");
  810. createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
  811. } else {
  812. DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
  813. addType(Arg, Ty);
  814. if (Ty.isArtificial())
  815. addFlag(Arg, dwarf::DW_AT_artificial);
  816. }
  817. }
  818. }
  819. /// constructTypeDIE - Construct type DIE from DICompositeType.
  820. void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
  821. // Add name if not anonymous or intermediate type.
  822. StringRef Name = CTy.getName();
  823. uint64_t Size = CTy.getSizeInBits() >> 3;
  824. uint16_t Tag = Buffer.getTag();
  825. switch (Tag) {
  826. case dwarf::DW_TAG_array_type:
  827. constructArrayTypeDIE(Buffer, CTy);
  828. break;
  829. case dwarf::DW_TAG_enumeration_type:
  830. constructEnumTypeDIE(Buffer, CTy);
  831. break;
  832. case dwarf::DW_TAG_subroutine_type: {
  833. // Add return type. A void return won't have a type.
  834. DITypeArray Elements = DISubroutineType(CTy).getTypeArray();
  835. DIType RTy(resolve(Elements.getElement(0)));
  836. if (RTy)
  837. addType(Buffer, RTy);
  838. bool isPrototyped = true;
  839. if (Elements.getNumElements() == 2 &&
  840. !Elements.getElement(1))
  841. isPrototyped = false;
  842. constructSubprogramArguments(Buffer, Elements);
  843. // Add prototype flag if we're dealing with a C language and the
  844. // function has been prototyped.
  845. uint16_t Language = getLanguage();
  846. if (isPrototyped &&
  847. (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
  848. Language == dwarf::DW_LANG_ObjC))
  849. addFlag(Buffer, dwarf::DW_AT_prototyped);
  850. if (CTy.isLValueReference())
  851. addFlag(Buffer, dwarf::DW_AT_reference);
  852. if (CTy.isRValueReference())
  853. addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
  854. } break;
  855. case dwarf::DW_TAG_structure_type:
  856. case dwarf::DW_TAG_union_type:
  857. case dwarf::DW_TAG_class_type: {
  858. // Add elements to structure type.
  859. DIArray Elements = CTy.getElements();
  860. for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
  861. DIDescriptor Element = Elements.getElement(i);
  862. if (Element.isSubprogram())
  863. getOrCreateSubprogramDIE(DISubprogram(Element));
  864. else if (Element.isDerivedType()) {
  865. DIDerivedType DDTy(Element);
  866. if (DDTy.getTag() == dwarf::DW_TAG_friend) {
  867. DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
  868. addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
  869. dwarf::DW_AT_friend);
  870. } else if (DDTy.isStaticMember()) {
  871. getOrCreateStaticMemberDIE(DDTy);
  872. } else {
  873. constructMemberDIE(Buffer, DDTy);
  874. }
  875. } else if (Element.isObjCProperty()) {
  876. DIObjCProperty Property(Element);
  877. DIE &ElemDie = createAndAddDIE(Property.getTag(), Buffer);
  878. StringRef PropertyName = Property.getObjCPropertyName();
  879. addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
  880. if (Property.getType())
  881. addType(ElemDie, Property.getType());
  882. addSourceLine(ElemDie, Property);
  883. StringRef GetterName = Property.getObjCPropertyGetterName();
  884. if (!GetterName.empty())
  885. addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
  886. StringRef SetterName = Property.getObjCPropertySetterName();
  887. if (!SetterName.empty())
  888. addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
  889. unsigned PropertyAttributes = 0;
  890. if (Property.isReadOnlyObjCProperty())
  891. PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
  892. if (Property.isReadWriteObjCProperty())
  893. PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
  894. if (Property.isAssignObjCProperty())
  895. PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
  896. if (Property.isRetainObjCProperty())
  897. PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
  898. if (Property.isCopyObjCProperty())
  899. PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
  900. if (Property.isNonAtomicObjCProperty())
  901. PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
  902. if (PropertyAttributes)
  903. addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
  904. PropertyAttributes);
  905. DIEEntry *Entry = getDIEEntry(Element);
  906. if (!Entry) {
  907. Entry = createDIEEntry(ElemDie);
  908. insertDIEEntry(Element, Entry);
  909. }
  910. } else
  911. continue;
  912. }
  913. if (CTy.isAppleBlockExtension())
  914. addFlag(Buffer, dwarf::DW_AT_APPLE_block);
  915. // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
  916. // inside C++ composite types to point to the base class with the vtable.
  917. DICompositeType ContainingType(resolve(CTy.getContainingType()));
  918. if (ContainingType)
  919. addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
  920. *getOrCreateTypeDIE(ContainingType));
  921. if (CTy.isObjcClassComplete())
  922. addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
  923. // Add template parameters to a class, structure or union types.
  924. // FIXME: The support isn't in the metadata for this yet.
  925. if (Tag == dwarf::DW_TAG_class_type ||
  926. Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
  927. addTemplateParams(Buffer, CTy.getTemplateParams());
  928. break;
  929. }
  930. default:
  931. break;
  932. }
  933. // Add name if not anonymous or intermediate type.
  934. if (!Name.empty())
  935. addString(Buffer, dwarf::DW_AT_name, Name);
  936. if (Tag == dwarf::DW_TAG_enumeration_type ||
  937. Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
  938. Tag == dwarf::DW_TAG_union_type) {
  939. // Add size if non-zero (derived types might be zero-sized.)
  940. // TODO: Do we care about size for enum forward declarations?
  941. if (Size)
  942. addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
  943. else if (!CTy.isForwardDecl())
  944. // Add zero size if it is not a forward declaration.
  945. addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0);
  946. // If we're a forward decl, say so.
  947. if (CTy.isForwardDecl())
  948. addFlag(Buffer, dwarf::DW_AT_declaration);
  949. // Add source line info if available.
  950. if (!CTy.isForwardDecl())
  951. addSourceLine(Buffer, CTy);
  952. // No harm in adding the runtime language to the declaration.
  953. unsigned RLang = CTy.getRunTimeLang();
  954. if (RLang)
  955. addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
  956. RLang);
  957. }
  958. }
  959. /// constructTemplateTypeParameterDIE - Construct new DIE for the given
  960. /// DITemplateTypeParameter.
  961. void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
  962. DITemplateTypeParameter TP) {
  963. DIE &ParamDIE =
  964. createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
  965. // Add the type if it exists, it could be void and therefore no type.
  966. if (TP.getType())
  967. addType(ParamDIE, resolve(TP.getType()));
  968. if (!TP.getName().empty())
  969. addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
  970. }
  971. /// constructTemplateValueParameterDIE - Construct new DIE for the given
  972. /// DITemplateValueParameter.
  973. void
  974. DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
  975. DITemplateValueParameter VP) {
  976. DIE &ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
  977. // Add the type if there is one, template template and template parameter
  978. // packs will not have a type.
  979. if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
  980. addType(ParamDIE, resolve(VP.getType()));
  981. if (!VP.getName().empty())
  982. addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
  983. if (Metadata *Val = VP.getValue()) {
  984. if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
  985. addConstantValue(ParamDIE, CI, resolve(VP.getType()));
  986. else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
  987. // For declaration non-type template parameters (such as global values and
  988. // functions)
  989. DIELoc *Loc = new (DIEValueAllocator) DIELoc();
  990. addOpAddress(*Loc, Asm->getSymbol(GV));
  991. // Emit DW_OP_stack_value to use the address as the immediate value of the
  992. // parameter, rather than a pointer to it.
  993. addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
  994. addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
  995. } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
  996. assert(isa<MDString>(Val));
  997. addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
  998. cast<MDString>(Val)->getString());
  999. } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
  1000. assert(isa<MDNode>(Val));
  1001. DIArray A(cast<MDNode>(Val));
  1002. addTemplateParams(ParamDIE, A);
  1003. }
  1004. }
  1005. }
  1006. /// getOrCreateNameSpace - Create a DIE for DINameSpace.
  1007. DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
  1008. // Construct the context before querying for the existence of the DIE in case
  1009. // such construction creates the DIE.
  1010. DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
  1011. if (DIE *NDie = getDIE(NS))
  1012. return NDie;
  1013. DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
  1014. StringRef Name = NS.getName();
  1015. if (!Name.empty())
  1016. addString(NDie, dwarf::DW_AT_name, NS.getName());
  1017. else
  1018. Name = "(anonymous namespace)";
  1019. DD->addAccelNamespace(Name, NDie);
  1020. addGlobalName(Name, NDie, NS.getContext());
  1021. addSourceLine(NDie, NS);
  1022. return &NDie;
  1023. }
  1024. /// getOrCreateSubprogramDIE - Create new DIE using SP.
  1025. DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP, bool Minimal) {
  1026. // Construct the context before querying for the existence of the DIE in case
  1027. // such construction creates the DIE (as is the case for member function
  1028. // declarations).
  1029. DIE *ContextDIE =
  1030. Minimal ? &getUnitDie() : getOrCreateContextDIE(resolve(SP.getContext()));
  1031. if (DIE *SPDie = getDIE(SP))
  1032. return SPDie;
  1033. if (DISubprogram SPDecl = SP.getFunctionDeclaration()) {
  1034. if (!Minimal) {
  1035. // Add subprogram definitions to the CU die directly.
  1036. ContextDIE = &getUnitDie();
  1037. // Build the decl now to ensure it precedes the definition.
  1038. getOrCreateSubprogramDIE(SPDecl);
  1039. }
  1040. }
  1041. // DW_TAG_inlined_subroutine may refer to this DIE.
  1042. DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
  1043. // Stop here and fill this in later, depending on whether or not this
  1044. // subprogram turns out to have inlined instances or not.
  1045. if (SP.isDefinition())
  1046. return &SPDie;
  1047. applySubprogramAttributes(SP, SPDie);
  1048. return &SPDie;
  1049. }
  1050. bool DwarfUnit::applySubprogramDefinitionAttributes(DISubprogram SP,
  1051. DIE &SPDie) {
  1052. DIE *DeclDie = nullptr;
  1053. StringRef DeclLinkageName;
  1054. if (DISubprogram SPDecl = SP.getFunctionDeclaration()) {
  1055. DeclDie = getDIE(SPDecl);
  1056. assert(DeclDie && "This DIE should've already been constructed when the "
  1057. "definition DIE was created in "
  1058. "getOrCreateSubprogramDIE");
  1059. DeclLinkageName = SPDecl.getLinkageName();
  1060. }
  1061. // Add function template parameters.
  1062. addTemplateParams(SPDie, SP.getTemplateParams());
  1063. // Add the linkage name if we have one and it isn't in the Decl.
  1064. StringRef LinkageName = SP.getLinkageName();
  1065. assert(((LinkageName.empty() || DeclLinkageName.empty()) ||
  1066. LinkageName == DeclLinkageName) &&
  1067. "decl has a linkage name and it is different");
  1068. if (!LinkageName.empty() && DeclLinkageName.empty())
  1069. addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
  1070. GlobalValue::getRealLinkageName(LinkageName));
  1071. if (!DeclDie)
  1072. return false;
  1073. // Refer to the function declaration where all the other attributes will be
  1074. // found.
  1075. addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
  1076. return true;
  1077. }
  1078. void DwarfUnit::applySubprogramAttributes(DISubprogram SP, DIE &SPDie,
  1079. bool Minimal) {
  1080. if (!Minimal)
  1081. if (applySubprogramDefinitionAttributes(SP, SPDie))
  1082. return;
  1083. // Constructors and operators for anonymous aggregates do not have names.
  1084. if (!SP.getName().empty())
  1085. addString(SPDie, dwarf::DW_AT_name, SP.getName());
  1086. // Skip the rest of the attributes under -gmlt to save space.
  1087. if (Minimal)
  1088. return;
  1089. addSourceLine(SPDie, SP);
  1090. // Add the prototype if we have a prototype and we have a C like
  1091. // language.
  1092. uint16_t Language = getLanguage();
  1093. if (SP.isPrototyped() &&
  1094. (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
  1095. Language == dwarf::DW_LANG_ObjC))
  1096. addFlag(SPDie, dwarf::DW_AT_prototyped);
  1097. DISubroutineType SPTy = SP.getType();
  1098. assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
  1099. "the type of a subprogram should be a subroutine");
  1100. DITypeArray Args = SPTy.getTypeArray();
  1101. // Add a return type. If this is a type like a C/C++ void type we don't add a
  1102. // return type.
  1103. if (resolve(Args.getElement(0)))
  1104. addType(SPDie, DIType(resolve(Args.getElement(0))));
  1105. unsigned VK = SP.getVirtuality();
  1106. if (VK) {
  1107. addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
  1108. DIELoc *Block = getDIELoc();
  1109. addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
  1110. addUInt(*Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
  1111. addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
  1112. ContainingTypeMap.insert(
  1113. std::make_pair(&SPDie, resolve(SP.getContainingType())));
  1114. }
  1115. if (!SP.isDefinition()) {
  1116. addFlag(SPDie, dwarf::DW_AT_declaration);
  1117. // Add arguments. Do not add arguments for subprogram definition. They will
  1118. // be handled while processing variables.
  1119. constructSubprogramArguments(SPDie, Args);
  1120. }
  1121. if (SP.isArtificial())
  1122. addFlag(SPDie, dwarf::DW_AT_artificial);
  1123. if (!SP.isLocalToUnit())
  1124. addFlag(SPDie, dwarf::DW_AT_external);
  1125. if (SP.isOptimized())
  1126. addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
  1127. if (unsigned isa = Asm->getISAEncoding()) {
  1128. addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
  1129. }
  1130. if (SP.isLValueReference())
  1131. addFlag(SPDie, dwarf::DW_AT_reference);
  1132. if (SP.isRValueReference())
  1133. addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
  1134. if (SP.isProtected())
  1135. addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
  1136. dwarf::DW_ACCESS_protected);
  1137. else if (SP.isPrivate())
  1138. addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
  1139. dwarf::DW_ACCESS_private);
  1140. else if (SP.isPublic())
  1141. addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
  1142. dwarf::DW_ACCESS_public);
  1143. if (SP.isExplicit())
  1144. addFlag(SPDie, dwarf::DW_AT_explicit);
  1145. }
  1146. /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
  1147. void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
  1148. DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
  1149. addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy);
  1150. // The LowerBound value defines the lower bounds which is typically zero for
  1151. // C/C++. The Count value is the number of elements. Values are 64 bit. If
  1152. // Count == -1 then the array is unbounded and we do not emit
  1153. // DW_AT_lower_bound and DW_AT_count attributes.
  1154. int64_t LowerBound = SR.getLo();
  1155. int64_t DefaultLowerBound = getDefaultLowerBound();
  1156. int64_t Count = SR.getCount();
  1157. if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
  1158. addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
  1159. if (Count != -1)
  1160. // FIXME: An unbounded array should reference the expression that defines
  1161. // the array.
  1162. addUInt(DW_Subrange, dwarf::DW_AT_count, None, Count);
  1163. }
  1164. DIE *DwarfUnit::getIndexTyDie() {
  1165. if (IndexTyDie)
  1166. return IndexTyDie;
  1167. // Construct an integer type to use for indexes.
  1168. IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, UnitDie);
  1169. addString(*IndexTyDie, dwarf::DW_AT_name, "sizetype");
  1170. addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
  1171. addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
  1172. dwarf::DW_ATE_unsigned);
  1173. return IndexTyDie;
  1174. }
  1175. /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
  1176. void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
  1177. if (CTy.isVector())
  1178. addFlag(Buffer, dwarf::DW_AT_GNU_vector);
  1179. // Emit the element type.
  1180. addType(Buffer, resolve(CTy.getTypeDerivedFrom()));
  1181. // Get an anonymous type for index type.
  1182. // FIXME: This type should be passed down from the front end
  1183. // as different languages may have different sizes for indexes.
  1184. DIE *IdxTy = getIndexTyDie();
  1185. // Add subranges to array type.
  1186. DIArray Elements = CTy.getElements();
  1187. for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
  1188. DIDescriptor Element = Elements.getElement(i);
  1189. if (Element.getTag() == dwarf::DW_TAG_subrange_type)
  1190. constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
  1191. }
  1192. }
  1193. /// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
  1194. void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
  1195. DIArray Elements = CTy.getElements();
  1196. // Add enumerators to enumeration type.
  1197. for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
  1198. DIEnumerator Enum(Elements.getElement(i));
  1199. if (Enum.isEnumerator()) {
  1200. DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
  1201. StringRef Name = Enum.getName();
  1202. addString(Enumerator, dwarf::DW_AT_name, Name);
  1203. int64_t Value = Enum.getEnumValue();
  1204. addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
  1205. Value);
  1206. }
  1207. }
  1208. DIType DTy = resolve(CTy.getTypeDerivedFrom());
  1209. if (DTy) {
  1210. addType(Buffer, DTy);
  1211. addFlag(Buffer, dwarf::DW_AT_enum_class);
  1212. }
  1213. }
  1214. /// constructContainingTypeDIEs - Construct DIEs for types that contain
  1215. /// vtables.
  1216. void DwarfUnit::constructContainingTypeDIEs() {
  1217. for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
  1218. CE = ContainingTypeMap.end();
  1219. CI != CE; ++CI) {
  1220. DIE &SPDie = *CI->first;
  1221. DIDescriptor D(CI->second);
  1222. if (!D)
  1223. continue;
  1224. DIE *NDie = getDIE(D);
  1225. if (!NDie)
  1226. continue;
  1227. addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
  1228. }
  1229. }
  1230. /// constructMemberDIE - Construct member DIE from DIDerivedType.
  1231. void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
  1232. DIE &MemberDie = createAndAddDIE(DT.getTag(), Buffer);
  1233. StringRef Name = DT.getName();
  1234. if (!Name.empty())
  1235. addString(MemberDie, dwarf::DW_AT_name, Name);
  1236. addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
  1237. addSourceLine(MemberDie, DT);
  1238. if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
  1239. // For C++, virtual base classes are not at fixed offset. Use following
  1240. // expression to extract appropriate offset from vtable.
  1241. // BaseAddr = ObAddr + *((*ObAddr) - Offset)
  1242. DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc();
  1243. addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
  1244. addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
  1245. addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
  1246. addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
  1247. addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
  1248. addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
  1249. addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
  1250. addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
  1251. } else {
  1252. uint64_t Size = DT.getSizeInBits();
  1253. uint64_t FieldSize = getBaseTypeSize(DD, DT);
  1254. uint64_t OffsetInBytes;
  1255. if (Size != FieldSize) {
  1256. // Handle bitfield, assume bytes are 8 bits.
  1257. addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
  1258. addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
  1259. uint64_t Offset = DT.getOffsetInBits();
  1260. uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
  1261. uint64_t HiMark = (Offset + FieldSize) & AlignMask;
  1262. uint64_t FieldOffset = (HiMark - FieldSize);
  1263. Offset -= FieldOffset;
  1264. // Maybe we need to work from the other end.
  1265. if (Asm->getDataLayout().isLittleEndian())
  1266. Offset = FieldSize - (Offset + Size);
  1267. addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
  1268. // Here DW_AT_data_member_location points to the anonymous
  1269. // field that includes this bit field.
  1270. OffsetInBytes = FieldOffset >> 3;
  1271. } else
  1272. // This is not a bitfield.
  1273. OffsetInBytes = DT.getOffsetInBits() >> 3;
  1274. if (DD->getDwarfVersion() <= 2) {
  1275. DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc();
  1276. addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
  1277. addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
  1278. addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
  1279. } else
  1280. addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
  1281. OffsetInBytes);
  1282. }
  1283. if (DT.isProtected())
  1284. addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
  1285. dwarf::DW_ACCESS_protected);
  1286. else if (DT.isPrivate())
  1287. addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
  1288. dwarf::DW_ACCESS_private);
  1289. // Otherwise C++ member and base classes are considered public.
  1290. else if (DT.isPublic())
  1291. addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
  1292. dwarf::DW_ACCESS_public);
  1293. if (DT.isVirtual())
  1294. addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
  1295. dwarf::DW_VIRTUALITY_virtual);
  1296. // Objective-C properties.
  1297. if (MDNode *PNode = DT.getObjCProperty())
  1298. if (DIEEntry *PropertyDie = getDIEEntry(PNode))
  1299. MemberDie.addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
  1300. PropertyDie);
  1301. if (DT.isArtificial())
  1302. addFlag(MemberDie, dwarf::DW_AT_artificial);
  1303. }
  1304. /// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
  1305. DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
  1306. if (!DT.Verify())
  1307. return nullptr;
  1308. // Construct the context before querying for the existence of the DIE in case
  1309. // such construction creates the DIE.
  1310. DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
  1311. assert(dwarf::isType(ContextDIE->getTag()) &&
  1312. "Static member should belong to a type.");
  1313. if (DIE *StaticMemberDIE = getDIE(DT))
  1314. return StaticMemberDIE;
  1315. DIE &StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
  1316. DIType Ty = resolve(DT.getTypeDerivedFrom());
  1317. addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
  1318. addType(StaticMemberDIE, Ty);
  1319. addSourceLine(StaticMemberDIE, DT);
  1320. addFlag(StaticMemberDIE, dwarf::DW_AT_external);
  1321. addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
  1322. // FIXME: We could omit private if the parent is a class_type, and
  1323. // public if the parent is something else.
  1324. if (DT.isProtected())
  1325. addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
  1326. dwarf::DW_ACCESS_protected);
  1327. else if (DT.isPrivate())
  1328. addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
  1329. dwarf::DW_ACCESS_private);
  1330. else if (DT.isPublic())
  1331. addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
  1332. dwarf::DW_ACCESS_public);
  1333. if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
  1334. addConstantValue(StaticMemberDIE, CI, Ty);
  1335. if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
  1336. addConstantFPValue(StaticMemberDIE, CFP);
  1337. return &StaticMemberDIE;
  1338. }
  1339. void DwarfUnit::emitHeader(const MCSymbol *ASectionSym) const {
  1340. // Emit size of content not including length itself
  1341. Asm->OutStreamer.AddComment("Length of Unit");
  1342. Asm->EmitInt32(getHeaderSize() + UnitDie.getSize());
  1343. Asm->OutStreamer.AddComment("DWARF version number");
  1344. Asm->EmitInt16(DD->getDwarfVersion());
  1345. Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
  1346. // We share one abbreviations table across all units so it's always at the
  1347. // start of the section. Use a relocatable offset where needed to ensure
  1348. // linking doesn't invalidate that offset.
  1349. if (ASectionSym)
  1350. Asm->EmitSectionOffset(ASectionSym, ASectionSym);
  1351. else
  1352. // Use a constant value when no symbol is provided.
  1353. Asm->EmitInt32(0);
  1354. Asm->OutStreamer.AddComment("Address Size (in bytes)");
  1355. Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
  1356. }
  1357. void DwarfUnit::initSection(const MCSection *Section) {
  1358. assert(!this->Section);
  1359. this->Section = Section;
  1360. }
  1361. void DwarfTypeUnit::emitHeader(const MCSymbol *ASectionSym) const {
  1362. DwarfUnit::emitHeader(ASectionSym);
  1363. Asm->OutStreamer.AddComment("Type Signature");
  1364. Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
  1365. Asm->OutStreamer.AddComment("Type DIE Offset");
  1366. // In a skeleton type unit there is no type DIE so emit a zero offset.
  1367. Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
  1368. sizeof(Ty->getOffset()));
  1369. }
  1370. bool DwarfTypeUnit::isDwoUnit() const {
  1371. // Since there are no skeleton type units, all type units are dwo type units
  1372. // when split DWARF is being used.
  1373. return DD->useSplitDwarf();
  1374. }