From fffb7130ddd43d8c53aaa077bbd07a0b14dadeb5 Mon Sep 17 00:00:00 2001 From: Eric Beckmann Date: Mon, 8 May 2017 01:48:55 +0000 Subject: [PATCH] Hopefully one last commit to fix this patch, addresses string reference issues. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302395 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/COFF.h | 5 +++-- lib/Object/COFFObjectFile.cpp | 10 +++------- tools/llvm-readobj/COFFDumper.cpp | 8 ++++++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/include/llvm/Object/COFF.h b/include/llvm/Object/COFF.h index 94fc10340e0..8b9b4973717 100644 --- a/include/llvm/Object/COFF.h +++ b/include/llvm/Object/COFF.h @@ -22,6 +22,7 @@ #include "llvm/Object/ObjectFile.h" #include "llvm/Support/BinaryByteStream.h" #include "llvm/Support/COFF.h" +#include "llvm/Support/ConvertUTF.h" #include "llvm/Support/Endian.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" @@ -1074,7 +1075,7 @@ public: ResourceSectionRef() = default; explicit ResourceSectionRef(StringRef Ref) : BBS(Ref, support::little) {} - ErrorOr getEntryNameString(const coff_resource_dir_entry &Entry); + ErrorOr> getEntryNameString(const coff_resource_dir_entry &Entry); ErrorOr getEntrySubDir(const coff_resource_dir_entry &Entry); ErrorOr getBaseTable(); @@ -1083,7 +1084,7 @@ private: BinaryByteStream BBS; ErrorOr getTableAtOffset(uint32_t Offset); - ErrorOr getDirStringAtOffset(uint32_t Offset); + ErrorOr> getDirStringAtOffset(uint32_t Offset); }; // Corresponds to `_FPO_DATA` structure in the PE/COFF spec. diff --git a/lib/Object/COFFObjectFile.cpp b/lib/Object/COFFObjectFile.cpp index 39762e1d216..b1223e81be4 100644 --- a/lib/Object/COFFObjectFile.cpp +++ b/lib/Object/COFFObjectFile.cpp @@ -21,7 +21,6 @@ #include "llvm/Object/ObjectFile.h" #include "llvm/Support/BinaryStreamReader.h" #include "llvm/Support/COFF.h" -#include "llvm/Support/ConvertUTF.h" #include "llvm/Support/Endian.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" @@ -1597,7 +1596,7 @@ std::error_code BaseRelocRef::getRVA(uint32_t &Result) const { if (auto EC = errorToErrorCode(X)) \ return EC; -ErrorOr ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) { +ErrorOr> ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) { BinaryStreamReader Reader = BinaryStreamReader(BBS); Reader.setOffset(Offset); uint16_t Length; @@ -1606,13 +1605,10 @@ ErrorOr ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) { // Strings are stored as 2-byte aligned unicode characters but readFixedString // assumes byte string, so we double length. RETURN_IF_ERROR(Reader.readArray(RawDirString, Length)); - std::string DirString; - if (!llvm::convertUTF16ToUTF8String(RawDirString, DirString)) - return object_error::parse_failed; - return DirString; + return RawDirString; } -ErrorOr +ErrorOr> ResourceSectionRef::getEntryNameString(const coff_resource_dir_entry &Entry) { return getDirStringAtOffset(Entry.Identifier.getNameOffset()); } diff --git a/tools/llvm-readobj/COFFDumper.cpp b/tools/llvm-readobj/COFFDumper.cpp index 64fd60ea209..9ffa8292738 100644 --- a/tools/llvm-readobj/COFFDumper.cpp +++ b/tools/llvm-readobj/COFFDumper.cpp @@ -44,6 +44,7 @@ #include "llvm/Support/BinaryByteStream.h" #include "llvm/Support/BinaryStreamReader.h" #include "llvm/Support/COFF.h" +#include "llvm/Support/ConvertUTF.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/DataExtractor.h" @@ -1560,9 +1561,12 @@ void COFFDumper::printResourceDirectoryTable( SmallString<20> IDStr; raw_svector_ostream OS(IDStr); if (i < Table.NumberOfNameEntries) { - StringRef EntryNameString = unwrapOrError(RSF.getEntryNameString(Entry)); + ArrayRef RawEntryNameString = unwrapOrError(RSF.getEntryNameString(Entry)); + std::string EntryNameString; + if (!llvm::convertUTF16ToUTF8String(RawEntryNameString, EntryNameString)) + error(object_error::parse_failed); OS << ": "; - OS << EntryNameString.str(); + OS << EntryNameString; } else { if (Level == "Type") { ScopedPrinter Printer(OS);