DWARFFormValue.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. //===- DWARFFormValue.cpp -------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/None.h"
  11. #include "llvm/ADT/Optional.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/BinaryFormat/Dwarf.h"
  14. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  15. #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
  16. #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. #include "llvm/Support/Format.h"
  19. #include "llvm/Support/WithColor.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <cinttypes>
  22. #include <cstdint>
  23. #include <limits>
  24. using namespace llvm;
  25. using namespace dwarf;
  26. static const DWARFFormValue::FormClass DWARF5FormClasses[] = {
  27. DWARFFormValue::FC_Unknown, // 0x0
  28. DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
  29. DWARFFormValue::FC_Unknown, // 0x02 unused
  30. DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
  31. DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
  32. DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
  33. // --- These can be FC_SectionOffset in DWARF3 and below:
  34. DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
  35. DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
  36. // ---
  37. DWARFFormValue::FC_String, // 0x08 DW_FORM_string
  38. DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
  39. DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
  40. DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
  41. DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
  42. DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
  43. DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
  44. DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
  45. DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
  46. DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
  47. DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
  48. DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
  49. DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
  50. DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
  51. DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
  52. DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
  53. DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
  54. DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
  55. DWARFFormValue::FC_String, // 0x1a DW_FORM_strx
  56. DWARFFormValue::FC_Address, // 0x1b DW_FORM_addrx
  57. DWARFFormValue::FC_Reference, // 0x1c DW_FORM_ref_sup4
  58. DWARFFormValue::FC_String, // 0x1d DW_FORM_strp_sup
  59. DWARFFormValue::FC_Constant, // 0x1e DW_FORM_data16
  60. DWARFFormValue::FC_String, // 0x1f DW_FORM_line_strp
  61. DWARFFormValue::FC_Reference, // 0x20 DW_FORM_ref_sig8
  62. DWARFFormValue::FC_Constant, // 0x21 DW_FORM_implicit_const
  63. DWARFFormValue::FC_SectionOffset, // 0x22 DW_FORM_loclistx
  64. DWARFFormValue::FC_SectionOffset, // 0x23 DW_FORM_rnglistx
  65. DWARFFormValue::FC_Reference, // 0x24 DW_FORM_ref_sup8
  66. DWARFFormValue::FC_String, // 0x25 DW_FORM_strx1
  67. DWARFFormValue::FC_String, // 0x26 DW_FORM_strx2
  68. DWARFFormValue::FC_String, // 0x27 DW_FORM_strx3
  69. DWARFFormValue::FC_String, // 0x28 DW_FORM_strx4
  70. DWARFFormValue::FC_Address, // 0x29 DW_FORM_addrx1
  71. DWARFFormValue::FC_Address, // 0x2a DW_FORM_addrx2
  72. DWARFFormValue::FC_Address, // 0x2b DW_FORM_addrx3
  73. DWARFFormValue::FC_Address, // 0x2c DW_FORM_addrx4
  74. DWARFFormValue::FC_Address, // 0x2001 DW_FORM_addrx_offset
  75. };
  76. DWARFFormValue DWARFFormValue::createFromSValue(dwarf::Form F, int64_t V) {
  77. return DWARFFormValue(F, ValueType(V));
  78. }
  79. DWARFFormValue DWARFFormValue::createFromUValue(dwarf::Form F, uint64_t V) {
  80. return DWARFFormValue(F, ValueType(V));
  81. }
  82. DWARFFormValue DWARFFormValue::createFromPValue(dwarf::Form F, const char *V) {
  83. return DWARFFormValue(F, ValueType(V));
  84. }
  85. DWARFFormValue DWARFFormValue::createFromBlockValue(dwarf::Form F,
  86. ArrayRef<uint8_t> D) {
  87. ValueType V;
  88. V.uval = D.size();
  89. V.data = D.data();
  90. return DWARFFormValue(F, V);
  91. }
  92. DWARFFormValue DWARFFormValue::createFromUnit(dwarf::Form F, const DWARFUnit *U,
  93. uint64_t *OffsetPtr) {
  94. DWARFFormValue FormValue(F);
  95. FormValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
  96. U->getFormParams(), U);
  97. return FormValue;
  98. }
  99. bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
  100. uint64_t *OffsetPtr,
  101. const dwarf::FormParams Params) {
  102. bool Indirect = false;
  103. do {
  104. switch (Form) {
  105. // Blocks of inlined data that have a length field and the data bytes
  106. // inlined in the .debug_info.
  107. case DW_FORM_exprloc:
  108. case DW_FORM_block: {
  109. uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
  110. *OffsetPtr += size;
  111. return true;
  112. }
  113. case DW_FORM_block1: {
  114. uint8_t size = DebugInfoData.getU8(OffsetPtr);
  115. *OffsetPtr += size;
  116. return true;
  117. }
  118. case DW_FORM_block2: {
  119. uint16_t size = DebugInfoData.getU16(OffsetPtr);
  120. *OffsetPtr += size;
  121. return true;
  122. }
  123. case DW_FORM_block4: {
  124. uint32_t size = DebugInfoData.getU32(OffsetPtr);
  125. *OffsetPtr += size;
  126. return true;
  127. }
  128. // Inlined NULL terminated C-strings.
  129. case DW_FORM_string:
  130. DebugInfoData.getCStr(OffsetPtr);
  131. return true;
  132. case DW_FORM_addr:
  133. case DW_FORM_ref_addr:
  134. case DW_FORM_flag_present:
  135. case DW_FORM_data1:
  136. case DW_FORM_data2:
  137. case DW_FORM_data4:
  138. case DW_FORM_data8:
  139. case DW_FORM_data16:
  140. case DW_FORM_flag:
  141. case DW_FORM_ref1:
  142. case DW_FORM_ref2:
  143. case DW_FORM_ref4:
  144. case DW_FORM_ref8:
  145. case DW_FORM_ref_sig8:
  146. case DW_FORM_ref_sup4:
  147. case DW_FORM_ref_sup8:
  148. case DW_FORM_strx1:
  149. case DW_FORM_strx2:
  150. case DW_FORM_strx4:
  151. case DW_FORM_addrx1:
  152. case DW_FORM_addrx2:
  153. case DW_FORM_addrx4:
  154. case DW_FORM_sec_offset:
  155. case DW_FORM_strp:
  156. case DW_FORM_strp_sup:
  157. case DW_FORM_line_strp:
  158. case DW_FORM_GNU_ref_alt:
  159. case DW_FORM_GNU_strp_alt:
  160. case DW_FORM_implicit_const:
  161. if (Optional<uint8_t> FixedSize =
  162. dwarf::getFixedFormByteSize(Form, Params)) {
  163. *OffsetPtr += *FixedSize;
  164. return true;
  165. }
  166. return false;
  167. // signed or unsigned LEB 128 values.
  168. case DW_FORM_sdata:
  169. DebugInfoData.getSLEB128(OffsetPtr);
  170. return true;
  171. case DW_FORM_udata:
  172. case DW_FORM_ref_udata:
  173. case DW_FORM_strx:
  174. case DW_FORM_addrx:
  175. case DW_FORM_loclistx:
  176. case DW_FORM_rnglistx:
  177. case DW_FORM_GNU_addr_index:
  178. case DW_FORM_GNU_str_index:
  179. DebugInfoData.getULEB128(OffsetPtr);
  180. return true;
  181. case DW_FORM_LLVM_addrx_offset:
  182. DebugInfoData.getULEB128(OffsetPtr);
  183. *OffsetPtr += 4;
  184. return true;
  185. case DW_FORM_indirect:
  186. Indirect = true;
  187. Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
  188. break;
  189. default:
  190. return false;
  191. }
  192. } while (Indirect);
  193. return true;
  194. }
  195. bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
  196. // First, check DWARF5 form classes.
  197. if (Form < makeArrayRef(DWARF5FormClasses).size() &&
  198. DWARF5FormClasses[Form] == FC)
  199. return true;
  200. // Check more forms from extensions and proposals.
  201. switch (Form) {
  202. case DW_FORM_GNU_ref_alt:
  203. return (FC == FC_Reference);
  204. case DW_FORM_GNU_addr_index:
  205. return (FC == FC_Address);
  206. case DW_FORM_GNU_str_index:
  207. case DW_FORM_GNU_strp_alt:
  208. return (FC == FC_String);
  209. case DW_FORM_LLVM_addrx_offset:
  210. return (FC == FC_Address);
  211. default:
  212. break;
  213. }
  214. if (FC == FC_SectionOffset) {
  215. if (Form == DW_FORM_strp || Form == DW_FORM_line_strp)
  216. return true;
  217. // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section
  218. // offset. If we don't have a DWARFUnit, default to the old behavior.
  219. if (Form == DW_FORM_data4 || Form == DW_FORM_data8)
  220. return !U || U->getVersion() <= 3;
  221. }
  222. return false;
  223. }
  224. bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data,
  225. uint64_t *OffsetPtr, dwarf::FormParams FP,
  226. const DWARFContext *Ctx,
  227. const DWARFUnit *CU) {
  228. if (!Ctx && CU)
  229. Ctx = &CU->getContext();
  230. C = Ctx;
  231. U = CU;
  232. Format = FP.Format;
  233. bool Indirect = false;
  234. bool IsBlock = false;
  235. Value.data = nullptr;
  236. // Read the value for the form into value and follow and DW_FORM_indirect
  237. // instances we run into
  238. Error Err = Error::success();
  239. do {
  240. Indirect = false;
  241. switch (Form) {
  242. case DW_FORM_addr:
  243. case DW_FORM_ref_addr: {
  244. uint16_t Size =
  245. (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
  246. Value.uval =
  247. Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex, &Err);
  248. break;
  249. }
  250. case DW_FORM_exprloc:
  251. case DW_FORM_block:
  252. Value.uval = Data.getULEB128(OffsetPtr, &Err);
  253. IsBlock = true;
  254. break;
  255. case DW_FORM_block1:
  256. Value.uval = Data.getU8(OffsetPtr, &Err);
  257. IsBlock = true;
  258. break;
  259. case DW_FORM_block2:
  260. Value.uval = Data.getU16(OffsetPtr, &Err);
  261. IsBlock = true;
  262. break;
  263. case DW_FORM_block4:
  264. Value.uval = Data.getU32(OffsetPtr, &Err);
  265. IsBlock = true;
  266. break;
  267. case DW_FORM_data1:
  268. case DW_FORM_ref1:
  269. case DW_FORM_flag:
  270. case DW_FORM_strx1:
  271. case DW_FORM_addrx1:
  272. Value.uval = Data.getU8(OffsetPtr, &Err);
  273. break;
  274. case DW_FORM_data2:
  275. case DW_FORM_ref2:
  276. case DW_FORM_strx2:
  277. case DW_FORM_addrx2:
  278. Value.uval = Data.getU16(OffsetPtr, &Err);
  279. break;
  280. case DW_FORM_strx3:
  281. Value.uval = Data.getU24(OffsetPtr, &Err);
  282. break;
  283. case DW_FORM_data4:
  284. case DW_FORM_ref4:
  285. case DW_FORM_ref_sup4:
  286. case DW_FORM_strx4:
  287. case DW_FORM_addrx4:
  288. Value.uval = Data.getRelocatedValue(4, OffsetPtr, nullptr, &Err);
  289. break;
  290. case DW_FORM_data8:
  291. case DW_FORM_ref8:
  292. case DW_FORM_ref_sup8:
  293. Value.uval = Data.getRelocatedValue(8, OffsetPtr, nullptr, &Err);
  294. break;
  295. case DW_FORM_data16:
  296. // Treat this like a 16-byte block.
  297. Value.uval = 16;
  298. IsBlock = true;
  299. break;
  300. case DW_FORM_sdata:
  301. Value.sval = Data.getSLEB128(OffsetPtr, &Err);
  302. break;
  303. case DW_FORM_udata:
  304. case DW_FORM_ref_udata:
  305. case DW_FORM_rnglistx:
  306. case DW_FORM_loclistx:
  307. case DW_FORM_GNU_addr_index:
  308. case DW_FORM_GNU_str_index:
  309. case DW_FORM_addrx:
  310. case DW_FORM_strx:
  311. Value.uval = Data.getULEB128(OffsetPtr, &Err);
  312. break;
  313. case DW_FORM_LLVM_addrx_offset:
  314. Value.uval = Data.getULEB128(OffsetPtr, &Err) << 32;
  315. Value.uval |= Data.getU32(OffsetPtr, &Err);
  316. break;
  317. case DW_FORM_string:
  318. Value.cstr = Data.getCStr(OffsetPtr, &Err);
  319. break;
  320. case DW_FORM_indirect:
  321. Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr, &Err));
  322. Indirect = true;
  323. break;
  324. case DW_FORM_strp:
  325. case DW_FORM_sec_offset:
  326. case DW_FORM_GNU_ref_alt:
  327. case DW_FORM_GNU_strp_alt:
  328. case DW_FORM_line_strp:
  329. case DW_FORM_strp_sup: {
  330. Value.uval = Data.getRelocatedValue(FP.getDwarfOffsetByteSize(),
  331. OffsetPtr, nullptr, &Err);
  332. break;
  333. }
  334. case DW_FORM_flag_present:
  335. Value.uval = 1;
  336. break;
  337. case DW_FORM_ref_sig8:
  338. Value.uval = Data.getU64(OffsetPtr, &Err);
  339. break;
  340. case DW_FORM_implicit_const:
  341. // Value has been already set by DWARFFormValue::createFromSValue.
  342. break;
  343. default:
  344. // DWARFFormValue::skipValue() will have caught this and caused all
  345. // DWARF DIEs to fail to be parsed, so this code is not be reachable.
  346. llvm_unreachable("unsupported form");
  347. }
  348. } while (Indirect && !Err);
  349. if (IsBlock)
  350. Value.data = Data.getBytes(OffsetPtr, Value.uval, &Err).bytes_begin();
  351. return !errorToBool(std::move(Err));
  352. }
  353. void DWARFFormValue::dumpAddress(raw_ostream &OS, uint8_t AddressSize,
  354. uint64_t Address) {
  355. uint8_t HexDigits = AddressSize * 2;
  356. OS << format("0x%*.*" PRIx64, HexDigits, HexDigits, Address);
  357. }
  358. void DWARFFormValue::dumpSectionedAddress(raw_ostream &OS,
  359. DIDumpOptions DumpOpts,
  360. object::SectionedAddress SA) const {
  361. dumpAddress(OS, U->getAddressByteSize(), SA.Address);
  362. dumpAddressSection(U->getContext().getDWARFObj(), OS, DumpOpts,
  363. SA.SectionIndex);
  364. }
  365. void DWARFFormValue::dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS,
  366. DIDumpOptions DumpOpts,
  367. uint64_t SectionIndex) {
  368. if (!DumpOpts.Verbose || SectionIndex == -1ULL)
  369. return;
  370. ArrayRef<SectionName> SectionNames = Obj.getSectionNames();
  371. const auto &SecRef = SectionNames[SectionIndex];
  372. OS << " \"" << SecRef.Name << '\"';
  373. // Print section index if name is not unique.
  374. if (!SecRef.IsNameUnique)
  375. OS << format(" [%" PRIu64 "]", SectionIndex);
  376. }
  377. void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
  378. uint64_t UValue = Value.uval;
  379. bool CURelativeOffset = false;
  380. raw_ostream &AddrOS = DumpOpts.ShowAddresses
  381. ? WithColor(OS, HighlightColor::Address).get()
  382. : nulls();
  383. int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
  384. switch (Form) {
  385. case DW_FORM_addr:
  386. dumpSectionedAddress(AddrOS, DumpOpts, {Value.uval, Value.SectionIndex});
  387. break;
  388. case DW_FORM_addrx:
  389. case DW_FORM_addrx1:
  390. case DW_FORM_addrx2:
  391. case DW_FORM_addrx3:
  392. case DW_FORM_addrx4:
  393. case DW_FORM_GNU_addr_index: {
  394. if (U == nullptr) {
  395. OS << "<invalid dwarf unit>";
  396. break;
  397. }
  398. Optional<object::SectionedAddress> A = U->getAddrOffsetSectionItem(UValue);
  399. if (!A || DumpOpts.Verbose)
  400. AddrOS << format("indexed (%8.8x) address = ", (uint32_t)UValue);
  401. if (A)
  402. dumpSectionedAddress(AddrOS, DumpOpts, *A);
  403. else
  404. OS << "<unresolved>";
  405. break;
  406. }
  407. case DW_FORM_LLVM_addrx_offset: {
  408. if (U == nullptr) {
  409. OS << "<invalid dwarf unit>";
  410. break;
  411. }
  412. uint32_t Index = UValue >> 32;
  413. uint32_t Offset = UValue & 0xffffffff;
  414. Optional<object::SectionedAddress> A = U->getAddrOffsetSectionItem(Index);
  415. if (!A || DumpOpts.Verbose)
  416. AddrOS << format("indexed (%8.8x) + 0x%x address = ", Index, Offset);
  417. if (A) {
  418. A->Address += Offset;
  419. dumpSectionedAddress(AddrOS, DumpOpts, *A);
  420. } else
  421. OS << "<unresolved>";
  422. break;
  423. }
  424. case DW_FORM_flag_present:
  425. OS << "true";
  426. break;
  427. case DW_FORM_flag:
  428. case DW_FORM_data1:
  429. OS << format("0x%02x", (uint8_t)UValue);
  430. break;
  431. case DW_FORM_data2:
  432. OS << format("0x%04x", (uint16_t)UValue);
  433. break;
  434. case DW_FORM_data4:
  435. OS << format("0x%08x", (uint32_t)UValue);
  436. break;
  437. case DW_FORM_ref_sig8:
  438. AddrOS << format("0x%016" PRIx64, UValue);
  439. break;
  440. case DW_FORM_data8:
  441. OS << format("0x%016" PRIx64, UValue);
  442. break;
  443. case DW_FORM_data16:
  444. OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
  445. break;
  446. case DW_FORM_string:
  447. OS << '"';
  448. OS.write_escaped(Value.cstr);
  449. OS << '"';
  450. break;
  451. case DW_FORM_exprloc:
  452. case DW_FORM_block:
  453. case DW_FORM_block1:
  454. case DW_FORM_block2:
  455. case DW_FORM_block4:
  456. if (UValue > 0) {
  457. switch (Form) {
  458. case DW_FORM_exprloc:
  459. case DW_FORM_block:
  460. AddrOS << format("<0x%" PRIx64 "> ", UValue);
  461. break;
  462. case DW_FORM_block1:
  463. AddrOS << format("<0x%2.2x> ", (uint8_t)UValue);
  464. break;
  465. case DW_FORM_block2:
  466. AddrOS << format("<0x%4.4x> ", (uint16_t)UValue);
  467. break;
  468. case DW_FORM_block4:
  469. AddrOS << format("<0x%8.8x> ", (uint32_t)UValue);
  470. break;
  471. default:
  472. break;
  473. }
  474. const uint8_t *DataPtr = Value.data;
  475. if (DataPtr) {
  476. // UValue contains size of block
  477. const uint8_t *EndDataPtr = DataPtr + UValue;
  478. while (DataPtr < EndDataPtr) {
  479. AddrOS << format("%2.2x ", *DataPtr);
  480. ++DataPtr;
  481. }
  482. } else
  483. OS << "NULL";
  484. }
  485. break;
  486. case DW_FORM_sdata:
  487. case DW_FORM_implicit_const:
  488. OS << Value.sval;
  489. break;
  490. case DW_FORM_udata:
  491. OS << Value.uval;
  492. break;
  493. case DW_FORM_strp:
  494. if (DumpOpts.Verbose)
  495. OS << format(" .debug_str[0x%0*" PRIx64 "] = ", OffsetDumpWidth, UValue);
  496. dumpString(OS);
  497. break;
  498. case DW_FORM_line_strp:
  499. if (DumpOpts.Verbose)
  500. OS << format(" .debug_line_str[0x%0*" PRIx64 "] = ", OffsetDumpWidth,
  501. UValue);
  502. dumpString(OS);
  503. break;
  504. case DW_FORM_strx:
  505. case DW_FORM_strx1:
  506. case DW_FORM_strx2:
  507. case DW_FORM_strx3:
  508. case DW_FORM_strx4:
  509. case DW_FORM_GNU_str_index:
  510. if (DumpOpts.Verbose)
  511. OS << format("indexed (%8.8x) string = ", (uint32_t)UValue);
  512. dumpString(OS);
  513. break;
  514. case DW_FORM_GNU_strp_alt:
  515. if (DumpOpts.Verbose)
  516. OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
  517. dumpString(OS);
  518. break;
  519. case DW_FORM_ref_addr:
  520. AddrOS << format("0x%016" PRIx64, UValue);
  521. break;
  522. case DW_FORM_ref1:
  523. CURelativeOffset = true;
  524. if (DumpOpts.Verbose)
  525. AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
  526. break;
  527. case DW_FORM_ref2:
  528. CURelativeOffset = true;
  529. if (DumpOpts.Verbose)
  530. AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
  531. break;
  532. case DW_FORM_ref4:
  533. CURelativeOffset = true;
  534. if (DumpOpts.Verbose)
  535. AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
  536. break;
  537. case DW_FORM_ref8:
  538. CURelativeOffset = true;
  539. if (DumpOpts.Verbose)
  540. AddrOS << format("cu + 0x%8.8" PRIx64, UValue);
  541. break;
  542. case DW_FORM_ref_udata:
  543. CURelativeOffset = true;
  544. if (DumpOpts.Verbose)
  545. AddrOS << format("cu + 0x%" PRIx64, UValue);
  546. break;
  547. case DW_FORM_GNU_ref_alt:
  548. AddrOS << format("<alt 0x%" PRIx64 ">", UValue);
  549. break;
  550. // All DW_FORM_indirect attributes should be resolved prior to calling
  551. // this function
  552. case DW_FORM_indirect:
  553. OS << "DW_FORM_indirect";
  554. break;
  555. case DW_FORM_rnglistx:
  556. OS << format("indexed (0x%x) rangelist = ", (uint32_t)UValue);
  557. break;
  558. case DW_FORM_loclistx:
  559. OS << format("indexed (0x%x) loclist = ", (uint32_t)UValue);
  560. break;
  561. case DW_FORM_sec_offset:
  562. AddrOS << format("0x%0*" PRIx64, OffsetDumpWidth, UValue);
  563. break;
  564. default:
  565. OS << format("DW_FORM(0x%4.4x)", Form);
  566. break;
  567. }
  568. if (CURelativeOffset) {
  569. if (DumpOpts.Verbose)
  570. OS << " => {";
  571. if (DumpOpts.ShowAddresses)
  572. WithColor(OS, HighlightColor::Address).get()
  573. << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
  574. if (DumpOpts.Verbose)
  575. OS << "}";
  576. }
  577. }
  578. void DWARFFormValue::dumpString(raw_ostream &OS) const {
  579. if (auto DbgStr = dwarf::toString(*this)) {
  580. auto COS = WithColor(OS, HighlightColor::String);
  581. COS.get() << '"';
  582. COS.get().write_escaped(*DbgStr);
  583. COS.get() << '"';
  584. }
  585. }
  586. Expected<const char *> DWARFFormValue::getAsCString() const {
  587. if (!isFormClass(FC_String))
  588. return make_error<StringError>("Invalid form for string attribute",
  589. inconvertibleErrorCode());
  590. if (Form == DW_FORM_string)
  591. return Value.cstr;
  592. // FIXME: Add support for DW_FORM_GNU_strp_alt
  593. if (Form == DW_FORM_GNU_strp_alt || C == nullptr)
  594. return make_error<StringError>("Unsupported form for string attribute",
  595. inconvertibleErrorCode());
  596. uint64_t Offset = Value.uval;
  597. Optional<uint32_t> Index;
  598. if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
  599. Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
  600. Form == DW_FORM_strx4) {
  601. if (!U)
  602. return make_error<StringError>("API limitation - string extraction not "
  603. "available without a DWARFUnit",
  604. inconvertibleErrorCode());
  605. Expected<uint64_t> StrOffset = U->getStringOffsetSectionItem(Offset);
  606. Index = Offset;
  607. if (!StrOffset)
  608. return StrOffset.takeError();
  609. Offset = *StrOffset;
  610. }
  611. // Prefer the Unit's string extractor, because for .dwo it will point to
  612. // .debug_str.dwo, while the Context's extractor always uses .debug_str.
  613. DataExtractor StrData = Form == DW_FORM_line_strp
  614. ? C->getLineStringExtractor()
  615. : U ? U->getStringExtractor()
  616. : C->getStringExtractor();
  617. if (const char *Str = StrData.getCStr(&Offset))
  618. return Str;
  619. std::string Msg = FormEncodingString(Form).str();
  620. if (Index)
  621. Msg += (" uses index " + Twine(*Index) + ", but the referenced string").str();
  622. Msg += (" offset " + Twine(Offset) + " is beyond .debug_str bounds").str();
  623. return make_error<StringError>(Msg,
  624. inconvertibleErrorCode());
  625. }
  626. Optional<uint64_t> DWARFFormValue::getAsAddress() const {
  627. if (auto SA = getAsSectionedAddress())
  628. return SA->Address;
  629. return None;
  630. }
  631. Optional<object::SectionedAddress>
  632. DWARFFormValue::getAsSectionedAddress() const {
  633. if (!isFormClass(FC_Address))
  634. return None;
  635. bool AddrOffset = Form == dwarf::DW_FORM_LLVM_addrx_offset;
  636. if (Form == DW_FORM_GNU_addr_index || Form == DW_FORM_addrx || AddrOffset) {
  637. uint32_t Index = AddrOffset ? (Value.uval >> 32) : Value.uval;
  638. if (!U)
  639. return None;
  640. Optional<object::SectionedAddress> SA = U->getAddrOffsetSectionItem(Index);
  641. if (!SA)
  642. return None;
  643. if (AddrOffset)
  644. SA->Address += (Value.uval & 0xffffffff);
  645. return SA;
  646. }
  647. return {{Value.uval, Value.SectionIndex}};
  648. }
  649. Optional<uint64_t> DWARFFormValue::getAsReference() const {
  650. if (auto R = getAsRelativeReference())
  651. return R->Unit ? R->Unit->getOffset() + R->Offset : R->Offset;
  652. return None;
  653. }
  654. Optional<DWARFFormValue::UnitOffset> DWARFFormValue::getAsRelativeReference() const {
  655. if (!isFormClass(FC_Reference))
  656. return None;
  657. switch (Form) {
  658. case DW_FORM_ref1:
  659. case DW_FORM_ref2:
  660. case DW_FORM_ref4:
  661. case DW_FORM_ref8:
  662. case DW_FORM_ref_udata:
  663. if (!U)
  664. return None;
  665. return UnitOffset{const_cast<DWARFUnit*>(U), Value.uval};
  666. case DW_FORM_ref_addr:
  667. case DW_FORM_ref_sig8:
  668. case DW_FORM_GNU_ref_alt:
  669. return UnitOffset{nullptr, Value.uval};
  670. default:
  671. return None;
  672. }
  673. }
  674. Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
  675. if (!isFormClass(FC_SectionOffset))
  676. return None;
  677. return Value.uval;
  678. }
  679. Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
  680. if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
  681. Form == DW_FORM_sdata)
  682. return None;
  683. return Value.uval;
  684. }
  685. Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
  686. if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
  687. (Form == DW_FORM_udata &&
  688. uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
  689. return None;
  690. switch (Form) {
  691. case DW_FORM_data4:
  692. return int32_t(Value.uval);
  693. case DW_FORM_data2:
  694. return int16_t(Value.uval);
  695. case DW_FORM_data1:
  696. return int8_t(Value.uval);
  697. case DW_FORM_sdata:
  698. case DW_FORM_data8:
  699. default:
  700. return Value.sval;
  701. }
  702. }
  703. Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
  704. if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
  705. Form != DW_FORM_data16)
  706. return None;
  707. return makeArrayRef(Value.data, Value.uval);
  708. }
  709. Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
  710. if (!isFormClass(FC_String) && Form == DW_FORM_string)
  711. return None;
  712. return Value.uval;
  713. }
  714. Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
  715. if (!isFormClass(FC_Reference))
  716. return None;
  717. return Value.uval;
  718. }
  719. Optional<std::string>
  720. DWARFFormValue::getAsFile(DILineInfoSpecifier::FileLineInfoKind Kind) const {
  721. if (U == nullptr || !isFormClass(FC_Constant))
  722. return None;
  723. DWARFUnit *DLU = const_cast<DWARFUnit *>(U)->getLinkedUnit();
  724. if (auto *LT = DLU->getContext().getLineTableForUnit(DLU)) {
  725. std::string FileName;
  726. if (LT->getFileNameByIndex(Value.uval, DLU->getCompilationDir(), Kind,
  727. FileName))
  728. return FileName;
  729. }
  730. return None;
  731. }