http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46770

--- Comment #17 from H.J. Lu <hjl.tools at gmail dot com> 2010-12-11 19:02:40 
UTC ---
(In reply to comment #16)
> On 12/11/2010 10:47 AM, hjl.tools at gmail dot com wrote:
> 
> > Linker supports sorting .ctors.NNNNN and .init_array.NNNN.
> > Within .ctors.NNNNN and .init_array.NNNN, the order is defined.
> > And ctors.NNNNN will be called before .init_array.NNNN.
> 
> Really?  I thought all of ctors.NNNN got sorted into a single big block.

Linker script has

 .preinit_array     :
  {
    PROVIDE_HIDDEN (__preinit_array_start = .);
    KEEP (*(.preinit_array))
    PROVIDE_HIDDEN (__preinit_array_end = .);
  }
  .init_array     :
  {
    PROVIDE_HIDDEN (__init_array_start = .);
    KEEP (*(SORT(.init_array.*)))
    KEEP (*(.init_array))
    PROVIDE_HIDDEN (__init_array_end = .);
  }
  .fini_array     :
  {
    PROVIDE_HIDDEN (__fini_array_start = .);
    KEEP (*(SORT(.fini_array.*)))
    KEEP (*(.fini_array))
    PROVIDE_HIDDEN (__fini_array_end = .);
  }
 .ctors          :
  {
    /* gcc uses crtbegin.o to find the start of
       the constructors, so we make sure it is
       first.  Because this is a wildcard, it
       doesn't matter if the user does not
       actually link against crtbegin.o; the
       linker won't look for a file to match a
       wildcard.  The wildcard also means that it
       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
       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 *crtend?.o ) .ctors))
    KEEP (*(SORT(.ctors.*)))
    KEEP (*(.ctors))
  }
  .dtors          :
  {
    KEEP (*crtbegin.o(.dtors))
    KEEP (*crtbegin?.o(.dtors))
    KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
    KEEP (*(SORT(.dtors.*)))
    KEEP (*(.dtors))
  }

> If the GNU linker (and GOLD) know how to interleave .ctors.NNNNN with
> .init_array.NNNNN so that constructor priority is honored even when
> mixing .ctors and .init_array, then I think we're OK.

I am not sure about GOLD. But it usually follows GNU linker.
For GNU linker, the constructor priority is honored within
.ctors.NNNNN and .init_array.NNNNN.  ctors.NNNNN will be called
before .init_array.NNNN.

Reply via email to