File: dynamic.c

package info (click to toggle)
helium 1.7~pre20090428-3.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,152 kB
  • sloc: haskell: 90,306; ansic: 11,294; java: 3,819; sh: 3,407; makefile: 694; xml: 41
file content (585 lines) | stat: -rw-r--r-- 16,298 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*-----------------------------------------------------------------------
  The Lazy Virtual Machine.

  Daan Leijen.

  Copyright 2001, Daan Leijen. All rights reserved. This file is
  distributed under the terms of the GNU Library General Public License.
-----------------------------------------------------------------------*/

/* $Id: dynamic.c 87 2002-02-25 12:25:50Z cvs-3 $ */

/*----------------------------------------------------------------------
  Platform independent loading of dynamic link libraries (.so or .dll files).
----------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mlvalues.h"
#include "memory.h"
#include "fail.h"
#include "module.h"
#include "dynamic.h"
#include "sys.h"
#include "custom.h"

char* stat_alloc_string( const char* s )
{
  long  len;
  char* p;

  len = str_len(s);
  p   = (char*)stat_alloc( len+1 );
  str_cpy( p, s, len+1 );
  return p;
}

/*----------------------------------------------------------------------
  Platform dependent includes + definition of a dynamic library handle
----------------------------------------------------------------------*/
#if defined(OS_WINDOWS)    /* eg. MINGW32, WINDOWS */
# include <windows.h>
  typedef HMODULE dynamic_handle;
#elif defined(HAS_DLFCN_H) /* eg CYGWIN, LINUX, SOLARIS, ULTRIX */
# include <dlfcn.h>
  typedef void* dynamic_handle;
#elif HAS_DL_H            /* eg HPUX */
# include <dl.h>
# define dynamic_handle shl_t
#else                     /* no dynamic linking */
  typedef void* dynamic_handle;
#endif

/*----------------------------------------------------------------------
 platform specific routines only return a status code.
----------------------------------------------------------------------*/
enum dynamic_err
{
  Err_success,
  Err_nodynamic,         /* no support for dynamic loading */
  Err_nolibrary,         /* can't load library */
  Err_noordinal,         /* no support for dynamic loading by ordinal */
  Err_nosymbol,          /* couldn't find symbol */
};

/*----------------------------------------------------------------------
  Platform specific routines (not thread-safe!)
----------------------------------------------------------------------*/
static void             sys_dynamic_close  ( dynamic_handle handle );
static enum dynamic_err sys_dynamic_open   ( const char* libpath, dynamic_handle* handle, const char** errinfo );
static enum dynamic_err sys_dynamic_symbol ( dynamic_handle* handle, const char* name, void** fun, const char** errinfo );
static enum dynamic_err sys_dynamic_ordinal( dynamic_handle* handle, long ordinal, void** fun, const char** errinfo );

/*----------------------------------------------------------------------
  We maintain a list of dynamic libraries that are loaded. These
  libraries are reference counted.
----------------------------------------------------------------------*/
struct dynamic_lib {
  struct dynamic_lib* next;
  long                refcount;
  char*               name;
  char*               path;
  dynamic_handle      handle;
};

static struct dynamic_lib* dynamic_libs = NULL;

static struct dynamic_lib* dynamic_open( const char* libname );

static void   dynamic_release( struct dynamic_lib* lib );
static void   dynamic_close  ( struct dynamic_lib* lib );
static value  dynamic_symbol ( struct dynamic_lib* lib, const char* name, bool is_ordinal );
static void   dynamic_decorate( char* name_buf, const char* name, long size
                              , enum call_conv cconv, const char* type );


/*----------------------------------------------------------------------
  [load_dynamic_symbol]
  the toplevel routine to load symbols. All resource management and
  decoration is done automatically. Returns a custom Symbol block.
----------------------------------------------------------------------*/
value load_dynamic_symbol( const char*    libname,
                           const char*    name,
                           enum call_conv cconv,
                           const char*    type,
                           enum name_flag flag )
{
    CAMLparam0();
    CAMLlocal1(v);
    struct dynamic_lib* lib;

    lib = dynamic_open( libname );

