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