[ELF] Allow SHF_LINK_ORDER sections to have sh_link=0
authorFangrui Song <maskray@google.com>
Wed, 5 Aug 2020 23:09:41 +0000 (16:09 -0700)
committerFangrui Song <i@maskray.me>
Wed, 5 Aug 2020 23:17:42 +0000 (16:17 -0700)
commitb216c80cc2496b87bf827260ce7e24dc62247d71
treec570eaea4fa2dc9a73e98082dbd4bfd91e7095e9
parent1adc494bce44f6004994deed61b30d4b71fe1d05
[ELF] Allow SHF_LINK_ORDER sections to have sh_link=0

Part of https://bugs.llvm.org/show_bug.cgi?id=41734

The semantics of SHF_LINK_ORDER have been extended to represent metadata
sections associated with some other sections (usually text).

The associated text section may be discarded (e.g. LTO) and we want the
metadata section to have sh_link=0 (D72899, D76802).

Normally the metadata section is only referenced by the associated text
section. sh_link=0 means the associated text section is discarded, and
the metadata section will be garbage collected. If there is another
section (.gc_root) referencing the metadata section, the metadata
section will be retained. It's the .gc_root consumer's job to validate
the metadata sections.

  # This creates a SHF_LINK_ORDER .meta with sh_link=0
  .section .meta,"awo",@progbits,0
  1:
  .section .meta,"awo",@progbits,foo
  2:

  .section .gc_root,"a",@progbits
  .quad 1b
  .quad 2b

Reviewed By: pcc, jhenderson

Differential Revision: https://reviews.llvm.org/D72904
lld/ELF/InputFiles.cpp
lld/ELF/InputSection.cpp
lld/ELF/Writer.cpp
lld/test/ELF/invalid/linkorder-invalid-sec2.test [deleted file]
lld/test/ELF/linkorder-mixed.s [new file with mode: 0644]