[all-commits] [llvm/llvm-project] 40a42f: [ELF] Make SORT_INIT_PRIORITY support .ctors.N

Fangrui Song via All-commits all-commits at lists.llvm.org
Thu Nov 12 08:56:32 PST 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: 40a42f9f3fe9bf34f2d365705e72f12960cbb16c
      https://github.com/llvm/llvm-project/commit/40a42f9f3fe9bf34f2d365705e72f12960cbb16c
  Author: Fangrui Song <i at maskray.me>
  Date:   2020-11-12 (Thu, 12 Nov 2020)

  Changed paths:
    M lld/ELF/OutputSections.cpp
    M lld/test/ELF/linkerscript/sort-init.s

  Log Message:
  -----------
  [ELF] Make SORT_INIT_PRIORITY support .ctors.N

Input sections `.ctors/.ctors.N` may go to either the output section `.init_array` or the output section `.ctors`:

* output `.ctors`: currently we sort them by name. This patch changes to sort by priority from high to low. If N in `.ctors.N` is in the form of %05u, there is no semantic difference. Actually GCC and Clang do use %05u. (In the test `ctors_dtors_priority.s` and Gold's test `gold/testsuite/script_test_14.s`, we can see %03u, but they are not really produced by compilers.)
* output `.init_array`: users can provide an input section description `SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)` to mix `.init_array.*` and `.ctors.*`. This can make .init_array.N and .ctors.(65535-N) interchangeable.

With this change, users can mix `.ctors.N` and `.init_array.N` in `.init_array` (PR44698 and PR48096) with linker scripts. As an example:

```
SECTIONS {
  .init_array : {
    *(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*))
    *(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)
  }
} INSERT AFTER .fini_array;
SECTIONS {
  .fini_array : {
    *(SORT_BY_INIT_PRIORITY(.fini_array.* .dtors.*))
    *(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)
  }
} INSERT BEFORE .init_array;
```

Reviewed By: psmith

Differential Revision: https://reviews.llvm.org/D91187




More information about the All-commits mailing list