ReactOS 0.4.15-dev-8191-gbc6c731
pesect.c File Reference
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <string.h>
Include dependency graph for pesect.c:

Go to the source code of this file.

Functions

WINBOOL _ValidateImageBase (PBYTE)
 
PIMAGE_SECTION_HEADER _FindPESection (PBYTE, DWORD_PTR)
 
PIMAGE_SECTION_HEADER _FindPESectionByName (const char *)
 
int __mingw_GetSectionCount (void)
 
PIMAGE_SECTION_HEADER __mingw_GetSectionForAddress (LPVOID p)
 
PIMAGE_SECTION_HEADER _FindPESectionExec (size_t)
 
PBYTE _GetPEImageBase (void)
 
WINBOOL _IsNonwritableInCurrentImage (PBYTE)
 
const char__mingw_enum_import_library_names (int)
 
HMODULE __mingw_get_msvcrt_handle (void)
 

Variables

IMAGE_DOS_HEADER __ImageBase
 

Function Documentation

◆ __mingw_enum_import_library_names()

const char * __mingw_enum_import_library_names ( int  i)

Definition at line 195 of file pesect.c.

196{
197 PBYTE pImageBase;
198 PIMAGE_NT_HEADERS pNTHeader;
199 PIMAGE_IMPORT_DESCRIPTOR importDesc;
200 PIMAGE_SECTION_HEADER pSection;
201 DWORD importsStartRVA;
202
203 pImageBase = (PBYTE) &__ImageBase;
204 if (! _ValidateImageBase (pImageBase))
205 return NULL;
206
207 pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew);
208
210 if (!importsStartRVA)
211 return NULL;
212
213 pSection = _FindPESection (pImageBase, importsStartRVA);
214 if (!pSection)
215 return NULL;
216
217 importDesc = (PIMAGE_IMPORT_DESCRIPTOR) (pImageBase + importsStartRVA);
218 if (!importDesc)
219 return NULL;
220
221 for (;;)
222 {
223 if (importDesc->TimeDateStamp == 0 && importDesc->Name == 0)
224 break;
225
226 if (i <= 0)
227 return (char *) (pImageBase + importDesc->Name);
228 --i;
229 importDesc++;
230 }
231
232 return NULL;
233}
#define NULL
Definition: types.h:112
unsigned long DWORD
Definition: ntddk_ex.h:95
PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS
Definition: ntddk_ex.h:187
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
DWORD e_lfanew
Definition: crypt.c:1156
struct _IMAGE_IMPORT_DESCRIPTOR * PIMAGE_IMPORT_DESCRIPTOR
#define IMAGE_DIRECTORY_ENTRY_IMPORT
Definition: pedump.c:260
BYTE * PBYTE
Definition: pedump.c:66
WINBOOL _ValidateImageBase(PBYTE)
Definition: pesect.c:27
PIMAGE_SECTION_HEADER _FindPESection(PBYTE, DWORD_PTR)
Definition: pesect.c:48
IMAGE_DOS_HEADER __ImageBase
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntddk_ex.h:178

Referenced by __mingw_get_msvcrt_handle().

◆ __mingw_get_msvcrt_handle()

HMODULE __mingw_get_msvcrt_handle ( void  )

Definition at line 236 of file pesect.c.

237{
238 static HANDLE msvcrt_handle;
239
240 if(!msvcrt_handle) {
241 const char *lib_name;
242 int i = 0;
243
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')))
251 break;
252 }
253
254 if(lib_name)
255 msvcrt_handle = GetModuleHandleA(lib_name);
256 if(!msvcrt_handle)
257 msvcrt_handle = LoadLibraryW(L"msvcrt.dll");
258 }
259
260 return msvcrt_handle;
261}
#define LoadLibraryW(x)
Definition: compat.h:747
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
#define L(x)
Definition: ntvdm.h:50
const char * __mingw_enum_import_library_names(int)
Definition: pesect.c:195

◆ __mingw_GetSectionCount()

int __mingw_GetSectionCount ( void  )

Definition at line 115 of file pesect.c.

