/**
 * \file    linker script
 * \brief   LD linker script
 * 
 * \par     Copyright (c) 2018 UWE Technologies Corp., Ltd. All Rights Reserved.
 * \author  James
 * \date    2018-03-30
 * \version 
 * 
 * \history 
 *  Date        Note
 *  2018-03-30  Original Creation
 * 
 */

OUTPUT_ARCH( "riscv" )
ENTRY(_start)

MEMORY {
  FLASH (rx) : ORIGIN = 0x0, LENGTH = 256K
  /* RAM (rw) : ORIGIN = 0x60000000, LENGTH = 8K */
  /* Reserved 660 bytes as retention memory */
  RAM (rw) : ORIGIN = 0x60000294, LENGTH = 7532
}

/*STACK_SIZE = 576;*/
STACK_SIZE = 1152;

SECTIONS {

  /* code segment */
  .text.crt : {
    *(.text.crt*)
  } >FLASH

  . = ALIGN(4);

  .text : {
    __TEXT_START__ = .;
    *(.text .text.*)
    *(.gnu.linkonce.t.*)
    __TEXT_END__ = .;
  } >FLASH

  .rodata : {
    *(.rdata .rdata.*)
    *(.rodata .rodata.*)
    *(.rodata.str1.4)
    *(.gnu.linkonce.r.*)
    *(.srodata.cst16)
    *(.srodata.cst8)
    *(.srodata.cst4)
    *(.srodata.cst2)
    *(.srodata .srodata.*)
    __RODATA_END__ = .;
  } >FLASH

  /* data segment */

  .data : AT (__RODATA_END__) {
    data_start = .;
    __global_pointer$ = . + 0x800;
    *(.sdata .sdata.* .gnu.linkonce.s.*)
    *(.sdata2 .sdata2.*)
    *(.data .data.* .gnu.linkonce.d.*)
    __DATA_END__ = .;
  } >RAM

  data_size = SIZEOF(.data);
  data_load_start = LOADADDR(.data);

  /* bss segment */
  .sbss : {
    PROVIDE(__BSS_START__ = .);
    *(.sbss .sbss.* .gnu.linkonce.sb.*)
    *(.scommon)
  } >RAM

  .bss : {
    *(.bss .bss.*)
    . = ALIGN(4);
    PROVIDE(__BSS_END__ = .);
  } >RAM

  . = ALIGN(16);
  PROVIDE(_end = .);
  PROVIDE(__end = .);
  PROVIDE(_heap_start = .);

  /* End of uninitalized data segement */

  .stack ORIGIN(RAM) + LENGTH(RAM) - STACK_SIZE : {
    PROVIDE(_heap_end = .);
    PROVIDE(__STACK_START__ = .);
    . += STACK_SIZE;
    PROVIDE(__C_STACK_TOP__ = .);
    PROVIDE(__STACK_END__ = .);
  } >RAM
  
  total_mem = LENGTH(RAM);
  global_stack_size = STACK_SIZE;
  global_data_size = __BSS_END__ -  data_start;

  __TLS0_BASE__ = .;

  /DISCARD/ : {
    *(.eh_frame .eh_frame.*)
  }
}
