mirror of
https://github.com/autc04/Retro68.git
synced 2024-12-01 11:52:47 +00:00
relocate.c: reduce reliance on the FLT header (it has to go)
This commit is contained in:
parent
278780642e
commit
5059b87fe4
@ -26,10 +26,9 @@ ENTRY( _start )
|
|||||||
SECTIONS
|
SECTIONS
|
||||||
{
|
{
|
||||||
.text : {
|
.text : {
|
||||||
|
_stext = . ;
|
||||||
*/libretrocrt.a:*(.text*)
|
*/libretrocrt.a:*(.text*)
|
||||||
*/foo.o(.text*)
|
|
||||||
*(.text*)
|
*(.text*)
|
||||||
_stext = . ;
|
|
||||||
|
|
||||||
*(.stub)
|
*(.stub)
|
||||||
*(.gnu.linkonce.t*)
|
*(.gnu.linkonce.t*)
|
||||||
@ -43,15 +42,15 @@ SECTIONS
|
|||||||
__fini_section = . ;
|
__fini_section = . ;
|
||||||
KEEP (*(.fini))
|
KEEP (*(.fini))
|
||||||
__fini_section_end = . ;
|
__fini_section_end = . ;
|
||||||
_etext = . ;
|
|
||||||
|
|
||||||
*(.eh_frame_hdr)
|
*(.eh_frame_hdr)
|
||||||
KEEP(*(.eh_frame))
|
KEEP(*(.eh_frame))
|
||||||
KEEP(*(.gcc_except_table))
|
KEEP(*(.gcc_except_table))
|
||||||
KEEP(*(.gcc_except_table.*))
|
KEEP(*(.gcc_except_table.*))
|
||||||
|
. = ALIGN(0x4) ;
|
||||||
|
_etext = . ;
|
||||||
}
|
}
|
||||||
.data : {
|
.data : {
|
||||||
. = ALIGN(0x4) ;
|
|
||||||
_sdata = . ;
|
_sdata = . ;
|
||||||
*(.got.plt)
|
*(.got.plt)
|
||||||
*(.got)
|
*(.got)
|
||||||
@ -92,10 +91,11 @@ SECTIONS
|
|||||||
KEEP (*(.dtors))
|
KEEP (*(.dtors))
|
||||||
|
|
||||||
*(.tm_clone_table)
|
*(.tm_clone_table)
|
||||||
|
. = ALIGN(0x4);
|
||||||
|
_edata = . ;
|
||||||
}
|
}
|
||||||
.bss : {
|
.bss ALIGN(0x4) : {
|
||||||
_sbss = ALIGN(0x4) ;
|
_sbss = .;
|
||||||
__bss_start = . ;
|
|
||||||
*(.dynsbss)
|
*(.dynsbss)
|
||||||
*(.sbss)
|
*(.sbss)
|
||||||
*(.sbss.*)
|
*(.sbss.*)
|
||||||
|
@ -66,16 +66,18 @@ extern void __fini_section_end(void);
|
|||||||
|
|
||||||
typedef struct Retro68RelocState
|
typedef struct Retro68RelocState
|
||||||
{
|
{
|
||||||
long headerVirtualAddress;
|
Ptr bssPtr;
|
||||||
Ptr bssPtr;
|
Handle codeHandle;
|
||||||
Handle codeHandle;
|
|
||||||
} Retro68RelocState;
|
} Retro68RelocState;
|
||||||
|
extern uint8_t _stext, _etext, _sdata, _edata, _sbss, _ebss;
|
||||||
|
|
||||||
static Retro68RelocState relocState __attribute__ ((nocommon)) = {
|
static Retro68RelocState relocState __attribute__ ((nocommon, section(".text"))) = {
|
||||||
-sizeof(struct flat_hdr), NULL, NULL
|
NULL, NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
#if 1
|
/* a simple version of assert - on failure, the line number is output
|
||||||
|
* using DebugStr. */
|
||||||
|
#if 0
|
||||||
#define assert(x) do { } while(0)
|
#define assert(x) do { } while(0)
|
||||||
#else
|
#else
|
||||||
#define assert(x) do { \
|
#define assert(x) do { \
|
||||||
@ -97,6 +99,43 @@ static Retro68RelocState relocState __attribute__ ((nocommon)) = {
|
|||||||
} while(0)
|
} while(0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
#define log(x) do { } while(0)
|
||||||
|
#else
|
||||||
|
#define log(x) do { \
|
||||||
|
{\
|
||||||
|
unsigned char str[10]; \
|
||||||
|
int ___i; \
|
||||||
|
unsigned l = (x); \
|
||||||
|
for(___i = 2; ___i < 10; ___i++) \
|
||||||
|
str[___i] = ' '; \
|
||||||
|
str[0] = 9; \
|
||||||
|
str[1] = 'L'; \
|
||||||
|
str[9] = '0'; \
|
||||||
|
for(___i = 8; l && ___i > 1; ___i--) \
|
||||||
|
{ \
|
||||||
|
str[___i] = '0' + (l & 0xF); \
|
||||||
|
if((l & 0xF) >= 0xA) \
|
||||||
|
str[___i] = 'A' - 10 + (l&0xF); \
|
||||||
|
l >>= 4; \
|
||||||
|
} \
|
||||||
|
DebugStr(str); \
|
||||||
|
} \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#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)
|
||||||
|
|
||||||
|
|
||||||
void Retro68Relocate()
|
void Retro68Relocate()
|
||||||
{
|
{
|
||||||
// memory address to retrieve the ROM type (64K or a later ROM)
|
// memory address to retrieve the ROM type (64K or a later ROM)
|
||||||
@ -144,16 +183,31 @@ void Retro68Relocate()
|
|||||||
// that are invoked more than once.
|
// that are invoked more than once.
|
||||||
|
|
||||||
// Lock the code to be sure.
|
// Lock the code to be sure.
|
||||||
HLock(rState->codeHandle);
|
if(rState->codeHandle)
|
||||||
|
HLock(rState->codeHandle);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Locate the start of the FLT file header inside the code resource
|
// Locate the start of the FLT file header inside the code resource
|
||||||
long headerOldVirtualAddress = rState->headerVirtualAddress;
|
uint8_t *orig_stext, *orig_etext, *orig_sdata, *orig_edata, *orig_sbss, *orig_ebss;
|
||||||
struct flat_hdr *header = (struct flat_hdr*) (headerOldVirtualAddress + displacement);
|
|
||||||
uint8_t *base = (uint8_t*) (header+1);
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
log(orig_stext);
|
||||||
|
log(orig_etext);
|
||||||
|
log(orig_sdata);
|
||||||
|
log(orig_edata);
|
||||||
|
log(orig_sbss);
|
||||||
|
log(orig_ebss);
|
||||||
|
|
||||||
|
uint8_t *base = (uint8_t*) (orig_stext + displacement);
|
||||||
|
struct flat_hdr *header = ((struct flat_hdr*) (orig_stext + displacement)) - 1;
|
||||||
// Recover the handle to the code resource by looking at the
|
// 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
|
// longword before the FLT header. The resource templates in Retro68.r store the offset
|
||||||
// from the beginning of the code resource there.
|
// from the beginning of the code resource there.
|
||||||
@ -172,46 +226,61 @@ void Retro68Relocate()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
long bss_size = header->bss_end - header->data_end;
|
long bss_size = &_ebss - &_sbss;
|
||||||
|
|
||||||
|
|
||||||
long n = header->reloc_count;
|
long n = header->reloc_count;
|
||||||
long *relocs = (long*)( (char*)header + header->reloc_start );
|
long *relocs = (long*)( (char*)header + header->reloc_start );
|
||||||
|
|
||||||
long i;
|
long i;
|
||||||
long data_end = header->data_end + headerOldVirtualAddress;
|
uint32_t text_and_data_size = orig_edata - orig_stext;
|
||||||
uint32_t flt_size = (uint32_t) header->data_end;
|
uint32_t total_size = orig_ebss - orig_stext; // FIXME: not true for repeated reloc
|
||||||
long bss_displacement = 0;
|
long bss_displacement = 0;
|
||||||
|
|
||||||
|
assert(text_and_data_size == header->data_end - sizeof(*header));
|
||||||
|
assert((uint8_t*)relocs == base + text_and_data_size);
|
||||||
|
assert(total_size == header->bss_end - sizeof(*header));
|
||||||
|
|
||||||
// Allocate BSS section (uninitialized/zero-initialized global data)
|
// Allocate BSS section (uninitialized/zero-initialized global data)
|
||||||
if(!rState->bssPtr)
|
if(!rState->bssPtr)
|
||||||
{
|
{
|
||||||
THz zone = ApplicationZone();
|
THz zone = ApplicationZone();
|
||||||
if(!zone || (char*)header < (char*)zone)
|
if(!zone || (uint8_t*)header < (uint8_t*)zone)
|
||||||
rState->bssPtr = NewPtrSysClear(bss_size);
|
rState->bssPtr = NewPtrSysClear(bss_size);
|
||||||
else
|
else
|
||||||
rState->bssPtr = NewPtrClear(bss_size);
|
rState->bssPtr = NewPtrClear(bss_size);
|
||||||
bss_displacement = (long)(rState->bssPtr) - data_end;
|
bss_displacement = (uint8_t*)rState->bssPtr - &_sbss;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process relocation records
|
// Process relocation records
|
||||||
for(i = 0; i < n; i++)
|
for(i = 0; i < n; i++)
|
||||||
{
|
{
|
||||||
|
//Debugger();
|
||||||
uint8_t *addrPtr = base + relocs[i];
|
uint8_t *addrPtr = base + relocs[i];
|
||||||
uint32_t addr;
|
uint32_t addr;
|
||||||
|
|
||||||
assert((Ptr)addrPtr >= (Ptr)header);
|
/*log(relocs + i);
|
||||||
assert((Ptr)addrPtr < (Ptr)header + flt_size);
|
log(relocs[i]);*/
|
||||||
|
assert((Ptr)addrPtr >= (Ptr)base);
|
||||||
|
assert((Ptr)addrPtr < (Ptr)base + text_and_data_size);
|
||||||
|
|
||||||
//addr = *(uint32_t*)addrPtr;
|
//addr = *(uint32_t*)addrPtr;
|
||||||
addr = (((((addrPtr[0] << 8) | addrPtr[1]) << 8) | addrPtr[2]) << 8) | addrPtr[3];
|
addr = (((((addrPtr[0] << 8) | addrPtr[1]) << 8) | addrPtr[2]) << 8) | addrPtr[3];
|
||||||
|
|
||||||
assert(addr + 0x40 >= headerOldVirtualAddress + 0x40);
|
//log(addr);
|
||||||
assert(addr + 0x40 < headerOldVirtualAddress + header->bss_end + 0x40);
|
|
||||||
|
|
||||||
addr += (uint32_t)(addr - headerOldVirtualAddress) >= flt_size ?
|
/* 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);*/
|
||||||
|
|
||||||
|
addr += (addr - (uint32_t)orig_stext) >= text_and_data_size ?
|
||||||
bss_displacement : displacement;
|
bss_displacement : displacement;
|
||||||
|
|
||||||
assert((Ptr)addr >= (Ptr)header && (Ptr)addr < (Ptr)header + flt_size
|
/*assert((Ptr)addr >= (Ptr)base && (Ptr)addr <= (Ptr)base + text_and_data_size
|
||||||
|| (Ptr)addr >= rState->bssPtr && (Ptr)addr < rState->bssPtr + bss_size);
|
|| (Ptr)addr >= rState->bssPtr && (Ptr)addr <= rState->bssPtr + bss_size);*/
|
||||||
|
|
||||||
addrPtr[3] = addr;
|
addrPtr[3] = addr;
|
||||||
addrPtr[2] = (addr >>= 8);
|
addrPtr[2] = (addr >>= 8);
|
||||||
@ -238,7 +307,6 @@ void Retro68Relocate()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// accessing globals and calling functions is OK below here.
|
// accessing globals and calling functions is OK below here.
|
||||||
rState->headerVirtualAddress += displacement;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Retro68CallConstructors()
|
void Retro68CallConstructors()
|
||||||
|
Loading…
Reference in New Issue
Block a user