ReactOS 0.4.16-dev-303-g11d5cb8
peloader.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef VOID(NTAPIPELDR_IMPORTDLL_LOAD_CALLBACK) (_In_ PCSTR FileName)
 

Functions

BOOLEAN PeLdrInitializeModuleList (VOID)
 
BOOLEAN PeLdrLoadImage (_In_ PCSTR FilePath, _In_ TYPE_OF_MEMORY MemoryType, _Out_ PVOID *ImageBasePA)
 
BOOLEAN PeLdrLoadImageEx (_In_ PCSTR FilePath, _In_ TYPE_OF_MEMORY MemoryType, _Out_ PVOID *ImageBasePA, _In_ BOOLEAN KernelMapping)
 Loads the specified image from the file.
 
BOOLEAN PeLdrAllocateDataTableEntry (IN OUT PLIST_ENTRY ModuleListHead, IN PCCH BaseDllName, IN PCCH FullDllName, IN PVOID BaseVA, OUT PLDR_DATA_TABLE_ENTRY *NewEntry)
 
VOID PeLdrFreeDataTableEntry (_In_ PLDR_DATA_TABLE_ENTRY Entry)
 
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)
 
PVOID PeLdrInitSecurityCookie (_In_ PLDR_DATA_TABLE_ENTRY LdrEntry)
 
BOOLEAN PeLdrLoadBootImage (_In_ PCSTR FilePath, _In_ PCSTR BaseDllName, _Out_ PVOID *ImageBase, _Out_ PLDR_DATA_TABLE_ENTRY *DataTableEntry)
 

Variables

PELDR_IMPORTDLL_LOAD_CALLBACK PeLdrImportDllLoadCallback
 

Typedef Documentation

◆ PELDR_IMPORTDLL_LOAD_CALLBACK

typedef VOID(NTAPI * PELDR_IMPORTDLL_LOAD_CALLBACK) (_In_ PCSTR FileName)

Definition at line 23 of file peloader.h.

Function Documentation

◆ PeLdrAllocateDataTableEntry()

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

Definition at line 706 of file peloader.c.

