[all-commits] [llvm/llvm-project] 73d01a: [ELF] Sort by input order within an input section ...

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


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

  Changed paths:
    M lld/ELF/LinkerScript.cpp
    M lld/test/ELF/linkerscript/exclude-multiple.s
    M lld/test/ELF/linkerscript/sort2.s

  Log Message:
  -----------
  [ELF] Sort by input order within an input section description

According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:

* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order

This patch implements the behavior. The interaction with `SORT*` and --sort-section is:

Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.

> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.

As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order.  If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.

This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.

The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096

Reviewed By: grimar, psmith

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




More information about the All-commits mailing list