##// END OF EJS Templates
Sync /!\ do not use this revision
jeandet -
r52:91fdf0217d26 default
parent child
Show More
Add another comment
@@ -0,0 +1,30
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the SocExplorer Software
3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Alexis Jeandet
20 -- Mail : alexis.jeandet@member.fsf.org
21 ----------------------------------------------------------------------------*/
22 #include "abstractbinfile.h"
23
24
25 codeFragment::codeFragment()
26 {
27 data = NULL;
28 size = 0;
29 address = 0;
30 }
@@ -0,0 +1,71
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the SocExplorer Software
3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Alexis Jeandet
20 -- Mail : alexis.jeandet@member.fsf.org
21 ----------------------------------------------------------------------------*/
22 #ifndef ABSTRACTBINFILE_H
23 #define ABSTRACTBINFILE_H
24
25 #include <QtCore/QObject>
26 #include <QtWidgets/QWidget>
27
28 class codeFragment
29 {
30 public:
31 codeFragment();
32 codeFragment(char* data, quint64 size, quint64 address):data(data),size(size),address(address){}
33 ~codeFragment()
34 {
35 free(data);
36 }
37 QString header;
38 char* data;
39 quint64 size;
40 quint64 address;
41 };
42
43 class abstractBinFile : public QObject
44 {
45 Q_OBJECT
46 public:
47
48 virtual bool openFile(const QString& File)=0;
49 virtual bool isopened()=0;
50 virtual int closeFile()=0;
51 virtual QList<codeFragment*> getFragments()=0;
52 virtual bool toSrec(const QString& File)=0;
53 virtual bool toBinary(const QString& File)=0;
54 protected:
55 QString p_fileName;
56 };
57
58
59 class abstractBinFileWidget : public QWidget
60 {
61 Q_OBJECT
62
63 public:
64 abstractBinFileWidget(QWidget* parent = 0):QWidget(parent){}
65
66 public slots:
67 virtual void setFile(abstractBinFile* file)=0;
68 virtual void reloadFile()=0;
69 };
70
71 #endif // ABSTRACTBINFILE_H
This diff has been collapsed as it changes many lines, (1075 lines changed) Show them Hide them
@@ -0,0 +1,1075
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the SocExplorer Software
3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author :
20 Alexis Jeandet
21 -- Mail :
22 alexis.jeandet@member.fsf.org
23 ----------------------------------------------------------------------------*/
24 #include "elffile.h"
25 #include "srecfile.h"
26 #include "binaryfile.h"
27 #include <stdint.h>
28
29 ElfFile::ElfFile()
30 :abstractBinFile()
31 {
32 this->opened = false;
33 this->type_elf = false;
34 this->elfFile = (int)NULL;
35 this->e = NULL;
36 }
37
38 ElfFile::ElfFile(const QString &File)
39 :abstractBinFile()
40 {
41 this->opened = false;
42 this->type_elf = false;
43 this->elfFile = (int)NULL;
44 this->e = NULL;
45 this->p_fileName = File;
46 openFile(File);
47 }
48
49 ElfFile::~ElfFile()
50 {
51 closeFile();
52 if(scn)free(scn);
53 if(data)free(data);
54 for(int i=0;i<this->sections.count();i++)
55 {
56 delete this->sections.at(i);
57 }
58 this->sections.clear();
59 for(int i=0;i<this->Segments.count();i++)
60 {
61 free(this->Segments.at(i));
62 }
63 this->Segments.clear();
64 for(int i=0;i<symbols.count();i++)
65 {
66 delete this->symbols.at(i);
67 }
68 this->symbols.clear();
69 }
70
71 bool ElfFile::openFile(const QString &File)
72 {
73 this->p_fileName = File;
74 this->closeFile();
75 if(elf_version(EV_CURRENT)==EV_NONE)return 0;
76 #ifdef _ELF_WINDOWS_
77 this->elfFile = open(File.toStdString().c_str(),O_RDONLY|O_BINARY ,0);
78 #else
79 this->elfFile = open(File.toStdString().c_str(),O_RDONLY ,0);
80 #endif
81 if(this->elfFile==(int)NULL)return 0;
82 this->e = elf_begin(this->elfFile,ELF_C_READ,NULL);
83 if(this->e==NULL)return 0;
84 this->ek = elf_kind(this->e);
85 gelf_getehdr (this->e, &this->ehdr );
86 elf_getshdrstrndx (this->e, &this->shstrndx);
87 this->updateSegments();
88 this->updateSections();
89 this->updateSymbols();
90 this->opened = true;
91 return 1;
92 }
93
94 bool ElfFile::isopened()
95 {
96 return this->opened;
97 }
98
99 int ElfFile::closeFile()
100 {
101 if(this->elfFile!=(int)NULL)
102 {
103 if(this->e!=NULL)
104 {
105 elf_end(this->e);
106 this->e = NULL;
107 }
108 close(this->elfFile);
109 this->elfFile = (int)NULL;
110 }
111 this->opened = false;
112 return 0;
113 }
114
115
116 QList<codeFragment*> ElfFile::getFragments(QStringList fragmentList)
117 {
118 QList<codeFragment*> fragments;
119 if (isopened())
120 {
121 for(int i =0;i<fragmentList.count();i++)
122 {
123 fragments.append(getFragment(fragmentList.at(i)));
124 }
125 }
126 return fragments;
127 }
128
129 QList<codeFragment*> ElfFile::getFragments()
130 {
131 return getFragments(QStringList()<<".data"<<".text");
132 }
133
134 codeFragment *ElfFile::getFragment(const QString &name)
135 {
136 codeFragment* fragment= new codeFragment();
137 for(int i=0;i<getSectionCount();i++)
138 {
139 if(getSectionName(i) == name)
140 {
141 fragment->data =NULL;
142 fragment->size = getSectionDatasz(i);
143 fragment->address = getSectionPaddr(i);
144 getSectionData(i,&fragment->data);
145 }
146 }
147 return fragment;
148 }
149
150
151
152
153
154
155
156 QString elfresolveMachine(Elf64_Half e_machine)
157 {
158 QString machineName;
159 //Update from with bash script don't write it by yourself!
160 switch(e_machine)
161 {
162 case EM_NONE:
163 machineName = " No machine ";
164 break;
165 case EM_M32:
166 machineName = " AT&T WE 32100 ";
167 break;
168 case EM_SPARC:
169 machineName = " SUN SPARC ";
170 break;
171 case EM_386:
172 machineName = " Intel 80386 ";
173 break;
174 case EM_68K:
175 machineName = " Motorola m68k family ";
176 break;
177 case EM_88K:
178 machineName = " Motorola m88k family ";
179 break;
180 case EM_860:
181 machineName = " Intel 80860 ";
182 break;
183 case EM_MIPS:
184 machineName = " MIPS R3000 big-endian ";
185 break;
186 case EM_S370:
187 machineName = " IBM System/370 ";
188 break;
189 case EM_MIPS_RS3_LE:
190 machineName = " MIPS R3000 little-endian ";
191 break;
192 case EM_PARISC:
193 machineName = " HPPA ";
194 break;
195 case EM_VPP500:
196 machineName = " Fujitsu VPP500 ";
197 break;
198 case EM_SPARC32PLUS:
199 machineName = " Sun's \"v8plus\" ";
200 break;
201 case EM_960:
202 machineName = " Intel 80960 ";
203 break;
204 case EM_PPC:
205 machineName = " PowerPC ";
206 break;
207 case EM_PPC64:
208 machineName = " PowerPC 64-bit ";
209 break;
210 case EM_S390:
211 machineName = " IBM S390 ";
212 break;
213 case EM_V800:
214 machineName = " NEC V800 series ";
215 break;
216 case EM_FR20:
217 machineName = " Fujitsu FR20 ";
218 break;
219 case EM_RH32:
220 machineName = " TRW RH-32 ";
221 break;
222 case EM_RCE:
223 machineName = " Motorola RCE ";
224 break;
225 case EM_ARM:
226 machineName = " ARM ";
227 break;
228 case EM_FAKE_ALPHA:
229 machineName = " Digital Alpha ";
230 break;
231 case EM_SH:
232 machineName = " Hitachi SH ";
233 break;
234 case EM_SPARCV9:
235 machineName = " SPARC v9 64-bit ";
236 break;
237 case EM_TRICORE:
238 machineName = " Siemens Tricore ";
239 break;
240 case EM_ARC:
241 machineName = " Argonaut RISC Core ";
242 break;
243 case EM_H8_300:
244 machineName = " Hitachi H8/300 ";
245 break;
246 case EM_H8_300H:
247 machineName = " Hitachi H8/300H ";
248 break;
249 case EM_H8S:
250 machineName = " Hitachi H8S ";
251 break;
252 case EM_H8_500:
253 machineName = " Hitachi H8/500 ";
254 break;
255 case EM_IA_64:
256 machineName = " Intel Merced ";
257 break;
258 case EM_MIPS_X:
259 machineName = " Stanford MIPS-X ";
260 break;
261 case EM_COLDFIRE:
262 machineName = " Motorola Coldfire ";
263 break;
264 case EM_68HC12:
265 machineName = " Motorola M68HC12 ";
266 break;
267 case EM_MMA:
268 machineName = " Fujitsu MMA Multimedia Accelerator";
269 break;
270 case EM_PCP:
271 machineName = " Siemens PCP ";
272 break;
273 case EM_NCPU:
274 machineName = " Sony nCPU embeeded RISC ";
275 break;
276 case EM_NDR1:
277 machineName = " Denso NDR1 microprocessor ";
278 break;
279 case EM_STARCORE:
280 machineName = " Motorola Start*Core processor ";
281 break;
282 case EM_ME16:
283 machineName = " Toyota ME16 processor ";
284 break;
285 case EM_ST100:
286 machineName = " STMicroelectronic ST100 processor ";
287 break;
288 case EM_TINYJ:
289 machineName = " Advanced Logic Corp. Tinyj emb.fam";
290 break;
291 case EM_X86_64:
292 machineName = " AMD x86-64 architecture ";
293 break;
294 case EM_PDSP:
295 machineName = " Sony DSP Processor ";
296 break;
297 case EM_FX66:
298 machineName = " Siemens FX66 microcontroller ";
299 break;
300 case EM_ST9PLUS:
301 machineName = " STMicroelectronics ST9+ 8/16 mc ";
302 break;
303 case EM_ST7:
304 machineName = " STmicroelectronics ST7 8 bit mc ";
305 break;
306 case EM_68HC16:
307 machineName = " Motorola MC68HC16 microcontroller ";
308 break;
309 case EM_68HC11:
310 machineName = " Motorola MC68HC11 microcontroller ";
311 break;
312 case EM_68HC08:
313 machineName = " Motorola MC68HC08 microcontroller ";
314 break;
315 case EM_68HC05:
316 machineName = " Motorola MC68HC05 microcontroller ";
317 break;
318 case EM_SVX:
319 machineName = " Silicon Graphics SVx ";
320 break;
321 case EM_ST19:
322 machineName = " STMicroelectronics ST19 8 bit mc ";
323 break;
324 case EM_VAX:
325 machineName = " Digital VAX ";
326 break;
327 case EM_CRIS:
328 machineName = " Axis Communications 32-bit embedded processor ";
329 break;
330 case EM_JAVELIN:
331 machineName = " Infineon Technologies 32-bit embedded processor ";
332 break;
333 case EM_FIREPATH:
334 machineName = " Element 14 64-bit DSP Processor ";
335 break;
336 case EM_ZSP:
337 machineName = " LSI Logic 16-bit DSP Processor ";
338 break;
339 case EM_MMIX:
340 machineName = " Donald Knuth's educational 64-bit processor ";
341 break;
342 case EM_HUANY:
343 machineName = " Harvard University machine-independent object files ";
344 break;
345 case EM_PRISM:
346 machineName = " SiTera Prism ";
347 break;
348 case EM_AVR:
349 machineName = " Atmel AVR 8-bit microcontroller ";
350 break;
351 case EM_FR30:
352 machineName = " Fujitsu FR30 ";
353 break;
354 case EM_D10V:
355 machineName = " Mitsubishi D10V ";
356 break;
357 case EM_D30V:
358 machineName = " Mitsubishi D30V ";
359 break;
360 case EM_V850:
361 machineName = " NEC v850 ";
362 break;
363 case EM_M32R:
364 machineName = " Mitsubishi M32R ";
365 break;
366 case EM_MN10300:
367 machineName = " Matsushita MN10300 ";
368 break;
369 case EM_MN10200:
370 machineName = " Matsushita MN10200 ";
371 break;
372 case EM_PJ:
373 machineName = " picoJava ";
374 break;
375 case EM_OPENRISC:
376 machineName = " OpenRISC 32-bit embedded processor ";
377 break;
378 case EM_ARC_A5:
379 machineName = " ARC Cores Tangent-A5 ";
380 break;
381 case EM_XTENSA:
382 machineName = " Tensilica Xtensa Architecture ";
383 break;
384 case EM_AARCH64:
385 machineName = " ARM AARCH64 ";
386 break;
387 case EM_TILEPRO:
388 machineName = " Tilera TILEPro ";
389 break;
390 case EM_MICROBLAZE:
391 machineName = " Xilinx MicroBlaze ";
392 break;
393 case EM_TILEGX:
394 machineName = " Tilera TILE-Gx ";
395 break;
396 case EM_NUM:
397 machineName = "";
398 break;
399 default:
400 machineName ="Unknow Machine";
401 break;
402 }
403 return machineName;
404 }
405
406
407
408
409 QString ElfFile::getClass()
410 {
411 if(this->e!=NULL)
412 {
413 int eclass = gelf_getclass(this->e);
414 if(eclass==ELFCLASS32)return "ELF32";
415 if(eclass==ELFCLASS64)return "ELF64";
416 }
417 return "none";
418 }
419
420
421 bool ElfFile::iself()
422 {
423 return (this->getType()!="Unknow");
424 }
425
426 QString ElfFile::getArchitecture()
427 {
428 if(this->e!=NULL)
429 {
430 return elfresolveMachine(this->ehdr.e_machine);
431 }
432 return "";
433 }
434
435
436 QString ElfFile::getType()
437 {
438 QString kind("");
439 if(this->e!=NULL)
440 {
441 switch(this->ek)
442 {
443 case ELF_K_AR:
444 kind = "Archive";
445 break;
446 case ELF_K_ELF:
447 kind = "Elf";
448 break;
449 case ELF_K_COFF:
450 kind = "COFF";
451 break;
452 case ELF_K_NUM:
453 kind = "NUM";
454 break;
455 case ELF_K_NONE:
456 kind = "Data";
457 break;
458 default:
459 kind = "Unknow";
460 break;
461 }
462 }
463 return kind;
464 }
465
466 QString ElfFile::getEndianness()
467 {
468 if(this->e!=NULL)
469 {
470 if(this->ehdr.e_ident[EI_DATA]==ELFDATA2LSB)return "2's complement, little endian";
471 if(this->ehdr.e_ident[EI_DATA]==ELFDATA2MSB)return "2's complement, big endian";
472 }
473 return "none";
474 }
475
476 QString ElfFile::getABI()
477 {
478 if(this->e!=NULL)
479 {
480 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_NONE)return "UNIX System V ABI";
481 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_SYSV)return "Alias";
482 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_HPUX)return "HP-UX";
483 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_NETBSD)return "NetBSD";
484 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_LINUX)return "Object uses GNU ELF extensions";
485 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_SOLARIS)return "Sun Solaris";
486 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_AIX)return "IBM AIX";
487 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_IRIX)return "SGI Irix";
488 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_FREEBSD)return "FreeBSD";
489 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_TRU64)return "Compaq TRU64 UNIX";
490 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_MODESTO)return " Novell Modesto";
491 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_OPENBSD)return "OpenBSD";
492 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_ARM_AEABI)return "ARM EABI";
493 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_ARM)return "ARM";
494 if(this->ehdr.e_ident[EI_OSABI]==ELFOSABI_STANDALONE)return "Standalone (embedded) application";
495 }
496 return "none";
497 }
498
499
500 qint64 ElfFile::getVersion()
501 {
502 if(this->e!=NULL)
503 {
504 return this->ehdr.e_version;
505 }
506 return -1;
507 }
508
509 qint64 ElfFile::getEntryPointAddress()
510 {
511 if(this->e!=NULL)
512 {
513 return this->ehdr.e_entry;
514 }
515 return -1;
516 }
517
518
519 int ElfFile::getSectionCount()
520 {
521 return (int)this->SectionCount;
522 }
523
524 int ElfFile::getSymbolCount()
525 {
526 return (int)this->SymbolCount;
527 }
528
529
530 int ElfFile::getSegmentCount()
531 {
532 return (int)this->SegmentCount;
533 }
534
535
536 QString ElfFile::getSegmentType(int index)
537 {
538 QString type("");
539 if(this->e!=NULL)
540 {
541 if(index < this->Segments.count())
542 {
543 switch(this->Segments.at(index)->p_type)
544 {
545 case PT_NULL:
546 type = "Program header table entry unused";
547 break;
548 case PT_LOAD:
549 type = "Loadable program segment";
550 break;
551 case PT_DYNAMIC :
552 type = "Dynamic linking information";
553 break;
554 case PT_INTERP:
555 type ="Program interpreter";
556 break;
557 case PT_NOTE:
558 type = "Auxiliary information";
559 break;
560 case PT_SHLIB:
561 type = "Reserved";
562 break;
563 case PT_PHDR:
564 type = "Entry for header table itself";
565 break;
566 case PT_TLS:
567 type = "Thread-local storage segment";
568 break;
569 case PT_NUM:
570 type = "Number of defined types";
571 break;
572 case PT_LOOS:
573 type = "Start of OS-specific";
574 break;
575 case PT_SUNWSTACK:
576 type = "Stack segment";
577 break;
578 case PT_LOPROC:
579 type = "Start of processor-specific";
580 break;
581 case PT_HIPROC:
582 type = "End of processor-specific";
583 break;
584 default:
585 type = "Unknow Section Type";
586 break;
587 }
588 }
589 }
590
591 return type;
592 }
593
594
595 qint64 ElfFile::getSegmentOffset(int index)
596 {
597 qint64 Offset = -1;
598 if(this->e!=NULL)
599 {
600 if(index < this->Segments.count())
601 {
602 Offset = (qint64)this->Segments.at(index)->p_offset;
603