DIE.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. uint64_t 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 dwarf::FormParams &FormParams,
  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(FormParams);
  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 =
  253. Child.computeOffsetsAndAbbrevs(FormParams, AbbrevSet, CUOffset);
  254. // Each child chain is terminated with a zero byte, adjust the offset.
  255. CUOffset += sizeof(int8_t);
  256. }
  257. // Compute the byte size of this DIE and all of its children correctly. This
  258. // is needed so that top level DIE can help the compile unit set its length
  259. // correctly.
  260. setSize(CUOffset - getOffset());
  261. return CUOffset;
  262. }
  263. //===----------------------------------------------------------------------===//
  264. // DIEUnit Implementation
  265. //===----------------------------------------------------------------------===//
  266. DIEUnit::DIEUnit(dwarf::Tag UnitTag) : Die(UnitTag) {
  267. Die.Owner = this;
  268. assert((UnitTag == dwarf::DW_TAG_compile_unit ||
  269. UnitTag == dwarf::DW_TAG_skeleton_unit ||
  270. UnitTag == dwarf::DW_TAG_type_unit ||
  271. UnitTag == dwarf::DW_TAG_partial_unit) &&
  272. "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 dwarf::FormParams &FormParams) 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(FormParams, 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,
  354. sizeOf(Asm->getDwarfFormParams(), Form));
  355. return;
  356. case dwarf::DW_FORM_GNU_str_index:
  357. case dwarf::DW_FORM_GNU_addr_index:
  358. case dwarf::DW_FORM_ref_udata:
  359. case dwarf::DW_FORM_strx:
  360. case dwarf::DW_FORM_addrx:
  361. case dwarf::DW_FORM_rnglistx:
  362. case dwarf::DW_FORM_udata:
  363. Asm->emitULEB128(Integer);
  364. return;
  365. case dwarf::DW_FORM_sdata:
  366. Asm->emitSLEB128(Integer);
  367. return;
  368. default: llvm_unreachable("DIE Value form not supported yet");
  369. }
  370. }
  371. /// sizeOf - Determine size of integer value in bytes.
  372. ///
  373. unsigned DIEInteger::sizeOf(const dwarf::FormParams &FormParams,
  374. dwarf::Form Form) const {
  375. if (Optional<uint8_t> FixedSize =
  376. dwarf::getFixedFormByteSize(Form, FormParams))
  377. return *FixedSize;
  378. switch (Form) {
  379. case dwarf::DW_FORM_GNU_str_index:
  380. case dwarf::DW_FORM_GNU_addr_index:
  381. case dwarf::DW_FORM_ref_udata:
  382. case dwarf::DW_FORM_strx:
  383. case dwarf::DW_FORM_addrx:
  384. case dwarf::DW_FORM_rnglistx:
  385. case dwarf::DW_FORM_udata:
  386. return getULEB128Size(Integer);
  387. case dwarf::DW_FORM_sdata:
  388. return getSLEB128Size(Integer);
  389. default: llvm_unreachable("DIE Value form not supported yet");
  390. }
  391. }
  392. LLVM_DUMP_METHOD
  393. void DIEInteger::print(raw_ostream &O) const {
  394. O << "Int: " << (int64_t)Integer << " 0x";
  395. O.write_hex(Integer);
  396. }
  397. //===----------------------------------------------------------------------===//
  398. // DIEExpr Implementation
  399. //===----------------------------------------------------------------------===//
  400. /// EmitValue - Emit expression value.
  401. ///
  402. void DIEExpr::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  403. AP->emitDebugValue(Expr, sizeOf(AP->getDwarfFormParams(), Form));
  404. }
  405. /// SizeOf - Determine size of expression value in bytes.
  406. ///
  407. unsigned DIEExpr::sizeOf(const dwarf::FormParams &FormParams,
  408. dwarf::Form Form) const {
  409. switch (Form) {
  410. case dwarf::DW_FORM_data4:
  411. return 4;
  412. case dwarf::DW_FORM_data8:
  413. return 8;
  414. case dwarf::DW_FORM_sec_offset:
  415. return FormParams.getDwarfOffsetByteSize();
  416. default:
  417. llvm_unreachable("DIE Value form not supported yet");
  418. }
  419. }
  420. LLVM_DUMP_METHOD
  421. void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
  422. //===----------------------------------------------------------------------===//
  423. // DIELabel Implementation
  424. //===----------------------------------------------------------------------===//
  425. /// EmitValue - Emit label value.
  426. ///
  427. void DIELabel::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  428. bool IsSectionRelative = Form != dwarf::DW_FORM_addr;
  429. AP->emitLabelReference(Label, sizeOf(AP->getDwarfFormParams(), Form),
  430. IsSectionRelative);
  431. }
  432. /// sizeOf - Determine size of label value in bytes.
  433. ///
  434. unsigned DIELabel::sizeOf(const dwarf::FormParams &FormParams,
  435. dwarf::Form Form) const {
  436. switch (Form) {
  437. case dwarf::DW_FORM_data4:
  438. return 4;
  439. case dwarf::DW_FORM_data8:
  440. return 8;
  441. case dwarf::DW_FORM_sec_offset:
  442. case dwarf::DW_FORM_strp:
  443. return FormParams.getDwarfOffsetByteSize();
  444. case dwarf::DW_FORM_addr:
  445. return FormParams.AddrSize;
  446. default:
  447. llvm_unreachable("DIE Value form not supported yet");
  448. }
  449. }
  450. LLVM_DUMP_METHOD
  451. void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
  452. //===----------------------------------------------------------------------===//
  453. // DIEBaseTypeRef Implementation
  454. //===----------------------------------------------------------------------===//
  455. void DIEBaseTypeRef::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  456. uint64_t Offset = CU->ExprRefedBaseTypes[Index].Die->getOffset();
  457. assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
  458. AP->emitULEB128(Offset, nullptr, ULEB128PadSize);
  459. }
  460. unsigned DIEBaseTypeRef::sizeOf(const dwarf::FormParams &, dwarf::Form) const {
  461. return ULEB128PadSize;
  462. }
  463. LLVM_DUMP_METHOD
  464. void DIEBaseTypeRef::print(raw_ostream &O) const { O << "BaseTypeRef: " << Index; }
  465. //===----------------------------------------------------------------------===//
  466. // DIEDelta Implementation
  467. //===----------------------------------------------------------------------===//
  468. /// EmitValue - Emit delta value.
  469. ///
  470. void DIEDelta::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  471. AP->emitLabelDifference(LabelHi, LabelLo,
  472. sizeOf(AP->getDwarfFormParams(), Form));
  473. }
  474. /// SizeOf - Determine size of delta value in bytes.
  475. ///
  476. unsigned DIEDelta::sizeOf(const dwarf::FormParams &FormParams,
  477. dwarf::Form Form) const {
  478. switch (Form) {
  479. case dwarf::DW_FORM_data4:
  480. return 4;
  481. case dwarf::DW_FORM_data8:
  482. return 8;
  483. case dwarf::DW_FORM_sec_offset:
  484. return FormParams.getDwarfOffsetByteSize();
  485. default:
  486. llvm_unreachable("DIE Value form not supported yet");
  487. }
  488. }
  489. LLVM_DUMP_METHOD
  490. void DIEDelta::print(raw_ostream &O) const {
  491. O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
  492. }
  493. //===----------------------------------------------------------------------===//
  494. // DIEString Implementation
  495. //===----------------------------------------------------------------------===//
  496. /// EmitValue - Emit string value.
  497. ///
  498. void DIEString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  499. // Index of string in symbol table.
  500. switch (Form) {
  501. case dwarf::DW_FORM_GNU_str_index:
  502. case dwarf::DW_FORM_strx:
  503. case dwarf::DW_FORM_strx1:
  504. case dwarf::DW_FORM_strx2:
  505. case dwarf::DW_FORM_strx3:
  506. case dwarf::DW_FORM_strx4:
  507. DIEInteger(S.getIndex()).emitValue(AP, Form);
  508. return;
  509. case dwarf::DW_FORM_strp:
  510. if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
  511. DIELabel(S.getSymbol()).emitValue(AP, Form);
  512. else
  513. DIEInteger(S.getOffset()).emitValue(AP, Form);
  514. return;
  515. default:
  516. llvm_unreachable("Expected valid string form");
  517. }
  518. }
  519. /// sizeOf - Determine size of delta value in bytes.
  520. ///
  521. unsigned DIEString::sizeOf(const dwarf::FormParams &FormParams,
  522. dwarf::Form Form) const {
  523. // Index of string in symbol table.
  524. switch (Form) {
  525. case dwarf::DW_FORM_GNU_str_index:
  526. case dwarf::DW_FORM_strx:
  527. case dwarf::DW_FORM_strx1:
  528. case dwarf::DW_FORM_strx2:
  529. case dwarf::DW_FORM_strx3:
  530. case dwarf::DW_FORM_strx4:
  531. return DIEInteger(S.getIndex()).sizeOf(FormParams, Form);
  532. case dwarf::DW_FORM_strp:
  533. if (FormParams.DwarfUsesRelocationsAcrossSections)
  534. return DIELabel(S.getSymbol()).sizeOf(FormParams, Form);
  535. return DIEInteger(S.getOffset()).sizeOf(FormParams, Form);
  536. default:
  537. llvm_unreachable("Expected valid string form");
  538. }
  539. }
  540. LLVM_DUMP_METHOD
  541. void DIEString::print(raw_ostream &O) const {
  542. O << "String: " << S.getString();
  543. }
  544. //===----------------------------------------------------------------------===//
  545. // DIEInlineString Implementation
  546. //===----------------------------------------------------------------------===//
  547. void DIEInlineString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  548. if (Form == dwarf::DW_FORM_string) {
  549. AP->OutStreamer->emitBytes(S);
  550. AP->emitInt8(0);
  551. return;
  552. }
  553. llvm_unreachable("Expected valid string form");
  554. }
  555. unsigned DIEInlineString::sizeOf(const dwarf::FormParams &, dwarf::Form) const {
  556. // Emit string bytes + NULL byte.
  557. return S.size() + 1;
  558. }
  559. LLVM_DUMP_METHOD
  560. void DIEInlineString::print(raw_ostream &O) const {
  561. O << "InlineString: " << S;
  562. }
  563. //===----------------------------------------------------------------------===//
  564. // DIEEntry Implementation
  565. //===----------------------------------------------------------------------===//
  566. /// EmitValue - Emit debug information entry offset.
  567. ///
  568. void DIEEntry::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  569. switch (Form) {
  570. case dwarf::DW_FORM_ref1:
  571. case dwarf::DW_FORM_ref2:
  572. case dwarf::DW_FORM_ref4:
  573. case dwarf::DW_FORM_ref8:
  574. AP->OutStreamer->emitIntValue(Entry->getOffset(),
  575. sizeOf(AP->getDwarfFormParams(), Form));
  576. return;
  577. case dwarf::DW_FORM_ref_udata:
  578. AP->emitULEB128(Entry->getOffset());
  579. return;
  580. case dwarf::DW_FORM_ref_addr: {
  581. // Get the absolute offset for this DIE within the debug info/types section.
  582. uint64_t Addr = Entry->getDebugSectionOffset();
  583. if (const MCSymbol *SectionSym =
  584. Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {
  585. AP->emitLabelPlusOffset(SectionSym, Addr,
  586. sizeOf(AP->getDwarfFormParams(), Form), true);
  587. return;
  588. }
  589. AP->OutStreamer->emitIntValue(Addr, sizeOf(AP->getDwarfFormParams(), Form));
  590. return;
  591. }
  592. default:
  593. llvm_unreachable("Improper form for DIE reference");
  594. }
  595. }
  596. unsigned DIEEntry::sizeOf(const dwarf::FormParams &FormParams,
  597. dwarf::Form Form) const {
  598. switch (Form) {
  599. case dwarf::DW_FORM_ref1:
  600. return 1;
  601. case dwarf::DW_FORM_ref2:
  602. return 2;
  603. case dwarf::DW_FORM_ref4:
  604. return 4;
  605. case dwarf::DW_FORM_ref8:
  606. return 8;
  607. case dwarf::DW_FORM_ref_udata:
  608. return getULEB128Size(Entry->getOffset());
  609. case dwarf::DW_FORM_ref_addr:
  610. return FormParams.getRefAddrByteSize();
  611. default:
  612. llvm_unreachable("Improper form for DIE reference");
  613. }
  614. }
  615. LLVM_DUMP_METHOD
  616. void DIEEntry::print(raw_ostream &O) const {
  617. O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
  618. }
  619. //===----------------------------------------------------------------------===//
  620. // DIELoc Implementation
  621. //===----------------------------------------------------------------------===//
  622. unsigned DIELoc::computeSize(const dwarf::FormParams &FormParams) const {
  623. if (!Size) {
  624. for (const auto &V : values())
  625. Size += V.sizeOf(FormParams);
  626. }
  627. return Size;
  628. }
  629. /// EmitValue - Emit location data.
  630. ///
  631. void DIELoc::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
  632. switch (Form) {
  633. default: llvm_unreachable("Improper form for block");
  634. case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
  635. case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
  636. case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
  637. case dwarf::DW_FORM_block:
  638. case dwarf::DW_FORM_exprloc:
  639. Asm->emitULEB128(Size);
  640. break;
  641. }
  642. for (const auto &V : values())
  643. V.emitValue(Asm);
  644. }
  645. /// sizeOf - Determine size of location data in bytes.
  646. ///
  647. unsigned DIELoc::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const {
  648. switch (Form) {
  649. case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
  650. case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
  651. case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
  652. case dwarf::DW_FORM_block:
  653. case dwarf::DW_FORM_exprloc:
  654. return Size + getULEB128Size(Size);
  655. default: llvm_unreachable("Improper form for block");
  656. }
  657. }
  658. LLVM_DUMP_METHOD
  659. void DIELoc::print(raw_ostream &O) const {
  660. printValues(O, *this, "ExprLoc", Size, 5);
  661. }
  662. //===----------------------------------------------------------------------===//
  663. // DIEBlock Implementation
  664. //===----------------------------------------------------------------------===//
  665. unsigned DIEBlock::computeSize(const dwarf::FormParams &FormParams) const {
  666. if (!Size) {
  667. for (const auto &V : values())
  668. Size += V.sizeOf(FormParams);
  669. }
  670. return Size;
  671. }
  672. /// EmitValue - Emit block data.
  673. ///
  674. void DIEBlock::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
  675. switch (Form) {
  676. default: llvm_unreachable("Improper form for block");
  677. case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
  678. case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
  679. case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
  680. case dwarf::DW_FORM_exprloc:
  681. case dwarf::DW_FORM_block:
  682. Asm->emitULEB128(Size);
  683. break;
  684. case dwarf::DW_FORM_string: break;
  685. case dwarf::DW_FORM_data16: break;
  686. }
  687. for (const auto &V : values())
  688. V.emitValue(Asm);
  689. }
  690. /// sizeOf - Determine size of block data in bytes.
  691. ///
  692. unsigned DIEBlock::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const {
  693. switch (Form) {
  694. case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
  695. case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
  696. case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
  697. case dwarf::DW_FORM_exprloc:
  698. case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
  699. case dwarf::DW_FORM_data16: return 16;
  700. default: llvm_unreachable("Improper form for block");
  701. }
  702. }
  703. LLVM_DUMP_METHOD
  704. void DIEBlock::print(raw_ostream &O) const {
  705. printValues(O, *this, "Blk", Size, 5);
  706. }
  707. //===----------------------------------------------------------------------===//
  708. // DIELocList Implementation
  709. //===----------------------------------------------------------------------===//
  710. unsigned DIELocList::sizeOf(const dwarf::FormParams &FormParams,
  711. dwarf::Form Form) const {
  712. switch (Form) {
  713. case dwarf::DW_FORM_loclistx:
  714. return getULEB128Size(Index);
  715. case dwarf::DW_FORM_data4:
  716. assert(FormParams.Format != dwarf::DWARF64 &&
  717. "DW_FORM_data4 is not suitable to emit a pointer to a location list "
  718. "in the 64-bit DWARF format");
  719. return 4;
  720. case dwarf::DW_FORM_data8:
  721. assert(FormParams.Format == dwarf::DWARF64 &&
  722. "DW_FORM_data8 is not suitable to emit a pointer to a location list "
  723. "in the 32-bit DWARF format");
  724. return 8;
  725. case dwarf::DW_FORM_sec_offset:
  726. return FormParams.getDwarfOffsetByteSize();
  727. default:
  728. llvm_unreachable("DIE Value form not supported yet");
  729. }
  730. }
  731. /// EmitValue - Emit label value.
  732. ///
  733. void DIELocList::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  734. if (Form == dwarf::DW_FORM_loclistx) {
  735. AP->emitULEB128(Index);
  736. return;
  737. }
  738. DwarfDebug *DD = AP->getDwarfDebug();
  739. MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
  740. AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
  741. }
  742. LLVM_DUMP_METHOD
  743. void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }
  744. //===----------------------------------------------------------------------===//
  745. // DIEAddrOffset Implementation
  746. //===----------------------------------------------------------------------===//
  747. unsigned DIEAddrOffset::sizeOf(const dwarf::FormParams &FormParams,
  748. dwarf::Form) const {
  749. return Addr.sizeOf(FormParams, dwarf::DW_FORM_addrx) +
  750. Offset.sizeOf(FormParams, dwarf::DW_FORM_data4);
  751. }
  752. /// EmitValue - Emit label value.
  753. ///
  754. void DIEAddrOffset::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  755. Addr.emitValue(AP, dwarf::DW_FORM_addrx);
  756. Offset.emitValue(AP, dwarf::DW_FORM_data4);
  757. }
  758. LLVM_DUMP_METHOD
  759. void DIEAddrOffset::print(raw_ostream &O) const {
  760. O << "AddrOffset: ";
  761. Addr.print(O);
  762. O << " + ";
  763. Offset.print(O);
  764. }