4 * Copyright 1995 Alexandre Julliard
11 #include <sys/types.h>
20 #include "stackframe.h"
24 /* #define DEBUG_MODULE */
28 static HMODULE hFirstModule = 0;
29 static HMODULE hCachedModule = 0; /* Module cached by MODULE_OpenFile */
32 /***********************************************************************
35 * Create the built-in modules.
37 BOOL MODULE_Init(void)
39 extern void load_entrypoints( HMODULE );
43 SEGTABLEENTRY *pSegTable;
44 struct dll_table_s *table;
48 /* Create the built-in modules */
50 for (i = 0, table = dll_builtin_table; i < N_BUILTINS; i++, table++)
52 if (!table->used) continue;
54 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, table->module_start,
55 table->module_end - table->module_start,
56 0, FALSE, FALSE, FALSE );
57 if (!hModule) return FALSE;
58 FarSetOwner( hModule, hModule );
60 table->hModule = hModule;
62 dprintf_module( stddeb, "Built-in %s: hmodule=%04x\n",
63 table->name, hModule );
65 /* Allocate the code segment */
67 pModule = (NE_MODULE *)GlobalLock( hModule );
68 pSegTable = NE_SEG_TABLE( pModule );
70 pSegTable->selector = GLOBAL_CreateBlock(GMEM_FIXED, table->code_start,
71 pSegTable->minsize, hModule,
73 if (!pSegTable->selector) return FALSE;
76 /* Allocate the data segment */
78 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, pSegTable->minsize,
79 hModule, FALSE, FALSE, FALSE );
80 if (!pSegTable->selector) return FALSE;
81 memcpy( GlobalLock( pSegTable->selector ), table->data_start,
84 pModule->next = hFirstModule;
85 hFirstModule = hModule;
86 load_entrypoints( hModule );
89 /* Initialize some KERNEL exported values */
91 if (!(hModule = GetModuleHandle( "KERNEL" ))) return TRUE;
93 /* KERNEL.178: __WINFLAGS */
94 MODULE_SetEntryPoint( hModule, 178, GetWinFlags() );
96 /* Allocate 7 64k segments for 0000, A000, B000, C000, D000, E000, F000. */
98 dosmem = malloc( 0x70000 );
100 MODULE_SetEntryPoint( hModule, 183, /* KERNEL.183: __0000H */
101 GLOBAL_CreateBlock( GMEM_FIXED, dosmem,
102 0x10000, hModule, FALSE, FALSE, FALSE ) );
103 MODULE_SetEntryPoint( hModule, 193, /* KERNEL.193: __0040H */
104 GLOBAL_CreateBlock( GMEM_FIXED, dosmem + 0x400,
105 0x100, hModule, FALSE, FALSE, FALSE ) );
106 MODULE_SetEntryPoint( hModule, 174, /* KERNEL.174: __A000H */
107 GLOBAL_CreateBlock( GMEM_FIXED, dosmem + 0x10000,
108 0x10000, hModule, FALSE, FALSE, FALSE ) );
109 MODULE_SetEntryPoint( hModule, 181, /* KERNEL.181: __B000H */
110 GLOBAL_CreateBlock( GMEM_FIXED, dosmem + 0x20000,
111 0x10000, hModule, FALSE, FALSE, FALSE ) );
112 MODULE_SetEntryPoint( hModule, 182, /* KERNEL.182: __B800H */
113 GLOBAL_CreateBlock( GMEM_FIXED, dosmem + 0x28000,
114 0x10000, hModule, FALSE, FALSE, FALSE ) );
115 MODULE_SetEntryPoint( hModule, 195, /* KERNEL.195: __C000H */
116 GLOBAL_CreateBlock( GMEM_FIXED, dosmem + 0x30000,
117 0x10000, hModule, FALSE, FALSE, FALSE ) );
118 MODULE_SetEntryPoint( hModule, 179, /* KERNEL.179: __D000H */
119 GLOBAL_CreateBlock( GMEM_FIXED, dosmem + 0x40000,
120 0x10000, hModule, FALSE, FALSE, FALSE ) );
121 MODULE_SetEntryPoint( hModule, 190, /* KERNEL.190: __E000H */
122 GLOBAL_CreateBlock( GMEM_FIXED, dosmem + 0x50000,
123 0x10000, hModule, FALSE, FALSE, FALSE ) );
124 MODULE_SetEntryPoint( hModule, 173, /* KERNEL.173: __ROMBIOS */
125 GLOBAL_CreateBlock( GMEM_FIXED, dosmem + 0x60000,
126 0x10000, hModule, FALSE, FALSE, FALSE ) );
127 MODULE_SetEntryPoint( hModule, 194, /* KERNEL.194: __F000H */
128 GLOBAL_CreateBlock( GMEM_FIXED, dosmem + 0x60000,
129 0x10000, hModule, FALSE, FALSE, FALSE ) );
135 /***********************************************************************
138 void MODULE_PrintModule( HMODULE hmodule )
144 NE_MODULE *pModule = (NE_MODULE *)GlobalLock( hmodule );
146 /* Dump the module info */
148 printf( "Module %04x:\n", hmodule );
149 printf( "count=%d flags=%04x heap=%d stack=%d\n",
150 pModule->count, pModule->flags,
151 pModule->heap_size, pModule->stack_size );
152 printf( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
153 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
154 pModule->seg_count, pModule->modref_count );
155 printf( "os_flags=%d swap_area=%d version=%04x\n",
156 pModule->os_flags, pModule->min_swap_area,
157 pModule->expected_version );
159 /* Dump the file info */
161 printf( "Filename: '%s'\n",
162 ((LOADEDFILEINFO *)((BYTE *)pModule + pModule->fileinfo))->filename );
164 /* Dump the segment table */
166 printf( "\nSegment table:\n" );
167 pSeg = NE_SEG_TABLE( pModule );
168 for (i = 0; i < pModule->seg_count; i++, pSeg++)
169 printf( "%02x: pos=%d size=%d flags=%04x minsize=%d sel=%04x\n",
170 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
171 pSeg->minsize, pSeg->selector );
173 /* Dump the resource table */
175 printf( "\nResource table:\n" );
176 if (pModule->res_table)
178 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
179 printf( "Alignment: %d\n", *pword++ );
182 struct resource_typeinfo_s *ptr = (struct resource_typeinfo_s *)pword;
183 struct resource_nameinfo_s *pname = (struct resource_nameinfo_s *)(ptr + 1);
184 printf( "id=%04x count=%d\n", ptr->type_id, ptr->count );
185 for (i = 0; i < ptr->count; i++, pname++)
186 printf( "offset=%d len=%d id=%04x\n",
187 pname->offset, pname->length, pname->id );
188 pword = (WORD *)pname;
191 else printf( "None\n" );
193 /* Dump the resident name table */
195 printf( "\nResident-name table:\n" );
196 pstr = (char *)pModule + pModule->name_table;
199 printf( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
200 *(WORD *)(pstr + *pstr + 1) );
201 pstr += *pstr + 1 + sizeof(WORD);
204 /* Dump the module reference table */
206 printf( "\nModule ref table:\n" );
207 if (pModule->modref_table)
209 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
210 for (i = 0; i < pModule->modref_count; i++, pword++)
212 char *name = (char *)pModule + pModule->import_table + *pword;
213 printf( "%d: %04x -> '%*.*s'\n",
214 i, *pword, *name, *name, name + 1 );
217 else printf( "None\n" );
219 /* Dump the entry table */
221 printf( "\nEntry table:\n" );
222 pstr = (char *)pModule + pModule->entry_table;
226 printf( "Bundle %d-%d: %02x\n", ordinal, ordinal + *pstr - 1, pstr[1]);
232 else if ((BYTE)pstr[1] == 0xff) /* moveable */
234 struct entry_tab_movable_s *pe = (struct entry_tab_movable_s*)(pstr+2);
235 for (i = 0; i < *pstr; i++, pe++)
236 printf( "%d: %02x:%04x (moveable)\n",
237 ordinal++, pe->seg_number, pe->offset );
242 struct entry_tab_fixed_s *pe = (struct entry_tab_fixed_s*)(pstr+2);
243 for (i = 0; i < *pstr; i++, pe++)
244 printf( "%d: %04x (fixed)\n",
245 ordinal++, pe->offset[0] + (pe->offset[1] << 8) );
250 /* Dump the non-resident names table */
252 printf( "\nNon-resident names table:\n" );
253 if (pModule->nrname_handle)
255 pstr = (char *)GlobalLock( pModule->nrname_handle );
258 printf( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
259 *(WORD *)(pstr + *pstr + 1) );
260 pstr += *pstr + 1 + sizeof(WORD);
267 /***********************************************************************
270 int MODULE_OpenFile( HMODULE hModule )
275 static int cachedfd = -1;
277 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
278 dprintf_module( stddeb, "MODULE_OpenFile(%04x) cache: mod=%04x fd=%d\n",
279 hModule, hCachedModule, cachedfd );
280 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return -1;
281 if (hCachedModule == hModule) return cachedfd;
283 hCachedModule = hModule;
284 name = ((LOADEDFILEINFO*)((char*)pModule + pModule->fileinfo))->filename;
285 cachedfd = open( DOS_GetUnixFileName( name ), O_RDONLY );
286 dprintf_module( stddeb, "MODULE_OpenFile: opened '%s' -> %d\n",
292 /***********************************************************************
293 * MODULE_CreateSegments
295 static BOOL MODULE_CreateSegments( HMODULE hModule )
297 SEGTABLEENTRY *pSegment;
301 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return FALSE;
302 pSegment = NE_SEG_TABLE( pModule );
303 for (i = 1; i <= pModule->seg_count; i++, pSegment++)
305 minsize = pSegment->minsize ? pSegment->minsize : 0x10000;
306 if (i == pModule->ss) minsize += pModule->stack_size;
307 if (i == pModule->dgroup)
310 /* FIXME: this is needed because heap growing is not implemented */
311 pModule->heap_size = 0x10000 - minsize;
313 /* The DGROUP is allocated by MODULE_CreateInstance */
316 pSegment->selector = GLOBAL_Alloc( GMEM_ZEROINIT | GMEM_FIXED,
318 !(pSegment->flags & NE_SEGFLAGS_DATA),
320 FALSE /*pSegment->flags & NE_SEGFLAGS_READONLY*/ );
321 if (!pSegment->selector) return FALSE;
324 pModule->dgroup_entry = pModule->dgroup ? pModule->seg_table +
325 (pModule->dgroup - 1) * sizeof(SEGTABLEENTRY) : 0;
330 /***********************************************************************
333 static HINSTANCE MODULE_GetInstance( HMODULE hModule )
335 SEGTABLEENTRY *pSegment;
338 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
339 if (pModule->dgroup == 0) return hModule;
341 pSegment = NE_SEG_TABLE( pModule ) + pModule->dgroup - 1;
343 return pSegment->selector;
347 /***********************************************************************
348 * MODULE_CreateInstance
350 static HINSTANCE MODULE_CreateInstance( HMODULE hModule, LOADPARAMS *params )
352 SEGTABLEENTRY *pSegment;
355 HINSTANCE hNewInstance, hPrevInstance;
357 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
358 if (pModule->dgroup == 0) return hModule;
360 pSegment = NE_SEG_TABLE( pModule ) + pModule->dgroup - 1;
361 hPrevInstance = pSegment->selector;
363 /* if it's a library, create a new instance only the first time */
366 if (pModule->flags & NE_FFLAGS_LIBMODULE) return hPrevInstance;
367 if (params == (LOADPARAMS*)-1) return hPrevInstance;
370 minsize = pSegment->minsize ? pSegment->minsize : 0x10000;
371 if (pModule->ss == pModule->dgroup) minsize += pModule->stack_size;
372 minsize += pModule->heap_size;
373 hNewInstance = GLOBAL_Alloc( GMEM_ZEROINIT | GMEM_FIXED,
374 minsize, hModule, FALSE, FALSE, FALSE );
375 if (!hNewInstance) return 0;
376 pSegment->selector = hNewInstance;
381 /***********************************************************************
382 * MODULE_LoadExeHeader
384 HMODULE MODULE_LoadExeHeader( int fd, OFSTRUCT *ofs )
386 struct mz_header_s mz_header;
387 struct ne_header_s ne_header;
392 char *buffer, *fastload = NULL;
393 int fastload_offset = 0, fastload_length = 0;
395 /* Read a block from either the file or the fast-load area. */
396 #define READ(offset,size,buffer) \
397 ((fastload && ((offset) >= fastload_offset) && \
398 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
399 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
400 (lseek( fd, mz_header.ne_offset+(offset), SEEK_SET), \
401 read( fd, (buffer), (size) ) == (size)))
403 lseek( fd, 0, SEEK_SET );
404 if ((read( fd, &mz_header, sizeof(mz_header) ) != sizeof(mz_header)) ||
405 (mz_header.mz_magic != MZ_SIGNATURE)) return 11; /* invalid exe */
407 lseek( fd, mz_header.ne_offset, SEEK_SET );
408 if (read( fd, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
409 return 11; /* invalid exe */
411 if (ne_header.ne_magic == PE_SIGNATURE) return 21; /* win32 exe */
412 if (ne_header.ne_magic != NE_SIGNATURE) return 11; /* invalid exe */
414 /* We now have a valid NE header */
416 size = sizeof(NE_MODULE) +
417 /* loaded file info */
418 sizeof(LOADEDFILEINFO) + strlen(ofs->szPathName) +
420 ne_header.n_segment_tab * sizeof(SEGTABLEENTRY) +
422 ne_header.rname_tab_offset - ne_header.resource_tab_offset +
423 /* resident names table */
424 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset +
425 /* module ref table */
426 ne_header.n_mod_ref_tab * sizeof(WORD) +
427 /* imported names table */
428 ne_header.entry_tab_offset - ne_header.iname_tab_offset +
429 /* entry table length */
430 ne_header.entry_tab_length;
432 hModule = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
433 if (!hModule) return 11; /* invalid exe */
434 FarSetOwner( hModule, hModule );
435 pModule = (NE_MODULE *)GlobalLock( hModule );
436 memcpy( pModule, &ne_header, sizeof(NE_MODULE) );
438 pData = (BYTE *)(pModule + 1);
440 /* Read the fast-load area */
442 if (ne_header.additional_flags & NE_AFLAGS_FASTLOAD)
444 fastload_offset=ne_header.fastload_offset<<ne_header.align_shift_count;
445 fastload_length=ne_header.fastload_length<<ne_header.align_shift_count;
446 dprintf_module( stddeb, "Using fast-load area offset=%x len=%d\n",
447 fastload_offset, fastload_length );
448 if ((fastload = (char *)malloc( fastload_length )) != NULL)
450 lseek( fd, mz_header.ne_offset + fastload_offset, SEEK_SET );
451 if (read( fd, fastload, fastload_length ) != fastload_length)
459 /* Store the filename information */
461 pModule->fileinfo = (int)pData - (int)pModule;
462 ((LOADEDFILEINFO*)pData)->length = sizeof(LOADEDFILEINFO)+strlen(ofs->szPathName);
463 ((LOADEDFILEINFO*)pData)->fixed_media = TRUE;
464 ((LOADEDFILEINFO*)pData)->error = 0;
465 ((LOADEDFILEINFO*)pData)->date = 0;
466 ((LOADEDFILEINFO*)pData)->time = 0;
467 strcpy( ((LOADEDFILEINFO*)pData)->filename, ofs->szPathName );
468 pData += ((LOADEDFILEINFO*)pData)->length--;
470 /* Get the segment table */
472 pModule->seg_table = (int)pData - (int)pModule;
473 buffer = malloc( ne_header.n_segment_tab * sizeof(struct ne_segment_table_entry_s) );
477 struct ne_segment_table_entry_s *pSeg;
479 if (!READ( ne_header.segment_tab_offset,
480 ne_header.n_segment_tab * sizeof(struct ne_segment_table_entry_s),
481 buffer )) return 11; /* invalid exe */
482 pSeg = (struct ne_segment_table_entry_s *)buffer;
483 for (i = ne_header.n_segment_tab; i > 0; i--, pSeg++)
485 memcpy( pData, pSeg, sizeof(*pSeg) );
486 pData += sizeof(SEGTABLEENTRY);
490 else return 11; /* invalid exe */
492 /* Get the resource table */
494 if (ne_header.resource_tab_offset < ne_header.rname_tab_offset)
496 pModule->res_table = (int)pData - (int)pModule;
497 if (!READ(ne_header.resource_tab_offset,
498 ne_header.rname_tab_offset - ne_header.resource_tab_offset,
499 pData )) return 11; /* invalid exe */
500 pData += ne_header.rname_tab_offset - ne_header.resource_tab_offset;
502 else pModule->res_table = 0; /* No resource table */
504 /* Get the resident names table */
506 pModule->name_table = (int)pData - (int)pModule;
507 if (!READ( ne_header.rname_tab_offset,
508 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset,
509 pData )) return 11; /* invalid exe */
510 pData += ne_header.moduleref_tab_offset - ne_header.rname_tab_offset;
512 /* Get the module references table */
514 if (ne_header.n_mod_ref_tab > 0)
516 pModule->modref_table = (int)pData - (int)pModule;
517 if (!READ( ne_header.moduleref_tab_offset,
518 ne_header.n_mod_ref_tab * sizeof(WORD),
519 pData )) return 11; /* invalid exe */
520 pData += ne_header.n_mod_ref_tab * sizeof(WORD);
522 else pModule->modref_table = 0; /* No module references */
524 /* Get the imported names table */
526 pModule->import_table = (int)pData - (int)pModule;
527 if (!READ( ne_header.iname_tab_offset,
528 ne_header.entry_tab_offset - ne_header.iname_tab_offset,
529 pData )) return 11; /* invalid exe */
530 pData += ne_header.entry_tab_offset - ne_header.iname_tab_offset;
532 /* Get the entry table */
534 pModule->entry_table = (int)pData - (int)pModule;
535 if (!READ( ne_header.entry_tab_offset,
536 ne_header.entry_tab_length,
537 pData )) return 11; /* invalid exe */
538 pData += ne_header.entry_tab_length;
540 /* Get the non-resident names table */
542 if (ne_header.nrname_tab_length)
544 pModule->nrname_handle = GLOBAL_Alloc( 0, ne_header.nrname_tab_length,
545 hModule, FALSE, FALSE, FALSE );
546 if (!pModule->nrname_handle) return 11; /* invalid exe */
547 buffer = GlobalLock( pModule->nrname_handle );
548 lseek( fd, ne_header.nrname_tab_offset, SEEK_SET );
549 if (read( fd, buffer, ne_header.nrname_tab_length )
550 != ne_header.nrname_tab_length) return 11; /* invalid exe */
552 else pModule->nrname_handle = 0;
554 /* Allocate a segment for the implicitly-loaded DLLs */
556 if (pModule->modref_count)
558 pModule->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,
559 (pModule->modref_count+1)*sizeof(HMODULE),
560 hModule, FALSE, FALSE, FALSE );
561 if (!pModule->dlls_to_init) return 11; /* invalid exe */
563 else pModule->dlls_to_init = 0;
565 if (debugging_module) MODULE_PrintModule( hModule );
566 pModule->next = hFirstModule;
567 hFirstModule = hModule;
572 /***********************************************************************
575 * Lookup the ordinal for a given name.
577 WORD MODULE_GetOrdinal( HMODULE hModule, char *name )
579 char buffer[256], *cpnt;
583 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
585 dprintf_module( stddeb, "MODULE_GetOrdinal(%04x,'%s')\n",
588 /* First handle names of the form '#xxxx' */
590 if (name[0] == '#') return atoi( name + 1 );
592 /* Now copy and uppercase the string */
594 strcpy( buffer, name );
596 len = strlen( buffer );
598 /* First search the resident names */
600 cpnt = (char *)pModule + pModule->name_table;
602 /* Skip the first entry (module name) */
603 cpnt += *cpnt + 1 + sizeof(WORD);
606 dprintf_module( stddeb, " Checking '%*.*s'\n", *cpnt, *cpnt, cpnt+1 );
607 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
609 dprintf_module( stddeb, " Found: ordinal=%d\n",
610 *(WORD *)(cpnt + *cpnt + 1) );
611 return *(WORD *)(cpnt + *cpnt + 1);
613 cpnt += *cpnt + 1 + sizeof(WORD);
616 /* Now search the non-resident names table */
618 if (!pModule->nrname_handle) return 0; /* No non-resident table */
619 cpnt = (char *)GlobalLock( pModule->nrname_handle );
621 /* Skip the first entry (module description string) */
622 cpnt += *cpnt + 1 + sizeof(WORD);
625 dprintf_module( stddeb, " Checking '%*.*s'\n", *cpnt, *cpnt, cpnt+1 );
626 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
628 dprintf_module( stddeb, " Found: ordinal=%d\n",
629 *(WORD *)(cpnt + *cpnt + 1) );
630 return *(WORD *)(cpnt + *cpnt + 1);
632 cpnt += *cpnt + 1 + sizeof(WORD);
638 /***********************************************************************
639 * MODULE_GetEntryPoint
641 * Return the entry point for a given ordinal.
643 DWORD MODULE_GetEntryPoint( HMODULE hModule, WORD ordinal )
650 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
652 p = (BYTE *)pModule + pModule->entry_table;
653 while (*p && (curOrdinal + *p <= ordinal))
655 /* Skipping this bundle */
659 case 0: p += 2; break; /* unused */
660 case 0xff: p += 2 + *p * 6; break; /* moveable */
661 default: p += 2 + *p * 3; break; /* fixed */
670 case 0xff: /* moveable */
671 p += 2 + 6 * (ordinal - curOrdinal);
673 offset = *(WORD *)(p + 4);
677 p += 2 + 3 * (ordinal - curOrdinal);
678 offset = *(WORD *)(p + 1);
682 if (sel == 0xfe) sel = 0xffff; /* constant entry */
683 else sel = NE_SEG_TABLE(pModule)[sel-1].selector;
684 return MAKELONG( offset, sel );
688 /***********************************************************************
689 * MODULE_SetEntryPoint
691 * Change the value of an entry point. Use with caution!
692 * It can only change the offset value, not the selector.
694 BOOL MODULE_SetEntryPoint( HMODULE hModule, WORD ordinal, WORD offset )
700 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return FALSE;
702 p = (BYTE *)pModule + pModule->entry_table;
703 while (*p && (curOrdinal + *p <= ordinal))
705 /* Skipping this bundle */
709 case 0: p += 2; break; /* unused */
710 case 0xff: p += 2 + *p * 6; break; /* moveable */
711 default: p += 2 + *p * 3; break; /* fixed */
714 if (!*p) return FALSE;
720 case 0xff: /* moveable */
721 p += 2 + 6 * (ordinal - curOrdinal);
722 *(WORD *)(p + 4) = offset;
725 p += 2 + 3 * (ordinal - curOrdinal);
726 *(WORD *)(p + 1) = offset;
733 /***********************************************************************
734 * MODULE_GetEntryPointName
736 * Return the entry point name for a given ordinal.
737 * Used only by relay debugging.
738 * Warning: returned pointer is to a Pascal-type string.
740 LPSTR MODULE_GetEntryPointName( HMODULE hModule, WORD ordinal )
745 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
747 /* First search the resident names */
749 cpnt = (char *)pModule + pModule->name_table;
752 cpnt += *cpnt + 1 + sizeof(WORD);
753 if (*(WORD *)(cpnt + *cpnt + 1) == ordinal) return cpnt;
756 /* Now search the non-resident names table */
758 if (!pModule->nrname_handle) return 0; /* No non-resident table */
759 cpnt = (char *)GlobalLock( pModule->nrname_handle );
762 cpnt += *cpnt + 1 + sizeof(WORD);
763 if (*(WORD *)(cpnt + *cpnt + 1) == ordinal) return cpnt;
769 /***********************************************************************
770 * MODULE_GetModuleName
772 LPSTR MODULE_GetModuleName( HMODULE hModule )
776 static char buffer[10];
778 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return NULL;
779 p = (BYTE *)pModule + pModule->name_table;
781 memcpy( buffer, p + 1, len );
787 /**********************************************************************
790 * Find a module from a path name.
792 HMODULE MODULE_FindModule( LPCSTR path )
794 HMODULE hModule = hFirstModule;
795 LPCSTR filename, dotptr, modulepath, modulename;
796 BYTE len, *name_table;
798 if (!(filename = strrchr( path, '\\' ))) filename = path;
800 if ((dotptr = strrchr( filename, '.' )) != NULL)
801 len = (BYTE)(dotptr - filename);
802 else len = strlen( filename );
806 NE_MODULE *pModule = (NE_MODULE *)GlobalLock( hModule );
808 modulepath = ((LOADEDFILEINFO*)((char*)pModule + pModule->fileinfo))->filename;
809 if (!(modulename = strrchr( modulepath, '\\' )))
810 modulename = modulepath;
812 if (!strcasecmp( modulename, filename )) return hModule;
814 name_table = (BYTE *)pModule + pModule->name_table;
815 if ((*name_table == len) && !strncasecmp(filename, name_table+1, len))
817 hModule = pModule->next;
823 /**********************************************************************
826 * Remove a module from memory.
828 static void MODULE_FreeModule( HMODULE hModule )
830 HMODULE *hPrevModule;
832 SEGTABLEENTRY *pSegment;
836 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return;
838 /* FIXME: should call the exit code for the library here */
840 /* Remove it from the linked list */
842 hPrevModule = &hFirstModule;
843 while (*hPrevModule && (*hPrevModule != hModule))
845 hPrevModule = &((NE_MODULE *)GlobalLock( *hPrevModule ))->next;
847 if (*hPrevModule) *hPrevModule = pModule->next;
849 /* Free all the segments */
851 pSegment = NE_SEG_TABLE( pModule );
852 for (i = 1; i <= pModule->seg_count; i++, pSegment++)
854 GlobalFree( pSegment->selector );
857 /* Free the referenced modules */
859 pModRef = NE_MODULE_TABLE( pModule );
860 for (i = 0; i < pModule->modref_count; i++, pModRef++)
862 FreeModule( *pModRef );
865 /* Free the module storage */
867 if (pModule->nrname_handle) GlobalFree( pModule->nrname_handle );
868 if (pModule->dlls_to_init) GlobalFree( pModule->dlls_to_init );
869 GlobalFree( hModule );
871 /* Remove module from cache */
873 if (hCachedModule == hModule) hCachedModule = 0;
877 /**********************************************************************
878 * LoadModule (KERNEL.45)
880 HINSTANCE LoadModule( LPCSTR name, LPVOID paramBlock )
883 HANDLE hInstance, hPrevInstance;
885 LOADPARAMS *params = (LOADPARAMS *)paramBlock;
886 WORD *pModRef, *pDLLs;
889 hModule = MODULE_FindModule( name );
890 if (!hModule) /* We have to load the module */
893 if (strchr( name, '/' )) name = DOS_GetDosFileName( name );
894 if ((fd = OpenFile( name, &ofs, OF_READ )) == -1)
895 return 2; /* File not found */
897 /* Create the module structure */
899 if ((hModule = MODULE_LoadExeHeader( fd, &ofs )) < 32)
902 fprintf( stderr, "LoadModule: can't load '%s', error=%d\n",
906 pModule = (NE_MODULE *)GlobalLock( hModule );
908 /* Allocate the segments for this module */
910 MODULE_CreateSegments( hModule );
913 hInstance = MODULE_CreateInstance( hModule, (LOADPARAMS*)paramBlock );
915 /* Load the referenced DLLs */
917 pModRef = (WORD *)((char *)pModule + pModule->modref_table);
918 pDLLs = (WORD *)GlobalLock( pModule->dlls_to_init );
919 for (i = 0; i < pModule->modref_count; i++, pModRef++)
922 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
923 memcpy( buffer, pstr + 1, *pstr );
924 strcpy( buffer + *pstr, ".dll" );
925 dprintf_module( stddeb, "Loading '%s'\n", buffer );
926 if (!(*pModRef = MODULE_FindModule( buffer )))
928 /* If the DLL is not loaded yet, load it and store */
929 /* its handle in the list of DLLs to initialize. */
932 if ((hDLL = LoadModule( buffer, (LPVOID)-1 )) == 2) /* file not found */
936 /* Try with prepending the path of the current module */
937 GetModuleFileName( hModule, buffer, 256 );
938 if (!(p = strrchr( buffer, '\\' ))) p = buffer;
939 memcpy( p + 1, pstr + 1, *pstr );
940 strcpy( p + 1 + *pstr, ".dll" );
941 hDLL = LoadModule( buffer, (LPVOID)-1 );
945 fprintf( stderr, "Could not load '%s' required by '%s', error = %d\n",
946 buffer, name, hDLL );
947 return 2; /* file not found */
949 *pModRef = GetExePtr( hDLL );
952 else /* Increment the reference count of the DLL */
954 NE_MODULE *pOldDLL = (NE_MODULE *)GlobalLock( *pModRef );
955 if (pOldDLL) pOldDLL->count++;
959 /* Load the segments */
961 for (i = 1; i <= pModule->seg_count; i++) NE_LoadSegment( hModule, i );
963 /* Fixup the functions prologs */
965 NE_FixupPrologs( hModule );
967 /* Make sure the usage count is 1 on the first loading of */
968 /* the module, even if it contains circular DLL references */
974 pModule = (NE_MODULE *)GlobalLock( hModule );
975 hPrevInstance = MODULE_GetInstance( hModule );
976 hInstance = MODULE_CreateInstance( hModule, params );
977 if (hInstance != hPrevInstance) /* not a library */
978 NE_LoadSegment( hModule, pModule->dgroup );
982 /* Create a task for this instance */
984 if (!(pModule->flags & NE_FFLAGS_LIBMODULE) && (paramBlock != (LPVOID)-1))
986 TASK_CreateTask( hModule, hInstance, hPrevInstance,
987 params->hEnvironment,
988 (LPSTR)PTR_SEG_TO_LIN( params->cmdLine ),
989 *((WORD *)PTR_SEG_TO_LIN(params->showCmd)+1) );
996 /**********************************************************************
997 * FreeModule (KERNEL.46)
999 BOOL FreeModule( HANDLE hModule )
1003 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
1004 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return FALSE;
1006 dprintf_module( stddeb, "FreeModule: %s count %d\n",
1007 MODULE_GetModuleName(hModule), pModule->count );
1008 if (--pModule->count == 0) MODULE_FreeModule( hModule );
1013 /**********************************************************************
1014 * GetModuleHandle (KERNEL.47)
1016 HMODULE GetModuleHandle( LPCSTR name )
1018 return MODULE_FindModule( name );
1022 /**********************************************************************
1023 * GetModuleUsage (KERNEL.48)
1025 int GetModuleUsage( HANDLE hModule )
1029 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
1030 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
1031 dprintf_module( stddeb, "GetModuleUsage(%04x): returning %d\n",
1032 hModule, pModule->count );
1033 return pModule->count;
1037 /**********************************************************************
1038 * GetModuleFileName (KERNEL.49)
1040 int GetModuleFileName( HANDLE hModule, LPSTR lpFileName, short nSize )
1045 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
1046 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
1047 name = ((LOADEDFILEINFO*)((char*)pModule + pModule->fileinfo))->filename;
1048 strncpy( lpFileName, name, nSize );
1049 lpFileName[nSize-1] = '\0';
1050 dprintf_module( stddeb, "GetModuleFilename: %s\n", lpFileName );
1051 return strlen(lpFileName);
1055 /***********************************************************************
1056 * LoadLibrary (KERNEL.95)
1058 HANDLE LoadLibrary( LPCSTR libname )
1062 dprintf_module( stddeb, "LoadLibrary: (%08x) %s\n", (int)libname, libname);
1063 /* This does not increment the module reference count, and will
1064 * therefore cause crashes on FreeLibrary calls.
1065 if ((handle = MODULE_FindModule( libname )) != 0) return handle;
1067 handle = LoadModule( libname, (LPVOID)-1 );
1068 if (handle == 2) /* file not found */
1071 strcpy( buffer, libname );
1072 strcat( buffer, ".dll" );
1073 handle = LoadModule( buffer, (LPVOID)-1 );
1075 if (handle >= 32) NE_InitializeDLLs( GetExePtr(handle) );
1080 /***********************************************************************
1081 * FreeLibrary (KERNEL.96)
1083 void FreeLibrary( HANDLE handle )
1085 dprintf_module( stddeb,"FreeLibrary: %04x\n", handle );
1086 FreeModule( handle );
1090 /***********************************************************************
1091 * WinExec (KERNEL.166)
1093 HANDLE WinExec( LPSTR lpCmdLine, WORD nCmdShow )
1096 HLOCAL cmdShowHandle, cmdLineHandle;
1099 char *p, *cmdline, filename[256];
1101 if (!(cmdShowHandle = GlobalAlloc( 0, 2 * sizeof(WORD) ))) return 0;
1102 if (!(cmdLineHandle = GlobalAlloc( 0, 256 ))) return 0;
1104 /* Store nCmdShow */
1106 cmdShowPtr = (WORD *)GlobalLock( cmdShowHandle );
1108 cmdShowPtr[1] = nCmdShow;
1110 /* Build the filename and command-line */
1112 cmdline = (char *)GlobalLock( cmdLineHandle );
1113 strncpy( filename, lpCmdLine, 256 );
1114 filename[255] = '\0';
1115 for (p = filename; *p && (*p != ' ') && (*p != '\t'); p++);
1118 strncpy( cmdline, p + 1, 128 );
1119 cmdline[127] = '\0';
1121 else cmdline[0] = '\0';
1124 /* Now load the executable file */
1126 params.hEnvironment = SELECTOROF( GetDOSEnvironment() );
1127 params.cmdLine = WIN16_GlobalLock( cmdLineHandle );
1128 params.showCmd = WIN16_GlobalLock( cmdShowHandle );
1129 params.reserved = 0;
1130 handle = LoadModule( filename, ¶ms );
1131 if (handle == 2) /* file not found */
1133 strcat( filename, ".exe" );
1134 handle = LoadModule( filename, ¶ms );
1137 GlobalFree( cmdShowHandle );
1138 GlobalFree( cmdLineHandle );
1143 /***********************************************************************
1144 * GetProcAddress (KERNEL.50)
1146 FARPROC GetProcAddress( HANDLE hModule, SEGPTR name )
1151 if (!hModule) hModule = GetCurrentTask();
1152 hModule = GetExePtr( hModule );
1154 if (HIWORD(name) != 0)
1156 ordinal = MODULE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1157 dprintf_module( stddeb, "GetProcAddress: %04x '%s'\n",
1158 hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1162 ordinal = LOWORD(name);
1163 dprintf_module( stddeb, "GetProcAddress: %04x %04x\n",
1166 if (!ordinal) return (FARPROC)0;
1168 ret = MODULE_GetEntryPoint( hModule, ordinal );
1170 dprintf_module( stddeb, "GetProcAddress: returning %08lx\n", ret );
1171 return (FARPROC)ret;
1175 /**********************************************************************
1176 * ModuleFirst (TOOLHELP.59)
1178 BOOL ModuleFirst( MODULEENTRY *lpme )
1180 lpme->wNext = hFirstModule;
1181 return ModuleNext( lpme );
1185 /**********************************************************************
1186 * ModuleNext (TOOLHELP.60)
1188 BOOL ModuleNext( MODULEENTRY *lpme )
1192 if (!lpme->wNext) return FALSE;
1193 if (!(pModule = (NE_MODULE *)GlobalLock( lpme->wNext ))) return FALSE;
1194 strncpy( lpme->szModule, (char *)pModule + pModule->name_table,
1196 lpme->szModule[MAX_MODULE_NAME] = '\0';
1197 lpme->hModule = lpme->wNext;
1198 lpme->wcUsage = pModule->count;
1199 strncpy( lpme->szExePath,
1200 ((LOADEDFILEINFO*)((char*)pModule + pModule->fileinfo))->filename,
1202 lpme->szExePath[MAX_PATH] = '\0';
1203 lpme->wNext = pModule->next;
1208 /**********************************************************************
1209 * ModuleFindName (TOOLHELP.61)
1211 BOOL ModuleFindName( MODULEENTRY *lpme, LPCSTR name )
1213 lpme->wNext = GetModuleHandle( name );
1214 return ModuleNext( lpme );
1218 /**********************************************************************
1219 * ModuleFindHandle (TOOLHELP.62)
1221 BOOL ModuleFindHandle( MODULEENTRY *lpme, HMODULE hModule )
1223 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
1224 lpme->wNext = hModule;
1225 return ModuleNext( lpme );