1 //===- DWARFDebugMacro.cpp ------------------------------------------------===//
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 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
10 #include "llvm/BinaryFormat/Dwarf.h"
11 #include "llvm/Support/WithColor.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include <cstdint>
14 
15 using namespace llvm;
16 using namespace dwarf;
17 
18 void DWARFDebugMacro::dump(raw_ostream &OS) const {
19   unsigned IndLevel = 0;
20   for (const Entry &E : Macros) {
21     // There should not be DW_MACINFO_end_file when IndLevel is Zero. However,
22     // this check handles the case of corrupted ".debug_macinfo" section.
23     if (IndLevel > 0)
24       IndLevel -= (E.Type == DW_MACINFO_end_file);
25     // Print indentation.
26     for (unsigned I = 0; I < IndLevel; I++)
27       OS << "  ";
28     IndLevel += (E.Type == DW_MACINFO_start_file);
29 
30     WithColor(OS, HighlightColor::Macro).get() << MacinfoString(E.Type);
31     switch (E.Type) {
32     default:
33       // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
34       break;
35     case DW_MACINFO_define:
36     case DW_MACINFO_undef:
37       OS << " - lineno: " << E.Line;
38       OS << " macro: " << E.MacroStr;
39       break;
40     case DW_MACINFO_start_file:
41       OS << " - lineno: " << E.Line;
42       OS << " filenum: " << E.File;
43       break;
44     case DW_MACINFO_end_file:
45       break;
46     case DW_MACINFO_vendor_ext:
47       OS << " - constant: " << E.ExtConstant;
48       OS << " string: " << E.ExtStr;
49       break;
50     }
51     OS << "\n";
52   }
53 }
54 
55 void DWARFDebugMacro::parse(DataExtractor data) {
56   uint32_t Offset = 0;
57   while (data.isValidOffset(Offset)) {
58     // A macro list entry consists of:
59     Entry E;
60     // 1. Macinfo type
61     E.Type = data.getULEB128(&Offset);
62 
63     if (E.Type == 0) {
64       // Reached end of ".debug_macinfo" section.
65       return;
66     }
67 
68     switch (E.Type) {
69     default:
70       // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
71       // Push the corrupted entry to the list and halt parsing.
72       E.Type = DW_MACINFO_invalid;
73       Macros.push_back(E);
74       return;
75     case DW_MACINFO_define:
76     case DW_MACINFO_undef:
77       // 2. Source line
78       E.Line = data.getULEB128(&Offset);
79       // 3. Macro string
80       E.MacroStr = data.getCStr(&Offset);
81       break;
82     case DW_MACINFO_start_file:
83       // 2. Source line
84       E.Line = data.getULEB128(&Offset);
85       // 3. Source file id
86       E.File = data.getULEB128(&Offset);
87       break;
88     case DW_MACINFO_end_file:
89       break;
90     case DW_MACINFO_vendor_ext:
91       // 2. Vendor extension constant
92       E.ExtConstant = data.getULEB128(&Offset);
93       // 3. Vendor extension string
94       E.ExtStr = data.getCStr(&Offset);
95       break;
96     }
97 
98     Macros.push_back(E);
99   }
100 }
101