Chromium Code Reviews| 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 | 9 |
| 9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/logging.h" | |
| 11 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 13 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 14 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 15 | 17 |
| 16 namespace base { | 18 namespace base { |
| 17 | 19 |
| 20 static NativeLibraryObjCStatus GetObjCStatusForImage( | |
| 21 const void* function_pointer) { | |
|
Scott Hess - ex-Googler
2013/03/14 21:12:53
Is void* even a valid assignment for a function po
Mark Mentovai
2013/03/14 21:17:55
shess wrote:
| |
| 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 32-bit images, ObjC can be recognized in __OBJC,__image_info, whereas | |
| 31 // in 64-bit, the data is in __DATA,__objc_imageinfo. | |
| 32 #if __LP64__ | |
| 33 const section_64* section = getsectbynamefromheader_64( | |
| 34 reinterpret_cast<const struct mach_header_64*>(info.dli_fbase), | |
| 35 SEG_DATA, "__objc_imageinfo"); | |
| 36 #else | |
| 37 const section* section = getsectbynamefromheader( | |
| 38 reinterpret_cast<const struct mach_header*>(info.dli_fbase), | |
|
Scott Hess - ex-Googler
2013/03/14 21:12:53
Perfect! Symmetric grodiness.
| |
| 39 SEG_OBJC, "__image_info"); | |
| 40 #endif | |
| 41 return section == NULL ? OBJC_NOT_PRESENT : OBJC_PRESENT; | |
| 42 } | |
| 43 | |
| 18 // static | 44 // static |
| 19 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, | 45 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, |
| 20 std::string* error) { | 46 std::string* error) { |
| 21 // dlopen() etc. open the file off disk. | 47 // dlopen() etc. open the file off disk. |
| 22 if (library_path.Extension() == "dylib" || | 48 if (library_path.Extension() == "dylib" || |
| 23 !file_util::DirectoryExists(library_path)) { | 49 !file_util::DirectoryExists(library_path)) { |
| 24 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); | 50 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); |
| 25 if (!dylib) | 51 if (!dylib) |
| 26 return NULL; | 52 return NULL; |
| 27 NativeLibrary native_lib = new NativeLibraryStruct(); | 53 NativeLibrary native_lib = new NativeLibraryStruct(); |
| 28 native_lib->type = DYNAMIC_LIB; | 54 native_lib->type = DYNAMIC_LIB; |
| 29 native_lib->dylib = dylib; | 55 native_lib->dylib = dylib; |
| 56 native_lib->objc_status = OBJC_UNKNOWN; | |
| 30 return native_lib; | 57 return native_lib; |
| 31 } | 58 } |
| 32 base::mac::ScopedCFTypeRef<CFURLRef> url( | 59 base::mac::ScopedCFTypeRef<CFURLRef> url( |
| 33 CFURLCreateFromFileSystemRepresentation( | 60 CFURLCreateFromFileSystemRepresentation( |
| 34 kCFAllocatorDefault, | 61 kCFAllocatorDefault, |
| 35 (const UInt8*)library_path.value().c_str(), | 62 (const UInt8*)library_path.value().c_str(), |
| 36 library_path.value().length(), | 63 library_path.value().length(), |
| 37 true)); | 64 true)); |
| 38 if (!url) | 65 if (!url) |
| 39 return NULL; | 66 return NULL; |
| 40 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); | 67 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); |
| 41 if (!bundle) | 68 if (!bundle) |
| 42 return NULL; | 69 return NULL; |
| 43 | 70 |
| 44 NativeLibrary native_lib = new NativeLibraryStruct(); | 71 NativeLibrary native_lib = new NativeLibraryStruct(); |
| 45 native_lib->type = BUNDLE; | 72 native_lib->type = BUNDLE; |
| 46 native_lib->bundle = bundle; | 73 native_lib->bundle = bundle; |
| 47 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); | 74 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); |
| 75 native_lib->objc_status = OBJC_UNKNOWN; | |
| 48 return native_lib; | 76 return native_lib; |
| 49 } | 77 } |
| 50 | 78 |
| 51 // static | 79 // static |
| 52 void UnloadNativeLibrary(NativeLibrary library) { | 80 void UnloadNativeLibrary(NativeLibrary library) { |
| 53 if (library->type == BUNDLE) { | 81 if (library->objc_status == OBJC_NOT_PRESENT) { |
| 54 CFBundleCloseBundleResourceMap(library->bundle, | 82 if (library->type == BUNDLE) { |
| 55 library->bundle_resource_ref); | 83 CFBundleCloseBundleResourceMap(library->bundle, |
| 56 CFRelease(library->bundle); | 84 library->bundle_resource_ref); |
| 85 CFRelease(library->bundle); | |
| 86 } else { | |
| 87 dlclose(library->dylib); | |
| 88 } | |
| 57 } else { | 89 } else { |
| 58 dlclose(library->dylib); | 90 VLOG(2) << "Not unloading NativeLibrary because it may contain an ObjC " |
| 91 "segment. library->objc_status = " << library->objc_status; | |
| 92 // Deliberately do not CFRelease the bundle or dlclose the dylib because | |
| 93 // doing so can corrupt the ObjC runtime method caches. See | |
| 94 // http://crbug.com/172319 for details. | |
| 59 } | 95 } |
| 60 delete library; | 96 delete library; |
| 61 } | 97 } |
| 62 | 98 |
| 63 // static | 99 // static |
| 64 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 100 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
| 65 const char* name) { | 101 const char* name) { |
| 102 void* function_pointer = NULL; | |
| 103 | |
| 104 // Get the function pointer using the right API for the type. | |
| 66 if (library->type == BUNDLE) { | 105 if (library->type == BUNDLE) { |
| 67 base::mac::ScopedCFTypeRef<CFStringRef> symbol_name( | 106 base::mac::ScopedCFTypeRef<CFStringRef> symbol_name( |
| 68 CFStringCreateWithCString(kCFAllocatorDefault, name, | 107 CFStringCreateWithCString(kCFAllocatorDefault, name, |
| 69 kCFStringEncodingUTF8)); | 108 kCFStringEncodingUTF8)); |
| 70 return CFBundleGetFunctionPointerForName(library->bundle, symbol_name); | 109 function_pointer = CFBundleGetFunctionPointerForName(library->bundle, |
| 110 symbol_name); | |
| 111 } else { | |
| 112 function_pointer = dlsym(library->dylib, name); | |
| 71 } | 113 } |
| 72 return dlsym(library->dylib, name); | 114 |
| 115 // If this library hasn't been tested for having ObjC, use the function | |
| 116 // pointer to look up the section information for the library. | |
| 117 if (function_pointer && library->objc_status == OBJC_UNKNOWN) | |
| 118 library->objc_status = GetObjCStatusForImage(function_pointer); | |
| 119 | |
| 120 return function_pointer; | |
| 73 } | 121 } |
| 74 | 122 |
| 75 // static | 123 // static |
| 76 string16 GetNativeLibraryName(const string16& name) { | 124 string16 GetNativeLibraryName(const string16& name) { |
| 77 return name + ASCIIToUTF16(".dylib"); | 125 return name + ASCIIToUTF16(".dylib"); |
| 78 } | 126 } |
| 79 | 127 |
| 80 } // namespace base | 128 } // namespace base |
| OLD | NEW |