src/loadso/dlopen/SDL_sysloadso.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 7598 4ea0b0177a57
child 8149 681eb46b8ac4
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h

The new header will include SDL_config.h, but allows for other global stuff.
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
slouken@1895
    33
void *
slouken@1895
    34
SDL_LoadObject(const char *sofile)
slouken@1361
    35
{
slouken@7598
    36
    void *handle = dlopen(sofile, RTLD_NOW|RTLD_LOCAL);
slouken@1895
    37
    const char *loaderror = (char *) dlerror();
slouken@1895
    38
    if (handle == NULL) {
slouken@1895
    39
        SDL_SetError("Failed loading %s: %s", sofile, loaderror);
slouken@1895
    40
    }
slouken@1895
    41
    return (handle);
slouken@1361
    42
}
slouken@1361
    43
slouken@1895
    44
void *
slouken@1895
    45
SDL_LoadFunction(void *handle, const char *name)
slouken@1361
    46
{
slouken@1895
    47
    void *symbol = dlsym(handle, name);
slouken@1895
    48
    if (symbol == NULL) {
icculus@2084
    49
        /* append an underscore for platforms that need that. */
slouken@1895
    50
        size_t len = 1 + SDL_strlen(name) + 1;
slouken@1895
    51
        char *_name = SDL_stack_alloc(char, len);
slouken@1895
    52
        _name[0] = '_';
slouken@1895
    53
        SDL_strlcpy(&_name[1], name, len);
icculus@2084
    54
        symbol = dlsym(handle, _name);
slouken@1895
    55
        SDL_stack_free(_name);
slouken@1895
    56
        if (symbol == NULL) {
slouken@1895
    57
            SDL_SetError("Failed loading %s: %s", name,
slouken@1895
    58
                         (const char *) dlerror());
slouken@1895
    59
        }
slouken@1895
    60
    }
slouken@1895
    61
    return (symbol);
slouken@1361
    62
}
slouken@1361
    63
slouken@1895
    64
void
slouken@1895
    65
SDL_UnloadObject(void *handle)
slouken@1361
    66
{
slouken@1895
    67
    if (handle != NULL) {
slouken@1895
    68
        dlclose(handle);
slouken@1895
    69
    }
slouken@1361
    70
}
slouken@1361
    71
slouken@1635
    72
#endif /* SDL_LOADSO_DLOPEN */
slouken@2773
    73
slouken@1895
    74
/* vi: set ts=4 sw=4 expandtab: */