Text file
src/runtime/cgo/gcc_ios_arm64.c
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include <limits.h>
6 #include <string.h> /* for strerror */
7 #include <sys/param.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10
11 #include "libcgo.h"
12
13 #include <TargetConditionals.h>
14
15 #if TARGET_OS_IPHONE
16 #include <CoreFoundation/CFBundle.h>
17 #include <CoreFoundation/CFString.h>
18 #endif
19
20 #if TARGET_OS_IPHONE
21
22 static void
23 threadentry_platform(void)
24 {
25 #if TARGET_OS_IPHONE
26 darwin_arm_init_thread_exception_port();
27 #endif
28 }
29
30 // init_working_dir sets the current working directory to the app root.
31 // By default ios/arm64 processes start in "/".
32 static void
33 init_working_dir()
34 {
35 CFBundleRef bundle;
36 CFURLRef url_ref;
37 CFStringRef url_str_ref;
38 char buf[MAXPATHLEN];
39 Boolean res;
40 int url_len;
41 char *dir;
42 CFStringRef wd_ref;
43
44 bundle = CFBundleGetMainBundle();
45 if (bundle == NULL) {
46 fprintf(stderr, "runtime/cgo: no main bundle\n");
47 return;
48 }
49 url_ref = CFBundleCopyResourceURL(bundle, CFSTR("Info"), CFSTR("plist"), NULL);
50 if (url_ref == NULL) {
51 // No Info.plist found. It can happen on Corellium virtual devices.
52 return;
53 }
54 url_str_ref = CFURLGetString(url_ref);
55 res = CFStringGetCString(url_str_ref, buf, sizeof(buf), kCFStringEncodingUTF8);
56 CFRelease(url_ref);
57 if (!res) {
58 fprintf(stderr, "runtime/cgo: cannot get URL string\n");
59 return;
60 }
61
62 // url is of the form "file:///path/to/Info.plist".
63 // strip it down to the working directory "/path/to".
64 url_len = strlen(buf);
65 if (url_len < sizeof("file://")+sizeof("/Info.plist")) {
66 fprintf(stderr, "runtime/cgo: bad URL: %s\n", buf);
67 return;
68 }
69 buf[url_len-sizeof("/Info.plist")+1] = 0;
70 dir = &buf[0] + sizeof("file://")-1;
71
72 if (chdir(dir) != 0) {
73 fprintf(stderr, "runtime/cgo: chdir(%s) failed\n", dir);
74 }
75
76 // The test harness in go_ios_exec passes the relative working directory
77 // in the GoExecWrapperWorkingDirectory property of the app bundle.
78 wd_ref = CFBundleGetValueForInfoDictionaryKey(bundle, CFSTR("GoExecWrapperWorkingDirectory"));
79 if (wd_ref != NULL) {
80 if (!CFStringGetCString(wd_ref, buf, sizeof(buf), kCFStringEncodingUTF8)) {
81 fprintf(stderr, "runtime/cgo: cannot get GoExecWrapperWorkingDirectory string\n");
82 return;
83 }
84 if (chdir(buf) != 0) {
85 fprintf(stderr, "runtime/cgo: chdir(%s) failed\n", buf);
86 }
87 }
88 }
89
90 #endif // TARGET_OS_IPHONE
91
92 static void
93 init_platform()
94 {
95 #if TARGET_OS_IPHONE
96 darwin_arm_init_mach_exception_handler();
97 darwin_arm_init_thread_exception_port();
98 init_working_dir();
99 #endif
100 }
101
102 void (*x_cgo_init_platform)(void) = init_platform;
103 void (*x_cgo_threadentry_platform)(void) = threadentry_platform;
104
View as plain text