void asmbpe(void) { IMAGE_SECTION_HEADER *t, *d; switch(thechar) { default: diag("unknown PE architecture"); errorexit(); case '6': fh.Machine = IMAGE_FILE_MACHINE_AMD64; break; case '8': fh.Machine = IMAGE_FILE_MACHINE_I386; break; } t = addpesection(".text", segtext.len, segtext.len); t->Characteristics = IMAGE_SCN_CNT_CODE| IMAGE_SCN_CNT_INITIALIZED_DATA| IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ; chksectseg(t, &segtext); textsect = nsect; d = addpesection(".data", segdata.len, segdata.filelen); d->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA| IMAGE_SCN_MEM_READ|IMAGE_SCN_MEM_WRITE; chksectseg(d, &segdata); if(!debug['s']) dwarfaddpeheaders(); cseek(nextfileoff); addimports(d); addexports(); addsymtable(); addpersrc(); fh.NumberOfSections = nsect; fh.TimeDateStamp = time(0); fh.Characteristics = IMAGE_FILE_RELOCS_STRIPPED| IMAGE_FILE_EXECUTABLE_IMAGE|IMAGE_FILE_DEBUG_STRIPPED; if (pe64) { fh.SizeOfOptionalHeader = sizeof(oh64); fh.Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE; set(Magic, 0x20b); // PE32+ } else { fh.SizeOfOptionalHeader = sizeof(oh); fh.Characteristics |= IMAGE_FILE_32BIT_MACHINE; set(Magic, 0x10b); // PE32 oh.BaseOfData = d->VirtualAddress; } set(MajorLinkerVersion, 3); set(MinorLinkerVersion, 0); set(SizeOfCode, t->SizeOfRawData); set(SizeOfInitializedData, d->SizeOfRawData); set(SizeOfUninitializedData, 0); set(AddressOfEntryPoint, entryvalue()-PEBASE); set(BaseOfCode, t->VirtualAddress); set(ImageBase, PEBASE); set(SectionAlignment, PESECTALIGN); set(FileAlignment, PEFILEALIGN); set(MajorOperatingSystemVersion, 4); set(MinorOperatingSystemVersion, 0); set(MajorImageVersion, 1); set(MinorImageVersion, 0); set(MajorSubsystemVersion, 4); set(MinorSubsystemVersion, 0); set(SizeOfImage, nextsectoff); set(SizeOfHeaders, PEFILEHEADR); if(strcmp(headstring, "windowsgui") == 0) set(Subsystem, IMAGE_SUBSYSTEM_WINDOWS_GUI); else set(Subsystem, IMAGE_SUBSYSTEM_WINDOWS_CUI); // Disable stack growth as we don't want Windows to // fiddle with the thread stack limits, which we set // ourselves to circumvent the stack checks in the // Windows exception dispatcher. // Commit size must be strictly less than reserve // size otherwise reserve will be rounded up to a // larger size, as verified with VMMap. // Go code would be OK with 64k stacks, but we need larger stacks for cgo. // That default stack reserve size affects only the main thread, // for other threads we specify stack size in runtime explicitly // (runtime knows whether cgo is enabled or not). // If you change stack reserve sizes here, // change STACKSIZE in runtime/cgo/gcc_windows_{386,amd64}.c as well. if(!iscgo) { set(SizeOfStackReserve, 0x00010000); set(SizeOfStackCommit, 0x0000ffff); } else { set(SizeOfStackReserve, pe64 ? 0x00200000 : 0x00100000); // account for 2 guard pages set(SizeOfStackCommit, (pe64 ? 0x00200000 : 0x00100000) - 0x2000); } set(SizeOfHeapReserve, 0x00100000); set(SizeOfHeapCommit, 0x00001000); set(NumberOfRvaAndSizes, 16); pewrite(); }
void asmbpe(void) { IMAGE_SECTION_HEADER *t, *d; switch(thechar) { default: diag("unknown PE architecture"); errorexit(); case '6': fh.Machine = IMAGE_FILE_MACHINE_AMD64; break; case '8': fh.Machine = IMAGE_FILE_MACHINE_I386; break; } t = addpesection(".text", segtext.len, segtext.len, &segtext); t->Characteristics = IMAGE_SCN_CNT_CODE| IMAGE_SCN_CNT_INITIALIZED_DATA| IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ; d = addpesection(".data", segdata.len, segdata.filelen, &segdata); d->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA| IMAGE_SCN_MEM_READ|IMAGE_SCN_MEM_WRITE; if(!debug['s']) dwarfaddpeheaders(); addimports(nextfileoff, d); addexports(nextfileoff); addsymtable(); addpersrc(); fh.NumberOfSections = nsect; fh.TimeDateStamp = time(0); fh.Characteristics = IMAGE_FILE_RELOCS_STRIPPED| IMAGE_FILE_EXECUTABLE_IMAGE|IMAGE_FILE_DEBUG_STRIPPED; if (pe64) { fh.SizeOfOptionalHeader = sizeof(oh64); fh.Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE; set(Magic, 0x20b); // PE32+ } else { fh.SizeOfOptionalHeader = sizeof(oh); fh.Characteristics |= IMAGE_FILE_32BIT_MACHINE; set(Magic, 0x10b); // PE32 oh.BaseOfData = d->VirtualAddress; } set(MajorLinkerVersion, 1); set(MinorLinkerVersion, 0); set(SizeOfCode, t->SizeOfRawData); set(SizeOfInitializedData, d->SizeOfRawData); set(SizeOfUninitializedData, 0); set(AddressOfEntryPoint, entryvalue()-PEBASE); set(BaseOfCode, t->VirtualAddress); set(ImageBase, PEBASE); set(SectionAlignment, PESECTALIGN); set(FileAlignment, PEFILEALIGN); set(MajorOperatingSystemVersion, 4); set(MinorOperatingSystemVersion, 0); set(MajorImageVersion, 1); set(MinorImageVersion, 0); set(MajorSubsystemVersion, 4); set(MinorSubsystemVersion, 0); set(SizeOfImage, nextsectoff); set(SizeOfHeaders, PEFILEHEADR); if(strcmp(headstring, "windowsgui") == 0) set(Subsystem, IMAGE_SUBSYSTEM_WINDOWS_GUI); else set(Subsystem, IMAGE_SUBSYSTEM_WINDOWS_CUI); set(SizeOfStackReserve, 0x0040000); set(SizeOfStackCommit, 0x00001000); set(SizeOfHeapReserve, 0x00100000); set(SizeOfHeapCommit, 0x00001000); set(NumberOfRvaAndSizes, 16); pewrite(); }