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
|
/*
* Copyright (c) 1993, 1998 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.
*/
/*
* Mach Operating System
* Copyright (c) 1993,1989 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
* i386-specific routines for loading a.out files.
*/
#include <oskit/exec/a.out.h>
#include <oskit/exec/exec.h>
/* See below...
Must be a power of two.
This should be kept small, because we may be running on a small stack,
and this code is not likely to be performance-critical anyway. */
#define SCAN_CHUNK 256
int exec_load_aout(exec_read_func_t *read, exec_read_exec_func_t *read_exec,
void *handle, exec_info_t *out_info)
{
struct exec x;
oskit_size_t actual;
oskit_addr_t text_start; /* text start in memory */
oskit_size_t text_size;
oskit_addr_t text_offset; /* text offset in file */
oskit_size_t data_size;
int err;
/* Read the exec header. */
err = (*read)(handle, 0, &x, sizeof(x), &actual);
if (err)
return err;
if (actual != sizeof(x))
return EX_NOT_EXECUTABLE;
/*printf("get_loader_info: magic %04o\n", (int)x.a_magic);*/
switch ((int)x.a_magic & 0xFFFF) {
case OMAGIC:
text_start = 0;
text_size = 0;
text_offset = sizeof(struct exec);
data_size = x.a_text + x.a_data;
break;
case NMAGIC:
text_start = 0;
text_size = x.a_text;
text_offset = sizeof(struct exec);
data_size = x.a_data;
break;
case ZMAGIC:
{
char buf[SCAN_CHUNK];
/* This kludge is not for the faint-of-heart...
Basically we're trying to sniff out the beginning of the text segment.
We assume that the first nonzero byte is the first byte of code,
and that x.a_entry is the virtual address of that first byte. */
for (text_offset = 0; ; text_offset++)
{
if ((text_offset & (SCAN_CHUNK-1)) == 0)
{
err = (*read)(handle, text_offset, buf,
SCAN_CHUNK, &actual);
if (err)
return err;
if (actual < SCAN_CHUNK)
buf[actual] = 0xff; /* ensure termination */
if (text_offset == 0)
text_offset = sizeof(struct exec);
}
if (buf[text_offset & (SCAN_CHUNK-1)])
break;
}
/* Account for the (unlikely) event that the first instruction
is actually an add instruction with a zero opcode.
Surely every a.out variant should be sensible enough at least
to align the text segment on a 32-byte boundary... */
text_offset &= ~0x1f;
text_start = x.a_entry;
text_size = x.a_text;
data_size = x.a_data;
break;
}
case QMAGIC:
text_start = 0x1000;
text_offset = 0;
text_size = x.a_text;
data_size = x.a_data;
break;
default:
/* Check for NetBSD big-endian ZMAGIC executable */
if ((int)x.a_magic == 0x0b018600) {
text_start = 0x1000;
text_size = x.a_text;
text_offset = 0;
data_size = x.a_data;
break;
}
return (EX_NOT_EXECUTABLE);
}
/* If the text segment overlaps the same page as the beginning of the data segment,
then cut the text segment short and grow the data segment appropriately. */
if ((text_start + text_size) & 0xfff)
{
oskit_size_t incr = (text_start + text_size) & 0xfff;
if (incr > text_size) incr = text_size;
text_size -= incr;
data_size += incr;
}
/*printf("exec_load_aout: text_start %08x text_offset %08x text_size %08x data_size %08x\n",
text_start, text_offset, text_size, data_size);*/
/* Load the read-only text segment, if any. */
if (text_size > 0)
{
err = (*read_exec)(handle, text_offset, text_size,
text_start, text_size,
EXEC_SECTYPE_READ |
EXEC_SECTYPE_EXECUTE |
EXEC_SECTYPE_ALLOC |
EXEC_SECTYPE_LOAD);
if (err)
return err;
}
/* Load the read-write data segment, if any. */
if (data_size + x.a_bss > 0)
{
err = (*read_exec)(handle,
text_offset + text_size,
data_size,
text_start + text_size,
data_size + x.a_bss,
EXEC_SECTYPE_READ |
EXEC_SECTYPE_WRITE |
EXEC_SECTYPE_EXECUTE |
EXEC_SECTYPE_ALLOC |
EXEC_SECTYPE_LOAD);
if (err)
return err;
}
/*
* Load the symbol table, if any.
* First the symtab, then the strtab.
*/
if (x.a_syms > 0)
{
unsigned strtabsize;
/* Load the symtab. */
err = (*read_exec)(handle,
text_offset + text_size + data_size,
x.a_syms,
0, 0,
EXEC_SECTYPE_AOUT_SYMTAB);
if (err)
return err;
/*
* Figure out size of strtab.
* The size is the first word and includes itself.
* If there is no strtab, this file is bogus.
*/
err = (*read)(handle,
text_offset + text_size + data_size + x.a_syms,
&strtabsize, sizeof(strtabsize),
&actual);
if (err)
return err;
if (actual != sizeof(strtabsize))
return EX_CORRUPT;
/* Load the strtab. */
err = (*read_exec)(handle,
text_offset + text_size + data_size + x.a_syms,
strtabsize,
0, 0,
EXEC_SECTYPE_AOUT_STRTAB);
if (err)
return err;
}
out_info->entry = x.a_entry;
return(0);
}
|