2011-06-01 Kai Tietz <kai.tietz@onevision.com> Merged from trunk rev. 4186: * crt/pesect.c, crt/pseudo-reloc.c: Optimize pseudo-relocation code and prevent multiple temporary write-access settings on same section. git-svn-id: svn+ssh://svn.code.sf.net/p/mingw-w64/code/branches/releases/v1.0@4187 4407c894-4637-0410-b4f5-ada5f102cad1
diff --git a/mingw-w64-crt/ChangeLog b/mingw-w64-crt/ChangeLog index 6ef6ceb..29e2602 100644 --- a/mingw-w64-crt/ChangeLog +++ b/mingw-w64-crt/ChangeLog
@@ -1,3 +1,10 @@ +2011-06-01 Kai Tietz <kai.tietz@onevision.com> + + Merged from trunk rev. 4186: + * crt/pesect.c, crt/pseudo-reloc.c: Optimize pseudo-relocation + code and prevent multiple temporary write-access settings on same + section. + 2011-05-05 Ozkan Sezer <sezeroz@gmail.com> * lib32/winscard.def: New, merged from trunk.
diff --git a/mingw-w64-crt/crt/pesect.c b/mingw-w64-crt/crt/pesect.c index 60bbbc4..8b83371 100644 --- a/mingw-w64-crt/crt/pesect.c +++ b/mingw-w64-crt/crt/pesect.c
@@ -89,6 +89,39 @@ return NULL; } +int __mingw_GetSectionCount (void); +PIMAGE_SECTION_HEADER __mingw_GetSectionForAddress (LPVOID p); + +PIMAGE_SECTION_HEADER +__mingw_GetSectionForAddress (LPVOID p) +{ + PBYTE pImageBase; + DWORD_PTR rva; + + pImageBase = (PBYTE) &__ImageBase; + if (! _ValidateImageBase (pImageBase)) + return NULL; + + rva = (DWORD_PTR) (((PBYTE) p) - pImageBase); + return _FindPESection (pImageBase, rva); +} + +int +__mingw_GetSectionCount (void) +{ + PBYTE pImageBase; + PIMAGE_NT_HEADERS pNTHeader; + + pImageBase = (PBYTE) &__ImageBase; + if (! _ValidateImageBase (pImageBase)) + return 0; + + pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew); + + return (int) pNTHeader->FileHeader.NumberOfSections; +} + + PIMAGE_SECTION_HEADER _FindPESectionExec (size_t); PIMAGE_SECTION_HEADER
diff --git a/mingw-w64-crt/crt/pseudo-reloc.c b/mingw-w64-crt/crt/pseudo-reloc.c index 77f0411..308ecda 100644 --- a/mingw-w64-crt/crt/pseudo-reloc.c +++ b/mingw-w64-crt/crt/pseudo-reloc.c
@@ -155,6 +155,87 @@ #endif } +/* For mingw-w64 we have additional helpers to get image information + on runtime. This allows us to cache for pseudo-relocation pass + the temporary access of code/read-only sections. + This step speeds up pseudo-relocation pass. */ +#ifdef __MINGW64_VERSION_MAJOR +extern int __mingw_GetSectionCount (void); +extern PIMAGE_SECTION_HEADER __mingw_GetSectionForAddress (LPVOID p); +extern PBYTE _GetPEImageBase (void); + +typedef struct sSecInfo { + /* Keeps altered section flags, or zero if nothing was changed. */ + DWORD old_protect; + PBYTE sec_start; + PIMAGE_SECTION_HEADER hash; +} sSecInfo; + +static sSecInfo *the_secs = NULL; +static int maxSections = 0; + +static void +mark_section_writable (LPVOID addr) +{ + MEMORY_BASIC_INFORMATION b; + PIMAGE_SECTION_HEADER h; + int i; + + for (i = 0; i < maxSections; i++) + { + if (the_secs[i].sec_start <= ((LPBYTE) addr) + && ((LPBYTE) addr) < (the_secs[i].sec_start + the_secs[i].hash->Misc.VirtualSize)) + return; + } + h = __mingw_GetSectionForAddress (addr); + if (!h) + { + __report_error ("Address %p has no image-section", addr); + return; + } + the_secs[i].hash = h; + the_secs[i].old_protect = 0; + the_secs[i].sec_start = _GetPEImageBase () + h->VirtualAddress; + + if (!VirtualQuery (the_secs[i].sec_start, &b, sizeof(b))) + { + __report_error (" VirtualQuery failed for %d bytes at address %p", + (int) h->Misc.VirtualSize, the_secs[i].sec_start); + return; + } + + if (b.Protect != PAGE_EXECUTE_READWRITE && b.Protect != PAGE_READWRITE) + VirtualProtect (b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, + &the_secs[i].old_protect); + ++maxSections; + return; +} + +static void +restore_modified_sections (void) +{ + int i; + MEMORY_BASIC_INFORMATION b; + DWORD oldprot; + + for (i = 0; i < maxSections; i++) + { + if (the_secs[i].old_protect == 0) + continue; + if (!VirtualQuery (the_secs[i].sec_start, &b, sizeof(b))) + { + __report_error (" VirtualQuery failed for %d bytes at address %p", + (int) the_secs[i].hash->Misc.VirtualSize, + the_secs[i].sec_start); + return; + } + VirtualProtect (b.BaseAddress, b.RegionSize, the_secs[i].old_protect, + &oldprot); + } +} + +#endif /* __MINGW64_VERSION_MAJOR */ + /* This function temporarily marks the page containing addr * writable, before copying len bytes from *src to *addr, and * then restores the original protection settings to the page. @@ -171,12 +252,15 @@ static void __write_memory (void *addr, const void *src, size_t len) { +#ifndef __MINGW64_VERSION_MAJOR MEMORY_BASIC_INFORMATION b; DWORD oldprot; +#endif /* ! __MINGW64_VERSION_MAJOR */ if (!len) return; +#ifndef __MINGW64_VERSION_MAJOR if (!VirtualQuery (addr, &b, sizeof(b))) { __report_error (" VirtualQuery failed for %d bytes at address %p", @@ -186,12 +270,18 @@ /* Temporarily allow write access to read-only protected memory. */ if (b.Protect != PAGE_EXECUTE_READWRITE && b.Protect != PAGE_READWRITE) VirtualProtect (b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, - &oldprot); + &oldprot); +#else /* ! __MINGW64_VERSION_MAJOR */ + mark_section_writable ((LPVOID) addr); +#endif /* __MINGW64_VERSION_MAJOR */ + /* write the data. */ memcpy (addr, src, len); /* Restore original protection. */ +#ifndef __MINGW64_VERSION_MAJOR if (b.Protect != PAGE_EXECUTE_READWRITE && b.Protect != PAGE_READWRITE) VirtualProtect (b.BaseAddress, b.RegionSize, oldprot, &oldprot); +#endif /* !__MINGW64_VERSION_MAJOR */ } #define RP_VERSION_V1 0 @@ -358,10 +448,23 @@ _pei386_runtime_relocator (void) { static NO_COPY int was_init = 0; +#ifdef __MINGW64_VERSION_MAJOR + int mSecs; +#endif /* __MINGW64_VERSION_MAJOR */ + if (was_init) return; ++was_init; +#ifdef __MINGW64_VERSION_MAJOR + mSecs = __mingw_GetSectionCount (); + the_secs = (sSecInfo *) alloca (sizeof (sSecInfo) * (size_t) mSecs); + maxSections = 0; +#endif /* __MINGW64_VERSION_MAJOR */ + do_pseudo_reloc (&__RUNTIME_PSEUDO_RELOC_LIST__, &__RUNTIME_PSEUDO_RELOC_LIST_END__, &__MINGW_LSYMBOL(_image_base__)); +#ifdef __MINGW64_VERSION_MAJOR + restore_modified_sections (); +#endif /* __MINGW64_VERSION_MAJOR */ }