SDL_sysloadso.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_LOADSO_DLOPEN
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. /* System dependent library loading routines */
  22. #include <stdio.h>
  23. #include <dlfcn.h>
  24. #ifdef SDL_VIDEO_DRIVER_UIKIT
  25. #include "../../video/uikit/SDL_uikitvideo.h"
  26. #endif
  27. void *SDL_LoadObject(const char *sofile)
  28. {
  29. void *handle;
  30. const char *loaderror;
  31. #ifdef SDL_VIDEO_DRIVER_UIKIT
  32. if (!UIKit_IsSystemVersionAtLeast(8.0)) {
  33. SDL_SetError("SDL_LoadObject requires iOS 8+");
  34. return NULL;
  35. }
  36. #endif
  37. handle = dlopen(sofile, RTLD_NOW | RTLD_LOCAL);
  38. loaderror = dlerror();
  39. if (!handle) {
  40. SDL_SetError("Failed loading %s: %s", sofile, loaderror);
  41. }
  42. return handle;
  43. }
  44. SDL_FunctionPointer SDL_LoadFunction(void *handle, const char *name)
  45. {
  46. void *symbol = dlsym(handle, name);
  47. if (!symbol) {
  48. /* prepend an underscore for platforms that need that. */
  49. SDL_bool isstack;
  50. size_t len = SDL_strlen(name) + 1;
  51. char *_name = SDL_small_alloc(char, len + 1, &isstack);
  52. _name[0] = '_';
  53. SDL_memcpy(&_name[1], name, len);
  54. symbol = dlsym(handle, _name);
  55. SDL_small_free(_name, isstack);
  56. if (!symbol) {
  57. SDL_SetError("Failed loading %s: %s", name,
  58. (const char *)dlerror());
  59. }
  60. }
  61. return symbol;
  62. }
  63. void SDL_UnloadObject(void *handle)
  64. {
  65. if (handle) {
  66. dlclose(handle);
  67. }
  68. }
  69. #endif /* SDL_LOADSO_DLOPEN */