Search code examples
linkercortex-miar

IAR equivalent symbol to __bss_end__


I want to know where is the end of my static data in RAM of Cortex-M. GCC CMSIS linker scripts provide a symbol for __bss_end__ and I simply take its address. Is there an IAR equivalent? Or do I have to make a dummy section with a single variable and place it after readwrite?

This is my (very standard) IAR linker script:

/*###ICF### Section handled by ICF editor, don't touch! ****/
/*-Editor annotation file-*/
/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */

/*-Specials-*/
define symbol __ICFEDIT_intvec_start__ = 0x00000000;

/*-Memory Regions-*/
define symbol __ICFEDIT_region_ROM_start__   = 0x00000000;
define symbol __ICFEDIT_region_ROM_end__     = (0x00000000+0x00010000-1);
define symbol __ICFEDIT_region_RAM_start__   = 0x20000000;
define symbol __ICFEDIT_region_RAM_end__     = (0x20000000+0x00002000-1);

/*-Sizes-*/
if ( !isdefinedsymbol( __ICFEDIT_size_cstack__ ) )
{ define symbol __ICFEDIT_size_cstack__   = 0x400; }

if ( !isdefinedsymbol( __ICFEDIT_size_heap__ ) )
{ define symbol __ICFEDIT_size_heap__     = 0x800; }

/**** End of ICF editor section. ###ICF###*/

define memory mem with size = 4G;
define region ROM_region   = mem:[from __ICFEDIT_region_ROM_start__   to __ICFEDIT_region_ROM_end__];
define region RAM_region   = mem:[from __ICFEDIT_region_RAM_start__   to __ICFEDIT_region_RAM_end__];

define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };
define block HEAP      with alignment = 8, size = __ICFEDIT_size_heap__     { };

initialize by copy { readwrite };

keep { section .intvec };
place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };

place in ROM_region   { readonly };
place in RAM_region   { readwrite,
                        block CSTACK,
                        block HEAP };

Solution

  • If you create a block, put the data in it and place the block you can use __section_begin() and __section_end() to get pointers to the beginning and the end of this block.

    Example linker configuration:

    /*-Specials-*/
    define symbol __ICFEDIT_intvec_start__ = 0x00000000;
    
    /*-Memory Regions-*/
    define symbol __ICFEDIT_region_ROM_start__   = 0x00000000;
    define symbol __ICFEDIT_region_ROM_end__     = (0x00000000+0x00010000-1);
    define symbol __ICFEDIT_region_RAM_start__   = 0x20000000;
    define symbol __ICFEDIT_region_RAM_end__     = (0x20000000+0x00002000-1);
    
    /*-Sizes-*/
    if ( !isdefinedsymbol( __ICFEDIT_size_cstack__ ) )
    { define symbol __ICFEDIT_size_cstack__   = 0x400; }
    
    if ( !isdefinedsymbol( __ICFEDIT_size_heap__ ) )
    { define symbol __ICFEDIT_size_heap__     = 0x800; }
    
    /**** End of ICF editor section. ###ICF###*/
    
    define memory mem with size = 4G;
    define region ROM_region   = mem:[from __ICFEDIT_region_ROM_start__   to __ICFEDIT_region_ROM_end__];
    define region RAM_region   = mem:[from __ICFEDIT_region_RAM_start__   to __ICFEDIT_region_RAM_end__];
    
    define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };
    define block HEAP      with alignment = 8, size = __ICFEDIT_size_heap__     { };
    define block RWDATA    { readwrite }; /* Create block for readwrite to find its boundaries */
    
    initialize by copy { readwrite };
    
    keep { section .intvec };
    place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };
    
    place in ROM_region   { readonly };
    place in RAM_region   { block RWDATA, block CSTACK, block HEAP };
    

    And the code to access the information

    #include <stdio.h>
    #pragma language = extended
    #pragma section  = "RWDATA"
    
    int main(void)
    {
      char const *rw_start = __section_begin("RWDATA");
      char const *rw_end = __section_end("RWDATA");
      printf("rw_start = %x, rw_end = %x\n", rw_start, rw_end);
    }