src/SDL_loadso.c
author Sam Lantinga
Wed, 10 Apr 2002 17:12:32 +0000
changeset 336 745873ea091f
parent 297 f6ffac90895c
child 651 35ff0890ac4e
permissions -rw-r--r--
BeOS compile fix for shared object loading code
slouken@294
     1
/*
slouken@294
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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>
slouken@294
    43
#else
slouken@294
    44
/*#error Unsupported dynamic link environment*/
slouken@294
    45
#endif /* system type */
slouken@294
    46
slouken@296
    47
#include "SDL_types.h"
slouken@294
    48
#include "SDL_error.h"
slouken@294
    49
#include "SDL_loadso.h"
slouken@294
    50
slouken@294
    51
void *SDL_LoadObject(const char *sofile)
slouken@294
    52
{
slouken@294
    53
	void *handle = NULL;
slouken@294
    54
	const char *loaderror = "SDL_LoadObject() not implemented";
slouken@294
    55
#if defined(USE_DLOPEN)
slouken@294
    56
/* * */
slouken@294
    57
	handle = dlopen(sofile, RTLD_NOW);
slouken@294
    58
	loaderror = (char *)dlerror();
slouken@294
    59
#elif defined(WIN32)
slouken@294
    60
/* * */
slouken@294
    61
	char errbuf[512];
slouken@294
    62
slouken@294
    63
	handle = (void *)LoadLibrary(sofile);
slouken@294
    64
slouken@294
    65
	/* Generate an error message if all loads failed */
slouken@294
    66
	if ( handle == NULL ) {
slouken@294
    67
		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
slouken@294
    68
					FORMAT_MESSAGE_FROM_SYSTEM),
slouken@294
    69
				NULL, GetLastError(), 
slouken@294
    70
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
slouken@294
    71
				errbuf, SDL_TABLESIZE(errbuf), NULL);
slouken@294
    72
		loaderror = errbuf;
slouken@294
    73
	}
slouken@294
    74
#elif defined(__BEOS__)
slouken@294
    75
/* * */
slouken@294
    76
	image_id library_id;
slouken@294
    77
slouken@294
    78
	library_id = load_add_on(sofile);
slouken@294
    79
	if ( library_id == B_ERROR ) {
slouken@294
    80
		loaderror = "BeOS error";
slouken@294
    81
	} else {
slouken@294
    82
		handle = (void *)(library_id);
slouken@294
    83
	}
slouken@294
    84
#elif defined(macintosh)
slouken@294
    85
/* * */
slouken@294
    86
	CFragConnectionID library_id;
slouken@294
    87
	Ptr mainAddr;
slouken@294
    88
	Str255 errName;
slouken@294
    89
	OSErr error;
slouken@294
    90
	char psofile[512];
slouken@294
    91
slouken@294
    92
	strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
slouken@294
    93
	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
slouken@294
    94
	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
slouken@294
    95
			kLoadCFrag, &library_id, &mainAddr, errName);
slouken@294
    96
	switch (error) {
slouken@294
    97
		case noErr:
slouken@294
    98
			break;
slouken@294
    99
		case cfragNoLibraryErr:
slouken@294
   100
			loaderror = "Library not found";
slouken@294
   101
			break;
slouken@294
   102
		case cfragUnresolvedErr:
slouken@294
   103
			loaderror = "Unabled to resolve symbols";
slouken@294
   104
			break;
slouken@294
   105
		case cfragNoPrivateMemErr:
slouken@294
   106
		case cfragNoClientMemErr:
slouken@294
   107
			loaderror = "Out of memory";
slouken@294
   108
			break;
slouken@294
   109
		default:
slouken@294
   110
			loaderror = "Unknown Code Fragment Manager error";
slouken@294
   111
			break;
slouken@294
   112
	}
slouken@294
   113
	if ( loaderror == NULL ) {
slouken@294
   114
		handle = (void *)(library_id);
slouken@294
   115
	}
slouken@294
   116
#endif /* system type */
slouken@294
   117
slouken@294
   118
	if ( handle == NULL ) {
slouken@294
   119
		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
slouken@294
   120
	}
