src/SDL_loadso.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 24 Aug 2005 21:57:48 +0000
changeset 1131 e044e7c70a50
parent 774 0c3e00cc9580
child 1135 cf6133247d34
permissions -rw-r--r--
Merged the Visual C++ 6 and 7 projects so all Visual C++ users unpack the same set of projects to get started.
slouken@294
     1
/*
slouken@294
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@294
     4
slouken@294
     5
    This library is free software; you can redistribute it and/or
slouken@294
     6
    modify it under the terms of the GNU Library General Public
slouken@294
     7
    License as published by the Free Software Foundation; either
slouken@294
     8
    version 2 of the License, or (at your option) any later version.
slouken@294
     9
slouken@294
    10
    This library is distributed in the hope that it will be useful,
slouken@294
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@294
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@294
    13
    Library General Public License for more details.
slouken@294
    14
slouken@294
    15
    You should have received a copy of the GNU Library General Public
slouken@294
    16
    License along with this library; if not, write to the Free
slouken@294
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@294
    18
slouken@294
    19
    Sam Lantinga
slouken@294
    20
    slouken@libsdl.org
slouken@294
    21
*/
slouken@294
    22
slouken@294
    23
#ifdef SAVE_RCSID
slouken@294
    24
static char rcsid =
slouken@294
    25
 "@(#) $Id$";
slouken@294
    26
#endif
slouken@294
    27
slouken@294
    28
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@294
    29
/* System dependent library loading routines                           */
slouken@294
    30
slouken@294
    31
#include <stdio.h>
slouken@294
    32
#if defined(USE_DLOPEN)
slouken@294
    33
# include <dlfcn.h>
slouken@294
    34
#elif defined(WIN32)
slouken@294
    35
# include <windows.h>
slouken@294
    36
#elif defined(__BEOS__)
slouken@294
    37
# include <be/kernel/image.h>
slouken@294
    38
#elif defined(macintosh)
slouken@294
    39
# include <string.h>
slouken@294
    40
# include <Strings.h>
slouken@294
    41
# include <CodeFragments.h>
slouken@294
    42
# include <Errors.h>
patmandin@651
    43
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
    44
# include <gem.h>
patmandin@651
    45
# include <ldg.h>
slouken@294
    46
#else
slouken@294
    47
/*#error Unsupported dynamic link environment*/
slouken@294
    48
#endif /* system type */
slouken@294
    49
slouken@296
    50
#include "SDL_types.h"
slouken@294
    51
#include "SDL_error.h"
slouken@294
    52
#include "SDL_loadso.h"
slouken@294
    53
slouken@294
    54
void *SDL_LoadObject(const char *sofile)
slouken@294
    55
{
slouken@294
    56
	void *handle = NULL;
slouken@294
    57
	const char *loaderror = "SDL_LoadObject() not implemented";
slouken@294
    58
#if defined(USE_DLOPEN)
slouken@294
    59
/* * */
slouken@294
    60
	handle = dlopen(sofile, RTLD_NOW);
slouken@294
    61
	loaderror = (char *)dlerror();
slouken@294
    62
#elif defined(WIN32)
slouken@294
    63
/* * */
slouken@294
    64
	char errbuf[512];
slouken@294
    65
slouken@294
    66
	handle = (void *)LoadLibrary(sofile);
slouken@294
    67
slouken@294
    68
	/* Generate an error message if all loads failed */
slouken@294
    69
	if ( handle == NULL ) {
slouken@294
    70
		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
slouken@294
    71
					FORMAT_MESSAGE_FROM_SYSTEM),
slouken@294
    72
				NULL, GetLastError(), 
slouken@294
    73
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
slouken@294
    74
				errbuf, SDL_TABLESIZE(errbuf), NULL);
slouken@294
    75
		loaderror = errbuf;
slouken@294
    76
	}
slouken@294
    77
#elif defined(__BEOS__)
slouken@294
    78
/* * */
slouken@294
    79
	image_id library_id;
slouken@294
    80
slouken@294
    81
	library_id = load_add_on(sofile);
slouken@294
    82
	if ( library_id == B_ERROR ) {
slouken@294
    83
		loaderror = "BeOS error";
slouken@294
    84
	} else {
slouken@294
    85
		handle = (void *)(library_id);
slouken@294
    86
	}
slouken@294
    87
#elif defined(macintosh)
slouken@294
    88
/* * */
slouken@294
    89
	CFragConnectionID library_id;
slouken@294
    90
	Ptr mainAddr;
slouken@294
    91
	Str255 errName;
slouken@294
    92
	OSErr error;
slouken@294
    93
	char psofile[512];
slouken@294
    94
slouken@294
    95
	strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
slouken@294
    96
	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
slouken@294
    97
	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
slouken@294
    98
			kLoadCFrag, &library_id, &mainAddr, errName);
slouken@294
    99
	switch (error) {
slouken@294
   100
		case noErr:
slouken@774
   101
			loaderror = NULL;
slouken@294
   102
			break;
slouken@294
   103
		case cfragNoLibraryErr:
slouken@294
   104
			loaderror = "Library not found";
slouken@294
   105
			break;
slouken@294
   106
		case cfragUnresolvedErr:
slouken@294
   107
			loaderror = "Unabled to resolve symbols";
slouken@294
   108
			break;
slouken@294
   109
		case cfragNoPrivateMemErr:
slouken@294
   110
		case cfragNoClientMemErr:
slouken@294
   111
			loaderror = "Out of memory";
slouken@294
   112
			break;
slouken@294
   113
		default:
slouken@294
   114
			loaderror = "Unknown Code Fragment Manager error";
slouken@294
   115
			break;
slouken@294
   116
	}
