[JITLink][ELF] Only make sections with SHF_WRITE writable
authorJob Noorman <jnoorman@igalia.com>
Mon, 3 Apr 2023 18:18:19 +0000 (20:18 +0200)
committerJob Noorman <jnoorman@igalia.com>
Mon, 3 Apr 2023 18:18:27 +0000 (20:18 +0200)
All non-executable sections used to be mapped as RW- causing read-only
sections such as .rodata to be writable.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D147442

llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h

index 1d98acf..c1f69d0 100644 (file)
@@ -360,11 +360,11 @@ template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::graphifySections() {
     });
 
     // Get the section's memory protection flags.
-    orc::MemProt Prot;
+    orc::MemProt Prot = orc::MemProt::Read;
     if (Sec.sh_flags & ELF::SHF_EXECINSTR)
-      Prot = orc::MemProt::Read | orc::MemProt::Exec;
-    else
-      Prot = orc::MemProt::Read | orc::MemProt::Write;
+      Prot |= orc::MemProt::Exec;
+    if (Sec.sh_flags & ELF::SHF_WRITE)
+      Prot |= orc::MemProt::Write;
 
     // Look for existing sections first.
     auto *GraphSec = G->findSectionByName(*Name);