This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Bug in 'ld' - objects with init_priority doesn't work with shared library


Hi Prashanth,
  Thanks for bug report, testcase, analysis and patch.  It's nice to
receive all four!  I'm copying your email to the binutils list as
explanation for the patch I'm about to make, which differs from yours
in that it handles a crtbegin{S,T}.o and crtendT.o as well.

On Mon, Apr 14, 2003 at 04:06:33PM +0530, Prashanth Tamraparni wrote:
> Hi Alan,
>       This is a bug on 'ld' part of binutils where a c++ test program with
> objects  having attributes init_priority are NOT being called when compiled
> as a shared library but works fine if compiled together with the main
> program.  I have attached below the two files:  main.C   &&  shared.C
> 
> Steps to Reproduce:
> 1.g++ -shared shared.C -o libb.a -fpic
> 2.g++ main.C -L . -lb
> 3.a.out
> 
> Actual Results:
> S::(1)
> S::(7)
> S::(8)
> S::(9)
> S::~S(9)
> S::~S(8)
> S::~S(7)
> S::~S(1)
> 
> Expected Results (produced with g++ main.C shared.C; a.out):
> S::(2)
> S::(5)
> S::(4)
> S::(3)
> S::(6)
> S::(1)
> S::(7)
> S::(8)
> S::(9)
> S::~S(9)
> S::~S(8)
> S::~S(7)
> S::~S(1)
> S::~S(6)
> S::~S(3)
> S::~S(4)
> S::~S(5)
> S::~S(2)
> 
> Looking at the objdump of the shared object file, we notice that the
> function pointers for the missing objects are not inserted between the
> fields 0xffffffff and 0x00000000.
> 
> This bug was noticed on Red Hat 7.3 (binutils-2.11.93.0.2) and also on
> SLES8 (binutils-2.12.90.0.15).  But I feel that this bug is also seen on
> the latest version of binutils from gnu.org (binutils-2.13.2.1).  Below is
> the patch attached for this version of binutils.
> 
> As we know, the linker uses the two files crtbeginS.o and crtendS.o (shared
> versions of crtbegin.o and crtend.o) while creating shared libraries. But
> when we look at the source code of 'ld' in binutilsI notice that the linker
> scripts (in 'scripttempl' directory) do NOT consider these shared version
> files.
> 
> For ex, if we look at ld/scripttempl/elf.sc, I notice that while
> consturcting the .ctors OR .dtors section, only crtend.o is excluded ...but
> not crtendS.o.   This caused the field 0x00000000 (from crtendS.o) to be
> inserted even before other constructors were sorted.  Hence the below
> attached patch makes this change of excluding crtendS.o while constructing
> .ctors and .dtors section.
> 
> I also feel that this change is to be incorporated in ALL the scripts of
> 'scripttempl' directory dealing with crtbegin.o/crtend.o (for files
> crisaout.sc, elf_chaos.sc, elfd10v.sc, elfd30v.sc, elfm68hc11.sc,
> elfm68hc12.sc, elf.sc, mmo.sc, v850.sc, xstormy16.sc in ld/scriptempl
> directory) Please let me know your view about this.

Some of these probably don't matter as they don't support shared libs,
however, I think it won't hurt to apply the patch to all.  Hmm, I'm
excluding elfm68hc* as those scripts have the sorting entries commented
out for some reason.

> I have attached below the patch for elf.sc file alone for
> binutils-2.13.2.1.  If the patch looks ok and if you would like to have all
> the above files being patched, please let me know.

	From Prashanth Tamraparni <prashanth_t at in dot ibm dot com>
	* scripttempl/elf.sc (CTOR): Handle crtbeginS.o, crtendS.o and
	other variants of crtbegin.o, crtend.o.
	(DTOR): Likewise.
	* scripttempl/crisaout.sc (ctors, dtors): Likewise.
	* scripttempl/elf_chaos.sc (CTOR, DTOR): Likewise.
	* scripttempl/elfd10v.sc (CTOR, DTOR): Likewise.
	* scripttempl/elfd30v.sc (CTOR, DTOR): Likewise.
	* scripttempl/elfxtensa.sc (CTOR, DTOR): Likewise.
	* scripttempl/iq2000.sc (CTOR, DTOR): Likewise.
	* scripttempl/mmo.sc (ctors, dtors): Likewise.
	* scripttempl/xstormy16.sc (CTOR, DTOR): Likewise.

