Rev 415 | Rev 418 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 415 | Rev 416 | ||
---|---|---|---|
Line 1462... | Line 1462... | ||
1462 | dwVal = (dwVal + dwAlign - 1) & dwMask; |
1462 | dwVal = (dwVal + dwAlign - 1) & dwMask; |
1463 | } |
1463 | } |
1464 | 1464 | ||
1465 | return(dwVal); |
1465 | return(dwVal); |
1466 | 1466 | ||
- | 1467 | } |
|
- | 1468 | ||
- | 1469 | ULONGLONG peRoundUpToAlignment64(ULONGLONG dwAlign, ULONGLONG dwVal) |
|
- | 1470 | { |
|
- | 1471 | // Fix by Fix by Daniel Marschall: Added this function, based on |
|
- | 1472 | // https://stackoverflow.com/questions/39022853/how-is-sizeofimage-in-the-pe-optional-header-computed |
|
1467 | } //peRoundUpToAlignment() |
1473 | if (dwAlign) |
- | 1474 | { |
|
- | 1475 | //do the rounding with bitwise operations... |
|
- | 1476 | ||
- | 1477 | //create bit mask of bits to keep |
|
- | 1478 | // e.g. if section alignment is 0x1000 1000000000000 |
|
- | 1479 | // we want the following bitmask 11111111111111111111000000000000 |
|
- | 1480 | ULONGLONG dwMask = ~(dwAlign - 1); |
|
- | 1481 | ||
- | 1482 | //round up by adding full alignment (dwAlign-1 since if already aligned we don't want anything to change), |
|
- | 1483 | // then mask off any lower bits |
|
- | 1484 | dwVal = (dwVal + dwAlign - 1) & dwMask; |
|
- | 1485 | } |
|
- | 1486 | ||
- | 1487 | return(dwVal); |
|
- | 1488 | ||
- | 1489 | } |
|
1468 | 1490 | ||
1469 | static BOOL write_raw_resources(QUEUEDUPDATES* updates) |
1491 | static BOOL write_raw_resources(QUEUEDUPDATES* updates) |
1470 | { |
1492 | { |
1471 | CHAR tempdir[MAX_PATH], tempfile[MAX_PATH]; |
1493 | CHAR tempdir[MAX_PATH], tempfile[MAX_PATH]; |
1472 | DWORD i, section_size; |
1494 | DWORD i, section_size; |
Line 1648... | Line 1670... | ||
1648 | 1670 | ||
1649 | /* adjust the PE header information */ |
1671 | /* adjust the PE header information */ |
1650 | sec->SizeOfRawData = section_size; |
1672 | sec->SizeOfRawData = section_size; |
1651 | sec->Misc.VirtualSize = virtual_section_size; |
1673 | sec->Misc.VirtualSize = virtual_section_size; |
1652 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
1674 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
1653 | DWORD pEndOfLastSection, pEndOfLastSectionMem, uCalcSizeOfFile; |
1675 | ULONGLONG pEndOfLastSection, pEndOfLastSectionMem, uCalcSizeOfFile; |
1654 | 1676 | ||
1655 | nt64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = sec->VirtualAddress; |
1677 | nt64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = sec->VirtualAddress; |
1656 | nt64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = res_size.total_size; |
1678 | nt64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = res_size.total_size; |
1657 | nt64->OptionalHeader.SizeOfInitializedData = get_init_data_size(write_map->base, mapping_size); |
1679 | nt64->OptionalHeader.SizeOfInitializedData = get_init_data_size(write_map->base, mapping_size); |
1658 | 1680 | ||
Line 1665... | Line 1687... | ||
1665 | // With the original implementation, Windows won't load some images! |
1687 | // With the original implementation, Windows won't load some images! |
1666 | // https://stackoverflow.com/questions/39022853/how-is-sizeofimage-in-the-pe-optional-header-computed |
1688 | // https://stackoverflow.com/questions/39022853/how-is-sizeofimage-in-the-pe-optional-header-computed |
1667 | lastsec = get_last_section(write_map->base, mapping_size); |
1689 | lastsec = get_last_section(write_map->base, mapping_size); |
1668 | pEndOfLastSection = lastsec->VirtualAddress + lastsec->Misc.VirtualSize + nt64->OptionalHeader.ImageBase; |
1690 | pEndOfLastSection = lastsec->VirtualAddress + lastsec->Misc.VirtualSize + nt64->OptionalHeader.ImageBase; |
1669 | //NOTE: we are rounding to memory section alignment, not file |
1691 | //NOTE: we are rounding to memory section alignment, not file |
1670 | pEndOfLastSectionMem = peRoundUpToAlignment(nt64->OptionalHeader.SectionAlignment, pEndOfLastSection); |
1692 | pEndOfLastSectionMem = peRoundUpToAlignment64(nt64->OptionalHeader.SectionAlignment, pEndOfLastSection); |
1671 | uCalcSizeOfFile = pEndOfLastSectionMem - nt64->OptionalHeader.ImageBase; |
1693 | uCalcSizeOfFile = pEndOfLastSectionMem - nt64->OptionalHeader.ImageBase; |
1672 | nt64->OptionalHeader.SizeOfImage = uCalcSizeOfFile; |
1694 | nt64->OptionalHeader.SizeOfImage = (DWORD)uCalcSizeOfFile; |
1673 | } |
1695 | } |
1674 | else { |
1696 | else { |
1675 | DWORD pEndOfLastSection, pEndOfLastSectionMem, uCalcSizeOfFile; |
1697 | DWORD pEndOfLastSection, pEndOfLastSectionMem, uCalcSizeOfFile; |
1676 | 1698 | ||
1677 | nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = sec->VirtualAddress; |
1699 | nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = sec->VirtualAddress; |