@@ -372,7 +372,13 @@ CHECK PEDecoder::CheckSection(COUNT_T previousAddressEnd, COUNT_T addressStart,
372372 CHECK (alignedSize >= VAL32 (pNT->OptionalHeader .SizeOfImage ));
373373
374374 // Check expected alignments
375+ #if TARGET_WINDOWS
376+ // On Windows we expect section starts to be a multiple of SectionAlignment.
377+ // That is not an explicit requirement in the documentation, but it looks like OS loader expects it.
378+ // In contrast to this, in Unix R2R files we keep lower 16bits of sections RVA and
379+ // this condition does not hold.
375380 CHECK (CheckAligned (addressStart, VAL32 (pNT->OptionalHeader .SectionAlignment )));
381+ #endif
376382 CHECK (CheckAligned (offsetStart, VAL32 (pNT->OptionalHeader .FileAlignment )));
377383 CHECK (CheckAligned (offsetSize, VAL32 (pNT->OptionalHeader .FileAlignment )));
378384
@@ -1659,86 +1665,6 @@ CHECK PEDecoder::CheckILOnlyEntryPoint() const
16591665}
16601666#endif // TARGET_X86
16611667
1662- #ifndef DACCESS_COMPILE
1663-
1664- void PEDecoder::LayoutILOnly (void *base, bool enableExecution) const
1665- {
1666- CONTRACT_VOID
1667- {
1668- INSTANCE_CHECK;
1669- PRECONDITION (CheckZeroedMemory (base, VAL32 (FindNTHeaders ()->OptionalHeader .SizeOfImage )));
1670- // Ideally we would require the layout address to honor the section alignment constraints.
1671- // However, we do have 8K aligned IL only images which we load on 32 bit platforms. In this
1672- // case, we can only guarantee OS page alignment (which after all, is good enough.)
1673- PRECONDITION (CheckAligned ((SIZE_T)base, GetOsPageSize ()));
1674- THROWS;
1675- GC_NOTRIGGER;
1676- }
1677- CONTRACT_END;
1678-
1679- // We're going to copy everything first, and write protect what we need to later.
1680-
1681- // First, copy headers
1682- CopyMemory (base, (void *)m_base, VAL32 (FindNTHeaders ()->OptionalHeader .SizeOfHeaders ));
1683-
1684- // Now, copy all sections to appropriate virtual address
1685-
1686- IMAGE_SECTION_HEADER *sectionStart = IMAGE_FIRST_SECTION (FindNTHeaders ());
1687- IMAGE_SECTION_HEADER *sectionEnd = sectionStart + VAL16 (FindNTHeaders ()->FileHeader .NumberOfSections );
1688-
1689- IMAGE_SECTION_HEADER *section = sectionStart;
1690- while (section < sectionEnd)
1691- {
1692- // Raw data may be less than section size if tail is zero, but may be more since VirtualSize is
1693- // not padded.
1694- DWORD size = min (VAL32 (section->SizeOfRawData ), VAL32 (section->Misc .VirtualSize ));
1695-
1696- CopyMemory ((BYTE *) base + VAL32 (section->VirtualAddress ), (BYTE *) m_base + VAL32 (section->PointerToRawData ), size);
1697-
1698- // Note that our memory is zeroed already, so no need to initialize any tail.
1699-
1700- section++;
1701- }
1702-
1703- // Apply write protection to copied headers
1704- DWORD oldProtection;
1705- if (!ClrVirtualProtect ((void *) base, VAL32 (FindNTHeaders ()->OptionalHeader .SizeOfHeaders ),
1706- PAGE_READONLY, &oldProtection))
1707- ThrowLastError ();
1708-
1709- // Finally, apply proper protection to copied sections
1710- for (section = sectionStart; section < sectionEnd; section++)
1711- {
1712- // Add appropriate page protection.
1713- DWORD newProtection;
1714- if (!enableExecution)
1715- {
1716- if (section->Characteristics & IMAGE_SCN_MEM_WRITE)
1717- continue ;
1718-
1719- newProtection = PAGE_READONLY;
1720- }
1721- else
1722- {
1723- newProtection = section->Characteristics & IMAGE_SCN_MEM_EXECUTE ?
1724- PAGE_EXECUTE_READ :
1725- section->Characteristics & IMAGE_SCN_MEM_WRITE ?
1726- PAGE_READWRITE :
1727- PAGE_READONLY;
1728- }
1729-
1730- if (!ClrVirtualProtect ((void *)((BYTE*)base + VAL32 (section->VirtualAddress )),
1731- VAL32 (section->Misc .VirtualSize ),
1732- newProtection, &oldProtection))
1733- {
1734- ThrowLastError ();
1735- }
1736- }
1737-
1738- RETURN;
1739- }
1740-
1741- #endif // #ifndef DACCESS_COMPILE
17421668
17431669bool ReadResourceDirectoryHeader (const PEDecoder *pDecoder, DWORD rvaOfResourceSection, DWORD rva, IMAGE_RESOURCE_DIRECTORY_ENTRY** ppDirectoryEntries, IMAGE_RESOURCE_DIRECTORY **ppResourceDirectory)
17441670{
0 commit comments