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> |
| 15 | |
| 16 | #include "reloc.h" |
| 17 | |
| 18 | #if ULONG_MAX == 0xffffffff |
| 19 | typedef Elf32_Ehdr Ehdr; |
| 20 | typedef Elf32_Phdr Phdr; |
| 21 | typedef Elf32_Sym Sym; |
| 22 | #define R_TYPE(x) ((x)&255) |
| 23 | #define R_SYM(x) ((x)>>8) |
| 24 | #else |
| 25 | typedef Elf64_Ehdr Ehdr; |
| 26 | typedef Elf64_Phdr Phdr; |
| 27 | typedef Elf64_Sym Sym; |
| 28 | #define R_TYPE(x) ((x)&0xffffffff) |
| 29 | #define R_SYM(x) ((x)>>32) |
| 30 | #endif |
| 31 | |
| 32 | struct dso |
| 33 | { |
| 34 | struct dso *next, *prev; |
| 35 | int refcnt; |
| 36 | size_t *dynv; |
| 37 | Sym *syms; |
Rich Felker | 596d60c | 2011-06-18 22:52:01 -0400 | [diff] [blame] | 38 | uint32_t *hashtab; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 39 | char *strings; |
| 40 | unsigned char *base; |
| 41 | unsigned char *map; |
| 42 | size_t map_len; |
| 43 | dev_t dev; |
| 44 | ino_t ino; |
| 45 | int global; |
| 46 | int relocated; |
| 47 | char name[]; |
| 48 | }; |
| 49 | |
| 50 | static struct dso *head, *tail, *libc; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame^] | 51 | static char *env_path, *sys_path; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 52 | |
| 53 | #define AUX_CNT 15 |
| 54 | #define DYN_CNT 34 |
| 55 | |
| 56 | static void decode_vec(size_t *v, size_t *a, size_t cnt) |
| 57 | { |
| 58 | memset(a, 0, cnt*sizeof(size_t)); |
| 59 | for (; v[0]; v+=2) if (v[0]<cnt) { |
| 60 | a[0] |= 1ULL<<v[0]; |
| 61 | a[v[0]] = v[1]; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static uint32_t hash(const char *s) |
| 66 | { |
| 67 | uint_fast32_t h = 0; |
| 68 | while (*s) { |
| 69 | h = 16*h + *s++; |
| 70 | h ^= h>>24 & 0xf0; |
| 71 | } |
| 72 | return h & 0xfffffff; |
| 73 | } |
| 74 | |
Rich Felker | 596d60c | 2011-06-18 22:52:01 -0400 | [diff] [blame] | 75 | 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] | 76 | { |
| 77 | size_t i; |
| 78 | for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) { |
| 79 | if (!strcmp(s, strings+syms[i].st_name)) |
| 80 | return syms+i; |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON) |
| 86 | |
| 87 | static void *find_sym(struct dso *dso, const char *s, int need_def) |
| 88 | { |
| 89 | uint32_t h = hash(s); |
| 90 | for (; dso; dso=dso->next) { |
| 91 | Sym *sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings); |
| 92 | if (sym && (!need_def || sym->st_shndx) && sym->st_value |
| 93 | && (1<<(sym->st_info&0xf) & OK_TYPES)) |
| 94 | return dso->base + sym->st_value; |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso) |
| 100 | { |
| 101 | Sym *sym; |
| 102 | const char *name; |
| 103 | size_t sym_val, sym_size; |
| 104 | size_t *reloc_addr; |
| 105 | void *ctx; |
| 106 | int type; |
| 107 | int sym_index; |
| 108 | |
| 109 | for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) { |
| 110 | reloc_addr = (void *)(base + rel[0]); |
| 111 | type = R_TYPE(rel[1]); |
| 112 | sym_index = R_SYM(rel[1]); |
| 113 | if (sym_index) { |
| 114 | sym = syms + sym_index; |
| 115 | name = strings + sym->st_name; |
| 116 | ctx = IS_COPY(type) ? dso->next : dso; |
| 117 | sym_val = (size_t)find_sym(ctx, name, 1); |
| 118 | sym_size = sym->st_size; |
| 119 | } |
| 120 | do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp) |
| 125 | { |
| 126 | size_t buf[896/sizeof(size_t)]; |
| 127 | size_t phsize; |
| 128 | size_t addr_min=SIZE_MAX, addr_max=0, map_len; |
| 129 | size_t this_min, this_max; |
| 130 | off_t off_start; |
| 131 | Ehdr *eh; |
| 132 | Phdr *ph; |
| 133 | unsigned prot; |
| 134 | unsigned char *map, *base; |
| 135 | size_t dyn; |
| 136 | size_t i; |
| 137 | |
| 138 | ssize_t l = read(fd, buf, sizeof buf); |
| 139 | if (l<sizeof *eh) return 0; |
| 140 | eh = (void *)buf; |
| 141 | phsize = eh->e_phentsize * eh->e_phnum; |
| 142 | if (phsize + sizeof *eh > l) return 0; |
| 143 | if (eh->e_phoff + phsize > l) { |
| 144 | l = pread(fd, buf+sizeof *eh, phsize, eh->e_phoff); |
| 145 | if (l != phsize) return 0; |
| 146 | eh->e_phoff = sizeof *eh; |
| 147 | } |
| 148 | ph = (void *)((char *)buf + eh->e_phoff); |
| 149 | for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { |
| 150 | if (ph->p_type == PT_DYNAMIC) |
| 151 | dyn = ph->p_vaddr; |
| 152 | if (ph->p_type != PT_LOAD) continue; |
| 153 | if (ph->p_vaddr < addr_min) { |
| 154 | addr_min = ph->p_vaddr; |
| 155 | off_start = ph->p_offset; |
| 156 | prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | |
| 157 | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | |
| 158 | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); |
| 159 | } |
| 160 | if (ph->p_vaddr+ph->p_memsz > addr_max) { |
| 161 | addr_max = ph->p_vaddr+ph->p_memsz; |
| 162 | } |
| 163 | } |
| 164 | if (!dyn) return 0; |
| 165 | addr_max += PAGE_SIZE-1; |
| 166 | addr_max &= -PAGE_SIZE; |
| 167 | addr_min &= -PAGE_SIZE; |
| 168 | off_start &= -PAGE_SIZE; |
| 169 | map_len = addr_max - addr_min + off_start; |
| 170 | /* The first time, we map too much, possibly even more than |
| 171 | * the length of the file. This is okay because we will not |
| 172 | * use the invalid part; we just need to reserve the right |
| 173 | * amount of virtual address space to map over later. */ |
| 174 | map = mmap(0, map_len, prot, MAP_PRIVATE, fd, off_start); |
| 175 | if (map==MAP_FAILED) return 0; |
| 176 | base = map - addr_min; |
| 177 | ph = (void *)((char *)buf + eh->e_phoff); |
| 178 | for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { |
| 179 | if (ph->p_type != PT_LOAD) continue; |
| 180 | /* Reuse the existing mapping for the lowest-address LOAD */ |
| 181 | if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue; |
| 182 | this_min = ph->p_vaddr & -PAGE_SIZE; |
| 183 | this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE; |
| 184 | off_start = ph->p_offset & -PAGE_SIZE; |
| 185 | prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | |
| 186 | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | |
| 187 | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); |
| 188 | if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) { |
| 189 | munmap(map, map_len); |
| 190 | return 0; |
| 191 | } |
| 192 | if (ph->p_memsz > ph->p_filesz) { |
| 193 | size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz; |
| 194 | size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE; |
| 195 | memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1); |
| 196 | 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) { |
| 197 | munmap(map, map_len); |
| 198 | return 0; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | *lenp = map_len; |
| 203 | *basep = base; |
| 204 | *dynp = dyn; |
| 205 | return map; |
| 206 | } |
| 207 | |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame^] | 208 | static int path_open(const char *name, const char *search) |
| 209 | { |
| 210 | char buf[2*NAME_MAX+2]; |
| 211 | const char *s, *z; |
| 212 | int l, fd; |
| 213 | for (s=search; *s; s+=l+!!z) { |
| 214 | z = strchr(s, ':'); |
| 215 | l = z ? z-s : strlen(s); |
| 216 | snprintf(buf, sizeof buf, "%.*s/%s", l, s, name); |
| 217 | if ((fd = open(buf, O_RDONLY))>=0) return fd; |
| 218 | } |
| 219 | return -1; |
| 220 | } |
| 221 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 222 | static struct dso *load_library(const char *name) |
| 223 | { |
| 224 | unsigned char *base, *map; |
| 225 | size_t dyno, map_len; |
| 226 | struct dso *p; |
| 227 | size_t dyn[DYN_CNT] = {0}; |
| 228 | int fd; |
| 229 | struct stat st; |
| 230 | |
| 231 | /* Catch and block attempts to reload the implementation itself */ |
| 232 | if (name[0]=='l' && name[1]=='i' && name[2]=='b') { |
| 233 | static const char *rp, reserved[] = |
| 234 | "c\0pthread\0rt\0m\0dl\0util\0xnet\0"; |
| 235 | char *z = strchr(name, '.'); |
| 236 | if (z) { |
| 237 | size_t l = z-name; |
| 238 | for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1); |
| 239 | if (*rp) { |
| 240 | if (!libc->prev) { |
| 241 | tail->next = libc; |
| 242 | libc->prev = tail; |
| 243 | tail = libc; |
| 244 | } |
| 245 | return libc; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | /* Search for the name to see if it's already loaded */ |
| 250 | for (p=head->next; p; p=p->next) { |
| 251 | if (!strcmp(p->name, name)) { |
| 252 | p->refcnt++; |
| 253 | return p; |
| 254 | } |
| 255 | } |
| 256 | if (name[0] == '/') { |
| 257 | fd = open(name, O_RDONLY); |
| 258 | } else { |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 259 | if (strlen(name) > NAME_MAX || strchr(name, '/')) return 0; |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame^] | 260 | fd = -1; |
| 261 | if (env_path) fd = path_open(name, env_path); |
| 262 | if (fd < 0) { |
| 263 | if (!sys_path) { |
| 264 | FILE *f = fopen(ETC_LDSO_PATH, "r"); |
| 265 | if (f) { |
| 266 | if (getline(&sys_path, (size_t[1]){0}, f) > 0) |
| 267 | sys_path[strlen(sys_path)-1]=0; |
| 268 | fclose(f); |
| 269 | } |
| 270 | } |
| 271 | if (sys_path) fd = path_open(name, sys_path); |
| 272 | else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib"); |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | if (fd < 0) return 0; |
| 276 | if (fstat(fd, &st) < 0) { |
| 277 | close(fd); |
| 278 | return 0; |
| 279 | } |
| 280 | for (p=head->next; p; p=p->next) { |
| 281 | if (p->dev == st.st_dev && p->ino == st.st_ino) { |
| 282 | close(fd); |
| 283 | p->refcnt++; |
| 284 | return p; |
| 285 | } |
| 286 | } |
| 287 | map = map_library(fd, &map_len, &base, &dyno); |
| 288 | close(fd); |
| 289 | if (!map) return 0; |
| 290 | p = calloc(1, sizeof *p + strlen(name) + 1); |
| 291 | if (!p) { |
| 292 | munmap(map, map_len); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | p->map = map; |
| 297 | p->map_len = map_len; |
| 298 | p->base = base; |
| 299 | p->dynv = (void *)(base + dyno); |
| 300 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 301 | |
| 302 | p->syms = (void *)(base + dyn[DT_SYMTAB]); |
| 303 | p->hashtab = (void *)(base + dyn[DT_HASH]); |
| 304 | p->strings = (void *)(base + dyn[DT_STRTAB]); |
| 305 | p->dev = st.st_dev; |
| 306 | p->ino = st.st_ino; |
| 307 | p->global = 1; |
| 308 | p->refcnt = 1; |
| 309 | strcpy(p->name, name); |
| 310 | |
| 311 | tail->next = p; |
| 312 | p->prev = tail; |
| 313 | tail = p; |
| 314 | |
| 315 | return p; |
| 316 | } |
| 317 | |
| 318 | static void load_deps(struct dso *p) |
| 319 | { |
| 320 | size_t i; |
| 321 | for (; p; p=p->next) { |
| 322 | for (i=0; p->dynv[i]; i+=2) { |
| 323 | if (p->dynv[i] != DT_NEEDED) continue; |
| 324 | load_library(p->strings + p->dynv[i+1]); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | static void reloc_all(struct dso *p) |
| 330 | { |
| 331 | size_t dyn[DYN_CNT] = {0}; |
| 332 | for (; p; p=p->next) { |
| 333 | if (p->relocated) continue; |
| 334 | decode_vec(p->dynv, dyn, DYN_CNT); |
| 335 | do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ], |
| 336 | 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, p); |
| 337 | do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], |
| 338 | 2, p->syms, p->strings, p); |
| 339 | do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], |
| 340 | 3, p->syms, p->strings, p); |
Rich Felker | 368ba4a | 2011-06-25 00:18:19 -0400 | [diff] [blame] | 341 | p->relocated = 1; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 345 | static void free_all(struct dso *p) |
| 346 | { |
| 347 | struct dso *n; |
| 348 | while (p) { |
| 349 | n = p->next; |
| 350 | if (p->map) free(p); |
| 351 | p = n; |
| 352 | } |
| 353 | } |
| 354 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 355 | void *__dynlink(int argc, char **argv, size_t *got) |
| 356 | { |
| 357 | size_t *auxv, aux[AUX_CNT] = {0}; |
| 358 | size_t app_dyn[DYN_CNT] = {0}; |
| 359 | size_t lib_dyn[DYN_CNT] = {0}; |
| 360 | size_t i; |
| 361 | Phdr *phdr; |
| 362 | struct dso lib, app; |
| 363 | |
| 364 | /* Find aux vector just past environ[] */ |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame^] | 365 | for (i=argc+1; argv[i]; i++) |
| 366 | if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16)) |
| 367 | env_path = argv[i]+16; |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 368 | auxv = (void *)(argv+i+1); |
| 369 | |
| 370 | decode_vec(auxv, aux, AUX_CNT); |
| 371 | |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame^] | 372 | /* Only trust user/env if kernel says we're not suid/sgid */ |
| 373 | if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID] |
| 374 | || aux[AT_GID]!=aux[AT_EGID]) { |
| 375 | env_path = 0; |
| 376 | } |
| 377 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 378 | /* Relocate ldso's DYNAMIC pointer and load vector */ |
| 379 | decode_vec((void *)(got[0] += aux[AT_BASE]), lib_dyn, DYN_CNT); |
| 380 | |
| 381 | /* Find the program image's DYNAMIC section and decode it */ |
| 382 | phdr = (void *)aux[AT_PHDR]; |
| 383 | for (i=aux[AT_PHNUM]; i--; phdr=(void *)((char *)phdr + aux[AT_PHENT])) { |
| 384 | if (phdr->p_type == PT_DYNAMIC) { |
| 385 | decode_vec((void *)phdr->p_vaddr, app_dyn, DYN_CNT); |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | app = (struct dso){ |
| 391 | .base = 0, |
| 392 | .strings = (void *)(app_dyn[DT_STRTAB]), |
| 393 | .hashtab = (void *)(app_dyn[DT_HASH]), |
| 394 | .syms = (void *)(app_dyn[DT_SYMTAB]), |
| 395 | .dynv = (void *)(phdr->p_vaddr), |
| 396 | .next = &lib |
| 397 | }; |
| 398 | |
| 399 | lib = (struct dso){ |
| 400 | .base = (void *)aux[AT_BASE], |
| 401 | .strings = (void *)(aux[AT_BASE]+lib_dyn[DT_STRTAB]), |
| 402 | .hashtab = (void *)(aux[AT_BASE]+lib_dyn[DT_HASH]), |
| 403 | .syms = (void *)(aux[AT_BASE]+lib_dyn[DT_SYMTAB]), |
| 404 | .dynv = (void *)(got[0]), |
| 405 | .relocated = 1 |
| 406 | }; |
| 407 | |
| 408 | /* Relocate the dynamic linker/libc */ |
| 409 | do_relocs((void *)aux[AT_BASE], (void *)(aux[AT_BASE]+lib_dyn[DT_REL]), |
| 410 | lib_dyn[DT_RELSZ], 2, lib.syms, lib.strings, &app); |
| 411 | do_relocs((void *)aux[AT_BASE], (void *)(aux[AT_BASE]+lib_dyn[DT_RELA]), |
| 412 | lib_dyn[DT_RELASZ], 3, lib.syms, lib.strings, &app); |
| 413 | |
| 414 | /* At this point the standard library is fully functional */ |
| 415 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 416 | head = tail = &app; |
| 417 | libc = &lib; |
| 418 | app.next = 0; |
| 419 | load_deps(head); |
| 420 | |
| 421 | reloc_all(head); |
| 422 | |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 423 | free_all(head); |
Rich Felker | 568b807 | 2011-06-25 01:56:34 -0400 | [diff] [blame^] | 424 | free(sys_path); |
Rich Felker | e8dbf00 | 2011-06-25 00:47:28 -0400 | [diff] [blame] | 425 | |
Rich Felker | 51e2d83 | 2011-06-18 19:48:42 -0400 | [diff] [blame] | 426 | errno = 0; |
| 427 | return (void *)aux[AT_ENTRY]; |
| 428 | } |