From 06f1f357e360c3a0bcc703182878583a98120f65 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Thu, 9 Oct 2014 08:42:31 +0000 Subject: [PATCH] Object, COFF: Move the VirtualSize/SizeOfRawData logic to getSectionSize While getSectionContents was updated to do the right thing, getSectionSize wasn't. Move the logic to getSectionSize and leverage it from getSectionContents. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219391 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/COFF.h | 1 + lib/Object/COFFObjectFile.cpp | 43 +++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/include/llvm/Object/COFF.h b/include/llvm/Object/COFF.h index 75857c954d8..c3428cdd1aa 100644 --- a/include/llvm/Object/COFF.h +++ b/include/llvm/Object/COFF.h @@ -655,6 +655,7 @@ public: } std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const; + uint64_t getSectionSize(const coff_section *Sec) const; std::error_code getSectionContents(const coff_section *Sec, ArrayRef &Res) const; diff --git a/lib/Object/COFFObjectFile.cpp b/lib/Object/COFFObjectFile.cpp index 72a6db4aab9..3beab00ed7d 100644 --- a/lib/Object/COFFObjectFile.cpp +++ b/lib/Object/COFFObjectFile.cpp @@ -271,8 +271,7 @@ uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const { } uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const { - const coff_section *Sec = toSec(Ref); - return Sec->SizeOfRawData; + return getSectionSize(toSec(Ref)); } std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, @@ -866,6 +865,26 @@ std::error_code COFFObjectFile::getSectionName(const coff_section *Sec, return object_error::success; } +uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const { + // SizeOfRawData and VirtualSize change what they represent depending on + // whether or not we have an executable image. + // + // For object files, SizeOfRawData contains the size of section's data; + // VirtualSize is always zero. + // + // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the + // actual section size is in VirtualSize. It is possible for VirtualSize to + // be greater than SizeOfRawData; the contents past that point should be + // considered to be zero. + uint32_t SectionSize; + if (Sec->VirtualSize) + SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData); + else + SectionSize = Sec->SizeOfRawData; + + return SectionSize; +} + std::error_code COFFObjectFile::getSectionContents(const coff_section *Sec, ArrayRef &Res) const { @@ -877,25 +896,11 @@ COFFObjectFile::getSectionContents(const coff_section *Sec, // 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; - // SizeOfRawData and VirtualSize change what they represent depending on - // whether or not we have an executable image. - // - // For object files, SizeOfRawData contains the size of section's data; - // VirtualSize is always zero. - // - // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the - // actual section size is in VirtualSize. It is possible for VirtualSize to - // be greater than SizeOfRawData; the contents past that point should be - // considered to be zero. - uint32_t DataSize; - if (Sec->VirtualSize) - DataSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData); - else - DataSize = Sec->SizeOfRawData; - uintptr_t ConEnd = ConStart + DataSize; + uint32_t SectionSize = getSectionSize(Sec); + uintptr_t ConEnd = ConStart + SectionSize; if (ConEnd > uintptr_t(Data.getBufferEnd())) return object_error::parse_failed; - Res = makeArrayRef(reinterpret_cast(ConStart), DataSize); + Res = makeArrayRef(reinterpret_cast(ConStart), SectionSize); return object_error::success; }