1 /* GMODULE - GLIB wrapper code for dynamic module loading
2 * Copyright (C) 1998, 2000 Tim Janik
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * MT safe
20 */
21
22 /* because we are compatible with archive format only since AIX 4.3 */
23
24 #define __AR_BIG__
25
26 #include "config.h"
27
28 #include <ar.h>
29 #include <stdlib.h>
30
31 #include <dlfcn.h>
32
33 /* --- functions --- */
34 static gchar*
fetch_dlerror(gboolean replace_null)35 fetch_dlerror (gboolean replace_null)
36 {
37 gchar *msg = dlerror ();
38
39 /* make sure we always return an error message != NULL, if
40 * expected to do so. */
41
42 if (!msg && replace_null)
43 return "unknown dl-error";
44
45 return msg;
46 }
47
_g_module_get_member(const gchar * file_name)48 static gchar* _g_module_get_member(const gchar* file_name)
49 {
50 gchar* member = NULL;
51 struct fl_hdr file_header;
52 struct ar_hdr ar_header;
53 long first_member;
54 long name_len;
55 int fd;
56
57 fd = open(file_name, O_RDONLY);
58 if (fd == -1)
59 return NULL;
60
61 if (read(fd, (void*)&file_header, FL_HSZ) != FL_HSZ)
62 goto exit;
63
64 if (strncmp(file_header.fl_magic, AIAMAGBIG, SAIAMAG) != 0)
65 goto exit;
66
67 /* read first archive file member header */
68
69 first_member = atol(file_header.fl_fstmoff);
70
71 if (lseek(fd, first_member, SEEK_SET) != first_member)
72 goto exit;
73
74 if (read(fd, (void*)&ar_header, AR_HSZ - 2) != AR_HSZ - 2)
75 goto exit;
76
77 /* read member name */
78
79 name_len = atol(ar_header.ar_namlen);
80
81 member = g_malloc(name_len+1);
82 if (!member)
83 goto exit;
84
85 if (read(fd, (void*)member, name_len) != name_len)
86 {
87 g_free(member);
88 member = NULL;
89 goto exit;
90 }
91
92 member[name_len] = 0;
93
94 exit:
95 close(fd);
96
97 return member;
98 }
99
100 static gpointer
_g_module_open(const gchar * file_name,gboolean bind_lazy,gboolean bind_local)101 _g_module_open (const gchar *file_name,
102 gboolean bind_lazy,
103 gboolean bind_local)
104 {
105 gpointer handle;
106 gchar* member;
107 gchar* full_name;
108
109 /* extract name of first member of archive */
110
111 member = _g_module_get_member (file_name);
112 if (member != NULL)
113 {
114 full_name = g_strconcat (file_name, "(", member, ")", NULL);
115 g_free (member);
116 }
117 else
118 full_name = g_strdup (file_name);
119
120 handle = dlopen (full_name,
121 (bind_local ? RTLD_LOCAL : RTLD_GLOBAL) | RTLD_MEMBER | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
122
123 g_free (full_name);
124
125 if (!handle)
126 g_module_set_error (fetch_dlerror (TRUE));
127
128 return handle;
129 }
130
131 static gpointer
_g_module_self(void)132 _g_module_self (void)
133 {
134 gpointer handle;
135
136 handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
137 if (!handle)
138 g_module_set_error (fetch_dlerror (TRUE));
139
140 return handle;
141 }
142
143 static void
_g_module_close(gpointer handle)144 _g_module_close (gpointer handle)
145 {
146 if (dlclose (handle) != 0)
147 g_module_set_error (fetch_dlerror (TRUE));
148 }
149
150 static gpointer
_g_module_symbol(gpointer handle,const gchar * symbol_name)151 _g_module_symbol (gpointer handle,
152 const gchar *symbol_name)
153 {
154 gpointer p;
155
156 p = dlsym (handle, symbol_name);
157 if (!p)
158 g_module_set_error (fetch_dlerror (FALSE));
159
160 return p;
161 }
162
163 static gchar*
_g_module_build_path(const gchar * directory,const gchar * module_name)164 _g_module_build_path (const gchar *directory,
165 const gchar *module_name)
166 {
167 if (directory && *directory) {
168 if (strncmp (module_name, "lib", 3) == 0)
169 return g_strconcat (directory, "/", module_name, NULL);
170 else
171 return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL);
172 } else if (strncmp (module_name, "lib", 3) == 0)
173 return g_strdup (module_name);
174 else
175 return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL);
176 }
177