116{
117 PBYTE pImageBase;
118 PIMAGE_NT_HEADERS pNTHeader;
119
120 pImageBase = (PBYTE) &__ImageBase;
121 if (! _ValidateImageBase (pImageBase))
122 return 0;
123
124 pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew);
125
126 return (int) pNTHeader->FileHeader.NumberOfSections;
127}
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183

Referenced by _pei386_runtime_relocator().

◆ __mingw_GetSectionForAddress()

PIMAGE_SECTION_HEADER __mingw_GetSectionForAddress ( LPVOID  p)

Definition at line 101 of file pesect.c.

102{
103 PBYTE pImageBase;
104 DWORD_PTR rva;
105
106 pImageBase = (PBYTE) &__ImageBase;
107 if (! _ValidateImageBase (pImageBase))
108 return NULL;
109
110 rva = (DWORD_PTR) (((PBYTE) p) - pImageBase);
111 return _FindPESection (pImageBase, rva);
112}
GLfloat GLfloat p
Definition: glext.h:8902
#define DWORD_PTR
Definition: treelist.c:76
uint32_t DWORD_PTR
Definition: typedefs.h:65

◆ _FindPESection()

PIMAGE_SECTION_HEADER _FindPESection ( PBYTE  pImageBase,
DWORD_PTR  rva 
)

Definition at line 48 of file pesect.c.

49{
50 PIMAGE_NT_HEADERS pNTHeader;
51 PIMAGE_SECTION_HEADER pSection;
52 unsigned int iSection;
53
54 pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew);
55
56 for (iSection = 0, pSection = IMAGE_FIRST_SECTION (pNTHeader);
57 iSection < pNTHeader->FileHeader.NumberOfSections;
58 ++iSection,++pSection)
59 {
60 if (rva >= pSection->VirtualAddress
61 && rva < pSection->VirtualAddress + pSection->Misc.VirtualSize)
62 return pSection;
63 }
64 return NULL;
65}
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:427
union _IMAGE_SECTION_HEADER::@1563 Misc
_Must_inspect_result_ _In_ WDFDMATRANSACTION _In_ PFN_WDF_PROGRAM_DMA _In_ WDF_DMA_DIRECTION _In_ PMDL _In_ PVOID VirtualAddress

Referenced by __mingw_enum_import_library_names(), __mingw_GetSectionForAddress(), and _IsNonwritableInCurrentImage().

◆ _FindPESectionByName()

PIMAGE_SECTION_HEADER _FindPESectionByName ( const char pName)

Definition at line 70 of file pesect.c.

71{
72 PBYTE pImageBase;
73 PIMAGE_NT_HEADERS pNTHeader;
74 PIMAGE_SECTION_HEADER pSection;
75 unsigned int iSection;
76
77 /* Long names aren't supported. */
79 return NULL;
80
81 pImageBase = (PBYTE) &__ImageBase;
82 if (! _ValidateImageBase (pImageBase))
83 return NULL;
84
85 pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew);
86
87 for (iSection = 0, pSection = IMAGE_FIRST_SECTION (pNTHeader);
88 iSection < pNTHeader->FileHeader.NumberOfSections;
89 ++iSection,++pSection)
90 {
91 if (!strncmp ((char *) &pSection->Name[0], pName, IMAGE_SIZEOF_SHORT_NAME))
92 return pSection;
93 }
94 return NULL;
95}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
static LPSTR pName
Definition: security.c:75
#define IMAGE_SIZEOF_SHORT_NAME
Definition: pedump.c:277
BYTE Name[IMAGE_SIZEOF_SHORT_NAME]
Definition: pedump.c:281

◆ _FindPESectionExec()

PIMAGE_SECTION_HEADER _FindPESectionExec ( size_t  eNo)

Definition at line 133 of file pesect.c.

