Update V8 to version 4.7.42.
[chromium-blink-merge.git] / base / native_library_mac.mm
blob8122c28cf4ae6c1a41171d5f70184ef1308507b2
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
3 // found in the LICENSE file.
5 #include "base/native_library.h"
7 #include <dlfcn.h>
8 #include <mach-o/getsect.h>
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/mac/scoped_cftyperef.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/thread_restrictions.h"
18 namespace base {
20 static NativeLibraryObjCStatus GetObjCStatusForImage(
21     const void* function_pointer) {
22   Dl_info info;
23   if (!dladdr(function_pointer, &info))
24     return OBJC_UNKNOWN;
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),
39       SEG_OBJC, "__image_info");
40 #endif
41   return section == NULL ? OBJC_NOT_PRESENT : OBJC_PRESENT;
44 std::string NativeLibraryLoadError::ToString() const {
45   return message;
48 // static
49 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path,
50                                 NativeLibraryLoadError* error) {
51   // dlopen() etc. open the file off disk.
52   if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) {
53     void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY);
54     if (!dylib) {
55       error->message = dlerror();
56       return NULL;
57     }
58     NativeLibrary native_lib = new NativeLibraryStruct();
59     native_lib->type = DYNAMIC_LIB;
60     native_lib->dylib = dylib;
61     native_lib->objc_status = OBJC_UNKNOWN;
62     return native_lib;
63   }
64   base::ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
65       kCFAllocatorDefault,
66       (const UInt8*)library_path.value().c_str(),
67       library_path.value().length(),
68       true));
69   if (!url)
70     return NULL;
71   CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
72   if (!bundle)
73     return NULL;
75   NativeLibrary native_lib = new NativeLibraryStruct();
76   native_lib->type = BUNDLE;
77   native_lib->bundle = bundle;
78   native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle);
79   native_lib->objc_status = OBJC_UNKNOWN;
80   return native_lib;
83 // static
84 void UnloadNativeLibrary(NativeLibrary library) {
85   if (library->objc_status == OBJC_NOT_PRESENT) {
86     if (library->type == BUNDLE) {
87       CFBundleCloseBundleResourceMap(library->bundle,
88                                      library->bundle_resource_ref);
89       CFRelease(library->bundle);
90     } else {
91       dlclose(library->dylib);
92     }
93   } else {
94     VLOG(2) << "Not unloading NativeLibrary because it may contain an ObjC "
95                "segment. library->objc_status = " << library->objc_status;
96     // Deliberately do not CFRelease the bundle or dlclose the dylib because
97     // doing so can corrupt the ObjC runtime method caches. See
98     // http://crbug.com/172319 for details.
99   }
100   delete library;
103 // static
104 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
105                                           const char* name) {
106   void* function_pointer = NULL;
108   // Get the function pointer using the right API for the type.
109   if (library->type == BUNDLE) {
110     base::ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString(
111         kCFAllocatorDefault, name, kCFStringEncodingUTF8));
112     function_pointer = CFBundleGetFunctionPointerForName(library->bundle,
113                                                          symbol_name);
114   } else {
115     function_pointer = dlsym(library->dylib, name);
116   }
118   // If this library hasn't been tested for having ObjC, use the function
119   // pointer to look up the section information for the library.
120   if (function_pointer && library->objc_status == OBJC_UNKNOWN)
121     library->objc_status = GetObjCStatusForImage(function_pointer);
123   return function_pointer;
126 // static
127 string16 GetNativeLibraryName(const string16& name) {
128   return name + ASCIIToUTF16(".dylib");
131 }  // namespace base