712{
713 PVOID BasePA = VaToPa(BaseVA);
714 PWSTR BaseDllNameBuffer, Buffer;
715 PLDR_DATA_TABLE_ENTRY DataTableEntry;
716 PIMAGE_NT_HEADERS NtHeaders;
718
719 TRACE("PeLdrAllocateDataTableEntry('%s', '%s', %p)\n",
720 BaseDllName, FullDllName, BasePA);
721
722 /* Allocate memory for a data table entry, zero-initialize it */
725 if (DataTableEntry == NULL)
726 return FALSE;
727
728 /* Get NT headers from the image */
729 NtHeaders = RtlImageNtHeader(BasePA);
730
731 /* Initialize corresponding fields of DTE based on NT headers value */
732 RtlZeroMemory(DataTableEntry, sizeof(LDR_DATA_TABLE_ENTRY));
733 DataTableEntry->DllBase = BaseVA;
734 DataTableEntry->SizeOfImage = NtHeaders->OptionalHeader.SizeOfImage;
735 DataTableEntry->EntryPoint = RVA(BaseVA, NtHeaders->OptionalHeader.AddressOfEntryPoint);
736 DataTableEntry->SectionPointer = 0;
737 DataTableEntry->CheckSum = NtHeaders->OptionalHeader.CheckSum;
738
739 /* Initialize BaseDllName field (UNICODE_STRING) from the Ansi BaseDllName
740 by simple conversion - copying each character */
741 Length = (USHORT)(strlen(BaseDllName) * sizeof(WCHAR));
743 if (Buffer == NULL)
744 {
745 FrLdrHeapFree(DataTableEntry, TAG_WLDR_DTE);
746 return FALSE;
747 }
748
749 /* Save Buffer, in case of later failure */
750 BaseDllNameBuffer = Buffer;
751
752 DataTableEntry->BaseDllName.Length = Length;
753 DataTableEntry->BaseDllName.MaximumLength = Length;
754 DataTableEntry->BaseDllName.Buffer = PaToVa(Buffer);
755
757 Length /= sizeof(WCHAR);
758 while (Length--)
759 {
760 *Buffer++ = *BaseDllName++;
761 }
762
763 /* Initialize FullDllName field (UNICODE_STRING) from the Ansi FullDllName
764 using the same method */
765 Length = (USHORT)(strlen(FullDllName) * sizeof(WCHAR));
767 if (Buffer == NULL)
768 {
769 FrLdrHeapFree(BaseDllNameBuffer, TAG_WLDR_NAME);
770 FrLdrHeapFree(DataTableEntry, TAG_WLDR_DTE);
771 return FALSE;
772 }
773
774 DataTableEntry->FullDllName.Length = Length;
775 DataTableEntry->FullDllName.MaximumLength = Length;
776 DataTableEntry->FullDllName.Buffer = PaToVa(Buffer);
777
779 Length /= sizeof(WCHAR);
780 while (Length--)
781 {
782 *Buffer++ = *FullDllName++;
783 }
784
785 /* Initialize what's left - LoadCount which is 1, and set Flags so that
786 we know this entry is processed */
787 DataTableEntry->Flags = LDRP_ENTRY_PROCESSED;
788 DataTableEntry->LoadCount = 1;
789
790 /* Honour the FORCE_INTEGRITY flag */
792 {
793 /*
794 * On Vista and above, the LDRP_IMAGE_INTEGRITY_FORCED flag must be set
795 * if IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY is set in the image header.
796 * This is done after the image has been loaded and the digital signature
797 * check has passed successfully. (We do not do it yet!)
798 *
799 * Several OS functionality depend on the presence of this flag.
800 * For example, when using Object-Manager callbacks the latter will call
801 * MmVerifyCallbackFunction() to verify whether the flag is present.
802 * If not callbacks will not work.
803 * (See Windows Internals Part 1, 6th edition, p. 176.)
804 */
805 DataTableEntry->Flags |= LDRP_IMAGE_INTEGRITY_FORCED;
806 }
807
808 /* Insert this DTE to a list in the LPB */
810 TRACE("Inserting DTE %p, name='%.*S' DllBase=%p\n", DataTableEntry,
811 DataTableEntry->BaseDllName.Length / sizeof(WCHAR),
812 VaToPa(DataTableEntry->BaseDllName.Buffer),
813 DataTableEntry->DllBase);
814
815 /* Save pointer to a newly allocated and initialized entry */
816 *NewEntry = DataTableEntry;
817
818 /* Return success */
819 return TRUE;
820}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
FORCEINLINE VOID FrLdrHeapFree(PVOID MemoryPointer, ULONG Tag)
Definition: mm.h:181
FORCEINLINE PVOID FrLdrHeapAlloc(SIZE_T MemorySize, ULONG Tag)
Definition: mm.h:174
struct _LDR_DATA_TABLE_ENTRY * PLDR_DATA_TABLE_ENTRY
Definition: bufpool.h:45
FORCEINLINE PVOID VaToPa(PVOID Va)
Definition: conversion.h:15
FORCEINLINE PVOID PaToVa(PVOID Pa)
Definition: conversion.h:22
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define RtlImageNtHeader
Definition: compat.h:806
#define InsertTailList(ListHead, Entry)
#define RVA(m, b)
Definition: freeldr.h:28
#define TAG_WLDR_DTE
Definition: winldr.h:13
#define TAG_WLDR_NAME
Definition: winldr.h:15
LIST_ENTRY * ModuleListHead
Definition: kdpacket.c:23
#define LDRP_IMAGE_INTEGRITY_FORCED
Definition: ldrtypes.h:41
#define LDRP_ENTRY_PROCESSED
Definition: ldrtypes.h:44
_In_ PCWSTR FullDllName
Definition: ldrtypes.h:247
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY
Definition: ntimage.h:456
unsigned short USHORT
Definition: pedump.c:61
#define TRACE(s)
Definition: solgame.cpp:4
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
Definition: btrfs_drv.h:1876
USHORT LoadCount
Definition: ntddk_ex.h:208
PVOID EntryPoint
Definition: ntddk_ex.h:203
UNICODE_STRING FullDllName
Definition: btrfs_drv.h:1882
ULONG SizeOfImage
Definition: ldrtypes.h:143
LIST_ENTRY InLoadOrderLinks
Definition: ldrtypes.h:138
PVOID DllBase
Definition: btrfs_drv.h:1880
ULONG Flags
Definition: ntddk_ex.h:207
PVOID SectionPointer
Definition: ntddk_ex.h:213
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:145
ULONG CheckSum
Definition: btrfs_drv.h:1886
USHORT MaximumLength
Definition: env_spec_w32.h:370
uint16_t * PWSTR
Definition: typedefs.h:56
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by LoadModule(), PeLdrInitializeModuleList(), PeLdrLoadBootImage(), PeLdrpLoadAndScanReferencedDll(), and WinLdrLoadDeviceDriver().

