// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-#include <string.h> /* for strerror */
+#include <limits.h>
#include <pthread.h>
#include <signal.h>
-#include <limits.h>
+#include <string.h> /* for strerror */
+#include <sys/param.h>
+#include <unistd.h>
+
+#include <CoreFoundation/CFBundle.h>
+#include <CoreFoundation/CFString.h>
+
#include "libcgo.h"
#define magic (0xe696c4f4U)
return nil;
}
+// init_working_dir sets the current working directory to the app root.
+// By default darwin/arm processes start in "/".
+static void
+init_working_dir()
+{
+ CFBundleRef bundle = CFBundleGetMainBundle();
+ if (bundle == NULL) {
+ fprintf(stderr, "runtime/cgo: no main bundle\n");
+ return;
+ }
+ CFURLRef url_ref = CFBundleCopyResourceURL(bundle, CFSTR("Info"), CFSTR("plist"), NULL);
+ if (url_ref == NULL) {
+ fprintf(stderr, "runtime/cgo: no Info.plist URL\n");
+ return;
+ }
+ CFStringRef url_str_ref = CFURLGetString(url_ref);
+ char url[MAXPATHLEN];
+ if (!CFStringGetCString(url_str_ref, url, sizeof(url), kCFStringEncodingUTF8)) {
+ fprintf(stderr, "runtime/cgo: cannot get URL string\n");
+ return;
+ }
+
+ // url is of the form "file:///path/to/Info.plist".
+ // strip it down to the working directory "/path/to".
+ int url_len = strlen(url);
+ if (url_len < sizeof("file://")+sizeof("/Info.plist")) {
+ fprintf(stderr, "runtime/cgo: bad URL: %s\n", url);
+ return;
+ }
+ url[url_len-sizeof("/Info.plist")+1] = 0;
+ char *dir = &url[0] + sizeof("file://")-1;
+
+ if (chdir(dir) != 0) {
+ fprintf(stderr, "runtime/cgo: chdir(%s) failed\n", dir);
+ }
+}
+
void
x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
{
// yes, tlsbase from mrc might not be correctly aligned.
inittls(tlsg, (void**)((uintptr)tlsbase & ~3));
+
+ init_working_dir();
}