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