summaryrefslogtreecommitdiffstats
path: root/winsup/cygwin/dll_init.cc
diff options
context:
space:
mode:
authorChristopher Faylor <me@cgf.cx>2011-05-28 20:27:56 +0000
committerChristopher Faylor <me@cgf.cx>2011-05-28 20:27:56 +0000
commit17a5c8c36ec36849772073b2cfa027aa347b48f2 (patch)
tree4411bd584e13e429ec240dbc1625a8798d9b88a4 /winsup/cygwin/dll_init.cc
parenta92339ab635febf6ce9873fecca6f3d113dbb3b5 (diff)
downloadcygnal-17a5c8c36ec36849772073b2cfa027aa347b48f2.tar.gz
cygnal-17a5c8c36ec36849772073b2cfa027aa347b48f2.tar.bz2
cygnal-17a5c8c36ec36849772073b2cfa027aa347b48f2.zip
* dll_init.cc (reserve_upto): Remove.
(release_upto): Ditto. (dll_list::reserve_space): New function to reserve space needed by DLL_LOAD dlls early in the fork process. (dll_list::load_after_fork): Rewrite to use recursion for tracking reservations made while trying to make dlls land where they belong. (dll_list::load_after_fork_impl): Ditto. (dll_list::alloc): Initialize image base field. * dll_init.h (struct dll_list): declare new functions. (dll::image_size): New member.
Diffstat (limited to 'winsup/cygwin/dll_init.cc')
-rw-r--r--winsup/cygwin/dll_init.cc26
1 files changed, 20 insertions, 6 deletions
diff --git a/winsup/cygwin/dll_init.cc b/winsup/cygwin/dll_init.cc
index 6fc117c0a..7882ed65f 100644
--- a/winsup/cygwin/dll_init.cc
+++ b/winsup/cygwin/dll_init.cc
@@ -173,6 +173,7 @@ dll_list::alloc (HINSTANCE h, per_process *p, dll_type type)
d->handle = h;
d->has_dtors = true;
d->p = p;
+ d->image_size = ((pefile*)h)->optional_hdr ()->SizeOfImage;
d->ndeps = 0;
d->deps = NULL;
d->modname = wcsrchr (d->name, L'\\');
@@ -416,21 +417,33 @@ release_upto (const PWCHAR name, DWORD here)
}
}
-/* Mark one page at "here" as reserved. This may force
- Windows NT to load a DLL elsewhere. */
+/* Reserve the chunk of free address space starting _here_ and (usually)
+ covering at least _dll_size_ bytes. However, we must take care not
+ to clobber the dll's target address range because it often overlaps.
+ */
static DWORD
-reserve_at (const PWCHAR name, DWORD here)
+reserve_at (const PWCHAR name, DWORD here, DWORD dll_base, DWORD dll_size)
{
DWORD size;
MEMORY_BASIC_INFORMATION mb;
if (!VirtualQuery ((void *) here, &mb, sizeof (mb)))
- size = 64 * 1024;
-
+ api_fatal ("couldn't examine memory at %08lx while mapping %W, %E",
+ here, name);
if (mb.State != MEM_FREE)
return 0;
size = mb.RegionSize;
+
+ // don't clobber the space where we want the dll to land
+ DWORD end = here + size;
+ DWORD dll_end = dll_base + dll_size;
+ if (dll_base < here && dll_end > here)
+ here = dll_end; // the dll straddles our left edge
+ else if (dll_base >= here && dll_base < end)
+ end = dll_base; // the dll overlaps partly or fully to our right
+
+ size = end - here;
if (!VirtualAlloc ((void *) here, size, MEM_RESERVE, PAGE_NOACCESS))
api_fatal ("couldn't allocate memory %p(%d) for '%W' alignment, %E\n",
here, size, name);
@@ -508,7 +521,8 @@ dll_list::load_after_fork (HANDLE parent)
can in the child, due to differences in the load ordering.
Block memory at it's preferred address and try again. */
if ((DWORD) h > (DWORD) d->handle)
- preferred_block = reserve_at (d->name, (DWORD) h);
+ preferred_block = reserve_at (d->name, (DWORD) h,
+ (DWORD) d->handle, d->image_size);
}
}