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