1 /*
2  * Copyright (c) 2017-2021 Free Software Foundation, Inc.
3  *
4  * This file is part of Wget.
5  *
6  * Wget is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Wget 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Wget.  If not, see <https://www.gnu.org/licenses/>.
18  *
19  *
20  * Dynamic loading abstraction for object files
21  *
22  */
23 
24 #ifndef SRC_WGET_DL_H
25 #define SRC_WGET_DL_H
26 
27 #include <wget.h>
28 
29 // Error handling for dynamic loader
30 typedef struct
31 {
32 	const char *msg;
33 } dl_error_t;
34 
35 // Initializes the error object for catching errors
dl_error_init(dl_error_t * e)36 static inline void dl_error_init(dl_error_t *e)
37 {
38 	e->msg = NULL;
39 }
40 
41 // Checks if the error is set
dl_error_is_set(dl_error_t * e)42 static inline int dl_error_is_set(dl_error_t *e)
43 {
44 	return e->msg ? 1 : 0;
45 }
46 
47 // Gets the error message if error is set, else NULL
48 // Error string is owned by the error object and will be freed when error is
49 // unset.
dl_error_get_msg(dl_error_t * e)50 static inline const char *dl_error_get_msg(dl_error_t *e)
51 {
52 	return e->msg;
53 }
54 
55 // Set an error message. Call with msg=NULL to clear error.
56 void dl_error_set(dl_error_t *e, const char *msg);
57 
58 // Set an error message with printf format.
59 void dl_error_set_printf
60 	(dl_error_t *e, const char *format, ...) WGET_GCC_PRINTF_FORMAT(2, 3);
61 
62 // Returns 1 if dynamic loader will work on the current platform, 0 otherwise
63 int dl_supported(void);
64 
65 // The dynamically loaded object file handle
66 typedef struct dl_file_st dl_file_t;
67 
68 /* Opens an object file. If the operation fails NULL is returned
69  * and error is set.
70  */
71 dl_file_t *dl_file_open(const char *filename, dl_error_t *e);
72 
73 /* Looks up a symbol in the loaded object file.
74  * On success it returns a pointer to the symbol,
75  * else it returns NULL and sets an error.
76  */
77 void *dl_file_lookup(dl_file_t *dm, const char *symbol, dl_error_t *e);
78 
79 // Unloads the loaded object file
80 void dl_file_close(dl_file_t *dm);
81 
82 /* Builds a module name given a path to the object file.
83  * Returns NULL if path does not match the pattern for object files and
84  * strict is set to 1.
85  * Free the returned string with wget_free().
86  */
87 char *dl_get_name_from_path(const char *path, int strict);
88 
89 /* Searches for an object file with a given name in the given list of
90  * directories. If found it returns the filename, else returns NULL.
91  * Free the returned string with wget_free().
92  */
93 char *dl_search(const char *name, const wget_vector *dirs);
94 
95 /* Creates a list of loadable object files in a given list of directories.
96  */
97 void dl_list(const wget_vector *dirs, wget_vector *names_out);
98 
99 #endif /* SRC_WGET_DL_H */
100