Index: ld/scripttempl/elf.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/elf.sc,v
retrieving revision 1.35
diff -u -p -r1.35 elf.sc
--- ld/scripttempl/elf.sc	25 Sep 2002 07:21:08 -0000	1.35
+++ ld/scripttempl/elf.sc	14 Apr 2003 12:38:16 -0000
@@ -127,14 +127,14 @@ CTOR=".ctors        ${CONSTRUCTING-0} : 
        doesn't matter which directory crtbegin.o
        is in.  */
 
-    KEEP (*crtbegin.o(.ctors))
+    KEEP (*crtbegin*.o(.ctors))
 
     /* We don't want to include the .ctor section from
        from the crtend.o file until after the sorted ctors.
        The .ctor section from the crtend file contains the
        end of ctors marker and it must be last */
 
-    KEEP (*(EXCLUDE_FILE (*crtend.o $OTHER_EXCLUDE_FILES) .ctors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o $OTHER_EXCLUDE_FILES) .ctors))
     KEEP (*(SORT(.ctors.*)))
     KEEP (*(.ctors))
     ${CONSTRUCTING+${CTOR_END}}
@@ -142,8 +142,8 @@ CTOR=".ctors        ${CONSTRUCTING-0} : 
 DTOR=".dtors        ${CONSTRUCTING-0} :
   {
     ${CONSTRUCTING+${DTOR_START}}
-    KEEP (*crtbegin.o(.dtors))
-    KEEP (*(EXCLUDE_FILE (*crtend.o $OTHER_EXCLUDE_FILES) .dtors))
+    KEEP (*crtbegin*.o(.dtors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o $OTHER_EXCLUDE_FILES) .dtors))
     KEEP (*(SORT(.dtors.*)))
     KEEP (*(.dtors))
     ${CONSTRUCTING+${DTOR_END}}
Index: ld/scripttempl/crisaout.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/crisaout.sc,v
retrieving revision 1.4
diff -u -p -r1.4 crisaout.sc
--- ld/scripttempl/crisaout.sc	30 Nov 2000 15:08:44 -0000	1.4
+++ ld/scripttempl/crisaout.sc	14 Apr 2003 12:38:16 -0000
@@ -48,15 +48,15 @@ SECTIONS
     /* Cater to linking from ELF.  */
     ${CONSTRUCTING+ PROVIDE(___ctors = .);}
     ${CONSTRUCTING+ ___elf_ctors_dtors_begin = .;}
-    ${CONSTRUCTING+ KEEP (*crtbegin.o(.ctors))}
-    ${CONSTRUCTING+ KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))}
+    ${CONSTRUCTING+ KEEP (*crtbegin*.o(.ctors))}
+    ${CONSTRUCTING+ KEEP (*(EXCLUDE_FILE (*crtend*.o) .ctors))}
     ${CONSTRUCTING+ KEEP (*(SORT(.ctors.*)))}
     ${CONSTRUCTING+ KEEP (*(.ctors))}
     ${CONSTRUCTING+ PROVIDE(___ctors_end = .);}
 
     ${CONSTRUCTING+ PROVIDE(___dtors = .);}
-    ${CONSTRUCTING+ KEEP (*crtbegin.o(.dtors))}
-    ${CONSTRUCTING+ KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))}
+    ${CONSTRUCTING+ KEEP (*crtbegin*.o(.dtors))}
+    ${CONSTRUCTING+ KEEP (*(EXCLUDE_FILE (*crtend*.o) .dtors))}
     ${CONSTRUCTING+ KEEP (*(SORT(.dtors.*)))}
     ${CONSTRUCTING+ KEEP (*(.dtors))}
     ${CONSTRUCTING+ PROVIDE(___dtors_end = .);}