    switch (flag) {
    case Name_ordinal:
        v = dynamic_symbol( lib, name, true );
        break;
    case Name_decorate: {
        char decorated[MAXPATH];
        dynamic_decorate( decorated, name, MAXPATH, cconv, type );
        v = dynamic_symbol( lib, decorated, false );
        break;
        }
    case Name_plain:
    default:
        v = dynamic_symbol( lib, name, false );
        break;
    }

    CAMLreturn(v);
}


/*----------------------------------------------------------------------
  Symbols in a dll are Custom blocks that hold both a
  pointer to the symbol and a pointer to the [dynamic_lib] struct.
----------------------------------------------------------------------*/
static void symbol_finalize( value v )
{
  gc_message( 8,"finalise dynamic symbol\n", 0 );
  dynamic_release(Symbol_lib(v));
  return;
}

static int symbol_compare(value v1, value v2)
{
  raise_internal( "comparing dynamic symbol" );
  return 0;
}

static long symbol_hash(value v)
{
  return (long)(Symbol_fun(v));
}

static void symbol_serialize(value v, unsigned long * wsize_32,
                            unsigned long * wsize_64)
{
  raise_internal( "serializing dynamic symbol" );
}

static unsigned long symbol_deserialize(void * dst)
{
  raise_internal( "deserializing dynamic symbol" );
  return 0;
}


static struct custom_operations symbol_ops = {
  "_dynamic_symbol",
  symbol_finalize,
  symbol_compare,
  symbol_hash,
  symbol_serialize,
  symbol_deserialize
};

static void dynamic_raise_symbol( enum dynamic_err err, const char* info
                                , const char* libname, const char* name )
{
  switch (err) {
    case Err_success:
      return;
    case Err_noordinal:
      if (info == NULL) info = "this platform doesn't support linking by ordinal";
      break;
    case Err_nodynamic:
      if (info == NULL) info = "this platform doesn't support dynamic linking";
      break;
    case Err_nosymbol:
    default:
      break;
  }

  if (info) raise_internal( "can not resolve symbol \"%s\" in \"%s\": %s", name, libname, info);
       else raise_internal( "can not resolve symbol \"%s\" in \"%s\"", name, libname );
}

static value dynamic_symbol( struct dynamic_lib* lib, const char* name, bool is_ordinal )
{
  CAMLparam0();
  CAMLlocal1(v);
  enum dynamic_err err;
  const char*      errinfo;
  void*            fun;

  Assert(lib);
  if (is_ordinal) err = sys_dynamic_ordinal( &(lib->handle), (long)name, &fun, &errinfo );
             else err = sys_dynamic_symbol( &(lib->handle), name, &fun, &errinfo );
  if (err != Err_success) dynamic_raise_symbol( err, errinfo, lib->name, name );

  v = alloc_custom( &symbol_ops, 2*sizeof(void*), 0, 1 );
  Symbol_lib(v) = lib;
  Symbol_fun(v) = fun;
  CAMLreturn(v);
};


/*----------------------------------------------------------------------
  [dynamic_lib] init/done
----------------------------------------------------------------------*/
void init_dynamic(void)
{
  dynamic_libs = NULL;
}

void done_dynamic(void)
{
  Assert(dynamic_libs == NULL);
  while (dynamic_libs != NULL) {
    dynamic_close(dynamic_libs);
  }
}

/*----------------------------------------------------------------------
  [dynamic_close], [dynamic_release] and [dynamic_open]
----------------------------------------------------------------------*/
static void dynamic_release( struct dynamic_lib* lib )
{
  if (lib == NULL) return;

  /* decrease the reference count */
  lib->refcount--;
  if (lib->refcount <= 0) dynamic_close(lib);
};

static void dynamic_close( struct dynamic_lib* lib )
{
  struct dynamic_lib *prev, *current;
  Assert( lib != NULL && lib->refcount == 0);
  if (lib == NULL) return;

  /* find the previous block and relink */
  for( prev = NULL, current = dynamic_libs;
       current != NULL && current != lib;
       prev = current, current = current->next) {}

  if (current == lib) { /* if found */
    if (prev == NULL) dynamic_libs = lib->next;
                 else prev->next   = lib->next;
  }

  gc_message( 8,"finalise dynamic library \"%s\"\n", (long)(lib->path) );

  /* free the block */
  sys_dynamic_close(lib->handle);
  stat_free(lib->name);
  stat_free(lib->path);
  stat_free(lib);
};

