mirror of
https://github.com/reactos/reactos.git
synced 2025-06-23 07:31:02 +00:00
- Split LdrpProcessImportDirectory into LdrpProcessImportDirectory and
LdrpProcessImportDirectoryEntry. Sometimes it is necessary to process a single entry from import directory and sometimes it is necessary to process all entries with the same name. svn path=/trunk/; revision=8684
This commit is contained in:
parent
05825a6aab
commit
c417fd21b1
1 changed files with 121 additions and 105 deletions
|
@ -1,4 +1,4 @@
|
||||||
/* $Id: utils.c,v 1.83 2004/03/06 20:32:06 navaraf Exp $
|
/* $Id: utils.c,v 1.84 2004/03/13 18:14:04 hbirr Exp $
|
||||||
*
|
*
|
||||||
* COPYRIGHT: See COPYING in the top level directory
|
* COPYRIGHT: See COPYING in the top level directory
|
||||||
* PROJECT: ReactOS kernel
|
* PROJECT: ReactOS kernel
|
||||||
|
@ -1416,11 +1416,10 @@ LdrpGetOrLoadModule(PWCHAR SerachPath,
|
||||||
}
|
}
|
||||||
|
|
||||||
static NTSTATUS
|
static NTSTATUS
|
||||||
LdrpProcessImportDirectory(
|
LdrpProcessImportDirectoryEntry(
|
||||||
PLDR_MODULE Module,
|
PLDR_MODULE Module,
|
||||||
PLDR_MODULE ImportedModule,
|
PLDR_MODULE ImportedModule,
|
||||||
PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory,
|
PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory)
|
||||||
PCHAR ImportedName)
|
|
||||||
{
|
{
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
PVOID* ImportAddressList;
|
PVOID* ImportAddressList;
|
||||||
|
@ -1430,37 +1429,9 @@ LdrpProcessImportDirectory(
|
||||||
ULONG Ordinal;
|
ULONG Ordinal;
|
||||||
ULONG IATSize;
|
ULONG IATSize;
|
||||||
|
|
||||||
DPRINT("LdrpProcessImportDirectory(%x '%wZ', '%s')\n",
|
if (ImportModuleDirectory == NULL || ImportModuleDirectory->dwRVAModuleName == 0)
|
||||||
Module, &Module->BaseDllName, ImportedName);
|
|
||||||
|
|
||||||
if (ImportModuleDirectory == NULL)
|
|
||||||
{
|
{
|
||||||
PCHAR Name;
|
return STATUS_UNSUCCESSFUL;
|
||||||
|
|
||||||
ImportModuleDirectory = (PIMAGE_IMPORT_MODULE_DIRECTORY)
|
|
||||||
RtlImageDirectoryEntryToData(Module->BaseAddress,
|
|
||||||
TRUE,
|
|
||||||
IMAGE_DIRECTORY_ENTRY_IMPORT,
|
|
||||||
NULL);
|
|
||||||
if (ImportModuleDirectory == NULL)
|
|
||||||
{
|
|
||||||
return STATUS_UNSUCCESSFUL;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (ImportModuleDirectory->dwRVAModuleName)
|
|
||||||
{
|
|
||||||
Name = (PCHAR)Module->BaseAddress + ImportModuleDirectory->dwRVAModuleName;
|
|
||||||
if (0 == _stricmp(Name, ImportedName))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ImportModuleDirectory++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ImportModuleDirectory->dwRVAModuleName == 0)
|
|
||||||
{
|
|
||||||
return STATUS_UNSUCCESSFUL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get the import address list. */
|
/* Get the import address list. */
|
||||||
|
@ -1483,41 +1454,41 @@ LdrpProcessImportDirectory(
|
||||||
IATSize++;
|
IATSize++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unprotect the region we are about to write into. */
|
/* Unprotect the region we are about to write into. */
|
||||||
IATBase = (PVOID)ImportAddressList;
|
IATBase = (PVOID)ImportAddressList;
|
||||||
Status = NtProtectVirtualMemory(NtCurrentProcess(),
|
Status = NtProtectVirtualMemory(NtCurrentProcess(),
|
||||||
IATBase,
|
IATBase,
|
||||||
IATSize * sizeof(PVOID*),
|
IATSize * sizeof(PVOID*),
|
||||||
PAGE_READWRITE,
|
PAGE_READWRITE,
|
||||||
&OldProtect);
|
&OldProtect);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("Failed to unprotect IAT.\n");
|
DPRINT1("Failed to unprotect IAT.\n");
|
||||||
return(Status);
|
return(Status);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Walk through function list and fixup addresses. */
|
/* Walk through function list and fixup addresses. */
|
||||||
while (*FunctionNameList != 0L)
|
while (*FunctionNameList != 0L)
|
||||||
{
|
{
|
||||||
if ((*FunctionNameList) & 0x80000000)
|
if ((*FunctionNameList) & 0x80000000)
|
||||||
{
|
{
|
||||||
Ordinal = (*FunctionNameList) & 0x7fffffff;
|
Ordinal = (*FunctionNameList) & 0x7fffffff;
|
||||||
*ImportAddressList = LdrGetExportByOrdinal(ImportedModule->BaseAddress, Ordinal);
|
*ImportAddressList = LdrGetExportByOrdinal(ImportedModule->BaseAddress, Ordinal);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
IMAGE_IMPORT_BY_NAME *pe_name;
|
IMAGE_IMPORT_BY_NAME *pe_name;
|
||||||
pe_name = RVA(Module->BaseAddress, *FunctionNameList);
|
pe_name = RVA(Module->BaseAddress, *FunctionNameList);
|
||||||
*ImportAddressList = LdrGetExportByName(ImportedModule->BaseAddress, pe_name->Name, pe_name->Hint);
|
*ImportAddressList = LdrGetExportByName(ImportedModule->BaseAddress, pe_name->Name, pe_name->Hint);
|
||||||
if ((*ImportAddressList) == NULL)
|
if ((*ImportAddressList) == NULL)
|
||||||
{
|
{
|
||||||
DPRINT1("Failed to import %s\n", pe_name->Name);
|
DPRINT1("Failed to import %s\n", pe_name->Name);
|
||||||
return STATUS_UNSUCCESSFUL;
|
return STATUS_UNSUCCESSFUL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ImportAddressList++;
|
ImportAddressList++;
|
||||||
FunctionNameList++;
|
FunctionNameList++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Protect the region we are about to write into. */
|
/* Protect the region we are about to write into. */
|
||||||
Status = NtProtectVirtualMemory(NtCurrentProcess(),
|
Status = NtProtectVirtualMemory(NtCurrentProcess(),
|
||||||
|
@ -1534,10 +1505,55 @@ LdrpProcessImportDirectory(
|
||||||
return STATUS_SUCCESS;
|
return STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static NTSTATUS
|
||||||
|
LdrpProcessImportDirectory(
|
||||||
|
PLDR_MODULE Module,
|
||||||
|
PLDR_MODULE ImportedModule,
|
||||||
|
PCHAR ImportedName)
|
||||||
|
{
|
||||||
|
NTSTATUS Status;
|
||||||
|
PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory;
|
||||||
|
PCHAR Name;
|
||||||
|
|
||||||
NTSTATUS LdrpAdjustImportDirectory(PLDR_MODULE Module,
|
DPRINT("LdrpProcessImportDirectory(%x '%wZ', '%s')\n",
|
||||||
PLDR_MODULE ImportedModule,
|
Module, &Module->BaseDllName, ImportedName);
|
||||||
PUCHAR ImportedName)
|
|
||||||
|
|
||||||
|
ImportModuleDirectory = (PIMAGE_IMPORT_MODULE_DIRECTORY)
|
||||||
|
RtlImageDirectoryEntryToData(Module->BaseAddress,
|
||||||
|
TRUE,
|
||||||
|
IMAGE_DIRECTORY_ENTRY_IMPORT,
|
||||||
|
NULL);
|
||||||
|
if (ImportModuleDirectory == NULL)
|
||||||
|
{
|
||||||
|
return STATUS_UNSUCCESSFUL;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (ImportModuleDirectory->dwRVAModuleName)
|
||||||
|
{
|
||||||
|
Name = (PCHAR)Module->BaseAddress + ImportModuleDirectory->dwRVAModuleName;
|
||||||
|
if (0 == _stricmp(Name, ImportedName))
|
||||||
|
{
|
||||||
|
Status = LdrpProcessImportDirectoryEntry(Module,
|
||||||
|
ImportedModule,
|
||||||
|
ImportModuleDirectory);
|
||||||
|
if (!NT_SUCCESS(Status))
|
||||||
|
{
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImportModuleDirectory++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return STATUS_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static NTSTATUS
|
||||||
|
LdrpAdjustImportDirectory(PLDR_MODULE Module,
|
||||||
|
PLDR_MODULE ImportedModule,
|
||||||
|
PUCHAR ImportedName)
|
||||||
{
|
{
|
||||||
PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory;
|
PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory;
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
|
@ -1591,46 +1607,46 @@ NTSTATUS LdrpAdjustImportDirectory(PLDR_MODULE Module,
|
||||||
IATSize++;
|
IATSize++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unprotect the region we are about to write into. */
|
/* Unprotect the region we are about to write into. */
|
||||||
IATBase = (PVOID)ImportAddressList;
|
IATBase = (PVOID)ImportAddressList;
|
||||||
Status = NtProtectVirtualMemory(NtCurrentProcess(),
|
Status = NtProtectVirtualMemory(NtCurrentProcess(),
|
||||||
IATBase,
|
IATBase,
|
||||||
IATSize * sizeof(PVOID*),
|
IATSize * sizeof(PVOID*),
|
||||||
PAGE_READWRITE,
|
PAGE_READWRITE,
|
||||||
&OldProtect);
|
&OldProtect);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("Failed to unprotect IAT.\n");
|
DPRINT1("Failed to unprotect IAT.\n");
|
||||||
return(Status);
|
return(Status);
|
||||||
}
|
}
|
||||||
|
|
||||||
NTHeaders = RtlImageNtHeader (ImportedModule->BaseAddress);
|
NTHeaders = RtlImageNtHeader (ImportedModule->BaseAddress);
|
||||||
Start = (PVOID)NTHeaders->OptionalHeader.ImageBase;
|
Start = (PVOID)NTHeaders->OptionalHeader.ImageBase;
|
||||||
End = Start + ImportedModule->SizeOfImage;
|
End = Start + ImportedModule->SizeOfImage;
|
||||||
Offset = ImportedModule->BaseAddress - Start;
|
Offset = ImportedModule->BaseAddress - Start;
|
||||||
|
|
||||||
/* Walk through function list and fixup addresses. */
|
/* Walk through function list and fixup addresses. */
|
||||||
while (*FunctionNameList != 0L)
|
while (*FunctionNameList != 0L)
|
||||||
{
|
{
|
||||||
if (*ImportAddressList >= Start && *ImportAddressList < End)
|
if (*ImportAddressList >= Start && *ImportAddressList < End)
|
||||||
{
|
{
|
||||||
(*ImportAddressList) += Offset;
|
(*ImportAddressList) += Offset;
|
||||||
}
|
}
|
||||||
ImportAddressList++;
|
ImportAddressList++;
|
||||||
FunctionNameList++;
|
FunctionNameList++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Protect the region we are about to write into. */
|
/* Protect the region we are about to write into. */
|
||||||
Status = NtProtectVirtualMemory(NtCurrentProcess(),
|
Status = NtProtectVirtualMemory(NtCurrentProcess(),
|
||||||
IATBase,
|
IATBase,
|
||||||
IATSize * sizeof(PVOID*),
|
IATSize * sizeof(PVOID*),
|
||||||
OldProtect,
|
OldProtect,
|
||||||
&OldProtect);
|
&OldProtect);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("Failed to protect IAT.\n");
|
DPRINT1("Failed to protect IAT.\n");
|
||||||
return(Status);
|
return(Status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ImportModuleDirectory++;
|
ImportModuleDirectory++;
|
||||||
}
|
}
|
||||||
|
@ -1733,7 +1749,7 @@ LdrFixupImports(IN PWSTR SearchPath OPTIONAL,
|
||||||
{
|
{
|
||||||
TRACE_LDR("%wZ has stale binding to %wZ\n",
|
TRACE_LDR("%wZ has stale binding to %wZ\n",
|
||||||
&Module->BaseDllName, &ImportedModule->BaseDllName);
|
&Module->BaseDllName, &ImportedModule->BaseDllName);
|
||||||
Status = LdrpProcessImportDirectory(Module, ImportedModule, NULL, ImportedName);
|
Status = LdrpProcessImportDirectory(Module, ImportedModule, ImportedName);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("failed to import %s\n", ImportedName);
|
DPRINT1("failed to import %s\n", ImportedName);
|
||||||
|
@ -1794,7 +1810,7 @@ LdrFixupImports(IN PWSTR SearchPath OPTIONAL,
|
||||||
if (WrongForwarder ||
|
if (WrongForwarder ||
|
||||||
ImportedModule->Flags & IMAGE_NOT_AT_BASE)
|
ImportedModule->Flags & IMAGE_NOT_AT_BASE)
|
||||||
{
|
{
|
||||||
Status = LdrpProcessImportDirectory(Module, ImportedModule, NULL, ImportedName);
|
Status = LdrpProcessImportDirectory(Module, ImportedModule, ImportedName);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("failed to import %s\n", ImportedName);
|
DPRINT1("failed to import %s\n", ImportedName);
|
||||||
|
@ -1820,7 +1836,7 @@ LdrFixupImports(IN PWSTR SearchPath OPTIONAL,
|
||||||
*/
|
*/
|
||||||
TRACE_LDR("Stale BIND %s from %wZ\n",
|
TRACE_LDR("Stale BIND %s from %wZ\n",
|
||||||
ImportedName, &Module->BaseDllName);
|
ImportedName, &Module->BaseDllName);
|
||||||
Status = LdrpProcessImportDirectory(Module, ImportedModule, NULL, ImportedName);
|
Status = LdrpProcessImportDirectory(Module, ImportedModule, ImportedName);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("faild to import %s\n", ImportedName);
|
DPRINT1("faild to import %s\n", ImportedName);
|
||||||
|
@ -1858,7 +1874,7 @@ LdrFixupImports(IN PWSTR SearchPath OPTIONAL,
|
||||||
|
|
||||||
TRACE_LDR("Initializing imports for %wZ from %s\n",
|
TRACE_LDR("Initializing imports for %wZ from %s\n",
|
||||||
&Module->BaseDllName, ImportedName);
|
&Module->BaseDllName, ImportedName);
|
||||||
Status = LdrpProcessImportDirectory(Module, ImportedModule, ImportModuleDirectoryCurrent, ImportedName);
|
Status = LdrpProcessImportDirectoryEntry(Module, ImportedModule, ImportModuleDirectoryCurrent);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("failed to import %s\n", ImportedName);
|
DPRINT1("failed to import %s\n", ImportedName);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue