e0013ec0eecfe698dd717135e79cb55b44214e78
[musl] / src / ldso / dynlink.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <stdint.h>
6 #include <elf.h>
7 #include <sys/mman.h>
8 #include <limits.h>
9 #include <stdint.h>
10 #include <fcntl.h>
11 #include <sys/stat.h>
12 #include <errno.h>
13 #include <limits.h>
14 #include <elf.h>
15 #include <setjmp.h>
16 #include <pthread.h>
17 #include <ctype.h>
18 #include <dlfcn.h>
19
20 static int errflag;
21
22 #ifdef __PIC__
23
24 #include "reloc.h"
25
26 #if ULONG_MAX == 0xffffffff
27 typedef Elf32_Ehdr Ehdr;
28 typedef Elf32_Phdr Phdr;
29 typedef Elf32_Sym Sym;
30 #define R_TYPE(x) ((x)&255)
31 #define R_SYM(x) ((x)>>8)
32 #else
33 typedef Elf64_Ehdr Ehdr;
34 typedef Elf64_Phdr Phdr;
35 typedef Elf64_Sym Sym;
36 #define R_TYPE(x) ((x)&0xffffffff)
37 #define R_SYM(x) ((x)>>32)
38 #endif
39
40 struct dso
41 {
42         struct dso *next, *prev;
43         int refcnt;
44         size_t *dynv;
45         Sym *syms;
46         uint32_t *hashtab;
47         char *strings;
48         unsigned char *base;
49         unsigned char *map;
50         size_t map_len;
51         dev_t dev;
52         ino_t ino;
53         char global;
54         char relocated;
55         char constructed;
56         struct dso **deps;
57         char *name;
58         char buf[];
59 };
60
61 static struct dso *head, *tail, *libc;
62 static char *env_path, *sys_path, *r_path;
63 static int rtld_used;
64 static int runtime;
65 static jmp_buf rtld_fail;
66 static pthread_rwlock_t lock;
67
68 #define AUX_CNT 24
69 #define DYN_CNT 34
70
71 static void decode_vec(size_t *v, size_t *a, size_t cnt)
72 {
73         memset(a, 0, cnt*sizeof(size_t));
74         for (; v[0]; v+=2) if (v[0]<cnt) {
75                 a[0] |= 1ULL<<v[0];
76                 a[v[0]] = v[1];
77         }
78 }
79
80 static uint32_t hash(const char *s0)
81 {
82         const unsigned char *s = (void *)s0;
83         uint_fast32_t h = 0;
84         while (*s) {
85                 h = 16*h + *s++;
86                 h ^= h>>24 & 0xf0;
87         }
88         return h & 0xfffffff;
89 }
90
91 static Sym *lookup(const char *s, uint32_t h, Sym *syms, uint32_t *hashtab, char *strings)
92 {
93         size_t i;
94         for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
95                 if (!strcmp(s, strings+syms[i].st_name))
96                         return syms+i;
97         }
98         return 0;
99 }
100
101 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
102 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
103
104 static void *find_sym(struct dso *dso, const char *s, int need_def)
105 {
106         uint32_t h = hash(s);
107         void *def = 0;
108         if (h==0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1;
109         if (h==0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1;
110         for (; dso; dso=dso->next) {
111                 Sym *sym;
112                 if (!dso->global) continue;
113                 sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings);
114                 if (sym && (!need_def || sym->st_shndx) && sym->st_value
115                  && (1<<(sym->st_info&0xf) & OK_TYPES)
116                  && (1<<(sym->st_info>>4) & OK_BINDS)) {
117                         if (def && sym->st_info>>4 == STB_WEAK) continue;
118                         def = dso->base + sym->st_value;
119                         if (sym->st_info>>4 == STB_GLOBAL) break;
120                 }
121         }
122         return def;
123 }
124
125 static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso)
126 {
127         Sym *sym;
128         const char *name;
129         size_t sym_val, sym_size;
130         size_t *reloc_addr;
131         void *ctx;
132         int type;
133         int sym_index;
134
135         for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
136                 reloc_addr = (void *)(base + rel[0]);
137                 type = R_TYPE(rel[1]);
138                 sym_index = R_SYM(rel[1]);
139                 if (sym_index) {
140                         sym = syms + sym_index;
141                         name = strings + sym->st_name;
142                         ctx = IS_COPY(type) ? dso->next : dso;
143                         sym_val = (size_t)find_sym(ctx, name, IS_PLT(type));
144                         if (!sym_val && sym->st_info>>4 != STB_WEAK) {
145                                 if (runtime) longjmp(rtld_fail, 1);
146                                 dprintf(2, "%s: symbol not found\n", name);
147                                 _exit(127);
148                         }
149                         sym_size = sym->st_size;
150                 }
151                 do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]);
152         }
153 }
154
155 /* A huge hack: to make up for the wastefulness of shared libraries
156  * needing at least a page of dirty memory even if they have no global
157  * data, we reclaim the gaps at the beginning and end of writable maps
158  * and "donate" them to the heap by setting up minimal malloc
159  * structures and then freeing them. */
160
161 static void reclaim(unsigned char *base, size_t start, size_t end)
162 {
163         size_t *a, *z;
164         start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
165         end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
166         if (start>end || end-start < 4*sizeof(size_t)) return;
167         a = (size_t *)(base + start);
168         z = (size_t *)(base + end);
169         a[-2] = 1;
170         a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
171         z[1] = 1;
172         free(a);
173 }
174
175 static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
176 {
177         for (; phcnt--; ph=(void *)((char *)ph+phent)) {
178                 if (ph->p_type!=PT_LOAD) continue;
179                 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
180                 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
181                 reclaim(base, ph->p_vaddr+ph->p_memsz,
182                         ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
183         }
184 }
185
186 static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp)
187 {
188         Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
189         size_t phsize;
190         size_t addr_min=SIZE_MAX, addr_max=0, map_len;
191         size_t this_min, this_max;
192         off_t off_start;
193         Ehdr *eh;
194         Phdr *ph;
195         unsigned prot;
196         unsigned char *map, *base;
197         size_t dyn;
198         size_t i;
199
200         ssize_t l = read(fd, buf, sizeof buf);
201         if (l<sizeof *eh) return 0;
202         eh = buf;
203         phsize = eh->e_phentsize * eh->e_phnum;
204         if (phsize + sizeof *eh > l) return 0;
205         if (eh->e_phoff + phsize > l) {
206                 l = pread(fd, buf+1, phsize, eh->e_phoff);
207                 if (l != phsize) return 0;
208                 eh->e_phoff = sizeof *eh;
209         }
210         ph = (void *)((char *)buf + eh->e_phoff);
211         for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
212                 if (ph->p_type == PT_DYNAMIC)
213                         dyn = ph->p_vaddr;
214                 if (ph->p_type != PT_LOAD) continue;
215                 if (ph->p_vaddr < addr_min) {
216                         addr_min = ph->p_vaddr;
217                         off_start = ph->p_offset;
218                         prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
219                                 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
220                                 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
221                 }
222                 if (ph->p_vaddr+ph->p_memsz > addr_max) {
223                         addr_max = ph->p_vaddr+ph->p_memsz;
224                 }
225         }
226         if (!dyn) return 0;
227         addr_max += PAGE_SIZE-1;
228         addr_max &= -PAGE_SIZE;
229         addr_min &= -PAGE_SIZE;
230         off_start &= -PAGE_SIZE;
231         map_len = addr_max - addr_min + off_start;
232         /* The first time, we map too much, possibly even more than
233          * the length of the file. This is okay because we will not
234          * use the invalid part; we just need to reserve the right
235          * amount of virtual address space to map over later. */
236         map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
237         if (map==MAP_FAILED) return 0;
238         base = map - addr_min;
239         ph = (void *)((char *)buf + eh->e_phoff);
240         for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
241                 if (ph->p_type != PT_LOAD) continue;
242                 /* Reuse the existing mapping for the lowest-address LOAD */
243                 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
244                 this_min = ph->p_vaddr & -PAGE_SIZE;
245                 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
246                 off_start = ph->p_offset & -PAGE_SIZE;
247                 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
248                         ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
249                         ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
250                 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) {
251                         munmap(map, map_len);
252                         return 0;
253                 }
254                 if (ph->p_memsz > ph->p_filesz) {
255                         size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
256                         size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
257                         memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
258                         if (pgbrk-(size_t)base < this_max && mmap((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED) {
259                                 munmap(map, map_len);
260                                 return 0;
261                         }
262                 }
263         }
264         for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
265                 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
266                         mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC);
267                         break;
268                 }
269         if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
270                 eh->e_phentsize, eh->e_phnum);
271         *lenp = map_len;
272         *basep = base;
273         *dynp = dyn;
274         return map;
275 }
276
277 static int path_open(const char *name, const char *search)
278 {
279         char buf[2*NAME_MAX+2];
280         const char *s=search, *z;
281         int l, fd;
282         for (;;) {
283                 while (*s==':') s++;
284                 if (!*s) return -1;
285                 z = strchr(s, ':');
286                 l = z ? z-s : strlen(s);
287                 snprintf(buf, sizeof buf, "%.*s/%s", l, s, name);
288                 if ((fd = open(buf, O_RDONLY))>=0) return fd;
289                 s += l;
290         }
291 }
292
293 static void decode_dyn(struct dso *p)
294 {
295         size_t dyn[DYN_CNT] = {0};
296         decode_vec(p->dynv, dyn, DYN_CNT);
297         p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
298         p->hashtab = (void *)(p->base + dyn[DT_HASH]);
299         p->strings = (void *)(p->base + dyn[DT_STRTAB]);
300 }
301
302 static struct dso *load_library(const char *name)
303 {
304         unsigned char *base, *map;
305         size_t dyno, map_len;
306         struct dso *p;
307         int fd;
308         struct stat st;
309
310         /* Catch and block attempts to reload the implementation itself */
311         if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
312                 static const char *rp, reserved[] =
313                         "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
314                 char *z = strchr(name, '.');
315                 if (z) {
316                         size_t l = z-name;
317                         for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
318                         if (*rp) {
319                                 if (!libc->prev) {
320                                         tail->next = libc;
321                                         libc->prev = tail;
322                                         tail = libc->next ? libc->next : libc;
323                                 }
324                                 return libc;
325                         }
326                 }
327         }
328         /* Search for the name to see if it's already loaded */
329         for (p=head->next; p; p=p->next) {
330                 if (!strcmp(p->name, name)) {
331                         p->refcnt++;
332                         return p;
333                 }
334         }
335         if (strchr(name, '/')) {
336                 fd = open(name, O_RDONLY);
337         } else {
338                 if (strlen(name) > NAME_MAX) return 0;
339                 fd = -1;
340                 if (r_path) fd = path_open(name, r_path);
341                 if (fd < 0 && env_path) fd = path_open(name, env_path);
342                 if (fd < 0) {
343                         if (!sys_path) {
344                                 FILE *f = fopen(ETC_LDSO_PATH, "r");
345                                 if (f) {
346                                         if (getline(&sys_path, (size_t[1]){0}, f) > 0)
347                                                 sys_path[strlen(sys_path)-1]=0;
348                                         fclose(f);
349                                 }
350                         }
351                         if (sys_path) fd = path_open(name, sys_path);
352                         else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib");
353                 }
354         }
355         if (fd < 0) return 0;
356         if (fstat(fd, &st) < 0) {
357                 close(fd);
358                 return 0;
359         }
360         for (p=head->next; p; p=p->next) {
361                 if (p->dev == st.st_dev && p->ino == st.st_ino) {
362                         close(fd);
363                         p->refcnt++;
364                         return p;
365                 }
366         }
367         map = map_library(fd, &map_len, &base, &dyno);
368         close(fd);
369         if (!map) return 0;
370         p = calloc(1, sizeof *p + strlen(name) + 1);
371         if (!p) {
372                 munmap(map, map_len);
373                 return 0;
374         }
375
376         p->map = map;
377         p->map_len = map_len;
378         p->base = base;
379         p->dynv = (void *)(base + dyno);
380         decode_dyn(p);
381
382         p->dev = st.st_dev;
383         p->ino = st.st_ino;
384         p->refcnt = 1;
385         p->name = p->buf;
386         strcpy(p->name, name);
387
388         tail->next = p;
389         p->prev = tail;
390         tail = p;
391
392         return p;
393 }
394
395 static void load_deps(struct dso *p)
396 {
397         size_t i, ndeps=0;
398         struct dso ***deps = &p->deps, **tmp, *dep;
399         for (; p; p=p->next) {
400                 for (i=0; p->dynv[i]; i+=2) {
401                         if (p->dynv[i] != DT_RPATH) continue;
402                         r_path = (void *)(p->strings + p->dynv[i+1]);
403                 }
404                 for (i=0; p->dynv[i]; i+=2) {
405                         if (p->dynv[i] != DT_NEEDED) continue;
406                         dep = load_library(p->strings + p->dynv[i+1]);
407                         if (!dep) {
408                                 if (runtime) longjmp(rtld_fail, 1);
409                                 dprintf(2, "%s: %m (needed by %s)\n",
410                                         p->strings + p->dynv[i+1], p->name);
411                                 _exit(127);
412                         }
413                         if (runtime) {
414                                 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
415                                 if (!tmp) longjmp(rtld_fail, 1);
416                                 tmp[ndeps++] = dep;
417                                 tmp[ndeps] = 0;
418                                 *deps = tmp;
419                         }
420                 }
421                 r_path = 0;
422         }
423 }
424
425 static void load_preload(char *s)
426 {
427         int tmp;
428         char *z;
429         for (z=s; *z; s=z) {
430                 for (   ; *s && isspace(*s); s++);
431                 for (z=s; *z && !isspace(*z); z++);
432                 tmp = *z;
433                 *z = 0;
434                 load_library(s);
435                 *z = tmp;
436         }
437 }
438
439 static void make_global(struct dso *p)
440 {
441         for (; p; p=p->next) p->global = 1;
442 }
443
444 static void reloc_all(struct dso *p)
445 {
446         size_t dyn[DYN_CNT] = {0};
447         for (; p; p=p->next) {
448                 if (p->relocated) continue;
449                 decode_vec(p->dynv, dyn, DYN_CNT);
450                 do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
451                         2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head);
452                 do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ],
453                         2, p->syms, p->strings, head);
454                 do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ],
455                         3, p->syms, p->strings, head);
456                 p->relocated = 1;
457         }
458 }
459
460 static void free_all(struct dso *p)
461 {
462         struct dso *n;
463         while (p) {
464                 n = p->next;
465                 if (p->map) free(p);
466                 p = n;
467         }
468 }
469
470 static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
471 {
472         for (; cnt--; ph = (void *)((char *)ph + stride))
473                 if (ph->p_type == PT_DYNAMIC)
474                         return ph->p_vaddr;
475         return 0;
476 }
477
478 static void do_init_fini(struct dso *p)
479 {
480         size_t dyn[DYN_CNT] = {0};
481         for (; p; p=p->prev) {
482                 if (p->constructed) return;
483                 decode_vec(p->dynv, dyn, DYN_CNT);
484                 if (dyn[0] & (1<<DT_FINI))
485                         atexit((void (*)(void))(p->base + dyn[DT_FINI]));
486                 if (dyn[0] & (1<<DT_INIT))
487                         ((void (*)(void))(p->base + dyn[DT_INIT]))();
488                 p->constructed = 1;
489         }
490 }
491
492 void *__dynlink(int argc, char **argv)
493 {
494         size_t *auxv, aux[AUX_CNT] = {0};
495         size_t i;
496         Phdr *phdr;
497         Ehdr *ehdr;
498         static struct dso builtin_dsos[3];
499         struct dso *const app = builtin_dsos+0;
500         struct dso *const lib = builtin_dsos+1;
501         struct dso *const vdso = builtin_dsos+2;
502         char *env_preload=0;
503
504         /* Find aux vector just past environ[] */
505         for (i=argc+1; argv[i]; i++)
506                 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
507                         env_path = argv[i]+16;
508                 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
509                         env_preload = argv[i]+11;
510         auxv = (void *)(argv+i+1);
511
512         decode_vec(auxv, aux, AUX_CNT);
513
514         /* Only trust user/env if kernel says we're not suid/sgid */
515         if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
516           || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
517                 env_path = 0;
518                 env_preload = 0;
519         }
520
521         /* The dynamic linker load address is passed by the kernel
522          * in the AUX vector, so this is easy. */
523         lib->base = (void *)aux[AT_BASE];
524         lib->name = "libc.so";
525         lib->global = 1;
526         ehdr = (void *)lib->base;
527         lib->dynv = (void *)(lib->base + find_dyn(
528                 (void *)(aux[AT_BASE]+ehdr->e_phoff),
529                 ehdr->e_phnum, ehdr->e_phentsize));
530         decode_dyn(lib);
531
532         /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
533         app->base = 0;
534         phdr = (void *)aux[AT_PHDR];
535         for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
536                 if (phdr->p_type == PT_PHDR)
537                         app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
538         }
539         app->name = argv[0];
540         app->global = 1;
541         app->constructed = 1;
542         app->dynv = (void *)(app->base + find_dyn(
543                 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
544         decode_dyn(app);
545
546         /* Attach to vdso, if provided by the kernel */
547         for (i=0; auxv[i]; i+=2) {
548                 size_t vdso_base = auxv[i+1];
549                 if (auxv[i] != AT_SYSINFO_EHDR) continue;
550                 ehdr = (void *)vdso_base;
551                 phdr = (void *)(vdso_base + ehdr->e_phoff);
552                 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
553                         if (phdr->p_type == PT_DYNAMIC)
554                                 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
555                         if (phdr->p_type == PT_LOAD)
556                                 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
557                 }
558                 vdso->name = "linux-gate.so.1";
559                 vdso->global = 1;
560                 decode_dyn(vdso);
561                 vdso->prev = lib;
562                 lib->next = vdso;
563                 break;
564         }
565
566         /* Initial dso chain consists only of the app. We temporarily
567          * append the dynamic linker/libc so we can relocate it, then
568          * restore the initial chain in preparation for loading third
569          * party libraries (preload/needed). */
570         head = tail = app;
571         libc = lib;
572         app->next = lib;
573         reloc_all(lib);
574         app->next = 0;
575
576         /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
577
578         /* Donate unused parts of app and library mapping to malloc */
579         reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
580         ehdr = (void *)lib->base;
581         reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
582                 ehdr->e_phentsize, ehdr->e_phnum);
583
584         /* Load preload/needed libraries, add their symbols to the global
585          * namespace, and perform all remaining relocations. The main
586          * program must be relocated LAST since it may contain copy
587          * relocations which depend on libraries' relocations. */
588         if (env_preload) load_preload(env_preload);
589         load_deps(app);
590         make_global(app);
591         reloc_all(app->next);
592         reloc_all(app);
593
594         /* Switch to runtime mode: any further failures in the dynamic
595          * linker are a reportable failure rather than a fatal startup
596          * error. If the dynamic loader (dlopen) will not be used, free
597          * all memory used by the dynamic linker. */
598         runtime = 1;
599
600         do_init_fini(tail);
601
602         if (!rtld_used) {
603                 free_all(head);
604                 free(sys_path);
605                 reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos);
606         }
607
608         errno = 0;
609         return (void *)aux[AT_ENTRY];
610 }
611
612 void *dlopen(const char *file, int mode)
613 {
614         struct dso *volatile p, *orig_tail = tail, *next;
615         size_t i;
616         int cs;
617
618         if (!file) return head;
619
620         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
621         pthread_rwlock_wrlock(&lock);
622
623         if (setjmp(rtld_fail)) {
624                 /* Clean up anything new that was (partially) loaded */
625                 if (p->deps) for (i=0; p->deps[i]; i++)
626                         if (p->deps[i]->global < 0)
627                                 p->deps[i]->global = 0;
628                 for (p=orig_tail->next; p; p=next) {
629                         next = p->next;
630                         munmap(p->map, p->map_len);
631                         free(p->deps);
632                         free(p);
633                 }
634                 tail = orig_tail;
635                 tail->next = 0;
636                 p = 0;
637         } else p = load_library(file);
638
639         if (!p) {
640                 errflag = 1;
641                 goto end;
642         }
643
644         /* First load handling */
645         if (!p->deps) {
646                 load_deps(p);
647                 if (p->deps) for (i=0; p->deps[i]; i++)
648                         if (!p->deps[i]->global)
649                                 p->deps[i]->global = -1;
650                 if (!p->global) p->global = -1;
651                 reloc_all(p);
652                 if (p->deps) for (i=0; p->deps[i]; i++)
653                         if (p->deps[i]->global < 0)
654                                 p->deps[i]->global = 0;
655                 if (p->global < 0) p->global = 0;
656         }
657
658         if (mode & RTLD_GLOBAL) {
659                 if (p->deps) for (i=0; p->deps[i]; i++)
660                         p->deps[i]->global = 1;
661                 p->global = 1;
662         }
663
664         do_init_fini(tail);
665 end:
666         pthread_rwlock_unlock(&lock);
667         pthread_setcancelstate(cs, 0);
668         return p;
669 }
670
671 static void *do_dlsym(struct dso *p, const char *s, void *ra)
672 {
673         size_t i;
674         uint32_t h;
675         Sym *sym;
676         if (p == RTLD_NEXT) {
677                 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
678                 if (!p) p=head;
679                 p=p->next;
680         }
681         if (p == head || p == RTLD_DEFAULT) {
682                 void *res = find_sym(head, s, 0);
683                 if (!res) errflag = 1;
684                 return res;
685         }
686         h = hash(s);
687         sym = lookup(s, h, p->syms, p->hashtab, p->strings);
688         if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
689                 return p->base + sym->st_value;
690         if (p->deps) for (i=0; p->deps[i]; i++) {
691                 sym = lookup(s, h, p->deps[i]->syms,
692                         p->deps[i]->hashtab, p->deps[i]->strings);
693                 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
694                         return p->deps[i]->base + sym->st_value;
695         }
696         errflag = 1;
697         return 0;
698 }
699
700 void *__dlsym(void *p, const char *s, void *ra)
701 {
702         void *res;
703         pthread_rwlock_rdlock(&lock);
704         res = do_dlsym(p, s, ra);
705         pthread_rwlock_unlock(&lock);
706         return res;
707 }
708 #else
709 void *dlopen(const char *file, int mode)
710 {
711         return 0;
712 }
713 void *__dlsym(void *p, const char *s, void *ra)
714 {
715         return 0;
716 }
717 #endif
718
719 char *dlerror()
720 {
721         if (!errflag) return 0;
722         errflag = 0;
723         return "unknown error";
724 }
725
726 int dlclose(void *p)
727 {
728         return 0;
729 }