diff options
author | Fangrui Song <maskray@google.com> | 2019-05-14 04:22:51 +0000 |
---|---|---|
committer | Fangrui Song <maskray@google.com> | 2019-05-14 04:22:51 +0000 |
commit | e1cb2c0f404d9fe68f5f465a281be295ca24ec33 (patch) | |
tree | 7b6e58ac12ce6b1e8deb5e2542f4a95cba445585 /llvm/lib/Object/COFFObjectFile.cpp | |
parent | fe1aec0dbb1638b5ce84c9ad71d1c8bee4f1f8ac (diff) | |
download | bcm5719-llvm-e1cb2c0f404d9fe68f5f465a281be295ca24ec33.tar.gz bcm5719-llvm-e1cb2c0f404d9fe68f5f465a281be295ca24ec33.zip |
[Object] Change ObjectFile::getSectionContents to return Expected<ArrayRef<uint8_t>>
Change
std::error_code getSectionContents(DataRefImpl, StringRef &) const;
to
Expected<ArrayRef<uint8_t>> getSectionContents(DataRefImpl) const;
Many object formats use ArrayRef<uint8_t> 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
Diffstat (limited to 'llvm/lib/Object/COFFObjectFile.cpp')
-rw-r--r-- | llvm/lib/Object/COFFObjectFile.cpp | 21 |
1 files changed, 10 insertions, 11 deletions
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<ArrayRef<uint8_t>> +COFFObjectFile::getSectionContents(DataRefImpl Ref) const { const coff_section *Sec = toSec(Ref); ArrayRef<uint8_t> Res; - std::error_code EC = getSectionContents(Sec, Res); - Result = StringRef(reinterpret_cast<const char*>(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<uint8_t> &Res) const { +Error COFFObjectFile::getSectionContents(const coff_section *Sec, + ArrayRef<uint8_t> &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<BinaryError>(); Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize); - return std::error_code(); + return Error::success(); } const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { |