1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This file is part of Gromacs Copyright (c) 1991-2008
5 * David van der Spoel, Erik Lindahl, Berk Hess, University of Groningen.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * To help us fund GROMACS development, we humbly ask that you cite
13 * the research papers on the package. Check out http://www.gromacs.org
16 * Gnomes, ROck Monsters And Chili Sauce
19 /***************************************************************************
21 *cr (C) Copyright 1995-2009 The Board of Trustees of the
22 *cr University of Illinois
23 *cr All Rights Reserved
25 Developed by: Theoretical and Computational Biophysics Group
26 University of Illinois at Urbana-Champaign
27 http://www.ks.uiuc.edu/
29 Permission is hereby granted, free of charge, to any person obtaining a copy of
30 this software and associated documentation files (the Software), to deal with
31 the Software without restriction, including without limitation the rights to
32 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
33 of the Software, and to permit persons to whom the Software is furnished to
34 do so, subject to the following conditions:
36 Redistributions of source code must retain the above copyright notice,
37 this list of conditions and the following disclaimers.
39 Redistributions in binary form must reproduce the above copyright notice,
40 this list of conditions and the following disclaimers in the documentation
41 and/or other materials provided with the distribution.
43 Neither the names of Theoretical and Computational Biophysics Group,
44 University of Illinois at Urbana-Champaign, nor the names of its contributors
45 may be used to endorse or promote products derived from this Software without
46 specific prior written permission.
48 THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
52 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
53 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
54 OTHER DEALINGS WITH THE SOFTWARE.
55 ***************************************************************************/
57 /***************************************************************************
60 * $RCSfile: vmddlopen.c,v $
61 * $Author: johns $ $Locker: $ $State: Exp $
62 * $Revision: 1.18 $ $Date: 2009/07/07 02:40:05 $
64 ***************************************************************************
66 * Routines for loading dynamic link libraries and shared object files
67 * on various platforms, abstracting from machine dependent APIs.
69 ***************************************************************************/
74 #include "vmddlopen.h"
82 void *vmddlopen( const char *path
) {
84 ret
= shl_load( path
, BIND_IMMEDIATE
| BIND_FIRST
| BIND_VERBOSE
, 0);
88 int vmddlclose( void *handle
) {
89 return shl_unload( (shl_t
) handle
);
92 void *vmddlsym( void *handle
, const char *sym
) {
95 if ( shl_findsym( (shl_t
*)&handle
, sym
, TYPE_UNDEFINED
, &value
) != 0 )
100 const char *vmddlerror( void ) {
101 return strerror( errno
);
104 #elif 0 && defined(__APPLE__)
106 * This is only needed for MacOS X version 10.3 or older
108 #include <mach-o/dyld.h>
110 void *vmddlopen( const char *path
) {
111 NSObjectFileImage image
;
112 NSObjectFileImageReturnCode retval
;
115 retval
= NSCreateObjectFileImageFromFile(path
, &image
);
116 if (retval
!= NSObjectFileImageSuccess
)
119 module
= NSLinkModule(image
, path
,
120 NSLINKMODULE_OPTION_BINDNOW
| NSLINKMODULE_OPTION_PRIVATE
121 | NSLINKMODULE_OPTION_RETURN_ON_ERROR
);
122 return module
; /* module will be NULL on error */
125 int vmddlclose( void *handle
) {
126 NSModule module
= (NSModule
*)handle
;
127 NSUnLinkModule(module
, NSUNLINKMODULE_OPTION_NONE
);
131 void *vmddlsym( void *handle
, const char *symname
) {
135 /* Hack around the leading underscore in the symbol name */
136 realsymname
= (char *)malloc(strlen(symname
)+2);
137 strcpy(realsymname
, "_");
138 strcat(realsymname
, symname
);
139 module
= (NSModule
)handle
;
140 sym
= NSLookupSymbolInModule(module
, realsymname
);
143 return (void *)(NSAddressOfSymbol(sym
));
147 const char *vmddlerror( void ) {
150 const char *fileName
;
151 const char *errorString
= NULL
;
152 NSLinkEditError(&c
, &errorNumber
, &fileName
, &errorString
);
156 #elif defined( _WIN32 ) || defined( _WIN64 )
160 void *vmddlopen(const char *fname
) {
161 return (void *)LoadLibrary(fname
);
164 const char *vmddlerror(void) {
165 static CHAR szBuf
[80];
166 DWORD dw
= GetLastError();
168 sprintf(szBuf
, "vmddlopen failed: GetLastError returned %lu\n", dw
);
172 void *vmddlsym(void *h
, const char *sym
) {
173 return (void *)GetProcAddress((HINSTANCE
)h
, sym
);
176 int vmddlclose(void *h
) {
177 /* FreeLibrary returns nonzero on success */
178 return !FreeLibrary((HINSTANCE
)h
);
183 /* All remaining platforms (not Windows, HP-UX, or MacOS X <= 10.3) */
186 void *vmddlopen(const char *fname
) {
187 return dlopen(fname
, RTLD_NOW
);
189 const char *vmddlerror(void) {
192 void *vmddlsym(void *h
, const char *sym
) {
193 return dlsym(h
, sym
);
195 int vmddlclose(void *h
) {