DWARFFormValue.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFFormValue.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_DWARFFORMVALUE_H
  14. #define LLVM_DEBUGINFO_DWARF_DWARFFORMVALUE_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/BinaryFormat/Dwarf.h"
  17. #include "llvm/DebugInfo/DIContext.h"
  18. #include "llvm/Support/DataExtractor.h"
  19. #include <cstdint>
  20. namespace llvm {
  21. class DWARFContext;
  22. class DWARFObject;
  23. class DWARFDataExtractor;
  24. class DWARFUnit;
  25. class raw_ostream;
  26. class DWARFFormValue {
  27. public:
  28. enum FormClass {
  29. FC_Unknown,
  30. FC_Address,
  31. FC_Block,
  32. FC_Constant,
  33. FC_String,
  34. FC_Flag,
  35. FC_Reference,
  36. FC_Indirect,
  37. FC_SectionOffset,
  38. FC_Exprloc
  39. };
  40. private:
  41. struct ValueType {
  42. ValueType() { uval = 0; }
  43. ValueType(int64_t V) : sval(V) {}
  44. ValueType(uint64_t V) : uval(V) {}
  45. ValueType(const char *V) : cstr(V) {}
  46. union {
  47. uint64_t uval;
  48. int64_t sval;
  49. const char *cstr;
  50. };
  51. const uint8_t *data = nullptr;
  52. uint64_t SectionIndex; /// Section index for reference forms.
  53. };
  54. dwarf::Form Form; /// Form for this value.
  55. dwarf::DwarfFormat Format =
  56. dwarf::DWARF32; /// Remember the DWARF format at extract time.
  57. ValueType Value; /// Contains all data for the form.
  58. const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
  59. const DWARFContext *C = nullptr; /// Context for extract time.
  60. DWARFFormValue(dwarf::Form F, ValueType V) : Form(F), Value(V) {}
  61. public:
  62. DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {}
  63. static DWARFFormValue createFromSValue(dwarf::Form F, int64_t V);
  64. static DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V);
  65. static DWARFFormValue createFromPValue(dwarf::Form F, const char *V);
  66. static DWARFFormValue createFromBlockValue(dwarf::Form F,
  67. ArrayRef<uint8_t> D);
  68. static DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit,
  69. uint64_t *OffsetPtr);
  70. dwarf::Form getForm() const { return Form; }
  71. uint64_t getRawUValue() const { return Value.uval; }
  72. bool isFormClass(FormClass FC) const;
  73. const DWARFUnit *getUnit() const { return U; }
  74. void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const;
  75. void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts,
  76. object::SectionedAddress SA) const;
  77. void dumpAddress(raw_ostream &OS, uint64_t Address) const;
  78. static void dumpAddress(raw_ostream &OS, uint8_t AddressSize,
  79. uint64_t Address);
  80. static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS,
  81. DIDumpOptions DumpOpts, uint64_t SectionIndex);
  82. /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
  83. /// in \p FormParams is needed to interpret some forms. The optional
  84. /// \p Context and \p Unit allows extracting information if the form refers
  85. /// to other sections (e.g., .debug_str).
  86. bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr,
  87. dwarf::FormParams FormParams,
  88. const DWARFContext *Context = nullptr,
  89. const DWARFUnit *Unit = nullptr);
  90. bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr,
  91. dwarf::FormParams FormParams, const DWARFUnit *U) {
  92. return extractValue(Data, OffsetPtr, FormParams, nullptr, U);
  93. }
  94. /// getAsFoo functions below return the extracted value as Foo if only
  95. /// DWARFFormValue has form class is suitable for representing Foo.
  96. std::optional<uint64_t> getAsReference() const;
  97. struct UnitOffset {
  98. DWARFUnit *Unit;
  99. uint64_t Offset;
  100. };
  101. std::optional<UnitOffset> getAsRelativeReference() const;
  102. std::optional<uint64_t> getAsUnsignedConstant() const;
  103. std::optional<int64_t> getAsSignedConstant() const;
  104. Expected<const char *> getAsCString() const;
  105. std::optional<uint64_t> getAsAddress() const;
  106. std::optional<object::SectionedAddress> getAsSectionedAddress() const;
  107. std::optional<uint64_t> getAsSectionOffset() const;
  108. std::optional<ArrayRef<uint8_t>> getAsBlock() const;
  109. std::optional<uint64_t> getAsCStringOffset() const;
  110. std::optional<uint64_t> getAsReferenceUVal() const;
  111. /// Correctly extract any file paths from a form value.
  112. ///
  113. /// These attributes can be in the from DW_AT_decl_file or DW_AT_call_file
  114. /// attributes. We need to use the file index in the correct DWARFUnit's line
  115. /// table prologue, and each DWARFFormValue has the DWARFUnit the form value
  116. /// was extracted from.
  117. ///
  118. /// \param Kind The kind of path to extract.
  119. ///
  120. /// \returns A valid string value on success, or std::nullopt if the form
  121. /// class is not FC_Constant, or if the file index is not valid.
  122. std::optional<std::string>
  123. getAsFile(DILineInfoSpecifier::FileLineInfoKind Kind) const;
  124. /// Skip a form's value in \p DebugInfoData at the offset specified by
  125. /// \p OffsetPtr.
  126. ///
  127. /// Skips the bytes for the current form and updates the offset.
  128. ///
  129. /// \param DebugInfoData The data where we want to skip the value.
  130. /// \param OffsetPtr A reference to the offset that will be updated.
  131. /// \param Params DWARF parameters to help interpret forms.
  132. /// \returns true on success, false if the form was not skipped.
  133. bool skipValue(DataExtractor DebugInfoData, uint64_t *OffsetPtr,
  134. const dwarf::FormParams Params) const {
  135. return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
  136. }
  137. /// Skip a form's value in \p DebugInfoData at the offset specified by
  138. /// \p OffsetPtr.
  139. ///
  140. /// Skips the bytes for the specified form and updates the offset.
  141. ///
  142. /// \param Form The DW_FORM enumeration that indicates the form to skip.
  143. /// \param DebugInfoData The data where we want to skip the value.
  144. /// \param OffsetPtr A reference to the offset that will be updated.
  145. /// \param FormParams DWARF parameters to help interpret forms.
  146. /// \returns true on success, false if the form was not skipped.
  147. static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
  148. uint64_t *OffsetPtr,
  149. const dwarf::FormParams FormParams);
  150. private:
  151. void dumpString(raw_ostream &OS) const;
  152. };
  153. namespace dwarf {
  154. /// Take an optional DWARFFormValue and try to extract a string value from it.
  155. ///
  156. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  157. /// \returns an optional value that contains a value if the form value
  158. /// was valid and was a string.
  159. inline std::optional<const char *>
  160. toString(const std::optional<DWARFFormValue> &V) {
  161. if (!V)
  162. return std::nullopt;
  163. Expected<const char*> E = V->getAsCString();
  164. if (!E) {
  165. consumeError(E.takeError());
  166. return std::nullopt;
  167. }
  168. return *E;
  169. }
  170. /// Take an optional DWARFFormValue and try to extract a string value from it.
  171. ///
  172. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  173. /// \returns an optional value that contains a value if the form value
  174. /// was valid and was a string.
  175. inline StringRef toStringRef(const std::optional<DWARFFormValue> &V,
  176. StringRef Default = {}) {
  177. if (!V)
  178. return Default;
  179. auto S = V->getAsCString();
  180. if (!S) {
  181. consumeError(S.takeError());
  182. return Default;
  183. }
  184. if (!*S)
  185. return Default;
  186. return *S;
  187. }
  188. /// Take an optional DWARFFormValue and extract a string value from it.
  189. ///
  190. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  191. /// \param Default the default value to return in case of failure.
  192. /// \returns the string value or Default if the V doesn't have a value or the
  193. /// form value's encoding wasn't a string.
  194. inline const char *toString(const std::optional<DWARFFormValue> &V,
  195. const char *Default) {
  196. if (auto E = toString(V))
  197. return *E;
  198. return Default;
  199. }
  200. /// Take an optional DWARFFormValue and try to extract an unsigned constant.
  201. ///
  202. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  203. /// \returns an optional value that contains a value if the form value
  204. /// was valid and has a unsigned constant form.
  205. inline std::optional<uint64_t>
  206. toUnsigned(const std::optional<DWARFFormValue> &V) {
  207. if (V)
  208. return V->getAsUnsignedConstant();
  209. return std::nullopt;
  210. }
  211. /// Take an optional DWARFFormValue and extract a unsigned constant.
  212. ///
  213. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  214. /// \param Default the default value to return in case of failure.
  215. /// \returns the extracted unsigned value or Default if the V doesn't have a
  216. /// value or the form value's encoding wasn't an unsigned constant form.
  217. inline uint64_t toUnsigned(const std::optional<DWARFFormValue> &V,
  218. uint64_t Default) {
  219. return toUnsigned(V).value_or(Default);
  220. }
  221. /// Take an optional DWARFFormValue and try to extract an reference.
  222. ///
  223. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  224. /// \returns an optional value that contains a value if the form value
  225. /// was valid and has a reference form.
  226. inline std::optional<uint64_t>
  227. toReference(const std::optional<DWARFFormValue> &V) {
  228. if (V)
  229. return V->getAsReference();
  230. return std::nullopt;
  231. }
  232. /// Take an optional DWARFFormValue and extract a reference.
  233. ///
  234. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  235. /// \param Default the default value to return in case of failure.
  236. /// \returns the extracted reference value or Default if the V doesn't have a
  237. /// value or the form value's encoding wasn't a reference form.
  238. inline uint64_t toReference(const std::optional<DWARFFormValue> &V,
  239. uint64_t Default) {
  240. return toReference(V).value_or(Default);
  241. }
  242. /// Take an optional DWARFFormValue and try to extract an signed constant.
  243. ///
  244. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  245. /// \returns an optional value that contains a value if the form value
  246. /// was valid and has a signed constant form.
  247. inline std::optional<int64_t> toSigned(const std::optional<DWARFFormValue> &V) {
  248. if (V)
  249. return V->getAsSignedConstant();
  250. return std::nullopt;
  251. }
  252. /// Take an optional DWARFFormValue and extract a signed integer.
  253. ///
  254. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  255. /// \param Default the default value to return in case of failure.
  256. /// \returns the extracted signed integer value or Default if the V doesn't
  257. /// have a value or the form value's encoding wasn't a signed integer form.
  258. inline int64_t toSigned(const std::optional<DWARFFormValue> &V,
  259. int64_t Default) {
  260. return toSigned(V).value_or(Default);
  261. }
  262. /// Take an optional DWARFFormValue and try to extract an address.
  263. ///
  264. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  265. /// \returns an optional value that contains a value if the form value
  266. /// was valid and has a address form.
  267. inline std::optional<uint64_t>
  268. toAddress(const std::optional<DWARFFormValue> &V) {
  269. if (V)
  270. return V->getAsAddress();
  271. return std::nullopt;
  272. }
  273. inline std::optional<object::SectionedAddress>
  274. toSectionedAddress(const std::optional<DWARFFormValue> &V) {
  275. if (V)
  276. return V->getAsSectionedAddress();
  277. return std::nullopt;
  278. }
  279. /// Take an optional DWARFFormValue and extract a address.
  280. ///
  281. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  282. /// \param Default the default value to return in case of failure.
  283. /// \returns the extracted address value or Default if the V doesn't have a
  284. /// value or the form value's encoding wasn't an address form.
  285. inline uint64_t toAddress(const std::optional<DWARFFormValue> &V,
  286. uint64_t Default) {
  287. return toAddress(V).value_or(Default);
  288. }
  289. /// Take an optional DWARFFormValue and try to extract an section offset.
  290. ///
  291. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  292. /// \returns an optional value that contains a value if the form value
  293. /// was valid and has a section offset form.
  294. inline std::optional<uint64_t>
  295. toSectionOffset(const std::optional<DWARFFormValue> &V) {
  296. if (V)
  297. return V->getAsSectionOffset();
  298. return std::nullopt;
  299. }
  300. /// Take an optional DWARFFormValue and extract a section offset.
  301. ///
  302. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  303. /// \param Default the default value to return in case of failure.
  304. /// \returns the extracted section offset value or Default if the V doesn't
  305. /// have a value or the form value's encoding wasn't a section offset form.
  306. inline uint64_t toSectionOffset(const std::optional<DWARFFormValue> &V,
  307. uint64_t Default) {
  308. return toSectionOffset(V).value_or(Default);
  309. }
  310. /// Take an optional DWARFFormValue and try to extract block data.
  311. ///
  312. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  313. /// \returns an optional value that contains a value if the form value
  314. /// was valid and has a block form.
  315. inline std::optional<ArrayRef<uint8_t>>
  316. toBlock(const std::optional<DWARFFormValue> &V) {
  317. if (V)
  318. return V->getAsBlock();
  319. return std::nullopt;
  320. }
  321. } // end namespace dwarf
  322. } // end namespace llvm
  323. #endif // LLVM_DEBUGINFO_DWARF_DWARFFORMVALUE_H
  324. #ifdef __GNUC__
  325. #pragma GCC diagnostic pop
  326. #endif