ReactOS  0.4.14-dev-384-g5b37caa
peloader.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

BOOLEAN PeLdrLoadImage (IN PCHAR FileName, IN TYPE_OF_MEMORY MemoryType, OUT PVOID *ImageBasePA)
 
BOOLEAN PeLdrAllocateDataTableEntry (IN OUT PLIST_ENTRY ModuleListHead, IN PCCH BaseDllName, IN PCCH FullDllName, IN PVOID BasePA, OUT PLDR_DATA_TABLE_ENTRY *NewEntry)
 
BOOLEAN PeLdrScanImportDescriptorTable (IN OUT PLIST_ENTRY ModuleListHead, IN PCCH DirectoryPath, IN PLDR_DATA_TABLE_ENTRY ScanDTE)
 
BOOLEAN PeLdrCheckForLoadedDll (IN OUT PLIST_ENTRY ModuleListHead, IN PCH DllName, OUT PLDR_DATA_TABLE_ENTRY *LoadedEntry)
 

Function Documentation

◆ PeLdrAllocateDataTableEntry()

BOOLEAN PeLdrAllocateDataTableEntry ( IN OUT PLIST_ENTRY  ModuleListHead,
IN PCCH  BaseDllName,
IN PCCH  FullDllName,
IN PVOID  BasePA,
OUT PLDR_DATA_TABLE_ENTRY NewEntry 
)

Definition at line 585 of file peloader.c.

