From 2d5c2cd3ce208ebfac2bceb4c73157bef4e00fdc Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Wed, 3 May 2017 17:11:11 +0000 Subject: [llvm-readobj] Update readobj to re-use parsing code. llvm-readobj hand rolls some CodeView parsing code for string tables, so this patch updates it to re-use some of the newly introduced parsing code in LLVMDebugInfoCodeView. Differential Revision: https://reviews.llvm.org/D32772 llvm-svn: 302052 --- llvm/lib/DebugInfo/CodeView/StringTable.cpp | 12 ++++++------ llvm/lib/DebugInfo/CodeView/SymbolDumper.cpp | 25 ++++++++++++------------ llvm/lib/DebugInfo/PDB/Native/PDBStringTable.cpp | 20 ++++++++++++------- 3 files changed, 32 insertions(+), 25 deletions(-) (limited to 'llvm/lib/DebugInfo') diff --git a/llvm/lib/DebugInfo/CodeView/StringTable.cpp b/llvm/lib/DebugInfo/CodeView/StringTable.cpp index 5d3a0dd9cfa..f496854ffaf 100644 --- a/llvm/lib/DebugInfo/CodeView/StringTable.cpp +++ b/llvm/lib/DebugInfo/CodeView/StringTable.cpp @@ -18,17 +18,17 @@ using namespace llvm::codeview; StringTableRef::StringTableRef() {} -Error StringTableRef::initialize(BinaryStreamReader &Reader) { - return Reader.readStreamRef(Stream, Reader.bytesRemaining()); +Error StringTableRef::initialize(BinaryStreamRef Contents) { + Stream = Contents; + return Error::success(); } -StringRef StringTableRef::getString(uint32_t Offset) const { +Expected StringTableRef::getString(uint32_t Offset) const { BinaryStreamReader Reader(Stream); Reader.setOffset(Offset); StringRef Result; - Error EC = Reader.readCString(Result); - assert(!EC); - consumeError(std::move(EC)); + if (auto EC = Reader.readCString(Result)) + return std::move(EC); return Result; } diff --git a/llvm/lib/DebugInfo/CodeView/SymbolDumper.cpp b/llvm/lib/DebugInfo/CodeView/SymbolDumper.cpp index 134471e81ca..5395e4349b2 100644 --- a/llvm/lib/DebugInfo/CodeView/SymbolDumper.cpp +++ b/llvm/lib/DebugInfo/CodeView/SymbolDumper.cpp @@ -13,6 +13,7 @@ #include "llvm/DebugInfo/CodeView/CVSymbolVisitor.h" #include "llvm/DebugInfo/CodeView/CVTypeDumper.h" #include "llvm/DebugInfo/CodeView/EnumTables.h" +#include "llvm/DebugInfo/CodeView/StringTable.h" #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" #include "llvm/DebugInfo/CodeView/SymbolDumpDelegate.h" #include "llvm/DebugInfo/CodeView/SymbolRecord.h" @@ -369,14 +370,14 @@ Error CVSymbolDumperImpl::visitKnownRecord( DictScope S(W, "DefRangeSubfield"); if (ObjDelegate) { - StringRef StringTable = ObjDelegate->getStringTable(); - auto ProgramStringTableOffset = DefRangeSubfield.Program; - if (ProgramStringTableOffset >= StringTable.size()) + StringTableRef Strings = ObjDelegate->getStringTable(); + auto ExpectedProgram = Strings.getString(DefRangeSubfield.Program); + if (!ExpectedProgram) { + consumeError(ExpectedProgram.takeError()); return llvm::make_error( "String table offset outside of bounds of String Table!"); - StringRef Program = - StringTable.drop_front(ProgramStringTableOffset).split('\0').first; - W.printString("Program", Program); + } + W.printString("Program", *ExpectedProgram); } W.printNumber("OffsetInParent", DefRangeSubfield.OffsetInParent); printLocalVariableAddrRange(DefRangeSubfield.Range, @@ -390,14 +391,14 @@ Error CVSymbolDumperImpl::visitKnownRecord(CVSymbol &CVR, DictScope S(W, "DefRange"); if (ObjDelegate) { - StringRef StringTable = ObjDelegate->getStringTable(); - auto ProgramStringTableOffset = DefRange.Program; - if (ProgramStringTableOffset >= StringTable.size()) + StringTableRef Strings = ObjDelegate->getStringTable(); + auto ExpectedProgram = Strings.getString(DefRange.Program); + if (!ExpectedProgram) { + consumeError(ExpectedProgram.takeError()); return llvm::make_error( "String table offset outside of bounds of String Table!"); - StringRef Program = - StringTable.drop_front(ProgramStringTableOffset).split('\0').first; - W.printString("Program", Program); + } + W.printString("Program", *ExpectedProgram); } printLocalVariableAddrRange(DefRange.Range, DefRange.getRelocationOffset()); printLocalVariableAddrGap(DefRange.Gaps); diff --git a/llvm/lib/DebugInfo/PDB/Native/PDBStringTable.cpp b/llvm/lib/DebugInfo/PDB/Native/PDBStringTable.cpp index ee32f61f16f..e84573fe07b 100644 --- a/llvm/lib/DebugInfo/PDB/Native/PDBStringTable.cpp +++ b/llvm/lib/DebugInfo/PDB/Native/PDBStringTable.cpp @@ -42,7 +42,11 @@ Error PDBStringTable::readHeader(BinaryStreamReader &Reader) { } Error PDBStringTable::readStrings(BinaryStreamReader &Reader) { - if (auto EC = Strings.initialize(Reader)) { + BinaryStreamRef Stream; + if (auto EC = Reader.readStreamRef(Stream)) + return EC; + + if (auto EC = Strings.initialize(Stream)) { return joinErrors(std::move(EC), make_error(raw_error_code::corrupt_file, "Invalid hash table byte length")); @@ -99,11 +103,11 @@ Error PDBStringTable::reload(BinaryStreamReader &Reader) { return Error::success(); } -StringRef PDBStringTable::getStringForID(uint32_t ID) const { +Expected PDBStringTable::getStringForID(uint32_t ID) const { return Strings.getString(ID); } -uint32_t PDBStringTable::getIDForString(StringRef Str) const { +Expected PDBStringTable::getIDForString(StringRef Str) const { uint32_t Hash = (Header->HashVersion == 1) ? hashStringV1(Str) : hashStringV2(Str); size_t Count = IDs.size(); @@ -115,12 +119,14 @@ uint32_t PDBStringTable::getIDForString(StringRef Str) const { uint32_t Index = (Start + I) % Count; uint32_t ID = IDs[Index]; - StringRef S = getStringForID(ID); - if (S == Str) + auto ExpectedStr = getStringForID(ID); + if (!ExpectedStr) + return ExpectedStr.takeError(); + + if (*ExpectedStr == Str) return ID; } - // IDs[0] contains the ID of the "invalid" entry. - return IDs[0]; + return make_error(raw_error_code::no_entry); } FixedStreamArray PDBStringTable::name_ids() const { -- cgit v1.2.3