◆ PeLdrCheckForLoadedDll()

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

Definition at line 586 of file peloader.c.

590{
591 PLDR_DATA_TABLE_ENTRY DataTableEntry;
592 LIST_ENTRY *ModuleEntry;
593
594 TRACE("PeLdrCheckForLoadedDll: DllName %s\n", DllName);
595
596 /* Just go through each entry in the LoadOrderList and compare loaded module's
597 name with a given name */
598 ModuleEntry = ModuleListHead->Flink;
599 while (ModuleEntry != ModuleListHead)
600 {
601 /* Get pointer to the current DTE */
602 DataTableEntry = CONTAINING_RECORD(ModuleEntry,
604 InLoadOrderLinks);
605
606 TRACE("PeLdrCheckForLoadedDll: DTE %p, EP %p, base %p name '%.*ws'\n",
607 DataTableEntry, DataTableEntry->EntryPoint, DataTableEntry->DllBase,
608 DataTableEntry->BaseDllName.Length / 2, VaToPa(DataTableEntry->BaseDllName.Buffer));
609
610 /* Compare names */
611 if (PeLdrpCompareDllName(DllName, &DataTableEntry->BaseDllName))
612 {
613 /* Yes, found it, report pointer to the loaded module's DTE
614 to the caller and increase load count for it */
615 *LoadedEntry = DataTableEntry;
616 DataTableEntry->LoadCount++;
617 TRACE("PeLdrCheckForLoadedDll: LoadedEntry 0x%p\n", DataTableEntry);
618 return TRUE;
619 }
620
621 /* Go to the next entry */
622 ModuleEntry = ModuleEntry->Flink;
623 }
624
625 /* Nothing found */
626 return FALSE;
627}
static BOOLEAN PeLdrpCompareDllName(IN PCH DllName, IN PUNICODE_STRING UnicodeName)
Definition: peloader.c:79
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260

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

◆ PeLdrFreeDataTableEntry()

VOID PeLdrFreeDataTableEntry ( _In_ PLDR_DATA_TABLE_ENTRY  Entry)

Definition at line 823 of file peloader.c.

826{
827 // ASSERT(ModuleListHead);
828 ASSERT(Entry);
829
830 RemoveEntryList(&Entry->InLoadOrderLinks);
831 FrLdrHeapFree(VaToPa(Entry->FullDllName.Buffer), TAG_WLDR_NAME);
832 FrLdrHeapFree(VaToPa(Entry->BaseDllName.Buffer), TAG_WLDR_NAME);
834}
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
#define ASSERT(a)
Definition: mode.c:44
base of all file and directory entries
Definition: entries.h:83

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

◆ PeLdrInitializeModuleList()

BOOLEAN PeLdrInitializeModuleList ( VOID  )

Definition at line 524 of file peloader.c.

525{
526 PLDR_DATA_TABLE_ENTRY FreeldrDTE;
527
529
530 /* Allocate a data table entry for freeldr.sys.
531 The base name is scsiport.sys for imports from ntbootdd.sys */
533 "scsiport.sys",
534 "freeldr.sys",
536 &FreeldrDTE))
537 {
538 /* Cleanup and bail out */
539 ERR("Failed to allocate DTE for freeldr\n");
540 return FALSE;
541 }
542
543 return TRUE;
544}
#define ERR(fmt,...)
Definition: precomp.h:57
#define __ImageBase
Definition: crt_handler.c:22
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
BOOLEAN PeLdrAllocateDataTableEntry(IN OUT PLIST_ENTRY ModuleListHead, IN PCCH BaseDllName, IN PCCH FullDllName, IN PVOID BaseVA, OUT PLDR_DATA_TABLE_ENTRY *NewEntry)
Definition: peloader.c:706
LIST_ENTRY FrLdrModuleList
Definition: peloader.c:28

Referenced by BootMain().

◆ PeLdrInitSecurityCookie()

