This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
[i386-PE] bug with ordering of init_priority vs default ctors (and possible fix)
- From: Danny Smith <danny_smith_0000 at yahoo dot co dot nz>
- To: binutils <binutils at sources dot redhat dot com>
- Date: Sat, 17 Apr 2004 20:07:17 +1200 (NZST)
- Subject: [i386-PE] bug with ordering of init_priority vs default ctors (and possible fix)
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?
Danny
* ld/scriptempl/pe.sc: Put numbered .ctors.* after .ctors
with default priority.
*** pe.sc.orig Mon Nov 24 23:02:05 2003
--- pe.sc Sat Apr 17 02:33:45 2004
*************** SECTIONS
*** 58,66 ****
*(.glue_7t)
*(.glue_7)
${CONSTRUCTING+ ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;
! LONG (-1); *(SORT(.ctors.*)); *(.ctors); *(.ctor); LONG (0); }
${CONSTRUCTING+ ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;
! LONG (-1); *(SORT(.dtors.*)); *(.dtors); *(.dtor); LONG (0); }
${RELOCATING+ *(.fini)}
/* ??? Why is .gcc_exc here? */
${RELOCATING+ *(.gcc_exc)}
--- 58,66 ----
*(.glue_7t)
*(.glue_7)
${CONSTRUCTING+ ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;
! LONG (-1);*(.ctors); *(.ctor); *(SORT(.ctors.*)); LONG (0); }
${CONSTRUCTING+ ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;
! LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*)); LONG (0); }
${RELOCATING+ *(.fini)}
/* ??? Why is .gcc_exc here? */
${RELOCATING+ *(.gcc_exc)}
Find local movie times and trailers on Yahoo! Movies.
http://au.movies.yahoo.com