-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreadelf.h
119 lines (88 loc) · 2.56 KB
/
readelf.h
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
#pragma once
#include <elf.h>
#include <stdint.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
struct elf {
uint8_t *file;
Elf64_Ehdr ehdr;
Elf64_Phdr *phdrs;
int n_phdrs;
Elf64_Shdr *shdrs;
int n_shdrs;
char *shdr_names;
Elf64_Sym *syms;
int n_syms;
char *sym_names;
};
char *get_shdr_name(struct elf *e, int i) {
return &e->shdr_names[e->shdrs[i].sh_name];
}
void *get_sym_addr(struct elf *e, int i) {
return (void *) e->syms[i].st_value;
}
char *get_sym_name(struct elf *e, int i) {
return &e->sym_names[e->syms[i].st_name];
}
size_t get_sym_size(struct elf *e, int i) {
return e->syms[i].st_size;
}
int get_sym_index(struct elf *e, char *name) {
int i;
for (i = 0; i < e->n_syms; i++) {
if (!strcmp(name, get_sym_name(e, i))) return i;
}
return -1;
}
int at_symbol(struct elf *e, void *addr) {
int i;
for (i = 0; i < e->n_syms; i++) {
if (get_sym_addr(e, i) == addr) return i;
}
return -1;
}
Elf64_Shdr *get_shdr(struct elf *e, char *name) {
int i;
for (i = 0; i < e->n_shdrs; i++) {
if (!strcmp(name, get_shdr_name(e, i))) return &e->shdrs[i];
}
return NULL;
}
bool addr_in_section(struct elf *e, void *addr, char *name) {
Elf64_Shdr *shdr;
uint64_t sec_start, sec_end;
shdr = get_shdr(e, name);
sec_start = shdr->sh_addr;
sec_end = sec_start + shdr->sh_size;
return (uint64_t) addr >= sec_start && (uint64_t) addr < sec_end;
}
bool sym_in_section(struct elf *e, int i, char *name) {
return addr_in_section(e, get_sym_addr(e, i), name);
}
uint8_t *bytes_from_addr_in_section(struct elf *e, void *addr, char *name) {
Elf64_Shdr *shdr;
shdr = get_shdr(e, name);
return &e->file[(uint64_t) addr - (uint64_t) shdr->sh_addr + shdr->sh_offset];
}
struct elf *readelf(int fd) {
struct stat st;
struct elf *e;
Elf64_Shdr *sym_hdr;
e = malloc(sizeof(*e));
fstat(fd, &st);
e->file = malloc(st.st_size);
read(fd, e->file, st.st_size);
e->ehdr = *(Elf64_Ehdr *) e->file;
e->phdrs = (Elf64_Phdr *) &e->file[e->ehdr.e_phoff];
e->n_phdrs = e->ehdr.e_phnum;
e->shdrs = (Elf64_Shdr *) &e->file[e->ehdr.e_shoff];
e->n_shdrs = e->ehdr.e_shnum;
e->shdr_names = &e->file[e->shdrs[e->ehdr.e_shstrndx].sh_offset];
sym_hdr = get_shdr(e, ".symtab");
e->syms = (Elf64_Sym *) &e->file[sym_hdr->sh_offset];
e->n_syms = sym_hdr->sh_size / sym_hdr->sh_entsize;
e->sym_names = &e->file[e->shdrs[sym_hdr->sh_link].sh_offset];
return e;
}