1 /* $NetBSD: map_object.c,v 1.52 2013/08/03 13:17:05 skrll Exp $ */ 2 3 /* 4 * Copyright 1996 John D. Polstra. 5 * Copyright 1996 Matt Thomas <matt@3am-software.com> 6 * Copyright 2002 Charles M. Hannum <root@ihack.net> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by John Polstra. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 __RCSID("$NetBSD: map_object.c,v 1.52 2013/08/03 13:17:05 skrll Exp $"); 38 #endif /* not lint */ 39 40 #include <errno.h> 41 #include <stddef.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include <sys/stat.h> 46 #include <sys/types.h> 47 #include <sys/mman.h> 48 49 #include "debug.h" 50 #include "rtld.h" 51 52 static int protflags(int); /* Elf flags -> mmap protection */ 53 54 #define EA_UNDEF (~(Elf_Addr)0) 55 56 /* 57 * Map a shared object into memory. The argument is a file descriptor, 58 * which must be open on the object and positioned at its beginning. 59 * 60 * The return value is a pointer to a newly-allocated Obj_Entry structure 61 * for the shared object. Returns NULL on failure. 62 */ 63 Obj_Entry * 64 _rtld_map_object(const char *path, int fd, const struct stat *sb) 65 { 66 Obj_Entry *obj; 67 Elf_Ehdr *ehdr; 68 Elf_Phdr *phdr; 69 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 70 Elf_Phdr *phtls; 71 #endif 72 size_t phsize; 73 Elf_Phdr *phlimit; 74 Elf_Phdr *segs[2]; 75 int nsegs; 76 caddr_t mapbase = MAP_FAILED; 77 size_t mapsize = 0; 78 int mapflags; 79 Elf_Off base_offset; 80 #ifdef MAP_ALIGNED 81 Elf_Addr base_alignment; 82 #endif 83 Elf_Addr base_vaddr; 84 Elf_Addr base_vlimit; 85 Elf_Addr text_vlimit; 86 int text_flags; 87 caddr_t base_addr; 88 Elf_Off data_offset; 89 Elf_Addr data_vaddr; 90 Elf_Addr data_vlimit; 91 int data_flags; 92 caddr_t data_addr; 93 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 94 Elf_Addr tls_vaddr = 0; /* Noise GCC */ 95 #endif 96 Elf_Addr phdr_vaddr; 97 size_t phdr_memsz; 98 caddr_t gap_addr; 99 size_t gap_size; 100 int i; 101 #ifdef RTLD_LOADER 102 Elf_Addr clear_vaddr; 103 caddr_t clear_addr; 104 size_t nclear; 105 #endif 106 107 if (sb != NULL && sb->st_size < (off_t)sizeof (Elf_Ehdr)) { 108 _rtld_error("%s: not ELF file (too short)", path); 109 return NULL; 110 } 111 112 obj = _rtld_obj_new(); 113 obj->path = xstrdup(path); 114 obj->pathlen = strlen(path); 115 if (sb != NULL) { 116 obj->dev = sb->st_dev; 117 obj->ino = sb->st_ino; 118 } 119 120 ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd, 121 (off_t)0); 122 obj->ehdr = ehdr; 123 if (ehdr == MAP_FAILED) { 124 _rtld_error("%s: read error: %s", path, xstrerror(errno)); 125 goto bad; 126 } 127 /* Make sure the file is valid */ 128 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0) { 129 _rtld_error("%s: not ELF file (magic number bad)", path); 130 goto bad; 131 } 132 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) { 133 _rtld_error("%s: invalid ELF class %x; expected %x", path, 134 ehdr->e_ident[EI_CLASS], ELFCLASS); 135 goto bad; 136 } 137 /* Elf_e_ident includes class */ 138 if (ehdr->e_ident[EI_VERSION] != EV_CURRENT || 139 ehdr->e_version != EV_CURRENT || 140 ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) { 141 _rtld_error("%s: unsupported file version", path); 142 goto bad; 143 } 144 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) { 145 _rtld_error("%s: unsupported file type", path); 146 goto bad; 147 } 148 switch (ehdr->e_machine) { 149 ELFDEFNNAME(MACHDEP_ID_CASES) 150 default: 151 _rtld_error("%s: unsupported machine", path); 152 goto bad; 153 } 154 155 /* 156 * We rely on the program header being in the first page. This is 157 * not strictly required by the ABI specification, but it seems to 158 * always true in practice. And, it simplifies things considerably. 159 */ 160 assert(ehdr->e_phentsize == sizeof(Elf_Phdr)); 161 assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <= 162 _rtld_pagesz); 163 164 /* 165 * Scan the program header entries, and save key information. 166 * 167 * We rely on there being exactly two load segments, text and data, 168 * in that order. 169 */ 170 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff); 171 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 172 phtls = NULL; 173 #endif 174 phsize = ehdr->e_phnum * sizeof(phdr[0]); 175 obj->phdr = NULL; 176 phdr_vaddr = EA_UNDEF; 177 phdr_memsz = 0; 178 phlimit = phdr + ehdr->e_phnum; 179 nsegs = 0; 180 while (phdr < phlimit) { 181 switch (phdr->p_type) { 182 case PT_INTERP: 183 obj->interp = (void *)(uintptr_t)phdr->p_vaddr; 184 dbg(("%s: PT_INTERP %p", obj->path, obj->interp)); 185 break; 186 187 case PT_LOAD: 188 if (nsegs < 2) 189 segs[nsegs] = phdr; 190 ++nsegs; 191 192 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_LOAD", 193 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz)); 194 break; 195 196 case PT_PHDR: 197 phdr_vaddr = phdr->p_vaddr; 198 phdr_memsz = phdr->p_memsz; 199 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_PHDR", 200 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz)); 201 break; 202 203 case PT_DYNAMIC: 204 obj->dynamic = (void *)(uintptr_t)phdr->p_vaddr; 205 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_DYNAMIC", 206 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz)); 207 break; 208 209 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 210 case PT_TLS: 211 phtls = phdr; 212 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_TLS", 213 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz)); 214 break; 215 #endif 216 #ifdef __ARM_EABI__ 217 case PT_ARM_EXIDX: 218 obj->exidx_start = (void *)(uintptr_t)phdr->p_vaddr; 219 obj->exidx_sz = phdr->p_memsz; 220 break; 221 #endif 222 } 223 224 ++phdr; 225 } 226 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff); 227 obj->entry = (void *)(uintptr_t)ehdr->e_entry; 228 if (!obj->dynamic) { 229 _rtld_error("%s: not dynamically linked", path); 230 goto bad; 231 } 232 if (nsegs != 2) { 233 _rtld_error("%s: wrong number of segments (%d != 2)", path, 234 nsegs); 235 goto bad; 236 } 237 238 /* 239 * Map the entire address space of the object as a file 240 * region to stake out our contiguous region and establish a 241 * base for relocation. We use a file mapping so that 242 * the kernel will give us whatever alignment is appropriate 243 * for the platform we're running on. 244 * 245 * We map it using the text protection, map the data segment 246 * into the right place, then map an anon segment for the bss 247 * and unmap the gaps left by padding to alignment. 248 */ 249 250 #ifdef MAP_ALIGNED 251 base_alignment = segs[0]->p_align; 252 #endif 253 base_offset = round_down(segs[0]->p_offset); 254 base_vaddr = round_down(segs[0]->p_vaddr); 255 base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz); 256 text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz); 257 text_flags = protflags(segs[0]->p_flags); 258 data_offset = round_down(segs[1]->p_offset); 259 data_vaddr = round_down(segs[1]->p_vaddr); 260 data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz); 261 data_flags = protflags(segs[1]->p_flags); 262 #ifdef RTLD_LOADER 263 clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz; 264 #endif 265 266 obj->textsize = text_vlimit - base_vaddr; 267 obj->vaddrbase = base_vaddr; 268 obj->isdynamic = ehdr->e_type == ET_DYN; 269 270 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 271 if (phtls != NULL) { 272 ++_rtld_tls_dtv_generation; 273 obj->tlsindex = ++_rtld_tls_max_index; 274 obj->tlssize = phtls->p_memsz; 275 obj->tlsalign = phtls->p_align; 276 obj->tlsinitsize = phtls->p_filesz; 277 tls_vaddr = phtls->p_vaddr; 278 } 279 #endif 280 281 obj->phdr_loaded = false; 282 for (i = 0; i < nsegs; i++) { 283 if (phdr_vaddr != EA_UNDEF && 284 segs[i]->p_vaddr <= phdr_vaddr && 285 segs[i]->p_memsz >= phdr_memsz) { 286 obj->phdr_loaded = true; 287 break; 288 } 289 if (segs[i]->p_offset <= ehdr->e_phoff && 290 segs[i]->p_memsz >= phsize) { 291 phdr_vaddr = segs[i]->p_vaddr + ehdr->e_phoff; 292 phdr_memsz = phsize; 293 obj->phdr_loaded = true; 294 break; 295 } 296 } 297 if (obj->phdr_loaded) { 298 obj->phdr = (void *)(uintptr_t)phdr_vaddr; 299 obj->phsize = phdr_memsz; 300 } else { 301 Elf_Phdr *buf; 302 buf = xmalloc(phsize); 303 if (buf == NULL) { 304 _rtld_error("%s: cannot allocate program header", path); 305 goto bad; 306 } 307 memcpy(buf, phdr, phsize); 308 obj->phdr = buf; 309 obj->phsize = phsize; 310 } 311 dbg(("%s: phdr %p phsize %zu (%s)", obj->path, obj->phdr, obj->phsize, 312 obj->phdr_loaded ? "loaded" : "allocated")); 313 314 /* Unmap header if it overlaps the first load section. */ 315 if (base_offset < _rtld_pagesz) { 316 munmap(ehdr, _rtld_pagesz); 317 obj->ehdr = MAP_FAILED; 318 } 319 320 /* 321 * Calculate log2 of the base section alignment. 322 */ 323 mapflags = 0; 324 #ifdef MAP_ALIGNED 325 if (base_alignment > _rtld_pagesz) { 326 unsigned int log2 = 0; 327 for (; base_alignment > 1; base_alignment >>= 1) 328 log2++; 329 mapflags = MAP_ALIGNED(log2); 330 } 331 #endif 332 333 #ifdef RTLD_LOADER 334 base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr; 335 #else 336 base_addr = NULL; 337 #endif 338 mapsize = base_vlimit - base_vaddr; 339 mapbase = mmap(base_addr, mapsize, text_flags, 340 mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset); 341 if (mapbase == MAP_FAILED) { 342 _rtld_error("mmap of entire address space failed: %s", 343 xstrerror(errno)); 344 goto bad; 345 } 346 347 /* Overlay the data segment onto the proper region. */ 348 data_addr = mapbase + (data_vaddr - base_vaddr); 349 if (mmap(data_addr, data_vlimit - data_vaddr, data_flags, 350 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) == 351 MAP_FAILED) { 352 _rtld_error("mmap of data failed: %s", xstrerror(errno)); 353 goto bad; 354 } 355 356 /* Overlay the bss segment onto the proper region. */ 357 if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit, 358 data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) == 359 MAP_FAILED) { 360 _rtld_error("mmap of bss failed: %s", xstrerror(errno)); 361 goto bad; 362 } 363 364 /* Unmap the gap between the text and data. */ 365 gap_addr = mapbase + round_up(text_vlimit - base_vaddr); 366 gap_size = data_addr - gap_addr; 367 if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) { 368 _rtld_error("mprotect of text -> data gap failed: %s", 369 xstrerror(errno)); 370 goto bad; 371 } 372 373 #ifdef RTLD_LOADER 374 /* Clear any BSS in the last page of the data segment. */ 375 clear_addr = mapbase + (clear_vaddr - base_vaddr); 376 if ((nclear = data_vlimit - clear_vaddr) > 0) 377 memset(clear_addr, 0, nclear); 378 379 /* Non-file portion of BSS mapped above. */ 380 #endif 381 382 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 383 if (phtls != NULL) 384 obj->tlsinit = mapbase + tls_vaddr; 385 #endif 386 387 obj->mapbase = mapbase; 388 obj->mapsize = mapsize; 389 obj->relocbase = mapbase - base_vaddr; 390 391 if (obj->dynamic) 392 obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->dynamic); 393 if (obj->entry) 394 obj->entry = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->entry); 395 if (obj->interp) 396 obj->interp = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->interp); 397 if (obj->phdr_loaded) 398 obj->phdr = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->phdr); 399 #ifdef __ARM_EABI__ 400 if (obj->exidx_start) 401 obj->exidx_start = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->exidx_start); 402 #endif 403 404 return obj; 405 406 bad: 407 if (obj->ehdr != MAP_FAILED) 408 munmap(obj->ehdr, _rtld_pagesz); 409 if (mapbase != MAP_FAILED) 410 munmap(mapbase, mapsize); 411 _rtld_obj_free(obj); 412 return NULL; 413 } 414 415 void 416 _rtld_obj_free(Obj_Entry *obj) 417 { 418 Objlist_Entry *elm; 419 Name_Entry *entry; 420 421 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 422 if (obj->tls_done) 423 _rtld_tls_offset_free(obj); 424 #endif 425 xfree(obj->path); 426 while (obj->needed != NULL) { 427 Needed_Entry *needed = obj->needed; 428 obj->needed = needed->next; 429 xfree(needed); 430 } 431 while ((entry = SIMPLEQ_FIRST(&obj->names)) != NULL) { 432 SIMPLEQ_REMOVE_HEAD(&obj->names, link); 433 xfree(entry); 434 } 435 while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) { 436 SIMPLEQ_REMOVE_HEAD(&obj->dldags, link); 437 xfree(elm); 438 } 439 while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) { 440 SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link); 441 xfree(elm); 442 } 443 if (!obj->phdr_loaded) 444 xfree((void *)(uintptr_t)obj->phdr); 445 xfree(obj); 446 #ifdef COMBRELOC 447 _rtld_combreloc_reset(obj); 448 #endif 449 } 450 451 Obj_Entry * 452 _rtld_obj_new(void) 453 { 454 Obj_Entry *obj; 455 456 obj = CNEW(Obj_Entry); 457 SIMPLEQ_INIT(&obj->names); 458 SIMPLEQ_INIT(&obj->dldags); 459 SIMPLEQ_INIT(&obj->dagmembers); 460 return obj; 461 } 462 463 /* 464 * Given a set of ELF protection flags, return the corresponding protection 465 * flags for MMAP. 466 */ 467 static int 468 protflags(int elfflags) 469 { 470 int prot = 0; 471 472 if (elfflags & PF_R) 473 prot |= PROT_READ; 474 #ifdef RTLD_LOADER 475 if (elfflags & PF_W) 476 prot |= PROT_WRITE; 477 #endif 478 if (elfflags & PF_X) 479 prot |= PROT_EXEC; 480 return prot; 481 } 482