blob: 6ff8850c8eb89d55ac4c16799bc12e5e09ca0d45 [file] [log] [blame]
Rich Felker4f4bf0a2011-09-18 16:42:06 -04001#ifdef __PIC__
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 Felker51e2d832011-06-18 19:48:42 -040020
21#include "reloc.h"
22
23#if ULONG_MAX == 0xffffffff
24typedef Elf32_Ehdr Ehdr;
25typedef Elf32_Phdr Phdr;
26typedef Elf32_Sym Sym;
27#define R_TYPE(x) ((x)&255)
28#define R_SYM(x) ((x)>>8)
29#else
30typedef Elf64_Ehdr Ehdr;
31typedef Elf64_Phdr Phdr;
32typedef Elf64_Sym Sym;
33#define R_TYPE(x) ((x)&0xffffffff)
34#define R_SYM(x) ((x)>>32)
35#endif
36
37struct dso
38{
39 struct dso *next, *prev;
40 int refcnt;
41 size_t *dynv;
42 Sym *syms;
Rich Felker596d60c2011-06-18 22:52:01 -040043 uint32_t *hashtab;
Rich Felker51e2d832011-06-18 19:48:42 -040044 char *strings;
45 unsigned char *base;
46 unsigned char *map;
47 size_t map_len;
48 dev_t dev;
49 ino_t ino;
Rich Felker700a8152012-02-07 20:29:29 -050050 char global;
51 char relocated;
52 char constructed;
Rich Felker59ab43f2011-06-26 19:23:28 -040053 struct dso **deps;
Rich Felker6b3d5e52011-06-26 17:39:17 -040054 char *name;
55 char buf[];
Rich Felker51e2d832011-06-18 19:48:42 -040056};
57
58static struct dso *head, *tail, *libc;
Rich Felker191ebca2011-06-30 23:02:27 -040059static char *env_path, *sys_path, *r_path;
Rich Felker59ab43f2011-06-26 19:23:28 -040060static int rtld_used;
Rich Felker6b3d5e52011-06-26 17:39:17 -040061static int runtime;
62static jmp_buf rtld_fail;
Rich Felker59ab43f2011-06-26 19:23:28 -040063static pthread_rwlock_t lock;
Rich Felker51e2d832011-06-18 19:48:42 -040064
Rich Felkera0458832011-08-16 07:46:42 -040065#define AUX_CNT 24
Rich Felker51e2d832011-06-18 19:48:42 -040066#define DYN_CNT 34
67
68static void decode_vec(size_t *v, size_t *a, size_t cnt)
69{
70 memset(a, 0, cnt*sizeof(size_t));
71 for (; v[0]; v+=2) if (v[0]<cnt) {
72 a[0] |= 1ULL<<v[0];
73 a[v[0]] = v[1];
74 }
75}
76
Rich Felker2adf2fb2012-01-17 00:34:58 -050077static uint32_t hash(const char *s0)
Rich Felker51e2d832011-06-18 19:48:42 -040078{
Rich Felker2adf2fb2012-01-17 00:34:58 -050079 const unsigned char *s = (void *)s0;
Rich Felker51e2d832011-06-18 19:48:42 -040080 uint_fast32_t h = 0;
81 while (*s) {
82 h = 16*h + *s++;
83 h ^= h>>24 & 0xf0;
84 }
85 return h & 0xfffffff;
86}
87
Rich Felker596d60c2011-06-18 22:52:01 -040088static Sym *lookup(const char *s, uint32_t h, Sym *syms, uint32_t *hashtab, char *strings)
Rich Felker51e2d832011-06-18 19:48:42 -040089{
90 size_t i;
91 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
92 if (!strcmp(s, strings+syms[i].st_name))
93 return syms+i;
94 }
95 return 0;
96}
97
98#define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
Rich Felker427173b2011-07-24 02:19:47 -040099#define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
Rich Felker51e2d832011-06-18 19:48:42 -0400100
101static void *find_sym(struct dso *dso, const char *s, int need_def)
102{
103 uint32_t h = hash(s);
Rich Felker427173b2011-07-24 02:19:47 -0400104 void *def = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400105 if (h==0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1;
106 if (h==0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400107 for (; dso; dso=dso->next) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400108 Sym *sym;
109 if (!dso->global) continue;
110 sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings);
Rich Felker51e2d832011-06-18 19:48:42 -0400111 if (sym && (!need_def || sym->st_shndx) && sym->st_value
Rich Felker427173b2011-07-24 02:19:47 -0400112 && (1<<(sym->st_info&0xf) & OK_TYPES)
113 && (1<<(sym->st_info>>4) & OK_BINDS)) {
Rich Felkere01ac672011-07-25 09:22:05 -0400114 if (def && sym->st_info>>4 == STB_WEAK) continue;
Rich Felker427173b2011-07-24 02:19:47 -0400115 def = dso->base + sym->st_value;
116 if (sym->st_info>>4 == STB_GLOBAL) break;
117 }
Rich Felker51e2d832011-06-18 19:48:42 -0400118 }
Rich Felker427173b2011-07-24 02:19:47 -0400119 return def;
Rich Felker51e2d832011-06-18 19:48:42 -0400120}
121
122static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso)
123{
124 Sym *sym;
125 const char *name;
126 size_t sym_val, sym_size;
127 size_t *reloc_addr;
128 void *ctx;
129 int type;
130 int sym_index;
131
132 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
133 reloc_addr = (void *)(base + rel[0]);
134 type = R_TYPE(rel[1]);
135 sym_index = R_SYM(rel[1]);
136 if (sym_index) {
137 sym = syms + sym_index;
138 name = strings + sym->st_name;
139 ctx = IS_COPY(type) ? dso->next : dso;
Rich Felker32de61e2011-06-25 22:36:21 -0400140 sym_val = (size_t)find_sym(ctx, name, IS_PLT(type));
Rich Felker6b3d5e52011-06-26 17:39:17 -0400141 if (!sym_val && sym->st_info>>4 != STB_WEAK) {
142 if (runtime) longjmp(rtld_fail, 1);
143 dprintf(2, "%s: symbol not found\n", name);
144 _exit(127);
145 }
Rich Felker51e2d832011-06-18 19:48:42 -0400146 sym_size = sym->st_size;
147 }
148 do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]);
149 }
150}
151
Rich Felker6717e622011-06-28 19:40:14 -0400152/* A huge hack: to make up for the wastefulness of shared libraries
153 * needing at least a page of dirty memory even if they have no global
154 * data, we reclaim the gaps at the beginning and end of writable maps
155 * and "donate" them to the heap by setting up minimal malloc
156 * structures and then freeing them. */
157
158static void reclaim(unsigned char *base, size_t start, size_t end)
159{
160 size_t *a, *z;
161 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
162 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
163 if (start>end || end-start < 4*sizeof(size_t)) return;
164 a = (size_t *)(base + start);
165 z = (size_t *)(base + end);
166 a[-2] = 1;
167 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
168 z[1] = 1;
169 free(a);
170}
171
172static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
173{
174 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
175 if (ph->p_type!=PT_LOAD) continue;
176 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
177 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
178 reclaim(base, ph->p_vaddr+ph->p_memsz,
179 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
180 }
181}
182
Rich Felker51e2d832011-06-18 19:48:42 -0400183static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp)
184{
Rich Felker59633c72011-06-25 12:26:08 -0400185 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
Rich Felker51e2d832011-06-18 19:48:42 -0400186 size_t phsize;
187 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
188 size_t this_min, this_max;
189 off_t off_start;
190 Ehdr *eh;
191 Phdr *ph;
192 unsigned prot;
193 unsigned char *map, *base;
194 size_t dyn;
195 size_t i;
196
197 ssize_t l = read(fd, buf, sizeof buf);
198 if (l<sizeof *eh) return 0;
Rich Felker59633c72011-06-25 12:26:08 -0400199 eh = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400200 phsize = eh->e_phentsize * eh->e_phnum;
201 if (phsize + sizeof *eh > l) return 0;
202 if (eh->e_phoff + phsize > l) {
Rich Felker59633c72011-06-25 12:26:08 -0400203 l = pread(fd, buf+1, phsize, eh->e_phoff);
Rich Felker51e2d832011-06-18 19:48:42 -0400204 if (l != phsize) return 0;
205 eh->e_phoff = sizeof *eh;
206 }
207 ph = (void *)((char *)buf + eh->e_phoff);
208 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
209 if (ph->p_type == PT_DYNAMIC)
210 dyn = ph->p_vaddr;
211 if (ph->p_type != PT_LOAD) continue;
212 if (ph->p_vaddr < addr_min) {
213 addr_min = ph->p_vaddr;
214 off_start = ph->p_offset;
215 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
216 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
217 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
218 }
219 if (ph->p_vaddr+ph->p_memsz > addr_max) {
220 addr_max = ph->p_vaddr+ph->p_memsz;
221 }
222 }
223 if (!dyn) return 0;
224 addr_max += PAGE_SIZE-1;
225 addr_max &= -PAGE_SIZE;
226 addr_min &= -PAGE_SIZE;
227 off_start &= -PAGE_SIZE;
228 map_len = addr_max - addr_min + off_start;
229 /* The first time, we map too much, possibly even more than
230 * the length of the file. This is okay because we will not
231 * use the invalid part; we just need to reserve the right
232 * amount of virtual address space to map over later. */
Rich Felkerbf301002011-06-28 14:20:41 -0400233 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
Rich Felker51e2d832011-06-18 19:48:42 -0400234 if (map==MAP_FAILED) return 0;
235 base = map - addr_min;
236 ph = (void *)((char *)buf + eh->e_phoff);
237 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
238 if (ph->p_type != PT_LOAD) continue;
239 /* Reuse the existing mapping for the lowest-address LOAD */
240 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
241 this_min = ph->p_vaddr & -PAGE_SIZE;
242 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
243 off_start = ph->p_offset & -PAGE_SIZE;
244 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
245 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
246 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
247 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) {
248 munmap(map, map_len);
249 return 0;
250 }
251 if (ph->p_memsz > ph->p_filesz) {
252 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
253 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
254 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
255 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) {
256 munmap(map, map_len);
257 return 0;
258 }
259 }
260 }
Rich Felker9f174132011-06-29 00:29:08 -0400261 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
262 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
263 mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC);
264 break;
265 }
Rich Felker6717e622011-06-28 19:40:14 -0400266 if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
267 eh->e_phentsize, eh->e_phnum);
Rich Felker51e2d832011-06-18 19:48:42 -0400268 *lenp = map_len;
269 *basep = base;
270 *dynp = dyn;
271 return map;
272}
273
Rich Felker568b8072011-06-25 01:56:34 -0400274static int path_open(const char *name, const char *search)
275{
276 char buf[2*NAME_MAX+2];
Rich Felker49388f32011-06-25 17:49:16 -0400277 const char *s=search, *z;
Rich Felker568b8072011-06-25 01:56:34 -0400278 int l, fd;
Rich Felker49388f32011-06-25 17:49:16 -0400279 for (;;) {
280 while (*s==':') s++;
281 if (!*s) return -1;
Rich Felker568b8072011-06-25 01:56:34 -0400282 z = strchr(s, ':');
283 l = z ? z-s : strlen(s);
284 snprintf(buf, sizeof buf, "%.*s/%s", l, s, name);
285 if ((fd = open(buf, O_RDONLY))>=0) return fd;
Rich Felker49388f32011-06-25 17:49:16 -0400286 s += l;
Rich Felker568b8072011-06-25 01:56:34 -0400287 }
Rich Felker568b8072011-06-25 01:56:34 -0400288}
289
Rich Felkerc82f4a32012-01-23 00:57:38 -0500290static void decode_dyn(struct dso *p)
291{
292 size_t dyn[DYN_CNT] = {0};
293 decode_vec(p->dynv, dyn, DYN_CNT);
294 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
295 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
296 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
297}
298
Rich Felker51e2d832011-06-18 19:48:42 -0400299static struct dso *load_library(const char *name)
300{
301 unsigned char *base, *map;
302 size_t dyno, map_len;
303 struct dso *p;
Rich Felker51e2d832011-06-18 19:48:42 -0400304 int fd;
305 struct stat st;
306
307 /* Catch and block attempts to reload the implementation itself */
308 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
309 static const char *rp, reserved[] =
310 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
311 char *z = strchr(name, '.');
312 if (z) {
313 size_t l = z-name;
314 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
315 if (*rp) {
316 if (!libc->prev) {
317 tail->next = libc;
318 libc->prev = tail;
Rich Felker6ab444d2011-07-24 00:54:55 -0400319 tail = libc->next ? libc->next : libc;
Rich Felker51e2d832011-06-18 19:48:42 -0400320 }
321 return libc;
322 }
323 }
324 }
325 /* Search for the name to see if it's already loaded */
326 for (p=head->next; p; p=p->next) {
327 if (!strcmp(p->name, name)) {
328 p->refcnt++;
329 return p;
330 }
331 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400332 if (strchr(name, '/')) {
Rich Felker51e2d832011-06-18 19:48:42 -0400333 fd = open(name, O_RDONLY);
334 } else {
Rich Felker59ab43f2011-06-26 19:23:28 -0400335 if (strlen(name) > NAME_MAX) return 0;
Rich Felker568b8072011-06-25 01:56:34 -0400336 fd = -1;
Rich Felker191ebca2011-06-30 23:02:27 -0400337 if (r_path) fd = path_open(name, r_path);
338 if (fd < 0 && env_path) fd = path_open(name, env_path);
Rich Felker568b8072011-06-25 01:56:34 -0400339 if (fd < 0) {
340 if (!sys_path) {
341 FILE *f = fopen(ETC_LDSO_PATH, "r");
342 if (f) {
343 if (getline(&sys_path, (size_t[1]){0}, f) > 0)
344 sys_path[strlen(sys_path)-1]=0;
345 fclose(f);
346 }
347 }
348 if (sys_path) fd = path_open(name, sys_path);
349 else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib");
Rich Felker51e2d832011-06-18 19:48:42 -0400350 }
351 }
352 if (fd < 0) return 0;
353 if (fstat(fd, &st) < 0) {
354 close(fd);
355 return 0;
356 }
357 for (p=head->next; p; p=p->next) {
358 if (p->dev == st.st_dev && p->ino == st.st_ino) {
359 close(fd);
360 p->refcnt++;
361 return p;
362 }
363 }
364 map = map_library(fd, &map_len, &base, &dyno);
365 close(fd);
366 if (!map) return 0;
367 p = calloc(1, sizeof *p + strlen(name) + 1);
368 if (!p) {
369 munmap(map, map_len);
370 return 0;
371 }
372
373 p->map = map;
374 p->map_len = map_len;
375 p->base = base;
376 p->dynv = (void *)(base + dyno);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500377 decode_dyn(p);
Rich Felker51e2d832011-06-18 19:48:42 -0400378
Rich Felker51e2d832011-06-18 19:48:42 -0400379 p->dev = st.st_dev;
380 p->ino = st.st_ino;
Rich Felker51e2d832011-06-18 19:48:42 -0400381 p->refcnt = 1;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400382 p->name = p->buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400383 strcpy(p->name, name);
384
385 tail->next = p;
386 p->prev = tail;
387 tail = p;
388
389 return p;
390}
391
392static void load_deps(struct dso *p)
393{
Rich Felker59ab43f2011-06-26 19:23:28 -0400394 size_t i, ndeps=0;
395 struct dso ***deps = &p->deps, **tmp, *dep;
Rich Felker51e2d832011-06-18 19:48:42 -0400396 for (; p; p=p->next) {
397 for (i=0; p->dynv[i]; i+=2) {
Rich Felker191ebca2011-06-30 23:02:27 -0400398 if (p->dynv[i] != DT_RPATH) continue;
399 r_path = (void *)(p->strings + p->dynv[i+1]);
400 }
401 for (i=0; p->dynv[i]; i+=2) {
Rich Felker51e2d832011-06-18 19:48:42 -0400402 if (p->dynv[i] != DT_NEEDED) continue;
Rich Felker59ab43f2011-06-26 19:23:28 -0400403 dep = load_library(p->strings + p->dynv[i+1]);
404 if (!dep) {
Rich Felker6b3d5e52011-06-26 17:39:17 -0400405 if (runtime) longjmp(rtld_fail, 1);
406 dprintf(2, "%s: %m (needed by %s)\n",
407 p->strings + p->dynv[i+1], p->name);
408 _exit(127);
409 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400410 if (runtime) {
411 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
412 if (!tmp) longjmp(rtld_fail, 1);
413 tmp[ndeps++] = dep;
414 tmp[ndeps] = 0;
415 *deps = tmp;
416 }
Rich Felker51e2d832011-06-18 19:48:42 -0400417 }
Rich Felker191ebca2011-06-30 23:02:27 -0400418 r_path = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400419 }
420}
421
Rich Felker2719cc82011-08-16 00:24:36 -0400422static void load_preload(char *s)
423{
424 int tmp;
425 char *z;
426 for (z=s; *z; s=z) {
427 for ( ; *s && isspace(*s); s++);
428 for (z=s; *z && !isspace(*z); z++);
429 tmp = *z;
430 *z = 0;
431 load_library(s);
432 *z = tmp;
433 }
434}
435
Rich Felker59ab43f2011-06-26 19:23:28 -0400436static void make_global(struct dso *p)
437{
438 for (; p; p=p->next) p->global = 1;
439}
440
Rich Felker51e2d832011-06-18 19:48:42 -0400441static void reloc_all(struct dso *p)
442{
443 size_t dyn[DYN_CNT] = {0};
444 for (; p; p=p->next) {
445 if (p->relocated) continue;
446 decode_vec(p->dynv, dyn, DYN_CNT);
447 do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400448 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head);
Rich Felker51e2d832011-06-18 19:48:42 -0400449 do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400450 2, p->syms, p->strings, head);
Rich Felker51e2d832011-06-18 19:48:42 -0400451 do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400452 3, p->syms, p->strings, head);
Rich Felker368ba4a2011-06-25 00:18:19 -0400453 p->relocated = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400454 }
455}
456
Rich Felkere8dbf002011-06-25 00:47:28 -0400457static void free_all(struct dso *p)
458{
459 struct dso *n;
460 while (p) {
461 n = p->next;
462 if (p->map) free(p);
463 p = n;
464 }
465}
466
Rich Felkerc82f4a32012-01-23 00:57:38 -0500467static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
468{
469 for (; cnt--; ph = (void *)((char *)ph + stride))
470 if (ph->p_type == PT_DYNAMIC)
471 return ph->p_vaddr;
472 return 0;
473}
474
Rich Felker4ce3cb52012-02-06 14:39:09 -0500475static void do_init_fini(struct dso *p)
476{
477 size_t dyn[DYN_CNT] = {0};
478 for (; p; p=p->prev) {
479 if (p->constructed) return;
480 decode_vec(p->dynv, dyn, DYN_CNT);
481 if (dyn[0] & (1<<DT_FINI))
482 atexit((void (*)(void))(p->base + dyn[DT_FINI]));
483 if (dyn[0] & (1<<DT_INIT))
484 ((void (*)(void))(p->base + dyn[DT_INIT]))();
485 p->constructed = 1;
486 }
487}
488
Rich Felkerc82f4a32012-01-23 00:57:38 -0500489void *__dynlink(int argc, char **argv)
Rich Felker51e2d832011-06-18 19:48:42 -0400490{
491 size_t *auxv, aux[AUX_CNT] = {0};
Rich Felker51e2d832011-06-18 19:48:42 -0400492 size_t i;
493 Phdr *phdr;
Rich Felker6717e622011-06-28 19:40:14 -0400494 Ehdr *ehdr;
Rich Felker6ab444d2011-07-24 00:54:55 -0400495 static struct dso builtin_dsos[3];
Rich Felkera53de812011-07-24 00:26:12 -0400496 struct dso *const app = builtin_dsos+0;
497 struct dso *const lib = builtin_dsos+1;
Rich Felker6ab444d2011-07-24 00:54:55 -0400498 struct dso *const vdso = builtin_dsos+2;
Rich Felker2719cc82011-08-16 00:24:36 -0400499 char *env_preload=0;
Rich Felker51e2d832011-06-18 19:48:42 -0400500
501 /* Find aux vector just past environ[] */
Rich Felker568b8072011-06-25 01:56:34 -0400502 for (i=argc+1; argv[i]; i++)
503 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
504 env_path = argv[i]+16;
Rich Felker2719cc82011-08-16 00:24:36 -0400505 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
506 env_preload = argv[i]+11;
Rich Felker51e2d832011-06-18 19:48:42 -0400507 auxv = (void *)(argv+i+1);
508
509 decode_vec(auxv, aux, AUX_CNT);
510
Rich Felker568b8072011-06-25 01:56:34 -0400511 /* Only trust user/env if kernel says we're not suid/sgid */
512 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
Rich Felkera0458832011-08-16 07:46:42 -0400513 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
Rich Felker568b8072011-06-25 01:56:34 -0400514 env_path = 0;
Rich Felker2719cc82011-08-16 00:24:36 -0400515 env_preload = 0;
Rich Felker568b8072011-06-25 01:56:34 -0400516 }
517
Rich Felkerc82f4a32012-01-23 00:57:38 -0500518 /* The dynamic linker load address is passed by the kernel
519 * in the AUX vector, so this is easy. */
520 lib->base = (void *)aux[AT_BASE];
521 lib->name = "libc.so";
522 lib->global = 1;
523 ehdr = (void *)lib->base;
524 lib->dynv = (void *)(lib->base + find_dyn(
525 (void *)(aux[AT_BASE]+ehdr->e_phoff),
526 ehdr->e_phnum, ehdr->e_phentsize));
527 decode_dyn(lib);
528
Rich Felkere12fe652012-01-23 02:02:59 -0500529 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
Rich Felkerc82f4a32012-01-23 00:57:38 -0500530 app->base = 0;
Rich Felkere12fe652012-01-23 02:02:59 -0500531 phdr = (void *)aux[AT_PHDR];
532 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
533 if (phdr->p_type == PT_PHDR)
534 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
535 }
Rich Felkerc82f4a32012-01-23 00:57:38 -0500536 app->name = argv[0];
537 app->global = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500538 app->constructed = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500539 app->dynv = (void *)(app->base + find_dyn(
540 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
541 decode_dyn(app);
542
543 /* Attach to vdso, if provided by the kernel */
Rich Felkercf8506a2011-08-16 08:50:03 -0400544 for (i=0; auxv[i]; i+=2) {
Rich Felkerc82f4a32012-01-23 00:57:38 -0500545 size_t vdso_base = auxv[i+1];
546 if (auxv[i] != AT_SYSINFO_EHDR) continue;
Rich Felker6ab444d2011-07-24 00:54:55 -0400547 ehdr = (void *)vdso_base;
548 phdr = (void *)(vdso_base + ehdr->e_phoff);
549 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
550 if (phdr->p_type == PT_DYNAMIC)
551 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
552 if (phdr->p_type == PT_LOAD)
553 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
554 }
Rich Felker6ab444d2011-07-24 00:54:55 -0400555 vdso->name = "linux-gate.so.1";
Rich Felker427173b2011-07-24 02:19:47 -0400556 vdso->global = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500557 decode_dyn(vdso);
Rich Felker6ab444d2011-07-24 00:54:55 -0400558 vdso->prev = lib;
559 lib->next = vdso;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500560 break;
Rich Felker6ab444d2011-07-24 00:54:55 -0400561 }
562
Rich Felkerc82f4a32012-01-23 00:57:38 -0500563 /* Initial dso chain consists only of the app. We temporarily
564 * append the dynamic linker/libc so we can relocate it, then
565 * restore the initial chain in preparation for loading third
566 * party libraries (preload/needed). */
567 head = tail = app;
568 libc = lib;
569 app->next = lib;
570 reloc_all(lib);
571 app->next = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400572
Rich Felkerc82f4a32012-01-23 00:57:38 -0500573 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
Rich Felker51e2d832011-06-18 19:48:42 -0400574
Rich Felkerc82f4a32012-01-23 00:57:38 -0500575 /* Donate unused parts of app and library mapping to malloc */
Rich Felkera53de812011-07-24 00:26:12 -0400576 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
577 ehdr = (void *)lib->base;
578 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
Rich Felker6717e622011-06-28 19:40:14 -0400579 ehdr->e_phentsize, ehdr->e_phnum);
580
Rich Felkerc82f4a32012-01-23 00:57:38 -0500581 /* Load preload/needed libraries, add their symbols to the global
Rich Felkerfd7015d2012-01-23 18:32:40 -0500582 * namespace, and perform all remaining relocations. The main
583 * program must be relocated LAST since it may contain copy
584 * relocations which depend on libraries' relocations. */
Rich Felker2719cc82011-08-16 00:24:36 -0400585 if (env_preload) load_preload(env_preload);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500586 load_deps(app);
587 make_global(app);
Rich Felkerfd7015d2012-01-23 18:32:40 -0500588 reloc_all(app->next);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500589 reloc_all(app);
Rich Felker51e2d832011-06-18 19:48:42 -0400590
Rich Felkerc82f4a32012-01-23 00:57:38 -0500591 /* Switch to runtime mode: any further failures in the dynamic
592 * linker are a reportable failure rather than a fatal startup
593 * error. If the dynamic loader (dlopen) will not be used, free
594 * all memory used by the dynamic linker. */
Rich Felkera53de812011-07-24 00:26:12 -0400595 runtime = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500596
597 do_init_fini(tail);
598
Rich Felkera53de812011-07-24 00:26:12 -0400599 if (!rtld_used) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400600 free_all(head);
601 free(sys_path);
Rich Felkera53de812011-07-24 00:26:12 -0400602 reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos);
Rich Felker59ab43f2011-06-26 19:23:28 -0400603 }
Rich Felkere8dbf002011-06-25 00:47:28 -0400604
Rich Felker51e2d832011-06-18 19:48:42 -0400605 errno = 0;
606 return (void *)aux[AT_ENTRY];
607}
Rich Felker59ab43f2011-06-26 19:23:28 -0400608
609void *dlopen(const char *file, int mode)
610{
Rich Felker2fdea172011-07-01 22:40:00 -0400611 struct dso *volatile p, *orig_tail = tail, *next;
Rich Felker59ab43f2011-06-26 19:23:28 -0400612 size_t i;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500613 int cs;
Rich Felker59ab43f2011-06-26 19:23:28 -0400614
615 if (!file) return head;
616
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500617 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker59ab43f2011-06-26 19:23:28 -0400618 pthread_rwlock_wrlock(&lock);
619
620 if (setjmp(rtld_fail)) {
621 /* Clean up anything new that was (partially) loaded */
Rich Felker92ab5d82011-06-26 21:21:04 -0400622 if (p->deps) for (i=0; p->deps[i]; i++)
623 if (p->deps[i]->global < 0)
624 p->deps[i]->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400625 for (p=orig_tail->next; p; p=next) {
626 next = p->next;
627 munmap(p->map, p->map_len);
628 free(p->deps);
629 free(p);
630 }
631 tail = orig_tail;
632 tail->next = 0;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500633 p = 0;
634 goto end;
Rich Felker59ab43f2011-06-26 19:23:28 -0400635 }
636
637 p = load_library(file);
Rich Felker06933cc2011-06-26 22:09:32 -0400638 if (!p) goto end;
Rich Felker59ab43f2011-06-26 19:23:28 -0400639
640 /* First load handling */
641 if (!p->deps) {
642 load_deps(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400643 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400644 if (!p->deps[i]->global)
645 p->deps[i]->global = -1;
646 if (!p->global) p->global = -1;
Rich Felker59ab43f2011-06-26 19:23:28 -0400647 reloc_all(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400648 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400649 if (p->deps[i]->global < 0)
650 p->deps[i]->global = 0;
651 if (p->global < 0) p->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400652 }
653
654 if (mode & RTLD_GLOBAL) {
Rich Felker0e4dae32011-06-26 21:36:44 -0400655 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker59ab43f2011-06-26 19:23:28 -0400656 p->deps[i]->global = 1;
657 p->global = 1;
658 }
659
Rich Felkerce4d97e2012-02-06 17:57:29 -0500660 do_init_fini(tail);
Rich Felker06933cc2011-06-26 22:09:32 -0400661end:
Rich Felker59ab43f2011-06-26 19:23:28 -0400662 pthread_rwlock_unlock(&lock);
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500663 pthread_setcancelstate(cs, 0);
Rich Felker59ab43f2011-06-26 19:23:28 -0400664 return p;
665}
666
Rich Felker623753a2011-08-16 00:42:13 -0400667static void *do_dlsym(struct dso *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400668{
669 size_t i;
670 uint32_t h;
671 Sym *sym;
Rich Felker623753a2011-08-16 00:42:13 -0400672 if (p == RTLD_NEXT) {
673 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
674 if (!p) p=head;
675 p=p->next;
676 }
Rich Felker97507bd2011-06-26 21:50:01 -0400677 if (p == head || p == RTLD_DEFAULT)
678 return find_sym(head, s, 0);
Rich Felker59ab43f2011-06-26 19:23:28 -0400679 h = hash(s);
680 sym = lookup(s, h, p->syms, p->hashtab, p->strings);
681 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
682 return p->base + sym->st_value;
683 if (p->deps) for (i=0; p->deps[i]; i++) {
684 sym = lookup(s, h, p->deps[i]->syms,
685 p->deps[i]->hashtab, p->deps[i]->strings);
686 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
687 return p->deps[i]->base + sym->st_value;
688 }
689 return 0;
690}
691
Rich Felker623753a2011-08-16 00:42:13 -0400692void *__dlsym(void *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400693{
694 void *res;
695 pthread_rwlock_rdlock(&lock);
Rich Felker623753a2011-08-16 00:42:13 -0400696 res = do_dlsym(p, s, ra);
Rich Felker59ab43f2011-06-26 19:23:28 -0400697 pthread_rwlock_unlock(&lock);
698 return res;
699}
Rich Felker5a09a532012-02-03 03:16:07 -0500700#else
701void *dlopen(const char *file, int mode)
702{
703 return 0;
704}
705void *__dlsym(void *p, const char *s, void *ra)
706{
707 return 0;
708}
709#endif
Rich Felker59ab43f2011-06-26 19:23:28 -0400710
711char *dlerror()
712{
713 return "unknown error";
714}
715
716int dlclose(void *p)
717{
718 return 0;
719}