591 {
592  PVOID BaseVA = PaToVa(BasePA);
593  PWSTR Buffer;
594  PLDR_DATA_TABLE_ENTRY DataTableEntry;
595  PIMAGE_NT_HEADERS NtHeaders;
596  USHORT Length;
597 
598  TRACE("PeLdrAllocateDataTableEntry(, '%s', '%s', %p)\n",
599  BaseDllName, FullDllName, BasePA);
600 
601  /* Allocate memory for a data table entry, zero-initialize it */
603  TAG_WLDR_DTE);
604  if (DataTableEntry == NULL)
605  return FALSE;
606  RtlZeroMemory(DataTableEntry, sizeof(LDR_DATA_TABLE_ENTRY));
607 
608  /* Get NT headers from the image */
609  NtHeaders = RtlImageNtHeader(BasePA);
610 
611  /* Initialize corresponding fields of DTE based on NT headers value */
612  DataTableEntry->DllBase = BaseVA;
613  DataTableEntry->SizeOfImage = NtHeaders->OptionalHeader.SizeOfImage;
614  DataTableEntry->EntryPoint = RVA(BaseVA, NtHeaders->OptionalHeader.AddressOfEntryPoint);
615  DataTableEntry->SectionPointer = 0;
616  DataTableEntry->CheckSum = NtHeaders->OptionalHeader.CheckSum;
617 
618  /* Initialize BaseDllName field (UNICODE_STRING) from the Ansi BaseDllName
619  by simple conversion - copying each character */
620  Length = (USHORT)(strlen(BaseDllName) * sizeof(WCHAR));
622  if (Buffer == NULL)
623  {
624  FrLdrHeapFree(DataTableEntry, TAG_WLDR_DTE);
625  return FALSE;
626  }
628 
629  DataTableEntry->BaseDllName.Length = Length;
630  DataTableEntry->BaseDllName.MaximumLength = Length;
631  DataTableEntry->BaseDllName.Buffer = PaToVa(Buffer);
632  while (*BaseDllName != 0)
633  {
634  *Buffer++ = *BaseDllName++;
635  }
636 
637  /* Initialize FullDllName field (UNICODE_STRING) from the Ansi FullDllName
638  using the same method */
639  Length = (USHORT)(strlen(FullDllName) * sizeof(WCHAR));
641  if (Buffer == NULL)
642  {
643  FrLdrHeapFree(DataTableEntry, TAG_WLDR_DTE);
644  return FALSE;
645  }
647 
648  DataTableEntry->FullDllName.Length = Length;
649  DataTableEntry->FullDllName.MaximumLength = Length;
650  DataTableEntry->FullDllName.Buffer = PaToVa(Buffer);
651  while (*FullDllName != 0)
652  {
653  *Buffer++ = *FullDllName++;
654  }
655 
656  /* Initialize what's left - LoadCount which is 1, and set Flags so that
657  we know this entry is processed */
658  DataTableEntry->Flags = LDRP_ENTRY_PROCESSED;
659  DataTableEntry->LoadCount = 1;
660 
661  /* Honour the FORCE_INTEGRITY flag */
663  {
664  /*
665  * On Vista and above, the LDRP_IMAGE_INTEGRITY_FORCED flag must be set
666  * if IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY is set in the image header.
667  * This is done after the image has been loaded and the digital signature
668  * check has passed successfully. (We do not do it yet!)
669  *
670  * Several OS functionality depend on the presence of this flag.
671  * For example, when using Object-Manager callbacks the latter will call
672  * MmVerifyCallbackFunction() to verify whether the flag is present.
673  * If not callbacks will not work.
674  * (See Windows Internals Part 1, 6th edition, p. 176.)
675  */
676  DataTableEntry->Flags |= LDRP_IMAGE_INTEGRITY_FORCED;
677  }
678 
679  /* Insert this DTE to a list in the LPB */
681  TRACE("Inserting DTE %p, name='%.*S' DllBase=%p \n", DataTableEntry,
682  DataTableEntry->BaseDllName.Length / 2,
683  VaToPa(DataTableEntry->BaseDllName.Buffer),
684  DataTableEntry->DllBase);
685 
686  /* Save pointer to a newly allocated and initialized entry */
687  *NewEntry = DataTableEntry;
688 
689  /* Return success */
690  return TRUE;
691 }
#define TAG_WLDR_DTE
Definition: winldr.h:13
#define LDRP_ENTRY_PROCESSED
Definition: ldrtypes.h:44
#define TRUE
Definition: types.h:120
FORCEINLINE VOID FrLdrHeapFree(PVOID MemoryPointer, ULONG Tag)
Definition: mm.h:170
USHORT MaximumLength
Definition: env_spec_w32.h:370
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
uint16_t * PWSTR
Definition: typedefs.h:54
ULONG SizeOfImage
Definition: ldrtypes.h:143
#define IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY
Definition: ntimage.h:456
#define InsertTailList(ListHead, Entry)
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
PVOID DllBase
Definition: btrfs_drv.h:1857
#define LDRP_IMAGE_INTEGRITY_FORCED
Definition: ldrtypes.h:41
PVOID EntryPoint
Definition: ntddk_ex.h:203
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
_In_ PCWSTR FullDllName
Definition: ldrtypes.h:247
#define TAG_WLDR_NAME
Definition: winldr.h:15
FORCEINLINE PVOID FrLdrHeapAlloc(SIZE_T MemorySize, ULONG Tag)
Definition: mm.h:163
#define TRACE(s)
Definition: solgame.cpp:4
__wchar_t WCHAR
Definition: xmlstorage.h:180
ULONG CheckSum
Definition: btrfs_drv.h:1863
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
struct _LDR_DATA_TABLE_ENTRY * PLDR_DATA_TABLE_ENTRY
Definition: btrfs_drv.h:1853
LIST_ENTRY InLoadOrderLinks
Definition: ldrtypes.h:138
#define RVA(m, b)
Definition: freeldr.h:24
PVOID SectionPointer
Definition: ntddk_ex.h:213
unsigned short USHORT
Definition: pedump.c:61
UNICODE_STRING FullDllName
Definition: btrfs_drv.h:1859
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:145
#define RtlImageNtHeader
Definition: compat.h:465
FORCEINLINE PVOID PaToVa(PVOID Pa)
Definition: conversion.h:22
ULONG Flags
Definition: ntddk_ex.h:207
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
FORCEINLINE PVOID VaToPa(PVOID Va)
Definition: conversion.h:15
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
LIST_ENTRY * ModuleListHead
Definition: kdpacket.c:23
USHORT LoadCount
Definition: ntddk_ex.h:208

