Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(588)

Unified Diff: third_party/crashpad/crashpad/compat/mac/mach-o/getsect.cc

Issue 1505213004: Copy Crashpad into the Chrome tree instead of importing it via DEPS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments, update README.chromium Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/crashpad/crashpad/compat/mac/mach-o/getsect.cc
diff --git a/third_party/crashpad/crashpad/compat/mac/mach-o/getsect.cc b/third_party/crashpad/crashpad/compat/mac/mach-o/getsect.cc
new file mode 100644
index 0000000000000000000000000000000000000000..71d3066c677aa79cbe05edf3a2a08a16d4bf0ddd
--- /dev/null
+++ b/third_party/crashpad/crashpad/compat/mac/mach-o/getsect.cc
@@ -0,0 +1,107 @@
+// Copyright 2014 The Crashpad Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <mach-o/getsect.h>
+
+// This is only necessary when building code that might run on systems earlier
+// than 10.7. When building for 10.7 or later, getsectiondata() and
+// getsegmentdata() are always present in libmacho and made available through
+// libSystem. When building for earlier systems, custom definitions of
+// these functions are needed.
+//
+// This file checks the deployment target instead of the SDK. The deployment
+// target is correct because it identifies the earliest possible system that
+// the code being compiled is expected to run on.
+
+#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
+
+#include <dlfcn.h>
+#include <stddef.h>
+
+#include "third_party/apple_cctools/cctools/include/mach-o/getsect.h"
+
+namespace {
+
+// Returns a dlopen() handle to the same library that provides the
+// getsectbyname() function. getsectbyname() is always present in libmacho.
+// getsectiondata() and getsegmentdata() are not always present, but when they
+// are, they’re in the same library as getsectbyname(). If the library cannot
+// be found or a handle to it cannot be returned, returns nullptr.
+void* SystemLibMachOHandle() {
+ Dl_info info;
+ if (!dladdr(reinterpret_cast<void*>(getsectbyname), &info)) {
+ return nullptr;
+ }
+ return dlopen(info.dli_fname, RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
+}
+
+// Returns a function pointer to a function in libmacho based on a lookup of
+// that function by symbol name. Returns nullptr if libmacho cannot be found or
+// opened, or if the named symbol cannot be found in libmacho.
+void* LookUpSystemLibMachOSymbol(const char* symbol) {
+ static void* dl_handle = SystemLibMachOHandle();
+ if (!dl_handle) {
+ return nullptr;
+ }
+ return dlsym(dl_handle, symbol);
+}
+
+#ifndef __LP64__
+using MachHeader = mach_header;
+#else
+using MachHeader = mach_header_64;
+#endif
+
+using GetSectionDataType =
+ uint8_t*(*)(const MachHeader*, const char*, const char*, unsigned long*);
+using GetSegmentDataType =
+ uint8_t*(*)(const MachHeader*, const char*, unsigned long*);
+
+} // namespace
+
+extern "C" {
+
+// These implementations look up their functions in libmacho at run time. If
+// the system libmacho provides these functions as it normally does on Mac OS X
+// 10.7 and later, the system’s versions are used directly. Otherwise, the
+// versions in third_party/apple_cctools are used, which are actually just
+// copies of the system’s functions.
+
+uint8_t* getsectiondata(const MachHeader* mhp,
+ const char* segname,
+ const char* sectname,
+ unsigned long* size) {
+ static GetSectionDataType system_getsectiondata =
+ reinterpret_cast<GetSectionDataType>(
+ LookUpSystemLibMachOSymbol("getsectiondata"));
+ if (system_getsectiondata) {
+ return system_getsectiondata(mhp, segname, sectname, size);
+ }
+ return crashpad_getsectiondata(mhp, segname, sectname, size);
+}
+
+uint8_t* getsegmentdata(
+ const MachHeader* mhp, const char* segname, unsigned long* size) {
+ static GetSegmentDataType system_getsegmentdata =
+ reinterpret_cast<GetSegmentDataType>(
+ LookUpSystemLibMachOSymbol("getsegmentdata"));
+ if (system_getsegmentdata) {
+ return system_getsegmentdata(mhp, segname, size);
+ }
+ return crashpad_getsegmentdata(mhp, segname, size);
+}
+
+} // extern "C"
+
+#endif // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7

Powered by Google App Engine
This is Rietveld 408576698