@@ -1,769 +1,769 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the SocExplorer Software |
|
3 | 3 | -- Copyright (C) 2013, Plasma Physics Laboratory - CNRS |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | -------------------------------------------------------------------------------*/ |
|
19 | 19 | /*-- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@lpp.polytechnique.fr |
|
21 | 21 | ----------------------------------------------------------------------------*/ |
|
22 | 22 | #include "elfparser.h" |
|
23 | 23 | #include <sys/types.h> |
|
24 | 24 | #include <sys/stat.h> |
|
25 | 25 | #include <fcntl.h> |
|
26 | 26 | #include <unistd.h> |
|
27 | 27 | |
|
28 | 28 | QString elfresolveMachine(Elf64_Half e_machine) |
|
29 | 29 | { |
|
30 | 30 | QString machineName; |
|
31 | 31 | //Update from with bash script don't write it by yourself! |
|
32 | 32 | switch(e_machine) |
|
33 | 33 | { |
|
34 | 34 | case EM_NONE: |
|
35 | 35 | machineName = " No machine "; |
|
36 | 36 | break; |
|
37 | 37 | case EM_M32: |
|
38 | 38 | machineName = " AT&T WE 32100 "; |
|
39 | 39 | break; |
|
40 | 40 | case EM_SPARC: |
|
41 | 41 | machineName = " SUN SPARC "; |
|
42 | 42 | break; |
|
43 | 43 | case EM_386: |
|
44 | 44 | machineName = " Intel 80386 "; |
|
45 | 45 | break; |
|
46 | 46 | case EM_68K: |
|
47 | 47 | machineName = " Motorola m68k family "; |
|
48 | 48 | break; |
|
49 | 49 | case EM_88K: |
|
50 | 50 | machineName = " Motorola m88k family "; |
|
51 | 51 | break; |
|
52 | 52 | case EM_860: |
|
53 | 53 | machineName = " Intel 80860 "; |
|
54 | 54 | break; |
|
55 | 55 | case EM_MIPS: |
|
56 | 56 | machineName = " MIPS R3000 big-endian "; |
|
57 | 57 | break; |
|
58 | 58 | case EM_S370: |
|
59 | 59 | machineName = " IBM System/370 "; |
|
60 | 60 | break; |
|
61 | 61 | case EM_MIPS_RS3_LE: |
|
62 | 62 | machineName = " MIPS R3000 little-endian "; |
|
63 | 63 | break; |
|
64 | 64 | case EM_PARISC: |
|
65 | 65 | machineName = " HPPA "; |
|
66 | 66 | break; |
|
67 | 67 | case EM_VPP500: |
|
68 | 68 | machineName = " Fujitsu VPP500 "; |
|
69 | 69 | break; |
|
70 | 70 | case EM_SPARC32PLUS: |
|
71 | 71 | machineName = " Sun's \"v8plus\" "; |
|
72 | 72 | break; |
|
73 | 73 | case EM_960: |
|
74 | 74 | machineName = " Intel 80960 "; |
|
75 | 75 | break; |
|
76 | 76 | case EM_PPC: |
|
77 | 77 | machineName = " PowerPC "; |
|
78 | 78 | break; |
|
79 | 79 | case EM_PPC64: |
|
80 | 80 | machineName = " PowerPC 64-bit "; |
|
81 | 81 | break; |
|
82 | 82 | case EM_S390: |
|
83 | 83 | machineName = " IBM S390 "; |
|
84 | 84 | break; |
|
85 | 85 | case EM_V800: |
|
86 | 86 | machineName = " NEC V800 series "; |
|
87 | 87 | break; |
|
88 | 88 | case EM_FR20: |
|
89 | 89 | machineName = " Fujitsu FR20 "; |
|
90 | 90 | break; |
|
91 | 91 | case EM_RH32: |
|
92 | 92 | machineName = " TRW RH-32 "; |
|
93 | 93 | break; |
|
94 | 94 | case EM_RCE: |
|
95 | 95 | machineName = " Motorola RCE "; |
|
96 | 96 | break; |
|
97 | 97 | case EM_ARM: |
|
98 | 98 | machineName = " ARM "; |
|
99 | 99 | break; |
|
100 | 100 | case EM_FAKE_ALPHA: |
|
101 | 101 | machineName = " Digital Alpha "; |
|
102 | 102 | break; |
|
103 | 103 | case EM_SH: |
|
104 | 104 | machineName = " Hitachi SH "; |
|
105 | 105 | break; |
|
106 | 106 | case EM_SPARCV9: |
|
107 | 107 | machineName = " SPARC v9 64-bit "; |
|
108 | 108 | break; |
|
109 | 109 | case EM_TRICORE: |
|
110 | 110 | machineName = " Siemens Tricore "; |
|
111 | 111 | break; |
|
112 | 112 | case EM_ARC: |
|
113 | 113 | machineName = " Argonaut RISC Core "; |
|
114 | 114 | break; |
|
115 | 115 | case EM_H8_300: |
|
116 | 116 | machineName = " Hitachi H8/300 "; |
|
117 | 117 | break; |
|
118 | 118 | case EM_H8_300H: |
|
119 | 119 | machineName = " Hitachi H8/300H "; |
|
120 | 120 | break; |
|
121 | 121 | case EM_H8S: |
|
122 | 122 | machineName = " Hitachi H8S "; |
|
123 | 123 | break; |
|
124 | 124 | case EM_H8_500: |
|
125 | 125 | machineName = " Hitachi H8/500 "; |
|
126 | 126 | break; |
|
127 | 127 | case EM_IA_64: |
|
128 | 128 | machineName = " Intel Merced "; |
|
129 | 129 | break; |
|
130 | 130 | case EM_MIPS_X: |
|
131 | 131 | machineName = " Stanford MIPS-X "; |
|
132 | 132 | break; |
|
133 | 133 | case EM_COLDFIRE: |
|
134 | 134 | machineName = " Motorola Coldfire "; |
|
135 | 135 | break; |
|
136 | 136 | case EM_68HC12: |
|
137 | 137 | machineName = " Motorola M68HC12 "; |
|
138 | 138 | break; |
|
139 | 139 | case EM_MMA: |
|
140 | 140 | machineName = " Fujitsu MMA Multimedia Accelerator"; |
|
141 | 141 | break; |
|
142 | 142 | case EM_PCP: |
|
143 | 143 | machineName = " Siemens PCP "; |
|
144 | 144 | break; |
|
145 | 145 | case EM_NCPU: |
|
146 | 146 | machineName = " Sony nCPU embeeded RISC "; |
|
147 | 147 | break; |
|
148 | 148 | case EM_NDR1: |
|
149 | 149 | machineName = " Denso NDR1 microprocessor "; |
|
150 | 150 | break; |
|
151 | 151 | case EM_STARCORE: |
|
152 | 152 | machineName = " Motorola Start*Core processor "; |
|
153 | 153 | break; |
|
154 | 154 | case EM_ME16: |
|
155 | 155 | machineName = " Toyota ME16 processor "; |
|
156 | 156 | break; |
|
157 | 157 | case EM_ST100: |
|
158 | 158 | machineName = " STMicroelectronic ST100 processor "; |
|
159 | 159 | break; |
|
160 | 160 | case EM_TINYJ: |
|
161 | 161 | machineName = " Advanced Logic Corp. Tinyj emb.fam"; |
|
162 | 162 | break; |
|
163 | 163 | case EM_X86_64: |
|
164 | 164 | machineName = " AMD x86-64 architecture "; |
|
165 | 165 | break; |
|
166 | 166 | case EM_PDSP: |
|
167 | 167 | machineName = " Sony DSP Processor "; |
|
168 | 168 | break; |
|
169 | 169 | case EM_FX66: |
|
170 | 170 | machineName = " Siemens FX66 microcontroller "; |
|
171 | 171 | break; |
|
172 | 172 | case EM_ST9PLUS: |
|
173 | 173 | machineName = " STMicroelectronics ST9+ 8/16 mc "; |
|
174 | 174 | break; |
|
175 | 175 | case EM_ST7: |
|
176 | 176 | machineName = " STmicroelectronics ST7 8 bit mc "; |
|
177 | 177 | break; |
|
178 | 178 | case EM_68HC16: |
|
179 | 179 | machineName = " Motorola MC68HC16 microcontroller "; |
|
180 | 180 | break; |
|
181 | 181 | case EM_68HC11: |
|
182 | 182 | machineName = " Motorola MC68HC11 microcontroller "; |
|
183 | 183 | break; |
|
184 | 184 | case EM_68HC08: |
|
185 | 185 | machineName = " Motorola MC68HC08 microcontroller "; |
|
186 | 186 | break; |
|
187 | 187 | case EM_68HC05: |
|
188 | 188 | machineName = " Motorola MC68HC05 microcontroller "; |
|
189 | 189 | break; |
|
190 | 190 | case EM_SVX: |
|
191 | 191 | machineName = " Silicon Graphics SVx "; |
|
192 | 192 | break; |
|
193 | 193 | case EM_ST19: |
|
194 | 194 | machineName = " STMicroelectronics ST19 8 bit mc "; |
|
195 | 195 | break; |
|
196 | 196 | case EM_VAX: |
|
197 | 197 | machineName = " Digital VAX "; |
|
198 | 198 | break; |
|
199 | 199 | case EM_CRIS: |
|
200 | 200 | machineName = " Axis Communications 32-bit embedded processor "; |
|
201 | 201 | break; |
|
202 | 202 | case EM_JAVELIN: |
|
203 | 203 | machineName = " Infineon Technologies 32-bit embedded processor "; |
|
204 | 204 | break; |
|
205 | 205 | case EM_FIREPATH: |
|
206 | 206 | machineName = " Element 14 64-bit DSP Processor "; |
|
207 | 207 | break; |
|
208 | 208 | case EM_ZSP: |
|
209 | 209 | machineName = " LSI Logic 16-bit DSP Processor "; |
|
210 | 210 | break; |
|
211 | 211 | case EM_MMIX: |
|
212 | 212 | machineName = " Donald Knuth's educational 64-bit processor "; |
|
213 | 213 | break; |
|
214 | 214 | case EM_HUANY: |
|
215 | 215 | machineName = " Harvard University machine-independent object files "; |
|
216 | 216 | break; |
|
217 | 217 | case EM_PRISM: |
|
218 | 218 | machineName = " SiTera Prism "; |
|
219 | 219 | break; |
|
220 | 220 | case EM_AVR: |
|
221 | 221 | machineName = " Atmel AVR 8-bit microcontroller "; |
|
222 | 222 | break; |
|
223 | 223 | case EM_FR30: |
|
224 | 224 | machineName = " Fujitsu FR30 "; |
|
225 | 225 | break; |
|
226 | 226 | case EM_D10V: |
|
227 | 227 | machineName = " Mitsubishi D10V "; |
|
228 | 228 | break; |
|
229 | 229 | case EM_D30V: |
|
230 | 230 | machineName = " Mitsubishi D30V "; |
|
231 | 231 | break; |
|
232 | 232 | case EM_V850: |
|
233 | 233 | machineName = " NEC v850 "; |
|
234 | 234 | break; |
|
235 | 235 | case EM_M32R: |
|
236 | 236 | machineName = " Mitsubishi M32R "; |
|
237 | 237 | break; |
|
238 | 238 | case EM_MN10300: |
|
239 | 239 | machineName = " Matsushita MN10300 "; |
|
240 | 240 | break; |
|
241 | 241 | case EM_MN10200: |
|
242 | 242 | machineName = " Matsushita MN10200 "; |
|
243 | 243 | break; |
|
244 | 244 | case EM_PJ: |
|
245 | 245 | machineName = " picoJava "; |
|
246 | 246 | break; |
|
247 | 247 | case EM_OPENRISC: |
|
248 | 248 | machineName = " OpenRISC 32-bit embedded processor "; |
|
249 | 249 | break; |
|
250 | 250 | case EM_ARC_A5: |
|
251 | 251 | machineName = " ARC Cores Tangent-A5 "; |
|
252 | 252 | break; |
|
253 | 253 | case EM_XTENSA: |
|
254 | 254 | machineName = " Tensilica Xtensa Architecture "; |
|
255 | 255 | break; |
|
256 | 256 | case EM_AARCH64: |
|
257 | 257 | machineName = " ARM AARCH64 "; |
|
258 | 258 | break; |
|
259 | 259 | case EM_TILEPRO: |
|
260 | 260 | machineName = " Tilera TILEPro "; |
|
261 | 261 | break; |
|
262 | 262 | case EM_MICROBLAZE: |
|
263 | 263 | machineName = " Xilinx MicroBlaze "; |
|
264 | 264 | break; |
|
265 | 265 | case EM_TILEGX: |
|
266 | 266 | machineName = " Tilera TILE-Gx "; |
|
267 | 267 | break; |
|
268 | 268 | case EM_NUM: |
|
269 | 269 | machineName = ""; |
|
270 | 270 | break; |
|
271 | 271 | default: |
|
272 | 272 | machineName ="Unknow Machine"; |
|
273 | 273 | break; |
|
274 | 274 | } |
|
275 | 275 | return machineName; |
|
276 | 276 | } |
|
277 | 277 | |
|
278 | 278 | |
|
279 | 279 | elfparser::elfparser() |
|
280 | 280 | { |
|
281 | 281 | this->opened = false; |
|
282 | 282 | this->type_elf = false; |
|
283 | 283 | this->elfFile = NULL; |
|
284 | 284 | this->e = NULL; |
|
285 | 285 | } |
|
286 | 286 | |
|
287 | 287 | |
|
288 | 288 | int elfparser::setFilename(const QString &name) |
|
289 | 289 | { |
|
290 | 290 | this->closeFile(); |
|
291 | 291 | if(elf_version(EV_CURRENT)==EV_NONE)return 0; |
|
292 | 292 | #ifdef _ELF_WINDOWS_ |
|
293 | 293 | this->elfFile = open(name.toStdString().c_str(),O_RDONLY|O_BINARY ,0); |
|
294 | 294 | #else |
|
295 | 295 | this->elfFile = open(name.toStdString().c_str(),O_RDONLY ,0); |
|
296 | 296 | #endif |
|
297 | 297 | if(this->elfFile==NULL)return 0; |
|
298 | 298 | this->e = elf_begin(this->elfFile,ELF_C_READ,NULL); |
|
299 | 299 | if(this->e==NULL)return 0; |
|
300 | 300 | this->ek = elf_kind(this->e); |
|
301 | 301 | gelf_getehdr (this->e, &this->ehdr ); |
|
302 | 302 | elf_getshdrstrndx (this->e, &this->shstrndx); |
|
303 | 303 | this->updateSegments(); |
|
304 | 304 | this->updateSections(); |
|
305 | 305 | return 1; |
|
306 | 306 | } |
|
307 | 307 | |
|
308 | 308 | |
|
309 | 309 | int elfparser::closeFile() |
|
310 | 310 | { |
|
311 | 311 | if(this->elfFile!=NULL) |
|
312 | 312 | { |
|
313 | 313 | if(this->e!=NULL) |
|
314 | 314 | { |
|
315 | 315 | elf_end(this->e); |
|
316 | 316 | this->e = NULL; |
|
317 | 317 | } |
|
318 | 318 | close(this->elfFile); |
|
319 | 319 | this->elfFile = NULL; |
|
320 | 320 | } |
|
321 | 321 | return 0; |
|
322 | 322 | } |
|
323 | 323 | |
|
324 | 324 | QString elfparser::getClass() |
|
325 | 325 | { |
|
326 | 326 | if(this->e!=NULL) |
|
327 | 327 | { |
|
328 | 328 | int eclass = gelf_getclass(this->e); |
|
329 | 329 | if(eclass==ELFCLASS32)return "ELF32"; |
|
330 | 330 | if(eclass==ELFCLASS64)return "ELF64"; |
|
331 | 331 | } |
|
332 | 332 | return "none"; |
|
333 | 333 | } |
|
334 | 334 | |
|
335 | 335 | |
|
336 | 336 | bool elfparser::isopened() |
|
337 | 337 | { |
|
338 | 338 | return this->opened; |
|
339 | 339 | } |
|
340 | 340 | |
|
341 | 341 | |
|
342 | 342 | bool elfparser::iself() |
|
343 | 343 | { |
|
344 | 344 | return this->type_elf; |
|
345 | 345 | } |
|
346 | 346 | |
|
347 | 347 | |
|
348 | 348 | QString elfparser::getArchitecture() |
|
349 | 349 | { |
|
350 | 350 | if(this->e!=NULL) |
|
351 | 351 | { |
|
352 | 352 | return elfresolveMachine(this->ehdr.e_machine); |
|
353 | 353 | } |
|
354 | 354 | return ""; |
|
355 | 355 | } |
|
356 | 356 | |
|
357 | 357 | |
|
358 | 358 | QString elfparser::getType() |
|
359 | 359 | { |
|
360 | 360 | QString kind(""); |
|
361 | 361 | if(this->e!=NULL) |
|
362 | 362 | { |
|
363 | 363 | switch(this->ek) |
|
364 | 364 | { |
|
365 | 365 | case ELF_K_AR: |
|
366 | 366 | kind = "Archive"; |
|
367 | 367 | break; |
|
368 | 368 | case ELF_K_ELF: |
|
369 | 369 | kind = "Elf"; |
|
370 | 370 | break; |
|
371 | 371 | case ELF_K_COFF: |
|
372 | 372 | kind = "COFF"; |
|
373 | 373 | break; |
|
374 | 374 | case ELF_K_NUM: |
|
375 | 375 | kind = "NUM"; |
|
376 | 376 | break; |
|
377 | 377 | case ELF_K_NONE: |
|
378 | 378 | kind = "Data"; |
|
379 | 379 | break; |
|
380 | 380 | default: |
|
381 | 381 | kind = "Unknow"; |
|
382 | 382 | break; |
|
383 | 383 | } |
|
384 | 384 | } |
|
385 | 385 | return kind; |
|
386 | 386 | } |
|
387 | 387 | |
|
388 | 388 | QString elfparser::getEndianness() |
|
389 | 389 | { |
|
390 | 390 | if(this->e!=NULL) |
|
391 | 391 | { |
|
392 | 392 | if(this->ehdr.e_ident[EI_DATA]==ELFDATA2LSB)return "2's complement, little endian"; |
|
393 | 393 | if(this->ehdr.e_ident[EI_DATA]==ELFDATA2MSB)return "2's complement, big endian"; |
|
394 | 394 | } |
|
395 | 395 | return "none"; |
|
396 | 396 | } |
|
397 | 397 | |
|
398 | 398 | QString elfparser::getABI() |
|
399 | 399 | { |
|
400 | 400 | if(this->e!=NULL) |
|
401 | 401 | { |
|
402 | 402 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_NONE)return "UNIX System V ABI"; |
|
403 | 403 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_SYSV)return "Alias"; |
|
404 | 404 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_HPUX)return "HP-UX"; |
|
405 | 405 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_NETBSD)return "NetBSD"; |
|
406 | 406 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_GNU)return "Object uses GNU ELF extensions"; |
|
407 | 407 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_LINUX)return "Compatibility alias"; |
|
408 | 408 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_SOLARIS)return "Sun Solaris"; |
|
409 | 409 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_AIX)return "IBM AIX"; |
|
410 | 410 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_IRIX)return "SGI Irix"; |
|
411 | 411 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_FREEBSD)return "FreeBSD"; |
|
412 | 412 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_TRU64)return "Compaq TRU64 UNIX"; |
|
413 | 413 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_MODESTO)return " Novell Modesto"; |
|
414 | 414 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_OPENBSD)return "OpenBSD"; |
|
415 | 415 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_ARM_AEABI)return "ARM EABI"; |
|
416 | 416 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_ARM)return "ARM"; |
|
417 | 417 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_STANDALONE)return "Standalone (embedded) application"; |
|
418 | 418 | } |
|
419 | 419 | return "none"; |
|
420 | 420 | } |
|
421 | 421 | |
|
422 | 422 | |
|
423 |
int32 |
|
|
423 | qint32 elfparser::getVersion() | |
|
424 | 424 | { |
|
425 | 425 | if(this->e!=NULL) |
|
426 | 426 | { |
|
427 | 427 | return this->ehdr.e_version; |
|
428 | 428 | } |
|
429 | 429 | } |
|
430 | 430 | |
|
431 |
int64 |
|
|
431 | qint64 elfparser::getEntryPointAddress() | |
|
432 | 432 | { |
|
433 | 433 | if(this->e!=NULL) |
|
434 | 434 | { |
|
435 | 435 | return this->ehdr.e_entry; |
|
436 | 436 | } |
|
437 | 437 | } |
|
438 | 438 | |
|
439 | 439 | |
|
440 | 440 | int elfparser::getSectioncount() |
|
441 | 441 | { |
|
442 | 442 | return (int)this->SectionCount; |
|
443 | 443 | } |
|
444 | 444 | |
|
445 | 445 | |
|
446 | 446 | int elfparser::getSegmentcount() |
|
447 | 447 | { |
|
448 | 448 | return (int)this->SegmentCount; |
|
449 | 449 | } |
|
450 | 450 | |
|
451 | 451 | |
|
452 | 452 | QString elfparser::getSegmentType(int index) |
|
453 | 453 | { |
|
454 | 454 | QString type(""); |
|
455 | 455 | if(this->e!=NULL) |
|
456 | 456 | { |
|
457 | 457 | if(index < this->Segments.count()) |
|
458 | 458 | { |
|
459 | 459 | switch(this->Segments.at(index)->p_type) |
|
460 | 460 | { |
|
461 | 461 | case PT_NULL: |
|
462 | 462 | type = "Program header table entry unused"; |
|
463 | 463 | break; |
|
464 | 464 | case PT_LOAD: |
|
465 | 465 | type = "Loadable program segment"; |
|
466 | 466 | break; |
|
467 | 467 | case PT_DYNAMIC : |
|
468 | 468 | type = "Dynamic linking information"; |
|
469 | 469 | break; |
|
470 | 470 | case PT_INTERP: |
|
471 | 471 | type ="Program interpreter"; |
|
472 | 472 | break; |
|
473 | 473 | case PT_NOTE: |
|
474 | 474 | type = "Auxiliary information"; |
|
475 | 475 | break; |
|
476 | 476 | case PT_SHLIB: |
|
477 | 477 | type = "Reserved"; |
|
478 | 478 | break; |
|
479 | 479 | case PT_PHDR: |
|
480 | 480 | type = "Entry for header table itself"; |
|
481 | 481 | break; |
|
482 | 482 | case PT_TLS: |
|
483 | 483 | type = "Thread-local storage segment"; |
|
484 | 484 | break; |
|
485 | 485 | case PT_NUM: |
|
486 | 486 | type = "Number of defined types"; |
|
487 | 487 | break; |
|
488 | 488 | case PT_LOOS: |
|
489 | 489 | type = "Start of OS-specific"; |
|
490 | 490 | break; |
|
491 | 491 | case PT_SUNWSTACK: |
|
492 | 492 | type = "Stack segment"; |
|
493 | 493 | break; |
|
494 | 494 | case PT_LOPROC: |
|
495 | 495 | type = "Start of processor-specific"; |
|
496 | 496 | break; |
|
497 | 497 | case PT_HIPROC: |
|
498 | 498 | type = "End of processor-specific"; |
|
499 | 499 | break; |
|
500 | 500 | default: |
|
501 | 501 | type = "Unknow Section Type"; |
|
502 | 502 | break; |
|
503 | 503 | } |
|
504 | 504 | } |
|
505 | 505 | } |
|
506 | 506 | |
|
507 | 507 | return type; |
|
508 | 508 | } |
|
509 | 509 | |
|
510 | 510 | |
|
511 |
int64 |
|
|
511 | qint64 elfparser::getSegmentOffset(int index) | |
|
512 | 512 | { |
|
513 | 513 | int64_t Offset; |
|
514 | 514 | if(this->e!=NULL) |
|
515 | 515 | { |
|
516 | 516 | if(index < this->Segments.count()) |
|
517 | 517 | { |
|
518 | 518 | Offset = (int64_t)this->Segments.at(index)->p_offset; |
|
519 | 519 | } |
|
520 | 520 | } |
|
521 | 521 | return Offset; |
|
522 | 522 | } |
|
523 | 523 | |
|
524 | 524 | |
|
525 |
int64 |
|
|
525 | qint64 elfparser::getSegmentVaddr(int index) | |
|
526 | 526 | { |
|
527 | 527 | int64_t Vaddr = 0; |
|
528 | 528 | if(this->e!=NULL) |
|
529 | 529 | { |
|
530 | 530 | if(index < this->Segments.count()) |
|
531 | 531 | { |
|
532 | 532 | Vaddr = (int64_t)this->Segments.at(index)->p_vaddr; |
|
533 | 533 | } |
|
534 | 534 | } |
|
535 | 535 | return Vaddr; |
|
536 | 536 | } |
|
537 | 537 | |
|
538 | 538 | |
|
539 |
int64 |
|
|
539 | qint64 elfparser::getSegmentPaddr(int index) | |
|
540 | 540 | { |
|
541 | 541 | int64_t Paddr=0; |
|
542 | 542 | if(this->e!=NULL) |
|
543 | 543 | { |
|
544 | 544 | if(index < this->Segments.count()) |
|
545 | 545 | { |
|
546 | 546 | Paddr = (int64_t)this->Segments.at(index)->p_paddr; |
|
547 | 547 | } |
|
548 | 548 | } |
|
549 | 549 | return Paddr; |
|
550 | 550 | } |
|
551 | 551 | |
|
552 |
int64 |
|
|
552 | qint64 elfparser::getSectionPaddr(int index) | |
|
553 | 553 | { |
|
554 | 554 | int64_t Paddr=0; |
|
555 | 555 | if(this->e!=NULL) |
|
556 | 556 | { |
|
557 | 557 | if(index < this->sections.count()) |
|
558 | 558 | { |
|
559 | 559 | Paddr = (int64_t)this->sections.at(index)->section_header->sh_addr; |
|
560 | 560 | } |
|
561 | 561 | } |
|
562 | 562 | return Paddr; |
|
563 | 563 | } |
|
564 | 564 | |
|
565 | 565 | |
|
566 |
int64 |
|
|
566 | qint64 elfparser::getSegmentFilesz(int index) | |
|
567 | 567 | { |
|
568 | 568 | int64_t FileSz=0; |
|
569 | 569 | if(this->e!=NULL) |
|
570 | 570 | { |
|
571 | 571 | if(index < this->Segments.count()) |
|
572 | 572 | { |
|
573 | 573 | FileSz = (int64_t)this->Segments.at(index)->p_filesz; |
|
574 | 574 | } |
|
575 | 575 | } |
|
576 | 576 | return FileSz; |
|
577 | 577 | } |
|
578 | 578 | |
|
579 |
int64 |
|
|
579 | qint64 elfparser::getSectionDatasz(int index) | |
|
580 | 580 | { |
|
581 | 581 | int64_t DataSz=0; |
|
582 | 582 | if(this->e!=NULL) |
|
583 | 583 | { |
|
584 | 584 | if(index < this->sections.count()) |
|
585 | 585 | { |
|
586 | 586 | DataSz = (int64_t)this->sections.at(index)->data->d_size; |
|
587 | 587 | } |
|
588 | 588 | } |
|
589 | 589 | return DataSz; |
|
590 | 590 | } |
|
591 | 591 | |
|
592 | 592 | bool elfparser::getSectionData(int index, char **buffer) |
|
593 | 593 | { |
|
594 | 594 | if(this->e!=NULL) |
|
595 | 595 | { |
|
596 | 596 | if(index < this->sections.count()) |
|
597 | 597 | { |
|
598 | 598 | *buffer = (char *)this->sections.at(index)->data->d_buf; |
|
599 | 599 | return true; |
|
600 | 600 | } |
|
601 | 601 | } |
|
602 | 602 | return false; |
|
603 | 603 | } |
|
604 | 604 | |
|
605 | 605 | |
|
606 |
int64 |
|
|
606 | qint64 elfparser::getSegmentMemsz(int index) | |
|
607 | 607 | { |
|
608 | 608 | int64_t MemSz=0; |
|
609 | 609 | if(this->e!=NULL) |
|
610 | 610 | { |
|
611 | 611 | if(index < this->Segments.count()) |
|
612 | 612 | { |
|
613 | 613 | MemSz = (int64_t)this->Segments.at(index)->p_memsz; |
|
614 | 614 | } |
|
615 | 615 | } |
|
616 | 616 | return MemSz; |
|
617 | 617 | } |
|
618 | 618 | |
|
619 |
int64 |
|
|
619 | qint64 elfparser::getSectionMemsz(int index) | |
|
620 | 620 | { |
|
621 | 621 | int64_t MemSz=0; |
|
622 | 622 | if(this->e!=NULL) |
|
623 | 623 | { |
|
624 | 624 | if(index < this->sections.count()) |
|
625 | 625 | { |
|
626 | 626 | MemSz = (int64_t)this->sections.at(index)->section_header->sh_size; |
|
627 | 627 | } |
|
628 | 628 | } |
|
629 | 629 | return MemSz; |
|
630 | 630 | } |
|
631 | 631 | |
|
632 | 632 | |
|
633 | 633 | QString elfparser::getSegmentFlags(int index) |
|
634 | 634 | { |
|
635 | 635 | QString flags(""); |
|
636 | 636 | if(this->e!=NULL) |
|
637 | 637 | { |
|
638 | 638 | if(index < this->Segments.count()) |
|
639 | 639 | { |
|
640 | 640 | if((this->Segments.at(index)->p_flags&PF_X) == PF_X)flags+="x"; |
|
641 | 641 | if((this->Segments.at(index)->p_flags&PF_W) == PF_W)flags+="w"; |
|
642 | 642 | if((this->Segments.at(index)->p_flags&PF_R) == PF_R)flags+="r"; |
|
643 | 643 | if((this->Segments.at(index)->p_flags&PF_MASKOS) == PF_MASKOS)flags+=" OS-specific"; |
|
644 | 644 | if((this->Segments.at(index)->p_flags&PF_MASKPROC) == PF_MASKPROC)flags+=" Processor-specific"; |
|
645 | 645 | } |
|
646 | 646 | } |
|
647 | 647 | return flags; |
|
648 | 648 | } |
|
649 | 649 | |
|
650 | 650 | |
|
651 | 651 | QString elfparser::getSectionName(int index) |
|
652 | 652 | { |
|
653 | 653 | char* nameChr = elf_strptr(this->e , this->shstrndx , this->sections.at(index)->section_header->sh_name); |
|
654 | 654 | return QString(nameChr); |
|
655 | 655 | } |
|
656 | 656 | |
|
657 | 657 | |
|
658 | 658 | void elfparser::updateSections() |
|
659 | 659 | { |
|
660 | 660 | for(int i=0;i<this->sections.count();i++) |
|
661 | 661 | { |
|
662 | 662 | delete this->sections.at(i); |
|
663 | 663 | } |
|
664 | 664 | this->sections.clear(); |
|
665 | 665 | this->scn = elf_nextscn (this->e , NULL ); |
|
666 | 666 | this->SectionCount = 0; |
|
667 | 667 | while( this->scn != NULL ) |
|
668 | 668 | { |
|
669 | 669 | GElf_Shdr* shdr = (GElf_Shdr*)malloc(sizeof(GElf_Shdr)); |
|
670 | 670 | gelf_getshdr ( this->scn , shdr ); |
|
671 | 671 | Elf_Data* data = elf_getdata(this->scn, NULL); |
|
672 | 672 | this->sections.append(new Elf_Section(data,shdr)); |
|
673 | 673 | this->SectionCount+=1; |
|
674 | 674 | this->scn = elf_nextscn(e , scn); |
|
675 | 675 | } |
|
676 | 676 | } |
|
677 | 677 | |
|
678 | 678 | |
|
679 | 679 | void elfparser::updateSegments() |
|
680 | 680 | { |
|
681 | 681 | elf_getphdrnum (this->e , &this->SegmentCount); |
|
682 | 682 | for(int i=0;i<this->Segments.count();i++) |
|
683 | 683 | { |
|
684 | 684 | free(this->Segments.at(i)); |
|
685 | 685 | } |
|
686 | 686 | this->Segments.clear(); |
|
687 | 687 | for(int i=0;i<this->SegmentCount;i++) |
|
688 | 688 | { |
|
689 | 689 | GElf_Phdr* header=(GElf_Phdr*)malloc(sizeof(GElf_Phdr)); |
|
690 | 690 | gelf_getphdr (this->e , i , header ); |
|
691 | 691 | this->Segments.append(header); |
|
692 | 692 | } |
|
693 | 693 | } |
|
694 | 694 | |
|
695 | 695 | |
|
696 | 696 | |
|
697 | 697 | |
|
698 | 698 | |
|
699 | 699 | QString elfparser::getSectionType(int index) |
|
700 | 700 | { |
|
701 | 701 | QString type(""); |
|
702 | 702 | if(this->e!=NULL) |
|
703 | 703 | { |
|
704 | 704 | if(index < this->Segments.count()) |
|
705 | 705 | { |
|
706 | 706 | switch(this->Segments.at(index)->p_type) |
|
707 | 707 | { |
|
708 | 708 | case SHT_NULL : type = "Section header table entry unused"; break; |
|
709 | 709 | case SHT_PROGBITS : type = "Program data"; break; |
|
710 | 710 | case SHT_SYMTAB : type = "Symbol table"; break; |
|
711 | 711 | case SHT_STRTAB : type = "String table"; break; |
|
712 | 712 | case SHT_RELA : type = "Relocation entries with addends"; break; |
|
713 | 713 | case SHT_HASH : type = "Symbol hash table"; break; |
|
714 | 714 | case SHT_DYNAMIC : type = "Dynamic linking information"; break; |
|
715 | 715 | case SHT_NOTE : type = "Notes"; break; |
|
716 | 716 | case SHT_NOBITS :type = "Program space with no data (bss)"; break; |
|
717 | 717 | case SHT_REL :type = "Relocation entries, no addends"; break; |
|
718 | 718 | case SHT_SHLIB : type = "Reserved"; break; |
|
719 | 719 | case SHT_DYNSYM : type = "Dynamic linker symbol table"; break; |
|
720 | 720 | case SHT_INIT_ARRAY : type = "Array of constructors"; break; |
|
721 | 721 | case SHT_FINI_ARRAY : type = "Array of destructors"; break; |
|
722 | 722 | case SHT_PREINIT_ARRAY : type = "Array of pre-constructors"; break; |
|
723 | 723 | case SHT_GROUP : type = "Section group"; break; |
|
724 | 724 | case SHT_SYMTAB_SHNDX : type = "Extended section indeces"; break; |
|
725 | 725 | case SHT_NUM : type = "Number of defined types. "; break; |
|
726 | 726 | case SHT_LOOS : type = "Start OS-specific. "; break; |
|
727 | 727 | case SHT_LOSUNW : type = "Sun-specific low bound. "; break; |
|
728 | 728 | case SHT_SUNW_COMDAT : type = " "; break; |
|
729 | 729 | case SHT_SUNW_syminfo : type = " "; break; |
|
730 | 730 | case SHT_GNU_verdef : type = "Version definition section. "; break; |
|
731 | 731 | case SHT_GNU_verneed : type = "Version needs section. "; break; |
|
732 | 732 | case SHT_GNU_versym : type = "Version symbol table. "; break; |
|
733 | 733 | case SHT_LOPROC : type = "Start of processor-specific"; break; |
|
734 | 734 | case SHT_HIPROC : type = "End of processor-specific"; break; |
|
735 | 735 | case SHT_HIUSER : type = "End of application-specific"; break; |
|
736 | 736 | } |
|
737 | 737 | } |
|
738 | 738 | } |
|
739 | 739 | return type; |
|
740 | 740 | } |
|
741 | 741 | |
|
742 | 742 | bool elfparser::isElf(const QString &File) |
|
743 | 743 | { |
|
744 | 744 | int file =0; |
|
745 | 745 | #ifdef _ELF_WINDOWS_ |
|
746 | 746 | file = open(File.toStdString().c_str(),O_RDONLY|O_BINARY ,0); |
|
747 | 747 | #else |
|
748 | 748 | file = open(File.toStdString().c_str(),O_RDONLY ,0); |
|
749 | 749 | #endif |
|
750 | 750 | char Magic[4]; |
|
751 | 751 | if(file!=-1) |
|
752 | 752 | { |
|
753 | 753 | read(file,Magic,4); |
|
754 | 754 | close(file); |
|
755 | 755 | if(Magic[0]==0x7f && Magic[1]==0x45 && Magic[2]==0x4c && Magic[3]==0x46) |
|
756 | 756 | { |
|
757 | 757 | return true; |
|
758 | 758 | } |
|
759 | 759 | } |
|
760 | 760 | return false; |
|
761 | 761 | } |
|
762 | 762 | |
|
763 | 763 | |
|
764 | 764 | |
|
765 | 765 | |
|
766 | 766 | |
|
767 | 767 | |
|
768 | 768 | |
|
769 | 769 |
@@ -1,101 +1,101 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the SocExplorer Software |
|
3 | 3 | -- Copyright (C) 2013, Plasma Physics Laboratory - CNRS |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | -------------------------------------------------------------------------------*/ |
|
19 | 19 | /*-- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@lpp.polytechnique.fr |
|
21 | 21 | ----------------------------------------------------------------------------*/ |
|
22 | 22 | #ifndef ELFPARSER_H |
|
23 | 23 | #define ELFPARSER_H |
|
24 | 24 | #include <libelf.h> |
|
25 | 25 | #include <gelf.h> |
|
26 | 26 | #include <QString> |
|
27 | 27 | #include <stdio.h> |
|
28 | 28 | #include <QList> |
|
29 | 29 | #include <stdint.h> |
|
30 | 30 | |
|
31 | 31 | class Elf_Section |
|
32 | 32 | { |
|
33 | 33 | public: |
|
34 | 34 | Elf_Section(){} |
|
35 | 35 | Elf_Section(Elf_Data* data,GElf_Shdr* section_header) |
|
36 | 36 | { |
|
37 | 37 | this->data = data; |
|
38 | 38 | this->section_header = section_header; |
|
39 | 39 | } |
|
40 | 40 | Elf_Data* data; |
|
41 | 41 | GElf_Shdr* section_header; |
|
42 | 42 | }; |
|
43 | 43 | |
|
44 | 44 | class elfparser |
|
45 | 45 | { |
|
46 | 46 | public: |
|
47 | 47 | elfparser(); |
|
48 | 48 | bool iself(); |
|
49 | 49 | bool isopened(); |
|
50 | 50 | int setFilename(const QString& name); |
|
51 | 51 | int closeFile(); |
|
52 | 52 | QString getClass(); |
|
53 | 53 | QString getArchitecture(); |
|
54 | 54 | QString getType(); |
|
55 | 55 | QString getEndianness(); |
|
56 | 56 | QString getABI(); |
|
57 |
int32 |
|
|
58 |
int64 |
|
|
57 | qint32 getVersion(); | |
|
58 | qint64 getEntryPointAddress(); | |
|
59 | 59 | int getSectioncount(); |
|
60 | 60 | int getSegmentcount(); |
|
61 | 61 | QString getSegmentType(int index); |
|
62 |
int64 |
|
|
63 |
int64 |
|
|
64 |
int64 |
|
|
65 |
int64 |
|
|
66 |
int64 |
|
|
67 |
int64 |
|
|
62 | qint64 getSegmentOffset(int index); | |
|
63 | qint64 getSegmentVaddr(int index); | |
|
64 | qint64 getSegmentPaddr(int index); | |
|
65 | qint64 getSectionPaddr(int index); | |
|
66 | qint64 getSegmentFilesz(int index); | |
|
67 | qint64 getSectionDatasz(int index); | |
|
68 | 68 | bool getSectionData(int index, char **buffer); |
|
69 |
int64 |
|
|
70 |
int64 |
|
|
69 | qint64 getSegmentMemsz(int index); | |
|
70 | qint64 getSectionMemsz(int index); | |
|
71 | 71 | QString getSegmentFlags(int index); |
|
72 | 72 | QString getSectionName(int index); |
|
73 | 73 | QString getSectionType(int index); |
|
74 | 74 | static bool isElf(const QString& File); |
|
75 | 75 | |
|
76 | 76 | private: |
|
77 | 77 | void updateSections(); |
|
78 | 78 | void updateSegments(); |
|
79 | 79 | int elfFile; |
|
80 | 80 | bool opened; |
|
81 | 81 | bool type_elf; |
|
82 | 82 | Elf* e; |
|
83 | 83 | Elf_Kind ek; |
|
84 | 84 | GElf_Ehdr ehdr; |
|
85 | 85 | Elf_Scn * scn; |
|
86 | 86 | Elf_Data * data; |
|
87 | 87 | size_t SectionCount,SegmentCount, shstrndx; |
|
88 | 88 | QList<GElf_Phdr*> Segments; |
|
89 | 89 | QList<Elf_Section*> sections; |
|
90 | 90 | |
|
91 | 91 | }; |
|
92 | 92 | |
|
93 | 93 | #endif // ELFPARSER_H |
|
94 | 94 | |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | |
|
98 | 98 | |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 |
General Comments 0
You need to be logged in to leave comments.
Login now