5.3. Memory Layout
Prev | Chapter 5. CS3™: The CodeSourcery Common Startup Code Sequence | Next

## 5.3. Memory Layout

The header file `cs3.h` declares variables and types that describe the layout of memory on the system to C code. The variables are defined by the CS3 linker script or in the board support library.

The following variables describe the regions of memory to be initialized at startup:

    /* The number of elements in __cs3_regions.  */
    const size_t __cs3_region_num;

    /* An untyped object, aligned on an eight-byte boundary.  */
    typedef unsigned char __cs3_byte_align8
                          __attribute__ ((aligned (8)));

    struct __cs3_region
    {
      /* Flags for this region.  None defined yet.  */
      unsigned flags;

      __cs3_byte_align8 *init;  /* Region's initial contents.  */
      __cs3_byte_align8 *data;  /* Region's start address.  */

      /* These sizes are always a multiple of eight.  */
      size_t init_size;     /* Size of initial data.  */
      size_t zero_size;     /* Additional size to be zeroed.  */
    };

    /* An array of memory regions.  __cs3_regions[0] describes
       the region holding .data and .bss.  */
    extern const struct __cs3_region __cs3_regions[];

The following variables describe the area of memory to be used for the dynamically-allocated heap:

    /* The addresses of these objects are the start and end of
       free space for the heap, typically following .data and .bss.
       However, &__cs3_heap_end may be zero, meaning that we must
       determine the heap limit in some other way --- perhaps via a
       supervisory operation on a simulator, or simply by treating
       the stack pointer as the limit.  */
    extern __cs3_byte_align8 __cs3_heap_start[];
    extern __cs3_byte_align8 __cs3_heap_end[];

    /* The end of free space for the heap, or zero if we haven't been
       able to determine it.  It usually points to __cs3_heap_end,
       but in some configurations, may be overridden by a supervisory
       call in the reset code.  */
    extern void *__cs3_heap_limit;

For each region named `R`, `cs3.h` declares the following variables:

    /* The start of the region.  */
    extern unsigned char __cs3_region_start_R[]
                         __attribute__ ((aligned (8)));

    /* The region's size, in bytes.  */
    extern const size_t __cs3_region_size_R;

At the assembly level, the linker script also defines symbols with the same names and values.

If the region is initialized, then `cs3.h` also declares the following variables, corresponding to the region's element in `__cs3_regions`:

    /* The data we initialize the region with.  */
    extern const unsigned char __cs3_region_init_R[]
                               __attribute__ ((aligned (8)));

    /* The size of the initialized portion of the region.  */
    extern const size_t __cs3_region_init_size_R;

    /* The size of the additional portion to be zeroed.  */
    extern const size_t __cs3_region_zero_size_R;

Any of these identifiers may actually be defined as preprocessor macros that expand to expressions of the appropriate type and value.

Like the struct __cs3_region members, these regions are all aligned on eight-byte boundaries, and their sizes are multiples of eight bytes.

CS3 linker scripts place the contents of sections named `.cs3.region-head.`R`` at the start of each memory region. Note that CS3 itself may want to place items (like interrupt vector tables) at these locations; if there is a conflict, CS3 raises an error at link time.

Prev | Up | Next
5.2. Exit and Embedded Systems | Home | 5.4. Interrupt Vectors and Handlers