;
; xcommon.s
;
; Common stuff for application and module stubs
;
;  1993-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's C library stubs (xstubs).
;
; xstubs 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 2, or (at your option)
; any later version.
;
; xstubs 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.
;
; You should have received a copy of the GNU General Public License
; along with xstubs.  If not, write to the Free Software Foundation,
; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;----- Standard header ------------------------------------------------------

		GET	libs:header

;----- External dependencies ------------------------------------------------

		IMPORT	|_clib_initialise|
		IMPORT	|_main|
		IMPORT  TrapHandler
		IMPORT  UncaughtTrapHandler
		IMPORT  EventHandler
		IMPORT  UnhandledEventHandler
		IMPORT	|_kernel_command_string|
		IMPORT  |_stub_kallocExtendsWS|
		IMPORT	|C$$code$$Base|
		IMPORT	|C$$code$$Limit|
		IMPORT	main,WEAK
		IMPORT	|DLL$$ExternalTable$$Base|,WEAK
		IMPORT	|DLL$$ExternalTable$$Limit|,WEAK
		IMPORT	|_dll_findall|,WEAK
		IMPORT	|_dll_appspace|,WEAK
		IMPORT	|_dll_clibdata|,WEAK
		IMPORT	|_dll_appEntryStubs|,WEAK
		IMPORT	|_dll_appEntryNames|,WEAK
		IMPORT	|_dll_regAppEntry|,WEAK

;----- Run-time support information -----------------------------------------

		AREA	|RTSK$$Data|,CODE,READONLY

xstub__startDef	DCD	xstub__endDef-xstub__startDef
		DCD	|C$$code$$Base|
		DCD	|C$$code$$Limit|
		DCD	xstub__c
		DCD	xstub__initC
		DCD	0
		DCD	TrapHandler
		DCD	UncaughtTrapHandler
		DCD	EventHandler
		DCD	UnhandledEventHandler
xstub__endDef

;----- C library initialisation ---------------------------------------------

		AREA	|C$$code|,CODE,READONLY

		; --- Magic numbers ---

xstub__c	DCB	"C",0			;Language name
		ALIGN

xstub__initC	LDR	a1,=|_stub_kallocExtendsWS| ;Get flag offset
		LDR	ip,[sl,#-536]		;Get client reloc
		ADD	a1,ip,a1		;Relocate the offset
		MOV	a2,#1			;Set the flag
		STRB	a2,[a1]			;Yup.  Tha's it
		STMFD	sp!,{lr}		;Preserve link register
		BL	|_clib_initialise|	;Initialise C library
		LDR	a1,=main		;Try and find main routine
		CMP	a1,#0			;Was it found?
		ADRNE	a1,xstub__goGoGo	;Insert own initialisation
		LDMFD	sp!,{pc}^

		LTORG

xstub__goGoGo	MOV	ip,sp			;Standard stack frame stuff
		STMFD	sp!,{fp,ip,lr,pc}
		SUB	fp,ip,#4

		; --- DLL initialisation stuff ---
		;
		; Find start and end of external DLL table.  If they're
		; equal, there's no table, and we don't care if the routine
		; to load them exists or not :-)

		LDR	a1,=|DLL$$ExternalTable$$Base|
		LDR	a2,=|DLL$$ExternalTable$$Limit|
		CMP	a1,a2
		BEQ	appentry
		BL	|_dll_findall|
		BL	|_dll_appspace|
		BL	|_dll_clibdata|

		; Next, see if we can register the application as having
		; an external entry table.  If so, do it.

appentry	LDR	a1,=|_dll_appEntryStubs|
		LDR	a2,=|_dll_appEntryNames|
		CMP	a1,a2
		BLNE	|_dll_regAppEntry|

		; --- Start C program from main ---

nodlls		BL	|_kernel_command_string| ;Find the command line
		LDR	R1,=main		;Find user's main program
		BL	|_main|			;Call it with correct args
		LDMDB	fp,{fp,sp,pc}^

		LTORG

;----- That's all for this one ----------------------------------------------

		END
