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 *); 42 static int convert_prot(int); /* Elf flags -> mmap protection */ 43 static int convert_flags(int); /* Elf flags -> mmap flags */ 44 45 /* 46 * Map a shared object into memory. The "fd" argument is a file descriptor, 47 * which must be open on the object and positioned at its beginning. 48 * The "path" argument is a pathname that is used only for error messages. 49 * 50 * The return value is a pointer to a newly-allocated Obj_Entry structure 51 * for the shared object. Returns NULL on failure. 52 */ 53 Obj_Entry * 54 map_object(int fd, const char *path, const struct stat *sb) 55 { 56 Obj_Entry *obj; 57 Elf_Ehdr *hdr; 58 int i; 59 Elf_Phdr *phdr; 60 Elf_Phdr *phlimit; 61 Elf_Phdr **segs; 62 int nsegs; 63 Elf_Phdr *phdyn; 64 Elf_Phdr *phinterp; 65 Elf_Phdr *phtls; 66 caddr_t mapbase; 67 size_t mapsize; 68 Elf_Off base_offset; 69 Elf_Addr base_vaddr; 70 Elf_Addr base_vlimit; 71 caddr_t base_addr; 72 Elf_Off data_offset; 73 Elf_Addr data_vaddr; 74 Elf_Addr data_vlimit; 75 caddr_t data_addr; 76 int data_prot; 77 int data_flags; 78 Elf_Addr clear_vaddr; 79 caddr_t clear_addr; 80 caddr_t clear_page; 81 Elf_Addr phdr_vaddr; 82 size_t nclear, phsize; 83 Elf_Addr bss_vaddr; 84 Elf_Addr bss_vlimit; 85 caddr_t bss_addr; 86 Elf_Word stack_flags; 87 88 hdr = get_elf_header(fd, path); 89 if (hdr == NULL) 90 return (NULL); 91 92 /* 93 * Scan the program header entries, and save key information. 94 * 95 * We expect that the loadable segments are ordered by load address. 96 */ 97 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff); 98 phsize = hdr->e_phnum * sizeof (phdr[0]); 99 phlimit = phdr + hdr->e_phnum; 100 nsegs = -1; 101 phdyn = phinterp = phtls = NULL; 102 phdr_vaddr = 0; 103 segs = alloca(sizeof(segs[0]) * hdr->e_phnum); 104 stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W; 105 while (phdr < phlimit) { 106 switch (phdr->p_type) { 107 108 case PT_INTERP: 109 phinterp = phdr; 110 break; 111 112 case PT_LOAD: 113 segs[++nsegs] = phdr; 114 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) { 115 _rtld_error("%s: PT_LOAD segment %d not page-aligned", 116 path, nsegs); 117 return NULL; 118 } 119 break; 120 121 case PT_PHDR: 122 phdr_vaddr = phdr->p_vaddr; 123 phsize = phdr->p_memsz; 124 break; 125 126 case PT_DYNAMIC: 127 phdyn = phdr; 128 break; 129 130 case PT_TLS: 131 phtls = phdr; 132 break; 133 134 case PT_GNU_STACK: 135 stack_flags = phdr->p_flags; 136 break; 137 } 138 139 ++phdr; 140 } 141 if (phdyn == NULL) { 142 _rtld_error("%s: object is not dynamically-linked", path); 143 return NULL; 144 } 145 146 if (nsegs < 0) { 147 _rtld_error("%s: too few PT_LOAD segments", path); 148 return NULL; 149 } 150 151 /* 152 * Map the entire address space of the object, to stake out our 153 * contiguous region, and to establish the base address for relocation. 154 */ 155 base_offset = trunc_page(segs[0]->p_offset); 156 base_vaddr = trunc_page(segs[0]->p_vaddr); 157 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz); 158 mapsize = base_vlimit - base_vaddr; 159 base_addr = hdr->e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL; 160 161 mapbase = mmap(base_addr, mapsize, PROT_NONE, MAP_ANON | MAP_PRIVATE | 162 MAP_NOCORE, -1, 0); 163 if (mapbase == (caddr_t) -1) { 164 _rtld_error("%s: mmap of entire address space failed: %s", 165 path, strerror(errno)); 166 return NULL; 167 } 168 if (base_addr != NULL && mapbase != base_addr) { 169 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p", 170 path, base_addr, mapbase); 171 munmap(mapbase, mapsize); 172 return NULL; 173 } 174 175 for (i = 0; i <= nsegs; i++) { 176 /* Overlay the segment onto the proper region. */ 177 data_offset = trunc_page(segs[i]->p_offset); 178 data_vaddr = trunc_page(segs[i]->p_vaddr); 179 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz); 180 data_addr = mapbase + (data_vaddr - base_vaddr); 181 data_prot = convert_prot(segs[i]->p_flags); 182 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED; 183 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot, 184 data_flags, fd, data_offset) == (caddr_t) -1) { 185 _rtld_error("%s: mmap of data failed: %s", path, strerror(errno)); 186 return NULL; 187 } 188 189 /* Do BSS setup */ 190 if (segs[i]->p_filesz != segs[i]->p_memsz) { 191 192 /* Clear any BSS in the last page of the segment. */ 193 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz; 194 clear_addr = mapbase + (clear_vaddr - base_vaddr); 195 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr); 196 197 if ((nclear = data_vlimit - clear_vaddr) > 0) { 198 /* Make sure the end of the segment is writable */ 199 if ((data_prot & PROT_WRITE) == 0 && -1 == 200 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) { 201 _rtld_error("%s: mprotect failed: %s", path, 202 strerror(errno)); 203 return NULL; 204 } 205 206 memset(clear_addr, 0, nclear); 207 208 /* Reset the data protection back */ 209 if ((data_prot & PROT_WRITE) == 0) 210 mprotect(clear_page, PAGE_SIZE, data_prot); 211 } 212 213 /* Overlay the BSS segment onto the proper region. */ 214 bss_vaddr = data_vlimit; 215 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz); 216 bss_addr = mapbase + (bss_vaddr - base_vaddr); 217 if (bss_vlimit > bss_vaddr) { /* There is something to do */ 218 if (mprotect(bss_addr, bss_vlimit - bss_vaddr, data_prot) == -1) { 219 _rtld_error("%s: mprotect of bss failed: %s", path, 220 strerror(errno)); 221 return NULL; 222 } 223 } 224 } 225 226 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff && 227 (data_vlimit - data_vaddr + data_offset) >= 228 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) { 229 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset; 230 } 231 } 232 233 obj = obj_new(); 234 if (sb != NULL) { 235 obj->dev = sb->st_dev; 236 obj->ino = sb->st_ino; 237 } 238 obj->mapbase = mapbase; 239 obj->mapsize = mapsize; 240 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) - 241 base_vaddr; 242 obj->vaddrbase = base_vaddr; 243 obj->relocbase = mapbase - base_vaddr; 244 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr); 245 if (hdr->e_entry != 0) 246 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry); 247 if (phdr_vaddr != 0) { 248 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr); 249 } else { 250 obj->phdr = malloc(phsize); 251 if (obj->phdr == NULL) { 252 obj_free(obj); 253 _rtld_error("%s: cannot allocate program header", path); 254 return NULL; 255 } 256 memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize); 257 obj->phdr_alloc = true; 258 } 259 obj->phsize = phsize; 260 if (phinterp != NULL) 261 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr); 262 if (phtls != NULL) { 263 tls_dtv_generation++; 264 obj->tlsindex = ++tls_max_index; 265 obj->tlssize = phtls->p_memsz; 266 obj->tlsalign = phtls->p_align; 267 obj->tlsinitsize = phtls->p_filesz; 268 obj->tlsinit = mapbase + phtls->p_vaddr; 269 } 270 obj->stack_flags = stack_flags; 271 return obj; 272 } 273 274 static Elf_Ehdr * 275 get_elf_header (int fd, const char *path) 276 { 277 static union { 278 Elf_Ehdr hdr; 279 char buf[PAGE_SIZE]; 280 } u; 281 ssize_t nbytes; 282 283 if ((nbytes = pread(fd, u.buf, PAGE_SIZE, 0)) == -1) { 284 _rtld_error("%s: read error: %s", path, strerror(errno)); 285 return NULL; 286 } 287 288 /* Make sure the file is valid */ 289 if (nbytes < (ssize_t)sizeof(Elf_Ehdr) || !IS_ELF(u.hdr)) { 290 _rtld_error("%s: invalid file format", path); 291 return NULL; 292 } 293 if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS 294 || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) { 295 _rtld_error("%s: unsupported file layout", path); 296 return NULL; 297 } 298 if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT 299 || u.hdr.e_version != EV_CURRENT) { 300 _rtld_error("%s: unsupported file version", path); 301 return NULL; 302 } 303 if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) { 304 _rtld_error("%s: unsupported file type", path); 305 return NULL; 306 } 307 if (u.hdr.e_machine != ELF_TARG_MACH) { 308 _rtld_error("%s: unsupported machine", path); 309 return NULL; 310 } 311 312 /* 313 * We rely on the program header being in the first page. This is 314 * not strictly required by the ABI specification, but it seems to 315 * always true in practice. And, it simplifies things considerably. 316 */ 317 if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) { 318 _rtld_error( 319 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path); 320 return NULL; 321 } 322 if (u.hdr.e_phoff + u.hdr.e_phnum * sizeof(Elf_Phdr) > (size_t)nbytes) { 323 _rtld_error("%s: program header too large", path); 324 return NULL; 325 } 326 327 return (&u.hdr); 328 } 329 330 void 331 obj_free(Obj_Entry *obj) 332 { 333 Objlist_Entry *elm; 334 335 if (obj->tls_done) 336 free_tls_offset(obj); 337 while (obj->needed != NULL) { 338 Needed_Entry *needed = obj->needed; 339 obj->needed = needed->next; 340 free(needed); 341 } 342 while (!STAILQ_EMPTY(&obj->names)) { 343 Name_Entry *entry = STAILQ_FIRST(&obj->names); 344 STAILQ_REMOVE_HEAD(&obj->names, link); 345 free(entry); 346 } 347 while (!STAILQ_EMPTY(&obj->dldags)) { 348 elm = STAILQ_FIRST(&obj->dldags); 349 STAILQ_REMOVE_HEAD(&obj->dldags, link); 350 free(elm); 351 } 352 while (!STAILQ_EMPTY(&obj->dagmembers)) { 353 elm = STAILQ_FIRST(&obj->dagmembers); 354 STAILQ_REMOVE_HEAD(&obj->dagmembers, link); 355 free(elm); 356 } 357 if (obj->vertab) 358 free(obj->vertab); 359 if (obj->origin_path) 360 free(obj->origin_path); 361 if (obj->z_origin) 362 free(obj->rpath); 363 if (obj->priv) 364 free(obj->priv); 365 if (obj->path) 366 free(obj->path); 367 if (obj->phdr_alloc) 368 free((void *)obj->phdr); 369 free(obj); 370 } 371 372 Obj_Entry * 373 obj_new(void) 374 { 375 Obj_Entry *obj; 376 377 obj = CNEW(Obj_Entry); 378 STAILQ_INIT(&obj->dldags); 379 STAILQ_INIT(&obj->dagmembers); 380 STAILQ_INIT(&obj->names); 381 return obj; 382 } 383 384 /* 385 * Given a set of ELF protection flags, return the corresponding protection 386 * flags for MMAP. 387 */ 388 static int 389 convert_prot(int elfflags) 390 { 391 int prot = 0; 392 if (elfflags & PF_R) 393 prot |= PROT_READ; 394 if (elfflags & PF_W) 395 prot |= PROT_WRITE; 396 if (elfflags & PF_X) 397 prot |= PROT_EXEC; 398 return prot; 399 } 400 401 static int 402 convert_flags(int elfflags) 403 { 404 int flags = MAP_PRIVATE; /* All mappings are private */ 405 406 /* 407 * Readonly mappings are marked "MAP_NOCORE", because they can be 408 * reconstructed by a debugger. 409 */ 410 if (!(elfflags & PF_W)) 411 flags |= MAP_NOCORE; 412 return flags; 413 } 414