PVOID PeLdrInitSecurityCookie ( _In_ PLDR_DATA_TABLE_ENTRY  LdrEntry)

◆ PeLdrLoadBootImage()

BOOLEAN PeLdrLoadBootImage ( _In_ PCSTR  FilePath,
_In_ PCSTR  BaseDllName,
_Out_ PVOID ImageBase,
_Out_ PLDR_DATA_TABLE_ENTRY DataTableEntry 
)

Definition at line 1049 of file peloader.c.

1054{
1056
1057 /* Load the image as a bootloader image */
1060 ImageBase,
1061 FALSE);
1062 if (!Success)
1063 {
1064 WARN("Failed to load boot image '%s'\n", FilePath);
1065 return FALSE;
1066 }
1067
1068 /* Allocate a DTE */
1070 BaseDllName,
1071 FilePath,
1072 *ImageBase,
1073 DataTableEntry);
1074 if (!Success)
1075 {
1076 /* Cleanup and bail out */
1077 ERR("Failed to allocate DTE for '%s'\n", FilePath);
1078 MmFreeMemory(*ImageBase);
1079 return FALSE;
1080 }
1081
1082 /* Resolve imports */
1084 if (!Success)
1085 {
1086 /* Cleanup and bail out */
1087 ERR("Failed to resolve imports for '%s'\n", FilePath);
1088 PeLdrFreeDataTableEntry(*DataTableEntry);
1089 MmFreeMemory(*ImageBase);
1090 return FALSE;
1091 }
1092
1093 return TRUE;
1094}
PCWSTR FilePath
unsigned char BOOLEAN
#define WARN(fmt,...)
Definition: precomp.h:61
VOID MmFreeMemory(PVOID MemoryPointer)
Definition: mm.c:215
@ Success
Definition: eventcreate.c:712
BOOLEAN PeLdrScanImportDescriptorTable(IN OUT PLIST_ENTRY ModuleListHead, IN PCCH DirectoryPath, IN PLDR_DATA_TABLE_ENTRY ScanDTE)
Definition: peloader.c:630
VOID PeLdrFreeDataTableEntry(_In_ PLDR_DATA_TABLE_ENTRY Entry)
Definition: peloader.c:823
BOOLEAN PeLdrLoadImageEx(_In_ PCSTR FilePath, _In_ TYPE_OF_MEMORY MemoryType, _Out_ PVOID *ImageBasePA, _In_ BOOLEAN KernelMapping)
Loads the specified image from the file.
Definition: peloader.c:848
@ LoaderLoadedProgram
Definition: arc.h:178

Referenced by LoadBootDeviceDriver().

◆ PeLdrLoadImage()

BOOLEAN PeLdrLoadImage ( _In_ PCSTR  FilePath,
_In_ TYPE_OF_MEMORY  MemoryType,
_Out_ PVOID ImageBasePA 
)

Definition at line 1040 of file peloader.c.

1044{
1045 return PeLdrLoadImageEx(FilePath, MemoryType, ImageBasePA, TRUE);
1046}

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

◆ PeLdrLoadImageEx()

BOOLEAN PeLdrLoadImageEx ( _In_ PCSTR  FilePath,
_In_ TYPE_OF_MEMORY  MemoryType,
_Out_ PVOID ImageBasePA,
_In_ BOOLEAN  KernelMapping 
)

Loads the specified image from the file.

PeLdrLoadImage doesn't perform any additional operations on the file path, it just directly calls the file I/O routines. It then relocates the image so that it's ready to be used when paging is enabled.

Note
Addressing mode: physical.

Definition at line 848 of file peloader.c.

