[arm-gnu] Static template member in assigned section -- section type conflict
Carlos O'Donell
carlos_odonell at mentor.com
Fri Jan 27 18:45:32 UTC 2012
On 1/27/2012 12:19 AM, Gene Smith wrote:
>> 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
It doesn't work.
Use -save-temps and -Wl,-Map,linkermap.txt to see where everything goes (I suggest also printing &priv, &priv.a, and &priv.b).
You will see that priv.a goes into .s1 because of the section attribute in main.cpp.
However, because of the explicit instantiation priv.b goes into .bss, it's a specialization, and isn't shared amongst other uses of the template so it's not common data anymore (which is the normal way in which static data is handled).
e.g.
6009d8 &priv
6009d8 &priv.a
.s1 0x00000000006009d8 0x4
.s1 0x00000000006009d8 0x4 priv2.o
6009f0 &priv.b
.bss 0x00000000006009e0 0x18
...
.bss 0x00000000006009f0 0x0 priv2-temp.o
.bss._ZN5outerIiiE1bE
0x00000000006009f0 0x4 priv2-temp.o
0x00000000006009f0 _ZN5outerIiiE1bE
You are perilously poking under the hood of a complex system :-)
Cheers,
Carlos.
--
Carlos O'Donell
Mentor Graphics / CodeSourcery
carlos at codesourcery.com
+1 (613) 963 1026
More information about the arm-gnu
mailing list