1 /* $NetBSD: load.c,v 1.25 2002/12/05 04:56:56 junyoung Exp $ */ 2 3 /* 4 * Copyright 1996 John D. Polstra. 5 * Copyright 1996 Matt Thomas <matt@3am-software.com> 6 * Copyright 2002 Charles M. Hannum <root@ihack.net> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by John Polstra. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Dynamic linker for ELF. 37 * 38 * John Polstra <jdp@polstra.com>. 39 */ 40 41 #include <err.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <stdarg.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include <sys/types.h> 50 #include <sys/param.h> 51 #include <sys/mman.h> 52 #include <sys/sysctl.h> 53 #include <dirent.h> 54 55 #include "debug.h" 56 #include "rtld.h" 57 58 static bool _rtld_load_by_name __P((const char *, Obj_Entry *, Needed_Entry **, 59 int)); 60 61 #ifdef RTLD_LOADER 62 Objlist _rtld_list_main = /* Objects loaded at program startup */ 63 SIMPLEQ_HEAD_INITIALIZER(_rtld_list_main); 64 Objlist _rtld_list_global = /* Objects dlopened with RTLD_GLOBAL */ 65 SIMPLEQ_HEAD_INITIALIZER(_rtld_list_global); 66 67 void 68 _rtld_objlist_add(list, obj) 69 Objlist *list; 70 Obj_Entry *obj; 71 { 72 Objlist_Entry *elm; 73 74 elm = NEW(Objlist_Entry); 75 elm->obj = obj; 76 SIMPLEQ_INSERT_TAIL(list, elm, link); 77 } 78 79 Objlist_Entry * 80 _rtld_objlist_find(Objlist *list, const Obj_Entry *obj) 81 { 82 Objlist_Entry *elm; 83 84 SIMPLEQ_FOREACH(elm, list, link) { 85 if (elm->obj == obj) 86 return elm; 87 } 88 return NULL; 89 } 90 #endif 91 92 /* 93 * Load a shared object into memory, if it is not already loaded. The 94 * argument must be a string allocated on the heap. This function assumes 95 * responsibility for freeing it when necessary. 96 * 97 * Returns a pointer to the Obj_Entry for the object. Returns NULL 98 * on failure. 99 */ 100 Obj_Entry * 101 _rtld_load_object(filepath, mode) 102 char *filepath; 103 int mode; 104 { 105 Obj_Entry *obj; 106 int fd = -1; 107 struct stat sb; 108 size_t pathlen = strlen(filepath); 109 110 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) 111 if (pathlen == obj->pathlen && !strcmp(obj->path, filepath)) 112 break; 113 114 /* 115 * If we didn't find a match by pathname, open the file and check 116 * again by device and inode. This avoids false mismatches caused 117 * by multiple links or ".." in pathnames. 118 * 119 * To avoid a race, we open the file and use fstat() rather than 120 * using stat(). 121 */ 122 if (obj == NULL) { 123 if ((fd = open(filepath, O_RDONLY)) == -1) { 124 _rtld_error("Cannot open \"%s\"", filepath); 125 return NULL; 126 } 127 if (fstat(fd, &sb) == -1) { 128 _rtld_error("Cannot fstat \"%s\"", filepath); 129 close(fd); 130 return NULL; 131 } 132 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) { 133 if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) { 134 close(fd); 135 break; 136 } 137 } 138 } 139 140 if (obj == NULL) { /* First use of this object, so we must map it in */ 141 obj = _rtld_map_object(filepath, fd, &sb); 142 (void)close(fd); 143 if (obj == NULL) { 144 free(filepath); 145 return NULL; 146 } 147 _rtld_digest_dynamic(obj); 148 149 *_rtld_objtail = obj; 150 _rtld_objtail = &obj->next; 151 #ifdef RTLD_LOADER 152 _rtld_linkmap_add(obj); /* for GDB */ 153 #endif 154 dbg((" %p .. %p: %s", obj->mapbase, 155 obj->mapbase + obj->mapsize - 1, obj->path)); 156 if (obj->textrel) 157 dbg((" WARNING: %s has impure text", obj->path)); 158 } else 159 free(filepath); 160 161 ++obj->refcount; 162 #ifdef RTLD_LOADER 163 if (mode & RTLD_MAIN && !obj->mainref) { 164 obj->mainref = 1; 165 rdbg(("adding %p (%s) to _rtld_list_main", obj, obj->path)); 166 _rtld_objlist_add(&_rtld_list_main, obj); 167 } 168 if (mode & RTLD_GLOBAL && !obj->globalref) { 169 obj->globalref = 1; 170 rdbg(("adding %p (%s) to _rtld_list_global", obj, obj->path)); 171 _rtld_objlist_add(&_rtld_list_global, obj); 172 } 173 #endif 174 return obj; 175 } 176 177 static bool 178 _rtld_load_by_name(name, obj, needed, mode) 179 const char *name; 180 Obj_Entry *obj; 181 Needed_Entry **needed; 182 int mode; 183 { 184 Library_Xform *x = _rtld_xforms; 185 Obj_Entry *o = NULL; 186 size_t i, j; 187 bool got = false; 188 union { 189 int i; 190 char s[16]; 191 } val; 192 193 dbg(("load by name %s %p", name, x)); 194 for (; x; x = x->next) { 195 if (strcmp(x->name, name) != 0) 196 continue; 197 198 i = sizeof(val); 199 200 if (sysctl(x->ctl, x->ctlmax, &val, &i, NULL, 0) == -1) { 201 xwarnx(_PATH_LD_HINTS ": unknown sysctl for %s", name); 202 break; 203 } 204 205 switch (x->ctltype[x->ctlmax - 1]) { 206 case CTLTYPE_INT: 207 xsnprintf(val.s, sizeof(val.s), "%d", val.i); 208 break; 209 case CTLTYPE_STRING: 210 break; 211 default: 212 xwarnx("unsupported sysctl type %d", 213 x->ctltype[x->ctlmax - 1]); 214 break; 215 } 216 217 dbg(("sysctl returns %s", val.s)); 218 219 for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL; 220 i++) { 221 dbg(("entry %ld", (unsigned long)i)); 222 if (strcmp(x->entry[i].value, val.s) == 0) 223 break; 224 } 225 226 if (i == RTLD_MAX_ENTRY) { 227 xwarnx("sysctl value %s not found for lib%s", 228 val.s, name); 229 break; 230 } 231 /* XXX: This can mess up debuggers, cause we lie about 232 * what we loaded in the needed objects */ 233 for (j = 0; j < RTLD_MAX_LIBRARY && 234 x->entry[i].library[j] != NULL; j++) { 235 o = _rtld_load_library(x->entry[i].library[j], obj, 236 mode); 237 if (o == NULL) { 238 xwarnx("could not load %s for %s", 239 x->entry[i].library[j], name); 240 continue; 241 } 242 got = true; 243 if (j == 0) 244 (*needed)->obj = o; 245 else { 246 /* make a new one and put it in the chain */ 247 Needed_Entry *ne = xmalloc(sizeof(*ne)); 248 ne->name = (*needed)->name; 249 ne->obj = o; 250 ne->next = (*needed)->next; 251 (*needed)->next = ne; 252 *needed = ne; 253 } 254 255 } 256 257 } 258 259 if (got) 260 return true; 261 262 return ((*needed)->obj = _rtld_load_library(name, obj, mode)) != NULL; 263 } 264 265 266 /* 267 * Given a shared object, traverse its list of needed objects, and load 268 * each of them. Returns 0 on success. Generates an error message and 269 * returns -1 on failure. 270 */ 271 int 272 _rtld_load_needed_objects(first, mode) 273 Obj_Entry *first; 274 int mode; 275 { 276 Obj_Entry *obj; 277 int status = 0; 278 279 for (obj = first; obj != NULL; obj = obj->next) { 280 Needed_Entry *needed; 281 282 for (needed = obj->needed; needed != NULL; 283 needed = needed->next) { 284 const char *name = obj->strtab + needed->name; 285 if (!_rtld_load_by_name(name, obj, &needed, mode)) 286 status = -1; /* FIXME - cleanup */ 287 #ifdef RTLD_LOADER 288 if (status == -1) 289 return status; 290 #endif 291 } 292 } 293 294 return status; 295 } 296 297 #ifdef RTLD_LOADER 298 int 299 _rtld_preload(preload_path) 300 const char *preload_path; 301 { 302 const char *path; 303 char *cp, *buf; 304 int status = 0; 305 306 if (preload_path != NULL) { 307 cp = buf = xstrdup(preload_path); 308 while ((path = strsep(&cp, " :")) != NULL && status == 0) { 309 if (!_rtld_load_object(xstrdup(path), RTLD_MAIN)) 310 status = -1; 311 else 312 dbg((" preloaded \"%s\"", path)); 313 } 314 free(buf); 315 } 316 317 return (status); 318 } 319 #endif 320