Playing a non-existent CD should fail instead of crash.
[wine/multimedia.git] / loader / ne / module.c
blob914584eed16a44595622d49c8628f2a35acf7856
1 /*
2 * NE modules
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include "module.h"
13 #include "file.h"
14 #include "ldt.h"
15 #include "callback.h"
16 #include "heap.h"
17 #include "task.h"
18 #include "global.h"
19 #include "process.h"
20 #include "toolhelp.h"
21 #include "snoop.h"
22 #include "stackframe.h"
23 #include "debug.h"
25 FARPROC16 (*fnSNOOP16_GetProcAddress16)(HMODULE16,DWORD,FARPROC16) = NULL;
26 void (*fnSNOOP16_RegisterDLL)(NE_MODULE*,LPCSTR) = NULL;
28 #define hFirstModule (pThhook->hExeHead)
30 static NE_MODULE *pCachedModule = 0; /* Module cached by NE_OpenFile */
32 static HMODULE16 NE_LoadBuiltin(LPCSTR name,BOOL32 force) { return 0; }
33 HMODULE16 (*fnBUILTIN_LoadModule)(LPCSTR name,BOOL32 force) = NE_LoadBuiltin;
36 /***********************************************************************
37 * NE_GetPtr
39 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
41 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
45 /***********************************************************************
46 * NE_DumpModule
48 void NE_DumpModule( HMODULE16 hModule )
50 int i, ordinal;
51 SEGTABLEENTRY *pSeg;
52 BYTE *pstr;
53 WORD *pword;
54 NE_MODULE *pModule;
56 if (!(pModule = NE_GetPtr( hModule )))
58 MSG( "**** %04x is not a module handle\n", hModule );
59 return;
62 /* Dump the module info */
63 DUMP( "---\n" );
64 DUMP( "Module %04x:\n", hModule );
65 DUMP( "count=%d flags=%04x heap=%d stack=%d\n",
66 pModule->count, pModule->flags,
67 pModule->heap_size, pModule->stack_size );
68 DUMP( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
69 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
70 pModule->seg_count, pModule->modref_count );
71 DUMP( "os_flags=%d swap_area=%d version=%04x\n",
72 pModule->os_flags, pModule->min_swap_area,
73 pModule->expected_version );
74 if (pModule->flags & NE_FFLAGS_WIN32)
75 DUMP( "PE module=%08x\n", pModule->module32 );
77 /* Dump the file info */
78 DUMP( "---\n" );
79 DUMP( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
81 /* Dump the segment table */
82 DUMP( "---\n" );
83 DUMP( "Segment table:\n" );
84 pSeg = NE_SEG_TABLE( pModule );
85 for (i = 0; i < pModule->seg_count; i++, pSeg++)
86 DUMP( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
87 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
88 pSeg->minsize, pSeg->hSeg );
90 /* Dump the resource table */
91 DUMP( "---\n" );
92 DUMP( "Resource table:\n" );
93 if (pModule->res_table)
95 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
96 DUMP( "Alignment: %d\n", *pword++ );
97 while (*pword)
99 struct resource_typeinfo_s *ptr = (struct resource_typeinfo_s *)pword;
100 struct resource_nameinfo_s *pname = (struct resource_nameinfo_s *)(ptr + 1);
101 DUMP( "id=%04x count=%d\n", ptr->type_id, ptr->count );
102 for (i = 0; i < ptr->count; i++, pname++)
103 DUMP( "offset=%d len=%d id=%04x\n",
104 pname->offset, pname->length, pname->id );
105 pword = (WORD *)pname;
108 else DUMP( "None\n" );
110 /* Dump the resident name table */
111 DUMP( "---\n" );
112 DUMP( "Resident-name table:\n" );
113 pstr = (char *)pModule + pModule->name_table;
114 while (*pstr)
116 DUMP( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
117 *(WORD *)(pstr + *pstr + 1) );
118 pstr += *pstr + 1 + sizeof(WORD);
121 /* Dump the module reference table */
122 DUMP( "---\n" );
123 DUMP( "Module ref table:\n" );
124 if (pModule->modref_table)
126 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
127 for (i = 0; i < pModule->modref_count; i++, pword++)
129 char name[10];
130 GetModuleName( *pword, name, sizeof(name) );
131 DUMP( "%d: %04x -> '%s'\n", i, *pword, name );
134 else DUMP( "None\n" );
136 /* Dump the entry table */
137 DUMP( "---\n" );
138 DUMP( "Entry table:\n" );
139 pstr = (char *)pModule + pModule->entry_table;
140 ordinal = 1;
141 while (*pstr)
143 DUMP( "Bundle %d-%d: %02x\n", ordinal, ordinal + *pstr - 1, pstr[1]);
144 if (!pstr[1])
146 ordinal += *pstr;
147 pstr += 2;
149 else if ((BYTE)pstr[1] == 0xff) /* moveable */
151 i = *pstr;
152 pstr += 2;
153 while (i--)
155 DUMP( "%d: %02x:%04x (moveable)\n",
156 ordinal++, pstr[3], *(WORD *)(pstr + 4) );
157 pstr += 6;
160 else /* fixed */
162 i = *pstr;
163 pstr += 2;
164 while (i--)
166 DUMP( "%d: %04x (fixed)\n",
167 ordinal++, *(WORD *)(pstr + 1) );
168 pstr += 3;
173 /* Dump the non-resident names table */
174 DUMP( "---\n" );
175 DUMP( "Non-resident names table:\n" );
176 if (pModule->nrname_handle)
178 pstr = (char *)GlobalLock16( pModule->nrname_handle );
179 while (*pstr)
181 DUMP( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
182 *(WORD *)(pstr + *pstr + 1) );
183 pstr += *pstr + 1 + sizeof(WORD);
186 DUMP( "\n" );
190 /***********************************************************************
191 * NE_WalkModules
193 * Walk the module list and print the modules.
195 void NE_WalkModules(void)
197 HMODULE16 hModule = hFirstModule;
198 MSG( "Module Flags Name\n" );
199 while (hModule)
201 NE_MODULE *pModule = NE_GetPtr( hModule );
202 if (!pModule)
204 MSG( "Bad module %04x in list\n", hModule );
205 return;
207 MSG( " %04x %04x %.*s\n", hModule, pModule->flags,
208 *((char *)pModule + pModule->name_table),
209 (char *)pModule + pModule->name_table + 1 );
210 hModule = pModule->next;
215 /**********************************************************************
216 * NE_RegisterModule
218 void NE_RegisterModule( NE_MODULE *pModule )
220 pModule->next = hFirstModule;
221 hFirstModule = pModule->self;
225 /***********************************************************************
226 * NE_GetOrdinal
228 * Lookup the ordinal for a given name.
230 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
232 unsigned char buffer[256], *cpnt;
233 BYTE len;
234 NE_MODULE *pModule;
236 if (!(pModule = NE_GetPtr( hModule ))) return 0;
237 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
239 TRACE( module, "(%04x,'%s')\n", hModule, name );
241 /* First handle names of the form '#xxxx' */
243 if (name[0] == '#') return atoi( name + 1 );
245 /* Now copy and uppercase the string */
247 strcpy( buffer, name );
248 CharUpper32A( buffer );
249 len = strlen( buffer );
251 /* First search the resident names */
253 cpnt = (char *)pModule + pModule->name_table;
255 /* Skip the first entry (module name) */
256 cpnt += *cpnt + 1 + sizeof(WORD);
257 while (*cpnt)
259 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
261 TRACE(module, " Found: ordinal=%d\n",
262 *(WORD *)(cpnt + *cpnt + 1) );
263 return *(WORD *)(cpnt + *cpnt + 1);
265 cpnt += *cpnt + 1 + sizeof(WORD);
268 /* Now search the non-resident names table */
270 if (!pModule->nrname_handle) return 0; /* No non-resident table */
271 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
273 /* Skip the first entry (module description string) */
274 cpnt += *cpnt + 1 + sizeof(WORD);
275 while (*cpnt)
277 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
279 TRACE(module, " Found: ordinal=%d\n",
280 *(WORD *)(cpnt + *cpnt + 1) );
281 return *(WORD *)(cpnt + *cpnt + 1);
283 cpnt += *cpnt + 1 + sizeof(WORD);
285 return 0;
289 /***********************************************************************
290 * NE_GetEntryPoint (WPROCS.27)
292 * Return the entry point for a given ordinal.
294 FARPROC16 NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
296 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
298 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
300 NE_MODULE *pModule;
301 WORD curOrdinal = 1;
302 BYTE *p;
303 WORD sel, offset;
305 if (!(pModule = NE_GetPtr( hModule ))) return 0;
306 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
308 p = (BYTE *)pModule + pModule->entry_table;
309 while (*p && (curOrdinal + *p <= ordinal))
311 /* Skipping this bundle */
312 curOrdinal += *p;
313 switch(p[1])
315 case 0: p += 2; break; /* unused */
316 case 0xff: p += 2 + *p * 6; break; /* moveable */
317 default: p += 2 + *p * 3; break; /* fixed */
320 if (!*p) return 0;
322 switch(p[1])
324 case 0: /* unused */
325 return 0;
326 case 0xff: /* moveable */
327 p += 2 + 6 * (ordinal - curOrdinal);
328 sel = p[3];
329 offset = *(WORD *)(p + 4);
330 break;
331 default: /* fixed */
332 sel = p[1];
333 p += 2 + 3 * (ordinal - curOrdinal);
334 offset = *(WORD *)(p + 1);
335 break;
338 if (sel == 0xfe) sel = 0xffff; /* constant entry */
339 else sel = GlobalHandleToSel(NE_SEG_TABLE(pModule)[sel-1].hSeg);
340 if (sel==0xffff)
341 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
342 if (!snoop || !fnSNOOP16_GetProcAddress16)
343 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
344 else
345 return (FARPROC16)fnSNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset ));
349 /***********************************************************************
350 * NE_SetEntryPoint
352 * Change the value of an entry point. Use with caution!
353 * It can only change the offset value, not the selector.
355 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
357 NE_MODULE *pModule;
358 WORD curOrdinal = 1;
359 BYTE *p;
361 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
362 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
364 p = (BYTE *)pModule + pModule->entry_table;
365 while (*p && (curOrdinal + *p <= ordinal))
367 /* Skipping this bundle */
368 curOrdinal += *p;
369 switch(p[1])
371 case 0: p += 2; break; /* unused */
372 case 0xff: p += 2 + *p * 6; break; /* moveable */
373 default: p += 2 + *p * 3; break; /* fixed */
376 if (!*p) return FALSE;
378 switch(p[1])
380 case 0: /* unused */
381 return FALSE;
382 case 0xff: /* moveable */
383 p += 2 + 6 * (ordinal - curOrdinal);
384 *(WORD *)(p + 4) = offset;
385 break;
386 default: /* fixed */
387 p += 2 + 3 * (ordinal - curOrdinal);
388 *(WORD *)(p + 1) = offset;
389 break;
391 return TRUE;
395 /***********************************************************************
396 * NE_OpenFile
398 HANDLE32 NE_OpenFile( NE_MODULE *pModule )
400 char *name;
402 static HANDLE32 cachedfd = -1;
404 TRACE( module, "(%p) cache: mod=%p fd=%d\n",
405 pModule, pCachedModule, cachedfd );
406 if (pCachedModule == pModule) return cachedfd;
407 CloseHandle( cachedfd );
408 pCachedModule = pModule;
409 name = NE_MODULE_NAME( pModule );
410 if ((cachedfd = CreateFile32A( name, GENERIC_READ, FILE_SHARE_READ,
411 NULL, OPEN_EXISTING, 0, -1 )) == -1)
412 MSG( "Can't open file '%s' for module %04x\n", name, pModule->self );
413 else
414 /* FIXME: should not be necessary */
415 cachedfd = ConvertToGlobalHandle(cachedfd);
416 TRACE(module, "opened '%s' -> %d\n",
417 name, cachedfd );
418 return cachedfd;
422 /***********************************************************************
423 * NE_LoadExeHeader
425 static HMODULE16 NE_LoadExeHeader( HFILE16 hFile, OFSTRUCT *ofs )
427 IMAGE_DOS_HEADER mz_header;
428 IMAGE_OS2_HEADER ne_header;
429 int size;
430 HMODULE16 hModule;
431 NE_MODULE *pModule;
432 BYTE *pData;
433 char *buffer, *fastload = NULL;
434 int fastload_offset = 0, fastload_length = 0;
436 /* Read a block from either the file or the fast-load area. */
437 #define READ(offset,size,buffer) \
438 ((fastload && ((offset) >= fastload_offset) && \
439 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
440 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
441 (_llseek16( hFile, (offset), SEEK_SET), \
442 _hread16( hFile, (buffer), (size) ) == (size)))
444 _llseek16( hFile, 0, SEEK_SET );
445 if ((_hread16(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
446 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
447 return (HMODULE16)11; /* invalid exe */
449 _llseek16( hFile, mz_header.e_lfanew, SEEK_SET );
450 if (_hread16( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
451 return (HMODULE16)11; /* invalid exe */
453 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
454 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
456 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
457 MSG("Sorry, this is an OS/2 linear executable (LX) file !\n");
458 return (HMODULE16)12;
461 /* We now have a valid NE header */
463 size = sizeof(NE_MODULE) +
464 /* segment table */
465 ne_header.n_segment_tab * sizeof(SEGTABLEENTRY) +
466 /* resource table */
467 ne_header.rname_tab_offset - ne_header.resource_tab_offset +
468 /* resident names table */
469 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset +
470 /* module ref table */
471 ne_header.n_mod_ref_tab * sizeof(WORD) +
472 /* imported names table */
473 ne_header.entry_tab_offset - ne_header.iname_tab_offset +
474 /* entry table length */
475 ne_header.entry_tab_length +
476 /* loaded file info */
477 sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
479 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
480 if (!hModule) return (HMODULE16)11; /* invalid exe */
481 FarSetOwner( hModule, hModule );
482 pModule = (NE_MODULE *)GlobalLock16( hModule );
483 memcpy( pModule, &ne_header, sizeof(ne_header) );
484 pModule->count = 0;
485 /* check *programs* for default minimal stack size */
486 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
487 && (pModule->stack_size < 0x1400) )
488 pModule->stack_size = 0x1400;
489 pModule->module32 = 0;
490 pModule->self = hModule;
491 pModule->self_loading_sel = 0;
492 pData = (BYTE *)(pModule + 1);
494 /* Clear internal Wine flags in case they are set in the EXE file */
496 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
498 /* Read the fast-load area */
500 if (ne_header.additional_flags & NE_AFLAGS_FASTLOAD)
502 fastload_offset=ne_header.fastload_offset<<ne_header.align_shift_count;
503 fastload_length=ne_header.fastload_length<<ne_header.align_shift_count;
504 TRACE(module, "Using fast-load area offset=%x len=%d\n",
505 fastload_offset, fastload_length );
506 if ((fastload = HeapAlloc( SystemHeap, 0, fastload_length )) != NULL)
508 _llseek16( hFile, fastload_offset, SEEK_SET);
509 if (_hread16(hFile, fastload, fastload_length) != fastload_length)
511 HeapFree( SystemHeap, 0, fastload );
512 WARN( module, "Error reading fast-load area!\n");
513 fastload = NULL;
518 /* Get the segment table */
520 pModule->seg_table = (int)pData - (int)pModule;
521 buffer = HeapAlloc( SystemHeap, 0, ne_header.n_segment_tab *
522 sizeof(struct ne_segment_table_entry_s));
523 if (buffer)
525 int i;
526 struct ne_segment_table_entry_s *pSeg;
528 if (!READ( mz_header.e_lfanew + ne_header.segment_tab_offset,
529 ne_header.n_segment_tab * sizeof(struct ne_segment_table_entry_s),
530 buffer ))
532 HeapFree( SystemHeap, 0, buffer );
533 if (fastload) HeapFree( SystemHeap, 0, fastload );
534 GlobalFree16( hModule );
535 return (HMODULE16)11; /* invalid exe */
537 pSeg = (struct ne_segment_table_entry_s *)buffer;
538 for (i = ne_header.n_segment_tab; i > 0; i--, pSeg++)
540 memcpy( pData, pSeg, sizeof(*pSeg) );
541 pData += sizeof(SEGTABLEENTRY);
543 HeapFree( SystemHeap, 0, buffer );
545 else
547 if (fastload) HeapFree( SystemHeap, 0, fastload );
548 GlobalFree16( hModule );
549 return (HMODULE16)11; /* invalid exe */
552 /* Get the resource table */
554 if (ne_header.resource_tab_offset < ne_header.rname_tab_offset)
556 pModule->res_table = (int)pData - (int)pModule;
557 if (!READ(mz_header.e_lfanew + ne_header.resource_tab_offset,
558 ne_header.rname_tab_offset - ne_header.resource_tab_offset,
559 pData )) return (HMODULE16)11; /* invalid exe */
560 pData += ne_header.rname_tab_offset - ne_header.resource_tab_offset;
561 NE_InitResourceHandler( hModule );
563 else pModule->res_table = 0; /* No resource table */
565 /* Get the resident names table */
567 pModule->name_table = (int)pData - (int)pModule;
568 if (!READ( mz_header.e_lfanew + ne_header.rname_tab_offset,
569 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset,
570 pData ))
572 if (fastload) HeapFree( SystemHeap, 0, fastload );
573 GlobalFree16( hModule );
574 return (HMODULE16)11; /* invalid exe */
576 pData += ne_header.moduleref_tab_offset - ne_header.rname_tab_offset;
578 /* Get the module references table */
580 if (ne_header.n_mod_ref_tab > 0)
582 pModule->modref_table = (int)pData - (int)pModule;
583 if (!READ( mz_header.e_lfanew + ne_header.moduleref_tab_offset,
584 ne_header.n_mod_ref_tab * sizeof(WORD),
585 pData ))
587 if (fastload) HeapFree( SystemHeap, 0, fastload );
588 GlobalFree16( hModule );
589 return (HMODULE16)11; /* invalid exe */
591 pData += ne_header.n_mod_ref_tab * sizeof(WORD);
593 else pModule->modref_table = 0; /* No module references */
595 /* Get the imported names table */
597 pModule->import_table = (int)pData - (int)pModule;
598 if (!READ( mz_header.e_lfanew + ne_header.iname_tab_offset,
599 ne_header.entry_tab_offset - ne_header.iname_tab_offset,
600 pData ))
602 if (fastload) HeapFree( SystemHeap, 0, fastload );
603 GlobalFree16( hModule );
604 return (HMODULE16)11; /* invalid exe */
606 pData += ne_header.entry_tab_offset - ne_header.iname_tab_offset;
608 /* Get the entry table */
610 pModule->entry_table = (int)pData - (int)pModule;
611 if (!READ( mz_header.e_lfanew + ne_header.entry_tab_offset,
612 ne_header.entry_tab_length,
613 pData ))
615 if (fastload) HeapFree( SystemHeap, 0, fastload );
616 GlobalFree16( hModule );
617 return (HMODULE16)11; /* invalid exe */
619 pData += ne_header.entry_tab_length;
621 /* Store the filename information */
623 pModule->fileinfo = (int)pData - (int)pModule;
624 size = sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
625 memcpy( pData, ofs, size );
626 ((OFSTRUCT *)pData)->cBytes = size - 1;
627 pData += size;
629 /* Free the fast-load area */
631 #undef READ
632 if (fastload) HeapFree( SystemHeap, 0, fastload );
634 /* Get the non-resident names table */
636 if (ne_header.nrname_tab_length)
638 pModule->nrname_handle = GLOBAL_Alloc( 0, ne_header.nrname_tab_length,
639 hModule, FALSE, FALSE, FALSE );
640 if (!pModule->nrname_handle)
642 GlobalFree16( hModule );
643 return (HMODULE16)11; /* invalid exe */
645 buffer = GlobalLock16( pModule->nrname_handle );
646 _llseek16( hFile, ne_header.nrname_tab_offset, SEEK_SET );
647 if (_hread16( hFile, buffer, ne_header.nrname_tab_length )
648 != ne_header.nrname_tab_length)
650 GlobalFree16( pModule->nrname_handle );
651 GlobalFree16( hModule );
652 return (HMODULE16)11; /* invalid exe */
655 else pModule->nrname_handle = 0;
657 /* Allocate a segment for the implicitly-loaded DLLs */
659 if (pModule->modref_count)
661 pModule->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,
662 (pModule->modref_count+1)*sizeof(HMODULE16),
663 hModule, FALSE, FALSE, FALSE );
664 if (!pModule->dlls_to_init)
666 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
667 GlobalFree16( hModule );
668 return (HMODULE16)11; /* invalid exe */
671 else pModule->dlls_to_init = 0;
673 NE_RegisterModule( pModule );
674 if (fnSNOOP16_RegisterDLL)
675 fnSNOOP16_RegisterDLL(pModule,ofs->szPathName);
676 return hModule;
680 /***********************************************************************
681 * NE_LoadDLLs
683 * Load all DLLs implicitly linked to a module.
685 static BOOL32 NE_LoadDLLs( NE_MODULE *pModule )
687 int i;
688 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
689 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
691 for (i = 0; i < pModule->modref_count; i++, pModRef++)
693 char buffer[260];
694 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
695 memcpy( buffer, pstr + 1, *pstr );
696 strcpy( buffer + *pstr, ".dll" );
697 TRACE(module, "Loading '%s'\n", buffer );
698 if (!(*pModRef = GetModuleHandle16( buffer )))
700 /* If the DLL is not loaded yet, load it and store */
701 /* its handle in the list of DLLs to initialize. */
702 HMODULE16 hDLL;
704 if ((hDLL = NE_LoadModule( buffer, NULL, TRUE, TRUE )) == 2)
706 /* file not found */
707 char *p;
709 /* Try with prepending the path of the current module */
710 GetModuleFileName16( pModule->self, buffer, sizeof(buffer) );
711 if (!(p = strrchr( buffer, '\\' ))) p = buffer;
712 memcpy( p + 1, pstr + 1, *pstr );
713 strcpy( p + 1 + *pstr, ".dll" );
714 hDLL = NE_LoadModule( buffer, NULL, TRUE, TRUE );
716 if (hDLL < 32)
718 /* FIXME: cleanup what was done */
720 MSG( "Could not load '%s' required by '%.*s', error=%d\n",
721 buffer, *((BYTE*)pModule + pModule->name_table),
722 (char *)pModule + pModule->name_table + 1, hDLL );
723 return FALSE;
725 *pModRef = GetExePtr( hDLL );
726 *pDLLs++ = *pModRef;
728 else /* Increment the reference count of the DLL */
730 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
731 if (pOldDLL) pOldDLL->count++;
734 return TRUE;
738 /**********************************************************************
739 * NE_LoadModule
741 * Implementation of LoadModule16().
743 HINSTANCE16 NE_LoadModule( LPCSTR name, HINSTANCE16 *hPrevInstance,
744 BOOL32 implicit, BOOL32 lib_only )
746 HMODULE16 hModule;
747 HINSTANCE16 hInstance;
748 NE_MODULE *pModule;
749 HFILE16 hFile;
750 OFSTRUCT ofs;
752 /* Check if the module is already loaded */
754 if ((hModule = GetModuleHandle16( name )) != 0)
756 HINSTANCE16 prev;
757 pModule = NE_GetPtr( hModule );
758 if ( pModule->module32 ) return (HINSTANCE16)21;
760 hInstance = NE_CreateInstance( pModule, &prev, lib_only );
761 if (hInstance != prev) /* not a library */
762 NE_LoadSegment( pModule, pModule->dgroup );
763 pModule->count++;
764 if (hPrevInstance) *hPrevInstance = prev;
765 return hInstance;
767 if (hPrevInstance) *hPrevInstance = 0;
769 /* Try to load the built-in first if not disabled */
771 if ((hModule = fnBUILTIN_LoadModule( name, FALSE ))) return hModule;
773 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
775 /* Now try the built-in even if disabled */
776 if ((hModule = fnBUILTIN_LoadModule( name, TRUE )))
778 MSG( "Could not load Windows DLL '%s', using built-in module.\n",
779 name );
780 return hModule;
782 return 2; /* File not found */
785 /* Create the module structure */
787 hModule = NE_LoadExeHeader( hFile, &ofs );
788 _lclose16( hFile );
789 if (hModule < 32) return hModule;
790 pModule = NE_GetPtr( hModule );
792 /* Allocate the segments for this module */
794 if (!NE_CreateSegments( pModule ) ||
795 !(hInstance = NE_CreateInstance( pModule, NULL, lib_only )))
797 GlobalFreeAll( hModule );
798 return 8; /* Insufficient memory */
801 /* Load the referenced DLLs */
803 if (!NE_LoadDLLs( pModule ))
804 return 2; /* File not found (FIXME: free everything) */
806 /* Load the segments */
808 NE_LoadAllSegments( pModule );
810 /* Fixup the functions prologs */
812 NE_FixupPrologs( pModule );
814 /* Make sure the usage count is 1 on the first loading of */
815 /* the module, even if it contains circular DLL references */
817 pModule->count = 1;
819 /* Call initialization rountines for all loaded DLLs. Note that
820 * when we load implicitly linked DLLs this will be done by InitTask().
823 if (!implicit && (pModule->flags & NE_FFLAGS_LIBMODULE))
824 NE_InitializeDLLs( hModule );
826 return hInstance;
830 /***********************************************************************
831 * LoadLibrary (KERNEL.95)
833 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
835 HINSTANCE16 handle;
836 LPCSTR p;
837 char *new_name;
839 TRACE(module, "(%08x) %s\n", (int)libname, libname);
841 /* Check for an extension */
843 if ((p = strrchr( libname, '.')) && !strchr( p, '/' ) && !strchr( p, '\\'))
845 /* An extension is present -> use the name as is */
846 return NE_LoadModule( libname, NULL, FALSE, TRUE );
849 /* Now append .dll before loading */
851 if (!(new_name = HeapAlloc( GetProcessHeap(), 0, strlen(libname) + 4 )))
852 return 0;
853 strcpy( new_name, libname );
854 strcat( new_name, ".dll" );
855 handle = NE_LoadModule( new_name, NULL, FALSE, TRUE );
856 HeapFree( GetProcessHeap(), 0, new_name );
857 return handle;
861 /**********************************************************************
862 * MODULE_CallWEP
864 * Call a DLL's WEP, allowing it to shut down.
865 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
867 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
869 FARPROC16 WEP = (FARPROC16)0;
870 WORD ordinal = NE_GetOrdinal( hModule, "WEP" );
872 if (ordinal) WEP = NE_GetEntryPoint( hModule, ordinal );
873 if (!WEP)
875 WARN(module, "module %04x doesn't have a WEP\n", hModule );
876 return FALSE;
878 return Callbacks->CallWindowsExitProc( WEP, WEP_FREE_DLL );
882 /**********************************************************************
883 * NE_FreeModule
885 * Implementation of FreeModule16().
887 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL32 call_wep )
889 HMODULE16 *hPrevModule;
890 NE_MODULE *pModule;
891 HMODULE16 *pModRef;
892 int i;
894 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
895 hModule = pModule->self;
897 TRACE( module, "%04x count %d\n", hModule, pModule->count );
899 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
900 else pModule->count = 0;
902 if (pModule->flags & NE_FFLAGS_BUILTIN)
903 return FALSE; /* Can't free built-in module */
905 if (call_wep)
907 if (pModule->flags & NE_FFLAGS_LIBMODULE)
909 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
910 MODULE_CallWEP( hModule );
912 /* Free the objects owned by the DLL module */
914 if (pTask && pTask->userhandler)
915 pTask->userhandler( hModule, USIG_DLL_UNLOAD, 0,
916 pTask->hInstance, pTask->hQueue );
918 else
919 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
923 /* Clear magic number just in case */
925 pModule->magic = pModule->self = 0;
927 /* Remove it from the linked list */
929 hPrevModule = &hFirstModule;
930 while (*hPrevModule && (*hPrevModule != hModule))
932 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
934 if (*hPrevModule) *hPrevModule = pModule->next;
936 /* Free the referenced modules */
938 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
939 for (i = 0; i < pModule->modref_count; i++, pModRef++)
941 NE_FreeModule( *pModRef, call_wep );
944 /* Free the module storage */
946 GlobalFreeAll( hModule );
948 /* Remove module from cache */
950 if (pCachedModule == pModule) pCachedModule = NULL;
951 return TRUE;
955 /**********************************************************************
956 * FreeModule16 (KERNEL.46)
958 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
960 return NE_FreeModule( hModule, TRUE );
964 /***********************************************************************
965 * FreeLibrary16 (KERNEL.96)
967 void WINAPI FreeLibrary16( HINSTANCE16 handle )
969 TRACE(module,"%04x\n", handle );
970 FreeModule16( handle );
974 /**********************************************************************
975 * GetModuleName (KERNEL.27)
977 BOOL16 WINAPI GetModuleName( HINSTANCE16 hinst, LPSTR buf, INT16 count )
979 NE_MODULE *pModule;
980 BYTE *p;
982 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
983 p = (BYTE *)pModule + pModule->name_table;
984 if (count > *p) count = *p + 1;
985 if (count > 0)
987 memcpy( buf, p + 1, count - 1 );
988 buf[count-1] = '\0';
990 return TRUE;
994 /**********************************************************************
995 * GetModuleUsage (KERNEL.48)
997 INT16 WINAPI GetModuleUsage( HINSTANCE16 hModule )
999 NE_MODULE *pModule = NE_GetPtr( hModule );
1000 return pModule ? pModule->count : 0;
1004 /**********************************************************************
1005 * GetExpWinVer (KERNEL.167)
1007 WORD WINAPI GetExpWinVer( HMODULE16 hModule )
1009 NE_MODULE *pModule = NE_GetPtr( hModule );
1010 return pModule ? pModule->expected_version : 0;
1014 /**********************************************************************
1015 * GetModuleFileName16 (KERNEL.49)
1017 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1018 INT16 nSize )
1020 NE_MODULE *pModule;
1022 if (!hModule) hModule = GetCurrentTask();
1023 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1024 lstrcpyn32A( lpFileName, NE_MODULE_NAME(pModule), nSize );
1025 TRACE(module, "%s\n", lpFileName );
1026 return strlen(lpFileName);
1030 /**********************************************************************
1031 * GetModuleHandle16 (KERNEL.47)
1033 * Find a module from a path name.
1035 * RETURNS
1036 * LOWORD:
1037 * the win16 module handle if found
1038 * 0 if not
1039 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1040 * Always hFirstModule
1042 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1044 if (HIWORD(name) == 0)
1045 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1046 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name)), hFirstModule );
1049 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1051 HMODULE16 hModule = hFirstModule;
1052 LPCSTR filename, dotptr, modulepath, modulename;
1053 BYTE len, *name_table;
1055 if (!(filename = strrchr( name, '\\' ))) filename = name;
1056 else filename++;
1057 if ((dotptr = strrchr( filename, '.' )) != NULL)
1058 len = (BYTE)(dotptr - filename);
1059 else len = strlen( filename );
1061 while (hModule)
1063 NE_MODULE *pModule = NE_GetPtr( hModule );
1064 if (!pModule) break;
1065 modulepath = NE_MODULE_NAME(pModule);
1066 if (!(modulename = strrchr( modulepath, '\\' )))
1067 modulename = modulepath;
1068 else modulename++;
1069 if (!lstrcmpi32A( modulename, filename )) return hModule;
1071 name_table = (BYTE *)pModule + pModule->name_table;
1072 if ((*name_table == len) && !lstrncmpi32A(filename, name_table+1, len))
1073 return hModule;
1074 hModule = pModule->next;
1076 return 0;
1080 /**********************************************************************
1081 * ModuleFirst (TOOLHELP.59)
1083 BOOL16 WINAPI ModuleFirst( MODULEENTRY *lpme )
1085 lpme->wNext = hFirstModule;
1086 return ModuleNext( lpme );
1090 /**********************************************************************
1091 * ModuleNext (TOOLHELP.60)
1093 BOOL16 WINAPI ModuleNext( MODULEENTRY *lpme )
1095 NE_MODULE *pModule;
1096 char *name;
1098 if (!lpme->wNext) return FALSE;
1099 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1100 name = (char *)pModule + pModule->name_table;
1101 memcpy( lpme->szModule, name + 1, *name );
1102 lpme->szModule[(BYTE)*name] = '\0';
1103 lpme->hModule = lpme->wNext;
1104 lpme->wcUsage = pModule->count;
1105 strncpy( lpme->szExePath, NE_MODULE_NAME(pModule), MAX_PATH );
1106 lpme->szExePath[MAX_PATH] = '\0';
1107 lpme->wNext = pModule->next;
1108 return TRUE;
1112 /**********************************************************************
1113 * ModuleFindName (TOOLHELP.61)
1115 BOOL16 WINAPI ModuleFindName( MODULEENTRY *lpme, LPCSTR name )
1117 lpme->wNext = GetModuleHandle16( name );
1118 return ModuleNext( lpme );
1122 /**********************************************************************
1123 * ModuleFindHandle (TOOLHELP.62)
1125 BOOL16 WINAPI ModuleFindHandle( MODULEENTRY *lpme, HMODULE16 hModule )
1127 hModule = GetExePtr( hModule );
1128 lpme->wNext = hModule;
1129 return ModuleNext( lpme );
1132 /***************************************************************************
1133 * MapHModuleLS (KERNEL32.520)
1135 HMODULE16 WINAPI MapHModuleLS(HMODULE32 hmod) {
1136 NE_MODULE *pModule;
1138 if (!hmod)
1139 return ((TDB*)GlobalLock16(GetCurrentTask()))->hInstance;
1140 if (!HIWORD(hmod))
1141 return hmod; /* we already have a 16 bit module handle */
1142 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1143 while (pModule) {
1144 if (pModule->module32 == hmod)
1145 return pModule->self;
1146 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1148 return 0;
1151 /***************************************************************************
1152 * MapHModuleSL (KERNEL32.521)
1154 HMODULE32 WINAPI MapHModuleSL(HMODULE16 hmod) {
1155 NE_MODULE *pModule;
1157 if (!hmod) {
1158 TDB *pTask = (TDB*)GlobalLock16(GetCurrentTask());
1160 hmod = pTask->hInstance;
1162 pModule = (NE_MODULE*)GlobalLock16(hmod);
1163 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1164 !(pModule->flags & NE_FFLAGS_WIN32)
1166 return 0;
1167 return pModule->module32;
1170 /***************************************************************************
1171 * MapHInstLS (KERNEL32.516)
1173 REGS_ENTRYPOINT(MapHInstLS) {
1174 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1177 /***************************************************************************
1178 * MapHInstSL (KERNEL32.518)
1180 REGS_ENTRYPOINT(MapHInstSL) {
1181 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1184 /***************************************************************************
1185 * MapHInstLS_PN (KERNEL32.517)
1187 REGS_ENTRYPOINT(MapHInstLS_PN) {
1188 if (EAX_reg(context))
1189 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1192 /***************************************************************************
1193 * MapHInstSL_PN (KERNEL32.519)
1195 REGS_ENTRYPOINT(MapHInstSL_PN) {
1196 if (EAX_reg(context))
1197 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1200 /***************************************************************************
1201 * WIN16_MapHInstLS (KERNEL.472)
1203 VOID WINAPI WIN16_MapHInstLS( CONTEXT *context ) {
1204 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1207 /***************************************************************************
1208 * WIN16_MapHInstSL (KERNEL.473)
1210 VOID WINAPI WIN16_MapHInstSL( CONTEXT *context ) {
1211 EAX_reg(context) = MapHModuleSL(EAX_reg(context));