Cygwin: strptime: add release note
[newlib-cygwin.git] / winsup / cygwin / local_includes / dll_init.h
blob65f4213dbb7a4bf668bbc75afd64bc28e2c3c5a9
1 /* dll_init.h
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
7 details. */
9 struct per_module
11 void (**ctors)(void);
12 void (**dtors)(void);
13 void *data_start;
14 void *data_end;
15 void *bss_start;
16 void *bss_end;
17 int (*main)(int, char **, char **);
18 per_module &operator = (per_process *p)
20 ctors = p->ctors;
21 dtors = p->dtors;
22 data_start = p->data_start;
23 data_end = p->data_end;
24 bss_start = p->bss_start;
25 bss_end = p->bss_end;
26 main = p->main;
27 return *this;
29 void run_ctors ();
30 void run_dtors ();
34 typedef enum
36 DLL_NONE,
37 DLL_SELF, /* main-program.exe, cygwin1.dll */
38 DLL_LINK,
39 DLL_LOAD,
40 DLL_ANY
41 } dll_type;
43 struct dll
45 struct dll *next, *prev;
46 per_module p;
47 HMODULE handle;
48 int count;
49 bool has_dtors;
50 dll_type type;
51 long ndeps;
52 dll** deps;
53 DWORD image_size;
54 void* preferred_base;
55 PWCHAR modname;
56 FILE_INTERNAL_INFORMATION fii;
57 PWCHAR forkable_ntname;
58 WCHAR ntname[1]; /* must be the last data member */
60 void detach ();
61 int init ();
62 bool stat_real_file_once ();
63 void nominate_forkable (PCWCHAR);
64 bool create_forkable ();
65 void run_dtors ()
67 if (has_dtors)
69 has_dtors = 0;
70 p.run_dtors ();
73 PWCHAR forkedntname ()
75 return forkable_ntname && *forkable_ntname ? forkable_ntname : ntname;
79 #define MAX_DLL_BEFORE_INIT 100
81 class dll_list
83 bool forkables_supported ()
85 return cygwin_shared->forkable_hardlink_support >= 0;
87 DWORD forkables_dirx_size;
88 bool forkables_created;
89 PWCHAR forkables_dirx_ntname;
90 PWCHAR forkables_mutex_name;
91 HANDLE forkables_mutex;
92 void track_self ();
93 dll *find_by_forkedntname (PCWCHAR ntname);
94 size_t forkable_ntnamesize (dll_type, PCWCHAR fullntname, PCWCHAR modname);
95 void prepare_forkables_nomination ();
96 void update_forkables_needs ();
97 bool update_forkables ();
98 bool create_forkables ();
99 void denominate_forkables ();
100 bool close_mutex ();
101 void try_remove_forkables (PWCHAR dirbuf, size_t dirlen, size_t dirbufsize);
102 void set_forkables_inheritance (bool);
103 void request_forkables ();
105 dll *end;
106 dll *hold;
107 dll_type hold_type;
108 static muto protect;
109 /* Use this buffer under loader lock conditions only. */
110 static WCHAR NO_COPY nt_max_path_buffer[NT_MAX_PATH];
111 public:
112 static HANDLE ntopenfile (PCWCHAR ntname, NTSTATUS *pstatus = NULL,
113 ULONG openopts = 0, ACCESS_MASK access = 0,
114 HANDLE rootDir = NULL);
115 static bool read_fii (HANDLE fh, PFILE_INTERNAL_INFORMATION pfii);
116 static PWCHAR form_ntname (PWCHAR ntbuf, size_t bufsize, PCWCHAR name);
117 static PWCHAR form_shortname (PWCHAR shortbuf, size_t bufsize, PCWCHAR name);
118 static PWCHAR nt_max_path_buf ()
120 return nt_max_path_buffer;
122 static PCWCHAR buffered_shortname (PCWCHAR name)
124 form_shortname (nt_max_path_buffer, NT_MAX_PATH, name);
125 return nt_max_path_buffer;
128 dll *main_executable;
129 dll start;
130 int loaded_dlls;
131 int reload_on_fork;
132 dll *operator [] (PCWCHAR ntname);
133 dll *alloc (HINSTANCE, per_process *, dll_type);
134 dll *find (void *);
135 void detach (void *);
136 void init ();
137 void load_after_fork (HANDLE);
138 void reserve_space ();
139 void load_after_fork_impl (HANDLE, dll* which, int retries);
140 dll *find_by_modname (PCWCHAR modname);
141 void populate_deps (dll* d);
142 void topsort ();
143 void topsort_visit (dll* d, bool goto_tail);
144 void append (dll* d);
146 void release_forkables ();
147 void cleanup_forkables ();
148 bool setup_forkables (bool with_forkables)
150 if (!forkables_supported ())
151 return true; /* no need to retry fork */
152 if (forkables_created)
153 /* Once created, use forkables in current
154 process chain on first fork try already. */
155 with_forkables = true;
156 if (with_forkables)
157 request_forkables ();
158 return with_forkables;
161 dll *inext ()
163 while ((hold = hold->next))
164 if (hold_type == DLL_ANY || hold->type == hold_type)
165 break;
166 return hold;
169 dll *istart (dll_type t)
171 hold_type = t;
172 hold = &start;
173 return inext ();
175 void guard(bool lockit)
177 if (lockit)
178 protect.acquire ();
179 else
180 protect.release ();
182 friend void dll_global_dtors ();
183 dll_list () { protect.init ("dll_list"); }
186 /* References:
187 http://msdn.microsoft.com/en-us/windows/hardware/gg463125
188 http://msdn.microsoft.com/en-us/library/ms809762.aspx
190 struct pefile
192 IMAGE_DOS_HEADER dos_hdr;
194 char* rva (ptrdiff_t offset) { return (char*) this + offset; }
195 PIMAGE_NT_HEADERS pe_hdr () { return (PIMAGE_NT_HEADERS) rva (dos_hdr.e_lfanew); }
196 PIMAGE_OPTIONAL_HEADER optional_hdr () { return &pe_hdr ()->OptionalHeader; }
197 PIMAGE_DATA_DIRECTORY idata_dir (DWORD which)
199 PIMAGE_OPTIONAL_HEADER oh = optional_hdr ();
200 return (which < oh->NumberOfRvaAndSizes)? oh->DataDirectory + which : 0;
204 extern dll_list dlls;
205 void dll_global_dtors ();
207 /* These probably belong in a newlib header but we can keep them here
208 for now. */
209 extern "C" int __cxa_atexit(void (*)(void*), void*, void*);
210 extern "C" int __cxa_finalize(void*);