[llvm-readobj] - Remove 'error(Error EC)' helper.

We do not need it. I replaced it with
reportError(StringRef Input, Error Err).

Differential revision: https://reviews.llvm.org/D66011

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368677 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
George Rimar 2019-08-13 12:07:41 +00:00
parent 9ca54d8f2f
commit a5068cdcdc
7 changed files with 103 additions and 82 deletions

View File

@ -79,10 +79,10 @@ Sections:
## that goes past the end of file.
# RUN: yaml2obj --docnum=2 %s > %t2.so
# RUN: not llvm-readelf --notes %t2.so 2>&1 | FileCheck %s --check-prefix=ERR1
# RUN: not llvm-readobj --notes %t2.so 2>&1 | FileCheck %s --check-prefix=ERR1
# RUN: not llvm-readelf --notes %t2.so 2>&1 | FileCheck -DFILE=%t2.so %s --check-prefix=ERR1
# RUN: not llvm-readobj --notes %t2.so 2>&1 | FileCheck -DFILE=%t2.so %s --check-prefix=ERR1
# ERR1: error: SHT_NOTE section [index 1] has invalid offset (0xffff0000) or size (0x0)
# ERR1: error: '[[FILE]]': SHT_NOTE section [index 1] has invalid offset (0xffff0000) or size (0x0)
--- !ELF
FileHeader:
@ -99,10 +99,10 @@ Sections:
## that goes past the end of file.
# RUN: yaml2obj --docnum=3 %s > %t3.so
# RUN: not llvm-readelf --notes %t3.so 2>&1 | FileCheck %s --check-prefix=ERR2
# RUN: not llvm-readobj --notes %t3.so 2>&1 | FileCheck %s --check-prefix=ERR2
# RUN: not llvm-readelf --notes %t3.so 2>&1 | FileCheck -DFILE=%t3.so %s --check-prefix=ERR2
# RUN: not llvm-readobj --notes %t3.so 2>&1 | FileCheck -DFILE=%t3.so %s --check-prefix=ERR2
# ERR2: error: SHT_NOTE section [index 1] has invalid offset (0x180) or size (0xffff0000)
# ERR2: error: '[[FILE]]': SHT_NOTE section [index 1] has invalid offset (0x180) or size (0xffff0000)
--- !ELF
FileHeader:
@ -119,10 +119,10 @@ Sections:
## goes past the end of file.
# RUN: yaml2obj --docnum=4 %s > %t4.so
# RUN: not llvm-readelf --notes %t4.so 2>&1 | FileCheck %s --check-prefix=ERR3
# RUN: not llvm-readobj --notes %t4.so 2>&1 | FileCheck %s --check-prefix=ERR3
# RUN: not llvm-readelf --notes %t4.so 2>&1 | FileCheck -DFILE=%t4.so %s --check-prefix=ERR3
# RUN: not llvm-readobj --notes %t4.so 2>&1 | FileCheck -DFILE=%t4.so %s --check-prefix=ERR3
# ERR3: error: PT_NOTE header has invalid offset (0xffff0000) or size (0x0)
# ERR3: error: '[[FILE]]': PT_NOTE header has invalid offset (0xffff0000) or size (0x0)
--- !ELF
FileHeader:
@ -143,10 +143,10 @@ ProgramHeaders:
## goes past the end of file.
# RUN: yaml2obj --docnum=5 %s > %t5.so
# RUN: not llvm-readelf --notes %t5.so 2>&1 | FileCheck %s --check-prefix=ERR4
# RUN: not llvm-readobj --notes %t5.so 2>&1 | FileCheck %s --check-prefix=ERR4
# RUN: not llvm-readelf --notes %t5.so 2>&1 | FileCheck -DFILE=%t5.so %s --check-prefix=ERR4
# RUN: not llvm-readobj --notes %t5.so 2>&1 | FileCheck -DFILE=%t5.so %s --check-prefix=ERR4
# ERR4: error: PT_NOTE header has invalid offset (0x1b8) or size (0xffff0000)
# ERR4: error: '[[FILE]]': PT_NOTE header has invalid offset (0x1b8) or size (0xffff0000)
--- !ELF
FileHeader:

View File