Referenced by LoadBootDeviceDriver(), LoadModule(), PeLdrpLoadAndScanReferencedDll(), and WinLdrLoadDeviceDriver().

◆ PeLdrCheckForLoadedDll()

BOOLEAN PeLdrCheckForLoadedDll ( IN OUT PLIST_ENTRY  ModuleListHead,
IN PCH  DllName,
OUT PLDR_DATA_TABLE_ENTRY LoadedEntry 
)

Definition at line 465 of file peloader.c.

469 {
470  PLDR_DATA_TABLE_ENTRY DataTableEntry;
471  LIST_ENTRY *ModuleEntry;
472 
473  TRACE("PeLdrCheckForLoadedDll: DllName %s\n", DllName);
474 
475  /* Just go through each entry in the LoadOrderList and compare loaded module's
476  name with a given name */
477  ModuleEntry = ModuleListHead->Flink;
478  while (ModuleEntry != ModuleListHead)
479  {
480  /* Get pointer to the current DTE */
481  DataTableEntry = CONTAINING_RECORD(ModuleEntry,
483  InLoadOrderLinks);
484 
485  TRACE("PeLdrCheckForLoadedDll: DTE %p, EP %p, base %p name '%.*ws'\n",
486  DataTableEntry, DataTableEntry->EntryPoint, DataTableEntry->DllBase,
487  DataTableEntry->BaseDllName.Length / 2, VaToPa(DataTableEntry->BaseDllName.Buffer));
488 
489  /* Compare names */
490  if (PeLdrpCompareDllName(DllName, &DataTableEntry->BaseDllName))
491  {
492  /* Yes, found it, report pointer to the loaded module's DTE
493  to the caller and increase load count for it */
494  *LoadedEntry = DataTableEntry;
495  DataTableEntry->LoadCount++;
496  TRACE("PeLdrCheckForLoadedDll: LoadedEntry %X\n", DataTableEntry);
497  return TRUE;
498  }
499 
500  /* Go to the next entry */
501  ModuleEntry = ModuleEntry->Flink;
502  }
503 
504  /* Nothing found */
505  return FALSE;
506 }
#define TRUE
Definition: types.h:120
PVOID DllBase
Definition: btrfs_drv.h:1857
PVOID EntryPoint
Definition: ntddk_ex.h:203
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
#define TRACE(s)
Definition: solgame.cpp:4
Definition: btrfs_drv.h:1853
Definition: typedefs.h:117
static BOOLEAN PeLdrpCompareDllName(IN PCH DllName, IN PUNICODE_STRING UnicodeName)
Definition: peloader.c:30
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:145
FORCEINLINE PVOID VaToPa(PVOID Va)
Definition: conversion.h:15
LIST_ENTRY * ModuleListHead
Definition: kdpacket.c:23
USHORT LoadCount
Definition: ntddk_ex.h:208

Referenced by PeLdrpBindImportName(), PeLdrScanImportDescriptorTable(), and WinLdrLoadDeviceDriver().

◆ PeLdrLoadImage()

BOOLEAN PeLdrLoadImage ( IN PCHAR  FileName,
IN TYPE_OF_MEMORY  MemoryType,
OUT PVOID ImageBasePA 
)

Definition at line 701 of file peloader.c.