134{
135 PBYTE pImageBase;
136 PIMAGE_NT_HEADERS pNTHeader;
137 PIMAGE_SECTION_HEADER pSection;
138 unsigned int iSection;
139
140 pImageBase = (PBYTE) &__ImageBase;
141 if (! _ValidateImageBase (pImageBase))
142 return NULL;
143
144 pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew);
145
146 for (iSection = 0, pSection = IMAGE_FIRST_SECTION (pNTHeader);
147 iSection < pNTHeader->FileHeader.NumberOfSections;
148 ++iSection,++pSection)
149 {
150 if ((pSection->Characteristics & IMAGE_SCN_MEM_EXECUTE) != 0)
151 {
152 if (!eNo)
153 return pSection;
154 --eNo;
155 }
156 }
157 return NULL;
158}
@ eNo
Definition: precomp.h:73
#define IMAGE_SCN_MEM_EXECUTE
Definition: ntimage.h:239

◆ _GetPEImageBase()

PBYTE _GetPEImageBase ( void  )

Definition at line 163 of file pesect.c.

164{
165 PBYTE pImageBase;
166 pImageBase = (PBYTE) &__ImageBase;
167 if (! _ValidateImageBase (pImageBase))
168 return NULL;
169 return pImageBase;
170}

◆ _IsNonwritableInCurrentImage()

WINBOOL _IsNonwritableInCurrentImage ( PBYTE  pTarget)

Definition at line 175 of file pesect.c.

176{
177 PBYTE pImageBase;
178 DWORD_PTR rvaTarget;
179 PIMAGE_SECTION_HEADER pSection;
180
181 pImageBase = (PBYTE) &__ImageBase;
182 if (! _ValidateImageBase (pImageBase))
183 return FALSE;
184 rvaTarget = pTarget - pImageBase;
185 pSection = _FindPESection (pImageBase, rvaTarget);
186 if (pSection == NULL)
187 return FALSE;
188 return (pSection->Characteristics & IMAGE_SCN_MEM_WRITE) == 0;
189}
#define FALSE
Definition: types.h:117
FxIoTarget * pTarget
Definition: fxdeviceapi.cpp:97
#define IMAGE_SCN_MEM_WRITE
Definition: ntimage.h:241

◆ _ValidateImageBase()

WINBOOL _ValidateImageBase ( PBYTE  pImageBase)

Definition at line 27 of file pesect.c.

28{
29 PIMAGE_DOS_HEADER pDOSHeader;
30 PIMAGE_NT_HEADERS pNTHeader;
31 PIMAGE_OPTIONAL_HEADER pOptHeader;
32
33 pDOSHeader = (PIMAGE_DOS_HEADER) pImageBase;
34 if (pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE)
35 return FALSE;
36 pNTHeader = (PIMAGE_NT_HEADERS) ((PBYTE) pDOSHeader + pDOSHeader->e_lfanew);
37 if (pNTHeader->Signature != IMAGE_NT_SIGNATURE)
38 return FALSE;
39 pOptHeader = (PIMAGE_OPTIONAL_HEADER) &pNTHeader->OptionalHeader;
40 if (pOptHeader->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC)
41 return FALSE;
42 return TRUE;
43}
#define TRUE
Definition: types.h:120
struct _IMAGE_DOS_HEADER * PIMAGE_DOS_HEADER
if(dx< 0)
Definition: linetemp.h:194
#define IMAGE_NT_OPTIONAL_HDR_MAGIC
Definition: ntimage.h:387
#define IMAGE_NT_SIGNATURE
Definition: pedump.c:93
struct _IMAGE_OPTIONAL_HEADER * PIMAGE_OPTIONAL_HEADER
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89

Referenced by __mingw_enum_import_library_names(), __mingw_GetSectionCount(), __mingw_GetSectionForAddress(), _FindPESectionByName(), _FindPESectionExec(), _GetPEImageBase(), and _IsNonwritableInCurrentImage().

Variable Documentation

◆ __ImageBase

IMAGE_DOS_HEADER __ImageBase
extern

This file has no copyright assigned and is placed in the Public Domain. This file is part of the w64 mingw-runtime package. No warranty is given; refer to the file DISCLAIMER.PD within this package.

Referenced by __mingw_enum_import_library_names(), __mingw_GetSectionCount(), __mingw_GetSectionForAddress(), _FindPESectionByName(), _FindPESectionExec(), _GetPEImageBase(), and _IsNonwritableInCurrentImage().