summaryrefslogtreecommitdiff
path: root/src/dynlib.c
blob: a41bed847bb822221b068385169b416397857edb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
209
210
211
212
213
214
215
216
217
218
/* Portable API for dynamic loading.

Copyright 2015 Free Software Foundation, Inc.

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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

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
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */


/* 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.  */

#include <config.h>

#include "dynlib.h"

#ifdef WINDOWSNT

/* MS-Windows systems.  */

#include <errno.h>
#include "lisp.h"
#include "w32.h"

static DWORD dynlib_last_err;

/* This needs to be called at startup to countermand any non-zero
   values recorded by temacs.  */
void
dynlib_reset_last_error (void)
{
  dynlib_last_err = 0;
}

dynlib_handle_ptr
dynlib_open (const char *dll_fname)
{
  HMODULE hdll;
  char dll_fname_local[MAX_UTF8_PATH];

  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;
}

void *
dynlib_sym (dynlib_handle_ptr h, const char *sym)
{
  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;
}

bool
dynlib_addr (void *ptr, const char **path, const char **sym)
{
  return false;  /* Not implemented yet.  */
}

const char *
dynlib_error (void)
{
  char *error_string = NULL;

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

  return error_string;
}

int
dynlib_close (dynlib_handle_ptr h)
{
  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;
}

#elif defined HAVE_UNISTD_H

/* POSIX systems.  */

#include <dlfcn.h>

dynlib_handle_ptr
dynlib_open (const char *path)
{
  return dlopen (path, RTLD_LAZY);
}

void *
dynlib_sym (dynlib_handle_ptr h, const char *sym)
{
  return dlsym (h, sym);
}

bool
dynlib_addr (void *ptr, const char **path, const char **sym)
{
#ifdef HAVE_DLADDR
  Dl_info info;
  if (dladdr (ptr, &info) && info.dli_fname && info.dli_sname)
    {
      *path = info.dli_fname;
      *sym = info.dli_sname;
      return true;
    }
#endif
  return false;
}

const char *
dynlib_error (void)
{
  return dlerror ();
}

/* FIXME: Currently there is no way to unload a module, so this
   function is never used.  */
#if false
int
dynlib_close (dynlib_handle_ptr h)
{
  return dlclose (h) == 0;
}
#endif

#else

#error "No dynamic loading for this system"

#endif

#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);
}