Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 1 | #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 Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 15 | #include <setjmp.h> |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 16 | #include <pthread.h> |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 17 | #include <ctype.h> |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 18 | #include <dlfcn.h> |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 19 | |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 20 | static int errflag; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 21 | static char errbuf[128]; |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 22 | |
Rich Felker | e864a29 | 2012-07-11 01:47:30 -0400 | [diff] [blame] | 23 | #ifdef SHARED |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 24 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 25 | #include "reloc.h" |
| 26 | |
| 27 | #if ULONG_MAX == 0xffffffff |
| 28 | typedef Elf32_Ehdr Ehdr; |
| 29 | typedef Elf32_Phdr Phdr; |
| 30 | typedef Elf32_Sym Sym; |
| 31 | #define R_TYPE(x) ((x)&255) |
| 32 | #define R_SYM(x) ((x)>>8) |
| 33 | #else |
| 34 | typedef Elf64_Ehdr Ehdr; |
| 35 | typedef Elf64_Phdr Phdr; |
| 36 | typedef Elf64_Sym Sym; |
| 37 | #define R_TYPE(x) ((x)&0xffffffff) |
| 38 | #define R_SYM(x) ((x)>>32) |
| 39 | #endif |
| 40 | |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 41 | struct debug { |
| 42 | int ver; |
| 43 | void *head; |
| 44 | void (*bp)(void); |
| 45 | int state; |
| 46 | void *base; |
| 47 | }; |
| 48 | |
| 49 | struct dso { |
| 50 | unsigned char *base; |
| 51 | char *name; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 52 | size_t *dynv; |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 53 | struct dso *next, *prev; |
| 54 | |
| 55 | int refcnt; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 56 | Sym *syms; |
Rich Felker | 596d60c | 2011-06-18 22:52:01 -0400 | [diff] [blame] | 57 | uint32_t *hashtab; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 58 | char *strings; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 59 | unsigned char *map; |
| 60 | size_t map_len; |
| 61 | dev_t dev; |
| 62 | ino_t ino; |
Rich Felker | 6343ac8 | 2012-06-09 21:20:44 -0400 | [diff] [blame] | 63 | signed char global; |
Rich Felker | 700a815 | 2012-02-07 20:29:29 -0500 | [diff] [blame] | 64 | char relocated; |
| 65 | char constructed; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 66 | struct dso **deps; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 67 | char *shortname; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 68 | char buf[]; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 69 | }; |
| 70 | |
Rich Felker | 58aa5f4 | 2012-05-03 20:42:45 -0400 | [diff] [blame] | 71 | void __init_ssp(size_t *); |
Rich Felker | 60872cf | 2012-04-24 18:07:59 -0400 | [diff] [blame] | 72 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 73 | static struct dso *head, *tail, *libc; |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 74 | static char *env_path, *sys_path, *r_path; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 75 | static int rtld_used; |
Rich Felker | 60872cf | 2012-04-24 18:07:59 -0400 | [diff] [blame] | 76 | static int ssp_used; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 77 | static int runtime; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 78 | static int ldd_mode; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 79 | static jmp_buf rtld_fail; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 80 | static pthread_rwlock_t lock; |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 81 | static struct debug debug; |
| 82 | |
| 83 | struct debug *_dl_debug_addr = &debug; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 84 | |
Rich Felker | a045883 | 2011-08-16 07:46:42 -0400 | [diff] [blame] | 85 | #define AUX_CNT 24 |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 86 | #define DYN_CNT 34 |
| 87 | |
| 88 | static 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 Felker | 2adf2fb | 2012-01-17 00:34:58 -0500 | [diff] [blame] | 97 | static uint32_t hash(const char *s0) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 98 | { |
Rich Felker | 2adf2fb | 2012-01-17 00:34:58 -0500 | [diff] [blame] | 99 | const unsigned char *s = (void *)s0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 100 | 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 Felker | 596d60c | 2011-06-18 22:52:01 -0400 | [diff] [blame] | 108 | static Sym *lookup(const char *s, uint32_t h, Sym *syms, uint32_t *hashtab, char *strings) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 109 | { |
| 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 Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 119 | #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 120 | |
| 121 | static void *find_sym(struct dso *dso, const char *s, int need_def) |
| 122 | { |
| 123 | uint32_t h = hash(s); |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 124 | void *def = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 125 | if (h==0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1; |
| 126 | if (h==0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1; |
Rich Felker | 60872cf | 2012-04-24 18:07:59 -0400 | [diff] [blame] | 127 | if (h==0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 128 | for (; dso; dso=dso->next) { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 129 | Sym *sym; |
| 130 | if (!dso->global) continue; |
| 131 | sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 132 | if (sym && (!need_def || sym->st_shndx) && sym->st_value |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 133 | && (1<<(sym->st_info&0xf) & OK_TYPES) |
| 134 | && (1<<(sym->st_info>>4) & OK_BINDS)) { |
Rich Felker | e01ac67 | 2011-07-25 09:22:05 -0400 | [diff] [blame] | 135 | if (def && sym->st_info>>4 == STB_WEAK) continue; |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 136 | def = dso->base + sym->st_value; |
| 137 | if (sym->st_info>>4 == STB_GLOBAL) break; |
| 138 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 139 | } |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 140 | return def; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static 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 Felker | 32de61e | 2011-06-25 22:36:21 -0400 | [diff] [blame] | 161 | sym_val = (size_t)find_sym(ctx, name, IS_PLT(type)); |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 162 | if (!sym_val && sym->st_info>>4 != STB_WEAK) { |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 163 | snprintf(errbuf, sizeof errbuf, |
| 164 | "Error relocating %s: %s: symbol not found", |
| 165 | dso->name, name); |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 166 | if (runtime) longjmp(rtld_fail, 1); |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 167 | dprintf(2, "%s\n", errbuf); |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 168 | _exit(127); |
| 169 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 170 | sym_size = sym->st_size; |
| 171 | } |
| 172 | do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]); |
| 173 | } |
| 174 | } |
| 175 | |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 176 | /* 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 | |
| 182 | static 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 | |
| 196 | static 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 Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 207 | static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp) |
| 208 | { |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 209 | Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)]; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 210 | 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 Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 223 | eh = buf; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 224 | phsize = eh->e_phentsize * eh->e_phnum; |
| 225 | if (phsize + sizeof *eh > l) return 0; |
| 226 | if (eh->e_phoff + phsize > l) { |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 227 | l = pread(fd, buf+1, phsize, eh->e_phoff); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 228 | 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 Felker | bf30100 | 2011-06-28 14:20:41 -0400 | [diff] [blame] | 257 | map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 258 | 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)); |
Rich Felker | f7d15dc | 2012-06-06 11:21:28 -0400 | [diff] [blame] | 271 | if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) |
| 272 | goto error; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 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); |
Rich Felker | f7d15dc | 2012-06-06 11:21:28 -0400 | [diff] [blame] | 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 | goto error; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 279 | } |
| 280 | } |
Rich Felker | 9f17413 | 2011-06-29 00:29:08 -0400 | [diff] [blame] | 281 | for (i=0; ((size_t *)(base+dyn))[i]; i+=2) |
| 282 | if (((size_t *)(base+dyn))[i]==DT_TEXTREL) { |
Rich Felker | f7d15dc | 2012-06-06 11:21:28 -0400 | [diff] [blame] | 283 | if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0) |
| 284 | goto error; |
Rich Felker | 9f17413 | 2011-06-29 00:29:08 -0400 | [diff] [blame] | 285 | break; |
| 286 | } |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 287 | if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff), |
| 288 | eh->e_phentsize, eh->e_phnum); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 289 | *lenp = map_len; |
| 290 | *basep = base; |
| 291 | *dynp = dyn; |
| 292 | return map; |
Rich Felker | f7d15dc | 2012-06-06 11:21:28 -0400 | [diff] [blame] | 293 | error: |
| 294 | munmap(map, map_len); |
| 295 | return 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 298 | static int path_open(const char *name, const char *search, char *buf, size_t buf_size) |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 299 | { |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 300 | const char *s=search, *z; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 301 | int l, fd; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 302 | for (;;) { |
| 303 | while (*s==':') s++; |
| 304 | if (!*s) return -1; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 305 | z = strchr(s, ':'); |
| 306 | l = z ? z-s : strlen(s); |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 307 | snprintf(buf, buf_size, "%.*s/%s", l, s, name); |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 308 | if ((fd = open(buf, O_RDONLY))>=0) return fd; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 309 | s += l; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 310 | } |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 313 | static 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 Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 322 | static struct dso *load_library(const char *name) |
| 323 | { |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 324 | char buf[2*NAME_MAX+2]; |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 325 | const char *pathname; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 326 | unsigned char *base, *map; |
| 327 | size_t dyno, map_len; |
| 328 | struct dso *p; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 329 | int fd; |
| 330 | struct stat st; |
| 331 | |
| 332 | /* Catch and block attempts to reload the implementation itself */ |
| 333 | if (name[0]=='l' && name[1]=='i' && name[2]=='b') { |
| 334 | static const char *rp, reserved[] = |
| 335 | "c\0pthread\0rt\0m\0dl\0util\0xnet\0"; |
| 336 | char *z = strchr(name, '.'); |
| 337 | if (z) { |
| 338 | size_t l = z-name; |
| 339 | for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1); |
| 340 | if (*rp) { |
| 341 | if (!libc->prev) { |
| 342 | tail->next = libc; |
| 343 | libc->prev = tail; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 344 | tail = libc->next ? libc->next : libc; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 345 | } |
| 346 | return libc; |
| 347 | } |
| 348 | } |
| 349 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 350 | if (strchr(name, '/')) { |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 351 | pathname = name; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 352 | fd = open(name, O_RDONLY); |
| 353 | } else { |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 354 | /* Search for the name to see if it's already loaded */ |
| 355 | for (p=head->next; p; p=p->next) { |
| 356 | if (p->shortname && !strcmp(p->shortname, name)) { |
| 357 | p->refcnt++; |
| 358 | return p; |
| 359 | } |
| 360 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 361 | if (strlen(name) > NAME_MAX) return 0; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 362 | fd = -1; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 363 | if (r_path) fd = path_open(name, r_path, buf, sizeof buf); |
| 364 | if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf); |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 365 | if (fd < 0) { |
| 366 | if (!sys_path) { |
| 367 | FILE *f = fopen(ETC_LDSO_PATH, "r"); |
| 368 | if (f) { |
| 369 | if (getline(&sys_path, (size_t[1]){0}, f) > 0) |
| 370 | sys_path[strlen(sys_path)-1]=0; |
| 371 | fclose(f); |
| 372 | } |
| 373 | } |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 374 | if (sys_path) fd = path_open(name, sys_path, buf, sizeof buf); |
| 375 | else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib", buf, sizeof buf); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 376 | } |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 377 | pathname = buf; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 378 | } |
| 379 | if (fd < 0) return 0; |
| 380 | if (fstat(fd, &st) < 0) { |
| 381 | close(fd); |
| 382 | return 0; |
| 383 | } |
| 384 | for (p=head->next; p; p=p->next) { |
| 385 | if (p->dev == st.st_dev && p->ino == st.st_ino) { |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 386 | /* If this library was previously loaded with a |
| 387 | * pathname but a search found the same inode, |
| 388 | * setup its shortname so it can be found by name. */ |
| 389 | if (!p->shortname) p->shortname = strrchr(p->name, '/')+1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 390 | close(fd); |
| 391 | p->refcnt++; |
| 392 | return p; |
| 393 | } |
| 394 | } |
| 395 | map = map_library(fd, &map_len, &base, &dyno); |
| 396 | close(fd); |
| 397 | if (!map) return 0; |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 398 | p = calloc(1, sizeof *p + strlen(pathname) + 1); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 399 | if (!p) { |
| 400 | munmap(map, map_len); |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | p->map = map; |
| 405 | p->map_len = map_len; |
| 406 | p->base = base; |
| 407 | p->dynv = (void *)(base + dyno); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 408 | decode_dyn(p); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 409 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 410 | p->dev = st.st_dev; |
| 411 | p->ino = st.st_ino; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 412 | p->refcnt = 1; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 413 | p->name = p->buf; |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 414 | strcpy(p->name, pathname); |
| 415 | /* Add a shortname only if name arg was not an explicit pathname. */ |
| 416 | if (pathname != name) p->shortname = strrchr(p->name, '/')+1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 417 | |
| 418 | tail->next = p; |
| 419 | p->prev = tail; |
| 420 | tail = p; |
| 421 | |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 422 | if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, base); |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 423 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 424 | return p; |
| 425 | } |
| 426 | |
| 427 | static void load_deps(struct dso *p) |
| 428 | { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 429 | size_t i, ndeps=0; |
| 430 | struct dso ***deps = &p->deps, **tmp, *dep; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 431 | for (; p; p=p->next) { |
| 432 | for (i=0; p->dynv[i]; i+=2) { |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 433 | if (p->dynv[i] != DT_RPATH) continue; |
| 434 | r_path = (void *)(p->strings + p->dynv[i+1]); |
| 435 | } |
| 436 | for (i=0; p->dynv[i]; i+=2) { |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 437 | if (p->dynv[i] != DT_NEEDED) continue; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 438 | dep = load_library(p->strings + p->dynv[i+1]); |
| 439 | if (!dep) { |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 440 | snprintf(errbuf, sizeof errbuf, |
| 441 | "Error loading shared library %s: %m (needed by %s)", |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 442 | p->strings + p->dynv[i+1], p->name); |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 443 | if (runtime) longjmp(rtld_fail, 1); |
| 444 | dprintf(2, "%s\n", errbuf); |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 445 | _exit(127); |
| 446 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 447 | if (runtime) { |
| 448 | tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2)); |
| 449 | if (!tmp) longjmp(rtld_fail, 1); |
| 450 | tmp[ndeps++] = dep; |
| 451 | tmp[ndeps] = 0; |
| 452 | *deps = tmp; |
| 453 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 454 | } |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 455 | r_path = 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 459 | static void load_preload(char *s) |
| 460 | { |
| 461 | int tmp; |
| 462 | char *z; |
| 463 | for (z=s; *z; s=z) { |
| 464 | for ( ; *s && isspace(*s); s++); |
| 465 | for (z=s; *z && !isspace(*z); z++); |
| 466 | tmp = *z; |
| 467 | *z = 0; |
| 468 | load_library(s); |
| 469 | *z = tmp; |
| 470 | } |
| 471 | } |
| 472 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 473 | static void make_global(struct dso *p) |
| 474 | { |
| 475 | for (; p; p=p->next) p->global = 1; |
| 476 | } |
| 477 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 478 | static void reloc_all(struct dso *p) |
| 479 | { |
| 480 | size_t dyn[DYN_CNT] = {0}; |
| 481 | for (; p; p=p->next) { |
| 482 | if (p->relocated) continue; |
| 483 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 484 | do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ], |
Rich Felker | 32de61e | 2011-06-25 22:36:21 -0400 | [diff] [blame] | 485 | 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 486 | do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], |
Rich Felker | 32de61e | 2011-06-25 22:36:21 -0400 | [diff] [blame] | 487 | 2, p->syms, p->strings, head); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 488 | do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], |
Rich Felker | 32de61e | 2011-06-25 22:36:21 -0400 | [diff] [blame] | 489 | 3, p->syms, p->strings, head); |
Rich Felker | 368ba4a | 2011-06-25 00:18:19 -0400 | [diff] [blame] | 490 | p->relocated = 1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 494 | static void free_all(struct dso *p) |
| 495 | { |
| 496 | struct dso *n; |
| 497 | while (p) { |
| 498 | n = p->next; |
| 499 | if (p->map) free(p); |
| 500 | p = n; |
| 501 | } |
| 502 | } |
| 503 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 504 | static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride) |
| 505 | { |
| 506 | for (; cnt--; ph = (void *)((char *)ph + stride)) |
| 507 | if (ph->p_type == PT_DYNAMIC) |
| 508 | return ph->p_vaddr; |
| 509 | return 0; |
| 510 | } |
| 511 | |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 512 | static void do_init_fini(struct dso *p) |
| 513 | { |
| 514 | size_t dyn[DYN_CNT] = {0}; |
| 515 | for (; p; p=p->prev) { |
| 516 | if (p->constructed) return; |
| 517 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 518 | if (dyn[0] & (1<<DT_FINI)) |
| 519 | atexit((void (*)(void))(p->base + dyn[DT_FINI])); |
| 520 | if (dyn[0] & (1<<DT_INIT)) |
| 521 | ((void (*)(void))(p->base + dyn[DT_INIT]))(); |
| 522 | p->constructed = 1; |
| 523 | } |
| 524 | } |
| 525 | |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 526 | void _dl_debug_state(void) |
| 527 | { |
| 528 | } |
| 529 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 530 | void *__dynlink(int argc, char **argv) |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 531 | { |
| 532 | size_t *auxv, aux[AUX_CNT] = {0}; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 533 | size_t i; |
| 534 | Phdr *phdr; |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 535 | Ehdr *ehdr; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 536 | static struct dso builtin_dsos[3]; |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 537 | struct dso *const app = builtin_dsos+0; |
| 538 | struct dso *const lib = builtin_dsos+1; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 539 | struct dso *const vdso = builtin_dsos+2; |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 540 | char *env_preload=0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 541 | |
| 542 | /* Find aux vector just past environ[] */ |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 543 | for (i=argc+1; argv[i]; i++) |
| 544 | if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16)) |
| 545 | env_path = argv[i]+16; |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 546 | else if (!memcmp(argv[i], "LD_PRELOAD=", 11)) |
| 547 | env_preload = argv[i]+11; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 548 | auxv = (void *)(argv+i+1); |
| 549 | |
| 550 | decode_vec(auxv, aux, AUX_CNT); |
| 551 | |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 552 | /* Only trust user/env if kernel says we're not suid/sgid */ |
| 553 | if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID] |
Rich Felker | a045883 | 2011-08-16 07:46:42 -0400 | [diff] [blame] | 554 | || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) { |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 555 | env_path = 0; |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 556 | env_preload = 0; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 557 | } |
| 558 | |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 559 | /* If the dynamic linker was invoked as a program itself, AT_BASE |
| 560 | * will not be set. In that case, we assume the base address is |
| 561 | * the start of the page containing the PHDRs; I don't know any |
| 562 | * better approach... */ |
| 563 | if (!aux[AT_BASE]) { |
| 564 | aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE; |
| 565 | aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0; |
| 566 | } |
| 567 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 568 | /* The dynamic linker load address is passed by the kernel |
| 569 | * in the AUX vector, so this is easy. */ |
| 570 | lib->base = (void *)aux[AT_BASE]; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 571 | lib->name = lib->shortname = "libc.so"; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 572 | lib->global = 1; |
| 573 | ehdr = (void *)lib->base; |
| 574 | lib->dynv = (void *)(lib->base + find_dyn( |
| 575 | (void *)(aux[AT_BASE]+ehdr->e_phoff), |
| 576 | ehdr->e_phnum, ehdr->e_phentsize)); |
| 577 | decode_dyn(lib); |
| 578 | |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 579 | if (aux[AT_PHDR]) { |
Rich Felker | 649cec5 | 2012-07-13 01:31:02 -0400 | [diff] [blame^] | 580 | size_t interp_off = 0; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 581 | /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */ |
| 582 | phdr = (void *)aux[AT_PHDR]; |
| 583 | for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) { |
| 584 | if (phdr->p_type == PT_PHDR) |
| 585 | app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr); |
Rich Felker | 649cec5 | 2012-07-13 01:31:02 -0400 | [diff] [blame^] | 586 | else if (phdr->p_type == PT_INTERP) |
| 587 | interp_off = (size_t)phdr->p_vaddr; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 588 | } |
Rich Felker | 649cec5 | 2012-07-13 01:31:02 -0400 | [diff] [blame^] | 589 | if (interp_off) lib->name = (char *)app->base + interp_off; |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 590 | app->name = argv[0]; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 591 | app->dynv = (void *)(app->base + find_dyn( |
| 592 | (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT])); |
| 593 | } else { |
| 594 | int fd; |
| 595 | char *ldname = argv[0]; |
| 596 | size_t dyno, l = strlen(ldname); |
| 597 | if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1; |
| 598 | *argv++ = (void *)-1; |
| 599 | if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1; |
| 600 | if (!argv[0]) { |
| 601 | dprintf(2, "musl libc/dynamic program loader\n"); |
| 602 | dprintf(2, "usage: %s pathname%s\n", ldname, |
| 603 | ldd_mode ? "" : " [args]"); |
| 604 | _exit(1); |
| 605 | } |
| 606 | fd = open(argv[0], O_RDONLY); |
| 607 | if (fd < 0) { |
| 608 | dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno)); |
| 609 | _exit(1); |
| 610 | } |
| 611 | runtime = 1; |
| 612 | ehdr = (void *)map_library(fd, &app->map_len, &app->base, &dyno); |
| 613 | if (!ehdr) { |
| 614 | dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]); |
| 615 | _exit(1); |
| 616 | } |
| 617 | runtime = 0; |
| 618 | close(fd); |
Rich Felker | 649cec5 | 2012-07-13 01:31:02 -0400 | [diff] [blame^] | 619 | lib->name = ldname; |
Rich Felker | 0420b87 | 2012-07-11 01:41:20 -0400 | [diff] [blame] | 620 | app->name = argv[0]; |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 621 | app->dynv = (void *)(app->base + dyno); |
| 622 | aux[AT_ENTRY] = ehdr->e_entry; |
Rich Felker | e12fe65 | 2012-01-23 02:02:59 -0500 | [diff] [blame] | 623 | } |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 624 | app->global = 1; |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 625 | app->constructed = 1; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 626 | decode_dyn(app); |
| 627 | |
| 628 | /* Attach to vdso, if provided by the kernel */ |
Rich Felker | cf8506a | 2011-08-16 08:50:03 -0400 | [diff] [blame] | 629 | for (i=0; auxv[i]; i+=2) { |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 630 | size_t vdso_base = auxv[i+1]; |
| 631 | if (auxv[i] != AT_SYSINFO_EHDR) continue; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 632 | ehdr = (void *)vdso_base; |
| 633 | phdr = (void *)(vdso_base + ehdr->e_phoff); |
| 634 | for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) { |
| 635 | if (phdr->p_type == PT_DYNAMIC) |
| 636 | vdso->dynv = (void *)(vdso_base + phdr->p_offset); |
| 637 | if (phdr->p_type == PT_LOAD) |
| 638 | vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset); |
| 639 | } |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 640 | vdso->name = vdso->shortname = "linux-gate.so.1"; |
Rich Felker | 427173b | 2011-07-24 02:19:47 -0400 | [diff] [blame] | 641 | vdso->global = 1; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 642 | decode_dyn(vdso); |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 643 | vdso->prev = lib; |
| 644 | lib->next = vdso; |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 645 | break; |
Rich Felker | 6ab444d | 2011-07-24 00:54:55 -0400 | [diff] [blame] | 646 | } |
| 647 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 648 | /* Initial dso chain consists only of the app. We temporarily |
| 649 | * append the dynamic linker/libc so we can relocate it, then |
| 650 | * restore the initial chain in preparation for loading third |
| 651 | * party libraries (preload/needed). */ |
| 652 | head = tail = app; |
| 653 | libc = lib; |
| 654 | app->next = lib; |
| 655 | reloc_all(lib); |
| 656 | app->next = 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 657 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 658 | /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */ |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 659 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 660 | /* Donate unused parts of app and library mapping to malloc */ |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 661 | reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]); |
| 662 | ehdr = (void *)lib->base; |
| 663 | reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff), |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 664 | ehdr->e_phentsize, ehdr->e_phnum); |
| 665 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 666 | /* Load preload/needed libraries, add their symbols to the global |
Rich Felker | fd7015d | 2012-01-23 18:32:40 -0500 | [diff] [blame] | 667 | * namespace, and perform all remaining relocations. The main |
| 668 | * program must be relocated LAST since it may contain copy |
| 669 | * relocations which depend on libraries' relocations. */ |
Rich Felker | 2719cc8 | 2011-08-16 00:24:36 -0400 | [diff] [blame] | 670 | if (env_preload) load_preload(env_preload); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 671 | load_deps(app); |
| 672 | make_global(app); |
Rich Felker | fd7015d | 2012-01-23 18:32:40 -0500 | [diff] [blame] | 673 | reloc_all(app->next); |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 674 | reloc_all(app); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 675 | |
Rich Felker | 5c1909a | 2012-05-27 16:01:44 -0400 | [diff] [blame] | 676 | if (ldd_mode) _exit(0); |
| 677 | |
Rich Felker | c82f4a3 | 2012-01-23 00:57:38 -0500 | [diff] [blame] | 678 | /* Switch to runtime mode: any further failures in the dynamic |
| 679 | * linker are a reportable failure rather than a fatal startup |
| 680 | * error. If the dynamic loader (dlopen) will not be used, free |
| 681 | * all memory used by the dynamic linker. */ |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 682 | runtime = 1; |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 683 | |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 684 | for (i=0; app->dynv[i]; i+=2) |
| 685 | if (app->dynv[i]==DT_DEBUG) |
| 686 | app->dynv[i+1] = (size_t)&debug; |
| 687 | debug.ver = 1; |
| 688 | debug.bp = _dl_debug_state; |
| 689 | debug.head = head; |
| 690 | debug.base = lib->base; |
| 691 | debug.state = 0; |
| 692 | _dl_debug_state(); |
| 693 | |
Rich Felker | 58aa5f4 | 2012-05-03 20:42:45 -0400 | [diff] [blame] | 694 | if (ssp_used) __init_ssp(auxv); |
| 695 | |
Rich Felker | 4ce3cb5 | 2012-02-06 14:39:09 -0500 | [diff] [blame] | 696 | do_init_fini(tail); |
| 697 | |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 698 | if (!rtld_used) { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 699 | free_all(head); |
| 700 | free(sys_path); |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame] | 701 | reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 702 | } |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 703 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 704 | errno = 0; |
| 705 | return (void *)aux[AT_ENTRY]; |
| 706 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 707 | |
| 708 | void *dlopen(const char *file, int mode) |
| 709 | { |
Rich Felker | 2fdea17 | 2011-07-01 22:40:00 -0400 | [diff] [blame] | 710 | struct dso *volatile p, *orig_tail = tail, *next; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 711 | size_t i; |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 712 | int cs; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 713 | |
| 714 | if (!file) return head; |
| 715 | |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 716 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 717 | pthread_rwlock_wrlock(&lock); |
| 718 | |
| 719 | if (setjmp(rtld_fail)) { |
| 720 | /* Clean up anything new that was (partially) loaded */ |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 721 | if (p->deps) for (i=0; p->deps[i]; i++) |
| 722 | if (p->deps[i]->global < 0) |
| 723 | p->deps[i]->global = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 724 | for (p=orig_tail->next; p; p=next) { |
| 725 | next = p->next; |
| 726 | munmap(p->map, p->map_len); |
| 727 | free(p->deps); |
| 728 | free(p); |
| 729 | } |
| 730 | tail = orig_tail; |
| 731 | tail->next = 0; |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 732 | p = 0; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 733 | errflag = 1; |
| 734 | goto end; |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 735 | } else p = load_library(file); |
| 736 | |
| 737 | if (!p) { |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 738 | snprintf(errbuf, sizeof errbuf, |
| 739 | "Error loading shared library %s: %m", file); |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 740 | errflag = 1; |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 741 | goto end; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 742 | } |
| 743 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 744 | /* First load handling */ |
| 745 | if (!p->deps) { |
| 746 | load_deps(p); |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 747 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 748 | if (!p->deps[i]->global) |
| 749 | p->deps[i]->global = -1; |
| 750 | if (!p->global) p->global = -1; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 751 | reloc_all(p); |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 752 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 753 | if (p->deps[i]->global < 0) |
| 754 | p->deps[i]->global = 0; |
| 755 | if (p->global < 0) p->global = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | if (mode & RTLD_GLOBAL) { |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 759 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 760 | p->deps[i]->global = 1; |
| 761 | p->global = 1; |
| 762 | } |
| 763 | |
Rich Felker | 3ec8d29 | 2012-04-25 00:05:42 -0400 | [diff] [blame] | 764 | _dl_debug_state(); |
| 765 | |
Rich Felker | ce4d97e | 2012-02-06 17:57:29 -0500 | [diff] [blame] | 766 | do_init_fini(tail); |
Rich Felker | 06933cc | 2011-06-26 22:09:32 -0400 | [diff] [blame] | 767 | end: |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 768 | pthread_rwlock_unlock(&lock); |
Rich Felker | f2baf4d | 2012-02-07 20:31:27 -0500 | [diff] [blame] | 769 | pthread_setcancelstate(cs, 0); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 770 | return p; |
| 771 | } |
| 772 | |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 773 | static void *do_dlsym(struct dso *p, const char *s, void *ra) |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 774 | { |
| 775 | size_t i; |
| 776 | uint32_t h; |
| 777 | Sym *sym; |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 778 | if (p == RTLD_NEXT) { |
| 779 | for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next); |
| 780 | if (!p) p=head; |
Rich Felker | d93e028 | 2012-07-07 16:32:27 -0400 | [diff] [blame] | 781 | void *res = find_sym(p->next, s, 0); |
| 782 | if (!res) goto failed; |
| 783 | return res; |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 784 | } |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 785 | if (p == head || p == RTLD_DEFAULT) { |
| 786 | void *res = find_sym(head, s, 0); |
Rich Felker | 4027f4e | 2012-05-04 20:18:18 -0400 | [diff] [blame] | 787 | if (!res) goto failed; |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 788 | return res; |
| 789 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 790 | h = hash(s); |
| 791 | sym = lookup(s, h, p->syms, p->hashtab, p->strings); |
| 792 | if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 793 | return p->base + sym->st_value; |
| 794 | if (p->deps) for (i=0; p->deps[i]; i++) { |
| 795 | sym = lookup(s, h, p->deps[i]->syms, |
| 796 | p->deps[i]->hashtab, p->deps[i]->strings); |
| 797 | if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 798 | return p->deps[i]->base + sym->st_value; |
| 799 | } |
Rich Felker | 4027f4e | 2012-05-04 20:18:18 -0400 | [diff] [blame] | 800 | failed: |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 801 | errflag = 1; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 802 | snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 803 | return 0; |
| 804 | } |
| 805 | |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 806 | void *__dlsym(void *p, const char *s, void *ra) |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 807 | { |
| 808 | void *res; |
| 809 | pthread_rwlock_rdlock(&lock); |
Rich Felker | 623753a | 2011-08-16 00:42:13 -0400 | [diff] [blame] | 810 | res = do_dlsym(p, s, ra); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 811 | pthread_rwlock_unlock(&lock); |
| 812 | return res; |
| 813 | } |
Rich Felker | 5a09a53 | 2012-02-03 03:16:07 -0500 | [diff] [blame] | 814 | #else |
| 815 | void *dlopen(const char *file, int mode) |
| 816 | { |
| 817 | return 0; |
| 818 | } |
| 819 | void *__dlsym(void *p, const char *s, void *ra) |
| 820 | { |
| 821 | return 0; |
| 822 | } |
| 823 | #endif |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 824 | |
| 825 | char *dlerror() |
| 826 | { |
Rich Felker | a9e85c0 | 2012-03-23 00:28:20 -0400 | [diff] [blame] | 827 | if (!errflag) return 0; |
| 828 | errflag = 0; |
Rich Felker | a5d10eb | 2012-04-23 12:03:31 -0400 | [diff] [blame] | 829 | return errbuf; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | int dlclose(void *p) |
| 833 | { |
| 834 | return 0; |
| 835 | } |