| 1 |
/* |
| 2 |
* loader.c |
| 3 |
* |
| 4 |
* This file is part of the Oxford Oberon-2 compiler |
| 5 |
* Copyright (c) 2006--2016 J. M. Spivey |
| 6 |
* All rights reserved |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions are met: |
| 10 |
* |
| 11 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 12 |
* this list of conditions and the following disclaimer. |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 |
* this list of conditions and the following disclaimer in the documentation |
| 15 |
* and/or other materials provided with the distribution. |
| 16 |
* 3. The name of the author may not be used to endorse or promote products |
| 17 |
* derived from this software without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 20 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 21 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 27 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 28 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "obx.h" |
| 32 |
#include "keiko.h" |
| 33 |
#include "exec.h" |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
/* get_int -- get a 4-byte value in portable byte order */ |
| 37 |
static int get_int(uchar *p) { |
| 38 |
return (p[3]<<24) + (p[2]<<16) + (p[1]<<8) + p[0]; |
| 39 |
} |
| 40 |
|
| 41 |
/* Here is the still centre of the whirling vortex that is byte-order |
| 42 |
independence. The compiler output, Kieko assembly language, is |
| 43 |
plain text. The assembler/linker translates this into a byte-order |
| 44 |
independent file of object code. |
| 45 |
|
| 46 |
The bytecode in this file contains one and two byte embedded |
| 47 |
constants that are in little-endian order, and the bytecode |
| 48 |
interpreter puts the bytes together where necessary, respecting the |
| 49 |
little-endian order in the code even on a big-endian machine. (It |
| 50 |
has to address bytecode one byte a time anyway, because of |
| 51 |
alignment restrictions.) |
| 52 |
|
| 53 |
The data segment in the object code consists of 4-byte words, and |
| 54 |
these are relocated when the program is loaded. Some of these |
| 55 |
words contain character data for string constants, and they require |
| 56 |
no relocation. Some words contain integer or floating-point |
| 57 |
constants, and they are relocated by swapping the byte order if |
| 58 |
necessary. Finally, some words contain addresses in the data or |
| 59 |
code segment, and they are relocated by swapping the byte order as |
| 60 |
needed, and adding the base address of the segment in question. |
| 61 |
Thus in the running program, both the memory and the evaluation |
| 62 |
stack contain only values in native byte order -- and all pointers |
| 63 |
are represented as absolute addresses, enabling the program to live |
| 64 |
in harmony with a conservative garbage collector. |
| 65 |
|
| 66 |
One final twist: double-precision values are always stored as two |
| 67 |
words, with each word in native byte order, but with the less |
| 68 |
significant word first, even on a big-endian machine. This is ok, |
| 69 |
because these values are always loaded and stored one word at a |
| 70 |
time, and assembled into native order immediately before doing |
| 71 |
arithmetic. */ |
| 72 |
|
| 73 |
#define REL_BLOCK 4096 |
| 74 |
|
| 75 |
static void reloc(int base, const uchar rbuf[], int size) { |
| 76 |
int i; |
| 77 |
value *p; |
| 78 |
unsigned m; |
| 79 |
|
| 80 |
for (i = 0; i < size; i += WORD_SIZE) { |
| 81 |
int rbits = reloc_bits(rbuf, i/WORD_SIZE); |
| 82 |
|
| 83 |
#ifdef DEBUG |
| 84 |
if (dflag > 3) |
| 85 |
printf("Reloc %d %d\n", base+i, rbits); |
| 86 |
#endif |
| 87 |
|
| 88 |
if (rbits == R_NONE) continue; |
| 89 |
|
| 90 |
p = (value *) &dmem[base+i]; |
| 91 |
m = get_int((uchar *) p); |
| 92 |
|
| 93 |
switch (rbits) { |
| 94 |
case R_WORD: |
| 95 |
(*p).i = m; |
| 96 |
break; |
| 97 |
case R_ADDR: |
| 98 |
if ((m & IBIT) == 0) |
| 99 |
(*p).a = dsegaddr(dmem + m); |
| 100 |
else |
| 101 |
(*p).a = codeaddr(imem + (m & ~IBIT)); |
| 102 |
break; |
| 103 |
case R_SUBR: |
| 104 |
switch (m) { |
| 105 |
case INTERP: (*p).a = interpreter; break; |
| 106 |
case DLTRAP: (*p).a = dyntrap; break; |
| 107 |
default: |
| 108 |
panic("bad subr code %x\n", m); |
| 109 |
} |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
#ifndef PRELOAD |
| 116 |
static FILE *binfp; |
| 117 |
|
| 118 |
static int binread(void *buf, int size) { |
| 119 |
return fread(buf, 1, size, binfp); |
| 120 |
} |
| 121 |
|
| 122 |
/* relocate -- read relocation data */ |
| 123 |
static void relocate(int size) { |
| 124 |
uchar rbuf[REL_BLOCK]; |
| 125 |
int n; |
| 126 |
|
| 127 |
for (int base = 0; base < size; base += n) { |
| 128 |
n = min(size - base, REL_BLOCK * CODES_PER_BYTE * WORD_SIZE); |
| 129 |
int nbytes = (n/WORD_SIZE+CODES_PER_BYTE-1)/CODES_PER_BYTE; |
| 130 |
binread(rbuf, nbytes); |
| 131 |
reloc(base, rbuf, n); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
static int bingetc(void) { |
| 136 |
char buf[1]; |
| 137 |
if (binread(buf, 1) == 0) return EOF; |
| 138 |
return buf[0]; |
| 139 |
} |
| 140 |
|
| 141 |
/* read_int -- input a 4-byte value in portable byte order */ |
| 142 |
static int read_int() { |
| 143 |
uchar buf[4]; |
| 144 |
binread(buf, 4); |
| 145 |
return get_int(buf); |
| 146 |
} |
| 147 |
|
| 148 |
/* read_string -- input a null-terminated string, allocate space dynamically */ |
| 149 |
static char *read_string() { |
| 150 |
int n = 0; |
| 151 |
int c; |
| 152 |
char *p; |
| 153 |
char buf[256]; |
| 154 |
|
| 155 |
do { |
| 156 |
c = bingetc(); |
| 157 |
if (c == EOF) panic("*unexpected EOF"); |
| 158 |
buf[n++] = c; |
| 159 |
} while (c != '\0'); |
| 160 |
|
| 161 |
p = scratch_alloc_atomic(n); |
| 162 |
strcpy(p, buf); |
| 163 |
return p; |
| 164 |
} |
| 165 |
|
| 166 |
/* read_symbols -- read symbol table */ |
| 167 |
static void read_symbols(int dseg) { |
| 168 |
word addr; |
| 169 |
int chksum, nlines; |
| 170 |
#ifdef DEBUG |
| 171 |
const char *kname; |
| 172 |
#define debug_kind(n) kname = n |
| 173 |
#else |
| 174 |
#define debug_kind(n) |
| 175 |
#endif |
| 176 |
|
| 177 |
for (int i = 0; i < nsyms; i++) { |
| 178 |
int kind = read_int(); |
| 179 |
char *name = read_string(); |
| 180 |
|
| 181 |
switch (kind) { |
| 182 |
case X_MODULE: |
| 183 |
debug_kind("Module"); |
| 184 |
addr = dsegaddr(dmem + read_int()); |
| 185 |
chksum = read_int(); |
| 186 |
nlines = read_int(); |
| 187 |
make_module(name, addr, chksum, nlines); |
| 188 |
break; |
| 189 |
|
| 190 |
case X_PROC: |
| 191 |
debug_kind("Proc"); |
| 192 |
addr = dsegaddr(dmem + read_int()); |
| 193 |
make_proc(name, addr); |
| 194 |
break; |
| 195 |
|
| 196 |
case X_DATA: |
| 197 |
debug_kind("Data"); |
| 198 |
addr = dsegaddr(dmem + read_int()); |
| 199 |
make_symbol("data", name, addr); |
| 200 |
break; |
| 201 |
|
| 202 |
case X_LINE: |
| 203 |
debug_kind("Line"); |
| 204 |
addr = codeaddr(imem + read_int()); |
| 205 |
make_symbol("line", name, addr); |
| 206 |
break; |
| 207 |
|
| 208 |
default: |
| 209 |
debug_kind("Unknown"); |
| 210 |
addr = 0; |
| 211 |
panic("*bad symbol %s", name); |
| 212 |
} |
| 213 |
|
| 214 |
#ifdef DEBUG |
| 215 |
if (dflag >= 1) printf("%s %s = %#x\n", kname, name, addr); |
| 216 |
#endif |
| 217 |
} |
| 218 |
|
| 219 |
fix_sizes(dseg); |
| 220 |
} |
| 221 |
|
| 222 |
/* load_file -- load a file of object code */ |
| 223 |
void load_file(FILE *bfp) { |
| 224 |
/* Get trailer */ |
| 225 |
trailer t; |
| 226 |
fseek(bfp, - (long) sizeof(trailer), SEEK_END); |
| 227 |
int nread = fread(&t, 1, sizeof(trailer), bfp); |
| 228 |
if (nread != sizeof(trailer)) panic("couldn't read trailer"); |
| 229 |
|
| 230 |
/* Check magic numbers */ |
| 231 |
if (nread < sizeof(trailer)) |
| 232 |
panic("couldn't read trailer"); |
| 233 |
if (strncmp((char *) t.magic, MAGIC, 4) != 0) |
| 234 |
panic("bad magic number\n%s", |
| 235 |
"[The program you are running is not a valid" |
| 236 |
" Oberon bytecode file]"); |
| 237 |
if (get_int(t.sig) != SIG) |
| 238 |
panic("bad signature %#0.8x\n%s\n%s", get_int(t.sig), |
| 239 |
"[Although this appears to be an Oberon bytecode file,", |
| 240 |
" it needs a different version of the runtime system]"); |
| 241 |
|
| 242 |
/* Decode the other data */ |
| 243 |
int seglen[NSEGS]; |
| 244 |
for (int i = 0; i < NSEGS; i++) |
| 245 |
seglen[i] = get_int(t.segment[i]); |
| 246 |
|
| 247 |
code_size = seglen[S_CODE]; |
| 248 |
stack_size = seglen[S_STACK]; |
| 249 |
|
| 250 |
nmods = get_int(t.nmods); nprocs = get_int(t.nprocs); |
| 251 |
nsyms = get_int(t.nsyms); |
| 252 |
int start = get_int(t.start); |
| 253 |
|
| 254 |
#ifdef DEBUG |
| 255 |
if (dflag >= 1) { |
| 256 |
printf("csize = %d, dsize = %d, bss = %d, stk = %d\n", |
| 257 |
seglen[S_CODE], seglen[S_DATA], |
| 258 |
seglen[S_BSS], seglen[S_STACK]); |
| 259 |
printf("nmods = %d, nprocs = %d, nsyms = %d\n", |
| 260 |
nmods, nprocs, nsyms); |
| 261 |
} |
| 262 |
#endif |
| 263 |
|
| 264 |
fseek(bfp, start, SEEK_END); |
| 265 |
binfp = bfp; |
| 266 |
|
| 267 |
/* Load the code */ |
| 268 |
imem = scratch_alloc_atomic(seglen[S_CODE]); |
| 269 |
binread(imem, seglen[S_CODE]); |
| 270 |
|
| 271 |
/* Load and relocate the data */ |
| 272 |
dmem = scratch_alloc(seglen[S_DATA]+seglen[S_BSS]); |
| 273 |
#ifdef SEGMEM |
| 274 |
data_vbase = map_segment(dmem, seglen[S_DATA]+seglen[S_BSS]); |
| 275 |
#endif |
| 276 |
binread(dmem, seglen[S_DATA]); |
| 277 |
relocate(seglen[S_DATA]); |
| 278 |
memset(dmem+seglen[S_DATA], 0, seglen[S_BSS]); |
| 279 |
|
| 280 |
/* Allocate stack */ |
| 281 |
stack = scratch_alloc(stack_size); |
| 282 |
#ifdef SEGMEM |
| 283 |
stack_vbase = map_segment(stack, stack_size); |
| 284 |
#endif |
| 285 |
/* Save the entry point, pointer map and library path */ |
| 286 |
entry = (value *) &dmem[get_int(t.entry)]; |
| 287 |
gcmap = (value *) &dmem[get_int(t.gcmap)]; |
| 288 |
if (get_int(t.libdir) != 0) |
| 289 |
libpath = (char *) &dmem[get_int(t.libdir)]; |
| 290 |
|
| 291 |
/* Read the symbols */ |
| 292 |
if (nsyms > 0) read_symbols(seglen[S_DATA]); |
| 293 |
} |
| 294 |
#else |
| 295 |
extern const uchar preload_imem[], preload_dmem[], preload_reloc[]; |
| 296 |
extern const unsigned preload_segsize[]; |
| 297 |
extern const unsigned preload_entry, preload_gcmap, preload_libdir; |
| 298 |
extern const unsigned preload_nprocs, preload_nmods; |
| 299 |
extern const struct _sym { int kind; char *name; int val; } preload_syms[]; |
| 300 |
|
| 301 |
#define seglen preload_segsize |
| 302 |
|
| 303 |
/* load_image -- unpack preloaded image */ |
| 304 |
void load_image(void) { |
| 305 |
int i; |
| 306 |
|
| 307 |
code_size = seglen[S_CODE]; |
| 308 |
stack_size = seglen[S_STACK]; |
| 309 |
nmods = preload_nmods; |
| 310 |
nprocs = preload_nprocs; |
| 311 |
nsyms = nmods+nprocs; |
| 312 |
|
| 313 |
imem = (uchar *) preload_imem; |
| 314 |
dmem = scratch_alloc(seglen[S_DATA]+seglen[S_BSS]); |
| 315 |
#ifdef SEGMEM |
| 316 |
data_vbase = map_segment(dmem, seglen[S_DATA]+seglen[S_BSS]); |
| 317 |
#endif |
| 318 |
memcpy(dmem, preload_dmem, seglen[S_DATA]); |
| 319 |
reloc(0, preload_reloc, seglen[S_DATA]); |
| 320 |
memset(dmem+seglen[S_DATA], 0, seglen[S_BSS]); |
| 321 |
stack = scratch_alloc(stack_size); |
| 322 |
#ifdef SEGMEM |
| 323 |
stack_vbase = map_segment(stack, stack_size); |
| 324 |
#endif |
| 325 |
|
| 326 |
for (i = 0; i < nsyms; i++) { |
| 327 |
const struct _sym *s = &preload_syms[i]; |
| 328 |
switch (s->kind) { |
| 329 |
case X_PROC: |
| 330 |
make_proc(s->name, dsegaddr(dmem + s->val)); |
| 331 |
break; |
| 332 |
case X_MODULE: |
| 333 |
make_module(s->name, dsegaddr(dmem + s->val), 0, 0); |
| 334 |
break; |
| 335 |
default: |
| 336 |
panic("Bad symbol code"); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
fix_sizes(seglen[S_DATA]); |
| 341 |
entry = (value *) &dmem[preload_entry]; |
| 342 |
gcmap = (value *) &dmem[preload_gcmap]; |
| 343 |
|
| 344 |
if (preload_libdir != 0) |
| 345 |
libpath = (char *) &dmem[preload_libdir]; |
| 346 |
} |
| 347 |
#endif |