As I noted in the comment, the sorting order of .[cd]tors are
different from .{init,fini}_array's.
Details
Diff Detail
Event Timeline
Some comments.
ELF/OutputSections.cpp | ||
---|---|---|
805 | Can you name '3' instead of using the magic number? | |
838 | Drive by comment: not a huge fan of X and Y as variable names. Sure, their scope is narrow but if you can come up with something more self-explicative, it would be better. | |
845 | It is not obvious to me why you need stable sorting. Do you mind adding a comment? |
- Added a test for the crtbegin.o rule.
ELF/OutputSections.cpp | ||
---|---|---|
805 | We can name it SuffixLen or something like that, but it is probably too narrow. Added a comment instead. | |
838 | I rather prefer this way. I actually tried to name NameA and NameB, but X and Y grows on me as I modified this code many times before sending for the code review. | |
845 | Added a comment to the function for init/fini. |
ELF/OutputSections.cpp | ||
---|---|---|
805 | 3 is pretty obvious here, I can't think of a name that would provide any clarification. The function has a comment stating what the regex is. |
GNU ld 2.22 has the following default linker script for .ctors/.dtors sections. I am sure that other versions have the same declarations.
.ctors : { 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)) }
So the order should be follow:
- .ctors section from *crtbegin.o
- .ctors section from *crtbegin?.o
- .ctors sections from other object files except *crtend.o and *crtend?.o
- .ctors.* from any files sorted by priority
- .ctors sections from *crtend.o and *crtend?.o
As far as I can see now .ctors/.dtors sections from the crtend.o go first.
You also need to check for crtend. Its .ctors sections need to go after other .ctors.N sections.