| 7 | #include <dlfcn.h> |
| 8 | #include <mach-o/getsect.h> |
| 9 |
| 10 | #include "base/files/file_path.h" |
| 11 | #include "base/files/file_util.h" |
| 12 | #include "base/logging.h" |
| 13 | #include "base/mac/scoped_cftyperef.h" |
| 14 | #include "base/strings/string_util.h" |
| 15 | #include "base/strings/utf_string_conversions.h" |
| 16 | #include "base/threading/thread_restrictions.h" |
| 17 |
| 18 | namespace base { |
| 19 |
| 20 | static NativeLibraryObjCStatus GetObjCStatusForImage( |
| 21 | const void* function_pointer) { |
| 22 | Dl_info info; |
| 23 | if (!dladdr(function_pointer, &info)) |
| 24 | return OBJC_UNKNOWN; |
| 25 |
| 26 | // See if the the image contains an "ObjC image info" segment. This method |
| 27 | // of testing is used in _CFBundleGrokObjcImageInfoFromFile in |
| 28 | // CF-744/CFBundle.c, around lines 2447-2474. |
| 29 | // |
| 30 | // In 64-bit images, ObjC can be recognized in __DATA,__objc_imageinfo. |
| 31 | const section_64* section = getsectbynamefromheader_64( |
| 32 | reinterpret_cast<const struct mach_header_64*>(info.dli_fbase), |
| 33 | SEG_DATA, "__objc_imageinfo"); |
| 34 | return section ? OBJC_PRESENT : OBJC_NOT_PRESENT; |
| 35 | } |
| 36 |
| 37 | std::string NativeLibraryLoadError::ToString() const { |
| 38 | return message; |
| 39 | } |
| 40 |
| 41 | NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path, |
| 42 | const NativeLibraryOptions& options, |
| 43 | NativeLibraryLoadError* error) { |
| 44 | // dlopen() etc. open the file off disk. |
| 45 | if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) { |
| 46 | void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); |
| 47 | if (!dylib) { |
| 48 | if (error) |
| 49 | error->message = dlerror(); |
| 50 | return nullptr; |
| 51 | } |
| 52 | NativeLibrary native_lib = new NativeLibraryStruct(); |
| 53 | native_lib->type = DYNAMIC_LIB; |
| 54 | native_lib->dylib = dylib; |
| 55 | native_lib->objc_status = OBJC_UNKNOWN; |
| 56 | return native_lib; |
| 57 | } |
| 58 | ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( |
| 59 | kCFAllocatorDefault, |
| 60 | (const UInt8*)library_path.value().c_str(), |
| 61 | library_path.value().length(), |
| 62 | true)); |
| 63 | if (!url) |
| 64 | return nullptr; |
| 65 | CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); |
| 66 | if (!bundle) |
| 67 | return nullptr; |
| 68 |
| 69 | NativeLibrary native_lib = new NativeLibraryStruct(); |
| 70 | native_lib->type = BUNDLE; |
| 71 | native_lib->bundle = bundle; |
| 72 | native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); |
| 73 | native_lib->objc_status = OBJC_UNKNOWN; |
| 74 | return native_lib; |
| 75 | } |
| 76 |
| 77 | void UnloadNativeLibrary(NativeLibrary library) { |
| 78 | if (library->objc_status == OBJC_NOT_PRESENT) { |
| 79 | if (library->type == BUNDLE) { |
| 80 | CFBundleCloseBundleResourceMap(library->bundle, |
| 81 | library->bundle_resource_ref); |
| 82 | CFRelease(library->bundle); |
| 83 | } else { |
| 84 | dlclose(library->dylib); |
| 85 | } |
| 86 | } else { |
| 87 | VLOG(2) << "Not unloading NativeLibrary because it may contain an ObjC " |
| 88 | "segment. library->objc_status = " << library->objc_status; |
| 89 | // Deliberately do not CFRelease the bundle or dlclose the dylib because |
| 90 | // doing so can corrupt the ObjC runtime method caches. See |
| 91 | // http://crbug.com/172319 for details. |
| 92 | } |
| 93 | delete library; |
| 94 | } |
| 95 |
| 96 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
| 97 | StringPiece name) { |
| 98 | void* function_pointer = nullptr; |
| 99 |
| 100 | // Get the function pointer using the right API for the type. |
| 101 | if (library->type == BUNDLE) { |
| 102 | ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString( |
| 103 | kCFAllocatorDefault, name.data(), kCFStringEncodingUTF8)); |
| 104 | function_pointer = CFBundleGetFunctionPointerForName(library->bundle, |
| 105 | symbol_name); |
| 106 | } else { |
| 107 | function_pointer = dlsym(library->dylib, name.data()); |
| 108 | } |
| 109 |
| 110 | // If this library hasn't been tested for having ObjC, use the function |
| 111 | // pointer to look up the section information for the library. |
| 112 | if (function_pointer && library->objc_status == OBJC_UNKNOWN) |
| 113 | library->objc_status = GetObjCStatusForImage(function_pointer); |
| 114 |
| 115 | return function_pointer; |
| 116 | } |
| 117 |
| 118 | std::string GetNativeLibraryName(StringPiece name) { |
| 119 | DCHECK(IsStringASCII(name)); |
| 120 | return "lib" + name.as_string() + ".dylib"; |
| 121 | } |
| 122 |
| 123 | std::string GetLoadableModuleName(StringPiece name) { |
| 124 | DCHECK(IsStringASCII(name)); |
| 125 | return name.as_string() + ".so"; |
| 126 | } |
| 127 |
| 128 | } // namespace base |
| 129 |