1 #define _GNU_SOURCE
2 #define SYSCALL_NO_TLS 1
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <stddef.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdint.h>
9 #include <elf.h>
10 #include <sys/mman.h>
11 #include <limits.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14 #include <errno.h>
15 #include <link.h>
16 #include <setjmp.h>
17 #include <pthread.h>
18 #include <ctype.h>
19 #include <dlfcn.h>
20 #include <semaphore.h>
21 #include <sys/membarrier.h>
22 #include "pthread_impl.h"
23 #include "fork_impl.h"
24 #include "libc.h"
25 #include "dynlink.h"
26
27 #define malloc __libc_malloc
28 #define calloc __libc_calloc
29 #define realloc __libc_realloc
30 #define free __libc_free
31
32 static void error(const char *, ...);
33
34 #define MAXP2(a,b) (-(-(a)&-(b)))
35 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
36
37 #define container_of(p,t,m) ((t*)((char *)(p)-offsetof(t,m)))
38 #define countof(a) ((sizeof (a))/(sizeof (a)[0]))
39
40 struct debug {
41 int ver;
42 void *head;
43 void (*bp)(void);
44 int state;
45 void *base;
46 };
47
48 struct td_index {
49 size_t args[2];
50 struct td_index *next;
51 };
52
53 struct dso {
54 #if DL_FDPIC
55 struct fdpic_loadmap *loadmap;
56 #else
57 unsigned char *base;
58 #endif
59 char *name;
60 size_t *dynv;
61 struct dso *next, *prev;
62
63 Phdr *phdr;
64 int phnum;
65 size_t phentsize;
66 Sym *syms;
67 Elf_Symndx *hashtab;
68 uint32_t *ghashtab;
69 int16_t *versym;
70 char *strings;
71 struct dso *syms_next, *lazy_next;
72 size_t *lazy, lazy_cnt;
73 unsigned char *map;
74 size_t map_len;
75 #ifndef __LITEOS_A__
76 dev_t dev;
77 ino_t ino;
78 #endif
79 char relocated;
80 char constructed;
81 char kernel_mapped;
82 char mark;
83 char bfs_built;
84 char runtime_loaded;
85 struct dso **deps, *needed_by;
86 size_t ndeps_direct;
87 size_t next_dep;
88 pthread_t ctor_visitor;
89 char *rpath_orig, *rpath;
90 struct tls_module tls;
91 size_t tls_id;
92 size_t relro_start, relro_end;
93 uintptr_t *new_dtv;
94 unsigned char *new_tls;
95 struct td_index *td_index;
96 struct dso *fini_next;
97 char *shortname;
98 #if DL_FDPIC
99 unsigned char *base;
100 #else
101 struct fdpic_loadmap *loadmap;
102 #endif
103 struct funcdesc {
104 void *addr;
105 size_t *got;
106 } *funcdescs;
107 size_t *got;
108 char buf[];
109 };
110
111 struct symdef {
112 Sym *sym;
113 struct dso *dso;
114 };
115
116 typedef void (*stage3_func)(size_t *, size_t *);
117
118 static struct builtin_tls {
119 char c;
120 struct pthread pt;
121 void *space[16];
122 } builtin_tls[1];
123 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
124
125 #define ADDEND_LIMIT 4096
126 static size_t *saved_addends, *apply_addends_to;
127
128 static struct dso ldso;
129 static struct dso *head, *tail, *fini_head, *syms_tail, *lazy_head;
130 static char *env_path, *sys_path;
131 static unsigned long long gencnt;
132 static int runtime;
133 static int ldd_mode;
134 static int ldso_fail;
135 static int noload;
136 static int shutting_down;
137 static jmp_buf *rtld_fail;
138 static pthread_rwlock_t lock;
139 static struct debug debug;
140 static struct tls_module *tls_tail;
141 static size_t tls_cnt, tls_offset, tls_align = MIN_TLS_ALIGN;
142 static size_t static_tls_cnt;
143 static pthread_mutex_t init_fini_lock;
144 static pthread_cond_t ctor_cond;
145 static struct dso *builtin_deps[2];
146 static struct dso *const no_deps[1];
147 static struct dso *builtin_ctor_queue[4];
148 static struct dso **main_ctor_queue;
149 static struct fdpic_loadmap *app_loadmap;
150 static struct fdpic_dummy_loadmap app_dummy_loadmap;
151
152 struct debug *_dl_debug_addr = &debug;
153
154 extern hidden int __malloc_replaced;
155
156 hidden void (*const __init_array_start)(void)=0, (*const __fini_array_start)(void)=0;
157
158 extern hidden void (*const __init_array_end)(void), (*const __fini_array_end)(void);
159
160 weak_alias(__init_array_start, __init_array_end);
161 weak_alias(__fini_array_start, __fini_array_end);
162
dl_strcmp(const char * l,const char * r)163 static int dl_strcmp(const char *l, const char *r)
164 {
165 for (; *l==*r && *l; l++, r++);
166 return *(unsigned char *)l - *(unsigned char *)r;
167 }
168 #define strcmp(l,r) dl_strcmp(l,r)
169
170 /* Compute load address for a virtual address in a given dso. */
171 #if DL_FDPIC
laddr(const struct dso * p,size_t v)172 static void *laddr(const struct dso *p, size_t v)
173 {
174 size_t j=0;
175 if (!p->loadmap) return p->base + v;
176 for (j=0; v-p->loadmap->segs[j].p_vaddr >= p->loadmap->segs[j].p_memsz; j++);
177 return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
178 }
laddr_pg(const struct dso * p,size_t v)179 static void *laddr_pg(const struct dso *p, size_t v)
180 {
181 size_t j=0;
182 size_t pgsz = PAGE_SIZE;
183 if (!p->loadmap) return p->base + v;
184 for (j=0; ; j++) {
185 size_t a = p->loadmap->segs[j].p_vaddr;
186 size_t b = a + p->loadmap->segs[j].p_memsz;
187 a &= -pgsz;
188 b += pgsz-1;
189 b &= -pgsz;
190 if (v-a<b-a) break;
191 }
192 return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
193 }
fdbarrier(void * p)194 static void (*fdbarrier(void *p))()
195 {
196 void (*fd)();
197 __asm__("" : "=r"(fd) : "0"(p));
198 return fd;
199 }
200 #define fpaddr(p, v) fdbarrier((&(struct funcdesc){ \
201 laddr(p, v), (p)->got }))
202 #else
203 #define laddr(p, v) (void *)((p)->base + (v))
204 #define laddr_pg(p, v) laddr(p, v)
205 #define fpaddr(p, v) ((void (*)())laddr(p, v))
206 #endif
207
decode_vec(size_t * v,size_t * a,size_t cnt)208 static void decode_vec(size_t *v, size_t *a, size_t cnt)
209 {
210 size_t i;
211 for (i=0; i<cnt; i++) a[i] = 0;
212 for (; v[0]; v+=2) if (v[0]-1<cnt-1) {
213 a[0] |= 1UL<<v[0];
214 a[v[0]] = v[1];
215 }
216 }
217
search_vec(size_t * v,size_t * r,size_t key)218 static int search_vec(size_t *v, size_t *r, size_t key)
219 {
220 for (; v[0]!=key; v+=2)
221 if (!v[0]) return 0;
222 *r = v[1];
223 return 1;
224 }
225
sysv_hash(const char * s0)226 static uint32_t sysv_hash(const char *s0)
227 {
228 const unsigned char *s = (void *)s0;
229 uint_fast32_t h = 0;
230 while (*s) {
231 h = 16*h + *s++;
232 h ^= h>>24 & 0xf0;
233 }
234 return h & 0xfffffff;
235 }
236
gnu_hash(const char * s0)237 static uint32_t gnu_hash(const char *s0)
238 {
239 const unsigned char *s = (void *)s0;
240 uint_fast32_t h = 5381;
241 for (; *s; s++)
242 h += h*32 + *s;
243 return h;
244 }
245
sysv_lookup(const char * s,uint32_t h,struct dso * dso)246 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
247 {
248 size_t i;
249 Sym *syms = dso->syms;
250 Elf_Symndx *hashtab = dso->hashtab;
251 char *strings = dso->strings;
252 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
253 if ((!dso->versym || dso->versym[i] >= 0)
254 && (!strcmp(s, strings+syms[i].st_name)))
255 return syms+i;
256 }
257 return 0;
258 }
259
gnu_lookup(uint32_t h1,uint32_t * hashtab,struct dso * dso,const char * s)260 static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s)
261 {
262 uint32_t nbuckets = hashtab[0];
263 uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
264 uint32_t i = buckets[h1 % nbuckets];
265
266 if (!i) return 0;
267
268 uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]);
269
270 for (h1 |= 1; ; i++) {
271 uint32_t h2 = *hashval++;
272 if ((h1 == (h2|1)) && (!dso->versym || dso->versym[i] >= 0)
273 && !strcmp(s, dso->strings + dso->syms[i].st_name))
274 return dso->syms+i;
275 if (h2 & 1) break;
276 }
277
278 return 0;
279 }
280
gnu_lookup_filtered(uint32_t h1,uint32_t * hashtab,struct dso * dso,const char * s,uint32_t fofs,size_t fmask)281 static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s, uint32_t fofs, size_t fmask)
282 {
283 const size_t *bloomwords = (const void *)(hashtab+4);
284 size_t f = bloomwords[fofs & (hashtab[2]-1)];
285 if (!(f & fmask)) return 0;
286
287 f >>= (h1 >> hashtab[3]) % (8 * sizeof f);
288 if (!(f & 1)) return 0;
289
290 return gnu_lookup(h1, hashtab, dso, s);
291 }
292
293 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
294 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
295
296 #ifndef ARCH_SYM_REJECT_UND
297 #define ARCH_SYM_REJECT_UND(s) 0
298 #endif
299
300 #if defined(__GNUC__)
301 __attribute__((always_inline))
302 #endif
find_sym2(struct dso * dso,const char * s,int need_def,int use_deps)303 static inline struct symdef find_sym2(struct dso *dso, const char *s, int need_def, int use_deps)
304 {
305 uint32_t h = 0, gh = gnu_hash(s), gho = gh / (8*sizeof(size_t)), *ght;
306 size_t ghm = 1ul << gh % (8*sizeof(size_t));
307 struct symdef def = {0};
308 struct dso **deps = use_deps ? dso->deps : 0;
309 for (; dso; dso=use_deps ? *deps++ : dso->syms_next) {
310 Sym *sym;
311 if ((ght = dso->ghashtab)) {
312 sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm);
313 } else {
314 if (!h) h = sysv_hash(s);
315 sym = sysv_lookup(s, h, dso);
316 }
317 if (!sym) continue;
318 if (!sym->st_shndx)
319 if (need_def || (sym->st_info&0xf) == STT_TLS
320 || ARCH_SYM_REJECT_UND(sym))
321 continue;
322 if (!sym->st_value)
323 if ((sym->st_info&0xf) != STT_TLS)
324 continue;
325 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue;
326 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue;
327 def.sym = sym;
328 def.dso = dso;
329 break;
330 }
331 return def;
332 }
333
find_sym(struct dso * dso,const char * s,int need_def)334 static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
335 {
336 return find_sym2(dso, s, need_def, 0);
337 }
338
do_relocs(struct dso * dso,size_t * rel,size_t rel_size,size_t stride)339 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
340 {
341 unsigned char *base = dso->base;
342 Sym *syms = dso->syms;
343 char *strings = dso->strings;
344 Sym *sym;
345 const char *name;
346 void *ctx;
347 int type;
348 int sym_index;
349 struct symdef def;
350 size_t *reloc_addr;
351 size_t sym_val;
352 size_t tls_val;
353 size_t addend;
354 int skip_relative = 0, reuse_addends = 0, save_slot = 0;
355
356 if (dso == &ldso) {
357 /* Only ldso's REL table needs addend saving/reuse. */
358 if (rel == apply_addends_to)
359 reuse_addends = 1;
360 skip_relative = 1;
361 }
362
363 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
364 if (skip_relative && IS_RELATIVE(rel[1], dso->syms)) continue;
365 type = R_TYPE(rel[1]);
366 if (type == REL_NONE) continue;
367 reloc_addr = laddr(dso, rel[0]);
368
369 if (stride > 2) {
370 addend = rel[2];
371 } else if (type==REL_GOT || type==REL_PLT|| type==REL_COPY) {
372 addend = 0;
373 } else if (reuse_addends) {
374 /* Save original addend in stage 2 where the dso
375 * chain consists of just ldso; otherwise read back
376 * saved addend since the inline one was clobbered. */
377 if (head==&ldso)
378 saved_addends[save_slot] = *reloc_addr;
379 addend = saved_addends[save_slot++];
380 } else {
381 addend = *reloc_addr;
382 }
383
384 sym_index = R_SYM(rel[1]);
385 if (sym_index) {
386 sym = syms + sym_index;
387 name = strings + sym->st_name;
388 ctx = type==REL_COPY ? head->syms_next : head;
389 def = (sym->st_info>>4) == STB_LOCAL
390 ? (struct symdef){ .dso = dso, .sym = sym }
391 : find_sym(ctx, name, type==REL_PLT);
392 if (!def.sym && (sym->st_shndx != SHN_UNDEF
393 || sym->st_info>>4 != STB_WEAK)) {
394 if (dso->lazy && (type==REL_PLT || type==REL_GOT)) {
395 dso->lazy[3*dso->lazy_cnt+0] = rel[0];
396 dso->lazy[3*dso->lazy_cnt+1] = rel[1];
397 dso->lazy[3*dso->lazy_cnt+2] = addend;
398 dso->lazy_cnt++;
399 continue;
400 }
401 error("Error relocating %s: %s: symbol not found",
402 dso->name, name);
403 if (runtime) longjmp(*rtld_fail, 1);
404 continue;
405 }
406 } else {
407 sym = 0;
408 def.sym = 0;
409 def.dso = dso;
410 }
411
412 sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0;
413 tls_val = def.sym ? def.sym->st_value : 0;
414
415 if ((type == REL_TPOFF || type == REL_TPOFF_NEG)
416 && def.dso->tls_id > static_tls_cnt) {
417 error("Error relocating %s: %s: initial-exec TLS "
418 "resolves to dynamic definition in %s",
419 dso->name, name, def.dso->name);
420 longjmp(*rtld_fail, 1);
421 }
422
423 switch(type) {
424 case REL_OFFSET:
425 addend -= (size_t)reloc_addr;
426 case REL_SYMBOLIC:
427 case REL_GOT:
428 case REL_PLT:
429 *reloc_addr = sym_val + addend;
430 break;
431 case REL_USYMBOLIC:
432 memcpy(reloc_addr, &(size_t){sym_val + addend}, sizeof(size_t));
433 break;
434 case REL_RELATIVE:
435 *reloc_addr = (size_t)base + addend;
436 break;
437 case REL_SYM_OR_REL:
438 if (sym) *reloc_addr = sym_val + addend;
439 else *reloc_addr = (size_t)base + addend;
440 break;
441 case REL_COPY:
442 memcpy(reloc_addr, (void *)sym_val, sym->st_size);
443 break;
444 case REL_OFFSET32:
445 *(uint32_t *)reloc_addr = sym_val + addend
446 - (size_t)reloc_addr;
447 break;
448 case REL_FUNCDESC:
449 *reloc_addr = def.sym ? (size_t)(def.dso->funcdescs
450 + (def.sym - def.dso->syms)) : 0;
451 break;
452 case REL_FUNCDESC_VAL:
453 if ((sym->st_info&0xf) == STT_SECTION) *reloc_addr += sym_val;
454 else *reloc_addr = sym_val;
455 reloc_addr[1] = def.sym ? (size_t)def.dso->got : 0;
456 break;
457 case REL_DTPMOD:
458 *reloc_addr = def.dso->tls_id;
459 break;
460 case REL_DTPOFF:
461 *reloc_addr = tls_val + addend - DTP_OFFSET;
462 break;
463 #ifdef TLS_ABOVE_TP
464 case REL_TPOFF:
465 *reloc_addr = tls_val + def.dso->tls.offset + TPOFF_K + addend;
466 break;
467 #else
468 case REL_TPOFF:
469 *reloc_addr = tls_val - def.dso->tls.offset + addend;
470 break;
471 case REL_TPOFF_NEG:
472 *reloc_addr = def.dso->tls.offset - tls_val + addend;
473 break;
474 #endif
475 case REL_TLSDESC:
476 if (stride<3) addend = reloc_addr[1];
477 if (def.dso->tls_id > static_tls_cnt) {
478 struct td_index *new = malloc(sizeof *new);
479 if (!new) {
480 error(
481 "Error relocating %s: cannot allocate TLSDESC for %s",
482 dso->name, sym ? name : "(local)" );
483 longjmp(*rtld_fail, 1);
484 }
485 new->next = dso->td_index;
486 dso->td_index = new;
487 new->args[0] = def.dso->tls_id;
488 new->args[1] = tls_val + addend - DTP_OFFSET;
489 reloc_addr[0] = (size_t)__tlsdesc_dynamic;
490 reloc_addr[1] = (size_t)new;
491 } else {
492 reloc_addr[0] = (size_t)__tlsdesc_static;
493 #ifdef TLS_ABOVE_TP
494 reloc_addr[1] = tls_val + def.dso->tls.offset
495 + TPOFF_K + addend;
496 #else
497 reloc_addr[1] = tls_val - def.dso->tls.offset
498 + addend;
499 #endif
500 }
501 #ifdef TLSDESC_BACKWARDS
502 /* Some archs (32-bit ARM at least) invert the order of
503 * the descriptor members. Fix them up here. */
504 size_t tmp = reloc_addr[0];
505 reloc_addr[0] = reloc_addr[1];
506 reloc_addr[1] = tmp;
507 #endif
508 break;
509 default:
510 error("Error relocating %s: unsupported relocation type %d",
511 dso->name, type);
512 if (runtime) longjmp(*rtld_fail, 1);
513 continue;
514 }
515 }
516 }
517
redo_lazy_relocs()518 static void redo_lazy_relocs()
519 {
520 struct dso *p = lazy_head, *next;
521 lazy_head = 0;
522 for (; p; p=next) {
523 next = p->lazy_next;
524 size_t size = p->lazy_cnt*3*sizeof(size_t);
525 p->lazy_cnt = 0;
526 do_relocs(p, p->lazy, size, 3);
527 if (p->lazy_cnt) {
528 p->lazy_next = lazy_head;
529 lazy_head = p;
530 } else {
531 free(p->lazy);
532 p->lazy = 0;
533 p->lazy_next = 0;
534 }
535 }
536 }
537
538 /* A huge hack: to make up for the wastefulness of shared libraries
539 * needing at least a page of dirty memory even if they have no global
540 * data, we reclaim the gaps at the beginning and end of writable maps
541 * and "donate" them to the heap. */
542
reclaim(struct dso * dso,size_t start,size_t end)543 static void reclaim(struct dso *dso, size_t start, size_t end)
544 {
545 if (start >= dso->relro_start && start < dso->relro_end) start = dso->relro_end;
546 if (end >= dso->relro_start && end < dso->relro_end) end = dso->relro_start;
547 if (start >= end) return;
548 char *base = laddr_pg(dso, start);
549 __malloc_donate(base, base+(end-start));
550 }
551
reclaim_gaps(struct dso * dso)552 static void reclaim_gaps(struct dso *dso)
553 {
554 Phdr *ph = dso->phdr;
555 size_t phcnt = dso->phnum;
556
557 for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) {
558 if (ph->p_type!=PT_LOAD) continue;
559 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
560 reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
561 reclaim(dso, ph->p_vaddr+ph->p_memsz,
562 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
563 }
564 }
565
read_loop(int fd,void * p,size_t n)566 static ssize_t read_loop(int fd, void *p, size_t n)
567 {
568 for (size_t i=0; i<n; ) {
569 ssize_t l = read(fd, (char *)p+i, n-i);
570 if (l<0) {
571 if (errno==EINTR) continue;
572 else return -1;
573 }
574 if (l==0) return i;
575 i += l;
576 }
577 return n;
578 }
579
mmap_fixed(void * p,size_t n,int prot,int flags,int fd,off_t off)580 static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
581 {
582 static int no_map_fixed;
583 char *q;
584 if (!n) return p;
585 if (!no_map_fixed) {
586 q = mmap(p, n, prot, flags|MAP_FIXED, fd, off);
587 if (!DL_NOMMU_SUPPORT || q != MAP_FAILED || errno != EINVAL)
588 return q;
589 no_map_fixed = 1;
590 }
591 /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
592 if (flags & MAP_ANONYMOUS) {
593 memset(p, 0, n);
594 return p;
595 }
596 ssize_t r;
597 if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED;
598 for (q=p; n; q+=r, off+=r, n-=r) {
599 r = read(fd, q, n);
600 if (r < 0 && errno != EINTR) return MAP_FAILED;
601 if (!r) {
602 memset(q, 0, n);
603 break;
604 }
605 }
606 return p;
607 }
608
unmap_library(struct dso * dso)609 static void unmap_library(struct dso *dso)
610 {
611 if (dso->loadmap) {
612 size_t i;
613 for (i=0; i<dso->loadmap->nsegs; i++) {
614 if (!dso->loadmap->segs[i].p_memsz)
615 continue;
616 munmap((void *)dso->loadmap->segs[i].addr,
617 dso->loadmap->segs[i].p_memsz);
618 }
619 free(dso->loadmap);
620 } else if (dso->map && dso->map_len) {
621 munmap(dso->map, dso->map_len);
622 }
623 }
624
map_library(int fd,struct dso * dso)625 static void *map_library(int fd, struct dso *dso)
626 {
627 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
628 void *allocated_buf=0;
629 size_t phsize;
630 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
631 size_t this_min, this_max;
632 size_t nsegs = 0;
633 off_t off_start;
634 Ehdr *eh;
635 Phdr *ph, *ph0;
636 unsigned prot;
637 unsigned char *map=MAP_FAILED, *base;
638 size_t dyn=0;
639 size_t tls_image=0;
640 size_t i;
641
642 ssize_t l = read(fd, buf, sizeof buf);
643 eh = buf;
644 if (l<0) return 0;
645 if (l<sizeof *eh || (eh->e_type != ET_DYN && eh->e_type != ET_EXEC))
646 goto noexec;
647 phsize = eh->e_phentsize * eh->e_phnum;
648 if (phsize > sizeof buf - sizeof *eh) {
649 allocated_buf = malloc(phsize);
650 if (!allocated_buf) return 0;
651 l = pread(fd, allocated_buf, phsize, eh->e_phoff);
652 if (l < 0) goto error;
653 if (l != phsize) goto noexec;
654 ph = ph0 = allocated_buf;
655 } else if (eh->e_phoff + phsize > l) {
656 l = pread(fd, buf+1, phsize, eh->e_phoff);
657 if (l < 0) goto error;
658 if (l != phsize) goto noexec;
659 ph = ph0 = (void *)(buf + 1);
660 } else {
661 ph = ph0 = (void *)((char *)buf + eh->e_phoff);
662 }
663 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
664 if (ph->p_type == PT_DYNAMIC) {
665 dyn = ph->p_vaddr;
666 } else if (ph->p_type == PT_TLS) {
667 tls_image = ph->p_vaddr;
668 dso->tls.align = ph->p_align;
669 dso->tls.len = ph->p_filesz;
670 dso->tls.size = ph->p_memsz;
671 } else if (ph->p_type == PT_GNU_RELRO) {
672 dso->relro_start = ph->p_vaddr & -PAGE_SIZE;
673 dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
674 } else if (ph->p_type == PT_GNU_STACK) {
675 if (!runtime && ph->p_memsz > __default_stacksize) {
676 __default_stacksize =
677 ph->p_memsz < DEFAULT_STACK_MAX ?
678 ph->p_memsz : DEFAULT_STACK_MAX;
679 }
680 }
681 if (ph->p_type != PT_LOAD) continue;
682 nsegs++;
683 if (ph->p_vaddr < addr_min) {
684 addr_min = ph->p_vaddr;
685 off_start = ph->p_offset;
686 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
687 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
688 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
689 }
690 if (ph->p_vaddr+ph->p_memsz > addr_max) {
691 addr_max = ph->p_vaddr+ph->p_memsz;
692 }
693 }
694 if (!dyn) goto noexec;
695 if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) {
696 dso->loadmap = calloc(1, sizeof *dso->loadmap
697 + nsegs * sizeof *dso->loadmap->segs);
698 if (!dso->loadmap) goto error;
699 dso->loadmap->nsegs = nsegs;
700 for (ph=ph0, i=0; i<nsegs; ph=(void *)((char *)ph+eh->e_phentsize)) {
701 if (ph->p_type != PT_LOAD) continue;
702 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
703 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
704 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
705 map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE-1),
706 prot, MAP_PRIVATE,
707 fd, ph->p_offset & -PAGE_SIZE);
708 if (map == MAP_FAILED) {
709 unmap_library(dso);
710 goto error;
711 }
712 dso->loadmap->segs[i].addr = (size_t)map +
713 (ph->p_vaddr & PAGE_SIZE-1);
714 dso->loadmap->segs[i].p_vaddr = ph->p_vaddr;
715 dso->loadmap->segs[i].p_memsz = ph->p_memsz;
716 i++;
717 if (prot & PROT_WRITE) {
718 size_t brk = (ph->p_vaddr & PAGE_SIZE-1)
719 + ph->p_filesz;
720 size_t pgbrk = brk + PAGE_SIZE-1 & -PAGE_SIZE;
721 size_t pgend = brk + ph->p_memsz - ph->p_filesz
722 + PAGE_SIZE-1 & -PAGE_SIZE;
723 if (pgend > pgbrk && mmap_fixed(map+pgbrk,
724 pgend-pgbrk, prot,
725 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS,
726 -1, off_start) == MAP_FAILED)
727 goto error;
728 memset(map + brk, 0, pgbrk-brk);
729 }
730 }
731 map = (void *)dso->loadmap->segs[0].addr;
732 map_len = 0;
733 goto done_mapping;
734 }
735 addr_max += PAGE_SIZE-1;
736 addr_max &= -PAGE_SIZE;
737 addr_min &= -PAGE_SIZE;
738 off_start &= -PAGE_SIZE;
739 map_len = addr_max - addr_min + off_start;
740 /* The first time, we map too much, possibly even more than
741 * the length of the file. This is okay because we will not
742 * use the invalid part; we just need to reserve the right
743 * amount of virtual address space to map over later. */
744 map = DL_NOMMU_SUPPORT
745 ? mmap((void *)addr_min, map_len, PROT_READ|PROT_WRITE|PROT_EXEC,
746 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
747 : mmap((void *)addr_min, map_len, prot,
748 MAP_PRIVATE, fd, off_start);
749 if (map==MAP_FAILED) goto error;
750 dso->map = map;
751 dso->map_len = map_len;
752 /* If the loaded file is not relocatable and the requested address is
753 * not available, then the load operation must fail. */
754 if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) {
755 errno = EBUSY;
756 goto error;
757 }
758 base = map - addr_min;
759 dso->phdr = 0;
760 dso->phnum = 0;
761 for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
762 if (ph->p_type != PT_LOAD) continue;
763 /* Check if the programs headers are in this load segment, and
764 * if so, record the address for use by dl_iterate_phdr. */
765 if (!dso->phdr && eh->e_phoff >= ph->p_offset
766 && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) {
767 dso->phdr = (void *)(base + ph->p_vaddr
768 + (eh->e_phoff-ph->p_offset));
769 dso->phnum = eh->e_phnum;
770 dso->phentsize = eh->e_phentsize;
771 }
772 this_min = ph->p_vaddr & -PAGE_SIZE;
773 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
774 off_start = ph->p_offset & -PAGE_SIZE;
775 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
776 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
777 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
778 #ifdef __LITEOS_A__
779 if ((ph->p_flags & PF_R) && (ph->p_flags & PF_X) && (!(ph->p_flags & PF_W))) {
780 Phdr *next_ph = ph;
781 for (int j = i - 1; j > 0; j--) {
782 next_ph = (void *)((char *)next_ph+eh->e_phentsize);
783 if (next_ph->p_type != PT_LOAD) {
784 continue;
785 }
786 size_t p_vaddr = (next_ph->p_vaddr & -(PAGE_SIZE));
787 if (p_vaddr > this_max) {
788 mprotect(base + this_max, p_vaddr - this_max, PROT_READ);
789 }
790 break;
791 }
792 }
793 #endif
794 /* Reuse the existing mapping for the lowest-address LOAD */
795 if ((ph->p_vaddr & -PAGE_SIZE) != addr_min || DL_NOMMU_SUPPORT)
796 if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
797 goto error;
798 if (ph->p_memsz > ph->p_filesz && (ph->p_flags&PF_W)) {
799 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
800 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
801 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
802 if (pgbrk-(size_t)base < this_max && mmap_fixed((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED)
803 goto error;
804 }
805 }
806 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
807 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
808 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC)
809 && errno != ENOSYS)
810 goto error;
811 break;
812 }
813 done_mapping:
814 dso->base = base;
815 dso->dynv = laddr(dso, dyn);
816 if (dso->tls.size) dso->tls.image = laddr(dso, tls_image);
817 free(allocated_buf);
818 return map;
819 noexec:
820 errno = ENOEXEC;
821 error:
822 if (map!=MAP_FAILED) unmap_library(dso);
823 free(allocated_buf);
824 return 0;
825 }
826
path_open(const char * name,const char * s,char * buf,size_t buf_size)827 static int path_open(const char *name, const char *s, char *buf, size_t buf_size)
828 {
829 size_t l;
830 int fd;
831 for (;;) {
832 s += strspn(s, ":\n");
833 l = strcspn(s, ":\n");
834 if (l-1 >= INT_MAX) return -1;
835 if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) < buf_size) {
836 if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
837 switch (errno) {
838 case ENOENT:
839 case ENOTDIR:
840 case EACCES:
841 case ENAMETOOLONG:
842 break;
843 default:
844 /* Any negative value but -1 will inhibit
845 * futher path search. */
846 return -2;
847 }
848 }
849 s += l;
850 }
851 }
852
fixup_rpath(struct dso * p,char * buf,size_t buf_size)853 static int fixup_rpath(struct dso *p, char *buf, size_t buf_size)
854 {
855 size_t n, l;
856 const char *s, *t, *origin;
857 char *d;
858 if (p->rpath || !p->rpath_orig) return 0;
859 if (!strchr(p->rpath_orig, '$')) {
860 p->rpath = p->rpath_orig;
861 return 0;
862 }
863 n = 0;
864 s = p->rpath_orig;
865 while ((t=strchr(s, '$'))) {
866 if (strncmp(t, "$ORIGIN", 7) && strncmp(t, "${ORIGIN}", 9))
867 return 0;
868 s = t+1;
869 n++;
870 }
871 if (n > SSIZE_MAX/PATH_MAX) return 0;
872
873 if (p->kernel_mapped) {
874 /* $ORIGIN searches cannot be performed for the main program
875 * when it is suid/sgid/AT_SECURE. This is because the
876 * pathname is under the control of the caller of execve.
877 * For libraries, however, $ORIGIN can be processed safely
878 * since the library's pathname came from a trusted source
879 * (either system paths or a call to dlopen). */
880 if (libc.secure)
881 return 0;
882 l = readlink("/proc/self/exe", buf, buf_size);
883 if (l == -1) switch (errno) {
884 case ENOENT:
885 case ENOTDIR:
886 case EACCES:
887 break;
888 default:
889 return -1;
890 }
891 if (l >= buf_size)
892 return 0;
893 buf[l] = 0;
894 origin = buf;
895 } else {
896 origin = p->name;
897 }
898 t = strrchr(origin, '/');
899 if (t) {
900 l = t-origin;
901 } else {
902 /* Normally p->name will always be an absolute or relative
903 * pathname containing at least one '/' character, but in the
904 * case where ldso was invoked as a command to execute a
905 * program in the working directory, app.name may not. Fix. */
906 origin = ".";
907 l = 1;
908 }
909 /* Disallow non-absolute origins for suid/sgid/AT_SECURE. */
910 if (libc.secure && *origin != '/')
911 return 0;
912 p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1);
913 if (!p->rpath) return -1;
914
915 d = p->rpath;
916 s = p->rpath_orig;
917 while ((t=strchr(s, '$'))) {
918 memcpy(d, s, t-s);
919 d += t-s;
920 memcpy(d, origin, l);
921 d += l;
922 /* It was determined previously that the '$' is followed
923 * either by "ORIGIN" or "{ORIGIN}". */
924 s = t + 7 + 2*(t[1]=='{');
925 }
926 strcpy(d, s);
927 return 0;
928 }
929
decode_dyn(struct dso * p)930 static void decode_dyn(struct dso *p)
931 {
932 size_t dyn[DYN_CNT];
933 decode_vec(p->dynv, dyn, DYN_CNT);
934 p->syms = laddr(p, dyn[DT_SYMTAB]);
935 p->strings = laddr(p, dyn[DT_STRTAB]);
936 if (dyn[0]&(1<<DT_HASH))
937 p->hashtab = laddr(p, dyn[DT_HASH]);
938 if (dyn[0]&(1<<DT_RPATH))
939 p->rpath_orig = p->strings + dyn[DT_RPATH];
940 if (dyn[0]&(1<<DT_RUNPATH))
941 p->rpath_orig = p->strings + dyn[DT_RUNPATH];
942 if (dyn[0]&(1<<DT_PLTGOT))
943 p->got = laddr(p, dyn[DT_PLTGOT]);
944 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
945 p->ghashtab = laddr(p, *dyn);
946 if (search_vec(p->dynv, dyn, DT_VERSYM))
947 p->versym = laddr(p, *dyn);
948 }
949
count_syms(struct dso * p)950 static size_t count_syms(struct dso *p)
951 {
952 if (p->hashtab) return p->hashtab[1];
953
954 size_t nsym, i;
955 uint32_t *buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
956 uint32_t *hashval;
957 for (i = nsym = 0; i < p->ghashtab[0]; i++) {
958 if (buckets[i] > nsym)
959 nsym = buckets[i];
960 }
961 if (nsym) {
962 hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]);
963 do nsym++;
964 while (!(*hashval++ & 1));
965 }
966 return nsym;
967 }
968
dl_mmap(size_t n)969 static void *dl_mmap(size_t n)
970 {
971 void *p;
972 int prot = PROT_READ|PROT_WRITE, flags = MAP_ANONYMOUS|MAP_PRIVATE;
973 #ifdef SYS_mmap2
974 p = (void *)__syscall(SYS_mmap2, 0, n, prot, flags, -1, 0);
975 #else
976 p = (void *)__syscall(SYS_mmap, 0, n, prot, flags, -1, 0);
977 #endif
978 return (unsigned long)p > -4096UL ? 0 : p;
979 }
980
makefuncdescs(struct dso * p)981 static void makefuncdescs(struct dso *p)
982 {
983 static int self_done;
984 size_t nsym = count_syms(p);
985 size_t i, size = nsym * sizeof(*p->funcdescs);
986
987 if (!self_done) {
988 p->funcdescs = dl_mmap(size);
989 self_done = 1;
990 } else {
991 p->funcdescs = malloc(size);
992 }
993 if (!p->funcdescs) {
994 if (!runtime) a_crash();
995 error("Error allocating function descriptors for %s", p->name);
996 longjmp(*rtld_fail, 1);
997 }
998 for (i=0; i<nsym; i++) {
999 if ((p->syms[i].st_info&0xf)==STT_FUNC && p->syms[i].st_shndx) {
1000 p->funcdescs[i].addr = laddr(p, p->syms[i].st_value);
1001 p->funcdescs[i].got = p->got;
1002 } else {
1003 p->funcdescs[i].addr = 0;
1004 p->funcdescs[i].got = 0;
1005 }
1006 }
1007 }
1008
load_library(const char * name,struct dso * needed_by)1009 static struct dso *load_library(const char *name, struct dso *needed_by)
1010 {
1011 char buf[2*NAME_MAX+2];
1012 #ifdef __LITEOS_A__
1013 char fullpath[2*NAME_MAX+2];
1014 #endif
1015 const char *pathname;
1016 unsigned char *map;
1017 struct dso *p, temp_dso = {0};
1018 int fd;
1019 struct stat st;
1020 size_t alloc_size;
1021 int n_th = 0;
1022 int is_self = 0;
1023
1024 if (!*name) {
1025 errno = EINVAL;
1026 return 0;
1027 }
1028
1029 /* Catch and block attempts to reload the implementation itself */
1030 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
1031 static const char reserved[] =
1032 "c.pthread.rt.m.dl.util.xnet.";
1033 const char *rp, *next;
1034 for (rp=reserved; *rp; rp=next) {
1035 next = strchr(rp, '.') + 1;
1036 if (strncmp(name+3, rp, next-rp) == 0)
1037 break;
1038 }
1039 if (*rp) {
1040 if (ldd_mode) {
1041 /* Track which names have been resolved
1042 * and only report each one once. */
1043 static unsigned reported;
1044 unsigned mask = 1U<<(rp-reserved);
1045 if (!(reported & mask)) {
1046 reported |= mask;
1047 dprintf(1, "\t%s => %s (%p)\n",
1048 name, ldso.name,
1049 ldso.base);
1050 }
1051 }
1052 is_self = 1;
1053 }
1054 }
1055 if (!strcmp(name, ldso.name)) is_self = 1;
1056 if (is_self) {
1057 if (!ldso.prev) {
1058 tail->next = &ldso;
1059 ldso.prev = tail;
1060 tail = &ldso;
1061 }
1062 return &ldso;
1063 }
1064 if (strchr(name, '/')) {
1065 pathname = name;
1066 fd = open(name, O_RDONLY|O_CLOEXEC);
1067 } else {
1068 /* Search for the name to see if it's already loaded */
1069 for (p=head->next; p; p=p->next) {
1070 if (p->shortname && !strcmp(p->shortname, name)) {
1071 return p;
1072 }
1073 }
1074 if (strlen(name) > NAME_MAX) return 0;
1075 fd = -1;
1076 if (env_path) fd = path_open(name, env_path, buf, sizeof buf);
1077 for (p=needed_by; fd == -1 && p; p=p->needed_by) {
1078 if (fixup_rpath(p, buf, sizeof buf) < 0)
1079 fd = -2; /* Inhibit further search. */
1080 if (p->rpath)
1081 fd = path_open(name, p->rpath, buf, sizeof buf);
1082 }
1083 if (fd == -1) {
1084 if (!sys_path) {
1085 char *prefix = 0;
1086 size_t prefix_len;
1087 if (ldso.name[0]=='/') {
1088 char *s, *t, *z;
1089 for (s=t=z=ldso.name; *s; s++)
1090 if (*s=='/') z=t, t=s;
1091 prefix_len = z-ldso.name;
1092 if (prefix_len < PATH_MAX)
1093 prefix = ldso.name;
1094 }
1095 if (!prefix) {
1096 prefix = "";
1097 prefix_len = 0;
1098 }
1099 char etc_ldso_path[prefix_len + 1
1100 + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"];
1101 snprintf(etc_ldso_path, sizeof etc_ldso_path,
1102 "%.*s/etc/ld-musl-" LDSO_ARCH ".path",
1103 (int)prefix_len, prefix);
1104 fd = open(etc_ldso_path, O_RDONLY|O_CLOEXEC);
1105 if (fd>=0) {
1106 size_t n = 0;
1107 if (!fstat(fd, &st)) n = st.st_size;
1108 if ((sys_path = malloc(n+1)))
1109 sys_path[n] = 0;
1110 if (!sys_path || read_loop(fd, sys_path, n)<0) {
1111 free(sys_path);
1112 sys_path = "";
1113 }
1114 close(fd);
1115 } else if (errno != ENOENT) {
1116 sys_path = "";
1117 }
1118 }
1119 #ifdef __LITEOS_A__
1120 if (!sys_path || sys_path[0] == 0) {
1121 sys_path = "/usr/lib:/lib:/usr/local/lib";
1122 }
1123 #else
1124 if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib";
1125 #endif
1126 fd = path_open(name, sys_path, buf, sizeof buf);
1127 }
1128 pathname = buf;
1129 }
1130 if (fd < 0) return 0;
1131 #ifdef __LITEOS_A__
1132 if (pathname[0] != '/') {
1133 if (!realpath(pathname, fullpath)) {
1134 close(fd);
1135 return 0;
1136 }
1137 pathname = fullpath;
1138 }
1139 #else
1140 if (fstat(fd, &st) < 0) {
1141 close(fd);
1142 return 0;
1143 }
1144 #endif
1145 for (p=head->next; p; p=p->next) {
1146 #ifdef __LITEOS_A__
1147 if (!strcmp(p->name, pathname)) {
1148 #else
1149 if (p->dev == st.st_dev && p->ino == st.st_ino) {
1150 #endif
1151 /* If this library was previously loaded with a
1152 * pathname but a search found the same inode,
1153 * setup its shortname so it can be found by name. */
1154 if (!p->shortname && pathname != name)
1155 p->shortname = strrchr(p->name, '/')+1;
1156 close(fd);
1157 return p;
1158 }
1159 }
1160 map = noload ? 0 : map_library(fd, &temp_dso);
1161 close(fd);
1162 if (!map) return 0;
1163
1164 /* Avoid the danger of getting two versions of libc mapped into the
1165 * same process when an absolute pathname was used. The symbols
1166 * checked are chosen to catch both musl and glibc, and to avoid
1167 * false positives from interposition-hack libraries. */
1168 decode_dyn(&temp_dso);
1169 if (find_sym(&temp_dso, "__libc_start_main", 1).sym &&
1170 find_sym(&temp_dso, "stdin", 1).sym) {
1171 unmap_library(&temp_dso);
1172 return load_library("libc.so", needed_by);
1173 }
1174 /* Past this point, if we haven't reached runtime yet, ldso has
1175 * committed either to use the mapped library or to abort execution.
1176 * Unmapping is not possible, so we can safely reclaim gaps. */
1177 if (!runtime) reclaim_gaps(&temp_dso);
1178
1179 /* Allocate storage for the new DSO. When there is TLS, this
1180 * storage must include a reservation for all pre-existing
1181 * threads to obtain copies of both the new TLS, and an
1182 * extended DTV capable of storing an additional slot for
1183 * the newly-loaded DSO. */
1184 alloc_size = sizeof *p + strlen(pathname) + 1;
1185 if (runtime && temp_dso.tls.image) {
1186 size_t per_th = temp_dso.tls.size + temp_dso.tls.align
1187 + sizeof(void *) * (tls_cnt+3);
1188 n_th = libc.threads_minus_1 + 1;
1189 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
1190 else alloc_size += n_th * per_th;
1191 }
1192 p = calloc(1, alloc_size);
1193 if (!p) {
1194 unmap_library(&temp_dso);
1195 return 0;
1196 }
1197 memcpy(p, &temp_dso, sizeof temp_dso);
1198 #ifndef __LITEOS_A__
1199 p->dev = st.st_dev;
1200 p->ino = st.st_ino;
1201 #endif
1202 p->needed_by = needed_by;
1203 p->name = p->buf;
1204 p->runtime_loaded = runtime;
1205 strcpy(p->name, pathname);
1206 /* Add a shortname only if name arg was not an explicit pathname. */
1207 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
1208 if (p->tls.image) {
1209 p->tls_id = ++tls_cnt;
1210 tls_align = MAXP2(tls_align, p->tls.align);
1211 #ifdef TLS_ABOVE_TP
1212 p->tls.offset = tls_offset + ( (p->tls.align-1) &
1213 (-tls_offset + (uintptr_t)p->tls.image) );
1214 tls_offset = p->tls.offset + p->tls.size;
1215 #else
1216 tls_offset += p->tls.size + p->tls.align - 1;
1217 tls_offset -= (tls_offset + (uintptr_t)p->tls.image)
1218 & (p->tls.align-1);
1219 p->tls.offset = tls_offset;
1220 #endif
1221 p->new_dtv = (void *)(-sizeof(size_t) &
1222 (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t)));
1223 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1));
1224 if (tls_tail) tls_tail->next = &p->tls;
1225 else libc.tls_head = &p->tls;
1226 tls_tail = &p->tls;
1227 }
1228
1229 tail->next = p;
1230 p->prev = tail;
1231 tail = p;
1232
1233 if (DL_FDPIC) makefuncdescs(p);
1234
1235 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base);
1236
1237 return p;
1238 }
1239
1240 static void load_direct_deps(struct dso *p)
1241 {
1242 size_t i, cnt=0;
1243
1244 if (p->deps) return;
1245 /* For head, all preloads are direct pseudo-dependencies.
1246 * Count and include them now to avoid realloc later. */
1247 if (p==head) for (struct dso *q=p->next; q; q=q->next)
1248 cnt++;
1249 for (i=0; p->dynv[i]; i+=2)
1250 if (p->dynv[i] == DT_NEEDED) cnt++;
1251 /* Use builtin buffer for apps with no external deps, to
1252 * preserve property of no runtime failure paths. */
1253 p->deps = (p==head && cnt<2) ? builtin_deps :
1254 calloc(cnt+1, sizeof *p->deps);
1255 if (!p->deps) {
1256 error("Error loading dependencies for %s", p->name);
1257 if (runtime) longjmp(*rtld_fail, 1);
1258 }
1259 cnt=0;
1260 if (p==head) for (struct dso *q=p->next; q; q=q->next)
1261 p->deps[cnt++] = q;
1262 for (i=0; p->dynv[i]; i+=2) {
1263 if (p->dynv[i] != DT_NEEDED) continue;
1264 struct dso *dep = load_library(p->strings + p->dynv[i+1], p);
1265 if (!dep) {
1266 error("Error loading shared library %s: %m (needed by %s)",
1267 p->strings + p->dynv[i+1], p->name);
1268 if (runtime) longjmp(*rtld_fail, 1);
1269 continue;
1270 }
1271 p->deps[cnt++] = dep;
1272 }
1273 p->deps[cnt] = 0;
1274 p->ndeps_direct = cnt;
1275 }
1276
1277 static void load_deps(struct dso *p)
1278 {
1279 if (p->deps) return;
1280 for (; p; p=p->next)
1281 load_direct_deps(p);
1282 }
1283
1284 static void extend_bfs_deps(struct dso *p)
1285 {
1286 size_t i, j, cnt, ndeps_all;
1287 struct dso **tmp;
1288
1289 /* Can't use realloc if the original p->deps was allocated at
1290 * program entry and malloc has been replaced, or if it's
1291 * the builtin non-allocated trivial main program deps array. */
1292 int no_realloc = (__malloc_replaced && !p->runtime_loaded)
1293 || p->deps == builtin_deps;
1294
1295 if (p->bfs_built) return;
1296 ndeps_all = p->ndeps_direct;
1297
1298 /* Mark existing (direct) deps so they won't be duplicated. */
1299 for (i=0; p->deps[i]; i++)
1300 p->deps[i]->mark = 1;
1301
1302 /* For each dependency already in the list, copy its list of direct
1303 * dependencies to the list, excluding any items already in the
1304 * list. Note that the list this loop iterates over will grow during
1305 * the loop, but since duplicates are excluded, growth is bounded. */
1306 for (i=0; p->deps[i]; i++) {
1307 struct dso *dep = p->deps[i];
1308 for (j=cnt=0; j<dep->ndeps_direct; j++)
1309 if (!dep->deps[j]->mark) cnt++;
1310 tmp = no_realloc ?
1311 malloc(sizeof(*tmp) * (ndeps_all+cnt+1)) :
1312 realloc(p->deps, sizeof(*tmp) * (ndeps_all+cnt+1));
1313 if (!tmp) {
1314 error("Error recording dependencies for %s", p->name);
1315 if (runtime) longjmp(*rtld_fail, 1);
1316 continue;
1317 }
1318 if (no_realloc) {
1319 memcpy(tmp, p->deps, sizeof(*tmp) * (ndeps_all+1));
1320 no_realloc = 0;
1321 }
1322 p->deps = tmp;
1323 for (j=0; j<dep->ndeps_direct; j++) {
1324 if (dep->deps[j]->mark) continue;
1325 dep->deps[j]->mark = 1;
1326 p->deps[ndeps_all++] = dep->deps[j];
1327 }
1328 p->deps[ndeps_all] = 0;
1329 }
1330 p->bfs_built = 1;
1331 for (p=head; p; p=p->next)
1332 p->mark = 0;
1333 }
1334
1335 static void load_preload(char *s)
1336 {
1337 int tmp;
1338 char *z;
1339 for (z=s; *z; s=z) {
1340 for ( ; *s && (isspace(*s) || *s==':'); s++);
1341 for (z=s; *z && !isspace(*z) && *z!=':'; z++);
1342 tmp = *z;
1343 *z = 0;
1344 load_library(s, 0);
1345 *z = tmp;
1346 }
1347 }
1348
1349 static void add_syms(struct dso *p)
1350 {
1351 if (!p->syms_next && syms_tail != p) {
1352 syms_tail->syms_next = p;
1353 syms_tail = p;
1354 }
1355 }
1356
1357 static void revert_syms(struct dso *old_tail)
1358 {
1359 struct dso *p, *next;
1360 /* Chop off the tail of the list of dsos that participate in
1361 * the global symbol table, reverting them to RTLD_LOCAL. */
1362 for (p=old_tail; p; p=next) {
1363 next = p->syms_next;
1364 p->syms_next = 0;
1365 }
1366 syms_tail = old_tail;
1367 }
1368
1369 static void do_mips_relocs(struct dso *p, size_t *got)
1370 {
1371 size_t i, j, rel[2];
1372 unsigned char *base = p->base;
1373 i=0; search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO);
1374 if (p==&ldso) {
1375 got += i;
1376 } else {
1377 while (i--) *got++ += (size_t)base;
1378 }
1379 j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
1380 i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
1381 Sym *sym = p->syms + j;
1382 rel[0] = (unsigned char *)got - base;
1383 for (i-=j; i; i--, sym++, rel[0]+=sizeof(size_t)) {
1384 rel[1] = R_INFO(sym-p->syms, R_MIPS_JUMP_SLOT);
1385 do_relocs(p, rel, sizeof rel, 2);
1386 }
1387 }
1388
1389 static void reloc_all(struct dso *p)
1390 {
1391 size_t dyn[DYN_CNT];
1392 for (; p; p=p->next) {
1393 if (p->relocated) continue;
1394 decode_vec(p->dynv, dyn, DYN_CNT);
1395 if (NEED_MIPS_GOT_RELOCS)
1396 do_mips_relocs(p, laddr(p, dyn[DT_PLTGOT]));
1397 do_relocs(p, laddr(p, dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
1398 2+(dyn[DT_PLTREL]==DT_RELA));
1399 do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2);
1400 do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3);
1401
1402 if (head != &ldso && p->relro_start != p->relro_end &&
1403 mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ)
1404 && errno != ENOSYS) {
1405 error("Error relocating %s: RELRO protection failed: %m",
1406 p->name);
1407 if (runtime) longjmp(*rtld_fail, 1);
1408 }
1409
1410 p->relocated = 1;
1411 }
1412 }
1413
1414 static void kernel_mapped_dso(struct dso *p)
1415 {
1416 size_t min_addr = -1, max_addr = 0, cnt;
1417 Phdr *ph = p->phdr;
1418 for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) {
1419 if (ph->p_type == PT_DYNAMIC) {
1420 p->dynv = laddr(p, ph->p_vaddr);
1421 } else if (ph->p_type == PT_GNU_RELRO) {
1422 p->relro_start = ph->p_vaddr & -PAGE_SIZE;
1423 p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
1424 } else if (ph->p_type == PT_GNU_STACK) {
1425 if (!runtime && ph->p_memsz > __default_stacksize) {
1426 __default_stacksize =
1427 ph->p_memsz < DEFAULT_STACK_MAX ?
1428 ph->p_memsz : DEFAULT_STACK_MAX;
1429 }
1430 }
1431 if (ph->p_type != PT_LOAD) continue;
1432 if (ph->p_vaddr < min_addr)
1433 min_addr = ph->p_vaddr;
1434 if (ph->p_vaddr+ph->p_memsz > max_addr)
1435 max_addr = ph->p_vaddr+ph->p_memsz;
1436 }
1437 min_addr &= -PAGE_SIZE;
1438 max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
1439 p->map = p->base + min_addr;
1440 p->map_len = max_addr - min_addr;
1441 p->kernel_mapped = 1;
1442 }
1443
1444 void __libc_exit_fini()
1445 {
1446 struct dso *p;
1447 size_t dyn[DYN_CNT];
1448 pthread_t self = __pthread_self();
1449
1450 /* Take both locks before setting shutting_down, so that
1451 * either lock is sufficient to read its value. The lock
1452 * order matches that in dlopen to avoid deadlock. */
1453 pthread_rwlock_wrlock(&lock);
1454 pthread_mutex_lock(&init_fini_lock);
1455 shutting_down = 1;
1456 pthread_rwlock_unlock(&lock);
1457 for (p=fini_head; p; p=p->fini_next) {
1458 while (p->ctor_visitor && p->ctor_visitor!=self)
1459 pthread_cond_wait(&ctor_cond, &init_fini_lock);
1460 if (!p->constructed) continue;
1461 decode_vec(p->dynv, dyn, DYN_CNT);
1462 if (dyn[0] & (1<<DT_FINI_ARRAY)) {
1463 size_t n = dyn[DT_FINI_ARRAYSZ]/sizeof(size_t);
1464 size_t *fn = (size_t *)laddr(p, dyn[DT_FINI_ARRAY])+n;
1465 while (n--) ((void (*)(void))*--fn)();
1466 }
1467 #ifndef NO_LEGACY_INITFINI
1468 if ((dyn[0] & (1<<DT_FINI)) && dyn[DT_FINI])
1469 fpaddr(p, dyn[DT_FINI])();
1470 #endif
1471 }
1472 }
1473
1474 void __ldso_atfork(int who)
1475 {
1476 if (who<0) {
1477 pthread_rwlock_wrlock(&lock);
1478 pthread_mutex_lock(&init_fini_lock);
1479 } else {
1480 pthread_mutex_unlock(&init_fini_lock);
1481 pthread_rwlock_unlock(&lock);
1482 }
1483 }
1484
1485 static struct dso **queue_ctors(struct dso *dso)
1486 {
1487 size_t cnt, qpos, spos, i;
1488 struct dso *p, **queue, **stack;
1489
1490 if (ldd_mode) return 0;
1491
1492 /* Bound on queue size is the total number of indirect deps.
1493 * If a bfs deps list was built, we can use it. Otherwise,
1494 * bound by the total number of DSOs, which is always safe and
1495 * is reasonable we use it (for main app at startup). */
1496 if (dso->bfs_built) {
1497 for (cnt=0; dso->deps[cnt]; cnt++)
1498 dso->deps[cnt]->mark = 0;
1499 cnt++; /* self, not included in deps */
1500 } else {
1501 for (cnt=0, p=head; p; cnt++, p=p->next)
1502 p->mark = 0;
1503 }
1504 cnt++; /* termination slot */
1505 if (dso==head && cnt <= countof(builtin_ctor_queue))
1506 queue = builtin_ctor_queue;
1507 else
1508 queue = calloc(cnt, sizeof *queue);
1509
1510 if (!queue) {
1511 error("Error allocating constructor queue: %m\n");
1512 if (runtime) longjmp(*rtld_fail, 1);
1513 return 0;
1514 }
1515
1516 /* Opposite ends of the allocated buffer serve as an output queue
1517 * and a working stack. Setup initial stack with just the argument
1518 * dso and initial queue empty... */
1519 stack = queue;
1520 qpos = 0;
1521 spos = cnt;
1522 stack[--spos] = dso;
1523 dso->next_dep = 0;
1524 dso->mark = 1;
1525
1526 /* Then perform pseudo-DFS sort, but ignoring circular deps. */
1527 while (spos<cnt) {
1528 p = stack[spos++];
1529 while (p->next_dep < p->ndeps_direct) {
1530 if (p->deps[p->next_dep]->mark) {
1531 p->next_dep++;
1532 } else {
1533 stack[--spos] = p;
1534 p = p->deps[p->next_dep];
1535 p->next_dep = 0;
1536 p->mark = 1;
1537 }
1538 }
1539 queue[qpos++] = p;
1540 }
1541 queue[qpos] = 0;
1542 for (i=0; i<qpos; i++) queue[i]->mark = 0;
1543 for (i=0; i<qpos; i++)
1544 if (queue[i]->ctor_visitor && queue[i]->ctor_visitor->tid < 0) {
1545 error("State of %s is inconsistent due to multithreaded fork\n",
1546 queue[i]->name);
1547 free(queue);
1548 if (runtime) longjmp(*rtld_fail, 1);
1549 }
1550
1551 return queue;
1552 }
1553
1554 static void do_init_fini(struct dso **queue)
1555 {
1556 struct dso *p;
1557 size_t dyn[DYN_CNT], i;
1558 pthread_t self = __pthread_self();
1559
1560 pthread_mutex_lock(&init_fini_lock);
1561 for (i=0; (p=queue[i]); i++) {
1562 while ((p->ctor_visitor && p->ctor_visitor!=self) || shutting_down)
1563 pthread_cond_wait(&ctor_cond, &init_fini_lock);
1564 if (p->ctor_visitor || p->constructed)
1565 continue;
1566 p->ctor_visitor = self;
1567
1568 decode_vec(p->dynv, dyn, DYN_CNT);
1569 if (dyn[0] & ((1<<DT_FINI) | (1<<DT_FINI_ARRAY))) {
1570 p->fini_next = fini_head;
1571 fini_head = p;
1572 }
1573
1574 pthread_mutex_unlock(&init_fini_lock);
1575
1576 #ifndef NO_LEGACY_INITFINI
1577 if ((dyn[0] & (1<<DT_INIT)) && dyn[DT_INIT])
1578 fpaddr(p, dyn[DT_INIT])();
1579 #endif
1580 if (dyn[0] & (1<<DT_INIT_ARRAY)) {
1581 size_t n = dyn[DT_INIT_ARRAYSZ]/sizeof(size_t);
1582 size_t *fn = laddr(p, dyn[DT_INIT_ARRAY]);
1583 while (n--) ((void (*)(void))*fn++)();
1584 }
1585
1586 pthread_mutex_lock(&init_fini_lock);
1587 p->ctor_visitor = 0;
1588 p->constructed = 1;
1589 pthread_cond_broadcast(&ctor_cond);
1590 }
1591 pthread_mutex_unlock(&init_fini_lock);
1592 }
1593
1594 void __libc_start_init(void)
1595 {
1596 do_init_fini(main_ctor_queue);
1597 if (!__malloc_replaced && main_ctor_queue != builtin_ctor_queue)
1598 free(main_ctor_queue);
1599 main_ctor_queue = 0;
1600 }
1601
1602 static void dl_debug_state(void)
1603 {
1604 }
1605
1606 weak_alias(dl_debug_state, _dl_debug_state);
1607
1608 void __init_tls(size_t *auxv)
1609 {
1610 }
1611
1612 static void update_tls_size()
1613 {
1614 libc.tls_cnt = tls_cnt;
1615 libc.tls_align = tls_align;
1616 libc.tls_size = ALIGN(
1617 (1+tls_cnt) * sizeof(void *) +
1618 tls_offset +
1619 sizeof(struct pthread) +
1620 tls_align * 2,
1621 tls_align);
1622 }
1623
1624 static void install_new_tls(void)
1625 {
1626 sigset_t set;
1627 pthread_t self = __pthread_self(), td;
1628 struct dso *dtv_provider = container_of(tls_tail, struct dso, tls);
1629 uintptr_t (*newdtv)[tls_cnt+1] = (void *)dtv_provider->new_dtv;
1630 struct dso *p;
1631 size_t i, j;
1632 size_t old_cnt = self->dtv[0];
1633
1634 __block_app_sigs(&set);
1635 __tl_lock();
1636 /* Copy existing dtv contents from all existing threads. */
1637 for (i=0, td=self; !i || td!=self; i++, td=td->next) {
1638 memcpy(newdtv+i, td->dtv,
1639 (old_cnt+1)*sizeof(uintptr_t));
1640 newdtv[i][0] = tls_cnt;
1641 }
1642 /* Install new dtls into the enlarged, uninstalled dtv copies. */
1643 for (p=head; ; p=p->next) {
1644 if (p->tls_id <= old_cnt) continue;
1645 unsigned char *mem = p->new_tls;
1646 for (j=0; j<i; j++) {
1647 unsigned char *new = mem;
1648 new += ((uintptr_t)p->tls.image - (uintptr_t)mem)
1649 & (p->tls.align-1);
1650 memcpy(new, p->tls.image, p->tls.len);
1651 newdtv[j][p->tls_id] =
1652 (uintptr_t)new + DTP_OFFSET;
1653 mem += p->tls.size + p->tls.align;
1654 }
1655 if (p->tls_id == tls_cnt) break;
1656 }
1657
1658 /* Broadcast barrier to ensure contents of new dtv is visible
1659 * if the new dtv pointer is. The __membarrier function has a
1660 * fallback emulation using signals for kernels that lack the
1661 * feature at the syscall level. */
1662
1663 __membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0);
1664
1665 /* Install new dtv for each thread. */
1666 for (j=0, td=self; !j || td!=self; j++, td=td->next) {
1667 td->dtv = newdtv[j];
1668 }
1669
1670 __tl_unlock();
1671 __restore_sigs(&set);
1672 }
1673
1674 /* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the
1675 * following stage 2 and stage 3 functions via primitive symbolic lookup
1676 * since it does not have access to their addresses to begin with. */
1677
1678 /* Stage 2 of the dynamic linker is called after relative relocations
1679 * have been processed. It can make function calls to static functions
1680 * and access string literals and static data, but cannot use extern
1681 * symbols. Its job is to perform symbolic relocations on the dynamic
1682 * linker itself, but some of the relocations performed may need to be
1683 * replaced later due to copy relocations in the main program. */
1684
1685 hidden void __dls2(unsigned char *base, size_t *sp)
1686 {
1687 size_t *auxv;
1688 for (auxv=sp+1+*sp+1; *auxv; auxv++);
1689 auxv++;
1690 if (DL_FDPIC) {
1691 void *p1 = (void *)sp[-2];
1692 void *p2 = (void *)sp[-1];
1693 if (!p1) {
1694 size_t aux[AUX_CNT];
1695 decode_vec(auxv, aux, AUX_CNT);
1696 if (aux[AT_BASE]) ldso.base = (void *)aux[AT_BASE];
1697 else ldso.base = (void *)(aux[AT_PHDR] & -4096);
1698 }
1699 app_loadmap = p2 ? p1 : 0;
1700 ldso.loadmap = p2 ? p2 : p1;
1701 ldso.base = laddr(&ldso, 0);
1702 } else {
1703 ldso.base = base;
1704 }
1705 Ehdr *ehdr = (void *)ldso.base;
1706 ldso.name = ldso.shortname = "libc.so";
1707 ldso.phnum = ehdr->e_phnum;
1708 ldso.phdr = laddr(&ldso, ehdr->e_phoff);
1709 ldso.phentsize = ehdr->e_phentsize;
1710 kernel_mapped_dso(&ldso);
1711 decode_dyn(&ldso);
1712
1713 if (DL_FDPIC) makefuncdescs(&ldso);
1714
1715 /* Prepare storage for to save clobbered REL addends so they
1716 * can be reused in stage 3. There should be very few. If
1717 * something goes wrong and there are a huge number, abort
1718 * instead of risking stack overflow. */
1719 size_t dyn[DYN_CNT];
1720 decode_vec(ldso.dynv, dyn, DYN_CNT);
1721 size_t *rel = laddr(&ldso, dyn[DT_REL]);
1722 size_t rel_size = dyn[DT_RELSZ];
1723 size_t symbolic_rel_cnt = 0;
1724 apply_addends_to = rel;
1725 for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t))
1726 if (!IS_RELATIVE(rel[1], ldso.syms)) symbolic_rel_cnt++;
1727 if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash();
1728 size_t addends[symbolic_rel_cnt+1];
1729 saved_addends = addends;
1730
1731 head = &ldso;
1732 reloc_all(&ldso);
1733
1734 ldso.relocated = 0;
1735
1736 /* Call dynamic linker stage-2b, __dls2b, looking it up
1737 * symbolically as a barrier against moving the address
1738 * load across the above relocation processing. */
1739 struct symdef dls2b_def = find_sym(&ldso, "__dls2b", 0);
1740 if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls2b_def.sym-ldso.syms])(sp, auxv);
1741 else ((stage3_func)laddr(&ldso, dls2b_def.sym->st_value))(sp, auxv);
1742 }
1743
1744 /* Stage 2b sets up a valid thread pointer, which requires relocations
1745 * completed in stage 2, and on which stage 3 is permitted to depend.
1746 * This is done as a separate stage, with symbolic lookup as a barrier,
1747 * so that loads of the thread pointer and &errno can be pure/const and
1748 * thereby hoistable. */
1749
1750 void __dls2b(size_t *sp, size_t *auxv)
1751 {
1752 /* Setup early thread pointer in builtin_tls for ldso/libc itself to
1753 * use during dynamic linking. If possible it will also serve as the
1754 * thread pointer at runtime. */
1755 search_vec(auxv, &__hwcap, AT_HWCAP);
1756 libc.auxv = auxv;
1757 libc.tls_size = sizeof builtin_tls;
1758 libc.tls_align = tls_align;
1759 if (__init_tp(__copy_tls((void *)builtin_tls)) < 0) {
1760 a_crash();
1761 }
1762
1763 struct symdef dls3_def = find_sym(&ldso, "__dls3", 0);
1764 if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls3_def.sym-ldso.syms])(sp, auxv);
1765 else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp, auxv);
1766 }
1767
1768 /* Stage 3 of the dynamic linker is called with the dynamic linker/libc
1769 * fully functional. Its job is to load (if not already loaded) and
1770 * process dependencies and relocations for the main application and
1771 * transfer control to its entry point. */
1772
1773 void __dls3(size_t *sp, size_t *auxv)
1774 {
1775 static struct dso app, vdso;
1776 size_t aux[AUX_CNT];
1777 size_t i;
1778 char *env_preload=0;
1779 char *replace_argv0=0;
1780 size_t vdso_base;
1781 int argc = *sp;
1782 char **argv = (void *)(sp+1);
1783 char **argv_orig = argv;
1784 char **envp = argv+argc+1;
1785
1786 /* Find aux vector just past environ[] and use it to initialize
1787 * global data that may be needed before we can make syscalls. */
1788 __environ = envp;
1789 decode_vec(auxv, aux, AUX_CNT);
1790 search_vec(auxv, &__sysinfo, AT_SYSINFO);
1791 __pthread_self()->sysinfo = __sysinfo;
1792 libc.page_size = aux[AT_PAGESZ];
1793 libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
1794 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
1795
1796 /* Only trust user/env if kernel says we're not suid/sgid */
1797 if (!libc.secure) {
1798 env_path = getenv("LD_LIBRARY_PATH");
1799 env_preload = getenv("LD_PRELOAD");
1800 }
1801
1802 /* If the main program was already loaded by the kernel,
1803 * AT_PHDR will point to some location other than the dynamic
1804 * linker's program headers. */
1805 if (aux[AT_PHDR] != (size_t)ldso.phdr) {
1806 size_t interp_off = 0;
1807 size_t tls_image = 0;
1808 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
1809 Phdr *phdr = app.phdr = (void *)aux[AT_PHDR];
1810 app.phnum = aux[AT_PHNUM];
1811 app.phentsize = aux[AT_PHENT];
1812 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
1813 if (phdr->p_type == PT_PHDR)
1814 app.base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
1815 else if (phdr->p_type == PT_INTERP)
1816 interp_off = (size_t)phdr->p_vaddr;
1817 else if (phdr->p_type == PT_TLS) {
1818 tls_image = phdr->p_vaddr;
1819 app.tls.len = phdr->p_filesz;
1820 app.tls.size = phdr->p_memsz;
1821 app.tls.align = phdr->p_align;
1822 }
1823 }
1824 if (DL_FDPIC) app.loadmap = app_loadmap;
1825 if (app.tls.size) app.tls.image = laddr(&app, tls_image);
1826 #ifdef __LITEOS_A__
1827 if (interp_off) ldso.name = "/lib/libc.so";
1828 #else
1829 if (interp_off) ldso.name = laddr(&app, interp_off);
1830 #endif
1831 #ifdef __LITEOS_A__
1832 if (argv[0])
1833 app.name = argv[0];
1834 else
1835 app.name = "none";
1836 #else
1837 if ((aux[0] & (1UL<<AT_EXECFN))
1838 && strncmp((char *)aux[AT_EXECFN], "/proc/", 6))
1839 app.name = (char *)aux[AT_EXECFN];
1840 else
1841 app.name = argv[0];
1842 #endif
1843 kernel_mapped_dso(&app);
1844 } else {
1845 int fd;
1846 char *ldname = argv[0];
1847 size_t l = strlen(ldname);
1848 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
1849 argv++;
1850 while (argv[0] && argv[0][0]=='-' && argv[0][1]=='-') {
1851 char *opt = argv[0]+2;
1852 *argv++ = (void *)-1;
1853 if (!*opt) {
1854 break;
1855 } else if (!memcmp(opt, "list", 5)) {
1856 ldd_mode = 1;
1857 } else if (!memcmp(opt, "library-path", 12)) {
1858 if (opt[12]=='=') env_path = opt+13;
1859 else if (opt[12]) *argv = 0;
1860 else if (*argv) env_path = *argv++;
1861 } else if (!memcmp(opt, "preload", 7)) {
1862 if (opt[7]=='=') env_preload = opt+8;
1863 else if (opt[7]) *argv = 0;
1864 else if (*argv) env_preload = *argv++;
1865 } else if (!memcmp(opt, "argv0", 5)) {
1866 if (opt[5]=='=') replace_argv0 = opt+6;
1867 else if (opt[5]) *argv = 0;
1868 else if (*argv) replace_argv0 = *argv++;
1869 } else {
1870 argv[0] = 0;
1871 }
1872 }
1873 argv[-1] = (void *)(argc - (argv-argv_orig));
1874 if (!argv[0]) {
1875 dprintf(2, "musl libc (" LDSO_ARCH ")\n"
1876 "Version %s\n"
1877 "Dynamic Program Loader\n"
1878 "Usage: %s [options] [--] pathname%s\n",
1879 __libc_version, ldname,
1880 ldd_mode ? "" : " [args]");
1881 _exit(1);
1882 }
1883 fd = open(argv[0], O_RDONLY);
1884 if (fd < 0) {
1885 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
1886 _exit(1);
1887 }
1888 Ehdr *ehdr = map_library(fd, &app);
1889 if (!ehdr) {
1890 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
1891 _exit(1);
1892 }
1893 close(fd);
1894 ldso.name = ldname;
1895 app.name = argv[0];
1896 aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry);
1897 /* Find the name that would have been used for the dynamic
1898 * linker had ldd not taken its place. */
1899 if (ldd_mode) {
1900 for (i=0; i<app.phnum; i++) {
1901 if (app.phdr[i].p_type == PT_INTERP)
1902 ldso.name = laddr(&app, app.phdr[i].p_vaddr);
1903 }
1904 dprintf(1, "\t%s (%p)\n", ldso.name, ldso.base);
1905 }
1906 }
1907 if (app.tls.size) {
1908 libc.tls_head = tls_tail = &app.tls;
1909 app.tls_id = tls_cnt = 1;
1910 #ifdef TLS_ABOVE_TP
1911 app.tls.offset = GAP_ABOVE_TP;
1912 app.tls.offset += (-GAP_ABOVE_TP + (uintptr_t)app.tls.image)
1913 & (app.tls.align-1);
1914 tls_offset = app.tls.offset + app.tls.size;
1915 #else
1916 tls_offset = app.tls.offset = app.tls.size
1917 + ( -((uintptr_t)app.tls.image + app.tls.size)
1918 & (app.tls.align-1) );
1919 #endif
1920 tls_align = MAXP2(tls_align, app.tls.align);
1921 }
1922 decode_dyn(&app);
1923 if (DL_FDPIC) {
1924 makefuncdescs(&app);
1925 if (!app.loadmap) {
1926 app.loadmap = (void *)&app_dummy_loadmap;
1927 app.loadmap->nsegs = 1;
1928 app.loadmap->segs[0].addr = (size_t)app.map;
1929 app.loadmap->segs[0].p_vaddr = (size_t)app.map
1930 - (size_t)app.base;
1931 app.loadmap->segs[0].p_memsz = app.map_len;
1932 }
1933 argv[-3] = (void *)app.loadmap;
1934 }
1935
1936 /* Initial dso chain consists only of the app. */
1937 head = tail = syms_tail = &app;
1938
1939 /* Donate unused parts of app and library mapping to malloc */
1940 reclaim_gaps(&app);
1941 reclaim_gaps(&ldso);
1942
1943 /* Load preload/needed libraries, add symbols to global namespace. */
1944 ldso.deps = (struct dso **)no_deps;
1945 if (env_preload) load_preload(env_preload);
1946 load_deps(&app);
1947 for (struct dso *p=head; p; p=p->next)
1948 add_syms(p);
1949
1950 /* Attach to vdso, if provided by the kernel, last so that it does
1951 * not become part of the global namespace. */
1952 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR) && vdso_base) {
1953 Ehdr *ehdr = (void *)vdso_base;
1954 Phdr *phdr = vdso.phdr = (void *)(vdso_base + ehdr->e_phoff);
1955 vdso.phnum = ehdr->e_phnum;
1956 vdso.phentsize = ehdr->e_phentsize;
1957 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
1958 if (phdr->p_type == PT_DYNAMIC)
1959 vdso.dynv = (void *)(vdso_base + phdr->p_offset);
1960 if (phdr->p_type == PT_LOAD)
1961 vdso.base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
1962 }
1963 vdso.name = "";
1964 #ifdef __LITEOS_A__
1965 vdso.shortname = "OHOS-vdso.so";
1966 #else
1967 vdso.shortname = "linux-gate.so.1";
1968 #endif
1969 vdso.relocated = 1;
1970 vdso.deps = (struct dso **)no_deps;
1971 decode_dyn(&vdso);
1972 vdso.prev = tail;
1973 tail->next = &vdso;
1974 tail = &vdso;
1975 }
1976
1977 for (i=0; app.dynv[i]; i+=2) {
1978 if (!DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG)
1979 app.dynv[i+1] = (size_t)&debug;
1980 if (DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG_INDIRECT) {
1981 size_t *ptr = (size_t *) app.dynv[i+1];
1982 *ptr = (size_t)&debug;
1983 }
1984 }
1985
1986 /* This must be done before final relocations, since it calls
1987 * malloc, which may be provided by the application. Calling any
1988 * application code prior to the jump to its entry point is not
1989 * valid in our model and does not work with FDPIC, where there
1990 * are additional relocation-like fixups that only the entry point
1991 * code can see to perform. */
1992 main_ctor_queue = queue_ctors(&app);
1993
1994 /* Initial TLS must also be allocated before final relocations
1995 * might result in calloc being a call to application code. */
1996 update_tls_size();
1997 void *initial_tls = builtin_tls;
1998 if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) {
1999 initial_tls = calloc(libc.tls_size, 1);
2000 if (!initial_tls) {
2001 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
2002 argv[0], libc.tls_size);
2003 _exit(127);
2004 }
2005 }
2006 static_tls_cnt = tls_cnt;
2007
2008 /* The main program must be relocated LAST since it may contain
2009 * copy relocations which depend on libraries' relocations. */
2010 reloc_all(app.next);
2011 reloc_all(&app);
2012
2013 /* Actual copying to new TLS needs to happen after relocations,
2014 * since the TLS images might have contained relocated addresses. */
2015 if (initial_tls != builtin_tls) {
2016 if (__init_tp(__copy_tls(initial_tls)) < 0) {
2017 a_crash();
2018 }
2019 } else {
2020 size_t tmp_tls_size = libc.tls_size;
2021 pthread_t self = __pthread_self();
2022 /* Temporarily set the tls size to the full size of
2023 * builtin_tls so that __copy_tls will use the same layout
2024 * as it did for before. Then check, just to be safe. */
2025 libc.tls_size = sizeof builtin_tls;
2026 if (__copy_tls((void*)builtin_tls) != self) a_crash();
2027 libc.tls_size = tmp_tls_size;
2028 }
2029
2030 if (ldso_fail) _exit(127);
2031 if (ldd_mode) _exit(0);
2032
2033 /* Determine if malloc was interposed by a replacement implementation
2034 * so that calloc and the memalign family can harden against the
2035 * possibility of incomplete replacement. */
2036 if (find_sym(head, "malloc", 1).dso != &ldso)
2037 __malloc_replaced = 1;
2038 if (find_sym(head, "aligned_alloc", 1).dso != &ldso)
2039 __aligned_alloc_replaced = 1;
2040
2041 /* Switch to runtime mode: any further failures in the dynamic
2042 * linker are a reportable failure rather than a fatal startup
2043 * error. */
2044 runtime = 1;
2045
2046 debug.ver = 1;
2047 debug.bp = dl_debug_state;
2048 debug.head = head;
2049 debug.base = ldso.base;
2050 debug.state = RT_CONSISTENT;
2051 _dl_debug_state();
2052
2053 if (replace_argv0) argv[0] = replace_argv0;
2054
2055 errno = 0;
2056
2057 CRTJMP((void *)aux[AT_ENTRY], argv-1);
2058 for(;;);
2059 }
2060
2061 static void prepare_lazy(struct dso *p)
2062 {
2063 size_t dyn[DYN_CNT], n, flags1=0;
2064 decode_vec(p->dynv, dyn, DYN_CNT);
2065 search_vec(p->dynv, &flags1, DT_FLAGS_1);
2066 if (dyn[DT_BIND_NOW] || (dyn[DT_FLAGS] & DF_BIND_NOW) || (flags1 & DF_1_NOW))
2067 return;
2068 n = dyn[DT_RELSZ]/2 + dyn[DT_RELASZ]/3 + dyn[DT_PLTRELSZ]/2 + 1;
2069 if (NEED_MIPS_GOT_RELOCS) {
2070 size_t j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
2071 size_t i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
2072 n += i-j;
2073 }
2074 p->lazy = calloc(n, 3*sizeof(size_t));
2075 if (!p->lazy) {
2076 error("Error preparing lazy relocation for %s: %m", p->name);
2077 longjmp(*rtld_fail, 1);
2078 }
2079 p->lazy_next = lazy_head;
2080 lazy_head = p;
2081 }
2082
2083 void *dlopen(const char *file, int mode)
2084 {
2085 struct dso *volatile p, *orig_tail, *orig_syms_tail, *orig_lazy_head, *next;
2086 struct tls_module *orig_tls_tail;
2087 size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
2088 size_t i;
2089 int cs;
2090 jmp_buf jb;
2091 struct dso **volatile ctor_queue = 0;
2092 #ifdef __LITEOS_A__
2093 if (mode & ~(RTLD_LAZY | RTLD_NOW | RTLD_NOLOAD | RTLD_GLOBAL | RTLD_LOCAL | RTLD_NODELETE)) {
2094 error("invalid mode parameter for dlopen().");
2095 return NULL;
2096 }
2097
2098 if ((mode & (RTLD_LAZY | RTLD_NOW)) == 0) {
2099 error("invalid mode, one of RTLD_LAZY and RTLD_NOW must be set.");
2100 return NULL;
2101 }
2102 #endif
2103 if (!file) return head;
2104
2105 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
2106 pthread_rwlock_wrlock(&lock);
2107 __inhibit_ptc();
2108
2109 debug.state = RT_ADD;
2110 _dl_debug_state();
2111
2112 p = 0;
2113 if (shutting_down) {
2114 error("Cannot dlopen while program is exiting.");
2115 goto end;
2116 }
2117 orig_tls_tail = tls_tail;
2118 orig_tls_cnt = tls_cnt;
2119 orig_tls_offset = tls_offset;
2120 orig_tls_align = tls_align;
2121 orig_lazy_head = lazy_head;
2122 orig_syms_tail = syms_tail;
2123 orig_tail = tail;
2124 noload = mode & RTLD_NOLOAD;
2125
2126 rtld_fail = &jb;
2127 if (setjmp(*rtld_fail)) {
2128 /* Clean up anything new that was (partially) loaded */
2129 revert_syms(orig_syms_tail);
2130 for (p=orig_tail->next; p; p=next) {
2131 next = p->next;
2132 while (p->td_index) {
2133 void *tmp = p->td_index->next;
2134 free(p->td_index);
2135 p->td_index = tmp;
2136 }
2137 free(p->funcdescs);
2138 if (p->rpath != p->rpath_orig)
2139 free(p->rpath);
2140 free(p->deps);
2141 unmap_library(p);
2142 free(p);
2143 }
2144 free(ctor_queue);
2145 ctor_queue = 0;
2146 if (!orig_tls_tail) libc.tls_head = 0;
2147 tls_tail = orig_tls_tail;
2148 if (tls_tail) tls_tail->next = 0;
2149 tls_cnt = orig_tls_cnt;
2150 tls_offset = orig_tls_offset;
2151 tls_align = orig_tls_align;
2152 lazy_head = orig_lazy_head;
2153 tail = orig_tail;
2154 tail->next = 0;
2155 p = 0;
2156 goto end;
2157 } else p = load_library(file, head);
2158
2159 if (!p) {
2160 error(noload ?
2161 "Library %s is not already loaded" :
2162 "Error loading shared library %s: %m",
2163 file);
2164 goto end;
2165 }
2166
2167 /* First load handling */
2168 load_deps(p);
2169 extend_bfs_deps(p);
2170 pthread_mutex_lock(&init_fini_lock);
2171 int constructed = p->constructed;
2172 pthread_mutex_unlock(&init_fini_lock);
2173 if (!constructed) ctor_queue = queue_ctors(p);
2174 if (!p->relocated && (mode & RTLD_LAZY)) {
2175 prepare_lazy(p);
2176 for (i=0; p->deps[i]; i++)
2177 if (!p->deps[i]->relocated)
2178 prepare_lazy(p->deps[i]);
2179 }
2180 if (!p->relocated || (mode & RTLD_GLOBAL)) {
2181 /* Make new symbols global, at least temporarily, so we can do
2182 * relocations. If not RTLD_GLOBAL, this is reverted below. */
2183 add_syms(p);
2184 for (i=0; p->deps[i]; i++)
2185 add_syms(p->deps[i]);
2186 }
2187 if (!p->relocated) {
2188 reloc_all(p);
2189 }
2190
2191 /* If RTLD_GLOBAL was not specified, undo any new additions
2192 * to the global symbol table. This is a nop if the library was
2193 * previously loaded and already global. */
2194 if (!(mode & RTLD_GLOBAL))
2195 revert_syms(orig_syms_tail);
2196
2197 /* Processing of deferred lazy relocations must not happen until
2198 * the new libraries are committed; otherwise we could end up with
2199 * relocations resolved to symbol definitions that get removed. */
2200 redo_lazy_relocs();
2201
2202 update_tls_size();
2203 if (tls_cnt != orig_tls_cnt)
2204 install_new_tls();
2205 orig_tail = tail;
2206 end:
2207 debug.state = RT_CONSISTENT;
2208 _dl_debug_state();
2209 __release_ptc();
2210 if (p) gencnt++;
2211 pthread_rwlock_unlock(&lock);
2212 if (ctor_queue) {
2213 do_init_fini(ctor_queue);
2214 free(ctor_queue);
2215 }
2216 pthread_setcancelstate(cs, 0);
2217 return p;
2218 }
2219
2220 hidden int __dl_invalid_handle(void *h)
2221 {
2222 struct dso *p;
2223 for (p=head; p; p=p->next) if (h==p) return 0;
2224 error("Invalid library handle %p", (void *)h);
2225 return 1;
2226 }
2227
2228 static void *addr2dso(size_t a)
2229 {
2230 struct dso *p;
2231 size_t i;
2232 if (DL_FDPIC) for (p=head; p; p=p->next) {
2233 i = count_syms(p);
2234 if (a-(size_t)p->funcdescs < i*sizeof(*p->funcdescs))
2235 return p;
2236 }
2237 for (p=head; p; p=p->next) {
2238 if (DL_FDPIC && p->loadmap) {
2239 for (i=0; i<p->loadmap->nsegs; i++) {
2240 if (a-p->loadmap->segs[i].p_vaddr
2241 < p->loadmap->segs[i].p_memsz)
2242 return p;
2243 }
2244 } else {
2245 Phdr *ph = p->phdr;
2246 size_t phcnt = p->phnum;
2247 size_t entsz = p->phentsize;
2248 size_t base = (size_t)p->base;
2249 for (; phcnt--; ph=(void *)((char *)ph+entsz)) {
2250 if (ph->p_type != PT_LOAD) continue;
2251 if (a-base-ph->p_vaddr < ph->p_memsz)
2252 return p;
2253 }
2254 if (a-(size_t)p->map < p->map_len)
2255 return 0;
2256 }
2257 }
2258 return 0;
2259 }
2260
2261 static void *do_dlsym(struct dso *p, const char *s, void *ra)
2262 {
2263 int use_deps = 0;
2264 if (p == head || p == RTLD_DEFAULT) {
2265 p = head;
2266 } else if (p == RTLD_NEXT) {
2267 p = addr2dso((size_t)ra);
2268 if (!p) p=head;
2269 p = p->next;
2270 } else if (__dl_invalid_handle(p)) {
2271 return 0;
2272 } else
2273 use_deps = 1;
2274 struct symdef def = find_sym2(p, s, 0, use_deps);
2275 if (!def.sym) {
2276 error("Symbol not found: %s", s);
2277 return 0;
2278 }
2279 if ((def.sym->st_info&0xf) == STT_TLS)
2280 return __tls_get_addr((tls_mod_off_t []){def.dso->tls_id, def.sym->st_value-DTP_OFFSET});
2281 if (DL_FDPIC && (def.sym->st_info&0xf) == STT_FUNC)
2282 return def.dso->funcdescs + (def.sym - def.dso->syms);
2283 return laddr(def.dso, def.sym->st_value);
2284 }
2285
2286 int dladdr(const void *addr_arg, Dl_info *info)
2287 {
2288 size_t addr = (size_t)addr_arg;
2289 struct dso *p;
2290 Sym *sym, *bestsym;
2291 uint32_t nsym;
2292 char *strings;
2293 size_t best = 0;
2294 size_t besterr = -1;
2295
2296 pthread_rwlock_rdlock(&lock);
2297 p = addr2dso(addr);
2298 pthread_rwlock_unlock(&lock);
2299
2300 if (!p) return 0;
2301
2302 sym = p->syms;
2303 strings = p->strings;
2304 nsym = count_syms(p);
2305
2306 if (DL_FDPIC) {
2307 size_t idx = (addr-(size_t)p->funcdescs)
2308 / sizeof(*p->funcdescs);
2309 if (idx < nsym && (sym[idx].st_info&0xf) == STT_FUNC) {
2310 best = (size_t)(p->funcdescs + idx);
2311 bestsym = sym + idx;
2312 besterr = 0;
2313 }
2314 }
2315
2316 if (!best) for (; nsym; nsym--, sym++) {
2317 if (sym->st_value
2318 && (1<<(sym->st_info&0xf) & OK_TYPES)
2319 && (1<<(sym->st_info>>4) & OK_BINDS)) {
2320 size_t symaddr = (size_t)laddr(p, sym->st_value);
2321 if (symaddr > addr || symaddr <= best)
2322 continue;
2323 best = symaddr;
2324 bestsym = sym;
2325 besterr = addr - symaddr;
2326 if (addr == symaddr)
2327 break;
2328 }
2329 }
2330
2331 if (best && besterr > bestsym->st_size-1) {
2332 best = 0;
2333 bestsym = 0;
2334 }
2335
2336 info->dli_fname = p->name;
2337 info->dli_fbase = p->map;
2338
2339 if (!best) {
2340 info->dli_sname = 0;
2341 info->dli_saddr = 0;
2342 return 1;
2343 }
2344
2345 if (DL_FDPIC && (bestsym->st_info&0xf) == STT_FUNC)
2346 best = (size_t)(p->funcdescs + (bestsym - p->syms));
2347 info->dli_sname = strings + bestsym->st_name;
2348 info->dli_saddr = (void *)best;
2349
2350 return 1;
2351 }
2352
2353 hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
2354 {
2355 void *res;
2356 pthread_rwlock_rdlock(&lock);
2357 res = do_dlsym(p, s, ra);
2358 pthread_rwlock_unlock(&lock);
2359 return res;
2360 }
2361
2362 hidden void *__dlsym_redir_time64(void *restrict p, const char *restrict s, void *restrict ra)
2363 {
2364 #if _REDIR_TIME64
2365 const char *suffix, *suffix2 = "";
2366 char redir[36];
2367
2368 /* Map the symbol name to a time64 version of itself according to the
2369 * pattern used for naming the redirected time64 symbols. */
2370 size_t l = strnlen(s, sizeof redir);
2371 if (l<4 || l==sizeof redir) goto no_redir;
2372 if (s[l-2]=='_' && s[l-1]=='r') {
2373 l -= 2;
2374 suffix2 = s+l;
2375 }
2376 if (l<4) goto no_redir;
2377 if (!strcmp(s+l-4, "time")) suffix = "64";
2378 else suffix = "_time64";
2379
2380 /* Use the presence of the remapped symbol name in libc to determine
2381 * whether it's one that requires time64 redirection; replace if so. */
2382 snprintf(redir, sizeof redir, "__%.*s%s%s", (int)l, s, suffix, suffix2);
2383 if (find_sym(&ldso, redir, 1).sym) s = redir;
2384 no_redir:
2385 #endif
2386 return __dlsym(p, s, ra);
2387 }
2388
2389 int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data)
2390 {
2391 struct dso *current;
2392 struct dl_phdr_info info;
2393 int ret = 0;
2394 for(current = head; current;) {
2395 info.dlpi_addr = (uintptr_t)current->base;
2396 info.dlpi_name = current->name;
2397 info.dlpi_phdr = current->phdr;
2398 info.dlpi_phnum = current->phnum;
2399 info.dlpi_adds = gencnt;
2400 info.dlpi_subs = 0;
2401 info.dlpi_tls_modid = current->tls_id;
2402 info.dlpi_tls_data = !current->tls_id ? 0 :
2403 __tls_get_addr((tls_mod_off_t[]){current->tls_id,0});
2404
2405 ret = (callback)(&info, sizeof (info), data);
2406
2407 if (ret != 0) break;
2408
2409 pthread_rwlock_rdlock(&lock);
2410 current = current->next;
2411 pthread_rwlock_unlock(&lock);
2412 }
2413 return ret;
2414 }
2415
2416 static void error(const char *fmt, ...)
2417 {
2418 va_list ap;
2419 va_start(ap, fmt);
2420 if (!runtime) {
2421 vdprintf(2, fmt, ap);
2422 dprintf(2, "\n");
2423 ldso_fail = 1;
2424 va_end(ap);
2425 return;
2426 }
2427 __dl_vseterr(fmt, ap);
2428 va_end(ap);
2429 }
2430