]>
Commit | Line | Data |
---|---|---|
1fd5e000 CF |
1 | /* dll_init.h |
2 | ||
bc837d22 CF |
3 | Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010, |
4 | 2011, 2012 Red Hat, Inc. | |
1fd5e000 CF |
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 | ||
2eb392bd | 12 | struct per_module |
1fd5e000 | 13 | { |
2eb392bd CF |
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 (); | |
1fd5e000 CF |
36 | }; |
37 | ||
1fd5e000 | 38 | |
2eb392bd | 39 | typedef enum |
1fd5e000 | 40 | { |
2eb392bd CF |
41 | DLL_NONE, |
42 | DLL_LINK, | |
43 | DLL_LOAD, | |
44 | DLL_ANY | |
45 | } dll_type; | |
1fd5e000 | 46 | |
2eb392bd | 47 | struct dll |
1fd5e000 | 48 | { |
2eb392bd CF |
49 | struct dll *next, *prev; |
50 | per_module p; | |
51 | HMODULE handle; | |
52 | int count; | |
6282fe16 | 53 | bool has_dtors; |
2eb392bd | 54 | dll_type type; |
977ad543 CF |
55 | long ndeps; |
56 | dll** deps; | |
6cd2e185 | 57 | DWORD image_size; |
6642f7da | 58 | void* preferred_base; |
9eba4de2 | 59 | PWCHAR modname; |
71f53a2f | 60 | WCHAR name[1]; |
2eb392bd CF |
61 | void detach (); |
62 | int init (); | |
6282fe16 CF |
63 | void run_dtors () |
64 | { | |
65 | if (has_dtors) | |
66 | { | |
67 | has_dtors = 0; | |
68 | p.run_dtors (); | |
69 | } | |
70 | } | |
1fd5e000 CF |
71 | }; |
72 | ||
2eb392bd | 73 | #define MAX_DLL_BEFORE_INIT 100 |
1fd5e000 | 74 | |
2eb392bd | 75 | class dll_list |
1fd5e000 | 76 | { |
2eb392bd CF |
77 | dll *end; |
78 | dll *hold; | |
79 | dll_type hold_type; | |
71c17c54 | 80 | static muto protect; |
1fd5e000 | 81 | public: |
2eb392bd | 82 | dll start; |
2eb392bd CF |
83 | int loaded_dlls; |
84 | int reload_on_fork; | |
69d704be | 85 | dll *operator [] (const PWCHAR name); |
2eb392bd | 86 | dll *alloc (HINSTANCE, per_process *, dll_type); |
fc6a0dc8 | 87 | dll *find (void *); |
052990e6 | 88 | void detach (void *); |
2eb392bd | 89 | void init (); |
71f53a2f | 90 | void load_after_fork (HANDLE); |
6642f7da CF |
91 | void reserve_space (); |
92 | void load_after_fork_impl (HANDLE, dll* which, int retries); | |
50124fc0 | 93 | dll *find_by_modname (const PWCHAR name); |
977ad543 CF |
94 | void populate_deps (dll* d); |
95 | void topsort (); | |
96 | void topsort_visit (dll* d, bool goto_tail); | |
97 | void append (dll* d); | |
b86f999a | 98 | |
2eb392bd CF |
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 | } | |
71c17c54 | 106 | |
243a041b CF |
107 | dll *istart (dll_type t) |
108 | { | |
109 | hold_type = t; | |
110 | hold = &start; | |
111 | return inext (); | |
112 | } | |
71c17c54 CF |
113 | void guard(bool lockit) |
114 | { | |
115 | if (lockit) | |
116 | protect.acquire (); | |
117 | else | |
118 | protect.release (); | |
119 | } | |
bee18f45 | 120 | friend void dll_global_dtors (); |
71c17c54 | 121 | dll_list () { protect.init ("dll_list"); } |
1fd5e000 CF |
122 | }; |
123 | ||
6cd2e185 CF |
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 | ||
2eb392bd | 142 | extern dll_list dlls; |
dda06573 | 143 | void dll_global_dtors (); |
fc6a0dc8 CF |
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*); |