853{
854 ULONG FileId;
855 PVOID PhysicalBase;
856 PVOID VirtualBase = NULL;
857 UCHAR HeadersBuffer[SECTOR_SIZE * 2];
858 PIMAGE_NT_HEADERS NtHeaders;
859 PIMAGE_SECTION_HEADER SectionHeader;
860 ULONG VirtualSize, SizeOfRawData, NumberOfSections;
864
865 TRACE("PeLdrLoadImage('%s', %ld)\n", FilePath, MemoryType);
866
867 /* Open the image file */
869 if (Status != ESUCCESS)
870 {
871 WARN("ArcOpen('%s') failed. Status: %u\n", FilePath, Status);
872 return FALSE;
873 }
874
875 /* Load the first 2 sectors of the image so we can read the PE header */
876 Status = ArcRead(FileId, HeadersBuffer, SECTOR_SIZE * 2, &BytesRead);
877 if (Status != ESUCCESS)
878 {
879 ERR("ArcRead('%s') failed. Status: %u\n", FilePath, Status);
880 ArcClose(FileId);
881 return FALSE;
882 }
883
884 /* Now read the MZ header to get the offset to the PE Header */
885 NtHeaders = RtlImageNtHeader(HeadersBuffer);
886 if (!NtHeaders)
887 {
888 ERR("No NT header found in \"%s\"\n", FilePath);
889 ArcClose(FileId);
890 return FALSE;
891 }
892
893 /* Ensure this is executable image */
895 {
896 ERR("Not an executable image \"%s\"\n", FilePath);
897 ArcClose(FileId);
898 return FALSE;
899 }
900
901 /* Store number of sections to read and a pointer to the first section */
902 NumberOfSections = NtHeaders->FileHeader.NumberOfSections;
903 SectionHeader = IMAGE_FIRST_SECTION(NtHeaders);
904
905 /* Try to allocate this memory; if it fails, allocate somewhere else */
906 PhysicalBase = MmAllocateMemoryAtAddress(NtHeaders->OptionalHeader.SizeOfImage,
907 (PVOID)((ULONG)NtHeaders->OptionalHeader.ImageBase & (KSEG0_BASE - 1)),
908 MemoryType);
909
910 if (PhysicalBase == NULL)
911 {
912 /* Don't fail, allocate again at any other "low" place */
913 PhysicalBase = MmAllocateMemoryWithType(NtHeaders->OptionalHeader.SizeOfImage, MemoryType);
914
915 if (PhysicalBase == NULL)
916 {
917 ERR("Failed to alloc %lu bytes for image %s\n", NtHeaders->OptionalHeader.SizeOfImage, FilePath);
918 ArcClose(FileId);
919 return FALSE;
920 }
921 }
922
923 /* This is the real image base, in form of a virtual address */
924 VirtualBase = KernelMapping ? PaToVa(PhysicalBase) : PhysicalBase;
925
926 TRACE("Base PA: 0x%p, VA: 0x%p\n", PhysicalBase, VirtualBase);
927
928 /* Copy headers from already read data */
929 RtlCopyMemory(PhysicalBase, HeadersBuffer, min(NtHeaders->OptionalHeader.SizeOfHeaders, sizeof(HeadersBuffer)));
930 /* If headers are quite big, request next bytes from file */
931 if (NtHeaders->OptionalHeader.SizeOfHeaders > sizeof(HeadersBuffer))
932 {
933 Status = ArcRead(FileId, (PUCHAR)PhysicalBase + sizeof(HeadersBuffer), NtHeaders->OptionalHeader.SizeOfHeaders - sizeof(HeadersBuffer), &BytesRead);
934 if (Status != ESUCCESS)
935 {
936 ERR("ArcRead('%s') failed. Status: %u\n", FilePath, Status);
937 // UiMessageBox("Error reading headers.");
938 ArcClose(FileId);
939 goto Failure;
940 }
941 }
942
943 /*
944 * On Vista and above, a digital signature check is performed when the image
945 * has the IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY flag set in its header.
946 * (We of course do not perform this check yet!)
947 */
948
949 /* Reload the NT Header */
950 NtHeaders = RtlImageNtHeader(PhysicalBase);
951
952 /* Load the first section */
953 SectionHeader = IMAGE_FIRST_SECTION(NtHeaders);
954
955 /* Walk through each section and read it (check/fix any possible
956 bad situations, if they arise) */
957 for (i = 0; i < NumberOfSections; i++)
958 {
959 VirtualSize = SectionHeader->Misc.VirtualSize;
960 SizeOfRawData = SectionHeader->SizeOfRawData;
961
962 /* Handle a case when VirtualSize equals 0 */
963 if (VirtualSize == 0)
964 VirtualSize = SizeOfRawData;
965
966 /* If PointerToRawData is 0, then force its size to be also 0 */
967 if (SectionHeader->PointerToRawData == 0)
968 {
969 SizeOfRawData = 0;
970 }
971 else
972 {
973 /* Cut the loaded size to the VirtualSize extents */
974 if (SizeOfRawData > VirtualSize)
975 SizeOfRawData = VirtualSize;
976 }
977
978 /* Actually read the section (if its size is not 0) */
979 if (SizeOfRawData != 0)
980 {
981 /* Seek to the correct position */
982 Position.QuadPart = SectionHeader->PointerToRawData;
983 Status = ArcSeek(FileId, &Position, SeekAbsolute);
984
985 TRACE("SH->VA: 0x%X\n", SectionHeader->VirtualAddress);
986
987 /* Read this section from the file, size = SizeOfRawData */
988 Status = ArcRead(FileId, (PUCHAR)PhysicalBase + SectionHeader->VirtualAddress, SizeOfRawData, &BytesRead);
989 if (Status != ESUCCESS)
990 {
991 ERR("PeLdrLoadImage(): Error reading section from file!\n");
992 break;
993 }
994 }
995
996 /* Size of data is less than the virtual size: fill up the remainder with zeroes */
997 if (SizeOfRawData < VirtualSize)
998 {
999 TRACE("PeLdrLoadImage(): SORD %d < VS %d\n", SizeOfRawData, VirtualSize);
1000 RtlZeroMemory((PVOID)(SectionHeader->VirtualAddress + (ULONG_PTR)PhysicalBase + SizeOfRawData), VirtualSize - SizeOfRawData);
1001 }
1002
1003 SectionHeader++;
1004 }
1005
1006 /* We are done with the file, close it */
1007 ArcClose(FileId);
1008
1009 /* If loading failed, return right now */
1010 if (Status != ESUCCESS)
1011 goto Failure;
1012
1013 /* Relocate the image, if it needs it */
1014 if (NtHeaders->OptionalHeader.ImageBase != (ULONG_PTR)VirtualBase)
1015 {
1016 WARN("Relocating %p -> %p\n", NtHeaders->OptionalHeader.ImageBase, VirtualBase);
1017 Status = LdrRelocateImageWithBias(PhysicalBase,
1018 (ULONG_PTR)VirtualBase - (ULONG_PTR)PhysicalBase,
1019 "FreeLdr",
1020 ESUCCESS,
1021 ESUCCESS, /* In case of conflict still return success */
1022 ENOEXEC);
1023 if (Status != ESUCCESS)
1024 goto Failure;
1025 }
1026
1027 /* Fill output parameters */
1028 *ImageBasePA = PhysicalBase;
1029
1030 TRACE("PeLdrLoadImage() done, PA = %p\n", *ImageBasePA);
1031 return TRUE;
1032
1033Failure:
1034 /* Cleanup and bail out */
1035 MmFreeMemory(PhysicalBase);
1036 return FALSE;
1037}
ARC_STATUS ArcSeek(ULONG FileId, LARGE_INTEGER *Position, SEEKMODE SeekMode)
Definition: fs.c:455
ARC_STATUS ArcOpen(CHAR *Path, OPENMODE OpenMode, ULONG *FileId)
Definition: fs.c:219
ARC_STATUS ArcClose(_In_ ULONG FileId)
Definition: fs.c:409
#define SECTOR_SIZE
Definition: fs.h:22
ARC_STATUS ArcRead(ULONG FileId, VOID *Buffer, ULONG N, ULONG *Count)
Definition: fs.c:448
PVOID MmAllocateMemoryAtAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
Definition: mm.c:85
PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType)
Definition: mm.c:31
#define ENOEXEC
Definition: errno.h:14
Status
Definition: gdiplustypes.h:25
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 min(a, b)
Definition: monoChain.cc:55
#define KSEG0_BASE
Definition: ketypes.h:343
ULONG NTAPI LdrRelocateImageWithBias(_In_ PVOID BaseAddress, _In_ LONGLONG AdditionalBias, _In_opt_ PCSTR LoaderName, _In_ ULONG Success, _In_ ULONG Conflict, _In_ ULONG Invalid)
Definition: image.c:474
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:427
#define IMAGE_FILE_EXECUTABLE_IMAGE
Definition: pedump.c:160
@ ESUCCESS
Definition: arc.h:32
ULONG ARC_STATUS
Definition: arc.h:4
@ SeekAbsolute
Definition: arc.h:59
@ OpenReadOnly
Definition: arc.h:65
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
union _IMAGE_SECTION_HEADER::@1572 Misc
DWORD PointerToRawData
Definition: pedump.c:290
static COORD Position
Definition: mouse.c:34
char * PSTR
Definition: typedefs.h:51
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG_PTR
Definition: typedefs.h:65
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PLONGLONG _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesRead
Definition: wdfiotarget.h:870
unsigned char UCHAR
Definition: xmlstorage.h:181

