blob: 820e8a36a3f88460681e26caa95f5bf7d09efe03 [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 Felker5c1909a2012-05-27 16:01:44 -040067 char *shortname;
Rich Felker6b3d5e52011-06-26 17:39:17 -040068 char buf[];
Rich Felker51e2d832011-06-18 19:48:42 -040069};
70
Rich Felker58aa5f42012-05-03 20:42:45 -040071void __init_ssp(size_t *);
Rich Felker60872cf2012-04-24 18:07:59 -040072
Rich Felker51e2d832011-06-18 19:48:42 -040073static struct dso *head, *tail, *libc;
Rich Felker191ebca2011-06-30 23:02:27 -040074static char *env_path, *sys_path, *r_path;
Rich Felker59ab43f2011-06-26 19:23:28 -040075static int rtld_used;
Rich Felker60872cf2012-04-24 18:07:59 -040076static int ssp_used;
Rich Felker6b3d5e52011-06-26 17:39:17 -040077static int runtime;
Rich Felker5c1909a2012-05-27 16:01:44 -040078static int ldd_mode;
Rich Felker6b3d5e52011-06-26 17:39:17 -040079static jmp_buf rtld_fail;
Rich Felker59ab43f2011-06-26 19:23:28 -040080static pthread_rwlock_t lock;
Rich Felker3ec8d292012-04-25 00:05:42 -040081static struct debug debug;
82
83struct debug *_dl_debug_addr = &debug;
Rich Felker51e2d832011-06-18 19:48:42 -040084
Rich Felkera0458832011-08-16 07:46:42 -040085#define AUX_CNT 24
Rich Felker51e2d832011-06-18 19:48:42 -040086#define DYN_CNT 34
87
88static void decode_vec(size_t *v, size_t *a, size_t cnt)
89{
90 memset(a, 0, cnt*sizeof(size_t));
91 for (; v[0]; v+=2) if (v[0]<cnt) {
92 a[0] |= 1ULL<<v[0];
93 a[v[0]] = v[1];
94 }
95}
96
Rich Felker2adf2fb2012-01-17 00:34:58 -050097static uint32_t hash(const char *s0)
Rich Felker51e2d832011-06-18 19:48:42 -040098{
Rich Felker2adf2fb2012-01-17 00:34:58 -050099 const unsigned char *s = (void *)s0;
Rich Felker51e2d832011-06-18 19:48:42 -0400100 uint_fast32_t h = 0;
101 while (*s) {
102 h = 16*h + *s++;
103 h ^= h>>24 & 0xf0;
104 }
105 return h & 0xfffffff;
106}
107
Rich Felker596d60c2011-06-18 22:52:01 -0400108static Sym *lookup(const char *s, uint32_t h, Sym *syms, uint32_t *hashtab, char *strings)
Rich Felker51e2d832011-06-18 19:48:42 -0400109{
110 size_t i;
111 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
112 if (!strcmp(s, strings+syms[i].st_name))
113 return syms+i;
114 }
115 return 0;
116}
117
118#define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
Rich Felker427173b2011-07-24 02:19:47 -0400119#define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
Rich Felker51e2d832011-06-18 19:48:42 -0400120
121static void *find_sym(struct dso *dso, const char *s, int need_def)
122{
123 uint32_t h = hash(s);
Rich Felker427173b2011-07-24 02:19:47 -0400124 void *def = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400125 if (h==0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1;
126 if (h==0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1;
Rich Felker60872cf2012-04-24 18:07:59 -0400127 if (h==0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400128 for (; dso; dso=dso->next) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400129 Sym *sym;
130 if (!dso->global) continue;
131 sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings);
Rich Felker51e2d832011-06-18 19:48:42 -0400132 if (sym && (!need_def || sym->st_shndx) && sym->st_value
Rich Felker427173b2011-07-24 02:19:47 -0400133 && (1<<(sym->st_info&0xf) & OK_TYPES)
134 && (1<<(sym->st_info>>4) & OK_BINDS)) {
Rich Felkere01ac672011-07-25 09:22:05 -0400135 if (def && sym->st_info>>4 == STB_WEAK) continue;
Rich Felker427173b2011-07-24 02:19:47 -0400136 def = dso->base + sym->st_value;
137 if (sym->st_info>>4 == STB_GLOBAL) break;
138 }
Rich Felker51e2d832011-06-18 19:48:42 -0400139 }
Rich Felker427173b2011-07-24 02:19:47 -0400140 return def;
Rich Felker51e2d832011-06-18 19:48:42 -0400141}
142
143static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso)
144{
145 Sym *sym;
146 const char *name;
147 size_t sym_val, sym_size;
148 size_t *reloc_addr;
149 void *ctx;
150 int type;
151 int sym_index;
152
153 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
154 reloc_addr = (void *)(base + rel[0]);
155 type = R_TYPE(rel[1]);
156 sym_index = R_SYM(rel[1]);
157 if (sym_index) {
158 sym = syms + sym_index;
159 name = strings + sym->st_name;
160 ctx = IS_COPY(type) ? dso->next : dso;
Rich Felker32de61e2011-06-25 22:36:21 -0400161 sym_val = (size_t)find_sym(ctx, name, IS_PLT(type));
Rich Felker6b3d5e52011-06-26 17:39:17 -0400162 if (!sym_val && sym->st_info>>4 != STB_WEAK) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400163 snprintf(errbuf, sizeof errbuf,
164 "Error relocating %s: %s: symbol not found",
165 dso->name, name);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400166 if (runtime) longjmp(rtld_fail, 1);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400167 dprintf(2, "%s\n", errbuf);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400168 _exit(127);
169 }
Rich Felker51e2d832011-06-18 19:48:42 -0400170 sym_size = sym->st_size;
171 }
172 do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]);
173 }
174}
175
Rich Felker6717e622011-06-28 19:40:14 -0400176/* A huge hack: to make up for the wastefulness of shared libraries
177 * needing at least a page of dirty memory even if they have no global
178 * data, we reclaim the gaps at the beginning and end of writable maps
179 * and "donate" them to the heap by setting up minimal malloc
180 * structures and then freeing them. */
181
182static void reclaim(unsigned char *base, size_t start, size_t end)
183{
184 size_t *a, *z;
185 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
186 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
187 if (start>end || end-start < 4*sizeof(size_t)) return;
188 a = (size_t *)(base + start);
189 z = (size_t *)(base + end);
190 a[-2] = 1;
191 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
192 z[1] = 1;
193 free(a);
194}
195
196static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
197{
198 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
199 if (ph->p_type!=PT_LOAD) continue;
200 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
201 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
202 reclaim(base, ph->p_vaddr+ph->p_memsz,
203 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
204 }
205}
206
Rich Felker51e2d832011-06-18 19:48:42 -0400207static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp)
208{
Rich Felker59633c72011-06-25 12:26:08 -0400209 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
Rich Felker51e2d832011-06-18 19:48:42 -0400210 size_t phsize;
211 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
212 size_t this_min, this_max;
213 off_t off_start;
214 Ehdr *eh;
215 Phdr *ph;
216 unsigned prot;
217 unsigned char *map, *base;
218 size_t dyn;
219 size_t i;
220
221 ssize_t l = read(fd, buf, sizeof buf);
222 if (l<sizeof *eh) return 0;
Rich Felker59633c72011-06-25 12:26:08 -0400223 eh = buf;
Rich Felker51e2d832011-06-18 19:48:42 -0400224 phsize = eh->e_phentsize * eh->e_phnum;
225 if (phsize + sizeof *eh > l) return 0;
226 if (eh->e_phoff + phsize > l) {
Rich Felker59633c72011-06-25 12:26:08 -0400227 l = pread(fd, buf+1, phsize, eh->e_phoff);
Rich Felker51e2d832011-06-18 19:48:42 -0400228 if (l != phsize) return 0;
229 eh->e_phoff = sizeof *eh;
230 }
231 ph = (void *)((char *)buf + eh->e_phoff);
232 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
233 if (ph->p_type == PT_DYNAMIC)
234 dyn = ph->p_vaddr;
235 if (ph->p_type != PT_LOAD) continue;
236 if (ph->p_vaddr < addr_min) {
237 addr_min = ph->p_vaddr;
238 off_start = ph->p_offset;
239 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
240 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
241 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
242 }
243 if (ph->p_vaddr+ph->p_memsz > addr_max) {
244 addr_max = ph->p_vaddr+ph->p_memsz;
245 }
246 }
247 if (!dyn) return 0;
248 addr_max += PAGE_SIZE-1;
249 addr_max &= -PAGE_SIZE;
250 addr_min &= -PAGE_SIZE;
251 off_start &= -PAGE_SIZE;
252 map_len = addr_max - addr_min + off_start;
253 /* The first time, we map too much, possibly even more than
254 * the length of the file. This is okay because we will not
255 * use the invalid part; we just need to reserve the right
256 * amount of virtual address space to map over later. */
Rich Felkerbf301002011-06-28 14:20:41 -0400257 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
Rich Felker51e2d832011-06-18 19:48:42 -0400258 if (map==MAP_FAILED) return 0;
259 base = map - addr_min;
260 ph = (void *)((char *)buf + eh->e_phoff);
261 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
262 if (ph->p_type != PT_LOAD) continue;
263 /* Reuse the existing mapping for the lowest-address LOAD */
264 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
265 this_min = ph->p_vaddr & -PAGE_SIZE;
266 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
267 off_start = ph->p_offset & -PAGE_SIZE;
268 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
269 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
270 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
271 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) {
272 munmap(map, map_len);
273 return 0;
274 }
275 if (ph->p_memsz > ph->p_filesz) {
276 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
277 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
278 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
279 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) {
280 munmap(map, map_len);
281 return 0;
282 }
283 }
284 }
Rich Felker9f174132011-06-29 00:29:08 -0400285 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
286 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
287 mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC);
288 break;
289 }
Rich Felker6717e622011-06-28 19:40:14 -0400290 if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
291 eh->e_phentsize, eh->e_phnum);
Rich Felker51e2d832011-06-18 19:48:42 -0400292 *lenp = map_len;
293 *basep = base;
294 *dynp = dyn;
295 return map;
296}
297
Rich Felker5c1909a2012-05-27 16:01:44 -0400298static int path_open(const char *name, const char *search, char *buf, size_t buf_size)
Rich Felker568b8072011-06-25 01:56:34 -0400299{
Rich Felker49388f32011-06-25 17:49:16 -0400300 const char *s=search, *z;
Rich Felker568b8072011-06-25 01:56:34 -0400301 int l, fd;
Rich Felker49388f32011-06-25 17:49:16 -0400302 for (;;) {
303 while (*s==':') s++;
304 if (!*s) return -1;
Rich Felker568b8072011-06-25 01:56:34 -0400305 z = strchr(s, ':');
306 l = z ? z-s : strlen(s);
Rich Felker5c1909a2012-05-27 16:01:44 -0400307 snprintf(buf, buf_size, "%.*s/%s", l, s, name);
Rich Felker568b8072011-06-25 01:56:34 -0400308 if ((fd = open(buf, O_RDONLY))>=0) return fd;
Rich Felker49388f32011-06-25 17:49:16 -0400309 s += l;
Rich Felker568b8072011-06-25 01:56:34 -0400310 }
Rich Felker568b8072011-06-25 01:56:34 -0400311}
312
Rich Felkerc82f4a32012-01-23 00:57:38 -0500313static void decode_dyn(struct dso *p)
314{
315 size_t dyn[DYN_CNT] = {0};
316 decode_vec(p->dynv, dyn, DYN_CNT);
317 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
318 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
319 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
320}
321
Rich Felker51e2d832011-06-18 19:48:42 -0400322static struct dso *load_library(const char *name)
323{
Rich Felker5c1909a2012-05-27 16:01:44 -0400324 char buf[2*NAME_MAX+2];
Rich Felker51e2d832011-06-18 19:48:42 -0400325 unsigned char *base, *map;
326 size_t dyno, map_len;
327 struct dso *p;
Rich Felker51e2d832011-06-18 19:48:42 -0400328 int fd;
329 struct stat st;
330
331 /* Catch and block attempts to reload the implementation itself */
332 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
333 static const char *rp, reserved[] =
334 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
335 char *z = strchr(name, '.');
336 if (z) {
337 size_t l = z-name;
338 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
339 if (*rp) {
340 if (!libc->prev) {
341 tail->next = libc;
342 libc->prev = tail;
Rich Felker6ab444d2011-07-24 00:54:55 -0400343 tail = libc->next ? libc->next : libc;
Rich Felker51e2d832011-06-18 19:48:42 -0400344 }
345 return libc;
346 }
347 }
348 }
349 /* Search for the name to see if it's already loaded */
350 for (p=head->next; p; p=p->next) {
Rich Felker5c1909a2012-05-27 16:01:44 -0400351 if (!strcmp(p->shortname, name)) {
Rich Felker51e2d832011-06-18 19:48:42 -0400352 p->refcnt++;
353 return p;
354 }
355 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400356 if (strchr(name, '/')) {
Rich Felker51e2d832011-06-18 19:48:42 -0400357 fd = open(name, O_RDONLY);
358 } else {
Rich Felker59ab43f2011-06-26 19:23:28 -0400359 if (strlen(name) > NAME_MAX) return 0;
Rich Felker568b8072011-06-25 01:56:34 -0400360 fd = -1;
Rich Felker5c1909a2012-05-27 16:01:44 -0400361 if (r_path) fd = path_open(name, r_path, buf, sizeof buf);
362 if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
Rich Felker568b8072011-06-25 01:56:34 -0400363 if (fd < 0) {
364 if (!sys_path) {
365 FILE *f = fopen(ETC_LDSO_PATH, "r");
366 if (f) {
367 if (getline(&sys_path, (size_t[1]){0}, f) > 0)
368 sys_path[strlen(sys_path)-1]=0;
369 fclose(f);
370 }
371 }
Rich Felker5c1909a2012-05-27 16:01:44 -0400372 if (sys_path) fd = path_open(name, sys_path, buf, sizeof buf);
373 else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib", buf, sizeof buf);
Rich Felker51e2d832011-06-18 19:48:42 -0400374 }
375 }
376 if (fd < 0) return 0;
377 if (fstat(fd, &st) < 0) {
378 close(fd);
379 return 0;
380 }
381 for (p=head->next; p; p=p->next) {
382 if (p->dev == st.st_dev && p->ino == st.st_ino) {
383 close(fd);
384 p->refcnt++;
385 return p;
386 }
387 }
388 map = map_library(fd, &map_len, &base, &dyno);
389 close(fd);
390 if (!map) return 0;
Rich Felker5c1909a2012-05-27 16:01:44 -0400391 p = calloc(1, sizeof *p + strlen(buf) + 1);
Rich Felker51e2d832011-06-18 19:48:42 -0400392 if (!p) {
393 munmap(map, map_len);
394 return 0;
395 }
396
397 p->map = map;
398 p->map_len = map_len;
399 p->base = base;
400 p->dynv = (void *)(base + dyno);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500401 decode_dyn(p);
Rich Felker51e2d832011-06-18 19:48:42 -0400402
Rich Felker51e2d832011-06-18 19:48:42 -0400403 p->dev = st.st_dev;
404 p->ino = st.st_ino;
Rich Felker51e2d832011-06-18 19:48:42 -0400405 p->refcnt = 1;
Rich Felker6b3d5e52011-06-26 17:39:17 -0400406 p->name = p->buf;
Rich Felker5c1909a2012-05-27 16:01:44 -0400407 strcpy(p->name, buf);
408 if (!strchr(name, '/')) p->shortname = strrchr(p->name, '/');
409 if (!p->shortname) p->shortname = p->name;
Rich Felker51e2d832011-06-18 19:48:42 -0400410
411 tail->next = p;
412 p->prev = tail;
413 tail = p;
414
Rich Felker5c1909a2012-05-27 16:01:44 -0400415 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, buf, base);
416
Rich Felker51e2d832011-06-18 19:48:42 -0400417 return p;
418}
419
420static void load_deps(struct dso *p)
421{
Rich Felker59ab43f2011-06-26 19:23:28 -0400422 size_t i, ndeps=0;
423 struct dso ***deps = &p->deps, **tmp, *dep;
Rich Felker51e2d832011-06-18 19:48:42 -0400424 for (; p; p=p->next) {
425 for (i=0; p->dynv[i]; i+=2) {
Rich Felker191ebca2011-06-30 23:02:27 -0400426 if (p->dynv[i] != DT_RPATH) continue;
427 r_path = (void *)(p->strings + p->dynv[i+1]);
428 }
429 for (i=0; p->dynv[i]; i+=2) {
Rich Felker51e2d832011-06-18 19:48:42 -0400430 if (p->dynv[i] != DT_NEEDED) continue;
Rich Felker59ab43f2011-06-26 19:23:28 -0400431 dep = load_library(p->strings + p->dynv[i+1]);
432 if (!dep) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400433 snprintf(errbuf, sizeof errbuf,
434 "Error loading shared library %s: %m (needed by %s)",
Rich Felker6b3d5e52011-06-26 17:39:17 -0400435 p->strings + p->dynv[i+1], p->name);
Rich Felkera5d10eb2012-04-23 12:03:31 -0400436 if (runtime) longjmp(rtld_fail, 1);
437 dprintf(2, "%s\n", errbuf);
Rich Felker6b3d5e52011-06-26 17:39:17 -0400438 _exit(127);
439 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400440 if (runtime) {
441 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
442 if (!tmp) longjmp(rtld_fail, 1);
443 tmp[ndeps++] = dep;
444 tmp[ndeps] = 0;
445 *deps = tmp;
446 }
Rich Felker51e2d832011-06-18 19:48:42 -0400447 }
Rich Felker191ebca2011-06-30 23:02:27 -0400448 r_path = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400449 }
450}
451
Rich Felker2719cc82011-08-16 00:24:36 -0400452static void load_preload(char *s)
453{
454 int tmp;
455 char *z;
456 for (z=s; *z; s=z) {
457 for ( ; *s && isspace(*s); s++);
458 for (z=s; *z && !isspace(*z); z++);
459 tmp = *z;
460 *z = 0;
461 load_library(s);
462 *z = tmp;
463 }
464}
465
Rich Felker59ab43f2011-06-26 19:23:28 -0400466static void make_global(struct dso *p)
467{
468 for (; p; p=p->next) p->global = 1;
469}
470
Rich Felker51e2d832011-06-18 19:48:42 -0400471static void reloc_all(struct dso *p)
472{
473 size_t dyn[DYN_CNT] = {0};
474 for (; p; p=p->next) {
475 if (p->relocated) continue;
476 decode_vec(p->dynv, dyn, DYN_CNT);
477 do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400478 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head);
Rich Felker51e2d832011-06-18 19:48:42 -0400479 do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400480 2, p->syms, p->strings, head);
Rich Felker51e2d832011-06-18 19:48:42 -0400481 do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ],
Rich Felker32de61e2011-06-25 22:36:21 -0400482 3, p->syms, p->strings, head);
Rich Felker368ba4a2011-06-25 00:18:19 -0400483 p->relocated = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400484 }
485}
486
Rich Felkere8dbf002011-06-25 00:47:28 -0400487static void free_all(struct dso *p)
488{
489 struct dso *n;
490 while (p) {
491 n = p->next;
492 if (p->map) free(p);
493 p = n;
494 }
495}
496
Rich Felkerc82f4a32012-01-23 00:57:38 -0500497static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
498{
499 for (; cnt--; ph = (void *)((char *)ph + stride))
500 if (ph->p_type == PT_DYNAMIC)
501 return ph->p_vaddr;
502 return 0;
503}
504
Rich Felker4ce3cb52012-02-06 14:39:09 -0500505static void do_init_fini(struct dso *p)
506{
507 size_t dyn[DYN_CNT] = {0};
508 for (; p; p=p->prev) {
509 if (p->constructed) return;
510 decode_vec(p->dynv, dyn, DYN_CNT);
511 if (dyn[0] & (1<<DT_FINI))
512 atexit((void (*)(void))(p->base + dyn[DT_FINI]));
513 if (dyn[0] & (1<<DT_INIT))
514 ((void (*)(void))(p->base + dyn[DT_INIT]))();
515 p->constructed = 1;
516 }
517}
518
Rich Felker3ec8d292012-04-25 00:05:42 -0400519void _dl_debug_state(void)
520{
521}
522
Rich Felkerc82f4a32012-01-23 00:57:38 -0500523void *__dynlink(int argc, char **argv)
Rich Felker51e2d832011-06-18 19:48:42 -0400524{
525 size_t *auxv, aux[AUX_CNT] = {0};
Rich Felker51e2d832011-06-18 19:48:42 -0400526 size_t i;
527 Phdr *phdr;
Rich Felker6717e622011-06-28 19:40:14 -0400528 Ehdr *ehdr;
Rich Felker6ab444d2011-07-24 00:54:55 -0400529 static struct dso builtin_dsos[3];
Rich Felkera53de812011-07-24 00:26:12 -0400530 struct dso *const app = builtin_dsos+0;
531 struct dso *const lib = builtin_dsos+1;
Rich Felker6ab444d2011-07-24 00:54:55 -0400532 struct dso *const vdso = builtin_dsos+2;
Rich Felker2719cc82011-08-16 00:24:36 -0400533 char *env_preload=0;
Rich Felker51e2d832011-06-18 19:48:42 -0400534
535 /* Find aux vector just past environ[] */
Rich Felker568b8072011-06-25 01:56:34 -0400536 for (i=argc+1; argv[i]; i++)
537 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
538 env_path = argv[i]+16;
Rich Felker2719cc82011-08-16 00:24:36 -0400539 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
540 env_preload = argv[i]+11;
Rich Felker51e2d832011-06-18 19:48:42 -0400541 auxv = (void *)(argv+i+1);
542
543 decode_vec(auxv, aux, AUX_CNT);
544
Rich Felker568b8072011-06-25 01:56:34 -0400545 /* Only trust user/env if kernel says we're not suid/sgid */
546 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
Rich Felkera0458832011-08-16 07:46:42 -0400547 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
Rich Felker568b8072011-06-25 01:56:34 -0400548 env_path = 0;
Rich Felker2719cc82011-08-16 00:24:36 -0400549 env_preload = 0;
Rich Felker568b8072011-06-25 01:56:34 -0400550 }
551
Rich Felker5c1909a2012-05-27 16:01:44 -0400552 /* If the dynamic linker was invoked as a program itself, AT_BASE
553 * will not be set. In that case, we assume the base address is
554 * the start of the page containing the PHDRs; I don't know any
555 * better approach... */
556 if (!aux[AT_BASE]) {
557 aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE;
558 aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0;
559 }
560
Rich Felkerc82f4a32012-01-23 00:57:38 -0500561 /* The dynamic linker load address is passed by the kernel
562 * in the AUX vector, so this is easy. */
563 lib->base = (void *)aux[AT_BASE];
Rich Felker5c1909a2012-05-27 16:01:44 -0400564 lib->name = lib->shortname = "libc.so";
Rich Felkerc82f4a32012-01-23 00:57:38 -0500565 lib->global = 1;
566 ehdr = (void *)lib->base;
567 lib->dynv = (void *)(lib->base + find_dyn(
568 (void *)(aux[AT_BASE]+ehdr->e_phoff),
569 ehdr->e_phnum, ehdr->e_phentsize));
570 decode_dyn(lib);
571
Rich Felker5c1909a2012-05-27 16:01:44 -0400572 if (aux[AT_PHDR]) {
573 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
574 phdr = (void *)aux[AT_PHDR];
575 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
576 if (phdr->p_type == PT_PHDR)
577 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
578 }
579 app->name = app->shortname = argv[0];
580 app->dynv = (void *)(app->base + find_dyn(
581 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
582 } else {
583 int fd;
584 char *ldname = argv[0];
585 size_t dyno, l = strlen(ldname);
586 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
587 *argv++ = (void *)-1;
588 if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1;
589 if (!argv[0]) {
590 dprintf(2, "musl libc/dynamic program loader\n");
591 dprintf(2, "usage: %s pathname%s\n", ldname,
592 ldd_mode ? "" : " [args]");
593 _exit(1);
594 }
595 fd = open(argv[0], O_RDONLY);
596 if (fd < 0) {
597 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
598 _exit(1);
599 }
600 runtime = 1;
601 ehdr = (void *)map_library(fd, &app->map_len, &app->base, &dyno);
602 if (!ehdr) {
603 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
604 _exit(1);
605 }
606 runtime = 0;
607 close(fd);
608 app->name = app->shortname = argv[0];
609 app->dynv = (void *)(app->base + dyno);
610 aux[AT_ENTRY] = ehdr->e_entry;
Rich Felkere12fe652012-01-23 02:02:59 -0500611 }
Rich Felkerc82f4a32012-01-23 00:57:38 -0500612 app->global = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500613 app->constructed = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500614 decode_dyn(app);
615
616 /* Attach to vdso, if provided by the kernel */
Rich Felkercf8506a2011-08-16 08:50:03 -0400617 for (i=0; auxv[i]; i+=2) {
Rich Felkerc82f4a32012-01-23 00:57:38 -0500618 size_t vdso_base = auxv[i+1];
619 if (auxv[i] != AT_SYSINFO_EHDR) continue;
Rich Felker6ab444d2011-07-24 00:54:55 -0400620 ehdr = (void *)vdso_base;
621 phdr = (void *)(vdso_base + ehdr->e_phoff);
622 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
623 if (phdr->p_type == PT_DYNAMIC)
624 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
625 if (phdr->p_type == PT_LOAD)
626 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
627 }
Rich Felker5c1909a2012-05-27 16:01:44 -0400628 vdso->name = vdso->shortname = "linux-gate.so.1";
Rich Felker427173b2011-07-24 02:19:47 -0400629 vdso->global = 1;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500630 decode_dyn(vdso);
Rich Felker6ab444d2011-07-24 00:54:55 -0400631 vdso->prev = lib;
632 lib->next = vdso;
Rich Felkerc82f4a32012-01-23 00:57:38 -0500633 break;
Rich Felker6ab444d2011-07-24 00:54:55 -0400634 }
635
Rich Felkerc82f4a32012-01-23 00:57:38 -0500636 /* Initial dso chain consists only of the app. We temporarily
637 * append the dynamic linker/libc so we can relocate it, then
638 * restore the initial chain in preparation for loading third
639 * party libraries (preload/needed). */
640 head = tail = app;
641 libc = lib;
642 app->next = lib;
643 reloc_all(lib);
644 app->next = 0;
Rich Felker51e2d832011-06-18 19:48:42 -0400645
Rich Felkerc82f4a32012-01-23 00:57:38 -0500646 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
Rich Felker51e2d832011-06-18 19:48:42 -0400647
Rich Felkerc82f4a32012-01-23 00:57:38 -0500648 /* Donate unused parts of app and library mapping to malloc */
Rich Felkera53de812011-07-24 00:26:12 -0400649 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
650 ehdr = (void *)lib->base;
651 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
Rich Felker6717e622011-06-28 19:40:14 -0400652 ehdr->e_phentsize, ehdr->e_phnum);
653
Rich Felkerc82f4a32012-01-23 00:57:38 -0500654 /* Load preload/needed libraries, add their symbols to the global
Rich Felkerfd7015d2012-01-23 18:32:40 -0500655 * namespace, and perform all remaining relocations. The main
656 * program must be relocated LAST since it may contain copy
657 * relocations which depend on libraries' relocations. */
Rich Felker2719cc82011-08-16 00:24:36 -0400658 if (env_preload) load_preload(env_preload);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500659 load_deps(app);
660 make_global(app);
Rich Felkerfd7015d2012-01-23 18:32:40 -0500661 reloc_all(app->next);
Rich Felkerc82f4a32012-01-23 00:57:38 -0500662 reloc_all(app);
Rich Felker51e2d832011-06-18 19:48:42 -0400663
Rich Felker5c1909a2012-05-27 16:01:44 -0400664 if (ldd_mode) _exit(0);
665
Rich Felkerc82f4a32012-01-23 00:57:38 -0500666 /* Switch to runtime mode: any further failures in the dynamic
667 * linker are a reportable failure rather than a fatal startup
668 * error. If the dynamic loader (dlopen) will not be used, free
669 * all memory used by the dynamic linker. */
Rich Felkera53de812011-07-24 00:26:12 -0400670 runtime = 1;
Rich Felker4ce3cb52012-02-06 14:39:09 -0500671
Rich Felker3ec8d292012-04-25 00:05:42 -0400672 for (i=0; app->dynv[i]; i+=2)
673 if (app->dynv[i]==DT_DEBUG)
674 app->dynv[i+1] = (size_t)&debug;
675 debug.ver = 1;
676 debug.bp = _dl_debug_state;
677 debug.head = head;
678 debug.base = lib->base;
679 debug.state = 0;
680 _dl_debug_state();
681
Rich Felker58aa5f42012-05-03 20:42:45 -0400682 if (ssp_used) __init_ssp(auxv);
683
Rich Felker4ce3cb52012-02-06 14:39:09 -0500684 do_init_fini(tail);
685
Rich Felkera53de812011-07-24 00:26:12 -0400686 if (!rtld_used) {
Rich Felker59ab43f2011-06-26 19:23:28 -0400687 free_all(head);
688 free(sys_path);
Rich Felkera53de812011-07-24 00:26:12 -0400689 reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos);
Rich Felker59ab43f2011-06-26 19:23:28 -0400690 }
Rich Felkere8dbf002011-06-25 00:47:28 -0400691
Rich Felker51e2d832011-06-18 19:48:42 -0400692 errno = 0;
693 return (void *)aux[AT_ENTRY];
694}
Rich Felker59ab43f2011-06-26 19:23:28 -0400695
696void *dlopen(const char *file, int mode)
697{
Rich Felker2fdea172011-07-01 22:40:00 -0400698 struct dso *volatile p, *orig_tail = tail, *next;
Rich Felker59ab43f2011-06-26 19:23:28 -0400699 size_t i;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500700 int cs;
Rich Felker59ab43f2011-06-26 19:23:28 -0400701
702 if (!file) return head;
703
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500704 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
Rich Felker59ab43f2011-06-26 19:23:28 -0400705 pthread_rwlock_wrlock(&lock);
706
707 if (setjmp(rtld_fail)) {
708 /* Clean up anything new that was (partially) loaded */
Rich Felker92ab5d82011-06-26 21:21:04 -0400709 if (p->deps) for (i=0; p->deps[i]; i++)
710 if (p->deps[i]->global < 0)
711 p->deps[i]->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400712 for (p=orig_tail->next; p; p=next) {
713 next = p->next;
714 munmap(p->map, p->map_len);
715 free(p->deps);
716 free(p);
717 }
718 tail = orig_tail;
719 tail->next = 0;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500720 p = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400721 errflag = 1;
722 goto end;
Rich Felkera9e85c02012-03-23 00:28:20 -0400723 } else p = load_library(file);
724
725 if (!p) {
Rich Felkera5d10eb2012-04-23 12:03:31 -0400726 snprintf(errbuf, sizeof errbuf,
727 "Error loading shared library %s: %m", file);
Rich Felkera9e85c02012-03-23 00:28:20 -0400728 errflag = 1;
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500729 goto end;
Rich Felker59ab43f2011-06-26 19:23:28 -0400730 }
731
Rich Felker59ab43f2011-06-26 19:23:28 -0400732 /* First load handling */
733 if (!p->deps) {
734 load_deps(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400735 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400736 if (!p->deps[i]->global)
737 p->deps[i]->global = -1;
738 if (!p->global) p->global = -1;
Rich Felker59ab43f2011-06-26 19:23:28 -0400739 reloc_all(p);
Rich Felker0e4dae32011-06-26 21:36:44 -0400740 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker92ab5d82011-06-26 21:21:04 -0400741 if (p->deps[i]->global < 0)
742 p->deps[i]->global = 0;
743 if (p->global < 0) p->global = 0;
Rich Felker59ab43f2011-06-26 19:23:28 -0400744 }
745
746 if (mode & RTLD_GLOBAL) {
Rich Felker0e4dae32011-06-26 21:36:44 -0400747 if (p->deps) for (i=0; p->deps[i]; i++)
Rich Felker59ab43f2011-06-26 19:23:28 -0400748 p->deps[i]->global = 1;
749 p->global = 1;
750 }
751
Rich Felker3ec8d292012-04-25 00:05:42 -0400752 _dl_debug_state();
753
Rich Felkerce4d97e2012-02-06 17:57:29 -0500754 do_init_fini(tail);
Rich Felker06933cc2011-06-26 22:09:32 -0400755end:
Rich Felker59ab43f2011-06-26 19:23:28 -0400756 pthread_rwlock_unlock(&lock);
Rich Felkerf2baf4d2012-02-07 20:31:27 -0500757 pthread_setcancelstate(cs, 0);
Rich Felker59ab43f2011-06-26 19:23:28 -0400758 return p;
759}
760
Rich Felker623753a2011-08-16 00:42:13 -0400761static void *do_dlsym(struct dso *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400762{
763 size_t i;
764 uint32_t h;
765 Sym *sym;
Rich Felker623753a2011-08-16 00:42:13 -0400766 if (p == RTLD_NEXT) {
767 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
768 if (!p) p=head;
769 p=p->next;
770 }
Rich Felkera9e85c02012-03-23 00:28:20 -0400771 if (p == head || p == RTLD_DEFAULT) {
772 void *res = find_sym(head, s, 0);
Rich Felker4027f4e2012-05-04 20:18:18 -0400773 if (!res) goto failed;
Rich Felkera9e85c02012-03-23 00:28:20 -0400774 return res;
775 }
Rich Felker59ab43f2011-06-26 19:23:28 -0400776 h = hash(s);
777 sym = lookup(s, h, p->syms, p->hashtab, p->strings);
778 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
779 return p->base + sym->st_value;
780 if (p->deps) for (i=0; p->deps[i]; i++) {
781 sym = lookup(s, h, p->deps[i]->syms,
782 p->deps[i]->hashtab, p->deps[i]->strings);
783 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
784 return p->deps[i]->base + sym->st_value;
785 }
Rich Felker4027f4e2012-05-04 20:18:18 -0400786failed:
Rich Felkera9e85c02012-03-23 00:28:20 -0400787 errflag = 1;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400788 snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
Rich Felker59ab43f2011-06-26 19:23:28 -0400789 return 0;
790}
791
Rich Felker623753a2011-08-16 00:42:13 -0400792void *__dlsym(void *p, const char *s, void *ra)
Rich Felker59ab43f2011-06-26 19:23:28 -0400793{
794 void *res;
795 pthread_rwlock_rdlock(&lock);
Rich Felker623753a2011-08-16 00:42:13 -0400796 res = do_dlsym(p, s, ra);
Rich Felker59ab43f2011-06-26 19:23:28 -0400797 pthread_rwlock_unlock(&lock);
798 return res;
799}
Rich Felker5a09a532012-02-03 03:16:07 -0500800#else
801void *dlopen(const char *file, int mode)
802{
803 return 0;
804}
805void *__dlsym(void *p, const char *s, void *ra)
806{
807 return 0;
808}
809#endif
Rich Felker59ab43f2011-06-26 19:23:28 -0400810
811char *dlerror()
812{
Rich Felkera9e85c02012-03-23 00:28:20 -0400813 if (!errflag) return 0;
814 errflag = 0;
Rich Felkera5d10eb2012-04-23 12:03:31 -0400815 return errbuf;
Rich Felker59ab43f2011-06-26 19:23:28 -0400816}
817
818int dlclose(void *p)
819{
820 return 0;
821}