Index: ld/scripttempl/elf_chaos.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/elf_chaos.sc,v
retrieving revision 1.1
diff -u -p -r1.1 elf_chaos.sc
--- ld/scripttempl/elf_chaos.sc	27 May 2002 17:52:56 -0000	1.1
+++ ld/scripttempl/elf_chaos.sc	14 Apr 2003 12:38:16 -0000
@@ -116,22 +116,22 @@ CTOR="
        doesn't matter which directory crtbegin.o
        is in.  */
 
-    KEEP (*crtbegin.o(.ctors))
+    KEEP (*crtbegin*.o(.ctors))
 
     /* We don't want to include the .ctor section from
        from the crtend.o file until after the sorted ctors.
        The .ctor section from the crtend file contains the
        end of ctors marker and it must be last */
 
-    KEEP (*(EXCLUDE_FILE (*crtend.o $OTHER_EXCLUDE_FILES) .ctors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o $OTHER_EXCLUDE_FILES) .ctors))
     KEEP (*(SORT(.ctors.*)))
     KEEP (*(.ctors))
     ${CONSTRUCTING+${CTOR_END}}
 "
 DTOR="
     ${CONSTRUCTING+${DTOR_START}}
-    KEEP (*crtbegin.o(.dtors))
-    KEEP (*(EXCLUDE_FILE (*crtend.o $OTHER_EXCLUDE_FILES) .dtors))
+    KEEP (*crtbegin*.o(.dtors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o $OTHER_EXCLUDE_FILES) .dtors))
     KEEP (*(SORT(.dtors.*)))
     KEEP (*(.dtors))
     ${CONSTRUCTING+${DTOR_END}}
Index: ld/scripttempl/elfd10v.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/elfd10v.sc,v
retrieving revision 1.7
diff -u -p -r1.7 elfd10v.sc
--- ld/scripttempl/elfd10v.sc	30 Aug 2002 11:34:40 -0000	1.7
+++ ld/scripttempl/elfd10v.sc	14 Apr 2003 12:38:16 -0000
@@ -20,14 +20,14 @@ CTOR=".ctors ${CONSTRUCTING-0} : 
        doesn't matter which directory crtbegin.o
        is in.  */
 
-    KEEP (*crtbegin.o(.ctors))
+    KEEP (*crtbegin*.o(.ctors))
 
     /* We don't want to include the .ctor section from
        from the crtend.o file until after the sorted ctors.
        The .ctor section from the crtend file contains the
        end of ctors marker and it must be last */
 
-    KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o) .ctors))
     KEEP (*(SORT(.ctors.*)))
     KEEP (*(.ctors))
     ${CONSTRUCTING+${CTOR_END}}
@@ -36,8 +36,8 @@ CTOR=".ctors ${CONSTRUCTING-0} : 
 DTOR=" .dtors       ${CONSTRUCTING-0} :
   {
     ${CONSTRUCTING+${DTOR_START}}
-    KEEP (*crtbegin.o(.dtors))
-    KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
+    KEEP (*crtbegin*.o(.dtors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o) .dtors))
     KEEP (*(SORT(.dtors.*)))
     KEEP (*(.dtors))
     ${CONSTRUCTING+${DTOR_END}}
Index: ld/scripttempl/elfd30v.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/elfd30v.sc,v
retrieving revision 1.6
diff -u -p -r1.6 elfd30v.sc
--- ld/scripttempl/elfd30v.sc	30 Sep 2002 09:31:31 -0000	1.6
+++ ld/scripttempl/elfd30v.sc	14 Apr 2003 12:38:16 -0000
@@ -12,14 +12,14 @@ CTOR=".ctors ${CONSTRUCTING-0} : 
        doesn't matter which directory crtbegin.o
        is in.  */
 
-    KEEP (*crtbegin.o(.ctors))
+    KEEP (*crtbegin*.o(.ctors))
 
     /* We don't want to include the .ctor section from
        from the crtend.o file until after the sorted ctors.
        The .ctor section from the crtend file contains the
        end of ctors marker and it must be last */
 
-    KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o) .ctors))
     KEEP (*(SORT(.ctors.*)))
     KEEP (*(.ctors))
     ${CONSTRUCTING+ __CTOR_END__ = .; }