static void dynamic_open_error( enum dynamic_err err, const char* info, const char* libpath )
{
  switch (err) {
    case Err_success:
      return;
    case Err_nodynamic:
      if (info == NULL) info = "this system doesn't support dynamic linking";
      /* fall through */
    default:
      if (info) raise_internal( "can not open library \"%s\": %s", libpath, info);
           else raise_internal( "can not open library \"%s\"", libpath );
      break;
  }
}

static struct dynamic_lib* dynamic_open( const char* libname )
{
  const char*         libpath;
  struct dynamic_lib* lib;
  dynamic_handle      handle;
  enum dynamic_err    err;
  const char*         errinfo;

  /* first try to find the library by name */
  for( lib = dynamic_libs; lib != NULL; lib = lib->next ) {
    if (strcmp(libname,lib->name) == 0) {
      /* found! */
      lib->refcount++;
      return lib;
    }
  }

  /* try to find the library and search again by path */
  libpath = searchpath_dll( libname );
  if (libpath == NULL) {
    libpath = libname;
  } else {
    for( lib = dynamic_libs; lib != NULL; lib = lib->next ) {
      if (strcmp(libpath,lib->path) == 0) {
        /* found! */
        lib->refcount++;
        return lib;
      }
    }
  }

  /* we load the library for the first time */
  gc_message( 8,"load dynamic library \"%s\"\n", (long)(libpath) );
  err = sys_dynamic_open( libpath, &handle, &errinfo );
  if (err != Err_success) dynamic_open_error( err, errinfo, libpath );

  lib           = stat_alloc( sizeof(struct dynamic_lib) );
  lib->refcount = 1;
  lib->name     = stat_alloc_string(libname);
  lib->path     = stat_alloc_string(libpath);
  lib->handle   = handle;
  lib->next     = dynamic_libs;
  dynamic_libs  = lib;

  return lib;
}

/*-----------------------------------------------------------------------
  decorate a symbol according to its calling convention:
  - ccall   : prefix with "_"
  - stdcall : prefix with "_", postfix with "@i" where i is the byte size
              of its arguments in decimal.
-----------------------------------------------------------------------*/
#define Symbol_extra      16

static int type_size( const char* type );
static int arg_size ( char rep );

static void dynamic_decorate( char* name_buf, const char* name, long size
                            , enum call_conv cconv, const char* type )
{
   Assert( name_buf );
   Assert( name );
   if (str_len(name) + Symbol_extra >= size)
      raise_internal( "symbol name too long: \"%s\"", name );

   switch (cconv)
   {
   case Call_std: {
       sprintf( name_buf, "_%s@%i", name, type_size(type) );
       break;
     }
   case Call_c: {
       sprintf( name_buf, "_%s", name );
       break;
     }
   default: {
     /* unknown call type, don't do anything */
       strcpy( name_buf, name );
       break;
     }
   }
}

/* return the size of a list of arguments */
static int type_size( const char* type )
{
   int count = type ? strlen(type)-1 : 0;
   int size;
   int i;

   Assert(type);
   Assert(count >= 0);

   for (size = 0, i = 1; i <= count; i++)
   {
      size += arg_size( type[i] );
   }

   return size;
}

/* return the representation size of type. */
static int  arg_size( char rep )
{
  switch (rep)
  {
    case 'b':
    case 'c':
    case 'i':
    case 'u': return (sizeof(int));

    case 'v':
    case 'I':
    case 'U': return (sizeof(long));

    case 'p':
    case 'z':
    case 'Z': return (sizeof(void*));

    case 'f': return (sizeof(float));
    case 'F': return (sizeof(double));

    case '0': return 0;
    case '1': return 4;
    case '2': return 4;
    case '4': return 4;
    case '8': return 8;

    default: {
      raise_internal( "unknown C type represenation: \"%c\"", rep );
      return 0;
    }
  }
}



/*-----------------------------------------------------------------------
 Platform specific routines.
-----------------------------------------------------------------------*/

#if defined(OS_WINDOWS)

#include <windows.h>

static void sys_dynamic_close( dynamic_handle handle )
{
#ifdef DEBUG
  if (FreeLibrary(handle) == 0) Assert(0);
#else
  FreeLibrary(handle);
#endif
}

