DWARFListTable.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/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. uint32_t getOffsetEntryCount() const { return HeaderData.OffsetEntryCount; }
  92. StringRef getSectionName() const { return SectionName; }
  93. StringRef getListTypeString() const { return ListTypeString; }
  94. dwarf::DwarfFormat getFormat() const { return Format; }
  95. /// Return the size of the table header including the length but not including
  96. /// the offsets.
  97. static uint8_t getHeaderSize(dwarf::DwarfFormat Format) {
  98. switch (Format) {
  99. case dwarf::DwarfFormat::DWARF32:
  100. return 12;
  101. case dwarf::DwarfFormat::DWARF64:
  102. return 20;
  103. }
  104. llvm_unreachable("Invalid DWARF format (expected DWARF32 or DWARF64");
  105. }
  106. void dump(DataExtractor Data, raw_ostream &OS,
  107. DIDumpOptions DumpOpts = {}) const;
  108. Optional<uint64_t> getOffsetEntry(DataExtractor Data, uint32_t Index) const {
  109. if (Index >= HeaderData.OffsetEntryCount)
  110. return None;
  111. return getOffsetEntry(Data, getHeaderOffset() + getHeaderSize(Format), Format, Index);
  112. }
  113. static 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 dump(DWARFDataExtractor Data, raw_ostream &OS,
  166. llvm::function_ref<Optional<object::SectionedAddress>(uint32_t)>
  167. LookupPooledAddress,
  168. DIDumpOptions DumpOpts = {}) const;
  169. /// Return the contents of the offset entry designated by a given index.
  170. Optional<uint64_t> getOffsetEntry(DataExtractor Data, uint32_t Index) const {
  171. return Header.getOffsetEntry(Data, Index);
  172. }
  173. /// Return the size of the table header including the length but not including
  174. /// the offsets. This is dependent on the table format, which is unambiguously
  175. /// derived from parsing the table.
  176. uint8_t getHeaderSize() const {
  177. return DWARFListTableHeader::getHeaderSize(getFormat());
  178. }
  179. uint64_t length() { return Header.length(); }
  180. };
  181. template <typename DWARFListType>
  182. Error DWARFListTableBase<DWARFListType>::extract(DWARFDataExtractor Data,
  183. uint64_t *OffsetPtr) {
  184. clear();
  185. if (Error E = extractHeaderAndOffsets(Data, OffsetPtr))
  186. return E;
  187. Data.setAddressSize(Header.getAddrSize());
  188. Data = DWARFDataExtractor(Data, getHeaderOffset() + Header.length());
  189. while (Data.isValidOffset(*OffsetPtr)) {
  190. DWARFListType CurrentList;
  191. uint64_t Off = *OffsetPtr;
  192. if (Error E = CurrentList.extract(Data, getHeaderOffset(), OffsetPtr,
  193. Header.getSectionName(),
  194. Header.getListTypeString()))
  195. return E;
  196. ListMap[Off] = CurrentList;
  197. }
  198. assert(*OffsetPtr == Data.size() &&
  199. "mismatch between expected length of table and length "
  200. "of extracted data");
  201. return Error::success();
  202. }
  203. template <typename ListEntryType>
  204. Error DWARFListType<ListEntryType>::extract(DWARFDataExtractor Data,
  205. uint64_t HeaderOffset,
  206. uint64_t *OffsetPtr,
  207. StringRef SectionName,
  208. StringRef ListTypeString) {
  209. if (*OffsetPtr < HeaderOffset || *OffsetPtr >= Data.size())
  210. return createStringError(errc::invalid_argument,
  211. "invalid %s list offset 0x%" PRIx64,
  212. ListTypeString.data(), *OffsetPtr);
  213. Entries.clear();
  214. while (Data.isValidOffset(*OffsetPtr)) {
  215. ListEntryType Entry;
  216. if (Error E = Entry.extract(Data, OffsetPtr))
  217. return E;
  218. Entries.push_back(Entry);
  219. if (Entry.isSentinel())
  220. return Error::success();
  221. }
  222. return createStringError(errc::illegal_byte_sequence,
  223. "no end of list marker detected at end of %s table "
  224. "starting at offset 0x%" PRIx64,
  225. SectionName.data(), HeaderOffset);
  226. }
  227. template <typename DWARFListType>
  228. void DWARFListTableBase<DWARFListType>::dump(
  229. DWARFDataExtractor Data, raw_ostream &OS,
  230. llvm::function_ref<Optional<object::SectionedAddress>(uint32_t)>
  231. LookupPooledAddress,
  232. DIDumpOptions DumpOpts) const {
  233. Header.dump(Data, OS, DumpOpts);
  234. OS << HeaderString << "\n";
  235. // Determine the length of the longest encoding string we have in the table,
  236. // so we can align the output properly. We only need this in verbose mode.
  237. size_t MaxEncodingStringLength = 0;
  238. if (DumpOpts.Verbose) {
  239. for (const auto &List : ListMap)
  240. for (const auto &Entry : List.second.getEntries())
  241. MaxEncodingStringLength =
  242. std::max(MaxEncodingStringLength,
  243. dwarf::RangeListEncodingString(Entry.EntryKind).size());
  244. }
  245. uint64_t CurrentBase = 0;
  246. for (const auto &List : ListMap)
  247. for (const auto &Entry : List.second.getEntries())
  248. Entry.dump(OS, getAddrSize(), MaxEncodingStringLength, CurrentBase,
  249. DumpOpts, LookupPooledAddress);
  250. }
  251. template <typename DWARFListType>
  252. Expected<DWARFListType>
  253. DWARFListTableBase<DWARFListType>::findList(DWARFDataExtractor Data,
  254. uint64_t Offset) const {
  255. // Extract the list from the section and enter it into the list map.
  256. DWARFListType List;
  257. if (Header.length())
  258. Data = DWARFDataExtractor(Data, getHeaderOffset() + Header.length());
  259. if (Error E =
  260. List.extract(Data, Header.length() ? getHeaderOffset() : 0, &Offset,
  261. Header.getSectionName(), Header.getListTypeString()))
  262. return std::move(E);
  263. return List;
  264. }
  265. } // end namespace llvm
  266. #endif // LLVM_DEBUGINFO_DWARF_DWARFLISTTABLE_H
  267. #ifdef __GNUC__
  268. #pragma GCC diagnostic pop
  269. #endif