[arm-gnu] Static template member in assigned section -- section type conflict

Gene Smith gds at chartertn.net
Fri Jan 27 05:19:05 UTC 2012


On 01/26/2012 12:01 PM, Carlos O'Donell wrote:
> On 1/22/2012 8:36 PM, Gene Smith wrote:
>> //@!    test: g++ bad2.cpp
>> //      bad2.cpp:18:25: error: priv causes a section type conflict
>>
>> template<class t1, class t2>
>> class outer
>> {
>> public:
>>      t1        a;
>>      static t2    b;
>> };
>>
>> template<class t1, class t2>
>> t2 outer<t1, t2>::b
>>          __attribute__((section(".s1")));
>>
>> int main (void)
>> {
>>      static outer<int, int>  priv __attribute__((section(".s1")));
>
> You can't place common data and non-common data in the same section.
>
> The conflict is as follows:
>
> * The variable t2 in outer->t2 is static and therefore common data,
>    there must only be one instance of t2 at all times.
>
> * Moving t2 into .s1 causes section .s1 to be marked as common data,
>    this is a flag that the linker must honour and it will bind other
>    references to t2 to the common location in .s1 (that's how static
>    is implemented in this case).
>
> * In main you say you want to store the *entire* contents of the
>    instantiated template into .s1, but .s1 is common data, and the
>    object is not common data, and that is a conflict.
>
>>      priv.a = 10;
>>      priv.b = 10;
>> }
>> //@! end of file
>
> The solution is to place priv into some *other* section.
>
> You will never have *all* of priv in the same place because
> the `static' clause makes that impossible for>  1 instances
> of the object (given the current implementation for static).
>
> Cheers,
> Carlos.

Thanks for the detailed explanation. However, I am not sure I follow it 
all and will look up the concept of "common" you refer to. While waiting 
for a response I found a solution by breaking up the code into 3 
separate files and I get the result I expect.

// temp.h
template<class t1, class t2>
class outer
{
public:
     t1        a;
     static t2    b;
};

// temp.cpp
#include "temp.h"

template<class t1, class t2>
t2 outer<t1, t2>::b
         __attribute__((section(".s1")));

// Explicit instantiation (NEW!)
template class outer<int,int>;

//main.c
#include "temp.h"
int main (void)
{
     static outer<int, int> priv __attribute__((section(".s1")));

     priv.a = 10;
     priv.b = 10;
}

arm-none-eabi-g++ -c temp.cpp
arm-none-eabi-g++ -c main.cpp
arm-none-eabi-g++ main.o temo.o
Produces a.out with only a warning about lack of _start. An objdump 
shows all objects/variables in the expected section.

Why does this work but having it all in a single file doesn't

-gene








More information about the arm-gnu mailing list