]> code.delx.au - gnu-emacs/blob - src/dynlib.c
Style fixes for indenting etc. in module code
[gnu-emacs] / src / dynlib.c
1 /* Portable API for dynamic loading.
2
3 Copyright 2015 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 /* Assume modules are enabled on modern systems... *Yes*, the
22 preprocessor macro checks could be more precise. I don't care.
23
24 If you think the abstraction is too leaky use libltdl (libtool),
25 don't reinvent the wheel by fixing this one. */
26
27 #include "dynlib.h"
28
29 #if defined _WIN32
30
31 /* MS-Windows systems. */
32
33 #include <windows.h>
34
35 dynlib_handle_ptr
36 dynlib_open (const char *path)
37 {
38
39 return (dynlib_handle_ptr) LoadLibrary (path);
40 }
41
42 void *
43 dynlib_sym (dynlib_handle_ptr h, const char *sym)
44 {
45 return GetProcAddress ((HMODULE) h, sym);
46 }
47
48 bool
49 dynlib_addr (void *ptr, const char **path, const char **sym)
50 {
51 return false; /* not implemented */
52 }
53
54 const char *
55 dynlib_error (void)
56 {
57 /* TODO: use GetLastError(), FormatMessage(), ... */
58 return "Can't load DLL";
59 }
60
61 int
62 dynlib_close (dynlib_handle_ptr h)
63 {
64 return FreeLibrary ((HMODULE) h) != 0;
65 }
66
67 #elif defined HAVE_UNISTD_H
68
69 /* POSIX systems. */
70
71 #include <dlfcn.h>
72
73 dynlib_handle_ptr
74 dynlib_open (const char *path)
75 {
76 return dlopen (path, RTLD_LAZY);
77 }
78
79 void *
80 dynlib_sym (dynlib_handle_ptr h, const char *sym)
81 {
82 return dlsym (h, sym);
83 }
84
85 bool
86 dynlib_addr (void *ptr, const char **path, const char **sym)
87 {
88 #ifdef HAVE_DLADDR
89 Dl_info info;
90 if (dladdr (ptr, &info) && info.dli_fname && info.dli_sname)
91 {
92 *path = info.dli_fname;
93 *sym = info.dli_sname;
94 return true;
95 }
96 #endif
97 return false;
98 }
99
100 const char *
101 dynlib_error (void)
102 {
103 return dlerror ();
104 }
105
106 int
107 dynlib_close (dynlib_handle_ptr h)
108 {
109 return dlclose (h) == 0;
110 }
111
112 #else
113
114 #error "No dynamic loading for this system"
115
116 #endif