| 1 |
pippijn |
1.1 |
/* |
| 2 |
|
|
* linker.C: Abstraction of the dynamic linking system. |
| 3 |
pippijn |
1.2 |
* Rights to this code are documented in doc/pod/license.pod. |
| 4 |
pippijn |
1.1 |
* |
| 5 |
|
|
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
static char const rcsid[] = "$Id"; |
| 9 |
|
|
|
| 10 |
|
|
#include "atheme.h" |
| 11 |
|
|
|
| 12 |
|
|
#include <dlfcn.h> |
| 13 |
|
|
|
| 14 |
|
|
#ifdef __OpenBSD__ |
| 15 |
|
|
# define RTLD_NOW RTLD_LAZY |
| 16 |
|
|
#endif |
| 17 |
|
|
|
| 18 |
|
|
#ifndef __HPUX__ |
| 19 |
|
|
# define PLATFORM_SUFFIX ".so" |
| 20 |
|
|
#else |
| 21 |
|
|
# define PLATFORM_SUFFIX ".sl" |
| 22 |
|
|
#endif |
| 23 |
|
|
|
| 24 |
|
|
/* |
| 25 |
|
|
* linker_open() |
| 26 |
|
|
* |
| 27 |
|
|
* Inputs: |
| 28 |
|
|
* path to file to open |
| 29 |
|
|
* |
| 30 |
|
|
* Outputs: |
| 31 |
|
|
* linker handle. |
| 32 |
|
|
* |
| 33 |
|
|
* Side Effects: |
| 34 |
|
|
* a shared module is loaded into the application's memory space |
| 35 |
|
|
*/ |
| 36 |
|
|
void * |
| 37 |
|
|
linker_open (char *path) |
| 38 |
|
|
{ |
| 39 |
|
|
return dlopen (path, RTLD_NOW); |
| 40 |
|
|
} |
| 41 |
|
|
|
| 42 |
|
|
/* |
| 43 |
|
|
* linker_open_ext() |
| 44 |
|
|
* |
| 45 |
|
|
* Inputs: |
| 46 |
|
|
* path to file to open |
| 47 |
|
|
* |
| 48 |
|
|
* Outputs: |
| 49 |
|
|
* linker handle |
| 50 |
|
|
* |
| 51 |
|
|
* Side Effects: |
| 52 |
|
|
* the extension is appended if it's not already there. |
| 53 |
|
|
* a shared module is loaded into the application's memory space |
| 54 |
|
|
*/ |
| 55 |
|
|
void * |
| 56 |
|
|
linker_open_ext (char *path) |
| 57 |
|
|
{ |
| 58 |
|
|
void *handle; |
| 59 |
|
|
char *buf = static_cast<char *> (smalloc (strlen (path) + 20)); |
| 60 |
|
|
|
| 61 |
|
|
strlcpy (buf, path, strlen (path) + 20); |
| 62 |
|
|
|
| 63 |
|
|
if (!strstr (buf, PLATFORM_SUFFIX)) |
| 64 |
|
|
strlcat (buf, PLATFORM_SUFFIX, strlen (path) + 20); |
| 65 |
|
|
|
| 66 |
|
|
handle = linker_open (buf); |
| 67 |
|
|
free (buf); |
| 68 |
|
|
|
| 69 |
|
|
return handle; |
| 70 |
|
|
} |
| 71 |
|
|
|
| 72 |
|
|
/* |
| 73 |
|
|
* linker_getsym() |
| 74 |
|
|
* |
| 75 |
|
|
* Inputs: |
| 76 |
|
|
* linker handle, symbol to retrieve |
| 77 |
|
|
* |
| 78 |
|
|
* Outputs: |
| 79 |
|
|
* pointer to symbol, or NULL if no symbol. |
| 80 |
|
|
* |
| 81 |
|
|
* Side Effects: |
| 82 |
|
|
* none |
| 83 |
|
|
*/ |
| 84 |
|
|
void * |
| 85 |
|
|
linker_getsym (void *vptr, char *sym) |
| 86 |
|
|
{ |
| 87 |
|
|
return dlsym (vptr, sym); |
| 88 |
|
|
} |
| 89 |
|
|
|
| 90 |
|
|
/* |
| 91 |
|
|
* linker_close() |
| 92 |
|
|
* |
| 93 |
|
|
* Inputs: |
| 94 |
|
|
* linker handle |
| 95 |
|
|
* |
| 96 |
|
|
* Outputs: |
| 97 |
|
|
* none |
| 98 |
|
|
* |
| 99 |
|
|
* Side Effects: |
| 100 |
|
|
* the module is unloaded from the linker |
| 101 |
|
|
*/ |
| 102 |
|
|
void |
| 103 |
|
|
linker_close (void *vptr) |
| 104 |
|
|
{ |
| 105 |
|
|
dlclose (vptr); |
| 106 |
|
|
} |