slouken@294
   121
	return(handle);
slouken@294
   122
}
slouken@294
   123
slouken@294
   124
void *SDL_LoadFunction(void *handle, const char *name)
slouken@294
   125
{
slouken@294
   126
	void *symbol = NULL;
slouken@294
   127
	const char *loaderror = "SDL_LoadFunction not implemented";
slouken@294
   128
#if defined(USE_DLOPEN)
slouken@294
   129
/* * */
slouken@294
   130
	symbol = dlsym(handle, name);
slouken@294
   131
	if ( symbol == NULL ) {
slouken@294
   132
		loaderror = (char *)dlerror();
slouken@294
   133
	}
slouken@294
   134
#elif defined(WIN32)
slouken@294
   135
/* * */
slouken@294
   136
	char errbuf[512];
slouken@294
   137
slouken@294
   138
	symbol = (void *)GetProcAddress((HMODULE)handle, name);
slouken@294
   139
	if ( symbol == NULL ) {
slouken@294
   140
		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
slouken@294
   141
					FORMAT_MESSAGE_FROM_SYSTEM),
slouken@294
   142
				NULL, GetLastError(), 
slouken@294
   143
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
slouken@294
   144
				errbuf, SDL_TABLESIZE(errbuf), NULL);
slouken@294
   145
		loaderror = errbuf;
slouken@294
   146
	}
slouken@294
   147
#elif defined(__BEOS__)
slouken@294
   148
/* * */
slouken@294
   149
	image_id library_id = (image_id)handle;
slouken@294
   150
	if ( get_image_symbol(library_id,
slouken@294
   151
		name, B_SYMBOL_TYPE_TEXT, &symbol) != B_NO_ERROR ) {
slouken@294
   152
		loaderror = "Symbol not found";
slouken@294
   153
	}
slouken@294
   154
#elif defined(macintosh)
slouken@294
   155
/* * */
slouken@294
   156
	CFragSymbolClass class;
slouken@294
   157
	CFragConnectionID library_id = (CFragConnectionID)handle;
slouken@294
   158
	char pname[512];
slouken@294
   159
slouken@294
   160
	strncpy(pname, name, SDL_TABLESIZE(pname));
slouken@294
   161
	pname[SDL_TABLESIZE(pname)-1] = '\0';
slouken@294
   162
	if ( FindSymbol(library_id, C2PStr(pname),
slouken@294
   163
	                (char **)&symbol, &class) != noErr ) {
slouken@294
   164
		loaderror = "Symbol not found";
slouken@294
   165
	}
slouken@294
   166
#endif /* system type */
slouken@294
   167
slouken@294
   168
	if ( symbol == NULL ) {
slouken@294
   169
		SDL_SetError("Failed loading %s: %s", name, loaderror);
slouken@294
   170
	}
slouken@294
   171
	return(symbol);
slouken@294
   172
}
slouken@294
   173
slouken@294
   174
void SDL_UnloadObject(void *handle)
slouken@294
   175
{
slouken@336
   176
#if defined(__BEOS__)
slouken@336
   177
	image_id library_id;
slouken@336
   178
#endif
slouken@294
   179
	if ( handle == NULL ) {
slouken@294
   180
		return;
slouken@294
   181
	}
slouken@294
   182
#if defined(USE_DLOPEN)
slouken@294
   183
/* * */
slouken@294
   184
	dlclose(handle);
slouken@294
   185
#elif defined(WIN32)
slouken@294
   186
/* * */
slouken@294
   187
	FreeLibrary((HMODULE)handle);
slouken@294
   188
#elif defined(__BEOS__)
slouken@294
   189
/* * */
slouken@336
   190
	library_id = (image_id)handle;
slouken@294
   191
	unload_add_on(library_id);
slouken@294
   192
#elif defined(macintosh)
slouken@294
   193
/* * */
slouken@294
   194
	CFragConnectionID library_id = (CFragConnectionID)handle;
slouken@294
   195
	CloseConnection(library_id);
slouken@294
   196
#endif /* system type */
slouken@294
   197
}