dynlib.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Definitions for dynamic library loading.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include "dynlib.hpp"
  14. #include "interp.hpp"
  15. #ifdef KP_STATIC
  16. KP_DECLS_BEGIN
  17. void* dynlib_open (interpreter *, const char *, uint32_t, uint32_t)
  18. {
  19. return (nullptr);
  20. }
  21. void* dynlib_sym (void *, const char *)
  22. {
  23. return (nullptr);
  24. }
  25. void dynlib_close (void *)
  26. {
  27. }
  28. const char* dynlib_extension ()
  29. {
  30. return ("");
  31. }
  32. #elif defined (KP_PLATFORM_WINDOWS)
  33. # define WIN32_LEAN_AND_MEAN
  34. #include <windows.h>
  35. KP_DECLS_BEGIN
  36. static void*
  37. win32_open (const char *src, int len, void *space)
  38. {
  39. wchar_t *outp = (wchar_t *)space;
  40. if (MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS,
  41. src, len, outp, INT_MAX))
  42. return (LoadLibraryW (outp));
  43. else
  44. return (nullptr);
  45. }
  46. void* dynlib_open (interpreter *interp,
  47. const char *path, uint32_t len, uint32_t flags)
  48. {
  49. if (!path)
  50. return (GetModuleHandle (nullptr));
  51. tmp_allocator ta { interp };
  52. void *space = nullptr;
  53. const char *mpath = path;
  54. if ((flags & DYNLIB_APPEXT) && path != nullptr &&
  55. len > 4 && memcmp (path + len - 4, ".dll", 4) != 0)
  56. {
  57. space = ta.alloc (4 * (len + 5));
  58. mpath = (const char *)memcpy (space, path, len);
  59. memcpy ((char *)space + len, ".dll", 5);
  60. space = (char *)space + len + 5;
  61. len += 4;
  62. }
  63. else
  64. space = ta.alloc (len * 4);
  65. return (win32_open (mpath, len, space));
  66. }
  67. void* dynlib_sym (void *dynlib, const char *symbol)
  68. {
  69. return ((void *)GetProcAddress ((HMODULE)dynlib, symbol));
  70. }
  71. void dynlib_close (void *dynlib)
  72. {
  73. if (dynlib != GetModuleHandle (nullptr))
  74. FreeLibrary ((HMODULE)dynlib);
  75. }
  76. const char* dynlib_extension ()
  77. {
  78. return (".dll");
  79. }
  80. #else
  81. #include <dlfcn.h>
  82. KP_DECLS_BEGIN
  83. void* dynlib_open (interpreter *interp,
  84. const char *path, uint32_t len, uint32_t flags)
  85. {
  86. tmp_allocator ta { interp };
  87. const char *mpath = path;
  88. if ((flags & DYNLIB_APPEXT) && path != nullptr &&
  89. len > 3 && memcmp (path + len - 3, ".so", 3) != 0)
  90. {
  91. void *space = ta.alloc (len + 4);
  92. mpath = (const char *)memcpy (space, path, len);
  93. memcpy ((char *)path + len, ".so", 3);
  94. }
  95. int rf = RTLD_NOW | ((flags & DYNLIB_LOCAL) ? RTLD_LOCAL : RTLD_GLOBAL);
  96. return (dlopen (mpath, rf));
  97. }
  98. void* dynlib_sym (void *dynlib, const char *symbol)
  99. {
  100. return (dlsym (dynlib, symbol));
  101. }
  102. void dynlib_close (void *dynlib)
  103. {
  104. dlclose (dynlib);
  105. }
  106. const char* dynlib_extension ()
  107. {
  108. return (".so");
  109. }
  110. #endif
  111. void* dynlib_open (interpreter *interp, const char *path, uint32_t flags)
  112. {
  113. return (dynlib_open (interp, path, path ? strlen (path) : 0, flags));
  114. }
  115. KP_DECLS_END