Index: chrome/app/chrome_exe_main_mac.c |
diff --git a/chrome/app/chrome_exe_main_mac.c b/chrome/app/chrome_exe_main_mac.c |
new file mode 100644 |
index 0000000000000000000000000000000000000000..93eee9f1ba2abb49f592b7e4deade4c19af79234 |
--- /dev/null |
+++ b/chrome/app/chrome_exe_main_mac.c |
@@ -0,0 +1,95 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// The entry point for all Mac Chromium processes, including the outer app |
+// bundle (browser) and helper app (renderer, plugin, and friends). |
+ |
+#include <dlfcn.h> |
+#include <mach-o/dyld.h> |
+#include <stdio.h> |
+#include <stdlib.h> |
+#include <string.h> |
+#include <unistd.h> |
+ |
+#include "chrome/common/chrome_version.h" |
+ |
+typedef int (*ChromeMainPtr)(int, char**); |
+ |
+__attribute__((visibility("default"))) int main(int argc, char* argv[]) { |
+#if defined(HELPER_EXECUTABLE) |
+ const char* const rel_path = |
+ "../../../" PRODUCT_FULLNAME_STRING |
+ " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework"; |
+#else |
+ const char* const rel_path = |
+ "../Versions/" CHROME_VERSION_STRING "/" PRODUCT_FULLNAME_STRING |
+ " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework"; |
+#endif // defined(HELPER_EXECUTABLE) |
+ |
+ uint32_t exec_path_size = 0; |
+ int rc = _NSGetExecutablePath(NULL, &exec_path_size); |
+ if (rc != -1) { |
+ fprintf(stderr, |
+ "Unexpected return condition from _NSGetExecutablePath().\n"); |
+ abort(); |
+ } |
+ |
+ char* exec_path = malloc(exec_path_size); |
Mark Mentovai
2015/10/22 23:15:11
fprintf(stderr, "malloc %zu: %s\n", framework_path
Greg K
2015/10/23 19:12:49
Done.
|
+ if (!exec_path) { |
+ fprintf(stderr, "Failed to allocate memory.\n"); |
+ abort(); |
+ } |
+ |
+ rc = _NSGetExecutablePath(exec_path, &exec_path_size); |
+ if (rc != 0) { |
+ fprintf(stderr, |
+ "_NSGetExecutablePath() call failed, " |
+ "cannot calculate framework path.\n"); |
+ abort(); |
+ } |
+ |
+ // Slice off the last part of the main executable path, and append the |
+ // version framework information. |
+ char* last_slash = strrchr(exec_path, '/'); |
Mark Mentovai
2015/10/22 23:15:11
Hmm, this will be messed-up with valid but stupid
Greg K
2015/10/23 19:12:48
Done.
|
+ if (last_slash) { |
+ *(last_slash + 1) = '\0'; |
+ } else { |
+ fprintf(stderr, "Unexpected executable path.\n"); |
+ abort(); |
+ } |
+ |
+ size_t exec_path_len = strlen(exec_path); |
+ size_t rel_path_len = strlen(rel_path); |
+ size_t framework_path_size = exec_path_len + rel_path_len + 1; |
+ char* framework_path = malloc(framework_path_size); |
+ if (!framework_path) { |
+ fprintf(stderr, "Failed to allocate memory.\n"); |
+ abort(); |
+ } |
+ |
+ memcpy(framework_path, exec_path, exec_path_len); |
+ memcpy(framework_path + exec_path_len, rel_path, rel_path_len); |
+ framework_path[exec_path_len + rel_path_len] = '\0'; |
Mark Mentovai
2015/10/22 23:15:12
snprintf(framework_path, framework_path_size, "%s/
Greg K
2015/10/23 19:12:49
Done.
|
+ |
+ void* library = dlopen(framework_path, RTLD_LAZY); |
Mark Mentovai
2015/10/22 23:15:11
| RTLD_LOCAL | RTLD_FIRST
Greg K
2015/10/23 19:12:48
Done.
|
+ if (!library) { |
+ fprintf(stderr, "Unable to dlopen() the framework.\n"); |
Mark Mentovai
2015/10/22 23:15:11
Say the framework name dlerror(). Something like
Greg K
2015/10/23 19:12:48
Done.
|
+ abort(); |
+ } |
Mark Mentovai
2015/10/22 23:15:11
free() framework_path now.
Greg K
2015/10/23 19:12:48
Done.
|
+ |
+ ChromeMainPtr ChromeMain = dlsym(library, "ChromeMain"); |
Mark Mentovai
2015/10/22 23:15:11
It’s still a variable, call it chrome_main
Greg K
2015/10/23 19:12:48
Done.
|
+ if (!ChromeMain) { |
+ fprintf(stderr, "Could not resolve the ChromeMain symbol.\n"); |
Mark Mentovai
2015/10/22 23:15:11
Similarly, fprintf(stderr, "dlopen ChromeMain: %s\
Greg K
2015/10/23 19:12:49
Done.
|
+ abort(); |
+ } |
+ int rv = ChromeMain(argc, argv); |
+ |
+ // Free all resources here. |
Mark Mentovai
2015/10/22 23:15:11
Technically the library handle is a freeable resou
Greg K
2015/10/23 19:12:48
Done.
|
+ free(exec_path); |
+ free(framework_path); |
+ |
+ // exit, don't return from main, to avoid the apparent removal of main from |
+ // stack backtraces under tail call optimization. |
+ exit(rv); |
+} |