diff --git a/llvm/include/llvm/Object/COFF.h b/llvm/include/llvm/Object/COFF.h --- a/llvm/include/llvm/Object/COFF.h +++ b/llvm/include/llvm/Object/COFF.h @@ -1079,13 +1079,15 @@ uint64_t getImageBase() const; Error getVaPtr(uint64_t VA, uintptr_t &Res) const; - Error getRvaPtr(uint32_t Rva, uintptr_t &Res) const; + Error getRvaPtr(uint32_t Rva, uintptr_t &Res, + const char *ErrorContext = nullptr) const; /// Given an RVA base and size, returns a valid array of bytes or an error /// code if the RVA and size is not contained completely within a valid /// section. Error getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size, - ArrayRef &Contents) const; + ArrayRef &Contents, + const char *ErrorContext = nullptr) const; Error getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const; diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp --- a/llvm/lib/Object/COFFObjectFile.cpp +++ b/llvm/lib/Object/COFFObjectFile.cpp @@ -447,7 +447,8 @@ // Check that the string table is null terminated if has any in it. if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0) - return errorCodeToError(object_error::parse_failed); + return createStringError(object_error::parse_failed, + "string table missing null terminator"); return Error::success(); } @@ -469,7 +470,8 @@ } // Returns the file offset for the given RVA. -Error COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const { +Error COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res, + const char *ErrorContext) const { for (const SectionRef &S : sections()) { const coff_section *Section = getCOFFSection(S); uint32_t SectionStart = Section->VirtualAddress; @@ -481,11 +483,18 @@ return Error::success(); } } - return errorCodeToError(object_error::parse_failed); + if (ErrorContext) + return createStringError(object_error::parse_failed, + "RVA %" PRIx32 " for %s not found", Addr, + ErrorContext); + else + return createStringError(object_error::parse_failed, + "RVA %" PRIx32 " not found", Addr); } Error COFFObjectFile::getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size, - ArrayRef &Contents) const { + ArrayRef &Contents, + const char *ErrorContext) const { for (const SectionRef &S : sections()) { const coff_section *Section = getCOFFSection(S); uint32_t SectionStart = Section->VirtualAddress; @@ -501,7 +510,13 @@ return Error::success(); } } - return errorCodeToError(object_error::parse_failed); + if (ErrorContext) + return createStringError(object_error::parse_failed, + "RVA %" PRIx32 " for %s not found", RVA, + ErrorContext); + else + return createStringError(object_error::parse_failed, + "RVA %" PRIx32 " not found", RVA); } // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name @@ -521,11 +536,12 @@ const codeview::DebugInfo *&PDBInfo, StringRef &PDBFileName) const { ArrayRef InfoBytes; - if (Error E = getRvaAndSizeAsBytes( - DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes)) + if (Error E = + getRvaAndSizeAsBytes(DebugDir->AddressOfRawData, DebugDir->SizeOfData, + InfoBytes, "PDB info")) return E; if (InfoBytes.size() < sizeof(*PDBInfo) + 1) - return errorCodeToError(object_error::parse_failed); + return createStringError(object_error::parse_failed, "PDB info too small"); PDBInfo = reinterpret_cast(InfoBytes.data()); InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo)); PDBFileName = StringRef(reinterpret_cast(InfoBytes.data()), @@ -563,7 +579,7 @@ // Find the section that contains the RVA. This is needed because the RVA is // the import table's memory address which is different from its file offset. uintptr_t IntPtr = 0; - if (Error E = getRvaPtr(ImportTableRva, IntPtr)) + if (Error E = getRvaPtr(ImportTableRva, IntPtr, "import table")) return E; if (Error E = checkOffset(Data, IntPtr, DataEntry->Size)) return E; @@ -586,7 +602,7 @@ sizeof(delay_import_directory_table_entry) - 1; uintptr_t IntPtr = 0; - if (Error E = getRvaPtr(RVA, IntPtr)) + if (Error E = getRvaPtr(RVA, IntPtr, "delay import table")) return E; DelayImportDirectory = reinterpret_cast< const delay_import_directory_table_entry *>(IntPtr); @@ -607,7 +623,7 @@ uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; uintptr_t IntPtr = 0; - if (Error E = getRvaPtr(ExportTableRva, IntPtr)) + if (Error E = getRvaPtr(ExportTableRva, IntPtr, "export table")) return E; ExportDirectory = reinterpret_cast(IntPtr); @@ -623,7 +639,8 @@ return Error::success(); uintptr_t IntPtr = 0; - if (Error E = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) + if (Error E = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr, + "base reloc table")) return E; BaseRelocHeader = reinterpret_cast( IntPtr); @@ -646,10 +663,12 @@ // Check that the size is a multiple of the entry size. if (DataEntry->Size % sizeof(debug_directory) != 0) - return errorCodeToError(object_error::parse_failed); + return createStringError(object_error::parse_failed, + "debug directory has uneven size"); uintptr_t IntPtr = 0; - if (Error E = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) + if (Error E = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr, + "debug directory")) return E; DebugDirectoryBegin = reinterpret_cast(IntPtr); DebugDirectoryEnd = reinterpret_cast( @@ -680,7 +699,8 @@ static_cast(DataEntry->Size), DirSize); uintptr_t IntPtr = 0; - if (Error E = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) + if (Error E = + getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr, "TLS directory")) return E; if (is64()) @@ -701,7 +721,8 @@ if (DataEntry->RelativeVirtualAddress == 0) return Error::success(); uintptr_t IntPtr = 0; - if (Error E = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) + if (Error E = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr, + "load config table")) return E; LoadConfig = (const void *)IntPtr; @@ -749,7 +770,8 @@ CurPtr = DH->AddressOfNewExeHeader; // Check the PE magic bytes. ("PE\0\0") if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) { - return errorCodeToError(object_error::parse_failed); + return createStringError(object_error::parse_failed, + "incorrect PE magic"); } CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes. HasPEHeader = true; @@ -805,7 +827,8 @@ DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize; } else { // It's neither PE32 nor PE32+. - return errorCodeToError(object_error::parse_failed); + return createStringError(object_error::parse_failed, + "incorrect PE magic"); } if (Error E = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)) return E; @@ -834,7 +857,8 @@ } else { // We had better not have any symbols if we don't have a symbol table. if (getNumberOfSymbols() != 0) { - return errorCodeToError(object_error::parse_failed); + return createStringError(object_error::parse_failed, + "symbol table missing"); } } @@ -1021,13 +1045,14 @@ // We already verified the section table data, so no need to check again. return SectionTable + (Index - 1); } - return errorCodeToError(object_error::parse_failed); + return createStringError(object_error::parse_failed, + "section index out of bounds"); } Expected COFFObjectFile::getString(uint32_t Offset) const { if (StringTableSize <= 4) // Tried to get a string from an empty string table. - return errorCodeToError(object_error::parse_failed); + return createStringError(object_error::parse_failed, "string table empty"); if (Offset >= StringTableSize) return errorCodeToError(object_error::unexpected_eof); return StringRef(StringTable + Offset); @@ -1414,7 +1439,8 @@ Error ImportDirectoryEntryRef::getName(StringRef &Result) const { uintptr_t IntPtr = 0; - if (Error E = OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr)) + if (Error E = OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr, + "import directory name")) return E; Result = StringRef(reinterpret_cast(IntPtr)); return Error::success(); @@ -1460,7 +1486,8 @@ Error DelayImportDirectoryEntryRef::getName(StringRef &Result) const { uintptr_t IntPtr = 0; - if (Error E = OwningObject->getRvaPtr(Table[Index].Name, IntPtr)) + if (Error E = OwningObject->getRvaPtr(Table[Index].Name, IntPtr, + "delay import directory name")) return E; Result = StringRef(reinterpret_cast(IntPtr)); return Error::success(); @@ -1477,7 +1504,7 @@ uint32_t RVA = Table[Index].DelayImportAddressTable + AddrIndex * (OwningObject->is64() ? 8 : 4); uintptr_t IntPtr = 0; - if (Error E = OwningObject->getRvaPtr(RVA, IntPtr)) + if (Error E = OwningObject->getRvaPtr(RVA, IntPtr, "import address")) return E; if (OwningObject->is64()) Result = *reinterpret_cast(IntPtr); @@ -1499,7 +1526,8 @@ // by ordinal, the empty string is set as a result. Error ExportDirectoryEntryRef::getDllName(StringRef &Result) const { uintptr_t IntPtr = 0; - if (Error E = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr)) + if (Error E = + OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr, "dll name")) return E; Result = StringRef(reinterpret_cast(IntPtr)); return Error::success(); @@ -1520,8 +1548,8 @@ // Returns the address of the current export symbol. Error ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { uintptr_t IntPtr = 0; - if (Error EC = - OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr)) + if (Error EC = OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, + IntPtr, "export address")) return EC; const export_address_table_entry *entry = reinterpret_cast(IntPtr); @@ -1534,8 +1562,8 @@ Error ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const { uintptr_t IntPtr = 0; - if (Error EC = - OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr)) + if (Error EC = OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr, + "export ordinal table")) return EC; const ulittle16_t *Start = reinterpret_cast(IntPtr); @@ -1545,11 +1573,12 @@ I < E; ++I, ++Offset) { if (*I != Index) continue; - if (Error EC = - OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr)) + if (Error EC = OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr, + "export table entry")) return EC; const ulittle32_t *NamePtr = reinterpret_cast(IntPtr); - if (Error EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) + if (Error EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr, + "export symbol name")) return EC; Result = StringRef(reinterpret_cast(IntPtr)); return Error::success(); @@ -1562,7 +1591,8 @@ const data_directory *DataEntry = OwningObject->getDataDirectory(COFF::EXPORT_TABLE); if (!DataEntry) - return errorCodeToError(object_error::parse_failed); + return createStringError(object_error::parse_failed, + "export table missing"); uint32_t RVA; if (auto EC = getExportRVA(RVA)) return EC; @@ -1577,7 +1607,7 @@ if (auto EC = getExportRVA(RVA)) return EC; uintptr_t IntPtr = 0; - if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr)) + if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr, "export forward target")) return EC; Result = StringRef(reinterpret_cast(IntPtr)); return Error::success(); @@ -1606,7 +1636,7 @@ RVA = Entry64[Index].getHintNameRVA(); } uintptr_t IntPtr = 0; - if (Error EC = OwningObject->getRvaPtr(RVA, IntPtr)) + if (Error EC = OwningObject->getRvaPtr(RVA, IntPtr, "import symbol name")) return EC; // +2 because the first two bytes is hint. Result = StringRef(reinterpret_cast(IntPtr + 2)); @@ -1645,7 +1675,7 @@ RVA = Entry64[Index].getHintNameRVA(); } uintptr_t IntPtr = 0; - if (Error EC = OwningObject->getRvaPtr(RVA, IntPtr)) + if (Error EC = OwningObject->getRvaPtr(RVA, IntPtr, "import symbol ordinal")) return EC; Result = *reinterpret_cast(IntPtr); return Error::success(); diff --git a/llvm/test/tools/llvm-readobj/COFF/tls-directory.test b/llvm/test/tools/llvm-readobj/COFF/tls-directory.test --- a/llvm/test/tools/llvm-readobj/COFF/tls-directory.test +++ b/llvm/test/tools/llvm-readobj/COFF/tls-directory.test @@ -104,7 +104,7 @@ # RUN: yaml2obj %s --docnum=2 -o %t.bad-tls-rva.exe -DTLSRVA=999999 -DTLSSIZE=40 # RUN: not llvm-readobj --coff-tls-directory %t.bad-tls-rva.exe 2>&1 | FileCheck %s --check-prefix BAD-TLS-RVA-ERR -# BAD-TLS-RVA-ERR: error: '{{.*}}': Invalid data was encountered while parsing the file +# BAD-TLS-RVA-ERR: error: '{{.*}}': RVA {{.*}} for TLS directory not found --- !COFF OptionalHeader: