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 bool ImageContainsObjectiveC(const struct mach_header* image_header) { | |
21 // See if the the image contains an "ObjC image info" segment. This method | |
22 // of testing is used in _CFBundleGrokObjcImageInfoFromFile in | |
23 // CF-744/CFBundle.c, around lines 2447-2474. | |
24 // | |
25 // In 32-bit images, ObjC can be recognized in __OBJC,__image_info, whereas | |
26 // in 64-bit, the data is in __DATA,__objc_imageinfo. | |
27 #if __LP64__ | |
28 const section_64* section = getsectbynamefromheader_64( | |
29 reinterpret_cast<const struct mach_header_64*>(image_header), | |
30 SEG_DATA, "__objc_imageinfo"); | |
31 #else | |
32 const section* section = getsectbynamefromheader( | |
33 image_header, SEG_OBJC, "__image_info"); | |
34 #endif | |
35 return section != NULL; | |
36 } | |
37 | |
18 // static | 38 // static |
19 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, | 39 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, |
20 std::string* error) { | 40 std::string* error) { |
21 // dlopen() etc. open the file off disk. | 41 // dlopen() etc. open the file off disk. |
22 if (library_path.Extension() == "dylib" || | 42 if (library_path.Extension() == "dylib" || |
23 !file_util::DirectoryExists(library_path)) { | 43 !file_util::DirectoryExists(library_path)) { |
24 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); | 44 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); |
25 if (!dylib) | 45 if (!dylib) |
26 return NULL; | 46 return NULL; |
27 NativeLibrary native_lib = new NativeLibraryStruct(); | 47 NativeLibrary native_lib = new NativeLibraryStruct(); |
28 native_lib->type = DYNAMIC_LIB; | 48 native_lib->type = DYNAMIC_LIB; |
29 native_lib->dylib = dylib; | 49 native_lib->dylib = dylib; |
50 native_lib->objc_status = OBJC_UNKNOWN; | |
30 return native_lib; | 51 return native_lib; |
31 } | 52 } |
32 base::mac::ScopedCFTypeRef<CFURLRef> url( | 53 base::mac::ScopedCFTypeRef<CFURLRef> url( |
33 CFURLCreateFromFileSystemRepresentation( | 54 CFURLCreateFromFileSystemRepresentation( |
34 kCFAllocatorDefault, | 55 kCFAllocatorDefault, |
35 (const UInt8*)library_path.value().c_str(), | 56 (const UInt8*)library_path.value().c_str(), |
36 library_path.value().length(), | 57 library_path.value().length(), |
37 true)); | 58 true)); |
38 if (!url) | 59 if (!url) |
39 return NULL; | 60 return NULL; |
40 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); | 61 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); |
41 if (!bundle) | 62 if (!bundle) |
42 return NULL; | 63 return NULL; |
43 | 64 |
44 NativeLibrary native_lib = new NativeLibraryStruct(); | 65 NativeLibrary native_lib = new NativeLibraryStruct(); |
45 native_lib->type = BUNDLE; | 66 native_lib->type = BUNDLE; |
46 native_lib->bundle = bundle; | 67 native_lib->bundle = bundle; |
47 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); | 68 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); |
69 native_lib->objc_status = OBJC_UNKNOWN; | |
48 return native_lib; | 70 return native_lib; |
49 } | 71 } |
50 | 72 |
51 // static | 73 // static |
52 void UnloadNativeLibrary(NativeLibrary library) { | 74 void UnloadNativeLibrary(NativeLibrary library) { |
53 if (library->type == BUNDLE) { | 75 if (library->objc_status == OBJC_NOT_PRESENT) { |
54 CFBundleCloseBundleResourceMap(library->bundle, | 76 if (library->type == BUNDLE) { |
55 library->bundle_resource_ref); | 77 CFBundleCloseBundleResourceMap(library->bundle, |
56 CFRelease(library->bundle); | 78 library->bundle_resource_ref); |
79 CFRelease(library->bundle); | |
80 } else { | |
81 dlclose(library->dylib); | |
82 } | |
57 } else { | 83 } else { |
58 dlclose(library->dylib); | 84 VLOG(2) << |
85 "Not unloading NativeLibrary because it contains an ObjC segment."; | |
Mark Mentovai
2013/03/14 18:22:36
This message isn’t strictly correct if the status
Robert Sesek
2013/03/14 18:27:38
Done.
| |
86 // Deliberately do not CFRelease the bundle or dlclose the dylib because | |
87 // doing so can corrupt the ObjC runtime method caches. See | |
88 // http://crbug.com/172319 for details. | |
59 } | 89 } |
60 delete library; | 90 delete library; |
61 } | 91 } |
62 | 92 |
63 // static | 93 // static |
64 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 94 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
65 const char* name) { | 95 const char* name) { |
96 void* function_pointer = NULL; | |
97 | |
98 // Get the function pointer using the right API for the type. | |
66 if (library->type == BUNDLE) { | 99 if (library->type == BUNDLE) { |
67 base::mac::ScopedCFTypeRef<CFStringRef> symbol_name( | 100 base::mac::ScopedCFTypeRef<CFStringRef> symbol_name( |
68 CFStringCreateWithCString(kCFAllocatorDefault, name, | 101 CFStringCreateWithCString(kCFAllocatorDefault, name, |
69 kCFStringEncodingUTF8)); | 102 kCFStringEncodingUTF8)); |
70 return CFBundleGetFunctionPointerForName(library->bundle, symbol_name); | 103 function_pointer = CFBundleGetFunctionPointerForName(library->bundle, |
104 symbol_name); | |
105 } else { | |
106 function_pointer = dlsym(library->dylib, name); | |
71 } | 107 } |
72 return dlsym(library->dylib, name); | 108 |
109 // If this library hasn't been tested for having ObjC, get the mach_header | |
110 // for the library and see if it has an ObjC segment. | |
111 if (library->objc_status == OBJC_UNKNOWN) { | |
Mark Mentovai
2013/03/14 18:22:36
&& function_pointer
Robert Sesek
2013/03/14 18:27:38
Done.
| |
112 Dl_info info; | |
113 if (dladdr(function_pointer, &info)) { | |
114 const struct mach_header* header = | |
115 reinterpret_cast<const struct mach_header*>(info.dli_fbase); | |
Scott Hess - ex-Googler
2013/03/14 18:30:39
I think that you should pass in a void* (or whatev
Robert Sesek
2013/03/14 20:41:25
Done.
| |
116 library->objc_status = | |
117 ImageContainsObjectiveC(header) ? OBJC_PRESENT : OBJC_NOT_PRESENT; | |
118 } | |
119 } | |
120 | |
121 return function_pointer; | |
73 } | 122 } |
74 | 123 |
75 // static | 124 // static |
76 string16 GetNativeLibraryName(const string16& name) { | 125 string16 GetNativeLibraryName(const string16& name) { |
77 return name + ASCIIToUTF16(".dylib"); | 126 return name + ASCIIToUTF16(".dylib"); |
78 } | 127 } |
79 | 128 |
80 } // namespace base | 129 } // namespace base |
OLD | NEW |