2015-07-17 19:07:03 +00:00
|
|
|
/*
|
2017-09-23 20:36:47 +00:00
|
|
|
Copyright 2017 Wolfgang Thaller.
|
2015-07-17 19:07:03 +00:00
|
|
|
|
|
|
|
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"
|
2017-09-23 20:36:47 +00:00
|
|
|
#include "PoorMansDebugging.h"
|
2015-07-17 19:07:03 +00:00
|
|
|
|
|
|
|
typedef void (*voidFunction)(void);
|
|
|
|
|
2017-09-23 20:36:47 +00:00
|
|
|
/*
|
|
|
|
Linker-defined addresses in the binary;
|
|
|
|
*/
|
|
|
|
extern uint8_t _stext, _etext, _sdata, _edata, _sbss, _ebss;
|
|
|
|
extern uint8_t __init_section, __init_section_end;
|
|
|
|
extern uint8_t __fini_section, __fini_section_end;
|
2015-07-17 19:07:03 +00:00
|
|
|
|
2015-07-21 21:16:59 +00:00
|
|
|
typedef struct Retro68RelocState
|
|
|
|
{
|
2017-09-23 17:56:36 +00:00
|
|
|
Ptr bssPtr;
|
|
|
|
Handle codeHandle;
|
2015-07-21 21:16:59 +00:00
|
|
|
} Retro68RelocState;
|
2015-07-17 19:07:03 +00:00
|
|
|
|
2017-09-23 17:56:36 +00:00
|
|
|
static Retro68RelocState relocState __attribute__ ((nocommon, section(".text"))) = {
|
|
|
|
NULL, NULL
|
2015-07-21 21:16:59 +00:00
|
|
|
};
|
2015-07-17 19:07:03 +00:00
|
|
|
|
2017-09-23 17:56:36 +00:00
|
|
|
|
|
|
|
#define GET_VIRTUAL_ADDRESS(NAME, SYM) \
|
|
|
|
do { \
|
|
|
|
__asm__( "\tlea " #SYM ", %0\n" \
|
|
|
|
: "=a"(NAME) ); \
|
|
|
|
if(hasStripAddr) \
|
|
|
|
NAME = StripAddress(NAME); \
|
|
|
|
else \
|
|
|
|
NAME = StripAddress24(NAME); \
|
|
|
|
} while(0)
|
|
|
|
|
2017-09-23 20:36:47 +00:00
|
|
|
#define READ_UNALIGNED_LONGWORD(ptr) \
|
|
|
|
(((((((ptr)[0] << 8) | (ptr)[1]) << 8) | (ptr)[2]) << 8) | (ptr)[3])
|
|
|
|
#define WRITE_UNALIGNED_LONGWORD(ptr, val) \
|
|
|
|
do { \
|
|
|
|
uint32_t _a = (val); \
|
|
|
|
uint8_t *_ptr = (ptr); \
|
|
|
|
_ptr[3] = _a; \
|
|
|
|
_ptr[2] = (_a >>= 8); \
|
|
|
|
_ptr[1] = (_a >>= 8); \
|
|
|
|
_ptr[0] = (_a >>= 8); \
|
|
|
|
} while(0)
|
|
|
|
|
2017-09-23 17:56:36 +00:00
|
|
|
|
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.
|
2017-09-23 17:56:36 +00:00
|
|
|
if(rState->codeHandle)
|
|
|
|
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
|
2017-09-23 17:56:36 +00:00
|
|
|
uint8_t *orig_stext, *orig_etext, *orig_sdata, *orig_edata, *orig_sbss, *orig_ebss;
|
|
|
|
|
|
|
|
GET_VIRTUAL_ADDRESS(orig_stext, _stext);
|
|
|
|
GET_VIRTUAL_ADDRESS(orig_etext, _etext);
|
|
|
|
GET_VIRTUAL_ADDRESS(orig_sdata, _sdata);
|
|
|
|
GET_VIRTUAL_ADDRESS(orig_edata, _edata);
|
|
|
|
GET_VIRTUAL_ADDRESS(orig_sbss, _sbss);
|
|
|
|
GET_VIRTUAL_ADDRESS(orig_ebss, _ebss);
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2017-09-23 17:56:36 +00:00
|
|
|
log(orig_stext);
|
|
|
|
log(orig_etext);
|
|
|
|
log(orig_sdata);
|
|
|
|
log(orig_edata);
|
|
|
|
log(orig_sbss);
|
|
|
|
log(orig_ebss);
|
|
|
|
|
2017-09-23 20:36:47 +00:00
|
|
|
uint8_t *base = orig_stext + displacement;
|
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.
|
2017-09-23 20:36:47 +00:00
|
|
|
uint32_t offsetInResource = *(uint32_t*)(base - 0x44) + 0x40;
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2017-09-23 20:36:47 +00:00
|
|
|
if(offsetInResource < 4096)
|
2015-07-20 00:02:28 +00:00
|
|
|
// 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
|
|
|
{
|
2017-09-23 20:36:47 +00:00
|
|
|
Handle h = RecoverHandle((Ptr) base - offsetInResource);
|
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;
|
2017-09-23 17:56:36 +00:00
|
|
|
}
|
2015-07-20 00:02:28 +00:00
|
|
|
}
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2017-09-23 17:56:36 +00:00
|
|
|
uint32_t text_and_data_size = orig_edata - orig_stext;
|
2017-09-23 20:36:47 +00:00
|
|
|
long bss_size = orig_ebss - orig_sbss;
|
2017-04-11 18:42:12 +00:00
|
|
|
|
2017-09-23 20:36:47 +00:00
|
|
|
//uint32_t total_size = orig_ebss - orig_stext; // FIXME: not true for repeated reloc
|
|
|
|
//assert(total_size == header->bss_end - sizeof(*header));
|
2017-09-23 17:56:36 +00:00
|
|
|
|
2017-09-23 18:11:38 +00:00
|
|
|
long bss_displacement = 0;
|
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();
|
2017-09-23 20:36:47 +00:00
|
|
|
if(!zone || base < (uint8_t*)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);
|
2017-09-23 20:36:47 +00:00
|
|
|
bss_displacement = (uint8_t*)rState->bssPtr - orig_sbss;
|
2015-07-17 19:07:03 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 18:11:38 +00:00
|
|
|
long i;
|
2015-07-21 21:16:59 +00:00
|
|
|
// Process relocation records
|
2017-09-23 18:11:38 +00:00
|
|
|
|
|
|
|
for(long *reloc = (long*)( base + text_and_data_size );
|
|
|
|
*reloc != -1;
|
|
|
|
++reloc)
|
2015-07-17 19:07:03 +00:00
|
|
|
{
|
2017-09-23 18:11:38 +00:00
|
|
|
uint8_t *addrPtr = base + *reloc;
|
2017-09-23 20:36:47 +00:00
|
|
|
uint8_t *addr;
|
2015-07-21 21:16:59 +00:00
|
|
|
|
2017-09-23 20:36:47 +00:00
|
|
|
assert(addrPtr >= base);
|
|
|
|
assert(addrPtr < base + text_and_data_size);
|
2015-07-17 19:07:03 +00:00
|
|
|
|
2017-09-23 20:36:47 +00:00
|
|
|
addr = (uint8_t*) READ_UNALIGNED_LONGWORD(addrPtr);
|
2017-09-23 17:56:36 +00:00
|
|
|
|
|
|
|
/* Check whether addresses are in range.
|
|
|
|
* This doesn't seem to work because exception handling tables
|
|
|
|
* seem to contain strange things.
|
|
|
|
*/
|
|
|
|
/*assert((uint8_t*)addr >= orig_stext); // TODO: not right for repeated reloc
|
|
|
|
assert((uint8_t*)addr <= orig_stext + total_size);*/
|
2015-07-21 21:16:59 +00:00
|
|
|
|
2017-09-23 20:36:47 +00:00
|
|
|
addr += (addr - orig_stext) >= text_and_data_size ?
|
2015-07-21 21:16:59 +00:00
|
|
|
bss_displacement : displacement;
|
|
|
|
|
2017-09-23 17:56:36 +00:00
|
|
|
/*assert((Ptr)addr >= (Ptr)base && (Ptr)addr <= (Ptr)base + text_and_data_size
|
|
|
|
|| (Ptr)addr >= rState->bssPtr && (Ptr)addr <= rState->bssPtr + bss_size);*/
|
2015-07-17 19:07:03 +00:00
|
|
|
|
2017-09-23 20:36:47 +00:00
|
|
|
WRITE_UNALIGNED_LONGWORD(addrPtr, (uint32_t) 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.
|
|
|
|
}
|
|
|
|
|
|
|
|
void Retro68CallConstructors()
|
|
|
|
{
|
2017-09-23 20:36:47 +00:00
|
|
|
uint8_t *p = &__init_section;
|
|
|
|
uint8_t *e = &__init_section_end;
|
2015-07-17 19:07:03 +00:00
|
|
|
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()
|
|
|
|
{
|
2017-09-23 20:36:47 +00:00
|
|
|
uint8_t *p = &__fini_section;
|
|
|
|
uint8_t *e = &__fini_section_end;
|
2015-08-29 18:11:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|