-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathSymbolLoader.mm
More file actions
148 lines (121 loc) · 5.07 KB
/
Copy pathSymbolLoader.mm
File metadata and controls
148 lines (121 loc) · 5.07 KB
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
#include <Foundation/Foundation.h>
#include "SymbolLoader.h"
#include "Helpers.h"
#include <dlfcn.h>
namespace tns {
class SymbolResolver {
public:
virtual void* loadFunctionSymbol(const char* symbolName) = 0;
virtual void* loadDataSymbol(const char* symbolName) = 0;
virtual bool load() = 0;
virtual ~SymbolResolver() {}
};
class CFBundleSymbolResolver : public SymbolResolver {
public:
CFBundleSymbolResolver(CFBundleRef bundle) : _bundle(bundle) { }
virtual void* loadFunctionSymbol(const char* symbolName) override {
CFStringRef cfName = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, symbolName, kCFStringEncodingUTF8, kCFAllocatorNull);
void* functionPointer = CFBundleGetFunctionPointerForName(this->_bundle, cfName);
CFRelease(cfName);
return functionPointer;
}
virtual void* loadDataSymbol(const char* symbolName) override {
CFStringRef cfName = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, symbolName, kCFStringEncodingUTF8, kCFAllocatorNull);
void* dataPointer = CFBundleGetDataPointerForName(this->_bundle, cfName);
CFRelease(cfName);
return dataPointer;
}
virtual bool load() override {
CFErrorRef error = nullptr;
bool loaded = CFBundleLoadExecutableAndReturnError(this->_bundle, &error);
if (error) {
NSLog(@"%s", [[(NSError*)error localizedDescription] UTF8String]);
}
return loaded;
}
private:
CFBundleRef _bundle;
};
class DlSymbolResolver : public SymbolResolver {
public:
DlSymbolResolver(void* libraryHandle) : _libraryHandle(libraryHandle) { }
virtual void* loadFunctionSymbol(const char* symbolName) override {
return dlsym(this->_libraryHandle, symbolName);
}
virtual void* loadDataSymbol(const char* symbolName) override {
return dlsym(this->_libraryHandle, symbolName);
}
virtual bool load() override {
return !!this->_libraryHandle;
}
private:
void* _libraryHandle;
};
SymbolLoader& SymbolLoader::instance() {
static SymbolLoader loader;
return loader;
}
SymbolResolver* SymbolLoader::resolveModule(const ModuleMeta* module) {
if (!module) {
return nullptr;
}
auto it = this->_cache.find(module);
if (it != this->_cache.end()) {
return it->second.get();
}
std::unique_ptr<SymbolResolver> resolver;
if (module->isFramework()) {
NSString* frameworkPathStr = [NSString stringWithFormat:@"%s.framework", module->getName()];
NSURL* baseUrl = nil;
if (module->isSystem()) {
#if TARGET_IPHONE_SIMULATOR
NSBundle* foundation = [NSBundle bundleForClass:[NSString class]];
NSString* foundationPath = [foundation bundlePath];
NSString* basePathStr = [foundationPath substringToIndex:[foundationPath rangeOfString:@"Foundation.framework"].location];
baseUrl = [NSURL fileURLWithPath:basePathStr isDirectory:YES];
#else
baseUrl = [NSURL fileURLWithPath:@"/System/Library/Frameworks" isDirectory:YES];
#endif
} else {
baseUrl = [[NSBundle mainBundle] privateFrameworksURL];
}
NSURL* bundleUrl = [NSURL URLWithString:frameworkPathStr relativeToURL:baseUrl];
if (CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, (CFURLRef)bundleUrl)) {
resolver = std::make_unique<CFBundleSymbolResolver>(bundle);
} else {
NSLog(@"NativeScript could not load bundle %s\n", bundleUrl.absoluteString.UTF8String);
}
} else if (module->libraries->count == 1) {
if (module->isSystem()) {
// NSObject is in /usr/lib/libobjc.dylib, so we get that
NSString* libsPath = [[NSBundle bundleForClass:[NSObject class]] bundlePath];
NSString* libraryPath = [NSString stringWithFormat:@"%@/lib%s.dylib", libsPath, module->libraries->first()->value().getName()];
if (void* library = dlopen(libraryPath.UTF8String, RTLD_LAZY | RTLD_LOCAL)) {
NSLog(@"NativeScript loaded library %s\n", libraryPath.UTF8String);
resolver = std::make_unique<DlSymbolResolver>(library);
} else if (const char* libraryError = dlerror()) {
NSLog(@"NativeScript could not load library %s, error: %s\n", libraryPath.UTF8String, libraryError);
}
}
}
return this->_cache.emplace(module, std::move(resolver)).first->second.get();
}
void* SymbolLoader::loadFunctionSymbol(const ModuleMeta* module, const char* symbolName) {
if (auto resolver = this->resolveModule(module)) {
return resolver->loadFunctionSymbol(symbolName);
}
return dlsym(RTLD_DEFAULT, symbolName);
}
void* SymbolLoader::loadDataSymbol(const ModuleMeta* module, const char* symbolName) {
if (auto resolver = this->resolveModule(module)) {
return resolver->loadDataSymbol(symbolName);
}
return dlsym(RTLD_DEFAULT, symbolName);
}
bool SymbolLoader::ensureModule(const ModuleMeta* module) {
if (auto resolver = this->resolveModule(module)) {
return resolver->load();
}
return false;
}
}