Annotation of parser3/src/lib/ltdl/loaders/dld_link.c, revision 1.2
1.1 moko 1: /* loader-dld_link.c -- dynamic linking with dld
2:
1.2 ! moko 3: Copyright (C) 1998-2000, 2004, 2006-2008, 2011-2019, 2021-2024 Free
! 4: Software Foundation, Inc.
1.1 moko 5: Written by Thomas Tanner, 1998
6:
7: NOTE: The canonical source of this file is maintained with the
8: GNU Libtool package. Report bugs to bug-libtool@gnu.org.
9:
10: GNU Libltdl is free software; you can redistribute it and/or
11: modify it under the terms of the GNU Lesser General Public
12: License as published by the Free Software Foundation; either
13: version 2 of the License, or (at your option) any later version.
14:
15: As a special exception to the GNU Lesser General Public License,
16: if you distribute this file as part of a program or library that
17: is built using GNU Libtool, you may include this file under the
18: same distribution terms that you use for the rest of that program.
19:
20: GNU Libltdl is distributed in the hope that it will be useful,
21: but WITHOUT ANY WARRANTY; without even the implied warranty of
22: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23: GNU Lesser General Public License for more details.
24:
25: You should have received a copy of the GNU Lesser General Public
1.2 ! moko 26: License along with GNU Libltdl. If not, see <https://www.gnu.org/licenses/>.
1.1 moko 27: */
28:
29: #include "lt__private.h"
30: #include "lt_dlloader.h"
31:
32: /* Use the preprocessor to rename non-static symbols to avoid namespace
33: collisions when the loader code is statically linked into libltdl.
34: Use the "<module_name>_LTX_" prefix so that the symbol addresses can
35: be fetched from the preloaded symbol list by lt_dlsym(): */
36: #define get_vtable dld_link_LTX_get_vtable
37:
38: LT_BEGIN_C_DECLS
39: LT_SCOPE lt_dlvtable *get_vtable (lt_user_data loader_data);
40: LT_END_C_DECLS
41:
42:
43: /* Boilerplate code to set up the vtable for hooking this loader into
44: libltdl's loader list: */
45: static int vl_exit (lt_user_data loader_data);
46: static lt_module vm_open (lt_user_data loader_data, const char *filename,
47: lt_dladvise advise);
48: static int vm_close (lt_user_data loader_data, lt_module module);
49: static void * vm_sym (lt_user_data loader_data, lt_module module,
50: const char *symbolname);
51:
52: static lt_dlvtable *vtable = 0;
53:
54: /* Return the vtable for this loader, only the name and sym_prefix
55: attributes (plus the virtual function implementations, obviously)
56: change between loaders. */
57: lt_dlvtable *
58: get_vtable (lt_user_data loader_data)
59: {
60: if (!vtable)
61: {
62: vtable = lt__zalloc (sizeof *vtable);
63: }
64:
65: if (vtable && !vtable->name)
66: {
67: vtable->name = "lt_dld_link";
68: vtable->module_open = vm_open;
69: vtable->module_close = vm_close;
70: vtable->find_sym = vm_sym;
71: vtable->dlloader_exit = vl_exit;
72: vtable->dlloader_data = loader_data;
73: vtable->priority = LT_DLLOADER_APPEND;
74: }
75:
76: if (vtable && (vtable->dlloader_data != loader_data))
77: {
78: LT__SETERROR (INIT_LOADER);
79: return 0;
80: }
81:
82: return vtable;
83: }
84:
85:
86:
87: /* --- IMPLEMENTATION --- */
88:
89:
1.2 ! moko 90: #if defined HAVE_DLD_H
1.1 moko 91: # include <dld.h>
92: #endif
93:
94: /* A function called through the vtable when this loader is no
95: longer needed by the application. */
96: static int
1.2 ! moko 97: vl_exit (lt_user_data loader_data LT__UNUSED)
1.1 moko 98: {
99: vtable = NULL;
100: return 0;
101: }
102:
103: /* A function called through the vtable to open a module with this
104: loader. Returns an opaque representation of the newly opened
105: module for processing with this loader's other vtable functions. */
106: static lt_module
1.2 ! moko 107: vm_open (lt_user_data loader_data LT__UNUSED, const char *filename,
! 108: lt_dladvise advise LT__UNUSED)
1.1 moko 109: {
110: lt_module module = lt__strdup (filename);
111:
1.2 ! moko 112: if (module && dld_link (filename) != 0)
1.1 moko 113: {
114: LT__SETERROR (CANNOT_OPEN);
115: FREE (module);
116: }
117:
118: return module;
119: }
120:
121: /* A function called through the vtable when a particular module
122: should be unloaded. */
123: static int
1.2 ! moko 124: vm_close (lt_user_data loader_data LT__UNUSED, lt_module module)
1.1 moko 125: {
126: int errors = 0;
127:
128: if (dld_unlink_by_file ((char*)(module), 1) != 0)
129: {
130: LT__SETERROR (CANNOT_CLOSE);
131: ++errors;
132: }
133: else
134: {
135: FREE (module);
136: }
137:
138: return errors;
139: }
140:
141: /* A function called through the vtable to get the address of
142: a symbol loaded from a particular module. */
143: static void *
1.2 ! moko 144: vm_sym (lt_user_data loader_data LT__UNUSED, lt_module module LT__UNUSED,
1.1 moko 145: const char *name)
146: {
147: void *address = dld_get_func (name);
148:
149: if (!address)
150: {
151: LT__SETERROR (SYMBOL_NOT_FOUND);
152: }
153:
154: return address;
155: }
E-mail: