Changed kernel32 to compile as a dll

svn path=/trunk/; revision=338
This commit is contained in:
David Welch 1999-03-24 11:07:33 +00:00
parent 00f2fc414e
commit 3f0a7424cb
20 changed files with 1182 additions and 1814 deletions

View file

@ -10,8 +10,6 @@ int mainCRTStartup(PWSTR args)
{
int nRet;
KERNEL32_Init(args);
// SetUnhandledExceptionFilter(NULL);
// _fpreset();

View file

@ -1,11 +1,10 @@
all: args.bin
all: args.exe
OBJECTS= ../common/crt0.o args.o
LIBS= ../../lib/kernel32/kernel32.a ../../lib/ntdll/ntdll.a
args.bin: $(OBJECTS) $(LIBS)
$(CC) -specs=../../specs -Ttext 0x10000 $(OBJECTS) $(LIBS) -lgcc \
-o args.exe
args.exe: $(OBJECTS) $(LIBS)
$(CC) -specs=../../specs $(OBJECTS) $(LIBS) -lgcc -o args.exe
$(NM) --numeric-sort args.exe > args.sym
../../ntoskrnl/utils/pe2bin/pe2bin$(EXE_POSTFIX) 0x10000 args.exe \
args.bin
include ../../rules.mak

View file

@ -1,10 +1,7 @@
#include <ddk/ntddk.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
void main()
int main(int argc, char* argv[])
{
NtDisplayString("Hello world\n");
ExitProcess(0);
printf("Hello world\n");
return(0);
}

View file

@ -1,9 +1,8 @@
all: hello.bin
all: hello.exe
OBJECTS = ../common/crt0.o hello.o
OBJECTS = hello.o
hello.bin: $(OBJECTS)
$(LD) -Ttext 0x10000 $(OBJECTS) ../../lib/kernel32/kernel32.a ../../lib/ntdll/ntdll.a -o hello.exe
$(OBJCOPY) -O binary hello.exe hello.bin
hello.exe: $(OBJECTS) $(LIBS)
$(CC) $(OBJECTS) -o hello.exe
include ../../rules.mak

View file

@ -2,15 +2,14 @@ all: ide.sys
OBJECTS = ide.o ../../../ntoskrnl/ntoskrnl.a
# --def ide.def --def ide.def
ide.sys: $(OBJECTS)
$(DLLTOOL) --dllname ide.sys --output-lib ide.a
$(CC) -specs=../../svc_specs -mdll -o junk.tmp -Wl,--defsym,_end=end \
-Wl,--defsym,_edata=__data_end__ -Wl,--defsym,_etext=etext \
-Wl,--base-file,base.tmp $(OBJECTS)
- $(RM) junk.tmp
$(DLLTOOL) --dllname ide.sys --base-file base.tmp \
--output-exp temp.exp
--output-exp temp.exp
- $(RM) base.tmp
$(CC) --verbose -Wl,--image-base,0x10000 -Wl,-e,_DriverEntry@8 -specs=../../svc_specs -mdll -o ide.sys $(OBJECTS) -Wl,temp.exp
$(CC) --verbose -Wl,--image-base,0x10000 -Wl,-e,_DriverEntry@8 \
-specs=../../svc_specs -mdll -o ide.sys $(OBJECTS) -Wl,temp.exp
- $(RM) temp.exp

View file

@ -325,6 +325,15 @@ DWORD RtlNtStatusToDosError(NTSTATUS StatusCode);
BOOL WINAPI RtlDestroyHeap(HANDLE hheap);
LPVOID STDCALL RtlReAllocHeap(HANDLE hheap, DWORD flags, LPVOID ptr,
DWORD size);
HANDLE WINAPI RtlGetProcessHeap(VOID);
BOOL WINAPI RtlLockHeap(HANDLE hheap);
BOOL WINAPI RtlUnlockHeap(HANDLE hheap);
UINT RtlCompactHeap(HANDLE hheap, DWORD flags);
DWORD WINAPI RtlSizeHeap(HANDLE hheap, DWORD flags, PVOID pmem);
BOOL WINAPI RtlValidateHeap(HANDLE hheap, DWORD flags, PVOID pmem);
#endif /* __DDK_RTL_H */

View file

@ -0,0 +1,78 @@
*asm:
*asm_final:
*cpp:
-remap %(cpp_cpu) %{posix:-D_POSIX_SOURCE}
*cc1:
%(cc1_spec)
*cc1plus:
*endfile:
*link:
%{mwindows:--subsystem windows} %{mdll:--dll -e _DllMainCRTStartup@12}
*lib:
*libgcc:
-lgcc
*startfile:
*switches_need_spaces:
*signed_char:
%{funsigned-char:-D__CHAR_UNSIGNED__}
*predefines:
-Di386 -D_WIN32 -DWIN32 -D__WIN32__ -D__MINGW32__ -DWINNT -D_X86_=1 -D__STDC__=1 -D__stdcall=__attribute__((__stdcall__)) _D_stdcall=__attribute__((__stdcall__)) -D__cdecl=__attribute__((__cdecl__)) -D__declspec(x)=__attribute__((x)) -Asystem(winnt) -Acpu(i386) -Amachine(i386)
*cross_compile:
1
*version:
egcs-2.91.57
*multilib:
. ;
*multilib_defaults:
*multilib_extra:
*multilib_matches:
*linker:
collect2
*cpp_486:
%{!ansi:-Di486} -D__i486 -D__i486__
*cpp_586:
%{!ansi:-Di586 -Dpentium} -D__i586 -D__i586__ -D__pentium -D__pentium__
*cpp_686:
%{!ansi:-Di686 -Dpentiumpro} -D__i686 -D__i686__ -D__pentiumpro -D__pentiumpro__
*cpp_cpu_default:
%(cpp_586)
*cpp_cpu:
-Acpu(i386) -Amachine(i386) %{!ansi:-Di386} -D__i386 -D__i386__ %{mcpu=i486:%(cpp_486)} %{m486:%(cpp_486)} %{mpentium:%(cpp_586)} %{mcpu=pentium:%(cpp_586)} %{mpentiumpro:%(cpp_686)} %{mcpu=pentiumpro:%(cpp_686)} %{!mcpu*:%{!m486:%{!mpentium*:%(cpp_cpu_default)}}}
*cc1_cpu:
%{!mcpu*: %{m386:-mcpu=i386 -march=i386} %{mno-486:-mcpu=i386 -march=i386} %{m486:-mcpu=i486 -march=i486} %{mno-386:-mcpu=i486 -march=i486} %{mno-pentium:-mcpu=i486 -march=i486} %{mpentium:-mcpu=pentium} %{mno-pentiumpro:-mcpu=pentium} %{mpentiumpro:-mcpu=pentiumpro}}

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,15 @@
all: kernel32.a
ifneq ($(HOST),mingw32-windows)
ifneq ($(HOST),mingw32-linux)
DLLTARGET=kernel32.a
else
DLLTARGET=kernel32.dll
endif
else
DLLTARGET=kernel32.dll
endif
all: $(DLLTARGET)
SYNCH_OBJECTS = synch/critical.o synch/event.o synch/wait.o
@ -19,7 +30,7 @@ PROCESS_OBJECTS = process/proc.o process/cmdline.o
STRING_OBJECTS = string/lstring.o
INTERNAL_OBJECTS = internal/dprintf.o internal/init.o internal/string.o
INTERNAL_OBJECTS = internal/dprintf.o internal/string.o
EXCEPT_OBJECTS = except/except.o
@ -27,11 +38,26 @@ EXCEPT_OBJECTS = except/except.o
OBJECTS = $(MISC_OBJECTS) $(FILE_OBJECTS) $(THREAD_OBJECTS) \
$(PROCESS_OBJECTS) $(STRING_OBJECTS) $(MEM_OBJECTS) $(NLS_OBJECTS) \
$(INTERNAL_OBJECTS) $(SYNCH_OBJECTS) $(EXCEPT_OBJECTS)
kernel32.a: $(OBJECTS)
$(AR) rcs kernel32.a $(OBJECTS)
$(AR) csr kernel32.a $(OBJECTS)
dummy:
kernel32.dll: $(DLLMAIN) $(OBJECTS) kernel32.def
$(LD) -r $(OBJECTS) -o kernel32.o
$(DLLTOOL) --dllname kernel32.dll --def kernel32.def \
--output-lib kernel32.a
$(CC) -specs=k32_specs -mdll -o junk.tmp \
-Wl,--base-file,base.tmp kernel32.o ../ntdll/ntdll.a
- $(RM) junk.tmp
$(DLLTOOL) --dllname kernel32.dll --base-file base.tmp \
--output-exp temp.exp --def kernel32.def
- $(RM) base.tmp
$(CC) -specs=k32_specs -mdll -o kernel32.dll kernel32.o ../ntdll/ntdll.a\
-Wl,--image-base,0x70000000 \
-Wl,--file-alignment,0x1000 \
-Wl,--section-alignment,0x1000 \
-Wl,temp.exp
- $(RM) temp.exp
$(NM) --numeric-sort kernel32.dll > kernel32.sym
include ../../rules.mak

File diff suppressed because it is too large Load diff

View file

@ -13,22 +13,19 @@
#include <wchar.h>
#include <kernel32/proc.h>
WINBOOL
STDCALL
DllMain (
HANDLE hInst,
ULONG ul_reason_for_call,
LPVOID lpReserved );
WINBOOL STDCALL DllMain (HANDLE hInst,
ULONG ul_reason_for_call,
LPVOID lpReserved);
NT_TEB *Teb;
BOOL WINAPI DllMainCRTStartup(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
{
return(TRUE);
}
WINBOOL STDCALL DllMain (HANDLE hInst,
ULONG ul_reason_for_call,

View file

@ -0,0 +1,45 @@
ifneq ($(HOST),mingw32-windows)
ifneq ($(HOST),mingw32-linux)
DLLTARGET=ntdll.a
DLLMAIN=
else
DLLTARGET=ntdll.dll
DLLMAIN=main/dllmain.o
endif
else
DLLTARGET=ntdll.dll
DLLMAIN=main/dllmain.o
endif
all: $(DLLTARGET)
OBJECTS = napi.o ldr/startup.o rtl/largeint.o rtl/namespc.o rtl/unicode.o \
stdio/vsprintf.o string/ctype.o string/memcpy.o string/memset.o \
string/strcat.o string/strcmp.o string/strcpy.o string/stricmp.o \
string/strlen.o string/strncmp.o string/strncpy.o string/strnlen.o \
string/strrchr.o string/wstring.o stubs/stubs.o rtl/heap.o \
rtl/critical.o rtl/mem.o
ntdll.a: $(OBJECTS)
$(AR) csr ntdll.a $(OBJECTS)
ntdll.dll: $(DLLMAIN) $(OBJECTS) def/ntdll.def
$(LD) -r $(DLLMAIN) $(OBJECTS) -o ntdll.o
$(DLLTOOL) --dllname ntdll.dll --def def/ntdll.def \
--output-lib ntdll.a
$(CC) -specs=ntdll_specs -mdll -o junk.tmp \
-Wl,--base-file,base.tmp ntdll.o
- $(RM) junk.tmp
$(DLLTOOL) --dllname ntdll.dll --base-file base.tmp \
--output-exp temp.exp --def def/ntdll.def
- $(RM) base.tmp
$(CC) -specs=ntdll_specs -mdll -o ntdll.dll ntdll.o \
-Wl,--entry=_LdrStartup \
-Wl,--image-base,0x80000000 \
-Wl,--file-alignment,0x1000 \
-Wl,--section-alignment,0x1000 \
-Wl,temp.exp
- $(RM) temp.exp
$(NM) --numeric-sort ntdll.dll > ntdll.sym
include ../../rules.mak

View file

@ -420,6 +420,17 @@ ZwReplyWaitSendChannel@12
ZwSendWaitReplyChannel@16
ZwSetContextChannel@4
ZwYieldExecution@0
RtlAllocateHeap@12
RtlCreateHeap@24
RtlCompactHeap
RtlDestroyHeap@4
RtlFreeHeap@12
RtlGetProcessHeap@0
RtlLockHeap@4
RtlReAllocHeap@16
RtlSizeHeap@12
RtlUnlockHeap@4
RtlValidateHeap@12
RtlInitAnsiString
RtlInitUnicodeString
RtlLargeIntegerDivide
@ -428,6 +439,7 @@ RtlEnlargedIntegerMultiply
RtlEnlargedUnsignedMultiply
RtlExtendedIntegerMultiply
isalpha
iswlower
memcpy
memset
strcat
@ -438,6 +450,7 @@ strncpy
strlen
strrchr
toupper
towupper
wcscpy
wcschr
wcscat

View file

@ -17,14 +17,14 @@
#include <internal/string.h>
#include <wchar.h>
#define NDEBUG
//#define NDEBUG
#include <ntdll/ntdll.h>
VOID WINAPI __RtlInitHeap(LPVOID base, ULONG minsize, ULONG maxsize);
PVOID WINAPI __RtlInitHeap(LPVOID base, ULONG minsize, ULONG maxsize);
/* MACROS ********************************************************************/
#define RVA(m, b) ((ULONG)b + m->BaseAddress)
#define RVA(m, b) ((ULONG)b + m)
/* TYPEDEFS ******************************************************************/
@ -42,6 +42,8 @@ typedef struct _DLL
static DLL DllListHead;
#define HEAP_BASE (0xa0000000)
/* FORWARD DECLARATIONS ******************************************************/
static PEPFUNC LdrPEStartup(DWORD ImageBase, HANDLE SectionHandle);
@ -83,12 +85,126 @@ static NTSTATUS LdrMapSections(PVOID ImageBase, HANDLE SectionHandle,
}
static NTSTATUS LdrLoadDll(PDLL* Base, PCHAR Name)
{
char fqname[255] = "\\??\\C:\\reactos\\system\\";
ANSI_STRING AnsiString;
UNICODE_STRING UnicodeString;
OBJECT_ATTRIBUTES FileObjectAttributes;
char BlockBuffer[1024];
PIMAGE_DOS_HEADER DosHeader;
NTSTATUS Status;
PIMAGE_NT_HEADERS NTHeaders;
PEPFUNC DllStartupAddr;
ULONG ImageBase, ImageSize, InitialViewSize;
HANDLE FileHandle, SectionHandle;
PDLL DllDesc;
DPRINT("LdrLoadDll(Base %x, Name %s)\n",Base,Name);
strcat(fqname, Name);
DPRINT("fqname %s\n",fqname);
RtlInitAnsiString(&AnsiString,fqname);
RtlAnsiStringToUnicodeString(&UnicodeString,&AnsiString,TRUE);
InitializeObjectAttributes(&FileObjectAttributes,
&UnicodeString,
0,
NULL,
NULL);
DPRINT("Opening dll\n");
Status = ZwOpenFile(&FileHandle, FILE_ALL_ACCESS, &FileObjectAttributes,
NULL, 0, 0);
if (!NT_SUCCESS(Status))
{
DPRINT("Dll open failed ");
return Status;
}
Status = ZwReadFile(FileHandle, 0, 0, 0, 0, BlockBuffer, 1024, 0, 0);
if (!NT_SUCCESS(Status))
{
DPRINT("Dll header read failed ");
ZwClose(FileHandle);
return Status;
}
DosHeader = (PIMAGE_DOS_HEADER) BlockBuffer;
if (DosHeader->e_magic != IMAGE_DOS_MAGIC ||
DosHeader->e_lfanew == 0L ||
*(PULONG)((PUCHAR)BlockBuffer + DosHeader->e_lfanew) != IMAGE_PE_MAGIC)
{
DPRINT("NTDLL format invalid\n");
ZwClose(FileHandle);
return STATUS_UNSUCCESSFUL;
}
NTHeaders = (PIMAGE_NT_HEADERS)(BlockBuffer + DosHeader->e_lfanew);
ImageBase = NTHeaders->OptionalHeader.ImageBase;
ImageSize = NTHeaders->OptionalHeader.SizeOfImage;
DPRINT("ImageBase %x\n",ImageBase);
DllStartupAddr = ImageBase + NTHeaders->OptionalHeader.AddressOfEntryPoint;
/* Create a section for NTDLL */
Status = ZwCreateSection(&SectionHandle,
SECTION_ALL_ACCESS,
NULL,
NULL,
PAGE_READWRITE,
MEM_COMMIT,
FileHandle);
if (!NT_SUCCESS(Status))
{
DPRINT("NTDLL create section failed ");
ZwClose(FileHandle);
return Status;
}
/* Map the NTDLL into the process */
InitialViewSize = DosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS)
+ sizeof(IMAGE_SECTION_HEADER) * NTHeaders->FileHeader.NumberOfSections;
Status = ZwMapViewOfSection(SectionHandle,
NtCurrentProcess(),
(PVOID *)&ImageBase,
0,
InitialViewSize,
NULL,
&InitialViewSize,
0,
MEM_COMMIT,
PAGE_READWRITE);
if (!NT_SUCCESS(Status))
{
DPRINT("NTDLL map view of secion failed ");
ZwClose(FileHandle);
return Status;
}
ZwClose(FileHandle);
DllDesc = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DLL));
DllDesc->Headers = NTHeaders;
DllDesc->BaseAddress = ImageBase;
DllDesc->Next = DllListHead.Next;
DllDesc->Prev = &DllListHead;
DllListHead.Next->Prev = DllDesc;
DllListHead.Next = DllDesc;
LdrPEStartup(ImageBase, SectionHandle);
*Base = DllDesc;
return(STATUS_SUCCESS);
}
static NTSTATUS LdrFindDll(PDLL* Base, PCHAR Name)
{
PIMAGE_EXPORT_DIRECTORY ExportDir;
DLL* current;
PIMAGE_OPTIONAL_HEADER OptionalHeader;
DPRINT("LdrLoadDll(Name %s)\n",Name);
DPRINT("LdrFindDll(Name %s)\n",Name);
current = &DllListHead;
do
@ -98,8 +214,6 @@ static NTSTATUS LdrLoadDll(PDLL* Base, PCHAR Name)
IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
ExportDir = ((ULONG)ExportDir + (ULONG)current->BaseAddress);
DPRINT("ExportDir %x\n",ExportDir);
DPRINT("Scanning %x\n",ExportDir->Name);
DPRINT("Scanning %s\n",ExportDir->Name + current->BaseAddress);
if (strcmp(ExportDir->Name + current->BaseAddress, Name) == 0)
{
@ -110,11 +224,11 @@ static NTSTATUS LdrLoadDll(PDLL* Base, PCHAR Name)
current = current->Next;
} while (current != &DllListHead);
return(STATUS_UNSUCCESSFUL);
DPRINT("Failed to find dll %s\n",Name);
return(LdrLoadDll(Base, Name));
}
#define HEAP_BASE (0xa0000000)
/* LdrStartup
* FUNCTION:
* Handles Process Startup Activities.
@ -125,7 +239,6 @@ VOID LdrStartup(HANDLE SectionHandle, DWORD ImageBase)
{
PEPFUNC EntryPoint;
PIMAGE_DOS_HEADER PEDosHeader;
char buffer[512];
NTSTATUS Status;
PIMAGE_NT_HEADERS NTHeaders;
@ -161,12 +274,26 @@ VOID LdrStartup(HANDLE SectionHandle, DWORD ImageBase)
ZwTerminateProcess(NULL,STATUS_UNSUCCESSFUL);
}
DPRINT("Transferring control to image\n");
DPRINT("Transferring control to image at %x\n",EntryPoint);
Status = EntryPoint();
ZwTerminateProcess(NtCurrentProcess(),Status);
}
static PVOID LdrGetExport(PDLL Module, PUCHAR SymbolName)
static PVOID LdrGetExportByOrdinal(PDLL Module, ULONG Ordinal)
{
PIMAGE_EXPORT_DIRECTORY ExportDir;
USHORT* ExOrdinals;
ExportDir = (Module->BaseAddress +
(Module->Headers->OptionalHeader.
DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress));
ExOrdinals = (USHORT*)RVA(Module->BaseAddress,
ExportDir->AddressOfNameOrdinals);
return(ExOrdinals[Ordinal - ExportDir->Base]);
}
static PVOID LdrGetExportByName(PDLL Module, PUCHAR SymbolName)
{
PIMAGE_EXPORT_DIRECTORY ExportDir;
PDWORD* ExFunctions;
@ -184,196 +311,200 @@ static PVOID LdrGetExport(PDLL Module, PUCHAR SymbolName)
DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress));
/* Get header pointers */
ExNames = (PDWORD*)RVA(Module, ExportDir->AddressOfNames);
ExOrdinals = (USHORT*)RVA(Module, ExportDir->AddressOfNameOrdinals);
ExFunctions = (PDWORD*)RVA(Module, ExportDir->AddressOfFunctions);
ExNames = (PDWORD*)RVA(Module->BaseAddress, ExportDir->AddressOfNames);
ExOrdinals = (USHORT*)RVA(Module->BaseAddress,
ExportDir->AddressOfNameOrdinals);
ExFunctions = (PDWORD*)RVA(Module->BaseAddress,
ExportDir->AddressOfFunctions);
for (i=0; i<ExportDir->NumberOfFunctions; i++)
{
ExName = RVA(Module, ExNames[i]);
ExName = RVA(Module->BaseAddress, ExNames[i]);
if (strcmp(ExName,SymbolName) == 0)
{
Ordinal = ExOrdinals[i];
return(RVA(Module, ExFunctions[Ordinal]));
return(RVA(Module->BaseAddress, ExFunctions[Ordinal]));
}
}
return(NULL);
}
static PEPFUNC LdrPEStartup(DWORD ImageBase, HANDLE SectionHandle)
static NTSTATUS LdrPerformRelocations(PIMAGE_NT_HEADERS NTHeaders,
DWORD ImageBase)
{
int i;
PVOID SectionBase;
NTSTATUS Status;
PEPFUNC EntryPoint;
PIMAGE_DOS_HEADER DosHeader;
PIMAGE_NT_HEADERS NTHeaders;
PIMAGE_SECTION_HEADER SectionList;
char buffer[512];
PDLL Module;
USHORT NumberOfEntries;
PUSHORT pValue16;
ULONG RelocationRVA;
ULONG Delta32, Offset;
PULONG pValue32;
PRELOCATION_DIRECTORY RelocationDir;
PRELOCATION_ENTRY RelocationBlock;
int i;
DosHeader = (PIMAGE_DOS_HEADER) ImageBase;
NTHeaders = (PIMAGE_NT_HEADERS)(ImageBase + DosHeader->e_lfanew);
SectionList = (PIMAGE_SECTION_HEADER) (ImageBase + DosHeader->e_lfanew +
sizeof(ULONG) + sizeof(IMAGE_FILE_HEADER) + sizeof(IMAGE_OPTIONAL_HEADER));
/* Initialize Image sections */
LdrMapSections(ImageBase, SectionHandle, NTHeaders);
/* FIXME: if actual load address is different from ImageBase, then reloc */
if (ImageBase != (DWORD) NTHeaders->OptionalHeader.ImageBase)
{
USHORT NumberOfEntries;
PUSHORT pValue16;
ULONG RelocationRVA;
ULONG Delta32, Offset;
PULONG pValue32;
PRELOCATION_DIRECTORY RelocationDir;
PRELOCATION_ENTRY RelocationBlock;
RelocationRVA = NTHeaders->OptionalHeader.DataDirectory[
IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
if (RelocationRVA)
{
RelocationDir = (PRELOCATION_DIRECTORY)
((PCHAR)ImageBase + RelocationRVA);
while (RelocationDir->SizeOfBlock)
{
Delta32 = (unsigned long)(ImageBase -
NTHeaders->OptionalHeader.ImageBase);
RelocationBlock = (PRELOCATION_ENTRY)
(RelocationRVA + ImageBase + sizeof(RELOCATION_DIRECTORY));
NumberOfEntries =
(RelocationDir->SizeOfBlock - sizeof(RELOCATION_DIRECTORY)) /
sizeof(RELOCATION_ENTRY);
for (i = 0; i < NumberOfEntries; i++)
{
RelocationRVA = NTHeaders->OptionalHeader.DataDirectory[
IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
if (RelocationRVA)
{
RelocationDir = (PRELOCATION_DIRECTORY)((PCHAR)ImageBase +
RelocationRVA);
while (RelocationDir->SizeOfBlock)
{
Delta32 = (unsigned long)(ImageBase -
NTHeaders->OptionalHeader.ImageBase);
RelocationBlock = (PRELOCATION_ENTRY)
(RelocationRVA + ImageBase + sizeof(RELOCATION_DIRECTORY));
NumberOfEntries =
(RelocationDir->SizeOfBlock - sizeof(RELOCATION_DIRECTORY)) /
sizeof(RELOCATION_ENTRY);
for (i = 0; i < NumberOfEntries; i++)
{
Offset = (RelocationBlock[i].TypeOffset & 0xfff) +
RelocationDir->VirtualAddress;
switch (RelocationBlock[i].TypeOffset >> 12)
{
case TYPE_RELOC_ABSOLUTE:
break;
case TYPE_RELOC_HIGH:
pValue16 = (PUSHORT) (ImageBase + Offset);
*pValue16 += Delta32 >> 16;
break;
case TYPE_RELOC_LOW:
pValue16 = (PUSHORT)(ImageBase + Offset);
*pValue16 += Delta32 & 0xffff;
break;
case TYPE_RELOC_HIGHLOW:
pValue32 = (PULONG) (ImageBase + Offset);
*pValue32 += Delta32;
break;
case TYPE_RELOC_HIGH:
pValue16 = (PUSHORT) (ImageBase + Offset);
*pValue16 += Delta32 >> 16;
break;
case TYPE_RELOC_LOW:
pValue16 = (PUSHORT)(ImageBase + Offset);
*pValue16 += Delta32 & 0xffff;
break;
case TYPE_RELOC_HIGHLOW:
pValue32 = (PULONG) (ImageBase + Offset);
*pValue32 += Delta32;
break;
case TYPE_RELOC_HIGHADJ:
/* FIXME: do the highadjust fixup */
DPRINT(
"TYPE_RELOC_HIGHADJ fixup not implemented, sorry\n");
return 0;
/* FIXME: do the highadjust fixup */
DPRINT("TYPE_RELOC_HIGHADJ fixup not implemented"
", sorry\n");
return(STATUS_UNSUCCESSFUL);
default:
DPRINT("unexpected fixup type\n");
return 0;
DPRINT("unexpected fixup type\n");
return(STATUS_UNSUCCESSFUL);
}
}
RelocationRVA += RelocationDir->SizeOfBlock;
RelocationDir = (PRELOCATION_DIRECTORY)(ImageBase +
RelocationRVA);
}
}
}
}
RelocationRVA += RelocationDir->SizeOfBlock;
RelocationDir = (PRELOCATION_DIRECTORY)(ImageBase +
RelocationRVA);
}
}
return(STATUS_SUCCESS);
}
/* FIXME: do import fixups/load required libraries */
/* Resolve Import Library references */
if (NTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].
VirtualAddress != 0)
{
PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory;
static NTSTATUS LdrFixupImports(PIMAGE_NT_HEADERS NTHeaders,
DWORD ImageBase)
{
PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory;
ULONG Ordinal;
PDLL Module;
NTSTATUS Status;
/* Process each import module */
ImportModuleDirectory = (PIMAGE_IMPORT_MODULE_DIRECTORY)
(ImageBase + NTHeaders->OptionalHeader.
DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
while (ImportModuleDirectory->dwRVAModuleName)
{
PVOID *ImportAddressList;
PULONG FunctionNameList;
DWORD pName;
PWORD pHint;
Status = LdrFindDll(&Module,
(PCHAR)(ImageBase +
ImportModuleDirectory->dwRVAModuleName));
if (!NT_SUCCESS(Status))
{
return 0;
}
/* Get the import address list */
ImportAddressList = (PVOID *)
(NTHeaders->OptionalHeader.ImageBase +
ImportModuleDirectory->dwRVAFunctionAddressList);
/* Get the list of functions to import */
if (ImportModuleDirectory->dwRVAFunctionNameList != 0)
{
FunctionNameList = (PULONG) (ImageBase +
ImportModuleDirectory->dwRVAFunctionNameList);
}
else
{
FunctionNameList = (PULONG) (ImageBase +
ImportModuleDirectory->dwRVAFunctionAddressList);
}
/* Walk through function list and fixup addresses */
while(*FunctionNameList != 0L)
{
if ((*FunctionNameList) & 0x80000000)
{
Ordinal = (*FunctionNameList) & 0x7fffffff;
*ImportAddressList = LdrGetExportByOrdinal(Module, Ordinal);
}
else
{
pName = (DWORD)(ImageBase + *FunctionNameList + 2);
pHint = (PWORD)(ImageBase + *FunctionNameList);
*ImportAddressList = LdrGetExportByName(Module,pName);
if ((*ImportAddressList) == NULL)
{
return(STATUS_UNSUCCESSFUL);
}
}
ImportAddressList++;
FunctionNameList++;
}
ImportModuleDirectory++;
}
return(STATUS_SUCCESS);
}
/* Process each import module */
ImportModuleDirectory = (PIMAGE_IMPORT_MODULE_DIRECTORY)
(ImageBase + NTHeaders->OptionalHeader.
DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
while (ImportModuleDirectory->dwRVAModuleName)
{
DWORD LibraryBase;
PIMAGE_DOS_HEADER LibDosHeader;
PIMAGE_NT_HEADERS LibNTHeaders;
PVOID *ImportAddressList; // was pImpAddr
PULONG FunctionNameList;
DWORD pName;
PWORD pHint;
Status = LdrLoadDll(&Module,
(PCHAR)(ImageBase +
ImportModuleDirectory->dwRVAModuleName));
if (!NT_SUCCESS(Status))
{
return 0;
}
/* Get the import address list */
ImportAddressList = (PVOID *)
(NTHeaders->OptionalHeader.ImageBase +
ImportModuleDirectory->dwRVAFunctionAddressList);
/* Get the list of functions to import */
if (ImportModuleDirectory->dwRVAFunctionNameList != 0)
{
FunctionNameList = (PULONG) (ImageBase +
ImportModuleDirectory->dwRVAFunctionNameList);
}
else
{
FunctionNameList = (PULONG) (ImageBase +
ImportModuleDirectory->dwRVAFunctionAddressList);
}
/* Walk through function list and fixup addresses */
while(*FunctionNameList != 0L)
{
if ((*FunctionNameList) & 0x80000000) // hint
{
// *ImportAddressList = LibraryExports[(*FunctionNameList) & 0x7fffffff];
DPRINT("Import by ordinal unimplemented\n");
for(;;);
}
else // hint-name
{
pName = (DWORD)(ImageBase + *FunctionNameList + 2);
pHint = (PWORD)(ImageBase + *FunctionNameList);
/* FIXME: verify name */
if (strcmp(pName,"vsprintf")==0)
{
DPRINT("Fixing up reference to %s at %x\n",
pName,ImportAddressList);
DPRINT("pHint %x\n",pHint);
}
*ImportAddressList = LdrGetExport(Module,pName);
}
/* FIXME: verify value of hint */
ImportAddressList++;
FunctionNameList++;
}
ImportModuleDirectory++;
}
}
/* FIXME: locate the entry point for the image */
EntryPoint = NTHeaders->OptionalHeader.ImageBase +
NTHeaders->OptionalHeader.AddressOfEntryPoint;
return EntryPoint;
static PEPFUNC LdrPEStartup(DWORD ImageBase, HANDLE SectionHandle)
{
NTSTATUS Status;
PEPFUNC EntryPoint;
PIMAGE_DOS_HEADER DosHeader;
PIMAGE_NT_HEADERS NTHeaders;
DosHeader = (PIMAGE_DOS_HEADER) ImageBase;
NTHeaders = (PIMAGE_NT_HEADERS)(ImageBase + DosHeader->e_lfanew);
/* Initialize Image sections */
LdrMapSections(ImageBase, SectionHandle, NTHeaders);
if (ImageBase != (DWORD) NTHeaders->OptionalHeader.ImageBase)
{
Status = LdrPerformRelocations(NTHeaders, ImageBase);
if (!NT_SUCCESS(Status))
{
return(NULL);
}
}
if (NTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].
VirtualAddress != 0)
{
Status = LdrFixupImports(NTHeaders, ImageBase);
if (!NT_SUCCESS(Status))
{
return(NULL);
}
}
EntryPoint = ImageBase + NTHeaders->OptionalHeader.AddressOfEntryPoint;
return(EntryPoint);
}

View file

@ -29,7 +29,7 @@
#include <string.h>
#include <ddk/ntddk.h>
//#define NDEBUG
#define NDEBUG
#include <ntdll/ntdll.h>
#define HEAP_VALIDATE
@ -753,7 +753,7 @@ PVOID STDCALL RtlAllocateHeap(HANDLE Heap,
DPRINT("HeapAlloc(hheap 0x%lX, flags 0x%lX, size 0x%lX )\n",
(ULONG) Heap, Flags, (ULONG) Size );
#ifdef HEAP_VALIDATE
HeapValidate(Heap, 0, 0);
RtlValidateHeap(Heap, 0, 0);
#endif
if(( Flags | pheap->Flags) & HEAP_NO_SERIALIZE )
RtlEnterCriticalSection(&(pheap->Synchronize));
@ -768,7 +768,7 @@ PVOID STDCALL RtlAllocateHeap(HANDLE Heap,
DPRINT("HeapAlloc returns 0x%lX\n", (ULONG) retval);
HeapValidate(Heap, 0, 0);
RtlValidateHeap(Heap, 0, 0);
return retval;
@ -788,7 +788,7 @@ LPVOID STDCALL RtlReAllocHeap(HANDLE hheap, DWORD flags, LPVOID ptr,
DPRINT("HeapReAlloc( 0x%lX, 0x%lX, 0x%lX, 0x%lX )\n",
(ULONG) hheap, flags, (ULONG) ptr, size );
#ifdef HEAP_VALIDATE
HeapValidate(hheap, 0, 0);
RtlValidateHeap(hheap, 0, 0);
#endif
if(( flags | pheap->Flags) & HEAP_NO_SERIALIZE )
RtlEnterCriticalSection(&(pheap->Synchronize));
@ -821,7 +821,7 @@ BOOLEAN STDCALL RtlFreeHeap(HANDLE Heap, ULONG Flags, PVOID Address)
DPRINT("HeapFree( 0x%lX, 0x%lX, 0x%lX )\n",
(ULONG) Heap, Flags, (ULONG) Address );
#ifdef HEAP_VALIDATE
HeapValidate(Heap, 0, 0);
RtlValidateHeap(Heap, 0, 0);
#endif
if(( Flags | pheap->Flags) & HEAP_NO_SERIALIZE )
RtlEnterCriticalSection(&(pheap->Synchronize));
@ -849,9 +849,9 @@ BOOLEAN STDCALL RtlFreeHeap(HANDLE Heap, ULONG Flags, PVOID Address)
/*********************************************************************
* GetProcessHeap -- KERNEL32 *
*********************************************************************/
HANDLE WINAPI GetProcessHeap(VOID)
HANDLE WINAPI RtlGetProcessHeap(VOID)
{
DPRINT("GetProcessHeap()\n");
DPRINT("RtlGetProcessHeap()\n");
return (HANDLE) __ProcessHeap;
}
@ -891,7 +891,6 @@ DWORD WINAPI RtlEnumProcessHeaps(DWORD maxheaps, PHANDLE phandles )
/*********************************************************************
* HeapLock -- KERNEL32 *
*********************************************************************/
BOOL WINAPI RtlLockHeap(HANDLE hheap)
{
PHEAP pheap=hheap;
@ -905,7 +904,6 @@ BOOL WINAPI RtlLockHeap(HANDLE hheap)
/*********************************************************************
* HeapUnlock -- KERNEL32 *
*********************************************************************/
BOOL WINAPI RtlUnlockHeap(HANDLE hheap)
{
PHEAP pheap=hheap;
@ -922,7 +920,6 @@ BOOL WINAPI RtlUnlockHeap(HANDLE hheap)
* NT uses this function to compact moveable blocks and other things *
* Here it does not compact, but it finds the largest free region *
*********************************************************************/
UINT RtlCompactHeap(HANDLE hheap, DWORD flags)
{
PHEAP pheap=hheap;
@ -955,7 +952,7 @@ UINT RtlCompactHeap(HANDLE hheap, DWORD flags)
/*********************************************************************
* HeapSize -- KERNEL32 *
*********************************************************************/
DWORD WINAPI HeapSize(HANDLE hheap, DWORD flags, LPCVOID pmem)
DWORD WINAPI RtlSizeHeap(HANDLE hheap, DWORD flags, LPCVOID pmem)
{
PHEAP pheap=(PHEAP) hheap;
PHEAP_BLOCK palloc=((PHEAP_BLOCK)pmem-1);
@ -995,7 +992,7 @@ DWORD WINAPI HeapSize(HANDLE hheap, DWORD flags, LPCVOID pmem)
* *
* NOTE: only implemented in NT *
*********************************************************************/
BOOL WINAPI HeapValidate(HANDLE hheap, DWORD flags, LPCVOID pmem)
BOOL WINAPI RtlValidateHeap(HANDLE hheap, DWORD flags, LPCVOID pmem)
{
PHEAP pheap=(PHEAP)hheap;
PHEAP_BLOCK pcheck;

View file

@ -43,32 +43,36 @@ ULONG RtlAnsiStringToUnicodeSize(IN PANSI_STRING AnsiString)
}
NTSTATUS RtlAnsiStringToUnicodeString(IN OUT PUNICODE_STRING DestinationString,
IN PANSI_STRING SourceString, IN BOOLEAN AllocateDestinationString)
IN PANSI_STRING SourceString,
IN BOOLEAN AllocateDestinationString)
{
unsigned long i;
unsigned long i;
if(AllocateDestinationString==TRUE) {
// DestinationString->Buffer=ExAllocatePool(NonPagedPool, (SourceString->Length+1)*2);
DestinationString->MaximumLength=SourceString->Length;
};
if (AllocateDestinationString == TRUE)
{
DestinationString->Buffer=RtlAllocateHeap(RtlGetProcessHeap(),
0,
(SourceString->Length+1)*2);
DestinationString->MaximumLength=SourceString->Length;
}
DestinationString->Length=SourceString->Length;
memset(DestinationString->Buffer, 0, SourceString->Length*2);
for (i=0; i<SourceString->Length; i++)
{
*DestinationString->Buffer=*SourceString->Buffer;
SourceString->Buffer++;
DestinationString->Buffer++;
};
*DestinationString->Buffer=0;
SourceString->Buffer-=SourceString->Length;
DestinationString->Buffer-=SourceString->Length;
return STATUS_SUCCESS;
};
DestinationString->Length=SourceString->Length;
memset(DestinationString->Buffer, 0, SourceString->Length*2);
for (i=0; i<SourceString->Length; i++)
{
*DestinationString->Buffer=*SourceString->Buffer;
SourceString->Buffer++;
DestinationString->Buffer++;
}
*DestinationString->Buffer=0;
SourceString->Buffer-=SourceString->Length;
DestinationString->Buffer-=SourceString->Length;
return STATUS_SUCCESS;
}
NTSTATUS RtlAppendUnicodeStringToString(IN OUT PUNICODE_STRING Destination,
IN PUNICODE_STRING Source)
@ -313,18 +317,21 @@ VOID RtlFreeUnicodeString(IN PUNICODE_STRING UnicodeString)
VOID RtlInitAnsiString(IN OUT PANSI_STRING DestinationString,
IN PCSZ SourceString)
{
unsigned long DestSize;
if(SourceString==NULL) {
DestinationString->Length=0;
DestinationString->MaximumLength=0;
} else {
DestSize=strlen((const char *)SourceString);
DestinationString->Length=DestSize;
DestinationString->MaximumLength=DestSize+1;
};
DestinationString->Buffer=(PCHAR)SourceString;
};
unsigned long DestSize;
if(SourceString==NULL)
{
DestinationString->Length=0;
DestinationString->MaximumLength=0;
}
else
{
DestSize=strlen((const char *)SourceString);
DestinationString->Length=DestSize;
DestinationString->MaximumLength=DestSize+1;
}
DestinationString->Buffer=(PCHAR)SourceString;
}
VOID RtlInitString(IN OUT PSTRING DestinationString,
IN PCSZ SourceString)

View file

@ -29,7 +29,7 @@ LOADERS = dos
#
# Select the device drivers and filesystems you want
#
KERNEL_SERVICES = parallel keyboard blues null mouse serial sound ide test sdisk \
KERNEL_SERVICES = parallel keyboard blues null mouse serial sound ide \
minix vfat ext2
APPS = hello shell args

View file

@ -188,7 +188,7 @@ NTSTATUS STDCALL ZwCreateFile(PHANDLE FileHandle,
FileObject->Flags = FileObject->Flags | FO_SYNCHRONOUS_IO;
}
KeInitializeEvent(&Event,NotificationEvent,FALSE);
KeInitializeEvent(&Event, NotificationEvent, FALSE);
Irp = IoAllocateIrp(FileObject->DeviceObject->StackSize, FALSE);
if (Irp==NULL)

View file

@ -174,15 +174,8 @@ asmlinkage void _main(boot_param* _bp)
*/
LdrLoadAutoConfigDrivers();
#ifdef KRNL_TEST
/*
* Test various features of the kernel
*/
TstBegin();
#endif
/*
* Launch initial thread
* Launch initial process
*/
LdrLoadInitialProcess();

View file

@ -1,4 +1,4 @@
all: ntoskrnl.exe utils/pe2bin/pe2bin$(EXE_POSTFIX)
all: objects ntoskrnl.exe
#
# Defines $(HAL_OBJECTS)
@ -45,14 +45,15 @@ SE_OBJECTS = se/semgr.o
CM_OBJECTS = cm/registry.o
TST_OBJECTS = tst/test.o
DBG_OBJECTS = dbg/brkpoint.o dbg/errinfo.o
LDR_OBJECTS = ldr/loader.o
CC_OBJECTS = cc/cacheman.o cc/block.o
objects: ../ntoskrnl/objects
mkdir objects
objects/hal.o: $(HAL_OBJECTS)
$(LD) -r $(HAL_OBJECTS) -o objects/hal.o
@ -83,9 +84,6 @@ objects/se.o: $(SE_OBJECTS)
objects/cm.o: $(CM_OBJECTS)
$(LD) -r $(CM_OBJECTS) -o objects/cm.o
objects/tst.o: $(TST_OBJECTS)
$(LD) -r $(TST_OBJECTS) -o objects/tst.o
objects/dbg.o: $(DBG_OBJECTS)
$(LD) -r $(DBG_OBJECTS) -o objects/dbg.o
@ -100,12 +98,9 @@ objects/cc.o: $(CC_OBJECTS)
OBJECTS = objects/hal.o objects/ke.o objects/rtl.o objects/mm.o \
objects/io.o objects/ob.o objects/ps.o objects/ex.o \
objects/se.o objects/cm.o objects/tst.o objects/dbg.o\
objects/se.o objects/cm.o objects/dbg.o\
objects/nt.o objects/cc.o objects/ldr.o
utils/pe2bin/pe2bin$(EXE_POSTFIX): utils/pe2bin/pe2bin.c
$(NATIVE_CC) -I../include -g utils/pe2bin/pe2bin.c -o utils/pe2bin/pe2bin$(EXE_POSTFIX)
utils/export/export$(EXE_POSTFIX): utils/export/export.c
$(NATIVE_CC) -g utils/export/export.c -o utils/export/export$(EXE_POSTFIX)