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/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/strcat.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread_restrictions.h"
22 static NativeLibraryObjCStatus GetObjCStatusForImage(
23 const void* function_pointer) {
25 if (!dladdr(function_pointer, &info))
28 // See if the image contains an "ObjC image info" segment. This method
29 // of testing is used in _CFBundleGrokObjcImageInfoFromFile in
30 // CF-744/CFBundle.c, around lines 2447-2474.
32 // In 64-bit images, ObjC can be recognized in __DATA,__objc_imageinfo.
33 const section_64* section = getsectbynamefromheader_64(
34 reinterpret_cast<const struct mach_header_64*>(info.dli_fbase), SEG_DATA,
38 // ....except when "SharedRegionEncodingV2" is on, it's in
39 // __DATA_CONST,__objc_image_info (see https://crbug.com/1220459#c16)
40 section = getsectbynamefromheader_64(
41 reinterpret_cast<const struct mach_header_64*>(info.dli_fbase),
42 "__DATA_CONST", "__objc_imageinfo");
43 return section ? OBJC_PRESENT : OBJC_NOT_PRESENT;
46 std::string NativeLibraryLoadError::ToString() const {
50 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
51 const NativeLibraryOptions& options,
52 NativeLibraryLoadError* error) {
53 // dlopen() etc. open the file off disk.
54 if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) {
55 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY);
58 error->message = dlerror();
61 NativeLibrary native_lib = new NativeLibraryStruct();
62 native_lib->type = DYNAMIC_LIB;
63 native_lib->dylib = dylib;
64 native_lib->objc_status = OBJC_UNKNOWN;
67 ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
69 (const UInt8*)library_path.value().c_str(),
70 library_path.value().length(),
74 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
78 NativeLibrary native_lib = new NativeLibraryStruct();
79 native_lib->type = BUNDLE;
80 native_lib->bundle = bundle;
81 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle);
82 native_lib->objc_status = OBJC_UNKNOWN;
86 void UnloadNativeLibrary(NativeLibrary library) {
87 if (library->objc_status == OBJC_NOT_PRESENT) {
88 if (library->type == BUNDLE) {
89 CFBundleCloseBundleResourceMap(library->bundle,
90 library->bundle_resource_ref);
91 CFRelease(library->bundle);
93 dlclose(library->dylib);
96 VLOG(2) << "Not unloading NativeLibrary because it may contain an ObjC "
97 "segment. library->objc_status = " << library->objc_status;
98 // Deliberately do not CFRelease the bundle or dlclose the dylib because
99 // doing so can corrupt the ObjC runtime method caches. See
100 // http://crbug.com/172319 for details.
105 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
107 void* function_pointer = nullptr;
109 // Get the function pointer using the right API for the type.
110 if (library->type == BUNDLE) {
111 ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString(
112 kCFAllocatorDefault, name.data(), kCFStringEncodingUTF8));
113 function_pointer = CFBundleGetFunctionPointerForName(library->bundle,
116 function_pointer = dlsym(library->dylib, name.data());
119 // If this library hasn't been tested for having ObjC, use the function
120 // pointer to look up the section information for the library.
121 if (function_pointer && library->objc_status == OBJC_UNKNOWN)
122 library->objc_status = GetObjCStatusForImage(function_pointer);
124 return function_pointer;
127 std::string GetNativeLibraryName(StringPiece name) {
128 DCHECK(IsStringASCII(name));
129 return StrCat({"lib", name, ".dylib"});
132 std::string GetLoadableModuleName(StringPiece name) {
133 DCHECK(IsStringASCII(name));
134 return StrCat({name, ".so"});