-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_2_section_headers.c
59 lines (54 loc) · 1.9 KB
/
example_2_section_headers.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
#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);
}
printf("Section Table:\n");
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(" Section #%u\n", i);
if (epep.kind == EPEP_OBJECT && sh.Name[0] == '/') {
printf(" Name: %s\n", &string_table[atoi(sh.Name + 1)]);
} else {
printf(" Name: %.*s\n", 8, sh.Name);
}
printf(" VirtualSize: %08x\n", sh.VirtualSize);
printf(" VirtualAddress: %08x\n", sh.VirtualAddress);
printf(" SizeOfRawData: %08x\n", sh.SizeOfRawData);
printf(" PointerToRawData: %08x\n", sh.PointerToRawData);
printf(" PointerToRelocations: %08x\n", sh.PointerToRelocations);
printf(" PointerToLinenumbers: %08x\n", sh.PointerToLinenumbers);
printf(" NumberOfRelocations: %08x\n", sh.NumberOfRelocations);
printf(" NumberOfLinenumbers: %08x\n", sh.NumberOfLinenumbers);
printf(" Characteristics: %08x\n", sh.Characteristics);
}
return 0;
}