Initialize Elf Header to zero to ensure that bytes not assigned any value later on...
authorRumeet Dhindsa <rdhindsa@google.com>
Fri, 30 Mar 2018 20:49:34 +0000 (20:49 +0000)
committerRumeet Dhindsa <rdhindsa@google.com>
Fri, 30 Mar 2018 20:49:34 +0000 (20:49 +0000)
Differential Revision: https://reviews.llvm.org/D44986

llvm-svn: 328902

lld/ELF/Writer.cpp
lld/test/ELF/elf-header.s [new file with mode: 0644]

index 4868a11..e685c8e 100644 (file)
@@ -2092,6 +2092,10 @@ static uint8_t getAbiVersion() {
 
 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.
diff --git a/lld/test/ELF/elf-header.s b/lld/test/ELF/elf-header.s
new file mode 100644 (file)
index 0000000..e188650
--- /dev/null
@@ -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:   }