#---------------------------------------------------------------------------
#  Project:  TwlSDK - tools - makelcf
#  File:     ARM9-TS.lcf.template
#
#  Copyright 2003-2008 Nintendo.  All rights reserved.
#
#  These coded instructions, statements, and computer programs contain
#  proprietary information of Nintendo of America Inc. and/or Nintendo
#  Company Ltd., and are protected by Federal copyright law.  They may
#  not be disclosed to third parties or copied or duplicated in any form,
#  in whole or in part, without the prior written consent of Nintendo.
#
#  $Date:: 2008-09-18#$
#  $Rev: 8573 $
#  $Author: okubata_ryoma $
#---------------------------------------------------------------------------
MEMORY
{
	main		(RWX) : ORIGIN = 0x02000000,   LENGTH = 0x0 >  main.sbin
	binary.AUTOLOAD_INFO		(RWX) : ORIGIN = 0, LENGTH = 0x0 >> main.sbin
	binary.STATIC_FOOTER		(RWX) : ORIGIN = 0, LENGTH = 0x0 >> main.sbin
	
	main_defs	(RW)  : ORIGIN = AFTER(main), LENGTH = 0x0 > main_defs.sbin
	main_table	(RW)  : ORIGIN = AFTER(main), LENGTH = 0x0 > main_table.sbin
	overlay_0		(RWXO): ORIGIN = 0x03f00000, LENGTH = 0x0 > overlay_0.sbin
	overlay_1		(RWXO): ORIGIN = AFTER(main), LENGTH = 0x0 > overlay_1.sbin
	overlay_2		(RWXO): ORIGIN = AFTER(overlay_1), LENGTH = 0x0 > overlay_2.sbin
	dummy.MAIN_EX		(RW)  : ORIGIN = 0x023e0000, LENGTH = 0x0
	arena.MAIN			(RW)  : ORIGIN = AFTER(main,overlay_1,overlay_2), LENGTH = 0x0
	arena.MAIN_EX		(RW)  : ORIGIN = AFTER(dummy.MAIN_EX), LENGTH = 0x0
	arena.ITCM		(RW)  : ORIGIN = AFTER(ITCM), LENGTH = 0x0
	arena.DTCM		(RW)  : ORIGIN = AFTER(DTCM), LENGTH = 0x0
	binary.MODULE_FILES		(RW)  : ORIGIN = 0x0, LENGTH = 0x0     > component.files
	check.ITCM			(RWX) : ORIGIN = 0x0, LENGTH = 0x08000 > itcm.check
	check.DTCM			(RW)  : ORIGIN = 0x0, LENGTH = 0x04000 > dtcm.check
}

FORCE_ACTIVE
{
	SVC_SoftReset
}

KEEP_SECTION
{
	.sinit
}

