xref: /freebsd/contrib/llvm-project/llvm/lib/BinaryFormat/Dwarf.cpp (revision c57c26179033f64c2011a2d2a904ee3fa62e826a)
1 //===-- llvm/BinaryFormat/Dwarf.cpp - Dwarf Framework ------------*- 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 // This file contains support for generic dwarf information.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/TargetParser/Triple.h"
17 
18 using namespace llvm;
19 using namespace dwarf;
20 
21 StringRef llvm::dwarf::TagString(unsigned Tag) {
22   switch (Tag) {
23   default:
24     return StringRef();
25 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND)                         \
26   case DW_TAG_##NAME:                                                          \
27     return "DW_TAG_" #NAME;
28 #include "llvm/BinaryFormat/Dwarf.def"
29   }
30 }
31 
32 unsigned llvm::dwarf::getTag(StringRef TagString) {
33   return StringSwitch<unsigned>(TagString)
34 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND)                         \
35   .Case("DW_TAG_" #NAME, DW_TAG_##NAME)
36 #include "llvm/BinaryFormat/Dwarf.def"
37       .Default(DW_TAG_invalid);
38 }
39 
40 unsigned llvm::dwarf::TagVersion(dwarf::Tag Tag) {
41   switch (Tag) {
42   default:
43     return 0;
44 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND)                         \
45   case DW_TAG_##NAME:                                                          \
46     return VERSION;
47 #include "llvm/BinaryFormat/Dwarf.def"
48   }
49 }
50 
51 unsigned llvm::dwarf::TagVendor(dwarf::Tag Tag) {
52   switch (Tag) {
53   default:
54     return 0;
55 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND)                         \
56   case DW_TAG_##NAME:                                                          \
57     return DWARF_VENDOR_##VENDOR;
58 #include "llvm/BinaryFormat/Dwarf.def"
59   }
60 }
61 
62 StringRef llvm::dwarf::ChildrenString(unsigned Children) {
63   switch (Children) {
64   case DW_CHILDREN_no:
65     return "DW_CHILDREN_no";
66   case DW_CHILDREN_yes:
67     return "DW_CHILDREN_yes";
68   }
69   return StringRef();
70 }
71 
72 StringRef llvm::dwarf::AttributeString(unsigned Attribute) {
73   switch (Attribute) {
74   default:
75     return StringRef();
76 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR)                                \
77   case DW_AT_##NAME:                                                           \
78     return "DW_AT_" #NAME;
79 #include "llvm/BinaryFormat/Dwarf.def"
80   }
81 }
82 
83 unsigned llvm::dwarf::AttributeVersion(dwarf::Attribute Attribute) {
84   switch (Attribute) {
85   default:
86     return 0;
87 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR)                                \
88   case DW_AT_##NAME:                                                           \
89     return VERSION;
90 #include "llvm/BinaryFormat/Dwarf.def"
91   }
92 }
93 
94 unsigned llvm::dwarf::AttributeVendor(dwarf::Attribute Attribute) {
95   switch (Attribute) {
96   default:
97     return 0;
98 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR)                                \
99   case DW_AT_##NAME:                                                           \
100     return DWARF_VENDOR_##VENDOR;
101 #include "llvm/BinaryFormat/Dwarf.def"
102   }
103 }
104 
105 StringRef llvm::dwarf::FormEncodingString(unsigned Encoding) {
106   switch (Encoding) {
107   default:
108     return StringRef();
109 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR)                              \
110   case DW_FORM_##NAME:                                                         \
111     return "DW_FORM_" #NAME;
112 #include "llvm/BinaryFormat/Dwarf.def"
113   }
114 }
115 
116 unsigned llvm::dwarf::FormVersion(dwarf::Form Form) {
117   switch (Form) {
118   default:
119     return 0;
120 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR)                              \
121   case DW_FORM_##NAME:                                                         \
122     return VERSION;
123 #include "llvm/BinaryFormat/Dwarf.def"
124   }
125 }
126 
127 unsigned llvm::dwarf::FormVendor(dwarf::Form Form) {
128   switch (Form) {
129   default:
130     return 0;
131 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR)                              \
132   case DW_FORM_##NAME:                                                         \
133     return DWARF_VENDOR_##VENDOR;
134 #include "llvm/BinaryFormat/Dwarf.def"
135   }
136 }
137 
138 StringRef llvm::dwarf::OperationEncodingString(unsigned Encoding) {
139   switch (Encoding) {
140   default:
141     return StringRef();
142 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR)                                \
143   case DW_OP_##NAME:                                                           \
144     return "DW_OP_" #NAME;
145 #include "llvm/BinaryFormat/Dwarf.def"
146   case DW_OP_LLVM_convert:
147     return "DW_OP_LLVM_convert";
148   case DW_OP_LLVM_fragment:
149     return "DW_OP_LLVM_fragment";
150   case DW_OP_LLVM_tag_offset:
151     return "DW_OP_LLVM_tag_offset";
152   case DW_OP_LLVM_entry_value:
153     return "DW_OP_LLVM_entry_value";
154   case DW_OP_LLVM_implicit_pointer:
155     return "DW_OP_LLVM_implicit_pointer";
156   case DW_OP_LLVM_arg:
157     return "DW_OP_LLVM_arg";
158   }
159 }
160 
161 unsigned llvm::dwarf::getOperationEncoding(StringRef OperationEncodingString) {
162   return StringSwitch<unsigned>(OperationEncodingString)
163 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR)                                \
164   .Case("DW_OP_" #NAME, DW_OP_##NAME)
165 #include "llvm/BinaryFormat/Dwarf.def"
166       .Case("DW_OP_LLVM_convert", DW_OP_LLVM_convert)
167       .Case("DW_OP_LLVM_fragment", DW_OP_LLVM_fragment)
168       .Case("DW_OP_LLVM_tag_offset", DW_OP_LLVM_tag_offset)
169       .Case("DW_OP_LLVM_entry_value", DW_OP_LLVM_entry_value)
170       .Case("DW_OP_LLVM_implicit_pointer", DW_OP_LLVM_implicit_pointer)
171       .Case("DW_OP_LLVM_arg", DW_OP_LLVM_arg)
172       .Default(0);
173 }
174 
175 static StringRef LlvmUserOperationEncodingString(unsigned Encoding) {
176   switch (Encoding) {
177   default:
178     llvm_unreachable("unhandled DWARF operation with LLVM user op");
179 #define HANDLE_DW_OP_LLVM_USEROP(ID, NAME)                                     \
180   case DW_OP_LLVM_##NAME:                                                      \
181     return "DW_OP_LLVM_" #NAME;
182 #include "llvm/BinaryFormat/Dwarf.def"
183   }
184 }
185 
186 static unsigned
187 getLlvmUserOperationEncoding(StringRef LlvmUserOperationEncodingString) {
188   unsigned E = StringSwitch<unsigned>(LlvmUserOperationEncodingString)
189 #define HANDLE_DW_OP_LLVM_USEROP(ID, NAME) .Case(#NAME, DW_OP_LLVM_##NAME)
190 #include "llvm/BinaryFormat/Dwarf.def"
191                    .Default(0);
192   assert(E && "unhandled DWARF operation string with LLVM user op");
193   return E;
194 }
195 
196 StringRef llvm::dwarf::SubOperationEncodingString(unsigned OpEncoding,
197                                                   unsigned SubOpEncoding) {
198   assert(OpEncoding == DW_OP_LLVM_user);
199   return LlvmUserOperationEncodingString(SubOpEncoding);
200 }
201 
202 unsigned
203 llvm::dwarf::getSubOperationEncoding(unsigned OpEncoding,
204                                      StringRef SubOperationEncodingString) {
205   assert(OpEncoding == DW_OP_LLVM_user);
206   return getLlvmUserOperationEncoding(SubOperationEncodingString);
207 }
208 
209 unsigned llvm::dwarf::OperationVersion(dwarf::LocationAtom Op) {
210   switch (Op) {
211   default:
212     return 0;
213 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR)                                \
214   case DW_OP_##NAME:                                                           \
215     return VERSION;
216 #include "llvm/BinaryFormat/Dwarf.def"
217   }
218 }
219 
220 unsigned llvm::dwarf::OperationVendor(dwarf::LocationAtom Op) {
221   switch (Op) {
222   default:
223     return 0;
224 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR)                                \
225   case DW_OP_##NAME:                                                           \
226     return DWARF_VENDOR_##VENDOR;
227 #include "llvm/BinaryFormat/Dwarf.def"
228   }
229 }
230 
231 StringRef llvm::dwarf::AttributeEncodingString(unsigned Encoding) {
232   switch (Encoding) {
233   default:
234     return StringRef();
235 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR)                               \
236   case DW_ATE_##NAME:                                                          \
237     return "DW_ATE_" #NAME;
238 #include "llvm/BinaryFormat/Dwarf.def"
239   }
240 }
241 
242 unsigned llvm::dwarf::getAttributeEncoding(StringRef EncodingString) {
243   return StringSwitch<unsigned>(EncodingString)
244 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR)                               \
245   .Case("DW_ATE_" #NAME, DW_ATE_##NAME)
246 #include "llvm/BinaryFormat/Dwarf.def"
247       .Default(0);
248 }
249 
250 unsigned llvm::dwarf::AttributeEncodingVersion(dwarf::TypeKind ATE) {
251   switch (ATE) {
252   default:
253     return 0;
254 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR)                               \
255   case DW_ATE_##NAME:                                                          \
256     return VERSION;
257 #include "llvm/BinaryFormat/Dwarf.def"
258   }
259 }
260 
261 unsigned llvm::dwarf::AttributeEncodingVendor(dwarf::TypeKind ATE) {
262   switch (ATE) {
263   default:
264     return 0;
265 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR)                               \
266   case DW_ATE_##NAME:                                                          \
267     return DWARF_VENDOR_##VENDOR;
268 #include "llvm/BinaryFormat/Dwarf.def"
269   }
270 }
271 
272 StringRef llvm::dwarf::DecimalSignString(unsigned Sign) {
273   switch (Sign) {
274   case DW_DS_unsigned:
275     return "DW_DS_unsigned";
276   case DW_DS_leading_overpunch:
277     return "DW_DS_leading_overpunch";
278   case DW_DS_trailing_overpunch:
279     return "DW_DS_trailing_overpunch";
280   case DW_DS_leading_separate:
281     return "DW_DS_leading_separate";
282   case DW_DS_trailing_separate:
283     return "DW_DS_trailing_separate";
284   }
285   return StringRef();
286 }
287 
288 StringRef llvm::dwarf::EndianityString(unsigned Endian) {
289   switch (Endian) {
290   case DW_END_default:
291     return "DW_END_default";
292   case DW_END_big:
293     return "DW_END_big";
294   case DW_END_little:
295     return "DW_END_little";
296   case DW_END_lo_user:
297     return "DW_END_lo_user";
298   case DW_END_hi_user:
299     return "DW_END_hi_user";
300   }
301   return StringRef();
302 }
303 
304 StringRef llvm::dwarf::AccessibilityString(unsigned Access) {
305   switch (Access) {
306   // Accessibility codes
307   case DW_ACCESS_public:
308     return "DW_ACCESS_public";
309   case DW_ACCESS_protected:
310     return "DW_ACCESS_protected";
311   case DW_ACCESS_private:
312     return "DW_ACCESS_private";
313   }
314   return StringRef();
315 }
316 
317 StringRef llvm::dwarf::DefaultedMemberString(unsigned DefaultedEncodings) {
318   switch (DefaultedEncodings) {
319   // Defaulted Member Encodings codes
320   case DW_DEFAULTED_no:
321     return "DW_DEFAULTED_no";
322   case DW_DEFAULTED_in_class:
323     return "DW_DEFAULTED_in_class";
324   case DW_DEFAULTED_out_of_class:
325     return "DW_DEFAULTED_out_of_class";
326   }
327   return StringRef();
328 }
329 
330 StringRef llvm::dwarf::VisibilityString(unsigned Visibility) {
331   switch (Visibility) {
332   case DW_VIS_local:
333     return "DW_VIS_local";
334   case DW_VIS_exported:
335     return "DW_VIS_exported";
336   case DW_VIS_qualified:
337     return "DW_VIS_qualified";
338   }
339   return StringRef();
340 }
341 
342 StringRef llvm::dwarf::VirtualityString(unsigned Virtuality) {
343   switch (Virtuality) {
344   default:
345     return StringRef();
346 #define HANDLE_DW_VIRTUALITY(ID, NAME)                                         \
347   case DW_VIRTUALITY_##NAME:                                                   \
348     return "DW_VIRTUALITY_" #NAME;
349 #include "llvm/BinaryFormat/Dwarf.def"
350   }
351 }
352 
353 unsigned llvm::dwarf::getVirtuality(StringRef VirtualityString) {
354   return StringSwitch<unsigned>(VirtualityString)
355 #define HANDLE_DW_VIRTUALITY(ID, NAME)                                         \
356   .Case("DW_VIRTUALITY_" #NAME, DW_VIRTUALITY_##NAME)
357 #include "llvm/BinaryFormat/Dwarf.def"
358       .Default(DW_VIRTUALITY_invalid);
359 }
360 
361 StringRef llvm::dwarf::LanguageString(unsigned Language) {
362   switch (Language) {
363   default:
364     return StringRef();
365 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR)                 \
366   case DW_LANG_##NAME:                                                         \
367     return "DW_LANG_" #NAME;
368 #include "llvm/BinaryFormat/Dwarf.def"
369   }
370 }
371 
372 unsigned llvm::dwarf::getLanguage(StringRef LanguageString) {
373   return StringSwitch<unsigned>(LanguageString)
374 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR)                 \
375   .Case("DW_LANG_" #NAME, DW_LANG_##NAME)
376 #include "llvm/BinaryFormat/Dwarf.def"
377       .Default(0);
378 }
379 
380 unsigned llvm::dwarf::LanguageVersion(dwarf::SourceLanguage Lang) {
381   switch (Lang) {
382   default:
383     return 0;
384 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR)                 \
385   case DW_LANG_##NAME:                                                         \
386     return VERSION;
387 #include "llvm/BinaryFormat/Dwarf.def"
388   }
389 }
390 
391 unsigned llvm::dwarf::LanguageVendor(dwarf::SourceLanguage Lang) {
392   switch (Lang) {
393   default:
394     return 0;
395 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR)                 \
396   case DW_LANG_##NAME:                                                         \
397     return DWARF_VENDOR_##VENDOR;
398 #include "llvm/BinaryFormat/Dwarf.def"
399   }
400 }
401 
402 std::optional<unsigned>
403 llvm::dwarf::LanguageLowerBound(dwarf::SourceLanguage Lang) {
404   switch (Lang) {
405   default:
406     return std::nullopt;
407 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR)                 \
408   case DW_LANG_##NAME:                                                         \
409     return LOWER_BOUND;
410 #include "llvm/BinaryFormat/Dwarf.def"
411   }
412 }
413 
414 StringRef llvm::dwarf::CaseString(unsigned Case) {
415   switch (Case) {
416   case DW_ID_case_sensitive:
417     return "DW_ID_case_sensitive";
418   case DW_ID_up_case:
419     return "DW_ID_up_case";
420   case DW_ID_down_case:
421     return "DW_ID_down_case";
422   case DW_ID_case_insensitive:
423     return "DW_ID_case_insensitive";
424   }
425   return StringRef();
426 }
427 
428 StringRef llvm::dwarf::ConventionString(unsigned CC) {
429   switch (CC) {
430   default:
431     return StringRef();
432 #define HANDLE_DW_CC(ID, NAME)                                                 \
433   case DW_CC_##NAME:                                                           \
434     return "DW_CC_" #NAME;
435 #include "llvm/BinaryFormat/Dwarf.def"
436   }
437 }
438 
439 unsigned llvm::dwarf::getCallingConvention(StringRef CCString) {
440   return StringSwitch<unsigned>(CCString)
441 #define HANDLE_DW_CC(ID, NAME) .Case("DW_CC_" #NAME, DW_CC_##NAME)
442 #include "llvm/BinaryFormat/Dwarf.def"
443       .Default(0);
444 }
445 
446 StringRef llvm::dwarf::InlineCodeString(unsigned Code) {
447   switch (Code) {
448   case DW_INL_not_inlined:
449     return "DW_INL_not_inlined";
450   case DW_INL_inlined:
451     return "DW_INL_inlined";
452   case DW_INL_declared_not_inlined:
453     return "DW_INL_declared_not_inlined";
454   case DW_INL_declared_inlined:
455     return "DW_INL_declared_inlined";
456   }
457   return StringRef();
458 }
459 
460 StringRef llvm::dwarf::ArrayOrderString(unsigned Order) {
461   switch (Order) {
462   case DW_ORD_row_major:
463     return "DW_ORD_row_major";
464   case DW_ORD_col_major:
465     return "DW_ORD_col_major";
466   }
467   return StringRef();
468 }
469 
470 StringRef llvm::dwarf::LNStandardString(unsigned Standard) {
471   switch (Standard) {
472   default:
473     return StringRef();
474 #define HANDLE_DW_LNS(ID, NAME)                                                \
475   case DW_LNS_##NAME:                                                          \
476     return "DW_LNS_" #NAME;
477 #include "llvm/BinaryFormat/Dwarf.def"
478   }
479 }
480 
481 StringRef llvm::dwarf::LNExtendedString(unsigned Encoding) {
482   switch (Encoding) {
483   default:
484     return StringRef();
485 #define HANDLE_DW_LNE(ID, NAME)                                                \
486   case DW_LNE_##NAME:                                                          \
487     return "DW_LNE_" #NAME;
488 #include "llvm/BinaryFormat/Dwarf.def"
489   }
490 }
491 
492 StringRef llvm::dwarf::MacinfoString(unsigned Encoding) {
493   switch (Encoding) {
494   // Macinfo Type Encodings
495   case DW_MACINFO_define:
496     return "DW_MACINFO_define";
497   case DW_MACINFO_undef:
498     return "DW_MACINFO_undef";
499   case DW_MACINFO_start_file:
500     return "DW_MACINFO_start_file";
501   case DW_MACINFO_end_file:
502     return "DW_MACINFO_end_file";
503   case DW_MACINFO_vendor_ext:
504     return "DW_MACINFO_vendor_ext";
505   case DW_MACINFO_invalid:
506     return "DW_MACINFO_invalid";
507   }
508   return StringRef();
509 }
510 
511 unsigned llvm::dwarf::getMacinfo(StringRef MacinfoString) {
512   return StringSwitch<unsigned>(MacinfoString)
513       .Case("DW_MACINFO_define", DW_MACINFO_define)
514       .Case("DW_MACINFO_undef", DW_MACINFO_undef)
515       .Case("DW_MACINFO_start_file", DW_MACINFO_start_file)
516       .Case("DW_MACINFO_end_file", DW_MACINFO_end_file)
517       .Case("DW_MACINFO_vendor_ext", DW_MACINFO_vendor_ext)
518       .Default(DW_MACINFO_invalid);
519 }
520 
521 StringRef llvm::dwarf::MacroString(unsigned Encoding) {
522   switch (Encoding) {
523   default:
524     return StringRef();
525 #define HANDLE_DW_MACRO(ID, NAME)                                              \
526   case DW_MACRO_##NAME:                                                        \
527     return "DW_MACRO_" #NAME;
528 #include "llvm/BinaryFormat/Dwarf.def"
529   }
530 }
531 
532 StringRef llvm::dwarf::GnuMacroString(unsigned Encoding) {
533   switch (Encoding) {
534   default:
535     return StringRef();
536 #define HANDLE_DW_MACRO_GNU(ID, NAME)                                          \
537   case DW_MACRO_GNU_##NAME:                                                    \
538     return "DW_MACRO_GNU_" #NAME;
539 #include "llvm/BinaryFormat/Dwarf.def"
540   }
541 }
542 
543 unsigned llvm::dwarf::getMacro(StringRef MacroString) {
544   return StringSwitch<unsigned>(MacroString)
545 #define HANDLE_DW_MACRO(ID, NAME) .Case("DW_MACRO_" #NAME, ID)
546 #include "llvm/BinaryFormat/Dwarf.def"
547       .Default(DW_MACINFO_invalid);
548 }
549 StringRef llvm::dwarf::RangeListEncodingString(unsigned Encoding) {
550   switch (Encoding) {
551   default:
552     return StringRef();
553 #define HANDLE_DW_RLE(ID, NAME)                                                \
554   case DW_RLE_##NAME:                                                          \
555     return "DW_RLE_" #NAME;
556 #include "llvm/BinaryFormat/Dwarf.def"
557   }
558 }
559 
560 StringRef llvm::dwarf::LocListEncodingString(unsigned Encoding) {
561   switch (Encoding) {
562   default:
563     return StringRef();
564 #define HANDLE_DW_LLE(ID, NAME)                                                \
565   case DW_LLE_##NAME:                                                          \
566     return "DW_LLE_" #NAME;
567 #include "llvm/BinaryFormat/Dwarf.def"
568   }
569 }
570 
571 StringRef llvm::dwarf::CallFrameString(unsigned Encoding,
572     Triple::ArchType Arch) {
573   assert(Arch != llvm::Triple::ArchType::UnknownArch);
574 #define SELECT_AARCH64 (Arch == llvm::Triple::aarch64_be || Arch == llvm::Triple::aarch64)
575 #define SELECT_MIPS64 Arch == llvm::Triple::mips64
576 #define SELECT_SPARC (Arch == llvm::Triple::sparc || Arch == llvm::Triple::sparcv9)
577 #define SELECT_X86 (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64)
578 #define HANDLE_DW_CFA(ID, NAME)
579 #define HANDLE_DW_CFA_PRED(ID, NAME, PRED) \
580   if (ID == Encoding && PRED) \
581     return "DW_CFA_" #NAME;
582 #include "llvm/BinaryFormat/Dwarf.def"
583 
584   switch (Encoding) {
585   default:
586     return StringRef();
587 #define HANDLE_DW_CFA_PRED(ID, NAME, PRED)
588 #define HANDLE_DW_CFA(ID, NAME)                                                \
589   case DW_CFA_##NAME:                                                          \
590     return "DW_CFA_" #NAME;
591 #include "llvm/BinaryFormat/Dwarf.def"
592 
593 #undef SELECT_X86
594 #undef SELECT_SPARC
595 #undef SELECT_MIPS64
596 #undef SELECT_AARCH64
597   }
598 }
599 
600 StringRef llvm::dwarf::ApplePropertyString(unsigned Prop) {
601   switch (Prop) {
602   default:
603     return StringRef();
604 #define HANDLE_DW_APPLE_PROPERTY(ID, NAME)                                     \
605   case DW_APPLE_PROPERTY_##NAME:                                               \
606     return "DW_APPLE_PROPERTY_" #NAME;
607 #include "llvm/BinaryFormat/Dwarf.def"
608   }
609 }
610 
611 StringRef llvm::dwarf::UnitTypeString(unsigned UT) {
612   switch (UT) {
613   default:
614     return StringRef();
615 #define HANDLE_DW_UT(ID, NAME)                                                 \
616   case DW_UT_##NAME:                                                           \
617     return "DW_UT_" #NAME;
618 #include "llvm/BinaryFormat/Dwarf.def"
619   }
620 }
621 
622 StringRef llvm::dwarf::AtomTypeString(unsigned AT) {
623   switch (AT) {
624   case dwarf::DW_ATOM_null:
625     return "DW_ATOM_null";
626   case dwarf::DW_ATOM_die_offset:
627     return "DW_ATOM_die_offset";
628   case DW_ATOM_cu_offset:
629     return "DW_ATOM_cu_offset";
630   case DW_ATOM_die_tag:
631     return "DW_ATOM_die_tag";
632   case DW_ATOM_type_flags:
633   case DW_ATOM_type_type_flags:
634     return "DW_ATOM_type_flags";
635   case DW_ATOM_qual_name_hash:
636     return "DW_ATOM_qual_name_hash";
637   }
638   return StringRef();
639 }
640 
641 StringRef llvm::dwarf::GDBIndexEntryKindString(GDBIndexEntryKind Kind) {
642   switch (Kind) {
643   case GIEK_NONE:
644     return "NONE";
645   case GIEK_TYPE:
646     return "TYPE";
647   case GIEK_VARIABLE:
648     return "VARIABLE";
649   case GIEK_FUNCTION:
650     return "FUNCTION";
651   case GIEK_OTHER:
652     return "OTHER";
653   case GIEK_UNUSED5:
654     return "UNUSED5";
655   case GIEK_UNUSED6:
656     return "UNUSED6";
657   case GIEK_UNUSED7:
658     return "UNUSED7";
659   }
660   llvm_unreachable("Unknown GDBIndexEntryKind value");
661 }
662 
663 StringRef
664 llvm::dwarf::GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage) {
665   switch (Linkage) {
666   case GIEL_EXTERNAL:
667     return "EXTERNAL";
668   case GIEL_STATIC:
669     return "STATIC";
670   }
671   llvm_unreachable("Unknown GDBIndexEntryLinkage value");
672 }
673 
674 StringRef llvm::dwarf::AttributeValueString(uint16_t Attr, unsigned Val) {
675   switch (Attr) {
676   case DW_AT_accessibility:
677     return AccessibilityString(Val);
678   case DW_AT_virtuality:
679     return VirtualityString(Val);
680   case DW_AT_language:
681     return LanguageString(Val);
682   case DW_AT_encoding:
683     return AttributeEncodingString(Val);
684   case DW_AT_decimal_sign:
685     return DecimalSignString(Val);
686   case DW_AT_endianity:
687     return EndianityString(Val);
688   case DW_AT_visibility:
689     return VisibilityString(Val);
690   case DW_AT_identifier_case:
691     return CaseString(Val);
692   case DW_AT_calling_convention:
693     return ConventionString(Val);
694   case DW_AT_inline:
695     return InlineCodeString(Val);
696   case DW_AT_ordering:
697     return ArrayOrderString(Val);
698   case DW_AT_APPLE_runtime_class:
699     return LanguageString(Val);
700   case DW_AT_defaulted:
701     return DefaultedMemberString(Val);
702   }
703 
704   return StringRef();
705 }
706 
707 StringRef llvm::dwarf::AtomValueString(uint16_t Atom, unsigned Val) {
708   switch (Atom) {
709   case DW_ATOM_null:
710     return "NULL";
711   case DW_ATOM_die_tag:
712     return TagString(Val);
713   }
714 
715   return StringRef();
716 }
717 
718 StringRef llvm::dwarf::IndexString(unsigned Idx) {
719   switch (Idx) {
720   default:
721     return StringRef();
722 #define HANDLE_DW_IDX(ID, NAME)                                                \
723   case DW_IDX_##NAME:                                                          \
724     return "DW_IDX_" #NAME;
725 #include "llvm/BinaryFormat/Dwarf.def"
726   }
727 }
728 
729 std::optional<uint8_t> llvm::dwarf::getFixedFormByteSize(dwarf::Form Form,
730                                                          FormParams Params) {
731   switch (Form) {
732   case DW_FORM_addr:
733     if (Params)
734       return Params.AddrSize;
735     return std::nullopt;
736 
737   case DW_FORM_block:          // ULEB128 length L followed by L bytes.
738   case DW_FORM_block1:         // 1 byte length L followed by L bytes.
739   case DW_FORM_block2:         // 2 byte length L followed by L bytes.
740   case DW_FORM_block4:         // 4 byte length L followed by L bytes.
741   case DW_FORM_string:         // C-string with null terminator.
742   case DW_FORM_sdata:          // SLEB128.
743   case DW_FORM_udata:          // ULEB128.
744   case DW_FORM_ref_udata:      // ULEB128.
745   case DW_FORM_indirect:       // ULEB128.
746   case DW_FORM_exprloc:        // ULEB128 length L followed by L bytes.
747   case DW_FORM_strx:           // ULEB128.
748   case DW_FORM_addrx:          // ULEB128.
749   case DW_FORM_loclistx:       // ULEB128.
750   case DW_FORM_rnglistx:       // ULEB128.
751   case DW_FORM_GNU_addr_index: // ULEB128.
752   case DW_FORM_GNU_str_index:  // ULEB128.
753     return std::nullopt;
754 
755   case DW_FORM_ref_addr:
756     if (Params)
757       return Params.getRefAddrByteSize();
758     return std::nullopt;
759 
760   case DW_FORM_flag:
761   case DW_FORM_data1:
762   case DW_FORM_ref1:
763   case DW_FORM_strx1:
764   case DW_FORM_addrx1:
765     return 1;
766 
767   case DW_FORM_data2:
768   case DW_FORM_ref2:
769   case DW_FORM_strx2:
770   case DW_FORM_addrx2:
771     return 2;
772 
773   case DW_FORM_strx3:
774   case DW_FORM_addrx3:
775     return 3;
776 
777   case DW_FORM_data4:
778   case DW_FORM_ref4:
779   case DW_FORM_ref_sup4:
780   case DW_FORM_strx4:
781   case DW_FORM_addrx4:
782     return 4;
783 
784   case DW_FORM_strp:
785   case DW_FORM_GNU_ref_alt:
786   case DW_FORM_GNU_strp_alt:
787   case DW_FORM_line_strp:
788   case DW_FORM_sec_offset:
789   case DW_FORM_strp_sup:
790     if (Params)
791       return Params.getDwarfOffsetByteSize();
792     return std::nullopt;
793 
794   case DW_FORM_data8:
795   case DW_FORM_ref8:
796   case DW_FORM_ref_sig8:
797   case DW_FORM_ref_sup8:
798     return 8;
799 
800   case DW_FORM_flag_present:
801     return 0;
802 
803   case DW_FORM_data16:
804     return 16;
805 
806   case DW_FORM_implicit_const:
807     // The implicit value is stored in the abbreviation as a SLEB128, and
808     // there no data in debug info.
809     return 0;
810 
811   default:
812     break;
813   }
814   return std::nullopt;
815 }
816 
817 bool llvm::dwarf::isValidFormForVersion(Form F, unsigned Version,
818                                         bool ExtensionsOk) {
819   if (FormVendor(F) == DWARF_VENDOR_DWARF) {
820     unsigned FV = FormVersion(F);
821     return FV > 0 && FV <= Version;
822   }
823   return ExtensionsOk;
824 }
825 
826 StringRef llvm::dwarf::FormatString(DwarfFormat Format) {
827   switch (Format) {
828   case DWARF32:
829     return "DWARF32";
830   case DWARF64:
831     return "DWARF64";
832   }
833   return StringRef();
834 }
835 
836 StringRef llvm::dwarf::FormatString(bool IsDWARF64) {
837   return FormatString(IsDWARF64 ? DWARF64 : DWARF32);
838 }
839 
840 StringRef llvm::dwarf::RLEString(unsigned RLE) {
841   switch (RLE) {
842   default:
843     return StringRef();
844 #define HANDLE_DW_RLE(ID, NAME)                                                \
845   case DW_RLE_##NAME:                                                          \
846     return "DW_RLE_" #NAME;
847 #include "llvm/BinaryFormat/Dwarf.def"
848   }
849 }
850 
851 constexpr char llvm::dwarf::EnumTraits<Attribute>::Type[];
852 constexpr char llvm::dwarf::EnumTraits<Form>::Type[];
853 constexpr char llvm::dwarf::EnumTraits<Index>::Type[];
854 constexpr char llvm::dwarf::EnumTraits<Tag>::Type[];
855 constexpr char llvm::dwarf::EnumTraits<LineNumberOps>::Type[];
856 constexpr char llvm::dwarf::EnumTraits<LocationAtom>::Type[];
857