1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/*
* Copyright (c) 1999 University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*-
* 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.
*
* $\Id: load_object.c,v 1.1 1999/06/29 17:37:55 stoller Exp $
*/
#ifdef OSKIT
#include <oskit/page.h>
#else
#include <sys/param.h>
#endif
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <malloc.h>
#include "rtld.h"
#include "debug.h"
void dump_object(Obj_Entry *pobj);
Obj_Entry *
read_object(int fd)
{
Obj_Entry *obj;
union {
Elf_Ehdr hdr;
char buf[PAGE_SIZE];
} u;
int nbytes, nsegs;
Elf_Phdr *phdr, *phlimit, *segs[2], *phdyn, *phphdr;
caddr_t mapbase, objbase;
size_t mapsize;
Elf_Off base_offset;
Elf_Addr base_vaddr, base_vlimit;
if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
_rtld_error("Read error: %s", strerror(errno));
return NULL;
}
/* Make sure the file is valid */
if (nbytes < sizeof(Elf_Ehdr)
|| 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) {
_rtld_error("Invalid file format");
return NULL;
}
if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
|| u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
_rtld_error("Unsupported file layout");
return NULL;
}
if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
|| u.hdr.e_version != EV_CURRENT) {
_rtld_error("Unsupported file version");
return NULL;
}
if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
_rtld_error("Unsupported file type");
return NULL;
}
if (u.hdr.e_machine != ELF_TARG_MACH) {
_rtld_error("Unsupported machine");
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.
*/
assert(u.hdr.e_phentsize == sizeof(Elf_Phdr));
assert(u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE);
assert(u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) <= nbytes);
/*
* Scan the program header entries, and save key information.
*
* We rely on there being exactly two load segments, text and data,
* in that order.
*/
phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff);
phlimit = phdr + u.hdr.e_phnum;
nsegs = 0;
phdyn = NULL;
phphdr = NULL;
while (phdr < phlimit) {
switch (phdr->p_type) {
case PT_LOAD:
assert(nsegs < 2);
segs[nsegs] = phdr;
++nsegs;
break;
case PT_PHDR:
phphdr = phdr;
break;
case PT_DYNAMIC:
phdyn = phdr;
break;
}
++phdr;
}
if (phdyn == NULL) {
_rtld_error("Object is not dynamically-linked");
return NULL;
}
assert(nsegs == 2);
assert(segs[0]->p_align >= PAGE_SIZE);
assert(segs[1]->p_align >= PAGE_SIZE);
/*
* Compute the object limits.
*/
base_offset = trunc_page(segs[0]->p_offset);
base_vaddr = trunc_page(segs[0]->p_vaddr);
base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
mapsize = base_vlimit - base_vaddr;
mapsize += PAGE_SIZE; /* For headers */
dbg("load_object: bo:0x%x bv:0x%x bvl:0x%x ms:0x%x\n",
base_offset, base_vaddr, base_vlimit, mapsize);
/*
* Allocate a region of memory for the object and read the object
* into it. This will form the base for relocation.
*/
if ((mapbase = (caddr_t) xsmemalign(PAGE_SIZE, mapsize)) == NULL) {
_rtld_error("load_object: Could not allocate %d bytes of "
"memory for object", mapsize);
return NULL;
}
memset(mapbase, 0, mapsize);
/*
* Copy the program header into the first page. The object starts
* in the second page.
*/
memcpy(mapbase, u.buf, PAGE_SIZE);
objbase = mapbase + (PAGE_SIZE/sizeof(caddr_t));
/*
* Read the text and data segments. The BSS is already cleared.
*/
lseek(fd, segs[0]->p_offset, SEEK_SET);
if ((nbytes = read(fd, objbase, segs[0]->p_filesz)) == -1) {
_rtld_error("load_object: Could not read text segment");
sfree(mapbase, mapsize);
return NULL;
}
lseek(fd, segs[1]->p_offset, SEEK_SET);
if ((nbytes = read(fd, (caddr_t) (objbase + segs[1]->p_vaddr),
segs[1]->p_filesz)) == -1) {
_rtld_error("load_object: Could not read text segment");
sfree(mapbase, mapsize);
return NULL;
}
/*
* Set up the Object descriptor.
*/
if ((obj = (Obj_Entry *) xcalloc(sizeof(Obj_Entry))) == NULL) {
_rtld_error("load_object: "
"Could not allocate object descriptor");
sfree(mapbase, mapsize);
return NULL;
}
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 = objbase - base_vaddr;
obj->dynamic = (const Elf_Dyn *)
(objbase + (phdyn->p_vaddr - base_vaddr));
if (u.hdr.e_entry != 0)
obj->entry = (caddr_t)
(objbase + (u.hdr.e_entry - base_vaddr));
if (phphdr != NULL) {
obj->phdr = (const Elf_Phdr *)
(objbase + (phphdr->p_vaddr - base_vaddr));
obj->phsize = phphdr->p_memsz;
}
#ifdef RTLDDEBUG
dump_object(obj);
#endif
return obj;
}
/*
* Load headers for the main program.
*/
Elf_Phdr *
load_headers(char *name, int *phnum, caddr_t *entry)
{
Elf_Phdr *phdr;
union headers {
Elf_Ehdr hdr;
char buf[PAGE_SIZE];
} u, *copyu;
int nbytes;
int fd;
/*
* Load the main program.
*/
if ((fd = open(name, O_RDONLY)) == -1) {
_rtld_error("Cannot open \"%s\"", "a.out");
return NULL;
}
if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
_rtld_error("Read error: %s", strerror(errno));
return NULL;
}
/* Make sure the file is valid */
if (nbytes < sizeof(Elf_Ehdr)
|| 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) {
_rtld_error("Invalid file format");
return NULL;
}
if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
|| u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
_rtld_error("Unsupported file layout");
return NULL;
}
if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
|| u.hdr.e_version != EV_CURRENT) {
_rtld_error("Unsupported file version");
return NULL;
}
if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
_rtld_error("Unsupported file type");
return NULL;
}
if (u.hdr.e_machine != ELF_TARG_MACH) {
_rtld_error("Unsupported machine");
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.
*/
assert(u.hdr.e_phentsize == sizeof(Elf_Phdr));
assert(u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE);
assert(u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) <= nbytes);
/*
* Okay, go ahead and make a copy of it.
*/
copyu = (union headers *) xcalloc(sizeof(*copyu));
*copyu = u;
*phnum = u.hdr.e_phnum;
phdr = (Elf_Phdr *) (copyu->buf + copyu->hdr.e_phoff);
return phdr;
}
void
dump_object(Obj_Entry *pobj)
{
dbg("mapbase: %p", pobj->mapbase);
dbg("mapsize: %d", pobj->mapsize);
dbg("textsize: %d", pobj->textsize);
dbg("vaddrbase: %p", pobj->vaddrbase);
dbg("relocbase: %p", pobj->relocbase);
dbg("dynamic: %p", pobj->dynamic);
dbg("entry: %p", pobj->entry);
dbg("phdr: %p", pobj->phdr);
dbg("phsize: %d", pobj->phsize);
}
|