[PATCH] D85056: [ELF] Add --keep-section to expose linkerscript KEEP directive as a linker flag

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 13 16:11:32 PDT 2020


MaskRay added a comment.

In D85056#2188969 <https://reviews.llvm.org/D85056#2188969>, @psmith wrote:

> I do have some sympathy with wanting to use a command line option to keep an individual section from the command line as it has been useful in Arm's proprietary linker, although this is mainly due to the convenience of not having to create or modify another file.
>
> One observation I'd make about the proposed implementation is that it looks like it only implements a subset of the Linker Script KEEP command that supports precise matches of a section name. Quoting from the GNU linker manual:
>
>   When link-time garbage collection is in use (`--gc-sections'), it is often useful to mark sections that should not be eliminated. This is accomplished by surrounding an input section's wildcard entry with KEEP(), as in KEEP(*(.init)) or KEEP(SORT_BY_NAME(*)(.ctors)).
>
> That permits the full power of the input section description to discriminate via object, as there can be many sections with the same name. Wildcards can sometimes be useful too.
>
> I think it would be worth considering a richer interface for keep-sections. In Arm's proprietary linker we permitted a similar syntax as for the equivalent linker script. This did mean quoting or escaping parentheses but did provide equivalence in what could be achieved.

Having a non-INSERT-AFTER/BEFORE SECTIONS command is considered an external linker script and can change the default layout decisions. I can also feel sympathy with the users but I am also wary of adding these non-orthogonal features. The recent D76482 <https://reviews.llvm.org/D76482> (`__build_id_start = .`) and this patch make me think of output section descriptions (a fragment of a SECTIONS command) which do not affect section layout.

@psmith @grimar I think we probably should start a conversation with binutils about such a feature. If they find needs as well, we will have a common ground, it'd be great. They need to be given the decision making opportunity to reduce the risk they create a similar but incompatible feature in the future.

One idea is:

  OVERRIDE SECTIONS {
    .foo : { KEEP(*(.foo)) }
    .bar : { KEEP(*(.bar)) }
    sym = .;   // symbol assignments are disallowed
  }

The output section descriptions will override .foo & .bar in the external linker script.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85056



More information about the llvm-commits mailing list