2015-07-17 19:07:03 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 Wolfgang Thaller.
|
|
|
|
|
|
|
|
This file is part of Retro68.
|
|
|
|
|
|
|
|
Retro68 is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Retro68 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
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
permissions described in the GCC Runtime Library Exception, version
|
|
|
|
3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License and
|
|
|
|
a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
see the files COPYING and COPYING.RUNTIME respectively. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <Processes.h>
|
|
|
|
#include <Sound.h>
|
|
|
|
#include <Memory.h>
|
|
|
|
#include <OSUtils.h>
|
2017-04-11 18:42:12 +00:00
|
|
|
#include <Traps.h>
|
2015-07-17 19:07:03 +00:00
|
|
|
|
|
|
|
#include "Retro68Runtime.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct flat_hdr {
|
|
|
|
char magic[4];
|
|
|
|
unsigned long rev; /* version */
|
|
|
|
unsigned long entry; /* Offset of first executable instruction
|
|
|
|
with text segment from beginning of file */
|
|
|
|
unsigned long data_start; /* Offset of data segment from beginning of
|
|
|
|
file */
|
|
|
|
unsigned long data_end; /* Offset of end of data segment
|
|
|
|
from beginning of file */
|
|
|
|
unsigned long bss_end; /* Offset of end of bss segment from beginning
|
|
|
|
of file */
|
|
|
|
|
|
|
|
/* (It is assumed that data_end through bss_end forms the bss segment.) */
|
|
|
|
|
|
|
|
unsigned long stack_size; /* Size of stack, in bytes */
|
|
|
|
unsigned long reloc_start; /* Offset of relocation records from
|
|
|
|
beginning of file */
|
|
|
|
unsigned long reloc_count; /* Number of relocation records */
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned long filler[6]; /* Reserved, set to zero */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef void (*voidFunction)(void);
|
|
|
|
|
|
|
|
extern void __init_section(void);
|
|
|
|
extern void __init_section_end(void);
|
|
|
|
extern void __fini_section(void);
|
|
|
|
extern void __fini_section_end(void);
|
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
typedef struct Retro68RelocState
|
|
|
|
{
|
|
|
|
long headerVirtualAddress;
|
|
|
|
Ptr bssPtr;
|
|
|
|
Handle codeHandle;
|
|
|
|
} Retro68RelocState;
|
2015-07-17 19:07:03 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
static Retro68RelocState relocState __attribute__ ((nocommon)) = {
|
|
|
|
-sizeof(struct flat_hdr), NULL, NULL
|
|
|
|
};
|
2015-07-17 19:07:03 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
#if 1
|
|
|
|
#define assert(x) do { } while(0)
|
|
|
|
#else
|
|
|
|
#define assert(x) do { \
|
|
|
|
if(!(x)) {\
|
|
|
|
unsigned char str[6]; \
|
|
|
|
int i; \
|
|
|
|
int l = __LINE__; \
|
|
|
|
for(i = 1; i < 6; i++) \
|
|
|
|
str[i] = ' '; \
|
|
|
|
str[0] = 5; \
|
|
|
|
str[5] = '0'; \
|
|
|
|
for(i = 5; l && i > 0; i--) \
|
|
|
|
{ \
|
|
|
|
str[i] = '0' + (l % 10); \
|
|
|
|
l /= 10; \
|
|
|
|
} \
|
|
|
|
DebugStr(str); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
#endif
|
2015-07-17 19:07:03 +00:00
|
|
|
|
|
|
|
void Retro68Relocate()
|
|
|
|
{
|
2017-04-11 18:42:12 +00:00
|
|
|
// memory address to retrieve the ROM type (64K or a later ROM)
|
|
|
|
// see for details http://www.mac.linux-m68k.org/devel/macalmanac.php
|
|
|
|
short* ROM85 = (short*) 0x028E;
|
|
|
|
|
|
|
|
// figure out which trap is supported
|
|
|
|
Boolean is128KROM = ((*ROM85) > 0);
|
|
|
|
Boolean hasSysEnvirons = false;
|
|
|
|
Boolean hasStripAddr = false;
|
|
|
|
if (is128KROM)
|
|
|
|
{
|
|
|
|
UniversalProcPtr trapSysEnv = GetOSTrapAddress(_SysEnvirons);
|
|
|
|
UniversalProcPtr trapStripAddr = GetOSTrapAddress(_StripAddress);
|
|
|
|
UniversalProcPtr trapUnimpl = GetOSTrapAddress(_Unimplemented);
|
|
|
|
|
|
|
|
hasSysEnvirons = (trapSysEnv != trapUnimpl);
|
|
|
|
hasStripAddr = (trapStripAddr != trapUnimpl);
|
|
|
|
}
|
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
// Figure out the displacement
|
|
|
|
// what is the difference between the addresses in our program code
|
|
|
|
// and an address calculated by PC-relative access?
|
2015-07-17 19:07:03 +00:00
|
|
|
long displacement;
|
2017-04-11 18:42:12 +00:00
|
|
|
|
|
|
|
if (hasStripAddr)
|
|
|
|
{
|
|
|
|
RETRO68_GET_DISPLACEMENT_STRIP(displacement);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RETRO68_GET_DISPLACEMENT_STRIP24(displacement);
|
|
|
|
}
|
2015-07-17 19:07:03 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
struct Retro68RelocState *rState = (Retro68RelocState*)
|
|
|
|
((char*)&relocState + displacement);
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
// rState now points to the global relocState variable
|
2015-07-20 00:02:28 +00:00
|
|
|
if(displacement == 0)
|
|
|
|
{
|
2015-07-21 21:16:59 +00:00
|
|
|
if(rState->bssPtr)
|
2015-07-20 00:02:28 +00:00
|
|
|
{
|
|
|
|
// this is not the first time, no relocations needed.
|
2015-07-21 21:16:59 +00:00
|
|
|
// should only happen for code resources
|
|
|
|
// that are invoked more than once.
|
|
|
|
|
|
|
|
// Lock the code to be sure.
|
|
|
|
HLock(rState->codeHandle);
|
2015-07-20 00:02:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
// Locate the start of the FLT file header inside the code resource
|
|
|
|
long headerOldVirtualAddress = rState->headerVirtualAddress;
|
2015-07-20 00:02:28 +00:00
|
|
|
struct flat_hdr *header = (struct flat_hdr*) (headerOldVirtualAddress + displacement);
|
|
|
|
uint8_t *base = (uint8_t*) (header+1);
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
// Recover the handle to the code resource by looking at the
|
|
|
|
// longword before the FLT header. The resource templates in Retro68.r store the offset
|
|
|
|
// from the beginning of the code resource there.
|
2015-07-20 00:02:28 +00:00
|
|
|
uint32_t headerOffsetInResource = ((uint32_t*)header)[-1];
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2015-07-20 00:02:28 +00:00
|
|
|
if(headerOffsetInResource < 4096)
|
|
|
|
// Arbitrary magic number. We expect the offset to be small, just a few header bytes before it.
|
2015-07-21 21:16:59 +00:00
|
|
|
// if it's out of range, assume the longword before the header is not the offset we're looking for.
|
2017-04-11 18:42:12 +00:00
|
|
|
{
|
|
|
|
Handle h = RecoverHandle((Ptr) header - headerOffsetInResource);
|
2015-07-20 00:02:28 +00:00
|
|
|
if(MemError() == noErr && h)
|
2017-04-11 18:42:12 +00:00
|
|
|
{
|
2015-07-21 21:16:59 +00:00
|
|
|
// Make sure the code is locked. Only relevant for some code resources.
|
2015-07-20 00:02:28 +00:00
|
|
|
HLock(h);
|
2017-04-11 18:42:12 +00:00
|
|
|
rState->codeHandle = h;
|
|
|
|
}
|
2015-07-20 00:02:28 +00:00
|
|
|
}
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
long bss_size = header->bss_end - header->data_end;
|
2015-07-17 19:07:03 +00:00
|
|
|
|
|
|
|
long n = header->reloc_count;
|
|
|
|
long *relocs = (long*)( (char*)header + header->reloc_start );
|
|
|
|
long i;
|
2015-07-20 00:02:28 +00:00
|
|
|
long data_end = header->data_end + headerOldVirtualAddress;
|
|
|
|
uint32_t flt_size = (uint32_t) header->data_end;
|
2015-07-17 19:07:03 +00:00
|
|
|
long bss_displacement = 0;
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
// Allocate BSS section (uninitialized/zero-initialized global data)
|
|
|
|
if(!rState->bssPtr)
|
2015-07-17 19:07:03 +00:00
|
|
|
{
|
2015-07-17 23:14:43 +00:00
|
|
|
THz zone = ApplicationZone();
|
|
|
|
if(!zone || (char*)header < (char*)zone)
|
2015-07-21 21:16:59 +00:00
|
|
|
rState->bssPtr = NewPtrSysClear(bss_size);
|
2015-07-17 23:14:43 +00:00
|
|
|
else
|
2015-07-21 21:16:59 +00:00
|
|
|
rState->bssPtr = NewPtrClear(bss_size);
|
|
|
|
bss_displacement = (long)(rState->bssPtr) - data_end;
|
2015-07-17 19:07:03 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
// Process relocation records
|
2015-07-17 19:07:03 +00:00
|
|
|
for(i = 0; i < n; i++)
|
|
|
|
{
|
2015-07-20 00:02:28 +00:00
|
|
|
uint8_t *addrPtr = base + relocs[i];
|
|
|
|
uint32_t addr;
|
2015-07-17 19:07:03 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
assert((Ptr)addrPtr >= (Ptr)header);
|
|
|
|
assert((Ptr)addrPtr < (Ptr)header + flt_size);
|
|
|
|
|
2015-07-20 00:02:28 +00:00
|
|
|
//addr = *(uint32_t*)addrPtr;
|
|
|
|
addr = (((((addrPtr[0] << 8) | addrPtr[1]) << 8) | addrPtr[2]) << 8) | addrPtr[3];
|
2015-07-17 19:07:03 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
assert(addr + 0x40 >= headerOldVirtualAddress + 0x40);
|
|
|
|
assert(addr + 0x40 < headerOldVirtualAddress + header->bss_end + 0x40);
|
|
|
|
|
|
|
|
addr += (uint32_t)(addr - headerOldVirtualAddress) >= flt_size ?
|
|
|
|
bss_displacement : displacement;
|
|
|
|
|
|
|
|
assert((Ptr)addr >= (Ptr)header && (Ptr)addr < (Ptr)header + flt_size
|
|
|
|
|| (Ptr)addr >= rState->bssPtr && (Ptr)addr < rState->bssPtr + bss_size);
|
2015-07-17 19:07:03 +00:00
|
|
|
|
|
|
|
addrPtr[3] = addr;
|
2015-07-20 00:02:28 +00:00
|
|
|
addrPtr[2] = (addr >>= 8);
|
|
|
|
addrPtr[1] = (addr >>= 8);
|
|
|
|
addrPtr[0] = (addr >>= 8);
|
|
|
|
//*(uint32_t*)addrPtr = addr;
|
2015-07-17 19:07:03 +00:00
|
|
|
}
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
// We're basically done.
|
|
|
|
// Now check whether we're on 68040 or later and need to flush the cache.
|
2017-04-11 18:42:12 +00:00
|
|
|
// only do this if SysEnvirons is available.
|
|
|
|
// if SysEnvirons is not available, that means we're on an old System or ROM
|
|
|
|
// and likely not using a 68040, so we won't do this
|
|
|
|
if (hasSysEnvirons)
|
2015-07-17 19:07:03 +00:00
|
|
|
{
|
2017-04-11 18:42:12 +00:00
|
|
|
SysEnvRec env;
|
|
|
|
|
|
|
|
env.processor = 0;
|
|
|
|
SysEnvirons(0, &env);
|
|
|
|
if(env.processor >= env68040)
|
|
|
|
{
|
|
|
|
FlushCodeCache();
|
|
|
|
}
|
2015-07-17 19:07:03 +00:00
|
|
|
}
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2015-07-17 19:07:03 +00:00
|
|
|
// accessing globals and calling functions is OK below here.
|
2015-07-21 21:16:59 +00:00
|
|
|
rState->headerVirtualAddress += displacement;
|
2015-07-17 19:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Retro68CallConstructors()
|
|
|
|
{
|
|
|
|
char *p = (char*)&__init_section;
|
|
|
|
char *e = (char*)&__init_section_end;
|
|
|
|
p += 2;
|
|
|
|
while( p < e )
|
|
|
|
{
|
|
|
|
(*(voidFunction)(*(long*)p))();
|
|
|
|
p += 6;
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 23:14:43 +00:00
|
|
|
|
2015-08-29 18:11:53 +00:00
|
|
|
void Retro68CallDestructors()
|
|
|
|
{
|
|
|
|
char *p = (char*)&__fini_section;
|
|
|
|
char *e = (char*)&__fini_section_end;
|
|
|
|
p += 2;
|
|
|
|
while( p < e )
|
|
|
|
{
|
|
|
|
(*(voidFunction)(*(long*)p))();
|
|
|
|
p += 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-17 23:14:43 +00:00
|
|
|
void Retro68FreeGlobals()
|
|
|
|
{
|
2015-07-21 21:16:59 +00:00
|
|
|
if(relocState.bssPtr != (Ptr) -1)
|
2015-07-17 23:14:43 +00:00
|
|
|
{
|
2015-07-21 21:16:59 +00:00
|
|
|
DisposePtr(relocState.bssPtr);
|
|
|
|
relocState.bssPtr = (Ptr) -1;
|
2015-07-17 23:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|