DWARFListTable.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFListTable.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_DWARFLISTTABLE_H
  14. #define LLVM_DEBUGINFO_DWARF_DWARFLISTTABLE_H
  15. #include "llvm/BinaryFormat/Dwarf.h"
  16. #include "llvm/DebugInfo/DIContext.h"
  17. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  18. #include "llvm/Support/Errc.h"
  19. #include "llvm/Support/Error.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <cstdint>
  22. #include <map>
  23. #include <vector>
  24. namespace llvm {
  25. /// A base class for DWARF list entries, such as range or location list
  26. /// entries.
  27. struct DWARFListEntryBase {
  28. /// The offset at which the entry is located in the section.
  29. uint64_t Offset;
  30. /// The DWARF encoding (DW_RLE_* or DW_LLE_*).
  31. uint8_t EntryKind;
  32. /// The index of the section this entry belongs to.
  33. uint64_t SectionIndex;
  34. };
  35. /// A base class for lists of entries that are extracted from a particular
  36. /// section, such as range lists or location lists.
  37. template <typename ListEntryType> class DWARFListType {
  38. using EntryType = ListEntryType;
  39. using ListEntries = std::vector<EntryType>;
  40. protected:
  41. ListEntries Entries;
  42. public:
  43. const ListEntries &getEntries() const { return Entries; }
  44. bool empty() const { return Entries.empty(); }
  45. void clear() { Entries.clear(); }
  46. Error extract(DWARFDataExtractor Data, uint64_t HeaderOffset,
  47. uint64_t *OffsetPtr, StringRef SectionName,
  48. StringRef ListStringName);
  49. };
  50. /// A class representing the header of a list table such as the range list
  51. /// table in the .debug_rnglists section.
  52. class DWARFListTableHeader {
  53. struct Header {
  54. /// The total length of the entries for this table, not including the length
  55. /// field itself.
  56. uint64_t Length = 0;
  57. /// The DWARF version number.
  58. uint16_t Version;
  59. /// The size in bytes of an address on the target architecture. For
  60. /// segmented addressing, this is the size of the offset portion of the
  61. /// address.
  62. uint8_t AddrSize;
  63. /// The size in bytes of a segment selector on the target architecture.
  64. /// If the target system uses a flat address space, this value is 0.
  65. uint8_t SegSize;
  66. /// The number of offsets that follow the header before the range lists.
  67. uint32_t OffsetEntryCount;
  68. };
  69. Header HeaderData;
  70. /// The table's format, either DWARF32 or DWARF64.
  71. dwarf::DwarfFormat Format;
  72. /// The offset at which the header (and hence the table) is located within
  73. /// its section.
  74. uint64_t HeaderOffset;
  75. /// The name of the section the list is located in.
  76. StringRef SectionName;
  77. /// A characterization of the list for dumping purposes, e.g. "range" or
  78. /// "location".
  79. StringRef ListTypeString;
  80. public:
  81. DWARFListTableHeader(StringRef SectionName, StringRef ListTypeString)
  82. : SectionName(SectionName), ListTypeString(ListTypeString) {}
  83. void clear() {
  84. HeaderData = {};
  85. }
  86. uint64_t getHeaderOffset() const { return HeaderOffset; }
  87. uint8_t getAddrSize() const { return HeaderData.AddrSize; }
  88. uint64_t getLength() const { return HeaderData.Length; }
  89. uint16_t getVersion() const { return HeaderData.Version; }
  90. uint32_t getOffsetEntryCount() const { return HeaderData.OffsetEntryCount; }
  91. StringRef getSectionName() const { return SectionName; }
  92. StringRef getListTypeString() const { return ListTypeString; }
  93. dwarf::DwarfFormat getFormat() const { return Format; }
  94. /// Return the size of the table header including the length but not including
  95. /// the offsets.
  96. static uint8_t getHeaderSize(dwarf::DwarfFormat Format) {
  97. switch (Format) {
  98. case dwarf::DwarfFormat::DWARF32:
  99. return 12;
  100. case dwarf::DwarfFormat::DWARF64:
  101. return 20;
  102. }
  103. llvm_unreachable("Invalid DWARF format (expected DWARF32 or DWARF64");
  104. }
  105. void dump(DataExtractor Data, raw_ostream &OS,
  106. DIDumpOptions DumpOpts = {}) const;
  107. std::optional<uint64_t> getOffsetEntry(DataExtractor Data,
  108. uint32_t Index) const {
  109. if (Index >= HeaderData.OffsetEntryCount)
  110. return std::nullopt;
  111. return getOffsetEntry(Data, getHeaderOffset() + getHeaderSize(Format), Format, Index);
  112. }
  113. static std::optional<uint64_t> getOffsetEntry(DataExtractor Data,
  114. uint64_t OffsetTableOffset,
  115. dwarf::DwarfFormat Format,
  116. uint32_t Index) {
  117. uint8_t OffsetByteSize = Format == dwarf::DWARF64 ? 8 : 4;
  118. uint64_t Offset = OffsetTableOffset + OffsetByteSize * Index;
  119. auto R = Data.getUnsigned(&Offset, OffsetByteSize);
  120. return R;
  121. }
  122. /// Extract the table header and the array of offsets.
  123. Error extract(DWARFDataExtractor Data, uint64_t *OffsetPtr);
  124. /// Returns the length of the table, including the length field, or 0 if the
  125. /// length has not been determined (e.g. because the table has not yet been
  126. /// parsed, or there was a problem in parsing).
  127. uint64_t length() const;
  128. };
  129. /// A class representing a table of lists as specified in the DWARF v5
  130. /// standard for location lists and range lists. The table consists of a header
  131. /// followed by an array of offsets into a DWARF section, followed by zero or
  132. /// more list entries. The list entries are kept in a map where the keys are
  133. /// the lists' section offsets.
  134. template <typename DWARFListType> class DWARFListTableBase {
  135. DWARFListTableHeader Header;
  136. /// A mapping between file offsets and lists. It is used to find a particular
  137. /// list based on an offset (obtained from DW_AT_ranges, for example).
  138. std::map<uint64_t, DWARFListType> ListMap;
  139. /// This string is displayed as a heading before the list is dumped
  140. /// (e.g. "ranges:").
  141. StringRef HeaderString;
  142. protected:
  143. DWARFListTableBase(StringRef SectionName, StringRef HeaderString,
  144. StringRef ListTypeString)
  145. : Header(SectionName, ListTypeString), HeaderString(HeaderString) {}
  146. public:
  147. void clear() {
  148. Header.clear();
  149. ListMap.clear();
  150. }
  151. /// Extract the table header and the array of offsets.
  152. Error extractHeaderAndOffsets(DWARFDataExtractor Data, uint64_t *OffsetPtr) {
  153. return Header.extract(Data, OffsetPtr);
  154. }
  155. /// Extract an entire table, including all list entries.
  156. Error extract(DWARFDataExtractor Data, uint64_t *OffsetPtr);
  157. /// Look up a list based on a given offset. Extract it and enter it into the
  158. /// list map if necessary.
  159. Expected<DWARFListType> findList(DWARFDataExtractor Data,
  160. uint64_t Offset) const;
  161. uint64_t getHeaderOffset() const { return Header.getHeaderOffset(); }
  162. uint8_t getAddrSize() const { return Header.getAddrSize(); }
  163. uint32_t getOffsetEntryCount() const { return Header.getOffsetEntryCount(); }
  164. dwarf::DwarfFormat getFormat() const { return Header.getFormat(); }
  165. void
  166. dump(DWARFDataExtractor Data, raw_ostream &OS,
  167. llvm::function_ref<std::optional<object::SectionedAddress>(uint32_t)>
  168. LookupPooledAddress,
  169. DIDumpOptions DumpOpts = {}) const;
  170. /// Return the contents of the offset entry designated by a given index.
  171. std::optional<uint64_t> getOffsetEntry(DataExtractor Data,
  172. uint32_t Index) const {
  173. return Header.getOffsetEntry(Data, Index);
  174. }
  175. /// Return the size of the table header including the length but not including
  176. /// the offsets. This is dependent on the table format, which is unambiguously
  177. /// derived from parsing the table.
  178. uint8_t getHeaderSize() const {
  179. return DWARFListTableHeader::getHeaderSize(getFormat());
  180. }
  181. uint64_t length() { return Header.length(); }
  182. };
  183. template <typename DWARFListType>
  184. Error DWARFListTableBase<DWARFListType>::extract(DWARFDataExtractor Data,
  185. uint64_t *OffsetPtr) {
  186. clear();
  187. if (Error E = extractHeaderAndOffsets(Data, OffsetPtr))
  188. return E;
  189. Data.setAddressSize(Header.getAddrSize());
  190. Data = DWARFDataExtractor(Data, getHeaderOffset() + Header.length());
  191. while (Data.isValidOffset(*OffsetPtr)) {
  192. DWARFListType CurrentList;
  193. uint64_t Off = *OffsetPtr;
  194. if (Error E = CurrentList.extract(Data, getHeaderOffset(), OffsetPtr,
  195. Header.getSectionName(),
  196. Header.getListTypeString()))
  197. return E;
  198. ListMap[Off] = CurrentList;
  199. }
  200. assert(*OffsetPtr == Data.size() &&
  201. "mismatch between expected length of table and length "
  202. "of extracted data");
  203. return Error::success();
  204. }
  205. template <typename ListEntryType>
  206. Error DWARFListType<ListEntryType>::extract(DWARFDataExtractor Data,
  207. uint64_t HeaderOffset,
  208. uint64_t *OffsetPtr,
  209. StringRef SectionName,
  210. StringRef ListTypeString) {
  211. if (*OffsetPtr < HeaderOffset || *OffsetPtr >= Data.size())
  212. return createStringError(errc::invalid_argument,
  213. "invalid %s list offset 0x%" PRIx64,
  214. ListTypeString.data(), *OffsetPtr);
  215. Entries.clear();
  216. while (Data.isValidOffset(*OffsetPtr)) {
  217. ListEntryType Entry;
  218. if (Error E = Entry.extract(Data, OffsetPtr))
  219. return E;
  220. Entries.push_back(Entry);
  221. if (Entry.isSentinel())
  222. return Error::success();
  223. }
  224. return createStringError(errc::illegal_byte_sequence,
  225. "no end of list marker detected at end of %s table "
  226. "starting at offset 0x%" PRIx64,
  227. SectionName.data(), HeaderOffset);
  228. }
  229. template <typename DWARFListType>
  230. void DWARFListTableBase<DWARFListType>::dump(
  231. DWARFDataExtractor Data, raw_ostream &OS,
  232. llvm::function_ref<std::optional<object::SectionedAddress>(uint32_t)>
  233. LookupPooledAddress,
  234. DIDumpOptions DumpOpts) const {
  235. Header.dump(Data, OS, DumpOpts);
  236. OS << HeaderString << "\n";
  237. // Determine the length of the longest encoding string we have in the table,
  238. // so we can align the output properly. We only need this in verbose mode.
  239. size_t MaxEncodingStringLength = 0;
  240. if (DumpOpts.Verbose) {
  241. for (const auto &List : ListMap)
  242. for (const auto &Entry : List.second.getEntries())
  243. MaxEncodingStringLength =
  244. std::max(MaxEncodingStringLength,
  245. dwarf::RangeListEncodingString(Entry.EntryKind).size());
  246. }
  247. uint64_t CurrentBase = 0;
  248. for (const auto &List : ListMap)
  249. for (const auto &Entry : List.second.getEntries())
  250. Entry.dump(OS, getAddrSize(), MaxEncodingStringLength, CurrentBase,
  251. DumpOpts, LookupPooledAddress);
  252. }
  253. template <typename DWARFListType>
  254. Expected<DWARFListType>
  255. DWARFListTableBase<DWARFListType>::findList(DWARFDataExtractor Data,
  256. uint64_t Offset) const {
  257. // Extract the list from the section and enter it into the list map.
  258. DWARFListType List;
  259. if (Header.length())
  260. Data = DWARFDataExtractor(Data, getHeaderOffset() + Header.length());
  261. if (Error E =
  262. List.extract(Data, Header.length() ? getHeaderOffset() : 0, &Offset,
  263. Header.getSectionName(), Header.getListTypeString()))
  264. return std::move(E);
  265. return List;
  266. }
  267. } // end namespace llvm
  268. #endif // LLVM_DEBUGINFO_DWARF_DWARFLISTTABLE_H
  269. #ifdef __GNUC__
  270. #pragma GCC diagnostic pop
  271. #endif