20 #include <semaphore.h>
21 #include <sys/membarrier.h>
22 #include "pthread_impl.h"
25 #include "malloc_impl.h"
27 static void error(const char *, ...);
29 #define MAXP2(a,b) (-(-(a)&-(b)))
30 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
32 #define container_of(p,t,m) ((t*)((char *)(p)-offsetof(t,m)))
33 #define countof(a) ((sizeof (a))/(sizeof (a)[0]))
45 struct td_index
*next
;
50 struct fdpic_loadmap
*loadmap
;
56 struct dso
*next
, *prev
;
66 struct dso
*syms_next
, *lazy_next
;
67 size_t *lazy
, lazy_cnt
;
78 struct dso
**deps
, *needed_by
;
82 char *rpath_orig
, *rpath
;
83 struct tls_module tls
;
85 size_t relro_start
, relro_end
;
87 unsigned char *new_tls
;
88 volatile int new_dtv_idx
, new_tls_idx
;
89 struct td_index
*td_index
;
90 struct dso
*fini_next
;
95 struct fdpic_loadmap
*loadmap
;
110 static struct builtin_tls
{
115 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
117 #define ADDEND_LIMIT 4096
118 static size_t *saved_addends
, *apply_addends_to
;
120 static struct dso ldso
;
121 static struct dso
*head
, *tail
, *fini_head
, *syms_tail
, *lazy_head
;
122 static char *env_path
, *sys_path
;
123 static unsigned long long gencnt
;
126 static int ldso_fail
;
128 static int shutting_down
;
129 static jmp_buf *rtld_fail
;
130 static pthread_rwlock_t lock
;
131 static struct debug debug
;
132 static struct tls_module
*tls_tail
;
133 static size_t tls_cnt
, tls_offset
, tls_align
= MIN_TLS_ALIGN
;
134 static size_t static_tls_cnt
;
135 static pthread_mutex_t init_fini_lock
;
136 static pthread_cond_t ctor_cond
;
137 static struct dso
*builtin_deps
[2];
138 static struct dso
*const no_deps
[1];
139 static struct dso
*builtin_ctor_queue
[4];
140 static struct dso
**main_ctor_queue
;
141 static struct fdpic_loadmap
*app_loadmap
;
142 static struct fdpic_dummy_loadmap app_dummy_loadmap
;
144 struct debug
*_dl_debug_addr
= &debug
;
146 extern hidden
int __malloc_replaced
;
148 hidden
void (*const __init_array_start
)(void)=0, (*const __fini_array_start
)(void)=0;
150 extern hidden
void (*const __init_array_end
)(void), (*const __fini_array_end
)(void);
152 weak_alias(__init_array_start
, __init_array_end
);
153 weak_alias(__fini_array_start
, __fini_array_end
);
155 static int dl_strcmp(const char *l
, const char *r
)
157 for (; *l
==*r
&& *l
; l
++, r
++);
158 return *(unsigned char *)l
- *(unsigned char *)r
;
160 #define strcmp(l,r) dl_strcmp(l,r)
162 /* Compute load address for a virtual address in a given dso. */
164 static void *laddr(const struct dso
*p
, size_t v
)
167 if (!p
->loadmap
) return p
->base
+ v
;
168 for (j
=0; v
-p
->loadmap
->segs
[j
].p_vaddr
>= p
->loadmap
->segs
[j
].p_memsz
; j
++);
169 return (void *)(v
- p
->loadmap
->segs
[j
].p_vaddr
+ p
->loadmap
->segs
[j
].addr
);
171 static void *laddr_pg(const struct dso
*p
, size_t v
)
174 size_t pgsz
= PAGE_SIZE
;
175 if (!p
->loadmap
) return p
->base
+ v
;
177 size_t a
= p
->loadmap
->segs
[j
].p_vaddr
;
178 size_t b
= a
+ p
->loadmap
->segs
[j
].p_memsz
;
184 return (void *)(v
- p
->loadmap
->segs
[j
].p_vaddr
+ p
->loadmap
->segs
[j
].addr
);
186 #define fpaddr(p, v) ((void (*)())&(struct funcdesc){ \
187 laddr(p, v), (p)->got })
189 #define laddr(p, v) (void *)((p)->base + (v))
190 #define laddr_pg(p, v) laddr(p, v)
191 #define fpaddr(p, v) ((void (*)())laddr(p, v))
194 static void decode_vec(size_t *v
, size_t *a
, size_t cnt
)
197 for (i
=0; i
<cnt
; i
++) a
[i
] = 0;
198 for (; v
[0]; v
+=2) if (v
[0]-1<cnt
-1) {
204 static int search_vec(size_t *v
, size_t *r
, size_t key
)
206 for (; v
[0]!=key
; v
+=2)
212 static uint32_t sysv_hash(const char *s0
)
214 const unsigned char *s
= (void *)s0
;
220 return h
& 0xfffffff;
223 static uint32_t gnu_hash(const char *s0
)
225 const unsigned char *s
= (void *)s0
;
226 uint_fast32_t h
= 5381;
232 static Sym
*sysv_lookup(const char *s
, uint32_t h
, struct dso
*dso
)
235 Sym
*syms
= dso
->syms
;
236 Elf_Symndx
*hashtab
= dso
->hashtab
;
237 char *strings
= dso
->strings
;
238 for (i
=hashtab
[2+h
%hashtab
[0]]; i
; i
=hashtab
[2+hashtab
[0]+i
]) {
239 if ((!dso
->versym
|| dso
->versym
[i
] >= 0)
240 && (!strcmp(s
, strings
+syms
[i
].st_name
)))
246 static Sym
*gnu_lookup(uint32_t h1
, uint32_t *hashtab
, struct dso
*dso
, const char *s
)
248 uint32_t nbuckets
= hashtab
[0];
249 uint32_t *buckets
= hashtab
+ 4 + hashtab
[2]*(sizeof(size_t)/4);
250 uint32_t i
= buckets
[h1
% nbuckets
];
254 uint32_t *hashval
= buckets
+ nbuckets
+ (i
- hashtab
[1]);
256 for (h1
|= 1; ; i
++) {
257 uint32_t h2
= *hashval
++;
258 if ((h1
== (h2
|1)) && (!dso
->versym
|| dso
->versym
[i
] >= 0)
259 && !strcmp(s
, dso
->strings
+ dso
->syms
[i
].st_name
))
267 static Sym
*gnu_lookup_filtered(uint32_t h1
, uint32_t *hashtab
, struct dso
*dso
, const char *s
, uint32_t fofs
, size_t fmask
)
269 const size_t *bloomwords
= (const void *)(hashtab
+4);
270 size_t f
= bloomwords
[fofs
& (hashtab
[2]-1)];
271 if (!(f
& fmask
)) return 0;
273 f
>>= (h1
>> hashtab
[3]) % (8 * sizeof f
);
274 if (!(f
& 1)) return 0;
276 return gnu_lookup(h1
, hashtab
, dso
, s
);
279 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
280 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
282 #ifndef ARCH_SYM_REJECT_UND
283 #define ARCH_SYM_REJECT_UND(s) 0
286 static struct symdef
find_sym(struct dso
*dso
, const char *s
, int need_def
)
288 uint32_t h
= 0, gh
= gnu_hash(s
), gho
= gh
/ (8*sizeof(size_t)), *ght
;
289 size_t ghm
= 1ul << gh
% (8*sizeof(size_t));
290 struct symdef def
= {0};
291 for (; dso
; dso
=dso
->syms_next
) {
293 if ((ght
= dso
->ghashtab
)) {
294 sym
= gnu_lookup_filtered(gh
, ght
, dso
, s
, gho
, ghm
);
296 if (!h
) h
= sysv_hash(s
);
297 sym
= sysv_lookup(s
, h
, dso
);
301 if (need_def
|| (sym
->st_info
&0xf) == STT_TLS
302 || ARCH_SYM_REJECT_UND(sym
))
305 if ((sym
->st_info
&0xf) != STT_TLS
)
307 if (!(1<<(sym
->st_info
&0xf) & OK_TYPES
)) continue;
308 if (!(1<<(sym
->st_info
>>4) & OK_BINDS
)) continue;
316 static void do_relocs(struct dso
*dso
, size_t *rel
, size_t rel_size
, size_t stride
)
318 unsigned char *base
= dso
->base
;
319 Sym
*syms
= dso
->syms
;
320 char *strings
= dso
->strings
;
331 int skip_relative
= 0, reuse_addends
= 0, save_slot
= 0;
334 /* Only ldso's REL table needs addend saving/reuse. */
335 if (rel
== apply_addends_to
)
340 for (; rel_size
; rel
+=stride
, rel_size
-=stride
*sizeof(size_t)) {
341 if (skip_relative
&& IS_RELATIVE(rel
[1], dso
->syms
)) continue;
342 type
= R_TYPE(rel
[1]);
343 if (type
== REL_NONE
) continue;
344 reloc_addr
= laddr(dso
, rel
[0]);
348 } else if (type
==REL_GOT
|| type
==REL_PLT
|| type
==REL_COPY
) {
350 } else if (reuse_addends
) {
351 /* Save original addend in stage 2 where the dso
352 * chain consists of just ldso; otherwise read back
353 * saved addend since the inline one was clobbered. */
355 saved_addends
[save_slot
] = *reloc_addr
;
356 addend
= saved_addends
[save_slot
++];
358 addend
= *reloc_addr
;
361 sym_index
= R_SYM(rel
[1]);
363 sym
= syms
+ sym_index
;
364 name
= strings
+ sym
->st_name
;
365 ctx
= type
==REL_COPY
? head
->syms_next
: head
;
366 def
= (sym
->st_info
&0xf) == STT_SECTION
367 ? (struct symdef
){ .dso
= dso
, .sym
= sym
}
368 : find_sym(ctx
, name
, type
==REL_PLT
);
369 if (!def
.sym
&& (sym
->st_shndx
!= SHN_UNDEF
370 || sym
->st_info
>>4 != STB_WEAK
)) {
371 if (dso
->lazy
&& (type
==REL_PLT
|| type
==REL_GOT
)) {
372 dso
->lazy
[3*dso
->lazy_cnt
+0] = rel
[0];
373 dso
->lazy
[3*dso
->lazy_cnt
+1] = rel
[1];
374 dso
->lazy
[3*dso
->lazy_cnt
+2] = addend
;
378 error("Error relocating %s: %s: symbol not found",
380 if (runtime
) longjmp(*rtld_fail
, 1);
389 sym_val
= def
.sym
? (size_t)laddr(def
.dso
, def
.sym
->st_value
) : 0;
390 tls_val
= def
.sym
? def
.sym
->st_value
: 0;
392 if ((type
== REL_TPOFF
|| type
== REL_TPOFF_NEG
)
393 && runtime
&& def
.dso
->tls_id
> static_tls_cnt
) {
394 error("Error relocating %s: %s: initial-exec TLS "
395 "resolves to dynamic definition in %s",
396 dso
->name
, name
, def
.dso
->name
);
397 longjmp(*rtld_fail
, 1);
404 addend
-= (size_t)reloc_addr
;
408 *reloc_addr
= sym_val
+ addend
;
411 *reloc_addr
= (size_t)base
+ addend
;
414 if (sym
) *reloc_addr
= sym_val
+ addend
;
415 else *reloc_addr
= (size_t)base
+ addend
;
418 memcpy(reloc_addr
, (void *)sym_val
, sym
->st_size
);
421 *(uint32_t *)reloc_addr
= sym_val
+ addend
422 - (size_t)reloc_addr
;
425 *reloc_addr
= def
.sym
? (size_t)(def
.dso
->funcdescs
426 + (def
.sym
- def
.dso
->syms
)) : 0;
428 case REL_FUNCDESC_VAL
:
429 if ((sym
->st_info
&0xf) == STT_SECTION
) *reloc_addr
+= sym_val
;
430 else *reloc_addr
= sym_val
;
431 reloc_addr
[1] = def
.sym
? (size_t)def
.dso
->got
: 0;
434 *reloc_addr
= def
.dso
->tls_id
;
437 *reloc_addr
= tls_val
+ addend
- DTP_OFFSET
;
441 *reloc_addr
= tls_val
+ def
.dso
->tls
.offset
+ TPOFF_K
+ addend
;
445 *reloc_addr
= tls_val
- def
.dso
->tls
.offset
+ addend
;
448 *reloc_addr
= def
.dso
->tls
.offset
- tls_val
+ addend
;
452 if (stride
<3) addend
= reloc_addr
[1];
453 if (runtime
&& def
.dso
->tls_id
> static_tls_cnt
) {
454 struct td_index
*new = malloc(sizeof *new);
457 "Error relocating %s: cannot allocate TLSDESC for %s",
458 dso
->name
, sym
? name
: "(local)" );
459 longjmp(*rtld_fail
, 1);
461 new->next
= dso
->td_index
;
463 new->args
[0] = def
.dso
->tls_id
;
464 new->args
[1] = tls_val
+ addend
- DTP_OFFSET
;
465 reloc_addr
[0] = (size_t)__tlsdesc_dynamic
;
466 reloc_addr
[1] = (size_t)new;
468 reloc_addr
[0] = (size_t)__tlsdesc_static
;
470 reloc_addr
[1] = tls_val
+ def
.dso
->tls
.offset
473 reloc_addr
[1] = tls_val
- def
.dso
->tls
.offset
477 #ifdef TLSDESC_BACKWARDS
478 /* Some archs (32-bit ARM at least) invert the order of
479 * the descriptor members. Fix them up here. */
480 size_t tmp
= reloc_addr
[0];
481 reloc_addr
[0] = reloc_addr
[1];
486 error("Error relocating %s: unsupported relocation type %d",
488 if (runtime
) longjmp(*rtld_fail
, 1);
494 static void redo_lazy_relocs()
496 struct dso
*p
= lazy_head
, *next
;
500 size_t size
= p
->lazy_cnt
*3*sizeof(size_t);
502 do_relocs(p
, p
->lazy
, size
, 3);
504 p
->lazy_next
= lazy_head
;
514 /* A huge hack: to make up for the wastefulness of shared libraries
515 * needing at least a page of dirty memory even if they have no global
516 * data, we reclaim the gaps at the beginning and end of writable maps
517 * and "donate" them to the heap. */
519 static void reclaim(struct dso
*dso
, size_t start
, size_t end
)
521 if (start
>= dso
->relro_start
&& start
< dso
->relro_end
) start
= dso
->relro_end
;
522 if (end
>= dso
->relro_start
&& end
< dso
->relro_end
) end
= dso
->relro_start
;
523 if (start
>= end
) return;
524 char *base
= laddr_pg(dso
, start
);
525 __malloc_donate(base
, base
+(end
-start
));
528 static void reclaim_gaps(struct dso
*dso
)
530 Phdr
*ph
= dso
->phdr
;
531 size_t phcnt
= dso
->phnum
;
533 for (; phcnt
--; ph
=(void *)((char *)ph
+dso
->phentsize
)) {
534 if (ph
->p_type
!=PT_LOAD
) continue;
535 if ((ph
->p_flags
&(PF_R
|PF_W
))!=(PF_R
|PF_W
)) continue;
536 reclaim(dso
, ph
->p_vaddr
& -PAGE_SIZE
, ph
->p_vaddr
);
537 reclaim(dso
, ph
->p_vaddr
+ph
->p_memsz
,
538 ph
->p_vaddr
+ph
->p_memsz
+PAGE_SIZE
-1 & -PAGE_SIZE
);
542 static void *mmap_fixed(void *p
, size_t n
, int prot
, int flags
, int fd
, off_t off
)
544 static int no_map_fixed
;
547 q
= mmap(p
, n
, prot
, flags
|MAP_FIXED
, fd
, off
);
548 if (!DL_NOMMU_SUPPORT
|| q
!= MAP_FAILED
|| errno
!= EINVAL
)
552 /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
553 if (flags
& MAP_ANONYMOUS
) {
558 if (lseek(fd
, off
, SEEK_SET
) < 0) return MAP_FAILED
;
559 for (q
=p
; n
; q
+=r
, off
+=r
, n
-=r
) {
561 if (r
< 0 && errno
!= EINTR
) return MAP_FAILED
;
570 static void unmap_library(struct dso
*dso
)
574 for (i
=0; i
<dso
->loadmap
->nsegs
; i
++) {
575 if (!dso
->loadmap
->segs
[i
].p_memsz
)
577 munmap((void *)dso
->loadmap
->segs
[i
].addr
,
578 dso
->loadmap
->segs
[i
].p_memsz
);
581 } else if (dso
->map
&& dso
->map_len
) {
582 munmap(dso
->map
, dso
->map_len
);
586 static void *map_library(int fd
, struct dso
*dso
)
588 Ehdr buf
[(896+sizeof(Ehdr
))/sizeof(Ehdr
)];
589 void *allocated_buf
=0;
591 size_t addr_min
=SIZE_MAX
, addr_max
=0, map_len
;
592 size_t this_min
, this_max
;
598 unsigned char *map
=MAP_FAILED
, *base
;
603 ssize_t l
= read(fd
, buf
, sizeof buf
);
606 if (l
<sizeof *eh
|| (eh
->e_type
!= ET_DYN
&& eh
->e_type
!= ET_EXEC
))
608 phsize
= eh
->e_phentsize
* eh
->e_phnum
;
609 if (phsize
> sizeof buf
- sizeof *eh
) {
610 allocated_buf
= malloc(phsize
);
611 if (!allocated_buf
) return 0;
612 l
= pread(fd
, allocated_buf
, phsize
, eh
->e_phoff
);
613 if (l
< 0) goto error
;
614 if (l
!= phsize
) goto noexec
;
615 ph
= ph0
= allocated_buf
;
616 } else if (eh
->e_phoff
+ phsize
> l
) {
617 l
= pread(fd
, buf
+1, phsize
, eh
->e_phoff
);
618 if (l
< 0) goto error
;
619 if (l
!= phsize
) goto noexec
;
620 ph
= ph0
= (void *)(buf
+ 1);
622 ph
= ph0
= (void *)((char *)buf
+ eh
->e_phoff
);
624 for (i
=eh
->e_phnum
; i
; i
--, ph
=(void *)((char *)ph
+eh
->e_phentsize
)) {
625 if (ph
->p_type
== PT_DYNAMIC
) {
627 } else if (ph
->p_type
== PT_TLS
) {
628 tls_image
= ph
->p_vaddr
;
629 dso
->tls
.align
= ph
->p_align
;
630 dso
->tls
.len
= ph
->p_filesz
;
631 dso
->tls
.size
= ph
->p_memsz
;
632 } else if (ph
->p_type
== PT_GNU_RELRO
) {
633 dso
->relro_start
= ph
->p_vaddr
& -PAGE_SIZE
;
634 dso
->relro_end
= (ph
->p_vaddr
+ ph
->p_memsz
) & -PAGE_SIZE
;
635 } else if (ph
->p_type
== PT_GNU_STACK
) {
636 if (!runtime
&& ph
->p_memsz
> __default_stacksize
) {
637 __default_stacksize
=
638 ph
->p_memsz
< DEFAULT_STACK_MAX
?
639 ph
->p_memsz
: DEFAULT_STACK_MAX
;
642 if (ph
->p_type
!= PT_LOAD
) continue;
644 if (ph
->p_vaddr
< addr_min
) {
645 addr_min
= ph
->p_vaddr
;
646 off_start
= ph
->p_offset
;
647 prot
= (((ph
->p_flags
&PF_R
) ? PROT_READ
: 0) |
648 ((ph
->p_flags
&PF_W
) ? PROT_WRITE
: 0) |
649 ((ph
->p_flags
&PF_X
) ? PROT_EXEC
: 0));
651 if (ph
->p_vaddr
+ph
->p_memsz
> addr_max
) {
652 addr_max
= ph
->p_vaddr
+ph
->p_memsz
;
655 if (!dyn
) goto noexec
;
656 if (DL_FDPIC
&& !(eh
->e_flags
& FDPIC_CONSTDISP_FLAG
)) {
657 dso
->loadmap
= calloc(1, sizeof *dso
->loadmap
658 + nsegs
* sizeof *dso
->loadmap
->segs
);
659 if (!dso
->loadmap
) goto error
;
660 dso
->loadmap
->nsegs
= nsegs
;
661 for (ph
=ph0
, i
=0; i
<nsegs
; ph
=(void *)((char *)ph
+eh
->e_phentsize
)) {
662 if (ph
->p_type
!= PT_LOAD
) continue;
663 prot
= (((ph
->p_flags
&PF_R
) ? PROT_READ
: 0) |
664 ((ph
->p_flags
&PF_W
) ? PROT_WRITE
: 0) |
665 ((ph
->p_flags
&PF_X
) ? PROT_EXEC
: 0));
666 map
= mmap(0, ph
->p_memsz
+ (ph
->p_vaddr
& PAGE_SIZE
-1),
668 fd
, ph
->p_offset
& -PAGE_SIZE
);
669 if (map
== MAP_FAILED
) {
673 dso
->loadmap
->segs
[i
].addr
= (size_t)map
+
674 (ph
->p_vaddr
& PAGE_SIZE
-1);
675 dso
->loadmap
->segs
[i
].p_vaddr
= ph
->p_vaddr
;
676 dso
->loadmap
->segs
[i
].p_memsz
= ph
->p_memsz
;
678 if (prot
& PROT_WRITE
) {
679 size_t brk
= (ph
->p_vaddr
& PAGE_SIZE
-1)
681 size_t pgbrk
= brk
+ PAGE_SIZE
-1 & -PAGE_SIZE
;
682 size_t pgend
= brk
+ ph
->p_memsz
- ph
->p_filesz
683 + PAGE_SIZE
-1 & -PAGE_SIZE
;
684 if (pgend
> pgbrk
&& mmap_fixed(map
+pgbrk
,
686 MAP_PRIVATE
|MAP_FIXED
|MAP_ANONYMOUS
,
687 -1, off_start
) == MAP_FAILED
)
689 memset(map
+ brk
, 0, pgbrk
-brk
);
692 map
= (void *)dso
->loadmap
->segs
[0].addr
;
696 addr_max
+= PAGE_SIZE
-1;
697 addr_max
&= -PAGE_SIZE
;
698 addr_min
&= -PAGE_SIZE
;
699 off_start
&= -PAGE_SIZE
;
700 map_len
= addr_max
- addr_min
+ off_start
;
701 /* The first time, we map too much, possibly even more than
702 * the length of the file. This is okay because we will not
703 * use the invalid part; we just need to reserve the right
704 * amount of virtual address space to map over later. */
705 map
= DL_NOMMU_SUPPORT
706 ? mmap((void *)addr_min
, map_len
, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
707 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0)
708 : mmap((void *)addr_min
, map_len
, prot
,
709 MAP_PRIVATE
, fd
, off_start
);
710 if (map
==MAP_FAILED
) goto error
;
712 dso
->map_len
= map_len
;
713 /* If the loaded file is not relocatable and the requested address is
714 * not available, then the load operation must fail. */
715 if (eh
->e_type
!= ET_DYN
&& addr_min
&& map
!=(void *)addr_min
) {
719 base
= map
- addr_min
;
722 for (ph
=ph0
, i
=eh
->e_phnum
; i
; i
--, ph
=(void *)((char *)ph
+eh
->e_phentsize
)) {
723 if (ph
->p_type
!= PT_LOAD
) continue;
724 /* Check if the programs headers are in this load segment, and
725 * if so, record the address for use by dl_iterate_phdr. */
726 if (!dso
->phdr
&& eh
->e_phoff
>= ph
->p_offset
727 && eh
->e_phoff
+phsize
<= ph
->p_offset
+ph
->p_filesz
) {
728 dso
->phdr
= (void *)(base
+ ph
->p_vaddr
729 + (eh
->e_phoff
-ph
->p_offset
));
730 dso
->phnum
= eh
->e_phnum
;
731 dso
->phentsize
= eh
->e_phentsize
;
733 this_min
= ph
->p_vaddr
& -PAGE_SIZE
;
734 this_max
= ph
->p_vaddr
+ph
->p_memsz
+PAGE_SIZE
-1 & -PAGE_SIZE
;
735 off_start
= ph
->p_offset
& -PAGE_SIZE
;
736 prot
= (((ph
->p_flags
&PF_R
) ? PROT_READ
: 0) |
737 ((ph
->p_flags
&PF_W
) ? PROT_WRITE
: 0) |
738 ((ph
->p_flags
&PF_X
) ? PROT_EXEC
: 0));
739 /* Reuse the existing mapping for the lowest-address LOAD */
740 if ((ph
->p_vaddr
& -PAGE_SIZE
) != addr_min
|| DL_NOMMU_SUPPORT
)
741 if (mmap_fixed(base
+this_min
, this_max
-this_min
, prot
, MAP_PRIVATE
|MAP_FIXED
, fd
, off_start
) == MAP_FAILED
)
743 if (ph
->p_memsz
> ph
->p_filesz
&& (ph
->p_flags
&PF_W
)) {
744 size_t brk
= (size_t)base
+ph
->p_vaddr
+ph
->p_filesz
;
745 size_t pgbrk
= brk
+PAGE_SIZE
-1 & -PAGE_SIZE
;
746 memset((void *)brk
, 0, pgbrk
-brk
& PAGE_SIZE
-1);
747 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
)
751 for (i
=0; ((size_t *)(base
+dyn
))[i
]; i
+=2)
752 if (((size_t *)(base
+dyn
))[i
]==DT_TEXTREL
) {
753 if (mprotect(map
, map_len
, PROT_READ
|PROT_WRITE
|PROT_EXEC
)
760 dso
->dynv
= laddr(dso
, dyn
);
761 if (dso
->tls
.size
) dso
->tls
.image
= laddr(dso
, tls_image
);
767 if (map
!=MAP_FAILED
) unmap_library(dso
);
772 static int path_open(const char *name
, const char *s
, char *buf
, size_t buf_size
)
777 s
+= strspn(s
, ":\n");
778 l
= strcspn(s
, ":\n");
779 if (l
-1 >= INT_MAX
) return -1;
780 if (snprintf(buf
, buf_size
, "%.*s/%s", (int)l
, s
, name
) < buf_size
) {
781 if ((fd
= open(buf
, O_RDONLY
|O_CLOEXEC
))>=0) return fd
;
789 /* Any negative value but -1 will inhibit
790 * futher path search. */
798 static int fixup_rpath(struct dso
*p
, char *buf
, size_t buf_size
)
801 const char *s
, *t
, *origin
;
803 if (p
->rpath
|| !p
->rpath_orig
) return 0;
804 if (!strchr(p
->rpath_orig
, '$')) {
805 p
->rpath
= p
->rpath_orig
;
810 while ((t
=strchr(s
, '$'))) {
811 if (strncmp(t
, "$ORIGIN", 7) && strncmp(t
, "${ORIGIN}", 9))
816 if (n
> SSIZE_MAX
/PATH_MAX
) return 0;
818 if (p
->kernel_mapped
) {
819 /* $ORIGIN searches cannot be performed for the main program
820 * when it is suid/sgid/AT_SECURE. This is because the
821 * pathname is under the control of the caller of execve.
822 * For libraries, however, $ORIGIN can be processed safely
823 * since the library's pathname came from a trusted source
824 * (either system paths or a call to dlopen). */
827 l
= readlink("/proc/self/exe", buf
, buf_size
);
828 if (l
== -1) switch (errno
) {
843 t
= strrchr(origin
, '/');
847 /* Normally p->name will always be an absolute or relative
848 * pathname containing at least one '/' character, but in the
849 * case where ldso was invoked as a command to execute a
850 * program in the working directory, app.name may not. Fix. */
854 /* Disallow non-absolute origins for suid/sgid/AT_SECURE. */
855 if (libc
.secure
&& *origin
!= '/')
857 p
->rpath
= malloc(strlen(p
->rpath_orig
) + n
*l
+ 1);
858 if (!p
->rpath
) return -1;
862 while ((t
=strchr(s
, '$'))) {
865 memcpy(d
, origin
, l
);
867 /* It was determined previously that the '$' is followed
868 * either by "ORIGIN" or "{ORIGIN}". */
869 s
= t
+ 7 + 2*(t
[1]=='{');
875 static void decode_dyn(struct dso
*p
)
878 decode_vec(p
->dynv
, dyn
, DYN_CNT
);
879 p
->syms
= laddr(p
, dyn
[DT_SYMTAB
]);
880 p
->strings
= laddr(p
, dyn
[DT_STRTAB
]);
881 if (dyn
[0]&(1<<DT_HASH
))
882 p
->hashtab
= laddr(p
, dyn
[DT_HASH
]);
883 if (dyn
[0]&(1<<DT_RPATH
))
884 p
->rpath_orig
= p
->strings
+ dyn
[DT_RPATH
];
885 if (dyn
[0]&(1<<DT_RUNPATH
))
886 p
->rpath_orig
= p
->strings
+ dyn
[DT_RUNPATH
];
887 if (dyn
[0]&(1<<DT_PLTGOT
))
888 p
->got
= laddr(p
, dyn
[DT_PLTGOT
]);
889 if (search_vec(p
->dynv
, dyn
, DT_GNU_HASH
))
890 p
->ghashtab
= laddr(p
, *dyn
);
891 if (search_vec(p
->dynv
, dyn
, DT_VERSYM
))
892 p
->versym
= laddr(p
, *dyn
);
895 static size_t count_syms(struct dso
*p
)
897 if (p
->hashtab
) return p
->hashtab
[1];
900 uint32_t *buckets
= p
->ghashtab
+ 4 + (p
->ghashtab
[2]*sizeof(size_t)/4);
902 for (i
= nsym
= 0; i
< p
->ghashtab
[0]; i
++) {
903 if (buckets
[i
] > nsym
)
907 hashval
= buckets
+ p
->ghashtab
[0] + (nsym
- p
->ghashtab
[1]);
909 while (!(*hashval
++ & 1));
914 static void *dl_mmap(size_t n
)
917 int prot
= PROT_READ
|PROT_WRITE
, flags
= MAP_ANONYMOUS
|MAP_PRIVATE
;
919 p
= (void *)__syscall(SYS_mmap2
, 0, n
, prot
, flags
, -1, 0);
921 p
= (void *)__syscall(SYS_mmap
, 0, n
, prot
, flags
, -1, 0);
923 return p
== MAP_FAILED
? 0 : p
;
926 static void makefuncdescs(struct dso
*p
)
928 static int self_done
;
929 size_t nsym
= count_syms(p
);
930 size_t i
, size
= nsym
* sizeof(*p
->funcdescs
);
933 p
->funcdescs
= dl_mmap(size
);
936 p
->funcdescs
= malloc(size
);
939 if (!runtime
) a_crash();
940 error("Error allocating function descriptors for %s", p
->name
);
941 longjmp(*rtld_fail
, 1);
943 for (i
=0; i
<nsym
; i
++) {
944 if ((p
->syms
[i
].st_info
&0xf)==STT_FUNC
&& p
->syms
[i
].st_shndx
) {
945 p
->funcdescs
[i
].addr
= laddr(p
, p
->syms
[i
].st_value
);
946 p
->funcdescs
[i
].got
= p
->got
;
948 p
->funcdescs
[i
].addr
= 0;
949 p
->funcdescs
[i
].got
= 0;
954 static struct dso
*load_library(const char *name
, struct dso
*needed_by
)
956 char buf
[2*NAME_MAX
+2];
957 const char *pathname
;
959 struct dso
*p
, temp_dso
= {0};
971 /* Catch and block attempts to reload the implementation itself */
972 if (name
[0]=='l' && name
[1]=='i' && name
[2]=='b') {
973 static const char reserved
[] =
974 "c.pthread.rt.m.dl.util.xnet.";
975 const char *rp
, *next
;
976 for (rp
=reserved
; *rp
; rp
=next
) {
977 next
= strchr(rp
, '.') + 1;
978 if (strncmp(name
+3, rp
, next
-rp
) == 0)
983 /* Track which names have been resolved
984 * and only report each one once. */
985 static unsigned reported
;
986 unsigned mask
= 1U<<(rp
-reserved
);
987 if (!(reported
& mask
)) {
989 dprintf(1, "\t%s => %s (%p)\n",
997 if (!strcmp(name
, ldso
.name
)) is_self
= 1;
1006 if (strchr(name
, '/')) {
1008 fd
= open(name
, O_RDONLY
|O_CLOEXEC
);
1010 /* Search for the name to see if it's already loaded */
1011 for (p
=head
->next
; p
; p
=p
->next
) {
1012 if (p
->shortname
&& !strcmp(p
->shortname
, name
)) {
1016 if (strlen(name
) > NAME_MAX
) return 0;
1018 if (env_path
) fd
= path_open(name
, env_path
, buf
, sizeof buf
);
1019 for (p
=needed_by
; fd
== -1 && p
; p
=p
->needed_by
) {
1020 if (fixup_rpath(p
, buf
, sizeof buf
) < 0)
1021 fd
= -2; /* Inhibit further search. */
1023 fd
= path_open(name
, p
->rpath
, buf
, sizeof buf
);
1029 if (ldso
.name
[0]=='/') {
1031 for (s
=t
=z
=ldso
.name
; *s
; s
++)
1032 if (*s
=='/') z
=t
, t
=s
;
1033 prefix_len
= z
-ldso
.name
;
1034 if (prefix_len
< PATH_MAX
)
1041 char etc_ldso_path
[prefix_len
+ 1
1042 + sizeof "/etc/ld-musl-" LDSO_ARCH
".path"];
1043 snprintf(etc_ldso_path
, sizeof etc_ldso_path
,
1044 "%.*s/etc/ld-musl-" LDSO_ARCH
".path",
1045 (int)prefix_len
, prefix
);
1046 FILE *f
= fopen(etc_ldso_path
, "rbe");
1048 if (getdelim(&sys_path
, (size_t[1]){0}, 0, f
) <= 0) {
1053 } else if (errno
!= ENOENT
) {
1057 if (!sys_path
) sys_path
= "/lib:/usr/local/lib:/usr/lib";
1058 fd
= path_open(name
, sys_path
, buf
, sizeof buf
);
1062 if (fd
< 0) return 0;
1063 if (fstat(fd
, &st
) < 0) {
1067 for (p
=head
->next
; p
; p
=p
->next
) {
1068 if (p
->dev
== st
.st_dev
&& p
->ino
== st
.st_ino
) {
1069 /* If this library was previously loaded with a
1070 * pathname but a search found the same inode,
1071 * setup its shortname so it can be found by name. */
1072 if (!p
->shortname
&& pathname
!= name
)
1073 p
->shortname
= strrchr(p
->name
, '/')+1;
1078 map
= noload
? 0 : map_library(fd
, &temp_dso
);
1082 /* Avoid the danger of getting two versions of libc mapped into the
1083 * same process when an absolute pathname was used. The symbols
1084 * checked are chosen to catch both musl and glibc, and to avoid
1085 * false positives from interposition-hack libraries. */
1086 decode_dyn(&temp_dso
);
1087 if (find_sym(&temp_dso
, "__libc_start_main", 1).sym
&&
1088 find_sym(&temp_dso
, "stdin", 1).sym
) {
1089 unmap_library(&temp_dso
);
1090 return load_library("libc.so", needed_by
);
1092 /* Past this point, if we haven't reached runtime yet, ldso has
1093 * committed either to use the mapped library or to abort execution.
1094 * Unmapping is not possible, so we can safely reclaim gaps. */
1095 if (!runtime
) reclaim_gaps(&temp_dso
);
1097 /* Allocate storage for the new DSO. When there is TLS, this
1098 * storage must include a reservation for all pre-existing
1099 * threads to obtain copies of both the new TLS, and an
1100 * extended DTV capable of storing an additional slot for
1101 * the newly-loaded DSO. */
1102 alloc_size
= sizeof *p
+ strlen(pathname
) + 1;
1103 if (runtime
&& temp_dso
.tls
.image
) {
1104 size_t per_th
= temp_dso
.tls
.size
+ temp_dso
.tls
.align
1105 + sizeof(void *) * (tls_cnt
+3);
1106 n_th
= libc
.threads_minus_1
+ 1;
1107 if (n_th
> SSIZE_MAX
/ per_th
) alloc_size
= SIZE_MAX
;
1108 else alloc_size
+= n_th
* per_th
;
1110 p
= calloc(1, alloc_size
);
1112 unmap_library(&temp_dso
);
1115 memcpy(p
, &temp_dso
, sizeof temp_dso
);
1118 p
->needed_by
= needed_by
;
1120 p
->runtime_loaded
= runtime
;
1121 strcpy(p
->name
, pathname
);
1122 /* Add a shortname only if name arg was not an explicit pathname. */
1123 if (pathname
!= name
) p
->shortname
= strrchr(p
->name
, '/')+1;
1125 p
->tls_id
= ++tls_cnt
;
1126 tls_align
= MAXP2(tls_align
, p
->tls
.align
);
1128 p
->tls
.offset
= tls_offset
+ ( (tls_align
-1) &
1129 -(tls_offset
+ (uintptr_t)p
->tls
.image
) );
1130 tls_offset
+= p
->tls
.size
;
1132 tls_offset
+= p
->tls
.size
+ p
->tls
.align
- 1;
1133 tls_offset
-= (tls_offset
+ (uintptr_t)p
->tls
.image
)
1135 p
->tls
.offset
= tls_offset
;
1137 p
->new_dtv
= (void *)(-sizeof(size_t) &
1138 (uintptr_t)(p
->name
+strlen(p
->name
)+sizeof(size_t)));
1139 p
->new_tls
= (void *)(p
->new_dtv
+ n_th
*(tls_cnt
+1));
1140 if (tls_tail
) tls_tail
->next
= &p
->tls
;
1141 else libc
.tls_head
= &p
->tls
;
1149 if (DL_FDPIC
) makefuncdescs(p
);
1151 if (ldd_mode
) dprintf(1, "\t%s => %s (%p)\n", name
, pathname
, p
->base
);
1156 static void load_direct_deps(struct dso
*p
)
1160 if (p
->deps
) return;
1161 /* For head, all preloads are direct pseudo-dependencies.
1162 * Count and include them now to avoid realloc later. */
1163 if (p
==head
) for (struct dso
*q
=p
->next
; q
; q
=q
->next
)
1165 for (i
=0; p
->dynv
[i
]; i
+=2)
1166 if (p
->dynv
[i
] == DT_NEEDED
) cnt
++;
1167 /* Use builtin buffer for apps with no external deps, to
1168 * preserve property of no runtime failure paths. */
1169 p
->deps
= (p
==head
&& cnt
<2) ? builtin_deps
:
1170 calloc(cnt
+1, sizeof *p
->deps
);
1172 error("Error loading dependencies for %s", p
->name
);
1173 if (runtime
) longjmp(*rtld_fail
, 1);
1176 if (p
==head
) for (struct dso
*q
=p
->next
; q
; q
=q
->next
)
1178 for (i
=0; p
->dynv
[i
]; i
+=2) {
1179 if (p
->dynv
[i
] != DT_NEEDED
) continue;
1180 struct dso
*dep
= load_library(p
->strings
+ p
->dynv
[i
+1], p
);
1182 error("Error loading shared library %s: %m (needed by %s)",
1183 p
->strings
+ p
->dynv
[i
+1], p
->name
);
1184 if (runtime
) longjmp(*rtld_fail
, 1);
1187 p
->deps
[cnt
++] = dep
;
1190 p
->ndeps_direct
= cnt
;
1193 static void load_deps(struct dso
*p
)
1195 if (p
->deps
) return;
1196 for (; p
; p
=p
->next
)
1197 load_direct_deps(p
);
1200 static void extend_bfs_deps(struct dso
*p
)
1202 size_t i
, j
, cnt
, ndeps_all
;
1205 /* Can't use realloc if the original p->deps was allocated at
1206 * program entry and malloc has been replaced, or if it's
1207 * the builtin non-allocated trivial main program deps array. */
1208 int no_realloc
= (__malloc_replaced
&& !p
->runtime_loaded
)
1209 || p
->deps
== builtin_deps
;
1211 if (p
->bfs_built
) return;
1212 ndeps_all
= p
->ndeps_direct
;
1214 /* Mark existing (direct) deps so they won't be duplicated. */
1215 for (i
=0; p
->deps
[i
]; i
++)
1216 p
->deps
[i
]->mark
= 1;
1218 /* For each dependency already in the list, copy its list of direct
1219 * dependencies to the list, excluding any items already in the
1220 * list. Note that the list this loop iterates over will grow during
1221 * the loop, but since duplicates are excluded, growth is bounded. */
1222 for (i
=0; p
->deps
[i
]; i
++) {
1223 struct dso
*dep
= p
->deps
[i
];
1224 for (j
=cnt
=0; j
<dep
->ndeps_direct
; j
++)
1225 if (!dep
->deps
[j
]->mark
) cnt
++;
1227 malloc(sizeof(*tmp
) * (ndeps_all
+cnt
+1)) :
1228 realloc(p
->deps
, sizeof(*tmp
) * (ndeps_all
+cnt
+1));
1230 error("Error recording dependencies for %s", p
->name
);
1231 if (runtime
) longjmp(*rtld_fail
, 1);
1235 memcpy(tmp
, p
->deps
, sizeof(*tmp
) * (ndeps_all
+1));
1239 for (j
=0; j
<dep
->ndeps_direct
; j
++) {
1240 if (dep
->deps
[j
]->mark
) continue;
1241 dep
->deps
[j
]->mark
= 1;
1242 p
->deps
[ndeps_all
++] = dep
->deps
[j
];
1244 p
->deps
[ndeps_all
] = 0;
1247 for (p
=head
; p
; p
=p
->next
)
1251 static void load_preload(char *s
)
1255 for (z
=s
; *z
; s
=z
) {
1256 for ( ; *s
&& (isspace(*s
) || *s
==':'); s
++);
1257 for (z
=s
; *z
&& !isspace(*z
) && *z
!=':'; z
++);
1265 static void add_syms(struct dso
*p
)
1267 if (!p
->syms_next
&& syms_tail
!= p
) {
1268 syms_tail
->syms_next
= p
;
1273 static void revert_syms(struct dso
*old_tail
)
1275 struct dso
*p
, *next
;
1276 /* Chop off the tail of the list of dsos that participate in
1277 * the global symbol table, reverting them to RTLD_LOCAL. */
1278 for (p
=old_tail
; p
; p
=next
) {
1279 next
= p
->syms_next
;
1282 syms_tail
= old_tail
;
1285 static void do_mips_relocs(struct dso
*p
, size_t *got
)
1287 size_t i
, j
, rel
[2];
1288 unsigned char *base
= p
->base
;
1289 i
=0; search_vec(p
->dynv
, &i
, DT_MIPS_LOCAL_GOTNO
);
1293 while (i
--) *got
++ += (size_t)base
;
1295 j
=0; search_vec(p
->dynv
, &j
, DT_MIPS_GOTSYM
);
1296 i
=0; search_vec(p
->dynv
, &i
, DT_MIPS_SYMTABNO
);
1297 Sym
*sym
= p
->syms
+ j
;
1298 rel
[0] = (unsigned char *)got
- base
;
1299 for (i
-=j
; i
; i
--, sym
++, rel
[0]+=sizeof(size_t)) {
1300 rel
[1] = R_INFO(sym
-p
->syms
, R_MIPS_JUMP_SLOT
);
1301 do_relocs(p
, rel
, sizeof rel
, 2);
1305 static void reloc_all(struct dso
*p
)
1307 size_t dyn
[DYN_CNT
];
1308 for (; p
; p
=p
->next
) {
1309 if (p
->relocated
) continue;
1310 decode_vec(p
->dynv
, dyn
, DYN_CNT
);
1311 if (NEED_MIPS_GOT_RELOCS
)
1312 do_mips_relocs(p
, laddr(p
, dyn
[DT_PLTGOT
]));
1313 do_relocs(p
, laddr(p
, dyn
[DT_JMPREL
]), dyn
[DT_PLTRELSZ
],
1314 2+(dyn
[DT_PLTREL
]==DT_RELA
));
1315 do_relocs(p
, laddr(p
, dyn
[DT_REL
]), dyn
[DT_RELSZ
], 2);
1316 do_relocs(p
, laddr(p
, dyn
[DT_RELA
]), dyn
[DT_RELASZ
], 3);
1318 if (head
!= &ldso
&& p
->relro_start
!= p
->relro_end
&&
1319 mprotect(laddr(p
, p
->relro_start
), p
->relro_end
-p
->relro_start
, PROT_READ
)
1320 && errno
!= ENOSYS
) {
1321 error("Error relocating %s: RELRO protection failed: %m",
1323 if (runtime
) longjmp(*rtld_fail
, 1);
1330 static void kernel_mapped_dso(struct dso
*p
)
1332 size_t min_addr
= -1, max_addr
= 0, cnt
;
1334 for (cnt
= p
->phnum
; cnt
--; ph
= (void *)((char *)ph
+ p
->phentsize
)) {
1335 if (ph
->p_type
== PT_DYNAMIC
) {
1336 p
->dynv
= laddr(p
, ph
->p_vaddr
);
1337 } else if (ph
->p_type
== PT_GNU_RELRO
) {
1338 p
->relro_start
= ph
->p_vaddr
& -PAGE_SIZE
;
1339 p
->relro_end
= (ph
->p_vaddr
+ ph
->p_memsz
) & -PAGE_SIZE
;
1340 } else if (ph
->p_type
== PT_GNU_STACK
) {
1341 if (!runtime
&& ph
->p_memsz
> __default_stacksize
) {
1342 __default_stacksize
=
1343 ph
->p_memsz
< DEFAULT_STACK_MAX
?
1344 ph
->p_memsz
: DEFAULT_STACK_MAX
;
1347 if (ph
->p_type
!= PT_LOAD
) continue;
1348 if (ph
->p_vaddr
< min_addr
)
1349 min_addr
= ph
->p_vaddr
;
1350 if (ph
->p_vaddr
+ph
->p_memsz
> max_addr
)
1351 max_addr
= ph
->p_vaddr
+ph
->p_memsz
;
1353 min_addr
&= -PAGE_SIZE
;
1354 max_addr
= (max_addr
+ PAGE_SIZE
-1) & -PAGE_SIZE
;
1355 p
->map
= p
->base
+ min_addr
;
1356 p
->map_len
= max_addr
- min_addr
;
1357 p
->kernel_mapped
= 1;
1360 void __libc_exit_fini()
1363 size_t dyn
[DYN_CNT
];
1364 int self
= __pthread_self()->tid
;
1366 /* Take both locks before setting shutting_down, so that
1367 * either lock is sufficient to read its value. The lock
1368 * order matches that in dlopen to avoid deadlock. */
1369 pthread_rwlock_wrlock(&lock
);
1370 pthread_mutex_lock(&init_fini_lock
);
1372 pthread_rwlock_unlock(&lock
);
1373 for (p
=fini_head
; p
; p
=p
->fini_next
) {
1374 while (p
->ctor_visitor
&& p
->ctor_visitor
!=self
)
1375 pthread_cond_wait(&ctor_cond
, &init_fini_lock
);
1376 if (!p
->constructed
) continue;
1377 decode_vec(p
->dynv
, dyn
, DYN_CNT
);
1378 if (dyn
[0] & (1<<DT_FINI_ARRAY
)) {
1379 size_t n
= dyn
[DT_FINI_ARRAYSZ
]/sizeof(size_t);
1380 size_t *fn
= (size_t *)laddr(p
, dyn
[DT_FINI_ARRAY
])+n
;
1381 while (n
--) ((void (*)(void))*--fn
)();
1383 #ifndef NO_LEGACY_INITFINI
1384 if ((dyn
[0] & (1<<DT_FINI
)) && dyn
[DT_FINI
])
1385 fpaddr(p
, dyn
[DT_FINI
])();
1390 static struct dso
**queue_ctors(struct dso
*dso
)
1392 size_t cnt
, qpos
, spos
, i
;
1393 struct dso
*p
, **queue
, **stack
;
1395 if (ldd_mode
) return 0;
1397 /* Bound on queue size is the total number of indirect deps.
1398 * If a bfs deps list was built, we can use it. Otherwise,
1399 * bound by the total number of DSOs, which is always safe and
1400 * is reasonable we use it (for main app at startup). */
1401 if (dso
->bfs_built
) {
1402 for (cnt
=0; dso
->deps
[cnt
]; cnt
++)
1403 dso
->deps
[cnt
]->mark
= 0;
1404 cnt
++; /* self, not included in deps */
1406 for (cnt
=0, p
=head
; p
; cnt
++, p
=p
->next
)
1409 cnt
++; /* termination slot */
1410 if (dso
==head
&& cnt
<= countof(builtin_ctor_queue
))
1411 queue
= builtin_ctor_queue
;
1413 queue
= calloc(cnt
, sizeof *queue
);
1416 error("Error allocating constructor queue: %m\n");
1417 if (runtime
) longjmp(*rtld_fail
, 1);
1421 /* Opposite ends of the allocated buffer serve as an output queue
1422 * and a working stack. Setup initial stack with just the argument
1423 * dso and initial queue empty... */
1427 stack
[--spos
] = dso
;
1431 /* Then perform pseudo-DFS sort, but ignoring circular deps. */
1434 while (p
->next_dep
< p
->ndeps_direct
) {
1435 if (p
->deps
[p
->next_dep
]->mark
) {
1439 p
= p
->deps
[p
->next_dep
];
1447 for (i
=0; i
<qpos
; i
++) queue
[i
]->mark
= 0;
1452 static void do_init_fini(struct dso
**queue
)
1455 size_t dyn
[DYN_CNT
], i
;
1456 int self
= __pthread_self()->tid
;
1458 pthread_mutex_lock(&init_fini_lock
);
1459 for (i
=0; (p
=queue
[i
]); i
++) {
1460 while ((p
->ctor_visitor
&& p
->ctor_visitor
!=self
) || shutting_down
)
1461 pthread_cond_wait(&ctor_cond
, &init_fini_lock
);
1462 if (p
->ctor_visitor
|| p
->constructed
)
1464 if (p
->constructed
) continue;
1465 p
->ctor_visitor
= self
;
1467 decode_vec(p
->dynv
, dyn
, DYN_CNT
);
1468 if (dyn
[0] & ((1<<DT_FINI
) | (1<<DT_FINI_ARRAY
))) {
1469 p
->fini_next
= fini_head
;
1473 pthread_mutex_unlock(&init_fini_lock
);
1475 #ifndef NO_LEGACY_INITFINI
1476 if ((dyn
[0] & (1<<DT_INIT
)) && dyn
[DT_INIT
])
1477 fpaddr(p
, dyn
[DT_INIT
])();
1479 if (dyn
[0] & (1<<DT_INIT_ARRAY
)) {
1480 size_t n
= dyn
[DT_INIT_ARRAYSZ
]/sizeof(size_t);
1481 size_t *fn
= laddr(p
, dyn
[DT_INIT_ARRAY
]);
1482 while (n
--) ((void (*)(void))*fn
++)();
1485 pthread_mutex_lock(&init_fini_lock
);
1486 p
->ctor_visitor
= 0;
1488 pthread_cond_broadcast(&ctor_cond
);
1490 pthread_mutex_unlock(&init_fini_lock
);
1493 void __libc_start_init(void)
1495 do_init_fini(main_ctor_queue
);
1496 if (!__malloc_replaced
&& main_ctor_queue
!= builtin_ctor_queue
)
1497 free(main_ctor_queue
);
1498 main_ctor_queue
= 0;
1501 static void dl_debug_state(void)
1505 weak_alias(dl_debug_state
, _dl_debug_state
);
1507 void __init_tls(size_t *auxv
)
1511 static void update_tls_size()
1513 libc
.tls_cnt
= tls_cnt
;
1514 libc
.tls_align
= tls_align
;
1515 libc
.tls_size
= ALIGN(
1516 (1+tls_cnt
) * sizeof(void *) +
1518 sizeof(struct pthread
) +
1523 static void install_new_tls(void)
1526 pthread_t self
= __pthread_self(), td
;
1527 struct dso
*dtv_provider
= container_of(tls_tail
, struct dso
, tls
);
1528 uintptr_t (*newdtv
)[tls_cnt
+1] = (void *)dtv_provider
->new_dtv
;
1531 size_t old_cnt
= self
->dtv
[0];
1533 __block_app_sigs(&set
);
1535 /* Copy existing dtv contents from all existing threads. */
1536 for (i
=0, td
=self
; !i
|| td
!=self
; i
++, td
=td
->next
) {
1537 memcpy(newdtv
+i
, td
->dtv
,
1538 (old_cnt
+1)*sizeof(uintptr_t));
1539 newdtv
[i
][0] = tls_cnt
;
1541 /* Install new dtls into the enlarged, uninstalled dtv copies. */
1542 for (p
=head
; ; p
=p
->next
) {
1543 if (p
->tls_id
<= old_cnt
) continue;
1544 unsigned char *mem
= p
->new_tls
;
1545 for (j
=0; j
<i
; j
++) {
1546 unsigned char *new = mem
;
1547 new += ((uintptr_t)p
->tls
.image
- (uintptr_t)mem
)
1549 memcpy(new, p
->tls
.image
, p
->tls
.len
);
1550 newdtv
[j
][p
->tls_id
] =
1551 (uintptr_t)new + DTP_OFFSET
;
1552 mem
+= p
->tls
.size
+ p
->tls
.align
;
1554 if (p
->tls_id
== tls_cnt
) break;
1557 /* Broadcast barrier to ensure contents of new dtv is visible
1558 * if the new dtv pointer is. The __membarrier function has a
1559 * fallback emulation using signals for kernels that lack the
1560 * feature at the syscall level. */
1562 __membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED
, 0);
1564 /* Install new dtv for each thread. */
1565 for (j
=0, td
=self
; !j
|| td
!=self
; j
++, td
=td
->next
) {
1566 td
->dtv
= td
->dtv_copy
= newdtv
[j
];
1570 __restore_sigs(&set
);
1573 /* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the
1574 * following stage 2 and stage 3 functions via primitive symbolic lookup
1575 * since it does not have access to their addresses to begin with. */
1577 /* Stage 2 of the dynamic linker is called after relative relocations
1578 * have been processed. It can make function calls to static functions
1579 * and access string literals and static data, but cannot use extern
1580 * symbols. Its job is to perform symbolic relocations on the dynamic
1581 * linker itself, but some of the relocations performed may need to be
1582 * replaced later due to copy relocations in the main program. */
1584 hidden
void __dls2(unsigned char *base
, size_t *sp
)
1587 void *p1
= (void *)sp
[-2];
1588 void *p2
= (void *)sp
[-1];
1590 size_t *auxv
, aux
[AUX_CNT
];
1591 for (auxv
=sp
+1+*sp
+1; *auxv
; auxv
++);
1593 decode_vec(auxv
, aux
, AUX_CNT
);
1594 if (aux
[AT_BASE
]) ldso
.base
= (void *)aux
[AT_BASE
];
1595 else ldso
.base
= (void *)(aux
[AT_PHDR
] & -4096);
1597 app_loadmap
= p2
? p1
: 0;
1598 ldso
.loadmap
= p2
? p2
: p1
;
1599 ldso
.base
= laddr(&ldso
, 0);
1603 Ehdr
*ehdr
= (void *)ldso
.base
;
1604 ldso
.name
= ldso
.shortname
= "libc.so";
1605 ldso
.phnum
= ehdr
->e_phnum
;
1606 ldso
.phdr
= laddr(&ldso
, ehdr
->e_phoff
);
1607 ldso
.phentsize
= ehdr
->e_phentsize
;
1608 kernel_mapped_dso(&ldso
);
1611 if (DL_FDPIC
) makefuncdescs(&ldso
);
1613 /* Prepare storage for to save clobbered REL addends so they
1614 * can be reused in stage 3. There should be very few. If
1615 * something goes wrong and there are a huge number, abort
1616 * instead of risking stack overflow. */
1617 size_t dyn
[DYN_CNT
];
1618 decode_vec(ldso
.dynv
, dyn
, DYN_CNT
);
1619 size_t *rel
= laddr(&ldso
, dyn
[DT_REL
]);
1620 size_t rel_size
= dyn
[DT_RELSZ
];
1621 size_t symbolic_rel_cnt
= 0;
1622 apply_addends_to
= rel
;
1623 for (; rel_size
; rel
+=2, rel_size
-=2*sizeof(size_t))
1624 if (!IS_RELATIVE(rel
[1], ldso
.syms
)) symbolic_rel_cnt
++;
1625 if (symbolic_rel_cnt
>= ADDEND_LIMIT
) a_crash();
1626 size_t addends
[symbolic_rel_cnt
+1];
1627 saved_addends
= addends
;
1634 /* Call dynamic linker stage-2b, __dls2b, looking it up
1635 * symbolically as a barrier against moving the address
1636 * load across the above relocation processing. */
1637 struct symdef dls2b_def
= find_sym(&ldso
, "__dls2b", 0);
1638 if (DL_FDPIC
) ((stage3_func
)&ldso
.funcdescs
[dls2b_def
.sym
-ldso
.syms
])(sp
);
1639 else ((stage3_func
)laddr(&ldso
, dls2b_def
.sym
->st_value
))(sp
);
1642 /* Stage 2b sets up a valid thread pointer, which requires relocations
1643 * completed in stage 2, and on which stage 3 is permitted to depend.
1644 * This is done as a separate stage, with symbolic lookup as a barrier,
1645 * so that loads of the thread pointer and &errno can be pure/const and
1646 * thereby hoistable. */
1648 _Noreturn
void __dls2b(size_t *sp
)
1650 /* Setup early thread pointer in builtin_tls for ldso/libc itself to
1651 * use during dynamic linking. If possible it will also serve as the
1652 * thread pointer at runtime. */
1653 libc
.tls_size
= sizeof builtin_tls
;
1654 libc
.tls_align
= tls_align
;
1655 if (__init_tp(__copy_tls((void *)builtin_tls
)) < 0) {
1659 struct symdef dls3_def
= find_sym(&ldso
, "__dls3", 0);
1660 if (DL_FDPIC
) ((stage3_func
)&ldso
.funcdescs
[dls3_def
.sym
-ldso
.syms
])(sp
);
1661 else ((stage3_func
)laddr(&ldso
, dls3_def
.sym
->st_value
))(sp
);
1664 /* Stage 3 of the dynamic linker is called with the dynamic linker/libc
1665 * fully functional. Its job is to load (if not already loaded) and
1666 * process dependencies and relocations for the main application and
1667 * transfer control to its entry point. */
1669 _Noreturn
void __dls3(size_t *sp
)
1671 static struct dso app
, vdso
;
1672 size_t aux
[AUX_CNT
], *auxv
;
1674 char *env_preload
=0;
1675 char *replace_argv0
=0;
1678 char **argv
= (void *)(sp
+1);
1679 char **argv_orig
= argv
;
1680 char **envp
= argv
+argc
+1;
1682 /* Find aux vector just past environ[] and use it to initialize
1683 * global data that may be needed before we can make syscalls. */
1685 for (i
=argc
+1; argv
[i
]; i
++);
1686 libc
.auxv
= auxv
= (void *)(argv
+i
+1);
1687 decode_vec(auxv
, aux
, AUX_CNT
);
1688 __hwcap
= aux
[AT_HWCAP
];
1689 libc
.page_size
= aux
[AT_PAGESZ
];
1690 libc
.secure
= ((aux
[0]&0x7800)!=0x7800 || aux
[AT_UID
]!=aux
[AT_EUID
]
1691 || aux
[AT_GID
]!=aux
[AT_EGID
] || aux
[AT_SECURE
]);
1693 /* Only trust user/env if kernel says we're not suid/sgid */
1695 env_path
= getenv("LD_LIBRARY_PATH");
1696 env_preload
= getenv("LD_PRELOAD");
1699 /* If the main program was already loaded by the kernel,
1700 * AT_PHDR will point to some location other than the dynamic
1701 * linker's program headers. */
1702 if (aux
[AT_PHDR
] != (size_t)ldso
.phdr
) {
1703 size_t interp_off
= 0;
1704 size_t tls_image
= 0;
1705 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
1706 Phdr
*phdr
= app
.phdr
= (void *)aux
[AT_PHDR
];
1707 app
.phnum
= aux
[AT_PHNUM
];
1708 app
.phentsize
= aux
[AT_PHENT
];
1709 for (i
=aux
[AT_PHNUM
]; i
; i
--, phdr
=(void *)((char *)phdr
+ aux
[AT_PHENT
])) {
1710 if (phdr
->p_type
== PT_PHDR
)
1711 app
.base
= (void *)(aux
[AT_PHDR
] - phdr
->p_vaddr
);
1712 else if (phdr
->p_type
== PT_INTERP
)
1713 interp_off
= (size_t)phdr
->p_vaddr
;
1714 else if (phdr
->p_type
== PT_TLS
) {
1715 tls_image
= phdr
->p_vaddr
;
1716 app
.tls
.len
= phdr
->p_filesz
;
1717 app
.tls
.size
= phdr
->p_memsz
;
1718 app
.tls
.align
= phdr
->p_align
;
1721 if (DL_FDPIC
) app
.loadmap
= app_loadmap
;
1722 if (app
.tls
.size
) app
.tls
.image
= laddr(&app
, tls_image
);
1723 if (interp_off
) ldso
.name
= laddr(&app
, interp_off
);
1724 if ((aux
[0] & (1UL<<AT_EXECFN
))
1725 && strncmp((char *)aux
[AT_EXECFN
], "/proc/", 6))
1726 app
.name
= (char *)aux
[AT_EXECFN
];
1729 kernel_mapped_dso(&app
);
1732 char *ldname
= argv
[0];
1733 size_t l
= strlen(ldname
);
1734 if (l
>= 3 && !strcmp(ldname
+l
-3, "ldd")) ldd_mode
= 1;
1736 while (argv
[0] && argv
[0][0]=='-' && argv
[0][1]=='-') {
1737 char *opt
= argv
[0]+2;
1738 *argv
++ = (void *)-1;
1741 } else if (!memcmp(opt
, "list", 5)) {
1743 } else if (!memcmp(opt
, "library-path", 12)) {
1744 if (opt
[12]=='=') env_path
= opt
+13;
1745 else if (opt
[12]) *argv
= 0;
1746 else if (*argv
) env_path
= *argv
++;
1747 } else if (!memcmp(opt
, "preload", 7)) {
1748 if (opt
[7]=='=') env_preload
= opt
+8;
1749 else if (opt
[7]) *argv
= 0;
1750 else if (*argv
) env_preload
= *argv
++;
1751 } else if (!memcmp(opt
, "argv0", 5)) {
1752 if (opt
[5]=='=') replace_argv0
= opt
+6;
1753 else if (opt
[5]) *argv
= 0;
1754 else if (*argv
) replace_argv0
= *argv
++;
1759 argv
[-1] = (void *)(argc
- (argv
-argv_orig
));
1761 dprintf(2, "musl libc (" LDSO_ARCH
")\n"
1763 "Dynamic Program Loader\n"
1764 "Usage: %s [options] [--] pathname%s\n",
1765 __libc_version
, ldname
,
1766 ldd_mode
? "" : " [args]");
1769 fd
= open(argv
[0], O_RDONLY
);
1771 dprintf(2, "%s: cannot load %s: %s\n", ldname
, argv
[0], strerror(errno
));
1774 Ehdr
*ehdr
= (void *)map_library(fd
, &app
);
1776 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname
, argv
[0]);
1782 aux
[AT_ENTRY
] = (size_t)laddr(&app
, ehdr
->e_entry
);
1783 /* Find the name that would have been used for the dynamic
1784 * linker had ldd not taken its place. */
1786 for (i
=0; i
<app
.phnum
; i
++) {
1787 if (app
.phdr
[i
].p_type
== PT_INTERP
)
1788 ldso
.name
= laddr(&app
, app
.phdr
[i
].p_vaddr
);
1790 dprintf(1, "\t%s (%p)\n", ldso
.name
, ldso
.base
);
1794 libc
.tls_head
= tls_tail
= &app
.tls
;
1795 app
.tls_id
= tls_cnt
= 1;
1797 app
.tls
.offset
= GAP_ABOVE_TP
;
1798 app
.tls
.offset
+= -GAP_ABOVE_TP
& (app
.tls
.align
-1);
1799 tls_offset
= app
.tls
.offset
+ app
.tls
.size
1800 + ( -((uintptr_t)app
.tls
.image
+ app
.tls
.size
)
1801 & (app
.tls
.align
-1) );
1803 tls_offset
= app
.tls
.offset
= app
.tls
.size
1804 + ( -((uintptr_t)app
.tls
.image
+ app
.tls
.size
)
1805 & (app
.tls
.align
-1) );
1807 tls_align
= MAXP2(tls_align
, app
.tls
.align
);
1811 makefuncdescs(&app
);
1813 app
.loadmap
= (void *)&app_dummy_loadmap
;
1814 app
.loadmap
->nsegs
= 1;
1815 app
.loadmap
->segs
[0].addr
= (size_t)app
.map
;
1816 app
.loadmap
->segs
[0].p_vaddr
= (size_t)app
.map
1818 app
.loadmap
->segs
[0].p_memsz
= app
.map_len
;
1820 argv
[-3] = (void *)app
.loadmap
;
1823 /* Initial dso chain consists only of the app. */
1824 head
= tail
= syms_tail
= &app
;
1826 /* Donate unused parts of app and library mapping to malloc */
1828 reclaim_gaps(&ldso
);
1830 /* Load preload/needed libraries, add symbols to global namespace. */
1831 ldso
.deps
= (struct dso
**)no_deps
;
1832 if (env_preload
) load_preload(env_preload
);
1834 for (struct dso
*p
=head
; p
; p
=p
->next
)
1837 /* Attach to vdso, if provided by the kernel, last so that it does
1838 * not become part of the global namespace. */
1839 if (search_vec(auxv
, &vdso_base
, AT_SYSINFO_EHDR
) && vdso_base
) {
1840 Ehdr
*ehdr
= (void *)vdso_base
;
1841 Phdr
*phdr
= vdso
.phdr
= (void *)(vdso_base
+ ehdr
->e_phoff
);
1842 vdso
.phnum
= ehdr
->e_phnum
;
1843 vdso
.phentsize
= ehdr
->e_phentsize
;
1844 for (i
=ehdr
->e_phnum
; i
; i
--, phdr
=(void *)((char *)phdr
+ ehdr
->e_phentsize
)) {
1845 if (phdr
->p_type
== PT_DYNAMIC
)
1846 vdso
.dynv
= (void *)(vdso_base
+ phdr
->p_offset
);
1847 if (phdr
->p_type
== PT_LOAD
)
1848 vdso
.base
= (void *)(vdso_base
- phdr
->p_vaddr
+ phdr
->p_offset
);
1851 vdso
.shortname
= "linux-gate.so.1";
1853 vdso
.deps
= (struct dso
**)no_deps
;
1860 for (i
=0; app
.dynv
[i
]; i
+=2) {
1861 if (!DT_DEBUG_INDIRECT
&& app
.dynv
[i
]==DT_DEBUG
)
1862 app
.dynv
[i
+1] = (size_t)&debug
;
1863 if (DT_DEBUG_INDIRECT
&& app
.dynv
[i
]==DT_DEBUG_INDIRECT
) {
1864 size_t *ptr
= (size_t *) app
.dynv
[i
+1];
1865 *ptr
= (size_t)&debug
;
1869 /* This must be done before final relocations, since it calls
1870 * malloc, which may be provided by the application. Calling any
1871 * application code prior to the jump to its entry point is not
1872 * valid in our model and does not work with FDPIC, where there
1873 * are additional relocation-like fixups that only the entry point
1874 * code can see to perform. */
1875 main_ctor_queue
= queue_ctors(&app
);
1877 /* The main program must be relocated LAST since it may contin
1878 * copy relocations which depend on libraries' relocations. */
1879 reloc_all(app
.next
);
1883 if (libc
.tls_size
> sizeof builtin_tls
|| tls_align
> MIN_TLS_ALIGN
) {
1884 void *initial_tls
= calloc(libc
.tls_size
, 1);
1886 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
1887 argv
[0], libc
.tls_size
);
1890 if (__init_tp(__copy_tls(initial_tls
)) < 0) {
1894 size_t tmp_tls_size
= libc
.tls_size
;
1895 pthread_t self
= __pthread_self();
1896 /* Temporarily set the tls size to the full size of
1897 * builtin_tls so that __copy_tls will use the same layout
1898 * as it did for before. Then check, just to be safe. */
1899 libc
.tls_size
= sizeof builtin_tls
;
1900 if (__copy_tls((void*)builtin_tls
) != self
) a_crash();
1901 libc
.tls_size
= tmp_tls_size
;
1903 static_tls_cnt
= tls_cnt
;
1905 if (ldso_fail
) _exit(127);
1906 if (ldd_mode
) _exit(0);
1908 /* Determine if malloc was interposed by a replacement implementation
1909 * so that calloc and the memalign family can harden against the
1910 * possibility of incomplete replacement. */
1911 if (find_sym(head
, "malloc", 1).dso
!= &ldso
)
1912 __malloc_replaced
= 1;
1914 /* Switch to runtime mode: any further failures in the dynamic
1915 * linker are a reportable failure rather than a fatal startup
1920 debug
.bp
= dl_debug_state
;
1922 debug
.base
= ldso
.base
;
1926 if (replace_argv0
) argv
[0] = replace_argv0
;
1930 CRTJMP((void *)aux
[AT_ENTRY
], argv
-1);
1934 static void prepare_lazy(struct dso
*p
)
1936 size_t dyn
[DYN_CNT
], n
, flags1
=0;
1937 decode_vec(p
->dynv
, dyn
, DYN_CNT
);
1938 search_vec(p
->dynv
, &flags1
, DT_FLAGS_1
);
1939 if (dyn
[DT_BIND_NOW
] || (dyn
[DT_FLAGS
] & DF_BIND_NOW
) || (flags1
& DF_1_NOW
))
1941 n
= dyn
[DT_RELSZ
]/2 + dyn
[DT_RELASZ
]/3 + dyn
[DT_PLTRELSZ
]/2 + 1;
1942 if (NEED_MIPS_GOT_RELOCS
) {
1943 size_t j
=0; search_vec(p
->dynv
, &j
, DT_MIPS_GOTSYM
);
1944 size_t i
=0; search_vec(p
->dynv
, &i
, DT_MIPS_SYMTABNO
);
1947 p
->lazy
= calloc(n
, 3*sizeof(size_t));
1949 error("Error preparing lazy relocation for %s: %m", p
->name
);
1950 longjmp(*rtld_fail
, 1);
1952 p
->lazy_next
= lazy_head
;
1956 void *dlopen(const char *file
, int mode
)
1958 struct dso
*volatile p
, *orig_tail
, *orig_syms_tail
, *orig_lazy_head
, *next
;
1959 struct tls_module
*orig_tls_tail
;
1960 size_t orig_tls_cnt
, orig_tls_offset
, orig_tls_align
;
1964 struct dso
**volatile ctor_queue
= 0;
1966 if (!file
) return head
;
1968 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
1969 pthread_rwlock_wrlock(&lock
);
1973 if (shutting_down
) {
1974 error("Cannot dlopen while program is exiting.");
1977 orig_tls_tail
= tls_tail
;
1978 orig_tls_cnt
= tls_cnt
;
1979 orig_tls_offset
= tls_offset
;
1980 orig_tls_align
= tls_align
;
1981 orig_lazy_head
= lazy_head
;
1982 orig_syms_tail
= syms_tail
;
1984 noload
= mode
& RTLD_NOLOAD
;
1987 if (setjmp(*rtld_fail
)) {
1988 /* Clean up anything new that was (partially) loaded */
1989 revert_syms(orig_syms_tail
);
1990 for (p
=orig_tail
->next
; p
; p
=next
) {
1992 while (p
->td_index
) {
1993 void *tmp
= p
->td_index
->next
;
1998 if (p
->rpath
!= p
->rpath_orig
)
2006 if (!orig_tls_tail
) libc
.tls_head
= 0;
2007 tls_tail
= orig_tls_tail
;
2008 if (tls_tail
) tls_tail
->next
= 0;
2009 tls_cnt
= orig_tls_cnt
;
2010 tls_offset
= orig_tls_offset
;
2011 tls_align
= orig_tls_align
;
2012 lazy_head
= orig_lazy_head
;
2017 } else p
= load_library(file
, head
);
2021 "Library %s is not already loaded" :
2022 "Error loading shared library %s: %m",
2027 /* First load handling */
2030 pthread_mutex_lock(&init_fini_lock
);
2031 if (!p
->constructed
) ctor_queue
= queue_ctors(p
);
2032 pthread_mutex_unlock(&init_fini_lock
);
2033 if (!p
->relocated
&& (mode
& RTLD_LAZY
)) {
2035 for (i
=0; p
->deps
[i
]; i
++)
2036 if (!p
->deps
[i
]->relocated
)
2037 prepare_lazy(p
->deps
[i
]);
2039 if (!p
->relocated
|| (mode
& RTLD_GLOBAL
)) {
2040 /* Make new symbols global, at least temporarily, so we can do
2041 * relocations. If not RTLD_GLOBAL, this is reverted below. */
2043 for (i
=0; p
->deps
[i
]; i
++)
2044 add_syms(p
->deps
[i
]);
2046 if (!p
->relocated
) {
2050 /* If RTLD_GLOBAL was not specified, undo any new additions
2051 * to the global symbol table. This is a nop if the library was
2052 * previously loaded and already global. */
2053 if (!(mode
& RTLD_GLOBAL
))
2054 revert_syms(orig_syms_tail
);
2056 /* Processing of deferred lazy relocations must not happen until
2057 * the new libraries are committed; otherwise we could end up with
2058 * relocations resolved to symbol definitions that get removed. */
2062 if (tls_cnt
!= orig_tls_cnt
)
2069 pthread_rwlock_unlock(&lock
);
2071 do_init_fini(ctor_queue
);
2074 pthread_setcancelstate(cs
, 0);
2078 hidden
int __dl_invalid_handle(void *h
)
2081 for (p
=head
; p
; p
=p
->next
) if (h
==p
) return 0;
2082 error("Invalid library handle %p", (void *)h
);
2086 static void *addr2dso(size_t a
)
2090 if (DL_FDPIC
) for (p
=head
; p
; p
=p
->next
) {
2092 if (a
-(size_t)p
->funcdescs
< i
*sizeof(*p
->funcdescs
))
2095 for (p
=head
; p
; p
=p
->next
) {
2096 if (DL_FDPIC
&& p
->loadmap
) {
2097 for (i
=0; i
<p
->loadmap
->nsegs
; i
++) {
2098 if (a
-p
->loadmap
->segs
[i
].p_vaddr
2099 < p
->loadmap
->segs
[i
].p_memsz
)
2104 size_t phcnt
= p
->phnum
;
2105 size_t entsz
= p
->phentsize
;
2106 size_t base
= (size_t)p
->base
;
2107 for (; phcnt
--; ph
=(void *)((char *)ph
+entsz
)) {
2108 if (ph
->p_type
!= PT_LOAD
) continue;
2109 if (a
-base
-ph
->p_vaddr
< ph
->p_memsz
)
2112 if (a
-(size_t)p
->map
< p
->map_len
)
2119 static void *do_dlsym(struct dso
*p
, const char *s
, void *ra
)
2122 uint32_t h
= 0, gh
= 0, *ght
;
2124 if (p
== head
|| p
== RTLD_DEFAULT
|| p
== RTLD_NEXT
) {
2125 if (p
== RTLD_DEFAULT
) {
2127 } else if (p
== RTLD_NEXT
) {
2128 p
= addr2dso((size_t)ra
);
2132 struct symdef def
= find_sym(p
, s
, 0);
2133 if (!def
.sym
) goto failed
;
2134 if ((def
.sym
->st_info
&0xf) == STT_TLS
)
2135 return __tls_get_addr((tls_mod_off_t
[]){def
.dso
->tls_id
, def
.sym
->st_value
-DTP_OFFSET
});
2136 if (DL_FDPIC
&& (def
.sym
->st_info
&0xf) == STT_FUNC
)
2137 return def
.dso
->funcdescs
+ (def
.sym
- def
.dso
->syms
);
2138 return laddr(def
.dso
, def
.sym
->st_value
);
2140 if (__dl_invalid_handle(p
))
2142 if ((ght
= p
->ghashtab
)) {
2144 sym
= gnu_lookup(gh
, ght
, p
, s
);
2147 sym
= sysv_lookup(s
, h
, p
);
2149 if (sym
&& (sym
->st_info
&0xf) == STT_TLS
)
2150 return __tls_get_addr((tls_mod_off_t
[]){p
->tls_id
, sym
->st_value
-DTP_OFFSET
});
2151 if (DL_FDPIC
&& sym
&& sym
->st_shndx
&& (sym
->st_info
&0xf) == STT_FUNC
)
2152 return p
->funcdescs
+ (sym
- p
->syms
);
2153 if (sym
&& sym
->st_value
&& (1<<(sym
->st_info
&0xf) & OK_TYPES
))
2154 return laddr(p
, sym
->st_value
);
2155 for (i
=0; p
->deps
[i
]; i
++) {
2156 if ((ght
= p
->deps
[i
]->ghashtab
)) {
2157 if (!gh
) gh
= gnu_hash(s
);
2158 sym
= gnu_lookup(gh
, ght
, p
->deps
[i
], s
);
2160 if (!h
) h
= sysv_hash(s
);
2161 sym
= sysv_lookup(s
, h
, p
->deps
[i
]);
2163 if (sym
&& (sym
->st_info
&0xf) == STT_TLS
)
2164 return __tls_get_addr((tls_mod_off_t
[]){p
->deps
[i
]->tls_id
, sym
->st_value
-DTP_OFFSET
});
2165 if (DL_FDPIC
&& sym
&& sym
->st_shndx
&& (sym
->st_info
&0xf) == STT_FUNC
)
2166 return p
->deps
[i
]->funcdescs
+ (sym
- p
->deps
[i
]->syms
);
2167 if (sym
&& sym
->st_value
&& (1<<(sym
->st_info
&0xf) & OK_TYPES
))
2168 return laddr(p
->deps
[i
], sym
->st_value
);
2171 error("Symbol not found: %s", s
);
2175 int dladdr(const void *addr_arg
, Dl_info
*info
)
2177 size_t addr
= (size_t)addr_arg
;
2183 size_t besterr
= -1;
2185 pthread_rwlock_rdlock(&lock
);
2187 pthread_rwlock_unlock(&lock
);
2192 strings
= p
->strings
;
2193 nsym
= count_syms(p
);
2196 size_t idx
= (addr
-(size_t)p
->funcdescs
)
2197 / sizeof(*p
->funcdescs
);
2198 if (idx
< nsym
&& (sym
[idx
].st_info
&0xf) == STT_FUNC
) {
2199 best
= (size_t)(p
->funcdescs
+ idx
);
2200 bestsym
= sym
+ idx
;
2205 if (!best
) for (; nsym
; nsym
--, sym
++) {
2207 && (1<<(sym
->st_info
&0xf) & OK_TYPES
)
2208 && (1<<(sym
->st_info
>>4) & OK_BINDS
)) {
2209 size_t symaddr
= (size_t)laddr(p
, sym
->st_value
);
2210 if (symaddr
> addr
|| symaddr
<= best
)
2214 besterr
= addr
- symaddr
;
2215 if (addr
== symaddr
)
2220 if (bestsym
&& besterr
> bestsym
->st_size
-1) {
2225 info
->dli_fname
= p
->name
;
2226 info
->dli_fbase
= p
->map
;
2229 info
->dli_sname
= 0;
2230 info
->dli_saddr
= 0;
2234 if (DL_FDPIC
&& (bestsym
->st_info
&0xf) == STT_FUNC
)
2235 best
= (size_t)(p
->funcdescs
+ (bestsym
- p
->syms
));
2236 info
->dli_sname
= strings
+ bestsym
->st_name
;
2237 info
->dli_saddr
= (void *)best
;
2242 hidden
void *__dlsym(void *restrict p
, const char *restrict s
, void *restrict ra
)
2245 pthread_rwlock_rdlock(&lock
);
2246 res
= do_dlsym(p
, s
, ra
);
2247 pthread_rwlock_unlock(&lock
);
2251 int dl_iterate_phdr(int(*callback
)(struct dl_phdr_info
*info
, size_t size
, void *data
), void *data
)
2253 struct dso
*current
;
2254 struct dl_phdr_info info
;
2256 for(current
= head
; current
;) {
2257 info
.dlpi_addr
= (uintptr_t)current
->base
;
2258 info
.dlpi_name
= current
->name
;
2259 info
.dlpi_phdr
= current
->phdr
;
2260 info
.dlpi_phnum
= current
->phnum
;
2261 info
.dlpi_adds
= gencnt
;
2263 info
.dlpi_tls_modid
= current
->tls_id
;
2264 info
.dlpi_tls_data
= current
->tls
.image
;
2266 ret
= (callback
)(&info
, sizeof (info
), data
);
2268 if (ret
!= 0) break;
2270 pthread_rwlock_rdlock(&lock
);
2271 current
= current
->next
;
2272 pthread_rwlock_unlock(&lock
);
2277 static void error(const char *fmt
, ...)
2282 vdprintf(2, fmt
, ap
);
2288 __dl_vseterr(fmt
, ap
);