DIE.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Data structures for DWARF info entries.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/DIE.h"
  13. #include "DwarfCompileUnit.h"
  14. #include "DwarfDebug.h"
  15. #include "DwarfUnit.h"
  16. #include "llvm/ADT/Twine.h"
  17. #include "llvm/CodeGen/AsmPrinter.h"
  18. #include "llvm/Config/llvm-config.h"
  19. #include "llvm/IR/DataLayout.h"
  20. #include "llvm/MC/MCAsmInfo.h"
  21. #include "llvm/MC/MCContext.h"
  22. #include "llvm/MC/MCStreamer.h"
  23. #include "llvm/MC/MCSymbol.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include "llvm/Support/Format.h"
  27. #include "llvm/Support/FormattedStream.h"
  28. #include "llvm/Support/LEB128.h"
  29. #include "llvm/Support/MD5.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. using namespace llvm;
  32. #define DEBUG_TYPE "dwarfdebug"
  33. //===----------------------------------------------------------------------===//
  34. // DIEAbbrevData Implementation
  35. //===----------------------------------------------------------------------===//
  36. /// Profile - Used to gather unique data for the abbreviation folding set.
  37. ///
  38. void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
  39. // Explicitly cast to an integer type for which FoldingSetNodeID has
  40. // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
  41. ID.AddInteger(unsigned(Attribute));
  42. ID.AddInteger(unsigned(Form));
  43. if (Form == dwarf::DW_FORM_implicit_const)
  44. ID.AddInteger(Value);
  45. }
  46. //===----------------------------------------------------------------------===//
  47. // DIEAbbrev Implementation
  48. //===----------------------------------------------------------------------===//
  49. /// Profile - Used to gather unique data for the abbreviation folding set.
  50. ///
  51. void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
  52. ID.AddInteger(unsigned(Tag));
  53. ID.AddInteger(unsigned(Children));
  54. // For each attribute description.
  55. for (unsigned i = 0, N = Data.size(); i < N; ++i)
  56. Data[i].Profile(ID);
  57. }
  58. /// Emit - Print the abbreviation using the specified asm printer.
  59. ///
  60. void DIEAbbrev::Emit(const AsmPrinter *AP) const {
  61. // Emit its Dwarf tag type.
  62. AP->EmitULEB128(Tag, dwarf::TagString(Tag).data());
  63. // Emit whether it has children DIEs.
  64. AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());
  65. // For each attribute description.
  66. for (unsigned i = 0, N = Data.size(); i < N; ++i) {
  67. const DIEAbbrevData &AttrData = Data[i];
  68. // Emit attribute type.
  69. AP->EmitULEB128(AttrData.getAttribute(),
  70. dwarf::AttributeString(AttrData.getAttribute()).data());
  71. // Emit form type.
  72. #ifndef NDEBUG
  73. // Could be an assertion, but this way we can see the failing form code
  74. // easily, which helps track down where it came from.
  75. if (!dwarf::isValidFormForVersion(AttrData.getForm(),
  76. AP->getDwarfVersion())) {
  77. LLVM_DEBUG(dbgs() << "Invalid form " << format("0x%x", AttrData.getForm())
  78. << " for DWARF version " << AP->getDwarfVersion()
  79. << "\n");
  80. llvm_unreachable("Invalid form for specified DWARF version");
  81. }
  82. #endif
  83. AP->EmitULEB128(AttrData.getForm(),
  84. dwarf::FormEncodingString(AttrData.getForm()).data());
  85. // Emit value for DW_FORM_implicit_const.
  86. if (AttrData.getForm() == dwarf::DW_FORM_implicit_const)
  87. AP->EmitSLEB128(AttrData.getValue());
  88. }
  89. // Mark end of abbreviation.
  90. AP->EmitULEB128(0, "EOM(1)");
  91. AP->EmitULEB128(0, "EOM(2)");
  92. }
  93. LLVM_DUMP_METHOD
  94. void DIEAbbrev::print(raw_ostream &O) const {
  95. O << "Abbreviation @"
  96. << format("0x%lx", (long)(intptr_t)this)
  97. << " "
  98. << dwarf::TagString(Tag)
  99. << " "
  100. << dwarf::ChildrenString(Children)
  101. << '\n';
  102. for (unsigned i = 0, N = Data.size(); i < N; ++i) {
  103. O << " "
  104. << dwarf::AttributeString(Data[i].getAttribute())
  105. << " "
  106. << dwarf::FormEncodingString(Data[i].getForm());
  107. if (Data[i].getForm() == dwarf::DW_FORM_implicit_const)
  108. O << " " << Data[i].getValue();
  109. O << '\n';
  110. }
  111. }
  112. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  113. LLVM_DUMP_METHOD void DIEAbbrev::dump() const {
  114. print(dbgs());
  115. }
  116. #endif
  117. //===----------------------------------------------------------------------===//
  118. // DIEAbbrevSet Implementation
  119. //===----------------------------------------------------------------------===//
  120. DIEAbbrevSet::~DIEAbbrevSet() {
  121. for (DIEAbbrev *Abbrev : Abbreviations)
  122. Abbrev->~DIEAbbrev();
  123. }
  124. DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) {
  125. FoldingSetNodeID ID;
  126. DIEAbbrev Abbrev = Die.generateAbbrev();
  127. Abbrev.Profile(ID);
  128. void *InsertPos;
  129. if (DIEAbbrev *Existing =
  130. AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
  131. Die.setAbbrevNumber(Existing->getNumber());
  132. return *Existing;
  133. }
  134. // Move the abbreviation to the heap and assign a number.
  135. DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev));
  136. Abbreviations.push_back(New);
  137. New->setNumber(Abbreviations.size());
  138. Die.setAbbrevNumber(Abbreviations.size());
  139. // Store it for lookup.
  140. AbbreviationsSet.InsertNode(New, InsertPos);
  141. return *New;
  142. }
  143. void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const {
  144. if (!Abbreviations.empty()) {
  145. // Start the debug abbrev section.
  146. AP->OutStreamer->SwitchSection(Section);
  147. AP->emitDwarfAbbrevs(Abbreviations);
  148. }
  149. }
  150. //===----------------------------------------------------------------------===//
  151. // DIE Implementation
  152. //===----------------------------------------------------------------------===//
  153. DIE *DIE::getParent() const {
  154. return Owner.dyn_cast<DIE*>();
  155. }
  156. DIEAbbrev DIE::generateAbbrev() const {
  157. DIEAbbrev Abbrev(Tag, hasChildren());
  158. for (const DIEValue &V : values())
  159. if (V.getForm() == dwarf::DW_FORM_implicit_const)
  160. Abbrev.AddImplicitConstAttribute(V.getAttribute(),
  161. V.getDIEInteger().getValue());
  162. else
  163. Abbrev.AddAttribute(V.getAttribute(), V.getForm());
  164. return Abbrev;
  165. }
  166. unsigned DIE::getDebugSectionOffset() const {
  167. const DIEUnit *Unit = getUnit();
  168. assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");
  169. return Unit->getDebugSectionOffset() + getOffset();
  170. }
  171. const DIE *DIE::getUnitDie() const {
  172. const DIE *p = this;
  173. while (p) {
  174. if (p->getTag() == dwarf::DW_TAG_compile_unit ||
  175. p->getTag() == dwarf::DW_TAG_type_unit)
  176. return p;
  177. p = p->getParent();
  178. }
  179. return nullptr;
  180. }
  181. DIEUnit *DIE::getUnit() const {
  182. const DIE *UnitDie = getUnitDie();
  183. if (UnitDie)
  184. return UnitDie->Owner.dyn_cast<DIEUnit*>();
  185. return nullptr;
  186. }
  187. DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
  188. // Iterate through all the attributes until we find the one we're
  189. // looking for, if we can't find it return NULL.
  190. for (const auto &V : values())
  191. if (V.getAttribute() == Attribute)
  192. return V;
  193. return DIEValue();
  194. }
  195. LLVM_DUMP_METHOD
  196. static void printValues(raw_ostream &O, const DIEValueList &Values,
  197. StringRef Type, unsigned Size, unsigned IndentCount) {
  198. O << Type << ": Size: " << Size << "\n";
  199. unsigned I = 0;
  200. const std::string Indent(IndentCount, ' ');
  201. for (const auto &V : Values.values()) {
  202. O << Indent;
  203. O << "Blk[" << I++ << "]";
  204. O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
  205. V.print(O);
  206. O << "\n";
  207. }
  208. }
  209. LLVM_DUMP_METHOD
  210. void DIE::print(raw_ostream &O, unsigned IndentCount) const {
  211. const std::string Indent(IndentCount, ' ');
  212. O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
  213. << ", Offset: " << Offset << ", Size: " << Size << "\n";
  214. O << Indent << dwarf::TagString(getTag()) << " "
  215. << dwarf::ChildrenString(hasChildren()) << "\n";
  216. IndentCount += 2;
  217. for (const auto &V : values()) {
  218. O << Indent;
  219. O << dwarf::AttributeString(V.getAttribute());
  220. O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
  221. V.print(O);
  222. O << "\n";
  223. }
  224. IndentCount -= 2;
  225. for (const auto &Child : children())
  226. Child.print(O, IndentCount + 4);
  227. O << "\n";
  228. }
  229. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  230. LLVM_DUMP_METHOD void DIE::dump() const {
  231. print(dbgs());
  232. }
  233. #endif
  234. unsigned DIE::computeOffsetsAndAbbrevs(const AsmPrinter *AP,
  235. DIEAbbrevSet &AbbrevSet,
  236. unsigned CUOffset) {
  237. // Unique the abbreviation and fill in the abbreviation number so this DIE
  238. // can be emitted.
  239. const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(*this);
  240. // Set compile/type unit relative offset of this DIE.
  241. setOffset(CUOffset);
  242. // Add the byte size of the abbreviation code.
  243. CUOffset += getULEB128Size(getAbbrevNumber());
  244. // Add the byte size of all the DIE attribute values.
  245. for (const auto &V : values())
  246. CUOffset += V.SizeOf(AP);
  247. // Let the children compute their offsets and abbreviation numbers.
  248. if (hasChildren()) {
  249. (void)Abbrev;
  250. assert(Abbrev.hasChildren() && "Children flag not set");
  251. for (auto &Child : children())
  252. CUOffset = Child.computeOffsetsAndAbbrevs(AP, AbbrevSet, CUOffset);
  253. // Each child chain is terminated with a zero byte, adjust the offset.
  254. CUOffset += sizeof(int8_t);
  255. }
  256. // Compute the byte size of this DIE and all of its children correctly. This
  257. // is needed so that top level DIE can help the compile unit set its length
  258. // correctly.
  259. setSize(CUOffset - getOffset());
  260. return CUOffset;
  261. }
  262. //===----------------------------------------------------------------------===//
  263. // DIEUnit Implementation
  264. //===----------------------------------------------------------------------===//
  265. DIEUnit::DIEUnit(uint16_t V, uint8_t A, dwarf::Tag UnitTag)
  266. : Die(UnitTag), Section(nullptr), Offset(0), Length(0), Version(V),
  267. AddrSize(A)
  268. {
  269. Die.Owner = this;
  270. assert((UnitTag == dwarf::DW_TAG_compile_unit ||
  271. UnitTag == dwarf::DW_TAG_type_unit ||
  272. UnitTag == dwarf::DW_TAG_partial_unit) && "expected a unit TAG");
  273. }
  274. void DIEValue::EmitValue(const AsmPrinter *AP) const {
  275. switch (Ty) {
  276. case isNone:
  277. llvm_unreachable("Expected valid DIEValue");
  278. #define HANDLE_DIEVALUE(T) \
  279. case is##T: \
  280. getDIE##T().EmitValue(AP, Form); \
  281. break;
  282. #include "llvm/CodeGen/DIEValue.def"
  283. }
  284. }
  285. unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
  286. switch (Ty) {
  287. case isNone:
  288. llvm_unreachable("Expected valid DIEValue");
  289. #define HANDLE_DIEVALUE(T) \
  290. case is##T: \
  291. return getDIE##T().SizeOf(AP, Form);
  292. #include "llvm/CodeGen/DIEValue.def"
  293. }
  294. llvm_unreachable("Unknown DIE kind");
  295. }
  296. LLVM_DUMP_METHOD
  297. void DIEValue::print(raw_ostream &O) const {
  298. switch (Ty) {
  299. case isNone:
  300. llvm_unreachable("Expected valid DIEValue");
  301. #define HANDLE_DIEVALUE(T) \
  302. case is##T: \
  303. getDIE##T().print(O); \
  304. break;
  305. #include "llvm/CodeGen/DIEValue.def"
  306. }
  307. }
  308. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  309. LLVM_DUMP_METHOD void DIEValue::dump() const {
  310. print(dbgs());
  311. }
  312. #endif
  313. //===----------------------------------------------------------------------===//
  314. // DIEInteger Implementation
  315. //===----------------------------------------------------------------------===//
  316. /// EmitValue - Emit integer of appropriate size.
  317. ///
  318. void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
  319. switch (Form) {
  320. case dwarf::DW_FORM_implicit_const:
  321. case dwarf::DW_FORM_flag_present:
  322. // Emit something to keep the lines and comments in sync.
  323. // FIXME: Is there a better way to do this?
  324. Asm->OutStreamer->AddBlankLine();
  325. return;
  326. case dwarf::DW_FORM_flag:
  327. case dwarf::DW_FORM_ref1:
  328. case dwarf::DW_FORM_data1:
  329. case dwarf::DW_FORM_strx1:
  330. case dwarf::DW_FORM_addrx1:
  331. case dwarf::DW_FORM_ref2:
  332. case dwarf::DW_FORM_data2:
  333. case dwarf::DW_FORM_strx2:
  334. case dwarf::DW_FORM_addrx2:
  335. case dwarf::DW_FORM_strx3:
  336. case dwarf::DW_FORM_strp:
  337. case dwarf::DW_FORM_ref4:
  338. case dwarf::DW_FORM_data4:
  339. case dwarf::DW_FORM_ref_sup4:
  340. case dwarf::DW_FORM_strx4:
  341. case dwarf::DW_FORM_addrx4:
  342. case dwarf::DW_FORM_ref8:
  343. case dwarf::DW_FORM_ref_sig8:
  344. case dwarf::DW_FORM_data8:
  345. case dwarf::DW_FORM_ref_sup8:
  346. case dwarf::DW_FORM_GNU_ref_alt:
  347. case dwarf::DW_FORM_GNU_strp_alt:
  348. case dwarf::DW_FORM_line_strp:
  349. case dwarf::DW_FORM_sec_offset:
  350. case dwarf::DW_FORM_strp_sup:
  351. case dwarf::DW_FORM_addr:
  352. case dwarf::DW_FORM_ref_addr:
  353. Asm->OutStreamer->EmitIntValue(Integer, SizeOf(Asm, Form));
  354. return;
  355. case dwarf::DW_FORM_GNU_str_index:
  356. case dwarf::DW_FORM_GNU_addr_index:
  357. case dwarf::DW_FORM_ref_udata:
  358. case dwarf::DW_FORM_strx:
  359. case dwarf::DW_FORM_addrx:
  360. case dwarf::DW_FORM_rnglistx:
  361. case dwarf::DW_FORM_udata:
  362. Asm->EmitULEB128(Integer);
  363. return;
  364. case dwarf::DW_FORM_sdata:
  365. Asm->EmitSLEB128(Integer);
  366. return;
  367. default: llvm_unreachable("DIE Value form not supported yet");
  368. }
  369. }
  370. /// SizeOf - Determine size of integer value in bytes.
  371. ///
  372. unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  373. dwarf::FormParams Params = {0, 0, dwarf::DWARF32};
  374. if (AP)
  375. Params = {AP->getDwarfVersion(), uint8_t(AP->getPointerSize()),
  376. AP->OutStreamer->getContext().getDwarfFormat()};
  377. if (Optional<uint8_t> FixedSize = dwarf::getFixedFormByteSize(Form, Params))
  378. return *FixedSize;
  379. switch (Form) {
  380. case dwarf::DW_FORM_GNU_str_index:
  381. case dwarf::DW_FORM_GNU_addr_index:
  382. case dwarf::DW_FORM_ref_udata:
  383. case dwarf::DW_FORM_strx:
  384. case dwarf::DW_FORM_addrx:
  385. case dwarf::DW_FORM_rnglistx:
  386. case dwarf::DW_FORM_udata:
  387. return getULEB128Size(Integer);
  388. case dwarf::DW_FORM_sdata:
  389. return getSLEB128Size(Integer);
  390. default: llvm_unreachable("DIE Value form not supported yet");
  391. }
  392. }
  393. LLVM_DUMP_METHOD
  394. void DIEInteger::print(raw_ostream &O) const {
  395. O << "Int: " << (int64_t)Integer << " 0x";
  396. O.write_hex(Integer);
  397. }
  398. //===----------------------------------------------------------------------===//
  399. // DIEExpr Implementation
  400. //===----------------------------------------------------------------------===//
  401. /// EmitValue - Emit expression value.
  402. ///
  403. void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  404. AP->EmitDebugValue(Expr, SizeOf(AP, Form));
  405. }
  406. /// SizeOf - Determine size of expression value in bytes.
  407. ///
  408. unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  409. if (Form == dwarf::DW_FORM_data4) return 4;
  410. if (Form == dwarf::DW_FORM_sec_offset) return 4;
  411. if (Form == dwarf::DW_FORM_strp) return 4;
  412. return AP->getPointerSize();
  413. }
  414. LLVM_DUMP_METHOD
  415. void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
  416. //===----------------------------------------------------------------------===//
  417. // DIELabel Implementation
  418. //===----------------------------------------------------------------------===//
  419. /// EmitValue - Emit label value.
  420. ///
  421. void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  422. AP->EmitLabelReference(Label, SizeOf(AP, Form),
  423. Form == dwarf::DW_FORM_strp ||
  424. Form == dwarf::DW_FORM_sec_offset ||
  425. Form == dwarf::DW_FORM_ref_addr ||
  426. Form == dwarf::DW_FORM_data4);
  427. }
  428. /// SizeOf - Determine size of label value in bytes.
  429. ///
  430. unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  431. if (Form == dwarf::DW_FORM_data4) return 4;
  432. if (Form == dwarf::DW_FORM_sec_offset) return 4;
  433. if (Form == dwarf::DW_FORM_strp) return 4;
  434. return AP->MAI->getCodePointerSize();
  435. }
  436. LLVM_DUMP_METHOD
  437. void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
  438. //===----------------------------------------------------------------------===//
  439. // DIEBaseTypeRef Implementation
  440. //===----------------------------------------------------------------------===//
  441. void DIEBaseTypeRef::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  442. uint64_t Offset = CU->ExprRefedBaseTypes[Index].Die->getOffset();
  443. assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
  444. AP->EmitULEB128(Offset, nullptr, ULEB128PadSize);
  445. }
  446. unsigned DIEBaseTypeRef::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  447. return ULEB128PadSize;
  448. }
  449. LLVM_DUMP_METHOD
  450. void DIEBaseTypeRef::print(raw_ostream &O) const { O << "BaseTypeRef: " << Index; }
  451. //===----------------------------------------------------------------------===//
  452. // DIEDelta Implementation
  453. //===----------------------------------------------------------------------===//
  454. /// EmitValue - Emit delta value.
  455. ///
  456. void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  457. AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
  458. }
  459. /// SizeOf - Determine size of delta value in bytes.
  460. ///
  461. unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  462. if (Form == dwarf::DW_FORM_data4) return 4;
  463. if (Form == dwarf::DW_FORM_sec_offset) return 4;
  464. if (Form == dwarf::DW_FORM_strp) return 4;
  465. return AP->MAI->getCodePointerSize();
  466. }
  467. LLVM_DUMP_METHOD
  468. void DIEDelta::print(raw_ostream &O) const {
  469. O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
  470. }
  471. //===----------------------------------------------------------------------===//
  472. // DIEString Implementation
  473. //===----------------------------------------------------------------------===//
  474. /// EmitValue - Emit string value.
  475. ///
  476. void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  477. // Index of string in symbol table.
  478. switch (Form) {
  479. case dwarf::DW_FORM_GNU_str_index:
  480. case dwarf::DW_FORM_strx:
  481. case dwarf::DW_FORM_strx1:
  482. case dwarf::DW_FORM_strx2:
  483. case dwarf::DW_FORM_strx3:
  484. case dwarf::DW_FORM_strx4:
  485. DIEInteger(S.getIndex()).EmitValue(AP, Form);
  486. return;
  487. case dwarf::DW_FORM_strp:
  488. if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
  489. DIELabel(S.getSymbol()).EmitValue(AP, Form);
  490. else
  491. DIEInteger(S.getOffset()).EmitValue(AP, Form);
  492. return;
  493. default:
  494. llvm_unreachable("Expected valid string form");
  495. }
  496. }
  497. /// SizeOf - Determine size of delta value in bytes.
  498. ///
  499. unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  500. // Index of string in symbol table.
  501. switch (Form) {
  502. case dwarf::DW_FORM_GNU_str_index:
  503. case dwarf::DW_FORM_strx:
  504. case dwarf::DW_FORM_strx1:
  505. case dwarf::DW_FORM_strx2:
  506. case dwarf::DW_FORM_strx3:
  507. case dwarf::DW_FORM_strx4:
  508. return DIEInteger(S.getIndex()).SizeOf(AP, Form);
  509. case dwarf::DW_FORM_strp:
  510. if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
  511. return DIELabel(S.getSymbol()).SizeOf(AP, Form);
  512. return DIEInteger(S.getOffset()).SizeOf(AP, Form);
  513. default:
  514. llvm_unreachable("Expected valid string form");
  515. }
  516. }
  517. LLVM_DUMP_METHOD
  518. void DIEString::print(raw_ostream &O) const {
  519. O << "String: " << S.getString();
  520. }
  521. //===----------------------------------------------------------------------===//
  522. // DIEInlineString Implementation
  523. //===----------------------------------------------------------------------===//
  524. void DIEInlineString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  525. if (Form == dwarf::DW_FORM_string) {
  526. AP->OutStreamer->EmitBytes(S);
  527. AP->emitInt8(0);
  528. return;
  529. }
  530. llvm_unreachable("Expected valid string form");
  531. }
  532. unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  533. // Emit string bytes + NULL byte.
  534. return S.size() + 1;
  535. }
  536. LLVM_DUMP_METHOD
  537. void DIEInlineString::print(raw_ostream &O) const {
  538. O << "InlineString: " << S;
  539. }
  540. //===----------------------------------------------------------------------===//
  541. // DIEEntry Implementation
  542. //===----------------------------------------------------------------------===//
  543. /// EmitValue - Emit debug information entry offset.
  544. ///
  545. void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  546. switch (Form) {
  547. case dwarf::DW_FORM_ref1:
  548. case dwarf::DW_FORM_ref2:
  549. case dwarf::DW_FORM_ref4:
  550. case dwarf::DW_FORM_ref8:
  551. AP->OutStreamer->EmitIntValue(Entry->getOffset(), SizeOf(AP, Form));
  552. return;
  553. case dwarf::DW_FORM_ref_udata:
  554. AP->EmitULEB128(Entry->getOffset());
  555. return;
  556. case dwarf::DW_FORM_ref_addr: {
  557. // Get the absolute offset for this DIE within the debug info/types section.
  558. unsigned Addr = Entry->getDebugSectionOffset();
  559. if (const MCSymbol *SectionSym =
  560. Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {
  561. AP->EmitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form), true);
  562. return;
  563. }
  564. AP->OutStreamer->EmitIntValue(Addr, SizeOf(AP, Form));
  565. return;
  566. }
  567. default:
  568. llvm_unreachable("Improper form for DIE reference");
  569. }
  570. }
  571. unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  572. switch (Form) {
  573. case dwarf::DW_FORM_ref1:
  574. return 1;
  575. case dwarf::DW_FORM_ref2:
  576. return 2;
  577. case dwarf::DW_FORM_ref4:
  578. return 4;
  579. case dwarf::DW_FORM_ref8:
  580. return 8;
  581. case dwarf::DW_FORM_ref_udata:
  582. return getULEB128Size(Entry->getOffset());
  583. case dwarf::DW_FORM_ref_addr:
  584. if (AP->getDwarfVersion() == 2)
  585. return AP->MAI->getCodePointerSize();
  586. switch (AP->OutStreamer->getContext().getDwarfFormat()) {
  587. case dwarf::DWARF32:
  588. return 4;
  589. case dwarf::DWARF64:
  590. return 8;
  591. }
  592. llvm_unreachable("Invalid DWARF format");
  593. default:
  594. llvm_unreachable("Improper form for DIE reference");
  595. }
  596. }
  597. LLVM_DUMP_METHOD
  598. void DIEEntry::print(raw_ostream &O) const {
  599. O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
  600. }
  601. //===----------------------------------------------------------------------===//
  602. // DIELoc Implementation
  603. //===----------------------------------------------------------------------===//
  604. /// ComputeSize - calculate the size of the location expression.
  605. ///
  606. unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
  607. if (!Size) {
  608. for (const auto &V : values())
  609. Size += V.SizeOf(AP);
  610. }
  611. return Size;
  612. }
  613. /// EmitValue - Emit location data.
  614. ///
  615. void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
  616. switch (Form) {
  617. default: llvm_unreachable("Improper form for block");
  618. case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
  619. case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
  620. case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
  621. case dwarf::DW_FORM_block:
  622. case dwarf::DW_FORM_exprloc:
  623. Asm->EmitULEB128(Size); break;
  624. }
  625. for (const auto &V : values())
  626. V.EmitValue(Asm);
  627. }
  628. /// SizeOf - Determine size of location data in bytes.
  629. ///
  630. unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  631. switch (Form) {
  632. case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
  633. case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
  634. case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
  635. case dwarf::DW_FORM_block:
  636. case dwarf::DW_FORM_exprloc:
  637. return Size + getULEB128Size(Size);
  638. default: llvm_unreachable("Improper form for block");
  639. }
  640. }
  641. LLVM_DUMP_METHOD
  642. void DIELoc::print(raw_ostream &O) const {
  643. printValues(O, *this, "ExprLoc", Size, 5);
  644. }
  645. //===----------------------------------------------------------------------===//
  646. // DIEBlock Implementation
  647. //===----------------------------------------------------------------------===//
  648. /// ComputeSize - calculate the size of the block.
  649. ///
  650. unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
  651. if (!Size) {
  652. for (const auto &V : values())
  653. Size += V.SizeOf(AP);
  654. }
  655. return Size;
  656. }
  657. /// EmitValue - Emit block data.
  658. ///
  659. void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
  660. switch (Form) {
  661. default: llvm_unreachable("Improper form for block");
  662. case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
  663. case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
  664. case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
  665. case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
  666. case dwarf::DW_FORM_string: break;
  667. case dwarf::DW_FORM_data16: break;
  668. }
  669. for (const auto &V : values())
  670. V.EmitValue(Asm);
  671. }
  672. /// SizeOf - Determine size of block data in bytes.
  673. ///
  674. unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  675. switch (Form) {
  676. case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
  677. case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
  678. case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
  679. case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
  680. case dwarf::DW_FORM_data16: return 16;
  681. default: llvm_unreachable("Improper form for block");
  682. }
  683. }
  684. LLVM_DUMP_METHOD
  685. void DIEBlock::print(raw_ostream &O) const {
  686. printValues(O, *this, "Blk", Size, 5);
  687. }
  688. //===----------------------------------------------------------------------===//
  689. // DIELocList Implementation
  690. //===----------------------------------------------------------------------===//
  691. unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  692. if (Form == dwarf::DW_FORM_data4)
  693. return 4;
  694. if (Form == dwarf::DW_FORM_sec_offset)
  695. return 4;
  696. return AP->MAI->getCodePointerSize();
  697. }
  698. /// EmitValue - Emit label value.
  699. ///
  700. void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  701. DwarfDebug *DD = AP->getDwarfDebug();
  702. MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
  703. AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
  704. }
  705. LLVM_DUMP_METHOD
  706. void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }