1 /* $NetBSD: map_object.c,v 1.43 2011/08/13 22:25:20 christos 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.43 2011/08/13 22:25:20 christos 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: unrecognized file format1", 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 ehdr->e_ident[EI_CLASS] != ELFCLASS) { 130 _rtld_error("%s: unrecognized file format2 [%x != %x]", path, 131 ehdr->e_ident[EI_CLASS], ELFCLASS); 132 goto bad; 133 } 134 /* Elf_e_ident includes class */ 135 if (ehdr->e_ident[EI_VERSION] != EV_CURRENT || 136 ehdr->e_version != EV_CURRENT || 137 ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) { 138 _rtld_error("%s: unsupported file version", path); 139 goto bad; 140 } 141 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) { 142 _rtld_error("%s: unsupported file type", path); 143 goto bad; 144 } 145 switch (ehdr->e_machine) { 146 ELFDEFNNAME(MACHDEP_ID_CASES) 147 default: 148 _rtld_error("%s: unsupported machine", path); 149 goto bad; 150 } 151 152 /* 153 * We rely on the program header being in the first page. This is 154 * not strictly required by the ABI specification, but it seems to 155 * always true in practice. And, it simplifies things considerably. 156 */ 157 assert(ehdr->e_phentsize == sizeof(Elf_Phdr)); 158 assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <= 159 _rtld_pagesz); 160 161 /* 162 * Scan the program header entries, and save key information. 163 * 164 * We rely on there being exactly two load segments, text and data, 165 * in that order. 166 */ 167 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff); 168 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 169 phtls = NULL; 170 #endif 171 phsize = ehdr->e_phnum * sizeof(phdr[0]); 172 obj->phdr = NULL; 173 phdr_vaddr = EA_UNDEF; 174 phdr_memsz = 0; 175 phlimit = phdr + ehdr->e_phnum; 176 nsegs = 0; 177 while (phdr < phlimit) { 178 switch (phdr->p_type) { 179 case PT_INTERP: 180 obj->interp = (void *)(uintptr_t)phdr->p_vaddr; 181 dbg(("%s: PT_INTERP %p", obj->path, obj->interp)); 182 break; 183 184 case PT_LOAD: 185 if (nsegs < 2) 186 segs[nsegs] = phdr; 187 ++nsegs; 188 dbg(("%s: %s %p phsize %zu", obj->path, "PT_LOAD", 189 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz)); 190 break; 191 192 case PT_PHDR: 193 phdr_vaddr = phdr->p_vaddr; 194 phdr_memsz = phdr->p_memsz; 195 dbg(("%s: %s %p phsize %zu", obj->path, "PT_PHDR", 196 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz)); 197 break; 198 199 case PT_DYNAMIC: 200 obj->dynamic = (void *)(uintptr_t)phdr->p_vaddr; 201 dbg(("%s: %s %p phsize %zu", obj->path, "PT_DYNAMIC", 202 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz)); 203 break; 204 205 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 206 case PT_TLS: 207 phtls = phdr; 208 dbg(("%s: %s %p phsize %zu", obj->path, "PT_TLS", 209 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz)); 210 break; 211 #endif 212 } 213 214 ++phdr; 215 } 216 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff); 217 obj->entry = (void *)(uintptr_t)ehdr->e_entry; 218 if (!obj->dynamic) { 219 _rtld_error("%s: not dynamically linked", path); 220 goto bad; 221 } 222 if (nsegs != 2) { 223 _rtld_error("%s: wrong number of segments (%d != 2)", path, 224 nsegs); 225 goto bad; 226 } 227 228 /* 229 * Map the entire address space of the object as a file 230 * region to stake out our contiguous region and establish a 231 * base for relocation. We use a file mapping so that 232 * the kernel will give us whatever alignment is appropriate 233 * for the platform we're running on. 234 * 235 * We map it using the text protection, map the data segment 236 * into the right place, then map an anon segment for the bss 237 * and unmap the gaps left by padding to alignment. 238 */ 239 240 #ifdef MAP_ALIGNED 241 base_alignment = segs[0]->p_align; 242 #endif 243 base_offset = round_down(segs[0]->p_offset); 244 base_vaddr = round_down(segs[0]->p_vaddr); 245 base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz); 246 text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz); 247 text_flags = protflags(segs[0]->p_flags); 248 data_offset = round_down(segs[1]->p_offset); 249 data_vaddr = round_down(segs[1]->p_vaddr); 250 data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz); 251 data_flags = protflags(segs[1]->p_flags); 252 #ifdef RTLD_LOADER 253 clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz; 254 #endif 255 256 obj->textsize = text_vlimit - base_vaddr; 257 obj->vaddrbase = base_vaddr; 258 obj->isdynamic = ehdr->e_type == ET_DYN; 259 260 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 261 if (phtls != NULL) { 262 ++_rtld_tls_dtv_generation; 263 obj->tlsindex = ++_rtld_tls_max_index; 264 obj->tlssize = phtls->p_memsz; 265 obj->tlsalign = phtls->p_align; 266 obj->tlsinitsize = phtls->p_filesz; 267 tls_vaddr = phtls->p_vaddr; 268 } 269 #endif 270 271 obj->phdr_loaded = false; 272 for (i = 0; i < nsegs; i++) { 273 if (phdr_vaddr != EA_UNDEF && 274 segs[i]->p_vaddr <= phdr_vaddr && 275 segs[i]->p_memsz >= phdr_memsz) { 276 obj->phdr_loaded = true; 277 break; 278 } 279 if (segs[i]->p_offset <= ehdr->e_phoff && 280 segs[i]->p_memsz >= phsize) { 281 phdr_vaddr = segs[i]->p_vaddr + ehdr->e_phoff; 282 phdr_memsz = phsize; 283 obj->phdr_loaded = true; 284 break; 285 } 286 } 287 if (obj->phdr_loaded) { 288 obj->phdr = (void *)(uintptr_t)phdr_vaddr; 289 obj->phsize = phdr_memsz; 290 } else { 291 Elf_Phdr *buf; 292 buf = xmalloc(phsize); 293 if (buf == NULL) { 294 _rtld_error("%s: cannot allocate program header", path); 295 goto bad; 296 } 297 memcpy(buf, phdr, phsize); 298 obj->phdr = buf; 299 obj->phsize = phsize; 300 } 301 dbg(("%s: phdr %p phsize %zu (%s)", obj->path, obj->phdr, obj->phsize, 302 obj->phdr_loaded ? "loaded" : "allocated")); 303 304 /* Unmap header if it overlaps the first load section. */ 305 if (base_offset < _rtld_pagesz) { 306 munmap(ehdr, _rtld_pagesz); 307 obj->ehdr = MAP_FAILED; 308 } 309 310 /* 311 * Calculate log2 of the base section alignment. 312 */ 313 mapflags = 0; 314 #ifdef MAP_ALIGNED 315 if (base_alignment > _rtld_pagesz) { 316 unsigned int log2 = 0; 317 for (; base_alignment > 1; base_alignment >>= 1) 318 log2++; 319 mapflags = MAP_ALIGNED(log2); 320 } 321 #endif 322 323 #ifdef RTLD_LOADER 324 base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr; 325 #else 326 base_addr = NULL; 327 #endif 328 mapsize = base_vlimit - base_vaddr; 329 mapbase = mmap(base_addr, mapsize, text_flags, 330 mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset); 331 if (mapbase == MAP_FAILED) { 332 _rtld_error("mmap of entire address space failed: %s", 333 xstrerror(errno)); 334 goto bad; 335 } 336 337 /* Overlay the data segment onto the proper region. */ 338 data_addr = mapbase + (data_vaddr - base_vaddr); 339 if (mmap(data_addr, data_vlimit - data_vaddr, data_flags, 340 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) == 341 MAP_FAILED) { 342 _rtld_error("mmap of data failed: %s", xstrerror(errno)); 343 goto bad; 344 } 345 346 /* Overlay the bss segment onto the proper region. */ 347 if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit, 348 data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) == 349 MAP_FAILED) { 350 _rtld_error("mmap of bss failed: %s", xstrerror(errno)); 351 goto bad; 352 } 353 354 /* Unmap the gap between the text and data. */ 355 gap_addr = mapbase + round_up(text_vlimit - base_vaddr); 356 gap_size = data_addr - gap_addr; 357 if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) { 358 _rtld_error("mprotect of text -> data gap failed: %s", 359 xstrerror(errno)); 360 goto bad; 361 } 362 363 #ifdef RTLD_LOADER 364 /* Clear any BSS in the last page of the data segment. */ 365 clear_addr = mapbase + (clear_vaddr - base_vaddr); 366 if ((nclear = data_vlimit - clear_vaddr) > 0) 367 memset(clear_addr, 0, nclear); 368 369 /* Non-file portion of BSS mapped above. */ 370 #endif 371 372 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 373 if (phtls != NULL) 374 obj->tlsinit = mapbase + tls_vaddr; 375 #endif 376 377 obj->mapbase = mapbase; 378 obj->mapsize = mapsize; 379 obj->relocbase = mapbase - base_vaddr; 380 381 if (obj->dynamic) 382 obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->dynamic); 383 if (obj->entry) 384 obj->entry = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->entry); 385 if (obj->interp) 386 obj->interp = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->interp); 387 if (obj->phdr_loaded) 388 obj->phdr = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->phdr); 389 390 return obj; 391 392 bad: 393 if (obj->ehdr != MAP_FAILED) 394 munmap(obj->ehdr, _rtld_pagesz); 395 if (mapbase != MAP_FAILED) 396 munmap(mapbase, mapsize); 397 _rtld_obj_free(obj); 398 return NULL; 399 } 400 401 void 402 _rtld_obj_free(Obj_Entry *obj) 403 { 404 Objlist_Entry *elm; 405 406 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 407 if (obj->tls_done) 408 _rtld_tls_offset_free(obj); 409 #endif 410 xfree(obj->path); 411 while (obj->needed != NULL) { 412 Needed_Entry *needed = obj->needed; 413 obj->needed = needed->next; 414 xfree(needed); 415 } 416 while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) { 417 SIMPLEQ_REMOVE_HEAD(&obj->dldags, link); 418 xfree(elm); 419 } 420 while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) { 421 SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link); 422 xfree(elm); 423 } 424 if (!obj->phdr_loaded) 425 xfree((void *)(uintptr_t)obj->phdr); 426 xfree(obj); 427 #ifdef COMBRELOC 428 _rtld_combreloc_reset(obj); 429 #endif 430 } 431 432 Obj_Entry * 433 _rtld_obj_new(void) 434 { 435 Obj_Entry *obj; 436 437 obj = CNEW(Obj_Entry); 438 SIMPLEQ_INIT(&obj->dldags); 439 SIMPLEQ_INIT(&obj->dagmembers); 440 return obj; 441 } 442 443 /* 444 * Given a set of ELF protection flags, return the corresponding protection 445 * flags for MMAP. 446 */ 447 static int 448 protflags(int elfflags) 449 { 450 int prot = 0; 451 452 if (elfflags & PF_R) 453 prot |= PROT_READ; 454 #ifdef RTLD_LOADER 455 if (elfflags & PF_W) 456 prot |= PROT_WRITE; 457 #endif 458 if (elfflags & PF_X) 459 prot |= PROT_EXEC; 460 return prot; 461 } 462