1 #ifndef ELF_H
2 #define ELF_H
3 
4 enum LocationType {
5   LOCATION_register,
6   LOCATION_memory,
7   LOCATION_value
8 };
9 
10 #define DW_ATE_boolean       0x02
11 #define DW_ATE_signed        0x05
12 #define DW_ATE_unsigned      0x07
13 #define DW_ATE_unsigned_char 0x08
14 
15 struct ELFHeader {
16   u32 magic;
17   u8 clazz;
18   u8 data;
19   u8 version;
20   u8 pad[9];
21   u16 e_type;
22   u16 e_machine;
23   u32 e_version;
24   u32 e_entry;
25   u32 e_phoff;
26   u32 e_shoff;
27   u32 e_flags;
28   u16 e_ehsize;
29   u16 e_phentsize;
30   u16 e_phnum;
31   u16 e_shentsize;
32   u16 e_shnum;
33   u16 e_shstrndx;
34 };
35 
36 struct ELFProgramHeader {
37   u32 type;
38   u32 offset;
39   u32 vaddr;
40   u32 paddr;
41   u32 filesz;
42   u32 memsz;
43   u32 flags;
44   u32 align;
45 };
46 
47 struct ELFSectionHeader {
48   u32 name;
49   u32 type;
50   u32 flags;
51   u32 addr;
52   u32 offset;
53   u32 size;
54   u32 link;
55   u32 info;
56   u32 addralign;
57   u32 entsize;
58 };
59 
60 struct ELFSymbol {
61   u32 name;
62   u32 value;
63   u32 size;
64   u8 info;
65   u8 other;
66   u16 shndx;
67 };
68 
69 struct ELFBlock {
70   int length;
71   u8 *data;
72 };
73 
74 struct ELFAttr {
75   u32 name;
76   u32 form;
77   union {
78     u32 value;
79     char *string;
80     u8 *data;
81     bool flag;
82     ELFBlock *block;
83   };
84 };
85 
86 struct ELFAbbrev {
87   u32 number;
88   u32 tag;
89   bool hasChildren;
90   int numAttrs;
91   ELFAttr *attrs;
92   ELFAbbrev *next;
93 };
94 
95 enum TypeEnum {
96   TYPE_base,
97   TYPE_pointer,
98   TYPE_function,
99   TYPE_void,
100   TYPE_array,
101   TYPE_struct,
102   TYPE_reference,
103   TYPE_enum,
104   TYPE_union
105 };
106 
107 struct Type;
108 struct Object;
109 
110 struct FunctionType {
111   Type *returnType;
112   Object *args;
113 };
114 
115 struct Member {
116   char *name;
117   Type *type;
118   int bitSize;
119   int bitOffset;
120   int byteSize;
121   ELFBlock *location;
122 };
123 
124 struct Struct {
125   int memberCount;
126   Member *members;
127 };
128 
129 struct Array {
130   Type *type;
131   int maxBounds;
132   int *bounds;
133 };
134 
135 struct EnumMember {
136   char *name;
137   u32 value;
138 };
139 
140 struct Enum {
141   int count;
142   EnumMember *members;
143 };
144 
145 struct Type {
146   u32 offset;
147   TypeEnum type;
148   const char *name;
149   int encoding;
150   int size;
151   int bitSize;
152   union {
153     Type *pointer;
154     FunctionType *function;
155     Array *array;
156     Struct *structure;
157     Enum *enumeration;
158   };
159   Type *next;
160 };
161 
162 struct Object {
163   char *name;
164   int file;
165   int line;
166   bool external;
167   Type *type;
168   ELFBlock *location;
169   u32 startScope;
170   u32 endScope;
171   Object *next;
172 };
173 
174 struct Function {
175   char *name;
176   u32 lowPC;
177   u32 highPC;
178   int file;
179   int line;
180   bool external;
181   Type *returnType;
182   Object *parameters;
183   Object *variables;
184   ELFBlock *frameBase;
185   Function *next;
186 };
187 
188 struct LineInfoItem {
189   u32 address;
190   char *file;
191   int line;
192 };
193 
194 struct LineInfo {
195   int fileCount;
196   char **files;
197   int number;
198   LineInfoItem *lines;
199 };
200 
201 struct ARange {
202   u32 lowPC;
203   u32 highPC;
204 };
205 
206 struct ARanges {
207   u32 offset;
208   int count;
209   ARange *ranges;
210 };
211 
212 struct CompileUnit {
213   u32 length;
214   u8 *top;
215   u32 offset;
216   ELFAbbrev **abbrevs;
217   ARanges *ranges;
218   char *name;
219   char *compdir;
220   u32 lowPC;
221   u32 highPC;
222   bool hasLineInfo;
223   u32 lineInfo;
224   LineInfo *lineInfoTable;
225   Function *functions;
226   Function *lastFunction;
227   Object *variables;
228   Type *types;
229   CompileUnit *next;
230 };
231 
232 struct DebugInfo {
233   u8 *debugfile;
234   u8 *abbrevdata;
235   u8 *debugdata;
236   u8 *infodata;
237   int numRanges;
238   ARanges *ranges;
239 };
240 
241 struct Symbol {
242   const char *name;
243   int type;
244   int binding;
245   u32 address;
246   u32 value;
247   u32 size;
248 };
249 
250 extern u32 elfReadLEB128(u8 *, int *);
251 extern s32 elfReadSignedLEB128(u8 *, int *);
252 extern bool elfRead(const char *, int &, FILE *f);
253 extern bool elfGetSymbolAddress(const char *,u32 *, u32 *, int *);
254 extern const char *elfGetAddressSymbol(u32);
255 extern const char *elfGetSymbol(int, u32 *, u32 *, int *);
256 extern void elfCleanUp();
257 extern bool elfGetCurrentFunction(u32, Function **, CompileUnit **c);
258 extern bool elfGetObject(const char *, Function *, CompileUnit *, Object **);
259 extern bool elfFindLineInUnit(u32 *, CompileUnit *, int);
260 extern bool elfFindLineInModule(u32 *, const char *, int);
261 u32 elfDecodeLocation(Function *, ELFBlock *, LocationType *);
262 u32 elfDecodeLocation(Function *, ELFBlock *, LocationType *, u32);
263 int elfFindLine(CompileUnit *unit, Function *func, u32 addr, const char **);
264 
265 #endif // ELF_H
266