Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame^] | 1 | //===- DWARFContext.h -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===/ |
| 9 | |
| 10 | #ifndef LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H |
| 11 | #define LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H |
| 12 | |
| 13 | #include "llvm/ADT/MapVector.h" |
| 14 | #include "llvm/ADT/SmallString.h" |
| 15 | #include "llvm/ADT/SmallVector.h" |
| 16 | #include "llvm/ADT/StringMap.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
| 18 | #include "llvm/ADT/iterator_range.h" |
| 19 | #include "llvm/DebugInfo/DIContext.h" |
| 20 | #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" |
| 21 | #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" |
| 22 | #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" |
| 23 | #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h" |
| 24 | #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h" |
| 25 | #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" |
| 26 | #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h" |
| 27 | #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h" |
| 28 | #include "llvm/DebugInfo/DWARF/DWARFDie.h" |
| 29 | #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h" |
| 30 | #include "llvm/DebugInfo/DWARF/DWARFObject.h" |
| 31 | #include "llvm/DebugInfo/DWARF/DWARFSection.h" |
| 32 | #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h" |
| 33 | #include "llvm/DebugInfo/DWARF/DWARFUnit.h" |
| 34 | #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" |
| 35 | #include "llvm/Object/Binary.h" |
| 36 | #include "llvm/Object/ObjectFile.h" |
| 37 | #include "llvm/Support/DataExtractor.h" |
| 38 | #include "llvm/Support/Error.h" |
| 39 | #include "llvm/Support/Host.h" |
| 40 | #include <cstdint> |
| 41 | #include <deque> |
| 42 | #include <map> |
| 43 | #include <memory> |
| 44 | |
| 45 | namespace llvm { |
| 46 | |
| 47 | class MCRegisterInfo; |
| 48 | class MemoryBuffer; |
| 49 | class raw_ostream; |
| 50 | |
| 51 | /// Used as a return value for a error callback passed to DWARF context. |
| 52 | /// Callback should return Halt if client application wants to stop |
| 53 | /// object parsing, or should return Continue otherwise. |
| 54 | enum class ErrorPolicy { Halt, Continue }; |
| 55 | |
| 56 | /// DWARFContext |
| 57 | /// This data structure is the top level entity that deals with dwarf debug |
| 58 | /// information parsing. The actual data is supplied through DWARFObj. |
| 59 | class DWARFContext : public DIContext { |
| 60 | DWARFUnitSection<DWARFCompileUnit> CUs; |
| 61 | std::deque<DWARFUnitSection<DWARFTypeUnit>> TUs; |
| 62 | std::unique_ptr<DWARFUnitIndex> CUIndex; |
| 63 | std::unique_ptr<DWARFGdbIndex> GdbIndex; |
| 64 | std::unique_ptr<DWARFUnitIndex> TUIndex; |
| 65 | std::unique_ptr<DWARFDebugAbbrev> Abbrev; |
| 66 | std::unique_ptr<DWARFDebugLoc> Loc; |
| 67 | std::unique_ptr<DWARFDebugAranges> Aranges; |
| 68 | std::unique_ptr<DWARFDebugLine> Line; |
| 69 | std::unique_ptr<DWARFDebugFrame> DebugFrame; |
| 70 | std::unique_ptr<DWARFDebugFrame> EHFrame; |
| 71 | std::unique_ptr<DWARFDebugMacro> Macro; |
| 72 | std::unique_ptr<DWARFDebugNames> Names; |
| 73 | std::unique_ptr<AppleAcceleratorTable> AppleNames; |
| 74 | std::unique_ptr<AppleAcceleratorTable> AppleTypes; |
| 75 | std::unique_ptr<AppleAcceleratorTable> AppleNamespaces; |
| 76 | std::unique_ptr<AppleAcceleratorTable> AppleObjC; |
| 77 | |
| 78 | DWARFUnitSection<DWARFCompileUnit> DWOCUs; |
| 79 | std::deque<DWARFUnitSection<DWARFTypeUnit>> DWOTUs; |
| 80 | std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO; |
| 81 | std::unique_ptr<DWARFDebugLocDWO> LocDWO; |
| 82 | |
| 83 | /// The maximum DWARF version of all units. |
| 84 | unsigned MaxVersion = 0; |
| 85 | |
| 86 | struct DWOFile { |
| 87 | object::OwningBinary<object::ObjectFile> File; |
| 88 | std::unique_ptr<DWARFContext> Context; |
| 89 | }; |
| 90 | StringMap<std::weak_ptr<DWOFile>> DWOFiles; |
| 91 | std::weak_ptr<DWOFile> DWP; |
| 92 | bool CheckedForDWP = false; |
| 93 | std::string DWPName; |
| 94 | |
| 95 | std::unique_ptr<MCRegisterInfo> RegInfo; |
| 96 | |
| 97 | /// Read compile units from the debug_info section (if necessary) |
| 98 | /// and store them in CUs. |
| 99 | void parseCompileUnits(); |
| 100 | |
| 101 | /// Read type units from the debug_types sections (if necessary) |
| 102 | /// and store them in TUs. |
| 103 | void parseTypeUnits(); |
| 104 | |
| 105 | /// Read compile units from the debug_info.dwo section (if necessary) |
| 106 | /// and store them in DWOCUs. |
| 107 | void parseDWOCompileUnits(); |
| 108 | |
| 109 | /// Read type units from the debug_types.dwo section (if necessary) |
| 110 | /// and store them in DWOTUs. |
| 111 | void parseDWOTypeUnits(); |
| 112 | |
| 113 | protected: |
| 114 | std::unique_ptr<const DWARFObject> DObj; |
| 115 | |
| 116 | public: |
| 117 | DWARFContext(std::unique_ptr<const DWARFObject> DObj, |
| 118 | std::string DWPName = ""); |
| 119 | ~DWARFContext(); |
| 120 | |
| 121 | DWARFContext(DWARFContext &) = delete; |
| 122 | DWARFContext &operator=(DWARFContext &) = delete; |
| 123 | |
| 124 | const DWARFObject &getDWARFObj() const { return *DObj; } |
| 125 | |
| 126 | static bool classof(const DIContext *DICtx) { |
| 127 | return DICtx->getKind() == CK_DWARF; |
| 128 | } |
| 129 | |
| 130 | /// Dump a textual representation to \p OS. If any \p DumpOffsets are present, |
| 131 | /// dump only the record at the specified offset. |
| 132 | void dump(raw_ostream &OS, DIDumpOptions DumpOpts, |
| 133 | std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets); |
| 134 | |
| 135 | void dump(raw_ostream &OS, DIDumpOptions DumpOpts) override { |
| 136 | std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets; |
| 137 | dump(OS, DumpOpts, DumpOffsets); |
| 138 | } |
| 139 | |
| 140 | bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) override; |
| 141 | |
| 142 | using cu_iterator_range = DWARFUnitSection<DWARFCompileUnit>::iterator_range; |
| 143 | using tu_iterator_range = DWARFUnitSection<DWARFTypeUnit>::iterator_range; |
| 144 | using tu_section_iterator_range = iterator_range<decltype(TUs)::iterator>; |
| 145 | |
| 146 | /// Get compile units in this context. |
| 147 | cu_iterator_range compile_units() { |
| 148 | parseCompileUnits(); |
| 149 | return cu_iterator_range(CUs.begin(), CUs.end()); |
| 150 | } |
| 151 | |
| 152 | /// Get type units in this context. |
| 153 | tu_section_iterator_range type_unit_sections() { |
| 154 | parseTypeUnits(); |
| 155 | return tu_section_iterator_range(TUs.begin(), TUs.end()); |
| 156 | } |
| 157 | |
| 158 | /// Get compile units in the DWO context. |
| 159 | cu_iterator_range dwo_compile_units() { |
| 160 | parseDWOCompileUnits(); |
| 161 | return cu_iterator_range(DWOCUs.begin(), DWOCUs.end()); |
| 162 | } |
| 163 | |
| 164 | /// Get type units in the DWO context. |
| 165 | tu_section_iterator_range dwo_type_unit_sections() { |
| 166 | parseDWOTypeUnits(); |
| 167 | return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end()); |
| 168 | } |
| 169 | |
| 170 | /// Get the number of compile units in this context. |
| 171 | unsigned getNumCompileUnits() { |
| 172 | parseCompileUnits(); |
| 173 | return CUs.size(); |
| 174 | } |
| 175 | |
| 176 | /// Get the number of compile units in this context. |
| 177 | unsigned getNumTypeUnits() { |
| 178 | parseTypeUnits(); |
| 179 | return TUs.size(); |
| 180 | } |
| 181 | |
| 182 | /// Get the number of compile units in the DWO context. |
| 183 | unsigned getNumDWOCompileUnits() { |
| 184 | parseDWOCompileUnits(); |
| 185 | return DWOCUs.size(); |
| 186 | } |
| 187 | |
| 188 | /// Get the number of compile units in the DWO context. |
| 189 | unsigned getNumDWOTypeUnits() { |
| 190 | parseDWOTypeUnits(); |
| 191 | return DWOTUs.size(); |
| 192 | } |
| 193 | |
| 194 | /// Get the compile unit at the specified index for this compile unit. |
| 195 | DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) { |
| 196 | parseCompileUnits(); |
| 197 | return CUs[index].get(); |
| 198 | } |
| 199 | |
| 200 | /// Get the compile unit at the specified index for the DWO compile units. |
| 201 | DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) { |
| 202 | parseDWOCompileUnits(); |
| 203 | return DWOCUs[index].get(); |
| 204 | } |
| 205 | |
| 206 | DWARFCompileUnit *getDWOCompileUnitForHash(uint64_t Hash); |
| 207 | |
| 208 | /// Get a DIE given an exact offset. |
| 209 | DWARFDie getDIEForOffset(uint32_t Offset); |
| 210 | |
| 211 | unsigned getMaxVersion() const { return MaxVersion; } |
| 212 | |
| 213 | void setMaxVersionIfGreater(unsigned Version) { |
| 214 | if (Version > MaxVersion) |
| 215 | MaxVersion = Version; |
| 216 | } |
| 217 | |
| 218 | const DWARFUnitIndex &getCUIndex(); |
| 219 | DWARFGdbIndex &getGdbIndex(); |
| 220 | const DWARFUnitIndex &getTUIndex(); |
| 221 | |
| 222 | /// Get a pointer to the parsed DebugAbbrev object. |
| 223 | const DWARFDebugAbbrev *getDebugAbbrev(); |
| 224 | |
| 225 | /// Get a pointer to the parsed DebugLoc object. |
| 226 | const DWARFDebugLoc *getDebugLoc(); |
| 227 | |
| 228 | /// Get a pointer to the parsed dwo abbreviations object. |
| 229 | const DWARFDebugAbbrev *getDebugAbbrevDWO(); |
| 230 | |
| 231 | /// Get a pointer to the parsed DebugLoc object. |
| 232 | const DWARFDebugLocDWO *getDebugLocDWO(); |
| 233 | |
| 234 | /// Get a pointer to the parsed DebugAranges object. |
| 235 | const DWARFDebugAranges *getDebugAranges(); |
| 236 | |
| 237 | /// Get a pointer to the parsed frame information object. |
| 238 | const DWARFDebugFrame *getDebugFrame(); |
| 239 | |
| 240 | /// Get a pointer to the parsed eh frame information object. |
| 241 | const DWARFDebugFrame *getEHFrame(); |
| 242 | |
| 243 | /// Get a pointer to the parsed DebugMacro object. |
| 244 | const DWARFDebugMacro *getDebugMacro(); |
| 245 | |
| 246 | /// Get a reference to the parsed accelerator table object. |
| 247 | const DWARFDebugNames &getDebugNames(); |
| 248 | |
| 249 | /// Get a reference to the parsed accelerator table object. |
| 250 | const AppleAcceleratorTable &getAppleNames(); |
| 251 | |
| 252 | /// Get a reference to the parsed accelerator table object. |
| 253 | const AppleAcceleratorTable &getAppleTypes(); |
| 254 | |
| 255 | /// Get a reference to the parsed accelerator table object. |
| 256 | const AppleAcceleratorTable &getAppleNamespaces(); |
| 257 | |
| 258 | /// Get a reference to the parsed accelerator table object. |
| 259 | const AppleAcceleratorTable &getAppleObjC(); |
| 260 | |
| 261 | /// Get a pointer to a parsed line table corresponding to a compile unit. |
| 262 | const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu); |
| 263 | |
| 264 | DataExtractor getStringExtractor() const { |
| 265 | return DataExtractor(DObj->getStringSection(), false, 0); |
| 266 | } |
| 267 | DataExtractor getLineStringExtractor() const { |
| 268 | return DataExtractor(DObj->getLineStringSection(), false, 0); |
| 269 | } |
| 270 | |
| 271 | /// Wraps the returned DIEs for a given address. |
| 272 | struct DIEsForAddress { |
| 273 | DWARFCompileUnit *CompileUnit = nullptr; |
| 274 | DWARFDie FunctionDIE; |
| 275 | DWARFDie BlockDIE; |
| 276 | explicit operator bool() const { return CompileUnit != nullptr; } |
| 277 | }; |
| 278 | |
| 279 | /// Get the compilation unit, the function DIE and lexical block DIE for the |
| 280 | /// given address where applicable. |
| 281 | DIEsForAddress getDIEsForAddress(uint64_t Address); |
| 282 | |
| 283 | DILineInfo getLineInfoForAddress(uint64_t Address, |
| 284 | DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override; |
| 285 | DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size, |
| 286 | DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override; |
| 287 | DIInliningInfo getInliningInfoForAddress(uint64_t Address, |
| 288 | DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override; |
| 289 | |
| 290 | bool isLittleEndian() const { return DObj->isLittleEndian(); } |
| 291 | static bool isSupportedVersion(unsigned version) { |
| 292 | return version == 2 || version == 3 || version == 4 || version == 5; |
| 293 | } |
| 294 | |
| 295 | std::shared_ptr<DWARFContext> getDWOContext(StringRef AbsolutePath); |
| 296 | |
| 297 | const MCRegisterInfo *getRegisterInfo() const { return RegInfo.get(); } |
| 298 | |
| 299 | /// Function used to handle default error reporting policy. Prints a error |
| 300 | /// message and returns Continue, so DWARF context ignores the error. |
| 301 | static ErrorPolicy defaultErrorHandler(Error E); |
| 302 | static std::unique_ptr<DWARFContext> |
| 303 | create(const object::ObjectFile &Obj, const LoadedObjectInfo *L = nullptr, |
| 304 | function_ref<ErrorPolicy(Error)> HandleError = defaultErrorHandler, |
| 305 | std::string DWPName = ""); |
| 306 | |
| 307 | static std::unique_ptr<DWARFContext> |
| 308 | create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, |
| 309 | uint8_t AddrSize, bool isLittleEndian = sys::IsLittleEndianHost); |
| 310 | |
| 311 | /// Loads register info for the architecture of the provided object file. |
| 312 | /// Improves readability of dumped DWARF expressions. Requires the caller to |
| 313 | /// have initialized the relevant target descriptions. |
| 314 | Error loadRegisterInfo(const object::ObjectFile &Obj); |
| 315 | |
| 316 | private: |
| 317 | /// Return the compile unit that includes an offset (relative to .debug_info). |
| 318 | DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset); |
| 319 | |
| 320 | /// Return the compile unit which contains instruction with provided |
| 321 | /// address. |
| 322 | DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address); |
| 323 | }; |
| 324 | |
| 325 | } // end namespace llvm |
| 326 | |
| 327 | #endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H |