slouken@294
   117
	if ( loaderror == NULL ) {
slouken@294
   118
		handle = (void *)(library_id);
slouken@294
   119
	}
patmandin@651
   120
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
   121
/* * */
patmandin@651
   122
	handle = (void *)ldg_open((char *)sofile, ldg_global);
slouken@294
   123
#endif /* system type */
slouken@294
   124
slouken@294
   125
	if ( handle == NULL ) {
slouken@294
   126
		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
slouken@294
   127
	}
slouken@294
   128
	return(handle);
slouken@294
   129
}
slouken@294
   130
slouken@294
   131
void *SDL_LoadFunction(void *handle, const char *name)
slouken@294
   132
{
slouken@294
   133
	void *symbol = NULL;
slouken@294
   134
	const char *loaderror = "SDL_LoadFunction not implemented";
slouken@294
   135
#if defined(USE_DLOPEN)
slouken@294
   136
/* * */
slouken@294
   137
	symbol = dlsym(handle, name);
slouken@294
   138
	if ( symbol == NULL ) {
slouken@294
   139
		loaderror = (char *)dlerror();
slouken@294
   140
	}
slouken@294
   141
#elif defined(WIN32)
slouken@294
   142
/* * */
slouken@294
   143
	char errbuf[512];
slouken@294
   144
slouken@294
   145
	symbol = (void *)GetProcAddress((HMODULE)handle, name);
slouken@294
   146
	if ( symbol == NULL ) {
slouken@294
   147
		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
slouken@294
   148
					FORMAT_MESSAGE_FROM_SYSTEM),
slouken@294
   149
				NULL, GetLastError(), 
slouken@294
   150
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
slouken@294
   151
				errbuf, SDL_TABLESIZE(errbuf), NULL);
slouken@294
   152
		loaderror = errbuf;
slouken@294
   153
	}
slouken@294
   154
#elif defined(__BEOS__)
slouken@294
   155
/* * */
slouken@294
   156
	image_id library_id = (image_id)handle;
slouken@294
   157
	if ( get_image_symbol(library_id,
slouken@294
   158
		name, B_SYMBOL_TYPE_TEXT, &symbol) != B_NO_ERROR ) {
slouken@294
   159
		loaderror = "Symbol not found";
slouken@294
   160
	}
slouken@294
   161
#elif defined(macintosh)
slouken@294
   162
/* * */
slouken@294
   163
	CFragSymbolClass class;
slouken@294
   164
	CFragConnectionID library_id = (CFragConnectionID)handle;
slouken@294
   165
	char pname[512];
slouken@294
   166
slouken@294
   167
	strncpy(pname, name, SDL_TABLESIZE(pname));
slouken@294
   168
	pname[SDL_TABLESIZE(pname)-1] = '\0';
slouken@294
   169
	if ( FindSymbol(library_id, C2PStr(pname),
slouken@294
   170
	                (char **)&symbol, &class) != noErr ) {
slouken@294
   171
		loaderror = "Symbol not found";
slouken@294
   172
	}
patmandin@651
   173
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
   174
/* * */
patmandin@651
   175
	symbol = (void *)ldg_find((char *)name, (LDG *)handle);
slouken@294
   176
#endif /* system type */
slouken@294
   177
slouken@294
   178
	if ( symbol == NULL ) {
slouken@294
   179
		SDL_SetError("Failed loading %s: %s", name, loaderror);
slouken@294
   180
	}
slouken@294
   181
	return(symbol);
slouken@294
   182
}
slouken@294
   183
slouken@294
   184
void SDL_UnloadObject(void *handle)
slouken@294
   185
{
slouken@336
   186
#if defined(__BEOS__)
slouken@336
   187
	image_id library_id;
slouken@707
   188
#elif defined(macintosh)
slouken@707
   189
	CFragConnectionID library_id;
slouken@336
   190
#endif
slouken@294
   191
	if ( handle == NULL ) {
slouken@294
   192
		return;
slouken@294
   193
	}
slouken@294
   194
#if defined(USE_DLOPEN)
slouken@294
   195
/* * */
slouken@294
   196
	dlclose(handle);
slouken@294
   197
#elif defined(WIN32)
slouken@294
   198
/* * */
slouken@294
   199
	FreeLibrary((HMODULE)handle);
slouken@294
   200
#elif defined(__BEOS__)
slouken@294
   201
/* * */
slouken@336
   202
	library_id = (image_id)handle;
slouken@294
   203
	unload_add_on(library_id);
slouken@294
   204
#elif defined(macintosh)
slouken@294
   205
/* * */
slouken@707
   206
	library_id = (CFragConnectionID)handle;
slouken@708
   207
	CloseConnection(&library_id);
patmandin@651
   208
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
   209
/* * */
patmandin@651
   210
	ldg_close((LDG *)handle, ldg_global);
slouken@294
   211
#endif /* system type */
slouken@294
   212
}