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