[M94 Dev][Tizen] Fix for errors for generating ninja files
[platform/framework/web/chromium-efl.git] / base / native_library_mac.mm
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.
4
5 #include "base/native_library.h"
6
7 #include <dlfcn.h>
8 #include <mach-o/getsect.h>
9
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"
19
20 namespace base {
21
22 static NativeLibraryObjCStatus GetObjCStatusForImage(
23     const void* function_pointer) {
24   Dl_info info;
25   if (!dladdr(function_pointer, &info))
26     return OBJC_UNKNOWN;
27
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.
31   //
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,
35       "__objc_imageinfo");
36   if (section)
37     return OBJC_PRESENT;
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;
44 }
45
46 std::string NativeLibraryLoadError::ToString() const {
47   return message;
48 }
49
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);
56     if (!dylib) {
57       if (error)
58         error->message = dlerror();
59       return nullptr;
60     }
61     NativeLibrary native_lib = new NativeLibraryStruct();
62     native_lib->type = DYNAMIC_LIB;
63     native_lib->dylib = dylib;
64     native_lib->objc_status = OBJC_UNKNOWN;
65     return native_lib;
66   }
67   ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
68       kCFAllocatorDefault,
69       (const UInt8*)library_path.value().c_str(),
70       library_path.value().length(),
71       true));
72   if (!url)
73     return nullptr;
74   CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
75   if (!bundle)
76     return nullptr;
77
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;
83   return native_lib;
84 }
85
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);
92     } else {
93       dlclose(library->dylib);
94     }
95   } else {
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.
101   }
102   delete library;
103 }
104
105 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
106                                           StringPiece name) {
107   void* function_pointer = nullptr;
108
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,
114                                                          symbol_name);
115   } else {
116     function_pointer = dlsym(library->dylib, name.data());
117   }
118
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);
123
124   return function_pointer;
125 }
126
127 std::string GetNativeLibraryName(StringPiece name) {
128   DCHECK(IsStringASCII(name));
129   return StrCat({"lib", name, ".dylib"});
130 }
131
132 std::string GetLoadableModuleName(StringPiece name) {
133   DCHECK(IsStringASCII(name));
134   return StrCat({name, ".so"});
135 }
136
137 }  // namespace base