-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.c
395 lines (281 loc) · 11.4 KB
/
parse.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "parse.h"
#include <stdio.h>
#include <assert.h>
void free_all(ElfData *data){
/*
* shstrtab contains details of section header string table. memeber
* strtab_content contains char * pointing to heap where name of every
* section is stored
*/
if(data->shdrtab.shstrtab.strtab_buffer != NULL)
free(data->shdrtab.shstrtab.strtab_buffer);
/* cleaning array of character pointers which points to section header string table */
if(data->shdrtab.shstrtab.strtab_content != NULL)
free(data->shdrtab.shstrtab.strtab_content);
/* cleaning memory where section header table is stored */
if(data->shdrtab.shdr_table != NULL)
free(data->shdrtab.shdr_table);
/* cleaning memort where program header table is stored */
if(data->phdrtab.phdr_table != NULL)
free(data->phdrtab.phdr_table);
free_note(&data->shdrtab.note_abi_tag);
free_note(&data->shdrtab.note_gnu_build_id);
free_note(&data->shdrtab.note_gnu_property);
free_symtab(&data->shdrtab.symtab);
free_symtab(&data->shdrtab.dynsym);
free_strtab(&data->shdrtab.strtab);
free_strtab(&data->shdrtab.dynstr);
free_dynamic(&data->shdrtab.dynamic);
free_relocation(&data->shdrtab.rela_debug_arranges);
free_relocation(&data->shdrtab.rela_debug_line);
free_relocation(&data->shdrtab.rela_rodata);
free_relocation(&data->shdrtab.rela_text);
free_relocation(&data->shdrtab.rela_debug_info);
fclose(data->fh);
}
int get_no_of_strings(char *buffer, int size){
assert(buffer != NULL);
int no_of_strings = 0;
for(int i = 0; i < size; i++){
if(buffer[i] == '\0' || buffer[i] == '\n'){
no_of_strings++;
}
}
return no_of_strings;
}
int get_section_index(struct shdr_table *shdr, const char *section_name){
assert(shdr != NULL);
assert(section_name != NULL);
int i;
for (i = 0; i < shdr->shstrtab.strtab_entries; i++){
if(shdr->shdr_table[i].sh_name < shdr->shstrtab.strtab_size){
char *buf = &shdr->shstrtab.strtab_buffer[shdr->shdr_table[i].sh_name];
if((buf))
if(strcmp(buf, section_name) == 0){
return i;
}
}
}
return NOT_FOUND;
}
char **get_strings(struct shdr_table *shdr, int entries){
assert(shdr != NULL);
char **parsed_string = malloc(sizeof(char *) * entries);
if(!parsed_string){
perror("memory allocation failed");
return NULL;
}
memset(parsed_string, 0, sizeof(char *) * entries);
for (int i = 0; i < entries; i++){
char *buf = &shdr->shstrtab.strtab_buffer[shdr->shdr_table[i].sh_name];
parsed_string[i] = buf;
}
return parsed_string;
}
uint32_t get_section_link(struct shdr_table *shdr, const char *sectionname){
assert(shdr != NULL);
assert(sectionname != NULL);
int index = get_section_index(shdr, sectionname);
if(index == NOT_FOUND) return NOT_FOUND;
return shdr->shdr_table[index].sh_link;
}
uint64_t get_section_address_align(struct shdr_table *shdr, const char *sectionname){
assert(shdr != NULL);
assert(sectionname != NULL);
int index = get_section_index(shdr, sectionname);
if(index == NOT_FOUND) return NOT_FOUND;
return shdr->shdr_table[index].sh_addralign;
}
uint32_t get_section_index_to_shstr(struct shdr_table *shdr, const char *sectionname){
assert(shdr != NULL);
assert(sectionname != NULL);
int index = get_section_index(shdr, sectionname);
if(index == NOT_FOUND) return NOT_FOUND;
return shdr->shdr_table[index].sh_name;
}
uint32_t get_section_type(struct shdr_table *shdr, const char *sectionname){
assert(shdr != NULL);
assert(sectionname != NULL);
int index = get_section_index(shdr, sectionname);
if(index == NOT_FOUND) return NOT_FOUND;
return shdr->shdr_table[index].sh_type;
}
uint32_t get_section_info(struct shdr_table *shdr, const char *sectionname){
assert(shdr != NULL);
assert(sectionname != NULL);
int index = get_section_index(shdr, sectionname);
if(index == NOT_FOUND) return NOT_FOUND;
return shdr->shdr_table[index].sh_info;
}
uint64_t get_section_flags(struct shdr_table *shdr, const char *sectionname){
assert(shdr != NULL);
assert(sectionname != NULL);
int index = get_section_index(shdr, sectionname);
if(index == NOT_FOUND) return NOT_FOUND;
return shdr->shdr_table[index].sh_flags;
}
Elf64_Addr get_section_address(struct shdr_table *shdr, const char *sectionname){
assert(shdr != NULL);
assert(sectionname != NULL);
int index = get_section_index(shdr, sectionname);
if(index == NOT_FOUND) return NOT_FOUND;
return shdr->shdr_table[index].sh_addr;
}
off_t get_section_offset(struct shdr_table *shdr, const char *sectionname){
assert(shdr != NULL);
assert(sectionname != NULL);
int index = get_section_index(shdr, sectionname);
if(index == NOT_FOUND) return NOT_FOUND;
return shdr->shdr_table[index].sh_offset;
}
uint64_t get_section_entrysize(struct shdr_table *shdr, const char *sectionname){
assert(shdr != NULL);
assert(sectionname != NULL);
int index = get_section_index(shdr, sectionname);
if(index == NOT_FOUND) return NOT_FOUND;
return shdr->shdr_table[index].sh_entsize;
}
uint64_t get_section_size(struct shdr_table *shdr, const char *sectionname){
assert(shdr != NULL);
assert(sectionname != NULL);
int index = get_section_index(shdr, sectionname);
if(index == NOT_FOUND) return NOT_FOUND;
return shdr->shdr_table[index].sh_size;
}
/*
* this function uses shdr table to parse section names. it stores parsed strings in shstrtab
* structure's strtab_content memeber. this memeber can be used when printing section names and so on
* get_section_index function uses values assigned by this function to track sections.
*/
int parse_shstrtab(ElfData *data){ //should have used struct shdr_table *
/*get the section header table index to get the string table. ehdr.e_shstrndx containts the index of shstrtab table*/
assert(data != NULL);
data->shdrtab.shstrtab.strtab_index = data->elf_header.ehdr.e_shstrndx;
/*use section header index to access access corresponding section;'s shdr structure and use sh_offset to get the offset of the string table */
data->shdrtab.shstrtab.strtab_offset = data->shdrtab.shdr_table[data->shdrtab.shstrtab.strtab_index].sh_offset;
/* seek to that offset */
fseek(data->fh, data->shdrtab.shstrtab.strtab_offset, SEEK_SET);
/* set size of the string table using sh_size */
data->shdrtab.shstrtab.strtab_size = data->shdrtab.shdr_table[data->shdrtab.shstrtab.strtab_index].sh_size;
/* allocate a buffer with size sh_size, then read shstrtab into that buffer */
data->shdrtab.shstrtab.strtab_buffer = malloc(data->shdrtab.shstrtab.strtab_size * sizeof(char)); //do not free this, used to get section names..
if(!data->shdrtab.shstrtab.strtab_buffer){
perror("memory allocation failed");
return -1;
}
memset(data->shdrtab.shstrtab.strtab_buffer, 0, data->shdrtab.shstrtab.strtab_size * sizeof(char));
if(fread(data->shdrtab.shstrtab.strtab_buffer, 1, data->shdrtab.shstrtab.strtab_size, data->fh) < (unsigned long)data->shdrtab.shstrtab.strtab_size){
perror("failed read file");
return -1;
}
/*
* after reading, we need tp get the exact number of strings in that table. this is also possible using something like
* e_shnum, which gives us number of sections. number of sections == no of strings in shstrtab
*/
#ifdef ___STRENT
data->shdrtab.shstrtab.strtab_entries = data->shdrtab.shdr_entries;
#else
data->shdrtab.shstrtab.strtab_entries = get_no_of_strings(data->shdrtab.shstrtab.strtab_buffer, data->shdrtab.shstrtab.strtab_size);
#endif
/* read to (char *) buffer , sh_size of bytes from current position (section .strtab offset)) */
data->shdrtab.shstrtab.strtab_content = get_strings(&data->shdrtab, data->shdrtab.shstrtab.strtab_entries);
return 0;
}
int check_elf(FILE *fh){
assert(fh != NULL);
uint8_t buf[5];
fseek(fh, 0, SEEK_SET);
if(fread(buf, sizeof(uint8_t), 5, fh) < 4){
perror("failed to read file");
return -1;
};
if((buf[0] == 0x7f) && (buf[1] == 'E') && (buf[2] == 'L') && (buf[3] == 'F'))
return 0;
else{
fprintf(stderr, "not an elf binary\n");
return -1;
}
return 0;
}
void init_ptr(struct shdr_table *shdr){
assert(shdr != NULL);
/* initialize all not-yet-used pointers to null */
shdr->shstrtab.strtab_buffer = NULL;
shdr->shstrtab.strtab_content = NULL;
shdr->dynstr.strtab_buffer = NULL;
shdr->dynstr.strtab_content = NULL;
shdr->strtab.strtab_buffer = NULL;
shdr->strtab.strtab_content = NULL;
shdr->dynsym.symtab = NULL;
shdr->symtab.symtab = NULL;
shdr->note_abi_tag.notetab = NULL;
shdr->note_gnu_build_id.notetab = NULL;
shdr->note_gnu_property.notetab = NULL;
shdr->dynamic.dyn = NULL;
shdr->rela_debug_line.relocation = NULL;
shdr->rela_debug_info.relocation = NULL;
shdr->rela_debug_arranges.relocation = NULL;
shdr->rela_rodata.relocation = NULL;
shdr->rela_text.relocation = NULL;
shdr->comment.buffer = NULL;
}
/*
* this function reads from the elf file and assign ehdr header and shdr, phdr tables
*/
int assign_headers(ElfData *data){
assert(data != NULL);
if(check_elf(data->fh) == -1) return -1;;
fseek(data->fh, 0, SEEK_SET);
/* assigning ehdr with data read from the binary */
data->elf_header.ehdr_size = sizeof(Elf64_Ehdr);
if(fread(&data->elf_header.ehdr, data->elf_header.ehdr_size, 1, data->fh) < 1){
perror("failed to read file: ");
return -1;
}
/* assigning phdr with data read from binary using ehdr->e_phoff */
if((data->elf_header.ehdr.e_type == ET_EXEC) || (data->elf_header.ehdr.e_type == ET_DYN)){
data->phdrtab.phdr_entries = data->elf_header.ehdr.e_phnum;
data->phdrtab.phdr_size = data->phdrtab.phdr_entries * sizeof(Elf64_Phdr);
data->phdrtab.phdr_offset = data->elf_header.ehdr.e_phoff;
data->phdrtab.phdr_table = malloc(data->phdrtab.phdr_size);
if(!data->phdrtab.phdr_table) {
perror("memory allocation failed: ");
return -1;
}
memset(data->phdrtab.phdr_table, 0, data->phdrtab.phdr_size);
fseek(data->fh, data->phdrtab.phdr_offset, SEEK_SET); //seeking file pointer to position where program header table begins
if(fread(data->phdrtab.phdr_table, sizeof(Elf64_Phdr), data->phdrtab.phdr_entries, data->fh) < (unsigned long)data->phdrtab.phdr_entries){ //reading from that location to allocated space in heap
perror("failed to read data\n");
return -1;
}
}
else{
fprintf(stderr, "binary file is not an executable file\n");
}
/* assigning shdr with data read from binary using ehdr->e_shoff */
data->shdrtab.shdr_entries = data->elf_header.ehdr.e_shnum;
data->shdrtab.shdr_size = data->shdrtab.shdr_entries * sizeof(Elf64_Shdr);
data->shdrtab.shdr_offset = data->elf_header.ehdr.e_shoff;
data->shdrtab.shdr_table = malloc(data->shdrtab.shdr_size); //do not free this memory. used througout the code
if(!data->shdrtab.shdr_table) {
perror("memory allocation failed: ");
return -1;
}
memset(data->shdrtab.shdr_table, 0, data->shdrtab.shdr_size);
fseek(data->fh, data->shdrtab.shdr_offset, SEEK_SET);
if(fread(data->shdrtab.shdr_table, sizeof(Elf64_Shdr), data->shdrtab.shdr_entries, data->fh) < (unsigned long)data->shdrtab.shdr_entries){
perror("could not read section header table");
return -1;
}
init_ptr(&data->shdrtab);
return 0;
}
FILE *open_file(const char *filename){
assert(filename != NULL);
FILE *local = fopen(filename, "rb");
if(!local){
perror("coud not open binary file");
return NULL;
}
return local;
}