1 /*- 2 * Copyright 1996-1998 John D. Polstra. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28 #include <sys/param.h> 29 #include <sys/mman.h> 30 #include <sys/stat.h> 31 32 #include <errno.h> 33 #include <stddef.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 38 #include "debug.h" 39 #include "rtld.h" 40 41 static Elf_Ehdr *get_elf_header(int, const char *, const struct stat *); 42 static int convert_flags(int); /* Elf flags -> mmap flags */ 43 44 /* 45 * Map a shared object into memory. The "fd" argument is a file descriptor, 46 * which must be open on the object and positioned at its beginning. 47 * The "path" argument is a pathname that is used only for error messages. 48 * 49 * The return value is a pointer to a newly-allocated Obj_Entry structure 50 * for the shared object. Returns NULL on failure. 51 */ 52 Obj_Entry * 53 map_object(int fd, const char *path, const struct stat *sb) 54 { 55 Obj_Entry *obj; 56 Elf_Ehdr *hdr; 57 int i; 58 Elf_Phdr *phdr; 59 Elf_Phdr *phlimit; 60 Elf_Phdr **segs; 61 int nsegs; 62 Elf_Phdr *phdyn; 63 Elf_Phdr *phinterp; 64 Elf_Phdr *phtls; 65 caddr_t mapbase; 66 size_t mapsize; 67 Elf_Addr base_vaddr; 68 Elf_Addr base_vlimit; 69 caddr_t base_addr; 70 int base_flags; 71 Elf_Off data_offset; 72 Elf_Addr data_vaddr; 73 Elf_Addr data_vlimit; 74 caddr_t data_addr; 75 int data_prot; 76 int data_flags; 77 Elf_Addr clear_vaddr; 78 caddr_t clear_addr; 79 caddr_t clear_page; 80 Elf_Addr phdr_vaddr; 81 size_t nclear, phsize; 82 Elf_Addr bss_vaddr; 83 Elf_Addr bss_vlimit; 84 caddr_t bss_addr; 85 Elf_Word stack_flags; 86 Elf_Addr relro_page; 87 size_t relro_size; 88 Elf_Addr note_start; 89 Elf_Addr note_end; 90 char *note_map; 91 size_t note_map_len; 92 93 hdr = get_elf_header(fd, path, sb); 94 if (hdr == NULL) 95 return (NULL); 96 97 /* 98 * Scan the program header entries, and save key information. 99 * 100 * We expect that the loadable segments are ordered by load address. 101 */ 102 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff); 103 phsize = hdr->e_phnum * sizeof (phdr[0]); 104 phlimit = phdr + hdr->e_phnum; 105 nsegs = -1; 106 phdyn = phinterp = phtls = NULL; 107 phdr_vaddr = 0; 108 relro_page = 0; 109 relro_size = 0; 110 note_start = 0; 111 note_end = 0; 112 note_map = NULL; 113 segs = alloca(sizeof(segs[0]) * hdr->e_phnum); 114 stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W; 115 while (phdr < phlimit) { 116 switch (phdr->p_type) { 117 118 case PT_INTERP: 119 phinterp = phdr; 120 break; 121 122 case PT_LOAD: 123 segs[++nsegs] = phdr; 124 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) { 125 _rtld_error("%s: PT_LOAD segment %d not page-aligned", 126 path, nsegs); 127 goto error; 128 } 129 break; 130 131 case PT_PHDR: 132 phdr_vaddr = phdr->p_vaddr; 133 phsize = phdr->p_memsz; 134 break; 135 136 case PT_DYNAMIC: 137 phdyn = phdr; 138 break; 139 140 case PT_TLS: 141 phtls = phdr; 142 break; 143 144 case PT_GNU_STACK: 145 stack_flags = phdr->p_flags; 146 break; 147 148 case PT_GNU_RELRO: 149 relro_page = phdr->p_vaddr; 150 relro_size = phdr->p_memsz; 151 break; 152 153 case PT_NOTE: 154 if (phdr->p_offset > PAGE_SIZE || 155 phdr->p_offset + phdr->p_filesz > PAGE_SIZE) { 156 note_map_len = round_page(phdr->p_offset + 157 phdr->p_filesz) - trunc_page(phdr->p_offset); 158 note_map = mmap(NULL, note_map_len, PROT_READ, 159 MAP_PRIVATE, fd, trunc_page(phdr->p_offset)); 160 if (note_map == MAP_FAILED) { 161 _rtld_error("%s: error mapping PT_NOTE (%d)", path, errno); 162 goto error; 163 } 164 note_start = (Elf_Addr)(note_map + phdr->p_offset - 165 trunc_page(phdr->p_offset)); 166 } else { 167 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset; 168 } 169 note_end = note_start + phdr->p_filesz; 170 break; 171 } 172 173 ++phdr; 174 } 175 if (phdyn == NULL) { 176 _rtld_error("%s: object is not dynamically-linked", path); 177 goto error; 178 } 179 180 if (nsegs < 0) { 181 _rtld_error("%s: too few PT_LOAD segments", path); 182 goto error; 183 } 184 185 /* 186 * Map the entire address space of the object, to stake out our 187 * contiguous region, and to establish the base address for relocation. 188 */ 189 base_vaddr = trunc_page(segs[0]->p_vaddr); 190 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz); 191 mapsize = base_vlimit - base_vaddr; 192 base_addr = (caddr_t) base_vaddr; 193 base_flags = MAP_PRIVATE | MAP_ANON | MAP_NOCORE; 194 if (npagesizes > 1 && round_page(segs[0]->p_filesz) >= pagesizes[1]) 195 base_flags |= MAP_ALIGNED_SUPER; 196 if (base_vaddr != 0) 197 base_flags |= MAP_FIXED | MAP_EXCL; 198 199 mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0); 200 if (mapbase == (caddr_t) -1) { 201 _rtld_error("%s: mmap of entire address space failed: %s", 202 path, rtld_strerror(errno)); 203 goto error; 204 } 205 if (base_addr != NULL && mapbase != base_addr) { 206 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p", 207 path, base_addr, mapbase); 208 goto error1; 209 } 210 211 for (i = 0; i <= nsegs; i++) { 212 /* Overlay the segment onto the proper region. */ 213 data_offset = trunc_page(segs[i]->p_offset); 214 data_vaddr = trunc_page(segs[i]->p_vaddr); 215 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz); 216 data_addr = mapbase + (data_vaddr - base_vaddr); 217 data_prot = convert_prot(segs[i]->p_flags); 218 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED; 219 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot, 220 data_flags | MAP_PREFAULT_READ, fd, data_offset) == (caddr_t) -1) { 221 _rtld_error("%s: mmap of data failed: %s", path, 222 rtld_strerror(errno)); 223 goto error1; 224 } 225 226 /* Do BSS setup */ 227 if (segs[i]->p_filesz != segs[i]->p_memsz) { 228 229 /* Clear any BSS in the last page of the segment. */ 230 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz; 231 clear_addr = mapbase + (clear_vaddr - base_vaddr); 232 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr); 233 234 if ((nclear = data_vlimit - clear_vaddr) > 0) { 235 /* Make sure the end of the segment is writable */ 236 if ((data_prot & PROT_WRITE) == 0 && -1 == 237 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) { 238 _rtld_error("%s: mprotect failed: %s", path, 239 rtld_strerror(errno)); 240 goto error1; 241 } 242 243 memset(clear_addr, 0, nclear); 244 245 /* Reset the data protection back */ 246 if ((data_prot & PROT_WRITE) == 0) 247 mprotect(clear_page, PAGE_SIZE, data_prot); 248 } 249 250 /* Overlay the BSS segment onto the proper region. */ 251 bss_vaddr = data_vlimit; 252 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz); 253 bss_addr = mapbase + (bss_vaddr - base_vaddr); 254 if (bss_vlimit > bss_vaddr) { /* There is something to do */ 255 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot, 256 data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) { 257 _rtld_error("%s: mmap of bss failed: %s", path, 258 rtld_strerror(errno)); 259 goto error1; 260 } 261 } 262 } 263 264 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff && 265 (data_vlimit - data_vaddr + data_offset) >= 266 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) { 267 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset; 268 } 269 } 270 271 obj = obj_new(); 272 if (sb != NULL) { 273 obj->dev = sb->st_dev; 274 obj->ino = sb->st_ino; 275 } 276 obj->mapbase = mapbase; 277 obj->mapsize = mapsize; 278 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) - 279 base_vaddr; 280 obj->vaddrbase = base_vaddr; 281 obj->relocbase = mapbase - base_vaddr; 282 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr); 283 if (hdr->e_entry != 0) 284 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry); 285 if (phdr_vaddr != 0) { 286 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr); 287 } else { 288 obj->phdr = malloc(phsize); 289 if (obj->phdr == NULL) { 290 obj_free(obj); 291 _rtld_error("%s: cannot allocate program header", path); 292 goto error1; 293 } 294 memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize); 295 obj->phdr_alloc = true; 296 } 297 obj->phsize = phsize; 298 if (phinterp != NULL) 299 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr); 300 if (phtls != NULL) { 301 tls_dtv_generation++; 302 obj->tlsindex = ++tls_max_index; 303 obj->tlssize = phtls->p_memsz; 304 obj->tlsalign = phtls->p_align; 305 obj->tlsinitsize = phtls->p_filesz; 306 obj->tlsinit = mapbase + phtls->p_vaddr; 307 } 308 obj->stack_flags = stack_flags; 309 obj->relro_page = obj->relocbase + trunc_page(relro_page); 310 obj->relro_size = round_page(relro_size); 311 if (note_start < note_end) 312 digest_notes(obj, note_start, note_end); 313 if (note_map != NULL) 314 munmap(note_map, note_map_len); 315 munmap(hdr, PAGE_SIZE); 316 return (obj); 317 318 error1: 319 munmap(mapbase, mapsize); 320 error: 321 if (note_map != NULL && note_map != MAP_FAILED) 322 munmap(note_map, note_map_len); 323 munmap(hdr, PAGE_SIZE); 324 return (NULL); 325 } 326 327 static Elf_Ehdr * 328 get_elf_header(int fd, const char *path, const struct stat *sbp) 329 { 330 Elf_Ehdr *hdr; 331 332 /* Make sure file has enough data for the ELF header */ 333 if (sbp != NULL && sbp->st_size < sizeof(Elf_Ehdr)) { 334 _rtld_error("%s: invalid file format", path); 335 return (NULL); 336 } 337 338 hdr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ, 339 fd, 0); 340 if (hdr == (Elf_Ehdr *)MAP_FAILED) { 341 _rtld_error("%s: read error: %s", path, rtld_strerror(errno)); 342 return (NULL); 343 } 344 345 /* Make sure the file is valid */ 346 if (!IS_ELF(*hdr)) { 347 _rtld_error("%s: invalid file format", path); 348 goto error; 349 } 350 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 351 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) { 352 _rtld_error("%s: unsupported file layout", path); 353 goto error; 354 } 355 if (hdr->e_ident[EI_VERSION] != EV_CURRENT || 356 hdr->e_version != EV_CURRENT) { 357 _rtld_error("%s: unsupported file version", path); 358 goto error; 359 } 360 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) { 361 _rtld_error("%s: unsupported file type", path); 362 goto error; 363 } 364 if (hdr->e_machine != ELF_TARG_MACH) { 365 _rtld_error("%s: unsupported machine", path); 366 goto error; 367 } 368 369 /* 370 * We rely on the program header being in the first page. This is 371 * not strictly required by the ABI specification, but it seems to 372 * always true in practice. And, it simplifies things considerably. 373 */ 374 if (hdr->e_phentsize != sizeof(Elf_Phdr)) { 375 _rtld_error( 376 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path); 377 goto error; 378 } 379 if (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) > 380 (size_t)PAGE_SIZE) { 381 _rtld_error("%s: program header too large", path); 382 goto error; 383 } 384 return (hdr); 385 386 error: 387 munmap(hdr, PAGE_SIZE); 388 return (NULL); 389 } 390 391 void 392 obj_free(Obj_Entry *obj) 393 { 394 Objlist_Entry *elm; 395 396 if (obj->tls_done) 397 free_tls_offset(obj); 398 while (obj->needed != NULL) { 399 Needed_Entry *needed = obj->needed; 400 obj->needed = needed->next; 401 free(needed); 402 } 403 while (!STAILQ_EMPTY(&obj->names)) { 404 Name_Entry *entry = STAILQ_FIRST(&obj->names); 405 STAILQ_REMOVE_HEAD(&obj->names, link); 406 free(entry); 407 } 408 while (!STAILQ_EMPTY(&obj->dldags)) { 409 elm = STAILQ_FIRST(&obj->dldags); 410 STAILQ_REMOVE_HEAD(&obj->dldags, link); 411 free(elm); 412 } 413 while (!STAILQ_EMPTY(&obj->dagmembers)) { 414 elm = STAILQ_FIRST(&obj->dagmembers); 415 STAILQ_REMOVE_HEAD(&obj->dagmembers, link); 416 free(elm); 417 } 418 if (obj->vertab) 419 free(obj->vertab); 420 if (obj->origin_path) 421 free(obj->origin_path); 422 if (obj->z_origin) 423 free(obj->rpath); 424 if (obj->priv) 425 free(obj->priv); 426 if (obj->path) 427 free(obj->path); 428 if (obj->phdr_alloc) 429 free((void *)obj->phdr); 430 free(obj); 431 } 432 433 Obj_Entry * 434 obj_new(void) 435 { 436 Obj_Entry *obj; 437 438 obj = CNEW(Obj_Entry); 439 STAILQ_INIT(&obj->dldags); 440 STAILQ_INIT(&obj->dagmembers); 441 STAILQ_INIT(&obj->names); 442 return obj; 443 } 444 445 /* 446 * Given a set of ELF protection flags, return the corresponding protection 447 * flags for MMAP. 448 */ 449 int 450 convert_prot(int elfflags) 451 { 452 int prot = 0; 453 if (elfflags & PF_R) 454 prot |= PROT_READ; 455 if (elfflags & PF_W) 456 prot |= PROT_WRITE; 457 if (elfflags & PF_X) 458 prot |= PROT_EXEC; 459 return prot; 460 } 461 462 static int 463 convert_flags(int elfflags) 464 { 465 int flags = MAP_PRIVATE; /* All mappings are private */ 466 467 /* 468 * Readonly mappings are marked "MAP_NOCORE", because they can be 469 * reconstructed by a debugger. 470 */ 471 if (!(elfflags & PF_W)) 472 flags |= MAP_NOCORE; 473 return flags; 474 } 475