Mercurial > pub > dyncall > dyncall
annotate dynload/dynload_unix.c @ 443:c1eba6f08064
- added test/suite_aggr to makefiles
author | Tassilo Philipp |
---|---|
date | Wed, 26 Jan 2022 16:14:07 +0100 |
parents | e221473a8217 |
children | 61c485f8cc06 |
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*/ |
356
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
43 # if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 3) |
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
44 # define DL_USE_GLIBC_ITER_PHDR |
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
45 # endif |
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
|
46 /* to access dl_iterate_phdr(), and related w/ glibc */ |
242 | 47 # define _GNU_SOURCE |
48 # define __USE_GNU | |
49 #endif | |
356
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
50 |
0 | 51 #include <dlfcn.h> |
52 | |
53 | |
54 DLLib* dlLoadLibrary(const char* libPath) | |
55 { | |
232 | 56 return (DLLib*)dlopen(libPath, RTLD_NOW|RTLD_GLOBAL); //@@@ should use RTLD_LAZY, maybe? |
0 | 57 } |
58 | |
59 | |
242 | 60 void* dlFindSymbol(DLLib* pLib, const char* pSymbolName) |
0 | 61 { |
242 | 62 return dlsym((void*)pLib, pSymbolName); |
63 } | |
64 | |
65 | |
66 void dlFreeLibrary(DLLib* pLib) | |
67 { | |
68 /* Check for NULL for cross-platform consistency. *BSD seems to do that in | |
69 dlclose, Linux does not. POSIX states "if handle does not refer to an open | |
70 object, dlclose() returns a non-zero value", which unfortunately sounds | |
71 like it's not explicitly specified. */ | |
72 if(pLib) | |
73 dlclose((void*)pLib); | |
0 | 74 } |
75 | |
76 | |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
77 |
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
|
78 /* 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
|
79 * merely checks lib names */ |
246 | 80 #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
|
81 # define RTLD_LIGHTEST RTLD_LAZY|RTLD_NOLOAD |
246 | 82 #else |
83 # define RTLD_LIGHTEST RTLD_LAZY | |
84 #endif | |
85 | |
86 | |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
87 /* 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
|
88 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
|
89 { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
90 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
|
91 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
|
92 strcpy(dst, src); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
93 return l; |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
94 } |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
95 |
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
|
96 /* 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
|
97 |
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 /* 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
|
99 * 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
|
100 * the handle is valid, thus rendering the returned values useless) check for |
356
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
101 * RTLD_DI_LINKMAP and RTLD_SELF, which are #defines used by dlinfo() on most |
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
102 * supported targets, or specifically check the OS (e.g. dlinfo() is originally |
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
103 * from Solaris) */ |
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
104 #if ((defined(RTLD_DI_LINKMAP) && defined(RTLD_SELF)) || 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
|
105 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
106 #include <link.h> |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
107 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
108 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
109 { |
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
|
110 struct link_map* p = NULL; |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
111 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
|
112 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
|
113 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
|
114 |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
115 return l+1; /* strlen + '\0' */ |
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 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
118 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
119 /* specific implementation needed on Darwin -----> */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
120 #elif defined(OS_Darwin) |
242 | 121 |
122 #include <stdint.h> | |
123 #include <mach-o/dyld.h> | |
124 | |
125 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
0 | 126 { |
242 | 127 uint32_t i; |
128 int l = -1; | |
129 | |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
130 /* 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
|
131 if(pLib == NULL) { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
132 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
|
133 if(libPath) |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
134 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
|
135 } |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
136 else { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
137 /* 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
|
138 * 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
|
139 * 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
|
140 * 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
|
141 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
|
142 { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
143 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
|
144 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
|
145 if(lib) { |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
146 dlclose(lib); |
0 | 147 |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
148 /* 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
|
149 /* be stored - should be safe b/c address needs alignment, anyways) */ |
320
85c80b0c021c
- Darwin/macos fix for dlGetLibraryPath() to correctly fail on bogus handles
Tassilo Philipp
parents:
317
diff
changeset
|
150 if(((uintptr_t)pLib ^ (uintptr_t)lib) < 4) { |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
151 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
|
152 break; |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
153 } |
242 | 154 } |
155 } | |
156 } | |
157 | |
158 return l+1; /* strlen + '\0' */ | |
0 | 159 } |
160 | |
242 | 161 |
356
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
162 /* - OpenBSD >= 3.7 has dl_iterate_phdr(), as well as glibc >= 2.2.4 |
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
163 - also some libc impls (like musl) provide dlinfo(), but not RTLD_SELF (see above), however they might come |
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
164 with dl_iterate_phdr (which comes from ELF program header iteration), so base it on that |
388 | 165 - skip and use dladdr()-based guessing (see below) if explicitly requested, e.g. by ./configure |
166 - Haiku/BeOS does have the headers but no implementation of dl_iterate_phdr() (at least as of 2021) */ | |
167 #elif !defined(DL_DLADDR_TO_LIBPATH) && (defined(OS_OpenBSD) || defined(DL_USE_GLIBC_ITER_PHDR) || (!defined(RTLD_SELF) && defined(__ELF__))) && !defined(OS_BeOS) | |
242 | 168 |
169 #include <sys/types.h> | |
170 #include <link.h> | |
171 | |
172 typedef struct { | |
173 DLLib* pLib; | |
174 char* sOut; | |
175 int bufSize; | |
176 } iter_phdr_data; | |
177 | |
178 static int iter_phdr_cb(struct dl_phdr_info* info, size_t size, void* data) | |
179 { | |
180 int l = -1; | |
181 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
|
182 void* lib = NULL; |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
183 |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
184 /* 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
|
185 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
|
186 /* 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
|
187 * 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
|
188 * increases ref count) and compare handles */ |
356
2f64957d6a46
- fix to dynload to build with musl libc (latter has dlinfo but not RTLD_SELF, so fallback to dl_iterate_phdr if on ELF targets)
Tassilo Philipp
parents:
320
diff
changeset
|
189 /* @@@ might be b/c it's the reloc addr... see below */ |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
190 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
|
191 if(lib) |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
192 dlclose(lib); |
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
193 } |
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 /* 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
|
196 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
|
197 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
|
198 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
|
199 |
405 | 200 /* dlpi_name might be empty for the own process (d->pLib == NULL), so */ |
201 /* try lookup via dladdr(proc_load_addr, ...) */ | |
317
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
202 if(l == 0 && d->pLib == NULL) { |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
203 /* dlpi_addr is the reloc base (0 if PIE), find real virtual load addr */ |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
204 void* vladdr = (void*)info->dlpi_addr; |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
205 int i = 0; |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
206 for(; i < info->dlpi_phnum; ++i) { |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
207 if(info->dlpi_phdr[i].p_type == PT_LOAD) { |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
208 vladdr = (void*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr); |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
209 break; |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
210 } |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
211 } |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
212 Dl_info di; |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
213 if(dladdr(vladdr, &di) != 0) |
3df50603afa9
- dynload fix to get proc name when elf relocation is in use
Tassilo Philipp
parents:
315
diff
changeset
|
214 l = dl_strlen_strcpy(d->sOut, di.dli_fname, d->bufSize); |
242 | 215 } |
216 } | |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
217 |
242 | 218 return l+1; /* strlen + '\0'; is 0 if lib not found, which continues iter */ |
219 } | |
220 | |
221 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
222 { | |
223 iter_phdr_data d = { pLib, sOut, bufSize }; | |
224 return dl_iterate_phdr(iter_phdr_cb, &d); | |
225 } | |
226 | |
248 | 227 |
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
|
228 /* 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
|
229 #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
|
230 |
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
|
231 @@@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
|
232 |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
233 /* fallback to dladdr() hack */ |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
234 #else |
248 | 235 |
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
|
236 #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
|
237 |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
238 /* 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
|
239 /* 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
|
240 /* 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
|
241 |
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
242 /* @@@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
|
243 /* 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
|
244 /* to get that to work (would also introduce a link dependency on libltdl) */ |
248 | 245 |
246 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) | |
247 { | |
315
3840e0188520
- allowing lookup of running executable's path by passing NULL to dynload's dlGetLibraryPath()
Tassilo Philipp
parents:
314
diff
changeset
|
248 /*@@@ missing handler for pLib == NULL*/ |
253
5cfe4322c500
- improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents:
248
diff
changeset
|
249 /* cross fingers that shared object is standard ELF and look for _fini */ |
248 | 250 int l = -1; |
251 void* s = dlsym((void*)pLib, "_fini"); | |
252 if(s) { | |
253 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
|
254 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
|
255 l = dl_strlen_strcpy(sOut, i.dli_fname, bufSize); |
248 | 256 } |
257 return l+1; /* strlen + '\0' */ | |
258 } | |
259 | |
242 | 260 #endif |
261 |