-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxcode.m
More file actions
executable file
·176 lines (135 loc) · 6.61 KB
/
Copy pathxcode.m
File metadata and controls
executable file
·176 lines (135 loc) · 6.61 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
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
/*
MIT License
Copyright (c) 2017 Appdome ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "xcode.h"
#include <objc/runtime.h>
#include <dlfcn.h>
/* This file is responsible to dynamically link against Xcode executables while
still allowing to use the class name directly (i.e. without objc_getClass,
etc), as long as that class appears in classes.x. This file also initializes
Xcode's libraries. */
/* Define false classes so we can link statically */
#define X(class, weak) char OBJC_CLASS_$_##class;
#include "classes.x"
#undef X
extern Class classrefs[] __asm("section$start$__DATA$__objc_classrefs");
extern Class classrefs_end __asm("section$end$__DATA$__objc_classrefs");
extern void IDEInitialize(int flags, id *arg1);
extern void IDESetSafeToLoadMobileDevice(void);
id (*AFCConnectionCreate)(int unknown, int socket, int unknown2, int unknown3, void *context);
int (*AMDServiceConnectionGetSocket)(ServiceConnectionRef connection);
void *(*AMDServiceConnectionGetSecureIOContext)(id service);
void (*AFCConnectionSetSecureContext)(id connection, void *context);
int (*AFCRemovePath)(id connection, const char *path);
int (*AFCDirectoryOpen)(id connection, const char *path, id *dir);
int (*AFCDirectoryRead)(id connection, id dir, char **dirent);
int (*AFCDirectoryClose)(id connection, id dir);
int (*AMDeviceSecureStartService)(id device, CFStringRef service_name, NSDictionary *options, ServiceConnectionRef result);
int (*AMDeviceStartSession)(id device);
int (*AMDeviceConnect)(id device);
int (*AMDServiceConnectionReceive)(ServiceConnectionRef con, void * data, size_t size);
int (*AMDServiceConnectionSend)(ServiceConnectionRef con, const void * data, size_t size);
__attribute__((constructor)) static void link_and_init_xcode(void)
{
/* Allow configuring a specific path to Xcode (for use with betas, etc) */
NSString *xcode_path = @"/Applications/Xcode.app";
const char *xcode_path_env = getenv("XCODE_PATH");
if (xcode_path_env) {
xcode_path = @(xcode_path_env);
}
#define XCODE_REL(x) [[xcode_path stringByAppendingString:(@x)] UTF8String]
/* Create symlinks to Xcode's framework to fix @rpath issues */
unlink("/tmp/.xcodelib1");
symlink(XCODE_REL("/Contents/Frameworks"), "/tmp/.xcodelib1");
unlink("/tmp/.xcodelib2");
symlink(XCODE_REL("/Contents/SharedFrameworks"), "/tmp/.xcodelib2");
unlink("/tmp/.xcodelib3");
symlink(XCODE_REL("/Contents/PlugIns"), "/tmp/.xcodelib3");
/* dlopen whatever we need */
/* Disable logs (Many irrelevant assertions are printed) */
int errfd = dup(STDERR_FILENO);
close(STDERR_FILENO);
int devnull = open("/dev/null", O_WRONLY);
if (devnull != STDERR_FILENO) {
dup2(devnull, STDERR_FILENO);
close(devnull);
}
if (!dlopen(XCODE_REL("/Contents/Frameworks/IDEFoundation.framework/Versions/A/IDEFoundation"), RTLD_LAZY) ||
!dlopen(XCODE_REL("/Contents/SharedFrameworks/DVTFoundation.framework/Versions/A/DVTFoundation"), RTLD_LAZY)) {
/* Enable logs again. */
dup2(errfd, STDERR_FILENO);
close(errfd);
fputs(dlerror(), stderr);
fputs("\n", stderr);
exit(1);
}
/* initialize "Xcode" */
typeof(IDEInitialize) *_IDEInitialize
= dlsym(RTLD_DEFAULT, "IDEInitialize");
assert(_IDEInitialize);
typeof(IDESetSafeToLoadMobileDevice) *_IDESetSafeToLoadMobileDevice
= dlsym(RTLD_DEFAULT, "IDESetSafeToLoadMobileDevice");
assert(_IDESetSafeToLoadMobileDevice);
id what = nil;
_IDEInitialize(7, &what);
_IDESetSafeToLoadMobileDevice();
/* Enable logs again. */
dup2(errfd, STDERR_FILENO);
close(errfd);
/* Dlsym C functions we need */
AFCConnectionCreate = dlsym(RTLD_DEFAULT, "AFCConnectionCreate");
assert(AFCConnectionCreate);
AMDServiceConnectionGetSocket = dlsym(RTLD_DEFAULT, "AMDServiceConnectionGetSocket");
assert(AMDServiceConnectionGetSocket);
AMDServiceConnectionGetSecureIOContext = dlsym(RTLD_DEFAULT, "AMDServiceConnectionGetSecureIOContext");
assert(AMDServiceConnectionGetSecureIOContext);
AFCConnectionSetSecureContext = dlsym(RTLD_DEFAULT, "AFCConnectionSetSecureContext");
assert(AFCConnectionSetSecureContext);
AFCRemovePath = dlsym(RTLD_DEFAULT, "AFCRemovePath");
assert(AFCRemovePath);
AFCDirectoryOpen = dlsym(RTLD_DEFAULT, "AFCDirectoryOpen");
assert(AFCDirectoryOpen);
AFCDirectoryRead = dlsym(RTLD_DEFAULT, "AFCDirectoryRead");
assert(AFCDirectoryRead);
AFCDirectoryClose = dlsym(RTLD_DEFAULT, "AFCDirectoryClose");
assert(AFCDirectoryClose);
AMDeviceSecureStartService = dlsym(RTLD_DEFAULT, "AMDeviceSecureStartService");
assert(AMDeviceSecureStartService);
AMDeviceStartSession = dlsym(RTLD_DEFAULT, "AMDeviceStartSession");
assert(AMDeviceStartSession);
AMDeviceConnect = dlsym(RTLD_DEFAULT, "AMDeviceConnect");
assert(AMDeviceConnect);
AMDServiceConnectionReceive = dlsym(RTLD_DEFAULT, "AMDServiceConnectionReceive");
assert(AMDServiceConnectionReceive);
AMDServiceConnectionSend = dlsym(RTLD_DEFAULT, "AMDServiceConnectionSend");
assert(AMDServiceConnectionSend);
/* After loading, check for any false class we declared to fix its
reference using objc_getClass. */
Class *classref = classrefs;
while (classref != &classrefs_end) {
#define X(class, weak) \
if (*classref == (Class) &OBJC_CLASS_$_##class) { \
*classref = objc_getClass(#class);\
assert((#class && *classref) || weak); \
}
#include "classes.x"
#undef X
classref++;
}
}