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"
8 #include <mach-o/getsect.h>
10 #include "base/file_util.h"
11 #include "base/files/file_path.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"
20 static NativeLibraryObjCStatus GetObjCStatusForImage(
21 const void* function_pointer) {
23 if (!dladdr(function_pointer, &info))
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.
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.
33 const section_64* section = getsectbynamefromheader_64(
34 reinterpret_cast<const struct mach_header_64*>(info.dli_fbase),
35 SEG_DATA, "__objc_imageinfo");
37 const section* section = getsectbynamefromheader(
38 reinterpret_cast<const struct mach_header*>(info.dli_fbase),
39 SEG_OBJC, "__image_info");
41 return section == NULL ? OBJC_NOT_PRESENT : OBJC_PRESENT;
45 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path,
47 // dlopen() etc. open the file off disk.
48 if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) {
49 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY);
52 NativeLibrary native_lib = new NativeLibraryStruct();
53 native_lib->type = DYNAMIC_LIB;
54 native_lib->dylib = dylib;
55 native_lib->objc_status = OBJC_UNKNOWN;
58 base::ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
60 (const UInt8*)library_path.value().c_str(),
61 library_path.value().length(),
65 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
69 NativeLibrary native_lib = new NativeLibraryStruct();
70 native_lib->type = BUNDLE;
71 native_lib->bundle = bundle;
72 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle);
73 native_lib->objc_status = OBJC_UNKNOWN;
78 void UnloadNativeLibrary(NativeLibrary library) {
79 if (library->objc_status == OBJC_NOT_PRESENT) {
80 if (library->type == BUNDLE) {
81 CFBundleCloseBundleResourceMap(library->bundle,
82 library->bundle_resource_ref);
83 CFRelease(library->bundle);
85 dlclose(library->dylib);
88 VLOG(2) << "Not unloading NativeLibrary because it may contain an ObjC "
89 "segment. library->objc_status = " << library->objc_status;
90 // Deliberately do not CFRelease the bundle or dlclose the dylib because
91 // doing so can corrupt the ObjC runtime method caches. See
92 // http://crbug.com/172319 for details.
98 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
100 void* function_pointer = NULL;
102 // Get the function pointer using the right API for the type.
103 if (library->type == BUNDLE) {
104 base::ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString(
105 kCFAllocatorDefault, name, kCFStringEncodingUTF8));
106 function_pointer = CFBundleGetFunctionPointerForName(library->bundle,
109 function_pointer = dlsym(library->dylib, name);
112 // If this library hasn't been tested for having ObjC, use the function
113 // pointer to look up the section information for the library.
114 if (function_pointer && library->objc_status == OBJC_UNKNOWN)
115 library->objc_status = GetObjCStatusForImage(function_pointer);
117 return function_pointer;
121 string16 GetNativeLibraryName(const string16& name) {
122 return name + ASCIIToUTF16(".dylib");