[LLD][ELF] Allow empty (.init|.preinit|.fini)_array to be RELRO
authorPeter Smith <peter.smith@arm.com>
Fri, 27 Mar 2020 10:15:02 +0000 (10:15 +0000)
committerPeter Smith <peter.smith@arm.com>
Tue, 31 Mar 2020 11:53:12 +0000 (12:53 +0100)
commit2539b4ae4765b14cd7774e801ba12f0a200ef734
tree1f999bea291e0470afa31dc9b5222f7c7491fb86
parentb37543750c766e8edc7e719ecae22b14a1ceed7f
[LLD][ELF] Allow empty (.init|.preinit|.fini)_array to be RELRO

The default GNU linker script uses the following idiom for the array
sections. I'll use .init_array here, but this also applies to
.preinit_array and .fini_array sections.

  .init_array    :
  {
    PROVIDE_HIDDEN (__init_array_start = .);
    KEEP (*(.init_array))
    PROVIDE_HIDDEN (__init_array_end = .);
  }

The C-library will take references to the _start and _end symbols to
process the array. This will make LLD keep the OutputSection even if there
are no .init_array sections. As the current check for RELRO uses the
section type for .init_array the above example with no .init_array
InputSections fails the checks as there are no .init_array sections to give
the OutputSection a type of SHT_INIT_ARRAY. This often leads to a
non-contiguous RELRO error message.

The simple fix is to a textual section match as well as a section type
match.

Differential Revision: https://reviews.llvm.org/D76915
lld/ELF/Writer.cpp
lld/test/ELF/relro-init-fini-script.s [new file with mode: 0644]