| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/native_library.h" | 5 #include "base/native_library.h" |
| 6 | 6 |
| 7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
| 8 #include <mach-o/getsect.h> | 8 #include <mach-o/getsect.h> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 // static | 41 // static |
| 42 NativeLibrary LoadNativeLibrary(const FilePath& library_path, | 42 NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
| 43 NativeLibraryLoadError* error) { | 43 NativeLibraryLoadError* error) { |
| 44 // dlopen() etc. open the file off disk. | 44 // dlopen() etc. open the file off disk. |
| 45 if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) { | 45 if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) { |
| 46 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); | 46 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); |
| 47 if (!dylib) { | 47 if (!dylib) { |
| 48 if (error) | 48 if (error) |
| 49 error->message = dlerror(); | 49 error->message = dlerror(); |
| 50 return NULL; | 50 return nullptr; |
| 51 } | 51 } |
| 52 NativeLibrary native_lib = new NativeLibraryStruct(); | 52 NativeLibrary native_lib = new NativeLibraryStruct(); |
| 53 native_lib->type = DYNAMIC_LIB; | 53 native_lib->type = DYNAMIC_LIB; |
| 54 native_lib->dylib = dylib; | 54 native_lib->dylib = dylib; |
| 55 native_lib->objc_status = OBJC_UNKNOWN; | 55 native_lib->objc_status = OBJC_UNKNOWN; |
| 56 return native_lib; | 56 return native_lib; |
| 57 } | 57 } |
| 58 ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( | 58 ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( |
| 59 kCFAllocatorDefault, | 59 kCFAllocatorDefault, |
| 60 (const UInt8*)library_path.value().c_str(), | 60 (const UInt8*)library_path.value().c_str(), |
| 61 library_path.value().length(), | 61 library_path.value().length(), |
| 62 true)); | 62 true)); |
| 63 if (!url) | 63 if (!url) |
| 64 return NULL; | 64 return nullptr; |
| 65 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); | 65 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); |
| 66 if (!bundle) | 66 if (!bundle) |
| 67 return NULL; | 67 return nullptr; |
| 68 | 68 |
| 69 NativeLibrary native_lib = new NativeLibraryStruct(); | 69 NativeLibrary native_lib = new NativeLibraryStruct(); |
| 70 native_lib->type = BUNDLE; | 70 native_lib->type = BUNDLE; |
| 71 native_lib->bundle = bundle; | 71 native_lib->bundle = bundle; |
| 72 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); | 72 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); |
| 73 native_lib->objc_status = OBJC_UNKNOWN; | 73 native_lib->objc_status = OBJC_UNKNOWN; |
| 74 return native_lib; | 74 return native_lib; |
| 75 } | 75 } |
| 76 | 76 |
| 77 // static | 77 // static |
| (...skipping 12 matching lines...) Expand all Loading... |
| 90 // Deliberately do not CFRelease the bundle or dlclose the dylib because | 90 // Deliberately do not CFRelease the bundle or dlclose the dylib because |
| 91 // doing so can corrupt the ObjC runtime method caches. See | 91 // doing so can corrupt the ObjC runtime method caches. See |
| 92 // http://crbug.com/172319 for details. | 92 // http://crbug.com/172319 for details. |
| 93 } | 93 } |
| 94 delete library; | 94 delete library; |
| 95 } | 95 } |
| 96 | 96 |
| 97 // static | 97 // static |
| 98 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 98 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
| 99 StringPiece name) { | 99 StringPiece name) { |
| 100 void* function_pointer = NULL; | 100 void* function_pointer = nullptr; |
| 101 | 101 |
| 102 // Get the function pointer using the right API for the type. | 102 // Get the function pointer using the right API for the type. |
| 103 if (library->type == BUNDLE) { | 103 if (library->type == BUNDLE) { |
| 104 ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString( | 104 ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString( |
| 105 kCFAllocatorDefault, name.data(), kCFStringEncodingUTF8)); | 105 kCFAllocatorDefault, name.data(), kCFStringEncodingUTF8)); |
| 106 function_pointer = CFBundleGetFunctionPointerForName(library->bundle, | 106 function_pointer = CFBundleGetFunctionPointerForName(library->bundle, |
| 107 symbol_name); | 107 symbol_name); |
| 108 } else { | 108 } else { |
| 109 function_pointer = dlsym(library->dylib, name.data()); | 109 function_pointer = dlsym(library->dylib, name.data()); |
| 110 } | 110 } |
| 111 | 111 |
| 112 // If this library hasn't been tested for having ObjC, use the function | 112 // If this library hasn't been tested for having ObjC, use the function |
| 113 // pointer to look up the section information for the library. | 113 // pointer to look up the section information for the library. |
| 114 if (function_pointer && library->objc_status == OBJC_UNKNOWN) | 114 if (function_pointer && library->objc_status == OBJC_UNKNOWN) |
| 115 library->objc_status = GetObjCStatusForImage(function_pointer); | 115 library->objc_status = GetObjCStatusForImage(function_pointer); |
| 116 | 116 |
| 117 return function_pointer; | 117 return function_pointer; |
| 118 } | 118 } |
| 119 | 119 |
| 120 // static | 120 // static |
| 121 string16 GetNativeLibraryName(StringPiece16 name) { | 121 std::string GetNativeLibraryName(StringPiece name) { |
| 122 return name.as_string() + ASCIIToUTF16(".dylib"); | 122 DCHECK(IsStringASCII(name)); |
| 123 return "lib" + name.as_string() + ".dylib"; |
| 123 } | 124 } |
| 124 | 125 |
| 125 } // namespace base | 126 } // namespace base |
| OLD | NEW |