[Object/COFF.h] - Stop returning std::error_code in a few methods. NFCI.

There are 4 methods that return std::error_code now,
though they do not have to because they are always succeed.
I refactored them.

This allows to simplify the code in tools a bit.

llvm-svn: 369263
This commit is contained in:
George Rimar 2019-08-19 14:32:23 +00:00
parent ac0e6c6502
commit 9d5e8a476f
7 changed files with 24 additions and 74 deletions

View File

@ -969,11 +969,14 @@ public:
return nullptr;
return reinterpret_cast<const dos_header *>(base());
}
std::error_code getCOFFHeader(const coff_file_header *&Res) const;
std::error_code
getCOFFBigObjHeader(const coff_bigobj_file_header *&Res) const;
std::error_code getPE32Header(const pe32_header *&Res) const;
std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
const coff_file_header *getCOFFHeader() const { return COFFHeader; }
const coff_bigobj_file_header *getCOFFBigObjHeader() const {
return COFFBigObjHeader;
}
const pe32_header *getPE32Header() const { return PE32Header; }
const pe32plus_header *getPE32PlusHeader() const { return PE32PlusHeader; }
std::error_code getDataDirectory(uint32_t index,
const data_directory *&Res) const;
std::error_code getSection(int32_t index, const coff_section *&Res) const;

View File

@ -936,29 +936,6 @@ iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
return make_range(base_reloc_begin(), base_reloc_end());
}
std::error_code
COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
Res = COFFHeader;
return std::error_code();
}
std::error_code
COFFObjectFile::getCOFFBigObjHeader(const coff_bigobj_file_header *&Res) const {
Res = COFFBigObjHeader;
return std::error_code();
}
std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Res = PE32Header;
return std::error_code();
}
std::error_code
COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
Res = PE32PlusHeader;
return std::error_code();
}
std::error_code
COFFObjectFile::getDataDirectory(uint32_t Index,
const data_directory *&Res) const {

View File

@ -36,14 +36,9 @@ Error COFFReader::readExecutableHeaders(Object &Obj) const {
DH->AddressOfNewExeHeader - sizeof(*DH));
if (COFFObj.is64()) {
const pe32plus_header *PE32Plus = nullptr;
if (auto EC = COFFObj.getPE32PlusHeader(PE32Plus))
return errorCodeToError(EC);
Obj.PeHeader = *PE32Plus;
Obj.PeHeader = *COFFObj.getPE32PlusHeader();
} else {
const pe32_header *PE32 = nullptr;
if (auto EC = COFFObj.getPE32Header(PE32))
return errorCodeToError(EC);
const pe32_header *PE32 = COFFObj.getPE32Header();
copyPeHeader(Obj.PeHeader, *PE32);
// The pe32plus_header (stored in Object) lacks the BaseOfData field.
Obj.BaseOfData = PE32->BaseOfData;
@ -198,14 +193,11 @@ Error COFFReader::setSymbolTargets(Object &Obj) const {
Expected<std::unique_ptr<Object>> COFFReader::create() const {
auto Obj = std::make_unique<Object>();
const coff_file_header *CFH = nullptr;
const coff_bigobj_file_header *CBFH = nullptr;
COFFObj.getCOFFHeader(CFH);
COFFObj.getCOFFBigObjHeader(CBFH);
bool IsBigObj = false;
if (CFH) {
if (const coff_file_header *CFH = COFFObj.getCOFFHeader()) {
Obj->CoffFileHeader = *CFH;
} else {
const coff_bigobj_file_header *CBFH = COFFObj.getCOFFBigObjHeader();
if (!CBFH)
return createStringError(object_error::parse_failed,
"no COFF file header returned");

View File

@ -234,9 +234,7 @@ printSEHTable(const COFFObjectFile *Obj, uint32_t TableVA, int Count) {
if (Count == 0)
return;
const pe32_header *PE32Header;
error(Obj->getPE32Header(PE32Header));
uint32_t ImageBase = PE32Header->ImageBase;
uint32_t ImageBase = Obj->getPE32Header()->ImageBase;
uintptr_t IntPtr = 0;
error(Obj->getVaPtr(TableVA, IntPtr));
const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr;
@ -268,11 +266,8 @@ static void printTLSDirectoryT(const coff_tls_directory<T> *TLSDir) {
}
static void printTLSDirectory(const COFFObjectFile *Obj) {
const pe32_header *PE32Header;
error(Obj->getPE32Header(PE32Header));
const pe32plus_header *PE32PlusHeader;
error(Obj->getPE32PlusHeader(PE32PlusHeader));
const pe32_header *PE32Header = Obj->getPE32Header();
const pe32plus_header *PE32PlusHeader = Obj->getPE32PlusHeader();
// Skip if it's not executable.
if (!PE32Header && !PE32PlusHeader)
@ -297,10 +292,7 @@ static void printTLSDirectory(const COFFObjectFile *Obj) {
}
static void printLoadConfiguration(const COFFObjectFile *Obj) {
// Skip if it's not executable.
const pe32_header *PE32Header;
error(Obj->getPE32Header(PE32Header));
if (!PE32Header)
if (!Obj->getPE32Header())
return;
// Currently only x86 is supported

View File

@ -1039,10 +1039,7 @@ bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
}
FunctionAddress = *FunctionAddressOrErr;
} else {
const pe32_header *PEHeader;
if (COFF.getPE32Header(PEHeader))
return false;
FunctionAddress = PEHeader->ImageBase + RF.BeginAddress;
FunctionAddress = COFF.getPE32Header()->ImageBase + RF.BeginAddress;
}
SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));

View File

@ -631,16 +631,10 @@ void COFFDumper::printFileHeaders() {
// Print PE header. This header does not exist if this is an object file and
// not an executable.
const pe32_header *PEHeader = nullptr;
if (std::error_code EC = Obj->getPE32Header(PEHeader))
reportError(EC, Obj->getFileName());
if (PEHeader)
if (const pe32_header *PEHeader = Obj->getPE32Header())
printPEHeader<pe32_header>(PEHeader);
const pe32plus_header *PEPlusHeader = nullptr;
if (std::error_code EC = Obj->getPE32PlusHeader(PEPlusHeader))
reportError(EC, Obj->getFileName());
if (PEPlusHeader)
if (const pe32plus_header *PEPlusHeader = Obj->getPE32PlusHeader())
printPEHeader<pe32plus_header>(PEPlusHeader);
if (const dos_header *DH = Obj->getDOSHeader())

View File

@ -38,17 +38,12 @@ public:
}
COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
const object::pe32_header *PE32Header = nullptr;
Obj.getPE32Header(PE32Header);
if (PE32Header) {
if (const object::pe32_header *PE32Header = Obj.getPE32Header())
dumpOptionalHeader(PE32Header);
} else {
const object::pe32plus_header *PE32PlusHeader = nullptr;
Obj.getPE32PlusHeader(PE32PlusHeader);
if (PE32PlusHeader) {
dumpOptionalHeader(PE32PlusHeader);
}
}
else if (const object::pe32plus_header *PE32PlusHeader =
Obj.getPE32PlusHeader())
dumpOptionalHeader(PE32PlusHeader);
dumpHeader();
dumpSections(Obj.getNumberOfSections());
dumpSymbols(Obj.getNumberOfSymbols());