DWARFUnit.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFUnit.h ----------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
  14. #define LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
  15. #include "llvm/ADT/DenseSet.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/iterator_range.h"
  20. #include "llvm/BinaryFormat/Dwarf.h"
  21. #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
  22. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  23. #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
  24. #include "llvm/DebugInfo/DWARF/DWARFDie.h"
  25. #include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
  26. #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
  27. #include "llvm/Support/DataExtractor.h"
  28. #include <cassert>
  29. #include <cstddef>
  30. #include <cstdint>
  31. #include <map>
  32. #include <memory>
  33. #include <set>
  34. #include <utility>
  35. #include <vector>
  36. namespace llvm {
  37. class DWARFAbbreviationDeclarationSet;
  38. class DWARFContext;
  39. class DWARFDebugAbbrev;
  40. class DWARFUnit;
  41. class DWARFDebugRangeList;
  42. class DWARFLocationTable;
  43. class DWARFObject;
  44. class raw_ostream;
  45. struct DIDumpOptions;
  46. struct DWARFSection;
  47. /// Base class describing the header of any kind of "unit." Some information
  48. /// is specific to certain unit types. We separate this class out so we can
  49. /// parse the header before deciding what specific kind of unit to construct.
  50. class DWARFUnitHeader {
  51. // Offset within section.
  52. uint64_t Offset = 0;
  53. // Version, address size, and DWARF format.
  54. dwarf::FormParams FormParams;
  55. uint64_t Length = 0;
  56. uint64_t AbbrOffset = 0;
  57. // For DWO units only.
  58. const DWARFUnitIndex::Entry *IndexEntry = nullptr;
  59. // For type units only.
  60. uint64_t TypeHash = 0;
  61. uint64_t TypeOffset = 0;
  62. // For v5 split or skeleton compile units only.
  63. std::optional<uint64_t> DWOId;
  64. // Unit type as parsed, or derived from the section kind.
  65. uint8_t UnitType = 0;
  66. // Size as parsed. uint8_t for compactness.
  67. uint8_t Size = 0;
  68. public:
  69. /// Parse a unit header from \p debug_info starting at \p offset_ptr.
  70. /// Note that \p SectionKind is used as a hint to guess the unit type
  71. /// for DWARF formats prior to DWARFv5. In DWARFv5 the unit type is
  72. /// explicitly defined in the header and the hint is ignored.
  73. bool extract(DWARFContext &Context, const DWARFDataExtractor &debug_info,
  74. uint64_t *offset_ptr, DWARFSectionKind SectionKind);
  75. // For units in DWARF Package File, remember the index entry and update
  76. // the abbreviation offset read by extract().
  77. bool applyIndexEntry(const DWARFUnitIndex::Entry *Entry);
  78. uint64_t getOffset() const { return Offset; }
  79. const dwarf::FormParams &getFormParams() const { return FormParams; }
  80. uint16_t getVersion() const { return FormParams.Version; }
  81. dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
  82. uint8_t getAddressByteSize() const { return FormParams.AddrSize; }
  83. uint8_t getRefAddrByteSize() const { return FormParams.getRefAddrByteSize(); }
  84. uint8_t getDwarfOffsetByteSize() const {
  85. return FormParams.getDwarfOffsetByteSize();
  86. }
  87. uint64_t getLength() const { return Length; }
  88. uint64_t getAbbrOffset() const { return AbbrOffset; }
  89. std::optional<uint64_t> getDWOId() const { return DWOId; }
  90. void setDWOId(uint64_t Id) {
  91. assert((!DWOId || *DWOId == Id) && "setting DWOId to a different value");
  92. DWOId = Id;
  93. }
  94. const DWARFUnitIndex::Entry *getIndexEntry() const { return IndexEntry; }
  95. uint64_t getTypeHash() const { return TypeHash; }
  96. uint64_t getTypeOffset() const { return TypeOffset; }
  97. uint8_t getUnitType() const { return UnitType; }
  98. bool isTypeUnit() const {
  99. return UnitType == dwarf::DW_UT_type || UnitType == dwarf::DW_UT_split_type;
  100. }
  101. uint8_t getSize() const { return Size; }
  102. uint8_t getUnitLengthFieldByteSize() const {
  103. return dwarf::getUnitLengthFieldByteSize(FormParams.Format);
  104. }
  105. uint64_t getNextUnitOffset() const {
  106. return Offset + Length + getUnitLengthFieldByteSize();
  107. }
  108. };
  109. const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
  110. DWARFSectionKind Kind);
  111. bool isCompileUnit(const std::unique_ptr<DWARFUnit> &U);
  112. /// Describe a collection of units. Intended to hold all units either from
  113. /// .debug_info and .debug_types, or from .debug_info.dwo and .debug_types.dwo.
  114. class DWARFUnitVector final : public SmallVector<std::unique_ptr<DWARFUnit>, 1> {
  115. std::function<std::unique_ptr<DWARFUnit>(uint64_t, DWARFSectionKind,
  116. const DWARFSection *,
  117. const DWARFUnitIndex::Entry *)>
  118. Parser;
  119. int NumInfoUnits = -1;
  120. public:
  121. using UnitVector = SmallVectorImpl<std::unique_ptr<DWARFUnit>>;
  122. using iterator = typename UnitVector::iterator;
  123. using iterator_range = llvm::iterator_range<typename UnitVector::iterator>;
  124. using compile_unit_range =
  125. decltype(make_filter_range(std::declval<iterator_range>(), isCompileUnit));
  126. DWARFUnit *getUnitForOffset(uint64_t Offset) const;
  127. DWARFUnit *getUnitForIndexEntry(const DWARFUnitIndex::Entry &E);
  128. /// Read units from a .debug_info or .debug_types section. Calls made
  129. /// before finishedInfoUnits() are assumed to be for .debug_info sections,
  130. /// calls after finishedInfoUnits() are for .debug_types sections. Caller
  131. /// must not mix calls to addUnitsForSection and addUnitsForDWOSection.
  132. void addUnitsForSection(DWARFContext &C, const DWARFSection &Section,
  133. DWARFSectionKind SectionKind);
  134. /// Read units from a .debug_info.dwo or .debug_types.dwo section. Calls
  135. /// made before finishedInfoUnits() are assumed to be for .debug_info.dwo
  136. /// sections, calls after finishedInfoUnits() are for .debug_types.dwo
  137. /// sections. Caller must not mix calls to addUnitsForSection and
  138. /// addUnitsForDWOSection.
  139. void addUnitsForDWOSection(DWARFContext &C, const DWARFSection &DWOSection,
  140. DWARFSectionKind SectionKind, bool Lazy = false);
  141. /// Add an existing DWARFUnit to this UnitVector. This is used by the DWARF
  142. /// verifier to process unit separately.
  143. DWARFUnit *addUnit(std::unique_ptr<DWARFUnit> Unit);
  144. /// Returns number of all units held by this instance.
  145. unsigned getNumUnits() const { return size(); }
  146. /// Returns number of units from all .debug_info[.dwo] sections.
  147. unsigned getNumInfoUnits() const {
  148. return NumInfoUnits == -1 ? size() : NumInfoUnits;
  149. }
  150. /// Returns number of units from all .debug_types[.dwo] sections.
  151. unsigned getNumTypesUnits() const { return size() - NumInfoUnits; }
  152. /// Indicate that parsing .debug_info[.dwo] is done, and remaining units
  153. /// will be from .debug_types[.dwo].
  154. void finishedInfoUnits() { NumInfoUnits = size(); }
  155. private:
  156. void addUnitsImpl(DWARFContext &Context, const DWARFObject &Obj,
  157. const DWARFSection &Section, const DWARFDebugAbbrev *DA,
  158. const DWARFSection *RS, const DWARFSection *LocSection,
  159. StringRef SS, const DWARFSection &SOS,
  160. const DWARFSection *AOS, const DWARFSection &LS, bool LE,
  161. bool IsDWO, bool Lazy, DWARFSectionKind SectionKind);
  162. };
  163. /// Represents base address of the CU.
  164. /// Represents a unit's contribution to the string offsets table.
  165. struct StrOffsetsContributionDescriptor {
  166. uint64_t Base = 0;
  167. /// The contribution size not including the header.
  168. uint64_t Size = 0;
  169. /// Format and version.
  170. dwarf::FormParams FormParams = {0, 0, dwarf::DwarfFormat::DWARF32};
  171. StrOffsetsContributionDescriptor(uint64_t Base, uint64_t Size,
  172. uint8_t Version, dwarf::DwarfFormat Format)
  173. : Base(Base), Size(Size), FormParams({Version, 0, Format}) {}
  174. StrOffsetsContributionDescriptor() = default;
  175. uint8_t getVersion() const { return FormParams.Version; }
  176. dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
  177. uint8_t getDwarfOffsetByteSize() const {
  178. return FormParams.getDwarfOffsetByteSize();
  179. }
  180. /// Determine whether a contribution to the string offsets table is
  181. /// consistent with the relevant section size and that its length is
  182. /// a multiple of the size of one of its entries.
  183. Expected<StrOffsetsContributionDescriptor>
  184. validateContributionSize(DWARFDataExtractor &DA);
  185. };
  186. class DWARFUnit {
  187. DWARFContext &Context;
  188. /// Section containing this DWARFUnit.
  189. const DWARFSection &InfoSection;
  190. DWARFUnitHeader Header;
  191. const DWARFDebugAbbrev *Abbrev;
  192. const DWARFSection *RangeSection;
  193. uint64_t RangeSectionBase;
  194. uint64_t LocSectionBase;
  195. /// Location table of this unit.
  196. std::unique_ptr<DWARFLocationTable> LocTable;
  197. const DWARFSection &LineSection;
  198. StringRef StringSection;
  199. const DWARFSection &StringOffsetSection;
  200. const DWARFSection *AddrOffsetSection;
  201. DWARFUnit *SU;
  202. std::optional<uint64_t> AddrOffsetSectionBase;
  203. bool IsLittleEndian;
  204. bool IsDWO;
  205. const DWARFUnitVector &UnitVector;
  206. /// Start, length, and DWARF format of the unit's contribution to the string
  207. /// offsets table (DWARF v5).
  208. std::optional<StrOffsetsContributionDescriptor>
  209. StringOffsetsTableContribution;
  210. mutable const DWARFAbbreviationDeclarationSet *Abbrevs;
  211. std::optional<object::SectionedAddress> BaseAddr;
  212. /// The compile unit debug information entry items.
  213. std::vector<DWARFDebugInfoEntry> DieArray;
  214. /// Map from range's start address to end address and corresponding DIE.
  215. /// IntervalMap does not support range removal, as a result, we use the
  216. /// std::map::upper_bound for address range lookup.
  217. std::map<uint64_t, std::pair<uint64_t, DWARFDie>> AddrDieMap;
  218. /// Map from the location (interpreted DW_AT_location) of a DW_TAG_variable,
  219. /// to the end address and the corresponding DIE.
  220. std::map<uint64_t, std::pair<uint64_t, DWARFDie>> VariableDieMap;
  221. DenseSet<uint64_t> RootsParsedForVariables;
  222. using die_iterator_range =
  223. iterator_range<std::vector<DWARFDebugInfoEntry>::iterator>;
  224. std::shared_ptr<DWARFUnit> DWO;
  225. protected:
  226. /// Return the index of a \p Die entry inside the unit's DIE vector.
  227. ///
  228. /// It is illegal to call this method with a DIE that hasn't be
  229. /// created by this unit. In other word, it's illegal to call this
  230. /// method on a DIE that isn't accessible by following
  231. /// children/sibling links starting from this unit's getUnitDIE().
  232. uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) const {
  233. auto First = DieArray.data();
  234. assert(Die >= First && Die < First + DieArray.size());
  235. return Die - First;
  236. }
  237. /// Return DWARFDebugInfoEntry for the specified index \p Index.
  238. const DWARFDebugInfoEntry *getDebugInfoEntry(unsigned Index) const {
  239. assert(Index < DieArray.size());
  240. return &DieArray[Index];
  241. }
  242. const DWARFDebugInfoEntry *
  243. getParentEntry(const DWARFDebugInfoEntry *Die) const;
  244. const DWARFDebugInfoEntry *
  245. getSiblingEntry(const DWARFDebugInfoEntry *Die) const;
  246. const DWARFDebugInfoEntry *
  247. getPreviousSiblingEntry(const DWARFDebugInfoEntry *Die) const;
  248. const DWARFDebugInfoEntry *
  249. getFirstChildEntry(const DWARFDebugInfoEntry *Die) const;
  250. const DWARFDebugInfoEntry *
  251. getLastChildEntry(const DWARFDebugInfoEntry *Die) const;
  252. const DWARFUnitHeader &getHeader() const { return Header; }
  253. /// Find the unit's contribution to the string offsets table and determine its
  254. /// length and form. The given offset is expected to be derived from the unit
  255. /// DIE's DW_AT_str_offsets_base attribute.
  256. Expected<std::optional<StrOffsetsContributionDescriptor>>
  257. determineStringOffsetsTableContribution(DWARFDataExtractor &DA);
  258. /// Find the unit's contribution to the string offsets table and determine its
  259. /// length and form. The given offset is expected to be 0 in a dwo file or,
  260. /// in a dwp file, the start of the unit's contribution to the string offsets
  261. /// table section (as determined by the index table).
  262. Expected<std::optional<StrOffsetsContributionDescriptor>>
  263. determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA);
  264. public:
  265. DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
  266. const DWARFUnitHeader &Header, const DWARFDebugAbbrev *DA,
  267. const DWARFSection *RS, const DWARFSection *LocSection,
  268. StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS,
  269. const DWARFSection &LS, bool LE, bool IsDWO,
  270. const DWARFUnitVector &UnitVector);
  271. virtual ~DWARFUnit();
  272. bool isLittleEndian() const { return IsLittleEndian; }
  273. bool isDWOUnit() const { return IsDWO; }
  274. DWARFContext& getContext() const { return Context; }
  275. const DWARFSection &getInfoSection() const { return InfoSection; }
  276. uint64_t getOffset() const { return Header.getOffset(); }
  277. const dwarf::FormParams &getFormParams() const {
  278. return Header.getFormParams();
  279. }
  280. uint16_t getVersion() const { return Header.getVersion(); }
  281. uint8_t getAddressByteSize() const { return Header.getAddressByteSize(); }
  282. uint8_t getRefAddrByteSize() const { return Header.getRefAddrByteSize(); }
  283. uint8_t getDwarfOffsetByteSize() const {
  284. return Header.getDwarfOffsetByteSize();
  285. }
  286. /// Size in bytes of the parsed unit header.
  287. uint32_t getHeaderSize() const { return Header.getSize(); }
  288. uint64_t getLength() const { return Header.getLength(); }
  289. dwarf::DwarfFormat getFormat() const { return Header.getFormat(); }
  290. uint8_t getUnitType() const { return Header.getUnitType(); }
  291. bool isTypeUnit() const { return Header.isTypeUnit(); }
  292. uint64_t getAbbrOffset() const { return Header.getAbbrOffset(); }
  293. uint64_t getNextUnitOffset() const { return Header.getNextUnitOffset(); }
  294. const DWARFSection &getLineSection() const { return LineSection; }
  295. StringRef getStringSection() const { return StringSection; }
  296. const DWARFSection &getStringOffsetSection() const {
  297. return StringOffsetSection;
  298. }
  299. void setSkeletonUnit(DWARFUnit *SU) { this->SU = SU; }
  300. // Returns itself if not using Split DWARF, or if the unit is a skeleton unit
  301. // - otherwise returns the split full unit's corresponding skeleton, if
  302. // available.
  303. DWARFUnit *getLinkedUnit() { return IsDWO ? SU : this; }
  304. void setAddrOffsetSection(const DWARFSection *AOS, uint64_t Base) {
  305. AddrOffsetSection = AOS;
  306. AddrOffsetSectionBase = Base;
  307. }
  308. std::optional<uint64_t> getAddrOffsetSectionBase() const {
  309. return AddrOffsetSectionBase;
  310. }
  311. /// Recursively update address to Die map.
  312. void updateAddressDieMap(DWARFDie Die);
  313. /// Recursively update address to variable Die map.
  314. void updateVariableDieMap(DWARFDie Die);
  315. void setRangesSection(const DWARFSection *RS, uint64_t Base) {
  316. RangeSection = RS;
  317. RangeSectionBase = Base;
  318. }
  319. uint64_t getLocSectionBase() const {
  320. return LocSectionBase;
  321. }
  322. std::optional<object::SectionedAddress>
  323. getAddrOffsetSectionItem(uint32_t Index) const;
  324. Expected<uint64_t> getStringOffsetSectionItem(uint32_t Index) const;
  325. DWARFDataExtractor getDebugInfoExtractor() const;
  326. DataExtractor getStringExtractor() const {
  327. return DataExtractor(StringSection, false, 0);
  328. }
  329. const DWARFLocationTable &getLocationTable() { return *LocTable; }
  330. /// Extract the range list referenced by this compile unit from the
  331. /// .debug_ranges section. If the extraction is unsuccessful, an error
  332. /// is returned. Successful extraction requires that the compile unit
  333. /// has already been extracted.
  334. Error extractRangeList(uint64_t RangeListOffset,
  335. DWARFDebugRangeList &RangeList) const;
  336. void clear();
  337. const std::optional<StrOffsetsContributionDescriptor> &
  338. getStringOffsetsTableContribution() const {
  339. return StringOffsetsTableContribution;
  340. }
  341. uint8_t getDwarfStringOffsetsByteSize() const {
  342. assert(StringOffsetsTableContribution);
  343. return StringOffsetsTableContribution->getDwarfOffsetByteSize();
  344. }
  345. uint64_t getStringOffsetsBase() const {
  346. assert(StringOffsetsTableContribution);
  347. return StringOffsetsTableContribution->Base;
  348. }
  349. uint64_t getAbbreviationsOffset() const { return Header.getAbbrOffset(); }
  350. const DWARFAbbreviationDeclarationSet *getAbbreviations() const;
  351. static bool isMatchingUnitTypeAndTag(uint8_t UnitType, dwarf::Tag Tag) {
  352. switch (UnitType) {
  353. case dwarf::DW_UT_compile:
  354. return Tag == dwarf::DW_TAG_compile_unit;
  355. case dwarf::DW_UT_type:
  356. return Tag == dwarf::DW_TAG_type_unit;
  357. case dwarf::DW_UT_partial:
  358. return Tag == dwarf::DW_TAG_partial_unit;
  359. case dwarf::DW_UT_skeleton:
  360. return Tag == dwarf::DW_TAG_skeleton_unit;
  361. case dwarf::DW_UT_split_compile:
  362. case dwarf::DW_UT_split_type:
  363. return dwarf::isUnitType(Tag);
  364. }
  365. return false;
  366. }
  367. std::optional<object::SectionedAddress> getBaseAddress();
  368. DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
  369. extractDIEsIfNeeded(ExtractUnitDIEOnly);
  370. if (DieArray.empty())
  371. return DWARFDie();
  372. return DWARFDie(this, &DieArray[0]);
  373. }
  374. DWARFDie getNonSkeletonUnitDIE(bool ExtractUnitDIEOnly = true,
  375. StringRef DWOAlternativeLocation = {}) {
  376. parseDWO(DWOAlternativeLocation);
  377. return DWO ? DWO->getUnitDIE(ExtractUnitDIEOnly)
  378. : getUnitDIE(ExtractUnitDIEOnly);
  379. }
  380. const char *getCompilationDir();
  381. std::optional<uint64_t> getDWOId() {
  382. extractDIEsIfNeeded(/*CUDieOnly*/ true);
  383. return getHeader().getDWOId();
  384. }
  385. void setDWOId(uint64_t NewID) { Header.setDWOId(NewID); }
  386. /// Return a vector of address ranges resulting from a (possibly encoded)
  387. /// range list starting at a given offset in the appropriate ranges section.
  388. Expected<DWARFAddressRangesVector> findRnglistFromOffset(uint64_t Offset);
  389. /// Return a vector of address ranges retrieved from an encoded range
  390. /// list whose offset is found via a table lookup given an index (DWARF v5
  391. /// and later).
  392. Expected<DWARFAddressRangesVector> findRnglistFromIndex(uint32_t Index);
  393. /// Return a rangelist's offset based on an index. The index designates
  394. /// an entry in the rangelist table's offset array and is supplied by
  395. /// DW_FORM_rnglistx.
  396. std::optional<uint64_t> getRnglistOffset(uint32_t Index);
  397. std::optional<uint64_t> getLoclistOffset(uint32_t Index);
  398. Expected<DWARFAddressRangesVector> collectAddressRanges();
  399. Expected<DWARFLocationExpressionsVector>
  400. findLoclistFromOffset(uint64_t Offset);
  401. /// Returns subprogram DIE with address range encompassing the provided
  402. /// address. The pointer is alive as long as parsed compile unit DIEs are not
  403. /// cleared.
  404. DWARFDie getSubroutineForAddress(uint64_t Address);
  405. /// Returns variable DIE for the address provided. The pointer is alive as
  406. /// long as parsed compile unit DIEs are not cleared.
  407. DWARFDie getVariableForAddress(uint64_t Address);
  408. /// getInlinedChainForAddress - fetches inlined chain for a given address.
  409. /// Returns empty chain if there is no subprogram containing address. The
  410. /// chain is valid as long as parsed compile unit DIEs are not cleared.
  411. void getInlinedChainForAddress(uint64_t Address,
  412. SmallVectorImpl<DWARFDie> &InlinedChain);
  413. /// Return the DWARFUnitVector containing this unit.
  414. const DWARFUnitVector &getUnitVector() const { return UnitVector; }
  415. /// Returns the number of DIEs in the unit. Parses the unit
  416. /// if necessary.
  417. unsigned getNumDIEs() {
  418. extractDIEsIfNeeded(false);
  419. return DieArray.size();
  420. }
  421. /// Return the index of a DIE inside the unit's DIE vector.
  422. ///
  423. /// It is illegal to call this method with a DIE that hasn't be
  424. /// created by this unit. In other word, it's illegal to call this
  425. /// method on a DIE that isn't accessible by following
  426. /// children/sibling links starting from this unit's getUnitDIE().
  427. uint32_t getDIEIndex(const DWARFDie &D) const {
  428. return getDIEIndex(D.getDebugInfoEntry());
  429. }
  430. /// Return the DIE object at the given index \p Index.
  431. DWARFDie getDIEAtIndex(unsigned Index) {
  432. return DWARFDie(this, getDebugInfoEntry(Index));
  433. }
  434. DWARFDie getParent(const DWARFDebugInfoEntry *Die);
  435. DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
  436. DWARFDie getPreviousSibling(const DWARFDebugInfoEntry *Die);
  437. DWARFDie getFirstChild(const DWARFDebugInfoEntry *Die);
  438. DWARFDie getLastChild(const DWARFDebugInfoEntry *Die);
  439. /// Return the DIE object for a given offset \p Offset inside the
  440. /// unit's DIE vector.
  441. DWARFDie getDIEForOffset(uint64_t Offset) {
  442. if (std::optional<uint32_t> DieIdx = getDIEIndexForOffset(Offset))
  443. return DWARFDie(this, &DieArray[*DieIdx]);
  444. return DWARFDie();
  445. }
  446. /// Return the DIE index for a given offset \p Offset inside the
  447. /// unit's DIE vector.
  448. std::optional<uint32_t> getDIEIndexForOffset(uint64_t Offset) {
  449. extractDIEsIfNeeded(false);
  450. auto It =
  451. llvm::partition_point(DieArray, [=](const DWARFDebugInfoEntry &DIE) {
  452. return DIE.getOffset() < Offset;
  453. });
  454. if (It != DieArray.end() && It->getOffset() == Offset)
  455. return It - DieArray.begin();
  456. return std::nullopt;
  457. }
  458. uint32_t getLineTableOffset() const {
  459. if (auto IndexEntry = Header.getIndexEntry())
  460. if (const auto *Contrib = IndexEntry->getContribution(DW_SECT_LINE))
  461. return Contrib->getOffset32();
  462. return 0;
  463. }
  464. die_iterator_range dies() {
  465. extractDIEsIfNeeded(false);
  466. return die_iterator_range(DieArray.begin(), DieArray.end());
  467. }
  468. virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0;
  469. Error tryExtractDIEsIfNeeded(bool CUDieOnly);
  470. private:
  471. /// Size in bytes of the .debug_info data associated with this compile unit.
  472. size_t getDebugInfoSize() const {
  473. return Header.getLength() + Header.getUnitLengthFieldByteSize() -
  474. getHeaderSize();
  475. }
  476. /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
  477. /// hasn't already been done
  478. void extractDIEsIfNeeded(bool CUDieOnly);
  479. /// extractDIEsToVector - Appends all parsed DIEs to a vector.
  480. void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
  481. std::vector<DWARFDebugInfoEntry> &DIEs) const;
  482. /// clearDIEs - Clear parsed DIEs to keep memory usage low.
  483. void clearDIEs(bool KeepCUDie);
  484. /// parseDWO - Parses .dwo file for current compile unit. Returns true if
  485. /// it was actually constructed.
  486. /// The \p AlternativeLocation specifies an alternative location to get
  487. /// the DWARF context for the DWO object; this is the case when it has
  488. /// been moved from its original location.
  489. bool parseDWO(StringRef AlternativeLocation = {});
  490. };
  491. inline bool isCompileUnit(const std::unique_ptr<DWARFUnit> &U) {
  492. return !U->isTypeUnit();
  493. }
  494. } // end namespace llvm
  495. #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
  496. #ifdef __GNUC__
  497. #pragma GCC diagnostic pop
  498. #endif