-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathPosixFindSymbol.cpp
199 lines (176 loc) · 5.15 KB
/
PosixFindSymbol.cpp
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
/* Cydia Substrate - Powerful Code Insertion Platform
* Copyright (C) 2008-2011 Jay Freeman (saurik)
*/
/* GNU Lesser General Public License, Version 3 {{{ */
/*
* Substrate is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Substrate is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Substrate. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#include "CydiaSubstrate.h"
#include <dlfcn.h>
#include <link.h>
#include <assert.h>
#include <string.h>
#include <gelf.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string>
#include <set>
#include <map>
#include <iterator>
#include <vector>
#include <boost/algorithm/string/predicate.hpp>
void *mmap_file(std::string path, size_t *size) {
int fd;
struct stat st;
void *ptr;
fd = open(path.c_str(), O_RDONLY);
assert(fd >= 0);
assert(fstat(fd, &st) == 0);
ptr = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
assert(ptr);
close(fd);
if (size)
*size = st.st_size;
return ptr;
}
_extern MSImageRef MSGetImageByName(const char *file) {
if (!file)
return NULL;
void *handle = dlopen("libdl.so", RTLD_LAZY | RTLD_NOW);
assert(handle);
struct link_map *map;
int ret = dlinfo(handle, RTLD_DI_LINKMAP, &map);
dlclose(handle);
assert(!ret);
assert(map);
// back up to find the first real entry
while (map->l_prev && strlen(map->l_prev->l_name) > 0)
map = map->l_prev;
while (map) {
//if (!strcmp(map->l_name, file))
// return (MSImageRef)map;
if (boost::algorithm::ends_with(map->l_name, file))
return (MSImageRef)map;
map = map->l_next;
}
return NULL;
}
static void MSFindSymbolSub(MSImageRef image, std::set<std::string> &worklist, std::map<std::string, void *> &resolved_symbols) {
Elf *e;
Elf_Scn *scn;
size_t shstrtab_idx;
size_t strtab_idx = 0;
size_t symtab_idx = 0;
size_t dynstr_idx = 0;
size_t dynsym_idx = 0;
GElf_Shdr shdr;
size_t sz;
size_t i;
char *elf;
Elf_Data *data;
GElf_Sym sym;
struct link_map *map = (struct link_map *)image;
if (worklist.empty())
return;
assert(elf = (char *)mmap_file(map->l_name, &sz));
assert(elf_version(EV_CURRENT) != EV_NONE);
assert(e = elf_memory(elf, sz));
assert(!elf_getshdrstrndx(e, &shstrtab_idx));
i = 1;
scn = NULL;
while ((scn = elf_nextscn(e, scn)) != NULL) {
assert(gelf_getshdr(scn, &shdr));
std::string name {elf_strptr(e, shstrtab_idx, shdr.sh_name)};
if (name == ".symtab")
symtab_idx = i;
else if (name == ".strtab")
strtab_idx = i;
else if (name == ".dynstr")
dynstr_idx = i;
else if (name == ".dynsym")
dynsym_idx = i;
i++;
}
if (symtab_idx != 0) {
assert(scn = elf_getscn(e, symtab_idx));
assert(gelf_getshdr(scn, &shdr));
assert(data = elf_getdata(scn, NULL));
for (unsigned int i = 0; i < shdr.sh_size / shdr.sh_entsize; i++) {
assert(gelf_getsym(data, i, &sym));
std::string sym_name {elf_strptr(e, strtab_idx, sym.st_name)};
if (worklist.find(sym_name) != worklist.end()) {
resolved_symbols[sym_name] = (void *)(map->l_addr + sym.st_value);
worklist.erase(sym_name);
if (worklist.empty())
goto done;
}
}
}
if (dynsym_idx != 0) {
assert(scn = elf_getscn(e, dynsym_idx));
assert(gelf_getshdr(scn, &shdr));
assert(data = elf_getdata(scn, NULL));
for (unsigned int i = 0; i < shdr.sh_size / shdr.sh_entsize; i++) {
assert(gelf_getsym(data, i, &sym));
std::string sym_name {elf_strptr(e, dynstr_idx, sym.st_name)};
if (worklist.find(sym_name) != worklist.end()) {
resolved_symbols[sym_name] = (void *)(map->l_addr + sym.st_value);
worklist.erase(sym_name);
if (worklist.empty())
goto done;
}
}
}
done:
munmap((void *)elf, sz);
}
static void MSFindSymbols(MSImageRef image, size_t count, const char *names[], void *values[]) {
std::set<std::string> worklist {names, names + count};
std::map<std::string, void *> resolved_symbols;
if (image) {
MSFindSymbolSub(image, worklist, resolved_symbols);
} else {
void *handle = dlopen("libdl.so", RTLD_LAZY | RTLD_NOW);
assert(handle);
struct link_map *map;
int ret = dlinfo(handle, RTLD_DI_LINKMAP, &map);
dlclose(handle);
assert(!ret);
assert(map);
// back up to find the first real entry
while (map->l_prev && strlen(map->l_prev->l_name) > 0)
map = map->l_prev;
while (map && !worklist.empty()) {
MSFindSymbolSub((MSImageRef)map, worklist, resolved_symbols);
map = map->l_next;
}
}
for (size_t i = 0; i < count; i++) {
try {
void *res = resolved_symbols.at(names[i]);
values[i] = res;
} catch (std::out_of_range) {
values[i] = NULL;
}
}
}
_extern void *MSFindSymbol(MSImageRef image, const char *name) {
void *value;
MSFindSymbols(image, 1, &name, &value);
return value;
}