Uploaded image for project: 'Embedded Software & Tools'
  1. Embedded Software & Tools
  2. EXT_EP-11021

Compiler places dynamically initialized const variables for instance of a class in the .const section instead of .data:variable_name

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Medium Medium
    • Code Generation Tools
    • CODEGEN-10848
    • Hide
      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
      Show
      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
    • Hide
      C7000_2.1.3.LTS*
      C7000_3.1.1.LTS
      C2000_22.6.1.LTS
      C6000_8.3.13
      C2000_21.6.2.LTS*
      Show
      C7000_2.1.3.LTS* C7000_3.1.1.LTS C2000_22.6.1.LTS C6000_8.3.13 C2000_21.6.2.LTS*
    • default
    • Hide
      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!
      };
      Show
      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! };

      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.

            syncuser TI User
            syncuser TI User
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: