13124c3e0SJohn Polstra /*- 23124c3e0SJohn Polstra * Copyright 1996-1998 John D. Polstra. 33124c3e0SJohn Polstra * All rights reserved. 43124c3e0SJohn Polstra * 53124c3e0SJohn Polstra * Redistribution and use in source and binary forms, with or without 63124c3e0SJohn Polstra * modification, are permitted provided that the following conditions 73124c3e0SJohn Polstra * are met: 83124c3e0SJohn Polstra * 1. Redistributions of source code must retain the above copyright 93124c3e0SJohn Polstra * notice, this list of conditions and the following disclaimer. 103124c3e0SJohn Polstra * 2. Redistributions in binary form must reproduce the above copyright 113124c3e0SJohn Polstra * notice, this list of conditions and the following disclaimer in the 123124c3e0SJohn Polstra * documentation and/or other materials provided with the distribution. 133124c3e0SJohn Polstra * 143124c3e0SJohn Polstra * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 153124c3e0SJohn Polstra * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 163124c3e0SJohn Polstra * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 173124c3e0SJohn Polstra * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 183124c3e0SJohn Polstra * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 193124c3e0SJohn Polstra * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 203124c3e0SJohn Polstra * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 213124c3e0SJohn Polstra * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 223124c3e0SJohn Polstra * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 233124c3e0SJohn Polstra * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 243124c3e0SJohn Polstra * 257f3dea24SPeter Wemm * $FreeBSD$ 263124c3e0SJohn Polstra */ 273124c3e0SJohn Polstra 283124c3e0SJohn Polstra #include <sys/param.h> 293124c3e0SJohn Polstra #include <sys/mman.h> 307360ae0fSJohn Polstra #include <sys/stat.h> 313124c3e0SJohn Polstra 323124c3e0SJohn Polstra #include <errno.h> 333124c3e0SJohn Polstra #include <stddef.h> 34926ea445SJohn Polstra #include <stdlib.h> 353124c3e0SJohn Polstra #include <string.h> 363124c3e0SJohn Polstra #include <unistd.h> 373124c3e0SJohn Polstra 38b5393d9fSDoug Rabson #include "debug.h" 393124c3e0SJohn Polstra #include "rtld.h" 403124c3e0SJohn Polstra 413124c3e0SJohn Polstra static int protflags(int); /* Elf flags -> mmap protection */ 423124c3e0SJohn Polstra 433124c3e0SJohn Polstra /* 44bfb1ef60SJohn Polstra * Map a shared object into memory. The "fd" argument is a file descriptor, 453124c3e0SJohn Polstra * which must be open on the object and positioned at its beginning. 46bfb1ef60SJohn Polstra * The "path" argument is a pathname that is used only for error messages. 473124c3e0SJohn Polstra * 483124c3e0SJohn Polstra * The return value is a pointer to a newly-allocated Obj_Entry structure 493124c3e0SJohn Polstra * for the shared object. Returns NULL on failure. 503124c3e0SJohn Polstra */ 513124c3e0SJohn Polstra Obj_Entry * 527360ae0fSJohn Polstra map_object(int fd, const char *path, const struct stat *sb) 533124c3e0SJohn Polstra { 543124c3e0SJohn Polstra Obj_Entry *obj; 553124c3e0SJohn Polstra union { 5613575fc4SDoug Rabson Elf_Ehdr hdr; 573124c3e0SJohn Polstra char buf[PAGE_SIZE]; 583124c3e0SJohn Polstra } u; 593124c3e0SJohn Polstra int nbytes; 6013575fc4SDoug Rabson Elf_Phdr *phdr; 6113575fc4SDoug Rabson Elf_Phdr *phlimit; 6213575fc4SDoug Rabson Elf_Phdr *segs[2]; 633124c3e0SJohn Polstra int nsegs; 6413575fc4SDoug Rabson Elf_Phdr *phdyn; 6513575fc4SDoug Rabson Elf_Phdr *phphdr; 66a607e5d7SJohn Polstra Elf_Phdr *phinterp; 673124c3e0SJohn Polstra caddr_t mapbase; 683124c3e0SJohn Polstra size_t mapsize; 6913575fc4SDoug Rabson Elf_Off base_offset; 7013575fc4SDoug Rabson Elf_Addr base_vaddr; 7113575fc4SDoug Rabson Elf_Addr base_vlimit; 723124c3e0SJohn Polstra caddr_t base_addr; 7313575fc4SDoug Rabson Elf_Off data_offset; 7413575fc4SDoug Rabson Elf_Addr data_vaddr; 7513575fc4SDoug Rabson Elf_Addr data_vlimit; 763124c3e0SJohn Polstra caddr_t data_addr; 7713575fc4SDoug Rabson Elf_Addr clear_vaddr; 783124c3e0SJohn Polstra caddr_t clear_addr; 793124c3e0SJohn Polstra size_t nclear; 8013575fc4SDoug Rabson Elf_Addr bss_vaddr; 8113575fc4SDoug Rabson Elf_Addr bss_vlimit; 823124c3e0SJohn Polstra caddr_t bss_addr; 833124c3e0SJohn Polstra 843124c3e0SJohn Polstra if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) { 85bfb1ef60SJohn Polstra _rtld_error("%s: read error: %s", path, strerror(errno)); 863124c3e0SJohn Polstra return NULL; 873124c3e0SJohn Polstra } 883124c3e0SJohn Polstra 893124c3e0SJohn Polstra /* Make sure the file is valid */ 9013575fc4SDoug Rabson if (nbytes < sizeof(Elf_Ehdr) 913124c3e0SJohn Polstra || u.hdr.e_ident[EI_MAG0] != ELFMAG0 923124c3e0SJohn Polstra || u.hdr.e_ident[EI_MAG1] != ELFMAG1 933124c3e0SJohn Polstra || u.hdr.e_ident[EI_MAG2] != ELFMAG2 943124c3e0SJohn Polstra || u.hdr.e_ident[EI_MAG3] != ELFMAG3) { 95bfb1ef60SJohn Polstra _rtld_error("%s: invalid file format", path); 963124c3e0SJohn Polstra return NULL; 973124c3e0SJohn Polstra } 9813575fc4SDoug Rabson if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS 9913575fc4SDoug Rabson || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) { 100bfb1ef60SJohn Polstra _rtld_error("%s: unsupported file layout", path); 1013124c3e0SJohn Polstra return NULL; 1023124c3e0SJohn Polstra } 1033124c3e0SJohn Polstra if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT 1043124c3e0SJohn Polstra || u.hdr.e_version != EV_CURRENT) { 105bfb1ef60SJohn Polstra _rtld_error("%s: unsupported file version", path); 1063124c3e0SJohn Polstra return NULL; 1073124c3e0SJohn Polstra } 1083124c3e0SJohn Polstra if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) { 109bfb1ef60SJohn Polstra _rtld_error("%s: unsupported file type", path); 1103124c3e0SJohn Polstra return NULL; 1113124c3e0SJohn Polstra } 11213575fc4SDoug Rabson if (u.hdr.e_machine != ELF_TARG_MACH) { 113bfb1ef60SJohn Polstra _rtld_error("%s: unsupported machine", path); 1143124c3e0SJohn Polstra return NULL; 1153124c3e0SJohn Polstra } 1163124c3e0SJohn Polstra 1173124c3e0SJohn Polstra /* 1183124c3e0SJohn Polstra * We rely on the program header being in the first page. This is 1193124c3e0SJohn Polstra * not strictly required by the ABI specification, but it seems to 1203124c3e0SJohn Polstra * always true in practice. And, it simplifies things considerably. 1213124c3e0SJohn Polstra */ 122bfb1ef60SJohn Polstra if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) { 123bfb1ef60SJohn Polstra _rtld_error( 124bfb1ef60SJohn Polstra "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path); 125bfb1ef60SJohn Polstra return NULL; 126bfb1ef60SJohn Polstra } 127bfb1ef60SJohn Polstra if (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) > nbytes) { 128bfb1ef60SJohn Polstra _rtld_error("%s: program header too large", path); 129bfb1ef60SJohn Polstra return NULL; 130bfb1ef60SJohn Polstra } 1313124c3e0SJohn Polstra 1323124c3e0SJohn Polstra /* 1333124c3e0SJohn Polstra * Scan the program header entries, and save key information. 1343124c3e0SJohn Polstra * 1353124c3e0SJohn Polstra * We rely on there being exactly two load segments, text and data, 1363124c3e0SJohn Polstra * in that order. 1373124c3e0SJohn Polstra */ 13813575fc4SDoug Rabson phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff); 1393124c3e0SJohn Polstra phlimit = phdr + u.hdr.e_phnum; 1403124c3e0SJohn Polstra nsegs = 0; 141a607e5d7SJohn Polstra phdyn = phphdr = phinterp = NULL; 1423124c3e0SJohn Polstra while (phdr < phlimit) { 1433124c3e0SJohn Polstra switch (phdr->p_type) { 1443124c3e0SJohn Polstra 145a607e5d7SJohn Polstra case PT_INTERP: 146a607e5d7SJohn Polstra phinterp = phdr; 147a607e5d7SJohn Polstra break; 148a607e5d7SJohn Polstra 1493124c3e0SJohn Polstra case PT_LOAD: 150bfb1ef60SJohn Polstra if (nsegs >= 2) { 151bfb1ef60SJohn Polstra _rtld_error("%s: too many PT_LOAD segments", path); 152bfb1ef60SJohn Polstra return NULL; 153bfb1ef60SJohn Polstra } 1543124c3e0SJohn Polstra segs[nsegs] = phdr; 1553124c3e0SJohn Polstra ++nsegs; 1563124c3e0SJohn Polstra break; 1573124c3e0SJohn Polstra 1583124c3e0SJohn Polstra case PT_PHDR: 1593124c3e0SJohn Polstra phphdr = phdr; 1603124c3e0SJohn Polstra break; 1613124c3e0SJohn Polstra 1623124c3e0SJohn Polstra case PT_DYNAMIC: 1633124c3e0SJohn Polstra phdyn = phdr; 1643124c3e0SJohn Polstra break; 1653124c3e0SJohn Polstra } 1663124c3e0SJohn Polstra 1673124c3e0SJohn Polstra ++phdr; 1683124c3e0SJohn Polstra } 1693124c3e0SJohn Polstra if (phdyn == NULL) { 170bfb1ef60SJohn Polstra _rtld_error("%s: object is not dynamically-linked", path); 1713124c3e0SJohn Polstra return NULL; 1723124c3e0SJohn Polstra } 1733124c3e0SJohn Polstra 174bfb1ef60SJohn Polstra if (nsegs < 2) { 175bfb1ef60SJohn Polstra _rtld_error("%s: too few PT_LOAD segments", path); 176bfb1ef60SJohn Polstra return NULL; 177bfb1ef60SJohn Polstra } 178bfb1ef60SJohn Polstra if (segs[0]->p_align < PAGE_SIZE || segs[1]->p_align < PAGE_SIZE) { 179bfb1ef60SJohn Polstra _rtld_error("%s: PT_LOAD segments not page-aligned", path); 180bfb1ef60SJohn Polstra return NULL; 181bfb1ef60SJohn Polstra } 1823124c3e0SJohn Polstra 1833124c3e0SJohn Polstra /* 1843124c3e0SJohn Polstra * Map the entire address space of the object, to stake out our 1853124c3e0SJohn Polstra * contiguous region, and to establish the base address for relocation. 1863124c3e0SJohn Polstra */ 1873124c3e0SJohn Polstra base_offset = trunc_page(segs[0]->p_offset); 1883124c3e0SJohn Polstra base_vaddr = trunc_page(segs[0]->p_vaddr); 1893124c3e0SJohn Polstra base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz); 1903124c3e0SJohn Polstra mapsize = base_vlimit - base_vaddr; 1913124c3e0SJohn Polstra base_addr = u.hdr.e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL; 1923124c3e0SJohn Polstra 1933124c3e0SJohn Polstra mapbase = mmap(base_addr, mapsize, protflags(segs[0]->p_flags), 1943124c3e0SJohn Polstra MAP_PRIVATE, fd, base_offset); 1953124c3e0SJohn Polstra if (mapbase == (caddr_t) -1) { 196bfb1ef60SJohn Polstra _rtld_error("%s: mmap of entire address space failed: %s", 197bfb1ef60SJohn Polstra path, strerror(errno)); 1983124c3e0SJohn Polstra return NULL; 1993124c3e0SJohn Polstra } 2003124c3e0SJohn Polstra if (base_addr != NULL && mapbase != base_addr) { 201bfb1ef60SJohn Polstra _rtld_error("%s: mmap returned wrong address: wanted %p, got %p", 202bfb1ef60SJohn Polstra path, base_addr, mapbase); 2033124c3e0SJohn Polstra munmap(mapbase, mapsize); 2043124c3e0SJohn Polstra return NULL; 2053124c3e0SJohn Polstra } 2063124c3e0SJohn Polstra 2073124c3e0SJohn Polstra /* Overlay the data segment onto the proper region. */ 2083124c3e0SJohn Polstra data_offset = trunc_page(segs[1]->p_offset); 2093124c3e0SJohn Polstra data_vaddr = trunc_page(segs[1]->p_vaddr); 2103124c3e0SJohn Polstra data_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_filesz); 2113124c3e0SJohn Polstra data_addr = mapbase + (data_vaddr - base_vaddr); 2123124c3e0SJohn Polstra if (mmap(data_addr, data_vlimit - data_vaddr, protflags(segs[1]->p_flags), 2133124c3e0SJohn Polstra MAP_PRIVATE|MAP_FIXED, fd, data_offset) == (caddr_t) -1) { 214bfb1ef60SJohn Polstra _rtld_error("%s: mmap of data failed: %s", path, strerror(errno)); 2153124c3e0SJohn Polstra return NULL; 2163124c3e0SJohn Polstra } 2173124c3e0SJohn Polstra 2183124c3e0SJohn Polstra /* Clear any BSS in the last page of the data segment. */ 2193124c3e0SJohn Polstra clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz; 2203124c3e0SJohn Polstra clear_addr = mapbase + (clear_vaddr - base_vaddr); 2213124c3e0SJohn Polstra if ((nclear = data_vlimit - clear_vaddr) > 0) 2223124c3e0SJohn Polstra memset(clear_addr, 0, nclear); 2233124c3e0SJohn Polstra 2243124c3e0SJohn Polstra /* Overlay the BSS segment onto the proper region. */ 2253124c3e0SJohn Polstra bss_vaddr = data_vlimit; 2263124c3e0SJohn Polstra bss_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz); 2273124c3e0SJohn Polstra bss_addr = mapbase + (bss_vaddr - base_vaddr); 2283124c3e0SJohn Polstra if (bss_vlimit > bss_vaddr) { /* There is something to do */ 2293124c3e0SJohn Polstra if (mmap(bss_addr, bss_vlimit - bss_vaddr, protflags(segs[1]->p_flags), 2303124c3e0SJohn Polstra MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) { 231bfb1ef60SJohn Polstra _rtld_error("%s: mmap of bss failed: %s", path, strerror(errno)); 2323124c3e0SJohn Polstra return NULL; 2333124c3e0SJohn Polstra } 2343124c3e0SJohn Polstra } 2353124c3e0SJohn Polstra 236926ea445SJohn Polstra obj = obj_new(); 2377360ae0fSJohn Polstra if (sb != NULL) { 2387360ae0fSJohn Polstra obj->dev = sb->st_dev; 2397360ae0fSJohn Polstra obj->ino = sb->st_ino; 2407360ae0fSJohn Polstra } 2413124c3e0SJohn Polstra obj->mapbase = mapbase; 2423124c3e0SJohn Polstra obj->mapsize = mapsize; 2433124c3e0SJohn Polstra obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) - 2443124c3e0SJohn Polstra base_vaddr; 2453124c3e0SJohn Polstra obj->vaddrbase = base_vaddr; 2463124c3e0SJohn Polstra obj->relocbase = mapbase - base_vaddr; 247a607e5d7SJohn Polstra obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr); 2483124c3e0SJohn Polstra if (u.hdr.e_entry != 0) 249a607e5d7SJohn Polstra obj->entry = (caddr_t) (obj->relocbase + u.hdr.e_entry); 2503124c3e0SJohn Polstra if (phphdr != NULL) { 251a607e5d7SJohn Polstra obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr); 2523124c3e0SJohn Polstra obj->phsize = phphdr->p_memsz; 2533124c3e0SJohn Polstra } 254a607e5d7SJohn Polstra if (phinterp != NULL) 255a607e5d7SJohn Polstra obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr); 2563124c3e0SJohn Polstra 2573124c3e0SJohn Polstra return obj; 2583124c3e0SJohn Polstra } 2593124c3e0SJohn Polstra 260926ea445SJohn Polstra void 261926ea445SJohn Polstra obj_free(Obj_Entry *obj) 262926ea445SJohn Polstra { 263926ea445SJohn Polstra Objlist_Entry *elm; 264926ea445SJohn Polstra 265926ea445SJohn Polstra free(obj->path); 266926ea445SJohn Polstra while (obj->needed != NULL) { 267926ea445SJohn Polstra Needed_Entry *needed = obj->needed; 268926ea445SJohn Polstra obj->needed = needed->next; 269926ea445SJohn Polstra free(needed); 270926ea445SJohn Polstra } 271926ea445SJohn Polstra while (!STAILQ_EMPTY(&obj->dldags)) { 272926ea445SJohn Polstra elm = STAILQ_FIRST(&obj->dldags); 273926ea445SJohn Polstra STAILQ_REMOVE_HEAD(&obj->dldags, link); 274926ea445SJohn Polstra free(elm); 275926ea445SJohn Polstra } 276926ea445SJohn Polstra while (!STAILQ_EMPTY(&obj->dagmembers)) { 277926ea445SJohn Polstra elm = STAILQ_FIRST(&obj->dagmembers); 278926ea445SJohn Polstra STAILQ_REMOVE_HEAD(&obj->dagmembers, link); 279926ea445SJohn Polstra free(elm); 280926ea445SJohn Polstra } 281926ea445SJohn Polstra free(obj); 282926ea445SJohn Polstra } 283926ea445SJohn Polstra 284926ea445SJohn Polstra Obj_Entry * 285926ea445SJohn Polstra obj_new(void) 286926ea445SJohn Polstra { 287926ea445SJohn Polstra Obj_Entry *obj; 288926ea445SJohn Polstra 289926ea445SJohn Polstra obj = CNEW(Obj_Entry); 290926ea445SJohn Polstra STAILQ_INIT(&obj->dldags); 291926ea445SJohn Polstra STAILQ_INIT(&obj->dagmembers); 292926ea445SJohn Polstra return obj; 293926ea445SJohn Polstra } 294926ea445SJohn Polstra 2953124c3e0SJohn Polstra /* 2963124c3e0SJohn Polstra * Given a set of ELF protection flags, return the corresponding protection 2973124c3e0SJohn Polstra * flags for MMAP. 2983124c3e0SJohn Polstra */ 2993124c3e0SJohn Polstra static int 3003124c3e0SJohn Polstra protflags(int elfflags) 3013124c3e0SJohn Polstra { 3023124c3e0SJohn Polstra int prot = 0; 3033124c3e0SJohn Polstra if (elfflags & PF_R) 3043124c3e0SJohn Polstra prot |= PROT_READ; 3053124c3e0SJohn Polstra if (elfflags & PF_W) 3063124c3e0SJohn Polstra prot |= PROT_WRITE; 3073124c3e0SJohn Polstra if (elfflags & PF_X) 3083124c3e0SJohn Polstra prot |= PROT_EXEC; 3093124c3e0SJohn Polstra return prot; 3103124c3e0SJohn Polstra } 311