From e1cb2c0f404d9fe68f5f465a281be295ca24ec33 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 14 May 2019 04:22:51 +0000 Subject: [Object] Change ObjectFile::getSectionContents to return Expected> Change std::error_code getSectionContents(DataRefImpl, StringRef &) const; to Expected> getSectionContents(DataRefImpl) const; Many object formats use ArrayRef as the underlying type, which is generally better than StringRef to represent binary data, so change the type to decrease the number of type conversions. Reviewed By: ruiu, sbc100 Differential Revision: https://reviews.llvm.org/D61781 llvm-svn: 360648 --- llvm/lib/Object/COFFObjectFile.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'llvm/lib/Object/COFFObjectFile.cpp') diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp index ccb861cb187..854664e679d 100644 --- a/llvm/lib/Object/COFFObjectFile.cpp +++ b/llvm/lib/Object/COFFObjectFile.cpp @@ -292,13 +292,13 @@ uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const { return getSectionSize(toSec(Ref)); } -std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, - StringRef &Result) const { +Expected> +COFFObjectFile::getSectionContents(DataRefImpl Ref) const { const coff_section *Sec = toSec(Ref); ArrayRef Res; - std::error_code EC = getSectionContents(Sec, Res); - Result = StringRef(reinterpret_cast(Res.data()), Res.size()); - return EC; + if (Error E = getSectionContents(Sec, Res)) + return std::move(E); + return Res; } uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const { @@ -1118,22 +1118,21 @@ uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const { return Sec->SizeOfRawData; } -std::error_code -COFFObjectFile::getSectionContents(const coff_section *Sec, - ArrayRef &Res) const { +Error COFFObjectFile::getSectionContents(const coff_section *Sec, + ArrayRef &Res) const { // In COFF, a virtual section won't have any in-file // content, so the file pointer to the content will be zero. if (Sec->PointerToRawData == 0) - return std::error_code(); + return Error::success(); // The only thing that we need to verify is that the contents is contained // within the file bounds. We don't need to make sure it doesn't cover other // data, as there's nothing that says that is not allowed. uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; uint32_t SectionSize = getSectionSize(Sec); if (checkOffset(Data, ConStart, SectionSize)) - return object_error::parse_failed; + return make_error(); Res = makeArrayRef(reinterpret_cast(ConStart), SectionSize); - return std::error_code(); + return Error::success(); } const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { -- cgit v1.2.3