To describe the desired layout of your program in memory the CrossWorks project system uses a memory map file and an optional linker placement file. These files are both xml files and can be edited either with the text editor or with the built-in memory map editor. The principle usage of the memory map file is to describe the physical location of memory segments on the target. The specification of where to place program sections is done in terms of these memory map segments.

##### Using a single file

In this scheme the sections are explicitly placed in the memory segments of the memory map file

> ROOT

> > PERIPHERALS1 (0x70000000)

> > PERIPHERALS2 (0x60000000)

> > FLASH (0x400000000)

> > > .text

> > > .vectors

> > SRAM (0x00000000)

> > > .stack

In this system the sections .text and .vectors are placed in the FLASH segment and the .stack section is placed in the SRAM section.

The memory map file to use for the linkage can either be included as a part of the project or alternatively it can be specified in the Memory Map File project property.

##### Using two files

In this scheme a separate section placement file is used to specify the section placement by referring to the memory segments of another file. This scheme enables a single hardware description to be shared across projects and also enables a project to be built for a variety of hardware descriptions. The format of a section placement is very similar to a memory map file - however no addresses are needed for the memory segments.

> ROOT

> > FLASH

> > > .text

> > > .vectors

> > SRAM

> > > .stack

The memory map file can just contain the memory segment descriptions

> ROOT

> > PERIPHERALS1 (0x70000000)

> > PERIPHERALS2 (0x60000000)

> > FLASH (0x400000000)

> > SRAM (0x00000000)

The section placement file to use for linkage can either be included as a part of the project or alternatively it can be specified in the Section Placement File project property.

##### Adding a new section

To add a new section you must create one using the either the assembler or the compiler. For the CrossWorks C compiler this can be achieved using the #pragma codeseg("name") directive. For the GNU C compiler this can be achieved using the __attribute__((section("name")) on the functions. For both compilers CrossWorks supports renaming of the code, constant, data and zero'd data using the Section Options properties.

Once you have created a section you can then place it into one of the memory segments of either the memory map file or the section placement file. Note that the placement order is kept when the linker command line is generated unless you specify explicitly an address that the section should be placed at.

##### Specifying load sections and run sections

If the section you have created is a code section then you should set the Load property of the section to "Yes". This makes the linker include the section in the program. For example a new code section called .text2 can be placed into the program using

> ROOT

> > FLASH

> > > .text2

> > > .text

> > > .vectors

> > SRAM

> > > .stack

If you specify a new data section then you will need to instruct the linker to put the initialisation information about the section into the program and you will need to modify the startup code to initialise the contents of this section from the program.

##### Data sections using the CrossWorks linker

For the CrossWorks linker you can specify that initialisation data is stored in the program using the .init directive and you can refer to the start and end of the section using the SFE and SFB directives. If for example you create a new data section called "IDATA2" you can store this in the program by putting the following into the startup code

> data_init_begin2

> .init "IDATA2"

> data_init_end2

You can then use these symbols to copy the stored section information into the data section using (an assembly coded version of)

memcpy(SFB(IDATA2), data_init_begin2, data_init_end2-data_init_end2)

##### Data sections using the GNU linker

For the GNU linker you have to specify a load section in the program where the initialisation data will be stored and the run section where it will be copied to. For example

> ROOT

> > FLASH

> > > .data2

> > > .......

> > SRAM

> > > .data2_run

> > > .......

The .data2 section will have the load attribute set to "Yes" and the "section to run in" attribute set to .data2_run, the .data2_run section will have the load attribute set to "No".

CrossWorks generates a GNU linker script containing three symbols for each section marking the load address, start run address and end run address. These symbols can be used to copy the sections from their load positions to their run positions.

memcpy(__data2_start__, __data2_load_start__, __data2_end__ - __data2_start__);