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