SDL 3.0
SDL_dlopennote.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/* WIKI CATEGORY: DlopenNotes */
23
24/**
25 * # CategoryDlopenNotes
26 *
27 * This header allows you to annotate your code so external tools know about
28 * dynamic shared library dependencies.
29 *
30 * If you determine that your toolchain doesn't support dlopen notes, you can
31 * disable this feature by defining `SDL_DISABLE_DLOPEN_NOTES`. You can use
32 * this CMake snippet to check for support:
33 *
34 * ```cmake
35 * set(CHECK_ELF_DLNOTES_SRC [==[
36 * #ifndef __ELF__
37 * ELF DL notes is only supported on ELF platforms
38 * #endif
39 * __attribute__ ((used,aligned(4),section(".note.dlopen"))) static const struct {
40 * struct { int a; int b; int c; } hdr; char name[4]; __attribute__((aligned(4))) char json[24];
41 * } dlnote = { { 4, 0x407c0c0aU, 16 }, "FDO", "[\\"a\\":{\\"a\\":\\"1\\",\\"b\\":\\"2\\"}]" };
42 * int main(int argc, char *argv[]) {
43 * return argc + dlnote.hdr.a;
44 * }
45 * ]==])
46 * check_c_source_compiles("${CHECK_ELF_DLNOTES_SRC}" COMPILER_SUPPORTS_ELFNOTES)
47 * if(NOT COMPILER_SUPPORTS_ELFNOTES)
48 * set(SDL_DISABLE_DLOPEN_NOTES TRUE)
49 * endif()
50 * ```
51 */
52
53#ifndef SDL_dlopennote_h
54#define SDL_dlopennote_h
55
56/**
57 * Use this macro with SDL_ELF_NOTE_DLOPEN() to note that a dynamic shared
58 * library dependency is optional.
59 *
60 * Optional functionality uses the dependency, the binary will work and the
61 * dependency is only needed for full-featured installations.
62 *
63 * \since This macro is available since SDL 3.4.0.
64 *
65 * \sa SDL_ELF_NOTE_DLOPEN
66 * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED
67 * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
68 */
69#define SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED "suggested"
70
71/**
72 * Use this macro with SDL_ELF_NOTE_DLOPEN() to note that a dynamic shared
73 * library dependency is recommended.
74 *
75 * Important functionality needs the dependency, the binary will work but in
76 * most cases the dependency should be provided.
77 *
78 * \since This macro is available since SDL 3.4.0.
79 *
80 * \sa SDL_ELF_NOTE_DLOPEN
81 * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED
82 * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
83 */
84#define SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED "recommended"
85
86/**
87 * Use this macro with SDL_ELF_NOTE_DLOPEN() to note that a dynamic shared
88 * library dependency is required.
89 *
90 * Core functionality needs the dependency, the binary will not work if it
91 * cannot be found.
92 *
93 * \since This macro is available since SDL 3.4.0.
94 *
95 * \sa SDL_ELF_NOTE_DLOPEN
96 * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED
97 * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED
98 */
99#define SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED "required"
100
101
102#if !defined(SDL_PLATFORM_UNIX) || defined(SDL_PLATFORM_ANDROID)
103/* The dlopen note functionality isn't used on this platform */
104#ifndef SDL_DISABLE_DLOPEN_NOTES
105#define SDL_DISABLE_DLOPEN_NOTES
106#endif
107#elif defined(__GNUC__) && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1))
108/* gcc < 3.1 too old */
109#ifndef SDL_DISABLE_DLOPEN_NOTES
110#define SDL_DISABLE_DLOPEN_NOTES
111#endif
112#endif /* SDL_PLATFORM_UNIX || SDL_PLATFORM_ANDROID */
113
114#if defined(__ELF__) && !defined(SDL_DISABLE_DLOPEN_NOTES)
115
116#include <SDL3/SDL_stdinc.h>
117
118#define SDL_ELF_NOTE_DLOPEN_VENDOR "FDO"
119#define SDL_ELF_NOTE_DLOPEN_TYPE 0x407c0c0aU
120
121#define SDL_ELF_NOTE_INTERNAL2(json, variable_name) \
122 __attribute__((aligned(4), used, section(".note.dlopen"))) \
123 static const struct { \
124 struct { \
125 Uint32 n_namesz; \
126 Uint32 n_descsz; \
127 Uint32 n_type; \
128 } nhdr; \
129 char name[4]; \
130 __attribute__((aligned(4))) char dlopen_json[sizeof(json)]; \
131 } variable_name = { \
132 { \
133 sizeof(SDL_ELF_NOTE_DLOPEN_VENDOR), \
134 sizeof(json), \
135 SDL_ELF_NOTE_DLOPEN_TYPE \
136 }, \
137 SDL_ELF_NOTE_DLOPEN_VENDOR, \
138 json \
139 }
140
141#define SDL_ELF_NOTE_INTERNAL(json, variable_name) \
142 SDL_ELF_NOTE_INTERNAL2(json, variable_name)
143
144#define SDL_SONAME_ARRAY1(N1) "[\"" N1 "\"]"
145#define SDL_SONAME_ARRAY2(N1,N2) "[\"" N1 "\",\"" N2 "\"]"
146#define SDL_SONAME_ARRAY3(N1,N2,N3) "[\"" N1 "\",\"" N2 "\",\"" N3 "\"]"
147#define SDL_SONAME_ARRAY4(N1,N2,N3,N4) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\"]"
148#define SDL_SONAME_ARRAY5(N1,N2,N3,N4,N5) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\"]"
149#define SDL_SONAME_ARRAY6(N1,N2,N3,N4,N5,N6) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\",\"" N6 "\"]"
150#define SDL_SONAME_ARRAY7(N1,N2,N3,N4,N5,N6,N7) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\",\"" N6 "\",\"" N7 "\"]"
151#define SDL_SONAME_ARRAY8(N1,N2,N3,N4,N5,N6,N7,N8) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\",\"" N6 "\",\"" N7 "\",\"" N8 "\"]"
152#define SDL_SONAME_ARRAY_GET(N1,N2,N3,N4,N5,N6,N7,N8,NAME,...) NAME
153#define SDL_SONAME_ARRAY(...) \
154 SDL_SONAME_ARRAY_GET(__VA_ARGS__, \
155 SDL_SONAME_ARRAY8, \
156 SDL_SONAME_ARRAY7, \
157 SDL_SONAME_ARRAY6, \
158 SDL_SONAME_ARRAY5, \
159 SDL_SONAME_ARRAY4, \
160 SDL_SONAME_ARRAY3, \
161 SDL_SONAME_ARRAY2, \
162 SDL_SONAME_ARRAY1 \
163 )(__VA_ARGS__)
164
165/* Create "unique" variable name using __LINE__,
166 * so creating elf notes on the same line is not supported
167 */
168#define SDL_ELF_NOTE_JOIN2(A,B) A##B
169#define SDL_ELF_NOTE_JOIN(A,B) SDL_ELF_NOTE_JOIN2(A,B)
170#define SDL_ELF_NOTE_UNIQUE_NAME SDL_ELF_NOTE_JOIN(s_SDL_dlopen_note_, __LINE__)
171
172/**
173 * Note that your application has dynamic shared library dependencies.
174 *
175 * You can do this by adding the following to the global scope:
176 *
177 * ```c
178 * SDL_ELF_NOTE_DLOPEN(
179 * "png",
180 * "Support for loading PNG images using libpng (required for APNG)",
181 * SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED,
182 * "libpng12.so.0"
183 * );
184 * ```
185 *
186 * Or if you support multiple versions of a library, you can list them:
187 *
188 * ```c
189 * // Our app supports SDL1, SDL2, and SDL3 by dynamically loading them
190 * SDL_ELF_NOTE_DLOPEN(
191 * "SDL",
192 * "Create windows through SDL video backend",
193 * SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
194 * "libSDL-1.2.so.0", "libSDL2-2.0.so.0", "libSDL3.so.0"
195 * );
196 * ```
197 *
198 * \since This macro is available since SDL 3.4.0.
199 *
200 * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED
201 * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED
202 * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
203 */
204#define SDL_ELF_NOTE_DLOPEN(feature, description, priority, ...) \
205 SDL_ELF_NOTE_INTERNAL( \
206 "[{\"feature\":\"" feature \
207 "\",\"description\":\"" description \
208 "\",\"priority\":\"" priority \
209 "\",\"soname\":" SDL_SONAME_ARRAY(__VA_ARGS__) "}]", \
210 SDL_ELF_NOTE_UNIQUE_NAME)
211
212#elif defined(__GNUC__) && __GNUC__ < 3
213
214#define SDL_ELF_NOTE_DLOPEN(args...)
215
216#elif defined(_MSC_VER) && _MSC_VER < 1400
217
218/* Variadic macros are not supported */
219#define SDL_ELF_NOTE_DLOPEN
220
221#else
222
223#define SDL_ELF_NOTE_DLOPEN(...)
224
225#endif
226
227#endif /* SDL_dlopennote_h */