/*------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
///
///  \file     	MyLink.ld
///
///  \brief    	linker script for STM32F103ZE
///
//-----------------------------------------------------------------------------*/

/*---------------------------------------------------------
// ENTRY 
//-------------------------------------------------------*/
ENTRY(_start)

/*---------------------------------------------------------
// MEMORY (original layout; remark by BATH in 2015-08-13!)
//-------------------------------------------------------*/
/* STM32F205RTC: FLASH: 256kB (0x08000000 - 0x0803FFFF)
//               SRAM:  112kB (0x20000000 - 0x2001BFFF)
//               SRAM:  16kB  (0x2001C000 - 0x2001FFFF) */

OUTPUT_FORMAT ("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
/* ENTRY(_start) */
SEARCH_DIR(.)
GROUP(-lgcc -lc -lcs3 -lcs3unhosted -lcs3micro)

/* The external reference of symbols, declared by linker-file. */
EXTERN(__cs3_reset_cortex_m)
EXTERN(__cs3_interrupt_vector_cortex_m)
/* EXTERN(__cs3_start_c main __cs3_stack __cs3_stack_size __cs3_heap_end) */
EXTERN(__cs3_start_c main __cs3_stack __cs3_stack_size)

/* Computations of new symbols by those, wo serve as external references. */
PROVIDE(__cs3_stack = __cs3_region_start_ram + __cs3_region_size_ram);
/* PROVIDE(__cs3_stack_size = __cs3_region_start_ram + __cs3_region_size_ram - _end); */
PROVIDE(__cs3_stack_size = 1024);
/* PROVIDE(__cs3_heap_start = _end); */
/* PROVIDE(__cs3_heap_end = __cs3_region_start_ram + __cs3_region_size_ram); */

/* The memory-layout for target-system. */
MEMORY
{
  DEFAULT (rx)    : ORIGIN = 0x08000000, LENGTH = 512K  /* end: 0x0807FFFF */
  APP (rx)        : ORIGIN = 0x08000000, LENGTH = 512K 	/* end: 0x0807FFFF */
  
  RAM (rw)        : ORIGIN = 0x20000000, LENGTH = 52K 	/* end: 0x2000ffff*/
  /* RAM_SYS (rw)    : ORIGIN = 0x2001C000, LENGTH = 12K   end: 0x20002C00  */
  /* DONT_USE (rwx)  : ORIGIN = 0x20002c00, LENGTH = 12K   DUMMY  */
}

/*---------------------------------------------------------
// VARIABLES
//-------------------------------------------------------*/
_app_origin = ORIGIN(APP); 

/* Thus, we have no use of heap here, this is actually commented out. */
/* _uheap_start = ORIGIN(RAM_SYS); */

/*---------------------------------------------------------
// SECTIONS
//-------------------------------------------------------*/
SECTIONS
{
	/* Some sections, not used here by now.*/
	/* ++++++++++++++++++++++++++++++++++++*/
	
	/* ------------------ .text section ------------------ */
	.text :
	{
	CREATE_OBJECT_SYMBOLS
	/* START OF ROM-REGION: 0x08000000 */
	__cs3_region_start_rom = .;

	/* The address of the interrupt-vector-table. */
	__cs3_interrupt_vector = __cs3_interrupt_vector_cortex_m;

	/* The reset-handler */
	__cs3_reset = __cs3_reset_cortex_m;
	
	/* Check if address actually takes code. (Optional!) */
	*(.cs3.reset)

	/* Other ROM-code... */
	*(.text)
	
	/* Constant-data... */
	*(.rodata)

	/* Other regions, declared here. */
	__cs3_regions = .;
	
	/* The start of RAM resides under this the value at this address in ROM given as 4byte-address explicitly. */
	LONG (__cs3_region_start_ram)
	LONG (__cs3_region_end_ram)
	LONG (__cs3_region_start_bss)
	LONG (__cs3_stack - __cs3_stack_size)

	} > APP

	. = ALIGN(0x04);
	_idata_src = .;  

	/* The data-section starts here. */
	/* ------------- .data section (idata) --------------- */

	.data : AT (_idata_src)
	{
	__cs3_region_start_ram = .;
	. = ALIGN (4);
	*(.data)
	__cs3_region_end_ram = .;
	. = ALIGN (4);
	} > RAM

	__cs3_region_start_bss = __cs3_region_end_ram;
	
	/* This bss is mainly taken from original STM-pattern. */
	/* -------------- .bss section (udata) --------------- */ 
	.bss (__cs3_region_start_bss) :
	{
	*(.bss)
	*(COMMON)
	. = ALIGN (4);
	_end = .;
	} > RAM AT > APP /* Zero-data. */

	. = ALIGN (4);
	
	
	.stack (__cs3_stack - __cs3_stack_size) :
	{
	*(.stack)
	} > RAM

	__cs3_region_size_ram = LENGTH(RAM);
} 