static enum dynamic_err sys_dynamic_open ( const char* libpath, dynamic_handle* handle, const char** error_info )
{
  if (error_info) *error_info = NULL;

  *handle = LoadLibrary(libpath);
  if (*handle == NULL) return Err_nolibrary; /* GetLastError doesn't tell anything more in practice. */
  return Err_success;
}

static enum dynamic_err sys_dynamic_symbol ( dynamic_handle* handle, const char* name
                                           , void** fun, const char** errinfo )
{
  if (errinfo) *errinfo = NULL;
  *fun = GetProcAddress(*handle,name);
  if (*fun == NULL) return Err_nosymbol;
  return Err_success;
}

static enum dynamic_err sys_dynamic_ordinal( dynamic_handle* handle, long ordinal
                                           , void** fun, const char** errinfo )
{
  if (errinfo) *errinfo = NULL;
  *fun = GetProcAddress(*handle,(LPCSTR)(ordinal));
  if (*fun == NULL) return Err_nosymbol;
  return Err_success;
}




#elif defined(HAS_DLFCN_H)
/* eg CYGWIN, LINUX, SOLARIS, ULTRIX */

#include <stdio.h>
#include <dlfcn.h>

#ifndef RTLD_NOW
# define RTLD_NOW 1
#endif

#ifndef RTLD_GLOBAL
# define RTLD_GLOBAL 0
#endif

static void sys_dynamic_close( dynamic_handle handle )
{
  dlclose(handle);
}

static enum dynamic_err sys_dynamic_open ( const char* libpath, dynamic_handle* handle, const char** error_info )
{
  if (error_info) *error_info = NULL;

  *handle = dlopen( libpath, RTLD_NOW | RTLD_GLOBAL );
  if (*handle == NULL) {
    if (error_info) *error_info = dlerror();
    return Err_nolibrary;
  }
  return Err_success;
}

static enum dynamic_err sys_dynamic_symbol ( dynamic_handle* handle, const char* name
                                           , void** fun, const char** errinfo )
{
  if (errinfo) *errinfo = NULL;
  *fun = dlsym(*handle,name);
  if (*fun == NULL) {
    if (errinfo) *errinfo = dlerror();
    return Err_nosymbol;
  }
  return Err_success;
}

static enum dynamic_err sys_dynamic_ordinal( dynamic_handle* handle, long ordinal
                                           , void** fun, const char** errinfo )
{
  if (errinfo) *errinfo = NULL;
  return Err_noordinal;
}


#elif HAS_DL_H /* eg HPUX */

#include <dl.h>

static void sys_dynamic_close( dynamic_handle handle )
{
  shl_close(handle);
}

static enum dynamic_err sys_dynamic_open ( const char* libpath, dynamic_handle* handle, const char** error_info )
{
  if (error_info) *error_info = NULL;

  *handle = shl_open(libpath,BIND_IMMEDIATE,0L);
  if (*handle == NULL) return Err_nolibrary;

  return Err_success;
}


static enum dynamic_err sys_dynamic_symbol ( dynamic_handle* handle, const char* name
                                           , void** fun, const char** errinfo )
{
  if (errinfo) *errinfo = NULL;
  if (0 == shl_findsym(handle,name,TYPE_PROCEDURE,fun))
   return Err_success;
  else
   return Err_nosymbol;
}

static enum dynamic_err sys_dynamic_ordinal( dynamic_handle* handle, long ordinal
                                           , void** fun, const char** errinfo )
{
  if (errinfo) *errinfo = NULL;
  return Err_noordinal;
}


#else /* Dynamic loading not available */

static void sys_dynamic_close( dynamic_handle handle )
{
}

static enum dynamic_err sys_dynamic_open ( const char* libpath, dynamic_handle* handle, const char** error_info )
{
  return Err_nodynamic;
}


static enum dynamic_err sys_dynamic_symbol ( dynamic_handle* handle, const char* name
                                           , void** fun, const char** errinfo )
{
  return Err_nodynamic;
}

static enum dynamic_err sys_dynamic_ordinal( dynamic_handle* handle, long ordinal
                                           , void** fun, const char** errinfo )
{
  return Err_nodynamic;
}


#endif