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
31namespace llvm {
32class StringRef;
33
34namespace 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
44enum 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 /// \}
51
52 /// Special values for an initial length field.
53 /// \{
54 DW_LENGTH_lo_reserved = 0xfffffff0, ///< Lower bound of the reserved range.
55 DW_LENGTH_DWARF64 = 0xffffffff, ///< Indicator of 64-bit DWARF format.
56 DW_LENGTH_hi_reserved = 0xffffffff, ///< Upper bound of the reserved range.
57 /// \}
58
59 /// Other constants.
60 /// \{
61 DWARF_VERSION = 4, ///< Default dwarf version we output.
62 DW_PUBTYPES_VERSION = 2, ///< Section version number for .debug_pubtypes.
63 DW_PUBNAMES_VERSION = 2, ///< Section version number for .debug_pubnames.
64 DW_ARANGES_VERSION = 2, ///< Section version number for .debug_aranges.
65 /// \}
66
67 /// Identifiers we use to distinguish vendor extensions.
68 /// \{
69 DWARF_VENDOR_DWARF = 0, ///< Defined in v2 or later of the DWARF standard.
70 DWARF_VENDOR_APPLE = 1,
71 DWARF_VENDOR_BORLAND = 2,
72 DWARF_VENDOR_GNU = 3,
73 DWARF_VENDOR_GOOGLE = 4,
74 DWARF_VENDOR_LLVM = 5,
75 DWARF_VENDOR_MIPS = 6,
76 DWARF_VENDOR_WASM = 7,
77 DWARF_VENDOR_ALTIUM,
78 DWARF_VENDOR_COMPAQ,
79 DWARF_VENDOR_GHS,
80 DWARF_VENDOR_GO,
81 DWARF_VENDOR_HP,
82 DWARF_VENDOR_IBM,
83 DWARF_VENDOR_INTEL,
84 DWARF_VENDOR_PGI,
85 DWARF_VENDOR_SUN,
86 DWARF_VENDOR_UPC,
87 ///\}
88};
89
90/// Constants that define the DWARF format as 32 or 64 bit.
91enum DwarfFormat : uint8_t { DWARF32, DWARF64 };
92
93/// Special ID values that distinguish a CIE from a FDE in DWARF CFI.
94/// Not inside an enum because a 64-bit value is needed.
95/// @{
96const uint32_t DW_CIE_ID = UINT32_MAX;
97const uint64_t DW64_CIE_ID = UINT64_MAX;
98/// @}
99
100/// Identifier of an invalid DIE offset in the .debug_info section.
101const uint32_t DW_INVALID_OFFSET = UINT32_MAX;
102
103enum Tag : uint16_t {
104#define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) DW_TAG_##NAME = ID,
105#include "llvm/BinaryFormat/Dwarf.def"
106 DW_TAG_lo_user = 0x4080,
107 DW_TAG_hi_user = 0xffff,
108 DW_TAG_user_base = 0x1000 ///< Recommended base for user tags.
109};
110
111inline bool isType(Tag T) {
112 switch (T) {
113 default:
114 return false;
115#define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) \
116 case DW_TAG_##NAME: \
117 return (KIND == DW_KIND_TYPE);
118#include "llvm/BinaryFormat/Dwarf.def"
119 }
120}
121
122/// Attributes.
123enum Attribute : uint16_t {
124#define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) DW_AT_##NAME = ID,
125#include "llvm/BinaryFormat/Dwarf.def"
126 DW_AT_lo_user = 0x2000,
127 DW_AT_hi_user = 0x3fff,
128};
129
130enum Form : uint16_t {
131#define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) DW_FORM_##NAME = ID,
132#include "llvm/BinaryFormat/Dwarf.def"
133 DW_FORM_lo_user = 0x1f00, ///< Not specified by DWARF.
134};
135
136enum LocationAtom {
137#define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) DW_OP_##NAME = ID,
138#include "llvm/BinaryFormat/Dwarf.def"
139 DW_OP_lo_user = 0xe0,
140 DW_OP_hi_user = 0xff,
141 DW_OP_LLVM_fragment = 0x1000, ///< Only used in LLVM metadata.
142 DW_OP_LLVM_convert = 0x1001, ///< Only used in LLVM metadata.
143 DW_OP_LLVM_tag_offset = 0x1002, ///< Only used in LLVM metadata.
144 DW_OP_LLVM_entry_value = 0x1003, ///< Only used in LLVM metadata.
145 DW_OP_LLVM_implicit_pointer = 0x1004, ///< Only used in LLVM metadata.
146 DW_OP_LLVM_arg = 0x1005, ///< Only used in LLVM metadata.
147};
148
149enum LlvmUserLocationAtom {
150#define HANDLE_DW_OP_LLVM_USEROP(ID, NAME) DW_OP_LLVM_##NAME = ID,
151#include "llvm/BinaryFormat/Dwarf.def"
152};
153
154enum TypeKind : uint8_t {
155#define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) DW_ATE_##NAME = ID,
156#include "llvm/BinaryFormat/Dwarf.def"
157 DW_ATE_lo_user = 0x80,
158 DW_ATE_hi_user = 0xff
159};
160
161enum DecimalSignEncoding {
162 // Decimal sign attribute values
163 DW_DS_unsigned = 0x01,
164 DW_DS_leading_overpunch = 0x02,
165 DW_DS_trailing_overpunch = 0x03,
166 DW_DS_leading_separate = 0x04,
167 DW_DS_trailing_separate = 0x05
168};
169
170enum EndianityEncoding {
171 // Endianity attribute values
172#define HANDLE_DW_END(ID, NAME) DW_END_##NAME = ID,
173#include "llvm/BinaryFormat/Dwarf.def"
174 DW_END_lo_user = 0x40,
175 DW_END_hi_user = 0xff
176};
177
178enum AccessAttribute {
179 // Accessibility codes
180 DW_ACCESS_public = 0x01,
181 DW_ACCESS_protected = 0x02,
182 DW_ACCESS_private = 0x03
183};
184
185enum VisibilityAttribute {
186 // Visibility codes
187 DW_VIS_local = 0x01,
188 DW_VIS_exported = 0x02,
189 DW_VIS_qualified = 0x03
190};
191
192enum VirtualityAttribute {
193#define HANDLE_DW_VIRTUALITY(ID, NAME) DW_VIRTUALITY_##NAME = ID,
194#include "llvm/BinaryFormat/Dwarf.def"
195 DW_VIRTUALITY_max = 0x02
196};
197
198enum DefaultedMemberAttribute {
199#define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID,
200#include "llvm/BinaryFormat/Dwarf.def"
201 DW_DEFAULTED_max = 0x02
202};
203
204enum SourceLanguage {
205#define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
206 DW_LANG_##NAME = ID,
207#include "llvm/BinaryFormat/Dwarf.def"
208 DW_LANG_lo_user = 0x8000,
209 DW_LANG_hi_user = 0xffff
210};
211
212inline bool isCPlusPlus(SourceLanguage S) {
213 bool result = false;
214 // Deliberately enumerate all the language options so we get a warning when
215 // new language options are added (-Wswitch) that'll hopefully help keep this
216 // switch up-to-date when new C++ versions are added.
217 switch (S) {
218 case DW_LANG_C_plus_plus:
219 case DW_LANG_C_plus_plus_03:
220 case DW_LANG_C_plus_plus_11:
221 case DW_LANG_C_plus_plus_14:
222 case DW_LANG_C_plus_plus_17:
223 case DW_LANG_C_plus_plus_20:
224 result = true;
225 break;
226 case DW_LANG_C89:
227 case DW_LANG_C:
228 case DW_LANG_Ada83:
229 case DW_LANG_Cobol74:
230 case DW_LANG_Cobol85:
231 case DW_LANG_Fortran77:
232 case DW_LANG_Fortran90:
233 case DW_LANG_Pascal83:
234 case DW_LANG_Modula2:
235 case DW_LANG_Java:
236 case DW_LANG_C99:
237 case DW_LANG_Ada95:
238 case DW_LANG_Fortran95:
239 case DW_LANG_PLI:
240 case DW_LANG_ObjC:
241 case DW_LANG_ObjC_plus_plus:
242 case DW_LANG_UPC:
243 case DW_LANG_D:
244 case DW_LANG_Python:
245 case DW_LANG_OpenCL:
246 case DW_LANG_Go:
247 case DW_LANG_Modula3:
248 case DW_LANG_Haskell:
249 case DW_LANG_OCaml:
250 case DW_LANG_Rust:
251 case DW_LANG_C11:
252 case DW_LANG_Swift:
253 case DW_LANG_Julia:
254 case DW_LANG_Dylan:
255 case DW_LANG_Fortran03:
256 case DW_LANG_Fortran08:
257 case DW_LANG_RenderScript:
258 case DW_LANG_BLISS:
259 case DW_LANG_Mips_Assembler:
260 case DW_LANG_GOOGLE_RenderScript:
261 case DW_LANG_BORLAND_Delphi:
262 case DW_LANG_lo_user:
263 case DW_LANG_hi_user:
264 case DW_LANG_Kotlin:
265 case DW_LANG_Zig:
266 case DW_LANG_Crystal:
267 case DW_LANG_C17:
268 case DW_LANG_Fortran18:
269 case DW_LANG_Ada2005:
270 case DW_LANG_Ada2012:
271 case DW_LANG_Mojo:
272 result = false;
273 break;
274 }
275
276 return result;
277}
278
279inline bool isFortran(SourceLanguage S) {
280 bool result = false;
281 // Deliberately enumerate all the language options so we get a warning when
282 // new language options are added (-Wswitch) that'll hopefully help keep this
283 // switch up-to-date when new Fortran versions are added.
284 switch (S) {
285 case DW_LANG_Fortran77:
286 case DW_LANG_Fortran90:
287 case DW_LANG_Fortran95:
288 case DW_LANG_Fortran03:
289 case DW_LANG_Fortran08:
290 case DW_LANG_Fortran18:
291 result = true;
292 break;
293 case DW_LANG_C89:
294 case DW_LANG_C:
295 case DW_LANG_Ada83:
296 case DW_LANG_C_plus_plus:
297 case DW_LANG_Cobol74:
298 case DW_LANG_Cobol85:
299 case DW_LANG_Pascal83:
300 case DW_LANG_Modula2:
301 case DW_LANG_Java:
302 case DW_LANG_C99:
303 case DW_LANG_Ada95:
304 case DW_LANG_PLI:
305 case DW_LANG_ObjC:
306 case DW_LANG_ObjC_plus_plus:
307 case DW_LANG_UPC:
308 case DW_LANG_D:
309 case DW_LANG_Python:
310 case DW_LANG_OpenCL:
311 case DW_LANG_Go:
312 case DW_LANG_Modula3:
313 case DW_LANG_Haskell:
314 case DW_LANG_C_plus_plus_03:
315 case DW_LANG_C_plus_plus_11:
316 case DW_LANG_OCaml:
317 case DW_LANG_Rust:
318 case DW_LANG_C11:
319 case DW_LANG_Swift:
320 case DW_LANG_Julia:
321 case DW_LANG_Dylan:
322 case DW_LANG_C_plus_plus_14:
323 case DW_LANG_RenderScript:
324 case DW_LANG_BLISS:
325 case DW_LANG_Mips_Assembler:
326 case DW_LANG_GOOGLE_RenderScript:
327 case DW_LANG_BORLAND_Delphi:
328 case DW_LANG_lo_user:
329 case DW_LANG_hi_user:
330 case DW_LANG_Kotlin:
331 case DW_LANG_Zig:
332 case DW_LANG_Crystal:
333 case DW_LANG_C_plus_plus_17:
334 case DW_LANG_C_plus_plus_20:
335 case DW_LANG_C17:
336 case DW_LANG_Ada2005:
337 case DW_LANG_Ada2012:
338 case DW_LANG_Mojo:
339 result = false;
340 break;
341 }
342
343 return result;
344}
345
346inline bool isC(SourceLanguage S) {
347 // Deliberately enumerate all the language options so we get a warning when
348 // new language options are added (-Wswitch) that'll hopefully help keep this
349 // switch up-to-date when new C++ versions are added.
350 switch (S) {
351 case DW_LANG_C11:
352 case DW_LANG_C17:
353 case DW_LANG_C89:
354 case DW_LANG_C99:
355 case DW_LANG_C:
356 case DW_LANG_ObjC:
357 return true;
358 case DW_LANG_C_plus_plus:
359 case DW_LANG_C_plus_plus_03:
360 case DW_LANG_C_plus_plus_11:
361 case DW_LANG_C_plus_plus_14:
362 case DW_LANG_C_plus_plus_17:
363 case DW_LANG_C_plus_plus_20:
364 case DW_LANG_Ada83:
365 case DW_LANG_Cobol74:
366 case DW_LANG_Cobol85:
367 case DW_LANG_Fortran77:
368 case DW_LANG_Fortran90:
369 case DW_LANG_Pascal83:
370 case DW_LANG_Modula2:
371 case DW_LANG_Java:
372 case DW_LANG_Ada95:
373 case DW_LANG_Fortran95:
374 case DW_LANG_PLI:
375 case DW_LANG_ObjC_plus_plus:
376 case DW_LANG_UPC:
377 case DW_LANG_D:
378 case DW_LANG_Python:
379 case DW_LANG_OpenCL:
380 case DW_LANG_Go:
381 case DW_LANG_Modula3:
382 case DW_LANG_Haskell:
383 case DW_LANG_OCaml:
384 case DW_LANG_Rust:
385 case DW_LANG_Swift:
386 case DW_LANG_Julia:
387 case DW_LANG_Dylan:
388 case DW_LANG_Fortran03:
389 case DW_LANG_Fortran08:
390 case DW_LANG_RenderScript:
391 case DW_LANG_BLISS:
392 case DW_LANG_Mips_Assembler:
393 case DW_LANG_GOOGLE_RenderScript:
394 case DW_LANG_BORLAND_Delphi:
395 case DW_LANG_lo_user:
396 case DW_LANG_hi_user:
397 case DW_LANG_Kotlin:
398 case DW_LANG_Zig:
399 case DW_LANG_Crystal:
400 case DW_LANG_Fortran18:
401 case DW_LANG_Ada2005:
402 case DW_LANG_Ada2012:
403 case DW_LANG_Mojo:
404 return false;
405 }
406 llvm_unreachable("Unknown language kind.");
407}
408
409inline TypeKind getArrayIndexTypeEncoding(SourceLanguage S) {
410 return isFortran(S) ? DW_ATE_signed : DW_ATE_unsigned;
411}
412
413enum CaseSensitivity {
414 // Identifier case codes
415 DW_ID_case_sensitive = 0x00,
416 DW_ID_up_case = 0x01,
417 DW_ID_down_case = 0x02,
418 DW_ID_case_insensitive = 0x03
419};
420
421enum CallingConvention {
422// Calling convention codes
423#define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID,
424#include "llvm/BinaryFormat/Dwarf.def"
425 DW_CC_lo_user = 0x40,
426 DW_CC_hi_user = 0xff
427};
428
429enum InlineAttribute {
430 // Inline codes
431 DW_INL_not_inlined = 0x00,
432 DW_INL_inlined = 0x01,
433 DW_INL_declared_not_inlined = 0x02,
434 DW_INL_declared_inlined = 0x03
435};
436
437enum ArrayDimensionOrdering {
438 // Array ordering
439 DW_ORD_row_major = 0x00,
440 DW_ORD_col_major = 0x01
441};
442
443enum DiscriminantList {
444 // Discriminant descriptor values
445 DW_DSC_label = 0x00,
446 DW_DSC_range = 0x01
447};
448
449/// Line Number Standard Opcode Encodings.
450enum LineNumberOps : uint8_t {
451#define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID,
452#include "llvm/BinaryFormat/Dwarf.def"
453};
454
455/// Line Number Extended Opcode Encodings.
456enum LineNumberExtendedOps {
457#define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID,
458#include "llvm/BinaryFormat/Dwarf.def"
459 DW_LNE_lo_user = 0x80,
460 DW_LNE_hi_user = 0xff
461};
462
463enum LineNumberEntryFormat {
464#define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID,
465#include "llvm/BinaryFormat/Dwarf.def"
466 DW_LNCT_lo_user = 0x2000,
467 DW_LNCT_hi_user = 0x3fff,
468};
469
470enum MacinfoRecordType {
471 // Macinfo Type Encodings
472 DW_MACINFO_define = 0x01,
473 DW_MACINFO_undef = 0x02,
474 DW_MACINFO_start_file = 0x03,
475 DW_MACINFO_end_file = 0x04,
476 DW_MACINFO_vendor_ext = 0xff
477};
478
479/// DWARF v5 macro information entry type encodings.
480enum MacroEntryType {
481#define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID,
482#include "llvm/BinaryFormat/Dwarf.def"
483 DW_MACRO_lo_user = 0xe0,
484 DW_MACRO_hi_user = 0xff
485};
486
487/// GNU .debug_macro macro information entry type encodings.
488enum GnuMacroEntryType {
489#define HANDLE_DW_MACRO_GNU(ID, NAME) DW_MACRO_GNU_##NAME = ID,
490#include "llvm/BinaryFormat/Dwarf.def"
491 DW_MACRO_GNU_lo_user = 0xe0,
492 DW_MACRO_GNU_hi_user = 0xff
493};
494
495/// DWARF v5 range list entry encoding values.
496enum RnglistEntries {
497#define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID,
498#include "llvm/BinaryFormat/Dwarf.def"
499};
500
501/// DWARF v5 loc list entry encoding values.
502enum LoclistEntries {
503#define HANDLE_DW_LLE(ID, NAME) DW_LLE_##NAME = ID,
504#include "llvm/BinaryFormat/Dwarf.def"
505};
506
507/// Call frame instruction encodings.
508enum CallFrameInfo {
509#define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID,
510#define HANDLE_DW_CFA_PRED(ID, NAME, ARCH) DW_CFA_##NAME = ID,
511#include "llvm/BinaryFormat/Dwarf.def"
512 DW_CFA_extended = 0x00,
513
514 DW_CFA_lo_user = 0x1c,
515 DW_CFA_hi_user = 0x3f
516};
517
518enum Constants {
519 // Children flag
520 DW_CHILDREN_no = 0x00,
521 DW_CHILDREN_yes = 0x01,
522
523 DW_EH_PE_absptr = 0x00,
524 DW_EH_PE_omit = 0xff,
525 DW_EH_PE_uleb128 = 0x01,
526 DW_EH_PE_udata2 = 0x02,
527 DW_EH_PE_udata4 = 0x03,
528 DW_EH_PE_udata8 = 0x04,
529 DW_EH_PE_sleb128 = 0x09,
530 DW_EH_PE_sdata2 = 0x0A,
531 DW_EH_PE_sdata4 = 0x0B,
532 DW_EH_PE_sdata8 = 0x0C,
533 DW_EH_PE_signed = 0x08,
534 DW_EH_PE_pcrel = 0x10,
535 DW_EH_PE_textrel = 0x20,
536 DW_EH_PE_datarel = 0x30,
537 DW_EH_PE_funcrel = 0x40,
538 DW_EH_PE_aligned = 0x50,
539 DW_EH_PE_indirect = 0x80
540};
541
542/// Constants for the DW_APPLE_PROPERTY_attributes attribute.
543/// Keep this list in sync with clang's DeclObjCCommon.h
544/// ObjCPropertyAttribute::Kind!
545enum ApplePropertyAttributes {
546#define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
547#include "llvm/BinaryFormat/Dwarf.def"
548};
549
550/// Constants for unit types in DWARF v5.
551enum UnitType : unsigned char {
552#define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
553#include "llvm/BinaryFormat/Dwarf.def"
554 DW_UT_lo_user = 0x80,
555 DW_UT_hi_user = 0xff
556};
557
558enum Index {
559#define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID,
560#include "llvm/BinaryFormat/Dwarf.def"
561 DW_IDX_lo_user = 0x2000,
562 DW_IDX_hi_user = 0x3fff
563};
564
565inline bool isUnitType(uint8_t UnitType) {
566 switch (UnitType) {
567 case DW_UT_compile:
568 case DW_UT_type:
569 case DW_UT_partial:
570 case DW_UT_skeleton:
571 case DW_UT_split_compile:
572 case DW_UT_split_type:
573 return true;
574 default:
575 return false;
576 }
577}
578
579inline bool isUnitType(dwarf::Tag T) {
580 switch (T) {
581 case DW_TAG_compile_unit:
582 case DW_TAG_type_unit:
583 case DW_TAG_partial_unit:
584 case DW_TAG_skeleton_unit:
585 return true;
586 default:
587 return false;
588 }
589}
590
591// Constants for the DWARF v5 Accelerator Table Proposal
592enum AcceleratorTable {
593 // Data layout descriptors.
594 DW_ATOM_null = 0u, /// Marker as the end of a list of atoms.
595 DW_ATOM_die_offset = 1u, // DIE offset in the debug_info section.
596 DW_ATOM_cu_offset = 2u, // Offset of the compile unit header that contains the
597 // item in question.
598 DW_ATOM_die_tag = 3u, // A tag entry.
599 DW_ATOM_type_flags = 4u, // Set of flags for a type.
600
601 DW_ATOM_type_type_flags = 5u, // Dsymutil type extension.
602 DW_ATOM_qual_name_hash = 6u, // Dsymutil qualified hash extension.
603
604 // DW_ATOM_type_flags values.
605
606 // Always set for C++, only set for ObjC if this is the @implementation for a
607 // class.
608 DW_FLAG_type_implementation = 2u,
609
610 // Hash functions.
611
612 // Daniel J. Bernstein hash.
613 DW_hash_function_djb = 0u
614};
615
616// Return a suggested bucket count for the DWARF v5 Accelerator Table.
617inline uint32_t getDebugNamesBucketCount(uint32_t UniqueHashCount) {
618 if (UniqueHashCount > 1024)
619 return UniqueHashCount / 4;
620 if (UniqueHashCount > 16)
621 return UniqueHashCount / 2;
622 return std::max<uint32_t>(a: UniqueHashCount, b: 1);
623}
624
625// Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
626enum GDBIndexEntryKind {
627 GIEK_NONE,
628 GIEK_TYPE,
629 GIEK_VARIABLE,
630 GIEK_FUNCTION,
631 GIEK_OTHER,
632 GIEK_UNUSED5,
633 GIEK_UNUSED6,
634 GIEK_UNUSED7
635};
636
637enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC };
638
639/// \defgroup DwarfConstantsDumping Dwarf constants dumping functions
640///
641/// All these functions map their argument's value back to the
642/// corresponding enumerator name or return an empty StringRef if the value
643/// isn't known.
644///
645/// @{
646StringRef TagString(unsigned Tag);
647StringRef ChildrenString(unsigned Children);
648StringRef AttributeString(unsigned Attribute);
649StringRef FormEncodingString(unsigned Encoding);
650StringRef OperationEncodingString(unsigned Encoding);
651StringRef SubOperationEncodingString(unsigned OpEncoding,
652 unsigned SubOpEncoding);
653StringRef AttributeEncodingString(unsigned Encoding);
654StringRef DecimalSignString(unsigned Sign);
655StringRef EndianityString(unsigned Endian);
656StringRef AccessibilityString(unsigned Access);
657StringRef DefaultedMemberString(unsigned DefaultedEncodings);
658StringRef VisibilityString(unsigned Visibility);
659StringRef VirtualityString(unsigned Virtuality);
660StringRef LanguageString(unsigned Language);
661StringRef CaseString(unsigned Case);
662StringRef ConventionString(unsigned Convention);
663StringRef InlineCodeString(unsigned Code);
664StringRef ArrayOrderString(unsigned Order);
665StringRef LNStandardString(unsigned Standard);
666StringRef LNExtendedString(unsigned Encoding);
667StringRef MacinfoString(unsigned Encoding);
668StringRef MacroString(unsigned Encoding);
669StringRef GnuMacroString(unsigned Encoding);
670StringRef RangeListEncodingString(unsigned Encoding);
671StringRef LocListEncodingString(unsigned Encoding);
672StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch);
673StringRef ApplePropertyString(unsigned);
674StringRef UnitTypeString(unsigned);
675StringRef AtomTypeString(unsigned Atom);
676StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind);
677StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage);
678StringRef IndexString(unsigned Idx);
679StringRef FormatString(DwarfFormat Format);
680StringRef FormatString(bool IsDWARF64);
681StringRef RLEString(unsigned RLE);
682/// @}
683
684/// \defgroup DwarfConstantsParsing Dwarf constants parsing functions
685///
686/// These functions map their strings back to the corresponding enumeration
687/// value or return 0 if there is none, except for these exceptions:
688///
689/// \li \a getTag() returns \a DW_TAG_invalid on invalid input.
690/// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input.
691/// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input.
692///
693/// @{
694unsigned getTag(StringRef TagString);
695unsigned getOperationEncoding(StringRef OperationEncodingString);
696unsigned getSubOperationEncoding(unsigned OpEncoding,
697 StringRef SubOperationEncodingString);
698unsigned getVirtuality(StringRef VirtualityString);
699unsigned getLanguage(StringRef LanguageString);
700unsigned getCallingConvention(StringRef LanguageString);
701unsigned getAttributeEncoding(StringRef EncodingString);
702unsigned getMacinfo(StringRef MacinfoString);
703unsigned getMacro(StringRef MacroString);
704/// @}
705
706/// \defgroup DwarfConstantsVersioning Dwarf version for constants
707///
708/// For constants defined by DWARF, returns the DWARF version when the constant
709/// was first defined. For vendor extensions, if there is a version-related
710/// policy for when to emit it, returns a version number for that policy.
711/// Otherwise returns 0.
712///
713/// @{
714unsigned TagVersion(Tag T);
715unsigned AttributeVersion(Attribute A);
716unsigned FormVersion(Form F);
717unsigned OperationVersion(LocationAtom O);
718unsigned AttributeEncodingVersion(TypeKind E);
719unsigned LanguageVersion(SourceLanguage L);
720/// @}
721
722/// \defgroup DwarfConstantsVendor Dwarf "vendor" for constants
723///
724/// These functions return an identifier describing "who" defined the constant,
725/// either the DWARF standard itself or the vendor who defined the extension.
726///
727/// @{
728unsigned TagVendor(Tag T);
729unsigned AttributeVendor(Attribute A);
730unsigned FormVendor(Form F);
731unsigned OperationVendor(LocationAtom O);
732unsigned AttributeEncodingVendor(TypeKind E);
733unsigned LanguageVendor(SourceLanguage L);
734/// @}
735
736std::optional<unsigned> LanguageLowerBound(SourceLanguage L);
737
738/// The size of a reference determined by the DWARF 32/64-bit format.
739inline uint8_t getDwarfOffsetByteSize(DwarfFormat Format) {
740 switch (Format) {
741 case DwarfFormat::DWARF32:
742 return 4;
743 case DwarfFormat::DWARF64:
744 return 8;
745 }
746 llvm_unreachable("Invalid Format value");
747}
748
749/// A helper struct providing information about the byte size of DW_FORM
750/// values that vary in size depending on the DWARF version, address byte
751/// size, or DWARF32/DWARF64.
752struct FormParams {
753 uint16_t Version;
754 uint8_t AddrSize;
755 DwarfFormat Format;
756 /// True if DWARF v2 output generally uses relocations for references
757 /// to other .debug_* sections.
758 bool DwarfUsesRelocationsAcrossSections = false;
759
760 /// The definition of the size of form DW_FORM_ref_addr depends on the
761 /// version. In DWARF v2 it's the size of an address; after that, it's the
762 /// size of a reference.
763 uint8_t getRefAddrByteSize() const {
764 if (Version == 2)
765 return AddrSize;
766 return getDwarfOffsetByteSize();
767 }
768
769 /// The size of a reference is determined by the DWARF 32/64-bit format.
770 uint8_t getDwarfOffsetByteSize() const {
771 return dwarf::getDwarfOffsetByteSize(Format);
772 }
773
774 explicit operator bool() const { return Version && AddrSize; }
775};
776
777/// Get the byte size of the unit length field depending on the DWARF format.
778inline uint8_t getUnitLengthFieldByteSize(DwarfFormat Format) {
779 switch (Format) {
780 case DwarfFormat::DWARF32:
781 return 4;
782 case DwarfFormat::DWARF64:
783 return 12;
784 }
785 llvm_unreachable("Invalid Format value");
786}
787
788/// Get the fixed byte size for a given form.
789///
790/// If the form has a fixed byte size, then an Optional with a value will be
791/// returned. If the form is always encoded using a variable length storage
792/// format (ULEB or SLEB numbers or blocks) then std::nullopt will be returned.
793///
794/// \param Form DWARF form to get the fixed byte size for.
795/// \param Params DWARF parameters to help interpret forms.
796/// \returns std::optional<uint8_t> value with the fixed byte size or
797/// std::nullopt if \p Form doesn't have a fixed byte size.
798std::optional<uint8_t> getFixedFormByteSize(dwarf::Form Form,
799 FormParams Params);
800
801/// Tells whether the specified form is defined in the specified version,
802/// or is an extension if extensions are allowed.
803bool isValidFormForVersion(Form F, unsigned Version, bool ExtensionsOk = true);
804
805/// Returns the symbolic string representing Val when used as a value
806/// for attribute Attr.
807StringRef AttributeValueString(uint16_t Attr, unsigned Val);
808
809/// Returns the symbolic string representing Val when used as a value
810/// for atom Atom.
811StringRef AtomValueString(uint16_t Atom, unsigned Val);
812
813/// Describes an entry of the various gnu_pub* debug sections.
814///
815/// The gnu_pub* kind looks like:
816///
817/// 0-3 reserved
818/// 4-6 symbol kind
819/// 7 0 == global, 1 == static
820///
821/// A gdb_index descriptor includes the above kind, shifted 24 bits up with the
822/// offset of the cu within the debug_info section stored in those 24 bits.
823struct PubIndexEntryDescriptor {
824 GDBIndexEntryKind Kind;
825 GDBIndexEntryLinkage Linkage;
826 PubIndexEntryDescriptor(GDBIndexEntryKind Kind, GDBIndexEntryLinkage Linkage)
827 : Kind(Kind), Linkage(Linkage) {}
828 /* implicit */ PubIndexEntryDescriptor(GDBIndexEntryKind Kind)
829 : Kind(Kind), Linkage(GIEL_EXTERNAL) {}
830 explicit PubIndexEntryDescriptor(uint8_t Value)
831 : Kind(
832 static_cast<GDBIndexEntryKind>((Value & KIND_MASK) >> KIND_OFFSET)),
833 Linkage(static_cast<GDBIndexEntryLinkage>((Value & LINKAGE_MASK) >>
834 LINKAGE_OFFSET)) {}
835 uint8_t toBits() const {
836 return Kind << KIND_OFFSET | Linkage << LINKAGE_OFFSET;
837 }
838
839private:
840 enum {
841 KIND_OFFSET = 4,
842 KIND_MASK = 7 << KIND_OFFSET,
843 LINKAGE_OFFSET = 7,
844 LINKAGE_MASK = 1 << LINKAGE_OFFSET
845 };
846};
847
848template <typename Enum> struct EnumTraits : public std::false_type {};
849
850template <> struct EnumTraits<Attribute> : public std::true_type {
851 static constexpr char Type[3] = "AT";
852 static constexpr StringRef (*StringFn)(unsigned) = &AttributeString;
853};
854
855template <> struct EnumTraits<Form> : public std::true_type {
856 static constexpr char Type[5] = "FORM";
857 static constexpr StringRef (*StringFn)(unsigned) = &FormEncodingString;
858};
859
860template <> struct EnumTraits<Index> : public std::true_type {
861 static constexpr char Type[4] = "IDX";
862 static constexpr StringRef (*StringFn)(unsigned) = &IndexString;
863};
864
865template <> struct EnumTraits<Tag> : public std::true_type {
866 static constexpr char Type[4] = "TAG";
867 static constexpr StringRef (*StringFn)(unsigned) = &TagString;
868};
869
870template <> struct EnumTraits<LineNumberOps> : public std::true_type {
871 static constexpr char Type[4] = "LNS";
872 static constexpr StringRef (*StringFn)(unsigned) = &LNStandardString;
873};
874
875template <> struct EnumTraits<LocationAtom> : public std::true_type {
876 static constexpr char Type[3] = "OP";
877 static constexpr StringRef (*StringFn)(unsigned) = &OperationEncodingString;
878};
879
880inline uint64_t computeTombstoneAddress(uint8_t AddressByteSize) {
881 return std::numeric_limits<uint64_t>::max() >> (8 - AddressByteSize) * 8;
882}
883
884} // End of namespace dwarf
885
886/// Dwarf constants format_provider
887///
888/// Specialization of the format_provider template for dwarf enums. Unlike the
889/// dumping functions above, these format unknown enumerator values as
890/// DW_TYPE_unknown_1234 (e.g. DW_TAG_unknown_ffff).
891template <typename Enum>
892struct format_provider<Enum, std::enable_if_t<dwarf::EnumTraits<Enum>::value>> {
893 static void format(const Enum &E, raw_ostream &OS, StringRef Style) {
894 StringRef Str = dwarf::EnumTraits<Enum>::StringFn(E);
895 if (Str.empty()) {
896 OS << "DW_" << dwarf::EnumTraits<Enum>::Type << "_unknown_"
897 << llvm::format("%x", E);
898 } else
899 OS << Str;
900 }
901};
902} // End of namespace llvm
903
904#endif
905

source code of llvm/include/llvm/BinaryFormat/Dwarf.h