[PEFIXUP] Fixup of resource sections in GCC builds. (#3594)

Make resource sections discardable for kernel-mode drivers and DLLs.
This commit is contained in:
Dmitry Borisov 2021-04-08 20:50:51 +06:00 committed by Hermès Bélusca-Maïto
parent 0fe7fdbdea
commit 6e33c8ffd3
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
2 changed files with 12 additions and 6 deletions

View file

@ -307,7 +307,7 @@ function(set_module_type_toolchain MODULE TYPE)
# Fixup section characteristics
# - Remove flags that LD overzealously puts (alignment flag, Initialized flags for code sections)
# - INIT section is made discardable
# - .rsrc is made read-only
# - .rsrc is made read-only and discardable
# - PAGE & .edata sections are made pageable.
add_custom_command(TARGET ${MODULE} POST_BUILD
COMMAND native-pefixup --${TYPE} $<TARGET_FILE:${MODULE}>)

View file

@ -142,9 +142,15 @@ static int driver_fixup(enum fixup_mode mode, unsigned char *buffer, PIMAGE_NT_H
if (Section->Characteristics & IMAGE_SCN_CNT_CODE)
Section->Characteristics &= ~IMAGE_SCN_CNT_INITIALIZED_DATA;
/* For some reason, .rsrc is made writable by windres */
if (strncmp((char*)Section->Name, ".rsrc", 5) == 0)
{
/* .rsrc is discardable for driver images, WDM drivers and Kernel-Mode DLLs */
if (mode == MODE_KERNELDRIVER || mode == MODE_WDMDRIVER || mode == MODE_KERNELDLL)
{
Section->Characteristics |= IMAGE_SCN_MEM_DISCARDABLE;
}
/* For some reason, .rsrc is made writable by windres */
Section->Characteristics &= ~IMAGE_SCN_MEM_WRITE;
continue;
}
@ -356,10 +362,10 @@ print_usage(void)
printf("Usage: %s <options> <filename>\n\n", g_ApplicationName);
printf("<options> can be one of the following options:\n"
" --loadconfig Fix the LOAD_CONFIG directory entry;\n"
" --kernelmodedriver Fix code and data sections for driver images;\n"
" --wdmdriver Fix code and data sections for WDM drivers;\n"
" --kerneldll Fix code and data sections for Kernel-Mode DLLs;\n"
" --kernel Fix code and data sections for kernels;\n"
" --kernelmodedriver Fix code, data and resource sections for driver images;\n"
" --wdmdriver Fix code, data and resource sections for WDM drivers;\n"
" --kerneldll Fix code, data and resource sections for Kernel-Mode DLLs;\n"
" --kernel Fix code, data and resource sections for kernels;\n"
"\n"
"and/or a combination of the following ones:\n"
" --section:name[=newname][,[[!]{CDEIKOMPRSUW}][A{1248PTSX}]]\n"