Bug Summary

File:lib/DebugInfo/DWARF/DWARFFormValue.cpp
Warning:line 182, column 31
The left operand of '==' is a garbage value due to array index out of bounds

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name DWARFFormValue.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-8/lib/clang/8.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/lib/DebugInfo/DWARF -I /build/llvm-toolchain-snapshot-8~svn345461/lib/DebugInfo/DWARF -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn345461/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/lib/DebugInfo/DWARF -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-10-27-211344-32123-1 -x c++ /build/llvm-toolchain-snapshot-8~svn345461/lib/DebugInfo/DWARF/DWARFFormValue.cpp -faddrsig
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
10#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
11#include "llvm/ADT/ArrayRef.h"
12#include "llvm/ADT/None.h"
13#include "llvm/ADT/Optional.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/BinaryFormat/Dwarf.h"
16#include "llvm/DebugInfo/DWARF/DWARFContext.h"
17#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
18#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
19#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/Format.h"
21#include "llvm/Support/WithColor.h"
22#include "llvm/Support/raw_ostream.h"
23#include <cinttypes>
24#include <cstdint>
25#include <limits>
26
27using namespace llvm;
28using namespace dwarf;
29
30static const DWARFFormValue::FormClass DWARF5FormClasses[] = {
31 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
59 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
79};
80
81bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
82 uint32_t *OffsetPtr,
83 const dwarf::FormParams Params) {
84 bool Indirect = false;
85 do {
86 switch (Form) {
87 // 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:
123 case DW_FORM_data16:
124 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:
144 if (Optional<uint8_t> FixedSize =
145 dwarf::getFixedFormByteSize(Form, Params)) {
146 *OffsetPtr += *FixedSize;
147 return true;
148 }
149 return false;
150
151 // signed or unsigned LEB 128 values.
152 case DW_FORM_sdata:
153 DebugInfoData.getSLEB128(OffsetPtr);
154 return true;
155
156 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;
166
167 case DW_FORM_indirect:
168 Indirect = true;
169 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr));
170 break;
171
172 default:
173 return false;
174 }
175 } while (Indirect);
176 return true;
177}
178
179bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const {
180 // First, check DWARF5 form classes.
181 if (Form < makeArrayRef(DWARF5FormClasses).size() &&
9
Assuming the condition is true
182 DWARF5FormClasses[Form] == FC)
10
The left operand of '==' is a garbage value due to array index out of bounds
183 return true;
184 // Check more forms from extensions and proposals.
185 switch (Form) {
186 case DW_FORM_GNU_ref_alt:
187 return (FC == FC_Reference);
188 case DW_FORM_GNU_addr_index:
189 return (FC == FC_Address);
190 case DW_FORM_GNU_str_index:
191 case DW_FORM_GNU_strp_alt:
192 return (FC == FC_String);
193 default:
194 break;
195 }
196 // 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
198 // by mistake. Also accept DW_FORM_[line_]strp since these are
199 // .debug_[line_]str section offsets.
200 return (Form == DW_FORM_data4 || Form == DW_FORM_data8 ||
201 Form == DW_FORM_strp || Form == DW_FORM_line_strp) &&
202 FC == FC_SectionOffset;
203}
204
205bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data,
206 uint32_t *OffsetPtr, dwarf::FormParams FP,
207 const DWARFContext *Ctx,
208 const DWARFUnit *CU) {
209 if (!Ctx && CU)
210 Ctx = &CU->getContext();
211 C = Ctx;
212 U = CU;
213 bool Indirect = false;
214 bool IsBlock = false;
215 Value.data = nullptr;
216 // Read the value for the form into value and follow and DW_FORM_indirect
217 // instances we run into
218 do {
219 Indirect = false;
220 switch (Form) {
221 case DW_FORM_addr:
222 case DW_FORM_ref_addr: {
223 uint16_t Size =
224 (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
225 Value.uval = Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex);
226 break;
227 }
228 case DW_FORM_exprloc:
229 case DW_FORM_block:
230 Value.uval = Data.getULEB128(OffsetPtr);
231 IsBlock = true;
232 break;
233 case DW_FORM_block1:
234 Value.uval = Data.getU8(OffsetPtr);
235 IsBlock = true;
236 break;
237 case DW_FORM_block2:
238 Value.uval = Data.getU16(OffsetPtr);
239 IsBlock = true;
240 break;
241 case DW_FORM_block4:
242 Value.uval = Data.getU32(OffsetPtr);
243 IsBlock = true;
244 break;
245 case DW_FORM_data1:
246 case DW_FORM_ref1:
247 case DW_FORM_flag:
248 case DW_FORM_strx1:
249 case DW_FORM_addrx1:
250 Value.uval = Data.getU8(OffsetPtr);
251 break;
252 case DW_FORM_data2:
253 case DW_FORM_ref2:
254 case DW_FORM_strx2:
255 case DW_FORM_addrx2:
256 Value.uval = Data.getU16(OffsetPtr);
257 break;
258 case DW_FORM_strx3:
259 Value.uval = Data.getU24(OffsetPtr);
260 break;
261 case DW_FORM_data4:
262 case DW_FORM_ref4:
263 case DW_FORM_ref_sup4:
264 case DW_FORM_strx4:
265 case DW_FORM_addrx4:
266 Value.uval = Data.getRelocatedValue(4, OffsetPtr);
267 break;
268 case DW_FORM_data8:
269 case DW_FORM_ref8:
270 case DW_FORM_ref_sup8:
271 Value.uval = Data.getU64(OffsetPtr);
272 break;
273 case DW_FORM_data16:
274 // Treat this like a 16-byte block.
275 Value.uval = 16;
276 IsBlock = true;
277 break;
278 case DW_FORM_sdata:
279 Value.sval = Data.getSLEB128(OffsetPtr);
280 break;
281 case DW_FORM_udata:
282 case DW_FORM_ref_udata:
283 case DW_FORM_rnglistx:
284 Value.uval = Data.getULEB128(OffsetPtr);
285 break;
286 case DW_FORM_string:
287 Value.cstr = Data.getCStr(OffsetPtr);
288 break;
289 case DW_FORM_indirect:
290 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr));
291 Indirect = true;
292 break;
293 case DW_FORM_strp:
294 case DW_FORM_sec_offset:
295 case DW_FORM_GNU_ref_alt:
296 case DW_FORM_GNU_strp_alt:
297 case DW_FORM_line_strp:
298 case DW_FORM_strp_sup: {
299 Value.uval =
300 Data.getRelocatedValue(FP.getDwarfOffsetByteSize(), OffsetPtr);
301 break;
302 }
303 case DW_FORM_flag_present:
304 Value.uval = 1;
305 break;
306 case DW_FORM_ref_sig8:
307 Value.uval = Data.getU64(OffsetPtr);
308 break;
309 case DW_FORM_GNU_addr_index:
310 case DW_FORM_GNU_str_index:
311 case DW_FORM_addrx:
312 case DW_FORM_strx:
313 Value.uval = Data.getULEB128(OffsetPtr);
314 break;
315 default:
316 // DWARFFormValue::skipValue() will have caught this and caused all
317 // DWARF DIEs to fail to be parsed, so this code is not be reachable.
318 llvm_unreachable("unsupported form")::llvm::llvm_unreachable_internal("unsupported form", "/build/llvm-toolchain-snapshot-8~svn345461/lib/DebugInfo/DWARF/DWARFFormValue.cpp"
, 318)
;
319 }
320 } while (Indirect);
321
322 if (IsBlock) {
323 StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval);
324 Value.data = nullptr;
325 if (!Str.empty()) {
326 Value.data = reinterpret_cast<const uint8_t *>(Str.data());
327 *OffsetPtr += Value.uval;
328 }
329 }
330
331 return true;
332}
333
334void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
335 uint64_t UValue = Value.uval;
336 bool CURelativeOffset = false;
337 raw_ostream &AddrOS = DumpOpts.ShowAddresses
1
Assuming the condition is false
2
'?' condition is false
338 ? WithColor(OS, HighlightColor::Address).get()
339 : nulls();
340 switch (Form) {
3
Control jumps to 'case DW_FORM_GNU_strp_alt:' at line 449
341 case DW_FORM_addr:
342 AddrOS << format("0x%016" PRIx64"l" "x", UValue);
343 break;
344 case DW_FORM_addrx:
345 case DW_FORM_addrx1:
346 case DW_FORM_addrx2:
347 case DW_FORM_addrx3:
348 case DW_FORM_addrx4:
349 case DW_FORM_GNU_addr_index: {
350 AddrOS << format(" indexed (%8.8x) address = ", (uint32_t)UValue);
351 if (U == nullptr)
352 OS << "<invalid dwarf unit>";
353 else if (Optional<SectionedAddress> A = U->getAddrOffsetSectionItem(UValue))
354 AddrOS << format("0x%016" PRIx64"l" "x", A->Address);
355 else
356 OS << "<no .debug_addr section>";
357 break;
358 }
359 case DW_FORM_flag_present:
360 OS << "true";
361 break;
362 case DW_FORM_flag:
363 case DW_FORM_data1:
364 OS << format("0x%02x", (uint8_t)UValue);
365 break;
366 case DW_FORM_data2:
367 OS << format("0x%04x", (uint16_t)UValue);
368 break;
369 case DW_FORM_data4:
370 OS << format("0x%08x", (uint32_t)UValue);
371 break;
372 case DW_FORM_ref_sig8:
373 AddrOS << format("0x%016" PRIx64"l" "x", UValue);
374 break;
375 case DW_FORM_data8:
376 OS << format("0x%016" PRIx64"l" "x", UValue);
377 break;
378 case DW_FORM_data16:
379 OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16);
380 break;
381 case DW_FORM_string:
382 OS << '"';
383 OS.write_escaped(Value.cstr);
384 OS << '"';
385 break;
386 case DW_FORM_exprloc:
387 case DW_FORM_block:
388 case DW_FORM_block1:
389 case DW_FORM_block2:
390 case DW_FORM_block4:
391 if (UValue > 0) {
392 switch (Form) {
393 case DW_FORM_exprloc:
394 case DW_FORM_block:
395 AddrOS << format("<0x%" PRIx64"l" "x" "> ", UValue);
396 break;
397 case DW_FORM_block1:
398 AddrOS << format("<0x%2.2x> ", (uint8_t)UValue);
399 break;
400 case DW_FORM_block2:
401 AddrOS << format("<0x%4.4x> ", (uint16_t)UValue);
402 break;
403 case DW_FORM_block4:
404 AddrOS << format("<0x%8.8x> ", (uint32_t)UValue);
405 break;
406 default:
407 break;
408 }
409
410 const uint8_t *DataPtr = Value.data;
411 if (DataPtr) {
412 // UValue contains size of block
413 const uint8_t *EndDataPtr = DataPtr + UValue;
414 while (DataPtr < EndDataPtr) {
415 AddrOS << format("%2.2x ", *DataPtr);
416 ++DataPtr;
417 }
418 } else
419 OS << "NULL";
420 }
421 break;
422
423 case DW_FORM_sdata:
424 OS << Value.sval;
425 break;
426 case DW_FORM_udata:
427 OS << Value.uval;
428 break;
429 case DW_FORM_strp:
430 if (DumpOpts.Verbose)
431 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue);
432 dumpString(OS);
433 break;
434 case DW_FORM_line_strp:
435 if (DumpOpts.Verbose)
436 OS << format(" .debug_line_str[0x%8.8x] = ", (uint32_t)UValue);
437 dumpString(OS);
438 break;
439 case DW_FORM_strx:
440 case DW_FORM_strx1:
441 case DW_FORM_strx2:
442 case DW_FORM_strx3:
443 case DW_FORM_strx4:
444 case DW_FORM_GNU_str_index:
445 if (DumpOpts.Verbose)
446 OS << format(" indexed (%8.8x) string = ", (uint32_t)UValue);
447 dumpString(OS);
448 break;
449 case DW_FORM_GNU_strp_alt:
450 if (DumpOpts.Verbose)
4
Assuming the condition is false
5
Taking false branch
451 OS << format("alt indirect string, offset: 0x%" PRIx64"l" "x" "", UValue);
452 dumpString(OS);
6
Calling 'DWARFFormValue::dumpString'
453 break;
454 case DW_FORM_ref_addr:
455 AddrOS << format("0x%016" PRIx64"l" "x", UValue);
456 break;
457 case DW_FORM_ref1:
458 CURelativeOffset = true;
459 if (DumpOpts.Verbose)
460 AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue);
461 break;
462 case DW_FORM_ref2:
463 CURelativeOffset = true;
464 if (DumpOpts.Verbose)
465 AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue);
466 break;
467 case DW_FORM_ref4:
468 CURelativeOffset = true;
469 if (DumpOpts.Verbose)
470 AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue);
471 break;
472 case DW_FORM_ref8:
473 CURelativeOffset = true;
474 if (DumpOpts.Verbose)
475 AddrOS << format("cu + 0x%8.8" PRIx64"l" "x", UValue);
476 break;
477 case DW_FORM_ref_udata:
478 CURelativeOffset = true;
479 if (DumpOpts.Verbose)
480 AddrOS << format("cu + 0x%" PRIx64"l" "x", UValue);
481 break;
482 case DW_FORM_GNU_ref_alt:
483 AddrOS << format("<alt 0x%" PRIx64"l" "x" ">", UValue);
484 break;
485
486 // All DW_FORM_indirect attributes should be resolved prior to calling
487 // this function
488 case DW_FORM_indirect:
489 OS << "DW_FORM_indirect";
490 break;
491
492 case DW_FORM_rnglistx:
493 OS << format("indexed (0x%x) rangelist = ", (uint32_t)UValue);
494 break;
495
496 // Should be formatted to 64-bit for DWARF64.
497 case DW_FORM_sec_offset:
498 AddrOS << format("0x%08x", (uint32_t)UValue);
499 break;
500
501 default:
502 OS << format("DW_FORM(0x%4.4x)", Form);
503 break;
504 }
505
506 if (CURelativeOffset) {
507 if (DumpOpts.Verbose)
508 OS << " => {";
509 if (DumpOpts.ShowAddresses)
510 WithColor(OS, HighlightColor::Address).get()
511 << format("0x%8.8" PRIx64"l" "x", UValue + (U ? U->getOffset() : 0));
512 if (DumpOpts.Verbose)
513 OS << "}";
514 }
515}
516
517void DWARFFormValue::dumpString(raw_ostream &OS) const {
518 Optional<const char *> DbgStr = getAsCString();
7
Calling 'DWARFFormValue::getAsCString'
519 if (DbgStr.hasValue()) {
520 auto COS = WithColor(OS, HighlightColor::String);
521 COS.get() << '"';
522 COS.get().write_escaped(DbgStr.getValue());
523 COS.get() << '"';
524 }
525}
526
527Optional<const char *> DWARFFormValue::getAsCString() const {
528 if (!isFormClass(FC_String))
8
Calling 'DWARFFormValue::isFormClass'
529 return None;
530 if (Form == DW_FORM_string)
531 return Value.cstr;
532 // FIXME: Add support for DW_FORM_GNU_strp_alt
533 if (Form == DW_FORM_GNU_strp_alt || C == nullptr)
534 return None;
535 uint32_t Offset = Value.uval;
536 if (Form == DW_FORM_line_strp) {
537 // .debug_line_str is tracked in the Context.
538 if (const char *Str = C->getLineStringExtractor().getCStr(&Offset))
539 return Str;
540 return None;
541 }
542 if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx ||
543 Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 ||
544 Form == DW_FORM_strx4) {
545 uint64_t StrOffset;
546 if (!U || !U->getStringOffsetSectionItem(Offset, StrOffset))
547 return None;
548 Offset = StrOffset;
549 }
550 // Prefer the Unit's string extractor, because for .dwo it will point to
551 // .debug_str.dwo, while the Context's extractor always uses .debug_str.
552 if (U) {
553 if (const char *Str = U->getStringExtractor().getCStr(&Offset))
554 return Str;
555 return None;
556 }
557 if (const char *Str = C->getStringExtractor().getCStr(&Offset))
558 return Str;
559 return None;
560}
561
562Optional<uint64_t> DWARFFormValue::getAsAddress() const {
563 if (auto SA = getAsSectionedAddress())
564 return SA->Address;
565 return None;
566}
567Optional<SectionedAddress> DWARFFormValue::getAsSectionedAddress() const {
568 if (!isFormClass(FC_Address))
569 return None;
570 if (Form == DW_FORM_GNU_addr_index || Form == DW_FORM_addrx) {
571 uint32_t Index = Value.uval;
572 if (!U)
573 return None;
574 Optional<SectionedAddress> SA = U->getAddrOffsetSectionItem(Index);
575 if (!SA)
576 return None;
577 return SA;
578 }
579 return {{Value.uval, Value.SectionIndex}};
580}
581
582Optional<uint64_t> DWARFFormValue::getAsReference() const {
583 if (!isFormClass(FC_Reference))
584 return None;
585 switch (Form) {
586 case DW_FORM_ref1:
587 case DW_FORM_ref2:
588 case DW_FORM_ref4:
589 case DW_FORM_ref8:
590 case DW_FORM_ref_udata:
591 if (!U)
592 return None;
593 return Value.uval + U->getOffset();
594 case DW_FORM_ref_addr:
595 case DW_FORM_ref_sig8:
596 case DW_FORM_GNU_ref_alt:
597 return Value.uval;
598 default:
599 return None;
600 }
601}
602
603Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const {
604 if (!isFormClass(FC_SectionOffset))
605 return None;
606 return Value.uval;
607}
608
609Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const {
610 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
611 Form == DW_FORM_sdata)
612 return None;
613 return Value.uval;
614}
615
616Optional<int64_t> DWARFFormValue::getAsSignedConstant() const {
617 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) ||
618 (Form == DW_FORM_udata &&
619 uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval))
620 return None;
621 switch (Form) {
622 case DW_FORM_data4:
623 return int32_t(Value.uval);
624 case DW_FORM_data2:
625 return int16_t(Value.uval);
626 case DW_FORM_data1:
627 return int8_t(Value.uval);
628 case DW_FORM_sdata:
629 case DW_FORM_data8:
630 default:
631 return Value.sval;
632 }
633}
634
635Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const {
636 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) &&
637 Form != DW_FORM_data16)
638 return None;
639 return makeArrayRef(Value.data, Value.uval);
640}
641
642Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const {
643 if (!isFormClass(FC_String) && Form == DW_FORM_string)
644 return None;
645 return Value.uval;
646}
647
648Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const {
649 if (!isFormClass(FC_Reference))
650 return None;
651 return Value.uval;
652}