@ -370,7 +370,7 @@ PrinterContext<ET>::FunctionAtAddress(unsigned Section,
return readobj_error::unknown_symbol;
auto StrTableOrErr = ELF->getStringTableForSymtab(*Symtab);
if (!StrTableOrErr)
error(StrTableOrErr.takeError());
reportError(StrTableOrErr.takeError(), FileName);
StringRef StrTable = *StrTableOrErr;
for (const Elf_Sym &Sym : unwrapOrError(FileName, ELF->symbols(Symtab)))
@ -405,7 +405,7 @@ PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
auto SymTabOrErr = ELF->getSection(Sec.sh_link);
if (!SymTabOrErr)
error(SymTabOrErr.takeError());
reportError(SymTabOrErr.takeError(), FileName);
const Elf_Shdr *SymTab = *SymTabOrErr;
for (const Elf_Rel &R : unwrapOrError(FileName, ELF->rels(&Sec))) {

View File

@ -912,26 +912,33 @@ void COFFDumper::initializeFileAndStringTables(BinaryStreamReader &Reader) {
// The section consists of a number of subsection in the following format:
// |SubSectionType|SubSectionSize|Contents...|
uint32_t SubType, SubSectionSize;
error(Reader.readInteger(SubType));
error(Reader.readInteger(SubSectionSize));
if (Error E = Reader.readInteger(SubType))
reportError(std::move(E), Obj->getFileName());
if (Error E = Reader.readInteger(SubSectionSize))
reportError(std::move(E), Obj->getFileName());
StringRef Contents;
error(Reader.readFixedString(Contents, SubSectionSize));
if (Error E = Reader.readFixedString(Contents, SubSectionSize))
reportError(std::move(E), Obj->getFileName());
BinaryStreamRef ST(Contents, support::little);
switch (DebugSubsectionKind(SubType)) {
case DebugSubsectionKind::FileChecksums:
error(CVFileChecksumTable.initialize(ST));
if (Error E = CVFileChecksumTable.initialize(ST))
reportError(std::move(E), Obj->getFileName());
break;
case DebugSubsectionKind::StringTable:
error(CVStringTable.initialize(ST));
if (Error E = CVStringTable.initialize(ST))
reportError(std::move(E), Obj->getFileName());
break;
default:
break;
}
uint32_t PaddedSize = alignTo(SubSectionSize, 4);
error(Reader.skip(PaddedSize - SubSectionSize));
if (Error E = Reader.skip(PaddedSize - SubSectionSize))
reportError(std::move(E), Obj->getFileName());
}
}
@ -949,7 +956,9 @@ void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
W.printNumber("Section", SectionName, Obj->getSectionID(Section));
uint32_t Magic;
error(consume(Data, Magic));
if (Error E = consume(Data, Magic))
reportError(std::move(E), Obj->getFileName());
W.printHex("Magic", Magic);
if (Magic != COFF::DEBUG_SECTION_MAGIC)
return error(object_error::parse_failed);
@ -962,8 +971,10 @@ void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
// The section consists of a number of subsection in the following format:
// |SubSectionType|SubSectionSize|Contents...|
uint32_t SubType, SubSectionSize;
error(consume(Data, SubType));
error(consume(Data, SubSectionSize));
if (Error E = consume(Data, SubType))
reportError(std::move(E), Obj->getFileName());
if (Error E = consume(Data, SubSectionSize))
reportError(std::move(E), Obj->getFileName());
ListScope S(W, "Subsection");
// Dump the subsection as normal even if the ignore bit is set.
@ -1038,7 +1049,8 @@ void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
BinaryStreamReader SR(Contents, llvm::support::little);
DebugFrameDataSubsectionRef FrameData;
error(FrameData.initialize(SR));
if (Error E = FrameData.initialize(SR))
reportError(std::move(E), Obj->getFileName());
StringRef LinkageName;
error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents,
@ -1100,7 +1112,8 @@ void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
BinaryStreamReader Reader(FunctionLineTables[Name], support::little);
DebugLinesSubsectionRef LineInfo;
error(LineInfo.initialize(Reader));
if (Error E = LineInfo.initialize(Reader))
reportError(std::move(E), Obj->getFileName());
W.printHex("Flags", LineInfo.header()->Flags);
W.printHex("CodeSize", LineInfo.header()->CodeSize);
@ -1154,9 +1167,9 @@ void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection,
error(object_error::parse_failed);
}
if (auto EC = CVSD.dump(Symbols)) {
if (Error E = CVSD.dump(Symbols)) {
W.flush();
error(std::move(EC));
reportError(std::move(E), Obj->getFileName());
}
CompilationCPUType = CVSD.getCompilationCPUType();
W.flush();
@ -1165,7 +1178,8 @@ void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection,
void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) {
BinaryStreamRef Stream(Subsection, llvm::support::little);
DebugChecksumsSubsectionRef Checksums;
error(Checksums.initialize(Stream));
if (Error E = Checksums.initialize(Stream))
reportError(std::move(E), Obj->getFileName());
for (auto &FC : Checksums) {
DictScope S(W, "FileChecksum");
@ -1184,7 +1198,8 @@ void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) {
void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) {
BinaryStreamReader SR(Subsection, llvm::support::little);
DebugInlineeLinesSubsectionRef Lines;
error(Lines.initialize(SR));
if (Error E = Lines.initialize(SR))
reportError(std::move(E), Obj->getFileName());
for (auto &Line : Lines) {
DictScope S(W, "InlineeSourceLine");
@ -1232,7 +1247,9 @@ void COFFDumper::mergeCodeViewTypes(MergingTypeTableBuilder &CVIDs,
if (SectionName == ".debug$T") {
StringRef Data = unwrapOrError(Obj->getFileName(), S.getContents());
uint32_t Magic;
error(consume(Data, Magic));
if (Error E = consume(Data, Magic))
reportError(std::move(E), Obj->getFileName());
if (Magic != 4)
error(object_error::parse_failed);
@ -1248,14 +1265,14 @@ void COFFDumper::mergeCodeViewTypes(MergingTypeTableBuilder &CVIDs,
if (GHash) {
std::vector<GloballyHashedType> Hashes =
GloballyHashedType::hashTypes(Types);
if (auto EC =
if (Error E =
mergeTypeAndIdRecords(GlobalCVIDs, GlobalCVTypes, SourceToDest,
Types, Hashes, PCHSignature))
return error(std::move(EC));
return reportError(std::move(E), Obj->getFileName());
} else {
if (auto EC = mergeTypeAndIdRecords(CVIDs, CVTypes, SourceToDest, Types,
if (Error E = mergeTypeAndIdRecords(CVIDs, CVTypes, SourceToDest, Types,
PCHSignature))
return error(std::move(EC));
return reportError(std::move(E), Obj->getFileName());
}
}
}
@ -1271,7 +1288,9 @@ void COFFDumper::printCodeViewTypeSection(StringRef SectionName,
W.printBinaryBlock("Data", Data);
uint32_t Magic;
error(consume(Data, Magic));
if (Error E = consume(Data, Magic))
reportError(std::move(E), Obj->getFileName());
W.printHex("Magic", Magic);
if (Magic != COFF::DEBUG_SECTION_MAGIC)
return error(object_error::parse_failed);
@ -1279,7 +1298,9 @@ void COFFDumper::printCodeViewTypeSection(StringRef SectionName,
Types.reset(Data, 100);
TypeDumpVisitor TDV(Types, &W, opts::CodeViewSubsectionBytes);
error(codeview::visitTypeStream(Types, TDV));
if (Error E = codeview::visitTypeStream(Types, TDV))
reportError(std::move(E), Obj->getFileName());
W.flush();
}
@ -1504,7 +1525,7 @@ void COFFDumper::printSymbol(const SymbolRef &Sym) {
error(EC);
Expected<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc);
if (!Res)
error(Res.takeError());
reportError(Res.takeError(), Obj->getFileName());
AssocName = *Res;
W.printNumber("AssocSection", AssocName, AuxNumber);
@ -1906,7 +1927,8 @@ void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer,
{
ListScope S(Writer, "MergedTypeStream");
TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes);
error(codeview::visitTypeStream(TpiTypes, TDV));
if (Error Err = codeview::visitTypeStream(TpiTypes, TDV))
reportError(std::move(Err), "<?>");
Writer.flush();
}
@ -1917,7 +1939,8 @@ void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer,
ListScope S(Writer, "MergedIDStream");
TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes);
TDV.setIpiTypes(IpiTypes);
error(codeview::visitTypeStream(IpiTypes, TDV));
if (Error Err = codeview::visitTypeStream(IpiTypes, TDV))
reportError(std::move(Err), "<?>");
Writer.flush();
}
}

View File

@ -4417,7 +4417,7 @@ void GNUStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
for (const auto &Note : Obj->notes(P, Err))
ProcessNote(Note);
if (Err)
error(std::move(Err));
reportError(std::move(Err), this->FileName);
}
} else {
for (const auto &S :
@ -4429,7 +4429,7 @@ void GNUStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
for (const auto &Note : Obj->notes(S, Err))
ProcessNote(Note);
if (Err)
error(std::move(Err));
reportError(std::move(Err), this->FileName);
}
}
}
@ -4483,10 +4483,10 @@ void DumpStyle<ELFT>::printFunctionStackSize(
// integer.
if (*Offset == PrevOffset)
reportError(
FileStr,
createStringError(object_error::parse_failed,
"could not extract a valid stack size in section %s",
SectionName.data()));
SectionName.data()),
FileStr);
printStackSizeEntry(StackSize, FuncName);
}
@ -4545,11 +4545,12 @@ void DumpStyle<ELFT>::printStackSize(const ELFObjectFile<ELFT> *Obj,
uint64_t Offset = Reloc.getOffset();
if (!Data.isValidOffsetForDataOfSize(Offset, sizeof(Elf_Addr) + 1))
reportError(FileStr, createStringError(
object_error::parse_failed,
"found invalid relocation offset into section %s "
"while trying to extract a stack size entry",
StackSizeSectionName.data()));
reportError(
createStringError(object_error::parse_failed,
"found invalid relocation offset into section %s "
"while trying to extract a stack size entry",
StackSizeSectionName.data()),
FileStr);
uint64_t Addend = Data.getAddress(&Offset);
uint64_t SymValue = Resolver(Reloc, RelocSymValue, Addend);
@ -4595,11 +4596,11 @@ void DumpStyle<ELFT>::printNonRelocatableStackSizes(
// size. Check for an extra byte before we try to process the entry.
if (!Data.isValidOffsetForDataOfSize(Offset, sizeof(Elf_Addr) + 1)) {
reportError(
FileStr,
createStringError(
object_error::parse_failed,
"section %s ended while trying to extract a stack size entry",
SectionName.data()));
SectionName.data()),
FileStr);
}
uint64_t SymValue = Data.getAddress(&Offset);
printFunctionStackSize(Obj, SymValue,
@ -4689,10 +4690,10 @@ void DumpStyle<ELFT>::printRelocatableStackSizes(
RelocSec.getName(RelocSectionName);
StringRef RelocName = EF->getRelocationTypeName(Reloc.getType());
reportError(
FileStr,
createStringError(object_error::parse_failed,
"unsupported relocation type in section %s: %s",
RelocSectionName.data(), RelocName.data()));
RelocSectionName.data(), RelocName.data()),
FileStr);
}
this->printStackSize(Obj, Reloc, FunctionSec, StackSizeSectionName,
Resolver, Data);
@ -5568,7 +5569,7 @@ void LLVMStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
for (const auto &Note : Obj->notes(P, Err))
ProcessNote(Note);
if (Err)
error(std::move(Err));
reportError(std::move(Err), this->FileName);
}
} else {
for (const auto &S : unwrapOrError(this->FileName, Obj->sections())) {
@ -5580,7 +5581,7 @@ void LLVMStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
for (const auto &Note : Obj->notes(S, Err))
ProcessNote(Note);
if (Err)
error(std::move(Err));
reportError(std::move(Err), this->FileName);
}
}
}

