2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the w64 mingw-runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
13 #if defined (_WIN64) && defined (__ia64__)
14 #error FIXME: Unsupported __ImageBase implementation.
17 /* Hack, for bug in ld. Will be removed soon. */
18 #define __ImageBase __MINGW_LSYMBOL(_image_base__)
20 /* This symbol is defined by the linker. */
21 extern IMAGE_DOS_HEADER __ImageBase
;
24 WINBOOL
_ValidateImageBase (PBYTE
);
27 _ValidateImageBase (PBYTE pImageBase
)
29 PIMAGE_DOS_HEADER pDOSHeader
;
30 PIMAGE_NT_HEADERS pNTHeader
;
31 PIMAGE_OPTIONAL_HEADER pOptHeader
;
33 pDOSHeader
= (PIMAGE_DOS_HEADER
) pImageBase
;
34 if (pDOSHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
36 pNTHeader
= (PIMAGE_NT_HEADERS
) ((PBYTE
) pDOSHeader
+ pDOSHeader
->e_lfanew
);
37 if (pNTHeader
->Signature
!= IMAGE_NT_SIGNATURE
)
39 pOptHeader
= (PIMAGE_OPTIONAL_HEADER
) &pNTHeader
->OptionalHeader
;
40 if (pOptHeader
->Magic
!= IMAGE_NT_OPTIONAL_HDR_MAGIC
)
45 PIMAGE_SECTION_HEADER
_FindPESection (PBYTE
, DWORD_PTR
);
48 _FindPESection (PBYTE pImageBase
, DWORD_PTR rva
)
50 PIMAGE_NT_HEADERS pNTHeader
;
51 PIMAGE_SECTION_HEADER pSection
;
52 unsigned int iSection
;
54 pNTHeader
= (PIMAGE_NT_HEADERS
) (pImageBase
+ ((PIMAGE_DOS_HEADER
) pImageBase
)->e_lfanew
);
56 for (iSection
= 0, pSection
= IMAGE_FIRST_SECTION (pNTHeader
);
57 iSection
< pNTHeader
->FileHeader
.NumberOfSections
;
58 ++iSection
,++pSection
)
60 if (rva
>= pSection
->VirtualAddress
61 && rva
< pSection
->VirtualAddress
+ pSection
->Misc
.VirtualSize
)
67 PIMAGE_SECTION_HEADER
_FindPESectionByName (const char *);
70 _FindPESectionByName (const char *pName
)
73 PIMAGE_NT_HEADERS pNTHeader
;
74 PIMAGE_SECTION_HEADER pSection
;
75 unsigned int iSection
;
77 /* Long names aren't supported. */
78 if (strlen (pName
) > IMAGE_SIZEOF_SHORT_NAME
)
81 pImageBase
= (PBYTE
) &__ImageBase
;
82 if (! _ValidateImageBase (pImageBase
))
85 pNTHeader
= (PIMAGE_NT_HEADERS
) (pImageBase
+ ((PIMAGE_DOS_HEADER
) pImageBase
)->e_lfanew
);
87 for (iSection
= 0, pSection
= IMAGE_FIRST_SECTION (pNTHeader
);
88 iSection
< pNTHeader
->FileHeader
.NumberOfSections
;
89 ++iSection
,++pSection
)
91 if (!strncmp ((char *) &pSection
->Name
[0], pName
, IMAGE_SIZEOF_SHORT_NAME
))
97 int __mingw_GetSectionCount (void);
98 PIMAGE_SECTION_HEADER
__mingw_GetSectionForAddress (LPVOID p
);
100 PIMAGE_SECTION_HEADER
101 __mingw_GetSectionForAddress (LPVOID p
)
106 pImageBase
= (PBYTE
) &__ImageBase
;
107 if (! _ValidateImageBase (pImageBase
))
110 rva
= (DWORD_PTR
) (((PBYTE
) p
) - pImageBase
);
111 return _FindPESection (pImageBase
, rva
);
115 __mingw_GetSectionCount (void)
118 PIMAGE_NT_HEADERS pNTHeader
;
120 pImageBase
= (PBYTE
) &__ImageBase
;
121 if (! _ValidateImageBase (pImageBase
))
124 pNTHeader
= (PIMAGE_NT_HEADERS
) (pImageBase
+ ((PIMAGE_DOS_HEADER
) pImageBase
)->e_lfanew
);
126 return (int) pNTHeader
->FileHeader
.NumberOfSections
;
130 PIMAGE_SECTION_HEADER
_FindPESectionExec (size_t);
132 PIMAGE_SECTION_HEADER
133 _FindPESectionExec (size_t eNo
)
136 PIMAGE_NT_HEADERS pNTHeader
;
137 PIMAGE_SECTION_HEADER pSection
;
138 unsigned int iSection
;
140 pImageBase
= (PBYTE
) &__ImageBase
;
141 if (! _ValidateImageBase (pImageBase
))
144 pNTHeader
= (PIMAGE_NT_HEADERS
) (pImageBase
+ ((PIMAGE_DOS_HEADER
) pImageBase
)->e_lfanew
);
146 for (iSection
= 0, pSection
= IMAGE_FIRST_SECTION (pNTHeader
);
147 iSection
< pNTHeader
->FileHeader
.NumberOfSections
;
148 ++iSection
,++pSection
)
150 if ((pSection
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) != 0)
160 PBYTE
_GetPEImageBase (void);
163 _GetPEImageBase (void)
166 pImageBase
= (PBYTE
) &__ImageBase
;
167 if (! _ValidateImageBase (pImageBase
))
172 WINBOOL
_IsNonwritableInCurrentImage (PBYTE
);
175 _IsNonwritableInCurrentImage (PBYTE pTarget
)
179 PIMAGE_SECTION_HEADER pSection
;
181 pImageBase
= (PBYTE
) &__ImageBase
;
182 if (! _ValidateImageBase (pImageBase
))
184 rvaTarget
= pTarget
- pImageBase
;
185 pSection
= _FindPESection (pImageBase
, rvaTarget
);
186 if (pSection
== NULL
)
188 return (pSection
->Characteristics
& IMAGE_SCN_MEM_WRITE
) == 0;
192 __mingw_enum_import_library_names (int);
195 __mingw_enum_import_library_names (int i
)
198 PIMAGE_NT_HEADERS pNTHeader
;
199 PIMAGE_IMPORT_DESCRIPTOR importDesc
;
200 PIMAGE_SECTION_HEADER pSection
;
201 DWORD importsStartRVA
;
203 pImageBase
= (PBYTE
) &__ImageBase
;
204 if (! _ValidateImageBase (pImageBase
))
207 pNTHeader
= (PIMAGE_NT_HEADERS
) (pImageBase
+ ((PIMAGE_DOS_HEADER
) pImageBase
)->e_lfanew
);
209 importsStartRVA
= pNTHeader
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].VirtualAddress
;
210 if (!importsStartRVA
)
213 pSection
= _FindPESection (pImageBase
, importsStartRVA
);
217 importDesc
= (PIMAGE_IMPORT_DESCRIPTOR
) (pImageBase
+ importsStartRVA
);
223 if (importDesc
->TimeDateStamp
== 0 && importDesc
->Name
== 0)
227 return (char *) (pImageBase
+ importDesc
->Name
);
235 HMODULE
__mingw_get_msvcrt_handle(void);
236 HMODULE
__mingw_get_msvcrt_handle(void)
238 static HANDLE msvcrt_handle
;
241 const char *lib_name
;
244 while ((lib_name
= __mingw_enum_import_library_names (i
++))) {
245 if((lib_name
[0] == 'm' || lib_name
[0] == 'M')
246 && (lib_name
[1] == 's' || lib_name
[1] == 'S')
247 && (lib_name
[2] == 'v' || lib_name
[2] == 'V')
248 && (lib_name
[3] == 'c' || lib_name
[3] == 'C')
249 && (lib_name
[4] == 'r' || lib_name
[4] == 'R')
250 && (lib_name
[5] == 't' || lib_name
[5] == 'T' || ('0' <= lib_name
[5] && lib_name
[5] <= '9')))
255 msvcrt_handle
= GetModuleHandleA(lib_name
);
257 msvcrt_handle
= LoadLibraryW(L
"msvcrt.dll");
260 return msvcrt_handle
;