[PATCH] D36262: [ELF] - Do not forget to fill last bytes of PT_LOADs with trap instructions.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 17 02:13:05 PDT 2017


grimar updated this revision to Diff 111480.
grimar added a comment.

- Addressed review comment.


https://reviews.llvm.org/D36262

Files:
  ELF/Writer.cpp
  test/ELF/build-id.s
  test/ELF/fill-trap.s


Index: test/ELF/fill-trap.s
===================================================================
--- test/ELF/fill-trap.s
+++ test/ELF/fill-trap.s
@@ -0,0 +1,25 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: ld.lld %t -o %t2
+# RUN: llvm-readobj -program-headers %t2 | FileCheck %s
+# RUN: hexdump -v -s 0x0001ff0 %t2 | FileCheck %s -check-prefix=FILL
+
+# CHECK: ProgramHeader {
+# CHECK:   Type: PT_LOAD
+# CHECK:   Offset: 0x1000
+# CHECK-NEXT:   VirtualAddress:
+# CHECK-NEXT:   PhysicalAddress:
+# CHECK-NEXT:   FileSize: 4096
+# CHECK-NEXT:   MemSize:
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     PF_R
+# CHECK-NEXT:     PF_X
+# CHECK-NEXT:   ]
+
+## Check that executable page is filled with traps at it's end.
+# FILL: 0001ff0 cccc cccc cccc cccc cccc cccc cccc cccc
+
+.globl _start
+_start:
+  nop
Index: test/ELF/build-id.s
===================================================================
--- test/ELF/build-id.s
+++ test/ELF/build-id.s
@@ -48,15 +48,15 @@
 # DEFAULT:      Contents of section .note.test:
 # DEFAULT:      Contents of section .note.gnu.build-id:
 # DEFAULT-NEXT: 04000000 08000000 03000000 474e5500  ............GNU.
-# DEFAULT-NEXT: d618a375 bc6301ec
+# DEFAULT-NEXT: b0148597 ba5eb7e9
 
 # MD5:      Contents of section .note.gnu.build-id:
 # MD5-NEXT: 04000000 10000000 03000000 474e5500  ............GNU.
-# MD5-NEXT: 051084fe ce1f30ed e035b79e 11262808
+# MD5-NEXT: dce3bcaf 5219454c e89a1fc7 86ab17bd
 
 # SHA1:      Contents of section .note.gnu.build-id:
 # SHA1-NEXT: 04000000 14000000 03000000 474e5500  ............GNU.
-# SHA1-NEXT: c70b9aec 903fd291 8b677cd8 1e04f8b6
+# SHA1-NEXT: 2f716666 fe3668fe 370a02a1 579c3eb2
 
 # UUID:      Contents of section .note.gnu.build-id:
 # UUID-NEXT: 04000000 10000000 03000000 474e5500  ............GNU.
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1855,11 +1855,6 @@
       Sec->writeTo<ELFT>(Buf + Sec->Offset);
 }
 
-static void fillTrap(uint8_t *I, uint8_t *End) {
-  for (; I + 4 < End; I += 4)
-    memcpy(I, &Target->TrapInstr, 4);
-}
-
 // Fill the last page of executable segments with trap instructions
 // instead of leaving them as zero. Even though it is not required by any
 // standard, it is in general a good thing to do for security reasons.
@@ -1872,10 +1867,14 @@
 
   // Fill the last page.
   uint8_t *Buf = Buffer->getBufferStart();
-  for (PhdrEntry *P : Phdrs)
-    if (P->p_type == PT_LOAD && (P->p_flags & PF_X))
-      fillTrap(Buf + alignDown(P->p_offset + P->p_filesz, Target->PageSize),
-               Buf + alignTo(P->p_offset + P->p_filesz, Target->PageSize));
+  for (PhdrEntry *P : Phdrs) {
+    if (P->p_type != PT_LOAD || !(P->p_flags & PF_X))
+      continue;
+    uint8_t *I = Buf + alignDown(P->p_offset + P->p_filesz, Target->PageSize);
+    uint8_t *End = Buf + alignTo(P->p_offset + P->p_filesz, Target->PageSize);
+    for (; I + 4 <= End; I += 4)
+      memcpy(I, &Target->TrapInstr, 4);
+  }
 
   // Round up the file size of the last segment to the page boundary iff it is
   // an executable segment to ensure that other other tools don't accidentally


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36262.111480.patch
Type: text/x-patch
Size: 3219 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170817/17f9801f/attachment.bin>


More information about the llvm-commits mailing list