Mercurial > pub > dyncall > dyncall
annotate dynload/dynload_unix.c @ 315:3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
author | Tassilo Philipp |
---|---|
date | Wed, 06 Nov 2019 14:13:49 +0100 |
parents | b2e4e23d9953 |
children | 3df50603afa9 |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: dynload | |
5 File: dynload/dynload_unix.c | |
6 Description: | |
7 License: | |
8 | |
281 | 9 Copyright (c) 2007-2018 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 | |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
41 #if defined(__GLIBC__) |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
42 /* @@@ version check glibc more precisely... dl_iterate_phdr(): glibc ver >= 2.2.4*/ |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
43 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 3) |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
44 # define DL_USE_GLIBC_ITER_PHDR |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
45 #endif |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
46 /* to access dl_iterate_phdr(), and related w/ glibc */ |
242 | 47 # define _GNU_SOURCE |
48 # define __USE_GNU | |
49 #endif | |
0 | 50 #include <dlfcn.h> |
51 | |
52 | |
53 DLLib* dlLoadLibrary(const char* libPath) | |
54 { | |
232 | 55 return (DLLib*)dlopen(libPath, RTLD_NOW|RTLD_GLOBAL); //@@@ should use RTLD_LAZY, maybe? |
0 | 56 } |
57 | |
58 | |
242 | 59 void* dlFindSymbol(DLLib* pLib, const char* pSymbolName) |
0 | 60 { |
242 | 61 return dlsym((void*)pLib, pSymbolName); |
62 } | |
63 | |
64 | |
65 void dlFreeLibrary(DLLib* pLib) | |
66 { | |
67 /* Check for NULL for cross-platform consistency. *BSD seems to do that in | |
68 dlclose, Linux does not. POSIX states "if handle does not refer to an open | |
69 object, dlclose() returns a non-zero value", which unfortunately sounds | |
70 like it's not explicitly specified. */ | |
71 if(pLib) | |
72 dlclose((void*)pLib); | |
0 | 73 } |
74 | |
75 | |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
76 |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
77 /* for dlopen-based dlGetLibraryPath impls below, prefer RTLD_NOLOAD that |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
78 * merely checks lib names */ |
246 | 79 #if defined(RTLD_NOLOAD) |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
80 # define RTLD_LIGHTEST RTLD_LAZY|RTLD_NOLOAD |
246 | 81 #else |
82 # define RTLD_LIGHTEST RTLD_LAZY | |
83 #endif | |
84 | |
85 | |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
86 /* helper copying string if buffer big enough, returning length (without \0) */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
87 static int dl_strlen_strcpy(char* dst, const char* src, int dstSize) |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
88 { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
89 int l = strlen(src); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
90 if(l < dstSize) /* l+'\0' <= bufSize */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
91 strcpy(dst, src); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
92 return l; |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
93 } |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
94 |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
95 /* code for dlGetLibraryPath() is platform specific */ |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
96 |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
97 /* if dlinfo() exists use it (except on glibc, where it exists since version |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
98 * 2.3.3, but its implementation is dangerous, as no checks are done whether |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
99 * the handle is valid, thus rendering the returned values useless) check for |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
100 * RTLD_DI_LINKMAP which is a #define for dlinfo() on most supported targets, |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
101 * or specifically check the OS (e.g. dlinfo() is originally from Solaris) */ |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
102 #if (defined(RTLD_DI_LINKMAP) || defined(OS_SunOS)) && !defined(DL_USE_GLIBC_ITER_PHDR) |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
103 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
104 #include <link.h> |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
105 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
106 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
107 { |
312
18de5758980e
- stability fix: avoid sigsegv in dynload's dlGetLibraryPath() in some cases (e.g. wrong handle given or OS specific quirk)
Tassilo Philipp
parents:
281
diff
changeset
|
108 struct link_map* p = NULL; |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
109 int l = -1; |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
110 if(dlinfo(pLib ? pLib : RTLD_SELF, RTLD_DI_LINKMAP, &p) == 0) |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
111 l = dl_strlen_strcpy(sOut, p->l_name, bufSize); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
112 |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
113 return l+1; /* strlen + '\0' */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
114 } |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
115 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
116 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
117 /* specific implementation needed on Darwin -----> */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
118 #elif defined(OS_Darwin) |
242 | 119 |
120 #include <stdint.h> | |
121 #include <mach-o/dyld.h> | |
122 | |
123 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
0 | 124 { |
242 | 125 uint32_t i; |
126 int l = -1; | |
127 | |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
128 /* request info about own process? lookup first loaded image */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
129 if(pLib == NULL) { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
130 const char* libPath = _dyld_get_image_name(0); //@@@ consider using _NSGetExecutablePath() |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
131 if(libPath) |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
132 l = dl_strlen_strcpy(sOut, libPath, bufSize); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
133 } |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
134 else { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
135 /* Darwin's code doesn't come with (non-standard) dlinfo(), so use dyld(1) |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
136 * code. There doesn't seem to be a direct way to query the library path, |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
137 * so "double-load" temporarily all already loaded images (just increases |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
138 * ref count) and compare handles until we found ours. Return the name. */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
139 for(i=_dyld_image_count(); i>0;) /* backwards, ours is more likely at end */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
140 { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
141 const char* libPath = _dyld_get_image_name(--i); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
142 void* lib = dlopen(libPath, RTLD_LIGHTEST); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
143 if(lib) { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
144 dlclose(lib); |
0 | 145 |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
146 /* compare handle pointers' high bits (in low 2 bits some flags might */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
147 /* be stored - should be safe b/c address needs alignment, anyways) */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
148 if(((intptr_t)pLib ^ (intptr_t)lib) < 4) { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
149 l = dl_strlen_strcpy(sOut, libPath, bufSize); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
150 break; |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
151 } |
242 | 152 } |
153 } | |
154 } | |
155 | |
156 return l+1; /* strlen + '\0' */ | |
0 | 157 } |
158 | |
242 | 159 |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
160 /* OpenBSD >= 3.7 has dl_iterate_phdr(), as well as glibc >= 2.2.4, however |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
161 * skip and use on dladdr()-based guessing if if explicitly requested, e.g. by |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
162 * ./configure */ |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
163 #elif !defined(DL_DLADDR_TO_LIBPATH) && (defined(OS_OpenBSD) || defined(DL_USE_GLIBC_ITER_PHDR)) |
242 | 164 |
165 #include <sys/types.h> | |
166 #include <link.h> | |
167 | |
168 typedef struct { | |
169 DLLib* pLib; | |
170 char* sOut; | |
171 int bufSize; | |
172 } iter_phdr_data; | |
173 | |
174 static int iter_phdr_cb(struct dl_phdr_info* info, size_t size, void* data) | |
175 { | |
176 int l = -1; | |
177 iter_phdr_data* d = (iter_phdr_data*)data; | |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
178 void* lib = NULL; |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
179 |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
180 /* get loaded object's handle if not requesting info about process itself */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
181 if(d->pLib != NULL) { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
182 /* unable to relate info->dlpi_addr directly to our dlopen handle, let's |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
183 * do what we do on macOS above, re-dlopen the already loaded lib (just |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
184 * increases ref count) and compare handles */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
185 lib = dlopen(info->dlpi_name, RTLD_LIGHTEST); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
186 if(lib) |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
187 dlclose(lib); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
188 } |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
189 |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
190 /* compare handles and get name if found; if d->pLib == NULL this will |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
191 enter info on first iterated object, which is the process itself */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
192 if(lib == (void*)d->pLib) { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
193 l = dl_strlen_strcpy(d->sOut, info->dlpi_name, d->bufSize); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
194 |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
195 /* on some platforms (e.g. Linux) dlpi_name is empty for the main process' |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
196 object, but dlpi_addr is given, in that case lookup name via dladdr */ |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
197 if(l == 0 && d->pLib == NULL && (void*)info->dlpi_addr != NULL) { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
198 Dl_info i; |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
199 if(dladdr((void*)info->dlpi_addr, &i) != 0) |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
200 l = dl_strlen_strcpy(d->sOut, i.dli_fname, d->bufSize); |
242 | 201 } |
202 } | |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
203 |
242 | 204 return l+1; /* strlen + '\0'; is 0 if lib not found, which continues iter */ |
205 } | |
206 | |
207 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
208 { | |
209 iter_phdr_data d = { pLib, sOut, bufSize }; | |
210 return dl_iterate_phdr(iter_phdr_cb, &d); | |
211 } | |
212 | |
248 | 213 |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
214 /* glibc with neither dl_iterate_phdr() nor dlinfo() (latter introduced after former) @@@ |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
215 #elif defined(__GLIBC__) && !defined(DL_USE_GLIBC_ITER_PHDR) |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
216 |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
217 @@@impl */ |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
218 |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
219 /* fallback to dladdr() hack */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
220 #else |
248 | 221 |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
222 #warning "Using non-optimal code for dlGetLibraryPath() b/c of platform limitations." |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
312
diff
changeset
|
223 |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
224 /* 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
|
225 /* 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
|
226 /* 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
|
227 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
228 /* @@@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
|
229 /* 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
|
230 /* to get that to work (would also introduce a link dependency on libltdl) */ |
248 | 231 |
232 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
233 { | |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
234 /*@@@ missing handler for pLib == NULL*/ |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
235 /* cross fingers that shared object is standard ELF and look for _fini */ |
248 | 236 int l = -1; |
237 void* s = dlsym((void*)pLib, "_fini"); | |
238 if(s) { | |
239 Dl_info i; | |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
240 if(dladdr(s, &i) != 0) |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
241 l = dl_strlen_strcpy(sOut, i.dli_fname, bufSize); |
248 | 242 } |
243 return l+1; /* strlen + '\0' */ | |
244 } | |
245 | |
242 | 246 #endif |
247 |