[SOLVED] Populating "Unused" variable for boot flag - linker & optimization

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [SOLVED] Populating "Unused" variable for boot flag - linker & optimization

    I am trying to use SEGGER Embedded Studio with the GNU Linker to place a flag at a specific location in flash memory to allow the bootloader and application to interact via the flag.



    To do this, I am using this line in flash_placement.xml:

    XML Source Code

    1. <ProgramSection alignment="4" load="Yes" name=". flag_boot" start="$(FLAG_BOOT)" size="4"/>
    Along with this Section Placement Macro in the Linker Options:

    Source Code

    1. FLAG_BOOT=0x36300
    This is the application code that’s used to populate this flag:

    C Source Code

    1. #pragma location=".flag_boot";
    2. const uint8_t __attribute__((section (".flag_boot"))) flag_boot[4] =
    3. {
    4. 0x01,
    5. 0x02,
    6. 0x03,
    7. 0x04,
    8. };


    I have two build configurations (“Release” and “Debug”) that are very similar. The “Release” build configuration places the correct 0x01 0x02 0x03 0x04 values at location 0x3600. The “Debug” build configuration does not, but instead fills with 0xFF 0xFF 0xFF 0xFF. I suspect that the problem may be related to optimization, as the flag_boot variable is not used anywhere in the application code, but is simply used to populate the values for the bootloader to access.



    The “Release” build configuration has the following optimization settings:

    For the Solution, Code Generation => Optimization Level is set to “Optimize For Size”. Strangely, this is not actually one of the options in the dropdown menu for that item.

    For the Project, Code Generation => Optimization Level is set to “Level 0”.



    The “Debug” build configuration has the following optimization settings:

    For the Solution, Code Generation => Optimization Level is set to “None”.

    For the Project, Code Generation => Optimization Level is set to “None”.



    If I change the Solution, Code Generation => Optimization Level of the “Release” build configuration to “Level 0” it no longer works – the locations get filled with 0xFF.



    Once I make a change to the “Release” build configuration I’m not able to get it back to the state where it works without reverting to the prior version. This makes me wonder if I have some type of problem with the .emProject file.



    In any case, I’m wondering if anyone can help me determine how to get the flag_boot variable placed into the specified location regardless of optimization settings, as I will want to turn optimization on at some point. I’ve tried the following so far without success:


    C Source Code

    1. const uint8_t __attribute__((section (".flag_boot"))) flag_boot[4] =
    2. const volatile uint8_t __attribute__((section (".flag_boot"))) flag_boot[4] =
    3. const uint8_t __attribute__((section (".flag_boot"),used)) flag_boot[4] =
    Thanks in advance,

    Andy
  • Hello Andy,

    Thank you for your inquiry.
    The compiler and linker may essentially optimize "unreachable/unused" code under certain circumstances. In the Debug config the compiler outputs the data section however the Linker decides that it is unused and discards it.
    With size optimization it is most likely that the compiler merges the data with other similar data so multiple symbols point to the data and thus the data is no longer "unused" (only assumptions here, but this is most likely what is happening). So the Linker does no longer assume the data is unused and does not discard it.

    To avoid such behaviour you can specifically instruct the linker to not discard certain symbols, no matter how they are used or where they are placed.
    You can do so under project options Linker->Keep Symbols.
    If you define flag_boot under keep symbols it should no longer be discarded.
    In source "const uint8_t __attribute__((section (".flag_boot"))) flag_boot[4] =" should be correct.
    Can you confirm this?

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hi Andy,

    Great to hear that you are up and running again.
    We will consider this thread as solved now.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.