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