This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to allocate "stack"-memory?

Other Parts Discussed in Thread: TM4C123GH6PM

Hi All.


I have been playing with the Tiva-TM4C123GXL evaulation-board, and am running into weird issues when I define a 1000-sized char array on the stack (but things work fine when I define the array globally).


Upon looking around, it seems that the default-stack for the board is really small, and that might be causing the issues. So, how can I define custom-sized stack? Presently, I am using the default-shipped TM4C123GH6PM.ld file as follows ::

MEMORY
{
    FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00040000
    SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0X00008000
}

SECTIONS
{
    /* code */
    .text :
    {
        _text = .;
        /* ensure ISR vectors are not removed by linker */
        KEEP(*(.isr_vector))
        *(.text*)
        *(.rodata*)
        _etext = .;
    } > FLASH

    /* static data */
    .data : AT(ADDR(.text) + SIZEOF(.text))
    {
        _data = .;
        *(vtable)
        *(.data*)
        _edata = .;
    } > SRAM

    /* static uninitialized data */
    .bss :
    {
        _bss = .;
        *(.bss*)
        *(COMMON)
        _ebss = .;
    } > SRAM

}

Will be really grateful for any help.

Thanks and Regards,

Ajay

  • Hello Ajay

    There is a line in the cmd file for Stack Size allocation. Did you happen to remove that?

    __STACK_TOP = __stack + 1024;

    Also the linker has the option for stack size as well.

    Regards
    Amit
  • Hi Amit.


    I don't think I removed it.

    I am using the same file, as obtained as per the steps at http://chrisrm.com/howto-develop-on-the-ti-tiva-launchpad-using-linux/ 

    Note that this is GCC-toolchain for Linux.

    Anyways, I added that line at the end, and it gives the error :

    ../embedded-flash-environment/tiva-template/TM4C123GH6PM.ld:39: undefined symbol `__stack' referenced in expression
    collect2: error: ld returned 1 exit status

    Do you think we will have to use CCS only to resolve the issues? Or there is a way for Linux as well?

  • Hello Ajay,

    Did you check the linker option. The method in cmd file is for ARM Compiler and not the GCC (my bad)

    Regards
    Amit
  • Hi,

    I think for your case the stack size should be specified in startup_gcc.c file.

    But there are some other methods:

    a) If I remember correctly, once I got a project linked by specifying (with -T) just the .cmd file.

    b) Use the linker file below, since is more comprehensive - you may use it also with C++ and also define your heap size.

    Change the extension of the file to .ld

    /******************************************************************************
    * Linker file for LM4F232H5QD 
    ******************************************************************************
    */
    OUTPUT_FORMAT ("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
    OUTPUT_ARCH(arm)
    SEARCH_DIR(.)
    ENTRY(ResetISR)
    
    MEMORY
    {
        FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 256K
        SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 32K
    }
    
    GROUP(libgcc.a libc.a libm.a libnosys.a)
    
    STACK_SIZE = 4096;
    HEAP_SIZE = 8192;
    
    SECTIONS
    {
        .isr_vector : {
    	KEEP(*(.isr_vector))
    	. = ALIGN(4);
        } > FLASH
    
        .text : {
            . = ALIGN(4);
            *(.text*)
    	
    	KEEP(*(.init))
    	KEEP(*(.fini))
    	
    	/* .ctors */
    	*crtbegin.o(.ctors)
    	*crtbegin?.o(.ctors)
    	*(EXCLUDE_FILE(*.crtend?.o *crtend.o) .ctors)
    	*(SORT(.ctors.*))
    	*(.ctors)
    
    	/* .dtors */
    	*crtbegin.o(.dtors)
    	*crtbegin?.o.(dtors)
    	*(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
    	*(SORT(.dtors.*))
    
    	*(.rodata*)
    	KEEP(*(.eh_frame*))
    
    	. = ALIGN(4);
        } > FLASH
    
        .ARM.extab : {
            *(.ARM.extab* .gnu.linkonce.armextab.*)
        } > FLASH
    
        __exidx_start = .;
        .ARM.exidx : {
            *(.ARM.exidx* .gnu.linkonce.armexidx.*)
        } > FLASH
        . = ALIGN(4);
    
        __exidx_end = .;
        
        _etext = .;
        
        .data : AT(_etext)
        {
            _data = .;
    	__data_start__ = .;
            *(vtable)
            *(.data*)
    	. = ALIGN(4);
    	/* preinit data */
    	PROVIDE_HIDDEN (__preinit_array_start = .);
    	KEEP(*(.preinit_array))
    	PROVIDE_HIDDEN (__preinit_array_end = .);
    
    	. = ALIGN(4);
    	/* init data */
    	PROVIDE_HIDDEN (__init_array_start = .);
    	KEEP(*(SORT(.init_array.*)))
    	KEEP(*(.init_array))
    	PROVIDE_HIDDEN (__init_array_end = .);
            
    	. = ALIGN(4);
    	/* finit data */
    	PROVIDE_HIDDEN (__fini_array_start = .);
    	KEEP(*(SORT(.fini_array.*)))
    	KEEP(*(.fini_array))
    	PROVIDE_HIDDEN (__fini_array_end = .);
    
    	. = ALIGN(4);
    	/* All data end */
    	__data_end__ = .;
            
        } > SRAM
    
        .bss : 
        {
            . = ALIGN(4);
    	_bss = .;
    	__bss_start__ = .;
            *(.bss*)
            *(COMMON)
    	. = ALIGN(4);
    	__bss_end__ = .;
    	_ebss = .;
        } > SRAM
    
        .heap : {
    	. = ALIGN(4);
    	__end__ = .;
    	end = __end__;
    	*(.heap*)
    	. = ALIGN(4);    
    	__HeapLimit = .;
        } > SRAM
    
        .stack_dummy : 
        {
    	*(.stack)
        } > SRAM
    
        __StackTop = ORIGIN(SRAM) + LENGTH(SRAM);
        __StackLimit = __StackTop - SIZEOF(.stack_dummy);
        PROVIDE(__stack = __StackTop);
    
        ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
        _edata = .;	
    }
    /* EOF */