-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelf_section_table.c
More file actions
226 lines (200 loc) · 5.92 KB
/
elf_section_table.c
File metadata and controls
226 lines (200 loc) · 5.92 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "elf_section_table.h"
#include "elf_io.h"
/* READ SECTION HEADER */
Elf32_Shdr read_section_header(FILE *f){
Elf32_Shdr line;
fread_32bits(&(line.sh_name), 1, f);
fread_32bits(&(line.sh_type), 1, f);
fread_32bits(&(line.sh_flags), 1, f);
fread_32bits(&(line.sh_addr), 1, f);
fread_32bits(&(line.sh_offset), 1, f);
fread_32bits(&(line.sh_size), 1, f);
fread_32bits(&(line.sh_link), 1, f);
fread_32bits(&(line.sh_info), 1, f);
fread_32bits(&(line.sh_addralign), 1, f);
fread_32bits(&(line.sh_entsize), 1, f);
return line;
}
void init_section_table(Elf32_Shdr e_table[], Elf32_Half e_shnum){
for(int i=0; i<e_shnum; i++){
e_table[i].sh_name = SHN_UNDEF;
e_table[i].sh_type = SHT_NULL;
e_table[i].sh_flags = 0;
e_table[i].sh_addr = 0;
e_table[i].sh_offset = 0;
e_table[i].sh_size = 0;
e_table[i].sh_link = 0;
e_table[i].sh_info = 0;
e_table[i].sh_addralign = 0;
e_table[i].sh_entsize = 0;
}
}
void read_elf_section_table(FILE *f, Elf32_Ehdr *header, Elf32_Shdr e_table[]){
unsigned int i;
for (i = 0; i<header->e_shnum; i++){
fseek(f, header->e_shoff + i * header->e_shentsize, SEEK_SET);
e_table[i] = read_section_header(f);
}
}
/* READ SECTION HEADER */
void write_section_header(FILE *f, Elf32_Shdr line){
fwrite_32bits(&(line.sh_name), 1, f);
fwrite_32bits(&(line.sh_type), 1, f);
fwrite_32bits(&(line.sh_flags), 1, f);
fwrite_32bits(&(line.sh_addr), 1, f);
fwrite_32bits(&(line.sh_offset), 1, f);
fwrite_32bits(&(line.sh_size), 1, f);
fwrite_32bits(&(line.sh_link), 1, f);
fwrite_32bits(&(line.sh_info), 1, f);
fwrite_32bits(&(line.sh_addralign), 1, f);
fwrite_32bits(&(line.sh_entsize), 1, f);
}
void write_elf_section_table(FILE *f, Elf32_Ehdr header, Elf32_Shdr e_table[]){
unsigned int i;
for (i = 0; i<header.e_shnum; i++){
fseek(f, header.e_shoff + i * header.e_shentsize, SEEK_SET);
write_section_header(f, e_table[i]);
}
}
/* DISPLAY SECTION HEADER */
const char * section_type(Elf32_Word sh_type){
switch (sh_type) {
case SHT_NULL:
return "NULL";
case SHT_PROGBITS:
return "PROGBITS";
case SHT_SYMTAB :
return "SYMTAB ";
case SHT_STRTAB:
return "STRTAB";
case SHT_RELA:
return "RELA";
case SHT_HASH :
return "HASH";
case SHT_DYNAMIC:
return "DYNAMIC";
case SHT_NOTE:
return "NOTE";
case SHT_NOBITS:
return "NOBITS";
case SHT_REL:
return "REL";
case SHT_SHLIB:
return "SHLIB";
case SHT_DYNSYM:
return "DYNSYM";
case SHT_ARM_EXIDX:
return "ARM_EXIDX";
case SHT_ARM_PREEMPTMAP:
return "ARM_PREEMPTMAP";
case SHT_ARM_ATTRIBUTES:
return "ARM_ATTRIBUTES";
case SHT_ARM_DEBUGOVERLAY:
return "ARM_DEBUGOVERLAY";
case SHT_ARM_OVERLAYSECTION:
return "ARM_OVERLAYSECTION";
case SHT_LOPROC:
return "LOPROC";
case SHT_HIPROC:
return "HIPROC";
case SHT_LOUSER:
return "LOUSER";
case SHT_HIUSER:
return "HIUSER";
default:
break;
}
return "erreurType";
}
inline const unsigned char * section_name(unsigned char* str_table, Elf32_Word sh_name){
return str_table + sh_name;
}
char * section_flags(Elf32_Word sh_flags){
char * tab = malloc(15); //14 flags maximum plus le \0 final
unsigned int indice = 0;
if ((sh_flags & SHF_WRITE) != 0){
tab[indice] = 'W';
indice++;
}
if ((sh_flags & SHF_ALLOC) != 0){
tab[indice] = 'A';
indice ++;
}
if ((sh_flags & SHF_EXECINSTR) != 0){
tab[indice] = 'X';
indice ++;
}
if ((sh_flags & SHF_MERGE) != 0){
tab[indice] = 'M';
indice ++;
}
if ((sh_flags & SHF_STRINGS) != 0){
tab[indice] = 'S';
indice ++;
}
if ((sh_flags & SHF_INFO_LINK) != 0){
tab[indice] = 'I';
indice ++;
}
if ((sh_flags & SHF_LINK_ORDER) != 0){
tab[indice] = 'L';
indice ++;
}
if ((sh_flags & SHF_OS_NONCONFORMING) != 0){
tab[indice] = 'N';
indice ++;
}
if ((sh_flags & SHF_GROUP) != 0){
tab[indice] = 'G';
indice ++;
}
if ((sh_flags & SHF_TLS) != 0){
tab[indice] = 'T';
indice ++;
}
if ((sh_flags & SHF_MASKOS) != 0){
tab[indice] = 'o';
indice ++;
}
if ((sh_flags & SHF_MASKPROC) != 0){
tab[indice] = 'p';
indice ++;
}
if ((sh_flags & SHF_ORDERED) != 0){
tab[indice] = 'O';
indice ++;
}
if ((sh_flags & SHF_EXCLUDE) != 0){
tab[indice] = 'E';
indice ++;
}
tab[indice] = '\0';
return tab;
}
void display_section_table(Elf32_Ehdr *header, Elf32_Shdr e_table[], unsigned char *str_table){
printf("Il y a %d en-têtes de section,\
débutant à l'adresse de décalage 0x%x:\n\n", header->e_shnum, header->e_shoff);
puts("En-têtes de section :");
printf("[%2s] %-17.17s %-16s %-8s %-6s %-6s %-2s %5s %-2s %-3s %-2s\n",
"Nr","Nom","Type","Adr","Décala.","Taille","ES","Fan","LN","Inf","Al");
Elf32_Half i;
for (i=0; i<header->e_shnum; i++){
const unsigned char *nom = str_table + e_table[i].sh_name;
const char *type = section_type(e_table[i].sh_type);
const Elf32_Addr addr = e_table[i].sh_addr;
const Elf32_Word offset = e_table[i].sh_offset;
const Elf32_Word size = e_table[i].sh_size;
const Elf32_Word es = e_table[i].sh_entsize;
char *flags = section_flags(e_table[i].sh_flags);
const Elf32_Word ln = e_table[i].sh_link;
const Elf32_Word inf = e_table[i].sh_info;
const Elf32_Word al = e_table[i].sh_addralign;
printf("[%2d] %-17.17s %-16s %08x %06x %06x %02x %5s %2d %3d %2d\n",
i, nom, type, addr, offset, size, es, flags, ln, inf, al);
free(flags);
}
puts("Clé des fanions: \n \
W (écriture), A (allocation), X (exécution), M (fusion), S (chaînes) \n \
I (info), L (ordre des liens), G (groupe), T (TLS), E (exclu), x (inconnu) \n \
O (traiterment additionnel requis pour l'OS) o (spécifique à l'OS), p (spécifique au processeur)");
}