[PATCH] D91187: [ELF] Make SORT_INIT_PRIORITY support .ctors.N

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 10 15:02:32 PST 2020


MaskRay added inline comments.


================
Comment at: lld/ELF/OutputSections.cpp:474
     return endB;
-  StringRef x = a->name;
-  StringRef y = b->name;
-  assert(x.startswith(".ctors") || x.startswith(".dtors"));
-  assert(y.startswith(".ctors") || y.startswith(".dtors"));
-  x = x.substr(6);
-  y = y.substr(6);
-  return x < y;
+  return getPriority(a->name) > getPriority(b->name);
 }
----------------
psmith wrote:
> I'm not sure this is right. As I understand it compCtors will only be called when not using a linker script. 
> 
> In that case we'd have a separate .ctors and .dtors section to an existing .init_array section and in this case the rules for sorting ctors and dtors are different i.e. the default is sort by name (see comment above). It looks like this bit of code will change the default linker script to the equivalent of:
> ```
>    .ctors : { SORT_BY_INIT_PRIORITY (*(.ctors)) }
> ```
> from:
> ```
>    .ctors : { SORT_BY_NAME (*(.ctors)) }
> ```
> I think the old way was closer to BFD as that only changes to SORT_BY_INIT_PRIORITY if .ctors are placed in the .init_array OutputSection.
The idea is: `getPriority(a->name) > getPriority(b->name)` is equivalent to `a->name < b->name` for `.ctors.N` sections where N is %05u

If the output section is `.ctors`, the order of input sections: `.ctors, .ctors.00005, .ctors.00100`
(This is tested by `ctors_dtors_priority.s`)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91187/new/

https://reviews.llvm.org/D91187



More information about the llvm-commits mailing list