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