Index: llvm/test/Object/elf-invalid-phdr.test =================================================================== --- llvm/test/Object/elf-invalid-phdr.test +++ /dev/null @@ -1,26 +0,0 @@ -# invalid-phdr.elf is generated by creating a simple elf file with yaml2obj: -# !ELF -# FileHeader: -# Class: ELFCLASS64 -# Data: ELFDATA2LSB -# Type: ET_EXEC -# Machine: EM_X86_64 -# Sections: -# - Name: .text -# Type: SHT_PROGBITS -# Flags: [ SHF_ALLOC, SHF_EXECINSTR ] -# AddressAlign: 0x0000000000001000 -# Content: "00000000" -# ProgramHeaders: -# - Type: PT_LOAD -# Flags: [ PF_X, PF_R ] -# VAddr: 0xAAAA1000 -# PAddr: 0xFFFF1000 -# Sections: -# - Section: .text -# -# Then editing the e_phoff in with a hexeditor to set it to 0xffffff -RUN: not --crash llvm-objdump --private-headers %p/Inputs/invalid-phdr.elf 2>&1 \ -RUN: | FileCheck %s - -CHECK: LLVM ERROR: program headers are longer than binary of size 4162: e_phoff = 0xffffff, e_phnum = 1, e_phentsize = 56 Index: llvm/test/Object/invalid.test =================================================================== --- llvm/test/Object/invalid.test +++ llvm/test/Object/invalid.test @@ -650,3 +650,17 @@ Sections: - Type: SHT_NULL Link: 0xff + +## Check the case when the e_phoff field is invalid. +# RUN: yaml2obj --docnum=31 %s -o %t31 +# RUN: not llvm-objdump --private-headers %t31 2>&1 | FileCheck -DFILE=%t31 %s --check-prefix=INVALID-PHOFF + +# INVALID-PHOFF: error: '[[FILE]]': program headers are longer than binary of size 280: e_phoff = 0xffffff, e_phnum = 0, e_phentsize = 0 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 + PhOff: 0xffffff Index: llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test @@ -0,0 +1,16 @@ +## Test how we handle the case when the e_phoff field is invalid. +# RUN: yaml2obj %s -o %t +# RUN: not llvm-objdump --private-headers %t 2>&1 | \ +# RUN: FileCheck -DFILE=%t %s --check-prefix=INVALID-PHOFF + +# INVALID-PHOFF: Program Header: +# INVALID-PHOFF-NEXT: warning: '[[FILE]]': unable to read program headers: program headers are longer than binary of size 280: e_phoff = 0xffffff, e_phnum = 0, e_phentsize = 0 +# INVALID-PHOFF-NEXT: error: '[[FILE]]': program headers are longer than binary of size 280: e_phoff = 0xffffff, e_phnum = 0, e_phentsize = 0 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 + PhOff: 0xffffff Index: llvm/tools/llvm-objdump/ELFDump.cpp =================================================================== --- llvm/tools/llvm-objdump/ELFDump.cpp +++ llvm/tools/llvm-objdump/ELFDump.cpp @@ -198,11 +198,17 @@ } } -template static void printProgramHeaders(const ELFFile *o) { +template +static void printProgramHeaders(const ELFFile *Obj, StringRef FileName) { outs() << "Program Header:\n"; - auto ProgramHeaderOrError = o->program_headers(); - if (!ProgramHeaderOrError) - report_fatal_error(toString(ProgramHeaderOrError.takeError())); + auto ProgramHeaderOrError = Obj->program_headers(); + if (!ProgramHeaderOrError) { + reportWarning("unable to read program headers: " + + toString(ProgramHeaderOrError.takeError()), + FileName); + return; + } + for (const typename ELFT::Phdr &Phdr : *ProgramHeaderOrError) { switch (Phdr.p_type) { case ELF::PT_DYNAMIC: @@ -346,13 +352,13 @@ void objdump::printELFFileHeader(const object::ObjectFile *Obj) { if (const auto *ELFObj = dyn_cast(Obj)) - printProgramHeaders(ELFObj->getELFFile()); + printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); else if (const auto *ELFObj = dyn_cast(Obj)) - printProgramHeaders(ELFObj->getELFFile()); + printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); else if (const auto *ELFObj = dyn_cast(Obj)) - printProgramHeaders(ELFObj->getELFFile()); + printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); else if (const auto *ELFObj = dyn_cast(Obj)) - printProgramHeaders(ELFObj->getELFFile()); + printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName()); } void objdump::printELFDynamicSection(const object::ObjectFile *Obj) {