void FixVSizes(IMAGE_NT_HEADERS * pNTH) { IMAGE_SECTION_HEADER * pSH; // fix virtual parts of the PE Header (a must for win2k) pSH = IMAGE_FIRST_SECTION(pNTH); for(WORD i=0; i < pNTH->FileHeader.NumberOfSections-1; i++) { pSH->Misc.VirtualSize = (pSH+1)->VirtualAddress - pSH->VirtualAddress; pSH++; } // (pSH -> pointer to last section) // align last section VSize to Sectionalignment pSH->Misc.VirtualSize = AlignUp(pSH->Misc.VirtualSize, HDR3264(pNTH, OptionalHeader.SectionAlignment)); if(IsPE64(pNTH)){ if(pSH->Misc.PhysicalAddress) ((IMAGE_NT_HEADERS64 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->Misc.VirtualSize; else ((IMAGE_NT_HEADERS64 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->SizeOfRawData; } else{ if(pSH->Misc.PhysicalAddress) ((IMAGE_NT_HEADERS32 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->Misc.VirtualSize; else ((IMAGE_NT_HEADERS32 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->SizeOfRawData; } }
HRESULT RuntimeIsValidAssemblyOnThisPlatform_CheckProcessorArchitecture(PEKIND processorArchitecture, BOOL bForInstall) { LIMITED_METHOD_CONTRACT; HRESULT hr = S_OK; // MSIL / legacy images always allowed if (IsPEMSIL(processorArchitecture) || (processorArchitecture == peNone)) { goto Exit; } else if (IsPE32(processorArchitecture)) { #ifdef _TARGET_ARM_ // ARM can use only native ones if (processorArchitecture != TargetNativePEKIND()) { hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); goto Exit; } #else //!_TARGET_ARM_ //ARM assemblies can be installed only on ARM if (processorArchitecture == peARM) { hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); goto Exit; } #endif //!_TARGET_ARM_ if (bForInstall) { goto Exit; } else { // won't allow bind to x86 while in 64 bit process. if (!IsProcess32()) { hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); } goto Exit; } } // 64 bit images must match processor type else if(IsPE64(processorArchitecture)) { if (!IsProcess32() && (processorArchitecture == TargetNativePEKIND())) { goto Exit; } else if (bForInstall && (GetCurrentRealProcessorPEKIND() == processorArchitecture)) { goto Exit; } } // Everything else, fails match hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); Exit: return hr; }
DWORD StripSection(void * pMap, DWORD dwFsize, DWORD nSection) { IMAGE_NT_HEADERS * pNTH; IMAGE_SECTION_HEADER * pSH, * pSHC; DWORD dwNewFsize; DWORD Diff; unsigned int i; pNTH = GetNTHeader(pMap); // section doesn't exist / only one section if(nSection >= pNTH->FileHeader.NumberOfSections || pNTH->FileHeader.NumberOfSections == 1) return 0; pSHC = IMAGE_FIRST_SECTION(pNTH); // used in loop pSH = &pSHC[nSection]; // section to delete pNTH->FileHeader.NumberOfSections--; if(nSection == pNTH->FileHeader.NumberOfSections) { // last section -> truncate at section start dwNewFsize = pSH->PointerToRawData; // ???? pSH--; if(IsPE64(pNTH)){ if(pSH->Misc.PhysicalAddress) ((IMAGE_NT_HEADERS64 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->Misc.VirtualSize; else // WATCOM is always a bit special >:-) ((IMAGE_NT_HEADERS64 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->SizeOfRawData; } else{ if(pSH->Misc.PhysicalAddress) ((IMAGE_NT_HEADERS32 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->Misc.VirtualSize; else ((IMAGE_NT_HEADERS32 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->SizeOfRawData; } } else // not the last section { if(pSH->SizeOfRawData != 0) { Diff = AlignUp(pSH->SizeOfRawData, HDR3264(pNTH, OptionalHeader.FileAlignment)); dwNewFsize = dwFsize - Diff; // copy section(s) after this section to the start of this section memcpy((void *)((ULONG_PTR)pMap + pSH->PointerToRawData), (void *)((ULONG_PTR)pMap + pSH->PointerToRawData + Diff), dwFsize - pSH->PointerToRawData - Diff); for(i=0; i <= pNTH->FileHeader.NumberOfSections; i++) { if(pSHC->PointerToRawData >= (pSH->PointerToRawData + Diff)) pSHC->PointerToRawData -= Diff; pSHC++; } } // fix section header if(nSection == 0) { pSH->SizeOfRawData = 0; pNTH->FileHeader.NumberOfSections++; } else { (pSH-1)->Misc.VirtualSize += pSH->Misc.VirtualSize; memcpy(pSH, (pSH+1), sizeof(IMAGE_SECTION_HEADER) * (pNTH->FileHeader.NumberOfSections-nSection)); } } if(nSection != 0) { // Zerofill last section header pSH = IMAGE_FIRST_SECTION(pNTH); memset(&pSH[pNTH->FileHeader.NumberOfSections], 0, sizeof(IMAGE_SECTION_HEADER)); } return dwNewFsize; }