[CMAKE] Add the kernel module type

Take this as an occasion to use target_link_options more
This commit is contained in:
Jérôme Gardou 2021-02-24 11:46:53 +01:00 committed by Jérôme Gardou
parent 812c9e5a11
commit e55123a6a2
5 changed files with 125 additions and 71 deletions

View file

@ -27,7 +27,10 @@ static const char* g_Target;
enum fixup_mode
{
MODE_LOADCONFIG,
MODE_DRIVER
MODE_KERNELDRIVER,
MODE_WDMDRIVER,
MODE_KERNELDLL,
MODE_KERNEL
};
void *rva_to_ptr(unsigned char *buffer, PIMAGE_NT_HEADERS nt_header, DWORD rva)
@ -127,13 +130,27 @@ static int add_loadconfig(unsigned char *buffer, PIMAGE_NT_HEADERS nt_header)
return 1;
}
static int driver_fixup(unsigned char *buffer, PIMAGE_NT_HEADERS nt_header)
static int driver_fixup(int mode, unsigned char *buffer, PIMAGE_NT_HEADERS nt_header)
{
/* GNU LD just doesn't know what a driver is, and has notably no idea of paged vs non-paged sections */
for (unsigned i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
{
PIMAGE_SECTION_HEADER Section = IMAGE_FIRST_SECTION(nt_header) + i;
/* LD puts alignment crap that nobody asked for */
Section->Characteristics &= ~IMAGE_SCN_ALIGN_MASK;
/* LD overdoes it and puts the initialized flag everywhere */
if (Section->Characteristics & IMAGE_SCN_CNT_CODE)
Section->Characteristics &= ~IMAGE_SCN_CNT_INITIALIZED_DATA;
/* For some reason, .rsrc is made writable by windres */
if (strncasecmp((char*)Section->Name, ".rsrc", 5) == 0)
{
Section->Characteristics &= ~IMAGE_SCN_MEM_WRITE;
continue;
}
/* Known sections which can be discarded */
if (strncasecmp((char*)Section->Name, "INIT", 4) == 0)
{
@ -143,6 +160,8 @@ static int driver_fixup(unsigned char *buffer, PIMAGE_NT_HEADERS nt_header)
/* Known sections which can be paged */
if ((strncasecmp((char*)Section->Name, "PAGE", 4) == 0)
|| (strncasecmp((char*)Section->Name, ".rsrc", 5) == 0)
|| (strncasecmp((char*)Section->Name, ".edata", 6) == 0)
|| (strncasecmp((char*)Section->Name, ".reloc", 6) == 0))
{
continue;
@ -164,8 +183,11 @@ print_usage(void)
{
printf("Usage: %s <mode> <filename>\n", g_ApplicationName);
printf("Where <mode> is on of the following:\n");
printf(" --loadconfig Fix the LOAD_CONFIG directory entry\n");
printf(" --driver Fix code and data sections for driver images\n");
printf(" --loadconfig Fix the LOAD_CONFIG directory entry\n");
printf(" --kernelmodedriver Fix code and data sections for driver images\n");
printf(" --wdmdriver Fix code and data sections for WDM drivers\n");
printf(" --kerneldll Fix code and data sections for Kernel-Mode DLLs\n");
printf(" --kernel Fix code and data sections for kernels\n");
}
int main(int argc, char **argv)
@ -189,9 +211,21 @@ int main(int argc, char **argv)
{
mode = MODE_LOADCONFIG;
}
else if (strcmp(argv[1], "--driver") == 0)
else if (strcmp(argv[1], "--kernelmodedriver") == 0)
{
mode = MODE_DRIVER;
mode = MODE_KERNELDRIVER;
}
else if (strcmp(argv[1], "--wdmdriver") == 0)
{
mode = MODE_WDMDRIVER;
}
else if (strcmp(argv[1], "--kerneldll") == 0)
{
mode = MODE_KERNELDLL;
}
else if (strcmp(argv[1], "--kernel") == 0)
{
mode = MODE_KERNEL;
}
else
{
@ -245,7 +279,7 @@ int main(int argc, char **argv)
if (mode == MODE_LOADCONFIG)
result = add_loadconfig(buffer, nt_header);
else
result = driver_fixup(buffer, nt_header);
result = driver_fixup(mode, buffer, nt_header);
if (!result)
{