705 {
706  ULONG FileId;
707  PVOID PhysicalBase;
708  PVOID VirtualBase = NULL;
709  UCHAR HeadersBuffer[SECTOR_SIZE * 2];
710  PIMAGE_NT_HEADERS NtHeaders;
711  PIMAGE_SECTION_HEADER SectionHeader;
712  ULONG VirtualSize, SizeOfRawData, NumberOfSections;
715  ULONG i, BytesRead;
716 
717  TRACE("PeLdrLoadImage(%s, %ld, *)\n", FileName, MemoryType);
718 
719  /* Open the image file */
720  Status = ArcOpen((PSTR)FileName, OpenReadOnly, &FileId);
721  if (Status != ESUCCESS)
722  {
723  WARN("ArcOpen(FileName: '%s') failed. Status: %u\n", FileName, Status);
724  return FALSE;
725  }
726 
727  /* Load the first 2 sectors of the image so we can read the PE header */
728  Status = ArcRead(FileId, HeadersBuffer, SECTOR_SIZE * 2, &BytesRead);
729  if (Status != ESUCCESS)
730  {
731  ERR("ArcRead(File: '%s') failed. Status: %u\n", FileName, Status);
732  UiMessageBox("Error reading from file.");
733  ArcClose(FileId);
734  return FALSE;
735  }
736 
737  /* Now read the MZ header to get the offset to the PE Header */
738  NtHeaders = RtlImageNtHeader(HeadersBuffer);
739  if (!NtHeaders)
740  {
741  ERR("No NT header found in \"%s\"\n", FileName);
742  UiMessageBox("Error: No NT header found.");
743  ArcClose(FileId);
744  return FALSE;
745  }
746 
747  /* Ensure this is executable image */
748  if (((NtHeaders->FileHeader.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) == 0))
749  {
750  ERR("Not an executable image \"%s\"\n", FileName);
751  UiMessageBox("Not an executable image.");
752  ArcClose(FileId);
753  return FALSE;
754  }
755 
756  /* Store number of sections to read and a pointer to the first section */
757  NumberOfSections = NtHeaders->FileHeader.NumberOfSections;
758  SectionHeader = IMAGE_FIRST_SECTION(NtHeaders);
759 
760  /* Try to allocate this memory, if fails - allocate somewhere else */
761  PhysicalBase = MmAllocateMemoryAtAddress(NtHeaders->OptionalHeader.SizeOfImage,
762  (PVOID)((ULONG)NtHeaders->OptionalHeader.ImageBase & (KSEG0_BASE - 1)),
763  MemoryType);
764 
765  if (PhysicalBase == NULL)
766  {
767  /* It's ok, we don't panic - let's allocate again at any other "low" place */
768  PhysicalBase = MmAllocateMemoryWithType(NtHeaders->OptionalHeader.SizeOfImage, MemoryType);
769 
770  if (PhysicalBase == NULL)
771  {
772  ERR("Failed to alloc %lu bytes for image %s\n", NtHeaders->OptionalHeader.SizeOfImage, FileName);
773  UiMessageBox("Failed to alloc pages for image.");
774  ArcClose(FileId);
775  return FALSE;
776  }
777  }
778 
779  /* This is the real image base - in form of a virtual address */
780  VirtualBase = PaToVa(PhysicalBase);
781 
782  TRACE("Base PA: 0x%X, VA: 0x%X\n", PhysicalBase, VirtualBase);
783 
784  /* Set to 0 position and fully load the file image */
785  Position.QuadPart = 0;
786  Status = ArcSeek(FileId, &Position, SeekAbsolute);
787  if (Status != ESUCCESS)
788  {
789  ERR("ArcSeek(File: '%s') failed. Status: 0x%lx\n", FileName, Status);
790  UiMessageBox("Error seeking the start of a file.");
791  ArcClose(FileId);
792  return FALSE;
793  }
794 
795  Status = ArcRead(FileId, PhysicalBase, NtHeaders->OptionalHeader.SizeOfHeaders, &BytesRead);
796  if (Status != ESUCCESS)
797  {
798  ERR("ArcRead(File: '%s') failed. Status: %u\n", FileName, Status);
799  UiMessageBox("Error reading headers.");
800  ArcClose(FileId);
801  return FALSE;
802  }
803 
804  /*
805  * On Vista and above, a digital signature check is performed when the image
806  * has the IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY flag set in its header.
807  * (We of course do not perform this check yet!)
808  */
809 
810  /* Reload the NT Header */
811  NtHeaders = RtlImageNtHeader(PhysicalBase);
812 
813  /* Load the first section */
814  SectionHeader = IMAGE_FIRST_SECTION(NtHeaders);
815 
816  /* Fill output parameters */
817  *ImageBasePA = PhysicalBase;
818 
819  /* Walk through each section and read it (check/fix any possible
820  bad situations, if they arise) */
821  for (i = 0; i < NumberOfSections; i++)
822  {
823  VirtualSize = SectionHeader->Misc.VirtualSize;
824  SizeOfRawData = SectionHeader->SizeOfRawData;
825 
826  /* Handle a case when VirtualSize equals 0 */
827  if (VirtualSize == 0)
828  VirtualSize = SizeOfRawData;
829 
830  /* If PointerToRawData is 0, then force its size to be also 0 */
831  if (SectionHeader->PointerToRawData == 0)
832  {
833  SizeOfRawData = 0;
834  }
835  else
836  {
837  /* Cut the loaded size to the VirtualSize extents */
838  if (SizeOfRawData > VirtualSize)
839  SizeOfRawData = VirtualSize;
840  }
841 
842  /* Actually read the section (if its size is not 0) */
843  if (SizeOfRawData != 0)
844  {
845  /* Seek to the correct position */
846  Position.LowPart = SectionHeader->PointerToRawData;
847  Status = ArcSeek(FileId, &Position, SeekAbsolute);
848 
849  TRACE("SH->VA: 0x%X\n", SectionHeader->VirtualAddress);
850 
851  /* Read this section from the file, size = SizeOfRawData */
852  Status = ArcRead(FileId, (PUCHAR)PhysicalBase + SectionHeader->VirtualAddress, SizeOfRawData, &BytesRead);
853  if (Status != ESUCCESS)
854  {
855  ERR("PeLdrLoadImage(): Error reading section from file!\n");
856  break;
857  }
858  }
859 
860  /* Size of data is less than the virtual size - fill up the remainder with zeroes */
861  if (SizeOfRawData < VirtualSize)
862  {
863  TRACE("PeLdrLoadImage(): SORD %d < VS %d\n", SizeOfRawData, VirtualSize);
864  RtlZeroMemory((PVOID)(SectionHeader->VirtualAddress + (ULONG_PTR)PhysicalBase + SizeOfRawData), VirtualSize - SizeOfRawData);
865  }
866 
867  SectionHeader++;
868  }
869 
870  /* We are done with the file - close it */
871  ArcClose(FileId);
872 
873  /* If loading failed - return right now */
874  if (Status != ESUCCESS)
875  return FALSE;
876 
877  /* Relocate the image, if it needs it */
878  if (NtHeaders->OptionalHeader.ImageBase != (ULONG_PTR)VirtualBase)
879  {
880  WARN("Relocating %p -> %p\n", NtHeaders->OptionalHeader.ImageBase, VirtualBase);
881  return (BOOLEAN)LdrRelocateImageWithBias(PhysicalBase,
882  (ULONG_PTR)VirtualBase - (ULONG_PTR)PhysicalBase,
883  "FreeLdr",
884  TRUE,
885  TRUE, /* in case of conflict still return success */
886  FALSE);
887  }
888 
889  TRACE("PeLdrLoadImage() done, PA = %p\n", *ImageBasePA);
890  return TRUE;
891 }
#define TRUE
Definition: types.h:120
Definition: arc.h:32
static COORD Position
Definition: mouse.c:34
unsigned char * PUCHAR
Definition: retypes.h:3
#define WARN(fmt,...)
Definition: debug.h:111
ULONG ARC_STATUS
Definition: arc.h:4
DWORD PointerToRawData
Definition: pedump.c:290
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
uint32_t ULONG_PTR
Definition: typedefs.h:63
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
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:427
#define KSEG0_BASE
Definition: ketypes.h:273
VOID UiMessageBox(PCSTR Format,...)
Definition: ui.c:320
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
#define TRACE(s)
Definition: solgame.cpp:4
#define IMAGE_FILE_EXECUTABLE_IMAGE
Definition: pedump.c:160
ARC_STATUS ArcRead(ULONG FileId, VOID *Buffer, ULONG N, ULONG *Count)
Definition: fs.c:236
unsigned char UCHAR
Definition: xmlstorage.h:181
PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType)
Definition: mm.c:31
ARC_STATUS ArcSeek(ULONG FileId, LARGE_INTEGER *Position, SEEKMODE SeekMode)
Definition: fs.c:243
PVOID MmAllocateMemoryAtAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
Definition: mm.c:85
Status
Definition: gdiplustypes.h:24
#define ERR(fmt,...)
Definition: debug.h:109
NTSYSAPI ULONG NTAPI LdrRelocateImageWithBias(_In_ PVOID NewAddress, _In_ LONGLONG AdditionalBias, _In_ PCCH LoaderName, _In_ ULONG Success, _In_ ULONG Conflict, _In_ ULONG Invalid)
signed char * PSTR
Definition: retypes.h:7
ARC_STATUS ArcClose(ULONG FileId)
Definition: fs.c:218
#define RtlImageNtHeader
Definition: compat.h:465
FORCEINLINE PVOID PaToVa(PVOID Pa)
Definition: conversion.h:22
ARC_STATUS ArcOpen(CHAR *Path, OPENMODE OpenMode, ULONG *FileId)
Definition: fs.c:56
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define SECTOR_SIZE
Definition: fs.h:22
union _IMAGE_SECTION_HEADER::@1543 Misc
_Must_inspect_result_ _In_ PFILE_OBJECT _In_opt_ PLARGE_INTEGER _In_ ULONG _In_ FLT_IO_OPERATION_FLAGS _Out_opt_ PULONG BytesRead
Definition: fltkernel.h:1255

