sinclairql: startup code fixes. set the top of the new stack as stack pointer, not the bottom of it. initialize the .bss section following the new linker script. better protection against re-relocation.

git-svn-id: trunk@47429 -
This commit is contained in:
Károly Balogh 2020-11-15 22:59:33 +00:00
parent 24226ef4af
commit f2ee1dae52

View File

@ -23,12 +23,16 @@ implementation
var
stacktop: pointer;
stackorig: pointer;
setjmpbuf: jmp_buf;
stklen: longint; external name '__stklen';
binstart: pointer; external name '_stext';
binend: pointer; external name '_etext';
binstart: byte; external name '_stext';
binend: byte; external name '_etext';
bssstart: byte; external name '_sbss';
bssend: byte; external name '_ebss';
{ this is const, so it will go into the .data section, not .bss }
const
stackorig: pointer = nil;
procedure PascalMain; external name 'PASCALMAIN';
@ -46,18 +50,28 @@ begin
{ get our actual position in RAM }
lea.l binstart(pc),a0
move.l a0,d0
{ get an offset to the end of the binary. this depends on the
fact that at this point the binary is not relocated yet }
{ get an offset to the end of the binary. this works both
relocated and not. The decision if to relocate is done
later then }
lea.l binend,a1
lea.l binstart,a0
sub.l a0,a1
add.l d0,a1
move.l d0,a0
{ first item in the relocation table is the number of relocs }
move.l (a1),d7
{ read the relocation marker, this is always two padding bytes
ad the end of .text, so we're free to poke there }
move.w -2(a1),d7
beq @noreloc
{ zero out the number of relocs in RAM, so if our code is
{ zero out the number of relocation marker, so if our code is
called again, without reload, it won't relocate itself twice }
move.l #0,(a1)+
move.w #0,-2(a1)
{ first item in the relocation table is the number of relocs }
move.l (a1)+,d7
beq @noreloc
@relocloop:
{ we read the offsets and relocate them }
move.l (a1)+,d1
@ -72,13 +86,17 @@ begin
move.l a7,stackorig
end;
{ initialize .bss }
FillChar(bssstart,PtrUInt(@bssend)-PtrUInt(@bssstart),#0);
newstack:=mt_alchp(stklen,nil,-1);
if not assigned(newstack) then
_FPC_proc_start:=ERR_OM
else
begin
stacktop:=pbyte(newstack)+stklen;
asm
move.l newstack,sp
move.l stacktop,sp
end;
if setjmp(setjmpbuf) = 0 then
PascalMain;