@@ -28,8 +28,8 @@ CTOR=".ctors ${CONSTRUCTING-0} : 
 DTOR="  .dtors	${CONSTRUCTING-0} :
   {
     ${CONSTRUCTING+ __DTOR_LIST__ = .; }
-    KEEP (*crtbegin.o(.dtors))
-    KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
+    KEEP (*crtbegin*.o(.dtors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o) .dtors))
     KEEP (*(SORT(.dtors.*)))
     KEEP (*(.dtors))
     ${CONSTRUCTING+ __DTOR_END__ = .; }
Index: ld/scripttempl/elfxtensa.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/elfxtensa.sc,v
retrieving revision 1.1
diff -u -p -r1.1 elfxtensa.sc
--- ld/scripttempl/elfxtensa.sc	1 Apr 2003 15:50:31 -0000	1.1
+++ ld/scripttempl/elfxtensa.sc	14 Apr 2003 12:38:16 -0000
@@ -125,14 +125,14 @@ CTOR=".ctors        ${CONSTRUCTING-0} : 
        doesn't matter which directory crtbegin.o
        is in.  */
 
-    KEEP (*crtbegin.o(.ctors))
+    KEEP (*crtbegin*.o(.ctors))
 
     /* We don't want to include the .ctor section from
        from the crtend.o file until after the sorted ctors.
        The .ctor section from the crtend file contains the
        end of ctors marker and it must be last */
 
-    KEEP (*(EXCLUDE_FILE (*crtend.o $OTHER_EXCLUDE_FILES) .ctors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o $OTHER_EXCLUDE_FILES) .ctors))
     KEEP (*(SORT(.ctors.*)))
     KEEP (*(.ctors))
     ${CONSTRUCTING+${CTOR_END}}
@@ -140,8 +140,8 @@ CTOR=".ctors        ${CONSTRUCTING-0} : 
 DTOR=".dtors        ${CONSTRUCTING-0} :
   {
     ${CONSTRUCTING+${DTOR_START}}
-    KEEP (*crtbegin.o(.dtors))
-    KEEP (*(EXCLUDE_FILE (*crtend.o $OTHER_EXCLUDE_FILES) .dtors))
+    KEEP (*crtbegin*.o(.dtors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o $OTHER_EXCLUDE_FILES) .dtors))
     KEEP (*(SORT(.dtors.*)))
     KEEP (*(.dtors))
     ${CONSTRUCTING+${DTOR_END}}
Index: ld/scripttempl/iq2000.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/iq2000.sc,v
retrieving revision 1.1
diff -u -p -r1.1 iq2000.sc
--- ld/scripttempl/iq2000.sc	4 Jan 2003 02:04:44 -0000	1.1
+++ ld/scripttempl/iq2000.sc	14 Apr 2003 12:38:17 -0000
@@ -84,14 +84,14 @@ CTOR=".ctors ${CONSTRUCTING-0} : 
        doesn't matter which directory crtbegin.o
        is in.  */
 
-    KEEP (*crtbegin.o(.ctors))
+    KEEP (*crtbegin*.o(.ctors))
 
     /* We don't want to include the .ctor section from
        from the crtend.o file until after the sorted ctors.
        The .ctor section from the crtend file contains the
        end of ctors marker and it must be last */
 
-    KEEP (*(EXCLUDE_FILE (*crtend.o $OTHER_EXCLUDE_FILES) .ctors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o $OTHER_EXCLUDE_FILES) .ctors))
     KEEP (*(SORT(.ctors.*)))
     KEEP (*(.ctors))
     ${CONSTRUCTING+${CTOR_END}}
@@ -100,8 +100,8 @@ CTOR=".ctors ${CONSTRUCTING-0} : 
 DTOR=" .dtors       ${CONSTRUCTING-0} :
   {
     ${CONSTRUCTING+${DTOR_START}}
-    KEEP (*crtbegin.o(.dtors))
-    KEEP (*(EXCLUDE_FILE (*crtend.o $OTHER_EXCLUDE_FILES) .dtors))
+    KEEP (*crtbegin*.o(.dtors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o $OTHER_EXCLUDE_FILES) .dtors))
     KEEP (*(SORT(.dtors.*)))
     KEEP (*(.dtors))
     ${CONSTRUCTING+${DTOR_END}}
