mirror of
https://github.com/RaySollium99/picodrive.git
synced 2025-09-06 23:58:04 -04:00
initial import
git-svn-id: file:///home/notaz/opt/svn/PicoDrive@2 be3aeb3a-fb24-0410-a615-afba39da0efa
This commit is contained in:
parent
2cadbd5e56
commit
cc68a136aa
341 changed files with 180839 additions and 0 deletions
BIN
cpu/Cyclone/epoc/crash_cyclone.bin
Normal file
BIN
cpu/Cyclone/epoc/crash_cyclone.bin
Normal file
Binary file not shown.
150
cpu/Cyclone/epoc/patchtable_symb.c
Normal file
150
cpu/Cyclone/epoc/patchtable_symb.c
Normal file
|
@ -0,0 +1,150 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
typedef struct {
|
||||
unsigned long _dontcare1[4];
|
||||
char signature[4]; // 'EPOC'
|
||||
unsigned long iCpu; // 0x1000 = X86, 0x2000 = ARM, 0x4000 = M*Core
|
||||
unsigned long iCheckSumCode; // sum of all 32 bit words in .text
|
||||
unsigned long _dontcare3[5];
|
||||
unsigned long iCodeSize; // size of code, import address table, constant data and export dir |+30
|
||||
unsigned long _dontcare4[12];
|
||||
unsigned long iCodeOffset; // file offset to code section |+64
|
||||
unsigned long _dontcare5[2];
|
||||
unsigned long iCodeRelocOffset; // relocations for code and const |+70
|
||||
unsigned long iDataRelocOffset; // relocations for data
|
||||
unsigned long iPriority; // priority of this process (EPriorityHigh=450)
|
||||
} E32ImageHeader;
|
||||
|
||||
|
||||
typedef struct {
|
||||
unsigned long iSize; // size of this relocation section
|
||||
unsigned long iNumberOfRelocs; // number of relocations in this section
|
||||
} E32RelocSection;
|
||||
|
||||
|
||||
typedef struct {
|
||||
unsigned long base;
|
||||
unsigned long size;
|
||||
} reloc_page_header;
|
||||
|
||||
|
||||
// E32Image relocation section consists of a number of pages
|
||||
// every page has 8 byte header and a number or 16bit relocation entries
|
||||
// entry format:
|
||||
// 0x3000 | <12bit_reloc_offset>
|
||||
//
|
||||
// if we have page_header.base == 0x1000 and a reloc entry 0x3110,
|
||||
// it means that 32bit value at offset 0x1110 of .text section
|
||||
// is relocatable
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE *f = 0;
|
||||
unsigned char pattern[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56 };
|
||||
unsigned char *buff, *p;
|
||||
unsigned long patt_offset; // pattern offset in .text section
|
||||
unsigned long size = 0, i, symbols, insert_pos, *handler;
|
||||
unsigned short reloc_entry;
|
||||
E32ImageHeader *header;
|
||||
E32RelocSection *reloc_section;
|
||||
reloc_page_header *reloc_page;
|
||||
|
||||
if(argc != 3) {
|
||||
printf("usage: %s <e32_exe> <nsymbols>\n\n", argv[0]);
|
||||
printf("note: this was written to fix a problem caused by as v.2.9-psion-98r2 and shouldn't be used for anything else.\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
f = fopen(argv[1], "rb+");
|
||||
if(!f) {
|
||||
printf("%s: couldn't open %s\n", argv[0], argv[1]);
|
||||
return 2;
|
||||
}
|
||||
|
||||
symbols = atoi(argv[2]);
|
||||
|
||||
// read the file
|
||||
fseek(f,0,SEEK_END); size=ftell(f); fseek(f,0,SEEK_SET);
|
||||
buff = (unsigned char *) malloc(size);
|
||||
fread(buff,1,size,f);
|
||||
|
||||
header = (E32ImageHeader *) buff;
|
||||
|
||||
if(strncmp(header->signature, "EPOC", 4) || header->iCpu != 0x2000) {
|
||||
printf("%s: not a E32 executable image for ARM target.\n", argv[0]);
|
||||
fclose(f);
|
||||
free(buff);
|
||||
return 2;
|
||||
}
|
||||
|
||||
// find the pattern
|
||||
for(i = 0; i < size-8; i++)
|
||||
if(memcmp(buff+i, pattern, 8) == 0) break;
|
||||
if(i == size-8 || i < 4) {
|
||||
printf("%s: failed to find the pattern.\n", argv[0]);
|
||||
fclose(f);
|
||||
free(buff);
|
||||
return 3;
|
||||
}
|
||||
patt_offset = i - header->iCodeOffset;
|
||||
|
||||
// find suitable reloc section
|
||||
reloc_section = (E32RelocSection *) (buff + header->iCodeRelocOffset);
|
||||
for(i = 0, p = buff+header->iCodeRelocOffset+8; i < reloc_section->iSize; ) {
|
||||
reloc_page = (reloc_page_header *) p;
|
||||
if(patt_offset - reloc_page->base >= 0 && patt_offset - reloc_page->base < 0x1000 - symbols*4) break;
|
||||
i += reloc_page->size;
|
||||
p += reloc_page->size;
|
||||
}
|
||||
|
||||
if(i >= reloc_section->iSize) {
|
||||
printf("%s: suitable reloc section not found.\n", argv[0]);
|
||||
fclose(f);
|
||||
free(buff);
|
||||
return 4;
|
||||
}
|
||||
|
||||
// now find the insert pos and update everything
|
||||
insert_pos = p + reloc_page->size - buff;
|
||||
reloc_page->size += symbols*2;
|
||||
reloc_section->iSize += symbols*2;
|
||||
reloc_section->iNumberOfRelocs += symbols;
|
||||
header->iDataRelocOffset += symbols*2; // data reloc section is now also pushed a little
|
||||
header->iPriority = 450; // let's boost our priority :)
|
||||
|
||||
// replace the placeholders themselves
|
||||
handler = (unsigned long *) (buff + patt_offset + header->iCodeOffset - 4);
|
||||
for(i = 1; i <= symbols; i++)
|
||||
*(handler+i) = *handler;
|
||||
|
||||
// recalculate checksum
|
||||
header->iCheckSumCode = 0;
|
||||
for(i = 0, p = buff+header->iCodeOffset; i < header->iCodeSize; i+=4, p+=4)
|
||||
header->iCheckSumCode += *(unsigned long *) p;
|
||||
|
||||
// check for possible padding
|
||||
if(!*(buff+insert_pos-1)) insert_pos -= 2;
|
||||
|
||||
// write all this joy
|
||||
fseek(f,0,SEEK_SET);
|
||||
fwrite(buff, 1, insert_pos, f);
|
||||
|
||||
// write new reloc entries
|
||||
for(i = 0; i < symbols; i++) {
|
||||
handler++;
|
||||
reloc_entry = ((unsigned char *) handler - buff - reloc_page->base - header->iCodeOffset) | 0x3000;
|
||||
fwrite(&reloc_entry, 1, 2, f);
|
||||
}
|
||||
|
||||
// write the remaining data
|
||||
fwrite(buff+insert_pos, 1, size-insert_pos, f);
|
||||
|
||||
// done at last!
|
||||
fclose(f);
|
||||
free(buff);
|
||||
|
||||
return 0;
|
||||
}
|
133
cpu/Cyclone/epoc/patchtable_symb2.c
Normal file
133
cpu/Cyclone/epoc/patchtable_symb2.c
Normal file
|
@ -0,0 +1,133 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#define symbols 2
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE *f = 0;
|
||||
unsigned char pattern[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56 };
|
||||
unsigned char *buff, *p;
|
||||
unsigned long patt_offset; // pattern offset in .text section
|
||||
unsigned long size = 0, i, insert_pos, *handler;//, symbols;
|
||||
unsigned short reloc_entry;
|
||||
IMAGE_BASE_RELOCATION *reloc_page;
|
||||
IMAGE_DOS_HEADER *dos_header;
|
||||
IMAGE_FILE_HEADER *file_header;
|
||||
IMAGE_SECTION_HEADER *sect_header, *relocsect_header = 0, *codesect_header = 0;
|
||||
|
||||
if(argc != 2) {
|
||||
printf("usage: %s <pe_exe_or_app_before_petran>\n\n", argv[0]);
|
||||
printf("note: this was written to fix a problem related to Cyclone and as v.2.9-psion-98r2 and shouldn't be used for anything else. See Readme.\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
f = fopen(argv[1], "rb+");
|
||||
if(!f) {
|
||||
printf("%s: couldn't open %s\n", argv[0], argv[1]);
|
||||
return 2;
|
||||
}
|
||||
|
||||
//symbols = atoi(argv[2]);
|
||||
|
||||
// read the file
|
||||
fseek(f,0,SEEK_END); size=ftell(f); fseek(f,0,SEEK_SET);
|
||||
buff = (unsigned char *) malloc(size);
|
||||
fread(buff,1,size,f);
|
||||
|
||||
dos_header = (IMAGE_DOS_HEADER *) buff;
|
||||
file_header= (IMAGE_FILE_HEADER *) (buff+dos_header->e_lfanew+4);
|
||||
sect_header= (IMAGE_SECTION_HEADER *) (buff+dos_header->e_lfanew+4+sizeof(IMAGE_FILE_HEADER)+sizeof(IMAGE_OPTIONAL_HEADER32));
|
||||
|
||||
if(size < 0x500 || dos_header->e_magic != IMAGE_DOS_SIGNATURE ||
|
||||
*(DWORD *)(buff+dos_header->e_lfanew) != IMAGE_NT_SIGNATURE || file_header->Machine != 0x0A00) {
|
||||
printf("%s: not a PE executable image for ARM target.\n", argv[0]);
|
||||
fclose(f);
|
||||
free(buff);
|
||||
return 2;
|
||||
}
|
||||
|
||||
// scan all sections for data and reloc sections
|
||||
for(i = 0; i < file_header->NumberOfSections; i++, sect_header++) {
|
||||
if(strncmp(sect_header->Name, ".text", 5) == 0) codesect_header = sect_header;
|
||||
else if(strncmp(sect_header->Name, ".reloc", 6) == 0) relocsect_header = sect_header;
|
||||
}
|
||||
|
||||
if(!codesect_header || !relocsect_header) {
|
||||
printf("%s: failed to find reloc and/or data section.\n", argv[0]);
|
||||
fclose(f);
|
||||
free(buff);
|
||||
return 3;
|
||||
}
|
||||
|
||||
if(relocsect_header != sect_header-1) {
|
||||
printf("%s: bug: reloc section is not last, this is unexpected and not supported.\n", argv[0]);
|
||||
fclose(f);
|
||||
free(buff);
|
||||
return 4;
|
||||
}
|
||||
|
||||
// find the pattern
|
||||
for(i = codesect_header->PointerToRawData; i < size-8; i+=2)
|
||||
if(memcmp(buff+i, pattern, 8) == 0) break;
|
||||
if(i == size-8 || i < 4) {
|
||||
printf("%s: failed to find the pattern.\n", argv[0]);
|
||||
fclose(f);
|
||||
free(buff);
|
||||
return 5;
|
||||
}
|
||||
|
||||
// calculate pattern offset in RVA (relative virtual address)
|
||||
patt_offset = i - codesect_header->PointerToRawData + codesect_header->VirtualAddress;
|
||||
|
||||
// replace the placeholders themselves
|
||||
handler = (unsigned long *) (buff + i - 4);
|
||||
for(i = 1; i <= symbols; i++)
|
||||
*(handler+i) = *handler;
|
||||
|
||||
// find suitable reloc section
|
||||
for(i = 0, p = buff+relocsect_header->PointerToRawData; i < relocsect_header->SizeOfRawData; ) {
|
||||
reloc_page = (IMAGE_BASE_RELOCATION *) p;
|
||||
if(patt_offset - reloc_page->VirtualAddress >= 0 && patt_offset - reloc_page->VirtualAddress < 0x1000 - symbols*2) break;
|
||||
i += reloc_page->SizeOfBlock;
|
||||
p += reloc_page->SizeOfBlock;
|
||||
}
|
||||
|
||||
if(i >= relocsect_header->SizeOfRawData) {
|
||||
printf("%s: suitable reloc section not found.\n", argv[0]);
|
||||
fclose(f);
|
||||
free(buff);
|
||||
return 6;
|
||||
}
|
||||
|
||||
// now find the insert pos and update everything
|
||||
insert_pos = p + reloc_page->SizeOfBlock - buff;
|
||||
reloc_page->SizeOfBlock += symbols*2;
|
||||
relocsect_header->SizeOfRawData += symbols*2;
|
||||
|
||||
// check for possible padding
|
||||
if(!*(buff+insert_pos-1)) insert_pos -= 2;
|
||||
|
||||
// write all this joy
|
||||
fseek(f,0,SEEK_SET);
|
||||
fwrite(buff, 1, insert_pos, f);
|
||||
|
||||
// write new reloc entries
|
||||
for(i = 0; i < symbols; i++) {
|
||||
handler++;
|
||||
reloc_entry = (unsigned short)(((unsigned char *) handler - buff) - reloc_page->VirtualAddress - codesect_header->PointerToRawData + codesect_header->VirtualAddress) | 0x3000; // quite nasty
|
||||
fwrite(&reloc_entry, 1, 2, f);
|
||||
}
|
||||
|
||||
// write the remaining data
|
||||
fwrite(buff+insert_pos, 1, size-insert_pos, f);
|
||||
|
||||
// done at last!
|
||||
fclose(f);
|
||||
free(buff);
|
||||
|
||||
return 0;
|
||||
}
|
42
cpu/Cyclone/epoc/readme.txt
Normal file
42
cpu/Cyclone/epoc/readme.txt
Normal file
|
@ -0,0 +1,42 @@
|
|||
*update*
|
||||
Use the "compress jumtable" Cyclone config.h option to fix this issue
|
||||
(no patcher will be needed then).
|
||||
|
||||
|
||||
There is a problem with Cyclone on symbian platform:
|
||||
GNU as generates COFF object files, which allow max of 0xFFFF (65535) relocation
|
||||
entries. Cyclone uses a jumptable of 0x10000 (65536, 1 for every opcode-word)
|
||||
antries. When the executable is loaded, all jumptable entries must be relocated
|
||||
to point to right code location. Because of this limitation, Cyclone's jumptable is
|
||||
incomplete (misses 2 entries), and if M68k opcodes 0xFFFE or 0xFFFF are ever
|
||||
encoundered when emulating, your emulator will crash.
|
||||
|
||||
I have written a little patcher to fix that. It writes those two missing entries and
|
||||
marks them as relocatable. Placeholders must not be deleted just after the jumttable
|
||||
in the Cyclone source code.
|
||||
|
||||
This version works with intermediate PE executable, which is used both for APPs and EXEs,
|
||||
and is produced by gcc toolkit just before running petran. So it's best to insert
|
||||
this in your makefile, in the rule which builds your APP/EXE, just after last 'ld'
|
||||
statement, for example:
|
||||
|
||||
$(EPOCTRGUREL)\PICODRIVEN.APP : $(EPOCBLDUREL)\PICODRIVEN.in $(EPOCSTATLINKUREL)\EDLL.LIB $(LIBSUREL)
|
||||
...
|
||||
ld -s -e _E32Dll -u _E32Dll --dll \
|
||||
"$(EPOCBLDUREL)\PICODRIVEN.exp" \
|
||||
-Map "$(EPOCTRGUREL)\PICODRIVEN.APP.map" -o "$(EPOCBLDUREL)\PICODRIVEN.APP" \
|
||||
"$(EPOCSTATLINKUREL)\EDLL.LIB" --whole-archive "$(EPOCBLDUREL)\PICODRIVEN.in" \
|
||||
--no-whole-archive $(LIBSUREL) $(USERLDFLAGS)
|
||||
-$(ERASE) "$(EPOCBLDUREL)\PICODRIVEN.exp"
|
||||
|
||||
patchtable_symb2 "$(EPOCBLDUREL)\PICODRIVEN.APP"
|
||||
|
||||
petran "$(EPOCBLDUREL)\PICODRIVEN.APP" "$@" \
|
||||
-nocall -uid1 0x10000079 -uid2 0x100039ce -uid3 0x1000c193
|
||||
-$(ERASE) "$(EPOCBLDUREL)\PICODRIVEN.APP"
|
||||
perl -S ecopyfile.pl "$@" "PICODRIVEN.APP"
|
||||
|
||||
|
||||
This is also compatible with ECompXL.
|
||||
|
||||
To test if this thing worked, you can load crash_cyclone.bin in your emulator.
|
Loading…
Add table
Add a link
Reference in a new issue