DIE.cpp 27 KB

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