Uploaded image for project: 'Qt for MCUs'
  1. Qt for MCUs
  2. QTMCU-99

Override of "end" symbol from .heap section on armgcc mimxrt1170-evk-freertos

    XMLWordPrintable

Details

    • Platform Sprint 2022/3.1

    Description

      The linker script for armgcc mimxrt1170 (<QtMCU directory>/platform/boards/nxp/mimxrt1170-evk-freertos/cmake/armgcc/MIMXRT1176xxxxx_cm7_flexspi_nor_sdram.ld) defines an overridable "end" symbol in the .heap section:

      .heap :
       {
       . = ALIGN(8);
       __end__ = .;
       PROVIDE(end = .);
       __HeapBase = .;
       *(.heap*)
       __HeapLimit = .;
       __heap_limit = .; /* Add for _sbrk */
       } > m_data
      

      This symbol is used by the toolchain's malloc implementation to find the location of the heap.

      In file <QtMCU directory>/platform/boards/nxp/mimxrt1170-evk-freertos/noncacheableallocator.cpp this symbol is overriden:

      #if defined(_ICCARM_)
      #pragma section = "NCACHE_HEAP"
      char *begin = reinterpret_cast<char *>(__section_begin("NCACHE_HEAP"));
      char *end = reinterpret_cast<char *>(__section_end("NCACHE_HEAP"));
      #else
      extern unsigned char __noncacheheap_start;
      extern unsigned char __noncacheheap_end;
      char *begin = reinterpret_cast<char *>(&__noncacheheap_start);
      char *end = reinterpret_cast<char *>(&__noncacheheap_end);
      #endif
      NonCacheableAllocator::NonCacheableAllocator()
       : FixedSizeBlockAllocator(begin, end)
       {}
      

      This override causes memory overlap problems when using the toolchain's malloc.

      It seems that the "begin" and "end" variables are not used outside of noncacheableallocator.cpp. If that's indeed the case, the problem can be solved by declaring them static.

      Way to reproduce:

      The attached QTMCU-99.zip contains:

      • FreeRTOSConfig.h: Use it to replace <QtForMCU 2.0.1 directory>/platform/boards/nxp/mimxrt1170-evk-freertos/FreeRTOSConfig.h. It changes configAPPLICATION_ALLOCATED_HEAP in order to make FreeRTOS's HEAP4 allocator NOT use the ".heap" section for its allocations
      • noncacheableallocator.cpp: Use it to replace <QtForMCU 2.0.1 directory>/boards/nxp/mimxrt1170-evk-freertos/noncacheableallocator.cpp. It contains the fix proposed above.
      • freertos_multitask: This is a copy of the example code from QtForMCU with the following changes:
        • A target_link_options() option in the CMakeLists.txt is added in order to generate a "map" file
        • src/main.cpp is modified to perform an allocation after the calls to Qul::init*. By default this allocation is disabled with a "#if 0".

      The problem with the "end" symbol is due to the fact that the toolchain's allocator calls the _sbrk implementation located in <SDK directory>/devices/MIMXRT1176/utilities/fsl_sbrk.c, which uses the "end" symbol to determine where to allocate the memory.

      The first step to witness the problem is to build the "freertos_multitask" with the modified FreeRTOSConfig.h provided.
      In the generated build directory, open the "output.map" file. You can see that in the ".heap" section, the "end" symbol has an address of "[!provide]". This indicates that the "end" symbol does not take the value specified in the linker script but has been overridden.
      If this symbol's override was unintentional, this override is a bug in itself, regardless of its consequences.

      If you flash the generated binary it should work as expected.

      Next, open src/main.cpp and change the "#if 0" in "#if 1", rebuild and reflash. It should not work anymore.
      If debugged, you would observe that the memory allocated by the vector is not located in the ".heap" section as we would expect.

      If proposed fix is applied, the libs are rebuilt, and the application is rebuilt with the new libs, you will see that in the "output.map" file the "end" symbol is not overridden anymore and matches the beginning the of ".heap section". The generated binary will also work without problems.

      Attachments

        1. QTMCU-99.zip
          36 kB
          Tuukka Kettunen
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            rakeller Rainer Keller
            mtoumson Mathieu Toumson
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes