native_library_mac.mm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "base/native_library.h"
  5. #include <dlfcn.h>
  6. #include <mach-o/getsect.h>
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/logging.h"
  10. #include "base/mac/scoped_cftyperef.h"
  11. #include "base/strings/strcat.h"
  12. #include "base/strings/string_piece.h"
  13. #include "base/strings/string_util.h"
  14. #include "base/strings/utf_string_conversions.h"
  15. #include "base/threading/thread_restrictions.h"
  16. namespace base {
  17. static NativeLibraryObjCStatus GetObjCStatusForImage(
  18. const void* function_pointer) {
  19. Dl_info info;
  20. if (!dladdr(function_pointer, &info))
  21. return OBJC_UNKNOWN;
  22. // See if the image contains an "ObjC image info" segment. This method
  23. // of testing is used in _CFBundleGrokObjcImageInfoFromFile in
  24. // CF-744/CFBundle.c, around lines 2447-2474.
  25. //
  26. // In 64-bit images, ObjC can be recognized in __DATA,__objc_imageinfo.
  27. const section_64* section = getsectbynamefromheader_64(
  28. reinterpret_cast<const struct mach_header_64*>(info.dli_fbase), SEG_DATA,
  29. "__objc_imageinfo");
  30. if (section)
  31. return OBJC_PRESENT;
  32. // ....except when "SharedRegionEncodingV2" is on, it's in
  33. // __DATA_CONST,__objc_image_info (see https://crbug.com/1220459#c16)
  34. section = getsectbynamefromheader_64(
  35. reinterpret_cast<const struct mach_header_64*>(info.dli_fbase),
  36. "__DATA_CONST", "__objc_imageinfo");
  37. return section ? OBJC_PRESENT : OBJC_NOT_PRESENT;
  38. }
  39. std::string NativeLibraryLoadError::ToString() const {
  40. return message;
  41. }
  42. NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
  43. const NativeLibraryOptions& options,
  44. NativeLibraryLoadError* error) {
  45. // dlopen() etc. open the file off disk.
  46. if (library_path.Extension() == "dylib" || !DirectoryExists(library_path)) {
  47. void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY);
  48. if (!dylib) {
  49. if (error)
  50. error->message = dlerror();
  51. return nullptr;
  52. }
  53. NativeLibrary native_lib = new NativeLibraryStruct();
  54. native_lib->type = DYNAMIC_LIB;
  55. native_lib->dylib = dylib;
  56. native_lib->objc_status = OBJC_UNKNOWN;
  57. return native_lib;
  58. }
  59. ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
  60. kCFAllocatorDefault, (const UInt8*)library_path.value().c_str(),
  61. checked_cast<CFIndex>(library_path.value().length()), true));
  62. if (!url)
  63. return nullptr;
  64. CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
  65. if (!bundle)
  66. return nullptr;
  67. NativeLibrary native_lib = new NativeLibraryStruct();
  68. native_lib->type = BUNDLE;
  69. native_lib->bundle = bundle;
  70. native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle);
  71. native_lib->objc_status = OBJC_UNKNOWN;
  72. return native_lib;
  73. }
  74. void UnloadNativeLibrary(NativeLibrary library) {
  75. if (library->objc_status == OBJC_NOT_PRESENT) {
  76. if (library->type == BUNDLE) {
  77. CFBundleCloseBundleResourceMap(library->bundle,
  78. library->bundle_resource_ref);
  79. CFRelease(library->bundle);
  80. } else {
  81. dlclose(library->dylib);
  82. }
  83. } else {
  84. VLOG(2) << "Not unloading NativeLibrary because it may contain an ObjC "
  85. "segment. library->objc_status = " << library->objc_status;
  86. // Deliberately do not CFRelease the bundle or dlclose the dylib because
  87. // doing so can corrupt the ObjC runtime method caches. See
  88. // http://crbug.com/172319 for details.
  89. }
  90. delete library;
  91. }
  92. void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
  93. StringPiece name) {
  94. void* function_pointer = nullptr;
  95. // Get the function pointer using the right API for the type.
  96. if (library->type == BUNDLE) {
  97. ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString(
  98. kCFAllocatorDefault, name.data(), kCFStringEncodingUTF8));
  99. function_pointer = CFBundleGetFunctionPointerForName(library->bundle,
  100. symbol_name);
  101. } else {
  102. function_pointer = dlsym(library->dylib, name.data());
  103. }
  104. // If this library hasn't been tested for having ObjC, use the function
  105. // pointer to look up the section information for the library.
  106. if (function_pointer && library->objc_status == OBJC_UNKNOWN)
  107. library->objc_status = GetObjCStatusForImage(function_pointer);
  108. return function_pointer;
  109. }
  110. std::string GetNativeLibraryName(StringPiece name) {
  111. DCHECK(IsStringASCII(name));
  112. return StrCat({"lib", name, ".dylib"});
  113. }
  114. std::string GetLoadableModuleName(StringPiece name) {
  115. DCHECK(IsStringASCII(name));
  116. return StrCat({name, ".so"});
  117. }
  118. } // namespace base