1 //===- DWARFDebugMacro.cpp ------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
16 using namespace dwarf
;
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.
24 IndLevel
-= (E
.Type
== DW_MACINFO_end_file
);
26 for (unsigned I
= 0; I
< IndLevel
; I
++)
28 IndLevel
+= (E
.Type
== DW_MACINFO_start_file
);
30 WithColor(OS
, HighlightColor::Macro
).get() << MacinfoString(E
.Type
);
33 // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
35 case DW_MACINFO_define
:
36 case DW_MACINFO_undef
:
37 OS
<< " - lineno: " << E
.Line
;
38 OS
<< " macro: " << E
.MacroStr
;
40 case DW_MACINFO_start_file
:
41 OS
<< " - lineno: " << E
.Line
;
42 OS
<< " filenum: " << E
.File
;
44 case DW_MACINFO_end_file
:
46 case DW_MACINFO_vendor_ext
:
47 OS
<< " - constant: " << E
.ExtConstant
;
48 OS
<< " string: " << E
.ExtStr
;
55 void DWARFDebugMacro::parse(DataExtractor data
) {
57 while (data
.isValidOffset(Offset
)) {
58 // A macro list entry consists of:
61 E
.Type
= data
.getULEB128(&Offset
);
64 // Reached end of ".debug_macinfo" section.
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
;
75 case DW_MACINFO_define
:
76 case DW_MACINFO_undef
:
78 E
.Line
= data
.getULEB128(&Offset
);
80 E
.MacroStr
= data
.getCStr(&Offset
);
82 case DW_MACINFO_start_file
:
84 E
.Line
= data
.getULEB128(&Offset
);
86 E
.File
= data
.getULEB128(&Offset
);
88 case DW_MACINFO_end_file
:
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
);