[i386-PE] bug with ordering of init_priority vs default ctors (and possible fix)
Christopher Faylor
cgf@alum.bu.edu
Sat Apr 17 23:19:00 GMT 2004
On Sat, Apr 17, 2004 at 08:07:17PM +1200, Danny Smith wrote:
>The g++ extension init_priority does not work correctly
>on cygwin or mingw, causing long-time failure of g++.dg/special/initp1.C.
>
>The bug appears to be due to the way ctors are ordered by ld.
>
>A simpler testcase. reproduced below, was reported to cygwin list
>
>// init_priority.cpp
>// Written by Rene Prillop
>// http://www.cygwin.com/ml/cygwin/2003-01/msg01155.html
>//
>#include <stdio.h>
>
>class CIntHolder{
>public:
> CIntHolder(int iValue) { m_iValue=iValue; }
> int m_iValue;
>};
>
>static int g_Counter;
>
>CIntHolder g_Object3(++g_Counter);
>CIntHolder __attribute__ ((init_priority(102))) g_Object2(++g_Counter);
>CIntHolder __attribute__ ((init_priority(101))) g_Object1(++g_Counter);
>CIntHolder g_Object4(++g_Counter);
>
>int main(){
> printf("%d %d %d %d\n", g_Object1.m_iValue,
> g_Object2.m_iValue,
> g_Object3.m_iValue,
> g_Object4.m_iValue);
> return 0;
>}
>
>When this is compiled and executed, it ouputs
>
>3 4 1 2
>
>rather than the expected 1 2 3 4
>
>Examination of the map file produced by ld, shows that the ctors
>with default priority are run before the init_priority ctors:
>
> 0x004015a0 ___CTOR_LIST__ = .
> 0x004015a0 __CTOR_LIST__ = .
> 0x004015a0 0x4 LONG 0xffffffff
> *(SORT(.ctors.*))
> .ctors.65433 0x004015a4 0x4 D:\TEMP/ccAhaaaa.o
> .ctors.65434 0x004015a8 0x4 D:\TEMP/ccAhaaaa.o
> *(.ctors)
> .ctors 0x004015ac 0x4 D:\TEMP/ccAhaaaa.o
> *(.ctor)
> 0x004015b0 0x4 LONG 0x0
>
>
>
>The following patch to ld/scriptempl/pe.sc fixes the above testcase
>as well as g++.dg/special/initp1.C. Is this the right thing to do?
Looks ok to me. Please check in.
cgf
More information about the Binutils
mailing list