4 * Copyright 1995 Alexandre Julliard
13 #include "wine/winbase16.h"
28 #include "stackframe.h"
31 #include "loadorder.h"
34 DEFAULT_DEBUG_CHANNEL(module)
36 FARPROC16 (*fnSNOOP16_GetProcAddress16)(HMODULE16,DWORD,FARPROC16) = NULL;
37 void (*fnSNOOP16_RegisterDLL)(NE_MODULE*,LPCSTR) = NULL;
39 #define hFirstModule (pThhook->hExeHead)
41 static NE_MODULE *pCachedModule = 0; /* Module cached by NE_OpenFile */
43 static HMODULE16 NE_LoadBuiltin(LPCSTR name,BOOL force) { return 0; }
44 HMODULE16 (*fnBUILTIN_LoadModule)(LPCSTR name,BOOL force) = NE_LoadBuiltin;
47 /***********************************************************************
50 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
52 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
56 /***********************************************************************
59 void NE_DumpModule( HMODULE16 hModule )
69 if (!(pModule = NE_GetPtr( hModule )))
71 MSG( "**** %04x is not a module handle\n", hModule );
75 /* Dump the module info */
77 DUMP( "Module %04x:\n", hModule );
78 DUMP( "count=%d flags=%04x heap=%d stack=%d\n",
79 pModule->count, pModule->flags,
80 pModule->heap_size, pModule->stack_size );
81 DUMP( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
82 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
83 pModule->seg_count, pModule->modref_count );
84 DUMP( "os_flags=%d swap_area=%d version=%04x\n",
85 pModule->os_flags, pModule->min_swap_area,
86 pModule->expected_version );
87 if (pModule->flags & NE_FFLAGS_WIN32)
88 DUMP( "PE module=%08x\n", pModule->module32 );
90 /* Dump the file info */
92 DUMP( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
94 /* Dump the segment table */
96 DUMP( "Segment table:\n" );
97 pSeg = NE_SEG_TABLE( pModule );
98 for (i = 0; i < pModule->seg_count; i++, pSeg++)
99 DUMP( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
100 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
101 pSeg->minsize, pSeg->hSeg );
103 /* Dump the resource table */
105 DUMP( "Resource table:\n" );
106 if (pModule->res_table)
108 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
109 DUMP( "Alignment: %d\n", *pword++ );
112 struct resource_typeinfo_s *ptr = (struct resource_typeinfo_s *)pword;
113 struct resource_nameinfo_s *pname = (struct resource_nameinfo_s *)(ptr + 1);
114 DUMP( "id=%04x count=%d\n", ptr->type_id, ptr->count );
115 for (i = 0; i < ptr->count; i++, pname++)
116 DUMP( "offset=%d len=%d id=%04x\n",
117 pname->offset, pname->length, pname->id );
118 pword = (WORD *)pname;
121 else DUMP( "None\n" );
123 /* Dump the resident name table */
125 DUMP( "Resident-name table:\n" );
126 pstr = (char *)pModule + pModule->name_table;
129 DUMP( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
130 *(WORD *)(pstr + *pstr + 1) );
131 pstr += *pstr + 1 + sizeof(WORD);
134 /* Dump the module reference table */
136 DUMP( "Module ref table:\n" );
137 if (pModule->modref_table)
139 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
140 for (i = 0; i < pModule->modref_count; i++, pword++)
143 GetModuleName16( *pword, name, sizeof(name) );
144 DUMP( "%d: %04x -> '%s'\n", i, *pword, name );
147 else DUMP( "None\n" );
149 /* Dump the entry table */
151 DUMP( "Entry table:\n" );
152 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->entry_table);
154 entry = (ET_ENTRY *)((BYTE *)bundle+6);
155 DUMP( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
156 ordinal = bundle->first;
157 while (ordinal < bundle->last)
159 if (entry->type == 0xff)
160 DUMP("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
162 DUMP("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
165 } while ( (bundle->next)
166 && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
168 /* Dump the non-resident names table */
170 DUMP( "Non-resident names table:\n" );
171 if (pModule->nrname_handle)
173 pstr = (char *)GlobalLock16( pModule->nrname_handle );
176 DUMP( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
177 *(WORD *)(pstr + *pstr + 1) );
178 pstr += *pstr + 1 + sizeof(WORD);
185 /***********************************************************************
188 * Walk the module list and print the modules.
190 void NE_WalkModules(void)
192 HMODULE16 hModule = hFirstModule;
193 MSG( "Module Flags Name\n" );
196 NE_MODULE *pModule = NE_GetPtr( hModule );
199 MSG( "Bad module %04x in list\n", hModule );
202 MSG( " %04x %04x %.*s\n", hModule, pModule->flags,
203 *((char *)pModule + pModule->name_table),
204 (char *)pModule + pModule->name_table + 1 );
205 hModule = pModule->next;
210 /**********************************************************************
213 void NE_RegisterModule( NE_MODULE *pModule )
215 pModule->next = hFirstModule;
216 hFirstModule = pModule->self;
220 /***********************************************************************
223 * Lookup the ordinal for a given name.
225 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
227 unsigned char buffer[256], *cpnt;
231 if (!(pModule = NE_GetPtr( hModule ))) return 0;
232 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
234 TRACE( module, "(%04x,'%s')\n", hModule, name );
236 /* First handle names of the form '#xxxx' */
238 if (name[0] == '#') return atoi( name + 1 );
240 /* Now copy and uppercase the string */
242 strcpy( buffer, name );
243 CharUpperA( buffer );
244 len = strlen( buffer );
246 /* First search the resident names */
248 cpnt = (char *)pModule + pModule->name_table;
250 /* Skip the first entry (module name) */
251 cpnt += *cpnt + 1 + sizeof(WORD);
254 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
256 TRACE(module, " Found: ordinal=%d\n",
257 *(WORD *)(cpnt + *cpnt + 1) );
258 return *(WORD *)(cpnt + *cpnt + 1);
260 cpnt += *cpnt + 1 + sizeof(WORD);
263 /* Now search the non-resident names table */
265 if (!pModule->nrname_handle) return 0; /* No non-resident table */
266 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
268 /* Skip the first entry (module description string) */
269 cpnt += *cpnt + 1 + sizeof(WORD);
272 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
274 TRACE(module, " Found: ordinal=%d\n",
275 *(WORD *)(cpnt + *cpnt + 1) );
276 return *(WORD *)(cpnt + *cpnt + 1);
278 cpnt += *cpnt + 1 + sizeof(WORD);
284 /***********************************************************************
285 * NE_GetEntryPoint (WPROCS.27)
287 * Return the entry point for a given ordinal.
289 FARPROC16 NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
291 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
293 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
301 if (!(pModule = NE_GetPtr( hModule ))) return 0;
302 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
304 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
305 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
309 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
312 entry = (ET_ENTRY *)((BYTE *)bundle+6);
313 for (i=0; i < (ordinal - bundle->first - 1); i++)
317 offset = entry->offs;
319 if (sel == 0xfe) sel = 0xffff; /* constant entry */
320 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
322 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
323 if (!snoop || !fnSNOOP16_GetProcAddress16)
324 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
326 return (FARPROC16)fnSNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset ));
330 /***********************************************************************
333 * Change the value of an entry point. Use with caution!
334 * It can only change the offset value, not the selector.
336 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
343 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
344 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
346 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
347 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
349 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
354 entry = (ET_ENTRY *)((BYTE *)bundle+6);
355 for (i=0; i < (ordinal - bundle->first - 1); i++)
358 entry->offs = offset;
363 /***********************************************************************
366 HANDLE NE_OpenFile( NE_MODULE *pModule )
370 static HANDLE cachedfd = -1;
372 TRACE( module, "(%p) cache: mod=%p fd=%d\n",
373 pModule, pCachedModule, cachedfd );
374 if (pCachedModule == pModule) return cachedfd;
375 CloseHandle( cachedfd );
376 pCachedModule = pModule;
377 name = NE_MODULE_NAME( pModule );
378 if ((cachedfd = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
379 NULL, OPEN_EXISTING, 0, -1 )) == -1)
380 MSG( "Can't open file '%s' for module %04x\n", name, pModule->self );
382 /* FIXME: should not be necessary */
383 cachedfd = ConvertToGlobalHandle(cachedfd);
384 TRACE(module, "opened '%s' -> %d\n",
390 /***********************************************************************
393 static HMODULE16 NE_LoadExeHeader( HFILE16 hFile, OFSTRUCT *ofs )
395 IMAGE_DOS_HEADER mz_header;
396 IMAGE_OS2_HEADER ne_header;
400 BYTE *pData, *pTempEntryTable;
401 char *buffer, *fastload = NULL;
402 int fastload_offset = 0, fastload_length = 0;
404 ET_BUNDLE *bundle, *oldbundle;
406 /* Read a block from either the file or the fast-load area. */
407 #define READ(offset,size,buffer) \
408 ((fastload && ((offset) >= fastload_offset) && \
409 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
410 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
411 (_llseek16( hFile, (offset), SEEK_SET), \
412 _hread16( hFile, (buffer), (size) ) == (size)))
414 _llseek16( hFile, 0, SEEK_SET );
415 if ((_hread16(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
416 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
417 return (HMODULE16)11; /* invalid exe */
419 _llseek16( hFile, mz_header.e_lfanew, SEEK_SET );
420 if (_hread16( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
421 return (HMODULE16)11; /* invalid exe */
423 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
424 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
426 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
427 MSG("Sorry, this is an OS/2 linear executable (LX) file !\n");
428 return (HMODULE16)12;
431 /* We now have a valid NE header */
433 size = sizeof(NE_MODULE) +
435 ne_header.n_segment_tab * sizeof(SEGTABLEENTRY) +
437 ne_header.rname_tab_offset - ne_header.resource_tab_offset +
438 /* resident names table */
439 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset +
440 /* module ref table */
441 ne_header.n_mod_ref_tab * sizeof(WORD) +
442 /* imported names table */
443 ne_header.entry_tab_offset - ne_header.iname_tab_offset +
444 /* entry table length */
445 ne_header.entry_tab_length +
446 /* entry table extra conversion space */
448 2 * (ne_header.entry_tab_length - ne_header.n_mov_entry_points*6) +
449 /* loaded file info */
450 sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
452 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
453 if (!hModule) return (HMODULE16)11; /* invalid exe */
454 FarSetOwner16( hModule, hModule );
455 pModule = (NE_MODULE *)GlobalLock16( hModule );
456 memcpy( pModule, &ne_header, sizeof(ne_header) );
458 /* check *programs* for default minimal stack size */
459 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
460 && (pModule->stack_size < 0x1400) )
461 pModule->stack_size = 0x1400;
462 pModule->module32 = 0;
463 pModule->self = hModule;
464 pModule->self_loading_sel = 0;
465 pData = (BYTE *)(pModule + 1);
467 /* Clear internal Wine flags in case they are set in the EXE file */
469 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
471 /* Read the fast-load area */
473 if (ne_header.additional_flags & NE_AFLAGS_FASTLOAD)
475 fastload_offset=ne_header.fastload_offset<<ne_header.align_shift_count;
476 fastload_length=ne_header.fastload_length<<ne_header.align_shift_count;
477 TRACE(module, "Using fast-load area offset=%x len=%d\n",
478 fastload_offset, fastload_length );
479 if ((fastload = HeapAlloc( SystemHeap, 0, fastload_length )) != NULL)
481 _llseek16( hFile, fastload_offset, SEEK_SET);
482 if (_hread16(hFile, fastload, fastload_length) != fastload_length)
484 HeapFree( SystemHeap, 0, fastload );
485 WARN( module, "Error reading fast-load area!\n");
491 /* Get the segment table */
493 pModule->seg_table = (int)pData - (int)pModule;
494 buffer = HeapAlloc( SystemHeap, 0, ne_header.n_segment_tab *
495 sizeof(struct ne_segment_table_entry_s));
499 struct ne_segment_table_entry_s *pSeg;
501 if (!READ( mz_header.e_lfanew + ne_header.segment_tab_offset,
502 ne_header.n_segment_tab * sizeof(struct ne_segment_table_entry_s),
505 HeapFree( SystemHeap, 0, buffer );
507 HeapFree( SystemHeap, 0, fastload );
508 GlobalFree16( hModule );
509 return (HMODULE16)11; /* invalid exe */
511 pSeg = (struct ne_segment_table_entry_s *)buffer;
512 for (i = ne_header.n_segment_tab; i > 0; i--, pSeg++)
514 memcpy( pData, pSeg, sizeof(*pSeg) );
515 pData += sizeof(SEGTABLEENTRY);
517 HeapFree( SystemHeap, 0, buffer );
522 HeapFree( SystemHeap, 0, fastload );
523 GlobalFree16( hModule );
524 return (HMODULE16)11; /* invalid exe */
527 /* Get the resource table */
529 if (ne_header.resource_tab_offset < ne_header.rname_tab_offset)
531 pModule->res_table = (int)pData - (int)pModule;
532 if (!READ(mz_header.e_lfanew + ne_header.resource_tab_offset,
533 ne_header.rname_tab_offset - ne_header.resource_tab_offset,
534 pData )) return (HMODULE16)11; /* invalid exe */
535 pData += ne_header.rname_tab_offset - ne_header.resource_tab_offset;
536 NE_InitResourceHandler( hModule );
538 else pModule->res_table = 0; /* No resource table */
540 /* Get the resident names table */
542 pModule->name_table = (int)pData - (int)pModule;
543 if (!READ( mz_header.e_lfanew + ne_header.rname_tab_offset,
544 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset,
548 HeapFree( SystemHeap, 0, fastload );
549 GlobalFree16( hModule );
550 return (HMODULE16)11; /* invalid exe */
552 pData += ne_header.moduleref_tab_offset - ne_header.rname_tab_offset;
554 /* Get the module references table */
556 if (ne_header.n_mod_ref_tab > 0)
558 pModule->modref_table = (int)pData - (int)pModule;
559 if (!READ( mz_header.e_lfanew + ne_header.moduleref_tab_offset,
560 ne_header.n_mod_ref_tab * sizeof(WORD),
564 HeapFree( SystemHeap, 0, fastload );
565 GlobalFree16( hModule );
566 return (HMODULE16)11; /* invalid exe */
568 pData += ne_header.n_mod_ref_tab * sizeof(WORD);
570 else pModule->modref_table = 0; /* No module references */
572 /* Get the imported names table */
574 pModule->import_table = (int)pData - (int)pModule;
575 if (!READ( mz_header.e_lfanew + ne_header.iname_tab_offset,
576 ne_header.entry_tab_offset - ne_header.iname_tab_offset,
580 HeapFree( SystemHeap, 0, fastload );
581 GlobalFree16( hModule );
582 return (HMODULE16)11; /* invalid exe */
584 pData += ne_header.entry_tab_offset - ne_header.iname_tab_offset;
586 /* Load entry table, convert it to the optimized version used by Windows */
588 if ((pTempEntryTable = HeapAlloc( SystemHeap, 0, ne_header.entry_tab_length)) != NULL)
590 BYTE nr_entries, type, *s;
592 TRACE(module, "Converting entry table.\n");
593 pModule->entry_table = (int)pData - (int)pModule;
594 if (!READ( mz_header.e_lfanew + ne_header.entry_tab_offset,
595 ne_header.entry_tab_length, pTempEntryTable ))
597 HeapFree( SystemHeap, 0, pTempEntryTable );
599 HeapFree( SystemHeap, 0, fastload );
600 GlobalFree16( hModule );
601 return (HMODULE16)11; /* invalid exe */
605 TRACE(module, "entry table: offs %04x, len %04x, entries %d\n", ne_header.entry_tab_offset, ne_header.entry_tab_length, *s);
607 bundle = (ET_BUNDLE *)pData;
608 TRACE(module, "first bundle: %p\n", bundle);
609 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
610 entry = (ET_ENTRY *)((BYTE *)bundle+6);
612 while ((nr_entries = *s++))
616 bundle->last += nr_entries;
623 entry->segnum = *s++;
624 entry->offs = *(WORD *)s; s += 2;
625 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
633 entry->segnum = type;
634 entry->offs = *(WORD *)s; s += 2;
635 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
641 if (bundle->first == bundle->last)
643 bundle->first += nr_entries;
644 bundle->last += nr_entries;
649 oldbundle->next = ((int)entry - (int)pModule);
650 bundle = (ET_BUNDLE *)entry;
651 TRACE(module, "new bundle: %p\n", bundle);
652 bundle->first = bundle->last =
653 oldbundle->last + nr_entries;
655 (BYTE *)entry += sizeof(ET_BUNDLE);
659 HeapFree( SystemHeap, 0, pTempEntryTable );
664 HeapFree( SystemHeap, 0, fastload );
665 GlobalFree16( hModule );
666 return (HMODULE16)11; /* invalid exe */
669 pData += ne_header.entry_tab_length + sizeof(ET_BUNDLE) +
670 2 * (ne_header.entry_tab_length - ne_header.n_mov_entry_points*6);
672 if ((DWORD)entry > (DWORD)pData)
673 ERR(module, "converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry, pData);
675 /* Store the filename information */
677 pModule->fileinfo = (int)pData - (int)pModule;
678 size = sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
679 memcpy( pData, ofs, size );
680 ((OFSTRUCT *)pData)->cBytes = size - 1;
683 /* Free the fast-load area */
687 HeapFree( SystemHeap, 0, fastload );
689 /* Get the non-resident names table */
691 if (ne_header.nrname_tab_length)
693 pModule->nrname_handle = GLOBAL_Alloc( 0, ne_header.nrname_tab_length,
694 hModule, FALSE, FALSE, FALSE );
695 if (!pModule->nrname_handle)
697 GlobalFree16( hModule );
698 return (HMODULE16)11; /* invalid exe */
700 buffer = GlobalLock16( pModule->nrname_handle );
701 _llseek16( hFile, ne_header.nrname_tab_offset, SEEK_SET );
702 if (_hread16( hFile, buffer, ne_header.nrname_tab_length )
703 != ne_header.nrname_tab_length)
705 GlobalFree16( pModule->nrname_handle );
706 GlobalFree16( hModule );
707 return (HMODULE16)11; /* invalid exe */
710 else pModule->nrname_handle = 0;
712 /* Allocate a segment for the implicitly-loaded DLLs */
714 if (pModule->modref_count)
716 pModule->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,
717 (pModule->modref_count+1)*sizeof(HMODULE16),
718 hModule, FALSE, FALSE, FALSE );
719 if (!pModule->dlls_to_init)
721 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
722 GlobalFree16( hModule );
723 return (HMODULE16)11; /* invalid exe */
726 else pModule->dlls_to_init = 0;
728 NE_RegisterModule( pModule );
729 if (fnSNOOP16_RegisterDLL)
730 fnSNOOP16_RegisterDLL(pModule,ofs->szPathName);
735 /***********************************************************************
738 * Load all DLLs implicitly linked to a module.
740 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
743 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
744 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
746 for (i = 0; i < pModule->modref_count; i++, pModRef++)
749 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
750 memcpy( buffer, pstr + 1, *pstr );
751 *(buffer + *pstr) = 0; /* terminate it */
753 TRACE(module, "Loading '%s'\n", buffer );
754 if (!(*pModRef = GetModuleHandle16( buffer )))
756 /* If the DLL is not loaded yet, load it and store */
757 /* its handle in the list of DLLs to initialize. */
760 if ((hDLL = MODULE_LoadModule16( buffer, TRUE )) < 32)
762 /* FIXME: cleanup what was done */
764 MSG( "Could not load '%s' required by '%.*s', error=%d\n",
765 buffer, *((BYTE*)pModule + pModule->name_table),
766 (char *)pModule + pModule->name_table + 1, hDLL );
769 *pModRef = GetExePtr( hDLL );
772 else /* Increment the reference count of the DLL */
776 pOldDLL = NE_GetPtr( *pModRef );
777 if (pOldDLL) pOldDLL->count++;
784 /**********************************************************************
787 * Load first instance of NE module from file.
788 * (Note: caller is responsible for ensuring the module isn't
791 static HINSTANCE16 NE_LoadFileModule( HFILE16 hFile, OFSTRUCT *ofs,
794 HINSTANCE16 hInstance;
798 /* Create the module structure */
800 hModule = NE_LoadExeHeader( hFile, ofs );
801 if (hModule < 32) return hModule;
802 pModule = NE_GetPtr( hModule );
804 /* Allocate the segments for this module */
806 if (!NE_CreateSegments( pModule ) ||
807 !(hInstance = NE_CreateInstance( pModule, NULL, FALSE )))
809 GlobalFreeAll16( hModule );
810 return 8; /* Insufficient memory */
813 /* Load the referenced DLLs */
815 if (!NE_LoadDLLs( pModule ))
816 return 2; /* File not found (FIXME: free everything) */
818 /* Load the segments */
820 NE_LoadAllSegments( pModule );
822 /* Fixup the functions prologs */
824 NE_FixupPrologs( pModule );
826 /* Make sure the usage count is 1 on the first loading of */
827 /* the module, even if it contains circular DLL references */
834 /**********************************************************************
837 * Load first instance of NE module, deciding whether to use
838 * built-in module or load module from file.
839 * (Note: caller is responsible for ensuring the module isn't
842 HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL implicit )
844 HINSTANCE16 hInstance;
848 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
852 /* 4 == strlen(".dll") */
853 strncpy(buffer, name, sizeof(buffer) - 1 - 4);
854 strcat(buffer, ".dll");
855 if ((hFile = OpenFile16( buffer, &ofs, OF_READ )) == HFILE_ERROR16) {
856 return 2; /* File not found */
860 hInstance = NE_LoadFileModule( hFile, &ofs, implicit );
867 /**********************************************************************
868 * MODULE_LoadModule16
870 * Load a NE module in the order of the loadorder specification.
871 * The caller is responsible that the module is not loaded already.
874 HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit )
878 module_loadorder_t *plo;
880 plo = MODULE_GetLoadOrder(libname);
882 for(i = 0; i < MODULE_LOADORDER_NTYPES; i++)
884 switch(plo->loadorder[i])
886 case MODULE_LOADORDER_DLL:
887 TRACE(module, "Trying native dll '%s'\n", libname);
888 hinst = NE_LoadModule(libname, implicit);
891 case MODULE_LOADORDER_ELFDLL:
892 TRACE(module, "Trying elfdll '%s'\n", libname);
893 hinst = ELFDLL_LoadModule16(libname, implicit);
896 case MODULE_LOADORDER_BI:
897 TRACE(module, "Trying built-in '%s'\n", libname);
898 hinst = fnBUILTIN_LoadModule(libname, TRUE);
902 ERR(module, "Got invalid loadorder type %d (%s index %d)\n", plo->loadorder[i], plo->modulename, i);
905 case MODULE_LOADORDER_SO: /* This is not supported for NE modules */
906 case MODULE_LOADORDER_INVALID: /* We ignore this as it is an empty entry */
918 hModule = GetModuleHandle16(libname);
921 ERR(module, "Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle\n",
923 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
926 pModule = NE_GetPtr(hModule);
929 ERR(module, "Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
931 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
934 TRACE(module, "Loaded module '%s' at 0x%04x, \n", libname, hinst);
937 * Call initialization routines for all loaded DLLs. Note that
938 * when we load implicitly linked DLLs this will be done by InitTask().
940 if(pModule->flags & NE_FFLAGS_LIBMODULE)
941 NE_InitializeDLLs(hModule);
948 /* We quit searching when we get another error than 'File not found' */
952 return hinst; /* The last error that occured */
956 /**********************************************************************
957 * LoadModule16 (KERNEL.45)
959 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
961 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
962 LOADPARAMS16 *params;
963 LPSTR cmd_line, new_cmd_line;
965 STARTUPINFOA startup;
966 PROCESS_INFORMATION info;
967 HINSTANCE16 hInstance, hPrevInstance = 0;
974 if ( (hModule = GetModuleHandle16(name) ) != 0 )
976 /* Special case: second instance of an already loaded NE module */
978 if ( !( pModule = NE_GetPtr( hModule ) ) ) return (HINSTANCE16)11;
979 if ( pModule->module32 ) return (HINSTANCE16)21;
981 hInstance = NE_CreateInstance( pModule, &hPrevInstance, lib_only );
982 if ( hInstance != hPrevInstance ) /* not a library */
983 NE_LoadSegment( pModule, pModule->dgroup );
989 /* Main case: load first instance of NE module */
991 if ( (hInstance = MODULE_LoadModule16( name, FALSE )) < 32 )
994 if ( !(pModule = NE_GetPtr( hInstance )) )
995 return (HINSTANCE16)11;
998 /* If library module, we're finished */
1000 if ( ( pModule->flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1003 /* Create a task for this instance */
1005 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1007 params = (LOADPARAMS16 *)paramBlock;
1008 cmd_line = (LPSTR)PTR_SEG_TO_LIN( params->cmdLine );
1009 if (!cmd_line) cmd_line = "";
1010 else if (*cmd_line) cmd_line++; /* skip the length byte */
1012 if (!(new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
1013 strlen(cmd_line)+strlen(name)+2 )))
1015 strcpy( new_cmd_line, name );
1016 strcat( new_cmd_line, " " );
1017 strcat( new_cmd_line, cmd_line );
1019 if (params->hEnvironment) env = GlobalLock16( params->hEnvironment );
1021 memset( &info, '\0', sizeof(info) );
1022 memset( &startup, '\0', sizeof(startup) );
1023 startup.cb = sizeof(startup);
1024 if (params->showCmd)
1026 startup.dwFlags = STARTF_USESHOWWINDOW;
1027 startup.wShowWindow = ((UINT16 *)PTR_SEG_TO_LIN(params->showCmd))[1];
1030 pdb = PROCESS_Create( pModule, new_cmd_line, env,
1031 hInstance, hPrevInstance,
1032 NULL, NULL, TRUE, &startup, &info );
1034 CloseHandle( info.hThread );
1035 CloseHandle( info.hProcess );
1037 if (params->hEnvironment) GlobalUnlock16( params->hEnvironment );
1038 HeapFree( GetProcessHeap(), 0, new_cmd_line );
1042 /**********************************************************************
1045 BOOL NE_CreateProcess( HFILE hFile, OFSTRUCT *ofs, LPCSTR cmd_line, LPCSTR env,
1046 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1047 BOOL inherit, LPSTARTUPINFOA startup,
1048 LPPROCESS_INFORMATION info )
1050 HINSTANCE16 hInstance, hPrevInstance = 0;
1055 /* Special case: second instance of an already loaded NE module */
1057 if ( ( hModule = GetModuleHandle16( ofs->szPathName ) ) != 0 )
1059 if ( !( pModule = NE_GetPtr( hModule) )
1060 || ( pModule->flags & NE_FFLAGS_LIBMODULE )
1061 || pModule->module32 )
1063 SetLastError( ERROR_BAD_FORMAT );
1067 hInstance = NE_CreateInstance( pModule, &hPrevInstance, FALSE );
1068 if ( hInstance != hPrevInstance ) /* not a library */
1069 NE_LoadSegment( pModule, pModule->dgroup );
1074 /* Main case: load first instance of NE module */
1077 /* If we didn't get a file handle, return */
1079 if ( hFile == HFILE_ERROR )
1082 /* Allocate temporary HFILE16 for NE_LoadFileModule */
1084 if (!DuplicateHandle( GetCurrentProcess(), hFile,
1085 GetCurrentProcess(), &hFile,
1086 0, FALSE, DUPLICATE_SAME_ACCESS ))
1088 SetLastError( ERROR_INVALID_HANDLE );
1091 hFile16 = FILE_AllocDosHandle( hFile );
1095 hInstance = NE_LoadFileModule( hFile16, ofs, TRUE );
1096 _lclose16( hFile16 );
1098 if ( hInstance < 32 )
1100 SetLastError( hInstance );
1104 if ( !( pModule = NE_GetPtr( hInstance ) )
1105 || ( pModule->flags & NE_FFLAGS_LIBMODULE) )
1107 /* FIXME: cleanup */
1108 SetLastError( ERROR_BAD_FORMAT );
1113 /* Create a task for this instance */
1115 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1117 if ( !PROCESS_Create( pModule, cmd_line, env,
1118 hInstance, hPrevInstance,
1119 psa, tsa, inherit, startup, info ) )
1125 /***********************************************************************
1126 * LoadLibrary16 (KERNEL.95)
1128 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1130 TRACE( module, "(%p) %s\n", libname, libname );
1132 /* Load library module */
1133 return LoadModule16( libname, (LPVOID)-1 );
1137 /**********************************************************************
1140 * Call a DLL's WEP, allowing it to shut down.
1141 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1143 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1145 FARPROC16 WEP = (FARPROC16)0;
1146 WORD ordinal = NE_GetOrdinal( hModule, "WEP" );
1148 if (ordinal) WEP = NE_GetEntryPoint( hModule, ordinal );
1151 WARN(module, "module %04x doesn't have a WEP\n", hModule );
1154 return Callbacks->CallWindowsExitProc( WEP, WEP_FREE_DLL );
1158 /**********************************************************************
1161 * Implementation of FreeModule16().
1163 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1165 HMODULE16 *hPrevModule;
1170 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1171 hModule = pModule->self;
1173 TRACE( module, "%04x count %d\n", hModule, pModule->count );
1175 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1176 else pModule->count = 0;
1178 if (pModule->flags & NE_FFLAGS_BUILTIN)
1179 return FALSE; /* Can't free built-in module */
1183 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1185 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1186 MODULE_CallWEP( hModule );
1188 /* Free the objects owned by the DLL module */
1190 if (pTask && pTask->userhandler)
1191 pTask->userhandler( hModule, USIG16_DLL_UNLOAD, 0,
1192 pTask->hInstance, pTask->hQueue );
1194 PROCESS_CallUserSignalProc( USIG_DLL_UNLOAD_WIN16, 0, hModule );
1197 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1201 /* Clear magic number just in case */
1203 pModule->magic = pModule->self = 0;
1205 /* Remove it from the linked list */
1207 hPrevModule = &hFirstModule;
1208 while (*hPrevModule && (*hPrevModule != hModule))
1210 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1212 if (*hPrevModule) *hPrevModule = pModule->next;
1214 /* Free the referenced modules */
1216 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
1217 for (i = 0; i < pModule->modref_count; i++, pModRef++)
1219 NE_FreeModule( *pModRef, call_wep );
1222 /* Free the module storage */
1224 GlobalFreeAll16( hModule );
1226 /* Remove module from cache */
1228 if (pCachedModule == pModule) pCachedModule = NULL;
1233 /**********************************************************************
1234 * FreeModule16 (KERNEL.46)
1236 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1238 return NE_FreeModule( hModule, TRUE );
1242 /***********************************************************************
1243 * FreeLibrary16 (KERNEL.96)
1245 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1247 TRACE(module,"%04x\n", handle );
1248 FreeModule16( handle );
1252 /**********************************************************************
1253 * GetModuleName (KERNEL.27)
1255 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1260 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1261 p = (BYTE *)pModule + pModule->name_table;
1262 if (count > *p) count = *p + 1;
1265 memcpy( buf, p + 1, count - 1 );
1266 buf[count-1] = '\0';
1272 /**********************************************************************
1273 * GetModuleUsage (KERNEL.48)
1275 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1277 NE_MODULE *pModule = NE_GetPtr( hModule );
1278 return pModule ? pModule->count : 0;
1282 /**********************************************************************
1283 * GetExpWinVer (KERNEL.167)
1285 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1287 NE_MODULE *pModule = NE_GetPtr( hModule );
1288 return pModule ? pModule->expected_version : 0;
1292 /**********************************************************************
1293 * GetModuleFileName16 (KERNEL.49)
1295 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1300 if (!hModule) hModule = GetCurrentTask();
1301 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1302 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1303 TRACE(module, "%s\n", lpFileName );
1304 return strlen(lpFileName);
1308 /**********************************************************************
1309 * GetModuleHandle16 (KERNEL.47)
1311 * Find a module from a module name.
1313 * NOTE: The current implementation works the same way the Windows 95 one
1314 * does. Do not try to 'fix' it, fix the callers.
1315 * + It does not do ANY extension handling (except that strange .EXE bit)!
1316 * + It does not care about paths, just about basenames. (same as Windows)
1320 * the win16 module handle if found
1322 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1323 * Always hFirstModule
1325 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1327 if (HIWORD(name) == 0)
1328 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1329 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name)), hFirstModule );
1332 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1334 HMODULE16 hModule = hFirstModule;
1336 BYTE len, *name_table;
1340 TRACE(module, "(%s)\n", name);
1343 return GetExePtr(LOWORD(name));
1349 strncpy(tmpstr, name, sizeof(tmpstr));
1350 tmpstr[sizeof(tmpstr)-1] = '\0';
1352 /* If 'name' matches exactly the module name of a module:
1353 * Return its handle.
1355 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1357 pModule = NE_GetPtr( hModule );
1358 if (!pModule) break;
1360 name_table = (BYTE *)pModule + pModule->name_table;
1361 if ((*name_table == len) && !strncmp(name, name_table+1, len))
1365 /* If uppercased 'name' matches exactly the module name of a module:
1368 for (s = tmpstr; *s; s++)
1371 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1373 pModule = NE_GetPtr( hModule );
1374 if (!pModule) break;
1376 name_table = (BYTE *)pModule + pModule->name_table;
1377 if ((*name_table == len) && !strncmp(tmpstr, name_table+1, len))
1381 /* If the base filename of 'name' matches the base filename of the module
1382 * filename of some module (case-insensitive compare):
1383 * Return its handle.
1386 /* basename: search backwards in passed name to \ / or : */
1387 s = tmpstr + strlen(tmpstr);
1390 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1395 /* search this in loaded filename list */
1396 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1401 pModule = NE_GetPtr( hModule );
1402 if (!pModule) break;
1403 if (!pModule->fileinfo) continue;
1405 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1406 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1407 /* basename: search backwards in pathname to \ / or : */
1408 while (loadedfn > (char*)ofs->szPathName)
1410 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1414 /* case insensitive compare ... */
1415 if (!lstrcmpiA(loadedfn, s))
1419 /* If the extension of 'name' is '.EXE' and the base filename of 'name'
1420 * matches the base filename of the module filename of some 32-bit module:
1421 * Return the corresponding 16-bit dummy module handle.
1423 if (len >= 4 && !strcasecmp(name+len-4, ".EXE"))
1425 HMODULE hModule = GetModuleHandleA( name );
1427 return MapHModuleLS( hModule );
1430 if (!strcmp(tmpstr,"MSDOS"))
1433 if (!strcmp(tmpstr,"TIMER"))
1435 FIXME(module, "Eh... Should return caller's code segment, expect crash\n");
1443 /**********************************************************************
1444 * ModuleFirst (TOOLHELP.59)
1446 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1448 lpme->wNext = hFirstModule;
1449 return ModuleNext16( lpme );
1453 /**********************************************************************
1454 * ModuleNext (TOOLHELP.60)
1456 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1461 if (!lpme->wNext) return FALSE;
1462 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1463 name = (char *)pModule + pModule->name_table;
1464 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1465 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1466 lpme->hModule = lpme->wNext;
1467 lpme->wcUsage = pModule->count;
1468 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1469 lpme->wNext = pModule->next;
1474 /**********************************************************************
1475 * ModuleFindName (TOOLHELP.61)
1477 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1479 lpme->wNext = GetModuleHandle16( name );
1480 return ModuleNext16( lpme );
1484 /**********************************************************************
1485 * ModuleFindHandle (TOOLHELP.62)
1487 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1489 hModule = GetExePtr( hModule );
1490 lpme->wNext = hModule;
1491 return ModuleNext16( lpme );
1495 /***************************************************************************
1496 * MapHModuleLS (KERNEL32.520)
1498 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod) {
1502 return ((TDB*)GlobalLock16(GetCurrentTask()))->hInstance;
1504 return hmod; /* we already have a 16 bit module handle */
1505 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1507 if (pModule->module32 == hmod)
1508 return pModule->self;
1509 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1514 /***************************************************************************
1515 * MapHModuleSL (KERNEL32.521)
1517 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod) {
1521 TDB *pTask = (TDB*)GlobalLock16(GetCurrentTask());
1523 hmod = pTask->hModule;
1525 pModule = (NE_MODULE*)GlobalLock16(hmod);
1526 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1527 !(pModule->flags & NE_FFLAGS_WIN32)
1530 return pModule->module32;
1533 /***************************************************************************
1534 * MapHInstLS (KERNEL32.516)
1536 REGS_ENTRYPOINT(MapHInstLS) {
1537 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1540 /***************************************************************************
1541 * MapHInstSL (KERNEL32.518)
1543 REGS_ENTRYPOINT(MapHInstSL) {
1544 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1547 /***************************************************************************
1548 * MapHInstLS_PN (KERNEL32.517)
1550 REGS_ENTRYPOINT(MapHInstLS_PN) {
1551 if (EAX_reg(context))
1552 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1555 /***************************************************************************
1556 * MapHInstSL_PN (KERNEL32.519)
1558 REGS_ENTRYPOINT(MapHInstSL_PN) {
1559 if (EAX_reg(context))
1560 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1563 /***************************************************************************
1564 * WIN16_MapHInstLS (KERNEL.472)
1566 VOID WINAPI WIN16_MapHInstLS( CONTEXT *context ) {
1567 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1570 /***************************************************************************
1571 * WIN16_MapHInstSL (KERNEL.473)
1573 VOID WINAPI WIN16_MapHInstSL( CONTEXT *context ) {
1574 EAX_reg(context) = MapHModuleSL(EAX_reg(context));