0
|
1 /*
|
|
2
|
|
3 Package: dyncall
|
|
4 Library: dynload
|
|
5 File: dynload/dynload_darwin.c
|
|
6 Description: dynload module for .dylib (mach-o darwin/OS X) files
|
|
7 License:
|
|
8
|
|
9 Copyright (c) 2007-2015 Olivier Chafik <olivier.chafik@gmail.com>
|
|
10 Minor bug-fix modifications by Daniel Adler.
|
|
11
|
|
12 Permission to use, copy, modify, and distribute this software for any
|
|
13 purpose with or without fee is hereby granted, provided that the above
|
|
14 copyright notice and this permission notice appear in all copies.
|
|
15
|
|
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
23
|
|
24 */
|
|
25
|
|
26
|
|
27
|
|
28 /*
|
|
29
|
|
30 dynload_darwin.c
|
|
31
|
|
32 dynload module for .dylib (mach-o darwin/OS X) files
|
|
33
|
|
34 */
|
|
35
|
|
36
|
|
37 #include "dynload.h"
|
|
38 #include "dynload_alloc.h"
|
|
39 #include <dlfcn.h>
|
|
40 #include <string.h>
|
|
41
|
|
42 struct DLLib_
|
|
43 {
|
|
44 char* libPath;
|
|
45 void* handle;
|
|
46 };
|
|
47
|
|
48
|
|
49 DLLib* dlLoadLibrary(const char* libPath)
|
|
50 {
|
|
51 void* handle;
|
|
52 size_t len;
|
|
53 DLLib* lib;
|
|
54
|
|
55 handle = dlopen(libPath, RTLD_LAZY);
|
|
56 if (!handle)
|
|
57 return NULL;
|
|
58
|
|
59
|
|
60 lib = (DLLib*)dlAllocMem(sizeof(DLLib));
|
|
61 lib->handle = handle;
|
|
62 /* libPath might be null (self reference on image) [Daniel] */
|
|
63 if (libPath != NULL) {
|
|
64 len = strlen(libPath);
|
|
65 lib->libPath = (char*)dlAllocMem(len + 1);
|
|
66 strcpy(lib->libPath, libPath);
|
|
67 lib->libPath[len] = '\0';
|
|
68 } else {
|
|
69 lib->libPath = NULL;
|
|
70 }
|
|
71 return lib;
|
|
72 }
|
|
73
|
|
74 void* dlFindSymbol(DLLib* libHandle, const char* symbol)
|
|
75 {
|
|
76 return dlsym(libHandle && libHandle->handle ? libHandle->handle : RTLD_DEFAULT, symbol);
|
|
77 }
|
|
78
|
|
79
|
|
80 void dlFreeLibrary(DLLib* libHandle)
|
|
81 {
|
|
82 if (!libHandle)
|
|
83 return;
|
|
84
|
|
85 dlclose(libHandle->handle);
|
|
86 if (libHandle->libPath)
|
|
87 dlFreeMem(libHandle->libPath);
|
|
88 dlFreeMem(libHandle);
|
|
89 }
|
|
90
|