[PATCH] D44986: Initialize Elf header to Zero

Rumeet Dhindsa via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 29 10:59:20 PDT 2018


rdhindsa updated this revision to Diff 140290.
rdhindsa retitled this revision from "Initialize Elf header and Program Header to Zero" to "Initialize Elf header to Zero".
rdhindsa edited the summary of this revision.

Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D44986

Files:
  lld/ELF/Writer.cpp
  lld/test/ELF/elf-header.s


Index: lld/test/ELF/elf-header.s
===================================================================
--- /dev/null
+++ lld/test/ELF/elf-header.s
@@ -0,0 +1,18 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld %t.o -o %t1
+# RUN: llvm-readobj -file-headers %t1 | FileCheck %s
+
+# RUN: ld.lld %t.o -no-rosegment -o %t2
+# RUN: llvm-readobj -file-headers %t2 | FileCheck %s
+
+# CHECK:      ElfHeader {
+# CHECK-NEXT:   Ident {
+# CHECK-NEXT:     Magic: (7F 45 4C 46)
+# CHECK-NEXT:     Class: 64-bit (0x2)
+# CHECK-NEXT:     DataEncoding: LittleEndian (0x1)
+# CHECK-NEXT:     FileVersion: 1
+# CHECK-NEXT:     OS/ABI: SystemV (0x0)
+# CHECK-NEXT:     ABIVersion: 0
+# CHECK-NEXT:     Unused: (00 00 00 00 00 00 00)
+# CHECK-NEXT:   }
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -2108,6 +2108,10 @@
 
 template <class ELFT> void Writer<ELFT>::writeHeader() {
   uint8_t *Buf = Buffer->getBufferStart();
+  // For executable segments, the trap instructions are written before writing
+  // the header. Setting Elf header bytes to zero ensures that any unused bytes
+  // in header are zero-cleared, instead of having trap instructions.
+  memset(Buf, 0, sizeof(Elf_Ehdr));
   memcpy(Buf, "\177ELF", 4);
 
   // Write the ELF header.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44986.140290.patch
Type: text/x-patch
Size: 1380 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180329/66e842f7/attachment.bin>


More information about the llvm-commits mailing list