Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: base/native_library_mac.mm

Issue 2042103002: Clean up some nits in base::NativeLibrary. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: restore comment Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/native_library_ios.mm ('k') | base/native_library_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/mac/scoped_cftyperef.h" 13 #include "base/mac/scoped_cftyperef.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/thread_restrictions.h" 16 #include "base/threading/thread_restrictions.h"
17 17
18 namespace base { 18 namespace base {
19 19
20 static NativeLibraryObjCStatus GetObjCStatusForImage( 20 static NativeLibraryObjCStatus GetObjCStatusForImage(
21 const void* function_pointer) { 21 const void* function_pointer) {
22 Dl_info info; 22 Dl_info info;
23 if (!dladdr(function_pointer, &info)) 23 if (!dladdr(function_pointer, &info))
24 return OBJC_UNKNOWN; 24 return OBJC_UNKNOWN;
25 25
26 // See if the the image contains an "ObjC image info" segment. This method 26 // See if the the image contains an "ObjC image info" segment. This method
27 // of testing is used in _CFBundleGrokObjcImageInfoFromFile in 27 // of testing is used in _CFBundleGrokObjcImageInfoFromFile in
28 // CF-744/CFBundle.c, around lines 2447-2474. 28 // CF-744/CFBundle.c, around lines 2447-2474.
29 // 29 //
30 // In 32-bit images, ObjC can be recognized in __OBJC,__image_info, whereas 30 // In 64-bit images, ObjC can be recognized in __DATA,__objc_imageinfo.
31 // in 64-bit, the data is in __DATA,__objc_imageinfo.
32 #if __LP64__
33 const section_64* section = getsectbynamefromheader_64( 31 const section_64* section = getsectbynamefromheader_64(
34 reinterpret_cast<const struct mach_header_64*>(info.dli_fbase), 32 reinterpret_cast<const struct mach_header_64*>(info.dli_fbase),
35 SEG_DATA, "__objc_imageinfo"); 33 SEG_DATA, "__objc_imageinfo");
36 #else 34 return section ? OBJC_PRESENT : OBJC_NOT_PRESENT;
37 const section* section = getsectbynamefromheader(
38 reinterpret_cast<const struct mach_header*>(info.dli_fbase),
39 SEG_OBJC, "__image_info");
40 #endif
41 return section == NULL ? OBJC_NOT_PRESENT : OBJC_PRESENT;
42 } 35 }
43 36
44 std::string NativeLibraryLoadError::ToString() const { 37 std::string NativeLibraryLoadError::ToString() const {
45 return message; 38 return message;
46 } 39 }
47 40
48 // static 41 // static
49 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, 42 NativeLibrary LoadNativeLibrary(const FilePath& library_path,
50 NativeLibraryLoadError* error) { 43 NativeLibraryLoadError* error) {
51 // dlopen() etc. open the file off disk. 44 // dlopen() etc. open the file off disk.
52 if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) { 45 if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) {
53 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); 46 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY);
54 if (!dylib) { 47 if (!dylib) {
55 if (error) 48 if (error)
56 error->message = dlerror(); 49 error->message = dlerror();
57 return NULL; 50 return NULL;
58 } 51 }
59 NativeLibrary native_lib = new NativeLibraryStruct(); 52 NativeLibrary native_lib = new NativeLibraryStruct();
60 native_lib->type = DYNAMIC_LIB; 53 native_lib->type = DYNAMIC_LIB;
61 native_lib->dylib = dylib; 54 native_lib->dylib = dylib;
62 native_lib->objc_status = OBJC_UNKNOWN; 55 native_lib->objc_status = OBJC_UNKNOWN;
63 return native_lib; 56 return native_lib;
64 } 57 }
65 base::ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( 58 ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
66 kCFAllocatorDefault, 59 kCFAllocatorDefault,
67 (const UInt8*)library_path.value().c_str(), 60 (const UInt8*)library_path.value().c_str(),
68 library_path.value().length(), 61 library_path.value().length(),
69 true)); 62 true));
70 if (!url) 63 if (!url)
71 return NULL; 64 return NULL;
72 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); 65 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
73 if (!bundle) 66 if (!bundle)
74 return NULL; 67 return NULL;
75 68
(...skipping 20 matching lines...) Expand all
96 "segment. library->objc_status = " << library->objc_status; 89 "segment. library->objc_status = " << library->objc_status;
97 // Deliberately do not CFRelease the bundle or dlclose the dylib because 90 // Deliberately do not CFRelease the bundle or dlclose the dylib because
98 // doing so can corrupt the ObjC runtime method caches. See 91 // doing so can corrupt the ObjC runtime method caches. See
99 // http://crbug.com/172319 for details. 92 // http://crbug.com/172319 for details.
100 } 93 }
101 delete library; 94 delete library;
102 } 95 }
103 96
104 // static 97 // static
105 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, 98 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
106 const char* name) { 99 StringPiece name) {
107 void* function_pointer = NULL; 100 void* function_pointer = NULL;
108 101
109 // Get the function pointer using the right API for the type. 102 // Get the function pointer using the right API for the type.
110 if (library->type == BUNDLE) { 103 if (library->type == BUNDLE) {
111 base::ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString( 104 ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString(
112 kCFAllocatorDefault, name, kCFStringEncodingUTF8)); 105 kCFAllocatorDefault, name.data(), kCFStringEncodingUTF8));
113 function_pointer = CFBundleGetFunctionPointerForName(library->bundle, 106 function_pointer = CFBundleGetFunctionPointerForName(library->bundle,
114 symbol_name); 107 symbol_name);
115 } else { 108 } else {
116 function_pointer = dlsym(library->dylib, name); 109 function_pointer = dlsym(library->dylib, name.data());
117 } 110 }
118 111
119 // 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
120 // pointer to look up the section information for the library. 113 // pointer to look up the section information for the library.
121 if (function_pointer && library->objc_status == OBJC_UNKNOWN) 114 if (function_pointer && library->objc_status == OBJC_UNKNOWN)
122 library->objc_status = GetObjCStatusForImage(function_pointer); 115 library->objc_status = GetObjCStatusForImage(function_pointer);
123 116
124 return function_pointer; 117 return function_pointer;
125 } 118 }
126 119
127 // static 120 // static
128 string16 GetNativeLibraryName(const string16& name) { 121 string16 GetNativeLibraryName(StringPiece16 name) {
129 return name + ASCIIToUTF16(".dylib"); 122 return name.as_string() + ASCIIToUTF16(".dylib");
130 } 123 }
131 124
132 } // namespace base 125 } // namespace base
OLDNEW
« no previous file with comments | « base/native_library_ios.mm ('k') | base/native_library_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698