OSDN Git Service

Rename cygWFMO to cygwait throughout and use the magic of polymorphism to "wait
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / dll_init.h
1 /* dll_init.h
2
3    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2008,
4    2009 Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 struct per_module
13 {
14   char ***envptr;
15   void (**ctors)(void);
16   void (**dtors)(void);
17   void *data_start;
18   void *data_end;
19   void *bss_start;
20   void *bss_end;
21   int (*main)(int, char **, char **);
22   per_module &operator = (per_process *p)
23   {
24     envptr = p->envptr;
25     ctors = p->ctors;
26     dtors = p->dtors;
27     data_start = p->data_start;
28     data_end = p->data_end;
29     bss_start = p->bss_start;
30     bss_end = p->bss_end;
31     main = p->main;
32     return *this;
33   }
34   void run_ctors ();
35   void run_dtors ();
36 };
37
38
39 typedef enum
40 {
41   DLL_NONE,
42   DLL_LINK,
43   DLL_LOAD,
44   DLL_ANY
45 } dll_type;
46
47 struct dll
48 {
49   struct dll *next, *prev;
50   per_module p;
51   HMODULE handle;
52   int count;
53   bool has_dtors;
54   dll_type type;
55   long ndeps;
56   dll** deps;
57   PWCHAR modname;
58   DWORD image_size;
59   void* preferred_base;
60   WCHAR name[1];
61   void detach ();
62   int init ();
63   void run_dtors ()
64   {
65     if (has_dtors)
66       {
67         has_dtors = 0;
68         p.run_dtors ();
69       }
70   }
71 };
72
73 #define MAX_DLL_BEFORE_INIT     100
74
75 class dll_list
76 {
77   dll *end;
78   dll *hold;
79   dll_type hold_type;
80   static muto protect;
81 public:
82   dll start;
83   int loaded_dlls;
84   int reload_on_fork;
85   dll *operator [] (const PWCHAR name);
86   dll *alloc (HINSTANCE, per_process *, dll_type);
87   dll *find (void *);
88   void detach (void *);
89   void init ();
90   void load_after_fork (HANDLE);
91   void reserve_space ();
92   void load_after_fork_impl (HANDLE, dll* which, int retries);
93   dll *find_by_modname (const PWCHAR name);
94   void populate_deps (dll* d);
95   void topsort ();
96   void topsort_visit (dll* d, bool goto_tail);
97   void append (dll* d);
98
99   dll *inext ()
100   {
101     while ((hold = hold->next))
102       if (hold_type == DLL_ANY || hold->type == hold_type)
103         break;
104     return hold;
105   }
106
107   dll *istart (dll_type t)
108   {
109     hold_type = t;
110     hold = &start;
111     return inext ();
112   }
113   void guard(bool lockit)
114   {
115     if (lockit)
116       protect.acquire ();
117     else
118       protect.release ();
119   }
120   friend void dll_global_dtors ();
121   dll_list () { protect.init ("dll_list"); }
122 };
123
124 /* References:
125    http://msdn.microsoft.com/en-us/windows/hardware/gg463125
126    http://msdn.microsoft.com/en-us/library/ms809762.aspx
127 */
128 struct pefile
129 {
130   IMAGE_DOS_HEADER dos_hdr;
131
132   char* rva (long offset) { return (char*) this + offset; }
133   PIMAGE_NT_HEADERS32 pe_hdr () { return (PIMAGE_NT_HEADERS32) rva (dos_hdr.e_lfanew); }
134   PIMAGE_OPTIONAL_HEADER32 optional_hdr () { return &pe_hdr ()->OptionalHeader; }
135   PIMAGE_DATA_DIRECTORY idata_dir (DWORD which)
136   {
137     PIMAGE_OPTIONAL_HEADER32 oh = optional_hdr ();
138     return (which < oh->NumberOfRvaAndSizes)? oh->DataDirectory + which : 0;
139   }
140 };
141
142 extern dll_list dlls;
143 void dll_global_dtors ();
144
145 /* These probably belong in a newlib header but we can keep them here
146    for now.  */
147 extern "C" int __cxa_atexit(void (*)(void*), void*, void*);
148 extern "C" int __cxa_finalize(void*);