Referenced by PeLdrLoadBootImage(), and PeLdrLoadImage().

◆ PeLdrScanImportDescriptorTable()

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

Definition at line 630 of file peloader.c.

634{
635 PLDR_DATA_TABLE_ENTRY DataTableEntry;
636 PIMAGE_IMPORT_DESCRIPTOR ImportTable;
637 ULONG ImportTableSize;
638 PCH ImportName;
640
641 /* Get a pointer to the import table of this image */
642 ImportTable = (PIMAGE_IMPORT_DESCRIPTOR)RtlImageDirectoryEntryToData(VaToPa(ScanDTE->DllBase),
643 TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &ImportTableSize);
644
645#if DBG
646 {
647 UNICODE_STRING BaseName;
648 BaseName.Buffer = VaToPa(ScanDTE->BaseDllName.Buffer);
649 BaseName.MaximumLength = ScanDTE->BaseDllName.MaximumLength;
650 BaseName.Length = ScanDTE->BaseDllName.Length;
651 TRACE("PeLdrScanImportDescriptorTable(): %wZ ImportTable = 0x%p\n",
652 &BaseName, ImportTable);
653 }
654#endif
655
656 /* If image doesn't have any import directory - just return success */
657 if (ImportTable == NULL)
658 return TRUE;
659
660 /* Loop through all entries */
661 for (;(ImportTable->Name != 0) && (ImportTable->FirstThunk != 0);ImportTable++)
662 {
663 /* Get pointer to the name */
664 ImportName = (PCH)VaToPa(RVA(ScanDTE->DllBase, ImportTable->Name));
665 TRACE("PeLdrScanImportDescriptorTable(): Looking at %s\n", ImportName);
666
667 /* In case we get a reference to ourselves - just skip it */
668 if (PeLdrpCompareDllName(ImportName, &ScanDTE->BaseDllName))
669 continue;
670
671 /* Load the DLL if it is not already loaded */
672 if (!PeLdrCheckForLoadedDll(ModuleListHead, ImportName, &DataTableEntry))
673 {
675 DirectoryPath,
676 ImportName,
677 &ScanDTE->InLoadOrderLinks,
678 &DataTableEntry);
679 if (!Success)
680 {
681 ERR("PeLdrpLoadAndScanReferencedDll() failed\n");
682 return Success;
683 }
684 }
685
686 /* Scan its import address table */
688 DataTableEntry->DllBase,
689 ScanDTE->DllBase,
690 (PIMAGE_THUNK_DATA)RVA(ScanDTE->DllBase, ImportTable->FirstThunk),
691 DirectoryPath,
692 &ScanDTE->InLoadOrderLinks);
693
694 if (!Success)
695 {
696 ERR("PeLdrpScanImportAddressTable() failed: ImportName = '%s', DirectoryPath = '%s'\n",
697 ImportName, DirectoryPath);
698 return Success;
699 }
700 }
701
702 return TRUE;
703}
DWORD RVA
Definition: compat.h:1262
#define RtlImageDirectoryEntryToData
Definition: compat.h:809
CHAR * PCH
Definition: ntbasedef.h:399
struct _IMAGE_IMPORT_DESCRIPTOR * PIMAGE_IMPORT_DESCRIPTOR
#define IMAGE_DIRECTORY_ENTRY_IMPORT
Definition: pedump.c:260
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:455
BOOLEAN PeLdrCheckForLoadedDll(IN OUT PLIST_ENTRY ModuleListHead, IN PCH DllName, OUT PLDR_DATA_TABLE_ENTRY *LoadedEntry)
Definition: peloader.c:586
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:391

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

Variable Documentation

◆ PeLdrImportDllLoadCallback

PELDR_IMPORTDLL_LOAD_CALLBACK PeLdrImportDllLoadCallback
extern

Definition at line 30 of file peloader.c.

Referenced by LoadAndBootWindowsCommon(), and PeLdrpLoadAndScanReferencedDll().