blob: 1aa43c6b6517df6a053bc059b0d3c7d71f7efcc6 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFFormValue.cpp -------------------------------------------------===//
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00002//
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
Paul Robinsonae2e6f372017-05-03 21:53:21 +000010#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Alexey Samsonov48cbda52013-10-28 23:01:48 +000011#include "llvm/ADT/ArrayRef.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000012#include "llvm/ADT/None.h"
13#include "llvm/ADT/Optional.h"
Alexey Samsonov48cbda52013-10-28 23:01:48 +000014#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000015#include "llvm/BinaryFormat/Dwarf.h"
Zachary Turner82af9432015-01-30 18:07:45 +000016#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000017#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
18#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000019#include "llvm/Support/ErrorHandling.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000020#include "llvm/Support/Format.h"
Jonas Devlieghere69217532018-03-09 09:56:24 +000021#include "llvm/Support/WithColor.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000022#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000023#include <cinttypes>
24#include <cstdint>
Matt Arsenault45cbfa52015-10-21 21:10:12 +000025#include <limits>
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000026
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000027using namespace llvm;
28using namespace dwarf;
29
Paul Robinson01158442018-01-25 23:06:36 +000030static const DWARFFormValue::FormClass DWARF5FormClasses[] = {
Paul Robinsonae2e6f372017-05-03 21:53:21 +000031 DWARFFormValue::FC_Unknown, // 0x0
32 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr
33 DWARFFormValue::FC_Unknown, // 0x02 unused
34 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2
35 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4
36 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2
37 // --- These can be FC_SectionOffset in DWARF3 and below:
38 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4
39 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8
40 // ---
41 DWARFFormValue::FC_String, // 0x08 DW_FORM_string
42 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block
43 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1
44 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1
45 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag
46 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata
47 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp
48 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata
49 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr
50 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1
51 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2
52 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4
53 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8
54 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata
55 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect
56 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset
57 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc
58 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present
Paul Robinson01158442018-01-25 23:06:36 +000059 DWARFFormValue::FC_String, // 0x1a DW_FORM_strx
60 DWARFFormValue::FC_Address, // 0x1b DW_FORM_addrx
61 DWARFFormValue::FC_Reference, // 0x1c DW_FORM_ref_sup4
62 DWARFFormValue::FC_String, // 0x1d DW_FORM_strp_sup
63 DWARFFormValue::FC_Constant, // 0x1e DW_FORM_data16
64 DWARFFormValue::FC_String, // 0x1f DW_FORM_line_strp
65 DWARFFormValue::FC_Reference, // 0x20 DW_FORM_ref_sig8
66 DWARFFormValue::FC_Constant, // 0x21 DW_FORM_implicit_const
67 DWARFFormValue::FC_SectionOffset, // 0x22 DW_FORM_loclistx
68 DWARFFormValue::FC_SectionOffset, // 0x23 DW_FORM_rnglistx
69 DWARFFormValue::FC_Reference, // 0x24 DW_FORM_ref_sup8
70 DWARFFormValue::FC_String, // 0x25 DW_FORM_strx1
71 DWARFFormValue::FC_String, // 0x26 DW_FORM_strx2
72 DWARFFormValue::FC_String, // 0x27 DW_FORM_strx3
73 DWARFFormValue::FC_String, // 0x28 DW_FORM_strx4
74 DWARFFormValue::FC_Address, // 0x29 DW_FORM_addrx1
75 DWARFFormValue::FC_Address, // 0x2a DW_FORM_addrx2
76 DWARFFormValue::FC_Address, // 0x2b DW_FORM_addrx3
77 DWARFFormValue::FC_Address, // 0x2c DW_FORM_addrx4
78
Alexey Samsonov48cbda52013-10-28 23:01:48 +000079};
80
Paul Robinson75c068c2017-06-26 18:43:01 +000081bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
82 uint32_t *OffsetPtr,
Pavel Labath322711f2018-03-14 09:39:54 +000083 const dwarf::FormParams Params) {
Greg Clayton82f12b12016-11-11 16:21:37 +000084 bool Indirect = false;
85 do {
86 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +000087 // Blocks of inlined data that have a length field and the data bytes
88 // inlined in the .debug_info.
89 case DW_FORM_exprloc:
90 case DW_FORM_block: {
91 uint64_t size = DebugInfoData.getULEB128(OffsetPtr);
92 *OffsetPtr += size;
93 return true;
94 }
95 case DW_FORM_block1: {
96 uint8_t size = DebugInfoData.getU8(OffsetPtr);
97 *OffsetPtr += size;
98 return true;
99 }
100 case DW_FORM_block2: {
101 uint16_t size = DebugInfoData.getU16(OffsetPtr);
102 *OffsetPtr += size;
103 return true;
104 }
105 case DW_FORM_block4: {
106 uint32_t size = DebugInfoData.getU32(OffsetPtr);
107 *OffsetPtr += size;
108 return true;
109 }
110
111 // Inlined NULL terminated C-strings.
112 case DW_FORM_string:
113 DebugInfoData.getCStr(OffsetPtr);
114 return true;
115
116 case DW_FORM_addr:
117 case DW_FORM_ref_addr:
118 case DW_FORM_flag_present:
119 case DW_FORM_data1:
120 case DW_FORM_data2:
121 case DW_FORM_data4:
122 case DW_FORM_data8:
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000123 case DW_FORM_data16:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000124 case DW_FORM_flag:
125 case DW_FORM_ref1:
126 case DW_FORM_ref2:
127 case DW_FORM_ref4:
128 case DW_FORM_ref8:
129 case DW_FORM_ref_sig8:
130 case DW_FORM_ref_sup4:
131 case DW_FORM_ref_sup8:
132 case DW_FORM_strx1:
133 case DW_FORM_strx2:
134 case DW_FORM_strx4:
135 case DW_FORM_addrx1:
136 case DW_FORM_addrx2:
137 case DW_FORM_addrx4:
138 case DW_FORM_sec_offset:
139 case DW_FORM_strp:
140 case DW_FORM_strp_sup:
141 case DW_FORM_line_strp:
142 case DW_FORM_GNU_ref_alt:
143 case DW_FORM_GNU_strp_alt:
Paul Robinson75c068c2017-06-26 18:43:01 +0000144 if (Optional<uint8_t> FixedSize =
Pavel Labath322711f2018-03-14 09:39:54 +0000145 dwarf::getFixedFormByteSize(Form, Params)) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000146 *OffsetPtr += *FixedSize;
Greg Clayton82f12b12016-11-11 16:21:37 +0000147 return true;
148 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000149 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000150
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000151 // signed or unsigned LEB 128 values.
152 case DW_FORM_sdata:
153 DebugInfoData.getSLEB128(OffsetPtr);
154 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000155
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000156 case DW_FORM_udata:
157 case DW_FORM_ref_udata:
158 case DW_FORM_strx:
159 case DW_FORM_addrx:
160 case DW_FORM_loclistx:
161 case DW_FORM_rnglistx:
162 case DW_FORM_GNU_addr_index:
163 case DW_FORM_GNU_str_index:
164 DebugInfoData.getULEB128(OffsetPtr);
165 return true;
Greg Clayton82f12b12016-11-11 16:21:37 +0000166
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000167 case DW_FORM_indirect:
168 Indirect = true;
169 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
170 break;
Greg Clayton82f12b12016-11-11 16:21:37 +0000171
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000172 default:
173 return false;
Greg Clayton82f12b12016-11-11 16:21:37 +0000174 }
175 } while (Indirect);
176 return true;
177}
178
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000179bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
Paul Robinson01158442018-01-25 23:06:36 +0000180 // First, check DWARF5 form classes.
181 if (Form < makeArrayRef(DWARF5FormClasses).size() &&
182 DWARF5FormClasses[Form] == FC)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000183 return true;
Paul Robinson01158442018-01-25 23:06:36 +0000184 // Check more forms from extensions and proposals.
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000185 switch (Form) {
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000186 case DW_FORM_GNU_ref_alt:
Alexey Samsonovcbd806a2013-10-29 16:32:19 +0000187 return (FC == FC_Reference);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000188 case DW_FORM_GNU_addr_index:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000189 return (FC == FC_Address);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000190 case DW_FORM_GNU_str_index:
191 case DW_FORM_GNU_strp_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000192 return (FC == FC_String);
Greg Clayton6c273762016-10-27 16:32:04 +0000193 default:
194 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000195 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000196 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset.
197 // Don't check for DWARF version here, as some producers may still do this
Paul Robinson0a227092018-02-05 20:43:15 +0000198 // by mistake. Also accept DW_FORM_[line_]strp since these are
199 // .debug_[line_]str section offsets.
Greg Clayton48432cf2017-05-01 22:07:02 +0000200 return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
Paul Robinson0a227092018-02-05 20:43:15 +0000201 Form == DW_FORM_strp || Form == DW_FORM_line_strp) &&
Benjamin Kramer68a29562015-05-25 13:28:03 +0000202 FC == FC_SectionOffset;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000203}
204
Paul Robinson17536b92017-06-29 16:52:08 +0000205bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data,
Pavel Labath322711f2018-03-14 09:39:54 +0000206 uint32_t *OffsetPtr, dwarf::FormParams FP,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000207 const DWARFContext *Ctx,
Paul Robinsone5400f82017-11-07 19:57:12 +0000208 const DWARFUnit *CU) {
Paul Robinsonbf750c82018-01-29 20:57:43 +0000209 if (!Ctx && CU)
210 Ctx = &CU->getContext();
211 C = Ctx;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000212 U = CU;
213 bool Indirect = false;
214 bool IsBlock = false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000215 Value.data = nullptr;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000216 // Read the value for the form into value and follow and DW_FORM_indirect
217 // instances we run into
218 do {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000219 Indirect = false;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000220 switch (Form) {
221 case DW_FORM_addr:
Eric Christopher7c678de2012-11-07 23:22:07 +0000222 case DW_FORM_ref_addr: {
Paul Robinsone5400f82017-11-07 19:57:12 +0000223 uint16_t Size =
224 (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
Paul Robinson17536b92017-06-29 16:52:08 +0000225 Value.uval = Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000226 break;
Alexey Samsonov9cb13d52012-11-12 14:25:36 +0000227 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000228 case DW_FORM_exprloc:
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000229 case DW_FORM_block:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000230 Value.uval = Data.getULEB128(OffsetPtr);
231 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000232 break;
233 case DW_FORM_block1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000234 Value.uval = Data.getU8(OffsetPtr);
235 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000236 break;
237 case DW_FORM_block2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000238 Value.uval = Data.getU16(OffsetPtr);
239 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000240 break;
241 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000242 Value.uval = Data.getU32(OffsetPtr);
243 IsBlock = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000244 break;
245 case DW_FORM_data1:
246 case DW_FORM_ref1:
247 case DW_FORM_flag:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000248 case DW_FORM_strx1:
249 case DW_FORM_addrx1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000250 Value.uval = Data.getU8(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000251 break;
252 case DW_FORM_data2:
253 case DW_FORM_ref2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000254 case DW_FORM_strx2:
255 case DW_FORM_addrx2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000256 Value.uval = Data.getU16(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000257 break;
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000258 case DW_FORM_strx3:
259 Value.uval = Data.getU24(OffsetPtr);
260 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000261 case DW_FORM_data4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000262 case DW_FORM_ref4:
263 case DW_FORM_ref_sup4:
264 case DW_FORM_strx4:
Paul Robinson17536b92017-06-29 16:52:08 +0000265 case DW_FORM_addrx4:
266 Value.uval = Data.getRelocatedValue(4, OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000267 break;
268 case DW_FORM_data8:
269 case DW_FORM_ref8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000270 case DW_FORM_ref_sup8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000271 Value.uval = Data.getU64(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000272 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000273 case DW_FORM_data16:
274 // Treat this like a 16-byte block.
275 Value.uval = 16;
276 IsBlock = true;
277 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000278 case DW_FORM_sdata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000279 Value.sval = Data.getSLEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000280 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000281 case DW_FORM_udata:
282 case DW_FORM_ref_udata:
Wolfgang Piebad605592018-05-18 20:12:54 +0000283 case DW_FORM_rnglistx:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000284 Value.uval = Data.getULEB128(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000285 break;
286 case DW_FORM_string:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000287 Value.cstr = Data.getCStr(OffsetPtr);
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000288 break;
289 case DW_FORM_indirect:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000290 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr));
291 Indirect = true;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000292 break;
Greg Clayton04c19282016-11-11 17:38:14 +0000293 case DW_FORM_strp:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000294 case DW_FORM_sec_offset:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000295 case DW_FORM_GNU_ref_alt:
Greg Clayton82f12b12016-11-11 16:21:37 +0000296 case DW_FORM_GNU_strp_alt:
297 case DW_FORM_line_strp:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000298 case DW_FORM_strp_sup: {
Paul Robinson17536b92017-06-29 16:52:08 +0000299 Value.uval =
Paul Robinsone5400f82017-11-07 19:57:12 +0000300 Data.getRelocatedValue(FP.getDwarfOffsetByteSize(), OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000301 break;
Eric Christopher55863be2013-04-07 03:43:09 +0000302 }
Eric Christopherd999bb72012-08-24 01:14:23 +0000303 case DW_FORM_flag_present:
304 Value.uval = 1;
305 break;
306 case DW_FORM_ref_sig8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000307 Value.uval = Data.getU64(OffsetPtr);
Eric Christopherd999bb72012-08-24 01:14:23 +0000308 break;
Eric Christopher59c53c22012-11-16 23:44:11 +0000309 case DW_FORM_GNU_addr_index:
Eric Christopher59c53c22012-11-16 23:44:11 +0000310 case DW_FORM_GNU_str_index:
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000311 case DW_FORM_strx:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000312 Value.uval = Data.getULEB128(OffsetPtr);
Eric Christopher59c53c22012-11-16 23:44:11 +0000313 break;
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000314 default:
Greg Clayton0e62ee72017-01-13 00:13:42 +0000315 // DWARFFormValue::skipValue() will have caught this and caused all
316 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
317 llvm_unreachable("unsupported form");
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000318 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000319 } while (Indirect);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000320
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000321 if (IsBlock) {
322 StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval);
Craig Topper2617dcc2014-04-15 06:32:26 +0000323 Value.data = nullptr;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000324 if (!Str.empty()) {
325 Value.data = reinterpret_cast<const uint8_t *>(Str.data());
326 *OffsetPtr += Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000327 }
328 }
329
330 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000331}
332
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000333void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000334 uint64_t UValue = Value.uval;
335 bool CURelativeOffset = false;
Jonas Devlieghere69217532018-03-09 09:56:24 +0000336 raw_ostream &AddrOS = DumpOpts.ShowAddresses
337 ? WithColor(OS, HighlightColor::Address).get()
338 : nulls();
Benjamin Kramereaa74332011-09-13 21:47:32 +0000339 switch (Form) {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000340 case DW_FORM_addr:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000341 AddrOS << format("0x%016" PRIx64, UValue);
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000342 break;
Eric Christopher962c9082013-01-15 23:56:56 +0000343 case DW_FORM_GNU_addr_index: {
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000344 AddrOS << format(" indexed (%8.8x) address = ", (uint32_t)UValue);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000345 uint64_t Address;
Greg Claytoncddab272016-10-31 16:46:02 +0000346 if (U == nullptr)
347 OS << "<invalid dwarf unit>";
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000348 else if (U->getAddrOffsetSectionItem(UValue, Address))
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000349 AddrOS << format("0x%016" PRIx64, Address);
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000350 else
Eric Christopher962c9082013-01-15 23:56:56 +0000351 OS << "<no .debug_addr section>";
352 break;
353 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000354 case DW_FORM_flag_present:
355 OS << "true";
356 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000357 case DW_FORM_flag:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000358 case DW_FORM_data1:
359 OS << format("0x%02x", (uint8_t)UValue);
360 break;
361 case DW_FORM_data2:
362 OS << format("0x%04x", (uint16_t)UValue);
363 break;
364 case DW_FORM_data4:
365 OS << format("0x%08x", (uint32_t)UValue);
366 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000367 case DW_FORM_ref_sig8:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000368 AddrOS << format("0x%016" PRIx64, UValue);
369 break;
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000370 case DW_FORM_data8:
371 OS << format("0x%016" PRIx64, UValue);
372 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000373 case DW_FORM_data16:
374 OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
375 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000376 case DW_FORM_string:
377 OS << '"';
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000378 OS.write_escaped(Value.cstr);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000379 OS << '"';
380 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000381 case DW_FORM_exprloc:
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000382 case DW_FORM_block:
383 case DW_FORM_block1:
384 case DW_FORM_block2:
385 case DW_FORM_block4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000386 if (UValue > 0) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000387 switch (Form) {
Eric Christopherd999bb72012-08-24 01:14:23 +0000388 case DW_FORM_exprloc:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000389 case DW_FORM_block:
390 OS << format("<0x%" PRIx64 "> ", UValue);
391 break;
392 case DW_FORM_block1:
393 OS << format("<0x%2.2x> ", (uint8_t)UValue);
394 break;
395 case DW_FORM_block2:
396 OS << format("<0x%4.4x> ", (uint16_t)UValue);
397 break;
398 case DW_FORM_block4:
399 OS << format("<0x%8.8x> ", (uint32_t)UValue);
400 break;
401 default:
402 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000403 }
404
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000405 const uint8_t *DataPtr = Value.data;
406 if (DataPtr) {
407 // UValue contains size of block
408 const uint8_t *EndDataPtr = DataPtr + UValue;
409 while (DataPtr < EndDataPtr) {
410 OS << format("%2.2x ", *DataPtr);
411 ++DataPtr;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000412 }
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000413 } else
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000414 OS << "NULL";
415 }
416 break;
417
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000418 case DW_FORM_sdata:
419 OS << Value.sval;
420 break;
421 case DW_FORM_udata:
422 OS << Value.uval;
423 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000424 case DW_FORM_strp:
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000425 if (DumpOpts.Verbose)
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000426 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000427 dumpString(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000428 break;
Paul Robinson0a227092018-02-05 20:43:15 +0000429 case DW_FORM_line_strp:
430 if (DumpOpts.Verbose)
431 OS << format(" .debug_line_str[0x%8.8x] = ", (uint32_t)UValue);
432 dumpString(OS);
433 break;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000434 case DW_FORM_strx:
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000435 case DW_FORM_strx1:
436 case DW_FORM_strx2:
437 case DW_FORM_strx3:
438 case DW_FORM_strx4:
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000439 case DW_FORM_GNU_str_index:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000440 if (DumpOpts.Verbose)
441 OS << format(" indexed (%8.8x) string = ", (uint32_t)UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000442 dumpString(OS);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000443 break;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +0000444 case DW_FORM_GNU_strp_alt:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000445 if (DumpOpts.Verbose)
446 OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue);
Greg Claytoncddab272016-10-31 16:46:02 +0000447 dumpString(OS);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000448 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000449 case DW_FORM_ref_addr:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000450 AddrOS << format("0x%016" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000451 break;
452 case DW_FORM_ref1:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000453 CURelativeOffset = true;
Jonas Devliegherebf8596f2018-03-07 16:28:53 +0000454 if (DumpOpts.Verbose)
455 AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000456 break;
457 case DW_FORM_ref2:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000458 CURelativeOffset = true;
Jonas Devliegherebf8596f2018-03-07 16:28:53 +0000459 if (DumpOpts.Verbose)
460 AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000461 break;
462 case DW_FORM_ref4:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000463 CURelativeOffset = true;
Jonas Devliegherebf8596f2018-03-07 16:28:53 +0000464 if (DumpOpts.Verbose)
465 AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000466 break;
467 case DW_FORM_ref8:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000468 CURelativeOffset = true;
Jonas Devliegherebf8596f2018-03-07 16:28:53 +0000469 if (DumpOpts.Verbose)
470 AddrOS << format("cu + 0x%8.8" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000471 break;
472 case DW_FORM_ref_udata:
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000473 CURelativeOffset = true;
Jonas Devliegherebf8596f2018-03-07 16:28:53 +0000474 if (DumpOpts.Verbose)
475 AddrOS << format("cu + 0x%" PRIx64, UValue);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000476 break;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000477 case DW_FORM_GNU_ref_alt:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000478 AddrOS << format("<alt 0x%" PRIx64 ">", UValue);
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000479 break;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000480
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000481 // All DW_FORM_indirect attributes should be resolved prior to calling
482 // this function
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000483 case DW_FORM_indirect:
484 OS << "DW_FORM_indirect";
485 break;
Eric Christopherd999bb72012-08-24 01:14:23 +0000486
Wolfgang Piebad605592018-05-18 20:12:54 +0000487 case DW_FORM_rnglistx:
488 OS << format("indexed (0x%x) rangelist = ", (uint32_t)UValue);
489 break;
490
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000491 // Should be formatted to 64-bit for DWARF64.
Eric Christopherd999bb72012-08-24 01:14:23 +0000492 case DW_FORM_sec_offset:
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000493 AddrOS << format("0x%08x", (uint32_t)UValue);
Eric Christopherd999bb72012-08-24 01:14:23 +0000494 break;
Eric Christopherb2120fd2013-01-07 22:40:48 +0000495
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000496 default:
497 OS << format("DW_FORM(0x%4.4x)", Form);
498 break;
499 }
500
Jonas Devliegherebf8596f2018-03-07 16:28:53 +0000501 if (CURelativeOffset) {
502 if (DumpOpts.Verbose)
503 OS << " => {";
Jonas Devlieghere69217532018-03-09 09:56:24 +0000504 WithColor(OS, HighlightColor::Address).get()
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000505 << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
Jonas Devliegherebf8596f2018-03-07 16:28:53 +0000506 if (DumpOpts.Verbose)
507 OS << "}";
Adrian Prantl0c36a752015-01-06 16:50:25 +0000508 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000509}
510
Greg Claytoncddab272016-10-31 16:46:02 +0000511void DWARFFormValue::dumpString(raw_ostream &OS) const {
512 Optional<const char *> DbgStr = getAsCString();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000513 if (DbgStr.hasValue()) {
Jonas Devlieghere69217532018-03-09 09:56:24 +0000514 auto COS = WithColor(OS, HighlightColor::String);
Adrian Prantl520789e2018-02-12 21:11:23 +0000515 COS.get() << '"';
516 COS.get().write_escaped(DbgStr.getValue());
517 COS.get() << '"';
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000518 }
519}
520
Greg Claytoncddab272016-10-31 16:46:02 +0000521Optional<const char *> DWARFFormValue::getAsCString() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000522 if (!isFormClass(FC_String))
523 return None;
524 if (Form == DW_FORM_string)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000525 return Value.cstr;
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000526 // FIXME: Add support for DW_FORM_GNU_strp_alt
Paul Robinsonbf750c82018-01-29 20:57:43 +0000527 if (Form == DW_FORM_GNU_strp_alt || C == nullptr)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000528 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000529 uint32_t Offset = Value.uval;
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000530 if (Form == DW_FORM_line_strp) {
Paul Robinsonbf750c82018-01-29 20:57:43 +0000531 // .debug_line_str is tracked in the Context.
532 if (const char *Str = C->getLineStringExtractor().getCStr(&Offset))
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000533 return Str;
534 return None;
535 }
Wolfgang Pieb258927e2017-06-21 19:37:44 +0000536 if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
537 Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
538 Form == DW_FORM_strx4) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000539 uint64_t StrOffset;
Paul Robinsonbf750c82018-01-29 20:57:43 +0000540 if (!U || !U->getStringOffsetSectionItem(Offset, StrOffset))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000541 return None;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000542 Offset = StrOffset;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000543 }
Paul Robinsonbf750c82018-01-29 20:57:43 +0000544 // Prefer the Unit's string extractor, because for .dwo it will point to
545 // .debug_str.dwo, while the Context's extractor always uses .debug_str.
546 if (U) {
547 if (const char *Str = U->getStringExtractor().getCStr(&Offset))
548 return Str;
549 return None;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000550 }
Paul Robinsonbf750c82018-01-29 20:57:43 +0000551 if (const char *Str = C->getStringExtractor().getCStr(&Offset))
552 return Str;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000553 return None;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000554}
555
Greg Claytoncddab272016-10-31 16:46:02 +0000556Optional<uint64_t> DWARFFormValue::getAsAddress() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000557 if (!isFormClass(FC_Address))
558 return None;
Alexey Samsonov742e6b82013-10-18 07:13:32 +0000559 if (Form == DW_FORM_GNU_addr_index) {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000560 uint32_t Index = Value.uval;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000561 uint64_t Result;
Craig Topper2617dcc2014-04-15 06:32:26 +0000562 if (!U || !U->getAddrOffsetSectionItem(Index, Result))
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000563 return None;
564 return Result;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000565 }
566 return Value.uval;
Eric Christopher962c9082013-01-15 23:56:56 +0000567}
568
Greg Claytoncddab272016-10-31 16:46:02 +0000569Optional<uint64_t> DWARFFormValue::getAsReference() const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000570 if (!isFormClass(FC_Reference))
571 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000572 switch (Form) {
573 case DW_FORM_ref1:
574 case DW_FORM_ref2:
575 case DW_FORM_ref4:
576 case DW_FORM_ref8:
577 case DW_FORM_ref_udata:
Craig Topper2617dcc2014-04-15 06:32:26 +0000578 if (!U)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000579 return None;
580 return Value.uval + U->getOffset();
581 case DW_FORM_ref_addr:
Greg Clayton82f12b12016-11-11 16:21:37 +0000582 case DW_FORM_ref_sig8:
583 case DW_FORM_GNU_ref_alt:
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000584 return Value.uval;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000585 default:
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000586 return None;
Benjamin Kramereaa74332011-09-13 21:47:32 +0000587 }
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000588}
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000589
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000590Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
591 if (!isFormClass(FC_SectionOffset))
592 return None;
593 return Value.uval;
594}
595
596Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000597 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
598 Form == DW_FORM_sdata)
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000599 return None;
600 return Value.uval;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000601}
Frederic Rissf3549a22014-09-04 06:14:35 +0000602
Frederic Riss77f850e2015-03-04 22:07:41 +0000603Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
604 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
Paul Robinsonae2e6f372017-05-03 21:53:21 +0000605 (Form == DW_FORM_udata &&
606 uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
Frederic Riss77f850e2015-03-04 22:07:41 +0000607 return None;
608 switch (Form) {
609 case DW_FORM_data4:
610 return int32_t(Value.uval);
611 case DW_FORM_data2:
612 return int16_t(Value.uval);
613 case DW_FORM_data1:
614 return int8_t(Value.uval);
615 case DW_FORM_sdata:
616 case DW_FORM_data8:
617 default:
618 return Value.sval;
619 }
620}
621
Frederic Risseab17772014-09-04 06:35:09 +0000622Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000623 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
624 Form != DW_FORM_data16)
Frederic Rissf3549a22014-09-04 06:14:35 +0000625 return None;
Craig Topper0013be12015-09-21 05:32:41 +0000626 return makeArrayRef(Value.data, Value.uval);
Frederic Rissf3549a22014-09-04 06:14:35 +0000627}
628
Chris Bienemane0e451d2016-12-22 22:44:27 +0000629Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
630 if (!isFormClass(FC_String) && Form == DW_FORM_string)
631 return None;
632 return Value.uval;
633}
634
635Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
636 if (!isFormClass(FC_Reference))
637 return None;
638 return Value.uval;
639}