DWARFFormValue.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. //===-- DWARFFormValue.cpp ------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "SyntaxHighlighting.h"
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
  13. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  14. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/Dwarf.h"
  17. #include "llvm/Support/Format.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include <cassert>
  20. #include <climits>
  21. using namespace llvm;
  22. using namespace dwarf;
  23. using namespace syntax;
  24. namespace {
  25. uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
  26. // FIXME: Support DWARF64.
  27. return (Version == 2) ? AddrSize : 4;
  28. }
  29. template <uint8_t AddrSize, uint8_t RefAddrSize>
  30. ArrayRef<uint8_t> makeFixedFormSizesArrayRef() {
  31. static const uint8_t sizes[] = {
  32. 0, // 0x00 unused
  33. AddrSize, // 0x01 DW_FORM_addr
  34. 0, // 0x02 unused
  35. 0, // 0x03 DW_FORM_block2
  36. 0, // 0x04 DW_FORM_block4
  37. 2, // 0x05 DW_FORM_data2
  38. 4, // 0x06 DW_FORM_data4
  39. 8, // 0x07 DW_FORM_data8
  40. 0, // 0x08 DW_FORM_string
  41. 0, // 0x09 DW_FORM_block
  42. 0, // 0x0a DW_FORM_block1
  43. 1, // 0x0b DW_FORM_data1
  44. 1, // 0x0c DW_FORM_flag
  45. 0, // 0x0d DW_FORM_sdata
  46. 4, // 0x0e DW_FORM_strp
  47. 0, // 0x0f DW_FORM_udata
  48. RefAddrSize, // 0x10 DW_FORM_ref_addr
  49. 1, // 0x11 DW_FORM_ref1
  50. 2, // 0x12 DW_FORM_ref2
  51. 4, // 0x13 DW_FORM_ref4
  52. 8, // 0x14 DW_FORM_ref8
  53. 0, // 0x15 DW_FORM_ref_udata
  54. 0, // 0x16 DW_FORM_indirect
  55. 4, // 0x17 DW_FORM_sec_offset
  56. 0, // 0x18 DW_FORM_exprloc
  57. 0, // 0x19 DW_FORM_flag_present
  58. };
  59. return makeArrayRef(sizes);
  60. }
  61. }
  62. ArrayRef<uint8_t> DWARFFormValue::getFixedFormSizes(uint8_t AddrSize,
  63. uint16_t Version) {
  64. uint8_t RefAddrSize = getRefAddrSize(AddrSize, Version);
  65. if (AddrSize == 4 && RefAddrSize == 4)
  66. return makeFixedFormSizesArrayRef<4, 4>();
  67. if (AddrSize == 4 && RefAddrSize == 8)
  68. return makeFixedFormSizesArrayRef<4, 8>();
  69. if (AddrSize == 8 && RefAddrSize == 4)
  70. return makeFixedFormSizesArrayRef<8, 4>();
  71. if (AddrSize == 8 && RefAddrSize == 8)
  72. return makeFixedFormSizesArrayRef<8, 8>();
  73. return None;
  74. }
  75. static const DWARFFormValue::FormClass DWARF4FormClasses[] = {
  76. DWARFFormValue::FC_Unknown, // 0x0
  77. DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
  78. DWARFFormValue::FC_Unknown, // 0x02 unused
  79. DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
  80. DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
  81. DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
  82. // --- These can be FC_SectionOffset in DWARF3 and below:
  83. DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
  84. DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
  85. // ---
  86. DWARFFormValue::FC_String, // 0x08 DW_FORM_string
  87. DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
  88. DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
  89. DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
  90. DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
  91. DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
  92. DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
  93. DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
  94. DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
  95. DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
  96. DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
  97. DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
  98. DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
  99. DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
  100. DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
  101. DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
  102. DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
  103. DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
  104. };
  105. bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
  106. // First, check DWARF4 form classes.
  107. if (Form < ArrayRef<FormClass>(DWARF4FormClasses).size() &&
  108. DWARF4FormClasses[Form] == FC)
  109. return true;
  110. // Check more forms from DWARF4 and DWARF5 proposals.
  111. switch (Form) {
  112. case DW_FORM_ref_sig8:
  113. case DW_FORM_GNU_ref_alt:
  114. return (FC == FC_Reference);
  115. case DW_FORM_GNU_addr_index:
  116. return (FC == FC_Address);
  117. case DW_FORM_GNU_str_index:
  118. case DW_FORM_GNU_strp_alt:
  119. return (FC == FC_String);
  120. }
  121. // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
  122. // Don't check for DWARF version here, as some producers may still do this
  123. // by mistake.
  124. return (Form == DW_FORM_data4 || Form == DW_FORM_data8) &&
  125. FC == FC_SectionOffset;
  126. }
  127. bool DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr,
  128. const DWARFUnit *cu) {
  129. bool indirect = false;
  130. bool is_block = false;
  131. Value.data = nullptr;
  132. // Read the value for the form into value and follow and DW_FORM_indirect
  133. // instances we run into
  134. do {
  135. indirect = false;
  136. switch (Form) {
  137. case DW_FORM_addr:
  138. case DW_FORM_ref_addr: {
  139. if (!cu)
  140. return false;
  141. uint16_t AddrSize =
  142. (Form == DW_FORM_addr)
  143. ? cu->getAddressByteSize()
  144. : getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
  145. RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr);
  146. if (AI != cu->getRelocMap()->end()) {
  147. const std::pair<uint8_t, int64_t> &R = AI->second;
  148. Value.uval = data.getUnsigned(offset_ptr, AddrSize) + R.second;
  149. } else
  150. Value.uval = data.getUnsigned(offset_ptr, AddrSize);
  151. break;
  152. }
  153. case DW_FORM_exprloc:
  154. case DW_FORM_block:
  155. Value.uval = data.getULEB128(offset_ptr);
  156. is_block = true;
  157. break;
  158. case DW_FORM_block1:
  159. Value.uval = data.getU8(offset_ptr);
  160. is_block = true;
  161. break;
  162. case DW_FORM_block2:
  163. Value.uval = data.getU16(offset_ptr);
  164. is_block = true;
  165. break;
  166. case DW_FORM_block4:
  167. Value.uval = data.getU32(offset_ptr);
  168. is_block = true;
  169. break;
  170. case DW_FORM_data1:
  171. case DW_FORM_ref1:
  172. case DW_FORM_flag:
  173. Value.uval = data.getU8(offset_ptr);
  174. break;
  175. case DW_FORM_data2:
  176. case DW_FORM_ref2:
  177. Value.uval = data.getU16(offset_ptr);
  178. break;
  179. case DW_FORM_data4:
  180. case DW_FORM_ref4: {
  181. Value.uval = data.getU32(offset_ptr);
  182. if (!cu)
  183. break;
  184. RelocAddrMap::const_iterator AI = cu->getRelocMap()->find(*offset_ptr-4);
  185. if (AI != cu->getRelocMap()->end())
  186. Value.uval += AI->second.second;
  187. break;
  188. }
  189. case DW_FORM_data8:
  190. case DW_FORM_ref8:
  191. Value.uval = data.getU64(offset_ptr);
  192. break;
  193. case DW_FORM_sdata:
  194. Value.sval = data.getSLEB128(offset_ptr);
  195. break;
  196. case DW_FORM_udata:
  197. case DW_FORM_ref_udata:
  198. Value.uval = data.getULEB128(offset_ptr);
  199. break;
  200. case DW_FORM_string:
  201. Value.cstr = data.getCStr(offset_ptr);
  202. break;
  203. case DW_FORM_indirect:
  204. Form = data.getULEB128(offset_ptr);
  205. indirect = true;
  206. break;
  207. case DW_FORM_sec_offset:
  208. case DW_FORM_strp:
  209. case DW_FORM_GNU_ref_alt:
  210. case DW_FORM_GNU_strp_alt: {
  211. // FIXME: This is 64-bit for DWARF64.
  212. Value.uval = data.getU32(offset_ptr);
  213. if (!cu)
  214. break;
  215. RelocAddrMap::const_iterator AI =
  216. cu->getRelocMap()->find(*offset_ptr - 4);
  217. if (AI != cu->getRelocMap()->end())
  218. Value.uval += AI->second.second;
  219. break;
  220. }
  221. case DW_FORM_flag_present:
  222. Value.uval = 1;
  223. break;
  224. case DW_FORM_ref_sig8:
  225. Value.uval = data.getU64(offset_ptr);
  226. break;
  227. case DW_FORM_GNU_addr_index:
  228. case DW_FORM_GNU_str_index:
  229. Value.uval = data.getULEB128(offset_ptr);
  230. break;
  231. default:
  232. return false;
  233. }
  234. } while (indirect);
  235. if (is_block) {
  236. StringRef str = data.getData().substr(*offset_ptr, Value.uval);
  237. Value.data = nullptr;
  238. if (!str.empty()) {
  239. Value.data = reinterpret_cast<const uint8_t *>(str.data());
  240. *offset_ptr += Value.uval;
  241. }
  242. }
  243. return true;
  244. }
  245. bool
  246. DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr,
  247. const DWARFUnit *cu) const {
  248. return DWARFFormValue::skipValue(Form, debug_info_data, offset_ptr, cu);
  249. }
  250. bool
  251. DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data,
  252. uint32_t *offset_ptr, const DWARFUnit *cu) {
  253. bool indirect = false;
  254. do {
  255. switch (form) {
  256. // Blocks if inlined data that have a length field and the data bytes
  257. // inlined in the .debug_info
  258. case DW_FORM_exprloc:
  259. case DW_FORM_block: {
  260. uint64_t size = debug_info_data.getULEB128(offset_ptr);
  261. *offset_ptr += size;
  262. return true;
  263. }
  264. case DW_FORM_block1: {
  265. uint8_t size = debug_info_data.getU8(offset_ptr);
  266. *offset_ptr += size;
  267. return true;
  268. }
  269. case DW_FORM_block2: {
  270. uint16_t size = debug_info_data.getU16(offset_ptr);
  271. *offset_ptr += size;
  272. return true;
  273. }
  274. case DW_FORM_block4: {
  275. uint32_t size = debug_info_data.getU32(offset_ptr);
  276. *offset_ptr += size;
  277. return true;
  278. }
  279. // Inlined NULL terminated C-strings
  280. case DW_FORM_string:
  281. debug_info_data.getCStr(offset_ptr);
  282. return true;
  283. // Compile unit address sized values
  284. case DW_FORM_addr:
  285. *offset_ptr += cu->getAddressByteSize();
  286. return true;
  287. case DW_FORM_ref_addr:
  288. *offset_ptr += getRefAddrSize(cu->getAddressByteSize(), cu->getVersion());
  289. return true;
  290. // 0 byte values - implied from the form.
  291. case DW_FORM_flag_present:
  292. return true;
  293. // 1 byte values
  294. case DW_FORM_data1:
  295. case DW_FORM_flag:
  296. case DW_FORM_ref1:
  297. *offset_ptr += 1;
  298. return true;
  299. // 2 byte values
  300. case DW_FORM_data2:
  301. case DW_FORM_ref2:
  302. *offset_ptr += 2;
  303. return true;
  304. // 4 byte values
  305. case DW_FORM_data4:
  306. case DW_FORM_ref4:
  307. *offset_ptr += 4;
  308. return true;
  309. // 8 byte values
  310. case DW_FORM_data8:
  311. case DW_FORM_ref8:
  312. case DW_FORM_ref_sig8:
  313. *offset_ptr += 8;
  314. return true;
  315. // signed or unsigned LEB 128 values
  316. // case DW_FORM_APPLE_db_str:
  317. case DW_FORM_sdata:
  318. case DW_FORM_udata:
  319. case DW_FORM_ref_udata:
  320. case DW_FORM_GNU_str_index:
  321. case DW_FORM_GNU_addr_index:
  322. debug_info_data.getULEB128(offset_ptr);
  323. return true;
  324. case DW_FORM_indirect:
  325. indirect = true;
  326. form = debug_info_data.getULEB128(offset_ptr);
  327. break;
  328. // FIXME: 4 for DWARF32, 8 for DWARF64.
  329. case DW_FORM_sec_offset:
  330. case DW_FORM_strp:
  331. case DW_FORM_GNU_ref_alt:
  332. case DW_FORM_GNU_strp_alt:
  333. *offset_ptr += 4;
  334. return true;
  335. default:
  336. return false;
  337. }
  338. } while (indirect);
  339. return true;
  340. }
  341. void
  342. DWARFFormValue::dump(raw_ostream &OS, const DWARFUnit *cu) const {
  343. uint64_t uvalue = Value.uval;
  344. bool cu_relative_offset = false;
  345. switch (Form) {
  346. case DW_FORM_addr: OS << format("0x%016" PRIx64, uvalue); break;
  347. case DW_FORM_GNU_addr_index: {
  348. OS << format(" indexed (%8.8x) address = ", (uint32_t)uvalue);
  349. uint64_t Address;
  350. if (cu->getAddrOffsetSectionItem(uvalue, Address))
  351. OS << format("0x%016" PRIx64, Address);
  352. else
  353. OS << "<no .debug_addr section>";
  354. break;
  355. }
  356. case DW_FORM_flag_present: OS << "true"; break;
  357. case DW_FORM_flag:
  358. case DW_FORM_data1: OS << format("0x%02x", (uint8_t)uvalue); break;
  359. case DW_FORM_data2: OS << format("0x%04x", (uint16_t)uvalue); break;
  360. case DW_FORM_data4: OS << format("0x%08x", (uint32_t)uvalue); break;
  361. case DW_FORM_ref_sig8:
  362. case DW_FORM_data8: OS << format("0x%016" PRIx64, uvalue); break;
  363. case DW_FORM_string:
  364. OS << '"';
  365. OS.write_escaped(Value.cstr);
  366. OS << '"';
  367. break;
  368. case DW_FORM_exprloc:
  369. case DW_FORM_block:
  370. case DW_FORM_block1:
  371. case DW_FORM_block2:
  372. case DW_FORM_block4:
  373. if (uvalue > 0) {
  374. switch (Form) {
  375. case DW_FORM_exprloc:
  376. case DW_FORM_block: OS << format("<0x%" PRIx64 "> ", uvalue); break;
  377. case DW_FORM_block1: OS << format("<0x%2.2x> ", (uint8_t)uvalue); break;
  378. case DW_FORM_block2: OS << format("<0x%4.4x> ", (uint16_t)uvalue); break;
  379. case DW_FORM_block4: OS << format("<0x%8.8x> ", (uint32_t)uvalue); break;
  380. default: break;
  381. }
  382. const uint8_t* data_ptr = Value.data;
  383. if (data_ptr) {
  384. // uvalue contains size of block
  385. const uint8_t* end_data_ptr = data_ptr + uvalue;
  386. while (data_ptr < end_data_ptr) {
  387. OS << format("%2.2x ", *data_ptr);
  388. ++data_ptr;
  389. }
  390. }
  391. else
  392. OS << "NULL";
  393. }
  394. break;
  395. case DW_FORM_sdata: OS << Value.sval; break;
  396. case DW_FORM_udata: OS << Value.uval; break;
  397. case DW_FORM_strp: {
  398. OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
  399. dumpString(OS, cu);
  400. break;
  401. }
  402. case DW_FORM_GNU_str_index: {
  403. OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
  404. dumpString(OS, cu);
  405. break;
  406. }
  407. case DW_FORM_GNU_strp_alt: {
  408. OS << format("alt indirect string, offset: 0x%" PRIx64 "", uvalue);
  409. dumpString(OS, cu);
  410. break;
  411. }
  412. case DW_FORM_ref_addr:
  413. OS << format("0x%016" PRIx64, uvalue);
  414. break;
  415. case DW_FORM_ref1:
  416. cu_relative_offset = true;
  417. OS << format("cu + 0x%2.2x", (uint8_t)uvalue);
  418. break;
  419. case DW_FORM_ref2:
  420. cu_relative_offset = true;
  421. OS << format("cu + 0x%4.4x", (uint16_t)uvalue);
  422. break;
  423. case DW_FORM_ref4:
  424. cu_relative_offset = true;
  425. OS << format("cu + 0x%4.4x", (uint32_t)uvalue);
  426. break;
  427. case DW_FORM_ref8:
  428. cu_relative_offset = true;
  429. OS << format("cu + 0x%8.8" PRIx64, uvalue);
  430. break;
  431. case DW_FORM_ref_udata:
  432. cu_relative_offset = true;
  433. OS << format("cu + 0x%" PRIx64, uvalue);
  434. break;
  435. case DW_FORM_GNU_ref_alt:
  436. OS << format("<alt 0x%" PRIx64 ">", uvalue);
  437. break;
  438. // All DW_FORM_indirect attributes should be resolved prior to calling
  439. // this function
  440. case DW_FORM_indirect:
  441. OS << "DW_FORM_indirect";
  442. break;
  443. // Should be formatted to 64-bit for DWARF64.
  444. case DW_FORM_sec_offset:
  445. OS << format("0x%08x", (uint32_t)uvalue);
  446. break;
  447. default:
  448. OS << format("DW_FORM(0x%4.4x)", Form);
  449. break;
  450. }
  451. if (cu_relative_offset) {
  452. OS << " => {";
  453. WithColor(OS, syntax::Address).get()
  454. << format("0x%8.8" PRIx64, uvalue + (cu ? cu->getOffset() : 0));
  455. OS << "}";
  456. }
  457. }
  458. void DWARFFormValue::dumpString(raw_ostream &OS, const DWARFUnit *U) const {
  459. Optional<const char *> DbgStr = getAsCString(U);
  460. if (DbgStr.hasValue()) {
  461. raw_ostream &COS = WithColor(OS, syntax::String);
  462. COS << '"';
  463. COS.write_escaped(DbgStr.getValue());
  464. COS << '"';
  465. }
  466. }
  467. Optional<const char *> DWARFFormValue::getAsCString(const DWARFUnit *U) const {
  468. if (!isFormClass(FC_String))
  469. return None;
  470. if (Form == DW_FORM_string)
  471. return Value.cstr;
  472. // FIXME: Add support for DW_FORM_GNU_strp_alt
  473. if (Form == DW_FORM_GNU_strp_alt || U == nullptr)
  474. return None;
  475. uint32_t Offset = Value.uval;
  476. if (Form == DW_FORM_GNU_str_index) {
  477. uint32_t StrOffset;
  478. if (!U->getStringOffsetSectionItem(Offset, StrOffset))
  479. return None;
  480. Offset = StrOffset;
  481. }
  482. if (const char *Str = U->getStringExtractor().getCStr(&Offset)) {
  483. return Str;
  484. }
  485. return None;
  486. }
  487. Optional<uint64_t> DWARFFormValue::getAsAddress(const DWARFUnit *U) const {
  488. if (!isFormClass(FC_Address))
  489. return None;
  490. if (Form == DW_FORM_GNU_addr_index) {
  491. uint32_t Index = Value.uval;
  492. uint64_t Result;
  493. if (!U || !U->getAddrOffsetSectionItem(Index, Result))
  494. return None;
  495. return Result;
  496. }
  497. return Value.uval;
  498. }
  499. Optional<uint64_t> DWARFFormValue::getAsReference(const DWARFUnit *U) const {
  500. if (!isFormClass(FC_Reference))
  501. return None;
  502. switch (Form) {
  503. case DW_FORM_ref1:
  504. case DW_FORM_ref2:
  505. case DW_FORM_ref4:
  506. case DW_FORM_ref8:
  507. case DW_FORM_ref_udata:
  508. if (!U)
  509. return None;
  510. return Value.uval + U->getOffset();
  511. case DW_FORM_ref_addr:
  512. return Value.uval;
  513. // FIXME: Add proper support for DW_FORM_ref_sig8 and DW_FORM_GNU_ref_alt.
  514. default:
  515. return None;
  516. }
  517. }
  518. Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
  519. if (!isFormClass(FC_SectionOffset))
  520. return None;
  521. return Value.uval;
  522. }
  523. Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
  524. if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag))
  525. || Form == DW_FORM_sdata)
  526. return None;
  527. return Value.uval;
  528. }
  529. Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
  530. if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
  531. (Form == DW_FORM_udata && uint64_t(LLONG_MAX) < Value.uval))
  532. return None;
  533. switch (Form) {
  534. case DW_FORM_data4:
  535. return int32_t(Value.uval);
  536. case DW_FORM_data2:
  537. return int16_t(Value.uval);
  538. case DW_FORM_data1:
  539. return int8_t(Value.uval);
  540. case DW_FORM_sdata:
  541. case DW_FORM_data8:
  542. default:
  543. return Value.sval;
  544. }
  545. }
  546. Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
  547. if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc))
  548. return None;
  549. return ArrayRef<uint8_t>(Value.data, Value.uval);
  550. }