11.3.2 Assigning Output Sections to Regions

Once the sizes of all output sections are known, they are assigned to memory regions. Normally a region is specified in the output section definition. If a region is not specified, the first defined memory region will be used.

Memory regions are filled sequentially, from lower to higher addresses, in the same order that sections appear in the section map. Memory reserved for boot or secure segments will be avoided, as well as sections that have been marked with the address attribute in source code. A location counter, unique to each region, keeps track of the next available memory location. There are two conditions which may cause gaps in the allocation of memory within a region:

  • The section map specifies an absolute address for an output section.
  • The output section has a particular alignment requirement.

In either case, any intervening memory between the current location counter and the absolute (or aligned) address is skipped. Once a range of memory has been skipped, it is available for use by the best-fit allocator. The exact address of all items allocated in memory may be determined from the link map file.

Section alignment requirements typically arise in DSP programming. To utilize modulo addressing, it is necessary to align a block of memory to a particular storage boundary. This can be accomplished with the aligned attribute in C, or with the .align directive in assembly language. The section containing an aligned memory block must also be aligned, to the same (or greater) power of 2. If two or more input sections have different alignment requirements, the largest alignment is used for the output section.

Another restriction on memory allocation is associated with read-only data sections when compiled for dsPIC33C/E/F architectures. Read-only data sections are identified with the psv section attribute and are dedicated for use in the PSV window or the Extended Data Space (EDS) window. The C compiler creates a read-only data section named .const to store constants when the --mconst-in-code option is selected.

To allow efficient access of constant tables in the PSV or EDS window, the linker ensures that a read-only section will not cross a page boundary. Therefore a single setting of the page register can be used to access the entire section. If necessary, output sections in program memory will be re-sorted after the sequential allocation pass to accommodate this restriction. If an absolute address has been specified in the linker script for a particular section, it will not be moved. In general, fully relocatable sections provide the most flexibility for efficient memory allocation.

Note: Sections with specific alignment requirements, such as psv sections or sections intended for modulo addressing, may be allocated most efficiently by the best-fit allocator. For best-fit allocation, these sections should not appear in the linker script.