-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_8_coff_relocations.c
64 lines (61 loc) · 1.86 KB
/
example_8_coff_relocations.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
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define EPEP_INST
#include "epep.h"
#define ERROR(epep) (printf("Error #%u from EPEP at " __FILE__ ": %u", epep.error_code, __LINE__), 1)
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage:\n%s <filename>\n", argv[0]);
return 0;
}
Epep epep = { 0 };
FILE *fp = fopen(argv[1], "rb");
if (!fp) {
printf("File not found: %s\n", argv[1]);
return 1;
}
if (!epep_init(&epep, fp)) {
printf("Not PE");
return 1;
}
// Get string table useful to show long names of sections
size_t string_table_size = 1;
if (epep.kind == EPEP_OBJECT && !epep_get_string_table_size(&epep, &string_table_size)) {
return ERROR(epep);
}
char *string_table = malloc(string_table_size);
if (epep.kind == EPEP_OBJECT && !epep_get_string_table(&epep, string_table)) {
return ERROR(epep);
}
if (epep.kind == EPEP_OBJECT) {
for (size_t i = 0; i < epep.coffFileHeader.NumberOfSections; i++) {
EpepSectionHeader sh = { 0 };
if (!epep_get_section_header_by_index(&epep, &sh, i)) {
return ERROR(epep);
}
printf(" Relocations for section #%u", i);
if (epep.kind == EPEP_OBJECT && sh.Name[0] == '/') {
printf(" (%s)\n", &string_table[atoi(sh.Name + 1)]);
} else {
printf(" (%.*s)\n", 8, sh.Name);
}
size_t number_of_relocations;
int extended;
if (!epep_get_section_number_of_relocations_x(&epep, &sh, &number_of_relocations, &extended)) {
return ERROR(epep);
}
for (size_t i = 0; i < number_of_relocations; i++) {
EpepCoffRelocation rel = { 0 };
if (!epep_get_section_relocation_by_index_x(&epep, &sh, &rel, i, extended)) {
return ERROR(epep);
}
printf(" COFF Relocation #%u\n", i);
printf(" VirtualAddress: %08x\n", rel.VirtualAddress);
printf(" SymbolTableIndex: %08x\n", rel.SymbolTableIndex);
printf(" Type: %04x\n", rel.Type);
}
}
}
return 0;
}