src/loadso/dlopen/SDL_sysloadso.c
author Alex Szpakowski <slime73@gmail.com>
Sun, 25 Sep 2016 15:02:06 -0300
changeset 10372 19b0437c4099
parent 9998 f67cf37e9cd4
child 10737 3406a0f8b041
permissions -rw-r--r--
Enable SDL_LoadObject on iOS 8+ and tvOS.
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@1361
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1361
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1361
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1361
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1361
    22
slouken@1635
    23
#ifdef SDL_LOADSO_DLOPEN
slouken@1635
    24
slouken@1361
    25
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@1361
    26
/* System dependent library loading routines                           */
slouken@1361
    27
slouken@1361
    28
#include <stdio.h>
slouken@1361
    29
#include <dlfcn.h>
slouken@1361
    30
slouken@1361
    31
#include "SDL_loadso.h"
slouken@1361
    32
slime73@10372
    33
#if SDL_VIDEO_DRIVER_UIKIT
slime73@10372
    34
#include "../../video/uikit/SDL_uikitvideo.h"
slime73@10372
    35
#endif
slime73@10372
    36
slouken@1895
    37
void *
slouken@1895
    38
SDL_LoadObject(const char *sofile)
slouken@1361
    39
{
slime73@10372
    40
    void *handle;
slime73@10372
    41
    const char *loaderror;
slime73@10372
    42
slime73@10372
    43
#if SDL_VIDEO_DRIVER_UIKIT
slime73@10372
    44
    if (!UIKit_IsSystemVersionAtLeast(8.0)) {
slime73@10372
    45
        SDL_SetError("SDL_LoadObject requires iOS 8+");
slime73@10372
    46
        return NULL;
slime73@10372
    47
    }
slime73@10372
    48
#endif
slime73@10372
    49
slime73@10372
    50
    handle = dlopen(sofile, RTLD_NOW|RTLD_LOCAL);
slime73@10372
    51
    loaderror = (char *) dlerror();
slouken@1895
    52
    if (handle == NULL) {
slouken@1895
    53
        SDL_SetError("Failed loading %s: %s", sofile, loaderror);
slouken@1895
    54
    }
slouken@1895
    55
    return (handle);
slouken@1361
    56
}
slouken@1361
    57
slouken@1895
    58
void *
slouken@1895
    59
SDL_LoadFunction(void *handle, const char *name)
slouken@1361
    60
{
slouken@1895
    61
    void *symbol = dlsym(handle, name);
slouken@1895
    62
    if (symbol == NULL) {
icculus@2084
    63
        /* append an underscore for platforms that need that. */
slouken@1895
    64
        size_t len = 1 + SDL_strlen(name) + 1;
slouken@1895
    65
        char *_name = SDL_stack_alloc(char, len);
slouken@1895
    66
        _name[0] = '_';
slouken@1895
    67
        SDL_strlcpy(&_name[1], name, len);
icculus@2084
    68
        symbol = dlsym(handle, _name);
slouken@1895
    69
        SDL_stack_free(_name);
slouken@1895
    70
        if (symbol == NULL) {
slouken@1895
    71
            SDL_SetError("Failed loading %s: %s", name,
slouken@1895
    72
                         (const char *) dlerror());
slouken@1895
    73
        }
slouken@1895
    74
    }
slouken@1895
    75
    return (symbol);
slouken@1361
    76
}
slouken@1361
    77
slouken@1895
    78
void
slouken@1895
    79
SDL_UnloadObject(void *handle)
slouken@1361
    80
{
slouken@1895
    81
    if (handle != NULL) {
slouken@1895
    82
        dlclose(handle);
slouken@1895
    83
    }
slouken@1361
    84
}
slouken@1361
    85
slouken@1635
    86
#endif /* SDL_LOADSO_DLOPEN */
slouken@2773
    87
slouken@1895
    88
/* vi: set ts=4 sw=4 expandtab: */