[llvm] r219388 - Object, COFF: Cap the section contents to min(VirtualSize, SizeOfRawData)
David Majnemer
david.majnemer at gmail.com
Thu Oct 9 00:49:29 PDT 2014
Author: majnemer
Date: Thu Oct 9 02:49:28 2014
New Revision: 219388
URL: http://llvm.org/viewvc/llvm-project?rev=219388&view=rev
Log:
Object, COFF: Cap the section contents to min(VirtualSize, SizeOfRawData)
It is not useful to return the data beyond VirtualSize it's less than
SizeOfRawData.
An implementation detail of COFF requires the section size to be rounded
up to a multiple of FileAlignment; this means that SizeOfRawData is not
representative of how large the section is. Instead, we should cap it
to VirtualSize when this occurs as it represents the true size of the
section.
Note that this is only relevant in executable files because this
rounding doesn't occur in object files (and VirtualSize is always zero).
Modified:
llvm/trunk/lib/Object/COFFObjectFile.cpp
Modified: llvm/trunk/lib/Object/COFFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFObjectFile.cpp?rev=219388&r1=219387&r2=219388&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/COFFObjectFile.cpp Thu Oct 9 02:49:28 2014
@@ -869,19 +869,33 @@ std::error_code COFFObjectFile::getSecti
std::error_code
COFFObjectFile::getSectionContents(const coff_section *Sec,
ArrayRef<uint8_t> &Res) const {
- // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't
- // do anything interesting for them.
+ // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
+ // don't do anything interesting for them.
assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
"BSS sections don't have contents!");
// 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;
- uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
+ // 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;
if (ConEnd > uintptr_t(Data.getBufferEnd()))
return object_error::parse_failed;
- Res = makeArrayRef(reinterpret_cast<const uint8_t*>(ConStart),
- Sec->SizeOfRawData);
+ Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), DataSize);
return object_error::success;
}
More information about the llvm-commits
mailing list