Index: ld/scripttempl/mmo.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/mmo.sc,v
retrieving revision 1.3
diff -u -p -r1.3 mmo.sc
--- ld/scripttempl/mmo.sc	1 Feb 2002 08:24:02 -0000	1.3
+++ ld/scripttempl/mmo.sc	14 Apr 2003 12:38:17 -0000
@@ -29,8 +29,8 @@ SECTIONS
     ${RELOCATING+ PROVIDE (__ctors_start = .);}
     ${RELOCATING+ PROVIDE (_ctors = .);}
     ${RELOCATING+ PROVIDE (__ctors = .);}
-    ${RELOCATING+ KEEP (*crtbegin.o(.ctors))}
-    ${RELOCATING+ KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))}
+    ${RELOCATING+ KEEP (*crtbegin*.o(.ctors))}
+    ${RELOCATING+ KEEP (*(EXCLUDE_FILE (*crtend*.o) .ctors))}
     ${RELOCATING+ KEEP (*(SORT(.ctors.*)))}
     ${RELOCATING+ KEEP (*(.ctors))}
     ${RELOCATING+ PROVIDE (_ctors_end = .);}
@@ -40,8 +40,8 @@ SECTIONS
     ${RELOCATING+ PROVIDE (__dtors_start = .);}
     ${RELOCATING+ PROVIDE (_dtors = .);}
     ${RELOCATING+ PROVIDE (__dtors = .);}
-    ${RELOCATING+ KEEP (*crtbegin.o(.dtors))}
-    ${RELOCATING+ KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))}
+    ${RELOCATING+ KEEP (*crtbegin*.o(.dtors))}
+    ${RELOCATING+ KEEP (*(EXCLUDE_FILE (*crtend*.o) .dtors))}
     ${RELOCATING+ KEEP (*(SORT(.dtors.*)))}
     ${RELOCATING+ KEEP (*(.dtors))}
     ${RELOCATING+ PROVIDE (_dtors_end = .);}
Index: ld/scripttempl/xstormy16.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/xstormy16.sc,v
retrieving revision 1.2
diff -u -p -r1.2 xstormy16.sc
--- ld/scripttempl/xstormy16.sc	1 Feb 2002 18:00:36 -0000	1.2
+++ ld/scripttempl/xstormy16.sc	14 Apr 2003 12:38:17 -0000
@@ -68,14 +68,14 @@ CTOR=".ctors ${CONSTRUCTING-0} : 
        doesn't matter which directory crtbegin.o
        is in.  */
 
-    KEEP (*crtbegin.o(.ctors))
+    KEEP (*crtbegin*.o(.ctors))
 
     /* We don't want to include the .ctor section from
        from the crtend.o file until after the sorted ctors.
        The .ctor section from the crtend file contains the
        end of ctors marker and it must be last */
 
-    KEEP (*(EXCLUDE_FILE (*crtend.o $OTHER_EXCLUDE_FILES) .ctors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o $OTHER_EXCLUDE_FILES) .ctors))
     KEEP (*(SORT(.ctors.*)))
     KEEP (*(.ctors))
     ${CONSTRUCTING+${CTOR_END}}
@@ -84,8 +84,8 @@ CTOR=".ctors ${CONSTRUCTING-0} : 
 DTOR=" .dtors       ${CONSTRUCTING-0} :
   {
     ${CONSTRUCTING+${DTOR_START}}
-    KEEP (*crtbegin.o(.dtors))
-    KEEP (*(EXCLUDE_FILE (*crtend.o $OTHER_EXCLUDE_FILES) .dtors))
+    KEEP (*crtbegin*.o(.dtors))
+    KEEP (*(EXCLUDE_FILE (*crtend*.o $OTHER_EXCLUDE_FILES) .dtors))
     KEEP (*(SORT(.dtors.*)))
     KEEP (*(.dtors))
     ${CONSTRUCTING+${DTOR_END}}

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

Attachment: main.C
Description: Text document

Attachment: shared.C
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]