Bug Summary

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