123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /* Definitions for dynamic library loading.
- This file is part of khipu.
- khipu is free software: you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public License
- along with this program. If not, see <https://www.gnu.org/licenses/>. */
- #include "dynlib.hpp"
- #include "interp.hpp"
- #ifdef KP_STATIC
- KP_DECLS_BEGIN
- void* dynlib_open (interpreter *, const char *, uint32_t, uint32_t)
- {
- return (nullptr);
- }
- void* dynlib_sym (void *, const char *)
- {
- return (nullptr);
- }
- void dynlib_close (void *)
- {
- }
- const char* dynlib_extension ()
- {
- return ("");
- }
- #elif defined (KP_PLATFORM_WINDOWS)
- # define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- KP_DECLS_BEGIN
- static void*
- win32_open (const char *src, int len, void *space)
- {
- wchar_t *outp = (wchar_t *)space;
- if (MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS,
- src, len, outp, INT_MAX))
- return (LoadLibraryW (outp));
- else
- return (nullptr);
- }
- void* dynlib_open (interpreter *interp,
- const char *path, uint32_t len, uint32_t flags)
- {
- if (!path)
- return (GetModuleHandle (nullptr));
- tmp_allocator ta { interp };
- void *space = nullptr;
- const char *mpath = path;
- if ((flags & DYNLIB_APPEXT) && path != nullptr &&
- len > 4 && memcmp (path + len - 4, ".dll", 4) != 0)
- {
- space = ta.alloc (4 * (len + 5));
- mpath = (const char *)memcpy (space, path, len);
- memcpy ((char *)space + len, ".dll", 5);
- space = (char *)space + len + 5;
- len += 4;
- }
- else
- space = ta.alloc (len * 4);
- return (win32_open (mpath, len, space));
- }
- void* dynlib_sym (void *dynlib, const char *symbol)
- {
- return ((void *)GetProcAddress ((HMODULE)dynlib, symbol));
- }
- void dynlib_close (void *dynlib)
- {
- if (dynlib != GetModuleHandle (nullptr))
- FreeLibrary ((HMODULE)dynlib);
- }
- const char* dynlib_extension ()
- {
- return (".dll");
- }
- #else
- #include <dlfcn.h>
- KP_DECLS_BEGIN
- void* dynlib_open (interpreter *interp,
- const char *path, uint32_t len, uint32_t flags)
- {
- tmp_allocator ta { interp };
- const char *mpath = path;
- if ((flags & DYNLIB_APPEXT) && path != nullptr &&
- len > 3 && memcmp (path + len - 3, ".so", 3) != 0)
- {
- void *space = ta.alloc (len + 4);
- mpath = (const char *)memcpy (space, path, len);
- memcpy ((char *)path + len, ".so", 3);
- }
- int rf = RTLD_NOW | ((flags & DYNLIB_LOCAL) ? RTLD_LOCAL : RTLD_GLOBAL);
- return (dlopen (mpath, rf));
- }
- void* dynlib_sym (void *dynlib, const char *symbol)
- {
- return (dlsym (dynlib, symbol));
- }
- void dynlib_close (void *dynlib)
- {
- dlclose (dynlib);
- }
- const char* dynlib_extension ()
- {
- return (".so");
- }
- #endif
- void* dynlib_open (interpreter *interp, const char *path, uint32_t flags)
- {
- return (dynlib_open (interp, path, path ? strlen (path) : 0, flags));
- }
- KP_DECLS_END
|