OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include <mach-o/getsect.h> |
| 16 |
| 17 // This is only necessary when building code that might run on systems earlier |
| 18 // than 10.7. When building for 10.7 or later, getsectiondata() and |
| 19 // getsegmentdata() are always present in libmacho and made available through |
| 20 // libSystem. When building for earlier systems, custom definitions of |
| 21 // these functions are needed. |
| 22 // |
| 23 // This file checks the deployment target instead of the SDK. The deployment |
| 24 // target is correct because it identifies the earliest possible system that |
| 25 // the code being compiled is expected to run on. |
| 26 |
| 27 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7 |
| 28 |
| 29 #include <dlfcn.h> |
| 30 #include <stddef.h> |
| 31 |
| 32 #include "third_party/apple_cctools/cctools/include/mach-o/getsect.h" |
| 33 |
| 34 namespace { |
| 35 |
| 36 // Returns a dlopen() handle to the same library that provides the |
| 37 // getsectbyname() function. getsectbyname() is always present in libmacho. |
| 38 // getsectiondata() and getsegmentdata() are not always present, but when they |
| 39 // are, they’re in the same library as getsectbyname(). If the library cannot |
| 40 // be found or a handle to it cannot be returned, returns NULL. |
| 41 void* SystemLibMachOHandle() { |
| 42 Dl_info info; |
| 43 if (!dladdr(reinterpret_cast<void*>(getsectbyname), &info)) { |
| 44 return NULL; |
| 45 } |
| 46 return dlopen(info.dli_fname, RTLD_LAZY | RTLD_LOCAL); |
| 47 } |
| 48 |
| 49 // Returns a function pointer to a function in libmacho based on a lookup of |
| 50 // that function by symbol name. Returns NULL if libmacho cannot be found or |
| 51 // opened, or if the named symbol cannot be found in libmacho. |
| 52 void* LookUpSystemLibMachOSymbol(const char* symbol) { |
| 53 static void* dl_handle = SystemLibMachOHandle(); |
| 54 if (!dl_handle) { |
| 55 return NULL; |
| 56 } |
| 57 return dlsym(dl_handle, symbol); |
| 58 } |
| 59 |
| 60 #ifndef __LP64__ |
| 61 typedef mach_header MachHeader; |
| 62 #else |
| 63 typedef mach_header_64 MachHeader; |
| 64 #endif |
| 65 |
| 66 typedef uint8_t*(*GetSectionDataType)( |
| 67 const MachHeader*, const char*, const char*, unsigned long*); |
| 68 typedef uint8_t*(*GetSegmentDataType)( |
| 69 const MachHeader*, const char*, unsigned long*); |
| 70 |
| 71 } // namespace |
| 72 |
| 73 extern "C" { |
| 74 |
| 75 // These implementations look up their functions in libmacho at run time. If |
| 76 // the system libmacho provides these functions as it normally does on Mac OS X |
| 77 // 10.7 and later, the system’s versions are used directly. Otherwise, the |
| 78 // versions in third_party/apple_cctools are used, which are actually just |
| 79 // copies of the system’s functions. |
| 80 |
| 81 uint8_t* getsectiondata(const MachHeader* mhp, |
| 82 const char* segname, |
| 83 const char* sectname, |
| 84 unsigned long* size) { |
| 85 static GetSectionDataType system_getsectiondata = |
| 86 reinterpret_cast<GetSectionDataType>( |
| 87 LookUpSystemLibMachOSymbol("getsectiondata")); |
| 88 if (system_getsectiondata) { |
| 89 return system_getsectiondata(mhp, segname, sectname, size); |
| 90 } |
| 91 return crashpad_getsectiondata(mhp, segname, sectname, size); |
| 92 } |
| 93 |
| 94 uint8_t* getsegmentdata( |
| 95 const MachHeader* mhp, const char* segname, unsigned long* size) { |
| 96 static GetSegmentDataType system_getsegmentdata = |
| 97 reinterpret_cast<GetSegmentDataType>( |
| 98 LookUpSystemLibMachOSymbol("getsegmentdata")); |
| 99 if (system_getsegmentdata) { |
| 100 return system_getsegmentdata(mhp, segname, size); |
| 101 } |
| 102 return crashpad_getsegmentdata(mhp, segname, size); |
| 103 } |
| 104 |
| 105 } // extern "C" |
| 106 |
| 107 #endif // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7 |
OLD | NEW |