NEURON
osxdlfcn.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2002 Peter O'Gorman <ogorman@users.sourceforge.net>
3 
4 Permission is hereby granted, free of charge, to any person obtaining
5 a copy of this software and associated documentation files (the
6 "Software"), to deal in the Software without restriction, including
7 without limitation the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions:
11 
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 
24 
25 /* Just to prove that it isn't that hard to add Mac calls to your code :)
26  This works with pretty much everything, including kde3 xemacs and the gimp,
27  I'd guess that it'd work in at least 95% of cases, use this as your starting
28  point, rather than the mess that is dlfcn.cpp, assuming that your code does not
29  require ref counting or symbol lookups in dependent libraries
30 */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <stdarg.h>
38 #include <limits.h>
39 #include <mach-o/dyld.h>
40 #include "osxdlfcn.h"
41 
42 
43 #define ERR_STR_LEN 256
44 
45 static void *dlsymIntern(void *handle, const char *symbol);
46 
47 static const char *error(int setget, const char *str, ...);
48 
49 
50 /* Set and get the error string for use by dlerror */
51 static const char *error(int setget, const char *str, ...) {
52  static char errstr[ERR_STR_LEN];
53  static int err_filled = 0;
54  const char *retval;
55  NSLinkEditErrors ler;
56  int lerno;
57  const char *dylderrstr;
58  const char *file;
59  va_list arg;
60  if (setget <= 0) {
61  va_start(arg, str);
62  strncpy(errstr, "dlsimple: ", ERR_STR_LEN);
63  vsnprintf(errstr + 10, ERR_STR_LEN - 10, str, arg);
64  va_end(arg);
65  /* We prefer to use the dyld error string if getset is 1*/
66  if (setget == 0) {
67  NSLinkEditError(&ler, &lerno, &file, &dylderrstr);
68  fprintf(stderr, "dyld: %s\n", dylderrstr);
69  if (dylderrstr && strlen(dylderrstr))
70  strncpy(errstr, dylderrstr, ERR_STR_LEN);
71  }
72  err_filled = 1;
73  retval = NULL;
74  } else {
75  if (!err_filled)
76  retval = NULL;
77  else
78  retval = errstr;
79  err_filled = 0;
80  }
81  return retval;
82 }
83 
84 /* dlopen */
85 extern "C" void *dlopen(const char *path, int mode) {
86  void *module = 0;
87  NSObjectFileImage ofi = 0;
88  NSObjectFileImageReturnCode ofirc;
89  static int (*make_private_module_public)(NSModule module) = 0;
90  unsigned int flags = NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE;
91 
92  /* If we got no path, the app wants the global namespace, use -1 as the marker
93  in this case */
94  if (!path)
95  return (void *) -1;
96 
97  /* Create the object file image, works for things linked with the -bundle arg to ld */
98  ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
99  switch (ofirc) {
100  case NSObjectFileImageSuccess:
101  /* It was okay, so use NSLinkModule to link in the image */
102  if (!(mode & RTLD_LAZY)) flags += NSLINKMODULE_OPTION_BINDNOW;
103  module = NSLinkModule(ofi, path, flags);
104  /* Don't forget to destroy the object file image, unless you like leaks */
105  NSDestroyObjectFileImage(ofi);
106  /* If the mode was global, then change the module, this avoids
107  multiply defined symbol errors to first load private then make
108  global. Silly, isn't it. */
109  if ((mode & RTLD_GLOBAL)) {
110  if (!make_private_module_public) {
111  _dyld_func_lookup("__dyld_NSMakePrivateModulePublic",
112  (unsigned long *) &make_private_module_public);
113  }
114  make_private_module_public(module);
115  }
116  break;
117  case NSObjectFileImageInappropriateFile:
118  /* It may have been a dynamic library rather than a bundle, try to load it */
119  module = (void *) NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
120  break;
121  case NSObjectFileImageFailure:
122  error(0, "Object file setup failure : \"%s\"", path);
123  return 0;
124  case NSObjectFileImageArch:
125  error(0, "No object for this architecture : \"%s\"", path);
126  return 0;
127  case NSObjectFileImageFormat:
128  error(0, "Bad object file format : \"%s\"", path);
129  return 0;
130  case NSObjectFileImageAccess:
131  error(0, "Can't read object file : \"%s\"", path);
132  return 0;
133  }
134  if (!module)
135  error(0, "Can not open \"%s\"", path);
136  return module;
137 }
138 
139 /* dlsymIntern is used by dlsym to find the symbol */
140 void *dlsymIntern(void *handle, const char *symbol) {
141  NSSymbol *nssym = 0;
142  /* If the handle is -1, if is the app global context */
143  if (handle == (void *) -1) {
144  /* Global context, use NSLookupAndBindSymbol */
145  if (NSIsSymbolNameDefined(symbol)) {
146  nssym = NSLookupAndBindSymbol(symbol);
147  }
148 
149  }
150  /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image
151  for libraries, and NSLookupSymbolInModule for bundles */
152  else {
153  /* Check for both possible magic numbers depending on x86/ppc byte order */
154  if ((((struct mach_header *) handle)->magic == MH_MAGIC) ||
155  (((struct mach_header *) handle)->magic == MH_CIGAM)) {
156  if (NSIsSymbolNameDefinedInImage((struct mach_header *) handle, symbol)) {
157  nssym = NSLookupSymbolInImage((struct mach_header *) handle,
158  symbol,
159  NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
160  | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
161  }
162 
163  } else {
164  nssym = NSLookupSymbolInModule(handle, symbol);
165  }
166  }
167  if (!nssym) {
168  error(0, "Symbol \"%s\" Not found", symbol);
169  return NULL;
170  }
171  return NSAddressOfSymbol(nssym);
172 }
173 
174 extern "C" const char *dlerror(void) {
175  return error(1, (char *) NULL);
176 }
177 
178 extern "C" int dlclose(void *handle) {
179  if ((((struct mach_header *) handle)->magic == MH_MAGIC) ||
180  (((struct mach_header *) handle)->magic == MH_CIGAM)) {
181  error(-1, "Can't remove dynamic libraries on darwin");
182  return 0;
183  }
184  if (!NSUnLinkModule(handle, 0)) {
185  error(0, "unable to unlink module %s", NSNameOfModule(handle));
186  return 1;
187  }
188  return 0;
189 }
190 
191 
192 /* dlsym, prepend the underscore and call dlsymIntern */
193 extern "C" void *dlsym(void *handle, const char *symbol) {
194  static char undersym[257]; /* Saves calls to malloc(3) */
195  int sym_len = strlen(symbol);
196  void *value = NULL;
197  char *malloc_sym = NULL;
198 
199  if (sym_len < 256) {
200  snprintf(undersym, 256, "_%s", symbol);
201  value = dlsymIntern(handle, undersym);
202  } else {
203  malloc_sym = malloc(sym_len + 2);
204  if (malloc_sym) {
205  sprintf(malloc_sym, "_%s", symbol);
206  value = dlsymIntern(handle, malloc_sym);
207  free(malloc_sym);
208  } else {
209  error(-1, "Unable to allocate memory");
210  }
211  }
212  return value;
213 }
214 
static realtype retval
const char * dlerror(void)
Definition: osxdlfcn.cpp:174
#define ERR_STR_LEN
Definition: osxdlfcn.cpp:43
#define RTLD_LAZY
Definition: osxdlfcn.h:60
sprintf(buf," if (secondorder) {\ " int _i;\" " for(_i=0;_i< %d;++_i) {\" " _p[_slist%d[_i]]+=dt *_p[_dlist%d[_i]];\" " }}\", numeqn, listnum, listnum)
int dlclose(void *handle)
Definition: osxdlfcn.cpp:178
static const char * error(int setget, const char *str,...)
Definition: osxdlfcn.cpp:51
int
Definition: nrnmusic.cpp:71
fprintf(stderr, "Don't know the location of params at %p\, pp)
void * dlopen(const char *path, int mode)
Definition: osxdlfcn.cpp:85
static uint32_t value
Definition: scoprand.cpp:26
static void * dlsymIntern(void *handle, const char *symbol)
Definition: osxdlfcn.cpp:140
#define arg
Definition: redef.h:28
static struct @32 flags[]
void * dlsym(void *handle, const char *symbol)
Definition: osxdlfcn.cpp:193
return NULL
Definition: cabcode.cpp:461
#define RTLD_GLOBAL
Definition: osxdlfcn.h:63