1 /* 2 * Copyright 2009-2010, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2012-2013, Rene Gollent, rene@gollent.com. 4 * Distributed under the terms of the MIT License. 5 */ 6 #ifndef DWARF_FILE_H 7 #define DWARF_FILE_H 8 9 10 #include <ObjectList.h> 11 #include <Referenceable.h> 12 #include <util/DoublyLinkedList.h> 13 #include <util/OpenHashTable.h> 14 15 #include "DebugInfoEntries.h" 16 #include "TypeUnit.h" 17 18 19 struct AbbreviationEntry; 20 class AbbreviationTable; 21 class BVariant; 22 class CfaContext; 23 class CompilationUnit; 24 class DataReader; 25 class DwarfTargetInterface; 26 class ElfFile; 27 class ElfSection; 28 class TargetAddressRangeList; 29 class ValueLocation; 30 31 32 class DwarfFile : public BReferenceable, 33 public DoublyLinkedListLinkImpl<DwarfFile> { 34 public: 35 DwarfFile(); 36 ~DwarfFile(); 37 38 status_t StartLoading(const char* fileName, 39 BString& _requiredExternalFile); 40 status_t Load(uint8 addressSize, 41 const BString& externalFilePath); 42 status_t FinishLoading(); 43 44 const char* Name() const { return fName; } 45 ElfFile* GetElfFile() const { return fElfFile; } 46 47 bool HasFrameInformation() const 48 { return fDebugFrameSection != NULL 49 || fEHFrameSection != NULL; } 50 51 int32 CountCompilationUnits() const; 52 CompilationUnit* CompilationUnitAt(int32 index) const; 53 CompilationUnit* CompilationUnitForDIE( 54 const DebugInfoEntry* entry) const; 55 56 TargetAddressRangeList* ResolveRangeList(CompilationUnit* unit, 57 uint64 offset) const; 58 59 status_t UnwindCallFrame(CompilationUnit* unit, 60 uint8 addressSize, 61 DIESubprogram* subprogramEntry, 62 target_addr_t location, 63 const DwarfTargetInterface* inputInterface, 64 DwarfTargetInterface* outputInterface, 65 target_addr_t& _framePointer); 66 67 status_t EvaluateExpression(CompilationUnit* unit, 68 uint8 addressSize, 69 DIESubprogram* subprogramEntry, 70 const void* expression, 71 off_t expressionLength, 72 const DwarfTargetInterface* targetInterface, 73 target_addr_t instructionPointer, 74 target_addr_t framePointer, 75 target_addr_t valueToPush, bool pushValue, 76 target_addr_t& _result); 77 status_t ResolveLocation(CompilationUnit* unit, 78 uint8 addressSize, 79 DIESubprogram* subprogramEntry, 80 const LocationDescription* location, 81 const DwarfTargetInterface* targetInterface, 82 target_addr_t instructionPointer, 83 target_addr_t objectPointer, 84 bool hasObjectPointer, 85 target_addr_t framePointer, 86 target_addr_t relocationDelta, 87 ValueLocation& _result); 88 // The returned location will have DWARF 89 // semantics regarding register numbers and 90 // bit offsets/sizes (cf. bit pieces). 91 92 status_t EvaluateConstantValue(CompilationUnit* unit, 93 uint8 addressSize, 94 DIESubprogram* subprogramEntry, 95 const ConstantAttributeValue* value, 96 const DwarfTargetInterface* targetInterface, 97 target_addr_t instructionPointer, 98 target_addr_t framePointer, 99 BVariant& _result); 100 status_t EvaluateDynamicValue(CompilationUnit* unit, uint8 addressSize, 101 DIESubprogram* subprogramEntry, 102 const DynamicAttributeValue* value, 103 const DwarfTargetInterface* targetInterface, 104 target_addr_t instructionPointer, 105 target_addr_t framePointer, 106 BVariant& _result, DIEType** _type = NULL); 107 108 private: 109 struct ExpressionEvaluationContext; 110 struct FDEAugmentation; 111 struct CIEAugmentation; 112 struct FDELookupInfo; 113 114 typedef DoublyLinkedList<AbbreviationTable> AbbreviationTableList; 115 typedef BObjectList<CompilationUnit> CompilationUnitList; 116 typedef BOpenHashTable<TypeUnitTableHashDefinition> TypeUnitTable; 117 typedef BObjectList<FDELookupInfo> FDEInfoList; 118 119 private: 120 status_t _ParseDebugInfoSection(); 121 status_t _ParseTypesSection(); 122 status_t _ParseFrameSection(ElfSection* section, 123 uint8 addressSize, bool ehFrame, 124 FDEInfoList& infos); 125 status_t _ParseCompilationUnit(CompilationUnit* unit); 126 status_t _ParseTypeUnit(TypeUnit* unit); 127 status_t _ParseDebugInfoEntry(DataReader& dataReader, 128 BaseUnit* unit, 129 AbbreviationTable* abbreviationTable, 130 DebugInfoEntry*& _entry, 131 bool& _endOfEntryList, int level = 0); 132 status_t _FinishUnit(BaseUnit* unit); 133 status_t _ParseEntryAttributes(DataReader& dataReader, 134 BaseUnit* unit, 135 DebugInfoEntry* entry, 136 AbbreviationEntry& abbreviationEntry); 137 138 status_t _ParseLineInfo(CompilationUnit* unit); 139 140 status_t _UnwindCallFrame(CompilationUnit* unit, 141 uint8 addressSize, 142 DIESubprogram* subprogramEntry, 143 target_addr_t location, 144 const FDELookupInfo* info, 145 const DwarfTargetInterface* inputInterface, 146 DwarfTargetInterface* outputInterface, 147 target_addr_t& _framePointer); 148 149 status_t _ParseCIEHeader(ElfSection* debugFrameSection, 150 bool usingEHFrameSection, 151 CompilationUnit* unit, 152 uint8 addressSize, 153 CfaContext& context, off_t cieOffset, 154 CIEAugmentation& cieAugmentation, 155 DataReader& reader, 156 off_t& _cieRemaining); 157 status_t _ParseFrameInfoInstructions( 158 CompilationUnit* unit, CfaContext& context, 159 DataReader& dataReader, 160 CIEAugmentation& cieAugmentation); 161 162 status_t _ParsePublicTypesInfo(); 163 status_t _ParsePublicTypesInfo(DataReader& dataReader, 164 bool dwarf64); 165 166 status_t _GetAbbreviationTable(off_t offset, 167 AbbreviationTable*& _table); 168 169 DebugInfoEntry* _ResolveReference(BaseUnit* unit, 170 uint64 offset, 171 uint8 refType) const; 172 173 status_t _GetLocationExpression(CompilationUnit* unit, 174 const LocationDescription* location, 175 target_addr_t instructionPointer, 176 const void*& _expression, 177 off_t& _length) const; 178 status_t _FindLocationExpression(CompilationUnit* unit, 179 uint64 offset, target_addr_t address, 180 const void*& _expression, 181 off_t& _length) const; 182 183 status_t _LocateDebugInfo( 184 BString& _requiredExternalFileName, 185 const char* locatedFilePath = NULL); 186 187 status_t _GetDebugInfoPath(const char* fileName, 188 BString& _infoPath) const; 189 190 TypeUnitTableEntry* _GetTypeUnit(uint64 signature) const; 191 CompilationUnit* _GetContainingCompilationUnit( 192 off_t refAddr) const; 193 194 FDELookupInfo* _GetContainingFDEInfo( 195 target_addr_t offset) const; 196 197 FDELookupInfo* _GetContainingFDEInfo( 198 target_addr_t offset, 199 const FDEInfoList& infoList) const; 200 201 private: 202 friend struct DwarfFile::ExpressionEvaluationContext; 203 204 private: 205 char* fName; 206 char* fAlternateName; 207 ElfFile* fElfFile; 208 ElfFile* fAlternateElfFile; 209 ElfSection* fDebugInfoSection; 210 ElfSection* fDebugAbbrevSection; 211 ElfSection* fDebugStringSection; 212 ElfSection* fDebugRangesSection; 213 ElfSection* fDebugLineSection; 214 ElfSection* fDebugFrameSection; 215 ElfSection* fEHFrameSection; 216 ElfSection* fDebugLocationSection; 217 ElfSection* fDebugPublicTypesSection; 218 ElfSection* fDebugTypesSection; 219 AbbreviationTableList fAbbreviationTables; 220 DebugInfoEntryFactory fDebugInfoFactory; 221 CompilationUnitList fCompilationUnits; 222 TypeUnitTable fTypeUnits; 223 FDEInfoList fDebugFrameInfos; 224 FDEInfoList fEHFrameInfos; 225 bool fTypesSectionRequired; 226 bool fFinished; 227 bool fItaniumEHFrameFormat; 228 status_t fFinishError; 229 }; 230 231 232 #endif // DWARF_FILE_H 233