blob: e07db33af79e03dffc84f4bcf3de006da46dfe8a [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 Felker6b3d5e52011-06-26 17:39:17 -040016#include <setjmp.h>
Rich Felker59ab43f2011-06-26 19:23:28 -040017#include <pthread.h>
Rich Felker2719cc82011-08-16 00:24:36 -040018#include <ctype.h>
Rich Felker59ab43f2011-06-26 19:23:28 -040019#include <dlfcn.h>
Rich Felker8431d792012-10-04 16:35:46 -040020#include "pthread_impl.h"
21#include "libc.h"
22#undef libc
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 Felker3ec8d292012-04-25 00:05:42 -040043struct debug {
44 int ver;
45 void *head;
46 void (*bp)(void);
47 int state;
48 void *base;
49};
50
51struct dso {
52 unsigned char *base;
53 char *name;
Rich Felker51e2d832011-06-18 19:48:42 -040054 size_t *dynv;
Rich Felker3ec8d292012-04-25 00:05:42 -040055 struct dso *next, *prev;
56
57 int refcnt;
Rich Felker51e2d832011-06-18 19:48:42 -040058 Sym *syms;
Rich Felker596d60c2011-06-18 22:52:01 -040059 uint32_t *hashtab;
Rich Felker2bd05a42012-08-25 17:13:28 -040060 uint32_t *ghashtab;
Rich Felker51e2d832011-06-18 19:48:42 -040061 char *strings;
Rich Felker51e2d832011-06-18 19:48:42 -040062 unsigned char *map;
63 size_t map_len;
64 dev_t dev;
65 ino_t ino;
Rich Felker6343ac82012-06-09 21:20:44 -040066 signed char global;
Rich Felker700a8152012-02-07 20:29:29 -050067 char relocated;
68 char constructed;
Rich Felker59ab43f2011-06-26 19:23:28 -040069 struct dso **deps;
Rich Felkerbc6a35f2012-10-04 20:04:13 -040070 void *tls_image;
Rich Felker9c748562012-10-04 22:48:33 -040071 size_t tls_len, tls_size, tls_align, tls_id, tls_offset;
Rich Felkerdcd60372012-10-05 11:51:50 -040072 void **new_dtv;
73 unsigned char *new_tls;
74 int new_dtv_idx, new_tls_idx;
Rich Felkerf4f77c02012-10-05 13:09:09 -040075 struct dso *fini_next;
Rich Felker5c1909a2012-05-27 16:01:44 -040076 char *shortname;
Rich Felker6b3d5e52011-06-26 17:39:17 -040077 char buf[];
Rich Felker51e2d832011-06-18 19:48:42 -040078};
79
Rich Felker9c748562012-10-04 22:48:33 -040080struct symdef {
81 Sym *sym;
82 struct dso *dso;
83};
84
Rich Felker59f40862012-08-05 13:46:39 -040085#include "reloc.h"
86
Rich Felker58aa5f42012-05-03 20:42:45 -040087void __init_ssp(size_t *);
Rich Felkerbc6a35f2012-10-04 20:04:13 -040088void *__install_initial_tls(void *);
Rich Felker60872cf2012-04-24 18:07:59 -040089
Rich Felkerf4f77c02012-10-05 13:09:09 -040090static struct dso *head, *tail, *libc, *fini_head;
Rich Felker191ebca2011-06-30 23:02:27 -040091static char *env_path, *sys_path, *r_path;
Rich Felker60872cf2012-04-24 18:07:59 -040092static int ssp_used;
Rich Felker6b3d5e52011-06-26 17:39:17 -040093static int runtime;
Rich Felker5c1909a2012-05-27 16:01:44 -040094static int ldd_mode;
Rich Felker04109502012-08-18 16:00:23 -040095static int ldso_fail;
Rich Felker6b3d5e52011-06-26 17:39:17 -040096static jmp_buf rtld_fail;
Rich Felker59ab43f2011-06-26 19:23:28 -040097static pthread_rwlock_t lock;
Rich Felker3ec8d292012-04-25 00:05:42 -040098static struct debug debug;
Rich Felker731e8ff2012-08-25 17:24:46 -040099static size_t *auxv;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400100static size_t tls_cnt, tls_size;
Rich Felkerf4f77c02012-10-05 13:09:09 -0400101static pthread_mutex_t init_fini_lock = { ._m_type = PTHREAD_MUTEX_RECURSIVE };
Rich Felker3ec8d292012-04-25 00:05:42 -0400102
103struct debug *_dl_debug_addr = &debug;
Rich Felker51e2d832011-06-18 19:48:42 -0400104
Rich Felkera0458832011-08-16 07:46:42 -0400105#define AUX_CNT 24
Rich Felker51e2d832011-06-18 19:48:42 -0400106#define DYN_CNT 34
107
108static void decode_vec(size_t *v, size_t *a, size_t cnt)
109{
110 memset(a, 0, cnt*sizeof(size_t));
111 for (; v[0]; v+=2) if (v[0]<cnt) {
112 a[0] |= 1ULL<<v[0];
113 a[v[0]] = v[1];
114 }
115}
116
Rich Felker2bd05a42012-08-25 17:13:28 -0400117static int search_vec(size_t *v, size_t *r, size_t key)
118{
119 for (; v[0]!=key; v+=2)
120 if (!v[0]) return 0;
121 *r = v[1];
122 return 1;
123}
124
125static uint32_t sysv_hash(const char *s0)
Rich Felker51e2d832011-06-18 19:48:42 -0400126{
Rich Felker2adf2fb2012-01-17 00:34:58 -0500127 const unsigned char *s = (void *)s0;
Rich Felker51e2d832011-06-18 19:48:42 -0400128 uint_fast32_t h = 0;
129 while (*s) {
130 h = 16*h + *s++;
131 h ^= h>>24 & 0xf0;
132 }
133 return h & 0xfffffff;
134}
135
Rich Felker2bd05a42012-08-25 17:13:28 -0400136static uint32_t gnu_hash(const char *s0)
137{
138 const unsigned char *s = (void *)s0;
139 uint_fast32_t h = 5381;
140 for (; *s; s++)
141 h = h*33 + *s;
142 return h;
143}
144
145static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
Rich Felker51e2d832011-06-18 19:48:42 -0400146{
147 size_t i;
Rich Felker05eff012012-08-05 02:38:35 -0400148 Sym *syms = dso->syms;
149 uint32_t *hashtab = dso->hashtab;
150 char *strings = dso->strings;
Rich Felker51e2d832011-06-18 19:48:42 -0400151 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
152 if (!strcmp(s, strings+syms[i].st_name))
153 return syms+i;
154 }
155 return 0;
156}
157
Rich Felker2bd05a42012-08-25 17:13:28 -0400158static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso)
159{
160 Sym *sym;
161 char *strings;
162 uint32_t *hashtab = dso->ghashtab;
163 uint32_t nbuckets = hashtab[0];
164 uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
165 uint32_t h2;
166 uint32_t *hashval;
167 uint32_t n = buckets[h1 % nbuckets];
168
169 if (!n) return 0;
170
171 strings = dso->strings;
172 sym = dso->syms + n;
173 hashval = buckets + nbuckets + (n - hashtab[1]);
174
175 for (h1 |= 1; ; sym++) {
176 h2 = *hashval++;
177 if ((h1 == (h2|1)) && !strcmp(s, strings + sym->st_name))
178 return sym;
179 if (h2 & 1) break;
180 }
181
182 return 0;
183}
184
Rich Felker9c748562012-10-04 22:48:33 -0400185#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 -0400186#define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
Rich Felker51e2d832011-06-18 19:48:42 -0400187
Rich Felker9c748562012-10-04 22:48:33 -0400188static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
Rich Felker51e2d832011-06-18 19:48:42 -0400189{
Rich Felker2bd05a42012-08-25 17:13:28 -0400190 uint32_t h = 0, gh = 0;
Rich Felker9c748562012-10-04 22:48:33 -0400191 struct symdef def = {0};
Rich Felker2bd05a42012-08-25 17:13:28 -0400192 if (dso->ghashtab) {
193 gh = gnu_hash(s);
Rich Felker2bd05a42012-08-25 17:13:28 -0400194 if (gh == 0x1f4039c9 && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
195 } else {
196 h = sysv_hash(s);
Rich Felker2bd05a42012-08-25 17:13:28 -0400197 if (h == 0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
198 }
Rich Felker51e2d832011-06-18 19:48:42 -0400199 for (; dso; dso=dso->next) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400200 Sym *sym;
201 if (!dso->global) continue;
Rich Felker2bd05a42012-08-25 17:13:28 -0400202 if (dso->ghashtab) {
203 if (!gh) gh = gnu_hash(s);
204 sym = gnu_lookup(s, gh, dso);
205 } else {
206 if (!h) h = sysv_hash(s);
207 sym = sysv_lookup(s, h, dso);
208 }
Rich Felker51e2d832011-06-18 19:48:42 -0400209 if (sym && (!need_def || sym->st_shndx) && sym->st_value
Rich Felker427173b2011-07-24 02:19:47 -0400210 && (1<<(sym->st_info&0xf) & OK_TYPES)
211 && (1<<(sym->st_info>>4) & OK_BINDS)) {
Rich Felker9c748562012-10-04 22:48:33 -0400212 if (def.sym && sym->st_info>>4 == STB_WEAK) continue;
213 def.sym = sym;
214 def.dso = dso;
Rich Felker427173b2011-07-24 02:19:47 -0400215 if (sym->st_info>>4 == STB_GLOBAL) break;
216 }
Rich Felker51e2d832011-06-18 19:48:42 -0400217 }
Rich Felker427173b2011-07-24 02:19:47 -0400218 return def;
Rich Felker51e2d832011-06-18 19:48:42 -0400219}
220
Rich Felker87d13a42012-08-05 02:49:02 -0400221static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
Rich Felker51e2d832011-06-18 19:48:42 -0400222{
Rich Felker87d13a42012-08-05 02:49:02 -0400223 unsigned char *base = dso->base;
224 Sym *syms = dso->syms;
225 char *strings = dso->strings;
Rich Felker51e2d832011-06-18 19:48:42 -0400226 Sym *sym;
227 const char *name;
Rich Felker51e2d832011-06-18 19:48:42 -0400228 void *ctx;
229 int type;
230 int sym_index;
Rich Felker9c748562012-10-04 22:48:33 -0400231 struct symdef def;
Rich Felker51e2d832011-06-18 19:48:42 -0400232
233 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
Rich Felker51e2d832011-06-18 19:48:42 -0400234 type = R_TYPE(rel[1]);
235 sym_index = R_SYM(rel[1]);
236 if (sym_index) {
237 sym = syms + sym_index;
238 name = strings + sym->st_name;
Rich Felker7cb44cd2012-08-05 02:44:32 -0400239 ctx = IS_COPY(type) ? head->next : head;
Rich Felker9c748562012-10-04 22:48:33 -0400240 def = find_sym(ctx, name, IS_PLT(type));
241 if (!def.sym && sym->st_info>>4 != STB_WEAK) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400242 snprintf(errbuf, sizeof errbuf,
243 "Error relocating %s: %s: symbol not found",
Rich Felker87d13a42012-08-05 02:49:02 -0400244 dso->name, name);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400245 if (runtime) longjmp(rtld_fail, 1);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400246 dprintf(2, "%s\n", errbuf);
Rich Felker04109502012-08-18 16:00:23 -0400247 ldso_fail = 1;
248 continue;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400249 }
Rich Felker7d9a5c62012-08-05 14:03:17 -0400250 } else {
Rich Felker9c748562012-10-04 22:48:33 -0400251 sym = 0;
252 def.sym = 0;
253 def.dso = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400254 }
Rich Felker9c748562012-10-04 22:48:33 -0400255 do_single_reloc(dso, base, (void *)(base + rel[0]), type,
256 stride>2 ? rel[2] : 0, sym, sym?sym->st_size:0, def,
257 def.sym?(size_t)(def.dso->base+def.sym->st_value):0);
Rich Felker51e2d832011-06-18 19:48:42 -0400258 }
259}
260
Rich Felker6717e622011-06-28 19:40:14 -0400261/* A huge hack: to make up for the wastefulness of shared libraries
262 * needing at least a page of dirty memory even if they have no global
263 * data, we reclaim the gaps at the beginning and end of writable maps
264 * and "donate" them to the heap by setting up minimal malloc
265 * structures and then freeing them. */
266
267static void reclaim(unsigned char *base, size_t start, size_t end)
268{
269 size_t *a, *z;
270 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
271 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
272 if (start>end || end-start < 4*sizeof(size_t)) return;
273 a = (size_t *)(base + start);
274 z = (size_t *)(base + end);
275 a[-2] = 1;
276 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
277 z[1] = 1;
278 free(a);
279}
280
281static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
282{
283 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
284 if (ph->p_type!=PT_LOAD) continue;
285 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
286 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
287 reclaim(base, ph->p_vaddr+ph->p_memsz,
288 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
289 }
290}
291
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400292static void *map_library(int fd, struct dso *dso)
Rich Felker51e2d832011-06-18 19:48:42 -0400293{
Rich Felker59633c72011-06-25 12:26:08 -0400294 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
Rich Felker51e2d832011-06-18 19:48:42 -0400295 size_t phsize;
296 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
297 size_t this_min, this_max;
298 off_t off_start;
299 Ehdr *eh;
300 Phdr *ph;
301 unsigned prot;
302 unsigned char *map, *base;
303 size_t dyn;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400304 size_t tls_image=0;
Rich Felker51e2d832011-06-18 19:48:42 -0400305 size_t i;
306
307 ssize_t l = read(fd, buf, sizeof buf);
308 if (l<sizeof *eh) return 0;
Rich Felker59633c72011-06-25 12:26:08 -0400309 eh = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400310 phsize = eh->e_phentsize * eh->e_phnum;
311 if (phsize + sizeof *eh > l) return 0;
312 if (eh->e_phoff + phsize > l) {
Rich Felker59633c72011-06-25 12:26:08 -0400313 l = pread(fd, buf+1, phsize, eh->e_phoff);
Rich Felker51e2d832011-06-18 19:48:42 -0400314 if (l != phsize) return 0;
315 eh->e_phoff = sizeof *eh;
316 }
317 ph = (void *)((char *)buf + eh->e_phoff);
318 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
319 if (ph->p_type == PT_DYNAMIC)
320 dyn = ph->p_vaddr;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400321 if (ph->p_type == PT_TLS) {
322 tls_image = ph->p_vaddr;
323 dso->tls_align = ph->p_align;
324 dso->tls_len = ph->p_filesz;
325 dso->tls_size = ph->p_memsz;
326 }
Rich Felker51e2d832011-06-18 19:48:42 -0400327 if (ph->p_type != PT_LOAD) continue;
328 if (ph->p_vaddr < addr_min) {
329 addr_min = ph->p_vaddr;
330 off_start = ph->p_offset;
331 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
332 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
333 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
334 }
335 if (ph->p_vaddr+ph->p_memsz > addr_max) {
336 addr_max = ph->p_vaddr+ph->p_memsz;
337 }
338 }
339 if (!dyn) return 0;
340 addr_max += PAGE_SIZE-1;
341 addr_max &= -PAGE_SIZE;
342 addr_min &= -PAGE_SIZE;
343 off_start &= -PAGE_SIZE;
344 map_len = addr_max - addr_min + off_start;
345 /* The first time, we map too much, possibly even more than
346 * the length of the file. This is okay because we will not
347 * use the invalid part; we just need to reserve the right
348 * amount of virtual address space to map over later. */
Rich Felkerbf301002011-06-28 14:20:41 -0400349 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
Rich Felker51e2d832011-06-18 19:48:42 -0400350 if (map==MAP_FAILED) return 0;
351 base = map - addr_min;
352 ph = (void *)((char *)buf + eh->e_phoff);
353 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
354 if (ph->p_type != PT_LOAD) continue;
355 /* Reuse the existing mapping for the lowest-address LOAD */
356 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
357 this_min = ph->p_vaddr & -PAGE_SIZE;
358 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
359 off_start = ph->p_offset & -PAGE_SIZE;
360 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
361 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
362 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400363 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
364 goto error;
Rich Felker51e2d832011-06-18 19:48:42 -0400365 if (ph->p_memsz > ph->p_filesz) {
366 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
367 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
368 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400369 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)
370 goto error;
Rich Felker51e2d832011-06-18 19:48:42 -0400371 }
372 }
Rich Felker9f174132011-06-29 00:29:08 -0400373 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
374 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400375 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
376 goto error;
Rich Felker9f174132011-06-29 00:29:08 -0400377 break;
378 }
Rich Felker6717e622011-06-28 19:40:14 -0400379 if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
380 eh->e_phentsize, eh->e_phnum);
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400381 dso->map = map;
382 dso->map_len = map_len;
383 dso->base = base;
384 dso->dynv = (void *)(base+dyn);
385 if (dso->tls_size) dso->tls_image = (void *)(base+tls_image);
Rich Felker51e2d832011-06-18 19:48:42 -0400386 return map;
Rich Felkerf7d15dc2012-06-06 11:21:28 -0400387error:
388 munmap(map, map_len);
389 return 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400390}
391
Rich Felker5c1909a2012-05-27 16:01:44 -0400392static int path_open(const char *name, const char *search, char *buf, size_t buf_size)
Rich Felker568b8072011-06-25 01:56:34 -0400393{
Rich Felker49388f32011-06-25 17:49:16 -0400394 const char *s=search, *z;
Rich Felker568b8072011-06-25 01:56:34 -0400395 int l, fd;
Rich Felker49388f32011-06-25 17:49:16 -0400396 for (;;) {
397 while (*s==':') s++;
398 if (!*s) return -1;
Rich Felker568b8072011-06-25 01:56:34 -0400399 z = strchr(s, ':');
400 l = z ? z-s : strlen(s);
Rich Felker5c1909a2012-05-27 16:01:44 -0400401 snprintf(buf, buf_size, "%.*s/%s", l, s, name);
Rich Felkerf2d08cf2012-09-29 17:59:50 -0400402 if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
Rich Felker49388f32011-06-25 17:49:16 -0400403 s += l;
Rich Felker568b8072011-06-25 01:56:34 -0400404 }
Rich Felker568b8072011-06-25 01:56:34 -0400405}
406
Rich Felkerc82f4a32012-01-23 00:57:38 -0500407static void decode_dyn(struct dso *p)
408{
409 size_t dyn[DYN_CNT] = {0};
410 decode_vec(p->dynv, dyn, DYN_CNT);
411 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500412 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
Rich Felker2bd05a42012-08-25 17:13:28 -0400413 if (dyn[0]&(1<<DT_HASH))
414 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
415 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
416 p->ghashtab = (void *)(p->base + *dyn);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500417}
418
Rich Felker51e2d832011-06-18 19:48:42 -0400419static struct dso *load_library(const char *name)
420{
Rich Felker5c1909a2012-05-27 16:01:44 -0400421 char buf[2*NAME_MAX+2];
Rich Felker0420b872012-07-11 01:41:20 -0400422 const char *pathname;
Rich Felker51e2d832011-06-18 19:48:42 -0400423 unsigned char *base, *map;
424 size_t dyno, map_len;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400425 struct dso *p, temp_dso = {0};
Rich Felker51e2d832011-06-18 19:48:42 -0400426 int fd;
427 struct stat st;
Rich Felkerdcd60372012-10-05 11:51:50 -0400428 size_t alloc_size;
429 int n_th = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400430
431 /* Catch and block attempts to reload the implementation itself */
432 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
433 static const char *rp, reserved[] =
434 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
435 char *z = strchr(name, '.');
436 if (z) {
437 size_t l = z-name;
438 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
439 if (*rp) {
440 if (!libc->prev) {
441 tail->next = libc;
442 libc->prev = tail;
Rich Felker6ab444d2011-07-24 00:54:55 -0400443 tail = libc->next ? libc->next : libc;
Rich Felker51e2d832011-06-18 19:48:42 -0400444 }
445 return libc;
446 }
447 }
448 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400449 if (strchr(name, '/')) {
Rich Felker0420b872012-07-11 01:41:20 -0400450 pathname = name;
Rich Felkerf2d08cf2012-09-29 17:59:50 -0400451 fd = open(name, O_RDONLY|O_CLOEXEC);
Rich Felker51e2d832011-06-18 19:48:42 -0400452 } else {
Rich Felker0420b872012-07-11 01:41:20 -0400453 /* Search for the name to see if it's already loaded */
454 for (p=head->next; p; p=p->next) {
455 if (p->shortname && !strcmp(p->shortname, name)) {
456 p->refcnt++;
457 return p;
458 }
459 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400460 if (strlen(name) > NAME_MAX) return 0;
Rich Felker568b8072011-06-25 01:56:34 -0400461 fd = -1;
Rich Felker5c1909a2012-05-27 16:01:44 -0400462 if (r_path) fd = path_open(name, r_path, buf, sizeof buf);
463 if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
Rich Felker568b8072011-06-25 01:56:34 -0400464 if (fd < 0) {
465 if (!sys_path) {
Rich Felkerd712dd32012-09-29 18:14:46 -0400466 FILE *f = fopen(ETC_LDSO_PATH, "rbe");
Rich Felker568b8072011-06-25 01:56:34 -0400467 if (f) {
468 if (getline(&sys_path, (size_t[1]){0}, f) > 0)
469 sys_path[strlen(sys_path)-1]=0;
470 fclose(f);
471 }
472 }
Rich Felker5c1909a2012-05-27 16:01:44 -0400473 if (sys_path) fd = path_open(name, sys_path, buf, sizeof buf);
474 else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib", buf, sizeof buf);
Rich Felker51e2d832011-06-18 19:48:42 -0400475 }
Rich Felker0420b872012-07-11 01:41:20 -0400476 pathname = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400477 }
478 if (fd < 0) return 0;
479 if (fstat(fd, &st) < 0) {
480 close(fd);
481 return 0;
482 }
483 for (p=head->next; p; p=p->next) {
484 if (p->dev == st.st_dev && p->ino == st.st_ino) {
Rich Felker0420b872012-07-11 01:41:20 -0400485 /* If this library was previously loaded with a
486 * pathname but a search found the same inode,
487 * setup its shortname so it can be found by name. */
Rich Felker5f88c0e2012-10-05 12:09:54 -0400488 if (!p->shortname && pathname != name)
489 p->shortname = strrchr(p->name, '/')+1;
Rich Felker51e2d832011-06-18 19:48:42 -0400490 close(fd);
491 p->refcnt++;
492 return p;
493 }
494 }
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400495 map = map_library(fd, &temp_dso);
Rich Felker51e2d832011-06-18 19:48:42 -0400496 close(fd);
497 if (!map) return 0;
Rich Felkerdcd60372012-10-05 11:51:50 -0400498
499 /* Allocate storage for the new DSO. When there is TLS, this
500 * storage must include a reservation for all pre-existing
501 * threads to obtain copies of both the new TLS, and an
502 * extended DTV capable of storing an additional slot for
503 * the newly-loaded DSO. */
504 alloc_size = sizeof *p + strlen(pathname) + 1;
505 if (runtime && temp_dso.tls_image) {
506 size_t per_th = temp_dso.tls_size + temp_dso.tls_align
507 + sizeof(void *) * (tls_cnt+3);
508 n_th = __libc.threads_minus_1 + 1;
509 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
510 else alloc_size += n_th * per_th;
511 }
512 p = calloc(1, alloc_size);
Rich Felker51e2d832011-06-18 19:48:42 -0400513 if (!p) {
514 munmap(map, map_len);
515 return 0;
516 }
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400517 memcpy(p, &temp_dso, sizeof temp_dso);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500518 decode_dyn(p);
Rich Felker51e2d832011-06-18 19:48:42 -0400519 p->dev = st.st_dev;
520 p->ino = st.st_ino;
Rich Felker51e2d832011-06-18 19:48:42 -0400521 p->refcnt = 1;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400522 p->name = p->buf;
Rich Felker0420b872012-07-11 01:41:20 -0400523 strcpy(p->name, pathname);
524 /* Add a shortname only if name arg was not an explicit pathname. */
525 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
Rich Felkerdcd60372012-10-05 11:51:50 -0400526 if (p->tls_image) {
527 p->tls_id = ++tls_cnt;
528 tls_size += p->tls_size + p->tls_align + 8*sizeof(size_t) - 1
529 & -4*sizeof(size_t);
530 p->new_dtv = (void *)(-sizeof(size_t) &
531 (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t)));
532 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1));
533 }
Rich Felker51e2d832011-06-18 19:48:42 -0400534
535 tail->next = p;
536 p->prev = tail;
537 tail = p;
538
Rich Felker0420b872012-07-11 01:41:20 -0400539 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, base);
Rich Felker5c1909a2012-05-27 16:01:44 -0400540
Rich Felker51e2d832011-06-18 19:48:42 -0400541 return p;
542}
543
544static void load_deps(struct dso *p)
545{
Rich Felker59ab43f2011-06-26 19:23:28 -0400546 size_t i, ndeps=0;
547 struct dso ***deps = &p->deps, **tmp, *dep;
Rich Felker51e2d832011-06-18 19:48:42 -0400548 for (; p; p=p->next) {
549 for (i=0; p->dynv[i]; i+=2) {
Rich Felker191ebca2011-06-30 23:02:27 -0400550 if (p->dynv[i] != DT_RPATH) continue;
551 r_path = (void *)(p->strings + p->dynv[i+1]);
552 }
553 for (i=0; p->dynv[i]; i+=2) {
Rich Felker51e2d832011-06-18 19:48:42 -0400554 if (p->dynv[i] != DT_NEEDED) continue;
Rich Felker59ab43f2011-06-26 19:23:28 -0400555 dep = load_library(p->strings + p->dynv[i+1]);
556 if (!dep) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400557 snprintf(errbuf, sizeof errbuf,
558 "Error loading shared library %s: %m (needed by %s)",
Rich Felker6b3d5e52011-06-26 17:39:17 -0400559 p->strings + p->dynv[i+1], p->name);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400560 if (runtime) longjmp(rtld_fail, 1);
561 dprintf(2, "%s\n", errbuf);
Rich Felker04109502012-08-18 16:00:23 -0400562 ldso_fail = 1;
563 continue;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400564 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400565 if (runtime) {
566 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
567 if (!tmp) longjmp(rtld_fail, 1);
568 tmp[ndeps++] = dep;
569 tmp[ndeps] = 0;
570 *deps = tmp;
571 }
Rich Felker51e2d832011-06-18 19:48:42 -0400572 }
Rich Felker191ebca2011-06-30 23:02:27 -0400573 r_path = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400574 }
575}
576
Rich Felker2719cc82011-08-16 00:24:36 -0400577static void load_preload(char *s)
578{
579 int tmp;
580 char *z;
581 for (z=s; *z; s=z) {
582 for ( ; *s && isspace(*s); s++);
583 for (z=s; *z && !isspace(*z); z++);
584 tmp = *z;
585 *z = 0;
586 load_library(s);
587 *z = tmp;
588 }
589}
590
Rich Felker59ab43f2011-06-26 19:23:28 -0400591static void make_global(struct dso *p)
592{
593 for (; p; p=p->next) p->global = 1;
594}
595
Rich Felker51e2d832011-06-18 19:48:42 -0400596static void reloc_all(struct dso *p)
597{
598 size_t dyn[DYN_CNT] = {0};
599 for (; p; p=p->next) {
600 if (p->relocated) continue;
601 decode_vec(p->dynv, dyn, DYN_CNT);
Rich Felkerbabf8202012-08-05 12:50:26 -0400602#ifdef NEED_ARCH_RELOCS
603 do_arch_relocs(p, head);
604#endif
Rich Felker87d13a42012-08-05 02:49:02 -0400605 do_relocs(p, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
606 2+(dyn[DT_PLTREL]==DT_RELA));
607 do_relocs(p, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], 2);
608 do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
Rich Felker368ba4a2011-06-25 00:18:19 -0400609 p->relocated = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400610 }
611}
612
Rich Felkere8dbf002011-06-25 00:47:28 -0400613static void free_all(struct dso *p)
614{
615 struct dso *n;
616 while (p) {
617 n = p->next;
Rich Felker8b28aa92012-08-27 10:07:32 -0400618 if (p->map && p!=libc && p!=head) free(p);
Rich Felkere8dbf002011-06-25 00:47:28 -0400619 p = n;
620 }
621}
622
Rich Felkerc82f4a32012-01-23 00:57:38 -0500623static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
624{
625 for (; cnt--; ph = (void *)((char *)ph + stride))
626 if (ph->p_type == PT_DYNAMIC)
627 return ph->p_vaddr;
628 return 0;
629}
630
Rich Felkerf419bcb2012-08-26 21:09:26 -0400631static void find_map_range(Phdr *ph, size_t cnt, size_t stride, struct dso *p)
632{
633 size_t min_addr = -1, max_addr = 0;
634 for (; cnt--; ph = (void *)((char *)ph + stride)) {
635 if (ph->p_type != PT_LOAD) continue;
636 if (ph->p_vaddr < min_addr)
637 min_addr = ph->p_vaddr;
638 if (ph->p_vaddr+ph->p_memsz > max_addr)
639 max_addr = ph->p_vaddr+ph->p_memsz;
640 }
641 min_addr &= -PAGE_SIZE;
642 max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
643 p->map = p->base + min_addr;
644 p->map_len = max_addr - min_addr;
645}
646
Rich Felkerf4f77c02012-10-05 13:09:09 -0400647static void do_fini()
648{
649 struct dso *p;
650 size_t dyn[DYN_CNT] = {0};
651 for (p=fini_head; p; p=p->fini_next) {
652 if (!p->constructed) continue;
653 decode_vec(p->dynv, dyn, DYN_CNT);
654 ((void (*)(void))(p->base + dyn[DT_FINI]))();
655 }
656}
657
Rich Felker4ce3cb52012-02-06 14:39:09 -0500658static void do_init_fini(struct dso *p)
659{
660 size_t dyn[DYN_CNT] = {0};
Rich Felkerf4f77c02012-10-05 13:09:09 -0400661 int need_locking = __libc.threads_minus_1;
662 /* Allow recursive calls that arise when a library calls
663 * dlopen from one of its constructors, but block any
664 * other threads until all ctors have finished. */
665 if (need_locking) pthread_mutex_lock(&init_fini_lock);
Rich Felker4ce3cb52012-02-06 14:39:09 -0500666 for (; p; p=p->prev) {
Rich Felkerf4f77c02012-10-05 13:09:09 -0400667 if (p->constructed) continue;
668 p->constructed = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500669 decode_vec(p->dynv, dyn, DYN_CNT);
Rich Felkerf4f77c02012-10-05 13:09:09 -0400670 if (dyn[0] & (1<<DT_FINI)) {
671 p->fini_next = fini_head;
672 fini_head = p;
673 }
Rich Felker4ce3cb52012-02-06 14:39:09 -0500674 if (dyn[0] & (1<<DT_INIT))
675 ((void (*)(void))(p->base + dyn[DT_INIT]))();
Rich Felker4ce3cb52012-02-06 14:39:09 -0500676 }
Rich Felkerf4f77c02012-10-05 13:09:09 -0400677 if (need_locking) pthread_mutex_unlock(&init_fini_lock);
Rich Felker4ce3cb52012-02-06 14:39:09 -0500678}
679
Rich Felker3ec8d292012-04-25 00:05:42 -0400680void _dl_debug_state(void)
681{
682}
683
Rich Felkerdcd60372012-10-05 11:51:50 -0400684void *__copy_tls(unsigned char *mem)
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400685{
686 struct dso *p;
687 void **dtv = (void *)mem;
Rich Felkerdcd60372012-10-05 11:51:50 -0400688 dtv[0] = (void *)tls_cnt;
689 mem = (void *)(dtv + tls_cnt + 1);
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400690 for (p=tail; p; p=p->prev) {
Rich Felkerdcd60372012-10-05 11:51:50 -0400691 if (!p->tls_id) continue;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400692 mem += -p->tls_len & (4*sizeof(size_t)-1);
693 mem += ((uintptr_t)p->tls_image - (uintptr_t)mem)
694 & (p->tls_align-1);
695 dtv[p->tls_id] = mem;
696 memcpy(mem, p->tls_image, p->tls_len);
697 mem += p->tls_size;
698 }
699 ((pthread_t)mem)->dtv = dtv;
700 return mem;
701}
702
Rich Felkerdcd60372012-10-05 11:51:50 -0400703void *__tls_get_addr(size_t *v)
Rich Felker9b153c02012-10-04 21:01:56 -0400704{
705 pthread_t self = __pthread_self();
Rich Felkerdcd60372012-10-05 11:51:50 -0400706 if (self->dtv && v[0]<=(size_t)self->dtv[0] && self->dtv[v[0]])
707 return (char *)self->dtv[v[0]]+v[1];
708
709 /* Block signals to make accessing new TLS async-signal-safe */
710 sigset_t set;
711 sigfillset(&set);
712 pthread_sigmask(SIG_BLOCK, &set, &set);
713 if (self->dtv && v[0]<=(size_t)self->dtv[0] && self->dtv[v[0]]) {
714 pthread_sigmask(SIG_SETMASK, &set, 0);
715 return (char *)self->dtv[v[0]]+v[1];
Rich Felker9b153c02012-10-04 21:01:56 -0400716 }
Rich Felkerdcd60372012-10-05 11:51:50 -0400717
718 /* This is safe without any locks held because, if the caller
719 * is able to request the Nth entry of the DTV, the DSO list
720 * must be valid at least that far out and it was synchronized
721 * at program startup or by an already-completed call to dlopen. */
722 struct dso *p;
723 for (p=head; p->tls_id != v[0]; p=p->next);
724
725 /* Get new DTV space from new DSO if needed */
726 if (!self->dtv || v[0] > (size_t)self->dtv[0]) {
727 void **newdtv = p->new_dtv +
728 (v[0]+1)*sizeof(void *)*a_fetch_add(&p->new_dtv_idx,1);
729 if (self->dtv) memcpy(newdtv, self->dtv,
730 ((size_t)self->dtv[0]+1) * sizeof(void *));
731 newdtv[0] = (void *)v[0];
732 self->dtv = newdtv;
733 }
734
735 /* Get new TLS memory from new DSO */
736 unsigned char *mem = p->new_tls +
737 (p->tls_size + p->tls_align) * a_fetch_add(&p->new_tls_idx,1);
738 mem += ((uintptr_t)p->tls_image - (uintptr_t)mem) & (p->tls_align-1);
739 self->dtv[v[0]] = mem;
740 memcpy(mem, p->tls_image, p->tls_size);
741 pthread_sigmask(SIG_SETMASK, &set, 0);
742 return mem + v[1];
Rich Felker9b153c02012-10-04 21:01:56 -0400743}
744
Rich Felkerc82f4a32012-01-23 00:57:38 -0500745void *__dynlink(int argc, char **argv)
Rich Felker51e2d832011-06-18 19:48:42 -0400746{
Rich Felker731e8ff2012-08-25 17:24:46 -0400747 size_t aux[AUX_CNT] = {0};
Rich Felker51e2d832011-06-18 19:48:42 -0400748 size_t i;
749 Phdr *phdr;
Rich Felker6717e622011-06-28 19:40:14 -0400750 Ehdr *ehdr;
Rich Felker6ab444d2011-07-24 00:54:55 -0400751 static struct dso builtin_dsos[3];
Rich Felkera53de812011-07-24 00:26:12 -0400752 struct dso *const app = builtin_dsos+0;
753 struct dso *const lib = builtin_dsos+1;
Rich Felker6ab444d2011-07-24 00:54:55 -0400754 struct dso *const vdso = builtin_dsos+2;
Rich Felker2719cc82011-08-16 00:24:36 -0400755 char *env_preload=0;
Rich Felkerdbcb3ad2012-08-25 17:31:59 -0400756 size_t vdso_base;
Rich Felker51e2d832011-06-18 19:48:42 -0400757
758 /* Find aux vector just past environ[] */
Rich Felker568b8072011-06-25 01:56:34 -0400759 for (i=argc+1; argv[i]; i++)
760 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
761 env_path = argv[i]+16;
Rich Felker2719cc82011-08-16 00:24:36 -0400762 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
763 env_preload = argv[i]+11;
Rich Felker51e2d832011-06-18 19:48:42 -0400764 auxv = (void *)(argv+i+1);
765
766 decode_vec(auxv, aux, AUX_CNT);
767
Rich Felker568b8072011-06-25 01:56:34 -0400768 /* Only trust user/env if kernel says we're not suid/sgid */
769 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
Rich Felkera0458832011-08-16 07:46:42 -0400770 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
Rich Felker568b8072011-06-25 01:56:34 -0400771 env_path = 0;
Rich Felker2719cc82011-08-16 00:24:36 -0400772 env_preload = 0;
Rich Felker568b8072011-06-25 01:56:34 -0400773 }
774
Rich Felker5c1909a2012-05-27 16:01:44 -0400775 /* If the dynamic linker was invoked as a program itself, AT_BASE
776 * will not be set. In that case, we assume the base address is
777 * the start of the page containing the PHDRs; I don't know any
778 * better approach... */
779 if (!aux[AT_BASE]) {
780 aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE;
781 aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0;
782 }
783
Rich Felkerc82f4a32012-01-23 00:57:38 -0500784 /* The dynamic linker load address is passed by the kernel
785 * in the AUX vector, so this is easy. */
786 lib->base = (void *)aux[AT_BASE];
Rich Felker5c1909a2012-05-27 16:01:44 -0400787 lib->name = lib->shortname = "libc.so";
Rich Felkerc82f4a32012-01-23 00:57:38 -0500788 lib->global = 1;
789 ehdr = (void *)lib->base;
Rich Felkerf419bcb2012-08-26 21:09:26 -0400790 find_map_range((void *)(aux[AT_BASE]+ehdr->e_phoff),
791 ehdr->e_phnum, ehdr->e_phentsize, lib);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500792 lib->dynv = (void *)(lib->base + find_dyn(
793 (void *)(aux[AT_BASE]+ehdr->e_phoff),
794 ehdr->e_phnum, ehdr->e_phentsize));
795 decode_dyn(lib);
796
Rich Felker5c1909a2012-05-27 16:01:44 -0400797 if (aux[AT_PHDR]) {
Rich Felker649cec52012-07-13 01:31:02 -0400798 size_t interp_off = 0;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400799 size_t tls_image = 0;
Rich Felker5c1909a2012-05-27 16:01:44 -0400800 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
801 phdr = (void *)aux[AT_PHDR];
802 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
803 if (phdr->p_type == PT_PHDR)
804 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
Rich Felker649cec52012-07-13 01:31:02 -0400805 else if (phdr->p_type == PT_INTERP)
806 interp_off = (size_t)phdr->p_vaddr;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400807 else if (phdr->p_type == PT_TLS) {
808 tls_image = phdr->p_vaddr;
809 app->tls_len = phdr->p_filesz;
810 app->tls_size = phdr->p_memsz;
811 app->tls_align = phdr->p_align;
812 }
Rich Felker5c1909a2012-05-27 16:01:44 -0400813 }
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400814 if (app->tls_size) app->tls_image = (char *)app->base + tls_image;
Rich Felker649cec52012-07-13 01:31:02 -0400815 if (interp_off) lib->name = (char *)app->base + interp_off;
Rich Felker0420b872012-07-11 01:41:20 -0400816 app->name = argv[0];
Rich Felker5c1909a2012-05-27 16:01:44 -0400817 app->dynv = (void *)(app->base + find_dyn(
818 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
Rich Felkerf419bcb2012-08-26 21:09:26 -0400819 find_map_range((void *)aux[AT_PHDR],
820 aux[AT_PHNUM], aux[AT_PHENT], app);
Rich Felker5c1909a2012-05-27 16:01:44 -0400821 } else {
822 int fd;
823 char *ldname = argv[0];
824 size_t dyno, l = strlen(ldname);
825 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
826 *argv++ = (void *)-1;
827 if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1;
828 if (!argv[0]) {
829 dprintf(2, "musl libc/dynamic program loader\n");
830 dprintf(2, "usage: %s pathname%s\n", ldname,
831 ldd_mode ? "" : " [args]");
832 _exit(1);
833 }
834 fd = open(argv[0], O_RDONLY);
835 if (fd < 0) {
836 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
837 _exit(1);
838 }
839 runtime = 1;
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400840 ehdr = (void *)map_library(fd, app);
Rich Felker5c1909a2012-05-27 16:01:44 -0400841 if (!ehdr) {
842 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
843 _exit(1);
844 }
845 runtime = 0;
846 close(fd);
Rich Felker649cec52012-07-13 01:31:02 -0400847 lib->name = ldname;
Rich Felker0420b872012-07-11 01:41:20 -0400848 app->name = argv[0];
Rich Felker5c1909a2012-05-27 16:01:44 -0400849 aux[AT_ENTRY] = ehdr->e_entry;
Rich Felkere12fe652012-01-23 02:02:59 -0500850 }
Rich Felkerbc6a35f2012-10-04 20:04:13 -0400851 if (app->tls_size) {
852 app->tls_id = ++tls_cnt;
853 tls_size += app->tls_size+app->tls_align + 8*sizeof(size_t)-1
854 & -4*sizeof(size_t);
855 }
Rich Felkerc82f4a32012-01-23 00:57:38 -0500856 app->global = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500857 app->constructed = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500858 decode_dyn(app);
859
860 /* Attach to vdso, if provided by the kernel */
Rich Felkerdbcb3ad2012-08-25 17:31:59 -0400861 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) {
Rich Felker6ab444d2011-07-24 00:54:55 -0400862 ehdr = (void *)vdso_base;
863 phdr = (void *)(vdso_base + ehdr->e_phoff);
864 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
865 if (phdr->p_type == PT_DYNAMIC)
866 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
867 if (phdr->p_type == PT_LOAD)
868 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
869 }
Rich Felker5c1909a2012-05-27 16:01:44 -0400870 vdso->name = vdso->shortname = "linux-gate.so.1";
Rich Felker427173b2011-07-24 02:19:47 -0400871 vdso->global = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500872 decode_dyn(vdso);
Rich Felker6ab444d2011-07-24 00:54:55 -0400873 vdso->prev = lib;
874 lib->next = vdso;
875 }
876
Rich Felkerc82f4a32012-01-23 00:57:38 -0500877 /* Initial dso chain consists only of the app. We temporarily
878 * append the dynamic linker/libc so we can relocate it, then
879 * restore the initial chain in preparation for loading third
880 * party libraries (preload/needed). */
881 head = tail = app;
882 libc = lib;
883 app->next = lib;
884 reloc_all(lib);
885 app->next = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400886
Rich Felkerc82f4a32012-01-23 00:57:38 -0500887 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
Rich Felker51e2d832011-06-18 19:48:42 -0400888
Rich Felkerc82f4a32012-01-23 00:57:38 -0500889 /* Donate unused parts of app and library mapping to malloc */
Rich Felkera53de812011-07-24 00:26:12 -0400890 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
891 ehdr = (void *)lib->base;
892 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
Rich Felker6717e622011-06-28 19:40:14 -0400893 ehdr->e_phentsize, ehdr->e_phnum);
894
Rich Felkerc82f4a32012-01-23 00:57:38 -0500895 /* Load preload/needed libraries, add their symbols to the global
Rich Felkerfd7015d2012-01-23 18:32:40 -0500896 * namespace, and perform all remaining relocations. The main
897 * program must be relocated LAST since it may contain copy
898 * relocations which depend on libraries' relocations. */
Rich Felker2719cc82011-08-16 00:24:36 -0400899 if (env_preload) load_preload(env_preload);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500900 load_deps(app);
901 make_global(app);
Rich Felker9c748562012-10-04 22:48:33 -0400902
903 /* Make an initial pass setting up TLS before performing relocs.
904 * This provides the TP-based offset of each DSO's TLS for
905 * use in TP-relative relocations. After relocations, we need
906 * to copy the TLS images again in case they had relocs. */
907 tls_size += sizeof(struct pthread) + 4*sizeof(size_t);
908 __libc.tls_size = tls_size;
Rich Felker9c748562012-10-04 22:48:33 -0400909 if (tls_cnt) {
910 struct dso *p;
911 void *mem = mmap(0, __libc.tls_size, PROT_READ|PROT_WRITE,
912 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
913 if (mem==MAP_FAILED ||
Rich Felkerdcd60372012-10-05 11:51:50 -0400914 !__install_initial_tls(__copy_tls(mem))) {
Rich Felker9c748562012-10-04 22:48:33 -0400915 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
916 argv[0], tls_size);
917 _exit(127);
918 }
919 for (p=head; p; p=p->next) {
920 if (!p->tls_id) continue;
921 p->tls_offset = (char *)__pthread_self()
922 - (char *)__pthread_self()->dtv[p->tls_id];
923 }
924 }
925
Rich Felkerfd7015d2012-01-23 18:32:40 -0500926 reloc_all(app->next);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500927 reloc_all(app);
Rich Felker51e2d832011-06-18 19:48:42 -0400928
Rich Felker9c748562012-10-04 22:48:33 -0400929 /* The initial DTV is located at the base of the memory
930 * allocated for TLS. Repeat copying TLS to pick up relocs. */
Rich Felkerdcd60372012-10-05 11:51:50 -0400931 if (tls_cnt) __copy_tls((void *)__pthread_self()->dtv);
Rich Felker9c748562012-10-04 22:48:33 -0400932
Rich Felker04109502012-08-18 16:00:23 -0400933 if (ldso_fail) _exit(127);
Rich Felker5c1909a2012-05-27 16:01:44 -0400934 if (ldd_mode) _exit(0);
935
Rich Felkerc82f4a32012-01-23 00:57:38 -0500936 /* Switch to runtime mode: any further failures in the dynamic
937 * linker are a reportable failure rather than a fatal startup
938 * error. If the dynamic loader (dlopen) will not be used, free
939 * all memory used by the dynamic linker. */
Rich Felkera53de812011-07-24 00:26:12 -0400940 runtime = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500941
Rich Felker7d9a5c62012-08-05 14:03:17 -0400942#ifndef DYNAMIC_IS_RO
Rich Felker3ec8d292012-04-25 00:05:42 -0400943 for (i=0; app->dynv[i]; i+=2)
944 if (app->dynv[i]==DT_DEBUG)
945 app->dynv[i+1] = (size_t)&debug;
Rich Felker7d9a5c62012-08-05 14:03:17 -0400946#endif
Rich Felker3ec8d292012-04-25 00:05:42 -0400947 debug.ver = 1;
948 debug.bp = _dl_debug_state;
949 debug.head = head;
950 debug.base = lib->base;
951 debug.state = 0;
952 _dl_debug_state();
953
Rich Felker58aa5f42012-05-03 20:42:45 -0400954 if (ssp_used) __init_ssp(auxv);
955
Rich Felkerf4f77c02012-10-05 13:09:09 -0400956 atexit(do_fini);
Rich Felker4ce3cb52012-02-06 14:39:09 -0500957 do_init_fini(tail);
958
Rich Felker51e2d832011-06-18 19:48:42 -0400959 errno = 0;
960 return (void *)aux[AT_ENTRY];
961}
Rich Felker59ab43f2011-06-26 19:23:28 -0400962
963void *dlopen(const char *file, int mode)
964{
Rich Felker642b7592012-10-05 01:15:25 -0400965 struct dso *volatile p, *orig_tail, *next;
Rich Felkerdcd60372012-10-05 11:51:50 -0400966 size_t orig_tls_cnt;
Rich Felker59ab43f2011-06-26 19:23:28 -0400967 size_t i;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500968 int cs;
Rich Felker59ab43f2011-06-26 19:23:28 -0400969
970 if (!file) return head;
971
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500972 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker59ab43f2011-06-26 19:23:28 -0400973 pthread_rwlock_wrlock(&lock);
Rich Felkerdcd60372012-10-05 11:51:50 -0400974 __inhibit_ptc();
Rich Felker59ab43f2011-06-26 19:23:28 -0400975
Rich Felkerdcd60372012-10-05 11:51:50 -0400976 p = 0;
977 orig_tls_cnt = tls_cnt;
Rich Felker642b7592012-10-05 01:15:25 -0400978 orig_tail = tail;
979
Rich Felker59ab43f2011-06-26 19:23:28 -0400980 if (setjmp(rtld_fail)) {
981 /* Clean up anything new that was (partially) loaded */
Rich Felkerdcd60372012-10-05 11:51:50 -0400982 if (p && p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400983 if (p->deps[i]->global < 0)
984 p->deps[i]->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400985 for (p=orig_tail->next; p; p=next) {
986 next = p->next;
987 munmap(p->map, p->map_len);
988 free(p->deps);
989 free(p);
990 }
Rich Felkerdcd60372012-10-05 11:51:50 -0400991 tls_cnt = orig_tls_cnt;
992 tls_size = __libc.tls_size;
Rich Felker59ab43f2011-06-26 19:23:28 -0400993 tail = orig_tail;
994 tail->next = 0;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500995 p = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400996 errflag = 1;
997 goto end;
Rich Felkera9e85c02012-03-23 00:28:20 -0400998 } else p = load_library(file);
999
1000 if (!p) {
Rich Felkera5d10eb2012-04-23 12:03:31 -04001001 snprintf(errbuf, sizeof errbuf,
1002 "Error loading shared library %s: %m", file);
Rich Felkera9e85c02012-03-23 00:28:20 -04001003 errflag = 1;
Rich Felkerf2baf4d2012-02-07 20:31:27 -05001004 goto end;
Rich Felker59ab43f2011-06-26 19:23:28 -04001005 }
1006
Rich Felker59ab43f2011-06-26 19:23:28 -04001007 /* First load handling */
1008 if (!p->deps) {
1009 load_deps(p);
Rich Felker0e4dae32011-06-26 21:36:44 -04001010 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -04001011 if (!p->deps[i]->global)
1012 p->deps[i]->global = -1;
1013 if (!p->global) p->global = -1;
Rich Felker59ab43f2011-06-26 19:23:28 -04001014 reloc_all(p);
Rich Felker0e4dae32011-06-26 21:36:44 -04001015 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -04001016 if (p->deps[i]->global < 0)
1017 p->deps[i]->global = 0;
1018 if (p->global < 0) p->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -04001019 }
1020
1021 if (mode & RTLD_GLOBAL) {
Rich Felker0e4dae32011-06-26 21:36:44 -04001022 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker59ab43f2011-06-26 19:23:28 -04001023 p->deps[i]->global = 1;
1024 p->global = 1;
1025 }
1026
Rich Felkerdcd60372012-10-05 11:51:50 -04001027 __libc.tls_size = tls_size;
1028
Rich Felker731e8ff2012-08-25 17:24:46 -04001029 if (ssp_used) __init_ssp(auxv);
1030
Rich Felker3ec8d292012-04-25 00:05:42 -04001031 _dl_debug_state();
Rich Felkerf4f77c02012-10-05 13:09:09 -04001032 orig_tail = tail;
Rich Felker06933cc2011-06-26 22:09:32 -04001033end:
Rich Felkerdcd60372012-10-05 11:51:50 -04001034 __release_ptc();
Rich Felker59ab43f2011-06-26 19:23:28 -04001035 pthread_rwlock_unlock(&lock);
Rich Felkerf4f77c02012-10-05 13:09:09 -04001036 if (p) do_init_fini(orig_tail);
Rich Felkerf2baf4d2012-02-07 20:31:27 -05001037 pthread_setcancelstate(cs, 0);
Rich Felker59ab43f2011-06-26 19:23:28 -04001038 return p;
1039}
1040
Rich Felker623753a2011-08-16 00:42:13 -04001041static void *do_dlsym(struct dso *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -04001042{
1043 size_t i;
Rich Felker2bd05a42012-08-25 17:13:28 -04001044 uint32_t h = 0, gh = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -04001045 Sym *sym;
Rich Felker9c748562012-10-04 22:48:33 -04001046 if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
1047 if (p == RTLD_NEXT) {
1048 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
1049 if (!p) p=head;
1050 }
1051 struct symdef def = find_sym(p->next, s, 0);
1052 if (!def.sym) goto failed;
1053 return def.dso->base + def.sym->st_value;
Rich Felkera9e85c02012-03-23 00:28:20 -04001054 }
Rich Felker2bd05a42012-08-25 17:13:28 -04001055 if (p->ghashtab) {
1056 gh = gnu_hash(s);
1057 sym = gnu_lookup(s, gh, p);
1058 } else {
1059 h = sysv_hash(s);
1060 sym = sysv_lookup(s, h, p);
1061 }
Rich Felker59ab43f2011-06-26 19:23:28 -04001062 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
1063 return p->base + sym->st_value;
1064 if (p->deps) for (i=0; p->deps[i]; i++) {
Rich Felker2bd05a42012-08-25 17:13:28 -04001065 if (p->deps[i]->ghashtab) {
1066 if (!gh) gh = gnu_hash(s);
Rich Felkera5d61992012-08-25 17:40:27 -04001067 sym = gnu_lookup(s, gh, p->deps[i]);
Rich Felker2bd05a42012-08-25 17:13:28 -04001068 } else {
1069 if (!h) h = sysv_hash(s);
1070 sym = sysv_lookup(s, h, p->deps[i]);
1071 }
Rich Felker59ab43f2011-06-26 19:23:28 -04001072 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
1073 return p->deps[i]->base + sym->st_value;
1074 }
Rich Felker4027f4e2012-05-04 20:18:18 -04001075failed:
Rich Felkera9e85c02012-03-23 00:28:20 -04001076 errflag = 1;
Rich Felkera5d10eb2012-04-23 12:03:31 -04001077 snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
Rich Felker59ab43f2011-06-26 19:23:28 -04001078 return 0;
1079}
1080
Rich Felkerf419bcb2012-08-26 21:09:26 -04001081int __dladdr(void *addr, Dl_info *info)
1082{
1083 struct dso *p;
1084 Sym *sym;
1085 uint32_t nsym;
1086 char *strings;
1087 size_t i;
1088 void *best = 0;
1089 char *bestname;
1090
1091 pthread_rwlock_rdlock(&lock);
1092 for (p=head; p && (unsigned char *)addr-p->map>p->map_len; p=p->next);
1093 pthread_rwlock_unlock(&lock);
1094
1095 if (!p) return 0;
1096
1097 sym = p->syms;
1098 strings = p->strings;
1099 if (p->hashtab) {
1100 nsym = p->hashtab[1];
1101 } else {
1102 uint32_t *buckets;
1103 uint32_t *hashval;
1104 buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
1105 sym += p->ghashtab[1];
1106 for (i = 0; i < p->ghashtab[0]; i++) {
1107 if (buckets[i] > nsym)
1108 nsym = buckets[i];
1109 }
1110 if (nsym) {
1111 nsym -= p->ghashtab[1];
1112 hashval = buckets + p->ghashtab[0] + nsym;
1113 do nsym++;
1114 while (!(*hashval++ & 1));
1115 }
1116 }
1117
1118 for (; nsym; nsym--, sym++) {
1119 if (sym->st_shndx && sym->st_value
1120 && (1<<(sym->st_info&0xf) & OK_TYPES)
1121 && (1<<(sym->st_info>>4) & OK_BINDS)) {
1122 void *symaddr = p->base + sym->st_value;
1123 if (symaddr > addr || symaddr < best)
1124 continue;
1125 best = symaddr;
1126 bestname = strings + sym->st_name;
1127 if (addr == symaddr)
1128 break;
1129 }
1130 }
1131
1132 if (!best) return 0;
1133
1134 info->dli_fname = p->name;
1135 info->dli_fbase = p->base;
1136 info->dli_sname = bestname;
1137 info->dli_saddr = best;
1138
1139 return 1;
1140}
1141
Rich Felker400c5e52012-09-06 22:44:55 -04001142void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
Rich Felker59ab43f2011-06-26 19:23:28 -04001143{
1144 void *res;
1145 pthread_rwlock_rdlock(&lock);
Rich Felker623753a2011-08-16 00:42:13 -04001146 res = do_dlsym(p, s, ra);
Rich Felker59ab43f2011-06-26 19:23:28 -04001147 pthread_rwlock_unlock(&lock);
1148 return res;
1149}
Rich Felker5a09a532012-02-03 03:16:07 -05001150#else
1151void *dlopen(const char *file, int mode)
1152{
1153 return 0;
1154}
Rich Felker400c5e52012-09-06 22:44:55 -04001155void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
Rich Felker5a09a532012-02-03 03:16:07 -05001156{
1157 return 0;
1158}
Rich Felkerf419bcb2012-08-26 21:09:26 -04001159int __dladdr (void *addr, Dl_info *info)
1160{
1161 return 0;
1162}
Rich Felker5a09a532012-02-03 03:16:07 -05001163#endif
Rich Felker59ab43f2011-06-26 19:23:28 -04001164
1165char *dlerror()
1166{
Rich Felkera9e85c02012-03-23 00:28:20 -04001167 if (!errflag) return 0;
1168 errflag = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -04001169 return errbuf;
Rich Felker59ab43f2011-06-26 19:23:28 -04001170}
1171
1172int dlclose(void *p)
1173{
1174 return 0;
1175}