1 //===- DWARFDebugMacro.h ----------------------------------------*- 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 #ifndef LLVM_DEBUGINFO_DWARF_DWARFDEBUGMACRO_H
10 #define LLVM_DEBUGINFO_DWARF_DWARFDEBUGMACRO_H
11 
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
14 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
15 #include "llvm/Support/Error.h"
16 #include <cstdint>
17 
18 namespace llvm {
19 
20 class raw_ostream;
21 
22 namespace dwarf_linker {
23 namespace classic {
24 class DwarfStreamer;
25 }
26 } // namespace dwarf_linker
27 
28 class DWARFDebugMacro {
29   friend dwarf_linker::classic::DwarfStreamer;
30   friend dwarf_linker::parallel::CompileUnit;
31 
32   /// DWARFv5 section 6.3.1 Macro Information Header.
33   enum HeaderFlagMask {
34 #define HANDLE_MACRO_FLAG(ID, NAME) MACRO_##NAME = ID,
35 #include "llvm/BinaryFormat/Dwarf.def"
36   };
37   struct MacroHeader {
38     /// Macro version information number.
39     uint16_t Version = 0;
40 
41     /// The bits of the flags field are interpreted as a set of flags, some of
42     /// which may indicate that additional fields follow. The following flags,
43     /// beginning with the least significant bit, are defined:
44     /// offset_size_flag:
45     ///   If the offset_size_flag is zero, the header is for a 32-bit DWARF
46     ///   format macro section and all offsets are 4 bytes long; if it is one,
47     ///   the header is for a 64-bit DWARF format macro section and all offsets
48     ///   are 8 bytes long.
49     /// debug_line_offset_flag:
50     ///   If the debug_line_offset_flag is one, the debug_line_offset field (see
51     ///   below) is present. If zero, that field is omitted.
52     /// opcode_operands_table_flag:
53     ///   If the opcode_operands_table_flag is one, the opcode_operands_table
54     ///   field (see below) is present. If zero, that field is omitted.
55     uint8_t Flags = 0;
56 
57     /// debug_line_offset
58     ///   An offset in the .debug_line section of the beginning of the line
59     ///   number information in the containing compilation unit, encoded as a
60     ///   4-byte offset for a 32-bit DWARF format macro section and an 8-byte
61     ///   offset for a 64-bit DWARF format macro section.
62     uint64_t DebugLineOffset;
63 
64     /// Print the macro header from the debug_macro section.
65     void dumpMacroHeader(raw_ostream &OS) const;
66 
67     /// Parse the debug_macro header.
68     Error parseMacroHeader(DWARFDataExtractor Data, uint64_t *Offset);
69 
70     /// Get the DWARF format according to the flags.
71     dwarf::DwarfFormat getDwarfFormat() const;
72 
73     /// Get the size of a reference according to the DWARF format.
74     uint8_t getOffsetByteSize() const;
75   };
76 
77   /// A single macro entry within a macro list.
78   struct Entry {
79     /// The type of the macro entry.
80     uint32_t Type;
81     union {
82       /// The source line where the macro is defined.
83       uint64_t Line;
84       /// Vendor extension constant value.
85       uint64_t ExtConstant;
86       /// Macro unit import offset.
87       uint64_t ImportOffset;
88     };
89 
90     union {
91       /// The string (name, value) of the macro entry.
92       const char *MacroStr;
93       // An unsigned integer indicating the identity of the source file.
94       uint64_t File;
95       /// Vendor extension string.
96       const char *ExtStr;
97     };
98   };
99 
100   struct MacroList {
101     // A value 0 in the `Header.Version` field indicates that we're parsing
102     // a macinfo[.dwo] section which doesn't have header itself, hence
103     // for that case other fields in the `Header` are uninitialized.
104     MacroHeader Header;
105     SmallVector<Entry, 4> Macros;
106     uint64_t Offset;
107 
108     /// Whether or not this is a .debug_macro section.
109     bool IsDebugMacro;
110   };
111 
112   /// A list of all the macro entries in the debug_macinfo section.
113   std::vector<MacroList> MacroLists;
114 
115 public:
116   DWARFDebugMacro() = default;
117 
118   /// Print the macro list found within the debug_macinfo/debug_macro section.
119   void dump(raw_ostream &OS) const;
120 
121   Error parseMacro(DWARFUnitVector::compile_unit_range Units,
122                    DataExtractor StringExtractor,
123                    DWARFDataExtractor MacroData) {
124     return parseImpl(Units, StringExtractor, MacroData, /*IsMacro=*/true);
125   }
126 
127   Error parseMacinfo(DWARFDataExtractor MacroData) {
128     return parseImpl(std::nullopt, std::nullopt, MacroData, /*IsMacro=*/false);
129   }
130 
131   /// Return whether the section has any entries.
132   bool empty() const { return MacroLists.empty(); }
133 
134   bool hasEntryForOffset(uint64_t Offset) const {
135     for (const MacroList &List : MacroLists)
136       if (Offset == List.Offset)
137         return true;
138 
139     return false;
140   }
141 
142 private:
143   /// Parse the debug_macinfo/debug_macro section accessible via the 'MacroData'
144   /// parameter.
145   Error parseImpl(std::optional<DWARFUnitVector::compile_unit_range> Units,
146                   std::optional<DataExtractor> StringExtractor,
147                   DWARFDataExtractor Data, bool IsMacro);
148 };
149 
150 } // end namespace llvm
151 
152 #endif // LLVM_DEBUGINFO_DWARF_DWARFDEBUGMACRO_H
153