DWARFFormValue.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/ADT/None.h"
  17. #include "llvm/ADT/Optional.h"
  18. #include "llvm/BinaryFormat/Dwarf.h"
  19. #include "llvm/DebugInfo/DIContext.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  21. #include <cstdint>
  22. namespace llvm {
  23. class DWARFContext;
  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. Optional<uint64_t> getAsReference() const;
  97. struct UnitOffset {
  98. DWARFUnit *Unit;
  99. uint64_t Offset;
  100. };
  101. Optional<UnitOffset> getAsRelativeReference() const;
  102. Optional<uint64_t> getAsUnsignedConstant() const;
  103. Optional<int64_t> getAsSignedConstant() const;
  104. Expected<const char *> getAsCString() const;
  105. Optional<uint64_t> getAsAddress() const;
  106. Optional<object::SectionedAddress> getAsSectionedAddress() const;
  107. Optional<uint64_t> getAsSectionOffset() const;
  108. Optional<ArrayRef<uint8_t>> getAsBlock() const;
  109. Optional<uint64_t> getAsCStringOffset() const;
  110. 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 llvm::None if the form class
  121. /// is not FC_Constant, or if the file index is not valid.
  122. 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 Optional<const char *> toString(const Optional<DWARFFormValue> &V) {
  160. if (!V)
  161. return None;
  162. Expected<const char*> E = V->getAsCString();
  163. if (!E) {
  164. consumeError(E.takeError());
  165. return None;
  166. }
  167. return *E;
  168. }
  169. /// Take an optional DWARFFormValue and try to extract a string value from it.
  170. ///
  171. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  172. /// \returns an optional value that contains a value if the form value
  173. /// was valid and was a string.
  174. inline StringRef toStringRef(const Optional<DWARFFormValue> &V,
  175. StringRef Default = {}) {
  176. if (!V)
  177. return Default;
  178. auto S = V->getAsCString();
  179. if (!S) {
  180. consumeError(S.takeError());
  181. return Default;
  182. }
  183. if (!*S)
  184. return Default;
  185. return *S;
  186. }
  187. /// Take an optional DWARFFormValue and extract a string value from it.
  188. ///
  189. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  190. /// \param Default the default value to return in case of failure.
  191. /// \returns the string value or Default if the V doesn't have a value or the
  192. /// form value's encoding wasn't a string.
  193. inline const char *toString(const Optional<DWARFFormValue> &V,
  194. const char *Default) {
  195. if (auto E = toString(V))
  196. return *E;
  197. return Default;
  198. }
  199. /// Take an optional DWARFFormValue and try to extract an unsigned constant.
  200. ///
  201. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  202. /// \returns an optional value that contains a value if the form value
  203. /// was valid and has a unsigned constant form.
  204. inline Optional<uint64_t> toUnsigned(const Optional<DWARFFormValue> &V) {
  205. if (V)
  206. return V->getAsUnsignedConstant();
  207. return None;
  208. }
  209. /// Take an optional DWARFFormValue and extract a unsigned constant.
  210. ///
  211. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  212. /// \param Default the default value to return in case of failure.
  213. /// \returns the extracted unsigned value or Default if the V doesn't have a
  214. /// value or the form value's encoding wasn't an unsigned constant form.
  215. inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
  216. uint64_t Default) {
  217. return toUnsigned(V).getValueOr(Default);
  218. }
  219. /// Take an optional DWARFFormValue and try to extract an reference.
  220. ///
  221. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  222. /// \returns an optional value that contains a value if the form value
  223. /// was valid and has a reference form.
  224. inline Optional<uint64_t> toReference(const Optional<DWARFFormValue> &V) {
  225. if (V)
  226. return V->getAsReference();
  227. return None;
  228. }
  229. /// Take an optional DWARFFormValue and extract a reference.
  230. ///
  231. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  232. /// \param Default the default value to return in case of failure.
  233. /// \returns the extracted reference value or Default if the V doesn't have a
  234. /// value or the form value's encoding wasn't a reference form.
  235. inline uint64_t toReference(const Optional<DWARFFormValue> &V,
  236. uint64_t Default) {
  237. return toReference(V).getValueOr(Default);
  238. }
  239. /// Take an optional DWARFFormValue and try to extract an signed constant.
  240. ///
  241. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  242. /// \returns an optional value that contains a value if the form value
  243. /// was valid and has a signed constant form.
  244. inline Optional<int64_t> toSigned(const Optional<DWARFFormValue> &V) {
  245. if (V)
  246. return V->getAsSignedConstant();
  247. return None;
  248. }
  249. /// Take an optional DWARFFormValue and extract a signed integer.
  250. ///
  251. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  252. /// \param Default the default value to return in case of failure.
  253. /// \returns the extracted signed integer value or Default if the V doesn't
  254. /// have a value or the form value's encoding wasn't a signed integer form.
  255. inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
  256. return toSigned(V).getValueOr(Default);
  257. }
  258. /// Take an optional DWARFFormValue and try to extract an address.
  259. ///
  260. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  261. /// \returns an optional value that contains a value if the form value
  262. /// was valid and has a address form.
  263. inline Optional<uint64_t> toAddress(const Optional<DWARFFormValue> &V) {
  264. if (V)
  265. return V->getAsAddress();
  266. return None;
  267. }
  268. inline Optional<object::SectionedAddress>
  269. toSectionedAddress(const Optional<DWARFFormValue> &V) {
  270. if (V)
  271. return V->getAsSectionedAddress();
  272. return None;
  273. }
  274. /// Take an optional DWARFFormValue and extract a address.
  275. ///
  276. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  277. /// \param Default the default value to return in case of failure.
  278. /// \returns the extracted address value or Default if the V doesn't have a
  279. /// value or the form value's encoding wasn't an address form.
  280. inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
  281. return toAddress(V).getValueOr(Default);
  282. }
  283. /// Take an optional DWARFFormValue and try to extract an section offset.
  284. ///
  285. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  286. /// \returns an optional value that contains a value if the form value
  287. /// was valid and has a section offset form.
  288. inline Optional<uint64_t> toSectionOffset(const Optional<DWARFFormValue> &V) {
  289. if (V)
  290. return V->getAsSectionOffset();
  291. return None;
  292. }
  293. /// Take an optional DWARFFormValue and extract a section offset.
  294. ///
  295. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  296. /// \param Default the default value to return in case of failure.
  297. /// \returns the extracted section offset value or Default if the V doesn't
  298. /// have a value or the form value's encoding wasn't a section offset form.
  299. inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
  300. uint64_t Default) {
  301. return toSectionOffset(V).getValueOr(Default);
  302. }
  303. /// Take an optional DWARFFormValue and try to extract block data.
  304. ///
  305. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  306. /// \returns an optional value that contains a value if the form value
  307. /// was valid and has a block form.
  308. inline Optional<ArrayRef<uint8_t>> toBlock(const Optional<DWARFFormValue> &V) {
  309. if (V)
  310. return V->getAsBlock();
  311. return None;
  312. }
  313. } // end namespace dwarf
  314. } // end namespace llvm
  315. #endif // LLVM_DEBUGINFO_DWARF_DWARFFORMVALUE_H
  316. #ifdef __GNUC__
  317. #pragma GCC diagnostic pop
  318. #endif