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