blob: e1c2ad7d883a4eaa3abc1d1baee7a85095ec18ec [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;
50 int global;
51 int relocated;
Rich Felker59ab43f2011-06-26 19:23:28 -040052 struct dso **deps;
Rich Felker6b3d5e52011-06-26 17:39:17 -040053 char *name;
54 char buf[];
Rich Felker51e2d832011-06-18 19:48:42 -040055};
56
57static struct dso *head, *tail, *libc;
Rich Felker191ebca2011-06-30 23:02:27 -040058static char *env_path, *sys_path, *r_path;
Rich Felker59ab43f2011-06-26 19:23:28 -040059static int rtld_used;
Rich Felker6b3d5e52011-06-26 17:39:17 -040060static int runtime;
61static jmp_buf rtld_fail;
Rich Felker59ab43f2011-06-26 19:23:28 -040062static pthread_rwlock_t lock;
Rich Felker51e2d832011-06-18 19:48:42 -040063
Rich Felkera0458832011-08-16 07:46:42 -040064#define AUX_CNT 24
Rich Felker51e2d832011-06-18 19:48:42 -040065#define DYN_CNT 34
66
67static void decode_vec(size_t *v, size_t *a, size_t cnt)
68{
69 memset(a, 0, cnt*sizeof(size_t));
70 for (; v[0]; v+=2) if (v[0]<cnt) {
71 a[0] |= 1ULL<<v[0];
72 a[v[0]] = v[1];
73 }
74}
75
Rich Felker2adf2fb2012-01-17 00:34:58 -050076static uint32_t hash(const char *s0)
Rich Felker51e2d832011-06-18 19:48:42 -040077{
Rich Felker2adf2fb2012-01-17 00:34:58 -050078 const unsigned char *s = (void *)s0;
Rich Felker51e2d832011-06-18 19:48:42 -040079 uint_fast32_t h = 0;
80 while (*s) {
81 h = 16*h + *s++;
82 h ^= h>>24 & 0xf0;
83 }
84 return h & 0xfffffff;
85}
86
Rich Felker596d60c2011-06-18 22:52:01 -040087static Sym *lookup(const char *s, uint32_t h, Sym *syms, uint32_t *hashtab, char *strings)
Rich Felker51e2d832011-06-18 19:48:42 -040088{
89 size_t i;
90 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
91 if (!strcmp(s, strings+syms[i].st_name))
92 return syms+i;
93 }
94 return 0;
95}
96
97#define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
Rich Felker427173b2011-07-24 02:19:47 -040098#define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
Rich Felker51e2d832011-06-18 19:48:42 -040099
100static void *find_sym(struct dso *dso, const char *s, int need_def)
101{
102 uint32_t h = hash(s);
Rich Felker427173b2011-07-24 02:19:47 -0400103 void *def = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400104 if (h==0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1;
105 if (h==0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400106 for (; dso; dso=dso->next) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400107 Sym *sym;
108 if (!dso->global) continue;
109 sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings);
Rich Felker51e2d832011-06-18 19:48:42 -0400110 if (sym && (!need_def || sym->st_shndx) && sym->st_value
Rich Felker427173b2011-07-24 02:19:47 -0400111 && (1<<(sym->st_info&0xf) & OK_TYPES)
112 && (1<<(sym->st_info>>4) & OK_BINDS)) {
Rich Felkere01ac672011-07-25 09:22:05 -0400113 if (def && sym->st_info>>4 == STB_WEAK) continue;
Rich Felker427173b2011-07-24 02:19:47 -0400114 def = dso->base + sym->st_value;
115 if (sym->st_info>>4 == STB_GLOBAL) break;
116 }
Rich Felker51e2d832011-06-18 19:48:42 -0400117 }
Rich Felker427173b2011-07-24 02:19:47 -0400118 return def;
Rich Felker51e2d832011-06-18 19:48:42 -0400119}
120
121static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso)
122{
123 Sym *sym;
124 const char *name;
125 size_t sym_val, sym_size;
126 size_t *reloc_addr;
127 void *ctx;
128 int type;
129 int sym_index;
130
131 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
132 reloc_addr = (void *)(base + rel[0]);
133 type = R_TYPE(rel[1]);
134 sym_index = R_SYM(rel[1]);
135 if (sym_index) {
136 sym = syms + sym_index;
137 name = strings + sym->st_name;
138 ctx = IS_COPY(type) ? dso->next : dso;
Rich Felker32de61e2011-06-25 22:36:21 -0400139 sym_val = (size_t)find_sym(ctx, name, IS_PLT(type));
Rich Felker6b3d5e52011-06-26 17:39:17 -0400140 if (!sym_val && sym->st_info>>4 != STB_WEAK) {
141 if (runtime) longjmp(rtld_fail, 1);
142 dprintf(2, "%s: symbol not found\n", name);
143 _exit(127);
144 }
Rich Felker51e2d832011-06-18 19:48:42 -0400145 sym_size = sym->st_size;
146 }
147 do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]);
148 }
149}
150
Rich Felker6717e622011-06-28 19:40:14 -0400151/* A huge hack: to make up for the wastefulness of shared libraries
152 * needing at least a page of dirty memory even if they have no global
153 * data, we reclaim the gaps at the beginning and end of writable maps
154 * and "donate" them to the heap by setting up minimal malloc
155 * structures and then freeing them. */
156
157static void reclaim(unsigned char *base, size_t start, size_t end)
158{
159 size_t *a, *z;
160 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
161 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
162 if (start>end || end-start < 4*sizeof(size_t)) return;
163 a = (size_t *)(base + start);
164 z = (size_t *)(base + end);
165 a[-2] = 1;
166 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
167 z[1] = 1;
168 free(a);
169}
170
171static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
172{
173 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
174 if (ph->p_type!=PT_LOAD) continue;
175 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
176 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
177 reclaim(base, ph->p_vaddr+ph->p_memsz,
178 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
179 }
180}
181
Rich Felker51e2d832011-06-18 19:48:42 -0400182static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp)
183{
Rich Felker59633c72011-06-25 12:26:08 -0400184 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
Rich Felker51e2d832011-06-18 19:48:42 -0400185 size_t phsize;
186 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
187 size_t this_min, this_max;
188 off_t off_start;
189 Ehdr *eh;
190 Phdr *ph;
191 unsigned prot;
192 unsigned char *map, *base;
193 size_t dyn;
194 size_t i;
195
196 ssize_t l = read(fd, buf, sizeof buf);
197 if (l<sizeof *eh) return 0;
Rich Felker59633c72011-06-25 12:26:08 -0400198 eh = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400199 phsize = eh->e_phentsize * eh->e_phnum;
200 if (phsize + sizeof *eh > l) return 0;
201 if (eh->e_phoff + phsize > l) {
Rich Felker59633c72011-06-25 12:26:08 -0400202 l = pread(fd, buf+1, phsize, eh->e_phoff);
Rich Felker51e2d832011-06-18 19:48:42 -0400203 if (l != phsize) return 0;
204 eh->e_phoff = sizeof *eh;
205 }
206 ph = (void *)((char *)buf + eh->e_phoff);
207 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
208 if (ph->p_type == PT_DYNAMIC)
209 dyn = ph->p_vaddr;
210 if (ph->p_type != PT_LOAD) continue;
211 if (ph->p_vaddr < addr_min) {
212 addr_min = ph->p_vaddr;
213 off_start = ph->p_offset;
214 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
215 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
216 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
217 }
218 if (ph->p_vaddr+ph->p_memsz > addr_max) {
219 addr_max = ph->p_vaddr+ph->p_memsz;
220 }
221 }
222 if (!dyn) return 0;
223 addr_max += PAGE_SIZE-1;
224 addr_max &= -PAGE_SIZE;
225 addr_min &= -PAGE_SIZE;
226 off_start &= -PAGE_SIZE;
227 map_len = addr_max - addr_min + off_start;
228 /* The first time, we map too much, possibly even more than
229 * the length of the file. This is okay because we will not
230 * use the invalid part; we just need to reserve the right
231 * amount of virtual address space to map over later. */
Rich Felkerbf301002011-06-28 14:20:41 -0400232 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
Rich Felker51e2d832011-06-18 19:48:42 -0400233 if (map==MAP_FAILED) return 0;
234 base = map - addr_min;
235 ph = (void *)((char *)buf + eh->e_phoff);
236 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
237 if (ph->p_type != PT_LOAD) continue;
238 /* Reuse the existing mapping for the lowest-address LOAD */
239 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
240 this_min = ph->p_vaddr & -PAGE_SIZE;
241 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
242 off_start = ph->p_offset & -PAGE_SIZE;
243 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
244 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
245 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
246 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) {
247 munmap(map, map_len);
248 return 0;
249 }
250 if (ph->p_memsz > ph->p_filesz) {
251 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
252 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
253 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
254 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) {
255 munmap(map, map_len);
256 return 0;
257 }
258 }
259 }
Rich Felker9f174132011-06-29 00:29:08 -0400260 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
261 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
262 mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC);
263 break;
264 }
Rich Felker6717e622011-06-28 19:40:14 -0400265 if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
266 eh->e_phentsize, eh->e_phnum);
Rich Felker51e2d832011-06-18 19:48:42 -0400267 *lenp = map_len;
268 *basep = base;
269 *dynp = dyn;
270 return map;
271}
272
Rich Felker568b8072011-06-25 01:56:34 -0400273static int path_open(const char *name, const char *search)
274{
275 char buf[2*NAME_MAX+2];
Rich Felker49388f32011-06-25 17:49:16 -0400276 const char *s=search, *z;
Rich Felker568b8072011-06-25 01:56:34 -0400277 int l, fd;
Rich Felker49388f32011-06-25 17:49:16 -0400278 for (;;) {
279 while (*s==':') s++;
280 if (!*s) return -1;
Rich Felker568b8072011-06-25 01:56:34 -0400281 z = strchr(s, ':');
282 l = z ? z-s : strlen(s);
283 snprintf(buf, sizeof buf, "%.*s/%s", l, s, name);
284 if ((fd = open(buf, O_RDONLY))>=0) return fd;
Rich Felker49388f32011-06-25 17:49:16 -0400285 s += l;
Rich Felker568b8072011-06-25 01:56:34 -0400286 }
Rich Felker568b8072011-06-25 01:56:34 -0400287}
288
Rich Felkerc82f4a32012-01-23 00:57:38 -0500289static void decode_dyn(struct dso *p)
290{
291 size_t dyn[DYN_CNT] = {0};
292 decode_vec(p->dynv, dyn, DYN_CNT);
293 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
294 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
295 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
296}
297
Rich Felker51e2d832011-06-18 19:48:42 -0400298static struct dso *load_library(const char *name)
299{
300 unsigned char *base, *map;
301 size_t dyno, map_len;
302 struct dso *p;
Rich Felker51e2d832011-06-18 19:48:42 -0400303 int fd;
304 struct stat st;
305
306 /* Catch and block attempts to reload the implementation itself */
307 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
308 static const char *rp, reserved[] =
309 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
310 char *z = strchr(name, '.');
311 if (z) {
312 size_t l = z-name;
313 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
314 if (*rp) {
315 if (!libc->prev) {
316 tail->next = libc;
317 libc->prev = tail;
Rich Felker6ab444d2011-07-24 00:54:55 -0400318 tail = libc->next ? libc->next : libc;
Rich Felker51e2d832011-06-18 19:48:42 -0400319 }
320 return libc;
321 }
322 }
323 }
324 /* Search for the name to see if it's already loaded */
325 for (p=head->next; p; p=p->next) {
326 if (!strcmp(p->name, name)) {
327 p->refcnt++;
328 return p;
329 }
330 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400331 if (strchr(name, '/')) {
Rich Felker51e2d832011-06-18 19:48:42 -0400332 fd = open(name, O_RDONLY);
333 } else {
Rich Felker59ab43f2011-06-26 19:23:28 -0400334 if (strlen(name) > NAME_MAX) return 0;
Rich Felker568b8072011-06-25 01:56:34 -0400335 fd = -1;
Rich Felker191ebca2011-06-30 23:02:27 -0400336 if (r_path) fd = path_open(name, r_path);
337 if (fd < 0 && env_path) fd = path_open(name, env_path);
Rich Felker568b8072011-06-25 01:56:34 -0400338 if (fd < 0) {
339 if (!sys_path) {
340 FILE *f = fopen(ETC_LDSO_PATH, "r");
341 if (f) {
342 if (getline(&sys_path, (size_t[1]){0}, f) > 0)
343 sys_path[strlen(sys_path)-1]=0;
344 fclose(f);
345 }
346 }
347 if (sys_path) fd = path_open(name, sys_path);
348 else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib");
Rich Felker51e2d832011-06-18 19:48:42 -0400349 }
350 }
351 if (fd < 0) return 0;
352 if (fstat(fd, &st) < 0) {
353 close(fd);
354 return 0;
355 }
356 for (p=head->next; p; p=p->next) {
357 if (p->dev == st.st_dev && p->ino == st.st_ino) {
358 close(fd);
359 p->refcnt++;
360 return p;
361 }
362 }
363 map = map_library(fd, &map_len, &base, &dyno);
364 close(fd);
365 if (!map) return 0;
366 p = calloc(1, sizeof *p + strlen(name) + 1);
367 if (!p) {
368 munmap(map, map_len);
369 return 0;
370 }
371
372 p->map = map;
373 p->map_len = map_len;
374 p->base = base;
375 p->dynv = (void *)(base + dyno);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500376 decode_dyn(p);
Rich Felker51e2d832011-06-18 19:48:42 -0400377
Rich Felker51e2d832011-06-18 19:48:42 -0400378 p->dev = st.st_dev;
379 p->ino = st.st_ino;
Rich Felker51e2d832011-06-18 19:48:42 -0400380 p->refcnt = 1;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400381 p->name = p->buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400382 strcpy(p->name, name);
383
384 tail->next = p;
385 p->prev = tail;
386 tail = p;
387
388 return p;
389}
390
391static void load_deps(struct dso *p)
392{
Rich Felker59ab43f2011-06-26 19:23:28 -0400393 size_t i, ndeps=0;
394 struct dso ***deps = &p->deps, **tmp, *dep;
Rich Felker51e2d832011-06-18 19:48:42 -0400395 for (; p; p=p->next) {
396 for (i=0; p->dynv[i]; i+=2) {
Rich Felker191ebca2011-06-30 23:02:27 -0400397 if (p->dynv[i] != DT_RPATH) continue;
398 r_path = (void *)(p->strings + p->dynv[i+1]);
399 }
400 for (i=0; p->dynv[i]; i+=2) {
Rich Felker51e2d832011-06-18 19:48:42 -0400401 if (p->dynv[i] != DT_NEEDED) continue;
Rich Felker59ab43f2011-06-26 19:23:28 -0400402 dep = load_library(p->strings + p->dynv[i+1]);
403 if (!dep) {
Rich Felker6b3d5e52011-06-26 17:39:17 -0400404 if (runtime) longjmp(rtld_fail, 1);
405 dprintf(2, "%s: %m (needed by %s)\n",
406 p->strings + p->dynv[i+1], p->name);
407 _exit(127);
408 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400409 if (runtime) {
410 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
411 if (!tmp) longjmp(rtld_fail, 1);
412 tmp[ndeps++] = dep;
413 tmp[ndeps] = 0;
414 *deps = tmp;
415 }
Rich Felker51e2d832011-06-18 19:48:42 -0400416 }
Rich Felker191ebca2011-06-30 23:02:27 -0400417 r_path = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400418 }
419}
420
Rich Felker2719cc82011-08-16 00:24:36 -0400421static void load_preload(char *s)
422{
423 int tmp;
424 char *z;
425 for (z=s; *z; s=z) {
426 for ( ; *s && isspace(*s); s++);
427 for (z=s; *z && !isspace(*z); z++);
428 tmp = *z;
429 *z = 0;
430 load_library(s);
431 *z = tmp;
432 }
433}
434
Rich Felker59ab43f2011-06-26 19:23:28 -0400435static void make_global(struct dso *p)
436{
437 for (; p; p=p->next) p->global = 1;
438}
439
Rich Felker51e2d832011-06-18 19:48:42 -0400440static void reloc_all(struct dso *p)
441{
442 size_t dyn[DYN_CNT] = {0};
443 for (; p; p=p->next) {
444 if (p->relocated) continue;
445 decode_vec(p->dynv, dyn, DYN_CNT);
446 do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400447 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head);
Rich Felker51e2d832011-06-18 19:48:42 -0400448 do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400449 2, p->syms, p->strings, head);
Rich Felker51e2d832011-06-18 19:48:42 -0400450 do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400451 3, p->syms, p->strings, head);
Rich Felker368ba4a2011-06-25 00:18:19 -0400452 p->relocated = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400453 }
454}
455
Rich Felkere8dbf002011-06-25 00:47:28 -0400456static void free_all(struct dso *p)
457{
458 struct dso *n;
459 while (p) {
460 n = p->next;
461 if (p->map) free(p);
462 p = n;
463 }
464}
465
Rich Felkerc82f4a32012-01-23 00:57:38 -0500466static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
467{
468 for (; cnt--; ph = (void *)((char *)ph + stride))
469 if (ph->p_type == PT_DYNAMIC)
470 return ph->p_vaddr;
471 return 0;
472}
473
474void *__dynlink(int argc, char **argv)
Rich Felker51e2d832011-06-18 19:48:42 -0400475{
476 size_t *auxv, aux[AUX_CNT] = {0};
Rich Felker51e2d832011-06-18 19:48:42 -0400477 size_t i;
478 Phdr *phdr;
Rich Felker6717e622011-06-28 19:40:14 -0400479 Ehdr *ehdr;
Rich Felker6ab444d2011-07-24 00:54:55 -0400480 static struct dso builtin_dsos[3];
Rich Felkera53de812011-07-24 00:26:12 -0400481 struct dso *const app = builtin_dsos+0;
482 struct dso *const lib = builtin_dsos+1;
Rich Felker6ab444d2011-07-24 00:54:55 -0400483 struct dso *const vdso = builtin_dsos+2;
Rich Felker2719cc82011-08-16 00:24:36 -0400484 char *env_preload=0;
Rich Felker51e2d832011-06-18 19:48:42 -0400485
486 /* Find aux vector just past environ[] */
Rich Felker568b8072011-06-25 01:56:34 -0400487 for (i=argc+1; argv[i]; i++)
488 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
489 env_path = argv[i]+16;
Rich Felker2719cc82011-08-16 00:24:36 -0400490 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
491 env_preload = argv[i]+11;
Rich Felker51e2d832011-06-18 19:48:42 -0400492 auxv = (void *)(argv+i+1);
493
494 decode_vec(auxv, aux, AUX_CNT);
495
Rich Felker568b8072011-06-25 01:56:34 -0400496 /* Only trust user/env if kernel says we're not suid/sgid */
497 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
Rich Felkera0458832011-08-16 07:46:42 -0400498 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
Rich Felker568b8072011-06-25 01:56:34 -0400499 env_path = 0;
Rich Felker2719cc82011-08-16 00:24:36 -0400500 env_preload = 0;
Rich Felker568b8072011-06-25 01:56:34 -0400501 }
502
Rich Felkerc82f4a32012-01-23 00:57:38 -0500503 /* The dynamic linker load address is passed by the kernel
504 * in the AUX vector, so this is easy. */
505 lib->base = (void *)aux[AT_BASE];
506 lib->name = "libc.so";
507 lib->global = 1;
508 ehdr = (void *)lib->base;
509 lib->dynv = (void *)(lib->base + find_dyn(
510 (void *)(aux[AT_BASE]+ehdr->e_phoff),
511 ehdr->e_phnum, ehdr->e_phentsize));
512 decode_dyn(lib);
513
514 /* Assume base address of 0 for the main program. This is not
515 * valid for PIE code; we will have to search the PHDR to get
516 * the correct load address in the PIE case (not yet supported). */
517 app->base = 0;
518 app->name = argv[0];
519 app->global = 1;
520 app->dynv = (void *)(app->base + find_dyn(
521 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
522 decode_dyn(app);
523
524 /* Attach to vdso, if provided by the kernel */
Rich Felkercf8506a2011-08-16 08:50:03 -0400525 for (i=0; auxv[i]; i+=2) {
Rich Felkerc82f4a32012-01-23 00:57:38 -0500526 size_t vdso_base = auxv[i+1];
527 if (auxv[i] != AT_SYSINFO_EHDR) continue;
Rich Felker6ab444d2011-07-24 00:54:55 -0400528 ehdr = (void *)vdso_base;
529 phdr = (void *)(vdso_base + ehdr->e_phoff);
530 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
531 if (phdr->p_type == PT_DYNAMIC)
532 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
533 if (phdr->p_type == PT_LOAD)
534 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
535 }
Rich Felker6ab444d2011-07-24 00:54:55 -0400536 vdso->name = "linux-gate.so.1";
Rich Felker427173b2011-07-24 02:19:47 -0400537 vdso->global = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500538 decode_dyn(vdso);
Rich Felker6ab444d2011-07-24 00:54:55 -0400539 vdso->prev = lib;
540 lib->next = vdso;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500541 break;
Rich Felker6ab444d2011-07-24 00:54:55 -0400542 }
543
Rich Felkerc82f4a32012-01-23 00:57:38 -0500544 /* Initial dso chain consists only of the app. We temporarily
545 * append the dynamic linker/libc so we can relocate it, then
546 * restore the initial chain in preparation for loading third
547 * party libraries (preload/needed). */
548 head = tail = app;
549 libc = lib;
550 app->next = lib;
551 reloc_all(lib);
552 app->next = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400553
Rich Felkerc82f4a32012-01-23 00:57:38 -0500554 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
Rich Felker51e2d832011-06-18 19:48:42 -0400555
Rich Felkerc82f4a32012-01-23 00:57:38 -0500556 /* Donate unused parts of app and library mapping to malloc */
Rich Felkera53de812011-07-24 00:26:12 -0400557 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
558 ehdr = (void *)lib->base;
559 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
Rich Felker6717e622011-06-28 19:40:14 -0400560 ehdr->e_phentsize, ehdr->e_phnum);
561
Rich Felkerc82f4a32012-01-23 00:57:38 -0500562 /* Load preload/needed libraries, add their symbols to the global
563 * namespace, and perform all remaining relocations. */
Rich Felker2719cc82011-08-16 00:24:36 -0400564 if (env_preload) load_preload(env_preload);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500565 load_deps(app);
566 make_global(app);
567 reloc_all(app);
Rich Felker51e2d832011-06-18 19:48:42 -0400568
Rich Felkerc82f4a32012-01-23 00:57:38 -0500569 /* Switch to runtime mode: any further failures in the dynamic
570 * linker are a reportable failure rather than a fatal startup
571 * error. If the dynamic loader (dlopen) will not be used, free
572 * all memory used by the dynamic linker. */
Rich Felkera53de812011-07-24 00:26:12 -0400573 runtime = 1;
574 if (!rtld_used) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400575 free_all(head);
576 free(sys_path);
Rich Felkera53de812011-07-24 00:26:12 -0400577 reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos);
Rich Felker59ab43f2011-06-26 19:23:28 -0400578 }
Rich Felkere8dbf002011-06-25 00:47:28 -0400579
Rich Felker51e2d832011-06-18 19:48:42 -0400580 errno = 0;
581 return (void *)aux[AT_ENTRY];
582}
Rich Felker59ab43f2011-06-26 19:23:28 -0400583
584void *dlopen(const char *file, int mode)
585{
Rich Felker2fdea172011-07-01 22:40:00 -0400586 struct dso *volatile p, *orig_tail = tail, *next;
Rich Felker59ab43f2011-06-26 19:23:28 -0400587 size_t i;
588
589 if (!file) return head;
590
591 pthread_rwlock_wrlock(&lock);
592
593 if (setjmp(rtld_fail)) {
594 /* Clean up anything new that was (partially) loaded */
Rich Felker92ab5d82011-06-26 21:21:04 -0400595 if (p->deps) for (i=0; p->deps[i]; i++)
596 if (p->deps[i]->global < 0)
597 p->deps[i]->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400598 for (p=orig_tail->next; p; p=next) {
599 next = p->next;
600 munmap(p->map, p->map_len);
601 free(p->deps);
602 free(p);
603 }
604 tail = orig_tail;
605 tail->next = 0;
606 pthread_rwlock_unlock(&lock);
607 return 0;
608 }
609
610 p = load_library(file);
Rich Felker06933cc2011-06-26 22:09:32 -0400611 if (!p) goto end;
Rich Felker59ab43f2011-06-26 19:23:28 -0400612
613 /* First load handling */
614 if (!p->deps) {
615 load_deps(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400616 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400617 if (!p->deps[i]->global)
618 p->deps[i]->global = -1;
619 if (!p->global) p->global = -1;
Rich Felker59ab43f2011-06-26 19:23:28 -0400620 reloc_all(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400621 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400622 if (p->deps[i]->global < 0)
623 p->deps[i]->global = 0;
624 if (p->global < 0) p->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400625 }
626
627 if (mode & RTLD_GLOBAL) {
Rich Felker0e4dae32011-06-26 21:36:44 -0400628 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker59ab43f2011-06-26 19:23:28 -0400629 p->deps[i]->global = 1;
630 p->global = 1;
631 }
632
Rich Felker06933cc2011-06-26 22:09:32 -0400633end:
Rich Felker59ab43f2011-06-26 19:23:28 -0400634 pthread_rwlock_unlock(&lock);
Rich Felker59ab43f2011-06-26 19:23:28 -0400635 return p;
636}
637
Rich Felker623753a2011-08-16 00:42:13 -0400638static void *do_dlsym(struct dso *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400639{
640 size_t i;
641 uint32_t h;
642 Sym *sym;
Rich Felker623753a2011-08-16 00:42:13 -0400643 if (p == RTLD_NEXT) {
644 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
645 if (!p) p=head;
646 p=p->next;
647 }
Rich Felker97507bd2011-06-26 21:50:01 -0400648 if (p == head || p == RTLD_DEFAULT)
649 return find_sym(head, s, 0);
Rich Felker59ab43f2011-06-26 19:23:28 -0400650 h = hash(s);
651 sym = lookup(s, h, p->syms, p->hashtab, p->strings);
652 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
653 return p->base + sym->st_value;
654 if (p->deps) for (i=0; p->deps[i]; i++) {
655 sym = lookup(s, h, p->deps[i]->syms,
656 p->deps[i]->hashtab, p->deps[i]->strings);
657 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
658 return p->deps[i]->base + sym->st_value;
659 }
660 return 0;
661}
662
Rich Felker623753a2011-08-16 00:42:13 -0400663void *__dlsym(void *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400664{
665 void *res;
666 pthread_rwlock_rdlock(&lock);
Rich Felker623753a2011-08-16 00:42:13 -0400667 res = do_dlsym(p, s, ra);
Rich Felker59ab43f2011-06-26 19:23:28 -0400668 pthread_rwlock_unlock(&lock);
669 return res;
670}
671
672char *dlerror()
673{
674 return "unknown error";
675}
676
677int dlclose(void *p)
678{
679 return 0;
680}
Rich Felker4f4bf0a2011-09-18 16:42:06 -0400681#endif