Nios® II Software Developer Handbook

ID 683525
Date 8/28/2023
Public
Document Table of Contents

7.15.2.2. Advanced Placement Options

In your program source code, you can specify a target memory section for each piece of data or code. In C or C++, you can use the section attribute. This attribute must be placed in a function prototype; you cannot place it in the function declaration itself.
Example 6–16. Manually Assigning C Code to a Specific Memory Section
/* data should be initialized when using the section attribute */
int foo __attribute__ ((section (".ext_ram.rwdata"))) = 0;
void bar (void) __attribute__ ((section (".sdram.text")));
void bar (void)
{
foo++;
}
Note: A variable foo is placed in the memory named ext_ram, and the function bar() is placed in the memory named sdram.

In assembly you do this using the .section directive. For example, all code after the following line is placed in the memory device named ext_ram:

.section .ext_ram.text 

The section names ext_ram and sdram are examples. You need to use section names corresponding to your hardware.

When creating section names, use the following extensions:
  • .text for code: for example, .sdram.text
  • .rodata for read-only data: for example, .cfi_flash.rodata
  • .rwdata for read-write data: for example, .ext_ram.rwdata

    For more information about the use of these features, refer to the GNU compiler and assembler documentation. This documentation is installed with the Nios® II EDS. To find it, open the Nios® II EDS documentation launchpad, scroll down to Software Development, and click Using the GNU Compiler Collection (GCC).

Note: A powerful way to manipulate the linker memory map is by using the Nios II BSP Editor. With the BSP Editor, you can assign linker sections to specific physical regions, and then review a graphical representation of memory showing unused or overlapping regions. You start the BSP Editor from the Nios® II Command Shell. For details about using the BSP Editor, refer to the editor’s tool tips.