KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
library.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/library.h
4 Copyright (C) 2003 Megan Potter
5 Copyright (C) 2024 Ruslan Rostovtsev
6
7*/
8
9/** \file kos/library.h
10 \brief Dynamically loadable library support.
11 \ingroup system_libraries
12
13 This file contains definitions for accessing loadable libraries at runtime.
14 Each library has a name and a version number that it can be referenced by.
15 One must be careful with these dynamic libraries as there is no private
16 storage per instance, and all memory space is shared.
17
18 Libraries can both export and import symbols. Imported symbols may require
19 other libraries to be loaded earlier. Libraries are reference counted so
20 that they can be opened multiple times without actually loading them
21 multiple times, and so that a close will act as expected in situations like
22 this.
23
24 \author Megan Potter
25 \author Ruslan Rostovtsev
26*/
27
28#ifndef __KOS_LIBRARY_H
29#define __KOS_LIBRARY_H
30
31#include <sys/cdefs.h>
32__BEGIN_DECLS
33
34#include <kos/thread.h>
35#include <kos/elf.h>
36#include <kos/fs.h>
37
38/** \defgroup system_libraries Libraries
39 \brief API for managing dynamically loaded libraries
40 \ingroup system
41
42 @{
43*/
44
45/** \cond */
46/* Pre-define list/queue types */
47struct klibrary;
48TAILQ_HEAD(klqueue, klibrary);
49LIST_HEAD(kllist, klibrary);
50/** \endcond */
51
52/* Thread IDs are ok for us */
53typedef tid_t libid_t; /**< \brief Library ID type. */
54
55/** \brief Loaded library structure.
56
57 This structure represents a single loaded library. Each library is
58 essentially a loaded binary of code and a set of exported entry points that
59 are standardized.
60
61 Each loaded library should export at least the functions described in this
62 structure:
63 \li const char *lib_get_name()
64 \li uint32 %lib_get_version()
65 \li int lib_open(struct klibrary *lib)
66 \li int lib_close(struct klibrary *lib)
67
68 You should not modify any members of this structure yourself (except if you
69 are implementing a library).
70
71 \headerfile kos/library.h
72*/
73typedef struct klibrary {
74 /** \brief Library list handle.
75
76 Contrary to what doxygen might think, this is not a function.
77 */
78 LIST_ENTRY(klibrary) list;
79
80 /** \brief Library ID (assigned at runtime). */
82
83 /** \brief Library flags. */
84 uint32_t flags;
85
86 /** \brief ELF image for this library.
87
88 This can be used to look up additional entry points in the library.
89 */
91
92 /** \brief Library reference count.
93
94 This value is incremented every time the library is opened, and
95 decremented each time it is closed. Once the library's reference count
96 hits 0, a close will actually destroy the library.
97 */
98 int refcnt;
99
100 /* Standard library entry points. Every loaded library must provide
101 at least these things. */
102
103 /** \brief Retrieve the library's symbolic name.
104
105 This function must be implemented by all loadable libraries to fetch the
106 library's symbolic name. This function must work before calling
107 lib_open() on the library.
108
109 \return The library's symbolic name
110 */
111 const char *(*lib_get_name)(void);
112
113 /** \brief Retrieve the library's version.
114
115 This function must be implemented by all loadble libraries to fetch the
116 library's version number. This function must work before calling
117 lib_open() on the library.
118
119 \return The library's version number
120 */
121 uint32_t(*lib_get_version)(void);
122
123 /** \brief Open a library.
124
125 This function must be implemented by all loadable libraries to
126 initialize the library on load.
127
128 \param lib The library structure
129 \return Values >= 0 indicate success, < 0 indicates failure.
130 A failure on the first lib_open is indicative that
131 the library should be removed from memory.
132 */
133 int (*lib_open)(struct klibrary *lib);
134
135 /** \brief Close an opened library.
136
137 This function must be implemented by all loadable libraries to close and
138 deinitialize a library.
139
140 \param lib The library structure
141 \return Values >= 0 indicate success, < 0 indicates failure
142 */
143 int (*lib_close)(struct klibrary *lib);
144} klibrary_t;
145
146/* Library flag values */
147#define LIBRARY_DEFAULTS 0 /**< \brief Defaults: no flags */
148
149/** \cond */
150/* Library list; note: do not manipulate directly */
151extern struct kllist library_list;
152/** \endcond */
153
154/** \brief Look up a library by ID.
155
156 This function looks up a library by its library ID.
157
158 \param libid The library ID to look up
159 \return The specified library, or NULL if not found
160*/
162
163/** \brief Create a new library shell.
164
165 This function creates a new library, adding it to the list of libraries. You
166 generally should not call this function directly, unless you have some good
167 reason to do so.
168
169 \param flags Flags to create the library with.
170 \return The newly created library, or NULL on error
171*/
173
174/** \brief Destroy a library.
175
176 This function will take a loaded library and destroy it, unloading it
177 completely. Generally, you should not call this function, but rather use
178 library_close() to make sure that you're not closing something that is still
179 in use.
180
181 \param lib The library to close
182 \retval 0 Upon successfully destroying the library
183*/
185
186/** \brief Try to open a library by name.
187
188 This function attempts to open a library by its name. If it cannot be found
189 by name, this function will attempt to open by filename. If it cannot be found
190 by filename, this function will attempt to load the library from the specified
191 filename. If the library is already opened, this may only involve increasing
192 the reference count.
193
194 \param name The symbolic name of the library
195 \param fn The filename to load the library from
196 \return A handle to the library, or NULL on error with errno
197 set as appropriate
198
199 \par Error Conditions:
200 \em EINVAL - the library was found or loaded, but invalid \n
201 \em ENOMEM - out of memory \n
202 \em ENOENT - library not found and no filename given
203*/
204klibrary_t *library_open(const char *name, const char *fn);
205
206/** \brief Look up a library by name.
207
208 This is useful if you want to reuse opened library and
209 this is used for library_open().
210
211 \param name The name of the library to search for
212 \return The library, if found. NULL if not found, errno set
213 as appropriate.
214
215 \par Error Conditions:
216 \em ENOENT - the library was not found
217*/
218klibrary_t *library_lookup(const char *name);
219
220/** \brief Look up a library by filename.
221
222 This is useful if you want to reuse opened library and
223 this is used for library_open().
224
225 \param fn The filename of the library to search for
226 \return The library, if found. NULL if not found, errno set
227 as appropriate.
228
229 \par Error Conditions:
230 \em ENOENT - the library was not found
231*/
233
234/** \brief Close a previously opened library.
235
236 This function will close the specified library. This may involve simply
237 decrementing its reference count, however, it may also involve actually
238 closing and freeing the library. Thus, don't try to use the library after
239 calling this without reopening it first.
240
241 \param lib The library to close
242 \retval 0 On success
243 \retval -1 On error, errno may be set to an appropriate code
244
245 \par Error Conditions:
246 \em EINVAL - the library is not valid
247*/
249
250/** \brief Retrieve the specified library's runtime-assigned ID.
251 \param lib The library to examine
252 \return The library's ID, or -1 on error
253
254 \par Error Conditions:
255 \em EINVAL - the library is not valid
256*/
258
259/** \brief Retrieve the specified library's reference count.
260 \param lib The library to examine
261 \return The library's reference count, or -1 on error
262
263 \par Error Conditions:
264 \em EINVAL - the library is not valid
265*/
267
268/** \brief Retrieve the specified library's name.
269 \param lib The library to examine
270 \return The library's symbolic name, or NULL on error
271
272 \par Error Conditions:
273 \em EINVAL - the library is not valid
274*/
275const char *library_get_name(klibrary_t *lib);
276
277/** \brief Retrieve the specified library's version.
278 \param lib The library to examine
279 \return The library's version number, or 0 on error
280
281 \par Error Conditions
282 \em EINVAL - the library is not valid
283*/
285
286/** \cond */
287/* Init */
288int library_init(void);
289
290/* Shutdown */
291void library_shutdown(void);
292/** \endcond */
293
294/** @} */
295
296__END_DECLS
297
298#endif /* __KOS_LIBRARY_H */
ELF binary loading support.
Virtual filesystem support.
const char * library_get_name(klibrary_t *lib)
Retrieve the specified library's name.
klibrary_t * library_open(const char *name, const char *fn)
Try to open a library by name.
libid_t library_get_libid(klibrary_t *lib)
Retrieve the specified library's runtime-assigned ID.
int library_close(klibrary_t *lib)
Close a previously opened library.
klibrary_t * library_lookup_fn(const char *fn)
Look up a library by filename.
uint32_t library_get_version(klibrary_t *lib)
Retrieve the specified library's version.
klibrary_t * library_lookup(const char *name)
Look up a library by name.
klibrary_t * library_by_libid(libid_t libid)
Look up a library by ID.
int library_destroy(klibrary_t *lib)
Destroy a library.
tid_t libid_t
Library ID type.
Definition library.h:53
klibrary_t * library_create(int flags)
Create a new library shell.
int library_get_refcnt(klibrary_t *lib)
Retrieve the specified library's reference count.
typedef LIST_HEAD(nmmgr_list, nmmgr_handler) nmmgr_list_t
Name handler list type.
handle_t tid_t
Thread ID type.
Definition types.h:85
Kernel-specific definition of a loaded ELF binary.
Definition elf.h:304
Loaded library structure.
Definition library.h:73
elf_prog_t image
ELF image for this library.
Definition library.h:90
libid_t libid
Library ID (assigned at runtime).
Definition library.h:81
uint32_t flags
Library flags.
Definition library.h:84
int refcnt
Library reference count.
Definition library.h:98
LIST_ENTRY(klibrary) list
Library list handle.
Threading support.