-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelfio_bin.cpp
executable file
·98 lines (83 loc) · 2.1 KB
/
elfio_bin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<elfio/elfio.hpp>
#include<cassert>
#include<cstdio>
using namespace ELFIO;
using namespace std;
#define REP(i,a,b) \
for(int i = a; i <= b; i++)
const char* DATAFILE = "data.txt";
const char* INSTFILE = "inst.txt";
const char* INFOFILE = "info.txt";
int main(int argc, char** argv )
{
elfio reader;
FILE *data_file,*inst_file,*info_file;
data_file = fopen(DATAFILE,"w");
inst_file = fopen(INSTFILE,"w");
info_file = fopen(INFOFILE,"w");
if (argc != 2)
{
cout<<"give me a file name !!\n";
return 1;
}
if( !reader.load( argv[1] ))
{
cout<<"error processing file-"<<argv[1]<<"\n";
return 2;
}
/*
cout<<"ELF File Class:\n";
if( reader.get_class() == ELFCLASS32)
cout<<"ELF 32\n";
else
cout<<"ELF 64\n";
*/
fprintf(info_file,"0x%lx\n",reader.get_entry());
// cout<<"ELF Encoding:\n";
if( reader.get_encoding() == ELFDATA2LSB)
fprintf(info_file,"L\n");
else
fprintf(info_file,"B\n");
//ELF section info
Elf_Half sec_num = reader.sections.size();
REP(i,0,sec_num-1)
{
section* psec = reader.sections[i];
const char* p = psec->get_data();
if ( psec->get_type() == SHT_SYMTAB )
{
const symbol_section_accessor symbols( reader, psec );
for ( unsigned int j = 0; j < symbols.get_symbols_num(); ++j ) {
std::string
name;
Elf64_Addr
value;
Elf_Xword
size;
unsigned char bind;
unsigned char type;
Elf_Half
section_index;
unsigned char other;
symbols.get_symbol( j, name, value, size, bind,type, section_index, other );
if(name != "" && name!= "$x")
fprintf(data_file,"%lu\n",value);
}
}
if( p != NULL && psec->get_name() == ".text")
{
int l=0;
while(l < psec->get_size()) { fprintf(inst_file,"%.2x",(unsigned)(unsigned char)p[l]); l++;} //note- cout<<hex could also be used
}
/* else if( p != NULL && psec->get_name() == ".data")
{
int l=0;
while(l < psec->get_size()) { fprintf(data_file,"%.2x",(unsigned)(unsigned char)p[l]); l++;} //note- cout<<hex could also be used
}*/
}
fclose(data_file);
fclose(inst_file);
fclose(info_file);
return 0;
}