Index: llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h =================================================================== --- llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h +++ llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h @@ -277,10 +277,10 @@ const DWARFDebugAranges *getDebugAranges(); /// Get a pointer to the parsed frame information object. - const DWARFDebugFrame *getDebugFrame(); + Expected getDebugFrame(); /// Get a pointer to the parsed eh frame information object. - const DWARFDebugFrame *getEHFrame(); + Expected getEHFrame(); /// Get a pointer to the parsed DebugMacinfo information object. const DWARFDebugMacro *getDebugMacinfo(); Index: llvm/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h =================================================================== --- llvm/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h +++ llvm/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h @@ -290,7 +290,7 @@ /// Parse the section from raw data. \p Data is assumed to contain the whole /// frame section contents to be parsed. - void parse(DWARFDataExtractor Data); + Error parse(DWARFDataExtractor Data); /// Return whether the section has any entries. bool empty() const { return Entries.empty(); } Index: llvm/lib/DebugInfo/DWARF/DWARFContext.cpp =================================================================== --- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -433,13 +433,23 @@ } } - if (const auto *Off = shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame, - DObj->getFrameSection().Data)) - getDebugFrame()->dump(OS, getRegisterInfo(), *Off); + if (const Optional *Off = + shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame, + DObj->getFrameSection().Data)) { + if (Expected DF = getDebugFrame()) + (*DF)->dump(OS, getRegisterInfo(), *Off); + else + RecoverableErrorHandler(DF.takeError()); + } - if (const auto *Off = shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame, - DObj->getEHFrameSection().Data)) - getEHFrame()->dump(OS, getRegisterInfo(), *Off); + if (const Optional *Off = + shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame, + DObj->getEHFrameSection().Data)) { + if (Expected DF = getEHFrame()) + (*DF)->dump(OS, getRegisterInfo(), *Off); + else + RecoverableErrorHandler(DF.takeError()); + } if (shouldDump(Explicit, ".debug_macro", DIDT_ID_DebugMacro, DObj->getMacroSection().Data)) { @@ -788,7 +798,7 @@ return Aranges.get(); } -const DWARFDebugFrame *DWARFContext::getDebugFrame() { +Expected DWARFContext::getDebugFrame() { if (DebugFrame) return DebugFrame.get(); @@ -803,19 +813,27 @@ // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html DWARFDataExtractor debugFrameData(*DObj, DObj->getFrameSection(), isLittleEndian(), DObj->getAddressSize()); - DebugFrame.reset(new DWARFDebugFrame(getArch(), false /* IsEH */)); - DebugFrame->parse(debugFrameData); + std::unique_ptr debugFrame = + std::make_unique(getArch(), /*IsEH=*/false); + if (Error E = debugFrame->parse(debugFrameData)) + return std::move(E); + + DebugFrame.swap(debugFrame); return DebugFrame.get(); } -const DWARFDebugFrame *DWARFContext::getEHFrame() { +Expected DWARFContext::getEHFrame() { if (EHFrame) return EHFrame.get(); DWARFDataExtractor debugFrameData(*DObj, DObj->getEHFrameSection(), isLittleEndian(), DObj->getAddressSize()); - DebugFrame.reset(new DWARFDebugFrame(getArch(), true /* IsEH */)); - DebugFrame->parse(debugFrameData); + + std::unique_ptr debugFrame = + std::make_unique(getArch(), /*IsEH=*/true); + if (Error E = debugFrame->parse(debugFrameData)) + return std::move(E); + DebugFrame.swap(debugFrame); return DebugFrame.get(); } Index: llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp =================================================================== --- llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp +++ llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp @@ -359,20 +359,7 @@ errs() << "\n"; } -// This is a workaround for old compilers which do not allow -// noreturn attribute usage in lambdas. Once the support for those -// compilers are phased out, we can remove this and return back to -// a ReportError lambda: [StartOffset](const char *ErrorMsg). -static void LLVM_ATTRIBUTE_NORETURN ReportError(uint64_t StartOffset, - const char *ErrorMsg) { - std::string Str; - raw_string_ostream OS(Str); - OS << format(ErrorMsg, StartOffset); - OS.flush(); - report_fatal_error(Str); -} - -void DWARFDebugFrame::parse(DWARFDataExtractor Data) { +Error DWARFDebugFrame::parse(DWARFDataExtractor Data) { uint64_t Offset = 0; DenseMap CIEs; @@ -421,51 +408,56 @@ // Walk the augmentation string to get all the augmentation data. for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) { switch (AugmentationString[i]) { - default: - ReportError( - StartOffset, - "Unknown augmentation character in entry at %" PRIx64); - case 'L': - LSDAPointerEncoding = Data.getU8(&Offset); - break; - case 'P': { - if (Personality) - ReportError(StartOffset, - "Duplicate personality in entry at %" PRIx64); - PersonalityEncoding = Data.getU8(&Offset); - Personality = Data.getEncodedPointer( - &Offset, *PersonalityEncoding, - EHFrameAddress ? EHFrameAddress + Offset : 0); - break; - } - case 'R': - FDEPointerEncoding = Data.getU8(&Offset); - break; - case 'S': - // Current frame is a signal trampoline. - break; - case 'z': - if (i) - ReportError(StartOffset, - "'z' must be the first character at %" PRIx64); - // Parse the augmentation length first. We only parse it if - // the string contains a 'z'. - AugmentationLength = Data.getULEB128(&Offset); - StartAugmentationOffset = Offset; - EndAugmentationOffset = Offset + *AugmentationLength; - break; - case 'B': - // B-Key is used for signing functions associated with this - // augmentation string - break; + default: + return make_error( + "Unknown augmentation character in entry at 0x" + + Twine::utohexstr(StartOffset), + inconvertibleErrorCode()); + case 'L': + LSDAPointerEncoding = Data.getU8(&Offset); + break; + case 'P': { + if (Personality) + return make_error( + "Duplicate personality in entry at 0x" + + Twine::utohexstr(StartOffset), + inconvertibleErrorCode()); + PersonalityEncoding = Data.getU8(&Offset); + Personality = Data.getEncodedPointer( + &Offset, *PersonalityEncoding, + EHFrameAddress ? EHFrameAddress + Offset : 0); + break; + } + case 'R': + FDEPointerEncoding = Data.getU8(&Offset); + break; + case 'S': + // Current frame is a signal trampoline. + break; + case 'z': + if (i) + return make_error( + "'z' must be the first character at 0x" + + Twine::utohexstr(StartOffset), + inconvertibleErrorCode()); + // Parse the augmentation length first. We only parse it if + // the string contains a 'z'. + AugmentationLength = Data.getULEB128(&Offset); + StartAugmentationOffset = Offset; + EndAugmentationOffset = Offset + *AugmentationLength; + break; + case 'B': + // B-Key is used for signing functions associated with this + // augmentation string + break; } } if (AugmentationLength.hasValue()) { if (Offset != EndAugmentationOffset) - ReportError(StartOffset, - "Parsing augmentation data at %" PRIx64 " failed"); - + return make_error("Parsing augmentation data at 0x" + + Twine::utohexstr(StartOffset), + inconvertibleErrorCode()); AugmentationData = Data.getData().slice(StartAugmentationOffset, EndAugmentationOffset); } @@ -490,8 +482,10 @@ if (IsEH) { // The address size is encoded in the CIE we reference. if (!Cie) - ReportError(StartOffset, "Parsing FDE data at %" PRIx64 - " failed due to missing CIE"); + return make_error("Parsing FDE data at 0x" + + Twine::utohexstr(StartOffset) + + " failed due to missing CIE", + inconvertibleErrorCode()); if (auto Val = Data.getEncodedPointer( &Offset, Cie->getFDEPointerEncoding(), @@ -518,8 +512,10 @@ } if (Offset != EndAugmentationOffset) - ReportError(StartOffset, - "Parsing augmentation data at %" PRIx64 " failed"); + return make_error("Parsing augmentation data at 0x" + + Twine::utohexstr(StartOffset) + + " failed", + inconvertibleErrorCode()); } } else { InitialLocation = Data.getRelocatedAddress(&Offset); @@ -532,14 +528,17 @@ } if (Error E = - Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset)) { - report_fatal_error(toString(std::move(E))); - } + Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset)) + return std::move(E); if (Offset != EndStructureOffset) - ReportError(StartOffset, - "Parsing entry instructions at %" PRIx64 " failed"); + return make_error("Parsing entry instructions at 0x" + + Twine::utohexstr(StartOffset) + + " failed", + inconvertibleErrorCode()); } + + return Error::success(); } FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const { Index: llvm/test/DebugInfo/X86/eh-frame-cie-id.s =================================================================== --- llvm/test/DebugInfo/X86/eh-frame-cie-id.s +++ llvm/test/DebugInfo/X86/eh-frame-cie-id.s @@ -1,8 +1,7 @@ -# RUN: llvm-mc -triple x86_64-unknown-linux %s -filetype=obj -o - | \ -# RUN: not --crash llvm-dwarfdump -debug-frame - 2>&1 | \ -# RUN: FileCheck %s +# RUN: llvm-mc -triple x86_64-unknown-linux %s -filetype=obj -o %t +# RUN: not llvm-dwarfdump -debug-frame %t 2>&1 | FileCheck %s -# CHECK: Parsing FDE data at 0 failed due to missing CIE +# CHECK: Parsing FDE data at 0x0 failed due to missing CIE .section .eh_frame,"a",@unwind ## This FDE was formerly wrongly interpreted as a CIE because its CIE pointer Index: llvm/test/tools/llvm-readobj/ELF/unwind.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/unwind.test +++ llvm/test/tools/llvm-readobj/ELF/unwind.test @@ -1,5 +1,5 @@ -# RUN: yaml2obj %s -o %t.exe -# RUN: llvm-readobj --unwind %t.exe | FileCheck %s +# RUN: yaml2obj --docnum=1 %s -o %t1.exe +# RUN: llvm-readobj --unwind %t1.exe | FileCheck %s # CHECK: EHFrameHeader { # CHECK-NEXT: Address: 0x4013c0 @@ -215,3 +215,32 @@ Sections: - Section: .eh_frame_hdr ... + +## Check we report a error when the tool is unable to parse .eh_frame section. +## Previously it also suggested to send a error report to LLVM. +# RUN: yaml2obj --docnum=2 %s -o %t2.exe +# RUN: not llvm-readobj --unwind %t2.exe 2>&1 | FileCheck %s -DFILE=%t2.exe --check-prefix=NO-CIE-ERR + +# NO-CIE-ERR: .eh_frame section at offset 0x40 address 0x0: +# NO-CIE-ERR-NEXT: error: '[[FILE]]': Parsing FDE data at 0x0 failed due to missing CIE +# NO-CIE-ERR-NOT: {{.}} + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .eh_frame + Type: SHT_X86_64_UNWIND +## The content is generated from the following code. It has no CIE. +## See the DebugInfoX86/eh-frame-cie-id.s test case for more history. +## .section .eh_frame,"a",@unwind +## .long .Lend - .LCIEptr # Length +## .LCIEptr: +## .long 0xffffffff # CIE pointer +## .quad 0x1111abcd # Initial location +## .quad 0x00010000 # Address range +## .Lend: + Content: 14000000FFFFFFFFCDAB1111000000000000010000000000 Index: llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h =================================================================== --- llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h +++ llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h @@ -191,7 +191,8 @@ ELFT::Is64Bits ? 8 : 4); DWARFDebugFrame EHFrame(Triple::ArchType(ObjF->getArch()), /*IsEH=*/true, /*EHFrameAddress=*/Address); - EHFrame.parse(DE); + if (Error E = EHFrame.parse(DE)) + reportError(std::move(E), ObjF->getFileName()); for (const auto &Entry : EHFrame) { if (const auto *CIE = dyn_cast(&Entry)) {