-
Notifications
You must be signed in to change notification settings - Fork 18
/
elfparse.c
204 lines (187 loc) · 5.4 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
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
/*
* Copyright (C) 2021 Stefano Moioli <smxdev4@gmail.com>
* This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#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 "config.h"
#include "ezinject_util.h"
#if defined(EZ_TARGET_LINUX)
#define ElfAddr ElfW(Addr)
#define ElfAddr ElfW(Addr)
#define ElfEhdr ElfW(Ehdr)
#define ElfEhdr ElfW(Ehdr)
#define ElfOff ElfW(Off)
#define ElfOff ElfW(Off)
#define ElfPhdr ElfW(Phdr)
#define ElfPhdr ElfW(Phdr)
#define ElfShdr ElfW(Shdr)
#define ElfShdr ElfW(Shdr)
#define ElfSym ElfW(Sym)
#define ElfSym ElfW(Sym)
#elif defined(EZ_TARGET_FREEBSD)
#define ElfAddr Elf_Addr
#define ElfEhdr Elf_Ehdr
#define ElfOff Elf_Off
#define ElfPhdr Elf_Phdr
#define ElfShdr Elf_Shdr
#define ElfSym Elf_Sym
#endif
struct elfparse_info
{
char *path;
int fd;
void *mapping;
size_t len;
ElfEhdr *ehdr;
ElfShdr *sec;
char *strtab;
ElfSym *symtab;
int symtab_entries;
char *dynstr;
ElfSym *dynsym;
int dynsym_entries;
};
static void elfparse_parse(struct elfparse_info *hndl);
void *elfparse_createhandle(const char *procpath) {
struct elfparse_info *hndl = NULL;
int rc = -1;
do {
hndl = calloc(1, sizeof(struct elfparse_info));
if(hndl == NULL){
break;
}
hndl->path = strdup(procpath);
if(hndl->path == NULL){
break;
}
hndl->fd = open(hndl->path, O_RDONLY);
if(hndl->fd < 0){
break;
}
struct stat statBuf;
if(fstat(hndl->fd, &statBuf) < 0){
break;
}
hndl->len = statBuf.st_size;
hndl->mapping = mmap(0, hndl->len, PROT_READ, MAP_SHARED, hndl->fd, 0);
if(hndl->mapping == MAP_FAILED){
break;
}
elfparse_parse(hndl);
rc = 0;
} while(0);
if(rc != 0){
if(hndl->path != NULL){
free(hndl->path);
}
if(hndl != NULL){
free(hndl);
hndl = NULL;
}
}
return hndl;
}
static void elfparse_parse(struct elfparse_info *hndl) {
ElfEhdr *ehdr = hndl->mapping;
hndl->ehdr = ehdr;
ElfShdr *sec = (ElfShdr *)((uint8_t *)ehdr + ehdr->e_shoff);
hndl->sec = sec;
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);
ElfShdr *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)
{
ElfShdr *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 uint8_t *elfparse_findfunction(
struct elfparse_info *hndl,
char *strtab, ElfSym *symtab,
int symtab_entries,
const char *funcname
){
for(int i = 0; i < symtab_entries; ++i) {
ElfSym *sym = &symtab[i];
char *curname = &strtab[sym->st_name];
if(!strcmp(curname, funcname)){
uint8_t *offset = (uint8_t *)(
hndl->sec[sym->st_shndx].sh_offset
+ sym->st_value - hndl->sec[sym->st_shndx].sh_addr
);
return offset;
}
}
return 0;
}
void *elfparse_getfuncaddr(void *handle, const char *funcname)
{
struct elfparse_info *hndl = (struct elfparse_info*)handle;
uint8_t *fn = elfparse_findfunction(hndl, hndl->strtab, hndl->symtab, hndl->symtab_entries, funcname);
if(fn){
return fn;
}
DBG("Function %s not found in symtab, trying dynsym", funcname);
fn = elfparse_findfunction(hndl, hndl->dynstr, hndl->dynsym, hndl->dynsym_entries, funcname);
if(fn){
return fn;
}
WARN("Function %s not found in symtab or dynsym", funcname);
return 0;
#if 0
if(hndl->ehdr->e_machine == EM_ARM) /* apply fix for Thumb functions */
fn = (uint8_t *)((uintptr_t)fn & ~1);
#endif
}
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);
}