Skip to content

Commit

Permalink
Initialize Elf Header to zero to ensure that bytes not assigned any v…
Browse files Browse the repository at this point in the history
…alue later on are initialized properly.

Differential Revision: https://reviews.llvm.org/D44986

llvm-svn: 328902
  • Loading branch information
rdhindsa14 committed Mar 30, 2018
1 parent 4092645 commit 7588a8e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lld/ELF/Writer.cpp
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions 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: }

0 comments on commit 7588a8e

Please sign in to comment.