;
; findall.s
;
; Load an application's DLLs from the stub table
;
;  1994 Straylight
;

;----- Standard stuff -------------------------------------------------------

		GET	libs:header
		GET	libs:swis

;----- Other external dependencies ------------------------------------------

		IMPORT	|x$stack_overflow|
		IMPORT	atexit
		IMPORT	malloc
		IMPORT	|_dll_giveMemory|

;----- Main code ------------------------------------------------------------

		AREA	|DLL$$Code|,CODE,READONLY

; --- _dll_findall ---
;
; On entry:	a1 == DLL$$ExternalTable$$Base
; 		a2 == DLL$$ExternalTable$$Limit
; On exit:	--

		EXPORT	|_dll_findall|
|_dll_findall|
		ROUT

		; --- APCS procedure entry ---

		MOV	ip,sp
		STMFD	sp!,{fp,ip,lr,pc}
		SUB	fp,ip,#4
		CMP	sp,sl
		BLLT	|x$stack_overflow|

		; --- Find the DLLs in the table ---

		SWI	DLL_FindFromTable	;Load DLLs in the app's table

		; --- Make sure the DLLs are freed at exit time ---

		LDR	a1,=|_dll_dying|	;Point to tidy up routine
		BL	atexit			;Add tidy-up routine to list

		; --- Give the DLLs some application space ---

		LDMDB	fp,{fp,sp,lr}		;Find registers again
		B	|_dll_giveMemory|	;Tail-call optimisation :-)

		LTORG

; --- _dll_dying ---

|_dll_dying|	ROUT

		MOV	ip,lr			;Keep link register safely
		SWI	DLL_AppDying		;Close down the application
		MOVS	pc,ip			;Return to caller

		LTORG

;----- That's all, folks ----------------------------------------------------

		END
