[PATCH] D44986: Initialize Elf header to Zero

Rumeet Dhindsa via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 30 13:52:31 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL328902: Initialize Elf Header to zero to ensure that bytes not assigned any value later… (authored by rdhindsa, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D44986?vs=140290&id=140483#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D44986

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


Index: lld/trunk/test/ELF/elf-header.s
===================================================================
--- lld/trunk/test/ELF/elf-header.s
+++ lld/trunk/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/trunk/ELF/Writer.cpp
===================================================================
--- lld/trunk/ELF/Writer.cpp
+++ lld/trunk/ELF/Writer.cpp
@@ -2092,6 +2092,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.140483.patch
Type: text/x-patch
Size: 1432 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180330/4464a471/attachment.bin>


More information about the llvm-commits mailing list