1/*
2 * Copyright 2009-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2012, 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
14#include "DebugInfoEntries.h"
15
16
17class AbbreviationEntry;
18class AbbreviationTable;
19class BVariant;
20class CfaContext;
21class CompilationUnit;
22class DataReader;
23class DwarfTargetInterface;
24class ElfFile;
25class ElfSection;
26class TargetAddressRangeList;
27class ValueLocation;
28
29
30class DwarfFile : public BReferenceable,
31	public DoublyLinkedListLinkImpl<DwarfFile> {
32public:
33								DwarfFile();
34								~DwarfFile();
35
36			status_t			Load(const char* fileName);
37			status_t			FinishLoading();
38
39			const char*			Name() const		{ return fName; }
40			ElfFile*			GetElfFile() const	{ return fElfFile; }
41
42			int32				CountCompilationUnits() const;
43			CompilationUnit*	CompilationUnitAt(int32 index) const;
44			CompilationUnit*	CompilationUnitForDIE(
45									const DebugInfoEntry* entry) const;
46
47			TargetAddressRangeList* ResolveRangeList(CompilationUnit* unit,
48									uint64 offset) const;
49
50			status_t			UnwindCallFrame(CompilationUnit* unit,
51									uint8 addressSize,
52									DIESubprogram* subprogramEntry,
53									target_addr_t location,
54									const DwarfTargetInterface* inputInterface,
55									DwarfTargetInterface* outputInterface,
56									target_addr_t& _framePointer);
57
58			status_t			EvaluateExpression(CompilationUnit* unit,
59									uint8 addressSize,
60									DIESubprogram* subprogramEntry,
61									const void* expression,
62									off_t expressionLength,
63									const DwarfTargetInterface* targetInterface,
64									target_addr_t instructionPointer,
65									target_addr_t framePointer,
66									target_addr_t valueToPush, bool pushValue,
67									target_addr_t& _result);
68			status_t			ResolveLocation(CompilationUnit* unit,
69									uint8 addressSize,
70									DIESubprogram* subprogramEntry,
71									const LocationDescription* location,
72									const DwarfTargetInterface* targetInterface,
73									target_addr_t instructionPointer,
74									target_addr_t objectPointer,
75									bool hasObjectPointer,
76									target_addr_t framePointer,
77									target_addr_t relocationDelta,
78									ValueLocation& _result);
79									// The returned location will have DWARF
80									// semantics regarding register numbers and
81									// bit offsets/sizes (cf. bit pieces).
82
83			status_t			EvaluateConstantValue(CompilationUnit* unit,
84									uint8 addressSize,
85									DIESubprogram* subprogramEntry,
86									const ConstantAttributeValue* value,
87									const DwarfTargetInterface* targetInterface,
88									target_addr_t instructionPointer,
89									target_addr_t framePointer,
90									BVariant& _result);
91			status_t			EvaluateDynamicValue(CompilationUnit* unit, uint8 addressSize,
92									DIESubprogram* subprogramEntry,
93									const DynamicAttributeValue* value,
94									const DwarfTargetInterface* targetInterface,
95									target_addr_t instructionPointer,
96									target_addr_t framePointer,
97									BVariant& _result, DIEType** _type = NULL);
98
99private:
100			struct ExpressionEvaluationContext;
101			struct FDEAugmentation;
102			struct CIEAugmentation;
103
104			typedef DoublyLinkedList<AbbreviationTable> AbbreviationTableList;
105			typedef BObjectList<CompilationUnit> CompilationUnitList;
106
107private:
108			status_t			_ParseCompilationUnit(CompilationUnit* unit);
109			status_t			_ParseDebugInfoEntry(DataReader& dataReader,
110									AbbreviationTable* abbreviationTable,
111									DebugInfoEntry*& _entry,
112									bool& _endOfEntryList, int level = 0);
113			status_t			_FinishCompilationUnit(CompilationUnit* unit);
114			status_t			_ParseEntryAttributes(DataReader& dataReader,
115									DebugInfoEntry* entry,
116									AbbreviationEntry& abbreviationEntry);
117
118			status_t			_ParseLineInfo(CompilationUnit* unit);
119
120			status_t			_UnwindCallFrame(bool usingEHFrameSection,
121									CompilationUnit* unit,
122									uint8 addressSize,
123									DIESubprogram* subprogramEntry,
124									target_addr_t location,
125									const DwarfTargetInterface* inputInterface,
126									DwarfTargetInterface* outputInterface,
127									target_addr_t& _framePointer);
128
129			status_t			_ParseCIEHeader(ElfSection* debugFrameSection,
130									bool usingEHFrameSection,
131									CompilationUnit* unit,
132									uint8 addressSize,
133									CfaContext& context, off_t cieOffset,
134									CIEAugmentation& cieAugmentation,
135									DataReader& reader,
136									off_t& _cieRemaining);
137			status_t			_ParseFrameInfoInstructions(
138									CompilationUnit* unit, CfaContext& context,
139									DataReader& dataReader,
140									CIEAugmentation& cieAugmentation);
141
142			status_t			_ParsePublicTypesInfo();
143			status_t			_ParsePublicTypesInfo(DataReader& dataReader,
144									bool dwarf64);
145
146			status_t			_GetAbbreviationTable(off_t offset,
147									AbbreviationTable*& _table);
148
149			DebugInfoEntry*		_ResolveReference(CompilationUnit* unit,
150									uint64 offset, bool localReference) const;
151
152			status_t			_GetLocationExpression(CompilationUnit* unit,
153									const LocationDescription* location,
154									target_addr_t instructionPointer,
155									const void*& _expression,
156									off_t& _length) const;
157			status_t			_FindLocationExpression(CompilationUnit* unit,
158									uint64 offset, target_addr_t address,
159									const void*& _expression,
160									off_t& _length) const;
161
162			status_t			_LocateDebugInfo();
163			status_t			_GetDebugInfoPath(const char* fileName,
164									BString& _infoPath);
165
166private:
167			friend class 		DwarfFile::ExpressionEvaluationContext;
168
169private:
170			char*				fName;
171			char*				fAlternateName;
172			ElfFile*			fElfFile;
173			ElfFile*			fAlternateElfFile;
174			ElfSection*			fDebugInfoSection;
175			ElfSection*			fDebugAbbrevSection;
176			ElfSection*			fDebugStringSection;
177			ElfSection*			fDebugRangesSection;
178			ElfSection*			fDebugLineSection;
179			ElfSection*			fDebugFrameSection;
180			ElfSection*			fEHFrameSection;
181			ElfSection*			fDebugLocationSection;
182			ElfSection*			fDebugPublicTypesSection;
183			AbbreviationTableList fAbbreviationTables;
184			DebugInfoEntryFactory fDebugInfoFactory;
185			CompilationUnitList	fCompilationUnits;
186			CompilationUnit*	fCurrentCompilationUnit;
187			bool				fFinished;
188			status_t			fFinishError;
189};
190
191
192#endif	// DWARF_FILE_H
193