Home > Back-end >  Section attribute within template
Section attribute within template

Time:11-08

I'd like to ask why this doesn't relocate the data into the desired section,

template <typename T>
struct Retram {
  static T data;

  inline auto operator=(const T& other) {
    data = other;
    return *this;
  }

  operator auto &() const {
    return data;
  }

  operator auto *() const {
    return &data;
  }
};

template <typename T>
__attribute__((section(".retram"))) T Retram<T>::data = T();

when this does

struct Retram {
  static int data;

  inline auto operator=(const int& other) {
    data = other;
    return *this;
  }

  operator auto &() const {
    return data;
  }

  operator auto *() const {
    return &data;
  }
};

__attribute__((section(".retram"))) int Retram::data = int();

You are welcome to provide alternate pretty solutions, but I'd still like to understand why this doesn't work.

Both compile, but the template version will NOT relocate the symbol as desired.

Minimal reproducible example:

Retram<int> my_retram_value;
my_retram_value = 42;

printf("retram value: %i\n", *my_retram_value);

CodePudding user response:

Gcc ignores attributes in templates generally. This is a know bug for many years now. There are more than 10 open bug reports related to this issue. See bugzilla. The problem affects not only member functions but also variable templates and free template functions.

Someone has started to work on it but after running in trouble with -flto he gave up. comments for patch Currently it seems that there is nobody interested in fixing this very old issue.

Especially for embedded devices this is a real show stopper. This makes it impossible to place templated data to flash or makes it impossible to run interrupt handlers in templated code and much more.

People running again on one of the bugs should make a comment in the bug report for the fields "Known to fail" and "Last confirmed". Maybe this makes it visible that the problem persists and the fix is interesting for some more people. But as always in open source projects: Products get better by helping them. I have no experience on gcc development, sorry :-)

  • Related