1 //===-- llvm/BinaryFormat/Dwarf.h ---Dwarf Constants-------------*- C++ -*-===//
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 /// \file
10 /// This file contains constants used for implementing Dwarf
11 /// debug support.
12 ///
13 /// For details on the Dwarf specfication see the latest DWARF Debugging
14 /// Information Format standard document on http://www.dwarfstd.org. This
15 /// file often includes support for non-released standard features.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_BINARYFORMAT_DWARF_H
20 #define LLVM_BINARYFORMAT_DWARF_H
21
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/FormatVariadicDetails.h"
27 #include "llvm/TargetParser/Triple.h"
28
29 #include <limits>
30
31 namespace llvm {
32 class StringRef;
33
34 namespace dwarf {
35
36 //===----------------------------------------------------------------------===//
37 // DWARF constants as gleaned from the DWARF Debugging Information Format V.5
38 // reference manual http://www.dwarfstd.org/.
39 //
40
41 // Do not mix the following two enumerations sets. DW_TAG_invalid changes the
42 // enumeration base type.
43
44 enum LLVMConstants : uint32_t {
45 /// LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def).
46 /// \{
47 DW_TAG_invalid = ~0U, ///< Tag for invalid results.
48 DW_VIRTUALITY_invalid = ~0U, ///< Virtuality for invalid results.
49 DW_MACINFO_invalid = ~0U, ///< Macinfo type for invalid results.
50 DW_APPLE_ENUM_KIND_invalid = ~0U, ///< Enum kind for invalid results.
51 /// \}
52
53 /// Special values for an initial length field.
54 /// \{
55 DW_LENGTH_lo_reserved = 0xfffffff0, ///< Lower bound of the reserved range.
56 DW_LENGTH_DWARF64 = 0xffffffff, ///< Indicator of 64-bit DWARF format.
57 DW_LENGTH_hi_reserved = 0xffffffff, ///< Upper bound of the reserved range.
58 /// \}
59
60 /// Other constants.
61 /// \{
62 DWARF_VERSION = 4, ///< Default dwarf version we output.
63 DW_PUBTYPES_VERSION = 2, ///< Section version number for .debug_pubtypes.
64 DW_PUBNAMES_VERSION = 2, ///< Section version number for .debug_pubnames.
65 DW_ARANGES_VERSION = 2, ///< Section version number for .debug_aranges.
66 /// \}
67
68 /// Identifiers we use to distinguish vendor extensions.
69 /// \{
70 DWARF_VENDOR_DWARF = 0, ///< Defined in v2 or later of the DWARF standard.
71 DWARF_VENDOR_APPLE = 1,
72 DWARF_VENDOR_BORLAND = 2,
73 DWARF_VENDOR_GNU = 3,
74 DWARF_VENDOR_GOOGLE = 4,
75 DWARF_VENDOR_LLVM = 5,
76 DWARF_VENDOR_MIPS = 6,
77 DWARF_VENDOR_WASM = 7,
78 DWARF_VENDOR_ALTIUM,
79 DWARF_VENDOR_COMPAQ,
80 DWARF_VENDOR_GHS,
81 DWARF_VENDOR_GO,
82 DWARF_VENDOR_HP,
83 DWARF_VENDOR_IBM,
84 DWARF_VENDOR_INTEL,
85 DWARF_VENDOR_PGI,
86 DWARF_VENDOR_SUN,
87 DWARF_VENDOR_UPC,
88 ///\}
89 };
90
91 /// Constants that define the DWARF format as 32 or 64 bit.
92 enum DwarfFormat : uint8_t { DWARF32, DWARF64 };
93
94 /// Special ID values that distinguish a CIE from a FDE in DWARF CFI.
95 /// Not inside an enum because a 64-bit value is needed.
96 /// @{
97 const uint32_t DW_CIE_ID = UINT32_MAX;
98 const uint64_t DW64_CIE_ID = UINT64_MAX;
99 /// @}
100
101 /// Identifier of an invalid DIE offset in the .debug_info section.
102 const uint32_t DW_INVALID_OFFSET = UINT32_MAX;
103
104 enum Tag : uint16_t {
105 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) DW_TAG_##NAME = ID,
106 #include "llvm/BinaryFormat/Dwarf.def"
107 DW_TAG_lo_user = 0x4080,
108 DW_TAG_hi_user = 0xffff,
109 DW_TAG_user_base = 0x1000 ///< Recommended base for user tags.
110 };
111
isType(Tag T)112 inline bool isType(Tag T) {
113 switch (T) {
114 default:
115 return false;
116 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) \
117 case DW_TAG_##NAME: \
118 return (KIND == DW_KIND_TYPE);
119 #include "llvm/BinaryFormat/Dwarf.def"
120 }
121 }
122
123 /// Attributes.
124 enum Attribute : uint16_t {
125 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) DW_AT_##NAME = ID,
126 #include "llvm/BinaryFormat/Dwarf.def"
127 DW_AT_lo_user = 0x2000,
128 DW_AT_hi_user = 0x3fff,
129 };
130
131 enum Form : uint16_t {
132 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) DW_FORM_##NAME = ID,
133 #include "llvm/BinaryFormat/Dwarf.def"
134 DW_FORM_lo_user = 0x1f00, ///< Not specified by DWARF.
135 };
136
137 enum LocationAtom {
138 #define HANDLE_DW_OP(ID, NAME, OPERANDS, ARITY, VERSION, VENDOR) \
139 DW_OP_##NAME = ID,
140 #include "llvm/BinaryFormat/Dwarf.def"
141 DW_OP_lo_user = 0xe0,
142 DW_OP_hi_user = 0xff,
143 DW_OP_LLVM_fragment = 0x1000, ///< Only used in LLVM metadata.
144 DW_OP_LLVM_convert = 0x1001, ///< Only used in LLVM metadata.
145 DW_OP_LLVM_tag_offset = 0x1002, ///< Only used in LLVM metadata.
146 DW_OP_LLVM_entry_value = 0x1003, ///< Only used in LLVM metadata.
147 DW_OP_LLVM_implicit_pointer = 0x1004, ///< Only used in LLVM metadata.
148 DW_OP_LLVM_arg = 0x1005, ///< Only used in LLVM metadata.
149 DW_OP_LLVM_extract_bits_sext = 0x1006, ///< Only used in LLVM metadata.
150 DW_OP_LLVM_extract_bits_zext = 0x1007, ///< Only used in LLVM metadata.
151 };
152
153 enum LlvmUserLocationAtom {
154 #define HANDLE_DW_OP_LLVM_USEROP(ID, NAME) DW_OP_LLVM_##NAME = ID,
155 #include "llvm/BinaryFormat/Dwarf.def"
156 };
157
158 enum TypeKind : uint8_t {
159 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) DW_ATE_##NAME = ID,
160 #include "llvm/BinaryFormat/Dwarf.def"
161 DW_ATE_lo_user = 0x80,
162 DW_ATE_hi_user = 0xff
163 };
164
165 enum DecimalSignEncoding {
166 // Decimal sign attribute values
167 DW_DS_unsigned = 0x01,
168 DW_DS_leading_overpunch = 0x02,
169 DW_DS_trailing_overpunch = 0x03,
170 DW_DS_leading_separate = 0x04,
171 DW_DS_trailing_separate = 0x05
172 };
173
174 enum EndianityEncoding {
175 // Endianity attribute values
176 #define HANDLE_DW_END(ID, NAME) DW_END_##NAME = ID,
177 #include "llvm/BinaryFormat/Dwarf.def"
178 DW_END_lo_user = 0x40,
179 DW_END_hi_user = 0xff
180 };
181
182 enum AccessAttribute {
183 // Accessibility codes
184 DW_ACCESS_public = 0x01,
185 DW_ACCESS_protected = 0x02,
186 DW_ACCESS_private = 0x03
187 };
188
189 enum VisibilityAttribute {
190 // Visibility codes
191 DW_VIS_local = 0x01,
192 DW_VIS_exported = 0x02,
193 DW_VIS_qualified = 0x03
194 };
195
196 enum VirtualityAttribute {
197 #define HANDLE_DW_VIRTUALITY(ID, NAME) DW_VIRTUALITY_##NAME = ID,
198 #include "llvm/BinaryFormat/Dwarf.def"
199 DW_VIRTUALITY_max = 0x02
200 };
201
202 enum EnumKindAttribute {
203 #define HANDLE_DW_APPLE_ENUM_KIND(ID, NAME) DW_APPLE_ENUM_KIND_##NAME = ID,
204 #include "llvm/BinaryFormat/Dwarf.def"
205 DW_APPLE_ENUM_KIND_max = 0x01
206 };
207
208 enum DefaultedMemberAttribute {
209 #define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID,
210 #include "llvm/BinaryFormat/Dwarf.def"
211 DW_DEFAULTED_max = 0x02
212 };
213
214 enum SourceLanguage {
215 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
216 DW_LANG_##NAME = ID,
217 #include "llvm/BinaryFormat/Dwarf.def"
218 DW_LANG_lo_user = 0x8000,
219 DW_LANG_hi_user = 0xffff
220 };
221
222 enum SourceLanguageName : uint16_t {
223 #define HANDLE_DW_LNAME(ID, NAME, DESC, LOWER_BOUND) DW_LNAME_##NAME = ID,
224 #include "llvm/BinaryFormat/Dwarf.def"
225 };
226
227 /// Convert a DWARF 6 pair of language name and version to a DWARF 5 DW_LANG.
228 /// If the version number doesn't exactly match a known version it is
229 /// rounded up to the next-highest known version number.
toDW_LANG(SourceLanguageName name,uint32_t version)230 inline std::optional<SourceLanguage> toDW_LANG(SourceLanguageName name,
231 uint32_t version) {
232 switch (name) {
233 case DW_LNAME_Ada: // YYYY
234 if (version <= 1983)
235 return DW_LANG_Ada83;
236 if (version <= 1995)
237 return DW_LANG_Ada95;
238 if (version <= 2005)
239 return DW_LANG_Ada2005;
240 if (version <= 2012)
241 return DW_LANG_Ada2012;
242 return {};
243 case DW_LNAME_BLISS:
244 return DW_LANG_BLISS;
245 case DW_LNAME_C: // YYYYMM, K&R 000000
246 if (version == 0)
247 return DW_LANG_C;
248 if (version <= 198912)
249 return DW_LANG_C89;
250 if (version <= 199901)
251 return DW_LANG_C99;
252 if (version <= 201112)
253 return DW_LANG_C11;
254 if (version <= 201710)
255 return DW_LANG_C17;
256 return {};
257 case DW_LNAME_C_plus_plus: // YYYYMM
258 if (version == 0)
259 return DW_LANG_C_plus_plus;
260 if (version <= 199711)
261 return DW_LANG_C_plus_plus;
262 if (version <= 200310)
263 return DW_LANG_C_plus_plus_03;
264 if (version <= 201103)
265 return DW_LANG_C_plus_plus_11;
266 if (version <= 201402)
267 return DW_LANG_C_plus_plus_14;
268 if (version <= 201703)
269 return DW_LANG_C_plus_plus_17;
270 if (version <= 202002)
271 return DW_LANG_C_plus_plus_20;
272 return {};
273 case DW_LNAME_Cobol: // YYYY
274 if (version <= 1974)
275 return DW_LANG_Cobol74;
276 if (version <= 1985)
277 return DW_LANG_Cobol85;
278 return {};
279 case DW_LNAME_Crystal:
280 return DW_LANG_Crystal;
281 case DW_LNAME_D:
282 return DW_LANG_D;
283 case DW_LNAME_Dylan:
284 return DW_LANG_Dylan;
285 case DW_LNAME_Fortran: // YYYY
286 if (version <= 1977)
287 return DW_LANG_Fortran77;
288 if (version <= 1990)
289 return DW_LANG_Fortran90;
290 if (version <= 1995)
291 return DW_LANG_Fortran95;
292 if (version <= 2003)
293 return DW_LANG_Fortran03;
294 if (version <= 2008)
295 return DW_LANG_Fortran08;
296 if (version <= 2018)
297 return DW_LANG_Fortran18;
298 return {};
299 case DW_LNAME_Go:
300 return DW_LANG_Go;
301 case DW_LNAME_Haskell:
302 return DW_LANG_Haskell;
303 // case DW_LNAME_HIP:
304 // return DW_LANG_HIP;
305 case DW_LNAME_Java:
306 return DW_LANG_Java;
307 case DW_LNAME_Julia:
308 return DW_LANG_Julia;
309 case DW_LNAME_Kotlin:
310 return DW_LANG_Kotlin;
311 case DW_LNAME_Modula2:
312 return DW_LANG_Modula2;
313 case DW_LNAME_Modula3:
314 return DW_LANG_Modula3;
315 case DW_LNAME_ObjC:
316 return DW_LANG_ObjC;
317 case DW_LNAME_ObjC_plus_plus:
318 return DW_LANG_ObjC_plus_plus;
319 case DW_LNAME_OCaml:
320 return DW_LANG_OCaml;
321 case DW_LNAME_OpenCL_C:
322 return DW_LANG_OpenCL;
323 case DW_LNAME_Pascal:
324 return DW_LANG_Pascal83;
325 case DW_LNAME_PLI:
326 return DW_LANG_PLI;
327 case DW_LNAME_Python:
328 return DW_LANG_Python;
329 case DW_LNAME_RenderScript:
330 return DW_LANG_RenderScript;
331 case DW_LNAME_Rust:
332 return DW_LANG_Rust;
333 case DW_LNAME_Swift:
334 return DW_LANG_Swift;
335 case DW_LNAME_UPC:
336 return DW_LANG_UPC;
337 case DW_LNAME_Zig:
338 return DW_LANG_Zig;
339 case DW_LNAME_Assembly:
340 return DW_LANG_Assembly;
341 case DW_LNAME_C_sharp:
342 return DW_LANG_C_sharp;
343 case DW_LNAME_Mojo:
344 return DW_LANG_Mojo;
345 case DW_LNAME_GLSL:
346 return DW_LANG_GLSL;
347 case DW_LNAME_GLSL_ES:
348 return DW_LANG_GLSL_ES;
349 case DW_LNAME_HLSL:
350 return DW_LANG_HLSL;
351 case DW_LNAME_OpenCL_CPP:
352 return DW_LANG_OpenCL_CPP;
353 case DW_LNAME_CPP_for_OpenCL:
354 return {};
355 case DW_LNAME_SYCL:
356 return DW_LANG_SYCL;
357 case DW_LNAME_Ruby:
358 return DW_LANG_Ruby;
359 case DW_LNAME_Move:
360 return DW_LANG_Move;
361 case DW_LNAME_Hylo:
362 return DW_LANG_Hylo;
363 case DW_LNAME_Metal:
364 return DW_LANG_Metal;
365 }
366 return {};
367 }
368
369 /// Convert a DWARF 5 DW_LANG to a DWARF 6 pair of language name and version.
370 inline std::optional<std::pair<SourceLanguageName, uint32_t>>
toDW_LNAME(SourceLanguage language)371 toDW_LNAME(SourceLanguage language) {
372 switch (language) {
373 case DW_LANG_Ada83:
374 return {{DW_LNAME_Ada, 1983}};
375 case DW_LANG_Ada95:
376 return {{DW_LNAME_Ada, 1995}};
377 case DW_LANG_Ada2005:
378 return {{DW_LNAME_Ada, 2005}};
379 case DW_LANG_Ada2012:
380 return {{DW_LNAME_Ada, 2012}};
381 case DW_LANG_BLISS:
382 return {{DW_LNAME_BLISS, 0}};
383 case DW_LANG_C:
384 return {{DW_LNAME_C, 0}};
385 case DW_LANG_C89:
386 return {{DW_LNAME_C, 198912}};
387 case DW_LANG_C99:
388 return {{DW_LNAME_C, 199901}};
389 case DW_LANG_C11:
390 return {{DW_LNAME_C, 201112}};
391 case DW_LANG_C17:
392 return {{DW_LNAME_C, 201712}};
393 case DW_LANG_C_plus_plus:
394 return {{DW_LNAME_C_plus_plus, 0}};
395 case DW_LANG_C_plus_plus_03:
396 return {{DW_LNAME_C_plus_plus, 200310}};
397 case DW_LANG_C_plus_plus_11:
398 return {{DW_LNAME_C_plus_plus, 201103}};
399 case DW_LANG_C_plus_plus_14:
400 return {{DW_LNAME_C_plus_plus, 201402}};
401 case DW_LANG_C_plus_plus_17:
402 return {{DW_LNAME_C_plus_plus, 201703}};
403 case DW_LANG_C_plus_plus_20:
404 return {{DW_LNAME_C_plus_plus, 202002}};
405 case DW_LANG_Cobol74:
406 return {{DW_LNAME_Cobol, 1974}};
407 case DW_LANG_Cobol85:
408 return {{DW_LNAME_Cobol, 1985}};
409 case DW_LANG_Crystal:
410 return {{DW_LNAME_Crystal, 0}};
411 case DW_LANG_D:
412 return {{DW_LNAME_D, 0}};
413 case DW_LANG_Dylan:
414 return {{DW_LNAME_Dylan, 0}};
415 case DW_LANG_Fortran77:
416 return {{DW_LNAME_Fortran, 1977}};
417 case DW_LANG_Fortran90:
418 return {{DW_LNAME_Fortran, 1990}};
419 case DW_LANG_Fortran95:
420 return {{DW_LNAME_Fortran, 1995}};
421 case DW_LANG_Fortran03:
422 return {{DW_LNAME_Fortran, 2003}};
423 case DW_LANG_Fortran08:
424 return {{DW_LNAME_Fortran, 2008}};
425 case DW_LANG_Fortran18:
426 return {{DW_LNAME_Fortran, 2018}};
427 case DW_LANG_Go:
428 return {{DW_LNAME_Go, 0}};
429 case DW_LANG_Haskell:
430 return {{DW_LNAME_Haskell, 0}};
431 case DW_LANG_HIP:
432 return {}; // return {{DW_LNAME_HIP, 0}};
433 case DW_LANG_Java:
434 return {{DW_LNAME_Java, 0}};
435 case DW_LANG_Julia:
436 return {{DW_LNAME_Julia, 0}};
437 case DW_LANG_Kotlin:
438 return {{DW_LNAME_Kotlin, 0}};
439 case DW_LANG_Modula2:
440 return {{DW_LNAME_Modula2, 0}};
441 case DW_LANG_Modula3:
442 return {{DW_LNAME_Modula3, 0}};
443 case DW_LANG_ObjC:
444 return {{DW_LNAME_ObjC, 0}};
445 case DW_LANG_ObjC_plus_plus:
446 return {{DW_LNAME_ObjC_plus_plus, 0}};
447 case DW_LANG_OCaml:
448 return {{DW_LNAME_OCaml, 0}};
449 case DW_LANG_OpenCL:
450 return {{DW_LNAME_OpenCL_C, 0}};
451 case DW_LANG_Pascal83:
452 return {{DW_LNAME_Pascal, 1983}};
453 case DW_LANG_PLI:
454 return {{DW_LNAME_PLI, 0}};
455 case DW_LANG_Python:
456 return {{DW_LNAME_Python, 0}};
457 case DW_LANG_RenderScript:
458 case DW_LANG_GOOGLE_RenderScript:
459 return {{DW_LNAME_RenderScript, 0}};
460 case DW_LANG_Rust:
461 return {{DW_LNAME_Rust, 0}};
462 case DW_LANG_Swift:
463 return {{DW_LNAME_Swift, 0}};
464 case DW_LANG_UPC:
465 return {{DW_LNAME_UPC, 0}};
466 case DW_LANG_Zig:
467 return {{DW_LNAME_Zig, 0}};
468 case DW_LANG_Assembly:
469 case DW_LANG_Mips_Assembler:
470 return {{DW_LNAME_Assembly, 0}};
471 case DW_LANG_C_sharp:
472 return {{DW_LNAME_C_sharp, 0}};
473 case DW_LANG_Mojo:
474 return {{DW_LNAME_Mojo, 0}};
475 case DW_LANG_GLSL:
476 return {{DW_LNAME_GLSL, 0}};
477 case DW_LANG_GLSL_ES:
478 return {{DW_LNAME_GLSL_ES, 0}};
479 case DW_LANG_HLSL:
480 return {{DW_LNAME_HLSL, 0}};
481 case DW_LANG_OpenCL_CPP:
482 return {{DW_LNAME_OpenCL_CPP, 0}};
483 case DW_LANG_SYCL:
484 return {{DW_LNAME_SYCL, 0}};
485 case DW_LANG_Ruby:
486 return {{DW_LNAME_Ruby, 0}};
487 case DW_LANG_Move:
488 return {{DW_LNAME_Move, 0}};
489 case DW_LANG_Hylo:
490 return {{DW_LNAME_Hylo, 0}};
491 case DW_LANG_Metal:
492 return {{DW_LNAME_Metal, 0}};
493 case DW_LANG_BORLAND_Delphi:
494 case DW_LANG_CPP_for_OpenCL:
495 case DW_LANG_lo_user:
496 case DW_LANG_hi_user:
497 return {};
498 }
499 return {};
500 }
501
502 LLVM_ABI llvm::StringRef LanguageDescription(SourceLanguageName name);
503
isCPlusPlus(SourceLanguage S)504 inline bool isCPlusPlus(SourceLanguage S) {
505 bool result = false;
506 // Deliberately enumerate all the language options so we get a warning when
507 // new language options are added (-Wswitch) that'll hopefully help keep this
508 // switch up-to-date when new C++ versions are added.
509 switch (S) {
510 case DW_LANG_C_plus_plus:
511 case DW_LANG_C_plus_plus_03:
512 case DW_LANG_C_plus_plus_11:
513 case DW_LANG_C_plus_plus_14:
514 case DW_LANG_C_plus_plus_17:
515 case DW_LANG_C_plus_plus_20:
516 result = true;
517 break;
518 case DW_LANG_C89:
519 case DW_LANG_C:
520 case DW_LANG_Ada83:
521 case DW_LANG_Cobol74:
522 case DW_LANG_Cobol85:
523 case DW_LANG_Fortran77:
524 case DW_LANG_Fortran90:
525 case DW_LANG_Pascal83:
526 case DW_LANG_Modula2:
527 case DW_LANG_Java:
528 case DW_LANG_C99:
529 case DW_LANG_Ada95:
530 case DW_LANG_Fortran95:
531 case DW_LANG_PLI:
532 case DW_LANG_ObjC:
533 case DW_LANG_ObjC_plus_plus:
534 case DW_LANG_UPC:
535 case DW_LANG_D:
536 case DW_LANG_Python:
537 case DW_LANG_OpenCL:
538 case DW_LANG_Go:
539 case DW_LANG_Modula3:
540 case DW_LANG_Haskell:
541 case DW_LANG_OCaml:
542 case DW_LANG_Rust:
543 case DW_LANG_C11:
544 case DW_LANG_Swift:
545 case DW_LANG_Julia:
546 case DW_LANG_Dylan:
547 case DW_LANG_Fortran03:
548 case DW_LANG_Fortran08:
549 case DW_LANG_RenderScript:
550 case DW_LANG_BLISS:
551 case DW_LANG_Mips_Assembler:
552 case DW_LANG_GOOGLE_RenderScript:
553 case DW_LANG_BORLAND_Delphi:
554 case DW_LANG_lo_user:
555 case DW_LANG_hi_user:
556 case DW_LANG_Kotlin:
557 case DW_LANG_Zig:
558 case DW_LANG_Crystal:
559 case DW_LANG_C17:
560 case DW_LANG_Fortran18:
561 case DW_LANG_Ada2005:
562 case DW_LANG_Ada2012:
563 case DW_LANG_HIP:
564 case DW_LANG_Assembly:
565 case DW_LANG_C_sharp:
566 case DW_LANG_Mojo:
567 case DW_LANG_GLSL:
568 case DW_LANG_GLSL_ES:
569 case DW_LANG_HLSL:
570 case DW_LANG_OpenCL_CPP:
571 case DW_LANG_CPP_for_OpenCL:
572 case DW_LANG_SYCL:
573 case DW_LANG_Ruby:
574 case DW_LANG_Move:
575 case DW_LANG_Hylo:
576 case DW_LANG_Metal:
577 result = false;
578 break;
579 }
580
581 return result;
582 }
583
isFortran(SourceLanguage S)584 inline bool isFortran(SourceLanguage S) {
585 bool result = false;
586 // Deliberately enumerate all the language options so we get a warning when
587 // new language options are added (-Wswitch) that'll hopefully help keep this
588 // switch up-to-date when new Fortran versions are added.
589 switch (S) {
590 case DW_LANG_Fortran77:
591 case DW_LANG_Fortran90:
592 case DW_LANG_Fortran95:
593 case DW_LANG_Fortran03:
594 case DW_LANG_Fortran08:
595 case DW_LANG_Fortran18:
596 result = true;
597 break;
598 case DW_LANG_C89:
599 case DW_LANG_C:
600 case DW_LANG_Ada83:
601 case DW_LANG_C_plus_plus:
602 case DW_LANG_Cobol74:
603 case DW_LANG_Cobol85:
604 case DW_LANG_Pascal83:
605 case DW_LANG_Modula2:
606 case DW_LANG_Java:
607 case DW_LANG_C99:
608 case DW_LANG_Ada95:
609 case DW_LANG_PLI:
610 case DW_LANG_ObjC:
611 case DW_LANG_ObjC_plus_plus:
612 case DW_LANG_UPC:
613 case DW_LANG_D:
614 case DW_LANG_Python:
615 case DW_LANG_OpenCL:
616 case DW_LANG_Go:
617 case DW_LANG_Modula3:
618 case DW_LANG_Haskell:
619 case DW_LANG_C_plus_plus_03:
620 case DW_LANG_C_plus_plus_11:
621 case DW_LANG_OCaml:
622 case DW_LANG_Rust:
623 case DW_LANG_C11:
624 case DW_LANG_Swift:
625 case DW_LANG_Julia:
626 case DW_LANG_Dylan:
627 case DW_LANG_C_plus_plus_14:
628 case DW_LANG_RenderScript:
629 case DW_LANG_BLISS:
630 case DW_LANG_Mips_Assembler:
631 case DW_LANG_GOOGLE_RenderScript:
632 case DW_LANG_BORLAND_Delphi:
633 case DW_LANG_lo_user:
634 case DW_LANG_hi_user:
635 case DW_LANG_Kotlin:
636 case DW_LANG_Zig:
637 case DW_LANG_Crystal:
638 case DW_LANG_C_plus_plus_17:
639 case DW_LANG_C_plus_plus_20:
640 case DW_LANG_C17:
641 case DW_LANG_Ada2005:
642 case DW_LANG_Ada2012:
643 case DW_LANG_HIP:
644 case DW_LANG_Assembly:
645 case DW_LANG_C_sharp:
646 case DW_LANG_Mojo:
647 case DW_LANG_GLSL:
648 case DW_LANG_GLSL_ES:
649 case DW_LANG_HLSL:
650 case DW_LANG_OpenCL_CPP:
651 case DW_LANG_CPP_for_OpenCL:
652 case DW_LANG_SYCL:
653 case DW_LANG_Ruby:
654 case DW_LANG_Move:
655 case DW_LANG_Hylo:
656 case DW_LANG_Metal:
657 result = false;
658 break;
659 }
660
661 return result;
662 }
663
isC(SourceLanguage S)664 inline bool isC(SourceLanguage S) {
665 // Deliberately enumerate all the language options so we get a warning when
666 // new language options are added (-Wswitch) that'll hopefully help keep this
667 // switch up-to-date when new C++ versions are added.
668 switch (S) {
669 case DW_LANG_C11:
670 case DW_LANG_C17:
671 case DW_LANG_C89:
672 case DW_LANG_C99:
673 case DW_LANG_C:
674 case DW_LANG_ObjC:
675 return true;
676 case DW_LANG_C_plus_plus:
677 case DW_LANG_C_plus_plus_03:
678 case DW_LANG_C_plus_plus_11:
679 case DW_LANG_C_plus_plus_14:
680 case DW_LANG_C_plus_plus_17:
681 case DW_LANG_C_plus_plus_20:
682 case DW_LANG_Ada83:
683 case DW_LANG_Cobol74:
684 case DW_LANG_Cobol85:
685 case DW_LANG_Fortran77:
686 case DW_LANG_Fortran90:
687 case DW_LANG_Pascal83:
688 case DW_LANG_Modula2:
689 case DW_LANG_Java:
690 case DW_LANG_Ada95:
691 case DW_LANG_Fortran95:
692 case DW_LANG_PLI:
693 case DW_LANG_ObjC_plus_plus:
694 case DW_LANG_UPC:
695 case DW_LANG_D:
696 case DW_LANG_Python:
697 case DW_LANG_OpenCL:
698 case DW_LANG_Go:
699 case DW_LANG_Modula3:
700 case DW_LANG_Haskell:
701 case DW_LANG_OCaml:
702 case DW_LANG_Rust:
703 case DW_LANG_Swift:
704 case DW_LANG_Julia:
705 case DW_LANG_Dylan:
706 case DW_LANG_Fortran03:
707 case DW_LANG_Fortran08:
708 case DW_LANG_RenderScript:
709 case DW_LANG_BLISS:
710 case DW_LANG_Mips_Assembler:
711 case DW_LANG_GOOGLE_RenderScript:
712 case DW_LANG_BORLAND_Delphi:
713 case DW_LANG_lo_user:
714 case DW_LANG_hi_user:
715 case DW_LANG_Kotlin:
716 case DW_LANG_Zig:
717 case DW_LANG_Crystal:
718 case DW_LANG_Fortran18:
719 case DW_LANG_Ada2005:
720 case DW_LANG_Ada2012:
721 case DW_LANG_HIP:
722 case DW_LANG_Assembly:
723 case DW_LANG_C_sharp:
724 case DW_LANG_Mojo:
725 case DW_LANG_GLSL:
726 case DW_LANG_GLSL_ES:
727 case DW_LANG_HLSL:
728 case DW_LANG_OpenCL_CPP:
729 case DW_LANG_CPP_for_OpenCL:
730 case DW_LANG_SYCL:
731 case DW_LANG_Ruby:
732 case DW_LANG_Move:
733 case DW_LANG_Hylo:
734 case DW_LANG_Metal:
735 return false;
736 }
737 llvm_unreachable("Unknown language kind.");
738 }
739
getArrayIndexTypeEncoding(SourceLanguage S)740 inline TypeKind getArrayIndexTypeEncoding(SourceLanguage S) {
741 return isFortran(S) ? DW_ATE_signed : DW_ATE_unsigned;
742 }
743
744 enum CaseSensitivity {
745 // Identifier case codes
746 DW_ID_case_sensitive = 0x00,
747 DW_ID_up_case = 0x01,
748 DW_ID_down_case = 0x02,
749 DW_ID_case_insensitive = 0x03
750 };
751
752 enum CallingConvention {
753 // Calling convention codes
754 #define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID,
755 #include "llvm/BinaryFormat/Dwarf.def"
756 DW_CC_lo_user = 0x40,
757 DW_CC_hi_user = 0xff
758 };
759
760 enum InlineAttribute {
761 // Inline codes
762 DW_INL_not_inlined = 0x00,
763 DW_INL_inlined = 0x01,
764 DW_INL_declared_not_inlined = 0x02,
765 DW_INL_declared_inlined = 0x03
766 };
767
768 enum ArrayDimensionOrdering {
769 // Array ordering
770 DW_ORD_row_major = 0x00,
771 DW_ORD_col_major = 0x01
772 };
773
774 enum DiscriminantList {
775 // Discriminant descriptor values
776 DW_DSC_label = 0x00,
777 DW_DSC_range = 0x01
778 };
779
780 /// Line Number Standard Opcode Encodings.
781 enum LineNumberOps : uint8_t {
782 #define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID,
783 #include "llvm/BinaryFormat/Dwarf.def"
784 };
785
786 /// Line Number Extended Opcode Encodings.
787 enum LineNumberExtendedOps {
788 #define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID,
789 #include "llvm/BinaryFormat/Dwarf.def"
790 DW_LNE_lo_user = 0x80,
791 DW_LNE_hi_user = 0xff
792 };
793
794 enum LineNumberEntryFormat {
795 #define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID,
796 #include "llvm/BinaryFormat/Dwarf.def"
797 DW_LNCT_lo_user = 0x2000,
798 DW_LNCT_hi_user = 0x3fff,
799 };
800
801 enum MacinfoRecordType {
802 // Macinfo Type Encodings
803 DW_MACINFO_define = 0x01,
804 DW_MACINFO_undef = 0x02,
805 DW_MACINFO_start_file = 0x03,
806 DW_MACINFO_end_file = 0x04,
807 DW_MACINFO_vendor_ext = 0xff
808 };
809
810 /// DWARF v5 macro information entry type encodings.
811 enum MacroEntryType {
812 #define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID,
813 #include "llvm/BinaryFormat/Dwarf.def"
814 DW_MACRO_lo_user = 0xe0,
815 DW_MACRO_hi_user = 0xff
816 };
817
818 /// GNU .debug_macro macro information entry type encodings.
819 enum GnuMacroEntryType {
820 #define HANDLE_DW_MACRO_GNU(ID, NAME) DW_MACRO_GNU_##NAME = ID,
821 #include "llvm/BinaryFormat/Dwarf.def"
822 DW_MACRO_GNU_lo_user = 0xe0,
823 DW_MACRO_GNU_hi_user = 0xff
824 };
825
826 /// DWARF v5 range list entry encoding values.
827 enum RnglistEntries {
828 #define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID,
829 #include "llvm/BinaryFormat/Dwarf.def"
830 };
831
832 /// DWARF v5 loc list entry encoding values.
833 enum LoclistEntries {
834 #define HANDLE_DW_LLE(ID, NAME) DW_LLE_##NAME = ID,
835 #include "llvm/BinaryFormat/Dwarf.def"
836 };
837
838 /// Call frame instruction encodings.
839 enum CallFrameInfo {
840 #define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID,
841 #define HANDLE_DW_CFA_PRED(ID, NAME, ARCH) DW_CFA_##NAME = ID,
842 #include "llvm/BinaryFormat/Dwarf.def"
843 DW_CFA_extended = 0x00,
844
845 DW_CFA_lo_user = 0x1c,
846 DW_CFA_hi_user = 0x3f
847 };
848
849 enum Constants {
850 // Children flag
851 DW_CHILDREN_no = 0x00,
852 DW_CHILDREN_yes = 0x01,
853
854 DW_EH_PE_absptr = 0x00,
855 DW_EH_PE_omit = 0xff,
856 DW_EH_PE_uleb128 = 0x01,
857 DW_EH_PE_udata2 = 0x02,
858 DW_EH_PE_udata4 = 0x03,
859 DW_EH_PE_udata8 = 0x04,
860 DW_EH_PE_sleb128 = 0x09,
861 DW_EH_PE_sdata2 = 0x0A,
862 DW_EH_PE_sdata4 = 0x0B,
863 DW_EH_PE_sdata8 = 0x0C,
864 DW_EH_PE_signed = 0x08,
865 DW_EH_PE_pcrel = 0x10,
866 DW_EH_PE_textrel = 0x20,
867 DW_EH_PE_datarel = 0x30,
868 DW_EH_PE_funcrel = 0x40,
869 DW_EH_PE_aligned = 0x50,
870 DW_EH_PE_indirect = 0x80
871 };
872
873 /// Constants for the DW_APPLE_PROPERTY_attributes attribute.
874 /// Keep this list in sync with clang's DeclObjCCommon.h
875 /// ObjCPropertyAttribute::Kind!
876 enum ApplePropertyAttributes {
877 #define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
878 #include "llvm/BinaryFormat/Dwarf.def"
879 };
880
881 /// Constants for unit types in DWARF v5.
882 enum UnitType : unsigned char {
883 #define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
884 #include "llvm/BinaryFormat/Dwarf.def"
885 DW_UT_lo_user = 0x80,
886 DW_UT_hi_user = 0xff
887 };
888
889 enum Index {
890 #define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID,
891 #include "llvm/BinaryFormat/Dwarf.def"
892 DW_IDX_lo_user = 0x2000,
893 DW_IDX_hi_user = 0x3fff
894 };
895
isUnitType(uint8_t UnitType)896 inline bool isUnitType(uint8_t UnitType) {
897 switch (UnitType) {
898 case DW_UT_compile:
899 case DW_UT_type:
900 case DW_UT_partial:
901 case DW_UT_skeleton:
902 case DW_UT_split_compile:
903 case DW_UT_split_type:
904 return true;
905 default:
906 return false;
907 }
908 }
909
isUnitType(dwarf::Tag T)910 inline bool isUnitType(dwarf::Tag T) {
911 switch (T) {
912 case DW_TAG_compile_unit:
913 case DW_TAG_type_unit:
914 case DW_TAG_partial_unit:
915 case DW_TAG_skeleton_unit:
916 return true;
917 default:
918 return false;
919 }
920 }
921
922 // Constants for the DWARF v5 Accelerator Table Proposal
923 enum AcceleratorTable {
924 // Data layout descriptors.
925 DW_ATOM_null = 0u, /// Marker as the end of a list of atoms.
926 DW_ATOM_die_offset = 1u, // DIE offset in the debug_info section.
927 DW_ATOM_cu_offset = 2u, // Offset of the compile unit header that contains the
928 // item in question.
929 DW_ATOM_die_tag = 3u, // A tag entry.
930 DW_ATOM_type_flags = 4u, // Set of flags for a type.
931
932 DW_ATOM_type_type_flags = 5u, // Dsymutil type extension.
933 DW_ATOM_qual_name_hash = 6u, // Dsymutil qualified hash extension.
934
935 // DW_ATOM_type_flags values.
936
937 // Always set for C++, only set for ObjC if this is the @implementation for a
938 // class.
939 DW_FLAG_type_implementation = 2u,
940
941 // Hash functions.
942
943 // Daniel J. Bernstein hash.
944 DW_hash_function_djb = 0u
945 };
946
947 // Return a suggested bucket count for the DWARF v5 Accelerator Table.
getDebugNamesBucketCount(uint32_t UniqueHashCount)948 inline uint32_t getDebugNamesBucketCount(uint32_t UniqueHashCount) {
949 if (UniqueHashCount > 1024)
950 return UniqueHashCount / 4;
951 if (UniqueHashCount > 16)
952 return UniqueHashCount / 2;
953 return std::max<uint32_t>(UniqueHashCount, 1);
954 }
955
956 // Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
957 enum GDBIndexEntryKind {
958 GIEK_NONE,
959 GIEK_TYPE,
960 GIEK_VARIABLE,
961 GIEK_FUNCTION,
962 GIEK_OTHER,
963 GIEK_UNUSED5,
964 GIEK_UNUSED6,
965 GIEK_UNUSED7
966 };
967
968 enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC };
969
970 /// \defgroup DwarfConstantsDumping Dwarf constants dumping functions
971 ///
972 /// All these functions map their argument's value back to the
973 /// corresponding enumerator name or return an empty StringRef if the value
974 /// isn't known.
975 ///
976 /// @{
977 LLVM_ABI StringRef TagString(unsigned Tag);
978 LLVM_ABI StringRef ChildrenString(unsigned Children);
979 LLVM_ABI StringRef AttributeString(unsigned Attribute);
980 LLVM_ABI StringRef FormEncodingString(unsigned Encoding);
981 LLVM_ABI StringRef OperationEncodingString(unsigned Encoding);
982 LLVM_ABI StringRef SubOperationEncodingString(unsigned OpEncoding,
983 unsigned SubOpEncoding);
984 LLVM_ABI StringRef AttributeEncodingString(unsigned Encoding);
985 LLVM_ABI StringRef DecimalSignString(unsigned Sign);
986 LLVM_ABI StringRef EndianityString(unsigned Endian);
987 LLVM_ABI StringRef AccessibilityString(unsigned Access);
988 LLVM_ABI StringRef DefaultedMemberString(unsigned DefaultedEncodings);
989 LLVM_ABI StringRef VisibilityString(unsigned Visibility);
990 LLVM_ABI StringRef VirtualityString(unsigned Virtuality);
991 LLVM_ABI StringRef EnumKindString(unsigned EnumKind);
992 LLVM_ABI StringRef LanguageString(unsigned Language);
993 LLVM_ABI StringRef CaseString(unsigned Case);
994 LLVM_ABI StringRef ConventionString(unsigned Convention);
995 LLVM_ABI StringRef InlineCodeString(unsigned Code);
996 LLVM_ABI StringRef ArrayOrderString(unsigned Order);
997 LLVM_ABI StringRef LNStandardString(unsigned Standard);
998 LLVM_ABI StringRef LNExtendedString(unsigned Encoding);
999 LLVM_ABI StringRef MacinfoString(unsigned Encoding);
1000 LLVM_ABI StringRef MacroString(unsigned Encoding);
1001 LLVM_ABI StringRef GnuMacroString(unsigned Encoding);
1002 LLVM_ABI StringRef RangeListEncodingString(unsigned Encoding);
1003 LLVM_ABI StringRef LocListEncodingString(unsigned Encoding);
1004 LLVM_ABI StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch);
1005 LLVM_ABI StringRef ApplePropertyString(unsigned);
1006 LLVM_ABI StringRef UnitTypeString(unsigned);
1007 LLVM_ABI StringRef AtomTypeString(unsigned Atom);
1008 LLVM_ABI StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind);
1009 LLVM_ABI StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage);
1010 LLVM_ABI StringRef IndexString(unsigned Idx);
1011 LLVM_ABI StringRef FormatString(DwarfFormat Format);
1012 LLVM_ABI StringRef FormatString(bool IsDWARF64);
1013 LLVM_ABI StringRef RLEString(unsigned RLE);
1014 /// @}
1015
1016 /// \defgroup DwarfConstantsParsing Dwarf constants parsing functions
1017 ///
1018 /// These functions map their strings back to the corresponding enumeration
1019 /// value or return 0 if there is none, except for these exceptions:
1020 ///
1021 /// \li \a getTag() returns \a DW_TAG_invalid on invalid input.
1022 /// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input.
1023 /// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input.
1024 ///
1025 /// @{
1026 LLVM_ABI unsigned getTag(StringRef TagString);
1027 LLVM_ABI unsigned getOperationEncoding(StringRef OperationEncodingString);
1028 LLVM_ABI unsigned getSubOperationEncoding(unsigned OpEncoding,
1029 StringRef SubOperationEncodingString);
1030 LLVM_ABI unsigned getVirtuality(StringRef VirtualityString);
1031 LLVM_ABI unsigned getEnumKind(StringRef EnumKindString);
1032 LLVM_ABI unsigned getLanguage(StringRef LanguageString);
1033 LLVM_ABI unsigned getCallingConvention(StringRef LanguageString);
1034 LLVM_ABI unsigned getAttributeEncoding(StringRef EncodingString);
1035 LLVM_ABI unsigned getMacinfo(StringRef MacinfoString);
1036 LLVM_ABI unsigned getMacro(StringRef MacroString);
1037 /// @}
1038
1039 /// \defgroup DwarfConstantsVersioning Dwarf version for constants
1040 ///
1041 /// For constants defined by DWARF, returns the DWARF version when the constant
1042 /// was first defined. For vendor extensions, if there is a version-related
1043 /// policy for when to emit it, returns a version number for that policy.
1044 /// Otherwise returns 0.
1045 ///
1046 /// @{
1047 LLVM_ABI unsigned TagVersion(Tag T);
1048 LLVM_ABI unsigned AttributeVersion(Attribute A);
1049 LLVM_ABI unsigned FormVersion(Form F);
1050 LLVM_ABI unsigned OperationVersion(LocationAtom O);
1051 LLVM_ABI unsigned AttributeEncodingVersion(TypeKind E);
1052 LLVM_ABI unsigned LanguageVersion(SourceLanguage L);
1053 /// @}
1054
1055 /// \defgroup DwarfConstantsVendor Dwarf "vendor" for constants
1056 ///
1057 /// These functions return an identifier describing "who" defined the constant,
1058 /// either the DWARF standard itself or the vendor who defined the extension.
1059 ///
1060 /// @{
1061 LLVM_ABI unsigned TagVendor(Tag T);
1062 LLVM_ABI unsigned AttributeVendor(Attribute A);
1063 LLVM_ABI unsigned FormVendor(Form F);
1064 LLVM_ABI unsigned OperationVendor(LocationAtom O);
1065 LLVM_ABI unsigned AttributeEncodingVendor(TypeKind E);
1066 LLVM_ABI unsigned LanguageVendor(SourceLanguage L);
1067 /// @}
1068
1069 /// The number of operands for the given LocationAtom.
1070 LLVM_ABI std::optional<unsigned> OperationOperands(LocationAtom O);
1071
1072 /// The arity of the given LocationAtom. This is the number of elements on the
1073 /// stack this operation operates on. Returns -1 if the arity is variable (e.g.
1074 /// depending on the argument) or unknown.
1075 LLVM_ABI std::optional<unsigned> OperationArity(LocationAtom O);
1076
1077 LLVM_ABI std::optional<unsigned> LanguageLowerBound(SourceLanguage L);
1078
1079 /// The size of a reference determined by the DWARF 32/64-bit format.
getDwarfOffsetByteSize(DwarfFormat Format)1080 inline uint8_t getDwarfOffsetByteSize(DwarfFormat Format) {
1081 switch (Format) {
1082 case DwarfFormat::DWARF32:
1083 return 4;
1084 case DwarfFormat::DWARF64:
1085 return 8;
1086 }
1087 llvm_unreachable("Invalid Format value");
1088 }
1089
1090 /// A helper struct providing information about the byte size of DW_FORM
1091 /// values that vary in size depending on the DWARF version, address byte
1092 /// size, or DWARF32/DWARF64.
1093 struct FormParams {
1094 uint16_t Version;
1095 uint8_t AddrSize;
1096 DwarfFormat Format;
1097 /// True if DWARF v2 output generally uses relocations for references
1098 /// to other .debug_* sections.
1099 bool DwarfUsesRelocationsAcrossSections = false;
1100
1101 /// The definition of the size of form DW_FORM_ref_addr depends on the
1102 /// version. In DWARF v2 it's the size of an address; after that, it's the
1103 /// size of a reference.
getRefAddrByteSizeFormParams1104 uint8_t getRefAddrByteSize() const {
1105 if (Version == 2)
1106 return AddrSize;
1107 return getDwarfOffsetByteSize();
1108 }
1109
1110 /// The size of a reference is determined by the DWARF 32/64-bit format.
getDwarfOffsetByteSizeFormParams1111 uint8_t getDwarfOffsetByteSize() const {
1112 return dwarf::getDwarfOffsetByteSize(Format);
1113 }
1114
1115 explicit operator bool() const { return Version && AddrSize; }
1116 };
1117
1118 /// Get the byte size of the unit length field depending on the DWARF format.
getUnitLengthFieldByteSize(DwarfFormat Format)1119 inline uint8_t getUnitLengthFieldByteSize(DwarfFormat Format) {
1120 switch (Format) {
1121 case DwarfFormat::DWARF32:
1122 return 4;
1123 case DwarfFormat::DWARF64:
1124 return 12;
1125 }
1126 llvm_unreachable("Invalid Format value");
1127 }
1128
1129 /// Get the fixed byte size for a given form.
1130 ///
1131 /// If the form has a fixed byte size, then an Optional with a value will be
1132 /// returned. If the form is always encoded using a variable length storage
1133 /// format (ULEB or SLEB numbers or blocks) then std::nullopt will be returned.
1134 ///
1135 /// \param Form DWARF form to get the fixed byte size for.
1136 /// \param Params DWARF parameters to help interpret forms.
1137 /// \returns std::optional<uint8_t> value with the fixed byte size or
1138 /// std::nullopt if \p Form doesn't have a fixed byte size.
1139 LLVM_ABI std::optional<uint8_t> getFixedFormByteSize(dwarf::Form Form,
1140 FormParams Params);
1141
1142 /// Tells whether the specified form is defined in the specified version,
1143 /// or is an extension if extensions are allowed.
1144 LLVM_ABI bool isValidFormForVersion(Form F, unsigned Version,
1145 bool ExtensionsOk = true);
1146
1147 /// Returns the symbolic string representing Val when used as a value
1148 /// for attribute Attr.
1149 LLVM_ABI StringRef AttributeValueString(uint16_t Attr, unsigned Val);
1150
1151 /// Returns the symbolic string representing Val when used as a value
1152 /// for atom Atom.
1153 LLVM_ABI StringRef AtomValueString(uint16_t Atom, unsigned Val);
1154
1155 /// Describes an entry of the various gnu_pub* debug sections.
1156 ///
1157 /// The gnu_pub* kind looks like:
1158 ///
1159 /// 0-3 reserved
1160 /// 4-6 symbol kind
1161 /// 7 0 == global, 1 == static
1162 ///
1163 /// A gdb_index descriptor includes the above kind, shifted 24 bits up with the
1164 /// offset of the cu within the debug_info section stored in those 24 bits.
1165 struct PubIndexEntryDescriptor {
1166 GDBIndexEntryKind Kind;
1167 GDBIndexEntryLinkage Linkage;
PubIndexEntryDescriptorPubIndexEntryDescriptor1168 PubIndexEntryDescriptor(GDBIndexEntryKind Kind, GDBIndexEntryLinkage Linkage)
1169 : Kind(Kind), Linkage(Linkage) {}
PubIndexEntryDescriptorPubIndexEntryDescriptor1170 /* implicit */ PubIndexEntryDescriptor(GDBIndexEntryKind Kind)
1171 : Kind(Kind), Linkage(GIEL_EXTERNAL) {}
PubIndexEntryDescriptorPubIndexEntryDescriptor1172 explicit PubIndexEntryDescriptor(uint8_t Value)
1173 : Kind(
1174 static_cast<GDBIndexEntryKind>((Value & KIND_MASK) >> KIND_OFFSET)),
1175 Linkage(static_cast<GDBIndexEntryLinkage>((Value & LINKAGE_MASK) >>
1176 LINKAGE_OFFSET)) {}
toBitsPubIndexEntryDescriptor1177 uint8_t toBits() const {
1178 return Kind << KIND_OFFSET | Linkage << LINKAGE_OFFSET;
1179 }
1180
1181 private:
1182 enum {
1183 KIND_OFFSET = 4,
1184 KIND_MASK = 7 << KIND_OFFSET,
1185 LINKAGE_OFFSET = 7,
1186 LINKAGE_MASK = 1 << LINKAGE_OFFSET
1187 };
1188 };
1189
1190 template <typename Enum> struct EnumTraits : public std::false_type {};
1191
1192 template <> struct EnumTraits<Attribute> : public std::true_type {
1193 static constexpr char Type[3] = "AT";
1194 LLVM_ABI static StringRef (*const StringFn)(unsigned);
1195 };
1196
1197 template <> struct EnumTraits<Form> : public std::true_type {
1198 static constexpr char Type[5] = "FORM";
1199 LLVM_ABI static StringRef (*const StringFn)(unsigned);
1200 };
1201
1202 template <> struct EnumTraits<Index> : public std::true_type {
1203 static constexpr char Type[4] = "IDX";
1204 LLVM_ABI static StringRef (*const StringFn)(unsigned);
1205 };
1206
1207 template <> struct EnumTraits<Tag> : public std::true_type {
1208 static constexpr char Type[4] = "TAG";
1209 LLVM_ABI static StringRef (*const StringFn)(unsigned);
1210 };
1211
1212 template <> struct EnumTraits<LineNumberOps> : public std::true_type {
1213 static constexpr char Type[4] = "LNS";
1214 LLVM_ABI static StringRef (*const StringFn)(unsigned);
1215 };
1216
1217 template <> struct EnumTraits<LocationAtom> : public std::true_type {
1218 static constexpr char Type[3] = "OP";
1219 LLVM_ABI static StringRef (*const StringFn)(unsigned);
1220 };
1221
1222 inline uint64_t computeTombstoneAddress(uint8_t AddressByteSize) {
1223 return std::numeric_limits<uint64_t>::max() >> (8 - AddressByteSize) * 8;
1224 }
1225
1226 } // End of namespace dwarf
1227
1228 /// Dwarf constants format_provider
1229 ///
1230 /// Specialization of the format_provider template for dwarf enums. Unlike the
1231 /// dumping functions above, these format unknown enumerator values as
1232 /// DW_TYPE_unknown_1234 (e.g. DW_TAG_unknown_ffff).
1233 template <typename Enum>
1234 struct format_provider<Enum, std::enable_if_t<dwarf::EnumTraits<Enum>::value>> {
1235 static void format(const Enum &E, raw_ostream &OS, StringRef Style) {
1236 StringRef Str = dwarf::EnumTraits<Enum>::StringFn(E);
1237 if (Str.empty()) {
1238 OS << "DW_" << dwarf::EnumTraits<Enum>::Type << "_unknown_"
1239 << llvm::format("%x", E);
1240 } else
1241 OS << Str;
1242 }
1243 };
1244 } // End of namespace llvm
1245
1246 #endif
1247