blob: 3a0bf95d03540b8532d6e18a26cf11a5cf8ccc3f [file] [log] [blame]
Rich Felkerf419bcb2012-08-26 21:09:26 -04001#define _GNU_SOURCE
Rich Felker51e2d832011-06-18 19:48:42 -04002#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
6#include <stdint.h>
7#include <elf.h>
8#include <sys/mman.h>
9#include <limits.h>
10#include <stdint.h>
11#include <fcntl.h>
12#include <sys/stat.h>
13#include <errno.h>
14#include <limits.h>
15#include <elf.h>
Rich Felker18c0e022012-10-31 21:27:48 -040016#include <link.h>
Rich Felker6b3d5e52011-06-26 17:39:17 -040017#include <setjmp.h>
Rich Felker59ab43f2011-06-26 19:23:28 -040018#include <pthread.h>
Rich Felker2719cc82011-08-16 00:24:36 -040019#include <ctype.h>
Rich Felker59ab43f2011-06-26 19:23:28 -040020#include <dlfcn.h>
Rich Felker8431d792012-10-04 16:35:46 -040021#include "pthread_impl.h"
22#include "libc.h"
Rich Felker51e2d832011-06-18 19:48:42 -040023
Rich Felkera9e85c02012-03-23 00:28:20 -040024static int errflag;
Rich Felkera5d10eb2012-04-23 12:03:31 -040025static char errbuf[128];
Rich Felkera9e85c02012-03-23 00:28:20 -040026
Rich Felkere864a292012-07-11 01:47:30 -040027#ifdef SHARED
Rich Felkera9e85c02012-03-23 00:28:20 -040028
Rich Felker51e2d832011-06-18 19:48:42 -040029#if ULONG_MAX == 0xffffffff
30typedef Elf32_Ehdr Ehdr;
31typedef Elf32_Phdr Phdr;
32typedef Elf32_Sym Sym;
33#define R_TYPE(x) ((x)&255)
34#define R_SYM(x) ((x)>>8)
35#else
36typedef Elf64_Ehdr Ehdr;
37typedef Elf64_Phdr Phdr;
38typedef Elf64_Sym Sym;
39#define R_TYPE(x) ((x)&0xffffffff)
40#define R_SYM(x) ((x)>>32)
41#endif
42
Rich Felkercf3fd3d2012-10-06 01:22:51 -040043#define MAXP2(a,b) (-(-(a)&-(b)))
44#define ALIGN(x,y) ((x)+(y)-1 & -(y))
45
Rich Felker3ec8d292012-04-25 00:05:42 -040046struct debug {
47 int ver;
48 void *head;
49 void (*bp)(void);
50 int state;
51 void *base;
52};
53
54struct dso {
55 unsigned char *base;
56 char *name;
Rich Felker51e2d832011-06-18 19:48:42 -040057 size_t *dynv;
Rich Felker3ec8d292012-04-25 00:05:42 -040058 struct dso *next, *prev;
59
Rich Felker18c0e022012-10-31 21:27:48 -040060 Phdr *phdr;
61 int phnum;
Rich Felker3ec8d292012-04-25 00:05:42 -040062 int refcnt;
Rich Felker51e2d832011-06-18 19:48:42 -040063 Sym *syms;
Rich Felker596d60c2011-06-18 22:52:01 -040064 uint32_t *hashtab;
Rich Felker2bd05a42012-08-25 17:13:28 -040065 uint32_t *ghashtab;
Rich Felker51e2d832011-06-18 19:48:42 -040066 char *strings;
Rich Felker51e2d832011-06-18 19:48:42 -040067 unsigned char *map;
68 size_t map_len;
69 dev_t dev;
70 ino_t ino;
Rich Felker6343ac82012-06-09 21:20:44 -040071 signed char global;
Rich Felker700a8152012-02-07 20:29:29 -050072 char relocated;
73 char constructed;
Rich Felker59ab43f2011-06-26 19:23:28 -040074 struct dso **deps;
Rich Felkerbc6a35f2012-10-04 20:04:13 -040075 void *tls_image;
Rich Felker9c748562012-10-04 22:48:33 -040076 size_t tls_len, tls_size, tls_align, tls_id, tls_offset;
Rich Felkerdcd60372012-10-05 11:51:50 -040077 void **new_dtv;
78 unsigned char *new_tls;
79 int new_dtv_idx, new_tls_idx;
Rich Felkerf4f77c02012-10-05 13:09:09 -040080 struct dso *fini_next;
Rich Felker5c1909a2012-05-27 16:01:44 -040081 char *shortname;
Rich Felker6b3d5e52011-06-26 17:39:17 -040082 char buf[];
Rich Felker51e2d832011-06-18 19:48:42 -040083};
84
Rich Felker9c748562012-10-04 22:48:33 -040085struct symdef {
86 Sym *sym;
87 struct dso *dso;
88};
89
Rich Felker59f40862012-08-05 13:46:39 -040090#include "reloc.h"
91
Rich Felker58aa5f42012-05-03 20:42:45 -040092void __init_ssp(size_t *);
Rich Felkerbc6a35f2012-10-04 20:04:13 -040093void *__install_initial_tls(void *);
Rich Felker75863602013-07-21 03:00:54 -040094void __init_libc(char **, char *);
Rich Felker60872cf2012-04-24 18:07:59 -040095
Rich Felkere23d3582012-10-13 23:25:20 -040096static struct dso *head, *tail, *ldso, *fini_head;
Rich Felker191ebca2011-06-30 23:02:27 -040097static char *env_path, *sys_path, *r_path;
Rich Felker18c0e022012-10-31 21:27:48 -040098static unsigned long long gencnt;
Rich Felker60872cf2012-04-24 18:07:59 -040099static int ssp_used;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400100static int runtime;
Rich Felker5c1909a2012-05-27 16:01:44 -0400101static int ldd_mode;
Rich Felker04109502012-08-18 16:00:23 -0400102static int ldso_fail;
Rich Felker4d07e552013-01-23 22:07:45 -0500103static int noload;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400104static jmp_buf rtld_fail;
Rich Felker59ab43f2011-06-26 19:23:28 -0400105static pthread_rwlock_t lock;
Rich Felker3ec8d292012-04-25 00:05:42 -0400106static struct debug debug;
Rich Felkerc62b9f32012-10-14 19:56:50 -0400107static size_t tls_cnt, tls_offset, tls_align = 4*sizeof(size_t);
Rich Felkerf4f77c02012-10-05 13:09:09 -0400108static pthread_mutex_t init_fini_lock = { ._m_type = PTHREAD_MUTEX_RECURSIVE };
Rich Felker3ec8d292012-04-25 00:05:42 -0400109
110struct debug *_dl_debug_addr = &debug;
Rich Felker51e2d832011-06-18 19:48:42 -0400111
Rich Felker0a96a372012-10-07 21:43:46 -0400112#define AUX_CNT 38
Rich Felker51e2d832011-06-18 19:48:42 -0400113#define DYN_CNT 34
114
115static void decode_vec(size_t *v, size_t *a, size_t cnt)
116{
117 memset(a, 0, cnt*sizeof(size_t));
118 for (; v[0]; v+=2) if (v[0]<cnt) {
119 a[0] |= 1ULL<<v[0];
120 a[v[0]] = v[1];
121 }
122}
123
Rich Felker2bd05a42012-08-25 17:13:28 -0400124static int search_vec(size_t *v, size_t *r, size_t key)
125{
126 for (; v[0]!=key; v+=2)
127 if (!v[0]) return 0;
128 *r = v[1];
129 return 1;
130}
131
132static uint32_t sysv_hash(const char *s0)
Rich Felker51e2d832011-06-18 19:48:42 -0400133{
Rich Felker2adf2fb2012-01-17 00:34:58 -0500134 const unsigned char *s = (void *)s0;
Rich Felker51e2d832011-06-18 19:48:42 -0400135 uint_fast32_t h = 0;
136 while (*s) {
137 h = 16*h + *s++;
138 h ^= h>>24 & 0xf0;
139 }
140 return h & 0xfffffff;
141}
142
Rich Felker2bd05a42012-08-25 17:13:28 -0400143static uint32_t gnu_hash(const char *s0)
144{
145 const unsigned char *s = (void *)s0;
146 uint_fast32_t h = 5381;
147 for (; *s; s++)
148 h = h*33 + *s;
149 return h;
150}
151
152static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
Rich Felker51e2d832011-06-18 19:48:42 -0400153{
154 size_t i;
Rich Felker05eff012012-08-05 02:38:35 -0400155 Sym *syms = dso->syms;
156 uint32_t *hashtab = dso->hashtab;
157 char *strings = dso->strings;
Rich Felker51e2d832011-06-18 19:48:42 -0400158 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
159 if (!strcmp(s, strings+syms[i].st_name))
160 return syms+i;
161 }
162 return 0;
163}
164
Rich Felker2bd05a42012-08-25 17:13:28 -0400165static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso)
166{
167 Sym *sym;
168 char *strings;
169 uint32_t *hashtab = dso->ghashtab;
170 uint32_t nbuckets = hashtab[0];
171 uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
172 uint32_t h2;
173 uint32_t *hashval;
174 uint32_t n = buckets[h1 % nbuckets];
175
176 if (!n) return 0;
177
178 strings = dso->strings;
179 sym = dso->syms + n;
180 hashval = buckets + nbuckets + (n - hashtab[1]);
181
182 for (h1 |= 1; ; sym++) {
183 h2 = *hashval++;
184 if ((h1 == (h2|1)) && !strcmp(s, strings + sym->st_name))
185 return sym;
186 if (h2 & 1) break;
187 }
188
189 return 0;
190}
191
Rich Felker9c748562012-10-04 22:48:33 -0400192#define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
Rich Felker427173b2011-07-24 02:19:47 -0400193#define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
Rich Felker51e2d832011-06-18 19:48:42 -0400194
Rich Felker9c748562012-10-04 22:48:33 -0400195static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
Rich Felker51e2d832011-06-18 19:48:42 -0400196{
Rich Felker2bd05a42012-08-25 17:13:28 -0400197 uint32_t h = 0, gh = 0;
Rich Felker9c748562012-10-04 22:48:33 -0400198 struct symdef def = {0};
Rich Felker2bd05a42012-08-25 17:13:28 -0400199 if (dso->ghashtab) {
200 gh = gnu_hash(s);
Rich Felker2bd05a42012-08-25 17:13:28 -0400201 if (gh == 0x1f4039c9 && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
202 } else {
203 h = sysv_hash(s);
Rich Felker2bd05a42012-08-25 17:13:28 -0400204 if (h == 0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
205 }
Rich Felker51e2d832011-06-18 19:48:42 -0400206 for (; dso; dso=dso->next) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400207 Sym *sym;
208 if (!dso->global) continue;
Rich Felker2bd05a42012-08-25 17:13:28 -0400209 if (dso->ghashtab) {
210 if (!gh) gh = gnu_hash(s);
211 sym = gnu_lookup(s, gh, dso);
212 } else {
213 if (!h) h = sysv_hash(s);
214 sym = sysv_lookup(s, h, dso);
215 }
Rich Felkerbd174312012-10-06 01:36:11 -0400216 if (!sym) continue;
217 if (!sym->st_shndx)
218 if (need_def || (sym->st_info&0xf) == STT_TLS)
219 continue;
220 if (!sym->st_value)
221 if ((sym->st_info&0xf) != STT_TLS)
222 continue;
223 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue;
224 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue;
225
226 if (def.sym && sym->st_info>>4 == STB_WEAK) continue;
227 def.sym = sym;
228 def.dso = dso;
229 if (sym->st_info>>4 == STB_GLOBAL) break;
Rich Felker51e2d832011-06-18 19:48:42 -0400230 }
Rich Felker427173b2011-07-24 02:19:47 -0400231 return def;
Rich Felker51e2d832011-06-18 19:48:42 -0400232}
233
Rich Felker87d13a42012-08-05 02:49:02 -0400234static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
Rich Felker51e2d832011-06-18 19:48:42 -0400235{
Rich Felker87d13a42012-08-05 02:49:02 -0400236 unsigned char *base = dso->base;
237 Sym *syms = dso->syms;
238 char *strings = dso->strings;
Rich Felker51e2d832011-06-18 19:48:42 -0400239 Sym *sym;
240 const char *name;
Rich Felker51e2d832011-06-18 19:48:42 -0400241 void *ctx;
242 int type;
243 int sym_index;
Rich Felker9c748562012-10-04 22:48:33 -0400244 struct symdef def;
Rich Felker51e2d832011-06-18 19:48:42 -0400245
246 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
Rich Felker51e2d832011-06-18 19:48:42 -0400247 type = R_TYPE(rel[1]);
248 sym_index = R_SYM(rel[1]);
249 if (sym_index) {
250 sym = syms + sym_index;
251 name = strings + sym->st_name;
Rich Felker7cb44cd2012-08-05 02:44:32 -0400252 ctx = IS_COPY(type) ? head->next : head;
Rich Felker9c748562012-10-04 22:48:33 -0400253 def = find_sym(ctx, name, IS_PLT(type));
254 if (!def.sym && sym->st_info>>4 != STB_WEAK) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400255 snprintf(errbuf, sizeof errbuf,
256 "Error relocating %s: %s: symbol not found",
Rich Felker87d13a42012-08-05 02:49:02 -0400257 dso->name, name);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400258 if (runtime) longjmp(rtld_fail, 1);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400259 dprintf(2, "%s\n", errbuf);
Rich Felker04109502012-08-18 16:00:23 -0400260 ldso_fail = 1;
261 continue;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400262 }
Rich Felker7d9a5c62012-08-05 14:03:17 -0400263 } else {
Rich Felker9c748562012-10-04 22:48:33 -0400264 sym = 0;
265 def.sym = 0;
266 def.dso = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400267 }
Rich Felker9c748562012-10-04 22:48:33 -0400268 do_single_reloc(dso, base, (void *)(base + rel[0]), type,
269 stride>2 ? rel[2] : 0, sym, sym?sym->st_size:0, def,
270 def.sym?(size_t)(def.dso->base+def.sym->st_value):0);
Rich Felker51e2d832011-06-18 19:48:42 -0400271 }
272}
273
Rich Felker6717e622011-06-28 19:40:14 -0400274/* A huge hack: to make up for the wastefulness of shared libraries
275 * needing at least a page of dirty memory even if they have no global
276 * data, we reclaim the gaps at the beginning and end of writable maps
277 * and "donate" them to the heap by setting up minimal malloc
278 * structures and then freeing them. */
279
280static void reclaim(unsigned char *base, size_t start, size_t end)
281{
282 size_t *a, *z;
283 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
284 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
285 if (start>end || end-start < 4*sizeof(size_t)) return;
286 a = (size_t *)(base + start);
287 z = (size_t *)(base + end);
288 a[-2] = 1;
289 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
290 z[1] = 1;
291 free(a);
292}
293
294static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
295{
296 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
297 if (ph->p_type!=PT_LOAD) continue;
298 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
299 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
300 reclaim(base, ph->p_vaddr+ph->p_memsz,
301 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
302 }
303}
304
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400305static void *map_library(int fd, struct dso *dso)
Rich Felker51e2d832011-06-18 19:48:42 -0400306{
Rich Felker59633c72011-06-25 12:26:08 -0400307 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
Rich Felker51e2d832011-06-18 19:48:42 -0400308 size_t phsize;
309 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
310 size_t this_min, this_max;
311 off_t off_start;
312 Ehdr *eh;
Rich Felker30763fd2013-07-10 14:38:20 -0400313 Phdr *ph, *ph0;
Rich Felker51e2d832011-06-18 19:48:42 -0400314 unsigned prot;
315 unsigned char *map, *base;
316 size_t dyn;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400317 size_t tls_image=0;
Rich Felker51e2d832011-06-18 19:48:42 -0400318 size_t i;
319
320 ssize_t l = read(fd, buf, sizeof buf);
321 if (l<sizeof *eh) return 0;
Rich Felker59633c72011-06-25 12:26:08 -0400322 eh = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400323 phsize = eh->e_phentsize * eh->e_phnum;
324 if (phsize + sizeof *eh > l) return 0;
325 if (eh->e_phoff + phsize > l) {
Rich Felker59633c72011-06-25 12:26:08 -0400326 l = pread(fd, buf+1, phsize, eh->e_phoff);
Rich Felker51e2d832011-06-18 19:48:42 -0400327 if (l != phsize) return 0;
Rich Felker30763fd2013-07-10 14:38:20 -0400328 ph = ph0 = (void *)(buf + 1);
329 } else {
330 ph = ph0 = (void *)((char *)buf + eh->e_phoff);
Rich Felker51e2d832011-06-18 19:48:42 -0400331 }
Rich Felker51e2d832011-06-18 19:48:42 -0400332 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
333 if (ph->p_type == PT_DYNAMIC)
334 dyn = ph->p_vaddr;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400335 if (ph->p_type == PT_TLS) {
336 tls_image = ph->p_vaddr;
337 dso->tls_align = ph->p_align;
338 dso->tls_len = ph->p_filesz;
339 dso->tls_size = ph->p_memsz;
340 }
Rich Felker51e2d832011-06-18 19:48:42 -0400341 if (ph->p_type != PT_LOAD) continue;
342 if (ph->p_vaddr < addr_min) {
343 addr_min = ph->p_vaddr;
344 off_start = ph->p_offset;
345 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
346 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
347 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
348 }
349 if (ph->p_vaddr+ph->p_memsz > addr_max) {
350 addr_max = ph->p_vaddr+ph->p_memsz;
351 }
352 }
353 if (!dyn) return 0;
354 addr_max += PAGE_SIZE-1;
355 addr_max &= -PAGE_SIZE;
356 addr_min &= -PAGE_SIZE;
357 off_start &= -PAGE_SIZE;
358 map_len = addr_max - addr_min + off_start;
359 /* The first time, we map too much, possibly even more than
360 * the length of the file. This is okay because we will not
361 * use the invalid part; we just need to reserve the right
362 * amount of virtual address space to map over later. */
Rich Felkerbf301002011-06-28 14:20:41 -0400363 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
Rich Felker51e2d832011-06-18 19:48:42 -0400364 if (map==MAP_FAILED) return 0;
365 base = map - addr_min;
Rich Felker30763fd2013-07-10 14:38:20 -0400366 dso->phdr = 0;
367 dso->phnum = 0;
368 for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
Rich Felker51e2d832011-06-18 19:48:42 -0400369 if (ph->p_type != PT_LOAD) continue;
Rich Felker30763fd2013-07-10 14:38:20 -0400370 /* Check if the programs headers are in this load segment, and
371 * if so, record the address for use by dl_iterate_phdr. */
372 if (!dso->phdr && eh->e_phoff >= ph->p_offset
373 && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) {
374 dso->phdr = (void *)(base + ph->p_vaddr
375 + (eh->e_phoff-ph->p_offset));
376 dso->phnum = eh->e_phnum;
377 }
Rich Felker51e2d832011-06-18 19:48:42 -0400378 /* Reuse the existing mapping for the lowest-address LOAD */
379 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
380 this_min = ph->p_vaddr & -PAGE_SIZE;
381 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
382 off_start = ph->p_offset & -PAGE_SIZE;
383 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
384 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
385 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400386 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
387 goto error;
Rich Felker51e2d832011-06-18 19:48:42 -0400388 if (ph->p_memsz > ph->p_filesz) {
389 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
390 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
391 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400392 if (pgbrk-(size_t)base < this_max && mmap((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED)
393 goto error;
Rich Felker51e2d832011-06-18 19:48:42 -0400394 }
395 }
Rich Felker9f174132011-06-29 00:29:08 -0400396 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
397 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400398 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
399 goto error;
Rich Felker9f174132011-06-29 00:29:08 -0400400 break;
401 }
Rich Felker30763fd2013-07-10 14:38:20 -0400402 if (!runtime) reclaim_gaps(base, ph0, eh->e_phentsize, eh->e_phnum);
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400403 dso->map = map;
404 dso->map_len = map_len;
405 dso->base = base;
406 dso->dynv = (void *)(base+dyn);
407 if (dso->tls_size) dso->tls_image = (void *)(base+tls_image);
Rich Felker51e2d832011-06-18 19:48:42 -0400408 return map;
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400409error:
410 munmap(map, map_len);
411 return 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400412}
413
Rich Felker8c203ea2013-04-20 11:51:58 -0400414static int path_open(const char *name, const char *s, char *buf, size_t buf_size)
Rich Felker568b8072011-06-25 01:56:34 -0400415{
Rich Felker8c203ea2013-04-20 11:51:58 -0400416 size_t l;
417 int fd;
Rich Felker49388f32011-06-25 17:49:16 -0400418 for (;;) {
Rich Felker8c203ea2013-04-20 11:51:58 -0400419 s += strspn(s, ":\n");
420 l = strcspn(s, ":\n");
421 if (l-1 >= INT_MAX) return -1;
422 if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) >= buf_size)
423 continue;
Rich Felkerf2d08cf2012-09-29 17:59:50 -0400424 if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
Rich Felker49388f32011-06-25 17:49:16 -0400425 s += l;
Rich Felker568b8072011-06-25 01:56:34 -0400426 }
Rich Felker568b8072011-06-25 01:56:34 -0400427}
428
Rich Felkerc82f4a32012-01-23 00:57:38 -0500429static void decode_dyn(struct dso *p)
430{
431 size_t dyn[DYN_CNT] = {0};
432 decode_vec(p->dynv, dyn, DYN_CNT);
433 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500434 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
Rich Felker2bd05a42012-08-25 17:13:28 -0400435 if (dyn[0]&(1<<DT_HASH))
436 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
437 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
438 p->ghashtab = (void *)(p->base + *dyn);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500439}
440
Rich Felker51e2d832011-06-18 19:48:42 -0400441static struct dso *load_library(const char *name)
442{
Rich Felker5c1909a2012-05-27 16:01:44 -0400443 char buf[2*NAME_MAX+2];
Rich Felker0420b872012-07-11 01:41:20 -0400444 const char *pathname;
Rich Felker1d7c4f82012-12-15 23:34:08 -0500445 unsigned char *map;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400446 struct dso *p, temp_dso = {0};
Rich Felker51e2d832011-06-18 19:48:42 -0400447 int fd;
448 struct stat st;
Rich Felkerdcd60372012-10-05 11:51:50 -0400449 size_t alloc_size;
450 int n_th = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400451
452 /* Catch and block attempts to reload the implementation itself */
453 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
454 static const char *rp, reserved[] =
455 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
456 char *z = strchr(name, '.');
457 if (z) {
458 size_t l = z-name;
459 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
460 if (*rp) {
Rich Felkere23d3582012-10-13 23:25:20 -0400461 if (!ldso->prev) {
462 tail->next = ldso;
463 ldso->prev = tail;
464 tail = ldso->next ? ldso->next : ldso;
Rich Felker51e2d832011-06-18 19:48:42 -0400465 }
Rich Felkere23d3582012-10-13 23:25:20 -0400466 return ldso;
Rich Felker51e2d832011-06-18 19:48:42 -0400467 }
468 }
469 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400470 if (strchr(name, '/')) {
Rich Felker0420b872012-07-11 01:41:20 -0400471 pathname = name;
Rich Felkerf2d08cf2012-09-29 17:59:50 -0400472 fd = open(name, O_RDONLY|O_CLOEXEC);
Rich Felker51e2d832011-06-18 19:48:42 -0400473 } else {
Rich Felker0420b872012-07-11 01:41:20 -0400474 /* Search for the name to see if it's already loaded */
475 for (p=head->next; p; p=p->next) {
476 if (p->shortname && !strcmp(p->shortname, name)) {
477 p->refcnt++;
478 return p;
479 }
480 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400481 if (strlen(name) > NAME_MAX) return 0;
Rich Felker568b8072011-06-25 01:56:34 -0400482 fd = -1;
Rich Felker5c1909a2012-05-27 16:01:44 -0400483 if (r_path) fd = path_open(name, r_path, buf, sizeof buf);
484 if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
Rich Felker568b8072011-06-25 01:56:34 -0400485 if (fd < 0) {
486 if (!sys_path) {
Rich Felkerf389c492013-07-18 19:29:44 -0400487 char *prefix = 0;
488 size_t prefix_len;
489 if (ldso->name[0]=='/') {
490 char *s, *t, *z;
491 for (s=t=z=ldso->name; *s; s++)
492 if (*s=='/') z=t, t=s;
493 prefix_len = z-ldso->name;
494 if (prefix_len < PATH_MAX)
495 prefix = ldso->name;
496 }
497 if (!prefix) {
498 prefix = "";
499 prefix_len = 0;
500 }
501 char etc_ldso_path[prefix_len + 1
502 + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"];
503 snprintf(etc_ldso_path, sizeof etc_ldso_path,
504 "%.*s/etc/ld-musl-" LDSO_ARCH ".path",
505 (int)prefix_len, prefix);
506 FILE *f = fopen(etc_ldso_path, "rbe");
Rich Felker568b8072011-06-25 01:56:34 -0400507 if (f) {
Rich Felker11bc1732013-06-26 10:17:29 -0400508 if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) {
Rich Felker59b481d2013-06-26 10:51:36 -0400509 free(sys_path);
Rich Felker11bc1732013-06-26 10:17:29 -0400510 sys_path = "";
Rich Felker65465102012-11-09 13:49:40 -0500511 }
Rich Felker568b8072011-06-25 01:56:34 -0400512 fclose(f);
513 }
514 }
Rich Felker40d5f7e2012-11-08 22:41:16 -0500515 if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib";
516 fd = path_open(name, sys_path, buf, sizeof buf);
Rich Felker51e2d832011-06-18 19:48:42 -0400517 }
Rich Felker0420b872012-07-11 01:41:20 -0400518 pathname = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400519 }
520 if (fd < 0) return 0;
521 if (fstat(fd, &st) < 0) {
522 close(fd);
523 return 0;
524 }
525 for (p=head->next; p; p=p->next) {
526 if (p->dev == st.st_dev && p->ino == st.st_ino) {
Rich Felker0420b872012-07-11 01:41:20 -0400527 /* If this library was previously loaded with a
528 * pathname but a search found the same inode,
529 * setup its shortname so it can be found by name. */
Rich Felker5f88c0e2012-10-05 12:09:54 -0400530 if (!p->shortname && pathname != name)
531 p->shortname = strrchr(p->name, '/')+1;
Rich Felker51e2d832011-06-18 19:48:42 -0400532 close(fd);
533 p->refcnt++;
534 return p;
535 }
536 }
Rich Felker4d07e552013-01-23 22:07:45 -0500537 map = noload ? 0 : map_library(fd, &temp_dso);
Rich Felker51e2d832011-06-18 19:48:42 -0400538 close(fd);
539 if (!map) return 0;
Rich Felkerdcd60372012-10-05 11:51:50 -0400540
541 /* Allocate storage for the new DSO. When there is TLS, this
542 * storage must include a reservation for all pre-existing
543 * threads to obtain copies of both the new TLS, and an
544 * extended DTV capable of storing an additional slot for
545 * the newly-loaded DSO. */
546 alloc_size = sizeof *p + strlen(pathname) + 1;
547 if (runtime && temp_dso.tls_image) {
548 size_t per_th = temp_dso.tls_size + temp_dso.tls_align
549 + sizeof(void *) * (tls_cnt+3);
Rich Felkere23d3582012-10-13 23:25:20 -0400550 n_th = libc.threads_minus_1 + 1;
Rich Felkerdcd60372012-10-05 11:51:50 -0400551 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
552 else alloc_size += n_th * per_th;
553 }
554 p = calloc(1, alloc_size);
Rich Felker51e2d832011-06-18 19:48:42 -0400555 if (!p) {
Rich Felker74025c82013-02-02 00:59:25 -0500556 munmap(map, temp_dso.map_len);
Rich Felker51e2d832011-06-18 19:48:42 -0400557 return 0;
558 }
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400559 memcpy(p, &temp_dso, sizeof temp_dso);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500560 decode_dyn(p);
Rich Felker51e2d832011-06-18 19:48:42 -0400561 p->dev = st.st_dev;
562 p->ino = st.st_ino;
Rich Felker51e2d832011-06-18 19:48:42 -0400563 p->refcnt = 1;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400564 p->name = p->buf;
Rich Felker0420b872012-07-11 01:41:20 -0400565 strcpy(p->name, pathname);
566 /* Add a shortname only if name arg was not an explicit pathname. */
567 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
Rich Felkerdcd60372012-10-05 11:51:50 -0400568 if (p->tls_image) {
Rich Felker017bf142012-10-07 20:19:11 -0400569 if (runtime && !__pthread_self_init()) {
Rich Felker74025c82013-02-02 00:59:25 -0500570 munmap(map, p->map_len);
Rich Felker92e1cd92012-10-06 16:56:35 -0400571 free(p);
Rich Felker92e1cd92012-10-06 16:56:35 -0400572 return 0;
573 }
Rich Felkerdcd60372012-10-05 11:51:50 -0400574 p->tls_id = ++tls_cnt;
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400575 tls_align = MAXP2(tls_align, p->tls_align);
Rich Felker9ec42832012-10-15 18:51:53 -0400576#ifdef TLS_ABOVE_TP
577 p->tls_offset = tls_offset + ( (tls_align-1) &
578 -(tls_offset + (uintptr_t)p->tls_image) );
579 tls_offset += p->tls_size;
580#else
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400581 tls_offset += p->tls_size + p->tls_align - 1;
582 tls_offset -= (tls_offset + (uintptr_t)p->tls_image)
583 & (p->tls_align-1);
584 p->tls_offset = tls_offset;
Rich Felker9ec42832012-10-15 18:51:53 -0400585#endif
Rich Felkerdcd60372012-10-05 11:51:50 -0400586 p->new_dtv = (void *)(-sizeof(size_t) &
587 (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t)));
588 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1));
589 }
Rich Felker51e2d832011-06-18 19:48:42 -0400590
591 tail->next = p;
592 p->prev = tail;
593 tail = p;
594
Rich Felker1d7c4f82012-12-15 23:34:08 -0500595 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base);
Rich Felker5c1909a2012-05-27 16:01:44 -0400596
Rich Felker51e2d832011-06-18 19:48:42 -0400597 return p;
598}
599
600static void load_deps(struct dso *p)
601{
Rich Felker59ab43f2011-06-26 19:23:28 -0400602 size_t i, ndeps=0;
603 struct dso ***deps = &p->deps, **tmp, *dep;
Rich Felker51e2d832011-06-18 19:48:42 -0400604 for (; p; p=p->next) {
605 for (i=0; p->dynv[i]; i+=2) {
Rich Felker191ebca2011-06-30 23:02:27 -0400606 if (p->dynv[i] != DT_RPATH) continue;
607 r_path = (void *)(p->strings + p->dynv[i+1]);
608 }
609 for (i=0; p->dynv[i]; i+=2) {
Rich Felker51e2d832011-06-18 19:48:42 -0400610 if (p->dynv[i] != DT_NEEDED) continue;
Rich Felker59ab43f2011-06-26 19:23:28 -0400611 dep = load_library(p->strings + p->dynv[i+1]);
612 if (!dep) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400613 snprintf(errbuf, sizeof errbuf,
614 "Error loading shared library %s: %m (needed by %s)",
Rich Felker6b3d5e52011-06-26 17:39:17 -0400615 p->strings + p->dynv[i+1], p->name);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400616 if (runtime) longjmp(rtld_fail, 1);
617 dprintf(2, "%s\n", errbuf);
Rich Felker04109502012-08-18 16:00:23 -0400618 ldso_fail = 1;
619 continue;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400620 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400621 if (runtime) {
622 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
623 if (!tmp) longjmp(rtld_fail, 1);
624 tmp[ndeps++] = dep;
625 tmp[ndeps] = 0;
626 *deps = tmp;
627 }
Rich Felker51e2d832011-06-18 19:48:42 -0400628 }
Rich Felker191ebca2011-06-30 23:02:27 -0400629 r_path = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400630 }
631}
632
Rich Felker2719cc82011-08-16 00:24:36 -0400633static void load_preload(char *s)
634{
635 int tmp;
636 char *z;
637 for (z=s; *z; s=z) {
638 for ( ; *s && isspace(*s); s++);
639 for (z=s; *z && !isspace(*z); z++);
640 tmp = *z;
641 *z = 0;
642 load_library(s);
643 *z = tmp;
644 }
645}
646
Rich Felker59ab43f2011-06-26 19:23:28 -0400647static void make_global(struct dso *p)
648{
649 for (; p; p=p->next) p->global = 1;
650}
651
Rich Felker51e2d832011-06-18 19:48:42 -0400652static void reloc_all(struct dso *p)
653{
654 size_t dyn[DYN_CNT] = {0};
655 for (; p; p=p->next) {
656 if (p->relocated) continue;
657 decode_vec(p->dynv, dyn, DYN_CNT);
Rich Felkerbabf8202012-08-05 12:50:26 -0400658#ifdef NEED_ARCH_RELOCS
659 do_arch_relocs(p, head);
660#endif
Rich Felker87d13a42012-08-05 02:49:02 -0400661 do_relocs(p, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
662 2+(dyn[DT_PLTREL]==DT_RELA));
663 do_relocs(p, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], 2);
664 do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
Rich Felker368ba4a2011-06-25 00:18:19 -0400665 p->relocated = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400666 }
667}
668
Rich Felkerc82f4a32012-01-23 00:57:38 -0500669static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
670{
671 for (; cnt--; ph = (void *)((char *)ph + stride))
672 if (ph->p_type == PT_DYNAMIC)
673 return ph->p_vaddr;
674 return 0;
675}
676
Rich Felkerf419bcb2012-08-26 21:09:26 -0400677static void find_map_range(Phdr *ph, size_t cnt, size_t stride, struct dso *p)
678{
679 size_t min_addr = -1, max_addr = 0;
680 for (; cnt--; ph = (void *)((char *)ph + stride)) {
681 if (ph->p_type != PT_LOAD) continue;
682 if (ph->p_vaddr < min_addr)
683 min_addr = ph->p_vaddr;
684 if (ph->p_vaddr+ph->p_memsz > max_addr)
685 max_addr = ph->p_vaddr+ph->p_memsz;
686 }
687 min_addr &= -PAGE_SIZE;
688 max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
689 p->map = p->base + min_addr;
690 p->map_len = max_addr - min_addr;
691}
692
Rich Felkerf4f77c02012-10-05 13:09:09 -0400693static void do_fini()
694{
695 struct dso *p;
696 size_t dyn[DYN_CNT] = {0};
697 for (p=fini_head; p; p=p->fini_next) {
698 if (!p->constructed) continue;
699 decode_vec(p->dynv, dyn, DYN_CNT);
Rich Felkere69ae842013-07-20 18:26:17 -0400700 if (dyn[0] & (1<<DT_FINI_ARRAY)) {
701 size_t n = dyn[DT_FINI_ARRAYSZ]/sizeof(size_t);
Rich Felker1b413572013-07-21 02:35:46 -0400702 size_t *fn = (size_t *)(p->base + dyn[DT_FINI_ARRAY])+n;
703 while (n--) ((void (*)(void))*--fn)();
Rich Felkere69ae842013-07-20 18:26:17 -0400704 }
705 if (dyn[0] & (1<<DT_FINI))
706 ((void (*)(void))(p->base + dyn[DT_FINI]))();
Rich Felkerf4f77c02012-10-05 13:09:09 -0400707 }
708}
709
Rich Felker4ce3cb52012-02-06 14:39:09 -0500710static void do_init_fini(struct dso *p)
711{
712 size_t dyn[DYN_CNT] = {0};
Rich Felkere23d3582012-10-13 23:25:20 -0400713 int need_locking = libc.threads_minus_1;
Rich Felkerf4f77c02012-10-05 13:09:09 -0400714 /* Allow recursive calls that arise when a library calls
715 * dlopen from one of its constructors, but block any
716 * other threads until all ctors have finished. */
717 if (need_locking) pthread_mutex_lock(&init_fini_lock);
Rich Felker4ce3cb52012-02-06 14:39:09 -0500718 for (; p; p=p->prev) {
Rich Felkerf4f77c02012-10-05 13:09:09 -0400719 if (p->constructed) continue;
720 p->constructed = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500721 decode_vec(p->dynv, dyn, DYN_CNT);
Rich Felkere69ae842013-07-20 18:26:17 -0400722 if (dyn[0] & ((1<<DT_FINI) | (1<<DT_FINI_ARRAY))) {
Rich Felkerf4f77c02012-10-05 13:09:09 -0400723 p->fini_next = fini_head;
724 fini_head = p;
725 }
Rich Felker4ce3cb52012-02-06 14:39:09 -0500726 if (dyn[0] & (1<<DT_INIT))
727 ((void (*)(void))(p->base + dyn[DT_INIT]))();
Rich Felkere69ae842013-07-20 18:26:17 -0400728 if (dyn[0] & (1<<DT_INIT_ARRAY)) {
729 size_t n = dyn[DT_INIT_ARRAYSZ]/sizeof(size_t);
730 size_t *fn = (void *)(p->base + dyn[DT_INIT_ARRAY]);
731 while (n--) ((void (*)(void))*fn++)();
732 }
Rich Felker509b50e2013-06-29 02:24:02 -0400733 if (!need_locking && libc.threads_minus_1) {
734 need_locking = 1;
735 pthread_mutex_lock(&init_fini_lock);
736 }
Rich Felker4ce3cb52012-02-06 14:39:09 -0500737 }
Rich Felkerf4f77c02012-10-05 13:09:09 -0400738 if (need_locking) pthread_mutex_unlock(&init_fini_lock);
Rich Felker4ce3cb52012-02-06 14:39:09 -0500739}
740
Rich Felker3ec8d292012-04-25 00:05:42 -0400741void _dl_debug_state(void)
742{
743}
744
Rich Felkerdcd60372012-10-05 11:51:50 -0400745void *__copy_tls(unsigned char *mem)
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400746{
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400747 pthread_t td;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400748 struct dso *p;
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400749
750 if (!tls_cnt) return mem;
751
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400752 void **dtv = (void *)mem;
Rich Felkerdcd60372012-10-05 11:51:50 -0400753 dtv[0] = (void *)tls_cnt;
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400754
Rich Felker9ec42832012-10-15 18:51:53 -0400755#ifdef TLS_ABOVE_TP
756 mem += sizeof(void *) * (tls_cnt+1);
757 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (tls_align-1);
758 td = (pthread_t)mem;
759 mem += sizeof(struct pthread);
760
761 for (p=head; p; p=p->next) {
762 if (!p->tls_id) continue;
763 dtv[p->tls_id] = mem + p->tls_offset;
764 memcpy(dtv[p->tls_id], p->tls_image, p->tls_len);
765 }
766#else
Rich Felkere23d3582012-10-13 23:25:20 -0400767 mem += libc.tls_size - sizeof(struct pthread);
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400768 mem -= (uintptr_t)mem & (tls_align-1);
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400769 td = (pthread_t)mem;
770
771 for (p=head; p; p=p->next) {
Rich Felkerdcd60372012-10-05 11:51:50 -0400772 if (!p->tls_id) continue;
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400773 dtv[p->tls_id] = mem - p->tls_offset;
774 memcpy(dtv[p->tls_id], p->tls_image, p->tls_len);
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400775 }
Rich Felker9ec42832012-10-15 18:51:53 -0400776#endif
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400777 td->dtv = dtv;
778 return td;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400779}
780
Rich Felkerdcd60372012-10-05 11:51:50 -0400781void *__tls_get_addr(size_t *v)
Rich Felker9b153c02012-10-04 21:01:56 -0400782{
783 pthread_t self = __pthread_self();
Rich Felker44b4d092013-06-03 16:35:59 -0400784 if (v[0]<=(size_t)self->dtv[0] && self->dtv[v[0]])
Rich Felkerdcd60372012-10-05 11:51:50 -0400785 return (char *)self->dtv[v[0]]+v[1];
786
787 /* Block signals to make accessing new TLS async-signal-safe */
788 sigset_t set;
Rich Felker00902c72012-10-06 23:57:51 -0400789 pthread_sigmask(SIG_BLOCK, SIGALL_SET, &set);
Rich Felker44b4d092013-06-03 16:35:59 -0400790 if (v[0]<=(size_t)self->dtv[0] && self->dtv[v[0]]) {
Rich Felkerdcd60372012-10-05 11:51:50 -0400791 pthread_sigmask(SIG_SETMASK, &set, 0);
792 return (char *)self->dtv[v[0]]+v[1];
Rich Felker9b153c02012-10-04 21:01:56 -0400793 }
Rich Felkerdcd60372012-10-05 11:51:50 -0400794
795 /* This is safe without any locks held because, if the caller
796 * is able to request the Nth entry of the DTV, the DSO list
797 * must be valid at least that far out and it was synchronized
798 * at program startup or by an already-completed call to dlopen. */
799 struct dso *p;
800 for (p=head; p->tls_id != v[0]; p=p->next);
801
802 /* Get new DTV space from new DSO if needed */
Rich Felker44b4d092013-06-03 16:35:59 -0400803 if (v[0] > (size_t)self->dtv[0]) {
Rich Felkerdcd60372012-10-05 11:51:50 -0400804 void **newdtv = p->new_dtv +
805 (v[0]+1)*sizeof(void *)*a_fetch_add(&p->new_dtv_idx,1);
Rich Felker44b4d092013-06-03 16:35:59 -0400806 memcpy(newdtv, self->dtv,
Rich Felkerdcd60372012-10-05 11:51:50 -0400807 ((size_t)self->dtv[0]+1) * sizeof(void *));
808 newdtv[0] = (void *)v[0];
809 self->dtv = newdtv;
810 }
811
812 /* Get new TLS memory from new DSO */
813 unsigned char *mem = p->new_tls +
814 (p->tls_size + p->tls_align) * a_fetch_add(&p->new_tls_idx,1);
815 mem += ((uintptr_t)p->tls_image - (uintptr_t)mem) & (p->tls_align-1);
816 self->dtv[v[0]] = mem;
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400817 memcpy(mem, p->tls_image, p->tls_len);
Rich Felkerdcd60372012-10-05 11:51:50 -0400818 pthread_sigmask(SIG_SETMASK, &set, 0);
819 return mem + v[1];
Rich Felker9b153c02012-10-04 21:01:56 -0400820}
821
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400822static void update_tls_size()
823{
Rich Felker9ec42832012-10-15 18:51:53 -0400824 libc.tls_size = ALIGN(
825 (1+tls_cnt) * sizeof(void *) +
826 tls_offset +
827 sizeof(struct pthread) +
828 tls_align * 2,
829 tls_align);
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400830}
831
Rich Felkerc82f4a32012-01-23 00:57:38 -0500832void *__dynlink(int argc, char **argv)
Rich Felker51e2d832011-06-18 19:48:42 -0400833{
Rich Felker731e8ff2012-08-25 17:24:46 -0400834 size_t aux[AUX_CNT] = {0};
Rich Felker51e2d832011-06-18 19:48:42 -0400835 size_t i;
836 Phdr *phdr;
Rich Felker6717e622011-06-28 19:40:14 -0400837 Ehdr *ehdr;
Rich Felker6ab444d2011-07-24 00:54:55 -0400838 static struct dso builtin_dsos[3];
Rich Felkera53de812011-07-24 00:26:12 -0400839 struct dso *const app = builtin_dsos+0;
840 struct dso *const lib = builtin_dsos+1;
Rich Felker6ab444d2011-07-24 00:54:55 -0400841 struct dso *const vdso = builtin_dsos+2;
Rich Felker2719cc82011-08-16 00:24:36 -0400842 char *env_preload=0;
Rich Felkerdbcb3ad2012-08-25 17:31:59 -0400843 size_t vdso_base;
Rich Felker2f2f1152012-11-01 23:49:57 -0400844 size_t *auxv;
Rich Felker75863602013-07-21 03:00:54 -0400845 char **envp = argv+argc+1;
Rich Felker51e2d832011-06-18 19:48:42 -0400846
847 /* Find aux vector just past environ[] */
Rich Felker568b8072011-06-25 01:56:34 -0400848 for (i=argc+1; argv[i]; i++)
849 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
850 env_path = argv[i]+16;
Rich Felker2719cc82011-08-16 00:24:36 -0400851 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
852 env_preload = argv[i]+11;
Rich Felker51e2d832011-06-18 19:48:42 -0400853 auxv = (void *)(argv+i+1);
854
855 decode_vec(auxv, aux, AUX_CNT);
856
Rich Felker568b8072011-06-25 01:56:34 -0400857 /* Only trust user/env if kernel says we're not suid/sgid */
858 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
Rich Felkera0458832011-08-16 07:46:42 -0400859 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
Rich Felker568b8072011-06-25 01:56:34 -0400860 env_path = 0;
Rich Felker2719cc82011-08-16 00:24:36 -0400861 env_preload = 0;
Rich Felker568b8072011-06-25 01:56:34 -0400862 }
863
Rich Felker5c1909a2012-05-27 16:01:44 -0400864 /* If the dynamic linker was invoked as a program itself, AT_BASE
865 * will not be set. In that case, we assume the base address is
866 * the start of the page containing the PHDRs; I don't know any
867 * better approach... */
868 if (!aux[AT_BASE]) {
869 aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE;
870 aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0;
871 }
872
Rich Felkerc82f4a32012-01-23 00:57:38 -0500873 /* The dynamic linker load address is passed by the kernel
874 * in the AUX vector, so this is easy. */
875 lib->base = (void *)aux[AT_BASE];
Rich Felker5c1909a2012-05-27 16:01:44 -0400876 lib->name = lib->shortname = "libc.so";
Rich Felkerc82f4a32012-01-23 00:57:38 -0500877 lib->global = 1;
878 ehdr = (void *)lib->base;
Rich Felker18c0e022012-10-31 21:27:48 -0400879 lib->phnum = ehdr->e_phnum;
880 lib->phdr = (void *)(aux[AT_BASE]+ehdr->e_phoff);
881 find_map_range(lib->phdr, ehdr->e_phnum, ehdr->e_phentsize, lib);
882 lib->dynv = (void *)(lib->base + find_dyn(lib->phdr,
883 ehdr->e_phnum, ehdr->e_phentsize));
Rich Felkerc82f4a32012-01-23 00:57:38 -0500884 decode_dyn(lib);
885
Rich Felker5c1909a2012-05-27 16:01:44 -0400886 if (aux[AT_PHDR]) {
Rich Felker649cec52012-07-13 01:31:02 -0400887 size_t interp_off = 0;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400888 size_t tls_image = 0;
Rich Felker5c1909a2012-05-27 16:01:44 -0400889 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
Rich Felker18c0e022012-10-31 21:27:48 -0400890 app->phdr = phdr = (void *)aux[AT_PHDR];
891 app->phnum = aux[AT_PHNUM];
Rich Felker5c1909a2012-05-27 16:01:44 -0400892 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
893 if (phdr->p_type == PT_PHDR)
894 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
Rich Felker649cec52012-07-13 01:31:02 -0400895 else if (phdr->p_type == PT_INTERP)
896 interp_off = (size_t)phdr->p_vaddr;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400897 else if (phdr->p_type == PT_TLS) {
898 tls_image = phdr->p_vaddr;
899 app->tls_len = phdr->p_filesz;
900 app->tls_size = phdr->p_memsz;
901 app->tls_align = phdr->p_align;
902 }
Rich Felker5c1909a2012-05-27 16:01:44 -0400903 }
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400904 if (app->tls_size) app->tls_image = (char *)app->base + tls_image;
Rich Felker649cec52012-07-13 01:31:02 -0400905 if (interp_off) lib->name = (char *)app->base + interp_off;
Rich Felker0420b872012-07-11 01:41:20 -0400906 app->name = argv[0];
Rich Felker5c1909a2012-05-27 16:01:44 -0400907 app->dynv = (void *)(app->base + find_dyn(
908 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
Rich Felkerf419bcb2012-08-26 21:09:26 -0400909 find_map_range((void *)aux[AT_PHDR],
910 aux[AT_PHNUM], aux[AT_PHENT], app);
Rich Felker5c1909a2012-05-27 16:01:44 -0400911 } else {
912 int fd;
913 char *ldname = argv[0];
Rich Felkera617a8e2012-11-01 23:46:39 -0400914 size_t l = strlen(ldname);
Rich Felker5c1909a2012-05-27 16:01:44 -0400915 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
916 *argv++ = (void *)-1;
917 if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1;
918 if (!argv[0]) {
919 dprintf(2, "musl libc/dynamic program loader\n");
920 dprintf(2, "usage: %s pathname%s\n", ldname,
921 ldd_mode ? "" : " [args]");
922 _exit(1);
923 }
924 fd = open(argv[0], O_RDONLY);
925 if (fd < 0) {
926 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
927 _exit(1);
928 }
929 runtime = 1;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400930 ehdr = (void *)map_library(fd, app);
Rich Felker5c1909a2012-05-27 16:01:44 -0400931 if (!ehdr) {
932 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
933 _exit(1);
934 }
935 runtime = 0;
936 close(fd);
Rich Felker649cec52012-07-13 01:31:02 -0400937 lib->name = ldname;
Rich Felker0420b872012-07-11 01:41:20 -0400938 app->name = argv[0];
Rich Felker18c0e022012-10-31 21:27:48 -0400939 app->phnum = ehdr->e_phnum;
940 app->phdr = (void *)(app->base + ehdr->e_phoff);
Rich Felker5c1909a2012-05-27 16:01:44 -0400941 aux[AT_ENTRY] = ehdr->e_entry;
Rich Felkere12fe652012-01-23 02:02:59 -0500942 }
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400943 if (app->tls_size) {
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400944 app->tls_id = tls_cnt = 1;
Rich Felker9ec42832012-10-15 18:51:53 -0400945#ifdef TLS_ABOVE_TP
946 app->tls_offset = 0;
947 tls_offset = app->tls_size
948 + ( -((uintptr_t)app->tls_image + app->tls_size)
949 & (app->tls_align-1) );
950#else
Rich Felkerc62b9f32012-10-14 19:56:50 -0400951 tls_offset = app->tls_offset = app->tls_size
952 + ( -((uintptr_t)app->tls_image + app->tls_size)
953 & (app->tls_align-1) );
Rich Felker9ec42832012-10-15 18:51:53 -0400954#endif
Rich Felkercf3fd3d2012-10-06 01:22:51 -0400955 tls_align = MAXP2(tls_align, app->tls_align);
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400956 }
Rich Felkerc82f4a32012-01-23 00:57:38 -0500957 app->global = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500958 decode_dyn(app);
959
960 /* Attach to vdso, if provided by the kernel */
Rich Felkerdbcb3ad2012-08-25 17:31:59 -0400961 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) {
Rich Felker6ab444d2011-07-24 00:54:55 -0400962 ehdr = (void *)vdso_base;
Rich Felker18c0e022012-10-31 21:27:48 -0400963 vdso->phdr = phdr = (void *)(vdso_base + ehdr->e_phoff);
964 vdso->phnum = ehdr->e_phnum;
Rich Felker6ab444d2011-07-24 00:54:55 -0400965 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
966 if (phdr->p_type == PT_DYNAMIC)
967 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
968 if (phdr->p_type == PT_LOAD)
969 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
970 }
Rich Felker75a31fa2012-11-25 20:56:31 -0500971 vdso->name = "";
972 vdso->shortname = "linux-gate.so.1";
Rich Felker427173b2011-07-24 02:19:47 -0400973 vdso->global = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500974 decode_dyn(vdso);
Rich Felker6ab444d2011-07-24 00:54:55 -0400975 vdso->prev = lib;
976 lib->next = vdso;
977 }
978
Rich Felkerc82f4a32012-01-23 00:57:38 -0500979 /* Initial dso chain consists only of the app. We temporarily
980 * append the dynamic linker/libc so we can relocate it, then
981 * restore the initial chain in preparation for loading third
982 * party libraries (preload/needed). */
983 head = tail = app;
Rich Felkere23d3582012-10-13 23:25:20 -0400984 ldso = lib;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500985 app->next = lib;
986 reloc_all(lib);
987 app->next = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400988
Rich Felkerc82f4a32012-01-23 00:57:38 -0500989 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
Rich Felker51e2d832011-06-18 19:48:42 -0400990
Rich Felkerc82f4a32012-01-23 00:57:38 -0500991 /* Donate unused parts of app and library mapping to malloc */
Rich Felkera53de812011-07-24 00:26:12 -0400992 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
993 ehdr = (void *)lib->base;
994 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
Rich Felker6717e622011-06-28 19:40:14 -0400995 ehdr->e_phentsize, ehdr->e_phnum);
996
Rich Felkerc82f4a32012-01-23 00:57:38 -0500997 /* Load preload/needed libraries, add their symbols to the global
Rich Felkerfd7015d2012-01-23 18:32:40 -0500998 * namespace, and perform all remaining relocations. The main
999 * program must be relocated LAST since it may contain copy
1000 * relocations which depend on libraries' relocations. */
Rich Felker2719cc82011-08-16 00:24:36 -04001001 if (env_preload) load_preload(env_preload);
Rich Felkerc82f4a32012-01-23 00:57:38 -05001002 load_deps(app);
1003 make_global(app);
Rich Felker9c748562012-10-04 22:48:33 -04001004
Rich Felkercf3fd3d2012-10-06 01:22:51 -04001005 reloc_all(app->next);
1006 reloc_all(app);
1007
1008 update_tls_size();
Rich Felker9c748562012-10-04 22:48:33 -04001009 if (tls_cnt) {
Rich Felkere23d3582012-10-13 23:25:20 -04001010 void *mem = mmap(0, libc.tls_size, PROT_READ|PROT_WRITE,
Rich Felker9c748562012-10-04 22:48:33 -04001011 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1012 if (mem==MAP_FAILED ||
Rich Felkerdcd60372012-10-05 11:51:50 -04001013 !__install_initial_tls(__copy_tls(mem))) {
Rich Felker9c748562012-10-04 22:48:33 -04001014 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
Rich Felkere23d3582012-10-13 23:25:20 -04001015 argv[0], libc.tls_size);
Rich Felker9c748562012-10-04 22:48:33 -04001016 _exit(127);
1017 }
Rich Felker9c748562012-10-04 22:48:33 -04001018 }
1019
Rich Felker04109502012-08-18 16:00:23 -04001020 if (ldso_fail) _exit(127);
Rich Felker5c1909a2012-05-27 16:01:44 -04001021 if (ldd_mode) _exit(0);
1022
Rich Felkerc82f4a32012-01-23 00:57:38 -05001023 /* Switch to runtime mode: any further failures in the dynamic
1024 * linker are a reportable failure rather than a fatal startup
1025 * error. If the dynamic loader (dlopen) will not be used, free
1026 * all memory used by the dynamic linker. */
Rich Felkera53de812011-07-24 00:26:12 -04001027 runtime = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -05001028
Rich Felker7d9a5c62012-08-05 14:03:17 -04001029#ifndef DYNAMIC_IS_RO
Rich Felker3ec8d292012-04-25 00:05:42 -04001030 for (i=0; app->dynv[i]; i+=2)
1031 if (app->dynv[i]==DT_DEBUG)
1032 app->dynv[i+1] = (size_t)&debug;
Rich Felker7d9a5c62012-08-05 14:03:17 -04001033#endif
Rich Felker3ec8d292012-04-25 00:05:42 -04001034 debug.ver = 1;
1035 debug.bp = _dl_debug_state;
1036 debug.head = head;
1037 debug.base = lib->base;
1038 debug.state = 0;
1039 _dl_debug_state();
1040
Rich Felker0a96a372012-10-07 21:43:46 -04001041 if (ssp_used) __init_ssp((void *)aux[AT_RANDOM]);
Rich Felker75863602013-07-21 03:00:54 -04001042 __init_libc(envp, argv[0]);
Rich Felkera7936f62012-11-30 17:56:23 -05001043 atexit(do_fini);
Rich Felker75863602013-07-21 03:00:54 -04001044 errno = 0;
Rich Felkera7936f62012-11-30 17:56:23 -05001045 do_init_fini(tail);
Rich Felker75863602013-07-21 03:00:54 -04001046
1047 return (void *)aux[AT_ENTRY];
Rich Felkera7936f62012-11-30 17:56:23 -05001048}
1049
Rich Felker59ab43f2011-06-26 19:23:28 -04001050void *dlopen(const char *file, int mode)
1051{
Rich Felker642b7592012-10-05 01:15:25 -04001052 struct dso *volatile p, *orig_tail, *next;
Rich Felkercf3fd3d2012-10-06 01:22:51 -04001053 size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
Rich Felker59ab43f2011-06-26 19:23:28 -04001054 size_t i;
Rich Felkerf2baf4d2012-02-07 20:31:27 -05001055 int cs;
Rich Felker59ab43f2011-06-26 19:23:28 -04001056
1057 if (!file) return head;
1058
Rich Felkerf2baf4d2012-02-07 20:31:27 -05001059 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker59ab43f2011-06-26 19:23:28 -04001060 pthread_rwlock_wrlock(&lock);
Rich Felkerdcd60372012-10-05 11:51:50 -04001061 __inhibit_ptc();
Rich Felker59ab43f2011-06-26 19:23:28 -04001062
Rich Felkerdcd60372012-10-05 11:51:50 -04001063 p = 0;
1064 orig_tls_cnt = tls_cnt;
Rich Felkercf3fd3d2012-10-06 01:22:51 -04001065 orig_tls_offset = tls_offset;
1066 orig_tls_align = tls_align;
Rich Felker642b7592012-10-05 01:15:25 -04001067 orig_tail = tail;
Rich Felker4d07e552013-01-23 22:07:45 -05001068 noload = mode & RTLD_NOLOAD;
Rich Felker642b7592012-10-05 01:15:25 -04001069
Rich Felker59ab43f2011-06-26 19:23:28 -04001070 if (setjmp(rtld_fail)) {
1071 /* Clean up anything new that was (partially) loaded */
Rich Felkerdcd60372012-10-05 11:51:50 -04001072 if (p && p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -04001073 if (p->deps[i]->global < 0)
1074 p->deps[i]->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -04001075 for (p=orig_tail->next; p; p=next) {
1076 next = p->next;
1077 munmap(p->map, p->map_len);
1078 free(p->deps);
1079 free(p);
1080 }
Rich Felkerdcd60372012-10-05 11:51:50 -04001081 tls_cnt = orig_tls_cnt;
Rich Felkercf3fd3d2012-10-06 01:22:51 -04001082 tls_offset = orig_tls_offset;
1083 tls_align = orig_tls_align;
Rich Felker59ab43f2011-06-26 19:23:28 -04001084 tail = orig_tail;
1085 tail->next = 0;
Rich Felkerf2baf4d2012-02-07 20:31:27 -05001086 p = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -04001087 errflag = 1;
1088 goto end;
Rich Felkera9e85c02012-03-23 00:28:20 -04001089 } else p = load_library(file);
1090
1091 if (!p) {
Rich Felker4d07e552013-01-23 22:07:45 -05001092 snprintf(errbuf, sizeof errbuf, noload ?
1093 "Library %s is not already loaded" :
1094 "Error loading shared library %s: %m",
1095 file);
Rich Felkera9e85c02012-03-23 00:28:20 -04001096 errflag = 1;
Rich Felkerf2baf4d2012-02-07 20:31:27 -05001097 goto end;
Rich Felker59ab43f2011-06-26 19:23:28 -04001098 }
1099
Rich Felker59ab43f2011-06-26 19:23:28 -04001100 /* First load handling */
1101 if (!p->deps) {
1102 load_deps(p);
Rich Felker0e4dae32011-06-26 21:36:44 -04001103 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -04001104 if (!p->deps[i]->global)
1105 p->deps[i]->global = -1;
1106 if (!p->global) p->global = -1;
Rich Felker59ab43f2011-06-26 19:23:28 -04001107 reloc_all(p);
Rich Felker0e4dae32011-06-26 21:36:44 -04001108 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -04001109 if (p->deps[i]->global < 0)
1110 p->deps[i]->global = 0;
1111 if (p->global < 0) p->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -04001112 }
1113
1114 if (mode & RTLD_GLOBAL) {
Rich Felker0e4dae32011-06-26 21:36:44 -04001115 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker59ab43f2011-06-26 19:23:28 -04001116 p->deps[i]->global = 1;
1117 p->global = 1;
1118 }
1119
Rich Felkercf3fd3d2012-10-06 01:22:51 -04001120 update_tls_size();
Rich Felkerdcd60372012-10-05 11:51:50 -04001121
Rich Felker2f2f1152012-11-01 23:49:57 -04001122 if (ssp_used) __init_ssp(libc.auxv);
Rich Felker731e8ff2012-08-25 17:24:46 -04001123
Rich Felker3ec8d292012-04-25 00:05:42 -04001124 _dl_debug_state();
Rich Felkerf4f77c02012-10-05 13:09:09 -04001125 orig_tail = tail;
Rich Felker06933cc2011-06-26 22:09:32 -04001126end:
Rich Felkerdcd60372012-10-05 11:51:50 -04001127 __release_ptc();
Rich Felker18c0e022012-10-31 21:27:48 -04001128 if (p) gencnt++;
Rich Felker59ab43f2011-06-26 19:23:28 -04001129 pthread_rwlock_unlock(&lock);
Rich Felkerf4f77c02012-10-05 13:09:09 -04001130 if (p) do_init_fini(orig_tail);
Rich Felkerf2baf4d2012-02-07 20:31:27 -05001131 pthread_setcancelstate(cs, 0);
Rich Felker59ab43f2011-06-26 19:23:28 -04001132 return p;
1133}
1134
Rich Felker4d982802013-01-16 11:49:00 -05001135static int invalid_dso_handle(void *h)
Rich Felker6468fc92013-01-10 14:05:40 -05001136{
1137 struct dso *p;
1138 for (p=head; p; p=p->next) if (h==p) return 0;
1139 snprintf(errbuf, sizeof errbuf, "Invalid library handle %p", (void *)h);
1140 errflag = 1;
1141 return 1;
1142}
1143
Rich Felker623753a2011-08-16 00:42:13 -04001144static void *do_dlsym(struct dso *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -04001145{
1146 size_t i;
Rich Felker2bd05a42012-08-25 17:13:28 -04001147 uint32_t h = 0, gh = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -04001148 Sym *sym;
Rich Felker9c748562012-10-04 22:48:33 -04001149 if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
Rich Felkerdeb15b32012-10-19 21:41:30 -04001150 if (p == RTLD_DEFAULT) {
1151 p = head;
1152 } else if (p == RTLD_NEXT) {
Rich Felker9c748562012-10-04 22:48:33 -04001153 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
1154 if (!p) p=head;
Rich Felkerdeb15b32012-10-19 21:41:30 -04001155 p = p->next;
Rich Felker9c748562012-10-04 22:48:33 -04001156 }
Rich Felkerdeb15b32012-10-19 21:41:30 -04001157 struct symdef def = find_sym(p, s, 0);
Rich Felker9c748562012-10-04 22:48:33 -04001158 if (!def.sym) goto failed;
Rich Felker0a1c2c12012-10-19 21:57:56 -04001159 if ((def.sym->st_info&0xf) == STT_TLS)
1160 return __tls_get_addr((size_t []){def.dso->tls_id, def.sym->st_value});
Rich Felker9c748562012-10-04 22:48:33 -04001161 return def.dso->base + def.sym->st_value;
Rich Felkera9e85c02012-03-23 00:28:20 -04001162 }
Rich Felker637dd2d2013-01-23 20:21:36 -05001163 if (p != RTLD_DEFAULT && p != RTLD_NEXT && invalid_dso_handle(p))
1164 return 0;
Rich Felker2bd05a42012-08-25 17:13:28 -04001165 if (p->ghashtab) {
1166 gh = gnu_hash(s);
1167 sym = gnu_lookup(s, gh, p);
1168 } else {
1169 h = sysv_hash(s);
1170 sym = sysv_lookup(s, h, p);
1171 }
Rich Felker0a1c2c12012-10-19 21:57:56 -04001172 if (sym && (sym->st_info&0xf) == STT_TLS)
1173 return __tls_get_addr((size_t []){p->tls_id, sym->st_value});
Rich Felker59ab43f2011-06-26 19:23:28 -04001174 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
1175 return p->base + sym->st_value;
1176 if (p->deps) for (i=0; p->deps[i]; i++) {
Rich Felker2bd05a42012-08-25 17:13:28 -04001177 if (p->deps[i]->ghashtab) {
1178 if (!gh) gh = gnu_hash(s);
Rich Felkera5d61992012-08-25 17:40:27 -04001179 sym = gnu_lookup(s, gh, p->deps[i]);
Rich Felker2bd05a42012-08-25 17:13:28 -04001180 } else {
1181 if (!h) h = sysv_hash(s);
1182 sym = sysv_lookup(s, h, p->deps[i]);
1183 }
Rich Felker0a1c2c12012-10-19 21:57:56 -04001184 if (sym && (sym->st_info&0xf) == STT_TLS)
1185 return __tls_get_addr((size_t []){p->deps[i]->tls_id, sym->st_value});
Rich Felker59ab43f2011-06-26 19:23:28 -04001186 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
1187 return p->deps[i]->base + sym->st_value;
1188 }
Rich Felker4027f4e2012-05-04 20:18:18 -04001189failed:
Rich Felkera9e85c02012-03-23 00:28:20 -04001190 errflag = 1;
Rich Felkera5d10eb2012-04-23 12:03:31 -04001191 snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
Rich Felker59ab43f2011-06-26 19:23:28 -04001192 return 0;
1193}
1194
Rich Felkerf419bcb2012-08-26 21:09:26 -04001195int __dladdr(void *addr, Dl_info *info)
1196{
1197 struct dso *p;
1198 Sym *sym;
1199 uint32_t nsym;
1200 char *strings;
1201 size_t i;
1202 void *best = 0;
1203 char *bestname;
1204
1205 pthread_rwlock_rdlock(&lock);
1206 for (p=head; p && (unsigned char *)addr-p->map>p->map_len; p=p->next);
1207 pthread_rwlock_unlock(&lock);
1208
1209 if (!p) return 0;
1210
1211 sym = p->syms;
1212 strings = p->strings;
1213 if (p->hashtab) {
1214 nsym = p->hashtab[1];
1215 } else {
1216 uint32_t *buckets;
1217 uint32_t *hashval;
1218 buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
1219 sym += p->ghashtab[1];
1220 for (i = 0; i < p->ghashtab[0]; i++) {
1221 if (buckets[i] > nsym)
1222 nsym = buckets[i];
1223 }
1224 if (nsym) {
1225 nsym -= p->ghashtab[1];
1226 hashval = buckets + p->ghashtab[0] + nsym;
1227 do nsym++;
1228 while (!(*hashval++ & 1));
1229 }
1230 }
1231
1232 for (; nsym; nsym--, sym++) {
Rich Felkercdc5c742013-01-16 11:47:35 -05001233 if (sym->st_value
Rich Felkerf419bcb2012-08-26 21:09:26 -04001234 && (1<<(sym->st_info&0xf) & OK_TYPES)
1235 && (1<<(sym->st_info>>4) & OK_BINDS)) {
1236 void *symaddr = p->base + sym->st_value;
1237 if (symaddr > addr || symaddr < best)
1238 continue;
1239 best = symaddr;
1240 bestname = strings + sym->st_name;
1241 if (addr == symaddr)
1242 break;
1243 }
1244 }
1245
1246 if (!best) return 0;
1247
1248 info->dli_fname = p->name;
1249 info->dli_fbase = p->base;
1250 info->dli_sname = bestname;
1251 info->dli_saddr = best;
1252
1253 return 1;
1254}
1255
Rich Felker400c5e52012-09-06 22:44:55 -04001256void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
Rich Felker59ab43f2011-06-26 19:23:28 -04001257{
1258 void *res;
1259 pthread_rwlock_rdlock(&lock);
Rich Felker623753a2011-08-16 00:42:13 -04001260 res = do_dlsym(p, s, ra);
Rich Felker59ab43f2011-06-26 19:23:28 -04001261 pthread_rwlock_unlock(&lock);
1262 return res;
1263}
Rich Felker18c0e022012-10-31 21:27:48 -04001264
1265int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data)
1266{
1267 struct dso *current;
1268 struct dl_phdr_info info;
1269 int ret = 0;
1270 for(current = head; current;) {
1271 info.dlpi_addr = (uintptr_t)current->base;
1272 info.dlpi_name = current->name;
1273 info.dlpi_phdr = current->phdr;
1274 info.dlpi_phnum = current->phnum;
1275 info.dlpi_adds = gencnt;
1276 info.dlpi_subs = 0;
1277 info.dlpi_tls_modid = current->tls_id;
1278 info.dlpi_tls_data = current->tls_image;
1279
1280 ret = (callback)(&info, sizeof (info), data);
1281
1282 if (ret != 0) break;
1283
1284 pthread_rwlock_rdlock(&lock);
1285 current = current->next;
1286 pthread_rwlock_unlock(&lock);
1287 }
1288 return ret;
1289}
Rich Felker5a09a532012-02-03 03:16:07 -05001290#else
Rich Felker4d982802013-01-16 11:49:00 -05001291static int invalid_dso_handle(void *h)
Rich Felker6468fc92013-01-10 14:05:40 -05001292{
1293 snprintf(errbuf, sizeof errbuf, "Invalid library handle %p", (void *)h);
1294 errflag = 1;
1295 return 1;
1296}
Rich Felker5a09a532012-02-03 03:16:07 -05001297void *dlopen(const char *file, int mode)
1298{
1299 return 0;
1300}
Rich Felker400c5e52012-09-06 22:44:55 -04001301void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
Rich Felker5a09a532012-02-03 03:16:07 -05001302{
1303 return 0;
1304}
Rich Felkerf419bcb2012-08-26 21:09:26 -04001305int __dladdr (void *addr, Dl_info *info)
1306{
1307 return 0;
1308}
Rich Felker5a09a532012-02-03 03:16:07 -05001309#endif
Rich Felker59ab43f2011-06-26 19:23:28 -04001310
Rich Felker780cbbe2013-06-29 12:46:46 -04001311int __dlinfo(void *dso, int req, void *res)
1312{
1313 if (invalid_dso_handle(dso)) return -1;
1314 if (req != RTLD_DI_LINKMAP) {
1315 snprintf(errbuf, sizeof errbuf, "Unsupported request %d", req);
1316 errflag = 1;
1317 return -1;
1318 }
1319 *(struct link_map **)res = dso;
1320 return 0;
1321}
1322
Rich Felker59ab43f2011-06-26 19:23:28 -04001323char *dlerror()
1324{
Rich Felkera9e85c02012-03-23 00:28:20 -04001325 if (!errflag) return 0;
1326 errflag = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -04001327 return errbuf;
Rich Felker59ab43f2011-06-26 19:23:28 -04001328}
1329
1330int dlclose(void *p)
1331{
Rich Felker6468fc92013-01-10 14:05:40 -05001332 return invalid_dso_handle(p);
Rich Felker59ab43f2011-06-26 19:23:28 -04001333}