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.
10 #if defined (_WIN64) && defined (__ia64__)
11 #error FIXME: Unsupported __ImageBase implementation.
13 /* Hack, for bug in ld. Will be removed soon. */
15 #define __ImageBase __MINGW_LSYMBOL(_image_base__)
17 /* This symbol is defined by the linker. */
18 extern IMAGE_DOS_HEADER __ImageBase
;
21 WINBOOL
_ValidateImageBase (PBYTE
);
24 _ValidateImageBase (PBYTE pImageBase
)
26 PIMAGE_DOS_HEADER pDOSHeader
;
27 PIMAGE_NT_HEADERS pNTHeader
;
28 PIMAGE_OPTIONAL_HEADER pOptHeader
;
30 pDOSHeader
= (PIMAGE_DOS_HEADER
) pImageBase
;
31 if (pDOSHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
33 pNTHeader
= (PIMAGE_NT_HEADERS
) ((PBYTE
) pDOSHeader
+ pDOSHeader
->e_lfanew
);
34 if (pNTHeader
->Signature
!= IMAGE_NT_SIGNATURE
)
36 pOptHeader
= (PIMAGE_OPTIONAL_HEADER
) &pNTHeader
->OptionalHeader
;
37 if (pOptHeader
->Magic
!= IMAGE_NT_OPTIONAL_HDR_MAGIC
)
42 PIMAGE_SECTION_HEADER
_FindPESection (PBYTE
, DWORD_PTR
);
45 _FindPESection (PBYTE pImageBase
, DWORD_PTR rva
)
47 PIMAGE_NT_HEADERS pNTHeader
;
48 PIMAGE_SECTION_HEADER pSection
;
49 unsigned int iSection
;
51 pNTHeader
= (PIMAGE_NT_HEADERS
) (pImageBase
+ ((PIMAGE_DOS_HEADER
) pImageBase
)->e_lfanew
);
53 for (iSection
= 0, pSection
= IMAGE_FIRST_SECTION (pNTHeader
);
54 iSection
< pNTHeader
->FileHeader
.NumberOfSections
;
55 ++iSection
,++pSection
)
57 if (rva
>= pSection
->VirtualAddress
58 && rva
< pSection
->VirtualAddress
+ pSection
->Misc
.VirtualSize
)
64 PIMAGE_SECTION_HEADER
_FindPESectionByName (const char *);
67 _FindPESectionByName (const char *pName
)
70 PIMAGE_NT_HEADERS pNTHeader
;
71 PIMAGE_SECTION_HEADER pSection
;
72 unsigned int iSection
;
74 /* Long names aren't supported. */
75 if (strlen (pName
) > IMAGE_SIZEOF_SHORT_NAME
)
78 pImageBase
= (PBYTE
) &__ImageBase
;
79 if (! _ValidateImageBase (pImageBase
))
82 pNTHeader
= (PIMAGE_NT_HEADERS
) (pImageBase
+ ((PIMAGE_DOS_HEADER
) pImageBase
)->e_lfanew
);
84 for (iSection
= 0, pSection
= IMAGE_FIRST_SECTION (pNTHeader
);
85 iSection
< pNTHeader
->FileHeader
.NumberOfSections
;
86 ++iSection
,++pSection
)
88 if (!strncmp ((char *) &pSection
->Name
[0], pName
, IMAGE_SIZEOF_SHORT_NAME
))
94 int __mingw_GetSectionCount (void);
95 PIMAGE_SECTION_HEADER
__mingw_GetSectionForAddress (LPVOID p
);
98 __mingw_GetSectionForAddress (LPVOID p
)
103 pImageBase
= (PBYTE
) &__ImageBase
;
104 if (! _ValidateImageBase (pImageBase
))
107 rva
= (DWORD_PTR
) (((PBYTE
) p
) - pImageBase
);
108 return _FindPESection (pImageBase
, rva
);
112 __mingw_GetSectionCount (void)
115 PIMAGE_NT_HEADERS pNTHeader
;
117 pImageBase
= (PBYTE
) &__ImageBase
;
118 if (! _ValidateImageBase (pImageBase
))
121 pNTHeader
= (PIMAGE_NT_HEADERS
) (pImageBase
+ ((PIMAGE_DOS_HEADER
) pImageBase
)->e_lfanew
);
123 return (int) pNTHeader
->FileHeader
.NumberOfSections
;
127 PIMAGE_SECTION_HEADER
_FindPESectionExec (size_t);
129 PIMAGE_SECTION_HEADER
130 _FindPESectionExec (size_t eNo
)
133 PIMAGE_NT_HEADERS pNTHeader
;
134 PIMAGE_SECTION_HEADER pSection
;
135 unsigned int iSection
;
137 pImageBase
= (PBYTE
) &__ImageBase
;
138 if (! _ValidateImageBase (pImageBase
))
141 pNTHeader
= (PIMAGE_NT_HEADERS
) (pImageBase
+ ((PIMAGE_DOS_HEADER
) pImageBase
)->e_lfanew
);
143 for (iSection
= 0, pSection
= IMAGE_FIRST_SECTION (pNTHeader
);
144 iSection
< pNTHeader
->FileHeader
.NumberOfSections
;
145 ++iSection
,++pSection
)
147 if ((pSection
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) != 0)
157 PBYTE
_GetPEImageBase (void);
160 _GetPEImageBase (void)
163 pImageBase
= (PBYTE
) &__ImageBase
;
164 if (! _ValidateImageBase (pImageBase
))
169 WINBOOL
_IsNonwritableInCurrentImage (PBYTE
);
172 _IsNonwritableInCurrentImage (PBYTE pTarget
)
176 PIMAGE_SECTION_HEADER pSection
;
178 pImageBase
= (PBYTE
) &__ImageBase
;
179 if (! _ValidateImageBase (pImageBase
))
181 rvaTarget
= pTarget
- pImageBase
;
182 pSection
= _FindPESection (pImageBase
, rvaTarget
);
183 if (pSection
== NULL
)
185 return (pSection
->Characteristics
& IMAGE_SCN_MEM_WRITE
) == 0;