[EXT_EP-11021] Compiler places dynamically initialized const variables for instance of a class in the .const section instead of .data:variable_name Created: 27/Jan/23  Updated: 02/Apr/24  Resolved: 15/Sep/23

Status: Fixed
Project: Embedded Software & Tools
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Medium
Reporter: TI User Assignee: TI User
Resolution: Fixed Votes: 1
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Product: Code Generation Tools
Internal ID: CODEGEN-10848
Forum URL: https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/171/t/1190358
Found In Release: C7000_1.4.0.LTS
C6000_8.2.0
C2000_22.6.0.LTS
C6000_8.1.2
C6000_8.3.0
C7000_2.1.0.LTS
C6000_7.4.0
C2000_21.6.0.LTS
C7000_3.1.0.LTS
C2000_18.12.0.LTS
C2000_20.2.0.LTS
Fix In Release: C7000_2.1.3.LTS*
C7000_3.1.1.LTS
C2000_22.6.1.LTS
C6000_8.3.13
C2000_21.6.2.LTS*
Affected Platform/Device: default
Workaround: Explicitly set the variable that is incorrectly being placed in .const to instead use .data section:
#pragma DATA_SECTION(".data:instance")
volatile static const use_variables instance = {
    has_static_members::c_Pi, // OK
    has_static_members::c_2Pi, // WRITTEN!
    has_static_members::c_3Pi, // WRITTEN!
    has_static_members::c_beef, // OK
    has_static_members::c_var // WRITTEN!
};

 Description   

When using --abi=eabi, the compiler incorrectly places class variables that will be dynamically initiailized into the .const section instead of the .data section. This results in incorrect runtime initialization of the variables that were incorrectly placed in .const

More details:
----------------
The attached file has these lines ...

#include <stdint.h>

class has_static_members final
{
public:
    static const float c_Pi = 3.14f;
    static const float c_2Pi;       // init'd elsewhere
    static const float c_3Pi;       // init'd elsewhere
    static const uint32_t c_beef = 0xDEADBEEF;
    static const uint32_t c_var;    // init'd elsewhere
};

class use_variables final
{
public:
    const float not_static_c_Pi;
    const float not_static_c_2Pi;
    const float not_static_c_3Pi;
    const uint32_t not_static_c_beef;
    const uint32_t not_static_c_var;
};

// This instance of the class is put in the .const section, then a function
// is auto-generated which writes to some of these members variables
volatile static const use_variables instance = {
    has_static_members::c_Pi,     // OK
    has_static_members::c_2Pi,    // WRITTEN!
    has_static_members::c_3Pi,    // WRITTEN!
    has_static_members::c_beef,   // OK
    has_static_members::c_var     // WRITTEN!
};

Build it ...

% cl2000 --abi=eabi -o0 -s file.cpp

Inspect the resulting assembly file. These lines show that the instance of the class is in .const ...

        .sect   ".const"
        .align  2
        .elfsym ||instance||,SYM_SIZE(10)
||instance||:
        .xfloat $strtod("0x1.91eb86p+1")                ; instance.not_static_c_Pi @ 0
        .xfloat $strtod("0x0p+0")               ; instance.not_static_c_2Pi @ 32
        .xfloat $strtod("0x0p+0")               ; instance.not_static_c_3Pi @ 64
        .bits           0xdeadbeef,32
                        ; instance.not_static_c_beef @ 96
        .bits           0,32
                        ; instance.not_static_c_var @ 128

These lines are the beginning of an auto-generated function which writes to some members in instance ...

||__sti___8_file_cpp_b59a82a8||:
;*** 25 -----------------------    instance.not_static_c_2Pi = has_static_members::c_2Pi;
;*** 25 -----------------------    instance.not_static_c_3Pi = has_static_members::c_3Pi;
;*** 25 -----------------------    instance.not_static_c_var = has_static_members::c_var;

The function _sti__8_file_cpp_b59a82a8 is called by the startup code. Thus, by the time main starts, the mistaken writes to these members of instance have already occurred.


Generated at Thu Apr 10 10:03:50 CDT 2025 using Jira 9.12.17#9120017-sha1:aba4002bcd633f188b6a4bb5dd8a0e1f20b79ee4.