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 | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 56 | static char *env_path, *sys_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 | |
| 142 | static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp) |
| 143 | { |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 144 | Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)]; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 145 | size_t phsize; |
| 146 | size_t addr_min=SIZE_MAX, addr_max=0, map_len; |
| 147 | size_t this_min, this_max; |
| 148 | off_t off_start; |
| 149 | Ehdr *eh; |
| 150 | Phdr *ph; |
| 151 | unsigned prot; |
| 152 | unsigned char *map, *base; |
| 153 | size_t dyn; |
| 154 | size_t i; |
| 155 | |
| 156 | ssize_t l = read(fd, buf, sizeof buf); |
| 157 | if (l<sizeof *eh) return 0; |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 158 | eh = buf; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 159 | phsize = eh->e_phentsize * eh->e_phnum; |
| 160 | if (phsize + sizeof *eh > l) return 0; |
| 161 | if (eh->e_phoff + phsize > l) { |
Rich Felker | 59633c7 | 2011-06-25 12:26:08 -0400 | [diff] [blame] | 162 | l = pread(fd, buf+1, phsize, eh->e_phoff); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 163 | if (l != phsize) return 0; |
| 164 | eh->e_phoff = sizeof *eh; |
| 165 | } |
| 166 | ph = (void *)((char *)buf + eh->e_phoff); |
| 167 | for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { |
| 168 | if (ph->p_type == PT_DYNAMIC) |
| 169 | dyn = ph->p_vaddr; |
| 170 | if (ph->p_type != PT_LOAD) continue; |
| 171 | if (ph->p_vaddr < addr_min) { |
| 172 | addr_min = ph->p_vaddr; |
| 173 | off_start = ph->p_offset; |
| 174 | prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | |
| 175 | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | |
| 176 | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); |
| 177 | } |
| 178 | if (ph->p_vaddr+ph->p_memsz > addr_max) { |
| 179 | addr_max = ph->p_vaddr+ph->p_memsz; |
| 180 | } |
| 181 | } |
| 182 | if (!dyn) return 0; |
| 183 | addr_max += PAGE_SIZE-1; |
| 184 | addr_max &= -PAGE_SIZE; |
| 185 | addr_min &= -PAGE_SIZE; |
| 186 | off_start &= -PAGE_SIZE; |
| 187 | map_len = addr_max - addr_min + off_start; |
| 188 | /* The first time, we map too much, possibly even more than |
| 189 | * the length of the file. This is okay because we will not |
| 190 | * use the invalid part; we just need to reserve the right |
| 191 | * amount of virtual address space to map over later. */ |
| 192 | map = mmap(0, map_len, prot, MAP_PRIVATE, fd, off_start); |
| 193 | if (map==MAP_FAILED) return 0; |
| 194 | base = map - addr_min; |
| 195 | ph = (void *)((char *)buf + eh->e_phoff); |
| 196 | for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { |
| 197 | if (ph->p_type != PT_LOAD) continue; |
| 198 | /* Reuse the existing mapping for the lowest-address LOAD */ |
| 199 | if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue; |
| 200 | this_min = ph->p_vaddr & -PAGE_SIZE; |
| 201 | this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE; |
| 202 | off_start = ph->p_offset & -PAGE_SIZE; |
| 203 | prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | |
| 204 | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | |
| 205 | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); |
| 206 | if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) { |
| 207 | munmap(map, map_len); |
| 208 | return 0; |
| 209 | } |
| 210 | if (ph->p_memsz > ph->p_filesz) { |
| 211 | size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz; |
| 212 | size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE; |
| 213 | memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1); |
| 214 | 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) { |
| 215 | munmap(map, map_len); |
| 216 | return 0; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | *lenp = map_len; |
| 221 | *basep = base; |
| 222 | *dynp = dyn; |
| 223 | return map; |
| 224 | } |
| 225 | |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 226 | static int path_open(const char *name, const char *search) |
| 227 | { |
| 228 | char buf[2*NAME_MAX+2]; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 229 | const char *s=search, *z; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 230 | int l, fd; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 231 | for (;;) { |
| 232 | while (*s==':') s++; |
| 233 | if (!*s) return -1; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 234 | z = strchr(s, ':'); |
| 235 | l = z ? z-s : strlen(s); |
| 236 | snprintf(buf, sizeof buf, "%.*s/%s", l, s, name); |
| 237 | if ((fd = open(buf, O_RDONLY))>=0) return fd; |
Rich Felker | 49388f3 | 2011-06-25 17:49:16 -0400 | [diff] [blame] | 238 | s += l; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 239 | } |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 242 | static struct dso *load_library(const char *name) |
| 243 | { |
| 244 | unsigned char *base, *map; |
| 245 | size_t dyno, map_len; |
| 246 | struct dso *p; |
| 247 | size_t dyn[DYN_CNT] = {0}; |
| 248 | int fd; |
| 249 | struct stat st; |
| 250 | |
| 251 | /* Catch and block attempts to reload the implementation itself */ |
| 252 | if (name[0]=='l' && name[1]=='i' && name[2]=='b') { |
| 253 | static const char *rp, reserved[] = |
| 254 | "c\0pthread\0rt\0m\0dl\0util\0xnet\0"; |
| 255 | char *z = strchr(name, '.'); |
| 256 | if (z) { |
| 257 | size_t l = z-name; |
| 258 | for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1); |
| 259 | if (*rp) { |
| 260 | if (!libc->prev) { |
| 261 | tail->next = libc; |
| 262 | libc->prev = tail; |
| 263 | tail = libc; |
| 264 | } |
| 265 | return libc; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | /* Search for the name to see if it's already loaded */ |
| 270 | for (p=head->next; p; p=p->next) { |
| 271 | if (!strcmp(p->name, name)) { |
| 272 | p->refcnt++; |
| 273 | return p; |
| 274 | } |
| 275 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 276 | if (strchr(name, '/')) { |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 277 | fd = open(name, O_RDONLY); |
| 278 | } else { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 279 | if (strlen(name) > NAME_MAX) return 0; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 280 | fd = -1; |
| 281 | if (env_path) fd = path_open(name, env_path); |
| 282 | if (fd < 0) { |
| 283 | if (!sys_path) { |
| 284 | FILE *f = fopen(ETC_LDSO_PATH, "r"); |
| 285 | if (f) { |
| 286 | if (getline(&sys_path, (size_t[1]){0}, f) > 0) |
| 287 | sys_path[strlen(sys_path)-1]=0; |
| 288 | fclose(f); |
| 289 | } |
| 290 | } |
| 291 | if (sys_path) fd = path_open(name, sys_path); |
| 292 | else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib"); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | if (fd < 0) return 0; |
| 296 | if (fstat(fd, &st) < 0) { |
| 297 | close(fd); |
| 298 | return 0; |
| 299 | } |
| 300 | for (p=head->next; p; p=p->next) { |
| 301 | if (p->dev == st.st_dev && p->ino == st.st_ino) { |
| 302 | close(fd); |
| 303 | p->refcnt++; |
| 304 | return p; |
| 305 | } |
| 306 | } |
| 307 | map = map_library(fd, &map_len, &base, &dyno); |
| 308 | close(fd); |
| 309 | if (!map) return 0; |
| 310 | p = calloc(1, sizeof *p + strlen(name) + 1); |
| 311 | if (!p) { |
| 312 | munmap(map, map_len); |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | p->map = map; |
| 317 | p->map_len = map_len; |
| 318 | p->base = base; |
| 319 | p->dynv = (void *)(base + dyno); |
| 320 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 321 | |
| 322 | p->syms = (void *)(base + dyn[DT_SYMTAB]); |
| 323 | p->hashtab = (void *)(base + dyn[DT_HASH]); |
| 324 | p->strings = (void *)(base + dyn[DT_STRTAB]); |
| 325 | p->dev = st.st_dev; |
| 326 | p->ino = st.st_ino; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 327 | p->refcnt = 1; |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 328 | p->name = p->buf; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 329 | strcpy(p->name, name); |
| 330 | |
| 331 | tail->next = p; |
| 332 | p->prev = tail; |
| 333 | tail = p; |
| 334 | |
| 335 | return p; |
| 336 | } |
| 337 | |
| 338 | static void load_deps(struct dso *p) |
| 339 | { |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 340 | size_t i, ndeps=0; |
| 341 | struct dso ***deps = &p->deps, **tmp, *dep; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 342 | for (; p; p=p->next) { |
| 343 | for (i=0; p->dynv[i]; i+=2) { |
| 344 | if (p->dynv[i] != DT_NEEDED) continue; |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 345 | dep = load_library(p->strings + p->dynv[i+1]); |
| 346 | if (!dep) { |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 347 | if (runtime) longjmp(rtld_fail, 1); |
| 348 | dprintf(2, "%s: %m (needed by %s)\n", |
| 349 | p->strings + p->dynv[i+1], p->name); |
| 350 | _exit(127); |
| 351 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 352 | if (runtime) { |
| 353 | tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2)); |
| 354 | if (!tmp) longjmp(rtld_fail, 1); |
| 355 | tmp[ndeps++] = dep; |
| 356 | tmp[ndeps] = 0; |
| 357 | *deps = tmp; |
| 358 | } |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 363 | static void make_global(struct dso *p) |
| 364 | { |
| 365 | for (; p; p=p->next) p->global = 1; |
| 366 | } |
| 367 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 368 | static void reloc_all(struct dso *p) |
| 369 | { |
| 370 | size_t dyn[DYN_CNT] = {0}; |
| 371 | for (; p; p=p->next) { |
| 372 | if (p->relocated) continue; |
| 373 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 374 | 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] | 375 | 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, head); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 376 | 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] | 377 | 2, p->syms, p->strings, head); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 378 | 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] | 379 | 3, p->syms, p->strings, head); |
Rich Felker | 368ba4a | 2011-06-25 00:18:19 -0400 | [diff] [blame] | 380 | p->relocated = 1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 384 | static void free_all(struct dso *p) |
| 385 | { |
| 386 | struct dso *n; |
| 387 | while (p) { |
| 388 | n = p->next; |
| 389 | if (p->map) free(p); |
| 390 | p = n; |
| 391 | } |
| 392 | } |
| 393 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 394 | void *__dynlink(int argc, char **argv, size_t *got) |
| 395 | { |
| 396 | size_t *auxv, aux[AUX_CNT] = {0}; |
| 397 | size_t app_dyn[DYN_CNT] = {0}; |
| 398 | size_t lib_dyn[DYN_CNT] = {0}; |
| 399 | size_t i; |
| 400 | Phdr *phdr; |
| 401 | struct dso lib, app; |
| 402 | |
| 403 | /* Find aux vector just past environ[] */ |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 404 | for (i=argc+1; argv[i]; i++) |
| 405 | if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16)) |
| 406 | env_path = argv[i]+16; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 407 | auxv = (void *)(argv+i+1); |
| 408 | |
| 409 | decode_vec(auxv, aux, AUX_CNT); |
| 410 | |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame] | 411 | /* Only trust user/env if kernel says we're not suid/sgid */ |
| 412 | if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID] |
| 413 | || aux[AT_GID]!=aux[AT_EGID]) { |
| 414 | env_path = 0; |
| 415 | } |
| 416 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 417 | /* Relocate ldso's DYNAMIC pointer and load vector */ |
| 418 | decode_vec((void *)(got[0] += aux[AT_BASE]), lib_dyn, DYN_CNT); |
| 419 | |
| 420 | /* Find the program image's DYNAMIC section and decode it */ |
| 421 | phdr = (void *)aux[AT_PHDR]; |
| 422 | for (i=aux[AT_PHNUM]; i--; phdr=(void *)((char *)phdr + aux[AT_PHENT])) { |
| 423 | if (phdr->p_type == PT_DYNAMIC) { |
| 424 | decode_vec((void *)phdr->p_vaddr, app_dyn, DYN_CNT); |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | app = (struct dso){ |
| 430 | .base = 0, |
| 431 | .strings = (void *)(app_dyn[DT_STRTAB]), |
| 432 | .hashtab = (void *)(app_dyn[DT_HASH]), |
| 433 | .syms = (void *)(app_dyn[DT_SYMTAB]), |
| 434 | .dynv = (void *)(phdr->p_vaddr), |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 435 | .name = argv[0], |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 436 | .global = 1, |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 437 | .next = &lib |
| 438 | }; |
| 439 | |
| 440 | lib = (struct dso){ |
| 441 | .base = (void *)aux[AT_BASE], |
| 442 | .strings = (void *)(aux[AT_BASE]+lib_dyn[DT_STRTAB]), |
| 443 | .hashtab = (void *)(aux[AT_BASE]+lib_dyn[DT_HASH]), |
| 444 | .syms = (void *)(aux[AT_BASE]+lib_dyn[DT_SYMTAB]), |
| 445 | .dynv = (void *)(got[0]), |
Rich Felker | 6b3d5e5 | 2011-06-26 17:39:17 -0400 | [diff] [blame] | 446 | .name = "libc.so", |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 447 | .global = 1, |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 448 | .relocated = 1 |
| 449 | }; |
| 450 | |
| 451 | /* Relocate the dynamic linker/libc */ |
| 452 | do_relocs((void *)aux[AT_BASE], (void *)(aux[AT_BASE]+lib_dyn[DT_REL]), |
| 453 | lib_dyn[DT_RELSZ], 2, lib.syms, lib.strings, &app); |
| 454 | do_relocs((void *)aux[AT_BASE], (void *)(aux[AT_BASE]+lib_dyn[DT_RELA]), |
| 455 | lib_dyn[DT_RELASZ], 3, lib.syms, lib.strings, &app); |
| 456 | |
| 457 | /* At this point the standard library is fully functional */ |
| 458 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 459 | head = tail = &app; |
| 460 | libc = &lib; |
| 461 | app.next = 0; |
| 462 | load_deps(head); |
| 463 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 464 | make_global(head); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 465 | reloc_all(head); |
| 466 | |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 467 | if (rtld_used) { |
| 468 | runtime = 1; |
| 469 | head->next->prev = malloc(sizeof *head); |
| 470 | *head->next->prev = *head; |
| 471 | head = head->next->prev; |
| 472 | libc->prev->next = malloc(sizeof *libc); |
| 473 | *libc->prev->next = *libc; |
| 474 | libc = libc->prev->next; |
| 475 | if (libc->next) libc->next->prev = libc; |
| 476 | } else { |
| 477 | free_all(head); |
| 478 | free(sys_path); |
| 479 | } |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 480 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 481 | errno = 0; |
| 482 | return (void *)aux[AT_ENTRY]; |
| 483 | } |
Rich Felker | 59ab43f | 2011-06-26 19:23:28 -0400 | [diff] [blame^] | 484 | |
| 485 | void *dlopen(const char *file, int mode) |
| 486 | { |
| 487 | struct dso *p, *orig_tail = tail, *next; |
| 488 | size_t i; |
| 489 | |
| 490 | if (!file) return head; |
| 491 | |
| 492 | pthread_rwlock_wrlock(&lock); |
| 493 | |
| 494 | if (setjmp(rtld_fail)) { |
| 495 | /* Clean up anything new that was (partially) loaded */ |
| 496 | for (p=orig_tail->next; p; p=next) { |
| 497 | next = p->next; |
| 498 | munmap(p->map, p->map_len); |
| 499 | free(p->deps); |
| 500 | free(p); |
| 501 | } |
| 502 | tail = orig_tail; |
| 503 | tail->next = 0; |
| 504 | pthread_rwlock_unlock(&lock); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | p = load_library(file); |
| 509 | if (!p) return 0; |
| 510 | |
| 511 | /* First load handling */ |
| 512 | if (!p->deps) { |
| 513 | load_deps(p); |
| 514 | reloc_all(p); |
| 515 | } |
| 516 | |
| 517 | if (mode & RTLD_GLOBAL) { |
| 518 | for (i=0; p->deps[i]; i++) |
| 519 | p->deps[i]->global = 1; |
| 520 | p->global = 1; |
| 521 | } |
| 522 | |
| 523 | pthread_rwlock_unlock(&lock); |
| 524 | |
| 525 | return p; |
| 526 | } |
| 527 | |
| 528 | static void *do_dlsym(struct dso *p, const char *s) |
| 529 | { |
| 530 | size_t i; |
| 531 | uint32_t h; |
| 532 | Sym *sym; |
| 533 | if (p == head) return find_sym(head, s, 0); |
| 534 | h = hash(s); |
| 535 | sym = lookup(s, h, p->syms, p->hashtab, p->strings); |
| 536 | if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 537 | return p->base + sym->st_value; |
| 538 | if (p->deps) for (i=0; p->deps[i]; i++) { |
| 539 | sym = lookup(s, h, p->deps[i]->syms, |
| 540 | p->deps[i]->hashtab, p->deps[i]->strings); |
| 541 | if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 542 | return p->deps[i]->base + sym->st_value; |
| 543 | } |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | void *dlsym(void *p, const char *s) |
| 548 | { |
| 549 | void *res; |
| 550 | pthread_rwlock_rdlock(&lock); |
| 551 | res = do_dlsym(p, s); |
| 552 | pthread_rwlock_unlock(&lock); |
| 553 | return res; |
| 554 | } |
| 555 | |
| 556 | char *dlerror() |
| 557 | { |
| 558 | return "unknown error"; |
| 559 | } |
| 560 | |
| 561 | int dlclose(void *p) |
| 562 | { |
| 563 | return 0; |
| 564 | } |