Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
6.9KB

  1. //------------------------------------------------------------------------
  2. // Project : SDK Base
  3. // Version : 1.0
  4. //
  5. // Category : Helpers
  6. // Filename : base/source/fdynlib.cpp
  7. // Created by : Steinberg, 1998
  8. // Description : Dynamic library loading
  9. //
  10. //-----------------------------------------------------------------------------
  11. // LICENSE
  12. // (c) 2017, Steinberg Media Technologies GmbH, All Rights Reserved
  13. //-----------------------------------------------------------------------------
  14. // Redistribution and use in source and binary forms, with or without modification,
  15. // are permitted provided that the following conditions are met:
  16. //
  17. // * Redistributions of source code must retain the above copyright notice,
  18. // this list of conditions and the following disclaimer.
  19. // * Redistributions in binary form must reproduce the above copyright notice,
  20. // this list of conditions and the following disclaimer in the documentation
  21. // and/or other materials provided with the distribution.
  22. // * Neither the name of the Steinberg Media Technologies nor the names of its
  23. // contributors may be used to endorse or promote products derived from this
  24. // software without specific prior written permission.
  25. //
  26. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  27. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  29. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  30. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  34. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. // OF THE POSSIBILITY OF SUCH DAMAGE.
  36. //-----------------------------------------------------------------------------
  37. #include "base/source/fdynlib.h"
  38. #include "pluginterfaces/base/fstrdefs.h"
  39. #include "base/source/fstring.h"
  40. #if WINDOWS
  41. #include <windows.h>
  42. #elif MAC
  43. #include <mach-o/dyld.h>
  44. #include <CoreFoundation/CoreFoundation.h>
  45. #if !TARGET_OS_IPHONE
  46. static const Steinberg::tchar kUnixDelimiter = STR ('/');
  47. #endif
  48. #endif
  49. namespace Steinberg {
  50. #if MAC
  51. #include <dlfcn.h>
  52. // we ignore for the moment that the NSAddImage functions are deprecated
  53. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  54. static bool CopyProcessPath (Steinberg::String& name)
  55. {
  56. Dl_info info;
  57. if (dladdr ((const void*)CopyProcessPath, &info))
  58. {
  59. if (info.dli_fname)
  60. {
  61. name.assign (info.dli_fname);
  62. #ifdef UNICODE
  63. name.toWideString ();
  64. #endif
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. #endif
  71. //------------------------------------------------------------------------
  72. // FDynLibrary
  73. //------------------------------------------------------------------------
  74. FDynLibrary::FDynLibrary (const tchar* n, bool addExtension)
  75. : isloaded (false)
  76. , instance (0)
  77. {
  78. if (n)
  79. init (n, addExtension);
  80. }
  81. //------------------------------------------------------------------------
  82. FDynLibrary::~FDynLibrary ()
  83. {
  84. unload ();
  85. }
  86. //------------------------------------------------------------------------
  87. bool FDynLibrary::init (const tchar* n, bool addExtension)
  88. {
  89. if (isLoaded ())
  90. return true;
  91. Steinberg::String name (n);
  92. #if WINDOWS
  93. if (addExtension)
  94. name.append (STR (".dll"));
  95. instance = LoadLibrary (name);
  96. if (instance)
  97. isloaded = true;
  98. #elif MAC
  99. isBundle = false;
  100. // first check if it is a bundle
  101. if (addExtension)
  102. name.append (STR (".bundle"));
  103. if (name.getChar16 (0) != STR('/')) // no absoltue path
  104. {
  105. Steinberg::String p;
  106. if (CopyProcessPath (p))
  107. {
  108. Steinberg::int32 index = p.findLast (STR ('/'));
  109. p.remove (index+1);
  110. name = p + name;
  111. }
  112. }
  113. CFStringRef fsString = (CFStringRef)name.toCFStringRef ();
  114. CFURLRef url = CFURLCreateWithFileSystemPath (NULL, fsString, kCFURLPOSIXPathStyle, true);
  115. if (url)
  116. {
  117. CFBundleRef bundle = CFBundleCreate (NULL, url);
  118. if (bundle)
  119. {
  120. if (CFBundleLoadExecutable (bundle))
  121. {
  122. isBundle = true;
  123. instance = (void*)bundle;
  124. }
  125. else
  126. CFRelease (bundle);
  127. }
  128. CFRelease (url);
  129. }
  130. CFRelease (fsString);
  131. name.assign (n);
  132. #if !TARGET_OS_IPHONE
  133. if (!isBundle)
  134. {
  135. // now we check for a dynamic library
  136. firstSymbol = NULL;
  137. if (addExtension)
  138. {
  139. name.append (STR (".dylib"));
  140. }
  141. // Only if name is a relative path we use the Process Path as root:
  142. if (name[0] != kUnixDelimiter)
  143. {
  144. Steinberg::String p;
  145. if (CopyProcessPath (p))
  146. {
  147. Steinberg::int32 index = p.findLast (STR ("/"));
  148. p.remove (index+1);
  149. p.append (name);
  150. p.toMultiByte (Steinberg::kCP_Utf8);
  151. instance = (void*) NSAddImage (p, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
  152. }
  153. }
  154. // Last but not least let the system search for it
  155. //
  156. if (instance == 0)
  157. {
  158. name.toMultiByte (Steinberg::kCP_Utf8);
  159. instance = (void*) NSAddImage (name, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
  160. }
  161. }
  162. #endif // !TARGET_OS_IPHONE
  163. if (instance)
  164. isloaded = true;
  165. #endif
  166. return isloaded;
  167. }
  168. //------------------------------------------------------------------------
  169. bool FDynLibrary::unload ()
  170. {
  171. if (!isLoaded ())
  172. return false;
  173. #if WINDOWS
  174. FreeLibrary ((HINSTANCE)instance);
  175. #elif MAC
  176. if (isBundle)
  177. {
  178. if (CFGetRetainCount ((CFTypeRef)instance) == 1)
  179. CFBundleUnloadExecutable ((CFBundleRef)instance);
  180. CFRelease ((CFBundleRef)instance);
  181. }
  182. else
  183. {
  184. // we don't use this anymore as the darwin dyld can't unload dynamic libraries yet and may crash
  185. /* if (firstSymbol)
  186. {
  187. NSModule module = NSModuleForSymbol ((NSSymbol)firstSymbol);
  188. if (module)
  189. NSUnLinkModule (module, NSUNLINKMODULE_OPTION_NONE);
  190. }*/
  191. }
  192. #endif
  193. instance = 0;
  194. isloaded = false;
  195. return true;
  196. }
  197. //------------------------------------------------------------------------
  198. void* FDynLibrary::getProcAddress (const char* name)
  199. {
  200. if (!isloaded)
  201. return 0;
  202. #if WINDOWS
  203. return (void*)GetProcAddress ((HINSTANCE)instance, name);
  204. #elif MAC
  205. if (isBundle)
  206. {
  207. CFStringRef functionName = CFStringCreateWithCString (NULL, name, kCFStringEncodingASCII);
  208. void* result = CFBundleGetFunctionPointerForName ((CFBundleRef)instance, functionName);
  209. CFRelease (functionName);
  210. return result;
  211. }
  212. #if !TARGET_OS_IPHONE
  213. else
  214. {
  215. char* symbolName = (char*) malloc (strlen (name) + 2);
  216. strcpy (symbolName, "_");
  217. strcat (symbolName, name);
  218. NSSymbol symbol;
  219. symbol = NSLookupSymbolInImage ((const struct mach_header*)instance, symbolName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
  220. free (symbolName);
  221. if (symbol)
  222. {
  223. if (firstSymbol == NULL)
  224. firstSymbol = symbol;
  225. return NSAddressOfSymbol (symbol);
  226. }
  227. }
  228. #endif // !TARGET_OS_IPHONE
  229. #endif
  230. return 0;
  231. }
  232. //------------------------------------------------------------------------
  233. } // namespace Steinberg