1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2021 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#include "../../SDL_internal.h"
22
23#ifdef SDL_LOADSO_DLOPEN
24
25/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26/* System dependent library loading routines */
27
28#include <stdio.h>
29#include <dlfcn.h>
30
31#include "SDL_loadso.h"
32
33#if SDL_VIDEO_DRIVER_UIKIT
34#include "../../video/uikit/SDL_uikitvideo.h"
35#endif
36
37void *
38SDL_LoadObject(const char *sofile)
39{
40 void *handle;
41 const char *loaderror;
42
43#if SDL_VIDEO_DRIVER_UIKIT
44 if (!UIKit_IsSystemVersionAtLeast(8.0)) {
45 SDL_SetError("SDL_LoadObject requires iOS 8+");
46 return NULL;
47 }
48#endif
49
50 handle = dlopen(sofile, RTLD_NOW|RTLD_LOCAL);
51 loaderror = dlerror();
52 if (handle == NULL) {
53 SDL_SetError("Failed loading %s: %s", sofile, loaderror);
54 }
55 return (handle);
56}
57
58void *
59SDL_LoadFunction(void *handle, const char *name)
60{
61 void *symbol = dlsym(handle, name);
62 if (symbol == NULL) {
63 /* append an underscore for platforms that need that. */
64 SDL_bool isstack;
65 size_t len = 1 + SDL_strlen(name) + 1;
66 char *_name = SDL_small_alloc(char, len, &isstack);
67 _name[0] = '_';
68 SDL_strlcpy(&_name[1], name, len);
69 symbol = dlsym(handle, _name);
70 SDL_small_free(_name, isstack);
71 if (symbol == NULL) {
72 SDL_SetError("Failed loading %s: %s", name,
73 (const char *) dlerror());
74 }
75 }
76 return (symbol);
77}
78
79void
80SDL_UnloadObject(void *handle)
81{
82 if (handle != NULL) {
83 dlclose(handle);
84 }
85}
86
87#endif /* SDL_LOADSO_DLOPEN */
88
89/* vi: set ts=4 sw=4 expandtab: */
90