ELF: Make .note.GNU-stack more compatible with traditional linkers.
With this patch, lld creates PT_GNU_STACK segments only when all input
files have .note.GNU-stack sections. This is in line with other linkers
with a minor difference (we don't care about .note.GNU-stack rwx bits as
you can always remove .note.GNU-stack sections instead of setting x bit.)
At least, NetBSD loader does not understand PT_GNU_STACK segments and
reject any executables that have the section. This patch makes lld
compatible with such operating systems.
llvm-svn: 253797
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 98e9f24..a993fc2 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -881,10 +881,13 @@
copyPhdr(PH, Out<ELFT>::Dynamic);
}
- Elf_Phdr *PH = &Phdrs[++PhdrIdx];
- PH->p_type = PT_GNU_STACK;
- PH->p_flags = Config->ZExecStack ? toPhdrFlags(SHF_WRITE | SHF_EXECINSTR)
- : toPhdrFlags(SHF_WRITE);
+ // PT_GNU_STACK is a special section to tell the loader to make the
+ // pages for the stack non-executable.
+ if (!Config->ZExecStack) {
+ Elf_Phdr *PH = &Phdrs[++PhdrIdx];
+ PH->p_type = PT_GNU_STACK;
+ PH->p_flags = PF_R | PF_W;
+ }
// Fix up PT_INTERP as we now know the address of .interp section.
if (Interp) {
@@ -908,11 +911,13 @@
// Returns the number of PHDR entries.
template <class ELFT> int Writer<ELFT>::getPhdrsNum() const {
bool Tls = false;
- int I = 3; // 3 for PT_PHDR, first PT_LOAD and PT_GNU_STACK
+ int I = 2; // 2 for PT_PHDR and first PT_LOAD
if (needsInterpSection())
++I;
if (isOutputDynamic())
++I;
+ if (!Config->ZExecStack)
+ ++I;
uintX_t Last = PF_R;
for (OutputSectionBase<ELFT> *Sec : OutputSections) {
if (!needsPhdr<ELFT>(Sec))