DWARFFormValue.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_DWARFFORMVALUE_H
  14. #define LLVM_DEBUGINFO_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. bool isInlinedCStr() const {
  95. return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
  96. }
  97. /// getAsFoo functions below return the extracted value as Foo if only
  98. /// DWARFFormValue has form class is suitable for representing Foo.
  99. Optional<uint64_t> getAsReference() const;
  100. struct UnitOffset {
  101. DWARFUnit *Unit;
  102. uint64_t Offset;
  103. };
  104. Optional<UnitOffset> getAsRelativeReference() const;
  105. Optional<uint64_t> getAsUnsignedConstant() const;
  106. Optional<int64_t> getAsSignedConstant() const;
  107. Optional<const char *> getAsCString() const;
  108. Optional<uint64_t> getAsAddress() const;
  109. Optional<object::SectionedAddress> getAsSectionedAddress() const;
  110. Optional<uint64_t> getAsSectionOffset() const;
  111. Optional<ArrayRef<uint8_t>> getAsBlock() const;
  112. Optional<uint64_t> getAsCStringOffset() const;
  113. Optional<uint64_t> getAsReferenceUVal() const;
  114. /// Skip a form's value in \p DebugInfoData at the offset specified by
  115. /// \p OffsetPtr.
  116. ///
  117. /// Skips the bytes for the current form and updates the offset.
  118. ///
  119. /// \param DebugInfoData The data where we want to skip the value.
  120. /// \param OffsetPtr A reference to the offset that will be updated.
  121. /// \param Params DWARF parameters to help interpret forms.
  122. /// \returns true on success, false if the form was not skipped.
  123. bool skipValue(DataExtractor DebugInfoData, uint64_t *OffsetPtr,
  124. const dwarf::FormParams Params) const {
  125. return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
  126. }
  127. /// Skip a form's value in \p DebugInfoData at the offset specified by
  128. /// \p OffsetPtr.
  129. ///
  130. /// Skips the bytes for the specified form and updates the offset.
  131. ///
  132. /// \param Form The DW_FORM enumeration that indicates the form to skip.
  133. /// \param DebugInfoData The data where we want to skip the value.
  134. /// \param OffsetPtr A reference to the offset that will be updated.
  135. /// \param FormParams DWARF parameters to help interpret forms.
  136. /// \returns true on success, false if the form was not skipped.
  137. static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
  138. uint64_t *OffsetPtr,
  139. const dwarf::FormParams FormParams);
  140. private:
  141. void dumpString(raw_ostream &OS) const;
  142. };
  143. namespace dwarf {
  144. /// Take an optional DWARFFormValue and try to extract a string value from it.
  145. ///
  146. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  147. /// \returns an optional value that contains a value if the form value
  148. /// was valid and was a string.
  149. inline Optional<const char *> toString(const Optional<DWARFFormValue> &V) {
  150. if (V)
  151. return V->getAsCString();
  152. return None;
  153. }
  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 StringRef toStringRef(const Optional<DWARFFormValue> &V,
  160. StringRef Default = {}) {
  161. if (V)
  162. if (auto S = V->getAsCString())
  163. return *S;
  164. return Default;
  165. }
  166. /// Take an optional DWARFFormValue and extract a string value from it.
  167. ///
  168. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  169. /// \param Default the default value to return in case of failure.
  170. /// \returns the string value or Default if the V doesn't have a value or the
  171. /// form value's encoding wasn't a string.
  172. inline const char *toString(const Optional<DWARFFormValue> &V,
  173. const char *Default) {
  174. return toString(V).getValueOr(Default);
  175. }
  176. /// Take an optional DWARFFormValue and try to extract an unsigned constant.
  177. ///
  178. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  179. /// \returns an optional value that contains a value if the form value
  180. /// was valid and has a unsigned constant form.
  181. inline Optional<uint64_t> toUnsigned(const Optional<DWARFFormValue> &V) {
  182. if (V)
  183. return V->getAsUnsignedConstant();
  184. return None;
  185. }
  186. /// Take an optional DWARFFormValue and extract a unsigned constant.
  187. ///
  188. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  189. /// \param Default the default value to return in case of failure.
  190. /// \returns the extracted unsigned value or Default if the V doesn't have a
  191. /// value or the form value's encoding wasn't an unsigned constant form.
  192. inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
  193. uint64_t Default) {
  194. return toUnsigned(V).getValueOr(Default);
  195. }
  196. /// Take an optional DWARFFormValue and try to extract an reference.
  197. ///
  198. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  199. /// \returns an optional value that contains a value if the form value
  200. /// was valid and has a reference form.
  201. inline Optional<uint64_t> toReference(const Optional<DWARFFormValue> &V) {
  202. if (V)
  203. return V->getAsReference();
  204. return None;
  205. }
  206. /// Take an optional DWARFFormValue and extract a reference.
  207. ///
  208. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  209. /// \param Default the default value to return in case of failure.
  210. /// \returns the extracted reference value or Default if the V doesn't have a
  211. /// value or the form value's encoding wasn't a reference form.
  212. inline uint64_t toReference(const Optional<DWARFFormValue> &V,
  213. uint64_t Default) {
  214. return toReference(V).getValueOr(Default);
  215. }
  216. /// Take an optional DWARFFormValue and try to extract an signed constant.
  217. ///
  218. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  219. /// \returns an optional value that contains a value if the form value
  220. /// was valid and has a signed constant form.
  221. inline Optional<int64_t> toSigned(const Optional<DWARFFormValue> &V) {
  222. if (V)
  223. return V->getAsSignedConstant();
  224. return None;
  225. }
  226. /// Take an optional DWARFFormValue and extract a signed integer.
  227. ///
  228. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  229. /// \param Default the default value to return in case of failure.
  230. /// \returns the extracted signed integer value or Default if the V doesn't
  231. /// have a value or the form value's encoding wasn't a signed integer form.
  232. inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
  233. return toSigned(V).getValueOr(Default);
  234. }
  235. /// Take an optional DWARFFormValue and try to extract an address.
  236. ///
  237. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  238. /// \returns an optional value that contains a value if the form value
  239. /// was valid and has a address form.
  240. inline Optional<uint64_t> toAddress(const Optional<DWARFFormValue> &V) {
  241. if (V)
  242. return V->getAsAddress();
  243. return None;
  244. }
  245. inline Optional<object::SectionedAddress>
  246. toSectionedAddress(const Optional<DWARFFormValue> &V) {
  247. if (V)
  248. return V->getAsSectionedAddress();
  249. return None;
  250. }
  251. /// Take an optional DWARFFormValue and extract a address.
  252. ///
  253. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  254. /// \param Default the default value to return in case of failure.
  255. /// \returns the extracted address value or Default if the V doesn't have a
  256. /// value or the form value's encoding wasn't an address form.
  257. inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
  258. return toAddress(V).getValueOr(Default);
  259. }
  260. /// Take an optional DWARFFormValue and try to extract an section offset.
  261. ///
  262. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  263. /// \returns an optional value that contains a value if the form value
  264. /// was valid and has a section offset form.
  265. inline Optional<uint64_t> toSectionOffset(const Optional<DWARFFormValue> &V) {
  266. if (V)
  267. return V->getAsSectionOffset();
  268. return None;
  269. }
  270. /// Take an optional DWARFFormValue and extract a section offset.
  271. ///
  272. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  273. /// \param Default the default value to return in case of failure.
  274. /// \returns the extracted section offset value or Default if the V doesn't
  275. /// have a value or the form value's encoding wasn't a section offset form.
  276. inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
  277. uint64_t Default) {
  278. return toSectionOffset(V).getValueOr(Default);
  279. }
  280. /// Take an optional DWARFFormValue and try to extract block data.
  281. ///
  282. /// \param V and optional DWARFFormValue to attempt to extract the value from.
  283. /// \returns an optional value that contains a value if the form value
  284. /// was valid and has a block form.
  285. inline Optional<ArrayRef<uint8_t>> toBlock(const Optional<DWARFFormValue> &V) {
  286. if (V)
  287. return V->getAsBlock();
  288. return None;
  289. }
  290. } // end namespace dwarf
  291. } // end namespace llvm
  292. #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H
  293. #ifdef __GNUC__
  294. #pragma GCC diagnostic pop
  295. #endif