annotate dynload/dynload_unix.c @ 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 - dynload_plain test * now testing getting exec's path * workdir independent
author Tassilo Philipp
date Wed, 06 Nov 2019 12:32:53 +0100
parents 18de5758980e
children 3840e0188520
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 /*
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 Package: dyncall
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 Library: dynload
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 File: dynload/dynload_unix.c
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 Description:
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 License:
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8
281
f5577f6bf97a - file header cleanups for release
Tassilo Philipp
parents: 254
diff changeset
9 Copyright (c) 2007-2018 Daniel Adler <dadler@uni-goettingen.de>,
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 Tassilo Philipp <tphilipp@potion-studios.com>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 Permission to use, copy, modify, and distribute this software for any
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 purpose with or without fee is hereby granted, provided that the above
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 copyright notice and this permission notice appear in all copies.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 */
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 /*
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 dynload_unix.c
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 dynload module for .so (unix) and .dylib (mach-o darwin/OS X) files
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 */
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 #include "dynload.h"
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
37 #include "../autovar/autovar_OS.h"
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
39 #include <string.h>
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
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__)
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
42 /* @@@ version check glibc correctly... dl_iterate_phdr(): glibc ver >= 2.2.4*/
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
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
47 # define _GNU_SOURCE
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
48 # define __USE_GNU
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
49 #endif
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 #include <dlfcn.h>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 DLLib* dlLoadLibrary(const char* libPath)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 {
232
76ed51a690a6 - todo comment
Tassilo Philipp
parents: 171
diff changeset
55 return (DLLib*)dlopen(libPath, RTLD_NOW|RTLD_GLOBAL); //@@@ should use RTLD_LAZY, maybe?
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
59 void* dlFindSymbol(DLLib* pLib, const char* pSymbolName)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 {
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
61 return dlsym((void*)pLib, pSymbolName);
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
62 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
63
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
64
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
65 void dlFreeLibrary(DLLib* pLib)
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
66 {
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
67 /* Check for NULL for cross-platform consistency. *BSD seems to do that in
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
68 dlclose, Linux does not. POSIX states "if handle does not refer to an open
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
69 object, dlclose() returns a non-zero value", which unfortunately sounds
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
70 like it's not explicitly specified. */
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
71 if(pLib)
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
72 dlclose((void*)pLib);
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
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
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
81 #else
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
82 # define RTLD_LIGHTEST RTLD_LAZY
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
83 #endif
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
84
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
85
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
86 /* 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
87
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
88 /* 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
89 * 2.3.3, but its implementation is dangerous, as no checks are done whether
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
90 * the handle is valid, thus rendering the returned values useless): check for
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
91 * 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
92 * 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
93 #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
94
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
95 #include <link.h>
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
96
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
97 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize)
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
98 {
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
99 struct link_map* p = NULL;
253
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
100 int l = -1;
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
101 if(dlinfo(pLib, RTLD_DI_LINKMAP, &p) == 0 && p) {
253
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
102 l = strlen(p->l_name);
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
103 if(l < bufSize) /* l+'\0' <= bufSize */
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
104 strcpy(sOut, p->l_name);
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 return l+1; /* strlen + '\0' */
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
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
109
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
110 /* specific implementation needed on Darwin -----> */
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
111 #elif defined(OS_Darwin)
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
112
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
113 #include <stdint.h>
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
114 #include <mach-o/dyld.h>
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
115
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
116 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 {
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
118 uint32_t i;
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
119 int l = -1;
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
120
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
121 /*if(pLib == RTLD_DEFAULT)
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
122 return NULL; @@@ return exec's path */
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
124 /* Darwin's code doesn't come with (non-standard) dlinfo(), so use dyld(1) */
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
125 /* code. There doesn't seem to be a direct way to query the library path, */
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
126 /* so "double-load" temporarily all already loaded images (just increases */
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
127 /* ref count) and compare handles until we found ours. Return the name. */
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
128 for(i=_dyld_image_count(); i>0;) /* backwards, ours is more likely at end */
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
129 {
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
130 const char* libPath = _dyld_get_image_name(--i);
246
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
131 void* lib = dlopen(libPath, RTLD_LIGHTEST);
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
132 if(lib) {
246
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
133 dlclose(lib);
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
134 /* compare handle pointers' high bits (in low 2 bits some flags might */
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
135 /* be stored - should be safe b/c address needs alignment, anywas) */
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
136 if(((intptr_t)pLib ^ (intptr_t)lib) < 4) {
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
137 l = strlen(libPath);
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
138 if(l < bufSize) /* l+'\0' <= bufSize */
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
139 strcpy(sOut, libPath);
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
140 break;
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
141 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
142 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
143 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
144
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
145 return l+1; /* strlen + '\0' */
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
146 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
147
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
148
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
149 /* 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
150 * 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
151 * ./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
152 #elif !defined(DL_DLADDR_TO_LIBPATH) && (defined(OS_OpenBSD) || defined(DL_USE_GLIBC_ITER_PHDR))
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
153
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
154 #include <sys/types.h>
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
155 #include <link.h>
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
156
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
157 typedef struct {
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
158 DLLib* pLib;
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
159 char* sOut;
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
160 int bufSize;
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
161 } iter_phdr_data;
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
162
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
163 static int iter_phdr_cb(struct dl_phdr_info* info, size_t size, void* data)
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
164 {
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
165 int l = -1;
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
166 iter_phdr_data* d = (iter_phdr_data*)data;
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
167 /* unable to relate info->dlpi_addr directly to our dlopen handle, let's */
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
168 /* do what we do on macOS above, re-dlopen the already loaded lib (just */
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
169 /* increases ref count) and compare handles. */
246
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
170 void* lib = dlopen(info->dlpi_name, RTLD_LIGHTEST);
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
171 if(lib) {
246
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
172 dlclose(lib);
06a354b2e120 changes for dynload for macOS and OpenBSD:
Tassilo Philipp
parents: 245
diff changeset
173 if(lib == (void*)d->pLib) {
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
174 l = strlen(info->dlpi_name);
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
175 if(l < d->bufSize) /* l+'\0' <= bufSize */
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
176 strcpy(d->sOut, info->dlpi_name);
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
177 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
178 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
179 return l+1; /* strlen + '\0'; is 0 if lib not found, which continues iter */
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
180 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
181
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
182 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize)
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
183 {
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
184 iter_phdr_data d = { pLib, sOut, bufSize };
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
185 return dl_iterate_phdr(iter_phdr_cb, &d);
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
186 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
187
248
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
188
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
189 /* 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
190 #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
191
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
192 @@@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
193
253
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
194 /* fallback to dladdr() hack */
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
195 #else
248
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
196
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
197 #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
198
253
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
199 /* 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
200 /* 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
201 /* 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
202
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
203 /* @@@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
204 /* 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
205 /* to get that to work (would also introduce a link dependency on libltdl) */
248
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
206
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
207 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize)
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
208 {
253
5cfe4322c500 - improved support for older OS versions for dynloads dlGetLibraryPath
Tassilo Philipp
parents: 248
diff changeset
209 /* cross fingers that shared object is standard ELF and look for _fini */
248
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
210 int l = -1;
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
211 void* s = dlsym((void*)pLib, "_fini");
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
212 if(s) {
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
213 Dl_info i;
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
214 if(dladdr(s, &i) != 0) {
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
215 l = strlen(i.dli_fname);
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
216 if(l < bufSize) /* l+'\0' <= bufSize */
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
217 strcpy(sOut, i.dli_fname);
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
218 }
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
219 }
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
220 return l+1; /* strlen + '\0' */
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
221 }
ab23f9f2934a - BeOS impl for dlGetLibraryPath
Tassilo Philipp
parents: 246
diff changeset
222
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
223 #endif
85b61e8facfe dynload:
Tassilo Philipp
parents: 232
diff changeset
224