2017-09-26 09:27:53 +00:00
|
|
|
#include <OSUtils.h>
|
|
|
|
#include <Traps.h>
|
|
|
|
#include <Resources.h>
|
|
|
|
#include <Memory.h>
|
2018-04-26 22:26:23 +00:00
|
|
|
#include <Processes.h>
|
2017-09-26 09:27:53 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "PoorMansDebugging.h"
|
|
|
|
|
|
|
|
#include "Retro68Runtime.h"
|
|
|
|
|
2018-04-26 22:26:23 +00:00
|
|
|
static UniversalProcPtr OriginalLoadSeg;
|
|
|
|
static UniversalProcPtr OriginalUnloadSeg;
|
|
|
|
static UniversalProcPtr OriginalExitToShell;
|
|
|
|
static UniversalProcPtr OriginalLaunch;
|
|
|
|
static UniversalProcPtr OriginalChain;
|
2017-09-26 09:27:53 +00:00
|
|
|
|
|
|
|
extern pascal void PatchedLoadSeg();
|
|
|
|
|
|
|
|
typedef union JTEntry
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
uint16_t offset;
|
|
|
|
uint16_t push;
|
|
|
|
int16_t id;
|
|
|
|
uint16_t loadseg;
|
|
|
|
} near;
|
|
|
|
struct {
|
|
|
|
int16_t id;
|
|
|
|
uint16_t loadseg;
|
|
|
|
uint32_t offset;
|
|
|
|
} far;
|
|
|
|
struct {
|
|
|
|
int16_t id;
|
|
|
|
uint16_t jmp;
|
|
|
|
void* addr;
|
|
|
|
} jmp;
|
|
|
|
|
|
|
|
} JTEntry;
|
|
|
|
|
|
|
|
typedef struct CODEHeader
|
|
|
|
{
|
|
|
|
int16_t magic0;
|
|
|
|
int16_t magic1;
|
|
|
|
uint32_t nearEntryOffset;
|
|
|
|
uint32_t nNearEntries;
|
|
|
|
uint32_t farEntryOffset;
|
|
|
|
uint32_t nFarEntries;
|
|
|
|
uint32_t a5relocOffset;
|
|
|
|
uint8_t *currentA5;
|
|
|
|
uint32_t relocOffset;
|
|
|
|
uint8_t *loadAddress;
|
|
|
|
uint32_t reserved;
|
|
|
|
} CODEHeader;
|
|
|
|
|
2017-09-26 22:30:06 +00:00
|
|
|
#define StripAddressCompat(p) (relocState.hasStripAddr ? StripAddress(p) : StripAddress24(p))
|
2017-09-26 09:27:53 +00:00
|
|
|
|
|
|
|
pascal void* Retro68LoadSegment(uint8_t *p)
|
|
|
|
{
|
|
|
|
union JTEntry *jtEntry = (JTEntry*) (p - 4);
|
|
|
|
|
|
|
|
short id = jtEntry->far.id;
|
|
|
|
uint32_t offset = jtEntry->far.offset;
|
|
|
|
|
2017-10-07 00:02:42 +00:00
|
|
|
/*{
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf+1, "Loading seg %d.;g", id);
|
|
|
|
buf[0] = strlen(buf+1);
|
|
|
|
DebugStr((unsigned char*) buf);
|
|
|
|
}*/
|
|
|
|
|
2017-09-26 09:27:53 +00:00
|
|
|
// TODO: UseResFile?
|
|
|
|
Handle CODE = GetResource('CODE', id);
|
2018-01-08 23:28:06 +00:00
|
|
|
assert(CODE);
|
2017-09-26 09:27:53 +00:00
|
|
|
HLock(CODE);
|
|
|
|
|
2017-09-26 22:30:06 +00:00
|
|
|
uint8_t *base = StripAddressCompat((uint8_t *)*CODE);
|
2017-09-26 09:27:53 +00:00
|
|
|
CODEHeader *header = (CODEHeader*) base;
|
|
|
|
uint32_t codeSize = GetHandleSize(CODE);
|
2017-10-07 00:02:42 +00:00
|
|
|
|
2017-09-26 22:30:06 +00:00
|
|
|
uint8_t * a5 = (uint8_t*) StripAddressCompat((void*)SetCurrentA5());
|
2017-09-26 09:27:53 +00:00
|
|
|
|
|
|
|
if(header->loadAddress != base || header->currentA5 != a5)
|
|
|
|
{
|
|
|
|
long displacements[4] = {
|
|
|
|
base - header->loadAddress, // code
|
|
|
|
a5 - header->currentA5,
|
2017-10-07 00:02:42 +00:00
|
|
|
a5 - header->currentA5,
|
2017-09-26 09:27:53 +00:00
|
|
|
a5 - header->currentA5
|
|
|
|
};
|
|
|
|
|
|
|
|
header->loadAddress = base;
|
|
|
|
header->currentA5 = a5;
|
2017-10-07 00:02:42 +00:00
|
|
|
|
2017-09-26 09:27:53 +00:00
|
|
|
Handle RELA = NULL;
|
|
|
|
RELA = GetResource('RELA', id);
|
|
|
|
assert(RELA);
|
2017-10-07 00:02:42 +00:00
|
|
|
Retro68ApplyRelocations(base + 40, codeSize, *RELA, displacements);
|
|
|
|
HPurge(RELA);
|
2017-09-26 09:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update JT Entries */
|
|
|
|
// FIXME: hardcoded JT offset, there is a LM global for this somewhere:
|
|
|
|
jtEntry = (JTEntry*) (a5 + 32 + header->farEntryOffset);
|
|
|
|
int n = header->nFarEntries;
|
|
|
|
while(n--)
|
|
|
|
{
|
|
|
|
void * addr = base + jtEntry->far.offset;
|
|
|
|
//jtEntry->jmp.id = jtEntry->far.id;
|
|
|
|
jtEntry->jmp.jmp = 0x4EF9;
|
|
|
|
jtEntry->jmp.addr = addr;
|
|
|
|
++jtEntry;
|
|
|
|
}
|
|
|
|
|
2017-09-28 15:51:19 +00:00
|
|
|
if(relocState.hasFlushCodeCache)
|
2017-09-26 22:30:06 +00:00
|
|
|
FlushCodeCache();
|
2017-09-26 09:27:53 +00:00
|
|
|
|
|
|
|
/* Load Exception Information */
|
|
|
|
if (__register_frame_info)
|
|
|
|
{
|
|
|
|
int32_t offset = ((int32_t*) (base + codeSize))[-1];
|
2017-09-26 18:37:28 +00:00
|
|
|
void *eh_frame_info = base + codeSize - 4 + offset;
|
2017-09-26 09:27:53 +00:00
|
|
|
struct object *object = (struct object*) (base + codeSize - 36);
|
|
|
|
__register_frame_info(eh_frame_info, object);
|
|
|
|
}
|
|
|
|
return base + offset;
|
|
|
|
}
|
|
|
|
|
2017-10-07 00:02:42 +00:00
|
|
|
static pascal void PatchedUnloadSeg(Ptr ptr)
|
2017-09-26 09:27:53 +00:00
|
|
|
{
|
2017-10-07 00:02:42 +00:00
|
|
|
union JTEntry *jtEntry = (JTEntry*) (ptr - 2);
|
|
|
|
if(jtEntry->jmp.jmp != 0x4EF9)
|
|
|
|
return; // not loaded or invalid pointer.
|
|
|
|
|
|
|
|
int id = jtEntry->jmp.id;
|
|
|
|
Handle CODE = GetResource('CODE', id);
|
|
|
|
uint8_t *base = StripAddressCompat((uint8_t *)*CODE);
|
|
|
|
CODEHeader *header = (CODEHeader*) base;
|
|
|
|
uint32_t codeSize = GetHandleSize(CODE);
|
|
|
|
|
|
|
|
// TODO: check header->magic, pass on to original UnloadSeg?
|
|
|
|
|
|
|
|
|
|
|
|
/* Load Exception Information */
|
|
|
|
if (__register_frame_info)
|
|
|
|
{
|
|
|
|
int32_t offset = ((int32_t*) (base + codeSize))[-1];
|
|
|
|
void *eh_frame_info = base + codeSize - 4 + offset;
|
|
|
|
__deregister_frame_info(eh_frame_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t * a5 = (uint8_t*) StripAddressCompat((void*)SetCurrentA5());
|
|
|
|
|
|
|
|
// FIXME: hardcoded JT offset, there is a LM global for this somewhere:
|
|
|
|
jtEntry = (JTEntry*) (a5 + 32 + header->farEntryOffset);
|
|
|
|
int n = header->nFarEntries;
|
|
|
|
while(n--)
|
|
|
|
{
|
|
|
|
if(jtEntry->jmp.jmp == 0x4EF9 && jtEntry->jmp.id == id)
|
|
|
|
{
|
|
|
|
uint32_t offset = (uint8_t*) jtEntry->jmp.addr - base;
|
|
|
|
jtEntry->far.loadseg = _LoadSeg;
|
|
|
|
jtEntry->far.offset = offset;
|
|
|
|
}
|
|
|
|
++jtEntry;
|
|
|
|
}
|
|
|
|
if(relocState.hasFlushCodeCache)
|
|
|
|
FlushCodeCache();
|
|
|
|
|
|
|
|
HUnlock(CODE);
|
|
|
|
HPurge(CODE);
|
2017-09-26 09:27:53 +00:00
|
|
|
}
|
2018-04-26 22:26:23 +00:00
|
|
|
|
|
|
|
static void InstallPatches();
|
|
|
|
static void UninstallPatches();
|
|
|
|
|
2017-09-26 09:27:53 +00:00
|
|
|
static pascal void PatchedExitToShell()
|
2018-04-26 22:26:23 +00:00
|
|
|
{
|
|
|
|
UninstallPatches();
|
|
|
|
ExitToShell();
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma parameter __D0 Launch(__A0)
|
2018-05-07 20:40:31 +00:00
|
|
|
OSErr Launch(void *param) = { 0xA9F2 };
|
2018-04-26 22:26:23 +00:00
|
|
|
|
|
|
|
#pragma parameter __D0 Chain(__A0)
|
2018-05-07 20:40:31 +00:00
|
|
|
OSErr Chain(void *param) = { 0xA9F2 };
|
2018-04-26 22:26:23 +00:00
|
|
|
|
|
|
|
#pragma parameter __D0 PatchedLaunch(__A0)
|
2018-05-07 20:40:31 +00:00
|
|
|
static OSErr PatchedLaunch(void *p)
|
2018-04-26 22:26:23 +00:00
|
|
|
{
|
|
|
|
UninstallPatches();
|
|
|
|
OSErr err = Launch(p);
|
|
|
|
InstallPatches();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma parameter __D0 PatchedChain(__A0)
|
2018-05-07 20:40:31 +00:00
|
|
|
static OSErr PatchedChain(void *p)
|
2018-04-26 22:26:23 +00:00
|
|
|
{
|
|
|
|
UninstallPatches();
|
|
|
|
OSErr err = Chain(p);
|
|
|
|
InstallPatches();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InstallPatches()
|
|
|
|
{
|
|
|
|
SetToolTrapAddress((UniversalProcPtr)&PatchedLoadSeg, _LoadSeg);
|
|
|
|
SetToolTrapAddress((UniversalProcPtr)&PatchedUnloadSeg, _UnLoadSeg);
|
|
|
|
SetToolTrapAddress((UniversalProcPtr)&PatchedExitToShell, _ExitToShell);
|
|
|
|
SetToolTrapAddress((UniversalProcPtr)&PatchedLaunch, _Launch);
|
|
|
|
SetToolTrapAddress((UniversalProcPtr)&PatchedChain, _Chain);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UninstallPatches()
|
2017-09-26 09:27:53 +00:00
|
|
|
{
|
|
|
|
SetToolTrapAddress((UniversalProcPtr)OriginalLoadSeg, _LoadSeg);
|
|
|
|
SetToolTrapAddress((UniversalProcPtr)OriginalUnloadSeg, _UnLoadSeg);
|
|
|
|
SetToolTrapAddress((UniversalProcPtr)OriginalExitToShell, _ExitToShell);
|
2018-04-26 22:26:23 +00:00
|
|
|
SetToolTrapAddress((UniversalProcPtr)OriginalLaunch, _Launch);
|
|
|
|
SetToolTrapAddress((UniversalProcPtr)OriginalChain, _Chain);
|
2017-09-26 09:27:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 22:26:23 +00:00
|
|
|
|
2017-09-26 09:27:53 +00:00
|
|
|
// section boundaries
|
2017-10-08 18:55:24 +00:00
|
|
|
extern uint8_t _stext, _etext, _sdata, _edata, _sbss[], _ebss;
|
2017-09-26 09:27:53 +00:00
|
|
|
|
|
|
|
void Retro68InitMultisegApp()
|
|
|
|
{
|
2017-09-26 22:30:06 +00:00
|
|
|
uint8_t * a5 = (uint8_t*) StripAddressCompat((void*)SetCurrentA5());
|
2017-09-26 09:27:53 +00:00
|
|
|
|
|
|
|
// CODE Segment 1 is already loaded - we are in it.
|
|
|
|
// Update the jump table addresses.
|
|
|
|
JTEntry *jtEntry = (JTEntry*) (a5 + 32 + 16); // TODO: hardcoded offsets
|
|
|
|
while(jtEntry->far.id == 1)
|
|
|
|
{
|
|
|
|
void * addr = &_stext - 4 + jtEntry->far.offset;
|
|
|
|
//jtEntry->jmp.id = jtEntry->far.id;
|
|
|
|
jtEntry->jmp.jmp = 0x4EF9;
|
|
|
|
jtEntry->jmp.addr = addr;
|
|
|
|
++jtEntry;
|
|
|
|
}
|
2017-10-27 23:34:24 +00:00
|
|
|
if(relocState.hasFlushCodeCache)
|
|
|
|
FlushCodeCache();
|
|
|
|
|
2017-09-26 09:27:53 +00:00
|
|
|
// Zero-initialize bss
|
|
|
|
for(uint32_t *p = (uint32_t*) &_sbss;
|
|
|
|
p < (uint32_t*) &_ebss; ++p)
|
2017-10-08 18:55:24 +00:00
|
|
|
*p = 0;
|
2017-09-26 09:27:53 +00:00
|
|
|
|
|
|
|
// Set up patched LoadSeg
|
2017-10-08 18:55:24 +00:00
|
|
|
|
|
|
|
// NOTE: OriginalLoadSeg is the first global variable we can use
|
2018-04-26 22:26:23 +00:00
|
|
|
OriginalLoadSeg = GetToolTrapAddress(_LoadSeg);
|
|
|
|
OriginalUnloadSeg = GetToolTrapAddress(_UnLoadSeg);
|
|
|
|
OriginalExitToShell = GetToolTrapAddress(_ExitToShell);
|
|
|
|
OriginalLaunch = GetToolTrapAddress(_Launch);
|
|
|
|
OriginalChain = GetToolTrapAddress(_Chain);
|
|
|
|
InstallPatches();
|
2017-09-26 09:27:53 +00:00
|
|
|
|
|
|
|
// Load and relocate statically initialized DATA
|
|
|
|
Handle DATA = Get1Resource('DATA', 0);
|
|
|
|
BlockMoveData(*DATA, &_sdata, &_edata - &_sdata);
|
|
|
|
ReleaseResource(DATA);
|
|
|
|
|
|
|
|
long displacements[4] = {
|
|
|
|
0,
|
|
|
|
a5 - (uint8_t*)NULL,
|
|
|
|
a5 - (uint8_t*)NULL,
|
|
|
|
a5 - (uint8_t*)NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
Handle RELA = NULL;
|
|
|
|
RELA = GetResource('RELA', 0);
|
|
|
|
assert(RELA);
|
|
|
|
Retro68ApplyRelocations(&_sdata, &_edata - &_sdata, *RELA, displacements);
|
2017-10-07 00:02:42 +00:00
|
|
|
ReleaseResource(RELA);
|
2017-09-26 09:27:53 +00:00
|
|
|
}
|