1998-03-07 19:24:35 +00:00
|
|
|
/*-
|
|
|
|
* Copyright 1996-1998 John D. Polstra.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1998-03-07 19:24:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mman.h>
|
1999-08-30 01:50:41 +00:00
|
|
|
#include <sys/stat.h>
|
1998-03-07 19:24:35 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
1999-08-30 01:48:19 +00:00
|
|
|
#include <stdlib.h>
|
1998-03-07 19:24:35 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2001-10-15 18:48:42 +00:00
|
|
|
#include "debug.h"
|
1998-03-07 19:24:35 +00:00
|
|
|
#include "rtld.h"
|
|
|
|
|
2002-12-16 19:24:43 +00:00
|
|
|
static int convert_prot(int); /* Elf flags -> mmap protection */
|
|
|
|
static int convert_flags(int); /* Elf flags -> mmap flags */
|
1998-03-07 19:24:35 +00:00
|
|
|
|
|
|
|
/*
|
1999-07-18 00:02:19 +00:00
|
|
|
* Map a shared object into memory. The "fd" argument is a file descriptor,
|
1998-03-07 19:24:35 +00:00
|
|
|
* which must be open on the object and positioned at its beginning.
|
1999-07-18 00:02:19 +00:00
|
|
|
* The "path" argument is a pathname that is used only for error messages.
|
1998-03-07 19:24:35 +00:00
|
|
|
*
|
|
|
|
* The return value is a pointer to a newly-allocated Obj_Entry structure
|
|
|
|
* for the shared object. Returns NULL on failure.
|
|
|
|
*/
|
|
|
|
Obj_Entry *
|
1999-08-30 01:50:41 +00:00
|
|
|
map_object(int fd, const char *path, const struct stat *sb)
|
1998-03-07 19:24:35 +00:00
|
|
|
{
|
|
|
|
Obj_Entry *obj;
|
|
|
|
union {
|
1998-09-04 19:03:57 +00:00
|
|
|
Elf_Ehdr hdr;
|
1998-03-07 19:24:35 +00:00
|
|
|
char buf[PAGE_SIZE];
|
|
|
|
} u;
|
2002-10-23 01:43:29 +00:00
|
|
|
int nbytes, i;
|
1998-09-04 19:03:57 +00:00
|
|
|
Elf_Phdr *phdr;
|
|
|
|
Elf_Phdr *phlimit;
|
2002-10-23 01:43:29 +00:00
|
|
|
Elf_Phdr **segs;
|
1998-03-07 19:24:35 +00:00
|
|
|
int nsegs;
|
1998-09-04 19:03:57 +00:00
|
|
|
Elf_Phdr *phdyn;
|
|
|
|
Elf_Phdr *phphdr;
|
1999-08-30 01:54:13 +00:00
|
|
|
Elf_Phdr *phinterp;
|
1998-03-07 19:24:35 +00:00
|
|
|
caddr_t mapbase;
|
|
|
|
size_t mapsize;
|
1998-09-04 19:03:57 +00:00
|
|
|
Elf_Off base_offset;
|
|
|
|
Elf_Addr base_vaddr;
|
|
|
|
Elf_Addr base_vlimit;
|
1998-03-07 19:24:35 +00:00
|
|
|
caddr_t base_addr;
|
1998-09-04 19:03:57 +00:00
|
|
|
Elf_Off data_offset;
|
|
|
|
Elf_Addr data_vaddr;
|
|
|
|
Elf_Addr data_vlimit;
|
1998-03-07 19:24:35 +00:00
|
|
|
caddr_t data_addr;
|
2002-10-23 01:43:29 +00:00
|
|
|
int data_prot;
|
2002-12-16 19:24:43 +00:00
|
|
|
int data_flags;
|
1998-09-04 19:03:57 +00:00
|
|
|
Elf_Addr clear_vaddr;
|
1998-03-07 19:24:35 +00:00
|
|
|
caddr_t clear_addr;
|
2002-10-23 01:43:29 +00:00
|
|
|
caddr_t clear_page;
|
1998-03-07 19:24:35 +00:00
|
|
|
size_t nclear;
|
1998-09-04 19:03:57 +00:00
|
|
|
Elf_Addr bss_vaddr;
|
|
|
|
Elf_Addr bss_vlimit;
|
1998-03-07 19:24:35 +00:00
|
|
|
caddr_t bss_addr;
|
|
|
|
|
|
|
|
if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
|
1999-07-18 00:02:19 +00:00
|
|
|
_rtld_error("%s: read error: %s", path, strerror(errno));
|
1998-03-07 19:24:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the file is valid */
|
1998-09-04 19:03:57 +00:00
|
|
|
if (nbytes < sizeof(Elf_Ehdr)
|
1998-03-07 19:24:35 +00:00
|
|
|
|| u.hdr.e_ident[EI_MAG0] != ELFMAG0
|
|
|
|
|| u.hdr.e_ident[EI_MAG1] != ELFMAG1
|
|
|
|
|| u.hdr.e_ident[EI_MAG2] != ELFMAG2
|
|
|
|
|| u.hdr.e_ident[EI_MAG3] != ELFMAG3) {
|
1999-07-18 00:02:19 +00:00
|
|
|
_rtld_error("%s: invalid file format", path);
|
1998-03-07 19:24:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1998-09-04 19:03:57 +00:00
|
|
|
if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
|
|
|
|
|| u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
|
1999-07-18 00:02:19 +00:00
|
|
|
_rtld_error("%s: unsupported file layout", path);
|
1998-03-07 19:24:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
|
|
|
|
|| u.hdr.e_version != EV_CURRENT) {
|
1999-07-18 00:02:19 +00:00
|
|
|
_rtld_error("%s: unsupported file version", path);
|
1998-03-07 19:24:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
|
1999-07-18 00:02:19 +00:00
|
|
|
_rtld_error("%s: unsupported file type", path);
|
1998-03-07 19:24:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1998-09-04 19:03:57 +00:00
|
|
|
if (u.hdr.e_machine != ELF_TARG_MACH) {
|
1999-07-18 00:02:19 +00:00
|
|
|
_rtld_error("%s: unsupported machine", path);
|
1998-03-07 19:24:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We rely on the program header being in the first page. This is
|
|
|
|
* not strictly required by the ABI specification, but it seems to
|
|
|
|
* always true in practice. And, it simplifies things considerably.
|
|
|
|
*/
|
1999-07-18 00:02:19 +00:00
|
|
|
if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
|
|
|
|
_rtld_error(
|
|
|
|
"%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) > nbytes) {
|
|
|
|
_rtld_error("%s: program header too large", path);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-03-07 19:24:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan the program header entries, and save key information.
|
|
|
|
*
|
|
|
|
* We rely on there being exactly two load segments, text and data,
|
|
|
|
* in that order.
|
|
|
|
*/
|
1998-09-04 19:03:57 +00:00
|
|
|
phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff);
|
1998-03-07 19:24:35 +00:00
|
|
|
phlimit = phdr + u.hdr.e_phnum;
|
2002-10-23 01:43:29 +00:00
|
|
|
nsegs = -1;
|
1999-08-30 01:54:13 +00:00
|
|
|
phdyn = phphdr = phinterp = NULL;
|
2002-10-23 01:43:29 +00:00
|
|
|
segs = alloca(sizeof(segs[0]) * u.hdr.e_phnum);
|
1998-03-07 19:24:35 +00:00
|
|
|
while (phdr < phlimit) {
|
|
|
|
switch (phdr->p_type) {
|
|
|
|
|
1999-08-30 01:54:13 +00:00
|
|
|
case PT_INTERP:
|
|
|
|
phinterp = phdr;
|
|
|
|
break;
|
|
|
|
|
1998-03-07 19:24:35 +00:00
|
|
|
case PT_LOAD:
|
2002-10-23 01:43:29 +00:00
|
|
|
segs[++nsegs] = phdr;
|
|
|
|
if (segs[nsegs]->p_align < PAGE_SIZE) {
|
|
|
|
_rtld_error("%s: PT_LOAD segment %d not page-aligned",
|
|
|
|
path, nsegs);
|
1999-07-18 00:02:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1998-03-07 19:24:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PT_PHDR:
|
|
|
|
phphdr = phdr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PT_DYNAMIC:
|
|
|
|
phdyn = phdr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++phdr;
|
|
|
|
}
|
|
|
|
if (phdyn == NULL) {
|
1999-07-18 00:02:19 +00:00
|
|
|
_rtld_error("%s: object is not dynamically-linked", path);
|
1998-03-07 19:24:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-10-23 01:43:29 +00:00
|
|
|
if (nsegs < 0) {
|
1999-07-18 00:02:19 +00:00
|
|
|
_rtld_error("%s: too few PT_LOAD segments", path);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-03-07 19:24:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Map the entire address space of the object, to stake out our
|
|
|
|
* contiguous region, and to establish the base address for relocation.
|
|
|
|
*/
|
|
|
|
base_offset = trunc_page(segs[0]->p_offset);
|
|
|
|
base_vaddr = trunc_page(segs[0]->p_vaddr);
|
2002-10-23 01:43:29 +00:00
|
|
|
base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
|
1998-03-07 19:24:35 +00:00
|
|
|
mapsize = base_vlimit - base_vaddr;
|
|
|
|
base_addr = u.hdr.e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
|
|
|
|
|
2002-12-16 19:24:43 +00:00
|
|
|
mapbase = mmap(base_addr, mapsize, convert_prot(segs[0]->p_flags),
|
|
|
|
convert_flags(segs[0]->p_flags), fd, base_offset);
|
1998-03-07 19:24:35 +00:00
|
|
|
if (mapbase == (caddr_t) -1) {
|
1999-07-18 00:02:19 +00:00
|
|
|
_rtld_error("%s: mmap of entire address space failed: %s",
|
|
|
|
path, strerror(errno));
|
1998-03-07 19:24:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (base_addr != NULL && mapbase != base_addr) {
|
1999-07-18 00:02:19 +00:00
|
|
|
_rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
|
|
|
|
path, base_addr, mapbase);
|
1998-03-07 19:24:35 +00:00
|
|
|
munmap(mapbase, mapsize);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-10-23 01:43:29 +00:00
|
|
|
for (i = 0; i <= nsegs; i++) {
|
|
|
|
/* Overlay the segment onto the proper region. */
|
|
|
|
data_offset = trunc_page(segs[i]->p_offset);
|
|
|
|
data_vaddr = trunc_page(segs[i]->p_vaddr);
|
|
|
|
data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
|
|
|
|
data_addr = mapbase + (data_vaddr - base_vaddr);
|
2002-12-16 19:24:43 +00:00
|
|
|
data_prot = convert_prot(segs[i]->p_flags);
|
|
|
|
data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
|
2002-10-23 01:43:29 +00:00
|
|
|
/* Do not call mmap on the first segment - this is redundant */
|
|
|
|
if (i && mmap(data_addr, data_vlimit - data_vaddr, data_prot,
|
2002-12-16 19:24:43 +00:00
|
|
|
data_flags, fd, data_offset) == (caddr_t) -1) {
|
2002-10-23 01:43:29 +00:00
|
|
|
_rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
|
1998-03-07 19:24:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-10-23 01:43:29 +00:00
|
|
|
|
|
|
|
/* Clear any BSS in the last page of the segment. */
|
|
|
|
clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
|
|
|
|
clear_addr = mapbase + (clear_vaddr - base_vaddr);
|
|
|
|
clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
|
|
|
|
if ((nclear = data_vlimit - clear_vaddr) > 0) {
|
|
|
|
/* Make sure the end of the segment is writable */
|
|
|
|
if ((data_prot & PROT_WRITE) == 0 &&
|
|
|
|
-1 == mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
|
|
|
|
_rtld_error("%s: mprotect failed: %s", path,
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(clear_addr, 0, nclear);
|
|
|
|
|
|
|
|
/* Reset the data protection back */
|
|
|
|
if ((data_prot & PROT_WRITE) == 0)
|
|
|
|
mprotect(clear_page, PAGE_SIZE, data_prot);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Overlay the BSS segment onto the proper region. */
|
|
|
|
bss_vaddr = data_vlimit;
|
|
|
|
bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
|
|
|
|
bss_addr = mapbase + (bss_vaddr - base_vaddr);
|
|
|
|
if (bss_vlimit > bss_vaddr) { /* There is something to do */
|
|
|
|
if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
|
|
|
|
MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
|
|
|
|
_rtld_error("%s: mmap of bss failed: %s", path,
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1998-03-07 19:24:35 +00:00
|
|
|
}
|
|
|
|
|
1999-08-30 01:48:19 +00:00
|
|
|
obj = obj_new();
|
1999-08-30 01:50:41 +00:00
|
|
|
if (sb != NULL) {
|
|
|
|
obj->dev = sb->st_dev;
|
|
|
|
obj->ino = sb->st_ino;
|
|
|
|
}
|
1998-03-07 19:24:35 +00:00
|
|
|
obj->mapbase = mapbase;
|
|
|
|
obj->mapsize = mapsize;
|
|
|
|
obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
|
|
|
|
base_vaddr;
|
|
|
|
obj->vaddrbase = base_vaddr;
|
|
|
|
obj->relocbase = mapbase - base_vaddr;
|
1999-08-30 01:54:13 +00:00
|
|
|
obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
|
1998-03-07 19:24:35 +00:00
|
|
|
if (u.hdr.e_entry != 0)
|
1999-08-30 01:54:13 +00:00
|
|
|
obj->entry = (caddr_t) (obj->relocbase + u.hdr.e_entry);
|
1998-03-07 19:24:35 +00:00
|
|
|
if (phphdr != NULL) {
|
1999-08-30 01:54:13 +00:00
|
|
|
obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr);
|
1998-03-07 19:24:35 +00:00
|
|
|
obj->phsize = phphdr->p_memsz;
|
|
|
|
}
|
1999-08-30 01:54:13 +00:00
|
|
|
if (phinterp != NULL)
|
|
|
|
obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
|
1998-03-07 19:24:35 +00:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
1999-08-30 01:48:19 +00:00
|
|
|
void
|
|
|
|
obj_free(Obj_Entry *obj)
|
|
|
|
{
|
|
|
|
Objlist_Entry *elm;
|
|
|
|
|
|
|
|
free(obj->path);
|
|
|
|
while (obj->needed != NULL) {
|
|
|
|
Needed_Entry *needed = obj->needed;
|
|
|
|
obj->needed = needed->next;
|
|
|
|
free(needed);
|
|
|
|
}
|
|
|
|
while (!STAILQ_EMPTY(&obj->dldags)) {
|
|
|
|
elm = STAILQ_FIRST(&obj->dldags);
|
|
|
|
STAILQ_REMOVE_HEAD(&obj->dldags, link);
|
|
|
|
free(elm);
|
|
|
|
}
|
|
|
|
while (!STAILQ_EMPTY(&obj->dagmembers)) {
|
|
|
|
elm = STAILQ_FIRST(&obj->dagmembers);
|
|
|
|
STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
|
|
|
|
free(elm);
|
|
|
|
}
|
|
|
|
free(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
Obj_Entry *
|
|
|
|
obj_new(void)
|
|
|
|
{
|
|
|
|
Obj_Entry *obj;
|
|
|
|
|
|
|
|
obj = CNEW(Obj_Entry);
|
|
|
|
STAILQ_INIT(&obj->dldags);
|
|
|
|
STAILQ_INIT(&obj->dagmembers);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
1998-03-07 19:24:35 +00:00
|
|
|
/*
|
|
|
|
* Given a set of ELF protection flags, return the corresponding protection
|
|
|
|
* flags for MMAP.
|
|
|
|
*/
|
|
|
|
static int
|
2002-12-16 19:24:43 +00:00
|
|
|
convert_prot(int elfflags)
|
1998-03-07 19:24:35 +00:00
|
|
|
{
|
|
|
|
int prot = 0;
|
|
|
|
if (elfflags & PF_R)
|
|
|
|
prot |= PROT_READ;
|
|
|
|
if (elfflags & PF_W)
|
|
|
|
prot |= PROT_WRITE;
|
|
|
|
if (elfflags & PF_X)
|
|
|
|
prot |= PROT_EXEC;
|
|
|
|
return prot;
|
|
|
|
}
|
2002-12-16 19:24:43 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
convert_flags(int elfflags)
|
|
|
|
{
|
|
|
|
int flags = MAP_PRIVATE; /* All mappings are private */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Readonly mappings are marked "MAP_NOCORE", because they can be
|
|
|
|
* reconstructed by a debugger.
|
|
|
|
*/
|
|
|
|
if (!(elfflags & PF_W))
|
|
|
|
flags |= MAP_NOCORE;
|
|
|
|
return flags;
|
|
|
|
}
|