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> |
| 17 | #include <dlfcn.h> |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 18 | |
| 19 | #include "reloc.h" |
| 20 | |
| 21 | #if ULONG_MAX == 0xffffffff |
| 22 | typedef Elf32_Ehdr Ehdr; |
| 23 | typedef Elf32_Phdr Phdr; |
| 24 | typedef Elf32_Sym Sym; |
| 25 | #define R_TYPE(x) ((x)&255) |
| 26 | #define R_SYM(x) ((x)>>8) |
| 27 | #else |
| 28 | typedef Elf64_Ehdr Ehdr; |
| 29 | typedef Elf64_Phdr Phdr; |
| 30 | typedef Elf64_Sym Sym; |
| 31 | #define R_TYPE(x) ((x)&0xffffffff) |
| 32 | #define R_SYM(x) ((x)>>32) |
| 33 | #endif |
| 34 | |
| 35 | struct dso |
| 36 | { |
| 37 | struct dso *next, *prev; |
| 38 | int refcnt; |
| 39 | size_t *dynv; |
| 40 | Sym *syms; |
Rich Felker | 596d60c | 2011-06-18 22:52:01 -0400 | [diff] [blame] | 41 | uint32_t *hashtab; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 42 | char *strings; |
| 43 | unsigned char *base; |
| 44 | unsigned char *map; |
| 45 | size_t map_len; |
| 46 | dev_t dev; |
| 47 | ino_t ino; |
| 48 | int global; |
| 49 | int relocated; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 50 | struct dso **deps; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 51 | char *name; |
| 52 | char buf[]; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static struct dso *head, *tail, *libc; |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 56 | static char *env_path, *sys_path, *r_path; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 57 | static int rtld_used; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 58 | static int runtime; |
| 59 | static jmp_buf rtld_fail; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 60 | static pthread_rwlock_t lock; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 61 | |
| 62 | #define AUX_CNT 15 |
| 63 | #define DYN_CNT 34 |
| 64 | |
| 65 | static void decode_vec(size_t *v, size_t *a, size_t cnt) |
| 66 | { |
| 67 | memset(a, 0, cnt*sizeof(size_t)); |
| 68 | for (; v[0]; v+=2) if (v[0]<cnt) { |
| 69 | a[0] |= 1ULL<<v[0]; |
| 70 | a[v[0]] = v[1]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static uint32_t hash(const char *s) |
| 75 | { |
| 76 | uint_fast32_t h = 0; |
| 77 | while (*s) { |
| 78 | h = 16*h + *s++; |
| 79 | h ^= h>>24 & 0xf0; |
| 80 | } |
| 81 | return h & 0xfffffff; |
| 82 | } |
| 83 | |
Rich Felker | 596d60c | 2011-06-18 22:52:01 -0400 | [diff] [blame] | 84 | 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] | 85 | { |
| 86 | size_t i; |
| 87 | for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) { |
| 88 | if (!strcmp(s, strings+syms[i].st_name)) |
| 89 | return syms+i; |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON) |
| 95 | |
| 96 | static void *find_sym(struct dso *dso, const char *s, int need_def) |
| 97 | { |
| 98 | uint32_t h = hash(s); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 99 | if (h==0x6b366be && !strcmp(s, "dlopen")) rtld_used = 1; |
| 100 | if (h==0x6b3afd && !strcmp(s, "dlsym")) rtld_used = 1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 101 | for (; dso; dso=dso->next) { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 102 | Sym *sym; |
| 103 | if (!dso->global) continue; |
| 104 | sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 105 | if (sym && (!need_def || sym->st_shndx) && sym->st_value |
| 106 | && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 107 | return dso->base + sym->st_value; |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso) |
| 113 | { |
| 114 | Sym *sym; |
| 115 | const char *name; |
| 116 | size_t sym_val, sym_size; |
| 117 | size_t *reloc_addr; |
| 118 | void *ctx; |
| 119 | int type; |
| 120 | int sym_index; |
| 121 | |
| 122 | for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) { |
| 123 | reloc_addr = (void *)(base + rel[0]); |
| 124 | type = R_TYPE(rel[1]); |
| 125 | sym_index = R_SYM(rel[1]); |
| 126 | if (sym_index) { |
| 127 | sym = syms + sym_index; |
| 128 | name = strings + sym->st_name; |
| 129 | ctx = IS_COPY(type) ? dso->next : dso; |
Rich Felker | 32de61e | 2011-06-25 22:36:21 -0400 | [diff] [blame] | 130 | sym_val = (size_t)find_sym(ctx, name, IS_PLT(type)); |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 131 | if (!sym_val && sym->st_info>>4 != STB_WEAK) { |
| 132 | if (runtime) longjmp(rtld_fail, 1); |
| 133 | dprintf(2, "%s: symbol not found\n", name); |
| 134 | _exit(127); |
| 135 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 136 | sym_size = sym->st_size; |
| 137 | } |
| 138 | do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]); |
| 139 | } |
| 140 | } |
| 141 | |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 142 | /* A huge hack: to make up for the wastefulness of shared libraries |
| 143 | * needing at least a page of dirty memory even if they have no global |
| 144 | * data, we reclaim the gaps at the beginning and end of writable maps |
| 145 | * and "donate" them to the heap by setting up minimal malloc |
| 146 | * structures and then freeing them. */ |
| 147 | |
| 148 | static void reclaim(unsigned char *base, size_t start, size_t end) |
| 149 | { |
| 150 | size_t *a, *z; |
| 151 | start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t); |
| 152 | end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t); |
| 153 | if (start>end || end-start < 4*sizeof(size_t)) return; |
| 154 | a = (size_t *)(base + start); |
| 155 | z = (size_t *)(base + end); |
| 156 | a[-2] = 1; |
| 157 | a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1; |
| 158 | z[1] = 1; |
| 159 | free(a); |
| 160 | } |
| 161 | |
| 162 | static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt) |
| 163 | { |
| 164 | for (; phcnt--; ph=(void *)((char *)ph+phent)) { |
| 165 | if (ph->p_type!=PT_LOAD) continue; |
| 166 | if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue; |
| 167 | reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr); |
| 168 | reclaim(base, ph->p_vaddr+ph->p_memsz, |
| 169 | ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE); |
| 170 | } |
| 171 | } |
| 172 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 173 | static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp) |
| 174 | { |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 175 | Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)]; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 176 | size_t phsize; |
| 177 | size_t addr_min=SIZE_MAX, addr_max=0, map_len; |
| 178 | size_t this_min, this_max; |
| 179 | off_t off_start; |
| 180 | Ehdr *eh; |
| 181 | Phdr *ph; |
| 182 | unsigned prot; |
| 183 | unsigned char *map, *base; |
| 184 | size_t dyn; |
| 185 | size_t i; |
| 186 | |
| 187 | ssize_t l = read(fd, buf, sizeof buf); |
| 188 | if (l<sizeof *eh) return 0; |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 189 | eh = buf; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 190 | phsize = eh->e_phentsize * eh->e_phnum; |
| 191 | if (phsize + sizeof *eh > l) return 0; |
| 192 | if (eh->e_phoff + phsize > l) { |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 193 | l = pread(fd, buf+1, phsize, eh->e_phoff); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 194 | if (l != phsize) return 0; |
| 195 | eh->e_phoff = sizeof *eh; |
| 196 | } |
| 197 | ph = (void *)((char *)buf + eh->e_phoff); |
| 198 | for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { |
| 199 | if (ph->p_type == PT_DYNAMIC) |
| 200 | dyn = ph->p_vaddr; |
| 201 | if (ph->p_type != PT_LOAD) continue; |
| 202 | if (ph->p_vaddr < addr_min) { |
| 203 | addr_min = ph->p_vaddr; |
| 204 | off_start = ph->p_offset; |
| 205 | prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | |
| 206 | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | |
| 207 | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); |
| 208 | } |
| 209 | if (ph->p_vaddr+ph->p_memsz > addr_max) { |
| 210 | addr_max = ph->p_vaddr+ph->p_memsz; |
| 211 | } |
| 212 | } |
| 213 | if (!dyn) return 0; |
| 214 | addr_max += PAGE_SIZE-1; |
| 215 | addr_max &= -PAGE_SIZE; |
| 216 | addr_min &= -PAGE_SIZE; |
| 217 | off_start &= -PAGE_SIZE; |
| 218 | map_len = addr_max - addr_min + off_start; |
| 219 | /* The first time, we map too much, possibly even more than |
| 220 | * the length of the file. This is okay because we will not |
| 221 | * use the invalid part; we just need to reserve the right |
| 222 | * amount of virtual address space to map over later. */ |
Rich Felker | bf30100 | 2011-06-28 14:20:41 -0400 | [diff] [blame] | 223 | 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] | 224 | if (map==MAP_FAILED) return 0; |
| 225 | base = map - addr_min; |
| 226 | ph = (void *)((char *)buf + eh->e_phoff); |
| 227 | for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { |
| 228 | if (ph->p_type != PT_LOAD) continue; |
| 229 | /* Reuse the existing mapping for the lowest-address LOAD */ |
| 230 | if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue; |
| 231 | this_min = ph->p_vaddr & -PAGE_SIZE; |
| 232 | this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE; |
| 233 | off_start = ph->p_offset & -PAGE_SIZE; |
| 234 | prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | |
| 235 | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | |
| 236 | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); |
| 237 | if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) { |
| 238 | munmap(map, map_len); |
| 239 | return 0; |
| 240 | } |
| 241 | if (ph->p_memsz > ph->p_filesz) { |
| 242 | size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz; |
| 243 | size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE; |
| 244 | memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1); |
| 245 | 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) { |
| 246 | munmap(map, map_len); |
| 247 | return 0; |
| 248 | } |
| 249 | } |
| 250 | } |
Rich Felker | 9f17413 | 2011-06-29 00:29:08 -0400 | [diff] [blame] | 251 | for (i=0; ((size_t *)(base+dyn))[i]; i+=2) |
| 252 | if (((size_t *)(base+dyn))[i]==DT_TEXTREL) { |
| 253 | mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC); |
| 254 | break; |
| 255 | } |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 256 | if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff), |
| 257 | eh->e_phentsize, eh->e_phnum); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 258 | *lenp = map_len; |
| 259 | *basep = base; |
| 260 | *dynp = dyn; |
| 261 | return map; |
| 262 | } |
| 263 | |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 264 | static int path_open(const char *name, const char *search) |
| 265 | { |
| 266 | char buf[2*NAME_MAX+2]; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 267 | const char *s=search, *z; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 268 | int l, fd; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 269 | for (;;) { |
| 270 | while (*s==':') s++; |
| 271 | if (!*s) return -1; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 272 | z = strchr(s, ':'); |
| 273 | l = z ? z-s : strlen(s); |
| 274 | snprintf(buf, sizeof buf, "%.*s/%s", l, s, name); |
| 275 | if ((fd = open(buf, O_RDONLY))>=0) return fd; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 276 | s += l; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 277 | } |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 278 | } |
| 279 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 280 | static struct dso *load_library(const char *name) |
| 281 | { |
| 282 | unsigned char *base, *map; |
| 283 | size_t dyno, map_len; |
| 284 | struct dso *p; |
| 285 | size_t dyn[DYN_CNT] = {0}; |
| 286 | int fd; |
| 287 | struct stat st; |
| 288 | |
| 289 | /* Catch and block attempts to reload the implementation itself */ |
| 290 | if (name[0]=='l' && name[1]=='i' && name[2]=='b') { |
| 291 | static const char *rp, reserved[] = |
| 292 | "c\0pthread\0rt\0m\0dl\0util\0xnet\0"; |
| 293 | char *z = strchr(name, '.'); |
| 294 | if (z) { |
| 295 | size_t l = z-name; |
| 296 | for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1); |
| 297 | if (*rp) { |
| 298 | if (!libc->prev) { |
| 299 | tail->next = libc; |
| 300 | libc->prev = tail; |
| 301 | tail = libc; |
| 302 | } |
| 303 | return libc; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | /* Search for the name to see if it's already loaded */ |
| 308 | for (p=head->next; p; p=p->next) { |
| 309 | if (!strcmp(p->name, name)) { |
| 310 | p->refcnt++; |
| 311 | return p; |
| 312 | } |
| 313 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 314 | if (strchr(name, '/')) { |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 315 | fd = open(name, O_RDONLY); |
| 316 | } else { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 317 | if (strlen(name) > NAME_MAX) return 0; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 318 | fd = -1; |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 319 | if (r_path) fd = path_open(name, r_path); |
| 320 | if (fd < 0 && env_path) fd = path_open(name, env_path); |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 321 | if (fd < 0) { |
| 322 | if (!sys_path) { |
| 323 | FILE *f = fopen(ETC_LDSO_PATH, "r"); |
| 324 | if (f) { |
| 325 | if (getline(&sys_path, (size_t[1]){0}, f) > 0) |
| 326 | sys_path[strlen(sys_path)-1]=0; |
| 327 | fclose(f); |
| 328 | } |
| 329 | } |
| 330 | if (sys_path) fd = path_open(name, sys_path); |
| 331 | else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib"); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | if (fd < 0) return 0; |
| 335 | if (fstat(fd, &st) < 0) { |
| 336 | close(fd); |
| 337 | return 0; |
| 338 | } |
| 339 | for (p=head->next; p; p=p->next) { |
| 340 | if (p->dev == st.st_dev && p->ino == st.st_ino) { |
| 341 | close(fd); |
| 342 | p->refcnt++; |
| 343 | return p; |
| 344 | } |
| 345 | } |
| 346 | map = map_library(fd, &map_len, &base, &dyno); |
| 347 | close(fd); |
| 348 | if (!map) return 0; |
| 349 | p = calloc(1, sizeof *p + strlen(name) + 1); |
| 350 | if (!p) { |
| 351 | munmap(map, map_len); |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | p->map = map; |
| 356 | p->map_len = map_len; |
| 357 | p->base = base; |
| 358 | p->dynv = (void *)(base + dyno); |
| 359 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 360 | |
| 361 | p->syms = (void *)(base + dyn[DT_SYMTAB]); |
| 362 | p->hashtab = (void *)(base + dyn[DT_HASH]); |
| 363 | p->strings = (void *)(base + dyn[DT_STRTAB]); |
| 364 | p->dev = st.st_dev; |
| 365 | p->ino = st.st_ino; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 366 | p->refcnt = 1; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 367 | p->name = p->buf; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 368 | strcpy(p->name, name); |
| 369 | |
| 370 | tail->next = p; |
| 371 | p->prev = tail; |
| 372 | tail = p; |
| 373 | |
| 374 | return p; |
| 375 | } |
| 376 | |
| 377 | static void load_deps(struct dso *p) |
| 378 | { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 379 | size_t i, ndeps=0; |
| 380 | struct dso ***deps = &p->deps, **tmp, *dep; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 381 | for (; p; p=p->next) { |
| 382 | for (i=0; p->dynv[i]; i+=2) { |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 383 | if (p->dynv[i] != DT_RPATH) continue; |
| 384 | r_path = (void *)(p->strings + p->dynv[i+1]); |
| 385 | } |
| 386 | for (i=0; p->dynv[i]; i+=2) { |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 387 | if (p->dynv[i] != DT_NEEDED) continue; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 388 | dep = load_library(p->strings + p->dynv[i+1]); |
| 389 | if (!dep) { |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 390 | if (runtime) longjmp(rtld_fail, 1); |
| 391 | dprintf(2, "%s: %m (needed by %s)\n", |
| 392 | p->strings + p->dynv[i+1], p->name); |
| 393 | _exit(127); |
| 394 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 395 | if (runtime) { |
| 396 | tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2)); |
| 397 | if (!tmp) longjmp(rtld_fail, 1); |
| 398 | tmp[ndeps++] = dep; |
| 399 | tmp[ndeps] = 0; |
| 400 | *deps = tmp; |
| 401 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 402 | } |
Rich Felker | 191ebca | 2011-06-30 23:02:27 -0400 | [diff] [blame] | 403 | r_path = 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 407 | static void make_global(struct dso *p) |
| 408 | { |
| 409 | for (; p; p=p->next) p->global = 1; |
| 410 | } |
| 411 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 412 | static void reloc_all(struct dso *p) |
| 413 | { |
| 414 | size_t dyn[DYN_CNT] = {0}; |
| 415 | for (; p; p=p->next) { |
| 416 | if (p->relocated) continue; |
| 417 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 418 | 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] | 419 | 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 420 | 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] | 421 | 2, p->syms, p->strings, head); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 422 | 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] | 423 | 3, p->syms, p->strings, head); |
Rich Felker | 368ba4a | 2011-06-25 00:18:19 -0400 | [diff] [blame] | 424 | p->relocated = 1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 428 | static void free_all(struct dso *p) |
| 429 | { |
| 430 | struct dso *n; |
| 431 | while (p) { |
| 432 | n = p->next; |
| 433 | if (p->map) free(p); |
| 434 | p = n; |
| 435 | } |
| 436 | } |
| 437 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 438 | void *__dynlink(int argc, char **argv, size_t *got) |
| 439 | { |
| 440 | size_t *auxv, aux[AUX_CNT] = {0}; |
| 441 | size_t app_dyn[DYN_CNT] = {0}; |
| 442 | size_t lib_dyn[DYN_CNT] = {0}; |
| 443 | size_t i; |
| 444 | Phdr *phdr; |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 445 | Ehdr *ehdr; |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame^] | 446 | static struct dso builtin_dsos[2]; |
| 447 | struct dso *const app = builtin_dsos+0; |
| 448 | struct dso *const lib = builtin_dsos+1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 449 | |
| 450 | /* Find aux vector just past environ[] */ |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 451 | for (i=argc+1; argv[i]; i++) |
| 452 | if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16)) |
| 453 | env_path = argv[i]+16; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 454 | auxv = (void *)(argv+i+1); |
| 455 | |
| 456 | decode_vec(auxv, aux, AUX_CNT); |
| 457 | |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 458 | /* Only trust user/env if kernel says we're not suid/sgid */ |
| 459 | if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID] |
| 460 | || aux[AT_GID]!=aux[AT_EGID]) { |
| 461 | env_path = 0; |
| 462 | } |
| 463 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 464 | /* Relocate ldso's DYNAMIC pointer and load vector */ |
| 465 | decode_vec((void *)(got[0] += aux[AT_BASE]), lib_dyn, DYN_CNT); |
| 466 | |
| 467 | /* Find the program image's DYNAMIC section and decode it */ |
| 468 | phdr = (void *)aux[AT_PHDR]; |
| 469 | for (i=aux[AT_PHNUM]; i--; phdr=(void *)((char *)phdr + aux[AT_PHENT])) { |
| 470 | if (phdr->p_type == PT_DYNAMIC) { |
| 471 | decode_vec((void *)phdr->p_vaddr, app_dyn, DYN_CNT); |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame^] | 476 | *app = (struct dso){ |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 477 | .base = 0, |
| 478 | .strings = (void *)(app_dyn[DT_STRTAB]), |
| 479 | .hashtab = (void *)(app_dyn[DT_HASH]), |
| 480 | .syms = (void *)(app_dyn[DT_SYMTAB]), |
| 481 | .dynv = (void *)(phdr->p_vaddr), |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 482 | .name = argv[0], |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 483 | .global = 1, |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame^] | 484 | .next = lib |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 485 | }; |
| 486 | |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame^] | 487 | *lib = (struct dso){ |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 488 | .base = (void *)aux[AT_BASE], |
| 489 | .strings = (void *)(aux[AT_BASE]+lib_dyn[DT_STRTAB]), |
| 490 | .hashtab = (void *)(aux[AT_BASE]+lib_dyn[DT_HASH]), |
| 491 | .syms = (void *)(aux[AT_BASE]+lib_dyn[DT_SYMTAB]), |
| 492 | .dynv = (void *)(got[0]), |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 493 | .name = "libc.so", |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 494 | .global = 1, |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 495 | .relocated = 1 |
| 496 | }; |
| 497 | |
| 498 | /* Relocate the dynamic linker/libc */ |
| 499 | do_relocs((void *)aux[AT_BASE], (void *)(aux[AT_BASE]+lib_dyn[DT_REL]), |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame^] | 500 | lib_dyn[DT_RELSZ], 2, lib->syms, lib->strings, app); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 501 | do_relocs((void *)aux[AT_BASE], (void *)(aux[AT_BASE]+lib_dyn[DT_RELA]), |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame^] | 502 | lib_dyn[DT_RELASZ], 3, lib->syms, lib->strings, app); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 503 | |
| 504 | /* At this point the standard library is fully functional */ |
| 505 | |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame^] | 506 | reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]); |
| 507 | ehdr = (void *)lib->base; |
| 508 | reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff), |
Rich Felker | 6717e62 | 2011-06-28 19:40:14 -0400 | [diff] [blame] | 509 | ehdr->e_phentsize, ehdr->e_phnum); |
| 510 | |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame^] | 511 | head = tail = app; |
| 512 | libc = lib; |
| 513 | app->next = 0; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 514 | load_deps(head); |
| 515 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 516 | make_global(head); |
Rich Felker | c7debe1 | 2011-06-28 14:13:51 -0400 | [diff] [blame] | 517 | reloc_all(head->next); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 518 | reloc_all(head); |
| 519 | |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame^] | 520 | runtime = 1; |
| 521 | if (!rtld_used) { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 522 | free_all(head); |
| 523 | free(sys_path); |
Rich Felker | a53de81 | 2011-07-24 00:26:12 -0400 | [diff] [blame^] | 524 | reclaim((void *)builtin_dsos, 0, sizeof builtin_dsos); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 525 | } |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 526 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 527 | errno = 0; |
| 528 | return (void *)aux[AT_ENTRY]; |
| 529 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 530 | |
| 531 | void *dlopen(const char *file, int mode) |
| 532 | { |
Rich Felker | 2fdea17 | 2011-07-01 22:40:00 -0400 | [diff] [blame] | 533 | struct dso *volatile p, *orig_tail = tail, *next; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 534 | size_t i; |
| 535 | |
| 536 | if (!file) return head; |
| 537 | |
| 538 | pthread_rwlock_wrlock(&lock); |
| 539 | |
| 540 | if (setjmp(rtld_fail)) { |
| 541 | /* Clean up anything new that was (partially) loaded */ |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 542 | if (p->deps) for (i=0; p->deps[i]; i++) |
| 543 | if (p->deps[i]->global < 0) |
| 544 | p->deps[i]->global = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 545 | for (p=orig_tail->next; p; p=next) { |
| 546 | next = p->next; |
| 547 | munmap(p->map, p->map_len); |
| 548 | free(p->deps); |
| 549 | free(p); |
| 550 | } |
| 551 | tail = orig_tail; |
| 552 | tail->next = 0; |
| 553 | pthread_rwlock_unlock(&lock); |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | p = load_library(file); |
Rich Felker | 06933cc | 2011-06-26 22:09:32 -0400 | [diff] [blame] | 558 | if (!p) goto end; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 559 | |
| 560 | /* First load handling */ |
| 561 | if (!p->deps) { |
| 562 | load_deps(p); |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 563 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 564 | if (!p->deps[i]->global) |
| 565 | p->deps[i]->global = -1; |
| 566 | if (!p->global) p->global = -1; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 567 | reloc_all(p); |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 568 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 92ab5d8 | 2011-06-26 21:21:04 -0400 | [diff] [blame] | 569 | if (p->deps[i]->global < 0) |
| 570 | p->deps[i]->global = 0; |
| 571 | if (p->global < 0) p->global = 0; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | if (mode & RTLD_GLOBAL) { |
Rich Felker | 0e4dae3 | 2011-06-26 21:36:44 -0400 | [diff] [blame] | 575 | if (p->deps) for (i=0; p->deps[i]; i++) |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 576 | p->deps[i]->global = 1; |
| 577 | p->global = 1; |
| 578 | } |
| 579 | |
Rich Felker | 06933cc | 2011-06-26 22:09:32 -0400 | [diff] [blame] | 580 | end: |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 581 | pthread_rwlock_unlock(&lock); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 582 | return p; |
| 583 | } |
| 584 | |
| 585 | static void *do_dlsym(struct dso *p, const char *s) |
| 586 | { |
| 587 | size_t i; |
| 588 | uint32_t h; |
| 589 | Sym *sym; |
Rich Felker | 97507bd | 2011-06-26 21:50:01 -0400 | [diff] [blame] | 590 | if (p == head || p == RTLD_DEFAULT) |
| 591 | return find_sym(head, s, 0); |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame] | 592 | h = hash(s); |
| 593 | sym = lookup(s, h, p->syms, p->hashtab, p->strings); |
| 594 | if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 595 | return p->base + sym->st_value; |
| 596 | if (p->deps) for (i=0; p->deps[i]; i++) { |
| 597 | sym = lookup(s, h, p->deps[i]->syms, |
| 598 | p->deps[i]->hashtab, p->deps[i]->strings); |
| 599 | if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 600 | return p->deps[i]->base + sym->st_value; |
| 601 | } |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | void *dlsym(void *p, const char *s) |
| 606 | { |
| 607 | void *res; |
| 608 | pthread_rwlock_rdlock(&lock); |
| 609 | res = do_dlsym(p, s); |
| 610 | pthread_rwlock_unlock(&lock); |
| 611 | return res; |
| 612 | } |
| 613 | |
| 614 | char *dlerror() |
| 615 | { |
| 616 | return "unknown error"; |
| 617 | } |
| 618 | |
| 619 | int dlclose(void *p) |
| 620 | { |
| 621 | return 0; |
| 622 | } |