DWARFListTable.h 11 KB

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