src/loadso/dlopen/SDL_sysloadso.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
child 1379 c0a74f199ecf
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
slouken@1361
     1
/*
slouken@1361
     2
    SDL - Simple DirectMedia Layer
slouken@1361
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1361
     4
slouken@1361
     5
    This library is free software; you can redistribute it and/or
slouken@1361
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1361
     7
    License as published by the Free Software Foundation; either
slouken@1361
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1361
     9
slouken@1361
    10
    This library is distributed in the hope that it will be useful,
slouken@1361
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1361
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1361
    13
    Lesser General Public License for more details.
slouken@1361
    14
slouken@1361
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1361
    16
    License along with this library; if not, write to the Free Software
slouken@1361
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1361
    18
slouken@1361
    19
    Sam Lantinga
slouken@1361
    20
    slouken@libsdl.org
slouken@1361
    21
*/
slouken@1361
    22
slouken@1361
    23
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@1361
    24
/* System dependent library loading routines                           */
slouken@1361
    25
slouken@1361
    26
#include <stdio.h>
slouken@1361
    27
#include <dlfcn.h>
slouken@1361
    28
slouken@1361
    29
#include "SDL_loadso.h"
slouken@1361
    30
slouken@1361
    31
void *SDL_LoadObject(const char *sofile)
slouken@1361
    32
{
slouken@1361
    33
	void *handle = dlopen(sofile, RTLD_NOW);
slouken@1361
    34
	const char *loaderror = (char *)dlerror();
slouken@1361
    35
	if ( handle == NULL ) {
slouken@1361
    36
		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
slouken@1361
    37
	}
slouken@1361
    38
	return(handle);
slouken@1361
    39
}
slouken@1361
    40
slouken@1361
    41
void *SDL_LoadFunction(void *handle, const char *name)
slouken@1361
    42
{
slouken@1361
    43
	void *symbol = dlsym(handle, name);
slouken@1361
    44
	if ( symbol == NULL ) {
slouken@1361
    45
		char *_name = SDL_stack_alloc(char, 1+SDL_strlen(name)+1);
slouken@1361
    46
		_name[0] = '_';
slouken@1361
    47
		SDL_strcpy(&_name[1], name);
slouken@1361
    48
		symbol = dlsym(handle, name);
slouken@1361
    49
		SDL_stack_free(_name);
slouken@1361
    50
		if ( symbol == NULL ) {
slouken@1361
    51
			SDL_SetError("Failed loading %s: %s", name, (const char *)dlerror());
slouken@1361
    52
		}
slouken@1361
    53
	}
slouken@1361
    54
	return(symbol);
slouken@1361
    55
}
slouken@1361
    56
slouken@1361
    57
void SDL_UnloadObject(void *handle)
slouken@1361
    58
{
slouken@1361
    59
	if ( handle != NULL ) {
slouken@1361
    60
		dlclose(handle);
slouken@1361
    61
	}
slouken@1361
    62
}
slouken@1361
    63