-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodule.cpp
46 lines (41 loc) · 1.26 KB
/
module.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
//
// libembryo
//
// Copyright Stanley Cen 2013
// Released under the MIT License
//
#include "module.hpp"
std::string getFileNameFromPath(const std::string& path)
{
int pos = (int)path.length();
while (pos && path[pos] != '/') --pos;
return path.substr(pos + 1);
}
namespace embryo {
module::module(const std::string& name) : m_start(0), m_size(0)
{
bool found = false;
unsigned int imageCount = _dyld_image_count();
for (int i = 0; i < imageCount; i++)
{
mach_header *header = (mach_header *)_dyld_get_image_header(i);
const char *imageName = _dyld_get_image_name(i);
if (!imageName) continue;
std::string shortName = getFileNameFromPath(std::string(imageName));
if (shortName != name) continue;
struct stat sb;
if (stat(imageName, &sb))
{
break;
}
found = true;
m_start = (void *)header;
m_size = (unsigned int)sb.st_size;
m_header = header;
m_name = shortName;
m_path = std::string(imageName);
m_handle = dlopen(imageName, RTLD_GLOBAL | RTLD_LAZY);
break;
}
}
}