[ELF] Don't emit empty PT_LOAD segment
Sometimes the very first PT_LOAD segment, created by lld, can be empty.
This happens when (all conditions met):
- Linker script is used
- First section in ELF image is not RO
- Not enough space for program headers.
Differential revision: https://reviews.llvm.org/D25330
llvm-svn: 283760
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index cb8a2b6..d7b0dc8 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -655,6 +655,17 @@
FirstPTLoad->First = Out<ELFT>::ElfHeader;
if (!FirstPTLoad->Last)
FirstPTLoad->Last = Out<ELFT>::ProgramHeaders;
+ } else if (!FirstPTLoad->First) {
+ // Sometimes the very first PT_LOAD segment can be empty.
+ // This happens if (all conditions met):
+ // - Linker script is used
+ // - First section in ELF image is not RO
+ // - Not enough space for program headers.
+ // The code below removes empty PT_LOAD segment and updates
+ // program headers size.
+ Phdrs.erase(FirstPTLoad);
+ Out<ELFT>::ProgramHeaders->setSize(sizeof(typename ELFT::Phdr) *
+ Phdrs.size());
}
}