blob: d7d6800238fea88d161ebdc2ac0ea8dc0b3cf167 [file] [log] [blame]
Rich Felker51e2d832011-06-18 19:48:42 -04001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <stdint.h>
6#include <elf.h>
7#include <sys/mman.h>
8#include <limits.h>
9#include <stdint.h>
10#include <fcntl.h>
11#include <sys/stat.h>
12#include <errno.h>
13#include <limits.h>
14#include <elf.h>
Rich Felker6b3d5e52011-06-26 17:39:17 -040015#include <setjmp.h>
Rich Felker59ab43f2011-06-26 19:23:28 -040016#include <pthread.h>
Rich Felker2719cc82011-08-16 00:24:36 -040017#include <ctype.h>
Rich Felker59ab43f2011-06-26 19:23:28 -040018#include <dlfcn.h>
Rich Felker51e2d832011-06-18 19:48:42 -040019
Rich Felkera9e85c02012-03-23 00:28:20 -040020static int errflag;
Rich Felkera5d10eb2012-04-23 12:03:31 -040021static char errbuf[128];
Rich Felkera9e85c02012-03-23 00:28:20 -040022
Rich Felkere864a292012-07-11 01:47:30 -040023#ifdef SHARED
Rich Felkera9e85c02012-03-23 00:28:20 -040024
Rich Felker51e2d832011-06-18 19:48:42 -040025#if ULONG_MAX == 0xffffffff
26typedef Elf32_Ehdr Ehdr;
27typedef Elf32_Phdr Phdr;
28typedef Elf32_Sym Sym;
29#define R_TYPE(x) ((x)&255)
30#define R_SYM(x) ((x)>>8)
31#else
32typedef Elf64_Ehdr Ehdr;
33typedef Elf64_Phdr Phdr;
34typedef Elf64_Sym Sym;
35#define R_TYPE(x) ((x)&0xffffffff)
36#define R_SYM(x) ((x)>>32)
37#endif
38
Rich Felker3ec8d292012-04-25 00:05:42 -040039struct debug {
40 int ver;
41 void *head;
42 void (*bp)(void);
43 int state;
44 void *base;
45};
46
47struct dso {
48 unsigned char *base;
49 char *name;
Rich Felker51e2d832011-06-18 19:48:42 -040050 size_t *dynv;
Rich Felker3ec8d292012-04-25 00:05:42 -040051 struct dso *next, *prev;
52
53 int refcnt;
Rich Felker51e2d832011-06-18 19:48:42 -040054 Sym *syms;
Rich Felker596d60c2011-06-18 22:52:01 -040055 uint32_t *hashtab;
Rich Felker2bd05a42012-08-25 17:13:28 -040056 uint32_t *ghashtab;
Rich Felker51e2d832011-06-18 19:48:42 -040057 char *strings;
Rich Felker51e2d832011-06-18 19:48:42 -040058 unsigned char *map;
59 size_t map_len;
60 dev_t dev;
61 ino_t ino;
Rich Felker6343ac82012-06-09 21:20:44 -040062 signed char global;
Rich Felker700a8152012-02-07 20:29:29 -050063 char relocated;
64 char constructed;
Rich Felker59ab43f2011-06-26 19:23:28 -040065 struct dso **deps;
Rich Felker5c1909a2012-05-27 16:01:44 -040066 char *shortname;
Rich Felker6b3d5e52011-06-26 17:39:17 -040067 char buf[];
Rich Felker51e2d832011-06-18 19:48:42 -040068};
69
Rich Felker59f40862012-08-05 13:46:39 -040070#include "reloc.h"
71
Rich Felker58aa5f42012-05-03 20:42:45 -040072void __init_ssp(size_t *);
Rich Felker60872cf2012-04-24 18:07:59 -040073
Rich Felker51e2d832011-06-18 19:48:42 -040074static struct dso *head, *tail, *libc;
Rich Felker191ebca2011-06-30 23:02:27 -040075static char *env_path, *sys_path, *r_path;
Rich Felker59ab43f2011-06-26 19:23:28 -040076static int rtld_used;
Rich Felker60872cf2012-04-24 18:07:59 -040077static int ssp_used;
Rich Felker6b3d5e52011-06-26 17:39:17 -040078static int runtime;
Rich Felker5c1909a2012-05-27 16:01:44 -040079static int ldd_mode;
Rich Felker04109502012-08-18 16:00:23 -040080static int ldso_fail;
Rich Felker6b3d5e52011-06-26 17:39:17 -040081static jmp_buf rtld_fail;
Rich Felker59ab43f2011-06-26 19:23:28 -040082static pthread_rwlock_t lock;
Rich Felker3ec8d292012-04-25 00:05:42 -040083static struct debug debug;
84
85struct debug *_dl_debug_addr = &debug;
Rich Felker51e2d832011-06-18 19:48:42 -040086
Rich Felkera0458832011-08-16 07:46:42 -040087#define AUX_CNT 24
Rich Felker51e2d832011-06-18 19:48:42 -040088#define DYN_CNT 34
89
90static void decode_vec(size_t *v, size_t *a, size_t cnt)
91{
92 memset(a, 0, cnt*sizeof(size_t));
93 for (; v[0]; v+=2) if (v[0]<cnt) {
94 a[0] |= 1ULL<<v[0];
95 a[v[0]] = v[1];
96 }
97}
98
Rich Felker2bd05a42012-08-25 17:13:28 -040099static int search_vec(size_t *v, size_t *r, size_t key)
100{
101 for (; v[0]!=key; v+=2)
102 if (!v[0]) return 0;
103 *r = v[1];
104 return 1;
105}
106
107static uint32_t sysv_hash(const char *s0)
Rich Felker51e2d832011-06-18 19:48:42 -0400108{
Rich Felker2adf2fb2012-01-17 00:34:58 -0500109 const unsigned char *s = (void *)s0;
Rich Felker51e2d832011-06-18 19:48:42 -0400110 uint_fast32_t h = 0;
111 while (*s) {
112 h = 16*h + *s++;
113 h ^= h>>24 & 0xf0;
114 }
115 return h & 0xfffffff;
116}
117
Rich Felker2bd05a42012-08-25 17:13:28 -0400118static uint32_t gnu_hash(const char *s0)
119{
120 const unsigned char *s = (void *)s0;
121 uint_fast32_t h = 5381;
122 for (; *s; s++)
123 h = h*33 + *s;
124 return h;
125}
126
127static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
Rich Felker51e2d832011-06-18 19:48:42 -0400128{
129 size_t i;
Rich Felker05eff012012-08-05 02:38:35 -0400130 Sym *syms = dso->syms;
131 uint32_t *hashtab = dso->hashtab;
132 char *strings = dso->strings;
Rich Felker51e2d832011-06-18 19:48:42 -0400133 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
134 if (!strcmp(s, strings+syms[i].st_name))
135 return syms+i;
136 }
137 return 0;
138}
139
Rich Felker2bd05a42012-08-25 17:13:28 -0400140static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso)
141{
142 Sym *sym;
143 char *strings;
144 uint32_t *hashtab = dso->ghashtab;
145 uint32_t nbuckets = hashtab[0];
146 uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
147 uint32_t h2;
148 uint32_t *hashval;
149 uint32_t n = buckets[h1 % nbuckets];
150
151 if (!n) return 0;
152
153 strings = dso->strings;
154 sym = dso->syms + n;
155 hashval = buckets + nbuckets + (n - hashtab[1]);
156
157 for (h1 |= 1; ; sym++) {
158 h2 = *hashval++;
159 if ((h1 == (h2|1)) && !strcmp(s, strings + sym->st_name))
160 return sym;
161 if (h2 & 1) break;
162 }
163
164 return 0;
165}
166
Rich Felker51e2d832011-06-18 19:48:42 -0400167#define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
Rich Felker427173b2011-07-24 02:19:47 -0400168#define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
Rich Felker51e2d832011-06-18 19:48:42 -0400169
170static void *find_sym(struct dso *dso, const char *s, int need_def)
171{
Rich Felker2bd05a42012-08-25 17:13:28 -0400172 uint32_t h = 0, gh = 0;
Rich Felker427173b2011-07-24 02:19:47 -0400173 void *def = 0;
Rich Felker2bd05a42012-08-25 17:13:28 -0400174 if (dso->ghashtab) {
175 gh = gnu_hash(s);
176 if (gh == 0xf9040207 && !strcmp(s, "dlopen")) rtld_used = 1;
177 if (gh == 0xf4dc4ae && !strcmp(s, "dlsym")) rtld_used = 1;
178 if (gh == 0x1f4039c9 && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
179 } else {
180 h = sysv_hash(s);
181 if (h == 0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1;
182 if (h == 0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1;
183 if (h == 0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
184 }
Rich Felker51e2d832011-06-18 19:48:42 -0400185 for (; dso; dso=dso->next) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400186 Sym *sym;
187 if (!dso->global) continue;
Rich Felker2bd05a42012-08-25 17:13:28 -0400188 if (dso->ghashtab) {
189 if (!gh) gh = gnu_hash(s);
190 sym = gnu_lookup(s, gh, dso);
191 } else {
192 if (!h) h = sysv_hash(s);
193 sym = sysv_lookup(s, h, dso);
194 }
Rich Felker51e2d832011-06-18 19:48:42 -0400195 if (sym && (!need_def || sym->st_shndx) && sym->st_value
Rich Felker427173b2011-07-24 02:19:47 -0400196 && (1<<(sym->st_info&0xf) & OK_TYPES)
197 && (1<<(sym->st_info>>4) & OK_BINDS)) {
Rich Felkere01ac672011-07-25 09:22:05 -0400198 if (def && sym->st_info>>4 == STB_WEAK) continue;
Rich Felker427173b2011-07-24 02:19:47 -0400199 def = dso->base + sym->st_value;
200 if (sym->st_info>>4 == STB_GLOBAL) break;
201 }
Rich Felker51e2d832011-06-18 19:48:42 -0400202 }
Rich Felker427173b2011-07-24 02:19:47 -0400203 return def;
Rich Felker51e2d832011-06-18 19:48:42 -0400204}
205
Rich Felker87d13a42012-08-05 02:49:02 -0400206static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
Rich Felker51e2d832011-06-18 19:48:42 -0400207{
Rich Felker87d13a42012-08-05 02:49:02 -0400208 unsigned char *base = dso->base;
209 Sym *syms = dso->syms;
210 char *strings = dso->strings;
Rich Felker51e2d832011-06-18 19:48:42 -0400211 Sym *sym;
212 const char *name;
213 size_t sym_val, sym_size;
214 size_t *reloc_addr;
215 void *ctx;
216 int type;
217 int sym_index;
218
219 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
220 reloc_addr = (void *)(base + rel[0]);
221 type = R_TYPE(rel[1]);
222 sym_index = R_SYM(rel[1]);
223 if (sym_index) {
224 sym = syms + sym_index;
225 name = strings + sym->st_name;
Rich Felker7cb44cd2012-08-05 02:44:32 -0400226 ctx = IS_COPY(type) ? head->next : head;
Rich Felker32de61e2011-06-25 22:36:21 -0400227 sym_val = (size_t)find_sym(ctx, name, IS_PLT(type));
Rich Felker6b3d5e52011-06-26 17:39:17 -0400228 if (!sym_val && sym->st_info>>4 != STB_WEAK) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400229 snprintf(errbuf, sizeof errbuf,
230 "Error relocating %s: %s: symbol not found",
Rich Felker87d13a42012-08-05 02:49:02 -0400231 dso->name, name);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400232 if (runtime) longjmp(rtld_fail, 1);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400233 dprintf(2, "%s\n", errbuf);
Rich Felker04109502012-08-18 16:00:23 -0400234 ldso_fail = 1;
235 continue;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400236 }
Rich Felker51e2d832011-06-18 19:48:42 -0400237 sym_size = sym->st_size;
Rich Felker7d9a5c62012-08-05 14:03:17 -0400238 } else {
239 sym_val = sym_size = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400240 }
241 do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]);
242 }
243}
244
Rich Felker6717e622011-06-28 19:40:14 -0400245/* A huge hack: to make up for the wastefulness of shared libraries
246 * needing at least a page of dirty memory even if they have no global
247 * data, we reclaim the gaps at the beginning and end of writable maps
248 * and "donate" them to the heap by setting up minimal malloc
249 * structures and then freeing them. */
250
251static void reclaim(unsigned char *base, size_t start, size_t end)
252{
253 size_t *a, *z;
254 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
255 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
256 if (start>end || end-start < 4*sizeof(size_t)) return;
257 a = (size_t *)(base + start);
258 z = (size_t *)(base + end);
259 a[-2] = 1;
260 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
261 z[1] = 1;
262 free(a);
263}
264
265static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
266{
267 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
268 if (ph->p_type!=PT_LOAD) continue;
269 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
270 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
271 reclaim(base, ph->p_vaddr+ph->p_memsz,
272 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
273 }
274}
275
Rich Felker51e2d832011-06-18 19:48:42 -0400276static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp)
277{
Rich Felker59633c72011-06-25 12:26:08 -0400278 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
Rich Felker51e2d832011-06-18 19:48:42 -0400279 size_t phsize;
280 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
281 size_t this_min, this_max;
282 off_t off_start;
283 Ehdr *eh;
284 Phdr *ph;
285 unsigned prot;
286 unsigned char *map, *base;
287 size_t dyn;
288 size_t i;
289
290 ssize_t l = read(fd, buf, sizeof buf);
291 if (l<sizeof *eh) return 0;
Rich Felker59633c72011-06-25 12:26:08 -0400292 eh = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400293 phsize = eh->e_phentsize * eh->e_phnum;
294 if (phsize + sizeof *eh > l) return 0;
295 if (eh->e_phoff + phsize > l) {
Rich Felker59633c72011-06-25 12:26:08 -0400296 l = pread(fd, buf+1, phsize, eh->e_phoff);
Rich Felker51e2d832011-06-18 19:48:42 -0400297 if (l != phsize) return 0;
298 eh->e_phoff = sizeof *eh;
299 }
300 ph = (void *)((char *)buf + eh->e_phoff);
301 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
302 if (ph->p_type == PT_DYNAMIC)
303 dyn = ph->p_vaddr;
304 if (ph->p_type != PT_LOAD) continue;
305 if (ph->p_vaddr < addr_min) {
306 addr_min = ph->p_vaddr;
307 off_start = ph->p_offset;
308 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
309 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
310 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
311 }
312 if (ph->p_vaddr+ph->p_memsz > addr_max) {
313 addr_max = ph->p_vaddr+ph->p_memsz;
314 }
315 }
316 if (!dyn) return 0;
317 addr_max += PAGE_SIZE-1;
318 addr_max &= -PAGE_SIZE;
319 addr_min &= -PAGE_SIZE;
320 off_start &= -PAGE_SIZE;
321 map_len = addr_max - addr_min + off_start;
322 /* The first time, we map too much, possibly even more than
323 * the length of the file. This is okay because we will not
324 * use the invalid part; we just need to reserve the right
325 * amount of virtual address space to map over later. */
Rich Felkerbf301002011-06-28 14:20:41 -0400326 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
Rich Felker51e2d832011-06-18 19:48:42 -0400327 if (map==MAP_FAILED) return 0;
328 base = map - addr_min;
329 ph = (void *)((char *)buf + eh->e_phoff);
330 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
331 if (ph->p_type != PT_LOAD) continue;
332 /* Reuse the existing mapping for the lowest-address LOAD */
333 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
334 this_min = ph->p_vaddr & -PAGE_SIZE;
335 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
336 off_start = ph->p_offset & -PAGE_SIZE;
337 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
338 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
339 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400340 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
341 goto error;
Rich Felker51e2d832011-06-18 19:48:42 -0400342 if (ph->p_memsz > ph->p_filesz) {
343 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
344 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
345 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400346 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)
347 goto error;
Rich Felker51e2d832011-06-18 19:48:42 -0400348 }
349 }
Rich Felker9f174132011-06-29 00:29:08 -0400350 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
351 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400352 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
353 goto error;
Rich Felker9f174132011-06-29 00:29:08 -0400354 break;
355 }
Rich Felker6717e622011-06-28 19:40:14 -0400356 if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
357 eh->e_phentsize, eh->e_phnum);
Rich Felker51e2d832011-06-18 19:48:42 -0400358 *lenp = map_len;
359 *basep = base;
360 *dynp = dyn;
361 return map;
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400362error:
363 munmap(map, map_len);
364 return 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400365}
366
Rich Felker5c1909a2012-05-27 16:01:44 -0400367static int path_open(const char *name, const char *search, char *buf, size_t buf_size)
Rich Felker568b8072011-06-25 01:56:34 -0400368{
Rich Felker49388f32011-06-25 17:49:16 -0400369 const char *s=search, *z;
Rich Felker568b8072011-06-25 01:56:34 -0400370 int l, fd;
Rich Felker49388f32011-06-25 17:49:16 -0400371 for (;;) {
372 while (*s==':') s++;
373 if (!*s) return -1;
Rich Felker568b8072011-06-25 01:56:34 -0400374 z = strchr(s, ':');
375 l = z ? z-s : strlen(s);
Rich Felker5c1909a2012-05-27 16:01:44 -0400376 snprintf(buf, buf_size, "%.*s/%s", l, s, name);
Rich Felker568b8072011-06-25 01:56:34 -0400377 if ((fd = open(buf, O_RDONLY))>=0) return fd;
Rich Felker49388f32011-06-25 17:49:16 -0400378 s += l;
Rich Felker568b8072011-06-25 01:56:34 -0400379 }
Rich Felker568b8072011-06-25 01:56:34 -0400380}
381
Rich Felkerc82f4a32012-01-23 00:57:38 -0500382static void decode_dyn(struct dso *p)
383{
384 size_t dyn[DYN_CNT] = {0};
385 decode_vec(p->dynv, dyn, DYN_CNT);
386 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500387 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
Rich Felker2bd05a42012-08-25 17:13:28 -0400388 if (dyn[0]&(1<<DT_HASH))
389 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
390 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
391 p->ghashtab = (void *)(p->base + *dyn);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500392}
393
Rich Felker51e2d832011-06-18 19:48:42 -0400394static struct dso *load_library(const char *name)
395{
Rich Felker5c1909a2012-05-27 16:01:44 -0400396 char buf[2*NAME_MAX+2];
Rich Felker0420b872012-07-11 01:41:20 -0400397 const char *pathname;
Rich Felker51e2d832011-06-18 19:48:42 -0400398 unsigned char *base, *map;
399 size_t dyno, map_len;
400 struct dso *p;
Rich Felker51e2d832011-06-18 19:48:42 -0400401 int fd;
402 struct stat st;
403
404 /* Catch and block attempts to reload the implementation itself */
405 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
406 static const char *rp, reserved[] =
407 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
408 char *z = strchr(name, '.');
409 if (z) {
410 size_t l = z-name;
411 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
412 if (*rp) {
413 if (!libc->prev) {
414 tail->next = libc;
415 libc->prev = tail;
Rich Felker6ab444d2011-07-24 00:54:55 -0400416 tail = libc->next ? libc->next : libc;
Rich Felker51e2d832011-06-18 19:48:42 -0400417 }
418 return libc;
419 }
420 }
421 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400422 if (strchr(name, '/')) {
Rich Felker0420b872012-07-11 01:41:20 -0400423 pathname = name;
Rich Felker51e2d832011-06-18 19:48:42 -0400424 fd = open(name, O_RDONLY);
425 } else {
Rich Felker0420b872012-07-11 01:41:20 -0400426 /* Search for the name to see if it's already loaded */
427 for (p=head->next; p; p=p->next) {
428 if (p->shortname && !strcmp(p->shortname, name)) {
429 p->refcnt++;
430 return p;
431 }
432 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400433 if (strlen(name) > NAME_MAX) return 0;
Rich Felker568b8072011-06-25 01:56:34 -0400434 fd = -1;
Rich Felker5c1909a2012-05-27 16:01:44 -0400435 if (r_path) fd = path_open(name, r_path, buf, sizeof buf);
436 if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
Rich Felker568b8072011-06-25 01:56:34 -0400437 if (fd < 0) {
438 if (!sys_path) {
439 FILE *f = fopen(ETC_LDSO_PATH, "r");
440 if (f) {
441 if (getline(&sys_path, (size_t[1]){0}, f) > 0)
442 sys_path[strlen(sys_path)-1]=0;
443 fclose(f);
444 }
445 }
Rich Felker5c1909a2012-05-27 16:01:44 -0400446 if (sys_path) fd = path_open(name, sys_path, buf, sizeof buf);
447 else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib", buf, sizeof buf);
Rich Felker51e2d832011-06-18 19:48:42 -0400448 }
Rich Felker0420b872012-07-11 01:41:20 -0400449 pathname = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400450 }
451 if (fd < 0) return 0;
452 if (fstat(fd, &st) < 0) {
453 close(fd);
454 return 0;
455 }
456 for (p=head->next; p; p=p->next) {
457 if (p->dev == st.st_dev && p->ino == st.st_ino) {
Rich Felker0420b872012-07-11 01:41:20 -0400458 /* If this library was previously loaded with a
459 * pathname but a search found the same inode,
460 * setup its shortname so it can be found by name. */
461 if (!p->shortname) p->shortname = strrchr(p->name, '/')+1;
Rich Felker51e2d832011-06-18 19:48:42 -0400462 close(fd);
463 p->refcnt++;
464 return p;
465 }
466 }
467 map = map_library(fd, &map_len, &base, &dyno);
468 close(fd);
469 if (!map) return 0;
Rich Felker0420b872012-07-11 01:41:20 -0400470 p = calloc(1, sizeof *p + strlen(pathname) + 1);
Rich Felker51e2d832011-06-18 19:48:42 -0400471 if (!p) {
472 munmap(map, map_len);
473 return 0;
474 }
475
476 p->map = map;
477 p->map_len = map_len;
478 p->base = base;
479 p->dynv = (void *)(base + dyno);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500480 decode_dyn(p);
Rich Felker51e2d832011-06-18 19:48:42 -0400481
Rich Felker51e2d832011-06-18 19:48:42 -0400482 p->dev = st.st_dev;
483 p->ino = st.st_ino;
Rich Felker51e2d832011-06-18 19:48:42 -0400484 p->refcnt = 1;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400485 p->name = p->buf;
Rich Felker0420b872012-07-11 01:41:20 -0400486 strcpy(p->name, pathname);
487 /* Add a shortname only if name arg was not an explicit pathname. */
488 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
Rich Felker51e2d832011-06-18 19:48:42 -0400489
490 tail->next = p;
491 p->prev = tail;
492 tail = p;
493
Rich Felker0420b872012-07-11 01:41:20 -0400494 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, base);
Rich Felker5c1909a2012-05-27 16:01:44 -0400495
Rich Felker51e2d832011-06-18 19:48:42 -0400496 return p;
497}
498
499static void load_deps(struct dso *p)
500{
Rich Felker59ab43f2011-06-26 19:23:28 -0400501 size_t i, ndeps=0;
502 struct dso ***deps = &p->deps, **tmp, *dep;
Rich Felker51e2d832011-06-18 19:48:42 -0400503 for (; p; p=p->next) {
504 for (i=0; p->dynv[i]; i+=2) {
Rich Felker191ebca2011-06-30 23:02:27 -0400505 if (p->dynv[i] != DT_RPATH) continue;
506 r_path = (void *)(p->strings + p->dynv[i+1]);
507 }
508 for (i=0; p->dynv[i]; i+=2) {
Rich Felker51e2d832011-06-18 19:48:42 -0400509 if (p->dynv[i] != DT_NEEDED) continue;
Rich Felker59ab43f2011-06-26 19:23:28 -0400510 dep = load_library(p->strings + p->dynv[i+1]);
511 if (!dep) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400512 snprintf(errbuf, sizeof errbuf,
513 "Error loading shared library %s: %m (needed by %s)",
Rich Felker6b3d5e52011-06-26 17:39:17 -0400514 p->strings + p->dynv[i+1], p->name);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400515 if (runtime) longjmp(rtld_fail, 1);
516 dprintf(2, "%s\n", errbuf);
Rich Felker04109502012-08-18 16:00:23 -0400517 ldso_fail = 1;
518 continue;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400519 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400520 if (runtime) {
521 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
522 if (!tmp) longjmp(rtld_fail, 1);
523 tmp[ndeps++] = dep;
524 tmp[ndeps] = 0;
525 *deps = tmp;
526 }
Rich Felker51e2d832011-06-18 19:48:42 -0400527 }
Rich Felker191ebca2011-06-30 23:02:27 -0400528 r_path = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400529 }
530}
531
Rich Felker2719cc82011-08-16 00:24:36 -0400532static void load_preload(char *s)
533{
534 int tmp;
535 char *z;
536 for (z=s; *z; s=z) {
537 for ( ; *s && isspace(*s); s++);
538 for (z=s; *z && !isspace(*z); z++);
539 tmp = *z;
540 *z = 0;
541 load_library(s);
542 *z = tmp;
543 }
544}
545
Rich Felker59ab43f2011-06-26 19:23:28 -0400546static void make_global(struct dso *p)
547{
548 for (; p; p=p->next) p->global = 1;
549}
550
Rich Felker51e2d832011-06-18 19:48:42 -0400551static void reloc_all(struct dso *p)
552{
553 size_t dyn[DYN_CNT] = {0};
554 for (; p; p=p->next) {
555 if (p->relocated) continue;
556 decode_vec(p->dynv, dyn, DYN_CNT);
Rich Felkerbabf8202012-08-05 12:50:26 -0400557#ifdef NEED_ARCH_RELOCS
558 do_arch_relocs(p, head);
559#endif
Rich Felker87d13a42012-08-05 02:49:02 -0400560 do_relocs(p, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
561 2+(dyn[DT_PLTREL]==DT_RELA));
562 do_relocs(p, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], 2);
563 do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
Rich Felker368ba4a2011-06-25 00:18:19 -0400564 p->relocated = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400565 }
566}
567
Rich Felkere8dbf002011-06-25 00:47:28 -0400568static void free_all(struct dso *p)
569{
570 struct dso *n;
571 while (p) {
572 n = p->next;
573 if (p->map) free(p);
574 p = n;
575 }
576}
577
Rich Felkerc82f4a32012-01-23 00:57:38 -0500578static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
579{
580 for (; cnt--; ph = (void *)((char *)ph + stride))
581 if (ph->p_type == PT_DYNAMIC)
582 return ph->p_vaddr;
583 return 0;
584}
585
Rich Felker4ce3cb52012-02-06 14:39:09 -0500586static void do_init_fini(struct dso *p)
587{
588 size_t dyn[DYN_CNT] = {0};
589 for (; p; p=p->prev) {
590 if (p->constructed) return;
591 decode_vec(p->dynv, dyn, DYN_CNT);
592 if (dyn[0] & (1<<DT_FINI))
593 atexit((void (*)(void))(p->base + dyn[DT_FINI]));
594 if (dyn[0] & (1<<DT_INIT))
595 ((void (*)(void))(p->base + dyn[DT_INIT]))();
596 p->constructed = 1;
597 }
598}
599
Rich Felker3ec8d292012-04-25 00:05:42 -0400600void _dl_debug_state(void)
601{
602}
603
Rich Felkerc82f4a32012-01-23 00:57:38 -0500604void *__dynlink(int argc, char **argv)
Rich Felker51e2d832011-06-18 19:48:42 -0400605{
606 size_t *auxv, aux[AUX_CNT] = {0};
Rich Felker51e2d832011-06-18 19:48:42 -0400607 size_t i;
608 Phdr *phdr;
Rich Felker6717e622011-06-28 19:40:14 -0400609 Ehdr *ehdr;
Rich Felker6ab444d2011-07-24 00:54:55 -0400610 static struct dso builtin_dsos[3];
Rich Felkera53de812011-07-24 00:26:12 -0400611 struct dso *const app = builtin_dsos+0;
612 struct dso *const lib = builtin_dsos+1;
Rich Felker6ab444d2011-07-24 00:54:55 -0400613 struct dso *const vdso = builtin_dsos+2;
Rich Felker2719cc82011-08-16 00:24:36 -0400614 char *env_preload=0;
Rich Felker51e2d832011-06-18 19:48:42 -0400615
616 /* Find aux vector just past environ[] */
Rich Felker568b8072011-06-25 01:56:34 -0400617 for (i=argc+1; argv[i]; i++)
618 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
619 env_path = argv[i]+16;
Rich Felker2719cc82011-08-16 00:24:36 -0400620 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
621 env_preload = argv[i]+11;
Rich Felker51e2d832011-06-18 19:48:42 -0400622 auxv = (void *)(argv+i+1);
623
624 decode_vec(auxv, aux, AUX_CNT);
625
Rich Felker568b8072011-06-25 01:56:34 -0400626 /* Only trust user/env if kernel says we're not suid/sgid */
627 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
Rich Felkera0458832011-08-16 07:46:42 -0400628 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
Rich Felker568b8072011-06-25 01:56:34 -0400629 env_path = 0;
Rich Felker2719cc82011-08-16 00:24:36 -0400630 env_preload = 0;
Rich Felker568b8072011-06-25 01:56:34 -0400631 }
632
Rich Felker5c1909a2012-05-27 16:01:44 -0400633 /* If the dynamic linker was invoked as a program itself, AT_BASE
634 * will not be set. In that case, we assume the base address is
635 * the start of the page containing the PHDRs; I don't know any
636 * better approach... */
637 if (!aux[AT_BASE]) {
638 aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE;
639 aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0;
640 }
641
Rich Felkerc82f4a32012-01-23 00:57:38 -0500642 /* The dynamic linker load address is passed by the kernel
643 * in the AUX vector, so this is easy. */
644 lib->base = (void *)aux[AT_BASE];
Rich Felker5c1909a2012-05-27 16:01:44 -0400645 lib->name = lib->shortname = "libc.so";
Rich Felkerc82f4a32012-01-23 00:57:38 -0500646 lib->global = 1;
647 ehdr = (void *)lib->base;
648 lib->dynv = (void *)(lib->base + find_dyn(
649 (void *)(aux[AT_BASE]+ehdr->e_phoff),
650 ehdr->e_phnum, ehdr->e_phentsize));
651 decode_dyn(lib);
652
Rich Felker5c1909a2012-05-27 16:01:44 -0400653 if (aux[AT_PHDR]) {
Rich Felker649cec52012-07-13 01:31:02 -0400654 size_t interp_off = 0;
Rich Felker5c1909a2012-05-27 16:01:44 -0400655 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
656 phdr = (void *)aux[AT_PHDR];
657 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
658 if (phdr->p_type == PT_PHDR)
659 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
Rich Felker649cec52012-07-13 01:31:02 -0400660 else if (phdr->p_type == PT_INTERP)
661 interp_off = (size_t)phdr->p_vaddr;
Rich Felker5c1909a2012-05-27 16:01:44 -0400662 }
Rich Felker649cec52012-07-13 01:31:02 -0400663 if (interp_off) lib->name = (char *)app->base + interp_off;
Rich Felker0420b872012-07-11 01:41:20 -0400664 app->name = argv[0];
Rich Felker5c1909a2012-05-27 16:01:44 -0400665 app->dynv = (void *)(app->base + find_dyn(
666 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
667 } else {
668 int fd;
669 char *ldname = argv[0];
670 size_t dyno, l = strlen(ldname);
671 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
672 *argv++ = (void *)-1;
673 if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1;
674 if (!argv[0]) {
675 dprintf(2, "musl libc/dynamic program loader\n");
676 dprintf(2, "usage: %s pathname%s\n", ldname,
677 ldd_mode ? "" : " [args]");
678 _exit(1);
679 }
680 fd = open(argv[0], O_RDONLY);
681 if (fd < 0) {
682 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
683 _exit(1);
684 }
685 runtime = 1;
686 ehdr = (void *)map_library(fd, &app->map_len, &app->base, &dyno);
687 if (!ehdr) {
688 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
689 _exit(1);
690 }
691 runtime = 0;
692 close(fd);
Rich Felker649cec52012-07-13 01:31:02 -0400693 lib->name = ldname;
Rich Felker0420b872012-07-11 01:41:20 -0400694 app->name = argv[0];
Rich Felker5c1909a2012-05-27 16:01:44 -0400695 app->dynv = (void *)(app->base + dyno);
696 aux[AT_ENTRY] = ehdr->e_entry;
Rich Felkere12fe652012-01-23 02:02:59 -0500697 }
Rich Felkerc82f4a32012-01-23 00:57:38 -0500698 app->global = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500699 app->constructed = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500700 decode_dyn(app);
701
702 /* Attach to vdso, if provided by the kernel */
Rich Felkercf8506a2011-08-16 08:50:03 -0400703 for (i=0; auxv[i]; i+=2) {
Rich Felkerc82f4a32012-01-23 00:57:38 -0500704 size_t vdso_base = auxv[i+1];
705 if (auxv[i] != AT_SYSINFO_EHDR) continue;
Rich Felker6ab444d2011-07-24 00:54:55 -0400706 ehdr = (void *)vdso_base;
707 phdr = (void *)(vdso_base + ehdr->e_phoff);
708 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
709 if (phdr->p_type == PT_DYNAMIC)
710 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
711 if (phdr->p_type == PT_LOAD)
712 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
713 }
Rich Felker5c1909a2012-05-27 16:01:44 -0400714 vdso->name = vdso->shortname = "linux-gate.so.1";
Rich Felker427173b2011-07-24 02:19:47 -0400715 vdso->global = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500716 decode_dyn(vdso);
Rich Felker6ab444d2011-07-24 00:54:55 -0400717 vdso->prev = lib;
718 lib->next = vdso;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500719 break;
Rich Felker6ab444d2011-07-24 00:54:55 -0400720 }
721
Rich Felkerc82f4a32012-01-23 00:57:38 -0500722 /* Initial dso chain consists only of the app. We temporarily
723 * append the dynamic linker/libc so we can relocate it, then
724 * restore the initial chain in preparation for loading third
725 * party libraries (preload/needed). */
726 head = tail = app;
727 libc = lib;
728 app->next = lib;
729 reloc_all(lib);
730 app->next = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400731
Rich Felkerc82f4a32012-01-23 00:57:38 -0500732 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
Rich Felker51e2d832011-06-18 19:48:42 -0400733
Rich Felkerc82f4a32012-01-23 00:57:38 -0500734 /* Donate unused parts of app and library mapping to malloc */
Rich Felkera53de812011-07-24 00:26:12 -0400735 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
736 ehdr = (void *)lib->base;
737 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
Rich Felker6717e622011-06-28 19:40:14 -0400738 ehdr->e_phentsize, ehdr->e_phnum);
739
Rich Felkerc82f4a32012-01-23 00:57:38 -0500740 /* Load preload/needed libraries, add their symbols to the global
Rich Felkerfd7015d2012-01-23 18:32:40 -0500741 * namespace, and perform all remaining relocations. The main
742 * program must be relocated LAST since it may contain copy
743 * relocations which depend on libraries' relocations. */
Rich Felker2719cc82011-08-16 00:24:36 -0400744 if (env_preload) load_preload(env_preload);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500745 load_deps(app);
746 make_global(app);
Rich Felkerfd7015d2012-01-23 18:32:40 -0500747 reloc_all(app->next);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500748 reloc_all(app);
Rich Felker51e2d832011-06-18 19:48:42 -0400749
Rich Felker04109502012-08-18 16:00:23 -0400750 if (ldso_fail) _exit(127);
Rich Felker5c1909a2012-05-27 16:01:44 -0400751 if (ldd_mode) _exit(0);
752
Rich Felkerc82f4a32012-01-23 00:57:38 -0500753 /* Switch to runtime mode: any further failures in the dynamic
754 * linker are a reportable failure rather than a fatal startup
755 * error. If the dynamic loader (dlopen) will not be used, free
756 * all memory used by the dynamic linker. */
Rich Felkera53de812011-07-24 00:26:12 -0400757 runtime = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500758
Rich Felker7d9a5c62012-08-05 14:03:17 -0400759#ifndef DYNAMIC_IS_RO
Rich Felker3ec8d292012-04-25 00:05:42 -0400760 for (i=0; app->dynv[i]; i+=2)
761 if (app->dynv[i]==DT_DEBUG)
762 app->dynv[i+1] = (size_t)&debug;
Rich Felker7d9a5c62012-08-05 14:03:17 -0400763#endif
Rich Felker3ec8d292012-04-25 00:05:42 -0400764 debug.ver = 1;
765 debug.bp = _dl_debug_state;
766 debug.head = head;
767 debug.base = lib->base;
768 debug.state = 0;
769 _dl_debug_state();
770
Rich Felker58aa5f42012-05-03 20:42:45 -0400771 if (ssp_used) __init_ssp(auxv);
772
Rich Felker4ce3cb52012-02-06 14:39:09 -0500773 do_init_fini(tail);
774
Rich Felkera53de812011-07-24 00:26:12 -0400775 if (!rtld_used) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400776 free_all(head);
777 free(sys_path);
Rich Felkera53de812011-07-24 00:26:12 -0400778 reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos);
Rich Felker59ab43f2011-06-26 19:23:28 -0400779 }
Rich Felkere8dbf002011-06-25 00:47:28 -0400780
Rich Felker51e2d832011-06-18 19:48:42 -0400781 errno = 0;
782 return (void *)aux[AT_ENTRY];
783}
Rich Felker59ab43f2011-06-26 19:23:28 -0400784
785void *dlopen(const char *file, int mode)
786{
Rich Felker2fdea172011-07-01 22:40:00 -0400787 struct dso *volatile p, *orig_tail = tail, *next;
Rich Felker59ab43f2011-06-26 19:23:28 -0400788 size_t i;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500789 int cs;
Rich Felker59ab43f2011-06-26 19:23:28 -0400790
791 if (!file) return head;
792
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500793 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker59ab43f2011-06-26 19:23:28 -0400794 pthread_rwlock_wrlock(&lock);
795
796 if (setjmp(rtld_fail)) {
797 /* Clean up anything new that was (partially) loaded */
Rich Felker92ab5d82011-06-26 21:21:04 -0400798 if (p->deps) for (i=0; p->deps[i]; i++)
799 if (p->deps[i]->global < 0)
800 p->deps[i]->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400801 for (p=orig_tail->next; p; p=next) {
802 next = p->next;
803 munmap(p->map, p->map_len);
804 free(p->deps);
805 free(p);
806 }
807 tail = orig_tail;
808 tail->next = 0;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500809 p = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400810 errflag = 1;
811 goto end;
Rich Felkera9e85c02012-03-23 00:28:20 -0400812 } else p = load_library(file);
813
814 if (!p) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400815 snprintf(errbuf, sizeof errbuf,
816 "Error loading shared library %s: %m", file);
Rich Felkera9e85c02012-03-23 00:28:20 -0400817 errflag = 1;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500818 goto end;
Rich Felker59ab43f2011-06-26 19:23:28 -0400819 }
820
Rich Felker59ab43f2011-06-26 19:23:28 -0400821 /* First load handling */
822 if (!p->deps) {
823 load_deps(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400824 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400825 if (!p->deps[i]->global)
826 p->deps[i]->global = -1;
827 if (!p->global) p->global = -1;
Rich Felker59ab43f2011-06-26 19:23:28 -0400828 reloc_all(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400829 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400830 if (p->deps[i]->global < 0)
831 p->deps[i]->global = 0;
832 if (p->global < 0) p->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400833 }
834
835 if (mode & RTLD_GLOBAL) {
Rich Felker0e4dae32011-06-26 21:36:44 -0400836 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker59ab43f2011-06-26 19:23:28 -0400837 p->deps[i]->global = 1;
838 p->global = 1;
839 }
840
Rich Felker3ec8d292012-04-25 00:05:42 -0400841 _dl_debug_state();
842
Rich Felkerce4d97e2012-02-06 17:57:29 -0500843 do_init_fini(tail);
Rich Felker06933cc2011-06-26 22:09:32 -0400844end:
Rich Felker59ab43f2011-06-26 19:23:28 -0400845 pthread_rwlock_unlock(&lock);
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500846 pthread_setcancelstate(cs, 0);
Rich Felker59ab43f2011-06-26 19:23:28 -0400847 return p;
848}
849
Rich Felker623753a2011-08-16 00:42:13 -0400850static void *do_dlsym(struct dso *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400851{
852 size_t i;
Rich Felker2bd05a42012-08-25 17:13:28 -0400853 uint32_t h = 0, gh = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400854 Sym *sym;
Rich Felker623753a2011-08-16 00:42:13 -0400855 if (p == RTLD_NEXT) {
856 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
857 if (!p) p=head;
Rich Felkerd93e0282012-07-07 16:32:27 -0400858 void *res = find_sym(p->next, s, 0);
859 if (!res) goto failed;
860 return res;
Rich Felker623753a2011-08-16 00:42:13 -0400861 }
Rich Felkera9e85c02012-03-23 00:28:20 -0400862 if (p == head || p == RTLD_DEFAULT) {
863 void *res = find_sym(head, s, 0);
Rich Felker4027f4e2012-05-04 20:18:18 -0400864 if (!res) goto failed;
Rich Felkera9e85c02012-03-23 00:28:20 -0400865 return res;
866 }
Rich Felker2bd05a42012-08-25 17:13:28 -0400867 if (p->ghashtab) {
868 gh = gnu_hash(s);
869 sym = gnu_lookup(s, gh, p);
870 } else {
871 h = sysv_hash(s);
872 sym = sysv_lookup(s, h, p);
873 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400874 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
875 return p->base + sym->st_value;
876 if (p->deps) for (i=0; p->deps[i]; i++) {
Rich Felker2bd05a42012-08-25 17:13:28 -0400877 if (p->deps[i]->ghashtab) {
878 if (!gh) gh = gnu_hash(s);
879 sym = gnu_lookup(s, h, p->deps[i]);
880 } else {
881 if (!h) h = sysv_hash(s);
882 sym = sysv_lookup(s, h, p->deps[i]);
883 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400884 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
885 return p->deps[i]->base + sym->st_value;
886 }
Rich Felker4027f4e2012-05-04 20:18:18 -0400887failed:
Rich Felkera9e85c02012-03-23 00:28:20 -0400888 errflag = 1;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400889 snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
Rich Felker59ab43f2011-06-26 19:23:28 -0400890 return 0;
891}
892
Rich Felker623753a2011-08-16 00:42:13 -0400893void *__dlsym(void *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400894{
895 void *res;
896 pthread_rwlock_rdlock(&lock);
Rich Felker623753a2011-08-16 00:42:13 -0400897 res = do_dlsym(p, s, ra);
Rich Felker59ab43f2011-06-26 19:23:28 -0400898 pthread_rwlock_unlock(&lock);
899 return res;
900}
Rich Felker5a09a532012-02-03 03:16:07 -0500901#else
902void *dlopen(const char *file, int mode)
903{
904 return 0;
905}
906void *__dlsym(void *p, const char *s, void *ra)
907{
908 return 0;
909}
910#endif
Rich Felker59ab43f2011-06-26 19:23:28 -0400911
912char *dlerror()
913{
Rich Felkera9e85c02012-03-23 00:28:20 -0400914 if (!errflag) return 0;
915 errflag = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400916 return errbuf;
Rich Felker59ab43f2011-06-26 19:23:28 -0400917}
918
919int dlclose(void *p)
920{
921 return 0;
922}