[PATCH] COFF: Minor fix to Section size calculation

Keno Fischer kfischer at college.harvard.edu
Tue Jun 10 10:49:42 PDT 2014


I noticed llvm-dwarfdump was crashing on COFF dlls with embedded DWARF debug info. The reason was that SizeOfRawData actually indicates the size on disc which is not the size of the actual section data in a PE file (*.dll, *.exe), because of alignment rules. Instead the size of given by min(SizeOfRawData,VirtualSize). 

I'm not sure what to do with respect to testing, since compiling a file from source would require a COFF capable linker. Is it acceptable to commit an entire PE exectuable to the repository? If so, I'll try to come up with a minimal example where this happens.

http://reviews.llvm.org/D4087

Files:
  lib/Object/COFFObjectFile.cpp

Index: lib/Object/COFFObjectFile.cpp
===================================================================
--- lib/Object/COFFObjectFile.cpp
+++ lib/Object/COFFObjectFile.cpp
@@ -829,11 +829,21 @@
   // 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;
+  // COFF requires sections to follow certain alignment rules in PE
+  // files. Thus (in a PE file) a section's actual data may be smaller
+  // than SizeOfRawData.
+  // Since for debug sections later on the actual data matters, we take
+  // the min of SizeOfRawData and VirtualSize which is the acutal size
+  // of the data.
+  uintptr_t Size = Sec->SizeOfRawData;
+  // COFF Object files always have VirtualSize set to 0
+  if (Sec->VirtualSize != 0)
+    Size = std::min(Size,(uintptr_t)Sec->VirtualSize);
+  uintptr_t ConEnd = ConStart + Size;
   if (ConEnd > uintptr_t(Data->getBufferEnd()))
     return object_error::parse_failed;
   Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
-                          Sec->SizeOfRawData);
+                          Size);
   return object_error::success;
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4087.10286.patch
Type: text/x-patch
Size: 1296 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140610/ed624127/attachment.bin>


More information about the llvm-commits mailing list