blob: 02c880b4b87ecddbc65ab5221847157b84d49e15 [file] [log] [blame]
Rich Felker51e2d832011-06-18 19:48:42 -04001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <stdint.h>
6#include <elf.h>
7#include <sys/mman.h>
8#include <limits.h>
9#include <stdint.h>
10#include <fcntl.h>
11#include <sys/stat.h>
12#include <errno.h>
13#include <limits.h>
14#include <elf.h>
Rich Felker6b3d5e52011-06-26 17:39:17 -040015#include <setjmp.h>
Rich Felker59ab43f2011-06-26 19:23:28 -040016#include <pthread.h>
Rich Felker2719cc82011-08-16 00:24:36 -040017#include <ctype.h>
Rich Felker59ab43f2011-06-26 19:23:28 -040018#include <dlfcn.h>
Rich Felker51e2d832011-06-18 19:48:42 -040019
Rich Felkera9e85c02012-03-23 00:28:20 -040020static int errflag;
Rich Felkera5d10eb2012-04-23 12:03:31 -040021static char errbuf[128];
Rich Felkera9e85c02012-03-23 00:28:20 -040022
23#ifdef __PIC__
24
Rich Felker51e2d832011-06-18 19:48:42 -040025#include "reloc.h"
26
27#if ULONG_MAX == 0xffffffff
28typedef Elf32_Ehdr Ehdr;
29typedef Elf32_Phdr Phdr;
30typedef Elf32_Sym Sym;
31#define R_TYPE(x) ((x)&255)
32#define R_SYM(x) ((x)>>8)
33#else
34typedef Elf64_Ehdr Ehdr;
35typedef Elf64_Phdr Phdr;
36typedef Elf64_Sym Sym;
37#define R_TYPE(x) ((x)&0xffffffff)
38#define R_SYM(x) ((x)>>32)
39#endif
40
Rich Felker3ec8d292012-04-25 00:05:42 -040041struct debug {
42 int ver;
43 void *head;
44 void (*bp)(void);
45 int state;
46 void *base;
47};
48
49struct dso {
50 unsigned char *base;
51 char *name;
Rich Felker51e2d832011-06-18 19:48:42 -040052 size_t *dynv;
Rich Felker3ec8d292012-04-25 00:05:42 -040053 struct dso *next, *prev;
54
55 int refcnt;
Rich Felker51e2d832011-06-18 19:48:42 -040056 Sym *syms;
Rich Felker596d60c2011-06-18 22:52:01 -040057 uint32_t *hashtab;
Rich Felker51e2d832011-06-18 19:48:42 -040058 char *strings;
Rich Felker51e2d832011-06-18 19:48:42 -040059 unsigned char *map;
60 size_t map_len;
61 dev_t dev;
62 ino_t ino;
Rich Felker700a8152012-02-07 20:29:29 -050063 char global;
64 char relocated;
65 char constructed;
Rich Felker59ab43f2011-06-26 19:23:28 -040066 struct dso **deps;
Rich Felker6b3d5e52011-06-26 17:39:17 -040067 char buf[];
Rich Felker51e2d832011-06-18 19:48:42 -040068};
69
Rich Felker58aa5f42012-05-03 20:42:45 -040070void __init_ssp(size_t *);
Rich Felker60872cf2012-04-24 18:07:59 -040071
Rich Felker51e2d832011-06-18 19:48:42 -040072static struct dso *head, *tail, *libc;
Rich Felker191ebca2011-06-30 23:02:27 -040073static char *env_path, *sys_path, *r_path;
Rich Felker59ab43f2011-06-26 19:23:28 -040074static int rtld_used;
Rich Felker60872cf2012-04-24 18:07:59 -040075static int ssp_used;
Rich Felker6b3d5e52011-06-26 17:39:17 -040076static int runtime;
77static jmp_buf rtld_fail;
Rich Felker59ab43f2011-06-26 19:23:28 -040078static pthread_rwlock_t lock;
Rich Felker3ec8d292012-04-25 00:05:42 -040079static struct debug debug;
80
81struct debug *_dl_debug_addr = &debug;
Rich Felker51e2d832011-06-18 19:48:42 -040082
Rich Felkera0458832011-08-16 07:46:42 -040083#define AUX_CNT 24
Rich Felker51e2d832011-06-18 19:48:42 -040084#define DYN_CNT 34
85
86static void decode_vec(size_t *v, size_t *a, size_t cnt)
87{
88 memset(a, 0, cnt*sizeof(size_t));
89 for (; v[0]; v+=2) if (v[0]<cnt) {
90 a[0] |= 1ULL<<v[0];
91 a[v[0]] = v[1];
92 }
93}
94
Rich Felker2adf2fb2012-01-17 00:34:58 -050095static uint32_t hash(const char *s0)
Rich Felker51e2d832011-06-18 19:48:42 -040096{
Rich Felker2adf2fb2012-01-17 00:34:58 -050097 const unsigned char *s = (void *)s0;
Rich Felker51e2d832011-06-18 19:48:42 -040098 uint_fast32_t h = 0;
99 while (*s) {
100 h = 16*h + *s++;
101 h ^= h>>24 & 0xf0;
102 }
103 return h & 0xfffffff;
104}
105
Rich Felker596d60c2011-06-18 22:52:01 -0400106static Sym *lookup(const char *s, uint32_t h, Sym *syms, uint32_t *hashtab, char *strings)
Rich Felker51e2d832011-06-18 19:48:42 -0400107{
108 size_t i;
109 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
110 if (!strcmp(s, strings+syms[i].st_name))
111 return syms+i;
112 }
113 return 0;
114}
115
116#define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
Rich Felker427173b2011-07-24 02:19:47 -0400117#define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
Rich Felker51e2d832011-06-18 19:48:42 -0400118
119static void *find_sym(struct dso *dso, const char *s, int need_def)
120{
121 uint32_t h = hash(s);
Rich Felker427173b2011-07-24 02:19:47 -0400122 void *def = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400123 if (h==0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1;
124 if (h==0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1;
Rich Felker60872cf2012-04-24 18:07:59 -0400125 if (h==0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400126 for (; dso; dso=dso->next) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400127 Sym *sym;
128 if (!dso->global) continue;
129 sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings);
Rich Felker51e2d832011-06-18 19:48:42 -0400130 if (sym && (!need_def || sym->st_shndx) && sym->st_value
Rich Felker427173b2011-07-24 02:19:47 -0400131 && (1<<(sym->st_info&0xf) & OK_TYPES)
132 && (1<<(sym->st_info>>4) & OK_BINDS)) {
Rich Felkere01ac672011-07-25 09:22:05 -0400133 if (def && sym->st_info>>4 == STB_WEAK) continue;
Rich Felker427173b2011-07-24 02:19:47 -0400134 def = dso->base + sym->st_value;
135 if (sym->st_info>>4 == STB_GLOBAL) break;
136 }
Rich Felker51e2d832011-06-18 19:48:42 -0400137 }
Rich Felker427173b2011-07-24 02:19:47 -0400138 return def;
Rich Felker51e2d832011-06-18 19:48:42 -0400139}
140
141static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso)
142{
143 Sym *sym;
144 const char *name;
145 size_t sym_val, sym_size;
146 size_t *reloc_addr;
147 void *ctx;
148 int type;
149 int sym_index;
150
151 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
152 reloc_addr = (void *)(base + rel[0]);
153 type = R_TYPE(rel[1]);
154 sym_index = R_SYM(rel[1]);
155 if (sym_index) {
156 sym = syms + sym_index;
157 name = strings + sym->st_name;
158 ctx = IS_COPY(type) ? dso->next : dso;
Rich Felker32de61e2011-06-25 22:36:21 -0400159 sym_val = (size_t)find_sym(ctx, name, IS_PLT(type));
Rich Felker6b3d5e52011-06-26 17:39:17 -0400160 if (!sym_val && sym->st_info>>4 != STB_WEAK) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400161 snprintf(errbuf, sizeof errbuf,
162 "Error relocating %s: %s: symbol not found",
163 dso->name, name);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400164 if (runtime) longjmp(rtld_fail, 1);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400165 dprintf(2, "%s\n", errbuf);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400166 _exit(127);
167 }
Rich Felker51e2d832011-06-18 19:48:42 -0400168 sym_size = sym->st_size;
169 }
170 do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]);
171 }
172}
173
Rich Felker6717e622011-06-28 19:40:14 -0400174/* A huge hack: to make up for the wastefulness of shared libraries
175 * needing at least a page of dirty memory even if they have no global
176 * data, we reclaim the gaps at the beginning and end of writable maps
177 * and "donate" them to the heap by setting up minimal malloc
178 * structures and then freeing them. */
179
180static void reclaim(unsigned char *base, size_t start, size_t end)
181{
182 size_t *a, *z;
183 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
184 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
185 if (start>end || end-start < 4*sizeof(size_t)) return;
186 a = (size_t *)(base + start);
187 z = (size_t *)(base + end);
188 a[-2] = 1;
189 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
190 z[1] = 1;
191 free(a);
192}
193
194static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
195{
196 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
197 if (ph->p_type!=PT_LOAD) continue;
198 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
199 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
200 reclaim(base, ph->p_vaddr+ph->p_memsz,
201 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
202 }
203}
204
Rich Felker51e2d832011-06-18 19:48:42 -0400205static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp)
206{
Rich Felker59633c72011-06-25 12:26:08 -0400207 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
Rich Felker51e2d832011-06-18 19:48:42 -0400208 size_t phsize;
209 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
210 size_t this_min, this_max;
211 off_t off_start;
212 Ehdr *eh;
213 Phdr *ph;
214 unsigned prot;
215 unsigned char *map, *base;
216 size_t dyn;
217 size_t i;
218
219 ssize_t l = read(fd, buf, sizeof buf);
220 if (l<sizeof *eh) return 0;
Rich Felker59633c72011-06-25 12:26:08 -0400221 eh = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400222 phsize = eh->e_phentsize * eh->e_phnum;
223 if (phsize + sizeof *eh > l) return 0;
224 if (eh->e_phoff + phsize > l) {
Rich Felker59633c72011-06-25 12:26:08 -0400225 l = pread(fd, buf+1, phsize, eh->e_phoff);
Rich Felker51e2d832011-06-18 19:48:42 -0400226 if (l != phsize) return 0;
227 eh->e_phoff = sizeof *eh;
228 }
229 ph = (void *)((char *)buf + eh->e_phoff);
230 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
231 if (ph->p_type == PT_DYNAMIC)
232 dyn = ph->p_vaddr;
233 if (ph->p_type != PT_LOAD) continue;
234 if (ph->p_vaddr < addr_min) {
235 addr_min = ph->p_vaddr;
236 off_start = ph->p_offset;
237 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
238 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
239 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
240 }
241 if (ph->p_vaddr+ph->p_memsz > addr_max) {
242 addr_max = ph->p_vaddr+ph->p_memsz;
243 }
244 }
245 if (!dyn) return 0;
246 addr_max += PAGE_SIZE-1;
247 addr_max &= -PAGE_SIZE;
248 addr_min &= -PAGE_SIZE;
249 off_start &= -PAGE_SIZE;
250 map_len = addr_max - addr_min + off_start;
251 /* The first time, we map too much, possibly even more than
252 * the length of the file. This is okay because we will not
253 * use the invalid part; we just need to reserve the right
254 * amount of virtual address space to map over later. */
Rich Felkerbf301002011-06-28 14:20:41 -0400255 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
Rich Felker51e2d832011-06-18 19:48:42 -0400256 if (map==MAP_FAILED) return 0;
257 base = map - addr_min;
258 ph = (void *)((char *)buf + eh->e_phoff);
259 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
260 if (ph->p_type != PT_LOAD) continue;
261 /* Reuse the existing mapping for the lowest-address LOAD */
262 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
263 this_min = ph->p_vaddr & -PAGE_SIZE;
264 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
265 off_start = ph->p_offset & -PAGE_SIZE;
266 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
267 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
268 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
269 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) {
270 munmap(map, map_len);
271 return 0;
272 }
273 if (ph->p_memsz > ph->p_filesz) {
274 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
275 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
276 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
277 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) {
278 munmap(map, map_len);
279 return 0;
280 }
281 }
282 }
Rich Felker9f174132011-06-29 00:29:08 -0400283 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
284 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
285 mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC);
286 break;
287 }
Rich Felker6717e622011-06-28 19:40:14 -0400288 if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
289 eh->e_phentsize, eh->e_phnum);
Rich Felker51e2d832011-06-18 19:48:42 -0400290 *lenp = map_len;
291 *basep = base;
292 *dynp = dyn;
293 return map;
294}
295
Rich Felker568b8072011-06-25 01:56:34 -0400296static int path_open(const char *name, const char *search)
297{
298 char buf[2*NAME_MAX+2];
Rich Felker49388f32011-06-25 17:49:16 -0400299 const char *s=search, *z;
Rich Felker568b8072011-06-25 01:56:34 -0400300 int l, fd;
Rich Felker49388f32011-06-25 17:49:16 -0400301 for (;;) {
302 while (*s==':') s++;
303 if (!*s) return -1;
Rich Felker568b8072011-06-25 01:56:34 -0400304 z = strchr(s, ':');
305 l = z ? z-s : strlen(s);
306 snprintf(buf, sizeof buf, "%.*s/%s", l, s, name);
307 if ((fd = open(buf, O_RDONLY))>=0) return fd;
Rich Felker49388f32011-06-25 17:49:16 -0400308 s += l;
Rich Felker568b8072011-06-25 01:56:34 -0400309 }
Rich Felker568b8072011-06-25 01:56:34 -0400310}
311
Rich Felkerc82f4a32012-01-23 00:57:38 -0500312static void decode_dyn(struct dso *p)
313{
314 size_t dyn[DYN_CNT] = {0};
315 decode_vec(p->dynv, dyn, DYN_CNT);
316 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
317 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
318 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
319}
320
Rich Felker51e2d832011-06-18 19:48:42 -0400321static struct dso *load_library(const char *name)
322{
323 unsigned char *base, *map;
324 size_t dyno, map_len;
325 struct dso *p;
Rich Felker51e2d832011-06-18 19:48:42 -0400326 int fd;
327 struct stat st;
328
329 /* Catch and block attempts to reload the implementation itself */
330 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
331 static const char *rp, reserved[] =
332 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
333 char *z = strchr(name, '.');
334 if (z) {
335 size_t l = z-name;
336 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
337 if (*rp) {
338 if (!libc->prev) {
339 tail->next = libc;
340 libc->prev = tail;
Rich Felker6ab444d2011-07-24 00:54:55 -0400341 tail = libc->next ? libc->next : libc;
Rich Felker51e2d832011-06-18 19:48:42 -0400342 }
343 return libc;
344 }
345 }
346 }
347 /* Search for the name to see if it's already loaded */
348 for (p=head->next; p; p=p->next) {
349 if (!strcmp(p->name, name)) {
350 p->refcnt++;
351 return p;
352 }
353 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400354 if (strchr(name, '/')) {
Rich Felker51e2d832011-06-18 19:48:42 -0400355 fd = open(name, O_RDONLY);
356 } else {
Rich Felker59ab43f2011-06-26 19:23:28 -0400357 if (strlen(name) > NAME_MAX) return 0;
Rich Felker568b8072011-06-25 01:56:34 -0400358 fd = -1;
Rich Felker191ebca2011-06-30 23:02:27 -0400359 if (r_path) fd = path_open(name, r_path);
360 if (fd < 0 && env_path) fd = path_open(name, env_path);
Rich Felker568b8072011-06-25 01:56:34 -0400361 if (fd < 0) {
362 if (!sys_path) {
363 FILE *f = fopen(ETC_LDSO_PATH, "r");
364 if (f) {
365 if (getline(&sys_path, (size_t[1]){0}, f) > 0)
366 sys_path[strlen(sys_path)-1]=0;
367 fclose(f);
368 }
369 }
370 if (sys_path) fd = path_open(name, sys_path);
371 else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib");
Rich Felker51e2d832011-06-18 19:48:42 -0400372 }
373 }
374 if (fd < 0) return 0;
375 if (fstat(fd, &st) < 0) {
376 close(fd);
377 return 0;
378 }
379 for (p=head->next; p; p=p->next) {
380 if (p->dev == st.st_dev && p->ino == st.st_ino) {
381 close(fd);
382 p->refcnt++;
383 return p;
384 }
385 }
386 map = map_library(fd, &map_len, &base, &dyno);
387 close(fd);
388 if (!map) return 0;
389 p = calloc(1, sizeof *p + strlen(name) + 1);
390 if (!p) {
391 munmap(map, map_len);
392 return 0;
393 }
394
395 p->map = map;
396 p->map_len = map_len;
397 p->base = base;
398 p->dynv = (void *)(base + dyno);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500399 decode_dyn(p);
Rich Felker51e2d832011-06-18 19:48:42 -0400400
Rich Felker51e2d832011-06-18 19:48:42 -0400401 p->dev = st.st_dev;
402 p->ino = st.st_ino;
Rich Felker51e2d832011-06-18 19:48:42 -0400403 p->refcnt = 1;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400404 p->name = p->buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400405 strcpy(p->name, name);
406
407 tail->next = p;
408 p->prev = tail;
409 tail = p;
410
411 return p;
412}
413
414static void load_deps(struct dso *p)
415{
Rich Felker59ab43f2011-06-26 19:23:28 -0400416 size_t i, ndeps=0;
417 struct dso ***deps = &p->deps, **tmp, *dep;
Rich Felker51e2d832011-06-18 19:48:42 -0400418 for (; p; p=p->next) {
419 for (i=0; p->dynv[i]; i+=2) {
Rich Felker191ebca2011-06-30 23:02:27 -0400420 if (p->dynv[i] != DT_RPATH) continue;
421 r_path = (void *)(p->strings + p->dynv[i+1]);
422 }
423 for (i=0; p->dynv[i]; i+=2) {
Rich Felker51e2d832011-06-18 19:48:42 -0400424 if (p->dynv[i] != DT_NEEDED) continue;
Rich Felker59ab43f2011-06-26 19:23:28 -0400425 dep = load_library(p->strings + p->dynv[i+1]);
426 if (!dep) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400427 snprintf(errbuf, sizeof errbuf,
428 "Error loading shared library %s: %m (needed by %s)",
Rich Felker6b3d5e52011-06-26 17:39:17 -0400429 p->strings + p->dynv[i+1], p->name);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400430 if (runtime) longjmp(rtld_fail, 1);
431 dprintf(2, "%s\n", errbuf);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400432 _exit(127);
433 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400434 if (runtime) {
435 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
436 if (!tmp) longjmp(rtld_fail, 1);
437 tmp[ndeps++] = dep;
438 tmp[ndeps] = 0;
439 *deps = tmp;
440 }
Rich Felker51e2d832011-06-18 19:48:42 -0400441 }
Rich Felker191ebca2011-06-30 23:02:27 -0400442 r_path = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400443 }
444}
445
Rich Felker2719cc82011-08-16 00:24:36 -0400446static void load_preload(char *s)
447{
448 int tmp;
449 char *z;
450 for (z=s; *z; s=z) {
451 for ( ; *s && isspace(*s); s++);
452 for (z=s; *z && !isspace(*z); z++);
453 tmp = *z;
454 *z = 0;
455 load_library(s);
456 *z = tmp;
457 }
458}
459
Rich Felker59ab43f2011-06-26 19:23:28 -0400460static void make_global(struct dso *p)
461{
462 for (; p; p=p->next) p->global = 1;
463}
464
Rich Felker51e2d832011-06-18 19:48:42 -0400465static void reloc_all(struct dso *p)
466{
467 size_t dyn[DYN_CNT] = {0};
468 for (; p; p=p->next) {
469 if (p->relocated) continue;
470 decode_vec(p->dynv, dyn, DYN_CNT);
471 do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400472 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head);
Rich Felker51e2d832011-06-18 19:48:42 -0400473 do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400474 2, p->syms, p->strings, head);
Rich Felker51e2d832011-06-18 19:48:42 -0400475 do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400476 3, p->syms, p->strings, head);
Rich Felker368ba4a2011-06-25 00:18:19 -0400477 p->relocated = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400478 }
479}
480
Rich Felkere8dbf002011-06-25 00:47:28 -0400481static void free_all(struct dso *p)
482{
483 struct dso *n;
484 while (p) {
485 n = p->next;
486 if (p->map) free(p);
487 p = n;
488 }
489}
490
Rich Felkerc82f4a32012-01-23 00:57:38 -0500491static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
492{
493 for (; cnt--; ph = (void *)((char *)ph + stride))
494 if (ph->p_type == PT_DYNAMIC)
495 return ph->p_vaddr;
496 return 0;
497}
498
Rich Felker4ce3cb52012-02-06 14:39:09 -0500499static void do_init_fini(struct dso *p)
500{
501 size_t dyn[DYN_CNT] = {0};
502 for (; p; p=p->prev) {
503 if (p->constructed) return;
504 decode_vec(p->dynv, dyn, DYN_CNT);
505 if (dyn[0] & (1<<DT_FINI))
506 atexit((void (*)(void))(p->base + dyn[DT_FINI]));
507 if (dyn[0] & (1<<DT_INIT))
508 ((void (*)(void))(p->base + dyn[DT_INIT]))();
509 p->constructed = 1;
510 }
511}
512
Rich Felker3ec8d292012-04-25 00:05:42 -0400513void _dl_debug_state(void)
514{
515}
516
Rich Felkerc82f4a32012-01-23 00:57:38 -0500517void *__dynlink(int argc, char **argv)
Rich Felker51e2d832011-06-18 19:48:42 -0400518{
519 size_t *auxv, aux[AUX_CNT] = {0};
Rich Felker51e2d832011-06-18 19:48:42 -0400520 size_t i;
521 Phdr *phdr;
Rich Felker6717e622011-06-28 19:40:14 -0400522 Ehdr *ehdr;
Rich Felker6ab444d2011-07-24 00:54:55 -0400523 static struct dso builtin_dsos[3];
Rich Felkera53de812011-07-24 00:26:12 -0400524 struct dso *const app = builtin_dsos+0;
525 struct dso *const lib = builtin_dsos+1;
Rich Felker6ab444d2011-07-24 00:54:55 -0400526 struct dso *const vdso = builtin_dsos+2;
Rich Felker2719cc82011-08-16 00:24:36 -0400527 char *env_preload=0;
Rich Felker51e2d832011-06-18 19:48:42 -0400528
529 /* Find aux vector just past environ[] */
Rich Felker568b8072011-06-25 01:56:34 -0400530 for (i=argc+1; argv[i]; i++)
531 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
532 env_path = argv[i]+16;
Rich Felker2719cc82011-08-16 00:24:36 -0400533 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
534 env_preload = argv[i]+11;
Rich Felker51e2d832011-06-18 19:48:42 -0400535 auxv = (void *)(argv+i+1);
536
537 decode_vec(auxv, aux, AUX_CNT);
538
Rich Felker568b8072011-06-25 01:56:34 -0400539 /* Only trust user/env if kernel says we're not suid/sgid */
540 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
Rich Felkera0458832011-08-16 07:46:42 -0400541 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
Rich Felker568b8072011-06-25 01:56:34 -0400542 env_path = 0;
Rich Felker2719cc82011-08-16 00:24:36 -0400543 env_preload = 0;
Rich Felker568b8072011-06-25 01:56:34 -0400544 }
545
Rich Felkerc82f4a32012-01-23 00:57:38 -0500546 /* The dynamic linker load address is passed by the kernel
547 * in the AUX vector, so this is easy. */
548 lib->base = (void *)aux[AT_BASE];
549 lib->name = "libc.so";
550 lib->global = 1;
551 ehdr = (void *)lib->base;
552 lib->dynv = (void *)(lib->base + find_dyn(
553 (void *)(aux[AT_BASE]+ehdr->e_phoff),
554 ehdr->e_phnum, ehdr->e_phentsize));
555 decode_dyn(lib);
556
Rich Felkere12fe652012-01-23 02:02:59 -0500557 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
Rich Felkerc82f4a32012-01-23 00:57:38 -0500558 app->base = 0;
Rich Felkere12fe652012-01-23 02:02:59 -0500559 phdr = (void *)aux[AT_PHDR];
560 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
561 if (phdr->p_type == PT_PHDR)
562 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
563 }
Rich Felkerc82f4a32012-01-23 00:57:38 -0500564 app->name = argv[0];
565 app->global = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500566 app->constructed = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500567 app->dynv = (void *)(app->base + find_dyn(
568 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
569 decode_dyn(app);
570
571 /* Attach to vdso, if provided by the kernel */
Rich Felkercf8506a2011-08-16 08:50:03 -0400572 for (i=0; auxv[i]; i+=2) {
Rich Felkerc82f4a32012-01-23 00:57:38 -0500573 size_t vdso_base = auxv[i+1];
574 if (auxv[i] != AT_SYSINFO_EHDR) continue;
Rich Felker6ab444d2011-07-24 00:54:55 -0400575 ehdr = (void *)vdso_base;
576 phdr = (void *)(vdso_base + ehdr->e_phoff);
577 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
578 if (phdr->p_type == PT_DYNAMIC)
579 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
580 if (phdr->p_type == PT_LOAD)
581 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
582 }
Rich Felker6ab444d2011-07-24 00:54:55 -0400583 vdso->name = "linux-gate.so.1";
Rich Felker427173b2011-07-24 02:19:47 -0400584 vdso->global = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500585 decode_dyn(vdso);
Rich Felker6ab444d2011-07-24 00:54:55 -0400586 vdso->prev = lib;
587 lib->next = vdso;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500588 break;
Rich Felker6ab444d2011-07-24 00:54:55 -0400589 }
590
Rich Felkerc82f4a32012-01-23 00:57:38 -0500591 /* Initial dso chain consists only of the app. We temporarily
592 * append the dynamic linker/libc so we can relocate it, then
593 * restore the initial chain in preparation for loading third
594 * party libraries (preload/needed). */
595 head = tail = app;
596 libc = lib;
597 app->next = lib;
598 reloc_all(lib);
599 app->next = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400600
Rich Felkerc82f4a32012-01-23 00:57:38 -0500601 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
Rich Felker51e2d832011-06-18 19:48:42 -0400602
Rich Felkerc82f4a32012-01-23 00:57:38 -0500603 /* Donate unused parts of app and library mapping to malloc */
Rich Felkera53de812011-07-24 00:26:12 -0400604 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
605 ehdr = (void *)lib->base;
606 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
Rich Felker6717e622011-06-28 19:40:14 -0400607 ehdr->e_phentsize, ehdr->e_phnum);
608
Rich Felkerc82f4a32012-01-23 00:57:38 -0500609 /* Load preload/needed libraries, add their symbols to the global
Rich Felkerfd7015d2012-01-23 18:32:40 -0500610 * namespace, and perform all remaining relocations. The main
611 * program must be relocated LAST since it may contain copy
612 * relocations which depend on libraries' relocations. */
Rich Felker2719cc82011-08-16 00:24:36 -0400613 if (env_preload) load_preload(env_preload);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500614 load_deps(app);
615 make_global(app);
Rich Felkerfd7015d2012-01-23 18:32:40 -0500616 reloc_all(app->next);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500617 reloc_all(app);
Rich Felker51e2d832011-06-18 19:48:42 -0400618
Rich Felkerc82f4a32012-01-23 00:57:38 -0500619 /* Switch to runtime mode: any further failures in the dynamic
620 * linker are a reportable failure rather than a fatal startup
621 * error. If the dynamic loader (dlopen) will not be used, free
622 * all memory used by the dynamic linker. */
Rich Felkera53de812011-07-24 00:26:12 -0400623 runtime = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500624
Rich Felker3ec8d292012-04-25 00:05:42 -0400625 for (i=0; app->dynv[i]; i+=2)
626 if (app->dynv[i]==DT_DEBUG)
627 app->dynv[i+1] = (size_t)&debug;
628 debug.ver = 1;
629 debug.bp = _dl_debug_state;
630 debug.head = head;
631 debug.base = lib->base;
632 debug.state = 0;
633 _dl_debug_state();
634
Rich Felker58aa5f42012-05-03 20:42:45 -0400635 if (ssp_used) __init_ssp(auxv);
636
Rich Felker4ce3cb52012-02-06 14:39:09 -0500637 do_init_fini(tail);
638
Rich Felkera53de812011-07-24 00:26:12 -0400639 if (!rtld_used) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400640 free_all(head);
641 free(sys_path);
Rich Felkera53de812011-07-24 00:26:12 -0400642 reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos);
Rich Felker59ab43f2011-06-26 19:23:28 -0400643 }
Rich Felkere8dbf002011-06-25 00:47:28 -0400644
Rich Felker51e2d832011-06-18 19:48:42 -0400645 errno = 0;
646 return (void *)aux[AT_ENTRY];
647}
Rich Felker59ab43f2011-06-26 19:23:28 -0400648
649void *dlopen(const char *file, int mode)
650{
Rich Felker2fdea172011-07-01 22:40:00 -0400651 struct dso *volatile p, *orig_tail = tail, *next;
Rich Felker59ab43f2011-06-26 19:23:28 -0400652 size_t i;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500653 int cs;
Rich Felker59ab43f2011-06-26 19:23:28 -0400654
655 if (!file) return head;
656
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500657 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker59ab43f2011-06-26 19:23:28 -0400658 pthread_rwlock_wrlock(&lock);
659
660 if (setjmp(rtld_fail)) {
661 /* Clean up anything new that was (partially) loaded */
Rich Felker92ab5d82011-06-26 21:21:04 -0400662 if (p->deps) for (i=0; p->deps[i]; i++)
663 if (p->deps[i]->global < 0)
664 p->deps[i]->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400665 for (p=orig_tail->next; p; p=next) {
666 next = p->next;
667 munmap(p->map, p->map_len);
668 free(p->deps);
669 free(p);
670 }
671 tail = orig_tail;
672 tail->next = 0;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500673 p = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400674 errflag = 1;
675 goto end;
Rich Felkera9e85c02012-03-23 00:28:20 -0400676 } else p = load_library(file);
677
678 if (!p) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400679 snprintf(errbuf, sizeof errbuf,
680 "Error loading shared library %s: %m", file);
Rich Felkera9e85c02012-03-23 00:28:20 -0400681 errflag = 1;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500682 goto end;
Rich Felker59ab43f2011-06-26 19:23:28 -0400683 }
684
Rich Felker59ab43f2011-06-26 19:23:28 -0400685 /* First load handling */
686 if (!p->deps) {
687 load_deps(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400688 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400689 if (!p->deps[i]->global)
690 p->deps[i]->global = -1;
691 if (!p->global) p->global = -1;
Rich Felker59ab43f2011-06-26 19:23:28 -0400692 reloc_all(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400693 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400694 if (p->deps[i]->global < 0)
695 p->deps[i]->global = 0;
696 if (p->global < 0) p->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400697 }
698
699 if (mode & RTLD_GLOBAL) {
Rich Felker0e4dae32011-06-26 21:36:44 -0400700 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker59ab43f2011-06-26 19:23:28 -0400701 p->deps[i]->global = 1;
702 p->global = 1;
703 }
704
Rich Felker3ec8d292012-04-25 00:05:42 -0400705 _dl_debug_state();
706
Rich Felkerce4d97e2012-02-06 17:57:29 -0500707 do_init_fini(tail);
Rich Felker06933cc2011-06-26 22:09:32 -0400708end:
Rich Felker59ab43f2011-06-26 19:23:28 -0400709 pthread_rwlock_unlock(&lock);
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500710 pthread_setcancelstate(cs, 0);
Rich Felker59ab43f2011-06-26 19:23:28 -0400711 return p;
712}
713
Rich Felker623753a2011-08-16 00:42:13 -0400714static void *do_dlsym(struct dso *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400715{
716 size_t i;
717 uint32_t h;
718 Sym *sym;
Rich Felker623753a2011-08-16 00:42:13 -0400719 if (p == RTLD_NEXT) {
720 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
721 if (!p) p=head;
722 p=p->next;
723 }
Rich Felkera9e85c02012-03-23 00:28:20 -0400724 if (p == head || p == RTLD_DEFAULT) {
725 void *res = find_sym(head, s, 0);
Rich Felker4027f4e2012-05-04 20:18:18 -0400726 if (!res) goto failed;
Rich Felkera9e85c02012-03-23 00:28:20 -0400727 return res;
728 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400729 h = hash(s);
730 sym = lookup(s, h, p->syms, p->hashtab, p->strings);
731 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
732 return p->base + sym->st_value;
733 if (p->deps) for (i=0; p->deps[i]; i++) {
734 sym = lookup(s, h, p->deps[i]->syms,
735 p->deps[i]->hashtab, p->deps[i]->strings);
736 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
737 return p->deps[i]->base + sym->st_value;
738 }
Rich Felker4027f4e2012-05-04 20:18:18 -0400739failed:
Rich Felkera9e85c02012-03-23 00:28:20 -0400740 errflag = 1;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400741 snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
Rich Felker59ab43f2011-06-26 19:23:28 -0400742 return 0;
743}
744
Rich Felker623753a2011-08-16 00:42:13 -0400745void *__dlsym(void *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400746{
747 void *res;
748 pthread_rwlock_rdlock(&lock);
Rich Felker623753a2011-08-16 00:42:13 -0400749 res = do_dlsym(p, s, ra);
Rich Felker59ab43f2011-06-26 19:23:28 -0400750 pthread_rwlock_unlock(&lock);
751 return res;
752}
Rich Felker5a09a532012-02-03 03:16:07 -0500753#else
754void *dlopen(const char *file, int mode)
755{
756 return 0;
757}
758void *__dlsym(void *p, const char *s, void *ra)
759{
760 return 0;
761}
762#endif
Rich Felker59ab43f2011-06-26 19:23:28 -0400763
764char *dlerror()
765{
Rich Felkera9e85c02012-03-23 00:28:20 -0400766 if (!errflag) return 0;
767 errflag = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400768 return errbuf;
Rich Felker59ab43f2011-06-26 19:23:28 -0400769}
770
771int dlclose(void *p)
772{
773 return 0;
774}