LLVM API Documentation

DWARFContext.h
Go to the documentation of this file.
00001 //===-- DWARFContext.h ------------------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===/
00009 
00010 #ifndef LLVM_LIB_DEBUGINFO_DWARFCONTEXT_H
00011 #define LLVM_LIB_DEBUGINFO_DWARFCONTEXT_H
00012 
00013 #include "DWARFCompileUnit.h"
00014 #include "DWARFDebugAranges.h"
00015 #include "DWARFDebugFrame.h"
00016 #include "DWARFDebugLine.h"
00017 #include "DWARFDebugLoc.h"
00018 #include "DWARFDebugRangeList.h"
00019 #include "DWARFTypeUnit.h"
00020 #include "llvm/ADT/MapVector.h"
00021 #include "llvm/ADT/SmallVector.h"
00022 #include "llvm/DebugInfo/DIContext.h"
00023 
00024 namespace llvm {
00025 
00026 /// DWARFContext
00027 /// This data structure is the top level entity that deals with dwarf debug
00028 /// information parsing. The actual data is supplied through pure virtual
00029 /// methods that a concrete implementation provides.
00030 class DWARFContext : public DIContext {
00031 
00032   DWARFUnitSection<DWARFCompileUnit> CUs;
00033   DWARFUnitSection<DWARFTypeUnit> TUs;
00034   std::unique_ptr<DWARFDebugAbbrev> Abbrev;
00035   std::unique_ptr<DWARFDebugLoc> Loc;
00036   std::unique_ptr<DWARFDebugAranges> Aranges;
00037   std::unique_ptr<DWARFDebugLine> Line;
00038   std::unique_ptr<DWARFDebugFrame> DebugFrame;
00039 
00040   DWARFUnitSection<DWARFCompileUnit> DWOCUs;
00041   DWARFUnitSection<DWARFTypeUnit> DWOTUs;
00042   std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
00043   std::unique_ptr<DWARFDebugLocDWO> LocDWO;
00044 
00045   DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
00046   DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
00047 
00048   /// Read compile units from the debug_info section (if necessary)
00049   /// and store them in CUs.
00050   void parseCompileUnits();
00051 
00052   /// Read type units from the debug_types sections (if necessary)
00053   /// and store them in TUs.
00054   void parseTypeUnits();
00055 
00056   /// Read compile units from the debug_info.dwo section (if necessary)
00057   /// and store them in DWOCUs.
00058   void parseDWOCompileUnits();
00059 
00060   /// Read type units from the debug_types.dwo section (if necessary)
00061   /// and store them in DWOTUs.
00062   void parseDWOTypeUnits();
00063 
00064 public:
00065   struct Section {
00066     StringRef Data;
00067     RelocAddrMap Relocs;
00068   };
00069 
00070   DWARFContext() : DIContext(CK_DWARF) {}
00071 
00072   static bool classof(const DIContext *DICtx) {
00073     return DICtx->getKind() == CK_DWARF;
00074   }
00075 
00076   void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
00077 
00078   typedef DWARFUnitSection<DWARFCompileUnit>::iterator_range cu_iterator_range;
00079   typedef DWARFUnitSection<DWARFTypeUnit>::iterator_range tu_iterator_range;
00080 
00081   /// Get compile units in this context.
00082   cu_iterator_range compile_units() {
00083     parseCompileUnits();
00084     return cu_iterator_range(CUs.begin(), CUs.end());
00085   }
00086 
00087   /// Get type units in this context.
00088   tu_iterator_range type_units() {
00089     parseTypeUnits();
00090     return tu_iterator_range(TUs.begin(), TUs.end());
00091   }
00092 
00093   /// Get compile units in the DWO context.
00094   cu_iterator_range dwo_compile_units() {
00095     parseDWOCompileUnits();
00096     return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
00097   }
00098 
00099   /// Get type units in the DWO context.
00100   tu_iterator_range dwo_type_units() {
00101     parseDWOTypeUnits();
00102     return tu_iterator_range(DWOTUs.begin(), DWOTUs.end());
00103   }
00104 
00105   /// Get the number of compile units in this context.
00106   unsigned getNumCompileUnits() {
00107     parseCompileUnits();
00108     return CUs.size();
00109   }
00110 
00111   /// Get the number of compile units in this context.
00112   unsigned getNumTypeUnits() {
00113     parseTypeUnits();
00114     return TUs.size();
00115   }
00116 
00117   /// Get the number of compile units in the DWO context.
00118   unsigned getNumDWOCompileUnits() {
00119     parseDWOCompileUnits();
00120     return DWOCUs.size();
00121   }
00122 
00123   /// Get the number of compile units in the DWO context.
00124   unsigned getNumDWOTypeUnits() {
00125     parseDWOTypeUnits();
00126     return DWOTUs.size();
00127   }
00128 
00129   /// Get the compile unit at the specified index for this compile unit.
00130   DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
00131     parseCompileUnits();
00132     return CUs[index].get();
00133   }
00134 
00135   /// Get the compile unit at the specified index for the DWO compile units.
00136   DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
00137     parseDWOCompileUnits();
00138     return DWOCUs[index].get();
00139   }
00140 
00141   /// Get a pointer to the parsed DebugAbbrev object.
00142   const DWARFDebugAbbrev *getDebugAbbrev();
00143 
00144   /// Get a pointer to the parsed DebugLoc object.
00145   const DWARFDebugLoc *getDebugLoc();
00146 
00147   /// Get a pointer to the parsed dwo abbreviations object.
00148   const DWARFDebugAbbrev *getDebugAbbrevDWO();
00149 
00150   /// Get a pointer to the parsed DebugLoc object.
00151   const DWARFDebugLocDWO *getDebugLocDWO();
00152 
00153   /// Get a pointer to the parsed DebugAranges object.
00154   const DWARFDebugAranges *getDebugAranges();
00155 
00156   /// Get a pointer to the parsed frame information object.
00157   const DWARFDebugFrame *getDebugFrame();
00158 
00159   /// Get a pointer to a parsed line table corresponding to a compile unit.
00160   const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
00161 
00162   DILineInfo getLineInfoForAddress(uint64_t Address,
00163       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
00164   DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
00165       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
00166   DIInliningInfo getInliningInfoForAddress(uint64_t Address,
00167       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
00168 
00169   virtual bool isLittleEndian() const = 0;
00170   virtual uint8_t getAddressSize() const = 0;
00171   virtual const Section &getInfoSection() = 0;
00172   typedef MapVector<object::SectionRef, Section,
00173                     std::map<object::SectionRef, unsigned> > TypeSectionMap;
00174   virtual const TypeSectionMap &getTypesSections() = 0;
00175   virtual StringRef getAbbrevSection() = 0;
00176   virtual const Section &getLocSection() = 0;
00177   virtual const Section &getLocDWOSection() = 0;
00178   virtual StringRef getARangeSection() = 0;
00179   virtual StringRef getDebugFrameSection() = 0;
00180   virtual const Section &getLineSection() = 0;
00181   virtual const Section &getLineDWOSection() = 0;
00182   virtual StringRef getStringSection() = 0;
00183   virtual StringRef getRangeSection() = 0;
00184   virtual StringRef getPubNamesSection() = 0;
00185   virtual StringRef getPubTypesSection() = 0;
00186   virtual StringRef getGnuPubNamesSection() = 0;
00187   virtual StringRef getGnuPubTypesSection() = 0;
00188 
00189   // Sections for DWARF5 split dwarf proposal.
00190   virtual const Section &getInfoDWOSection() = 0;
00191   virtual const TypeSectionMap &getTypesDWOSections() = 0;
00192   virtual StringRef getAbbrevDWOSection() = 0;
00193   virtual StringRef getStringDWOSection() = 0;
00194   virtual StringRef getStringOffsetDWOSection() = 0;
00195   virtual StringRef getRangeDWOSection() = 0;
00196   virtual StringRef getAddrSection() = 0;
00197 
00198   static bool isSupportedVersion(unsigned version) {
00199     return version == 2 || version == 3 || version == 4;
00200   }
00201 private:
00202   /// Return the compile unit that includes an offset (relative to .debug_info).
00203   DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
00204 
00205   /// Return the compile unit which contains instruction with provided
00206   /// address.
00207   DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
00208 };
00209 
00210 /// DWARFContextInMemory is the simplest possible implementation of a
00211 /// DWARFContext. It assumes all content is available in memory and stores
00212 /// pointers to it.
00213 class DWARFContextInMemory : public DWARFContext {
00214   virtual void anchor();
00215   bool IsLittleEndian;
00216   uint8_t AddressSize;
00217   Section InfoSection;
00218   TypeSectionMap TypesSections;
00219   StringRef AbbrevSection;
00220   Section LocSection;
00221   Section LocDWOSection;
00222   StringRef ARangeSection;
00223   StringRef DebugFrameSection;
00224   Section LineSection;
00225   Section LineDWOSection;
00226   StringRef StringSection;
00227   StringRef RangeSection;
00228   StringRef PubNamesSection;
00229   StringRef PubTypesSection;
00230   StringRef GnuPubNamesSection;
00231   StringRef GnuPubTypesSection;
00232 
00233   // Sections for DWARF5 split dwarf proposal.
00234   Section InfoDWOSection;
00235   TypeSectionMap TypesDWOSections;
00236   StringRef AbbrevDWOSection;
00237   StringRef StringDWOSection;
00238   StringRef StringOffsetDWOSection;
00239   StringRef RangeDWOSection;
00240   StringRef AddrSection;
00241 
00242   SmallVector<SmallString<32>, 4> UncompressedSections;
00243 
00244 public:
00245   DWARFContextInMemory(object::ObjectFile &);
00246   bool isLittleEndian() const override { return IsLittleEndian; }
00247   uint8_t getAddressSize() const override { return AddressSize; }
00248   const Section &getInfoSection() override { return InfoSection; }
00249   const TypeSectionMap &getTypesSections() override { return TypesSections; }
00250   StringRef getAbbrevSection() override { return AbbrevSection; }
00251   const Section &getLocSection() override { return LocSection; }
00252   const Section &getLocDWOSection() override { return LocDWOSection; }
00253   StringRef getARangeSection() override { return ARangeSection; }
00254   StringRef getDebugFrameSection() override { return DebugFrameSection; }
00255   const Section &getLineSection() override { return LineSection; }
00256   const Section &getLineDWOSection() override { return LineDWOSection; }
00257   StringRef getStringSection() override { return StringSection; }
00258   StringRef getRangeSection() override { return RangeSection; }
00259   StringRef getPubNamesSection() override { return PubNamesSection; }
00260   StringRef getPubTypesSection() override { return PubTypesSection; }
00261   StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
00262   StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
00263 
00264   // Sections for DWARF5 split dwarf proposal.
00265   const Section &getInfoDWOSection() override { return InfoDWOSection; }
00266   const TypeSectionMap &getTypesDWOSections() override {
00267     return TypesDWOSections;
00268   }
00269   StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
00270   StringRef getStringDWOSection() override { return StringDWOSection; }
00271   StringRef getStringOffsetDWOSection() override {
00272     return StringOffsetDWOSection;
00273   }
00274   StringRef getRangeDWOSection() override { return RangeDWOSection; }
00275   StringRef getAddrSection() override {
00276     return AddrSection;
00277   }
00278 };
00279 
00280 }
00281 
00282 #endif