forked from smx-smx/ezinject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelfparse.c
142 lines (129 loc) · 3.6 KB
/
elfparse.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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <elf.h>
#include <link.h>
#include "util.h"
struct elfparse_info
{
char *path;
int fd;
void *mapping;
size_t len;
ElfW(Ehdr) *ehdr;
char *strtab;
ElfW(Sym) *symtab;
int symtab_entries;
char *dynstr;
ElfW(Sym) *dynsym;
int dynsym_entries;
};
static void elfparse_parse(struct elfparse_info *hndl);
void *elfparse_createhandle(const char *procpath)
{
struct elfparse_info *hndl = malloc(sizeof(struct elfparse_info));
if(!hndl) return 0;
memset(hndl, 0, sizeof(struct elfparse_info));
hndl->path = strdup(procpath);
if(!hndl->path)
goto free_mem;
hndl->fd = open(hndl->path, O_RDONLY);
if(hndl->fd < 0)
goto free_path;
hndl->len = lseek(hndl->fd, 0, SEEK_END);
lseek(hndl->fd, 0, SEEK_SET);
hndl->mapping = mmap(0, hndl->len, PROT_READ, MAP_SHARED, hndl->fd, 0);
if(hndl->mapping == MAP_FAILED)
goto free_path;
elfparse_parse(hndl);
return hndl;
free_path:
free(hndl->path);
free_mem:
free(hndl);
return 0;
}
static void elfparse_parse(struct elfparse_info *hndl)
{
ElfW(Ehdr) *ehdr = hndl->mapping;
hndl->ehdr = ehdr;
DBG("e_ident=%s", ehdr->e_ident);
DBG("e_phoff=%zu", ehdr->e_phoff);
DBG("e_shoff=%zu", ehdr->e_shoff);
DBG("e_shentsize=%u", ehdr->e_shentsize);
DBG("e_shnum=%u", ehdr->e_shnum);
ElfW(Shdr) *shdr = hndl->mapping + ehdr->e_shoff;
char *strtab = hndl->mapping + shdr[ehdr->e_shstrndx].sh_offset;
for(int i = 0; i < ehdr->e_shnum; ++i)
{
ElfW(Shdr) *cur_shdr = &shdr[i];
char *name = &strtab[cur_shdr->sh_name];
if(!strcmp(name, ".symtab"))
{
hndl->symtab = hndl->mapping + cur_shdr->sh_offset;
hndl->symtab_entries = cur_shdr->sh_size / cur_shdr->sh_entsize;
DBG("Found symbol table (%u entries): %p", hndl->symtab_entries, hndl->symtab);
}
else if(!strcmp(name, ".strtab"))
{
hndl->strtab = hndl->mapping + cur_shdr->sh_offset;
DBG("Found string table: %p", hndl->strtab);
}
else if(!strcmp(name, ".dynsym"))
{
hndl->dynsym = hndl->mapping + cur_shdr->sh_offset;
hndl->dynsym_entries = cur_shdr->sh_size / cur_shdr->sh_entsize;
DBG("Found dynsym (%u entries): %p", hndl->dynsym_entries, hndl->dynsym);
}
else if(!strcmp(name, ".dynstr"))
{
hndl->dynstr = hndl->mapping + cur_shdr->sh_offset;
DBG("Found dynstr: %p", hndl->dynstr);
}
}
}
bool elfparse_needs_reloc(void *handle)
{
struct elfparse_info *hndl = (struct elfparse_info *)handle;
return hndl->ehdr->e_type != ET_EXEC;
}
static char *elfparse_findfunction(char *strtab, ElfW(Sym) *symtab, int symtab_entries, const char *funcname)
{
for(int i = 0; i < symtab_entries; ++i)
{
char *curname = &strtab[symtab[i].st_name];
if(!strcmp(curname, funcname))
return (char *)symtab[i].st_value;
}
return 0;
}
char *elfparse_getfuncaddr(void *handle, const char *funcname)
{
struct elfparse_info *hndl = (struct elfparse_info*)handle;
char *fn = elfparse_findfunction(hndl->strtab, hndl->symtab, hndl->symtab_entries, funcname);
if(fn)
goto ret;
DBG("Function %s not found in symtab, trying dynsym", funcname);
fn = elfparse_findfunction(hndl->dynstr, hndl->dynsym, hndl->dynsym_entries, funcname);
if(fn)
goto ret;
WARN("Function %s not found in symtab or dynsym", funcname);
return 0;
ret:
if(hndl->ehdr->e_machine == EM_ARM) /* apply fix for Thumb functions */
fn = (char *)((uintptr_t)fn & ~1);
return fn;
}
void elfparse_destroyhandle(void *handle)
{
struct elfparse_info *hndl = (struct elfparse_info*)handle;
free(hndl->path);
munmap(hndl->mapping, hndl->len);
close(hndl->fd);
free(hndl);
}