1 /* GMODULE - GLIB wrapper code for dynamic module loading
2 * Copyright (C) 1998, 2000 Tim Janik
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 /* because we are compatible with archive format only since AIX 4.3 */
35 /* --- functions --- */
37 fetch_dlerror (gboolean replace_null)
39 gchar *msg = dlerror ();
41 /* make sure we always return an error message != NULL, if
42 * expected to do so. */
44 if (!msg && replace_null)
45 return "unknown dl-error";
50 static gchar* _g_module_get_member(const gchar* file_name)
53 struct fl_hdr file_header;
54 struct ar_hdr ar_header;
59 fd = open(file_name, O_RDONLY);
63 if (read(fd, (void*)&file_header, FL_HSZ) != FL_HSZ)
66 if (strncmp(file_header.fl_magic, AIAMAGBIG, SAIAMAG) != 0)
69 /* read first archive file member header */
71 first_member = atol(file_header.fl_fstmoff);
73 if (lseek(fd, first_member, SEEK_SET) != first_member)
76 if (read(fd, (void*)&ar_header, AR_HSZ - 2) != AR_HSZ - 2)
79 /* read member name */
81 name_len = atol(ar_header.ar_namlen);
83 member = g_malloc(name_len+1);
87 if (read(fd, (void*)member, name_len) != name_len)
103 _g_module_open (const gchar *file_name,
112 /* extract name of first member of archive */
114 member = _g_module_get_member (file_name);
117 full_name = g_strconcat (file_name, "(", member, ")", NULL);
121 full_name = g_strdup (file_name);
123 handle = dlopen (full_name,
124 (bind_local ? RTLD_LOCAL : RTLD_GLOBAL) | RTLD_MEMBER | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
130 const gchar *message = fetch_dlerror (TRUE);
132 g_module_set_error (message);
133 g_set_error_literal (error, G_MODULE_ERROR, G_MODULE_ERROR_FAILED, message);
140 _g_module_self (void)
144 handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
146 g_module_set_error (fetch_dlerror (TRUE));
152 _g_module_close (gpointer handle)
154 if (dlclose (handle) != 0)
155 g_module_set_error (fetch_dlerror (TRUE));
159 _g_module_symbol (gpointer handle,
160 const gchar *symbol_name)
164 p = dlsym (handle, symbol_name);
166 g_module_set_error (fetch_dlerror (FALSE));