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