Mercurial > pub > dyncall > dyncall
annotate dynload/dynload_unix.c @ 245:0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
* removed mode bit checking on macOS, and assured dlopening files with same flags, instead
* consistency changes for OpenBSD
author | Tassilo Philipp |
---|---|
date | Thu, 04 May 2017 15:11:46 +0200 |
parents | 85b61e8facfe |
children | 06a354b2e120 |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: dynload | |
5 File: dynload/dynload_unix.c | |
6 Description: | |
7 License: | |
8 | |
242 | 9 Copyright (c) 2007-2017 Daniel Adler <dadler@uni-goettingen.de>, |
0 | 10 Tassilo Philipp <tphilipp@potion-studios.com> |
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 dynload_unix.c | |
30 | |
31 dynload module for .so (unix) and .dylib (mach-o darwin/OS X) files | |
32 | |
33 */ | |
34 | |
35 | |
36 #include "dynload.h" | |
242 | 37 #include "../autovar/autovar_OS.h" |
0 | 38 |
242 | 39 #include <string.h> |
40 | |
41 #if defined(__GLIBC__) /* to access dlinfo */ | |
42 # define _GNU_SOURCE | |
43 # define __USE_GNU | |
44 #endif | |
0 | 45 #include <dlfcn.h> |
46 | |
47 | |
48 DLLib* dlLoadLibrary(const char* libPath) | |
49 { | |
232 | 50 return (DLLib*)dlopen(libPath, RTLD_NOW|RTLD_GLOBAL); //@@@ should use RTLD_LAZY, maybe? |
0 | 51 } |
52 | |
53 | |
242 | 54 void* dlFindSymbol(DLLib* pLib, const char* pSymbolName) |
0 | 55 { |
242 | 56 return dlsym((void*)pLib, pSymbolName); |
57 } | |
58 | |
59 | |
60 void dlFreeLibrary(DLLib* pLib) | |
61 { | |
62 /* Check for NULL for cross-platform consistency. *BSD seems to do that in | |
63 dlclose, Linux does not. POSIX states "if handle does not refer to an open | |
64 object, dlclose() returns a non-zero value", which unfortunately sounds | |
65 like it's not explicitly specified. */ | |
66 if(pLib) | |
67 dlclose((void*)pLib); | |
0 | 68 } |
69 | |
70 | |
242 | 71 /* code for dlGetLibraryPath differs on Darwin */ |
72 #if defined(OS_Darwin) | |
73 | |
74 #include <stdint.h> | |
75 #include <mach-o/dyld.h> | |
76 | |
77 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
0 | 78 { |
242 | 79 uint32_t i; |
80 int l = -1; | |
81 | |
82 /*if(pLib == RTLD_DEFAULT) | |
83 return NULL; @@@ return exec's path */ | |
0 | 84 |
242 | 85 /* Darwin's code doesn't come with (non-standard) dlinfo(), so use dyld(1) */ |
86 /* code. There doesn't seem to be a direct way to query the library path, */ | |
87 /* so "double-load" temporarily all already loaded images (just increases */ | |
88 /* ref count) and compare handles until we found ours. Return the name. */ | |
89 for(i=_dyld_image_count(); i>0;) /* iterate libs from end, more likely ours */ | |
90 { | |
91 const char* libPath = _dyld_get_image_name(--i); | |
245
0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
Tassilo Philipp
parents:
242
diff
changeset
|
92 DLLib* lib = dlLoadLibrary(libPath); /* re-open same way for same handle */ |
242 | 93 if(lib) { |
245
0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
Tassilo Philipp
parents:
242
diff
changeset
|
94 dlFreeLibrary(lib); |
0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
Tassilo Philipp
parents:
242
diff
changeset
|
95 if(pLib == lib) { |
242 | 96 l = strlen(libPath); |
97 if(l < bufSize) /* l+'\0' <= bufSize */ | |
98 strcpy(sOut, libPath); | |
99 break; | |
100 } | |
101 } | |
102 } | |
103 | |
104 return l+1; /* strlen + '\0' */ | |
0 | 105 } |
106 | |
242 | 107 #else /* non-Darwin --> */ |
108 | |
109 #if defined(OS_OpenBSD) /* doesn't have dlinfo() but dl_iterate_phdr() --> */ | |
110 | |
111 /* @@@ dl_iterate_phdr() only exists on OpenBSD >= 3.7 */ | |
112 | |
113 #include <sys/types.h> | |
114 #include <link.h> | |
115 | |
116 typedef struct { | |
117 DLLib* pLib; | |
118 char* sOut; | |
119 int bufSize; | |
120 } iter_phdr_data; | |
121 | |
122 static int iter_phdr_cb(struct dl_phdr_info* info, size_t size, void* data) | |
123 { | |
124 int l = -1; | |
125 iter_phdr_data* d = (iter_phdr_data*)data; | |
126 /* unable to relate info->dlpi_addr directly to our dlopen handle, let's */ | |
127 /* do what we do on macOS above, re-dlopen the already loaded lib (just */ | |
128 /* increases ref count) and compare handles. */ | |
245
0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
Tassilo Philipp
parents:
242
diff
changeset
|
129 DLLib* lib = dlLoadLibrary(info->dlpi_name); /* re-open same way for same handle */ |
242 | 130 if(lib) { |
245
0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
Tassilo Philipp
parents:
242
diff
changeset
|
131 dlFreeLibrary(lib); |
0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
Tassilo Philipp
parents:
242
diff
changeset
|
132 if(lib == d->pLib) { |
242 | 133 l = strlen(info->dlpi_name); |
134 if(l < d->bufSize) /* l+'\0' <= bufSize */ | |
135 strcpy(d->sOut, info->dlpi_name); | |
136 } | |
137 } | |
138 return l+1; /* strlen + '\0'; is 0 if lib not found, which continues iter */ | |
139 } | |
140 | |
141 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
142 { | |
143 iter_phdr_data d = { pLib, sOut, bufSize }; | |
144 return dl_iterate_phdr(iter_phdr_cb, &d); | |
145 } | |
146 | |
147 #else /* use dlinfo() --> */ | |
148 | |
149 #include <link.h> | |
150 | |
151 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
152 { | |
153 struct link_map* p; | |
154 int l = -1; | |
155 if(dlinfo(pLib, RTLD_DI_LINKMAP, &p) == 0) { | |
156 l = strlen(p->l_name); | |
157 if(l < bufSize) /* l+'\0' <= bufSize */ | |
158 strcpy(sOut, p->l_name); | |
159 } | |
160 return l+1; /* strlen + '\0' */ | |
161 } | |
162 | |
163 #endif | |
164 | |
165 #endif | |
166 |