Unexpected behavior regarding linking sections

Michael Matz matz@suse.de
Thu Dec 13 17:01:00 GMT 2018


On Thu, 13 Dec 2018, Alan Modra wrote:

> > I'm guessing it's too hot right now in Australia and there is some 
> > kind of biological explanation for your behavior.

It's generally advisable to listen to Alan carefully.  Had you done so you 
would have done this:

% ld --verbose | less

Then with the remarks about init/fini_array and ctors/dtors and 
crtbegin/crtend you would have found this amongst other things:

  .init_array     :
    KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o 
                                      *crtend?.o ) .ctors))
  .fini_array     :
    KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o 
                                      *crtend?.o ) .dtors))

  .dtors          :
    KEEP (*crtbegin.o(.dtors))
    KEEP (*crtbegin?.o(.dtors))
    KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
    KEEP (*(SORT(.dtors.*)))
    KEEP (*(.dtors))

And there you have your explanation: the .dtors section from your start.o
file is placed into the init_array section (because it's not named 
crtbegin.o), but the the one from the crtend.o file is placed into the 
.dtors section.  Same with .ctors.  If your startup code doesn't support 
init_array/fini_array you can't used the standard ELF linker scripts.


More information about the Binutils mailing list