Debian Codegraph @

src/basic/dlfcn-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <dlfcn.h> /* IWYU pragma: export */
5
6 #include "basic-forward.h"
7
8 void* safe_dlclose(void *dl);
9
10 static inline void dlclosep(void **dlp) {
11 safe_dlclose(*dlp);
12 }
13
14 int dlopen_verbose(void **dlp, const char *filename, int log_level);
15 int dlsym_many_or_warn_sentinel(void *dl, int log_level, ...) _sentinel_;
16 int dlopen_many_sym_or_warn_sentinel(void **dlp, const char *filename, int log_level, ...) _sentinel_;
17
18 #define dlsym_many_or_warn(dl, log_level, ...) \
19 dlsym_many_or_warn_sentinel(dl, log_level, __VA_ARGS__, NULL)
20 #define dlopen_many_sym_or_warn(dlp, filename, log_level, ...) \
21 dlopen_many_sym_or_warn_sentinel(dlp, filename, log_level, __VA_ARGS__, NULL)
22
23 #define DLSYM_PROTOTYPE(symbol) \
24 typeof(symbol)* sym_##symbol
25
26 /* Macro useful for putting together variable/symbol name pairs when calling dlsym_many_or_warn(). Assumes
27 * that each library symbol to resolve will be placed in a variable with the "sym_" prefix, i.e. a symbol
28 * "foobar" is loaded into a variable "sym_foobar". */
29 #define DLSYM_ARG(arg) \
30 ({ assert_cc(__builtin_types_compatible_p(typeof(sym_##arg), typeof(&arg))); &sym_##arg; }), STRINGIFY(arg)
31
32 /* libbpf is a bit confused about type-safety and API compatibility. Provide a macro that can tape over that mess. Sad. */
33 #define DLSYM_ARG_FORCE(arg) \
34 &sym_##arg, STRINGIFY(arg)
35
36 /* Resolve a single optional symbol from an already-opened library handle. The pointer variable is expected
37 * to be named sym_<name> (same convention as DLSYM_ARG). Only assigns on success, so the pointer keeps its
38 * pre-existing value if the symbol is not present — useful for fallback initialization. dlerror() is
39 * cleared first so callers can distinguish "symbol not found" from "symbol's value is NULL" by checking
40 * dlerror() after; for function symbols (which can never be NULL on success) the _v check below is
41 * sufficient. */
42 #define DLSYM_OPTIONAL(dl, name) \
43 ({ \
44 (void) dlerror(); \
45 typeof(sym_##name) _v = (typeof(sym_##name)) dlsym((dl), #name); \
46 if (_v) sym_##name = _v; \
47 })
48
49 /* If called dlopen_many_sym_or_warn() will fail with EPERM. This can be used to block lazy loading of shared
50 * libs, if we transfer a process into a different namespace. Note that this does not work for all calls of
51 * dlopen(), just those through our dlopen_safe() wrapper (which we use comprehensively in our
52 * codebase). This hence has *no* effect on NSS. (Would be great if we could change that...) */
53 void block_dlopen(void);
54
55 int dlopen_safe(const char *filename, void **ret, const char **reterr_dlerror);