[PATCH] D28267: ELF: Round p_memsz of the PT_GNU_RELRO program header up to the page size.

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 3 17:16:49 PST 2017


pcc created this revision.
pcc added reviewers: ruiu, rafael, davide.
pcc added a subscriber: llvm-commits.
Herald added a subscriber: nemanjai.

The glibc dynamic loader rounds the size down, so without this the loader
will fail to change the memory protection for the last page.


https://reviews.llvm.org/D28267

Files:
  lld/ELF/Writer.cpp
  lld/test/ELF/basic-mips.s
  lld/test/ELF/basic-ppc.s


Index: lld/test/ELF/basic-ppc.s
===================================================================
--- lld/test/ELF/basic-ppc.s
+++ lld/test/ELF/basic-ppc.s
@@ -295,7 +295,7 @@
 // CHECK-NEXT:     VirtualAddress: 0x2000
 // CHECK-NEXT:     PhysicalAddress: 0x2000
 // CHECK-NEXT:     FileSize: 48
-// CHECK-NEXT:     MemSize: 48
+// CHECK-NEXT:     MemSize: 4096
 // CHECK-NEXT:     Flags [ (0x4)
 // CHECK-NEXT:       PF_R (0x4)
 // CHECK-NEXT:     ]
Index: lld/test/ELF/basic-mips.s
===================================================================
--- lld/test/ELF/basic-mips.s
+++ lld/test/ELF/basic-mips.s
@@ -297,7 +297,7 @@
 # CHECK-NEXT:     VirtualAddress: 0x30000
 # CHECK-NEXT:     PhysicalAddress: 0x30000
 # CHECK-NEXT:     FileSize: 8
-# CHECK-NEXT:     MemSize: 8
+# CHECK-NEXT:     MemSize: 65536
 # CHECK-NEXT:     Flags [ (0x4)
 # CHECK-NEXT:       PF_R (0x4)
 # CHECK-NEXT:     ]
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -1447,8 +1447,12 @@
     }
     if (P.p_type == PT_LOAD)
       P.p_align = Config->MaxPageSize;
-    else if (P.p_type == PT_GNU_RELRO)
+    else if (P.p_type == PT_GNU_RELRO) {
       P.p_align = 1;
+      // The glibc dynamic loader rounds the size down, so we need to round up
+      // to protect the last page.
+      P.p_memsz = alignTo(P.p_memsz, Config->MaxPageSize);
+    }
 
     // The TLS pointer goes after PT_TLS. At least glibc will align it,
     // so round up the size to make sure the offsets are correct.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28267.82978.patch
Type: text/x-patch
Size: 1571 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170104/0b059004/attachment.bin>


More information about the llvm-commits mailing list