View File

@ -289,7 +289,9 @@ void Dumper::printRuntimeFunction(const Context &Ctx,
resolveRelocation(Ctx, Section, SectionOffset + 8, XData, Offset);
ArrayRef<uint8_t> Contents;
error(Ctx.COFF.getSectionContents(XData, Contents));
if (Error E = Ctx.COFF.getSectionContents(XData, Contents))
reportError(std::move(E), Ctx.COFF.getFileName());
if (Contents.empty())
return;
@ -311,7 +313,9 @@ void Dumper::printData(const Context &Ctx) {
const coff_section *PData = Ctx.COFF.getCOFFSection(Section);
ArrayRef<uint8_t> Contents;
error(Ctx.COFF.getSectionContents(PData, Contents));
if (Error E = Ctx.COFF.getSectionContents(PData, Contents))
reportError(std::move(E), Ctx.COFF.getFileName());
if (Contents.empty())
continue;

View File

@ -382,10 +382,12 @@ LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
exit(1);
}
void reportError(StringRef Input, Error Err) {
void reportError(Error Err, StringRef Input) {
assert(Err);
if (Input == "-")
Input = "<stdin>";
error(createFileError(Input, std::move(Err)));
handleAllErrors(createFileError(Input, std::move(Err)),
[&](const ErrorInfoBase &EI) { reportError(EI.message()); });
}
void reportWarning(Twine Msg) {
@ -406,13 +408,6 @@ void warn(Error Err) {
});
}
void error(Error EC) {
if (!EC)
return;
handleAllErrors(std::move(EC),
[&](const ErrorInfoBase &EI) { reportError(EI.message()); });
}
void error(std::error_code EC) {
if (!EC)
return;
@ -421,8 +416,9 @@ void error(std::error_code EC) {
} // namespace llvm
static void reportError(StringRef Input, std::error_code EC) {
reportError(Input, errorCodeToError(EC));
static void reportError(std::error_code EC, StringRef Input) {
assert(EC != readobj_error::success);
reportError(errorCodeToError(EC), Input);
}
static bool isMipsArch(unsigned Arch) {
@ -482,7 +478,7 @@ static void dumpObject(const ObjectFile *Obj, ScopedPrinter &Writer,
std::unique_ptr<ObjDumper> Dumper;
if (std::error_code EC = createDumper(Obj, Writer, Dumper))
reportError(FileStr, EC);
reportError(EC, FileStr);
if (opts::Output == opts::LLVM || opts::InputFilenames.size() > 1 || A) {
Writer.startLine() << "\n";
@ -604,9 +600,8 @@ static void dumpArchive(const Archive *Arc, ScopedPrinter &Writer) {
for (auto &Child : Arc->children(Err)) {
Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
if (!ChildOrErr) {
if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
reportError(Arc->getFileName(), std::move(E));
}
if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
reportError(std::move(E), Arc->getFileName());
continue;
}
if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
@ -614,10 +609,10 @@ static void dumpArchive(const Archive *Arc, ScopedPrinter &Writer) {
else if (COFFImportFile *Imp = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
dumpCOFFImportFile(Imp, Writer);
else
reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
reportError(readobj_error::unrecognized_file_format, Arc->getFileName());
}
if (Err)
reportError(Arc->getFileName(), std::move(Err));
reportError(std::move(Err), Arc->getFileName());
}
/// Dumps each object file in \a MachO Universal Binary;
@ -627,9 +622,8 @@ static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary,
Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
if (ObjOrErr)
dumpObject(&*ObjOrErr.get(), Writer);
else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
reportError(UBinary->getFileName(), ObjOrErr.takeError());
}
else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError()))
reportError(ObjOrErr.takeError(), UBinary->getFileName());
else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
dumpArchive(&*AOrErr.get(), Writer);
}
@ -640,7 +634,7 @@ static void dumpWindowsResourceFile(WindowsResource *WinRes,
ScopedPrinter &Printer) {
WindowsRes::Dumper Dumper(WinRes, Printer);
if (auto Err = Dumper.printData())
reportError(WinRes->getFileName(), std::move(Err));
reportError(std::move(Err), WinRes->getFileName());
}
@ -649,7 +643,7 @@ static void dumpInput(StringRef File, ScopedPrinter &Writer) {
// Attempt to open the binary.
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
if (!BinaryOrErr)
reportError(File, BinaryOrErr.takeError());
reportError(BinaryOrErr.takeError(), File);
Binary &Binary = *BinaryOrErr.get().getBinary();
if (Archive *Arc = dyn_cast<Archive>(&Binary))
@ -664,7 +658,7 @@ static void dumpInput(StringRef File, ScopedPrinter &Writer) {
else if (WindowsResource *WinRes = dyn_cast<WindowsResource>(&Binary))
dumpWindowsResourceFile(WinRes, Writer);
else
reportError(File, readobj_error::unrecognized_file_format);
reportError(readobj_error::unrecognized_file_format, File);
CVTypes.Binaries.push_back(std::move(*BinaryOrErr));
}

View File

@ -22,17 +22,16 @@ namespace llvm {
// Various helper functions.
LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg);
void reportError(StringRef Input, Error Err);
void reportError(Error Err, StringRef Input);
void reportWarning(Twine Msg);
void reportWarning(StringRef Input, Error Err);
void warn(llvm::Error Err);
void error(std::error_code EC);
void error(llvm::Error EC);
template <class T> T unwrapOrError(StringRef Input, Expected<T> EO) {
if (EO)
return *EO;
reportError(Input, EO.takeError());
reportError(EO.takeError(), Input);
llvm_unreachable("reportError shouldn't return in this case");
}
} // namespace llvm