-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadElf.c
More file actions
69 lines (62 loc) · 1.54 KB
/
readElf.c
File metadata and controls
69 lines (62 loc) · 1.54 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
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <elf.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "programheader.h"
#include "fileHeader.h"
#include "sectionheader.h"
int main(int argc, char **argv)
{
int fd, opt;
uint8_t *mem; // memory for executable
struct stat st; // get file status
if (argc < 3) {
printf("Usage: %s [-h -l -S -s] <executable>\n", argv[0]);
exit(0);
}
if ((fd = open(argv[2], O_RDONLY)) < 0) {
perror("open");
exit(-1);
}
if (fstat(fd, &st) < 0) {
perror("fstat");
exit(-1);
}
mem = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mem == MAP_FAILED) {
perror("mmap");
exit(-1);
}
if (mem[0] != 0x7f && strcmp(&mem[1], "ELF")) {
fprintf(stderr, "%s is not an ELF file\n", argv[1]);
exit(-1);
}
while ((opt = getopt(argc, argv, "hlSs")) != -1) {
switch (opt) {
case 'h':
printf("You selected h!\n");
dispFileHdr(mem);
break;
case 'l':
printf("You selected l!\n");
parseL(mem);
break;
case 'S':
printf("You selected S!\n");
parseSection(mem);
break;
case 's':
printf("You selected s!\n!");
break;
default:
printf("Unknown option selected!\n");
exit(0);
}
}
}