dynlib.c 8.04 KB
Newer Older
1 2
/* Portable API for dynamic loading.

Paul Eggert's avatar
Paul Eggert committed
3
Copyright 2015-2020 Free Software Foundation, Inc.
4 5 6 7 8

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
11 12 13 14 15 16 17

GNU Emacs 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 General Public License for more details.

You should have received a copy of the GNU General Public License
18
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
19 20 21 22 23 24 25


/* Assume modules are enabled on modern systems...  *Yes*, the
   preprocessor macro checks could be more precise.  I don't care.

   If you think the abstraction is too leaky use libltdl (libtool),
   don't reinvent the wheel by fixing this one.  */
26

Paul Eggert's avatar
Paul Eggert committed
27 28
#include <config.h>

29 30
#include "dynlib.h"

31 32
#include <stddef.h>

33
#ifdef WINDOWSNT
34 35

/* MS-Windows systems.  */
36

37 38
#include <errno.h>
#include "lisp.h"
39
#include "w32common.h"	/* for os_subtype */
40 41
#include "w32.h"

42
static BOOL g_b_init_get_module_handle_ex;
43 44
static DWORD dynlib_last_err;

45 46 47 48 49 50 51 52 53 54
/* Some versions of w32api headers only expose the following when
   _WIN32_WINNT is set to higher values that we use.  */
typedef BOOL (WINAPI *GetModuleHandleExA_Proc) (DWORD,LPCSTR,HMODULE*);
#ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
# define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
#endif
#ifndef GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
# define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
#endif

55 56
/* This needs to be called at startup to countermand any non-zero
   values recorded by temacs.  */
57
void dynlib_reset_last_error (void);
58 59 60
void
dynlib_reset_last_error (void)
{
61
  g_b_init_get_module_handle_ex = 0;
62 63
  dynlib_last_err = 0;
}
64

65
dynlib_handle_ptr
66
dynlib_open (const char *dll_fname)
67
{
68 69
  HMODULE hdll;
  char dll_fname_local[MAX_UTF8_PATH];
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  if (!dll_fname)
    {
      errno = ENOTSUP;
      return NULL;
    }

  if (!dll_fname)
    hdll = GetModuleHandle (NULL);
  else
    {
      /* LoadLibrary wants backslashes.  */
      strcpy (dll_fname_local, dll_fname);
      unixtodos_filename (dll_fname_local);

      if (w32_unicode_filenames)
	{
	  wchar_t dll_fname_w[MAX_PATH];

	  filename_to_utf16 (dll_fname_local, dll_fname_w);
	  hdll = LoadLibraryW (dll_fname_w);
	}
      else
	{
	  char dll_fname_a[MAX_PATH];

	  filename_to_ansi (dll_fname_local, dll_fname_a);
	  hdll = LoadLibraryA (dll_fname_a);
	}
    }

  if (!hdll)
    dynlib_last_err = GetLastError ();

  return (dynlib_handle_ptr) hdll;
105 106
}

107 108
void *
dynlib_sym (dynlib_handle_ptr h, const char *sym)
109
{
110 111 112 113 114 115 116 117 118 119 120 121 122
  FARPROC sym_addr = NULL;

  if (!h || h == INVALID_HANDLE_VALUE || !sym)
    {
      dynlib_last_err = ERROR_INVALID_PARAMETER;
      return NULL;
    }

  sym_addr = GetProcAddress ((HMODULE) h, sym);
  if (!sym_addr)
    dynlib_last_err = GetLastError ();

  return (void *)sym_addr;
123 124
}

