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

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: 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
Lei Zhang 2016/06/07 01:25:39 And self-reviewing after sending out the CL... I p
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( 26 const section_64* section = getsectbynamefromheader_64(
34 reinterpret_cast<const struct mach_header_64*>(info.dli_fbase), 27 reinterpret_cast<const struct mach_header_64*>(info.dli_fbase),
35 SEG_DATA, "__objc_imageinfo"); 28 SEG_DATA, "__objc_imageinfo");
36 #else 29 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 } 30 }
43 31
44 std::string NativeLibraryLoadError::ToString() const { 32 std::string NativeLibraryLoadError::ToString() const {
45 return message; 33 return message;
46 } 34 }
47 35
48 // static 36 // static
49 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path, 37 NativeLibrary LoadNativeLibrary(const FilePath& library_path,
50 NativeLibraryLoadError* error) { 38 NativeLibraryLoadError* error) {
51 // dlopen() etc. open the file off disk. 39 // dlopen() etc. open the file off disk.
52 if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) { 40 if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) {
53 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); 41 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY);
54 if (!dylib) { 42 if (!dylib) {
55 if (error) 43 if (error)
56 error->message = dlerror(); 44 error->message = dlerror();
57 return NULL; 45 return NULL;
58 } 46 }
59 NativeLibrary native_lib = new NativeLibraryStruct(); 47 NativeLibrary native_lib = new NativeLibraryStruct();
60 native_lib->type = DYNAMIC_LIB; 48 native_lib->type = DYNAMIC_LIB;
61 native_lib->dylib = dylib; 49 native_lib->dylib = dylib;
62 native_lib->objc_status = OBJC_UNKNOWN; 50 native_lib->objc_status = OBJC_UNKNOWN;
63 return native_lib; 51 return native_lib;
64 } 52 }
65 base::ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( 53 ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
66 kCFAllocatorDefault, 54 kCFAllocatorDefault,
67 (const UInt8*)library_path.value().c_str(), 55 (const UInt8*)library_path.value().c_str(),
68 library_path.value().length(), 56 library_path.value().length(),
69 true)); 57 true));
70 if (!url) 58 if (!url)
71 return NULL; 59 return NULL;
72 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); 60 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
73 if (!bundle) 61 if (!bundle)
74 return NULL; 62 return NULL;
75 63
(...skipping 20 matching lines...) Expand all
96 "segment. library->objc_status = " << library->objc_status; 84 "segment. library->objc_status = " << library->objc_status;
97 // Deliberately do not CFRelease the bundle or dlclose the dylib because 85 // Deliberately do not CFRelease the bundle or dlclose the dylib because
98 // doing so can corrupt the ObjC runtime method caches. See 86 // doing so can corrupt the ObjC runtime method caches. See
99 // http://crbug.com/172319 for details. 87 // http://crbug.com/172319 for details.
100 } 88 }
101 delete library; 89 delete library;
102 } 90 }
103 91
104 // static 92 // static
105 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, 93 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
106 const char* name) { 94 StringPiece name) {
107 void* function_pointer = NULL; 95 void* function_pointer = NULL;
108 96
109 // Get the function pointer using the right API for the type. 97 // Get the function pointer using the right API for the type.
110 if (library->type == BUNDLE) { 98 if (library->type == BUNDLE) {
111 base::ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString( 99 ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString(
112 kCFAllocatorDefault, name, kCFStringEncodingUTF8)); 100 kCFAllocatorDefault, name.data(), kCFStringEncodingUTF8));
113 function_pointer = CFBundleGetFunctionPointerForName(library->bundle, 101 function_pointer = CFBundleGetFunctionPointerForName(library->bundle,
114 symbol_name); 102 symbol_name);
115 } else { 103 } else {
116 function_pointer = dlsym(library->dylib, name); 104 function_pointer = dlsym(library->dylib, name.data());
117 } 105 }
118 106
119 // If this library hasn't been tested for having ObjC, use the function 107 // If this library hasn't been tested for having ObjC, use the function
120 // pointer to look up the section information for the library. 108 // pointer to look up the section information for the library.
121 if (function_pointer && library->objc_status == OBJC_UNKNOWN) 109 if (function_pointer && library->objc_status == OBJC_UNKNOWN)
122 library->objc_status = GetObjCStatusForImage(function_pointer); 110 library->objc_status = GetObjCStatusForImage(function_pointer);
123 111
124 return function_pointer; 112 return function_pointer;
125 } 113 }
126 114
127 // static 115 // static
128 string16 GetNativeLibraryName(const string16& name) { 116 string16 GetNativeLibraryName(StringPiece16 name) {
129 return name + ASCIIToUTF16(".dylib"); 117 return name.as_string() + ASCIIToUTF16(".dylib");
Lei Zhang 2016/06/07 01:39:52 BTW, I suspect this is wrong, and it should be lib
xhwang 2016/06/07 16:39:27 +1
Mark Mentovai 2016/06/10 20:06:18 Lei Zhang wrote:
130 } 118 }
131 119
132 } // namespace base 120 } // 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