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/task_info.h> | |
| 9 #include <mach-o/dyld_images.h> | |
| 10 #include <mach-o/getsect.h> | |
| 11 | |
| 12 #include <Foundation/Foundation.h> | |
|
Mark Mentovai
2013/03/12 19:19:31
#import me.
Robert Sesek
2013/03/12 20:02:10
Done.
| |
| 8 | 13 |
| 9 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 15 #include "base/logging.h" |
| 16 #include "base/mac/mac_util.h" | |
| 11 #include "base/mac/scoped_cftyperef.h" | 17 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 13 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 14 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 15 | 21 |
| 16 namespace base { | 22 namespace base { |
| 17 | 23 |
| 24 // In 32-bit images, ObjC can be recognized in __OBJC.__image_info, whereas | |
|
Mark Mentovai
2013/03/12 19:19:31
It’s usual to separate the segment and section nam
Robert Sesek
2013/03/12 20:02:10
Done.
| |
| 25 // in 64-bit, the data is in __DATA.__objc_imageinfo. | |
| 26 static const char kObjCImageInfo32[] = "__image_info"; | |
|
Mark Mentovai
2013/03/12 19:19:31
Why do you need to check for both flavors? If you’
Robert Sesek
2013/03/12 20:02:10
Done.
| |
| 27 static const char kObjCImageInfo64[] = "__objc_imageinfo"; | |
| 28 | |
| 29 static bool ImageContainsObjectiveC(const base::FilePath& library_path) { | |
| 30 // Get the dyld info from the kernel, which will contain the array of all | |
| 31 // loaded modules. | |
| 32 task_dyld_info_data_t task_dyld_info; | |
| 33 mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; | |
| 34 kern_return_t result = task_info(mach_task_self(), TASK_DYLD_INFO, | |
| 35 reinterpret_cast<task_info_t>(&task_dyld_info), &count); | |
| 36 if (result != KERN_SUCCESS) { | |
| 37 VLOG(1) << "Failed to get TASK_DYLD_INFO: " << result; | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 const char* const library_path_ascii = library_path.MaybeAsASCII().c_str(); | |
|
Mark Mentovai
2013/03/12 19:19:31
This is bogus since you know what platform you’re
Robert Sesek
2013/03/12 20:02:10
Done.
| |
| 42 VLOG(2) << "Looking up image data for " << library_path_ascii; | |
|
Mark Mentovai
2013/03/12 19:19:31
Is this line even needed now?
Robert Sesek
2013/03/12 20:02:10
Removed.
| |
| 43 | |
| 44 // Search the array of loaded images for the |library_path| that is being | |
| 45 // tested for ObjC. | |
| 46 struct dyld_all_image_infos* infos = | |
| 47 reinterpret_cast<struct dyld_all_image_infos*>( | |
| 48 task_dyld_info.all_image_info_addr); | |
| 49 for (uint32_t i = 0; i < infos->infoArrayCount; ++i) { | |
| 50 const struct dyld_image_info image_info = infos->infoArray[i]; | |
| 51 if (strcmp(image_info.imageFilePath, library_path_ascii) != 0) | |
| 52 continue; | |
| 53 | |
| 54 // See if the the image contains an "ObjC image info" segment. This method | |
| 55 // of testing is copied from _CFBundleGrokObjcImageInfoFromFile in | |
| 56 // CF-744/CFBundle.c, around lines 2447-2474. | |
| 57 if (image_info.imageLoadAddress->cputype == CPU_TYPE_X86_64) { | |
|
Mark Mentovai
2013/03/12 19:19:31
Evaluating this conditional at runtime is stupid (
Robert Sesek
2013/03/12 20:02:10
Done.
| |
| 58 const section_64* section = getsectbynamefromheader_64( | |
| 59 reinterpret_cast<const struct mach_header_64*>( | |
| 60 image_info.imageLoadAddress), | |
| 61 SEG_DATA, kObjCImageInfo64); | |
|
Mark Mentovai
2013/03/12 19:19:31
kObjCImageInfo64/32 didn’t need to be defined exte
Robert Sesek
2013/03/12 20:02:10
Done.
| |
| 62 return section != NULL; | |
| 63 } else { | |
| 64 const section* section = getsectbynamefromheader( | |
| 65 image_info.imageLoadAddress, SEG_OBJC, kObjCImageInfo32); | |
| 66 return section != NULL; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 VLOG(1) << "Could not find image info for " << library_path_ascii; | |
| 71 | |
| 72 return false; | |
|
Scott Hess - ex-Googler
2013/03/12 20:10:38
AFAICT, in this case we couldn't find the informat
Robert Sesek
2013/03/12 20:19:43
I see your point, but what should happen in the He
Scott Hess - ex-Googler
2013/03/12 20:43:53
Am I reading it wrong? In the HellIfIKnow case, t
| |
| 73 } | |
| 74 | |
| 18 // static | 75 // static |
| 19 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, | 76 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, |
| 20 std::string* error) { | 77 std::string* error) { |
| 21 // dlopen() etc. open the file off disk. | 78 // dlopen() etc. open the file off disk. |
| 22 if (library_path.Extension() == "dylib" || | 79 if (library_path.Extension() == "dylib" || |
| 23 !file_util::DirectoryExists(library_path)) { | 80 !file_util::DirectoryExists(library_path)) { |
| 24 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); | 81 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); |
| 25 if (!dylib) | 82 if (!dylib) |
| 26 return NULL; | 83 return NULL; |
| 27 NativeLibrary native_lib = new NativeLibraryStruct(); | 84 NativeLibrary native_lib = new NativeLibraryStruct(); |
| 28 native_lib->type = DYNAMIC_LIB; | 85 native_lib->type = DYNAMIC_LIB; |
| 29 native_lib->dylib = dylib; | 86 native_lib->dylib = dylib; |
| 87 native_lib->image_path = library_path; | |
| 30 return native_lib; | 88 return native_lib; |
| 31 } | 89 } |
| 32 base::mac::ScopedCFTypeRef<CFURLRef> url( | 90 base::mac::ScopedCFTypeRef<CFURLRef> url( |
| 33 CFURLCreateFromFileSystemRepresentation( | 91 CFURLCreateFromFileSystemRepresentation( |
| 34 kCFAllocatorDefault, | 92 kCFAllocatorDefault, |
| 35 (const UInt8*)library_path.value().c_str(), | 93 (const UInt8*)library_path.value().c_str(), |
| 36 library_path.value().length(), | 94 library_path.value().length(), |
| 37 true)); | 95 true)); |
| 38 if (!url) | 96 if (!url) |
| 39 return NULL; | 97 return NULL; |
| 40 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); | 98 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); |
| 41 if (!bundle) | 99 if (!bundle) |
| 42 return NULL; | 100 return NULL; |
| 43 | 101 |
| 44 NativeLibrary native_lib = new NativeLibraryStruct(); | 102 NativeLibrary native_lib = new NativeLibraryStruct(); |
| 45 native_lib->type = BUNDLE; | 103 native_lib->type = BUNDLE; |
| 46 native_lib->bundle = bundle; | 104 native_lib->bundle = bundle; |
| 47 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); | 105 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); |
| 106 | |
| 107 base::mac::ScopedCFTypeRef<CFURLRef> executable_url( | |
| 108 CFBundleCopyExecutableURL(bundle)); | |
| 109 NSURL* executable_url_ns = base::mac::CFToNSCast(executable_url); | |
| 110 native_lib->image_path = | |
| 111 base::FilePath([[executable_url_ns path] UTF8String]); | |
|
Mark Mentovai
2013/03/12 19:19:31
Not UTF8String, but fileSystemRepresentation.
Robert Sesek
2013/03/12 20:02:10
Done.
| |
| 48 return native_lib; | 112 return native_lib; |
| 49 } | 113 } |
| 50 | 114 |
| 51 // static | 115 // static |
| 52 void UnloadNativeLibrary(NativeLibrary library) { | 116 void UnloadNativeLibrary(NativeLibrary library) { |
| 117 if (ImageContainsObjectiveC(library->image_path)) { | |
|
Scott Hess - ex-Googler
2013/03/12 20:10:38
Is there any downside to looking this up at Load a
Robert Sesek
2013/03/12 20:19:43
We think alike ;). But that's not possible because
Scott Hess - ex-Googler
2013/03/12 20:43:53
Sigh. Is there any case which differs from:
Line
| |
| 118 VLOG(2) << "Not unloading NativeLibrary at " | |
| 119 << library->image_path.MaybeAsASCII() << " because it contains " | |
| 120 << "an Objective-C segment."; | |
|
Mark Mentovai
2013/03/12 19:19:31
Simplify the object code by coalescing these last
Robert Sesek
2013/03/12 20:02:10
Done.
| |
| 121 // Deliberately do not CFRelease the bundle or dlclose the dylib because | |
| 122 // doing so can corrupt the ObjC runtime method caches. See | |
| 123 // http://crbug.com/172319 for details. | |
| 124 delete library; | |
|
Mark Mentovai
2013/03/12 19:19:31
Why don’t you share the “delete” with the delete b
Robert Sesek
2013/03/12 20:02:10
Done.
| |
| 125 return; | |
| 126 } | |
| 127 | |
| 53 if (library->type == BUNDLE) { | 128 if (library->type == BUNDLE) { |
| 54 CFBundleCloseBundleResourceMap(library->bundle, | 129 CFBundleCloseBundleResourceMap(library->bundle, |
| 55 library->bundle_resource_ref); | 130 library->bundle_resource_ref); |
| 56 CFRelease(library->bundle); | 131 CFRelease(library->bundle); |
| 57 } else { | 132 } else { |
| 58 dlclose(library->dylib); | 133 dlclose(library->dylib); |
| 59 } | 134 } |
| 60 delete library; | 135 delete library; |
| 61 } | 136 } |
| 62 | 137 |
| 63 // static | 138 // static |
| 64 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 139 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
| 65 const char* name) { | 140 const char* name) { |
| 66 if (library->type == BUNDLE) { | 141 if (library->type == BUNDLE) { |
| 67 base::mac::ScopedCFTypeRef<CFStringRef> symbol_name( | 142 base::mac::ScopedCFTypeRef<CFStringRef> symbol_name( |
| 68 CFStringCreateWithCString(kCFAllocatorDefault, name, | 143 CFStringCreateWithCString(kCFAllocatorDefault, name, |
| 69 kCFStringEncodingUTF8)); | 144 kCFStringEncodingUTF8)); |
| 70 return CFBundleGetFunctionPointerForName(library->bundle, symbol_name); | 145 return CFBundleGetFunctionPointerForName(library->bundle, symbol_name); |
| 71 } | 146 } |
| 72 return dlsym(library->dylib, name); | 147 return dlsym(library->dylib, name); |
| 73 } | 148 } |
| 74 | 149 |
| 75 // static | 150 // static |
| 76 string16 GetNativeLibraryName(const string16& name) { | 151 string16 GetNativeLibraryName(const string16& name) { |
| 77 return name + ASCIIToUTF16(".dylib"); | 152 return name + ASCIIToUTF16(".dylib"); |
| 78 } | 153 } |
| 79 | 154 |
| 80 } // namespace base | 155 } // namespace base |
| OLD | NEW |