2 * reactos ldr/ (loader) emulation of libcaptive
3 * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "captive/ldr.h" /* self */
23 #include "captive/ldr_exports.h" /* self */
24 #include "reactos/internal/ldr.h" /* self */
25 #include "captive/unicode.h"
26 #include "captive/rtl-file.h"
27 #include <glib/gtypes.h>
28 #include <glib/gmessages.h>
29 #include <glib/gmem.h>
30 #include "reactos/napi/types.h"
31 #include "reactos/internal/kd.h" /* for KDB_LOADDRIVER_HOOK */
33 #include <sys/mman.h> /* for PROT_READ, MAP_SHARED */
34 #include "captive/macros.h"
36 #include "reactos/ntos/rtl.h" /* for InsertTailList() */
37 #include "reactos/internal/debug.h" /* for assert() */
38 #include <glib/gutils.h> /* for g_path_get_basename() */
40 #include "captive/options-module.h"
43 /* map: ExportAddress -> (struct captive_ModuleList_patchpoint *) */
44 static GHashTable *captive_ModuleList_patchpoint_hash;
46 /* initialize 'captive_ModuleList_patchpoint_hash' */
47 static void captive_ModuleList_patchpoint_hash_init(void)
49 if (captive_ModuleList_patchpoint_hash)
51 captive_ModuleList_patchpoint_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
55 /* map: ExportAddress -> (CHAR *)funcname */
56 static GHashTable *captive_ModuleList_function_disable_hash;
58 /* initialize 'captive_ModuleList_function_disable_hash' */
59 static void captive_ModuleList_function_disable_hash_init(void)
61 if (captive_ModuleList_function_disable_hash)
63 captive_ModuleList_function_disable_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
67 /* reactos/ntoskrnl/ldr/loader.c file-scope global declaration: */
68 NTSTATUS LdrProcessModule(PVOID ModuleLoadBase,PUNICODE_STRING ModuleName,PMODULE_OBJECT *ModuleObject);
69 PVOID LdrGetExportAddress(PMODULE_OBJECT ModuleObject,char *Name,unsigned short Hint);
72 /* 'ntoskrnl/ldr/loader.c' file-scoped declaration: */
73 VOID LdrpBuildModuleBaseName(PUNICODE_STRING BaseName,PUNICODE_STRING FullName);
76 static MODULE_OBJECT *captive_LdrLoadModule_gmodule(const gchar *Filename_utf8,const gchar *Filename_bslash_utf8)
80 MODULE_TEXT_SECTION *ModuleTextSection;
83 g_return_val_if_fail(Filename_utf8!=NULL,NULL);
84 g_return_val_if_fail(Filename_bslash_utf8!=NULL,NULL);
85 g_return_val_if_fail(TRUE==g_module_supported(),NULL);
87 gmodule=g_module_open(Filename_utf8,
88 0); /* Flags; !LAZY */
90 g_message(g_module_error());
91 g_assert(gmodule!=NULL);
95 errbool=g_module_symbol(gmodule,"DriverEntry",&r->EntryPoint);
96 g_assert(errbool==TRUE);
98 r->Base=(PVOID)gmodule;
99 r->Flags=MODULE_FLAG_COFF; /* some weird value - reactos uses MODULE_FLAG_PE */
101 RtlCreateUnicodeString(&r->FullName,captive_utf8_to_UnicodeString_alloca(Filename_bslash_utf8)->Buffer);
102 LdrpBuildModuleBaseName(&r->BaseName,&r->FullName);
107 InsertTailList(&ModuleListHead,&r->ListEntry);
109 captive_new0(ModuleTextSection);
110 /* ModuleTextSection->Base=0; */
111 /* ModuleTextSection->Length=0; */
112 ModuleTextSection->Name=g_memdup(r->BaseName.Buffer,
113 (captive_ucs2_strlen(r->BaseName.Buffer)+1)*sizeof(captive_ucs2));
114 /* ModuleTextSection->OptionalHeader=NULL; */
115 InsertTailList(&ModuleTextListHead,&ModuleTextSection->ListEntry);
116 r->TextSection=ModuleTextSection;
123 * captive_LdrLoadModule:
124 * @Filename: Filename of module to load based on host OS disk.
125 * Loading of already loaded module is forbidden.
126 * @ModuleObject: Returns initialized module object.
128 * Load and initialize module to reactos using host OS functions.
130 * Returns: STATUS_SUCCESS if the module was loaded successfuly during the call.
132 NTSTATUS captive_LdrLoadModule(struct captive_options_module *options_module,PMODULE_OBJECT *ModuleObjectp)
134 PMODULE_OBJECT Module;
136 gchar *Filename_utf8=options_module->pathname_utf8;
137 gchar *Filename_bslash_utf8,*s;
138 UNICODE_STRING *Filename_bslash;
142 /* Filename=~s#/#\\#g -> Filename_bslash to satisfy basename-derivations by reactos. */
143 Filename_bslash_utf8=(/* de-const as we do not change the length */ gchar *)captive_strdup_alloca(Filename_utf8);
144 for (s=Filename_bslash_utf8;(s=strchr(s,'/'));s++)
146 Filename_bslash=captive_utf8_to_UnicodeString_alloca(Filename_bslash_utf8);
148 switch (options_module->type) {
149 case CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY: g_assert_not_reached();
151 case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
152 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: Loading module format: %s",G_STRLOC,"MZ/PE-32");
153 /* examine/relocate the module */
154 err=LdrProcessModule(options_module->u.pe32.base,Filename_bslash,&Module);
155 if (!NT_SUCCESS(err)) {
156 g_error("LdrLoadModule(): LdrProcessModule()=0x%08lX",(gulong)err);
161 case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
162 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: Loading module format: %s",G_STRLOC,"native .so");
163 Module=captive_LdrLoadModule_gmodule(Filename_utf8,Filename_bslash_utf8);
164 g_assert(Module!=NULL);
167 /* we were successful */
168 *ModuleObjectp=Module;
170 /* Hook for KDB on loading a driver. */
171 KDB_LOADDRIVER_HOOK(Filename_bslash,Module);
173 return STATUS_SUCCESS;
176 g_return_val_if_reached(err);
180 NTSTATUS LdrLoadModule(PUNICODE_STRING Filename,PMODULE_OBJECT *ModuleObject)
182 return STATUS_OBJECT_NAME_NOT_FOUND;
187 * captive_LdrpLoadAndCallImage:
188 * @ModuleObjectp: Returns PMODULE_OBJECT successfuly loaded.
189 * @ModuleName: Filename of module to load based on host OS disk.
190 * Loading of already loaded module is forbidden despite original
191 * LdrpLoadAndCallImage().
192 * @DriverEntry_DriverObject: argument #DriverObject of #PDRIVER_INITIALIZE call.
193 * @DriverEntry_RegistryPath: argument #RegistryPath of #PDRIVER_INITIALIZE call.
195 * Corresponds to reactos LdrpLoadAndCallImage() but it also provides arguments
196 * to pass to #PDRIVER_INITIALIZE call of module driver initialization.
198 * Returns: STATUS_SUCCESS if the driver module was loaded and initialized
199 * successfuly during this call. Ignore returned @ModuleObjectp if function failed.
201 NTSTATUS captive_LdrpLoadAndCallImage(PMODULE_OBJECT *ModuleObjectp,struct captive_options_module *options_module,
202 PDRIVER_OBJECT DriverEntry_DriverObject,PUNICODE_STRING DriverEntry_RegistryPath)
204 PDRIVER_INITIALIZE DriverEntry;
205 PMODULE_OBJECT ModuleObject_tmp;
209 /* check for duplicity */
210 g_return_val_if_fail(LdrGetModuleObject(ModuleName)==NULL,STATUS_IMAGE_ALREADY_LOADED);
213 /* provide our temporary storage if the caller doesn't want to know ModuleObject address */
215 ModuleObjectp=&ModuleObject_tmp;
217 /* load the module */
218 err=captive_LdrLoadModule(options_module,ModuleObjectp);
219 g_return_val_if_fail(NT_SUCCESS(err),err);
221 /* initialize the module */
222 DriverEntry=(PDRIVER_INITIALIZE)(*ModuleObjectp)->EntryPoint;
223 err=(*DriverEntry)(DriverEntry_DriverObject,DriverEntry_RegistryPath);
224 if (!NT_SUCCESS(err))
225 goto err_LdrUnloadModule;
227 /* assumed STATUS_SUCCESS */
232 LdrUnloadModule(*ModuleObjectp);
235 g_return_val_if_reached(err);
240 * captive_ModuleList_add_builtin:
241 * @FullName_utf8: String to fill in #PMODULE_OBJECT->FullName.
242 * @...: (const gchar *sym_name,void *sym_val) symbol list terminated by %NULL.
244 * Adds simulated built-in module to #ModuleListHead module list.
245 * It can be used for the functionality of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement().
246 * libcaptive does not support Ordinals - we just pretend liner (%0-based)
247 * Ordinal numbers of the functions in given @... stdargs order.
249 * Returns: %TRUE if the module was successfuly added.
251 gboolean captive_ModuleList_add_builtin(const gchar *FullName_utf8,...)
253 MODULE_OBJECT *ModuleObject;
256 const gchar *sym_name;
258 gchar *basename_utf8;
259 IMAGE_DOS_HEADER *DosHeader;
260 IMAGE_NT_HEADERS *NTHeaders;
261 IMAGE_EXPORT_DIRECTORY *ExportDir;
262 guint symi,symN; /* index,number of passed stdarg sym_name/sym_val pairs */
263 ptrdiff_t *NameList; /* filled by sym_name; MODULEOBJECT_BASE_OFFSET_MINUS()ed */
264 WORD *OrdinalList; /* linear list - libcaptive doesn't support Ordinals */
265 ptrdiff_t *FunctionList; /* filled by sym_val; MODULEOBJECT_BASE_OFFSET_MINUS()ed */
267 g_return_val_if_fail(FullName_utf8!=NULL,FALSE);
269 captive_new0(ModuleObject);
270 /* A lot of offsets in the structures are relative to ModuleObject->Base */
271 #define MODULEOBJECT_BASE_OFFSET_MINUS(addr) ((ptrdiff_t)((char *)addr-(char *)ModuleObject->Base))
273 ModuleObject->Flags = MODULE_FLAG_PE;
275 /* fill ModuleObject->FullName
276 * ModuleObject->FullName should be probably "\\SYSTEM32\\..." style but
277 * we put there '/'-based UNIX filenames in libcaptive.
279 errbool=RtlCreateUnicodeString(&ModuleObject->FullName,captive_utf8_to_UnicodeString_alloca(FullName_utf8)->Buffer);
281 g_error("captive_ModuleList_add_builtin: RtlCreateUnicodeString()==FALSE");
282 goto err_g_free_ModuleObject;
285 /* fill ModuleObject->BaseName
286 * reactos/ntoskrnl/ldr/loader.c/LdrpBuildModuleBaseName() does this one
287 * but it is 'static' for that file and also it is stricly backslash based.
288 * ModuleObject->FullName should be probably "\\SYSTEM32\\..." style but
289 * we put there '/'-based UNIX filenames in libcaptive.
291 basename_utf8=g_path_get_basename(FullName_utf8);
292 errbool=RtlCreateUnicodeString(&ModuleObject->BaseName,captive_utf8_to_UnicodeString_alloca(basename_utf8)->Buffer);
293 g_free(basename_utf8);
295 g_error("captive_ModuleList_add_builtin: RtlCreateUnicodeString()==FALSE");
296 goto err_g_free_ModuleObject;
299 /* We must satisfy reactos/ntoskrnl/ldr/rtl.c/RtlImageDirectoryEntryToData(,,IMAGE_DIRECTORY_ENTRY_EXPORT)
300 * prepare module headers
302 captive_new0(DosHeader);
303 ModuleObject->Base=DosHeader; /* link DosHeader to ModuleObject */
304 DosHeader->e_magic=IMAGE_DOS_MAGIC;
305 captive_new0(NTHeaders);
306 DosHeader->e_lfanew=MODULEOBJECT_BASE_OFFSET_MINUS(NTHeaders); /* link NTHeaders to DosHeader */
307 NTHeaders->Signature=IMAGE_PE_MAGIC;
308 NTHeaders->OptionalHeader.NumberOfRvaAndSizes=IMAGE_DIRECTORY_ENTRY_EXPORT+1;
309 NTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size
310 =sizeof(*ExportDir); /* in bytes; just prevent LdrPEFixupForward() invocation */
311 captive_new0(ExportDir);
312 NTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress
313 =MODULEOBJECT_BASE_OFFSET_MINUS(ExportDir); /* link ExportDir to NTHeaders */
314 ExportDir->Base=0; /* base Ordinal number; Ordinals are not supported by libcaptive */
316 /* count the number of symbols */
317 va_start(ap,FullName_utf8);
318 for (symN=0;captive_va_arg(sym_name,ap);symN++)
319 captive_va_arg(sym_val,ap);
321 ExportDir->NumberOfNames=symN;
322 ExportDir->NumberOfFunctions=symN;
324 /* allocate and link the symbol tables */
325 captive_newn(NameList,symN);
326 ExportDir->AddressOfNames=(PDWORD *)MODULEOBJECT_BASE_OFFSET_MINUS(NameList); /* link NameList to ExportDir */
327 captive_newn(OrdinalList,symN);
328 ExportDir->AddressOfNameOrdinals=(PWORD *)MODULEOBJECT_BASE_OFFSET_MINUS(OrdinalList); /* link OrdinalList to ExportDir */
329 captive_newn(FunctionList,symN);
330 ExportDir->AddressOfFunctions=(PDWORD *)MODULEOBJECT_BASE_OFFSET_MINUS(FunctionList); /* link FunctionList to ExportDir */
332 /* fill in symbol tables */
333 va_start(ap,FullName_utf8);
334 for (symi=0;symi<symN;symi++) {
335 captive_va_arg(sym_name,ap);
336 captive_va_arg(sym_val ,ap);
337 NameList[symi]=MODULEOBJECT_BASE_OFFSET_MINUS(sym_name);
338 OrdinalList[symi]=symi; /* Ordinals are not supported by libcaptive */
339 FunctionList[symi]=MODULEOBJECT_BASE_OFFSET_MINUS(sym_val);
343 /* successful module info build */
344 InsertTailList(&ModuleListHead,&ModuleObject->ListEntry);
347 err_g_free_ModuleObject:
348 g_free(ModuleObject);
350 g_return_val_if_reached(FALSE);
354 static gsize instruction_length(const guint8 *instr)
356 if ((instr[0] & 0xF8)==0x50) /* push %regular-register */
358 if ((instr[0] & 0xF8)==0x58) /* pop %regular-register */
360 if ((instr[0] & 0xE7)==0x06) /* push %segment-register */
362 if ((instr[0] & 0xE7)==0x07 && instr[0]!=0x0F) /* pop %segment-register */
365 case 0x33: /* xor GB,Ev */
367 case 0x64: /* prefix %fs */
368 return instruction_length(instr+1);
369 case 0x68: /* push $quad-byte */
371 case 0x6A: /* push $single-byte */
375 case 0x3D: /* cmpb $byte,immediate-quad-indirect */
376 return 1+1+4+1; /* 80 3D immediate-quad-indirect byte */
377 default: g_assert_not_reached();
381 case 0x01: /* addl $byte,(%ecx) */
383 case 0x3D: /* cmpl $byte,immediate-quad-indirect */
384 return 1+1+4+1; /* 83 3D immediate-quad-indirect byte */
385 default: g_assert_not_reached();
387 case 0x8A: /* mov ??,?? */
388 case 0x8B: /* mov Gb,Eb */
390 case 0x0D: /* mov immediate-quad-indirect,%ecx */
392 case 0x15: /* mov immediate-quad-indirect,%edx */
394 case 0x44: /* mov #offset(%reg,%reg,#mult),%reg */
395 case 0x4C: /* mov #offset(%reg,#mult),%reg */
396 case 0x54: /* mov #offset(%reg,#mult),%reg */
397 return 1+1+1+1; /* 8B 44 address-mode offset */
398 default: g_assert_not_reached();
400 case 0x8D: /* lea Gb,M */
402 case 0x44: /* mov #offset(%reg,%reg,#mult),%reg */
403 return 4; /* 8B 44 address-mode offset */
404 default: g_assert_not_reached();
406 case 0x8F: /* lea Gb,M */
410 case 0x24: /* popl (%esp,1) */
412 default: g_assert_not_reached();
414 default: g_assert_not_reached();
416 case 0x9C: /* pushf */
418 case 0x9D: /* popf */
420 case 0xA1: /* mov immediate-quad-indirect,%eax */
422 case 0xB8: /* mov $immediate-quad,%eax */
424 case 0xC2: /* ret $immediate-double */
426 case 0xCC: /* int $0x3 */
432 case 0xFF: /* inc/dec Ev */
434 default: g_assert_not_reached();
436 g_assert_not_reached(); /* TODO:fill the table */
442 /* A lot of offsets in the structures are relative to ModuleObject->Base */
443 #define MODULEOBJECT_BASE_OFFSET_PLUS(addr) ((gpointer)((char *)addr+(ptrdiff_t)ModuleObject->Base))
445 static void captive_ModuleList_patch_function_disable
446 (CHAR *funcname,PVOID *ExportAddressp,MODULE_OBJECT *ModuleObject /* user_data */)
450 g_return_if_fail(funcname!=NULL);
451 g_return_if_fail(ExportAddressp!=NULL);
452 g_return_if_fail(ModuleObject!=NULL);
454 ExportAddress=(PVOID)MODULEOBJECT_BASE_OFFSET_PLUS(*ExportAddressp);
455 g_return_if_fail(ExportAddress!=NULL);
457 /* Some alised name; safe to ignore as we checked for possible W32 binary 0xF4
458 * hits during our initialization in captive_ModuleList_patch().
460 if (0xF4 /* hlt */ ==*(guint8 *)ExportAddress)
463 captive_ModuleList_function_disable_hash_init();
465 *(guint8 *)ExportAddress=0xF4; /* hlt */
466 /* We may get aliased here; already existing entry is therefore allowed. */
467 g_hash_table_insert(captive_ModuleList_function_disable_hash,
468 ExportAddress, /* key */
469 funcname); /* value */
474 * captive_ModuleList_patch:
475 * @FullName_utf8: String to find #PMODULE_OBJECT by #FullName.
476 * @...: (const gchar *sym_name,void (*sym_val)(void),struct captive_ModuleList_patchpoint *patchpoint) symbol list terminated by %NULL.
478 * Patches existing @FullName_utf8 module to use for function named #sym_name
479 * pointer to the handler #sym_val. If #patchpoint is not %NULL it gets assigned the original
480 * pointer value (used for %pass keyword in #exports.captivesym).
482 * Put here 0xF4 'hlt' instead of 0xCC 'int $0x3; breakpoint'
483 * as 'hlt' will generate handled SIGSEGV instead of SIGTRAP which
484 * is used by gdb(1) during debugging.
485 * See also libcaptive/ps/signal.c/ sigaction_SIGSEGV().
487 * Returns: %TRUE if the module was successfuly added.
489 gboolean captive_ModuleList_patch(const gchar *FullName_utf8,...)
491 MODULE_OBJECT *ModuleObject;
493 IMAGE_EXPORT_DIRECTORY *ExportDir;
494 ptrdiff_t *NameList; /* filled by sym_name; MODULEOBJECT_BASE_OFFSET_PLUS()ed */
495 WORD *OrdinalList; /* linear list - libcaptive doesn't support Ordinals */
496 ptrdiff_t *FunctionList; /* filled by sym_val; MODULEOBJECT_BASE_OFFSET_PLUS()ed */
499 PVOID ExportAddress,*ExportAddressp;
500 const gchar *sym_name;
501 void (*sym_val)(void);
502 struct captive_ModuleList_patchpoint *patchpoint;
503 GHashTable *exportdir_hash;
506 g_return_val_if_fail(FullName_utf8!=NULL,FALSE);
508 captive_ModuleList_patchpoint_hash_init();
510 ModuleObject=LdrGetModuleObject(captive_utf8_to_UnicodeString_alloca(g_path_get_basename(FullName_utf8)));
511 g_return_val_if_fail(ModuleObject!=NULL,FALSE);
513 ExportDir=(PIMAGE_EXPORT_DIRECTORY)RtlImageDirectoryEntryToData(ModuleObject->Base,
514 TRUE,IMAGE_DIRECTORY_ENTRY_EXPORT,&ExportDirSize);
515 g_return_val_if_fail(ExportDir!=NULL,FALSE);
517 NameList =MODULEOBJECT_BASE_OFFSET_PLUS(ExportDir->AddressOfNames);
518 OrdinalList =MODULEOBJECT_BASE_OFFSET_PLUS(ExportDir->AddressOfNameOrdinals);
519 FunctionList=MODULEOBJECT_BASE_OFFSET_PLUS(ExportDir->AddressOfFunctions);
521 /* map (CHAR *)funcname->(PVOID *)ExportAddressp */
522 exportdir_hash=g_hash_table_new(g_str_hash,g_str_equal);
524 /* Initialize 'exportdir_hash' by all W32 exported functions. */
525 for (Idx=0;Idx<ExportDir->NumberOfNames;Idx++) {
526 CHAR *funcname=MODULEOBJECT_BASE_OFFSET_PLUS(NameList[Idx]);
528 ExportAddressp=(PVOID *)(FunctionList+OrdinalList[Idx]);
529 ExportAddress=(PVOID)MODULEOBJECT_BASE_OFFSET_PLUS(*ExportAddressp);
530 if (0xF4 /* hlt */ ==*(guint8 *)ExportAddress) {
531 /* FIXME: 'data' type symbols should be permitted to have 0xF4 byte */
532 g_error("%s: Function already patched although we did not touch it yet: %s",G_STRLOC,funcname);
533 g_assert_not_reached();
535 g_assert(NULL==g_hash_table_lookup(exportdir_hash,funcname));
536 g_hash_table_insert(exportdir_hash,
538 ExportAddressp); /* value */
541 /* Patch wished functions and remove them from 'exportdir_hash'. */
542 va_start(ap,FullName_utf8);
543 while (captive_va_arg(sym_name,ap)) {
544 captive_va_arg(sym_val ,ap);
545 /* 'sym_val' may be NULL if 'data' type && "pass"ed */
546 captive_va_arg(patchpoint,ap); /* 'data' type if ==NULL */
547 ExportAddressp=g_hash_table_lookup(exportdir_hash,sym_name);
548 if (ExportAddressp==NULL) {
549 g_error("%s: Function not found for patchpoint: %s",G_STRLOC,sym_name);
550 g_assert_not_reached();
552 errbool=g_hash_table_remove(exportdir_hash,sym_name);
553 g_assert(errbool==TRUE);
554 if (!sym_val) { /* 'data' type && "pass"ed => do not corrupt it by 0xF4 */
555 g_assert(!patchpoint);
558 ExportAddress=(PVOID)MODULEOBJECT_BASE_OFFSET_PLUS(*ExportAddressp);
559 *ExportAddressp=(PVOID)MODULEOBJECT_BASE_OFFSET_MINUS(sym_val);
560 if (((ULONG)ExportAddress >= (ULONG)ExportDir) &&
561 ((ULONG)ExportAddress < (ULONG)ExportDir + ExportDirSize))
562 g_assert_not_reached(); /* LdrPEFixupForward() needed */
563 if (!patchpoint) /* 'data' type && !"pass"ed => do not corrupt it by 0xF4 */
565 patchpoint->orig_w32_func=ExportAddress;
566 if (0xF4 /* hlt */ ==*patchpoint->orig_w32_func) /* Already patched by name-aliased function? */
568 g_assert(0xF4 /* hlt */ !=*patchpoint->orig_w32_func);
569 patchpoint->orig_w32_2ndinstr=patchpoint->orig_w32_func
570 +instruction_length((guint8 *)patchpoint->orig_w32_func);
571 g_assert(0xF4 /* hlt */ !=*patchpoint->orig_w32_2ndinstr);
572 patchpoint->wrap_wrap_func=sym_val;
573 patchpoint->orig_w32_func_byte=*patchpoint->orig_w32_func;
574 patchpoint->orig_w32_2ndinstr_byte=*patchpoint->orig_w32_2ndinstr;
575 patchpoint->through_w32_func=FALSE;
576 g_assert(NULL==g_hash_table_lookup(captive_ModuleList_patchpoint_hash,patchpoint->orig_w32_func));
577 g_hash_table_insert(captive_ModuleList_patchpoint_hash,
578 patchpoint->orig_w32_func, /* key */
579 patchpoint); /* value */
580 g_assert(NULL==g_hash_table_lookup(captive_ModuleList_patchpoint_hash,patchpoint->orig_w32_2ndinstr));
581 g_hash_table_insert(captive_ModuleList_patchpoint_hash,
582 patchpoint->orig_w32_2ndinstr, /* key */
583 patchpoint); /* value */
584 *(guint8 *)ExportAddress=0xF4; /* hlt */
588 /* The remaining entries of 'exportdir_hash' are W32 native functions
589 * unspecified by .captivesym file; we patch them as not-implemented ones.
591 g_hash_table_foreach(exportdir_hash,
592 (GHFunc)captive_ModuleList_patch_function_disable, /* func */
593 ModuleObject); /* used_data; unused */
595 g_hash_table_destroy(exportdir_hash);
597 #undef MODULEOBJECT_BASE_OFFSET_PLUS /* no longer valid */
598 #undef MODULEOBJECT_BASE_OFFSET_MINUS /* no longer valid */
603 struct captive_ModuleList_patchpoint *captive_ModuleList_patchpoint_find(gconstpointer ExportAddress)
605 struct captive_ModuleList_patchpoint *r;
607 g_return_val_if_fail(ExportAddress!=NULL,NULL);
609 captive_ModuleList_patchpoint_hash_init();
611 r=g_hash_table_lookup(captive_ModuleList_patchpoint_hash,ExportAddress);
612 g_return_val_if_fail(r!=NULL,NULL);
613 g_assert(r->orig_w32_func==ExportAddress || r->orig_w32_2ndinstr==ExportAddress);
619 G_CONST_RETURN gchar *captive_ModuleList_function_disable_find(gconstpointer ExportAddress)
621 g_return_val_if_fail(ExportAddress!=NULL,NULL);
623 return g_hash_table_lookup(captive_ModuleList_function_disable_hash,ExportAddress); /* funcname */
627 void *captive_Module_GetExportAddress(const gchar *ModuleName_utf8,const gchar *FunctionName)
629 MODULE_OBJECT *ModuleObject;
632 g_return_val_if_fail(ModuleName_utf8!=NULL,NULL);
633 g_return_val_if_fail(FunctionName!=NULL,NULL);
635 ModuleObject=LdrGetModuleObject(captive_utf8_to_UnicodeString_alloca(g_path_get_basename(ModuleName_utf8)));
636 g_return_val_if_fail(ModuleObject!=NULL,NULL);
638 r=LdrGetExportAddress(
639 ModuleObject, /* ModuleObject */
640 (/* de-const */char *)FunctionName, /* Name */
642 g_return_val_if_fail(r!=NULL,NULL);