comparison 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
comparison
equal deleted inserted replaced
313:73b5b9e224e2 314:b2e4e23d9953
36 #include "dynload.h" 36 #include "dynload.h"
37 #include "../autovar/autovar_OS.h" 37 #include "../autovar/autovar_OS.h"
38 38
39 #include <string.h> 39 #include <string.h>
40 40
41 #if defined(__GLIBC__) /* to access dlinfo */ 41 #if defined(__GLIBC__)
42 /* @@@ version check glibc correctly... dl_iterate_phdr(): glibc ver >= 2.2.4*/
43 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 3)
44 # define DL_USE_GLIBC_ITER_PHDR
45 #endif
46 /* to access dl_iterate_phdr(), and related w/ glibc */
42 # define _GNU_SOURCE 47 # define _GNU_SOURCE
43 # define __USE_GNU 48 # define __USE_GNU
44 #endif 49 #endif
45 #include <dlfcn.h> 50 #include <dlfcn.h>
46 51
67 dlclose((void*)pLib); 72 dlclose((void*)pLib);
68 } 73 }
69 74
70 75
71 76
72 /* for dlopen-based dlGetLibraryPath impls below, prefer RTLD_NOLOAD */ 77 /* for dlopen-based dlGetLibraryPath impls below, prefer RTLD_NOLOAD that
73 /* that merely checks lib names */ 78 * merely checks lib names */
74 #if defined(RTLD_NOLOAD) 79 #if defined(RTLD_NOLOAD)
75 # define RTLD_LIGHTEST RTLD_NOLOAD 80 # define RTLD_LIGHTEST RTLD_LAZY|RTLD_NOLOAD
76 #else 81 #else
77 # define RTLD_LIGHTEST RTLD_LAZY 82 # define RTLD_LIGHTEST RTLD_LAZY
78 #endif 83 #endif
79 84
80 85
81 /* code for dlGetLibraryPath is platform specific - if dlinfo() exists use */ 86 /* code for dlGetLibraryPath() is platform specific */
82 /* that: check for RTLD_DI_LINKMAP (#define for dlinfo()), or if GNU C Lib */ 87
83 /* is used (where RTLD_DI_LINKMAP is an enum), or by OS (dlinfo comes from */ 88 /* if dlinfo() exists use it (except on glibc, where it exists since version
84 /* Solaris), etc. */ 89 * 2.3.3, but its implementation is dangerous, as no checks are done whether
85 #if defined(RTLD_DI_LINKMAP) || defined(OS_SunOS) || defined(__GLIBC__) /* @@@ dlinfo() was introduced in glibc 2.3.3 (in 2003), somehow check for that, also */ 90 * the handle is valid, thus rendering the returned values useless): check for
91 * RTLD_DI_LINKMAP which is a #define for dlinfo() on most supported targets,
92 * or specifically check the OS (e.g. dlinfo() is originally from Solaris) */
93 #if (defined(RTLD_DI_LINKMAP) || defined(OS_SunOS)) && !defined(DL_USE_GLIBC_ITER_PHDR)
86 94
87 #include <link.h> 95 #include <link.h>
88 96
89 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize) 97 int dlGetLibraryPath(DLLib* pLib, char* sOut, int bufSize)
90 { 98 {
91 struct link_map* p = NULL; 99 struct link_map* p = NULL;
92 int l = -1; 100 int l = -1;
93 /* on some platforms dlinfo() "succeeds" for any handle, returning a */ 101 if(dlinfo(pLib, RTLD_DI_LINKMAP, &p) == 0 && p) {
94 /* legit pointer to a struct w/o any fields set; fail if unset */
95 if(dlinfo(pLib, RTLD_DI_LINKMAP, &p) == 0 && p && p->l_name) {
96 l = strlen(p->l_name); 102 l = strlen(p->l_name);
97 if(l < bufSize) /* l+'\0' <= bufSize */ 103 if(l < bufSize) /* l+'\0' <= bufSize */
98 strcpy(sOut, p->l_name); 104 strcpy(sOut, p->l_name);
99 } 105 }
100 return l+1; /* strlen + '\0' */ 106 return l+1; /* strlen + '\0' */
117 123
118 /* Darwin's code doesn't come with (non-standard) dlinfo(), so use dyld(1) */ 124 /* Darwin's code doesn't come with (non-standard) dlinfo(), so use dyld(1) */
119 /* code. There doesn't seem to be a direct way to query the library path, */ 125 /* code. There doesn't seem to be a direct way to query the library path, */
120 /* so "double-load" temporarily all already loaded images (just increases */ 126 /* so "double-load" temporarily all already loaded images (just increases */
121 /* ref count) and compare handles until we found ours. Return the name. */ 127 /* ref count) and compare handles until we found ours. Return the name. */
122 for(i=_dyld_image_count(); i>0;) /* iterate libs from end, more likely ours */ 128 for(i=_dyld_image_count(); i>0;) /* backwards, ours is more likely at end */
123 { 129 {
124 const char* libPath = _dyld_get_image_name(--i); 130 const char* libPath = _dyld_get_image_name(--i);
125 void* lib = dlopen(libPath, RTLD_LIGHTEST); 131 void* lib = dlopen(libPath, RTLD_LIGHTEST);
126 if(lib) { 132 if(lib) {
127 dlclose(lib); 133 dlclose(lib);
138 144
139 return l+1; /* strlen + '\0' */ 145 return l+1; /* strlen + '\0' */
140 } 146 }
141 147
142 148
143 /* OpenBSD >= 3.7 has dl_iterate_phdr(), use it if not explicitly requesting */ 149 /* OpenBSD >= 3.7 has dl_iterate_phdr(), as well as glibc >= 2.2.4, however
144 /* to use dladdr()-based guessing (set by configure) -----> */ 150 * skip and use on dladdr()-based guessing if if explicitly requested, e.g. by
145 #elif defined(OS_OpenBSD) && !defined(DL_DLADDR_TO_LIBPATH) 151 * ./configure */
152 #elif !defined(DL_DLADDR_TO_LIBPATH) && (defined(OS_OpenBSD) || defined(DL_USE_GLIBC_ITER_PHDR))
146 153
147 #include <sys/types.h> 154 #include <sys/types.h>
148 #include <link.h> 155 #include <link.h>
149 156
150 typedef struct { 157 typedef struct {
177 iter_phdr_data d = { pLib, sOut, bufSize }; 184 iter_phdr_data d = { pLib, sOut, bufSize };
178 return dl_iterate_phdr(iter_phdr_cb, &d); 185 return dl_iterate_phdr(iter_phdr_cb, &d);
179 } 186 }
180 187
181 188
189 /* glibc with neither dl_iterate_phdr() nor dlinfo() (latter introduced after former) @@@
190 #elif defined(__GLIBC__) && !defined(DL_USE_GLIBC_ITER_PHDR)
191
192 @@@impl */
193
182 /* fallback to dladdr() hack */ 194 /* fallback to dladdr() hack */
183 #else 195 #else
196
197 #warning "Using non-optimal code for dlGetLibraryPath() b/c of platform limitations."
184 198
185 /* if nothing else is available, fall back to guessing using dladdr() - this */ 199 /* if nothing else is available, fall back to guessing using dladdr() - this */
186 /* might not always work, as it's trying to getit via the _fini() symbol, */ 200 /* might not always work, as it's trying to getit via the _fini() symbol, */
187 /* which is usually defined in ELF files, but not guaranteed */ 201 /* which is usually defined in ELF files, but not guaranteed */
188 202