Mercurial > pub > dyncall > dyncall
annotate dynload/dynload_unix.c @ 253:5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
author | Tassilo Philipp |
---|---|
date | Mon, 15 May 2017 02:50:03 +0200 |
parents | ab23f9f2934a |
children | 9d70178c1ded |
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 | |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
71 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
72 /* for dlopen-based dlGetLibraryPath impls below, prefer RTLD_NOLOAD */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
73 /* that merely checks lib names */ |
246 | 74 #if defined(RTLD_NOLOAD) |
75 # define RTLD_LIGHTEST RTLD_NOLOAD | |
76 #else | |
77 # define RTLD_LIGHTEST RTLD_LAZY | |
78 #endif | |
79 | |
80 | |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
81 /* code for dlGetLibraryPath is platform specific - if dlinfo() exists use */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
82 /* that (checked through existance of RTLD_DI_LINKMAP, usually a #define */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
83 /* for dlinfo(), or by OS (always on Solaris where it's from, usually on */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
84 /* Linux, where the flag might be an enum instead, ...) */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
85 #if defined(RTLD_DI_LINKMAP) || defined(OS_SunOS) || (defined(OS_Linux) && !defined(DL_DLADDR_TO_LIBPATH)) |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
86 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
87 #include <link.h> |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
88 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
89 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
90 { |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
91 struct link_map* p; |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
92 int l = -1; |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
93 if(dlinfo(pLib, RTLD_DI_LINKMAP, &p) == 0) { |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
94 l = strlen(p->l_name); |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
95 if(l < bufSize) /* l+'\0' <= bufSize */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
96 strcpy(sOut, p->l_name); |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
97 } |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
98 return l+1; /* strlen + '\0' */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
99 } |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
100 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
101 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
102 /* specific implementation needed on Darwin -----> */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
103 #elif defined(OS_Darwin) |
242 | 104 |
105 #include <stdint.h> | |
106 #include <mach-o/dyld.h> | |
107 | |
108 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
0 | 109 { |
242 | 110 uint32_t i; |
111 int l = -1; | |
112 | |
113 /*if(pLib == RTLD_DEFAULT) | |
114 return NULL; @@@ return exec's path */ | |
0 | 115 |
242 | 116 /* Darwin's code doesn't come with (non-standard) dlinfo(), so use dyld(1) */ |
117 /* code. There doesn't seem to be a direct way to query the library path, */ | |
118 /* so "double-load" temporarily all already loaded images (just increases */ | |
119 /* ref count) and compare handles until we found ours. Return the name. */ | |
120 for(i=_dyld_image_count(); i>0;) /* iterate libs from end, more likely ours */ | |
121 { | |
122 const char* libPath = _dyld_get_image_name(--i); | |
246 | 123 void* lib = dlopen(libPath, RTLD_LIGHTEST); |
242 | 124 if(lib) { |
246 | 125 dlclose(lib); |
126 /* compare handle pointers' high bits (in low 2 bits some flags might */ | |
127 /* be stored - should be safe b/c address needs alignment, anywas) */ | |
128 if(((intptr_t)pLib ^ (intptr_t)lib) < 4) { | |
242 | 129 l = strlen(libPath); |
130 if(l < bufSize) /* l+'\0' <= bufSize */ | |
131 strcpy(sOut, libPath); | |
132 break; | |
133 } | |
134 } | |
135 } | |
136 | |
137 return l+1; /* strlen + '\0' */ | |
0 | 138 } |
139 | |
242 | 140 |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
141 /* OpenBSD >= 3.7 has dl_iterate_phdr(), use it if not explicitly requesting */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
142 /* to use dladdr()-based guessing (set by configure) -----> */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
143 #elif defined(OS_OpenBSD) && !defined(DL_DLADDR_TO_LIBPATH) |
242 | 144 |
145 #include <sys/types.h> | |
146 #include <link.h> | |
147 | |
148 typedef struct { | |
149 DLLib* pLib; | |
150 char* sOut; | |
151 int bufSize; | |
152 } iter_phdr_data; | |
153 | |
154 static int iter_phdr_cb(struct dl_phdr_info* info, size_t size, void* data) | |
155 { | |
156 int l = -1; | |
157 iter_phdr_data* d = (iter_phdr_data*)data; | |
158 /* unable to relate info->dlpi_addr directly to our dlopen handle, let's */ | |
159 /* do what we do on macOS above, re-dlopen the already loaded lib (just */ | |
160 /* increases ref count) and compare handles. */ | |
246 | 161 void* lib = dlopen(info->dlpi_name, RTLD_LIGHTEST); |
242 | 162 if(lib) { |
246 | 163 dlclose(lib); |
164 if(lib == (void*)d->pLib) { | |
242 | 165 l = strlen(info->dlpi_name); |
166 if(l < d->bufSize) /* l+'\0' <= bufSize */ | |
167 strcpy(d->sOut, info->dlpi_name); | |
168 } | |
169 } | |
170 return l+1; /* strlen + '\0'; is 0 if lib not found, which continues iter */ | |
171 } | |
172 | |
173 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
174 { | |
175 iter_phdr_data d = { pLib, sOut, bufSize }; | |
176 return dl_iterate_phdr(iter_phdr_cb, &d); | |
177 } | |
178 | |
248 | 179 |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
180 /* fallback to dladdr() hack */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
181 #else |
248 | 182 |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
183 /* if nothing else is available, fall back to guessing using dladdr() - this */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
184 /* might not always work, as it's trying to getit via the _fini() symbol, */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
185 /* which is usually defined in ELF files, but not guaranteed */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
186 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
187 /* @@@Note: On some platforms this might be improved, e.g. on BeOS we have */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
188 /* lt_dlgetinfo, which requires iterating over ltdl stuff, but was unable */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
189 /* to get that to work (would also introduce a link dependency on libltdl) */ |
248 | 190 |
191 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
192 { | |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
193 /* cross fingers that shared object is standard ELF and look for _fini */ |
248 | 194 int l = -1; |
195 void* s = dlsym((void*)pLib, "_fini"); | |
196 if(s) { | |
197 Dl_info i; | |
198 if(dladdr(s, &i) != 0) { | |
199 l = strlen(i.dli_fname); | |
200 if(l < bufSize) /* l+'\0' <= bufSize */ | |
201 strcpy(sOut, i.dli_fname); | |
202 } | |
203 } | |
204 return l+1; /* strlen + '\0' */ | |
205 } | |
206 | |
242 | 207 #endif |
208 |