How to place a variable or function at specific location in IAR compiler?


I am using IAR Embedded Workbench for Renesas Syenrgy (ARM cortex M4)

Code flash size : 1MB

Consider following example:

void function (void)
     unsigned char a;
     a = a+1;

I want to put this function at a specific location in flash. What is method for defining a user defined section for this code.

I have been gone through IAR compiler user guide and implemented a method mentioned as below:

#pragma default_function_attribute = @ "MY_FUNC"
void function (void)
     unsigned char a;
     a = a + 1;
#pragma default_function_attribute =

And in .icf file, i have added the section as follows :

define symbol region_VECT_start    = 0x00000000;
define symbol region_VECT_end      = 0x000003FF;
define symbol region_ROMREG_start  = 0x00000400;
define symbol region_ROMREG_end    = 0x000004FF;
define symbol region_FLASH_start   = 0x00000500;
define symbol region_FLASH_end     = 0x000BFFFF;  // Fklsh end address is modified from 0x000FFFFF to 0x000BFFFF
define symbol region_MY_FUNC_start = 0x000C0000;  // This statement is added
define symbol region_MY_FUNC_end   = 0x000FFFFF;  // This statement is added
define symbol region_RAM_start     = 0x20000000;
define symbol region_RAM_end       = 0x2002FFFF;
define symbol region_DF_start      = 0x40100000;
define symbol region_DF_end        = 0x40103FFF;
define symbol region_QSPI_start    = 0x60000000;
define symbol region_QSPI_end      = 0x63FFFFFF;

/* Stack and heap sizes are defined in bsp_cfg.h */

define memory mem with size     = 4G;
define region VECT_region       = mem:[from region_VECT_start   to region_VECT_end];
define region ROMREG_region     = mem:[from region_ROMREG_start to region_ROMREG_end];
define region FLASH_region      = mem:[from region_FLASH_start  to region_FLASH_end];
define region RAM_region        = mem:[from region_RAM_start    to region_RAM_end];
define region DF_region         = mem:[from region_DF_start     to region_DF_end];
define region QSPI_region       = mem:[from region_QSPI_start   to region_QSPI_end];
define region MY_FUNC           = mem:[from region_MY_FUNC_start to region_MY_FUNC_end];  // New section is added

With these changes, the code is compiling properly. But, "function" is not placed at required location 0x000C0000

Why is it so?

asked on Stack Overflow Feb 7, 2017 by Nama • edited Feb 7, 2017 by Nama

2 Answers


You can place _Pragma("location=\"__your_section__\"") in front of the function.

And then in the linker file (for example):

define block __your_section_block__ size = 0x100 { section __your_section__ };

And make sure you add the line to the linker:

place in MY_FUNC   { block __your_section_block__ };
answered on Stack Overflow Feb 7, 2017 by Realtime Rik

You should change the definition of "MY_FUNC" to "MY_FUNC_region" in the .icf file to avoid confusions:

define region MY_FUNC_region     = mem:[from region_MY_FUNC_start to region_MY_FUNC_end];

And then add the following line to place the "MY_FUNC" code inside the "MY_FUNC_region":

place in MY_FUNC_region   { readonly section MY_FUNC };  

(Not entirely sure if "readonly" keyword is absolutely needed but works for me)

Or, alternatively:

place at address mem:region_MY_FUNC_start { readonly section MY_FUNC};
answered on Stack Overflow Dec 31, 2019 by Rex

User contributions licensed under CC BY-SA 3.0