2000-02-18 03:38:33 +08:00
|
|
|
/* dll_init.h
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
struct per_module
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2017-11-27 21:36:06 +08:00
|
|
|
#ifdef __i386__
|
2000-07-15 10:48:11 +08:00
|
|
|
char ***envptr;
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif
|
2000-07-15 10:48:11 +08:00
|
|
|
void (**ctors)(void);
|
|
|
|
void (**dtors)(void);
|
|
|
|
void *data_start;
|
|
|
|
void *data_end;
|
|
|
|
void *bss_start;
|
|
|
|
void *bss_end;
|
|
|
|
int (*main)(int, char **, char **);
|
|
|
|
per_module &operator = (per_process *p)
|
|
|
|
{
|
2017-11-27 21:36:06 +08:00
|
|
|
#ifdef __i386__
|
2000-07-15 10:48:11 +08:00
|
|
|
envptr = p->envptr;
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif
|
2000-07-15 10:48:11 +08:00
|
|
|
ctors = p->ctors;
|
|
|
|
dtors = p->dtors;
|
|
|
|
data_start = p->data_start;
|
|
|
|
data_end = p->data_end;
|
|
|
|
bss_start = p->bss_start;
|
|
|
|
bss_end = p->bss_end;
|
|
|
|
main = p->main;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
void run_ctors ();
|
|
|
|
void run_dtors ();
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
typedef enum
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-15 10:48:11 +08:00
|
|
|
DLL_NONE,
|
|
|
|
DLL_LINK,
|
|
|
|
DLL_LOAD,
|
|
|
|
DLL_ANY
|
|
|
|
} dll_type;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
struct dll
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-15 10:48:11 +08:00
|
|
|
struct dll *next, *prev;
|
|
|
|
per_module p;
|
|
|
|
HMODULE handle;
|
|
|
|
int count;
|
2009-08-22 05:32:06 +08:00
|
|
|
bool has_dtors;
|
2000-07-15 10:48:11 +08:00
|
|
|
dll_type type;
|
2011-05-30 14:52:12 +08:00
|
|
|
long ndeps;
|
|
|
|
dll** deps;
|
2011-05-29 04:55:34 +08:00
|
|
|
DWORD image_size;
|
2011-05-31 00:09:29 +08:00
|
|
|
void* preferred_base;
|
2012-02-09 22:41:21 +08:00
|
|
|
PWCHAR modname;
|
2009-06-08 11:53:40 +08:00
|
|
|
WCHAR name[1];
|
2000-07-15 10:48:11 +08:00
|
|
|
void detach ();
|
|
|
|
int init ();
|
2009-08-22 05:32:06 +08:00
|
|
|
void run_dtors ()
|
|
|
|
{
|
|
|
|
if (has_dtors)
|
|
|
|
{
|
|
|
|
has_dtors = 0;
|
|
|
|
p.run_dtors ();
|
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
#define MAX_DLL_BEFORE_INIT 100
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
class dll_list
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-15 10:48:11 +08:00
|
|
|
dll *end;
|
|
|
|
dll *hold;
|
|
|
|
dll_type hold_type;
|
2011-04-21 08:53:55 +08:00
|
|
|
static muto protect;
|
2000-02-18 03:38:33 +08:00
|
|
|
public:
|
2000-07-15 10:48:11 +08:00
|
|
|
dll start;
|
|
|
|
int loaded_dlls;
|
|
|
|
int reload_on_fork;
|
2008-08-13 16:25:44 +08:00
|
|
|
dll *operator [] (const PWCHAR name);
|
2000-07-15 10:48:11 +08:00
|
|
|
dll *alloc (HINSTANCE, per_process *, dll_type);
|
2010-02-02 10:00:01 +08:00
|
|
|
dll *find (void *);
|
2002-11-14 12:29:39 +08:00
|
|
|
void detach (void *);
|
2000-07-15 10:48:11 +08:00
|
|
|
void init ();
|
2009-06-08 11:53:40 +08:00
|
|
|
void load_after_fork (HANDLE);
|
2011-05-31 00:09:29 +08:00
|
|
|
void reserve_space ();
|
|
|
|
void load_after_fork_impl (HANDLE, dll* which, int retries);
|
2012-03-04 21:50:12 +08:00
|
|
|
dll *find_by_modname (const PWCHAR name);
|
2011-05-30 14:52:12 +08:00
|
|
|
void populate_deps (dll* d);
|
|
|
|
void topsort ();
|
|
|
|
void topsort_visit (dll* d, bool goto_tail);
|
|
|
|
void append (dll* d);
|
2011-06-06 13:02:13 +08:00
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
dll *inext ()
|
|
|
|
{
|
|
|
|
while ((hold = hold->next))
|
|
|
|
if (hold_type == DLL_ANY || hold->type == hold_type)
|
|
|
|
break;
|
|
|
|
return hold;
|
|
|
|
}
|
2011-04-21 08:53:55 +08:00
|
|
|
|
2001-11-03 11:32:27 +08:00
|
|
|
dll *istart (dll_type t)
|
|
|
|
{
|
|
|
|
hold_type = t;
|
|
|
|
hold = &start;
|
|
|
|
return inext ();
|
|
|
|
}
|
2011-04-21 08:53:55 +08:00
|
|
|
void guard(bool lockit)
|
|
|
|
{
|
|
|
|
if (lockit)
|
|
|
|
protect.acquire ();
|
|
|
|
else
|
|
|
|
protect.release ();
|
|
|
|
}
|
2008-08-04 23:55:32 +08:00
|
|
|
friend void dll_global_dtors ();
|
2011-04-21 08:53:55 +08:00
|
|
|
dll_list () { protect.init ("dll_list"); }
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2011-05-29 04:55:34 +08:00
|
|
|
/* References:
|
|
|
|
http://msdn.microsoft.com/en-us/windows/hardware/gg463125
|
|
|
|
http://msdn.microsoft.com/en-us/library/ms809762.aspx
|
|
|
|
*/
|
|
|
|
struct pefile
|
|
|
|
{
|
|
|
|
IMAGE_DOS_HEADER dos_hdr;
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
char* rva (ptrdiff_t offset) { return (char*) this + offset; }
|
|
|
|
PIMAGE_NT_HEADERS pe_hdr () { return (PIMAGE_NT_HEADERS) rva (dos_hdr.e_lfanew); }
|
|
|
|
PIMAGE_OPTIONAL_HEADER optional_hdr () { return &pe_hdr ()->OptionalHeader; }
|
2011-05-29 04:55:34 +08:00
|
|
|
PIMAGE_DATA_DIRECTORY idata_dir (DWORD which)
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
PIMAGE_OPTIONAL_HEADER oh = optional_hdr ();
|
2011-05-29 04:55:34 +08:00
|
|
|
return (which < oh->NumberOfRvaAndSizes)? oh->DataDirectory + which : 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
extern dll_list dlls;
|
2005-04-15 01:34:03 +08:00
|
|
|
void dll_global_dtors ();
|
2010-02-02 10:00:01 +08:00
|
|
|
|
|
|
|
/* These probably belong in a newlib header but we can keep them here
|
|
|
|
for now. */
|
|
|
|
extern "C" int __cxa_atexit(void (*)(void*), void*, void*);
|
|
|
|
extern "C" int __cxa_finalize(void*);
|