SECTIONS
{
	############################ STATIC #################################
	.main:
	{
	ALIGNALL(4);			. = ALIGN(32);	# Fit to cache line
	
	#
	#  TEXT BLOCK: READ ONLY
	#
	SDK_STATIC_START		=.;
	SDK_STATIC_TEXT_START		=.;
					#:::::::::: text/rodata
					libsyscall.a (.text)
					crt0.o (.text)
					crt0.o (.rodata)
					* (.version)
					OBJECT(NitroMain,*)
					obj_a.o (.text)
					obj_b.o (.text)
					obj_c.o (.text)
					. = ALIGN(4);
					* (.exception)
					. = ALIGN(4);
	SDK_STATIC_ETABLE_START		=.;
					EXCEPTION
	SDK_STATIC_ETABLE_END		=.;
					. = ALIGN(4);
					. = ALIGN(4);
					. = ALIGN(4);

	SDK_STATIC_SINIT_START		=.;
					#:::::::::: ctor
					WRITEW	0;
					#:::::::::: ctor
	SDK_STATIC_SINIT_END		=.;

					#:::::::::: text/rodata
					. = ALIGN(32);
	SDK_STATIC_TEXT_END		=.;
	
	#
	#  DATA BLOCK: READ WRITE
	#
	SDK_STATIC_DATA_START		=.;
					#:::::::::: data
					. = ALIGN(4);
					. = ALIGN(4);
	SDK_OVERLAY_DIGEST		=.;
					# NO DIGEST
	SDK_OVERLAY_DIGEST_END		=.;
					#:::::::::: data
					. = ALIGN(32);
	SDK_STATIC_DATA_END		=.;
	SDK_STATIC_END			=.;

	SDK_STATIC_TEXT_SIZE		= SDK_STATIC_TEXT_END - SDK_STATIC_TEXT_START;
	SDK_STATIC_DATA_SIZE		= SDK_STATIC_DATA_END - SDK_STATIC_DATA_START;
	SDK_STATIC_SIZE			= SDK_STATIC_END      - SDK_STATIC_START;
	__sinit__			= SDK_STATIC_SINIT_START;	# for static initializer
	__exception_table_start__	= SDK_STATIC_ETABLE_START;	# for exception table
	__exception_table_end__		= SDK_STATIC_ETABLE_END;	# for exception table
	} > main

	.main.bss:
	{
	ALIGNALL(4);			. = ALIGN(32);

	#
	#  BSS BLOCK
	#
	SDK_STATIC_BSS_START		=.;
					#:::::::::: bss
					. = ALIGN(4);
					. = ALIGN(4);
					#:::::::::: bss
					. = ALIGN(32);
	SDK_STATIC_BSS_END		= .;
	SDK_STATIC_BSS_SIZE		= SDK_STATIC_BSS_END - SDK_STATIC_BSS_START;

	} >> main
	
	
	############################ AUTOLOADS ##############################
	SDK_AUTOLOAD.ITCM.START			= 0x01ff8000;
	SDK_AUTOLOAD.ITCM.END			= SDK_AUTOLOAD.ITCM.START;
	SDK_AUTOLOAD.ITCM.BSS_END		= SDK_AUTOLOAD.ITCM.START;
	SDK_AUTOLOAD.ITCM.SIZE			= 0;
	SDK_AUTOLOAD.ITCM.BSS_SIZE		= 0;
	SDK_AUTOLOAD.DTCM.START			= 0x027e0000;
	SDK_AUTOLOAD.DTCM.END			= SDK_AUTOLOAD.DTCM.START;
	SDK_AUTOLOAD.DTCM.BSS_END		= SDK_AUTOLOAD.DTCM.START;
	SDK_AUTOLOAD.DTCM.SIZE			= 0;
	SDK_AUTOLOAD.DTCM.BSS_SIZE		= 0;
	SDK_AUTOLOAD_START			= SDK_STATIC_END;
	SDK_AUTOLOAD_SIZE			= 0;
	SDK_AUTOLOAD_NUMBER			= 0;


	SDK_AUTOLOAD_ITCM_START			= SDK_AUTOLOAD.ITCM.START;
	SDK_AUTOLOAD_ITCM_END			= SDK_AUTOLOAD.ITCM.END;
	SDK_AUTOLOAD_ITCM_BSS_END		= SDK_AUTOLOAD.ITCM.BSS_END;
	SDK_AUTOLOAD_ITCM_SIZE			= SDK_AUTOLOAD.ITCM.SIZE;
	SDK_AUTOLOAD_ITCM_BSS_SIZE		= SDK_AUTOLOAD.ITCM.BSS_SIZE;
	SDK_AUTOLOAD_DTCM_START			= SDK_AUTOLOAD.DTCM.START;
	SDK_AUTOLOAD_DTCM_END			= SDK_AUTOLOAD.DTCM.END;
	SDK_AUTOLOAD_DTCM_BSS_END		= SDK_AUTOLOAD.DTCM.BSS_END;
	SDK_AUTOLOAD_DTCM_SIZE			= SDK_AUTOLOAD.DTCM.SIZE;
	SDK_AUTOLOAD_DTCM_BSS_SIZE		= SDK_AUTOLOAD.DTCM.BSS_SIZE;

	############################ AUTOLOAD_INFO ##########################
	.binary.AUTOLOAD_INFO:
	{
	} > binary.AUTOLOAD_INFO
	
	SDK_AUTOLOAD_LIST			= SDK_AUTOLOAD_START + SDK_AUTOLOAD_SIZE;
	SDK_AUTOLOAD_LIST_END			= SDK_AUTOLOAD_START + SDK_AUTOLOAD_SIZE + SIZEOF(.binary.AUTOLOAD_INFO);
	SDK_AUTOLOAD_SIZE			= SDK_AUTOLOAD_SIZE  + SIZEOF(.binary.AUTOLOAD_INFO);
	
	############################ STATIC_FOOTER ##########################
	.binary.STATIC_FOOTER:
	{
		WRITEW 0xdec00621;		# LE(0x2106C0DE) = NITRO CODE
		WRITEW _start_ModuleParams - ADDR(.main);
		WRITEW 0;			# NO DIGEST
	} > binary.STATIC_FOOTER
	
	############################ OVERLAYS ###############################
	SDK_OVERLAY_NUMBER			= 3;

	.overlay_0:
	{
	ALIGNALL(4);				. = ALIGN(32);
	
	#
	#   TEXT BLOCK: READ ONLY
	#
	SDK_OVERLAY_overlay_0_ID		=0;	### SEGMENT overlay_0 OVERLAY ID
	SDK_OVERLAY.overlay_0.ID		=0;
	SDK_OVERLAY.overlay_0.START	=.;
	SDK_OVERLAY.overlay_0.TEXT_START	=.;
						#:::::::::: text/rodata
						aaa.o (.text)
						bbb.o (.text)
						. = ALIGN(4);
						aaa.o (.rodata)
						bbb.o (.rodata)
						. = ALIGN(4);
						aaa.o (.init)
						bbb.o (.init)
						. = ALIGN(4);
	SDK_OVERLAY.overlay_0.SINIT_START	=.;
						#:::::::::: ctor
						aaa.o (.ctor)
						bbb.o (.ctor)
						aaa.o (.sinit)
						bbb.o (.sinit)
						WRITEW	0;
						#:::::::::: ctor
	SDK_OVERLAY.overlay_0.SINIT_END	=.;

						#:::::::::: text/rodata
						. = ALIGN(32);
	SDK_OVERLAY.overlay_0.TEXT_END	=.;

	#
	#  DATA BLOCK: READ WRITE
	#
	SDK_OVERLAY.overlay_0.DATA_START	=.;
						#:::::::::: data
						aaa.o (.sdata)
						bbb.o (.sdata)
						. = ALIGN(4);
						aaa.o (.data)
						bbb.o (.data)
						. = ALIGN(4);
						#:::::::::: data
						. = ALIGN(32);
	SDK_OVERLAY.overlay_0.DATA_END	=.;
	SDK_OVERLAY.overlay_0.END		=.;

	SDK_OVERLAY.overlay_0.TEXT_SIZE	= SDK_OVERLAY.overlay_0.TEXT_END - SDK_OVERLAY.overlay_0.TEXT_START;
	SDK_OVERLAY.overlay_0.DATA_SIZE	= SDK_OVERLAY.overlay_0.DATA_END - SDK_OVERLAY.overlay_0.DATA_START;
	SDK_OVERLAY.overlay_0.SIZE		= SDK_OVERLAY.overlay_0.END      - SDK_OVERLAY.overlay_0.START;

	} > overlay_0

	.overlay_0.bss:
	{
	ALIGNALL(4);				. = ALIGN(32);
	
	#
	#  BSS BLOCK
	#
	SDK_OVERLAY.overlay_0.BSS_START	= .;
						#:::::::::: bss
						aaa.o (.bss)
						bbb.o (.bss)
						. = ALIGN(4);
						aaa.o (.sbss)
						bbb.o (.sbss)
						. = ALIGN(4);
						#:::::::::: bss
						. = ALIGN(32);
	SDK_OVERLAY.overlay_0.BSS_END	= .;

	SDK_OVERLAY.overlay_0.BSS_SIZE	= SDK_OVERLAY.overlay_0.BSS_END - SDK_OVERLAY.overlay_0.BSS_START;
	
	} >> overlay_0

	.overlay_1:
	{
	ALIGNALL(4);				. = ALIGN(32);
	
	#
	#   TEXT BLOCK: READ ONLY
	#
	SDK_OVERLAY_overlay_1_ID		=1;	### SEGMENT overlay_1 OVERLAY ID
	SDK_OVERLAY.overlay_1.ID		=1;
	SDK_OVERLAY.overlay_1.START	=.;
	SDK_OVERLAY.overlay_1.TEXT_START	=.;
						#:::::::::: text/rodata
						ccc.o (.text)
						ddd.o (.text)
						. = ALIGN(4);
						ccc.o (.rodata)
						ddd.o (.rodata)
						. = ALIGN(4);
						ccc.o (.init)
						ddd.o (.init)
						. = ALIGN(4);
	SDK_OVERLAY.overlay_1.SINIT_START	=.;
						#:::::::::: ctor
						ccc.o (.ctor)
						ddd.o (.ctor)
						ccc.o (.sinit)
						ddd.o (.sinit)
						WRITEW	0;
						#:::::::::: ctor
	SDK_OVERLAY.overlay_1.SINIT_END	=.;

						#:::::::::: text/rodata
						. = ALIGN(32);
	SDK_OVERLAY.overlay_1.TEXT_END	=.;

	#
	#  DATA BLOCK: READ WRITE
	#
	SDK_OVERLAY.overlay_1.DATA_START	=.;
						#:::::::::: data
						ccc.o (.sdata)
						ddd.o (.sdata)
						. = ALIGN(4);
						ccc.o (.data)
						ddd.o (.data)
						. = ALIGN(4);
						#:::::::::: data
						. = ALIGN(32);
	SDK_OVERLAY.overlay_1.DATA_END	=.;
	SDK_OVERLAY.overlay_1.END		=.;

	SDK_OVERLAY.overlay_1.TEXT_SIZE	= SDK_OVERLAY.overlay_1.TEXT_END - SDK_OVERLAY.overlay_1.TEXT_START;
	SDK_OVERLAY.overlay_1.DATA_SIZE	= SDK_OVERLAY.overlay_1.DATA_END - SDK_OVERLAY.overlay_1.DATA_START;
	SDK_OVERLAY.overlay_1.SIZE		= SDK_OVERLAY.overlay_1.END      - SDK_OVERLAY.overlay_1.START;

	} > overlay_1

	.overlay_1.bss:
	{
	ALIGNALL(4);				. = ALIGN(32);
	
	#
	#  BSS BLOCK
	#
	SDK_OVERLAY.overlay_1.BSS_START	= .;
						#:::::::::: bss
						ccc.o (.bss)
						ddd.o (.bss)
						. = ALIGN(4);
						ccc.o (.sbss)
						ddd.o (.sbss)
						. = ALIGN(4);
						#:::::::::: bss
						. = ALIGN(32);
	SDK_OVERLAY.overlay_1.BSS_END	= .;

	SDK_OVERLAY.overlay_1.BSS_SIZE	= SDK_OVERLAY.overlay_1.BSS_END - SDK_OVERLAY.overlay_1.BSS_START;
	
	} >> overlay_1

	.overlay_2:
	{
	ALIGNALL(4);				. = ALIGN(32);
	
	#
	#   TEXT BLOCK: READ ONLY
	#
	SDK_OVERLAY_overlay_2_ID		=2;	### SEGMENT overlay_2 OVERLAY ID
	SDK_OVERLAY.overlay_2.ID		=2;
	SDK_OVERLAY.overlay_2.START	=.;
	SDK_OVERLAY.overlay_2.TEXT_START	=.;
						#:::::::::: text/rodata
						eee.o (.text)
						fff.o (.text)
						. = ALIGN(4);
						eee.o (.rodata)
						fff.o (.rodata)
						. = ALIGN(4);
						eee.o (.init)
						fff.o (.init)
						. = ALIGN(4);
	SDK_OVERLAY.overlay_2.SINIT_START	=.;
						#:::::::::: ctor
						eee.o (.ctor)
						fff.o (.ctor)
						eee.o (.sinit)
						fff.o (.sinit)
						WRITEW	0;
						#:::::::::: ctor
	SDK_OVERLAY.overlay_2.SINIT_END	=.;

						#:::::::::: text/rodata
						. = ALIGN(32);
	SDK_OVERLAY.overlay_2.TEXT_END	=.;

	#
	#  DATA BLOCK: READ WRITE
	#
	SDK_OVERLAY.overlay_2.DATA_START	=.;
						#:::::::::: data
						eee.o (.sdata)
						fff.o (.sdata)
						. = ALIGN(4);
						eee.o (.data)
						fff.o (.data)
						. = ALIGN(4);
						#:::::::::: data
						. = ALIGN(32);
	SDK_OVERLAY.overlay_2.DATA_END	=.;
	SDK_OVERLAY.overlay_2.END		=.;

	SDK_OVERLAY.overlay_2.TEXT_SIZE	= SDK_OVERLAY.overlay_2.TEXT_END - SDK_OVERLAY.overlay_2.TEXT_START;
	SDK_OVERLAY.overlay_2.DATA_SIZE	= SDK_OVERLAY.overlay_2.DATA_END - SDK_OVERLAY.overlay_2.DATA_START;
	SDK_OVERLAY.overlay_2.SIZE		= SDK_OVERLAY.overlay_2.END      - SDK_OVERLAY.overlay_2.START;

	} > overlay_2

	.overlay_2.bss:
	{
	ALIGNALL(4);				. = ALIGN(32);
	
	#
	#  BSS BLOCK
	#
	SDK_OVERLAY.overlay_2.BSS_START	= .;
						#:::::::::: bss
						eee.o (.bss)
						fff.o (.bss)
						. = ALIGN(4);
						eee.o (.sbss)
						fff.o (.sbss)
						. = ALIGN(4);
						#:::::::::: bss
						. = ALIGN(32);
	SDK_OVERLAY.overlay_2.BSS_END	= .;

	SDK_OVERLAY.overlay_2.BSS_SIZE	= SDK_OVERLAY.overlay_2.BSS_END - SDK_OVERLAY.overlay_2.BSS_START;
	
	} >> overlay_2

	
	############################ MAIN EX ##################################
	# MAIN EX Area
	.dummy.MAIN_EX:
	{
		. = ALIGN(32);
	} > dummy.MAIN_EX
	
	############################ ARENA ##################################
	.arena.MAIN:
	{
						. = ALIGN(32);
		SDK_SECTION_ARENA_START		=.;
	} > arena.MAIN
	
	.arena.MAIN_EX:
	{
						. = ALIGN(32);
		SDK_SECTION_ARENA_EX_START	=.;
	} > arena.MAIN_EX
	
	.arena.ITCM:
	{
						. = ALIGN(32);
		SDK_SECTION_ARENA_ITCM_START	=.;
	} > arena.ITCM

	.arena.DTCM:
	{
						. = ALIGN(32);
		SDK_SECTION_ARENA_DTCM_START	=.;
	} > arena.DTCM
	
	############################ OVERLAYDEFS ############################
	.main_defs:
	{
		###  main module information 
		WRITEW ADDR(.main);			# load address
		WRITEW _start;					# entry address
		WRITEW SDK_STATIC_SIZE + SDK_AUTOLOAD_SIZE;	# size of module
		WRITEW _start_AutoloadDoneCallback;		# callback autoload done
		
		###  overlay filename 
		WRITES ("overlay_0.sbin");	# Overlay 0
		WRITES ("overlay_1.sbin");	# Overlay 1
		WRITES ("overlay_2.sbin");	# Overlay 2
	
	} > main_defs


	############################ OVERLAYTABLE ###########################
	.main_table:
	{
		#	 Overlay 0
		WRITEW 0;				# overlay ID
		WRITEW ADDR(.overlay_0);			# load address
		WRITEW SDK_OVERLAY.overlay_0.SIZE;		# size of module
		WRITEW SDK_OVERLAY.overlay_0.BSS_SIZE;	# size of bss
		WRITEW SDK_OVERLAY.overlay_0.SINIT_START;	# start address of static init
		WRITEW SDK_OVERLAY.overlay_0.SINIT_END;	# end address of static init
		WRITEW 0;				# ROM file ID
		WRITEW 0;					# Reserved
		
		#	 Overlay 1
		WRITEW 1;				# overlay ID
		WRITEW ADDR(.overlay_1);			# load address
		WRITEW SDK_OVERLAY.overlay_1.SIZE;		# size of module
		WRITEW SDK_OVERLAY.overlay_1.BSS_SIZE;	# size of bss
		WRITEW SDK_OVERLAY.overlay_1.SINIT_START;	# start address of static init
		WRITEW SDK_OVERLAY.overlay_1.SINIT_END;	# end address of static init
		WRITEW 1;				# ROM file ID
		WRITEW 0;					# Reserved
		
		#	 Overlay 2
		WRITEW 2;				# overlay ID
		WRITEW ADDR(.overlay_2);			# load address
		WRITEW SDK_OVERLAY.overlay_2.SIZE;		# size of module
		WRITEW SDK_OVERLAY.overlay_2.BSS_SIZE;	# size of bss
		WRITEW SDK_OVERLAY.overlay_2.SINIT_START;	# start address of static init
		WRITEW SDK_OVERLAY.overlay_2.SINIT_END;	# end address of static init
		WRITEW 2;				# ROM file ID
		WRITEW 0;					# Reserved
		
		
	} > main_table
	
	
	############################ OTHERS #################################
	SDK_MAIN_ARENA_LO = SDK_SECTION_ARENA_START;
	SDK_IRQ_STACKSIZE = 1024;	# allocated in DTCM
	SDK_SYS_STACKSIZE = 8192;		# when 0 means all remains of DTCM
	
	# Module filelist
	.binary.MODULE_FILES:
	{
		WRITES ("main.sbin");
		WRITES ("main_defs.sbin");
		WRITES ("main_table.sbin");
	} > binary.MODULE_FILES

	# ITCM/DTCM size checker => check AUTOLOAD_ITCM/DTCM
	.check.ITCM:
	{
		. = . + SDK_AUTOLOAD_ITCM_SIZE + SDK_AUTOLOAD_ITCM_BSS_SIZE;
	} > check.ITCM
	
	SDK_SYS_STACKSIZE_SIGN = (SDK_SYS_STACKSIZE < 0x80000000) * 2 - 1;
	.check.DTCM:
	{
		. = . + SDK_AUTOLOAD_DTCM_SIZE + SDK_AUTOLOAD_DTCM_BSS_SIZE;
		. = . + SDK_IRQ_STACKSIZE + SDK_SYS_STACKSIZE * SDK_SYS_STACKSIZE_SIGN;
	} > check.DTCM

}
