Please note as of Wednesday, August 15th, 2018 this wiki has been set to read only. If you are a TI Employee and require Edit ability please contact x0211426 from the company directory.

How to Overlay Initialization Code and a Heap

From Texas Instruments Wiki
Jump to: navigation, search

Question

How can I save memory by using a part of memory first for initialization code, and then for a heap?

Answer

Presume the initialization code is in a section named “init_code” and the heap is in a section named “user_heap”.

You may be tempted to use the UNION directive to set up the overlay …

SECTIONS
{
   UNION
   {
      init_code : 
         { /* input sects here */ }
         load = RAM
      user_heap :
         { /* input sects here */ }
         /* nothing to load */
   } > RAM
   …
}

You end up not saving any memory. The section init_code ends up being allocated into the RAM memory range twice: once for a load address and once for a run address. The UNION directive cannot model a section of code being loaded and run at the same address, while being overlaid by something else.

The alternative approach requires two steps.

Step one is to dedicate, for overlaying, a memory range in the MEMORY directive of the linker command file. Make sure this memory range is big enough to hold the largest of the code or heap sections. It would look something like …

MEMORY
{
   FOR_OVERLAY : origin = 0x1000, length = 0x1000
   …
}

Step two is to allocate both the code and heap sections to the FOR_OVERLAY memory range. Do not allocate any other sections to the FOR_OVERLAY memory range. The heap section is additionally given the DSECT attribute. It would be similar to this …

SECTIONS
{
   init_code :
      { /* input sects here */ }
      > FOR_OVERLAY
   user_heap :
      { /* input sects here */ }
      type = DSECT
      > FOR_OVERLAY
}

DSECT stands for dummy section. For details on DSECT consult the article Linker Special Section Types. Be aware that no error checking is done for a DSECT qualified section. Thus, it is easy to make a mistake using DSECT. Avoid using DSECT outside of the circumstances described in this article.

A DSECT section is not allocated space in the memory map, and it does not appear in the resulting .out file. Thus you can use this DSECT-based technique only when part of the overlay is an uninitialized section like a heap, and the DSECT property is applied to that uninitialized section.

There is no error checking to insure this overlay will work. Here are some presumptions which are never checked. One presumption is that once the code in the section init_code has completed executing, none of those functions will ever execute again. Another presumption is that none of the memory in the section user_heap is referenced until after the init_code routines are all finished. If such presumptions are violated, your code fails with no warning.

Please do not confuse the user created heap discussed here with the heap used by the family of malloc functions in the compiler RTS library. For reasons beyond the scope of this article, you cannot use the technique described here to overlay the .cinit section and the malloc heap.

See Also

  • tidoc:spraa46 - Advanced Linker Techniques for Convenient and Efficient Memory Usage