@@ -1,846 +1,1006 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the SocExplorer Software |
|
3 | 3 | -- Copyright (C) 2014, 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 2 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 : |
|
20 | 20 | Alexis Jeandet |
|
21 | 21 | -- Mail : |
|
22 | 22 | alexis.jeandet@member.fsf.org |
|
23 | 23 | ----------------------------------------------------------------------------*/ |
|
24 | 24 | #include "elffile.h" |
|
25 | 25 | |
|
26 | 26 | ElfFile::ElfFile() |
|
27 | 27 | :abstractExecFile() |
|
28 | 28 | { |
|
29 | 29 | this->opened = false; |
|
30 | 30 | this->type_elf = false; |
|
31 | 31 | this->elfFile = NULL; |
|
32 | 32 | this->e = NULL; |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | ElfFile::ElfFile(const QString &File) |
|
36 | 36 | :abstractExecFile() |
|
37 | 37 | { |
|
38 | 38 | this->opened = false; |
|
39 | 39 | this->type_elf = false; |
|
40 | 40 | this->elfFile = NULL; |
|
41 | 41 | this->e = NULL; |
|
42 | 42 | this->p_fileName = File; |
|
43 | 43 | openFile(File); |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | bool ElfFile::openFile(const QString &File) |
|
47 | 47 | { |
|
48 | 48 | this->p_fileName = File; |
|
49 | 49 | this->closeFile(); |
|
50 | 50 | if(elf_version(EV_CURRENT)==EV_NONE)return 0; |
|
51 | 51 | #ifdef _ELF_WINDOWS_ |
|
52 | 52 | this->elfFile = open(File.toStdString().c_str(),O_RDONLY|O_BINARY ,0); |
|
53 | 53 | #else |
|
54 | 54 | this->elfFile = open(File.toStdString().c_str(),O_RDONLY ,0); |
|
55 | 55 | #endif |
|
56 | 56 | if(this->elfFile==NULL)return 0; |
|
57 | 57 | this->e = elf_begin(this->elfFile,ELF_C_READ,NULL); |
|
58 | 58 | if(this->e==NULL)return 0; |
|
59 | 59 | this->ek = elf_kind(this->e); |
|
60 | 60 | gelf_getehdr (this->e, &this->ehdr ); |
|
61 | 61 | elf_getshdrstrndx (this->e, &this->shstrndx); |
|
62 | 62 | this->updateSegments(); |
|
63 | 63 | this->updateSections(); |
|
64 | 64 | this->updateSymbols(); |
|
65 | 65 | this->opened = true; |
|
66 | 66 | return 1; |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | bool ElfFile::isopened() |
|
70 | 70 | { |
|
71 | 71 | return this->opened; |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | int ElfFile::closeFile() |
|
75 | 75 | { |
|
76 | 76 | if(this->elfFile!=NULL) |
|
77 | 77 | { |
|
78 | 78 | if(this->e!=NULL) |
|
79 | 79 | { |
|
80 | 80 | elf_end(this->e); |
|
81 | 81 | this->e = NULL; |
|
82 | 82 | } |
|
83 | 83 | close(this->elfFile); |
|
84 | 84 | this->elfFile = NULL; |
|
85 | 85 | } |
|
86 | 86 | this->opened = false; |
|
87 | 87 | return 0; |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | QList<codeFragment*> ElfFile::getFragments(QStringList fragmentList) |
|
92 | 92 | { |
|
93 | 93 | QList<codeFragment*> fragments; |
|
94 | 94 | if (isopened()) |
|
95 | 95 | { |
|
96 | 96 | for(int i =0;i<fragmentList.count();i++) |
|
97 | 97 | { |
|
98 | 98 | fragments.append(getFragment(fragmentList.at(i))); |
|
99 | 99 | } |
|
100 | 100 | } |
|
101 | 101 | return fragments; |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | QList<codeFragment*> ElfFile::getFragments() |
|
105 | 105 | { |
|
106 | 106 | return getFragments(QStringList()<<".data"<<".text"); |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | codeFragment *ElfFile::getFragment(const QString &name) |
|
110 | 110 | { |
|
111 | 111 | codeFragment* fragment= new codeFragment(); |
|
112 | 112 | for(int i=0;i<getSectionCount();i++) |
|
113 | 113 | { |
|
114 | 114 | if(getSectionName(i) == name) |
|
115 | 115 | { |
|
116 | 116 | fragment->data =NULL; |
|
117 | 117 | fragment->size = getSectionDatasz(i); |
|
118 | 118 | fragment->address = getSectionPaddr(i); |
|
119 | 119 | getSectionData(i,&fragment->data); |
|
120 | 120 | } |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | |
|
126 | 126 | |
|
127 | 127 | |
|
128 | 128 | |
|
129 | 129 | |
|
130 | 130 | |
|
131 | 131 | QString elfresolveMachine(Elf64_Half e_machine) |
|
132 | 132 | { |
|
133 | 133 | QString machineName; |
|
134 | 134 | //Update from with bash script don't write it by yourself! |
|
135 | 135 | switch(e_machine) |
|
136 | 136 | { |
|
137 | 137 | case EM_NONE: |
|
138 | 138 | machineName = " No machine "; |
|
139 | 139 | break; |
|
140 | 140 | case EM_M32: |
|
141 | 141 | machineName = " AT&T WE 32100 "; |
|
142 | 142 | break; |
|
143 | 143 | case EM_SPARC: |
|
144 | 144 | machineName = " SUN SPARC "; |
|
145 | 145 | break; |
|
146 | 146 | case EM_386: |
|
147 | 147 | machineName = " Intel 80386 "; |
|
148 | 148 | break; |
|
149 | 149 | case EM_68K: |
|
150 | 150 | machineName = " Motorola m68k family "; |
|
151 | 151 | break; |
|
152 | 152 | case EM_88K: |
|
153 | 153 | machineName = " Motorola m88k family "; |
|
154 | 154 | break; |
|
155 | 155 | case EM_860: |
|
156 | 156 | machineName = " Intel 80860 "; |
|
157 | 157 | break; |
|
158 | 158 | case EM_MIPS: |
|
159 | 159 | machineName = " MIPS R3000 big-endian "; |
|
160 | 160 | break; |
|
161 | 161 | case EM_S370: |
|
162 | 162 | machineName = " IBM System/370 "; |
|
163 | 163 | break; |
|
164 | 164 | case EM_MIPS_RS3_LE: |
|
165 | 165 | machineName = " MIPS R3000 little-endian "; |
|
166 | 166 | break; |
|
167 | 167 | case EM_PARISC: |
|
168 | 168 | machineName = " HPPA "; |
|
169 | 169 | break; |
|
170 | 170 | case EM_VPP500: |
|
171 | 171 | machineName = " Fujitsu VPP500 "; |
|
172 | 172 | break; |
|
173 | 173 | case EM_SPARC32PLUS: |
|
174 | 174 | machineName = " Sun's \"v8plus\" "; |
|
175 | 175 | break; |
|
176 | 176 | case EM_960: |
|
177 | 177 | machineName = " Intel 80960 "; |
|
178 | 178 | break; |
|
179 | 179 | case EM_PPC: |
|
180 | 180 | machineName = " PowerPC "; |
|
181 | 181 | break; |
|
182 | 182 | case EM_PPC64: |
|
183 | 183 | machineName = " PowerPC 64-bit "; |
|
184 | 184 | break; |
|
185 | 185 | case EM_S390: |
|
186 | 186 | machineName = " IBM S390 "; |
|
187 | 187 | break; |
|
188 | 188 | case EM_V800: |
|
189 | 189 | machineName = " NEC V800 series "; |
|
190 | 190 | break; |
|
191 | 191 | case EM_FR20: |
|
192 | 192 | machineName = " Fujitsu FR20 "; |
|
193 | 193 | break; |
|
194 | 194 | case EM_RH32: |
|
195 | 195 | machineName = " TRW RH-32 "; |
|
196 | 196 | break; |
|
197 | 197 | case EM_RCE: |
|
198 | 198 | machineName = " Motorola RCE "; |
|
199 | 199 | break; |
|
200 | 200 | case EM_ARM: |
|
201 | 201 | machineName = " ARM "; |
|
202 | 202 | break; |
|
203 | 203 | case EM_FAKE_ALPHA: |
|
204 | 204 | machineName = " Digital Alpha "; |
|
205 | 205 | break; |
|
206 | 206 | case EM_SH: |
|
207 | 207 | machineName = " Hitachi SH "; |
|
208 | 208 | break; |
|
209 | 209 | case EM_SPARCV9: |
|
210 | 210 | machineName = " SPARC v9 64-bit "; |
|
211 | 211 | break; |
|
212 | 212 | case EM_TRICORE: |
|
213 | 213 | machineName = " Siemens Tricore "; |
|
214 | 214 | break; |
|
215 | 215 | case EM_ARC: |
|
216 | 216 | machineName = " Argonaut RISC Core "; |
|
217 | 217 | break; |
|
218 | 218 | case EM_H8_300: |
|
219 | 219 | machineName = " Hitachi H8/300 "; |
|
220 | 220 | break; |
|
221 | 221 | case EM_H8_300H: |
|
222 | 222 | machineName = " Hitachi H8/300H "; |
|
223 | 223 | break; |
|
224 | 224 | case EM_H8S: |
|
225 | 225 | machineName = " Hitachi H8S "; |
|
226 | 226 | break; |
|
227 | 227 | case EM_H8_500: |
|
228 | 228 | machineName = " Hitachi H8/500 "; |
|
229 | 229 | break; |
|
230 | 230 | case EM_IA_64: |
|
231 | 231 | machineName = " Intel Merced "; |
|
232 | 232 | break; |
|
233 | 233 | case EM_MIPS_X: |
|
234 | 234 | machineName = " Stanford MIPS-X "; |
|
235 | 235 | break; |
|
236 | 236 | case EM_COLDFIRE: |
|
237 | 237 | machineName = " Motorola Coldfire "; |
|
238 | 238 | break; |
|
239 | 239 | case EM_68HC12: |
|
240 | 240 | machineName = " Motorola M68HC12 "; |
|
241 | 241 | break; |
|
242 | 242 | case EM_MMA: |
|
243 | 243 | machineName = " Fujitsu MMA Multimedia Accelerator"; |
|
244 | 244 | break; |
|
245 | 245 | case EM_PCP: |
|
246 | 246 | machineName = " Siemens PCP "; |
|
247 | 247 | break; |
|
248 | 248 | case EM_NCPU: |
|
249 | 249 | machineName = " Sony nCPU embeeded RISC "; |
|
250 | 250 | break; |
|
251 | 251 | case EM_NDR1: |
|
252 | 252 | machineName = " Denso NDR1 microprocessor "; |
|
253 | 253 | break; |
|
254 | 254 | case EM_STARCORE: |
|
255 | 255 | machineName = " Motorola Start*Core processor "; |
|
256 | 256 | break; |
|
257 | 257 | case EM_ME16: |
|
258 | 258 | machineName = " Toyota ME16 processor "; |
|
259 | 259 | break; |
|
260 | 260 | case EM_ST100: |
|
261 | 261 | machineName = " STMicroelectronic ST100 processor "; |
|
262 | 262 | break; |
|
263 | 263 | case EM_TINYJ: |
|
264 | 264 | machineName = " Advanced Logic Corp. Tinyj emb.fam"; |
|
265 | 265 | break; |
|
266 | 266 | case EM_X86_64: |
|
267 | 267 | machineName = " AMD x86-64 architecture "; |
|
268 | 268 | break; |
|
269 | 269 | case EM_PDSP: |
|
270 | 270 | machineName = " Sony DSP Processor "; |
|
271 | 271 | break; |
|
272 | 272 | case EM_FX66: |
|
273 | 273 | machineName = " Siemens FX66 microcontroller "; |
|
274 | 274 | break; |
|
275 | 275 | case EM_ST9PLUS: |
|
276 | 276 | machineName = " STMicroelectronics ST9+ 8/16 mc "; |
|
277 | 277 | break; |
|
278 | 278 | case EM_ST7: |
|
279 | 279 | machineName = " STmicroelectronics ST7 8 bit mc "; |
|
280 | 280 | break; |
|
281 | 281 | case EM_68HC16: |
|
282 | 282 | machineName = " Motorola MC68HC16 microcontroller "; |
|
283 | 283 | break; |
|
284 | 284 | case EM_68HC11: |
|
285 | 285 | machineName = " Motorola MC68HC11 microcontroller "; |
|
286 | 286 | break; |
|
287 | 287 | case EM_68HC08: |
|
288 | 288 | machineName = " Motorola MC68HC08 microcontroller "; |
|
289 | 289 | break; |
|
290 | 290 | case EM_68HC05: |
|
291 | 291 | machineName = " Motorola MC68HC05 microcontroller "; |
|
292 | 292 | break; |
|
293 | 293 | case EM_SVX: |
|
294 | 294 | machineName = " Silicon Graphics SVx "; |
|
295 | 295 | break; |
|
296 | 296 | case EM_ST19: |
|
297 | 297 | machineName = " STMicroelectronics ST19 8 bit mc "; |
|
298 | 298 | break; |
|
299 | 299 | case EM_VAX: |
|
300 | 300 | machineName = " Digital VAX "; |
|
301 | 301 | break; |
|
302 | 302 | case EM_CRIS: |
|
303 | 303 | machineName = " Axis Communications 32-bit embedded processor "; |
|
304 | 304 | break; |
|
305 | 305 | case EM_JAVELIN: |
|
306 | 306 | machineName = " Infineon Technologies 32-bit embedded processor "; |
|
307 | 307 | break; |
|
308 | 308 | case EM_FIREPATH: |
|
309 | 309 | machineName = " Element 14 64-bit DSP Processor "; |
|
310 | 310 | break; |
|
311 | 311 | case EM_ZSP: |
|
312 | 312 | machineName = " LSI Logic 16-bit DSP Processor "; |
|
313 | 313 | break; |
|
314 | 314 | case EM_MMIX: |
|
315 | 315 | machineName = " Donald Knuth's educational 64-bit processor "; |
|
316 | 316 | break; |
|
317 | 317 | case EM_HUANY: |
|
318 | 318 | machineName = " Harvard University machine-independent object files "; |
|
319 | 319 | break; |
|
320 | 320 | case EM_PRISM: |
|
321 | 321 | machineName = " SiTera Prism "; |
|
322 | 322 | break; |
|
323 | 323 | case EM_AVR: |
|
324 | 324 | machineName = " Atmel AVR 8-bit microcontroller "; |
|
325 | 325 | break; |
|
326 | 326 | case EM_FR30: |
|
327 | 327 | machineName = " Fujitsu FR30 "; |
|
328 | 328 | break; |
|
329 | 329 | case EM_D10V: |
|
330 | 330 | machineName = " Mitsubishi D10V "; |
|
331 | 331 | break; |
|
332 | 332 | case EM_D30V: |
|
333 | 333 | machineName = " Mitsubishi D30V "; |
|
334 | 334 | break; |
|
335 | 335 | case EM_V850: |
|
336 | 336 | machineName = " NEC v850 "; |
|
337 | 337 | break; |
|
338 | 338 | case EM_M32R: |
|
339 | 339 | machineName = " Mitsubishi M32R "; |
|
340 | 340 | break; |
|
341 | 341 | case EM_MN10300: |
|
342 | 342 | machineName = " Matsushita MN10300 "; |
|
343 | 343 | break; |
|
344 | 344 | case EM_MN10200: |
|
345 | 345 | machineName = " Matsushita MN10200 "; |
|
346 | 346 | break; |
|
347 | 347 | case EM_PJ: |
|
348 | 348 | machineName = " picoJava "; |
|
349 | 349 | break; |
|
350 | 350 | case EM_OPENRISC: |
|
351 | 351 | machineName = " OpenRISC 32-bit embedded processor "; |
|
352 | 352 | break; |
|
353 | 353 | case EM_ARC_A5: |
|
354 | 354 | machineName = " ARC Cores Tangent-A5 "; |
|
355 | 355 | break; |
|
356 | 356 | case EM_XTENSA: |
|
357 | 357 | machineName = " Tensilica Xtensa Architecture "; |
|
358 | 358 | break; |
|
359 | 359 | case EM_AARCH64: |
|
360 | 360 | machineName = " ARM AARCH64 "; |
|
361 | 361 | break; |
|
362 | 362 | case EM_TILEPRO: |
|
363 | 363 | machineName = " Tilera TILEPro "; |
|
364 | 364 | break; |
|
365 | 365 | case EM_MICROBLAZE: |
|
366 | 366 | machineName = " Xilinx MicroBlaze "; |
|
367 | 367 | break; |
|
368 | 368 | case EM_TILEGX: |
|
369 | 369 | machineName = " Tilera TILE-Gx "; |
|
370 | 370 | break; |
|
371 | 371 | case EM_NUM: |
|
372 | 372 | machineName = ""; |
|
373 | 373 | break; |
|
374 | 374 | default: |
|
375 | 375 | machineName ="Unknow Machine"; |
|
376 | 376 | break; |
|
377 | 377 | } |
|
378 | 378 | return machineName; |
|
379 | 379 | } |
|
380 | 380 | |
|
381 | 381 | |
|
382 | 382 | |
|
383 | 383 | |
|
384 | 384 | QString ElfFile::getClass() |
|
385 | 385 | { |
|
386 | 386 | if(this->e!=NULL) |
|
387 | 387 | { |
|
388 | 388 | int eclass = gelf_getclass(this->e); |
|
389 | 389 | if(eclass==ELFCLASS32)return "ELF32"; |
|
390 | 390 | if(eclass==ELFCLASS64)return "ELF64"; |
|
391 | 391 | } |
|
392 | 392 | return "none"; |
|
393 | 393 | } |
|
394 | 394 | |
|
395 | 395 | |
|
396 | 396 | bool ElfFile::iself() |
|
397 | 397 | { |
|
398 | 398 | return (this->getType()!="Unknow"); |
|
399 | 399 | } |
|
400 | 400 | |
|
401 | 401 | QString ElfFile::getArchitecture() |
|
402 | 402 | { |
|
403 | 403 | if(this->e!=NULL) |
|
404 | 404 | { |
|
405 | 405 | return elfresolveMachine(this->ehdr.e_machine); |
|
406 | 406 | } |
|
407 | 407 | return ""; |
|
408 | 408 | } |
|
409 | 409 | |
|
410 | 410 | |
|
411 | 411 | QString ElfFile::getType() |
|
412 | 412 | { |
|
413 | 413 | QString kind(""); |
|
414 | 414 | if(this->e!=NULL) |
|
415 | 415 | { |
|
416 | 416 | switch(this->ek) |
|
417 | 417 | { |
|
418 | 418 | case ELF_K_AR: |
|
419 | 419 | kind = "Archive"; |
|
420 | 420 | break; |
|
421 | 421 | case ELF_K_ELF: |
|
422 | 422 | kind = "Elf"; |
|
423 | 423 | break; |
|
424 | 424 | case ELF_K_COFF: |
|
425 | 425 | kind = "COFF"; |
|
426 | 426 | break; |
|
427 | 427 | case ELF_K_NUM: |
|
428 | 428 | kind = "NUM"; |
|
429 | 429 | break; |
|
430 | 430 | case ELF_K_NONE: |
|
431 | 431 | kind = "Data"; |
|
432 | 432 | break; |
|
433 | 433 | default: |
|
434 | 434 | kind = "Unknow"; |
|
435 | 435 | break; |
|
436 | 436 | } |
|
437 | 437 | } |
|
438 | 438 | return kind; |
|
439 | 439 | } |
|
440 | 440 | |
|
441 | 441 | QString ElfFile::getEndianness() |
|
442 | 442 | { |
|
443 | 443 | if(this->e!=NULL) |
|
444 | 444 | { |
|
445 | 445 | if(this->ehdr.e_ident[EI_DATA]==ELFDATA2LSB)return "2's complement, little endian"; |
|
446 | 446 | if(this->ehdr.e_ident[EI_DATA]==ELFDATA2MSB)return "2's complement, big endian"; |
|
447 | 447 | } |
|
448 | 448 | return "none"; |
|
449 | 449 | } |
|
450 | 450 | |
|
451 | 451 | QString ElfFile::getABI() |
|
452 | 452 | { |
|
453 | 453 | if(this->e!=NULL) |
|
454 | 454 | { |
|
455 | 455 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_NONE)return "UNIX System V ABI"; |
|
456 | 456 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_SYSV)return "Alias"; |
|
457 | 457 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_HPUX)return "HP-UX"; |
|
458 | 458 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_NETBSD)return "NetBSD"; |
|
459 | 459 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_GNU)return "Object uses GNU ELF extensions"; |
|
460 | 460 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_LINUX)return "Compatibility alias"; |
|
461 | 461 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_SOLARIS)return "Sun Solaris"; |
|
462 | 462 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_AIX)return "IBM AIX"; |
|
463 | 463 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_IRIX)return "SGI Irix"; |
|
464 | 464 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_FREEBSD)return "FreeBSD"; |
|
465 | 465 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_TRU64)return "Compaq TRU64 UNIX"; |
|
466 | 466 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_MODESTO)return " Novell Modesto"; |
|
467 | 467 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_OPENBSD)return "OpenBSD"; |
|
468 | 468 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_ARM_AEABI)return "ARM EABI"; |
|
469 | 469 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_ARM)return "ARM"; |
|
470 | 470 | if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_STANDALONE)return "Standalone (embedded) application"; |
|
471 | 471 | } |
|
472 | 472 | return "none"; |
|
473 | 473 | } |
|
474 | 474 | |
|
475 | 475 | |
|
476 | 476 | qint64 ElfFile::getVersion() |
|
477 | 477 | { |
|
478 | 478 | if(this->e!=NULL) |
|
479 | 479 | { |
|
480 | 480 | return this->ehdr.e_version; |
|
481 | 481 | } |
|
482 | 482 | } |
|
483 | 483 | |
|
484 | 484 | qint64 ElfFile::getEntryPointAddress() |
|
485 | 485 | { |
|
486 | 486 | if(this->e!=NULL) |
|
487 | 487 | { |
|
488 | 488 | return this->ehdr.e_entry; |
|
489 | 489 | } |
|
490 | 490 | } |
|
491 | 491 | |
|
492 | 492 | |
|
493 | 493 | int ElfFile::getSectionCount() |
|
494 | 494 | { |
|
495 | 495 | return (int)this->SectionCount; |
|
496 | 496 | } |
|
497 | 497 | |
|
498 | 498 | int ElfFile::getSymbolCount() |
|
499 | 499 | { |
|
500 | 500 | return (int)this->SymbolCount; |
|
501 | 501 | } |
|
502 | 502 | |
|
503 | 503 | |
|
504 | 504 | int ElfFile::getSegmentCount() |
|
505 | 505 | { |
|
506 | 506 | return (int)this->SegmentCount; |
|
507 | 507 | } |
|
508 | 508 | |
|
509 | 509 | |
|
510 | 510 | QString ElfFile::getSegmentType(int index) |
|
511 | 511 | { |
|
512 | 512 | QString type(""); |
|
513 | 513 | if(this->e!=NULL) |
|
514 | 514 | { |
|
515 | 515 | if(index < this->Segments.count()) |
|
516 | 516 | { |
|
517 | 517 | switch(this->Segments.at(index)->p_type) |
|
518 | 518 | { |
|
519 | 519 | case PT_NULL: |
|
520 | 520 | type = "Program header table entry unused"; |
|
521 | 521 | break; |
|
522 | 522 | case PT_LOAD: |
|
523 | 523 | type = "Loadable program segment"; |
|
524 | 524 | break; |
|
525 | 525 | case PT_DYNAMIC : |
|
526 | 526 | type = "Dynamic linking information"; |
|
527 | 527 | break; |
|
528 | 528 | case PT_INTERP: |
|
529 | 529 | type ="Program interpreter"; |
|
530 | 530 | break; |
|
531 | 531 | case PT_NOTE: |
|
532 | 532 | type = "Auxiliary information"; |
|
533 | 533 | break; |
|
534 | 534 | case PT_SHLIB: |
|
535 | 535 | type = "Reserved"; |
|
536 | 536 | break; |
|
537 | 537 | case PT_PHDR: |
|
538 | 538 | type = "Entry for header table itself"; |
|
539 | 539 | break; |
|
540 | 540 | case PT_TLS: |
|
541 | 541 | type = "Thread-local storage segment"; |
|
542 | 542 | break; |
|
543 | 543 | case PT_NUM: |
|
544 | 544 | type = "Number of defined types"; |
|
545 | 545 | break; |
|
546 | 546 | case PT_LOOS: |
|
547 | 547 | type = "Start of OS-specific"; |
|
548 | 548 | break; |
|
549 | 549 | case PT_SUNWSTACK: |
|
550 | 550 | type = "Stack segment"; |
|
551 | 551 | break; |
|
552 | 552 | case PT_LOPROC: |
|
553 | 553 | type = "Start of processor-specific"; |
|
554 | 554 | break; |
|
555 | 555 | case PT_HIPROC: |
|
556 | 556 | type = "End of processor-specific"; |
|
557 | 557 | break; |
|
558 | 558 | default: |
|
559 | 559 | type = "Unknow Section Type"; |
|
560 | 560 | break; |
|
561 | 561 | } |
|
562 | 562 | } |
|
563 | 563 | } |
|
564 | 564 | |
|
565 | 565 | return type; |
|
566 | 566 | } |
|
567 | 567 | |
|
568 | 568 | |
|
569 | 569 | qint64 ElfFile::getSegmentOffset(int index) |
|
570 | 570 | { |
|
571 | 571 | int64_t Offset; |
|
572 | 572 | if(this->e!=NULL) |
|
573 | 573 | { |
|
574 | 574 | if(index < this->Segments.count()) |
|
575 | 575 | { |
|
576 | 576 | Offset = (int64_t)this->Segments.at(index)->p_offset; |
|
577 | 577 | } |
|
578 | 578 | } |
|
579 | 579 | return Offset; |
|
580 | 580 | } |
|
581 | 581 | |
|
582 | 582 | |
|
583 | 583 | qint64 ElfFile::getSegmentVaddr(int index) |
|
584 | 584 | { |
|
585 | 585 | int64_t Vaddr = 0; |
|
586 | 586 | if(this->e!=NULL) |
|
587 | 587 | { |
|
588 | 588 | if(index < this->Segments.count()) |
|
589 | 589 | { |
|
590 | 590 | Vaddr = (int64_t)this->Segments.at(index)->p_vaddr; |
|
591 | 591 | } |
|
592 | 592 | } |
|
593 | 593 | return Vaddr; |
|
594 | 594 | } |
|
595 | 595 | |
|
596 | 596 | |
|
597 | 597 | qint64 ElfFile::getSegmentPaddr(int index) |
|
598 | 598 | { |
|
599 | 599 | int64_t Paddr=0; |
|
600 | 600 | if(this->e!=NULL) |
|
601 | 601 | { |
|
602 | 602 | if(index < this->Segments.count()) |
|
603 | 603 | { |
|
604 | 604 | Paddr = (int64_t)this->Segments.at(index)->p_paddr; |
|
605 | 605 | } |
|
606 | 606 | } |
|
607 | 607 | return Paddr; |
|
608 | 608 | } |
|
609 | 609 | |
|
610 | 610 | qint64 ElfFile::getSectionPaddr(int index) |
|
611 | 611 | { |
|
612 | 612 | int64_t Paddr=0; |
|
613 | 613 | if(this->e!=NULL) |
|
614 | 614 | { |
|
615 | 615 | if(index < this->sections.count()) |
|
616 | 616 | { |
|
617 | 617 | Paddr = (int64_t)this->sections.at(index)->section_header->sh_addr; |
|
618 | 618 | } |
|
619 | 619 | } |
|
620 | 620 | return Paddr; |
|
621 | 621 | } |
|
622 | 622 | |
|
623 | 623 | |
|
624 | 624 | qint64 ElfFile::getSegmentFilesz(int index) |
|
625 | 625 | { |
|
626 | 626 | int64_t FileSz=0; |
|
627 | 627 | if(this->e!=NULL) |
|
628 | 628 | { |
|
629 | 629 | if(index < this->Segments.count()) |
|
630 | 630 | { |
|
631 | 631 | FileSz = (int64_t)this->Segments.at(index)->p_filesz; |
|
632 | 632 | } |
|
633 | 633 | } |
|
634 | 634 | return FileSz; |
|
635 | 635 | } |
|
636 | 636 | |
|
637 | 637 | qint64 ElfFile::getSectionDatasz(int index) |
|
638 | 638 | { |
|
639 | 639 | int64_t DataSz=0; |
|
640 | 640 | if(this->e!=NULL) |
|
641 | 641 | { |
|
642 | 642 | if(index < this->sections.count()) |
|
643 | 643 | { |
|
644 | 644 | DataSz = (int64_t)this->sections.at(index)->data->d_size; |
|
645 | 645 | } |
|
646 | 646 | } |
|
647 | 647 | return DataSz; |
|
648 | 648 | } |
|
649 | 649 | |
|
650 | 650 | bool ElfFile::getSectionData(int index, char **buffer) |
|
651 | 651 | { |
|
652 | 652 | if(this->e!=NULL) |
|
653 | 653 | { |
|
654 | 654 | if(index < this->sections.count()) |
|
655 | 655 | { |
|
656 | 656 | *buffer = (char *)this->sections.at(index)->data->d_buf; |
|
657 | 657 | return true; |
|
658 | 658 | } |
|
659 | 659 | } |
|
660 | 660 | return false; |
|
661 | 661 | } |
|
662 | 662 | |
|
663 | 663 | |
|
664 | 664 | qint64 ElfFile::getSegmentMemsz(int index) |
|
665 | 665 | { |
|
666 | 666 | int64_t MemSz=0; |
|
667 | 667 | if(this->e!=NULL) |
|
668 | 668 | { |
|
669 | 669 | if(index < this->Segments.count()) |
|
670 | 670 | { |
|
671 | 671 | MemSz = (int64_t)this->Segments.at(index)->p_memsz; |
|
672 | 672 | } |
|
673 | 673 | } |
|
674 | 674 | return MemSz; |
|
675 | 675 | } |
|
676 | 676 | |
|
677 | 677 | qint64 ElfFile::getSectionMemsz(int index) |
|
678 | 678 | { |
|
679 | 679 | int64_t MemSz=0; |
|
680 | 680 | if(this->e!=NULL) |
|
681 | 681 | { |
|
682 | 682 | if(index < this->sections.count()) |
|
683 | 683 | { |
|
684 | 684 | MemSz = (int64_t)this->sections.at(index)->section_header->sh_size; |
|
685 | 685 | } |
|
686 | 686 | } |
|
687 | 687 | return MemSz; |
|
688 | 688 | } |
|
689 | 689 | |
|
690 | 690 | |
|
691 | 691 | QString ElfFile::getSegmentFlags(int index) |
|
692 | 692 | { |
|
693 | 693 | QString flags(""); |
|
694 | 694 | if(this->e!=NULL) |
|
695 | 695 | { |
|
696 | 696 | if(index < this->Segments.count()) |
|
697 | 697 | { |
|
698 | 698 | if((this->Segments.at(index)->p_flags&PF_X) == PF_X)flags+="x"; |
|
699 | 699 | if((this->Segments.at(index)->p_flags&PF_W) == PF_W)flags+="w"; |
|
700 | 700 | if((this->Segments.at(index)->p_flags&PF_R) == PF_R)flags+="r"; |
|
701 | 701 | if((this->Segments.at(index)->p_flags&PF_MASKOS) == PF_MASKOS)flags+=" OS-specific"; |
|
702 | 702 | if((this->Segments.at(index)->p_flags&PF_MASKPROC) == PF_MASKPROC)flags+=" Processor-specific"; |
|
703 | 703 | } |
|
704 | 704 | } |
|
705 | 705 | return flags; |
|
706 | 706 | } |
|
707 | 707 | |
|
708 | 708 | |
|
709 | 709 | QString ElfFile::getSectionName(int index) |
|
710 | 710 | { |
|
711 | char* nameChr = elf_strptr(this->e , this->shstrndx , this->sections.at(index)->section_header->sh_name); | |
|
712 | return QString(nameChr); | |
|
711 | if((index<sections.count()) && (index>=0)) | |
|
712 | { | |
|
713 | char* nameChr = elf_strptr(this->e , this->shstrndx , this->sections.at(index)->section_header->sh_name); | |
|
714 | return QString(nameChr); | |
|
715 | } | |
|
716 | return ""; | |
|
713 | 717 | } |
|
714 | 718 | |
|
715 | 719 | |
|
716 | 720 | void ElfFile::updateSections() |
|
717 | 721 | { |
|
718 | 722 | for(int i=0;i<this->sections.count();i++) |
|
719 | 723 | { |
|
720 | 724 | delete this->sections.at(i); |
|
721 | 725 | } |
|
722 | 726 | this->sections.clear(); |
|
723 | 727 | this->scn = elf_nextscn (this->e , NULL ); |
|
724 | 728 | this->SectionCount = 0; |
|
725 | 729 | while( this->scn != NULL ) |
|
726 | 730 | { |
|
727 | 731 | GElf_Shdr* shdr = (GElf_Shdr*)malloc(sizeof(GElf_Shdr)); |
|
728 | 732 | gelf_getshdr ( this->scn , shdr ); |
|
729 | 733 | Elf_Data* data = elf_getdata(this->scn, NULL); |
|
730 | 734 | this->sections.append(new Elf_Section(data,shdr)); |
|
731 | 735 | this->SectionCount+=1; |
|
732 | 736 | this->scn = elf_nextscn(e , scn); |
|
733 | 737 | } |
|
734 | 738 | } |
|
735 | 739 | |
|
736 | 740 | |
|
737 | 741 | void ElfFile::updateSegments() |
|
738 | 742 | { |
|
739 | 743 | elf_getphdrnum (this->e , &this->SegmentCount); |
|
740 | 744 | for(int i=0;i<this->Segments.count();i++) |
|
741 | 745 | { |
|
742 | 746 | free(this->Segments.at(i)); |
|
743 | 747 | } |
|
744 | 748 | this->Segments.clear(); |
|
745 | 749 | for(int i=0;i<this->SegmentCount;i++) |
|
746 | 750 | { |
|
747 | 751 | GElf_Phdr* header=(GElf_Phdr*)malloc(sizeof(GElf_Phdr)); |
|
748 | 752 | gelf_getphdr (this->e , i , header ); |
|
749 | 753 | this->Segments.append(header); |
|
750 | 754 | } |
|
751 | 755 | } |
|
752 | 756 | |
|
753 | 757 | void ElfFile::updateSymbols() |
|
754 | 758 | { |
|
755 | 759 | for(int i=0;i<symbols.count();i++) |
|
756 | 760 | { |
|
757 | 761 | delete this->symbols.at(i); |
|
758 | 762 | } |
|
759 | 763 | this->symbols.clear(); |
|
760 | 764 | updateSections(); //Useless in most case but safer to do it |
|
761 | 765 | for(int i=0;i<SectionCount;i++) |
|
762 | 766 | { |
|
763 | 767 | //First find Symbol table |
|
764 | 768 | if(this->getSectionName(i)==".symtab") |
|
765 | 769 | { |
|
766 | 770 | Elf_Section* sec = sections.at(i); |
|
767 | 771 | this->SymbolCount = sec->section_header->sh_size / sec->section_header->sh_entsize; |
|
768 | 772 | //Then list all symbols |
|
769 | 773 | for(int j=0;j<this->SymbolCount;j++) |
|
770 | 774 | { |
|
771 | 775 | GElf_Sym* esym = (GElf_Sym*)malloc(sizeof(GElf_Sym)); |
|
772 | 776 | gelf_getsym(sec->data, j, esym); |
|
773 | 777 | QString name = elf_strptr(this->e,sec->section_header->sh_link,esym->st_name); |
|
774 | 778 | Elf_Symbol* sym = new Elf_Symbol(name,esym); |
|
775 | 779 | symbols.append(sym); |
|
776 | 780 | } |
|
777 | 781 | } |
|
778 | 782 | } |
|
779 | 783 | |
|
780 | 784 | } |
|
781 | 785 | |
|
782 | 786 | |
|
783 | 787 | |
|
784 | 788 | QString ElfFile::getSectionType(int index) |
|
785 | 789 | { |
|
786 | 790 | QString type(""); |
|
787 | 791 | if(this->e!=NULL) |
|
788 | 792 | { |
|
789 | 793 | if(index < this->Segments.count()) |
|
790 | 794 | { |
|
791 | 795 | switch(this->Segments.at(index)->p_type) |
|
792 | 796 | { |
|
793 | 797 | case SHT_NULL : type = "Section header table entry unused"; break; |
|
794 | 798 | case SHT_PROGBITS : type = "Program data"; break; |
|
795 | 799 | case SHT_SYMTAB : type = "Symbol table"; break; |
|
796 | 800 | case SHT_STRTAB : type = "String table"; break; |
|
797 | 801 | case SHT_RELA : type = "Relocation entries with addends"; break; |
|
798 | 802 | case SHT_HASH : type = "Symbol hash table"; break; |
|
799 | 803 | case SHT_DYNAMIC : type = "Dynamic linking information"; break; |
|
800 | 804 | case SHT_NOTE : type = "Notes"; break; |
|
801 | 805 | case SHT_NOBITS :type = "Program space with no data (bss)"; break; |
|
802 | 806 | case SHT_REL :type = "Relocation entries, no addends"; break; |
|
803 | 807 | case SHT_SHLIB : type = "Reserved"; break; |
|
804 | 808 | case SHT_DYNSYM : type = "Dynamic linker symbol table"; break; |
|
805 | 809 | case SHT_INIT_ARRAY : type = "Array of constructors"; break; |
|
806 | 810 | case SHT_FINI_ARRAY : type = "Array of destructors"; break; |
|
807 | 811 | case SHT_PREINIT_ARRAY : type = "Array of pre-constructors"; break; |
|
808 | 812 | case SHT_GROUP : type = "Section group"; break; |
|
809 | 813 | case SHT_SYMTAB_SHNDX : type = "Extended section indeces"; break; |
|
810 | 814 | case SHT_NUM : type = "Number of defined types. "; break; |
|
811 | 815 | case SHT_LOOS : type = "Start OS-specific. "; break; |
|
812 | 816 | case SHT_LOSUNW : type = "Sun-specific low bound. "; break; |
|
813 | 817 | case SHT_SUNW_COMDAT : type = " "; break; |
|
814 | 818 | case SHT_SUNW_syminfo : type = " "; break; |
|
815 | 819 | case SHT_GNU_verdef : type = "Version definition section. "; break; |
|
816 | 820 | case SHT_GNU_verneed : type = "Version needs section. "; break; |
|
817 | 821 | case SHT_GNU_versym : type = "Version symbol table. "; break; |
|
818 | 822 | case SHT_LOPROC : type = "Start of processor-specific"; break; |
|
819 | 823 | case SHT_HIPROC : type = "End of processor-specific"; break; |
|
820 | 824 | case SHT_HIUSER : type = "End of application-specific"; break; |
|
821 | 825 | } |
|
822 | 826 | } |
|
823 | 827 | } |
|
824 | 828 | return type; |
|
825 | 829 | } |
|
826 | 830 | |
|
831 | QString ElfFile::getSymbolName(int index) | |
|
832 | { | |
|
833 | if(this->e!=NULL) | |
|
834 | { | |
|
835 | if(index < this->symbols.count()) | |
|
836 | { | |
|
837 | return symbols.at(index)->name; | |
|
838 | } | |
|
839 | } | |
|
840 | return ""; | |
|
841 | } | |
|
842 | ||
|
843 | QString ElfFile::getSymbolType(int index) | |
|
844 | { | |
|
845 | if(this->e!=NULL) | |
|
846 | { | |
|
847 | if(index < this->symbols.count()) | |
|
848 | { | |
|
849 | int type = GELF_ST_TYPE(symbols.at(index)->sym->st_info); | |
|
850 | switch(type) | |
|
851 | { | |
|
852 | case STT_NOTYPE: | |
|
853 | return "No Type"; | |
|
854 | break; | |
|
855 | case STT_OBJECT: | |
|
856 | return "Object"; | |
|
857 | break; | |
|
858 | case STT_FUNC: | |
|
859 | return "Function"; | |
|
860 | break; | |
|
861 | case STT_SECTION: | |
|
862 | return "Section"; | |
|
863 | break; | |
|
864 | case STT_FILE: | |
|
865 | return "File"; | |
|
866 | break; | |
|
867 | case STT_COMMON: | |
|
868 | return "Common data object"; | |
|
869 | break; | |
|
870 | case STT_TLS: | |
|
871 | return "Thread-local data object"; | |
|
872 | break; | |
|
873 | case STT_NUM: | |
|
874 | return "Number of defined types"; | |
|
875 | break; | |
|
876 | case STT_LOOS: | |
|
877 | return "Start of OS-specific"; | |
|
878 | break; | |
|
879 | case STT_HIOS: | |
|
880 | return "End of OS-specific"; | |
|
881 | break; | |
|
882 | case STT_LOPROC: | |
|
883 | return "Start of processor-specific"; | |
|
884 | break; | |
|
885 | case STT_HIPROC: | |
|
886 | return "End of processor-specific"; | |
|
887 | break; | |
|
888 | default: | |
|
889 | return "none"; | |
|
890 | break; | |
|
891 | } | |
|
892 | } | |
|
893 | } | |
|
894 | return "none"; | |
|
895 | } | |
|
896 | ||
|
897 | quint64 ElfFile::getSymbolSize(int index) | |
|
898 | { | |
|
899 | if(this->e!=NULL) | |
|
900 | { | |
|
901 | if((index < this->symbols.count()) && (index>=0)) | |
|
902 | { | |
|
903 | return symbols.at(index)->sym->st_size; | |
|
904 | } | |
|
905 | } | |
|
906 | return 0; | |
|
907 | } | |
|
908 | ||
|
909 | QString ElfFile::getSymbolSectionName(int index) | |
|
910 | { | |
|
911 | if(this->e!=NULL) | |
|
912 | { | |
|
913 | if((index < this->symbols.count()) && (index>=0)) | |
|
914 | { | |
|
915 | return getSectionName(symbols.at(index)->sym->st_shndx-1); | |
|
916 | } | |
|
917 | } | |
|
918 | return "none"; | |
|
919 | } | |
|
920 | ||
|
921 | int ElfFile::getSymbolSectionIndex(int index) | |
|
922 | { | |
|
923 | if(this->e!=NULL) | |
|
924 | { | |
|
925 | if((index < this->symbols.count()) && (index>=0)) | |
|
926 | { | |
|
927 | return symbols.at(index)->sym->st_shndx; | |
|
928 | } | |
|
929 | } | |
|
930 | return 0; | |
|
931 | } | |
|
932 | ||
|
933 | quint64 ElfFile::getSymbolAddress(int index) | |
|
934 | { | |
|
935 | if(this->e!=NULL) | |
|
936 | { | |
|
937 | if((index < this->symbols.count()) && (index>=0)) | |
|
938 | { | |
|
939 | return symbols.at(index)->sym->st_value; | |
|
940 | } | |
|
941 | } | |
|
942 | return 0; | |
|
943 | } | |
|
944 | ||
|
945 | QString ElfFile::getSymbolLinkType(int index) | |
|
946 | { | |
|
947 | if(this->e!=NULL) | |
|
948 | { | |
|
949 | if(index < this->symbols.count()) | |
|
950 | { | |
|
951 | int btype = GELF_ST_BIND(symbols.at(index)->sym->st_info); | |
|
952 | switch(btype) | |
|
953 | { | |
|
954 | case STB_LOCAL: | |
|
955 | return "Local"; | |
|
956 | break; | |
|
957 | case STB_GLOBAL: | |
|
958 | return "Global"; | |
|
959 | break; | |
|
960 | case STB_WEAK: | |
|
961 | return "Weak"; | |
|
962 | break; | |
|
963 | case STB_NUM: | |
|
964 | return "Number of defined types"; | |
|
965 | break; | |
|
966 | case STB_LOOS: | |
|
967 | return "Start of OS-specific"; | |
|
968 | break; | |
|
969 | case STB_HIOS: | |
|
970 | return "End of OS-specific"; | |
|
971 | break; | |
|
972 | case STB_LOPROC: | |
|
973 | return "Start of processor-specific"; | |
|
974 | break; | |
|
975 | case STB_HIPROC: | |
|
976 | return "End of processor-specific"; | |
|
977 | break; | |
|
978 | default: | |
|
979 | return "none"; | |
|
980 | break; | |
|
981 | } | |
|
982 | } | |
|
983 | } | |
|
984 | return "none"; | |
|
985 | } | |
|
986 | ||
|
827 | 987 | bool ElfFile::isElf(const QString &File) |
|
828 | 988 | { |
|
829 | 989 | int file =0; |
|
830 | 990 | #ifdef _ELF_WINDOWS_ |
|
831 | 991 | file = open(File.toStdString().c_str(),O_RDONLY|O_BINARY ,0); |
|
832 | 992 | #else |
|
833 | 993 | file = open(File.toStdString().c_str(),O_RDONLY ,0); |
|
834 | 994 | #endif |
|
835 | 995 | char Magic[4]; |
|
836 | 996 | if(file!=-1) |
|
837 | 997 | { |
|
838 | 998 | read(file,Magic,4); |
|
839 | 999 | close(file); |
|
840 | 1000 | if(Magic[0]==0x7f && Magic[1]==0x45 && Magic[2]==0x4c && Magic[3]==0x46) |
|
841 | 1001 | { |
|
842 | 1002 | return true; |
|
843 | 1003 | } |
|
844 | 1004 | } |
|
845 | 1005 | return false; |
|
846 | 1006 | } |
@@ -1,119 +1,130 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the SocExplorer Software |
|
3 | 3 | -- Copyright (C) 2014, 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 2 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@member.fsf.org |
|
21 | 21 | ----------------------------------------------------------------------------*/ |
|
22 | 22 | #include <abstractexecfile.h> |
|
23 | 23 | #include <QtCore/QObject> |
|
24 | 24 | #include <QtCore/QStringList> |
|
25 | 25 | #include <libelf.h> |
|
26 | 26 | #include <gelf.h> |
|
27 | 27 | #include <sys/types.h> |
|
28 | 28 | #include <sys/stat.h> |
|
29 | 29 | #include <fcntl.h> |
|
30 | 30 | #include <unistd.h> |
|
31 | 31 | #ifndef ELFFILE_H |
|
32 | 32 | #define ELFFILE_H |
|
33 | 33 | |
|
34 | 34 | class Elf_Section |
|
35 | 35 | { |
|
36 | 36 | public: |
|
37 | 37 | Elf_Section(){} |
|
38 | 38 | Elf_Section(Elf_Data* data,GElf_Shdr* section_header) |
|
39 | 39 | { |
|
40 | 40 | this->data = data; |
|
41 | 41 | this->section_header = section_header; |
|
42 | 42 | } |
|
43 | 43 | ~Elf_Section() |
|
44 | 44 | { |
|
45 | 45 | free(section_header); |
|
46 | 46 | } |
|
47 | 47 | Elf_Data* data; |
|
48 | 48 | GElf_Shdr* section_header; |
|
49 | 49 | }; |
|
50 | 50 | |
|
51 | 51 | class Elf_Symbol |
|
52 | 52 | { |
|
53 | 53 | public: |
|
54 | 54 | Elf_Symbol(){} |
|
55 | 55 | Elf_Symbol(const QString& name,GElf_Sym* sym):name(name),sym(sym){} |
|
56 | 56 | ~Elf_Symbol(){free(sym);} |
|
57 | 57 | QString name; |
|
58 | 58 | GElf_Sym* sym; |
|
59 | 59 | }; |
|
60 | 60 | |
|
61 | 61 | class ElfFile : public abstractExecFile |
|
62 | 62 | { |
|
63 | 63 | Q_OBJECT |
|
64 | 64 | public: |
|
65 | 65 | ElfFile(); |
|
66 | 66 | ElfFile(const QString& File); |
|
67 | 67 | bool openFile(const QString& File); |
|
68 | 68 | bool isopened(); |
|
69 | 69 | int closeFile(); |
|
70 | 70 | QList<codeFragment*> getFragments(); |
|
71 | 71 | QList<codeFragment*> getFragments(QStringList fragmentList); |
|
72 | 72 | |
|
73 | 73 | QString getClass(); |
|
74 | 74 | QString getArchitecture(); |
|
75 | 75 | QString getType(); |
|
76 | 76 | QString getEndianness(); |
|
77 | 77 | QString getABI(); |
|
78 | 78 | qint64 getVersion(); |
|
79 | 79 | qint64 getEntryPointAddress(); |
|
80 | ||
|
80 | 81 | int getSectionCount(); |
|
81 | 82 | int getSymbolCount(); |
|
82 | 83 | int getSegmentCount(); |
|
84 | ||
|
83 | 85 | QString getSegmentType(int index); |
|
84 | 86 | qint64 getSegmentOffset(int index); |
|
85 | 87 | qint64 getSegmentVaddr(int index); |
|
86 | 88 | qint64 getSegmentPaddr(int index); |
|
87 | qint64 getSectionPaddr(int index); | |
|
88 | 89 | qint64 getSegmentFilesz(int index); |
|
89 | 90 | qint64 getSectionDatasz(int index); |
|
91 | qint64 getSegmentMemsz(int index); | |
|
92 | QString getSegmentFlags(int index); | |
|
93 | ||
|
90 | 94 | bool getSectionData(int index, char **buffer); |
|
91 |
qint64 getSe |
|
|
95 | qint64 getSectionPaddr(int index); | |
|
92 | 96 | qint64 getSectionMemsz(int index); |
|
93 | QString getSegmentFlags(int index); | |
|
94 | 97 | QString getSectionName(int index); |
|
95 | 98 | QString getSectionType(int index); |
|
99 | ||
|
100 | QString getSymbolName(int index); | |
|
101 | QString getSymbolType(int index); | |
|
102 | quint64 getSymbolSize(int index); | |
|
103 | QString getSymbolSectionName(int index); | |
|
104 | int getSymbolSectionIndex(int index); | |
|
105 | quint64 getSymbolAddress(int index); | |
|
106 | QString getSymbolLinkType(int index); | |
|
96 | 107 | bool iself(); |
|
97 | 108 | static bool isElf(const QString& File); |
|
98 | 109 | |
|
99 | 110 | private: |
|
100 | 111 | codeFragment* getFragment(const QString& name); |
|
101 | 112 | void updateSections(); |
|
102 | 113 | void updateSegments(); |
|
103 | 114 | void updateSymbols(); |
|
104 | 115 | int elfFile; |
|
105 | 116 | bool opened; |
|
106 | 117 | bool type_elf; |
|
107 | 118 | Elf* e; |
|
108 | 119 | Elf_Kind ek; |
|
109 | 120 | GElf_Ehdr ehdr; |
|
110 | 121 | Elf_Scn * scn; |
|
111 | 122 | Elf_Data * data; |
|
112 | 123 | size_t SymbolCount,SectionCount,SegmentCount, shstrndx; |
|
113 | 124 | QList<GElf_Phdr*> Segments; |
|
114 | 125 | QList<Elf_Section*> sections; |
|
115 | 126 | QList<Elf_Symbol*> symbols; |
|
116 | 127 | |
|
117 | 128 | }; |
|
118 | 129 | |
|
119 | 130 | #endif // ELFFILE_H |
@@ -1,46 +1,81 | |||
|
1 | 1 | #include "elffilewidget.h" |
|
2 | 2 | #include "ui_elffilewidget.h" |
|
3 | #include <QtWidgets/QTableWidgetItem> | |
|
3 | 4 | |
|
4 | 5 | elfFileWidget::elfFileWidget(QWidget *parent) : |
|
5 | 6 | QWidget(parent), |
|
6 | 7 | ui(new Ui::elfFileWidget) |
|
7 | 8 | { |
|
8 | 9 | ui->setupUi(this); |
|
9 | 10 | } |
|
10 | 11 | |
|
11 | 12 | elfFileWidget::~elfFileWidget() |
|
12 | 13 | { |
|
13 | 14 | delete ui; |
|
14 | 15 | } |
|
15 | 16 | |
|
16 | 17 | void elfFileWidget::updateElfFile(ElfFile *file) |
|
17 | 18 | { |
|
18 | 19 | this->p_elf = file; |
|
19 | 20 | if(p_elf->isopened() && p_elf->iself()) |
|
20 | 21 | { |
|
21 | 22 | this->ui->classLabel->setText(p_elf->getClass()); |
|
22 | 23 | this->ui->VersionLabel->setText(QString::number(p_elf->getVersion())); |
|
23 | 24 | this->ui->machineLabel->setText(p_elf->getArchitecture()); |
|
24 | 25 | this->ui->endiannesLabel->setText(p_elf->getEndianness()); |
|
25 | 26 | this->ui->abiLabel->setText(p_elf->getABI()); |
|
26 | 27 | this->ui->entryPointLabel->setText(QString("0x%1").arg((uint)p_elf->getEntryPointAddress(),8,16)); |
|
27 | 28 | this->ui->typeLabel->setText(p_elf->getType()); |
|
28 | 29 | this->ui->sectionCountLabel->setText(QString::number(p_elf->getSectionCount())); |
|
29 | 30 | this->ui->symbolCountLabel->setText(QString::number(p_elf->getSymbolCount())); |
|
30 | 31 | } |
|
32 | updateSymbols(); | |
|
33 | } | |
|
34 | ||
|
35 | void elfFileWidget::updateSymbols() | |
|
36 | { | |
|
37 | this->ui->symbolsList->clear(); | |
|
38 | this->ui->symbolsList->setRowCount(p_elf->getSymbolCount()); | |
|
39 | this->ui->symbolsList->setHorizontalHeaderLabels(QStringList()<<"Value"<<"Size"<<"Type"<<"Link"<<"Section"<<"Name"); | |
|
40 | for(int i=0;i<p_elf->getSymbolCount();i++) | |
|
41 | { | |
|
42 | QTableWidgetItem *newItem = new QTableWidgetItem(QString("0x%1").arg(p_elf->getSymbolAddress(i),8,16)); | |
|
43 | newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable); | |
|
44 | this->ui->symbolsList->setItem(i, 0, newItem); | |
|
45 | ||
|
46 | newItem = new QTableWidgetItem(QString("0x%1").arg(p_elf->getSymbolSize(i),8,16)); | |
|
47 | newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable); | |
|
48 | this->ui->symbolsList->setItem(i, 1, newItem); | |
|
49 | ||
|
50 | newItem = new QTableWidgetItem(p_elf->getSymbolType(i)); | |
|
51 | newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable); | |
|
52 | this->ui->symbolsList->setItem(i, 2, newItem); | |
|
53 | ||
|
54 | newItem = new QTableWidgetItem(p_elf->getSymbolLinkType(i)); | |
|
55 | newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable); | |
|
56 | this->ui->symbolsList->setItem(i, 3, newItem); | |
|
57 | ||
|
58 | newItem = new QTableWidgetItem(p_elf->getSymbolSectionName(i)); | |
|
59 | newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable); | |
|
60 | this->ui->symbolsList->setItem(i, 4, newItem); | |
|
61 | ||
|
62 | newItem = new QTableWidgetItem(p_elf->getSymbolName(i)); | |
|
63 | newItem->setFlags(newItem->flags() ^ Qt::ItemIsEditable); | |
|
64 | this->ui->symbolsList->setItem(i, 5, newItem); | |
|
65 | } | |
|
66 | this->ui->symbolsList->resizeColumnsToContents(); | |
|
31 | 67 | } |
|
32 | 68 | |
|
33 | 69 | |
|
34 | 70 | |
|
35 | 71 | |
|
36 | 72 | |
|
37 | 73 | |
|
38 | 74 | |
|
39 | 75 | |
|
40 | 76 | |
|
41 | 77 | |
|
42 | 78 | |
|
43 | 79 | |
|
44 | 80 | |
|
45 | 81 | |
|
46 |
@@ -1,27 +1,28 | |||
|
1 | 1 | #ifndef ELFFILEWIDGET_H |
|
2 | 2 | #define ELFFILEWIDGET_H |
|
3 | 3 | |
|
4 | 4 | #include <QtWidgets/QWidget> |
|
5 | 5 | #include "elffile.h" |
|
6 | 6 | |
|
7 | 7 | namespace Ui { |
|
8 | 8 | class elfFileWidget; |
|
9 | 9 | } |
|
10 | 10 | |
|
11 | 11 | class elfFileWidget : public QWidget |
|
12 | 12 | { |
|
13 | 13 | Q_OBJECT |
|
14 | 14 | |
|
15 | 15 | public: |
|
16 | 16 | elfFileWidget(QWidget *parent = 0); |
|
17 | 17 | ~elfFileWidget(); |
|
18 | 18 | |
|
19 | 19 | public slots: |
|
20 | 20 | void updateElfFile(ElfFile* file); |
|
21 | void updateSymbols(); | |
|
21 | 22 | |
|
22 | 23 | private: |
|
23 | 24 | Ui::elfFileWidget *ui; |
|
24 | 25 | ElfFile* p_elf; |
|
25 | 26 | }; |
|
26 | 27 | |
|
27 | 28 | #endif // ELFFILEWIDGET_H |
@@ -1,226 +1,260 | |||
|
1 | 1 | <?xml version="1.0" encoding="UTF-8"?> |
|
2 | 2 | <ui version="4.0"> |
|
3 | 3 | <class>elfFileWidget</class> |
|
4 | 4 | <widget class="QWidget" name="elfFileWidget"> |
|
5 | 5 | <property name="geometry"> |
|
6 | 6 | <rect> |
|
7 | 7 | <x>0</x> |
|
8 | 8 | <y>0</y> |
|
9 |
<width>6 |
|
|
10 |
<height>3 |
|
|
9 | <width>786</width> | |
|
10 | <height>387</height> | |
|
11 | 11 | </rect> |
|
12 | 12 | </property> |
|
13 | 13 | <property name="minimumSize"> |
|
14 | 14 | <size> |
|
15 | 15 | <width>0</width> |
|
16 | 16 | <height>0</height> |
|
17 | 17 | </size> |
|
18 | 18 | </property> |
|
19 | 19 | <property name="focusPolicy"> |
|
20 | 20 | <enum>Qt::NoFocus</enum> |
|
21 | 21 | </property> |
|
22 | 22 | <property name="windowTitle"> |
|
23 | 23 | <string>Form</string> |
|
24 | 24 | </property> |
|
25 | 25 | <layout class="QGridLayout" name="gridLayout"> |
|
26 | 26 | <item row="0" column="0"> |
|
27 | 27 | <widget class="QTabWidget" name="tabWidget"> |
|
28 | 28 | <property name="currentIndex"> |
|
29 |
<number> |
|
|
29 | <number>1</number> | |
|
30 | 30 | </property> |
|
31 | 31 | <widget class="QWidget" name="generalInfoTab"> |
|
32 | 32 | <attribute name="title"> |
|
33 | 33 | <string>File Informations</string> |
|
34 | 34 | </attribute> |
|
35 | 35 | <layout class="QGridLayout" name="gridLayout_4"> |
|
36 | 36 | <item row="0" column="0"> |
|
37 | 37 | <widget class="QGroupBox" name="groupBox"> |
|
38 | 38 | <property name="title"> |
|
39 | 39 | <string>Elf header</string> |
|
40 | 40 | </property> |
|
41 | 41 | <layout class="QFormLayout" name="formLayout"> |
|
42 | 42 | <item row="0" column="0"> |
|
43 | 43 | <widget class="QLabel" name="label"> |
|
44 | 44 | <property name="text"> |
|
45 | 45 | <string>Class:</string> |
|
46 | 46 | </property> |
|
47 | 47 | </widget> |
|
48 | 48 | </item> |
|
49 | 49 | <item row="0" column="1"> |
|
50 | 50 | <widget class="QLabel" name="classLabel"> |
|
51 | 51 | <property name="text"> |
|
52 | 52 | <string>none</string> |
|
53 | 53 | </property> |
|
54 | 54 | </widget> |
|
55 | 55 | </item> |
|
56 | 56 | <item row="1" column="0"> |
|
57 | 57 | <widget class="QLabel" name="label_3"> |
|
58 | 58 | <property name="text"> |
|
59 | 59 | <string>Endianness:</string> |
|
60 | 60 | </property> |
|
61 | 61 | </widget> |
|
62 | 62 | </item> |
|
63 | 63 | <item row="1" column="1"> |
|
64 | 64 | <widget class="QLabel" name="endiannesLabel"> |
|
65 | 65 | <property name="text"> |
|
66 | 66 | <string>none</string> |
|
67 | 67 | </property> |
|
68 | 68 | </widget> |
|
69 | 69 | </item> |
|
70 | 70 | <item row="2" column="0"> |
|
71 | 71 | <widget class="QLabel" name="label_5"> |
|
72 | 72 | <property name="text"> |
|
73 | 73 | <string>Version:</string> |
|
74 | 74 | </property> |
|
75 | 75 | </widget> |
|
76 | 76 | </item> |
|
77 | 77 | <item row="3" column="0"> |
|
78 | 78 | <widget class="QLabel" name="label_6"> |
|
79 | 79 | <property name="text"> |
|
80 | 80 | <string>Type:</string> |
|
81 | 81 | </property> |
|
82 | 82 | </widget> |
|
83 | 83 | </item> |
|
84 | 84 | <item row="4" column="0"> |
|
85 | 85 | <widget class="QLabel" name="label_7"> |
|
86 | 86 | <property name="text"> |
|
87 | 87 | <string>Machine:</string> |
|
88 | 88 | </property> |
|
89 | 89 | </widget> |
|
90 | 90 | </item> |
|
91 | 91 | <item row="6" column="0"> |
|
92 | 92 | <widget class="QLabel" name="label_8"> |
|
93 | 93 | <property name="text"> |
|
94 | 94 | <string>Entry point address:</string> |
|
95 | 95 | </property> |
|
96 | 96 | </widget> |
|
97 | 97 | </item> |
|
98 | 98 | <item row="2" column="1"> |
|
99 | 99 | <widget class="QLabel" name="VersionLabel"> |
|
100 | 100 | <property name="text"> |
|
101 | 101 | <string>none</string> |
|
102 | 102 | </property> |
|
103 | 103 | </widget> |
|
104 | 104 | </item> |
|
105 | 105 | <item row="3" column="1"> |
|
106 | 106 | <widget class="QLabel" name="typeLabel"> |
|
107 | 107 | <property name="text"> |
|
108 | 108 | <string>none</string> |
|
109 | 109 | </property> |
|
110 | 110 | </widget> |
|
111 | 111 | </item> |
|
112 | 112 | <item row="4" column="1"> |
|
113 | 113 | <widget class="QLabel" name="machineLabel"> |
|
114 | 114 | <property name="text"> |
|
115 | 115 | <string>none</string> |
|
116 | 116 | </property> |
|
117 | 117 | </widget> |
|
118 | 118 | </item> |
|
119 | 119 | <item row="6" column="1"> |
|
120 | 120 | <widget class="QLabel" name="entryPointLabel"> |
|
121 | 121 | <property name="text"> |
|
122 | 122 | <string>none</string> |
|
123 | 123 | </property> |
|
124 | 124 | </widget> |
|
125 | 125 | </item> |
|
126 | 126 | <item row="5" column="0"> |
|
127 | 127 | <widget class="QLabel" name="label_17"> |
|
128 | 128 | <property name="text"> |
|
129 | 129 | <string>OS/ABI:</string> |
|
130 | 130 | </property> |
|
131 | 131 | </widget> |
|
132 | 132 | </item> |
|
133 | 133 | <item row="5" column="1"> |
|
134 | 134 | <widget class="QLabel" name="abiLabel"> |
|
135 | 135 | <property name="text"> |
|
136 | 136 | <string>none</string> |
|
137 | 137 | </property> |
|
138 | 138 | </widget> |
|
139 | 139 | </item> |
|
140 | 140 | </layout> |
|
141 | 141 | </widget> |
|
142 | 142 | </item> |
|
143 | 143 | <item row="1" column="0"> |
|
144 | 144 | <widget class="QGroupBox" name="groupBox_2"> |
|
145 | 145 | <property name="title"> |
|
146 | 146 | <string>Sections & Symbols</string> |
|
147 | 147 | </property> |
|
148 | 148 | <layout class="QFormLayout" name="formLayout_2"> |
|
149 | 149 | <item row="0" column="0"> |
|
150 | 150 | <widget class="QLabel" name="label_9"> |
|
151 | 151 | <property name="text"> |
|
152 | 152 | <string>Section count:</string> |
|
153 | 153 | </property> |
|
154 | 154 | </widget> |
|
155 | 155 | </item> |
|
156 | 156 | <item row="1" column="0"> |
|
157 | 157 | <widget class="QLabel" name="label_10"> |
|
158 | 158 | <property name="text"> |
|
159 | 159 | <string>Symbols count:</string> |
|
160 | 160 | </property> |
|
161 | 161 | </widget> |
|
162 | 162 | </item> |
|
163 | 163 | <item row="0" column="1"> |
|
164 | 164 | <widget class="QLabel" name="sectionCountLabel"> |
|
165 | 165 | <property name="text"> |
|
166 | 166 | <string>none</string> |
|
167 | 167 | </property> |
|
168 | 168 | </widget> |
|
169 | 169 | </item> |
|
170 | 170 | <item row="1" column="1"> |
|
171 | 171 | <widget class="QLabel" name="symbolCountLabel"> |
|
172 | 172 | <property name="text"> |
|
173 | 173 | <string>none</string> |
|
174 | 174 | </property> |
|
175 | 175 | </widget> |
|
176 | 176 | </item> |
|
177 | 177 | </layout> |
|
178 | 178 | </widget> |
|
179 | 179 | </item> |
|
180 | 180 | </layout> |
|
181 | 181 | </widget> |
|
182 | 182 | <widget class="QWidget" name="symbolsTab"> |
|
183 | 183 | <attribute name="title"> |
|
184 | 184 | <string>Symbols</string> |
|
185 | 185 | </attribute> |
|
186 | 186 | <layout class="QGridLayout" name="gridLayout_2"> |
|
187 | 187 | <item row="0" column="0"> |
|
188 |
<widget class="QTableWidget" name="symbolsList" |
|
|
188 | <widget class="QTableWidget" name="symbolsList"> | |
|
189 | <property name="sortingEnabled"> | |
|
190 | <bool>true</bool> | |
|
191 | </property> | |
|
192 | <column> | |
|
193 | <property name="text"> | |
|
194 | <string>Value</string> | |
|
195 | </property> | |
|
196 | </column> | |
|
197 | <column> | |
|
198 | <property name="text"> | |
|
199 | <string>Size</string> | |
|
200 | </property> | |
|
201 | </column> | |
|
202 | <column> | |
|
203 | <property name="text"> | |
|
204 | <string>Type</string> | |
|
205 | </property> | |
|
206 | </column> | |
|
207 | <column> | |
|
208 | <property name="text"> | |
|
209 | <string>Link</string> | |
|
210 | </property> | |
|
211 | </column> | |
|
212 | <column> | |
|
213 | <property name="text"> | |
|
214 | <string>Section</string> | |
|
215 | </property> | |
|
216 | </column> | |
|
217 | <column> | |
|
218 | <property name="text"> | |
|
219 | <string>Name</string> | |
|
220 | </property> | |
|
221 | </column> | |
|
222 | </widget> | |
|
189 | 223 | </item> |
|
190 | 224 | </layout> |
|
191 | 225 | </widget> |
|
192 | 226 | <widget class="QWidget" name="sectionsTab"> |
|
193 | 227 | <attribute name="title"> |
|
194 | 228 | <string>Sections</string> |
|
195 | 229 | </attribute> |
|
196 | 230 | <layout class="QGridLayout" name="gridLayout_3"> |
|
197 | 231 | <item row="0" column="0"> |
|
198 | 232 | <widget class="QHexEdit" name="sectionsHexView" native="true"> |
|
199 | 233 | <property name="minimumSize"> |
|
200 | 234 | <size> |
|
201 | 235 | <width>100</width> |
|
202 | 236 | <height>0</height> |
|
203 | 237 | </size> |
|
204 | 238 | </property> |
|
205 | 239 | </widget> |
|
206 | 240 | </item> |
|
207 | 241 | <item row="0" column="1"> |
|
208 | 242 | <widget class="QTableWidget" name="sectionsList"/> |
|
209 | 243 | </item> |
|
210 | 244 | </layout> |
|
211 | 245 | </widget> |
|
212 | 246 | </widget> |
|
213 | 247 | </item> |
|
214 | 248 | </layout> |
|
215 | 249 | </widget> |
|
216 | 250 | <customwidgets> |
|
217 | 251 | <customwidget> |
|
218 | 252 | <class>QHexEdit</class> |
|
219 | 253 | <extends>QWidget</extends> |
|
220 | 254 | <header location="global">qhexedit.h</header> |
|
221 | 255 | <container>1</container> |
|
222 | 256 | </customwidget> |
|
223 | 257 | </customwidgets> |
|
224 | 258 | <resources/> |
|
225 | 259 | <connections/> |
|
226 | 260 | </ui> |
General Comments 0
You need to be logged in to leave comments.
Login now