125
void
126
dynlib_addr (void (*funcptr) (void), const char **fname, const char **symname)
127
{
128 129 130 131 132 133 134
  static char dll_filename[MAX_UTF8_PATH];
  static GetModuleHandleExA_Proc s_pfn_Get_Module_HandleExA = NULL;
  char *dll_fn = NULL;
  HMODULE hm_kernel32 = NULL;
  HMODULE hm_dll = NULL;
  wchar_t mfn_w[MAX_PATH];
  char mfn_a[MAX_PATH];
135
  void *addr = (void *) funcptr;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

  /* Step 1: Find the handle of the module where ADDR lives.  */
  if (os_subtype == OS_9X
      /* Windows NT family version before XP (v5.1).  */
      || ((w32_major_version + (w32_minor_version > 0)) < 6))
    {
      MEMORY_BASIC_INFORMATION mbi;

      /* According to Matt Pietrek, the module handle is just the base
	 address where it's loaded in memory.  */
      if (VirtualQuery (addr, &mbi, sizeof(mbi)))
	hm_dll = (HMODULE)mbi.AllocationBase;
    }
  else
    {
      /* Use the documented API when available (XP and later).  */
      if (g_b_init_get_module_handle_ex == 0)
	{
	  g_b_init_get_module_handle_ex = 1;
	  hm_kernel32 = LoadLibrary ("kernel32.dll");
	  /* We load the ANSI version of the function because the
	     address we pass to it is not an address of a string, but
	     an address of a function.  So we don't care about the
	     Unicode version.  */
160 161
	  s_pfn_Get_Module_HandleExA = (GetModuleHandleExA_Proc)
            get_proc_addr (hm_kernel32, "GetModuleHandleExA");
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	}
      if (s_pfn_Get_Module_HandleExA)
	{
	  DWORD flags = (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
			 /* We don't want to call FreeLibrary at the
			    end, because then we'd need to remember
			    whether we obtained the handle by this
			    method or the above one.  */
			 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT);

	  if (!s_pfn_Get_Module_HandleExA (flags, addr, &hm_dll))
	    {
	      dynlib_last_err = GetLastError ();
	      hm_dll = NULL;
	    }
	}
    }

  /* Step 2: Find the absolute file name of the module corresponding
     to the hm_dll handle.  */
  if (hm_dll)
    {
      DWORD retval;

      if (w32_unicode_filenames)
	{
	  retval = GetModuleFileNameW (hm_dll, mfn_w, MAX_PATH);
	  if (retval > 0 && retval < MAX_PATH
	      && filename_from_utf16 (mfn_w, dll_filename) == 0)
	    dll_fn = dll_filename;
	  else if (retval == MAX_PATH)
	    dynlib_last_err = ERROR_INSUFFICIENT_BUFFER;
	  else
	    dynlib_last_err = GetLastError ();
	}
      else
	{
	  retval = GetModuleFileNameA (hm_dll, mfn_a, MAX_PATH);
	  if (retval > 0 && retval < MAX_PATH
	      && filename_from_ansi (mfn_a, dll_filename) == 0)
	    dll_fn = dll_filename;
	  else if (retval == MAX_PATH)
	    dynlib_last_err = ERROR_INSUFFICIENT_BUFFER;
	  else
	    dynlib_last_err = GetLastError ();
	}
      if (dll_fn)
209
        dostounix_filename (dll_fn);
210 211 212
    }

  *fname = dll_fn;
213 214 215 216 217

  /* We cannot easily produce the function name, since typically all
     of the module functions will be unexported, and probably even
     static, which means the symbols can be obtained only if we link
     against libbfd (and the DLL can be stripped anyway).  So we just
218 219 220
     show the address and the file name (see print_vectorlike in
     print.c); they can use that with addr2line or GDB to recover the
     symbolic name.  */
221
  *symname = NULL;
222 223
}

224 225
const char *
dynlib_error (void)
226
{
227 228 229 230 231 232 233 234 235
  char *error_string = NULL;

  if (dynlib_last_err)
    {
      error_string = w32_strerror (dynlib_last_err);
      dynlib_last_err = 0;
    }

  return error_string;
236 237
}

238 239
int
dynlib_close (dynlib_handle_ptr h)
240
{
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  if (!h || h == INVALID_HANDLE_VALUE)
    {
      dynlib_last_err = ERROR_INVALID_PARAMETER;
      return -1;
    }
  /* If the handle is for the main module (the .exe file), it
     shouldn't be passed to FreeLibrary, because GetModuleHandle
     doesn't increment the refcount, but FreeLibrary does decrement
     it.  I don't think this should matter for the main module, but
     just in case, we avoid the call here, relying on another call to
     GetModuleHandle to return the same value.  */
  if (h == GetModuleHandle (NULL))
    return 0;

  if (!FreeLibrary ((HMODULE) h))
    {
      dynlib_last_err = GetLastError ();
      return -1;
    }

  return 0;
262 263
}

264
#elif defined HAVE_UNISTD_H
265

266
/* POSIX systems.  */
267 268 269

#include <dlfcn.h>

270 271
dynlib_handle_ptr
dynlib_open (const char *path)
272 273 274 275
{
  return dlopen (path, RTLD_LAZY);
}

276 277
void *
dynlib_sym (dynlib_handle_ptr h, const char *sym)
278 279 280 281
{
  return dlsym (h, sym);
}

282
void
283
dynlib_addr (void (*funcptr) (void), const char **path, const char **sym)
284
{
285 286
  *path = NULL;
  *sym = NULL;
287
#ifdef HAVE_DLADDR
288
  void *ptr = (void *) funcptr;
289
  Dl_info info;
290
  if (dladdr (ptr, &info) && info.dli_fname && info.dli_sname)
291 292 293 294 295 296 297
    {
      *path = info.dli_fname;
      *sym = info.dli_sname;
    }
#endif
}

298 299
const char *
dynlib_error (void)
300 301 302 303
{
  return dlerror ();
}

304 305 306
/* FIXME: Currently there is no way to unload a module, so this
   function is never used.  */
#if false
307 308
int
dynlib_close (dynlib_handle_ptr h)
309 310 311
{
  return dlclose (h) == 0;
}
312
#endif
313 314 315 316 317 318

#else

#error "No dynamic loading for this system"

#endif
319 320 321 322 323 324 325 326 327 328

#if !HAVE_DLFUNC
# define dlfunc dynlib_sym
#endif

dynlib_function_ptr
dynlib_func (dynlib_handle_ptr h, const char *sym)
{
  return (dynlib_function_ptr) dlfunc (h, sym);
}