-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
266 lines (212 loc) · 5.37 KB
/
misc.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
#include <errno.h>
#include <stdlib.h>
#include "misc.h"
#ifdef __FreeBSD__
#define PTRACE_PEEKDATA PT_READ_D
#endif
/* read data from location addr */
int
memread(pid_t pid,
unsigned long addr,
void *outp,
size_t len)
{
int i, count;
long word;
unsigned long *ptr = outp;
int ret;
count = i = 0;
while (count < len) {
/* Since -1 is a valid value for 'word', to detect
* an error only have to rely on errno
*/
errno = 0;
word = ptrace(PTRACE_PEEKDATA, pid, addr + count, NULL);
if (errno) {
ret = -1;
goto end;
}
count += sizeof word;
ptr[i++] = word;
}
ret = 0;
end:
return ret;
}
int
elf_getshstrtab(telf_ctx *ctx,
char **strtabp)
{
Elf64_Shdr shdr;
int rc;
size_t to_read;
unsigned long addr;
char *strtab = NULL;
to_read = sizeof shdr;
addr = (unsigned long) (uintptr_t) ctx->addr + ctx->ehdr->e_shstrndx;
rc = memread(ctx->pid, addr, (void *) &shdr, to_read);
if (-1 == rc)
goto err;
to_read = shdr.sh_size;
addr = (unsigned long) (uintptr_t) ctx->addr + shdr.sh_offset;
strtab = malloc(to_read);
if (! strtab)
goto err;
rc = memread(ctx->pid, addr, (void *) &strtab, to_read);
if (-1 == rc)
goto err;
if (strtabp)
*strtabp = strtab;
return 0;
err:
if (strtab)
free(strtab);
return -1;
}
Elf64_Shdr *
elf_getnsection(telf_ctx *ctx,
int n)
{
if (n < 0 || n >= ctx->n_sections)
return NULL;
return ctx->shdr + n;
}
char *
elf_getsectionname(telf_ctx *ctx,
Elf64_Shdr *shdr)
{
Elf64_Shdr *sh_strtab = ctx->shdr + ctx->ehdr->e_shstrndx;
char *sh_strtab_p = ctx->addr + sh_strtab->sh_offset;
return sh_strtab_p + shdr->sh_name;
}
char *
elf_getnsectionname(telf_ctx *ctx,
int n)
{
if (n < 0 || n >= ctx->n_sections)
return NULL;
Elf64_Shdr *sh_strtab = ctx->shdr + ctx->ehdr->e_shstrndx;
char *sh_strtab_p = ctx->addr + sh_strtab->sh_offset;
return sh_strtab_p + ctx->shdr[n].sh_name;
}
Elf64_Shdr *
elf_getsectionbyname(telf_ctx *ctx,
char *name)
{
int i;
for (i = 0; i < ctx->n_sections; i++) {
Elf64_Shdr *shdr = ctx->shdr + i;
char *i_name = elf_getsectionname(ctx, shdr);
if (0 == strcmp(i_name, name))
return shdr;
}
return NULL;
}
Elf64_Shdr *
elf_getsectionbytype(telf_ctx *ctx,
unsigned int type)
{
Elf64_Shdr *shdr = NULL;
int i;
int found = 0;
for (i = 0; i < ctx->ehdr->e_shnum && 0 == found; i++) {
shdr = elf_getnsection(ctx, i);
if (type == shdr->sh_type)
found = 1;
}
if (found)
return shdr;
else
return NULL;
}
char *
elf_getsymname(telf_ctx *ctx,
Elf64_Sym *sym)
{
return &ctx->strtab[sym->st_name];
}
char *
elf_getdsymname(telf_ctx *ctx,
Elf64_Sym *sym)
{
return &ctx->dstrtab[sym->st_name];
}
Elf64_Sym *
elf_getnsym(telf_ctx *ctx,
int n)
{
if (n < 0 || n >= ctx->n_syms)
return NULL;
return ctx->symtab + n;
}
Elf64_Sym *
elf_getndsym(telf_ctx *ctx,
int n)
{
if (n < 0 || n >= ctx->n_dsyms)
return NULL;
return ctx->dsymtab + n;
}
Elf64_Sym *
elf_getsymbyname(telf_ctx *ctx,
char *name)
{
int i;
Elf64_Sym *sym = NULL;
for (i = 0; i < ctx->n_syms; i++) {
sym = elf_getnsym(ctx, i);
if (0 == strcmp(name, elf_getsymname(ctx, sym)))
goto end;
}
sym = NULL;
end:
return sym;
}
Elf64_Sym *
elf_getdsymbyname(telf_ctx *ctx,
char *name)
{
int i;
for (i = 0; i < ctx->n_dsyms; i++) {
Elf64_Sym *sym = elf_getndsym(ctx, i);
if (0 == strcmp(name, elf_getdsymname(ctx, sym)))
return sym;
}
return NULL;
}
char *
sym_bind_to_str(Elf64_Sym *sym)
{
if (! sym)
return "unknown";
unsigned char b = ELF64_ST_BIND(sym->st_info);
switch (b) {
#define MAP(x) case x: return #x
MAP(STB_LOCAL);
MAP(STB_GLOBAL);
MAP(STB_WEAK);
MAP(STB_LOPROC);
MAP(STB_HIPROC);
#undef MAP
}
return "impossible";
}
char *
sym_type_to_str(Elf64_Sym *sym)
{
if (! sym)
return "unknown";
unsigned char t = ELF64_ST_TYPE(sym->st_info);
switch (t) {
#define MAP(x) case x: return #x
MAP(STT_NOTYPE);
MAP(STT_OBJECT);
MAP(STT_FUNC);
MAP(STT_SECTION);
MAP(STT_FILE);
MAP(STT_LOPROC);
MAP(STT_HIPROC);
#undef MAP
}
return "impossible";
}