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 { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 60 | DWARFUnitVector NormalUnits; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 61 | std::unique_ptr<DWARFUnitIndex> CUIndex; |
| 62 | std::unique_ptr<DWARFGdbIndex> GdbIndex; |
| 63 | std::unique_ptr<DWARFUnitIndex> TUIndex; |
| 64 | std::unique_ptr<DWARFDebugAbbrev> Abbrev; |
| 65 | std::unique_ptr<DWARFDebugLoc> Loc; |
| 66 | std::unique_ptr<DWARFDebugAranges> Aranges; |
| 67 | std::unique_ptr<DWARFDebugLine> Line; |
| 68 | std::unique_ptr<DWARFDebugFrame> DebugFrame; |
| 69 | std::unique_ptr<DWARFDebugFrame> EHFrame; |
| 70 | std::unique_ptr<DWARFDebugMacro> Macro; |
| 71 | std::unique_ptr<DWARFDebugNames> Names; |
| 72 | std::unique_ptr<AppleAcceleratorTable> AppleNames; |
| 73 | std::unique_ptr<AppleAcceleratorTable> AppleTypes; |
| 74 | std::unique_ptr<AppleAcceleratorTable> AppleNamespaces; |
| 75 | std::unique_ptr<AppleAcceleratorTable> AppleObjC; |
| 76 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 77 | DWARFUnitVector DWOUnits; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 78 | std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO; |
| 79 | std::unique_ptr<DWARFDebugLocDWO> LocDWO; |
| 80 | |
| 81 | /// The maximum DWARF version of all units. |
| 82 | unsigned MaxVersion = 0; |
| 83 | |
| 84 | struct DWOFile { |
| 85 | object::OwningBinary<object::ObjectFile> File; |
| 86 | std::unique_ptr<DWARFContext> Context; |
| 87 | }; |
| 88 | StringMap<std::weak_ptr<DWOFile>> DWOFiles; |
| 89 | std::weak_ptr<DWOFile> DWP; |
| 90 | bool CheckedForDWP = false; |
| 91 | std::string DWPName; |
| 92 | |
| 93 | std::unique_ptr<MCRegisterInfo> RegInfo; |
| 94 | |
| 95 | /// Read compile units from the debug_info section (if necessary) |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 96 | /// and type units from the debug_types sections (if necessary) |
| 97 | /// and store them in NormalUnits. |
| 98 | void parseNormalUnits(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 99 | |
| 100 | /// Read compile units from the debug_info.dwo section (if necessary) |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 101 | /// and type units from the debug_types.dwo section (if necessary) |
| 102 | /// and store them in DWOUnits. |
| 103 | /// If \p Lazy is true, set up to parse but don't actually parse them. |
| 104 | enum { EagerParse = false, LazyParse = true }; |
| 105 | void parseDWOUnits(bool Lazy = false); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 106 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 107 | std::unique_ptr<const DWARFObject> DObj; |
| 108 | |
| 109 | public: |
| 110 | DWARFContext(std::unique_ptr<const DWARFObject> DObj, |
| 111 | std::string DWPName = ""); |
| 112 | ~DWARFContext(); |
| 113 | |
| 114 | DWARFContext(DWARFContext &) = delete; |
| 115 | DWARFContext &operator=(DWARFContext &) = delete; |
| 116 | |
| 117 | const DWARFObject &getDWARFObj() const { return *DObj; } |
| 118 | |
| 119 | static bool classof(const DIContext *DICtx) { |
| 120 | return DICtx->getKind() == CK_DWARF; |
| 121 | } |
| 122 | |
| 123 | /// Dump a textual representation to \p OS. If any \p DumpOffsets are present, |
| 124 | /// dump only the record at the specified offset. |
| 125 | void dump(raw_ostream &OS, DIDumpOptions DumpOpts, |
| 126 | std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets); |
| 127 | |
| 128 | void dump(raw_ostream &OS, DIDumpOptions DumpOpts) override { |
| 129 | std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets; |
| 130 | dump(OS, DumpOpts, DumpOffsets); |
| 131 | } |
| 132 | |
| 133 | bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) override; |
| 134 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 135 | using unit_iterator_range = DWARFUnitVector::iterator_range; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 136 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 137 | /// Get units from .debug_info in this context. |
| 138 | unit_iterator_range info_section_units() { |
| 139 | parseNormalUnits(); |
| 140 | return unit_iterator_range(NormalUnits.begin(), |
| 141 | NormalUnits.begin() + |
| 142 | NormalUnits.getNumInfoUnits()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 145 | /// Get units from .debug_types in this context. |
| 146 | unit_iterator_range types_section_units() { |
| 147 | parseNormalUnits(); |
| 148 | return unit_iterator_range( |
| 149 | NormalUnits.begin() + NormalUnits.getNumInfoUnits(), NormalUnits.end()); |
| 150 | } |
| 151 | |
| 152 | /// Get compile units in this context. |
| 153 | unit_iterator_range compile_units() { return info_section_units(); } |
| 154 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 155 | /// Get type units in this context. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 156 | unit_iterator_range type_units() { return types_section_units(); } |
| 157 | |
| 158 | /// Get all normal compile/type units in this context. |
| 159 | unit_iterator_range normal_units() { |
| 160 | parseNormalUnits(); |
| 161 | return unit_iterator_range(NormalUnits.begin(), NormalUnits.end()); |
| 162 | } |
| 163 | |
| 164 | /// Get units from .debug_info..dwo in the DWO context. |
| 165 | unit_iterator_range dwo_info_section_units() { |
| 166 | parseDWOUnits(); |
| 167 | return unit_iterator_range(DWOUnits.begin(), |
| 168 | DWOUnits.begin() + DWOUnits.getNumInfoUnits()); |
| 169 | } |
| 170 | |
| 171 | /// Get units from .debug_types.dwo in the DWO context. |
| 172 | unit_iterator_range dwo_types_section_units() { |
| 173 | parseDWOUnits(); |
| 174 | return unit_iterator_range(DWOUnits.begin() + DWOUnits.getNumInfoUnits(), |
| 175 | DWOUnits.end()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /// Get compile units in the DWO context. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 179 | unit_iterator_range dwo_compile_units() { return dwo_info_section_units(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 180 | |
| 181 | /// Get type units in the DWO context. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 182 | unit_iterator_range dwo_type_units() { return dwo_types_section_units(); } |
| 183 | |
| 184 | /// Get all units in the DWO context. |
| 185 | unit_iterator_range dwo_units() { |
| 186 | parseDWOUnits(); |
| 187 | return unit_iterator_range(DWOUnits.begin(), DWOUnits.end()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /// Get the number of compile units in this context. |
| 191 | unsigned getNumCompileUnits() { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 192 | parseNormalUnits(); |
| 193 | return NormalUnits.getNumInfoUnits(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 196 | /// Get the number of type units in this context. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 197 | unsigned getNumTypeUnits() { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 198 | parseNormalUnits(); |
| 199 | return NormalUnits.getNumTypesUnits(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /// Get the number of compile units in the DWO context. |
| 203 | unsigned getNumDWOCompileUnits() { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 204 | parseDWOUnits(); |
| 205 | return DWOUnits.getNumInfoUnits(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 208 | /// Get the number of type units in the DWO context. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 209 | unsigned getNumDWOTypeUnits() { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 210 | parseDWOUnits(); |
| 211 | return DWOUnits.getNumTypesUnits(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 214 | /// Get the unit at the specified index. |
| 215 | DWARFUnit *getUnitAtIndex(unsigned index) { |
| 216 | parseNormalUnits(); |
| 217 | return NormalUnits[index].get(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 220 | /// Get the unit at the specified index for the DWO units. |
| 221 | DWARFUnit *getDWOUnitAtIndex(unsigned index) { |
| 222 | parseDWOUnits(); |
| 223 | return DWOUnits[index].get(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | DWARFCompileUnit *getDWOCompileUnitForHash(uint64_t Hash); |
| 227 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 228 | /// Return the compile unit that includes an offset (relative to .debug_info). |
| 229 | DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset); |
| 230 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 231 | /// Get a DIE given an exact offset. |
| 232 | DWARFDie getDIEForOffset(uint32_t Offset); |
| 233 | |
| 234 | unsigned getMaxVersion() const { return MaxVersion; } |
| 235 | |
| 236 | void setMaxVersionIfGreater(unsigned Version) { |
| 237 | if (Version > MaxVersion) |
| 238 | MaxVersion = Version; |
| 239 | } |
| 240 | |
| 241 | const DWARFUnitIndex &getCUIndex(); |
| 242 | DWARFGdbIndex &getGdbIndex(); |
| 243 | const DWARFUnitIndex &getTUIndex(); |
| 244 | |
| 245 | /// Get a pointer to the parsed DebugAbbrev object. |
| 246 | const DWARFDebugAbbrev *getDebugAbbrev(); |
| 247 | |
| 248 | /// Get a pointer to the parsed DebugLoc object. |
| 249 | const DWARFDebugLoc *getDebugLoc(); |
| 250 | |
| 251 | /// Get a pointer to the parsed dwo abbreviations object. |
| 252 | const DWARFDebugAbbrev *getDebugAbbrevDWO(); |
| 253 | |
| 254 | /// Get a pointer to the parsed DebugLoc object. |
| 255 | const DWARFDebugLocDWO *getDebugLocDWO(); |
| 256 | |
| 257 | /// Get a pointer to the parsed DebugAranges object. |
| 258 | const DWARFDebugAranges *getDebugAranges(); |
| 259 | |
| 260 | /// Get a pointer to the parsed frame information object. |
| 261 | const DWARFDebugFrame *getDebugFrame(); |
| 262 | |
| 263 | /// Get a pointer to the parsed eh frame information object. |
| 264 | const DWARFDebugFrame *getEHFrame(); |
| 265 | |
| 266 | /// Get a pointer to the parsed DebugMacro object. |
| 267 | const DWARFDebugMacro *getDebugMacro(); |
| 268 | |
| 269 | /// Get a reference to the parsed accelerator table object. |
| 270 | const DWARFDebugNames &getDebugNames(); |
| 271 | |
| 272 | /// Get a reference to the parsed accelerator table object. |
| 273 | const AppleAcceleratorTable &getAppleNames(); |
| 274 | |
| 275 | /// Get a reference to the parsed accelerator table object. |
| 276 | const AppleAcceleratorTable &getAppleTypes(); |
| 277 | |
| 278 | /// Get a reference to the parsed accelerator table object. |
| 279 | const AppleAcceleratorTable &getAppleNamespaces(); |
| 280 | |
| 281 | /// Get a reference to the parsed accelerator table object. |
| 282 | const AppleAcceleratorTable &getAppleObjC(); |
| 283 | |
| 284 | /// Get a pointer to a parsed line table corresponding to a compile unit. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 285 | /// Report any parsing issues as warnings on stderr. |
| 286 | const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *U); |
| 287 | |
| 288 | /// Get a pointer to a parsed line table corresponding to a compile unit. |
| 289 | /// Report any recoverable parsing problems using the callback. |
| 290 | Expected<const DWARFDebugLine::LineTable *> |
| 291 | getLineTableForUnit(DWARFUnit *U, |
| 292 | std::function<void(Error)> RecoverableErrorCallback); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 293 | |
| 294 | DataExtractor getStringExtractor() const { |
| 295 | return DataExtractor(DObj->getStringSection(), false, 0); |
| 296 | } |
| 297 | DataExtractor getLineStringExtractor() const { |
| 298 | return DataExtractor(DObj->getLineStringSection(), false, 0); |
| 299 | } |
| 300 | |
| 301 | /// Wraps the returned DIEs for a given address. |
| 302 | struct DIEsForAddress { |
| 303 | DWARFCompileUnit *CompileUnit = nullptr; |
| 304 | DWARFDie FunctionDIE; |
| 305 | DWARFDie BlockDIE; |
| 306 | explicit operator bool() const { return CompileUnit != nullptr; } |
| 307 | }; |
| 308 | |
| 309 | /// Get the compilation unit, the function DIE and lexical block DIE for the |
| 310 | /// given address where applicable. |
| 311 | DIEsForAddress getDIEsForAddress(uint64_t Address); |
| 312 | |
| 313 | DILineInfo getLineInfoForAddress(uint64_t Address, |
| 314 | DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override; |
| 315 | DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size, |
| 316 | DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override; |
| 317 | DIInliningInfo getInliningInfoForAddress(uint64_t Address, |
| 318 | DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override; |
| 319 | |
| 320 | bool isLittleEndian() const { return DObj->isLittleEndian(); } |
| 321 | static bool isSupportedVersion(unsigned version) { |
| 322 | return version == 2 || version == 3 || version == 4 || version == 5; |
| 323 | } |
| 324 | |
| 325 | std::shared_ptr<DWARFContext> getDWOContext(StringRef AbsolutePath); |
| 326 | |
| 327 | const MCRegisterInfo *getRegisterInfo() const { return RegInfo.get(); } |
| 328 | |
| 329 | /// Function used to handle default error reporting policy. Prints a error |
| 330 | /// message and returns Continue, so DWARF context ignores the error. |
| 331 | static ErrorPolicy defaultErrorHandler(Error E); |
| 332 | static std::unique_ptr<DWARFContext> |
| 333 | create(const object::ObjectFile &Obj, const LoadedObjectInfo *L = nullptr, |
| 334 | function_ref<ErrorPolicy(Error)> HandleError = defaultErrorHandler, |
| 335 | std::string DWPName = ""); |
| 336 | |
| 337 | static std::unique_ptr<DWARFContext> |
| 338 | create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, |
| 339 | uint8_t AddrSize, bool isLittleEndian = sys::IsLittleEndianHost); |
| 340 | |
| 341 | /// Loads register info for the architecture of the provided object file. |
| 342 | /// Improves readability of dumped DWARF expressions. Requires the caller to |
| 343 | /// have initialized the relevant target descriptions. |
| 344 | Error loadRegisterInfo(const object::ObjectFile &Obj); |
| 345 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 346 | /// Get address size from CUs. |
| 347 | /// TODO: refactor compile_units() to make this const. |
| 348 | uint8_t getCUAddrSize(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 349 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 350 | private: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 351 | /// Return the compile unit which contains instruction with provided |
| 352 | /// address. |
| 353 | DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address); |
| 354 | }; |
| 355 | |
| 356 | } // end namespace llvm |
| 357 | |
| 358 | #endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H |