Referenced by LoadBootDeviceDriver(), LoadModule(), PeLdrpLoadAndScanReferencedDll(), and WinLdrLoadDeviceDriver().

◆ PeLdrScanImportDescriptorTable()

BOOLEAN PeLdrScanImportDescriptorTable ( IN OUT PLIST_ENTRY  ModuleListHead,
IN PCCH  DirectoryPath,
IN PLDR_DATA_TABLE_ENTRY  ScanDTE 
)

Definition at line 509 of file peloader.c.

513 {
514  PLDR_DATA_TABLE_ENTRY DataTableEntry;
515  PIMAGE_IMPORT_DESCRIPTOR ImportTable;
516  ULONG ImportTableSize;
517  PCH ImportName;
519 
520  /* Get a pointer to the import table of this image */
521  ImportTable = (PIMAGE_IMPORT_DESCRIPTOR)RtlImageDirectoryEntryToData(VaToPa(ScanDTE->DllBase),
522  TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &ImportTableSize);
523 
524 #if DBG
525  {
526  UNICODE_STRING BaseName;
527  BaseName.Buffer = VaToPa(ScanDTE->BaseDllName.Buffer);
528  BaseName.MaximumLength = ScanDTE->BaseDllName.MaximumLength;
529  BaseName.Length = ScanDTE->BaseDllName.Length;
530  TRACE("PeLdrScanImportDescriptorTable(): %wZ ImportTable = 0x%X\n",
531  &BaseName, ImportTable);
532  }
533 #endif
534 
535  /* If image doesn't have any import directory - just return success */
536  if (ImportTable == NULL)
537  return TRUE;
538 
539  /* Loop through all entries */
540  for (;(ImportTable->Name != 0) && (ImportTable->FirstThunk != 0);ImportTable++)
541  {
542  /* Get pointer to the name */
543  ImportName = (PCH)VaToPa(RVA(ScanDTE->DllBase, ImportTable->Name));
544  TRACE("PeLdrScanImportDescriptorTable(): Looking at %s\n", ImportName);
545 
546  /* In case we get a reference to ourselves - just skip it */
547  if (PeLdrpCompareDllName(ImportName, &ScanDTE->BaseDllName))
548  continue;
549 
550  /* Load the DLL if it is not already loaded */
551  if (!PeLdrCheckForLoadedDll(ModuleListHead, ImportName, &DataTableEntry))
552  {
554  DirectoryPath,
555  ImportName,
556  &ScanDTE->InLoadOrderLinks,
557  &DataTableEntry);
558  if (!Success)
559  {
560  ERR("PeLdrpLoadAndScanReferencedDll() failed\n");
561  return Success;
562  }
563  }
564 
565  /* Scan its import address table */
567  DataTableEntry->DllBase,
568  ScanDTE->DllBase,
569  (PIMAGE_THUNK_DATA)RVA(ScanDTE->DllBase, ImportTable->FirstThunk),
570  DirectoryPath,
571  &ScanDTE->InLoadOrderLinks);
572 
573  if (!Success)
574  {
575  ERR("PeLdrpScanImportAddressTable() failed: ImportName = '%s', DirectoryPath = '%s'\n",
576  ImportName, DirectoryPath);
577  return Success;
578  }
579  }
580 
581  return TRUE;
582 }
#define TRUE
Definition: types.h:120
USHORT MaximumLength
Definition: env_spec_w32.h:370
struct _IMAGE_IMPORT_DESCRIPTOR * PIMAGE_IMPORT_DESCRIPTOR
BOOLEAN PeLdrCheckForLoadedDll(IN OUT PLIST_ENTRY ModuleListHead, IN PCH DllName, OUT PLDR_DATA_TABLE_ENTRY *LoadedEntry)
Definition: peloader.c:465
PVOID DllBase
Definition: btrfs_drv.h:1857
CHAR * PCH
Definition: ntbasedef.h:398
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define TRACE(s)
Definition: solgame.cpp:4
#define RtlImageDirectoryEntryToData
Definition: compat.h:468
Definition: btrfs_drv.h:1853
static BOOLEAN PeLdrpCompareDllName(IN PCH DllName, IN PUNICODE_STRING UnicodeName)
Definition: peloader.c:30
#define ERR(fmt,...)
Definition: debug.h:109
#define RVA(m, b)
Definition: freeldr.h:24
static BOOLEAN PeLdrpScanImportAddressTable(IN OUT PLIST_ENTRY ModuleListHead, IN PVOID DllBase, IN PVOID ImageBase, IN PIMAGE_THUNK_DATA ThunkData, IN PCSTR DirectoryPath, IN PLIST_ENTRY Parent)
Definition: peloader.c:395
#define IMAGE_DIRECTORY_ENTRY_IMPORT
Definition: pedump.c:260
DWORD RVA
Definition: compat.h:911
unsigned int ULONG
Definition: retypes.h:1
FORCEINLINE PVOID VaToPa(PVOID Va)
Definition: conversion.h:15
LIST_ENTRY * ModuleListHead
Definition: kdpacket.c:23
static BOOLEAN PeLdrpLoadAndScanReferencedDll(IN OUT PLIST_ENTRY ModuleListHead, IN PCCH DirectoryPath, IN PCH ImportName, IN PLIST_ENTRY Parent OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY *DataTableEntry)
Definition: peloader.c:342

Referenced by LoadBootDeviceDriver(), LoadWindowsCore(), PeLdrpLoadAndScanReferencedDll(), and WinLdrLoadDeviceDriver().