#include "reactos/ddk/rtl.h" /* for InsertTailList() */
#include "reactos/internal/debug.h" /* for assert() */
#include <glib/gutils.h> /* for g_path_get_basename() */
+#include <gmodule.h>
/* reactos/ntoskrnl/ldr/loader.c file-scope global declaration: */
NTSTATUS LdrProcessModule(PVOID ModuleLoadBase,PUNICODE_STRING ModuleName,PMODULE_OBJECT *ModuleObject);
+/* 'ntoskrnl/ldr/loader.c' file-scoped declaration: */
+VOID LdrpBuildModuleBaseName(PUNICODE_STRING BaseName,PUNICODE_STRING FullName);
+
+
+static MODULE_OBJECT *captive_LdrLoadModule_gmodule(const gchar *Filename_utf8)
+{
+MODULE_OBJECT *r;
+GModule *gmodule;
+MODULE_TEXT_SECTION *ModuleTextSection;
+gboolean errbool;
+
+ g_return_val_if_fail(Filename_utf8!=NULL,NULL);
+ g_return_val_if_fail(TRUE==g_module_supported(),NULL);
+
+ gmodule=g_module_open(Filename_utf8,
+ 0); /* Flags; !LAZY */
+ if (!gmodule)
+ g_message(g_module_error());
+ g_assert(gmodule!=NULL);
+
+ captive_new0(r);
+
+ errbool=g_module_symbol(gmodule,"DriverEntry",&r->EntryPoint);
+ g_assert(errbool==TRUE);
+
+ r->Base=(PVOID)gmodule;
+ r->Flags=MODULE_FLAG_COFF; /* some weird value - reactos uses MODULE_FLAG_PE */
+
+ RtlCreateUnicodeString(&r->FullName,captive_utf8_to_UnicodeString_alloca(Filename_utf8)->Buffer);
+ LdrpBuildModuleBaseName(&r->BaseName,&r->FullName);
+
+ /* r->Length=0; */
+
+ /* Insert module */
+ InsertTailList(&ModuleListHead,&r->ListEntry);
+
+ captive_new0(ModuleTextSection);
+ /* ModuleTextSection->Base=0; */
+ /* ModuleTextSection->Length=0; */
+ ModuleTextSection->Name=g_memdup(r->BaseName.Buffer,
+ (captive_ucs2_strlen(r->BaseName.Buffer)+1)*sizeof(captive_ucs2));
+ /* ModuleTextSection->OptionalHeader=NULL; */
+ InsertTailList(&ModuleTextListHead,&ModuleTextSection->ListEntry);
+ r->TextSection=ModuleTextSection;
+
+ return r;
+}
+
+
/**
* LdrLoadModule:
* @Filename: Filename of module to load based on host OS disk.
PVOID ModuleLoadBase;
PMODULE_OBJECT Module;
NTSTATUS err;
+const gchar *Filename_utf8=captive_UnicodeString_to_utf8_alloca(Filename);
*ModuleObjectp=NULL;
/* Open the Module */
ModuleLoadBase=captive_file_mmap(
NULL, /* lenp */
- captive_UnicodeString_to_utf8_alloca(Filename), /* path */
+ Filename_utf8, /* path */
O_RDONLY, /* open_flags */
PROT_READ, /* mmap_prot */
MAP_SHARED); /* mmap_flags */
/* FIXME: convert errno instead of STATUS_INSUFFICIENT_RESOURCES */
g_return_val_if_fail(ModuleLoadBase!=NULL,STATUS_INSUFFICIENT_RESOURCES);
- /* examine/relocate the module */
- err=LdrProcessModule(ModuleLoadBase,Filename,&Module);
- if (!NT_SUCCESS(err)) {
- g_error("LdrLoadModule(): LdrProcessModule()=0x%08lX",err);
- goto err_captive_file_munmap;
+ if ((('M'<<8U)|('Z'<<0U))==GUINT16_FROM_BE(*(const guint16 *)ModuleLoadBase)) {
+ g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: Loading module format: %s",G_STRLOC,"MZ/PE-32");
+ /* examine/relocate the module */
+ err=LdrProcessModule(ModuleLoadBase,Filename,&Module);
+ if (!NT_SUCCESS(err)) {
+ g_error("LdrLoadModule(): LdrProcessModule()=0x%08lX",err);
+ goto err_captive_file_munmap;
+ }
+ }
+ else {
+ g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: Loading module format: %s",G_STRLOC,"native .so");
+ Module=captive_LdrLoadModule_gmodule(Filename_utf8);
+ g_assert(Module!=NULL);
}
/* we were successful */