2000-02-17 20:38:33 +01:00
|
|
|
/* dll_init.cc
|
|
|
|
|
2009-06-06 13:47:29 +02:00
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
|
2011-02-15 16:56:03 +01:00
|
|
|
2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2000-08-22 05:58:47 +02:00
|
|
|
#include "cygerrno.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include "perprocess.h"
|
2011-04-21 02:53:55 +02:00
|
|
|
#include "sync.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include "dll_init.h"
|
|
|
|
#include "environ.h"
|
2001-09-07 23:32:07 +02:00
|
|
|
#include "security.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
#include "fhandler.h"
|
2001-09-07 23:32:07 +02:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2002-06-02 08:07:01 +02:00
|
|
|
#include "pinfo.h"
|
2006-03-09 00:55:14 +01:00
|
|
|
#include "cygtls.h"
|
2010-02-28 16:54:25 +01:00
|
|
|
#include "exception.h"
|
2008-08-13 10:25:44 +02:00
|
|
|
#include <wchar.h>
|
2010-01-29 19:34:09 +01:00
|
|
|
#include <sys/reent.h>
|
2010-05-03 18:40:33 +02:00
|
|
|
#include <assert.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
extern void __stdcall check_sanity_and_sync (per_process *);
|
|
|
|
|
2009-06-08 05:53:40 +02:00
|
|
|
dll_list dlls;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2011-04-21 02:53:55 +02:00
|
|
|
muto dll_list::protect;
|
|
|
|
|
2005-04-14 19:34:03 +02:00
|
|
|
static bool dll_global_dtors_recorded;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Run destructors for all DLLs on exit. */
|
2005-04-14 19:34:03 +02:00
|
|
|
void
|
2002-09-23 02:31:31 +02:00
|
|
|
dll_global_dtors ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2011-05-04 18:02:45 +02:00
|
|
|
/* Don't attempt to call destructors if we're still in fork processing
|
|
|
|
since that likely means fork is failing and everything will not have been
|
|
|
|
set up. */
|
|
|
|
if (in_forkee)
|
|
|
|
return;
|
2005-04-14 19:34:03 +02:00
|
|
|
int recorded = dll_global_dtors_recorded;
|
|
|
|
dll_global_dtors_recorded = false;
|
2008-08-05 16:26:47 +02:00
|
|
|
if (recorded && dlls.start.next)
|
2008-08-04 17:55:32 +02:00
|
|
|
for (dll *d = dlls.end; d != &dlls.start; d = d->prev)
|
2009-08-21 23:32:06 +02:00
|
|
|
d->run_dtors ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Run all constructors associated with a dll */
|
|
|
|
void
|
|
|
|
per_module::run_ctors ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-07-15 04:48:11 +02:00
|
|
|
void (**pfunc)() = ctors;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Run ctors backwards, so skip the first entry and find how many
|
|
|
|
there are, then run them. */
|
|
|
|
|
|
|
|
if (pfunc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 1; pfunc[i]; i++);
|
|
|
|
|
2000-09-04 19:52:42 +02:00
|
|
|
for (int j = i - 1; j > 0; j--)
|
2000-02-17 20:38:33 +01:00
|
|
|
(pfunc[j]) ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Run all destructors associated with a dll */
|
|
|
|
void
|
|
|
|
per_module::run_dtors ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-07-15 04:48:11 +02:00
|
|
|
void (**pfunc)() = dtors;
|
2010-02-05 16:05:22 +01:00
|
|
|
while (*++pfunc)
|
|
|
|
(*pfunc) ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Initialize an individual DLL */
|
|
|
|
int
|
|
|
|
dll::init ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-07-15 04:48:11 +02:00
|
|
|
int ret = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2010-05-07 23:25:19 +02:00
|
|
|
/* This should be a no-op. Why didn't we just import this variable? */
|
2011-03-27 21:14:02 +02:00
|
|
|
if (!p.envptr)
|
|
|
|
p.envptr = &__cygwin_environ;
|
2011-04-06 15:58:00 +02:00
|
|
|
else if (*(p.envptr) != __cygwin_environ)
|
2011-03-27 21:14:02 +02:00
|
|
|
*(p.envptr) = __cygwin_environ;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Don't run constructors or the "main" if we've forked. */
|
|
|
|
if (!in_forkee)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
/* global contructors */
|
2000-07-15 04:48:11 +02:00
|
|
|
p.run_ctors ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* entry point of dll (use main of per_process with null args...) */
|
2000-07-15 04:48:11 +02:00
|
|
|
if (p.main)
|
2010-05-07 23:25:19 +02:00
|
|
|
ret = p.main (0, 0, 0);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Look for a dll based on name */
|
|
|
|
dll *
|
2008-08-13 10:25:44 +02:00
|
|
|
dll_list::operator[] (const PWCHAR name)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-07-15 04:48:11 +02:00
|
|
|
dll *d = &start;
|
|
|
|
while ((d = d->next) != NULL)
|
2008-08-13 10:25:44 +02:00
|
|
|
if (!wcscasecmp (name, d->name))
|
2000-07-15 04:48:11 +02:00
|
|
|
return d;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
return NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-10-21 05:38:41 +02:00
|
|
|
#define RETRIES 1000
|
2000-07-15 04:48:11 +02:00
|
|
|
|
2009-06-08 05:53:40 +02:00
|
|
|
/* Allocate space for a dll struct. */
|
2000-07-15 04:48:11 +02:00
|
|
|
dll *
|
|
|
|
dll_list::alloc (HINSTANCE h, per_process *p, dll_type type)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2008-08-13 10:25:44 +02:00
|
|
|
WCHAR name[NT_MAX_PATH];
|
|
|
|
DWORD namelen = GetModuleFileNameW (h, name, sizeof (name));
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2011-04-21 02:53:55 +02:00
|
|
|
guard (true);
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Already loaded? */
|
|
|
|
dll *d = dlls[name];
|
|
|
|
if (d)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2009-06-08 05:53:40 +02:00
|
|
|
if (!in_forkee)
|
|
|
|
d->count++; /* Yes. Bump the usage count. */
|
2011-05-02 17:28:35 +02:00
|
|
|
d->p = p;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2010-05-03 18:40:33 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* FIXME: Change this to new at some point. */
|
|
|
|
d = (dll *) cmalloc (HEAP_2_DLL, sizeof (*d) + (namelen * sizeof (*name)));
|
|
|
|
|
|
|
|
/* Now we've allocated a block of information. Fill it in with the supplied
|
|
|
|
info about this DLL. */
|
|
|
|
d->count = 1;
|
|
|
|
wcscpy (d->name, name);
|
|
|
|
d->handle = h;
|
|
|
|
d->has_dtors = true;
|
|
|
|
d->p = p;
|
|
|
|
d->type = type;
|
|
|
|
if (end == NULL)
|
|
|
|
end = &start; /* Point to "end" of dll chain. */
|
|
|
|
end->next = d; /* Standard linked list stuff. */
|
|
|
|
d->next = NULL;
|
|
|
|
d->prev = end;
|
|
|
|
end = d;
|
|
|
|
tot++;
|
|
|
|
if (type == DLL_LOAD)
|
|
|
|
loaded_dlls++;
|
|
|
|
}
|
2011-04-21 02:53:55 +02:00
|
|
|
guard (false);
|
2010-05-03 18:40:33 +02:00
|
|
|
assert (p->envptr != NULL);
|
2000-07-15 04:48:11 +02:00
|
|
|
return d;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2010-02-02 03:00:01 +01:00
|
|
|
dll *
|
|
|
|
dll_list::find (void *retaddr)
|
2010-01-29 19:34:09 +01:00
|
|
|
{
|
2010-02-02 03:00:01 +01:00
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
if (!VirtualQuery (retaddr, &m, sizeof m))
|
|
|
|
return NULL;
|
|
|
|
HMODULE h = (HMODULE) m.AllocationBase;
|
|
|
|
|
|
|
|
dll *d = &start;
|
|
|
|
while ((d = d->next))
|
|
|
|
if (d->handle == h)
|
|
|
|
break;
|
|
|
|
return d;
|
2010-01-29 19:34:09 +01:00
|
|
|
}
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Detach a DLL from the chain. */
|
2000-02-17 20:38:33 +01:00
|
|
|
void
|
2002-11-14 05:29:39 +01:00
|
|
|
dll_list::detach (void *retaddr)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2010-02-02 03:00:01 +01:00
|
|
|
dll *d;
|
2011-05-04 21:19:46 +02:00
|
|
|
/* Don't attempt to call destructors if we're still in fork processing
|
|
|
|
since that likely means fork is failing and everything will not have been
|
|
|
|
set up. */
|
|
|
|
if (!myself || in_forkee)
|
2002-11-14 05:29:39 +01:00
|
|
|
return;
|
2011-04-21 02:53:55 +02:00
|
|
|
guard (true);
|
|
|
|
if ((d = find (retaddr)))
|
2010-02-02 03:00:01 +01:00
|
|
|
{
|
2011-04-21 02:53:55 +02:00
|
|
|
if (d->count <= 0)
|
|
|
|
system_printf ("WARNING: trying to detach an already detached dll ...");
|
|
|
|
if (--d->count == 0)
|
|
|
|
{
|
|
|
|
/* Ensure our exception handler is enabled for destructors */
|
|
|
|
exception protect;
|
|
|
|
/* Call finalize function if we are not already exiting */
|
|
|
|
if (!exit_state)
|
|
|
|
__cxa_finalize (d);
|
|
|
|
d->run_dtors ();
|
|
|
|
d->prev->next = d->next;
|
|
|
|
if (d->next)
|
|
|
|
d->next->prev = d->prev;
|
|
|
|
if (d->type == DLL_LOAD)
|
|
|
|
loaded_dlls--;
|
|
|
|
if (end == d)
|
|
|
|
end = d->prev;
|
|
|
|
cfree (d);
|
|
|
|
}
|
2010-02-02 03:00:01 +01:00
|
|
|
}
|
2011-04-21 02:53:55 +02:00
|
|
|
guard (false);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-07-16 06:10:33 +02:00
|
|
|
/* Initialization for all linked DLLs, called by dll_crt0_1. */
|
2000-02-17 20:38:33 +01:00
|
|
|
void
|
2000-07-15 04:48:11 +02:00
|
|
|
dll_list::init ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Walk the dll chain, initializing each dll */
|
|
|
|
dll *d = &start;
|
2008-08-05 16:26:47 +02:00
|
|
|
dll_global_dtors_recorded = d->next != NULL;
|
2000-07-15 04:48:11 +02:00
|
|
|
while ((d = d->next))
|
|
|
|
d->init ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define A64K (64 * 1024)
|
|
|
|
|
|
|
|
/* Mark every memory address up to "here" as reserved. This may force
|
|
|
|
Windows NT to load a DLL in the next available, lowest slot. */
|
2000-07-15 04:48:11 +02:00
|
|
|
static void
|
2008-08-13 10:25:44 +02:00
|
|
|
reserve_upto (const PWCHAR name, DWORD here)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
MEMORY_BASIC_INFORMATION mb;
|
|
|
|
for (DWORD start = 0x10000; start < here; start += size)
|
|
|
|
if (!VirtualQuery ((void *) start, &mb, sizeof (mb)))
|
2002-10-14 22:25:52 +02:00
|
|
|
size = A64K;
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size = A64K * ((mb.RegionSize + A64K - 1) / A64K);
|
|
|
|
start = A64K * (((DWORD) mb.BaseAddress + A64K - 1) / A64K);
|
|
|
|
|
|
|
|
if (start + size > here)
|
|
|
|
size = here - start;
|
|
|
|
if (mb.State == MEM_FREE &&
|
|
|
|
!VirtualAlloc ((void *) start, size, MEM_RESERVE, PAGE_NOACCESS))
|
2008-08-13 10:25:44 +02:00
|
|
|
api_fatal ("couldn't allocate memory %p(%d) for '%W' alignment, %E\n",
|
2000-02-17 20:38:33 +01:00
|
|
|
start, size, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release all of the memory previously allocated by "upto" above.
|
|
|
|
Note that this may also free otherwise reserved memory. If that becomes
|
|
|
|
a problem, we'll have to keep track of the memory that we reserve above. */
|
2000-07-15 04:48:11 +02:00
|
|
|
static void
|
2008-08-13 10:25:44 +02:00
|
|
|
release_upto (const PWCHAR name, DWORD here)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
MEMORY_BASIC_INFORMATION mb;
|
|
|
|
for (DWORD start = 0x10000; start < here; start += size)
|
|
|
|
if (!VirtualQuery ((void *) start, &mb, sizeof (mb)))
|
|
|
|
size = 64 * 1024;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = mb.RegionSize;
|
2009-06-06 13:47:29 +02:00
|
|
|
if (!(mb.State == MEM_RESERVE && mb.AllocationProtect == PAGE_NOACCESS
|
|
|
|
&& (((void *) start < cygheap->user_heap.base
|
|
|
|
|| (void *) start > cygheap->user_heap.top)
|
|
|
|
&& ((void *) start < (void *) cygheap
|
|
|
|
|| (void *) start
|
|
|
|
> (void *) ((char *) cygheap + CYGHEAPSIZE)))))
|
2000-02-17 20:38:33 +01:00
|
|
|
continue;
|
|
|
|
if (!VirtualFree ((void *) start, 0, MEM_RELEASE))
|
2008-08-13 10:25:44 +02:00
|
|
|
api_fatal ("couldn't release memory %p(%d) for '%W' alignment, %E\n",
|
2000-02-17 20:38:33 +01:00
|
|
|
start, size, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 15:58:00 +02:00
|
|
|
/* Mark one page at "here" as reserved. This may force
|
|
|
|
Windows NT to load a DLL elsewhere. */
|
|
|
|
static DWORD
|
|
|
|
reserve_at (const PWCHAR name, DWORD here)
|
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
MEMORY_BASIC_INFORMATION mb;
|
|
|
|
|
|
|
|
if (!VirtualQuery ((void *) here, &mb, sizeof (mb)))
|
|
|
|
size = 64 * 1024;
|
|
|
|
|
|
|
|
if (mb.State != MEM_FREE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size = mb.RegionSize;
|
|
|
|
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);
|
|
|
|
return here;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the memory previously allocated by "reserve_at" above. */
|
|
|
|
static void
|
|
|
|
release_at (const PWCHAR name, DWORD here)
|
|
|
|
{
|
|
|
|
if (!VirtualFree ((void *) here, 0, MEM_RELEASE))
|
|
|
|
api_fatal ("couldn't release memory %p for '%W' alignment, %E\n",
|
|
|
|
here, name);
|
|
|
|
}
|
|
|
|
|
2009-06-06 13:47:29 +02:00
|
|
|
/* Reload DLLs after a fork. Iterates over the list of dynamically loaded
|
2009-06-08 05:53:40 +02:00
|
|
|
DLLs and attempts to load them in the same place as they were loaded in the
|
|
|
|
parent. */
|
2000-02-17 20:38:33 +01:00
|
|
|
void
|
2009-06-08 05:53:40 +02:00
|
|
|
dll_list::load_after_fork (HANDLE parent)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2011-04-06 15:58:00 +02:00
|
|
|
DWORD preferred_block = 0;
|
|
|
|
|
2009-06-08 05:53:40 +02:00
|
|
|
for (dll *d = &dlls.start; (d = d->next) != NULL; )
|
|
|
|
if (d->type == DLL_LOAD)
|
|
|
|
for (int i = 0; i < 2; i++)
|
2000-07-15 04:48:11 +02:00
|
|
|
{
|
|
|
|
/* See if DLL will load in proper place. If so, free it and reload
|
|
|
|
it the right way.
|
2009-06-08 05:53:40 +02:00
|
|
|
It stinks that we can't invert the order of the initial LoadLibrary
|
2011-02-08 21:14:37 +01:00
|
|
|
and FreeLibrary since Microsoft documentation seems to imply that
|
2009-06-08 05:53:40 +02:00
|
|
|
should do what we want. However, once a library is loaded as
|
|
|
|
above, the second LoadLibrary will not execute its startup code
|
|
|
|
unless it is first unloaded. */
|
|
|
|
HMODULE h = LoadLibraryExW (d->name, NULL, DONT_RESOLVE_DLL_REFERENCES);
|
|
|
|
|
|
|
|
if (!h)
|
|
|
|
system_printf ("can't reload %W, %E", d->name);
|
2000-07-15 04:48:11 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
FreeLibrary (h);
|
2009-06-08 05:53:40 +02:00
|
|
|
if (h == d->handle)
|
|
|
|
h = LoadLibraryW (d->name);
|
2000-07-15 04:48:11 +02:00
|
|
|
}
|
2011-04-06 15:58:00 +02:00
|
|
|
|
2009-06-08 05:53:40 +02:00
|
|
|
/* If we reached here on the second iteration of the for loop
|
|
|
|
then there is a lot of memory to release. */
|
|
|
|
if (i > 0)
|
2011-04-06 15:58:00 +02:00
|
|
|
{
|
|
|
|
release_upto (d->name, (DWORD) d->handle);
|
|
|
|
|
|
|
|
if (preferred_block)
|
|
|
|
release_at (d->name, preferred_block);
|
|
|
|
preferred_block = 0;
|
|
|
|
}
|
|
|
|
|
2009-06-08 05:53:40 +02:00
|
|
|
if (h == d->handle)
|
|
|
|
break; /* Success */
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
/* We tried once to relocate the dll and it failed. */
|
|
|
|
api_fatal ("unable to remap %W to same address as parent: %p != %p",
|
|
|
|
d->name, d->handle, h);
|
|
|
|
|
|
|
|
/* Dll loaded in the wrong place. Dunno why this happens but it
|
2011-04-06 15:58:00 +02:00
|
|
|
always seems to happen when there are multiple DLLs with the
|
|
|
|
same base address. In the "forked" process, the relocated DLL
|
|
|
|
may load at a different address. So, block all of the memory up
|
|
|
|
to the relocated load address and try again. */
|
2009-06-08 05:53:40 +02:00
|
|
|
reserve_upto (d->name, (DWORD) d->handle);
|
2011-04-06 15:58:00 +02:00
|
|
|
|
|
|
|
/* Also, if the DLL loaded at a higher address than wanted (probably
|
|
|
|
it's base address), reserve the memory at that address. This can
|
|
|
|
happen if it couldn't load at the preferred base in the parent, but
|
|
|
|
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);
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
}
|
2005-12-29 21:46:34 +01:00
|
|
|
in_forkee = false;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2006-06-02 02:09:50 +02:00
|
|
|
struct dllcrt0_info
|
|
|
|
{
|
|
|
|
HMODULE h;
|
|
|
|
per_process *p;
|
|
|
|
int res;
|
2011-04-06 15:58:00 +02:00
|
|
|
dllcrt0_info (HMODULE h0, per_process *p0): h (h0), p (p0) {}
|
2006-06-02 02:09:50 +02:00
|
|
|
};
|
|
|
|
|
2000-07-12 06:49:07 +02:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
dll_dllcrt0 (HMODULE h, per_process *p)
|
|
|
|
{
|
2006-06-02 02:09:50 +02:00
|
|
|
dllcrt0_info x (h, p);
|
|
|
|
|
|
|
|
if (_my_tls.isinitialized ())
|
|
|
|
dll_dllcrt0_1 (&x);
|
|
|
|
else
|
|
|
|
_my_tls.call ((DWORD (*) (void *, void *)) dll_dllcrt0_1, &x);
|
|
|
|
return x.res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dll_dllcrt0_1 (VOID *x)
|
|
|
|
{
|
2011-04-06 15:58:00 +02:00
|
|
|
HMODULE& h = ((dllcrt0_info *) x)->h;
|
|
|
|
per_process*& p = ((dllcrt0_info *) x)->p;
|
|
|
|
int& res = ((dllcrt0_info *) x)->res;
|
2006-06-02 02:09:50 +02:00
|
|
|
|
2000-07-06 01:46:44 +02:00
|
|
|
if (p == NULL)
|
|
|
|
p = &__cygwin_user_data;
|
|
|
|
else
|
2010-05-07 23:25:19 +02:00
|
|
|
{
|
|
|
|
*(p->impure_ptr_ptr) = __cygwin_user_data.impure_ptr;
|
|
|
|
_pei386_runtime_relocator (p);
|
|
|
|
}
|
2000-07-06 01:46:44 +02:00
|
|
|
|
2006-06-02 02:09:50 +02:00
|
|
|
bool linked = !in_forkee && !cygwin_finished_initializing;
|
2006-05-28 00:55:58 +02:00
|
|
|
|
2009-08-13 09:35:50 +02:00
|
|
|
/* Broken DLLs built against Cygwin versions 1.7.0-49 up to 1.7.0-57
|
|
|
|
override the cxx_malloc pointer in their DLL initialization code,
|
|
|
|
when loaded either statically or dynamically. Because this leaves
|
|
|
|
a stale pointer into demapped memory space if the DLL is unloaded
|
|
|
|
by a call to dlclose, we prevent this happening for dynamically
|
|
|
|
loaded DLLS in dlopen by saving and restoring cxx_malloc around
|
|
|
|
the call to LoadLibrary, which invokes the DLL's startup sequence.
|
|
|
|
Modern DLLs won't even attempt to override the pointer when loaded
|
|
|
|
statically, but will write their overrides directly into the
|
|
|
|
struct it points to. With all modern DLLs, this will remain the
|
|
|
|
default_cygwin_cxx_malloc struct in cxx.cc, but if any broken DLLs
|
|
|
|
are in the mix they will have overridden the pointer and subsequent
|
|
|
|
overrides will go into their embedded cxx_malloc structs. This is
|
|
|
|
almost certainly not a problem as they can never be unloaded, but
|
|
|
|
if we ever did want to do anything about it, we could check here to
|
|
|
|
see if the pointer had been altered in the early parts of the DLL's
|
|
|
|
startup, and if so copy back the new overrides and reset it here.
|
|
|
|
However, that's just a note for the record; at the moment, we can't
|
|
|
|
see any need to worry about this happening. */
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Partially initialize Cygwin guts for non-cygwin apps. */
|
2000-07-06 01:46:44 +02:00
|
|
|
if (dynamically_loaded && user_data->magic_biscuit == 0)
|
|
|
|
dll_crt0 (p);
|
2000-07-17 21:18:21 +02:00
|
|
|
else
|
2000-07-15 04:48:11 +02:00
|
|
|
check_sanity_and_sync (p);
|
|
|
|
|
|
|
|
dll_type type;
|
|
|
|
|
|
|
|
/* If this function is called before cygwin has finished
|
|
|
|
initializing, then the DLL must be a cygwin-aware DLL
|
|
|
|
that was explicitly linked into the program rather than
|
|
|
|
a dlopened DLL. */
|
2006-06-02 02:09:50 +02:00
|
|
|
if (linked)
|
2000-07-15 04:48:11 +02:00
|
|
|
type = DLL_LINK;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
type = DLL_LOAD;
|
|
|
|
dlls.reload_on_fork = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate and initialize space for the DLL. */
|
|
|
|
dll *d = dlls.alloc (h, p, type);
|
|
|
|
|
|
|
|
/* If d == NULL, then something is broken.
|
|
|
|
Otherwise, if we've finished initializing, it's ok to
|
|
|
|
initialize the DLL. If we haven't finished initializing,
|
|
|
|
it may not be safe to call the dll's "main" since not
|
|
|
|
all of cygwin's internal structures may have been set up. */
|
2006-06-02 02:09:50 +02:00
|
|
|
if (!d || (!linked && !d->init ()))
|
|
|
|
res = -1;
|
|
|
|
else
|
|
|
|
res = (DWORD) d;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2010-01-29 18:37:54 +01:00
|
|
|
/* OBSOLETE: This function is obsolete and will go away in the
|
2000-02-17 20:38:33 +01:00
|
|
|
future. Cygwin can now handle being loaded from a noncygwin app
|
|
|
|
using the same entry point. */
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
dll_noncygwin_dllcrt0 (HMODULE h, per_process *p)
|
|
|
|
{
|
|
|
|
return dll_dllcrt0 (h, p);
|
|
|
|
}
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
extern "C" void
|
2002-11-14 05:29:39 +01:00
|
|
|
cygwin_detach_dll (dll *)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2006-03-13 00:57:05 +01:00
|
|
|
HANDLE retaddr;
|
|
|
|
if (_my_tls.isinitialized ())
|
2010-02-02 03:00:01 +01:00
|
|
|
retaddr = (void *) _my_tls.retaddr ();
|
2006-03-13 00:57:05 +01:00
|
|
|
else
|
|
|
|
retaddr = __builtin_return_address (0);
|
|
|
|
dlls.detach (retaddr);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
extern "C" void
|
2000-02-17 20:38:33 +01:00
|
|
|
dlfork (int val)
|
|
|
|
{
|
2000-07-15 04:48:11 +02:00
|
|
|
dlls.reload_on_fork = val;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Called from various places to update all of the individual
|
|
|
|
ideas of the environ block. Explain to me again why we didn't
|
|
|
|
just import __cygwin_environ? */
|
|
|
|
void __stdcall
|
|
|
|
update_envptrs ()
|
|
|
|
{
|
|
|
|
for (dll *d = dlls.istart (DLL_ANY); d; d = dlls.inext ())
|
2011-04-06 15:58:00 +02:00
|
|
|
if (*(d->p.envptr) != __cygwin_environ)
|
|
|
|
*(d->p.envptr) = __cygwin_environ;
|
2000-07-15 04:48:11 +02:00
|
|
|
*main_environ = __cygwin_environ;
|
|
|
|
}
|