Initial revision
[wine/multimedia.git] / loader / ne / module.c
blob3e193ac1a6b52001ab65d0160acf8d6080ba0d31
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 NE_MODULE *pModule;
297 WORD curOrdinal = 1;
298 BYTE *p;
299 WORD sel, offset;
301 if (!(pModule = NE_GetPtr( hModule ))) return 0;
302 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
304 p = (BYTE *)pModule + pModule->entry_table;
305 while (*p && (curOrdinal + *p <= ordinal))
307 /* Skipping this bundle */
308 curOrdinal += *p;
309 switch(p[1])
311 case 0: p += 2; break; /* unused */
312 case 0xff: p += 2 + *p * 6; break; /* moveable */
313 default: p += 2 + *p * 3; break; /* fixed */
316 if (!*p) return 0;
318 switch(p[1])
320 case 0: /* unused */
321 return 0;
322 case 0xff: /* moveable */
323 p += 2 + 6 * (ordinal - curOrdinal);
324 sel = p[3];
325 offset = *(WORD *)(p + 4);
326 break;
327 default: /* fixed */
328 sel = p[1];
329 p += 2 + 3 * (ordinal - curOrdinal);
330 offset = *(WORD *)(p + 1);
331 break;
334 if (sel == 0xfe) sel = 0xffff; /* constant entry */
335 else sel = GlobalHandleToSel(NE_SEG_TABLE(pModule)[sel-1].hSeg);
336 if (sel==0xffff)
337 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
338 if (!fnSNOOP16_GetProcAddress16)
339 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
340 else
341 return (FARPROC16)fnSNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset ));
345 /***********************************************************************
346 * NE_SetEntryPoint
348 * Change the value of an entry point. Use with caution!
349 * It can only change the offset value, not the selector.
351 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
353 NE_MODULE *pModule;
354 WORD curOrdinal = 1;
355 BYTE *p;
357 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
358 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
360 p = (BYTE *)pModule + pModule->entry_table;
361 while (*p && (curOrdinal + *p <= ordinal))
363 /* Skipping this bundle */
364 curOrdinal += *p;
365 switch(p[1])
367 case 0: p += 2; break; /* unused */
368 case 0xff: p += 2 + *p * 6; break; /* moveable */
369 default: p += 2 + *p * 3; break; /* fixed */
372 if (!*p) return FALSE;
374 switch(p[1])
376 case 0: /* unused */
377 return FALSE;
378 case 0xff: /* moveable */
379 p += 2 + 6 * (ordinal - curOrdinal);
380 *(WORD *)(p + 4) = offset;
381 break;
382 default: /* fixed */
383 p += 2 + 3 * (ordinal - curOrdinal);
384 *(WORD *)(p + 1) = offset;
385 break;
387 return TRUE;
391 /***********************************************************************
392 * NE_OpenFile
394 int NE_OpenFile( NE_MODULE *pModule )
396 DOS_FULL_NAME full_name;
397 char *name;
399 static int cachedfd = -1;
401 TRACE( module, "(%p) cache: mod=%p fd=%d\n",
402 pModule, pCachedModule, cachedfd );
403 if (pCachedModule == pModule) return cachedfd;
404 close( cachedfd );
405 pCachedModule = pModule;
406 name = NE_MODULE_NAME( pModule );
407 if (!DOSFS_GetFullName( name, TRUE, &full_name ) ||
408 (cachedfd = open( full_name.long_name, O_RDONLY )) == -1)
409 MSG( "Can't open file '%s' for module %04x\n", name, pModule->self );
410 TRACE(module, "opened '%s' -> %d\n",
411 name, cachedfd );
412 return cachedfd;
416 /***********************************************************************
417 * NE_LoadExeHeader
419 static HMODULE16 NE_LoadExeHeader( HFILE16 hFile, OFSTRUCT *ofs )
421 IMAGE_DOS_HEADER mz_header;
422 IMAGE_OS2_HEADER ne_header;
423 int size;
424 HMODULE16 hModule;
425 NE_MODULE *pModule;
426 BYTE *pData;
427 char *buffer, *fastload = NULL;
428 int fastload_offset = 0, fastload_length = 0;
430 /* Read a block from either the file or the fast-load area. */
431 #define READ(offset,size,buffer) \
432 ((fastload && ((offset) >= fastload_offset) && \
433 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
434 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
435 (_llseek16( hFile, (offset), SEEK_SET), \
436 _hread16( hFile, (buffer), (size) ) == (size)))
438 _llseek16( hFile, 0, SEEK_SET );
439 if ((_hread16(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
440 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
441 return (HMODULE16)11; /* invalid exe */
443 _llseek16( hFile, mz_header.e_lfanew, SEEK_SET );
444 if (_hread16( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
445 return (HMODULE16)11; /* invalid exe */
447 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
448 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
450 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
451 MSG("Sorry, this is an OS/2 linear executable (LX) file !\n");
452 return (HMODULE16)12;
455 /* We now have a valid NE header */
457 size = sizeof(NE_MODULE) +
458 /* segment table */
459 ne_header.n_segment_tab * sizeof(SEGTABLEENTRY) +
460 /* resource table */
461 ne_header.rname_tab_offset - ne_header.resource_tab_offset +
462 /* resident names table */
463 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset +
464 /* module ref table */
465 ne_header.n_mod_ref_tab * sizeof(WORD) +
466 /* imported names table */
467 ne_header.entry_tab_offset - ne_header.iname_tab_offset +
468 /* entry table length */
469 ne_header.entry_tab_length +
470 /* loaded file info */
471 sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
473 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
474 if (!hModule) return (HMODULE16)11; /* invalid exe */
475 FarSetOwner( hModule, hModule );
476 pModule = (NE_MODULE *)GlobalLock16( hModule );
477 memcpy( pModule, &ne_header, sizeof(ne_header) );
478 pModule->count = 0;
479 pModule->module32 = 0;
480 pModule->self = hModule;
481 pModule->self_loading_sel = 0;
482 pData = (BYTE *)(pModule + 1);
484 /* Clear internal Wine flags in case they are set in the EXE file */
486 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
488 /* Read the fast-load area */
490 if (ne_header.additional_flags & NE_AFLAGS_FASTLOAD)
492 fastload_offset=ne_header.fastload_offset<<ne_header.align_shift_count;
493 fastload_length=ne_header.fastload_length<<ne_header.align_shift_count;
494 TRACE(module, "Using fast-load area offset=%x len=%d\n",
495 fastload_offset, fastload_length );
496 if ((fastload = HeapAlloc( SystemHeap, 0, fastload_length )) != NULL)
498 _llseek16( hFile, fastload_offset, SEEK_SET);
499 if (_hread16(hFile, fastload, fastload_length) != fastload_length)
501 HeapFree( SystemHeap, 0, fastload );
502 WARN( module, "Error reading fast-load area!\n");
503 fastload = NULL;
508 /* Get the segment table */
510 pModule->seg_table = (int)pData - (int)pModule;
511 buffer = HeapAlloc( SystemHeap, 0, ne_header.n_segment_tab *
512 sizeof(struct ne_segment_table_entry_s));
513 if (buffer)
515 int i;
516 struct ne_segment_table_entry_s *pSeg;
518 if (!READ( mz_header.e_lfanew + ne_header.segment_tab_offset,
519 ne_header.n_segment_tab * sizeof(struct ne_segment_table_entry_s),
520 buffer ))
522 HeapFree( SystemHeap, 0, buffer );
523 if (fastload) HeapFree( SystemHeap, 0, fastload );
524 GlobalFree16( hModule );
525 return (HMODULE16)11; /* invalid exe */
527 pSeg = (struct ne_segment_table_entry_s *)buffer;
528 for (i = ne_header.n_segment_tab; i > 0; i--, pSeg++)
530 memcpy( pData, pSeg, sizeof(*pSeg) );
531 pData += sizeof(SEGTABLEENTRY);
533 HeapFree( SystemHeap, 0, buffer );
535 else
537 if (fastload) HeapFree( SystemHeap, 0, fastload );
538 GlobalFree16( hModule );
539 return (HMODULE16)11; /* invalid exe */
542 /* Get the resource table */
544 if (ne_header.resource_tab_offset < ne_header.rname_tab_offset)
546 pModule->res_table = (int)pData - (int)pModule;
547 if (!READ(mz_header.e_lfanew + ne_header.resource_tab_offset,
548 ne_header.rname_tab_offset - ne_header.resource_tab_offset,
549 pData )) return (HMODULE16)11; /* invalid exe */
550 pData += ne_header.rname_tab_offset - ne_header.resource_tab_offset;
551 NE_InitResourceHandler( hModule );
553 else pModule->res_table = 0; /* No resource table */
555 /* Get the resident names table */
557 pModule->name_table = (int)pData - (int)pModule;
558 if (!READ( mz_header.e_lfanew + ne_header.rname_tab_offset,
559 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset,
560 pData ))
562 if (fastload) HeapFree( SystemHeap, 0, fastload );
563 GlobalFree16( hModule );
564 return (HMODULE16)11; /* invalid exe */
566 pData += ne_header.moduleref_tab_offset - ne_header.rname_tab_offset;
568 /* Get the module references table */
570 if (ne_header.n_mod_ref_tab > 0)
572 pModule->modref_table = (int)pData - (int)pModule;
573 if (!READ( mz_header.e_lfanew + ne_header.moduleref_tab_offset,
574 ne_header.n_mod_ref_tab * sizeof(WORD),
575 pData ))
577 if (fastload) HeapFree( SystemHeap, 0, fastload );
578 GlobalFree16( hModule );
579 return (HMODULE16)11; /* invalid exe */
581 pData += ne_header.n_mod_ref_tab * sizeof(WORD);
583 else pModule->modref_table = 0; /* No module references */
585 /* Get the imported names table */
587 pModule->import_table = (int)pData - (int)pModule;
588 if (!READ( mz_header.e_lfanew + ne_header.iname_tab_offset,
589 ne_header.entry_tab_offset - ne_header.iname_tab_offset,
590 pData ))
592 if (fastload) HeapFree( SystemHeap, 0, fastload );
593 GlobalFree16( hModule );
594 return (HMODULE16)11; /* invalid exe */
596 pData += ne_header.entry_tab_offset - ne_header.iname_tab_offset;
598 /* Get the entry table */
600 pModule->entry_table = (int)pData - (int)pModule;
601 if (!READ( mz_header.e_lfanew + ne_header.entry_tab_offset,
602 ne_header.entry_tab_length,
603 pData ))
605 if (fastload) HeapFree( SystemHeap, 0, fastload );
606 GlobalFree16( hModule );
607 return (HMODULE16)11; /* invalid exe */
609 pData += ne_header.entry_tab_length;
611 /* Store the filename information */
613 pModule->fileinfo = (int)pData - (int)pModule;
614 size = sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
615 memcpy( pData, ofs, size );
616 ((OFSTRUCT *)pData)->cBytes = size - 1;
617 pData += size;
619 /* Free the fast-load area */
621 #undef READ
622 if (fastload) HeapFree( SystemHeap, 0, fastload );
624 /* Get the non-resident names table */
626 if (ne_header.nrname_tab_length)
628 pModule->nrname_handle = GLOBAL_Alloc( 0, ne_header.nrname_tab_length,
629 hModule, FALSE, FALSE, FALSE );
630 if (!pModule->nrname_handle)
632 GlobalFree16( hModule );
633 return (HMODULE16)11; /* invalid exe */
635 buffer = GlobalLock16( pModule->nrname_handle );
636 _llseek16( hFile, ne_header.nrname_tab_offset, SEEK_SET );
637 if (_hread16( hFile, buffer, ne_header.nrname_tab_length )
638 != ne_header.nrname_tab_length)
640 GlobalFree16( pModule->nrname_handle );
641 GlobalFree16( hModule );
642 return (HMODULE16)11; /* invalid exe */
645 else pModule->nrname_handle = 0;
647 /* Allocate a segment for the implicitly-loaded DLLs */
649 if (pModule->modref_count)
651 pModule->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,
652 (pModule->modref_count+1)*sizeof(HMODULE16),
653 hModule, FALSE, FALSE, FALSE );
654 if (!pModule->dlls_to_init)
656 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
657 GlobalFree16( hModule );
658 return (HMODULE16)11; /* invalid exe */
661 else pModule->dlls_to_init = 0;
663 NE_RegisterModule( pModule );
664 if (fnSNOOP16_RegisterDLL)
665 fnSNOOP16_RegisterDLL(pModule,ofs->szPathName);
666 return hModule;
670 /***********************************************************************
671 * NE_LoadDLLs
673 * Load all DLLs implicitly linked to a module.
675 static BOOL32 NE_LoadDLLs( NE_MODULE *pModule )
677 int i;
678 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
679 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
681 for (i = 0; i < pModule->modref_count; i++, pModRef++)
683 char buffer[260];
684 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
685 memcpy( buffer, pstr + 1, *pstr );
686 strcpy( buffer + *pstr, ".dll" );
687 TRACE(module, "Loading '%s'\n", buffer );
688 if (!(*pModRef = GetModuleHandle16( buffer )))
690 /* If the DLL is not loaded yet, load it and store */
691 /* its handle in the list of DLLs to initialize. */
692 HMODULE16 hDLL;
694 if ((hDLL = NE_LoadModule( buffer, NULL, TRUE, TRUE )) == 2)
696 /* file not found */
697 char *p;
699 /* Try with prepending the path of the current module */
700 GetModuleFileName16( pModule->self, buffer, sizeof(buffer) );
701 if (!(p = strrchr( buffer, '\\' ))) p = buffer;
702 memcpy( p + 1, pstr + 1, *pstr );
703 strcpy( p + 1 + *pstr, ".dll" );
704 hDLL = NE_LoadModule( buffer, NULL, TRUE, TRUE );
706 if (hDLL < 32)
708 /* FIXME: cleanup what was done */
710 MSG( "Could not load '%s' required by '%.*s', error=%d\n",
711 buffer, *((BYTE*)pModule + pModule->name_table),
712 (char *)pModule + pModule->name_table + 1, hDLL );
713 return FALSE;
715 *pModRef = GetExePtr( hDLL );
716 *pDLLs++ = *pModRef;
718 else /* Increment the reference count of the DLL */
720 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
721 if (pOldDLL) pOldDLL->count++;
724 return TRUE;
728 /**********************************************************************
729 * NE_LoadModule
731 * Implementation of LoadModule16().
733 HINSTANCE16 NE_LoadModule( LPCSTR name, HINSTANCE16 *hPrevInstance,
734 BOOL32 implicit, BOOL32 lib_only )
736 HMODULE16 hModule;
737 HINSTANCE16 hInstance;
738 NE_MODULE *pModule;
739 HFILE16 hFile;
740 OFSTRUCT ofs;
742 /* Check if the module is already loaded */
744 if ((hModule = GetModuleHandle16( name )) != 0)
746 HINSTANCE16 prev;
747 pModule = NE_GetPtr( hModule );
748 if ( pModule->module32 ) return (HINSTANCE16)21;
750 hInstance = NE_CreateInstance( pModule, &prev, lib_only );
751 if (hInstance != prev) /* not a library */
752 NE_LoadSegment( pModule, pModule->dgroup );
753 pModule->count++;
754 if (hPrevInstance) *hPrevInstance = prev;
755 return hInstance;
757 if (hPrevInstance) *hPrevInstance = 0;
759 /* Try to load the built-in first if not disabled */
761 if ((hModule = fnBUILTIN_LoadModule( name, FALSE ))) return hModule;
763 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
765 /* Now try the built-in even if disabled */
766 if ((hModule = fnBUILTIN_LoadModule( name, TRUE )))
768 MSG( "Could not load Windows DLL '%s', using built-in module.\n",
769 name );
770 return hModule;
772 return 2; /* File not found */
775 /* Create the module structure */
777 hModule = NE_LoadExeHeader( hFile, &ofs );
778 _lclose16( hFile );
779 if (hModule < 32) return hModule;
780 pModule = NE_GetPtr( hModule );
782 /* Allocate the segments for this module */
784 if (!NE_CreateSegments( pModule ) ||
785 !(hInstance = NE_CreateInstance( pModule, NULL, lib_only )))
787 GlobalFreeAll( hModule );
788 return 8; /* Insufficient memory */
791 /* Load the referenced DLLs */
793 if (!NE_LoadDLLs( pModule ))
794 return 2; /* File not found (FIXME: free everything) */
796 /* Load the segments */
798 NE_LoadAllSegments( pModule );
800 /* Fixup the functions prologs */
802 NE_FixupPrologs( pModule );
804 /* Make sure the usage count is 1 on the first loading of */
805 /* the module, even if it contains circular DLL references */
807 pModule->count = 1;
809 /* Call initialization rountines for all loaded DLLs. Note that
810 * when we load implicitly linked DLLs this will be done by InitTask().
813 if (!implicit && (pModule->flags & NE_FFLAGS_LIBMODULE))
814 NE_InitializeDLLs( hModule );
816 return hInstance;
820 /***********************************************************************
821 * LoadLibrary (KERNEL.95)
823 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
825 HINSTANCE16 handle;
826 LPCSTR p;
827 char *new_name;
829 TRACE(module, "(%08x) %s\n", (int)libname, libname);
831 /* Check for an extension */
833 if ((p = strrchr( libname, '.')) && !strchr( p, '/' ) && !strchr( p, '\\'))
835 /* An extension is present -> use the name as is */
836 return NE_LoadModule( libname, NULL, FALSE, TRUE );
839 /* Now append .dll before loading */
841 if (!(new_name = HeapAlloc( GetProcessHeap(), 0, strlen(libname) + 4 )))
842 return 0;
843 strcpy( new_name, libname );
844 strcat( new_name, ".dll" );
845 handle = NE_LoadModule( new_name, NULL, FALSE, TRUE );
846 HeapFree( GetProcessHeap(), 0, new_name );
847 return handle;
851 /**********************************************************************
852 * MODULE_CallWEP
854 * Call a DLL's WEP, allowing it to shut down.
855 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
857 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
859 FARPROC16 WEP = (FARPROC16)0;
860 WORD ordinal = NE_GetOrdinal( hModule, "WEP" );
862 if (ordinal) WEP = NE_GetEntryPoint( hModule, ordinal );
863 if (!WEP)
865 WARN(module, "module %04x doesn't have a WEP\n", hModule );
866 return FALSE;
868 return Callbacks->CallWindowsExitProc( WEP, WEP_FREE_DLL );
872 /**********************************************************************
873 * NE_FreeModule
875 * Implementation of FreeModule16().
877 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL32 call_wep )
879 HMODULE16 *hPrevModule;
880 NE_MODULE *pModule;
881 HMODULE16 *pModRef;
882 int i;
884 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
885 hModule = pModule->self;
887 TRACE( module, "%04x count %d\n", hModule, pModule->count );
889 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
890 else pModule->count = 0;
892 if (pModule->flags & NE_FFLAGS_BUILTIN)
893 return FALSE; /* Can't free built-in module */
895 if (call_wep)
897 if (pModule->flags & NE_FFLAGS_LIBMODULE)
899 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
900 MODULE_CallWEP( hModule );
902 /* Free the objects owned by the DLL module */
904 if (pTask && pTask->userhandler)
905 pTask->userhandler( hModule, USIG_DLL_UNLOAD, 0,
906 pTask->hInstance, pTask->hQueue );
908 else
909 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
913 /* Clear magic number just in case */
915 pModule->magic = pModule->self = 0;
917 /* Remove it from the linked list */
919 hPrevModule = &hFirstModule;
920 while (*hPrevModule && (*hPrevModule != hModule))
922 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
924 if (*hPrevModule) *hPrevModule = pModule->next;
926 /* Free the referenced modules */
928 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
929 for (i = 0; i < pModule->modref_count; i++, pModRef++)
931 NE_FreeModule( *pModRef, call_wep );
934 /* Free the module storage */
936 GlobalFreeAll( hModule );
938 /* Remove module from cache */
940 if (pCachedModule == pModule) pCachedModule = NULL;
941 return TRUE;
945 /**********************************************************************
946 * FreeModule16 (KERNEL.46)
948 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
950 return NE_FreeModule( hModule, TRUE );
954 /***********************************************************************
955 * FreeLibrary16 (KERNEL.96)
957 void WINAPI FreeLibrary16( HINSTANCE16 handle )
959 TRACE(module,"%04x\n", handle );
960 FreeModule16( handle );
964 /**********************************************************************
965 * GetModuleName (KERNEL.27)
967 BOOL16 WINAPI GetModuleName( HINSTANCE16 hinst, LPSTR buf, INT16 count )
969 NE_MODULE *pModule;
970 BYTE *p;
972 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
973 p = (BYTE *)pModule + pModule->name_table;
974 if (count > *p) count = *p + 1;
975 if (count > 0)
977 memcpy( buf, p + 1, count - 1 );
978 buf[count-1] = '\0';
980 return TRUE;
984 /**********************************************************************
985 * GetModuleUsage (KERNEL.48)
987 INT16 WINAPI GetModuleUsage( HINSTANCE16 hModule )
989 NE_MODULE *pModule = NE_GetPtr( hModule );
990 return pModule ? pModule->count : 0;
994 /**********************************************************************
995 * GetExpWinVer (KERNEL.167)
997 WORD WINAPI GetExpWinVer( HMODULE16 hModule )
999 NE_MODULE *pModule = NE_GetPtr( hModule );
1000 return pModule ? pModule->expected_version : 0;
1004 /**********************************************************************
1005 * GetModuleFileName16 (KERNEL.49)
1007 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1008 INT16 nSize )
1010 NE_MODULE *pModule;
1012 if (!hModule) hModule = GetCurrentTask();
1013 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1014 lstrcpyn32A( lpFileName, NE_MODULE_NAME(pModule), nSize );
1015 TRACE(module, "%s\n", lpFileName );
1016 return strlen(lpFileName);
1020 /**********************************************************************
1021 * GetModuleHandle16 (KERNEL.47)
1023 * Find a module from a path name.
1025 * RETURNS
1026 * LOWORD:
1027 * the win16 module handle if found
1028 * 0 if not
1029 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1030 * Always hFirstModule
1032 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1034 if (HIWORD(name) == 0)
1035 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1036 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name)), hFirstModule );
1039 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1041 HMODULE16 hModule = hFirstModule;
1042 LPCSTR filename, dotptr, modulepath, modulename;
1043 BYTE len, *name_table;
1045 if (!(filename = strrchr( name, '\\' ))) filename = name;
1046 else filename++;
1047 if ((dotptr = strrchr( filename, '.' )) != NULL)
1048 len = (BYTE)(dotptr - filename);
1049 else len = strlen( filename );
1051 while (hModule)
1053 NE_MODULE *pModule = NE_GetPtr( hModule );
1054 if (!pModule) break;
1055 modulepath = NE_MODULE_NAME(pModule);
1056 if (!(modulename = strrchr( modulepath, '\\' )))
1057 modulename = modulepath;
1058 else modulename++;
1059 if (!lstrcmpi32A( modulename, filename )) return hModule;
1061 name_table = (BYTE *)pModule + pModule->name_table;
1062 if ((*name_table == len) && !lstrncmpi32A(filename, name_table+1, len))
1063 return hModule;
1064 hModule = pModule->next;
1066 return 0;
1070 /**********************************************************************
1071 * ModuleFirst (TOOLHELP.59)
1073 BOOL16 WINAPI ModuleFirst( MODULEENTRY *lpme )
1075 lpme->wNext = hFirstModule;
1076 return ModuleNext( lpme );
1080 /**********************************************************************
1081 * ModuleNext (TOOLHELP.60)
1083 BOOL16 WINAPI ModuleNext( MODULEENTRY *lpme )
1085 NE_MODULE *pModule;
1086 char *name;
1088 if (!lpme->wNext) return FALSE;
1089 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1090 name = (char *)pModule + pModule->name_table;
1091 memcpy( lpme->szModule, name + 1, *name );
1092 lpme->szModule[(BYTE)*name] = '\0';
1093 lpme->hModule = lpme->wNext;
1094 lpme->wcUsage = pModule->count;
1095 strncpy( lpme->szExePath, NE_MODULE_NAME(pModule), MAX_PATH );
1096 lpme->szExePath[MAX_PATH] = '\0';
1097 lpme->wNext = pModule->next;
1098 return TRUE;
1102 /**********************************************************************
1103 * ModuleFindName (TOOLHELP.61)
1105 BOOL16 WINAPI ModuleFindName( MODULEENTRY *lpme, LPCSTR name )
1107 lpme->wNext = GetModuleHandle16( name );
1108 return ModuleNext( lpme );
1112 /**********************************************************************
1113 * ModuleFindHandle (TOOLHELP.62)
1115 BOOL16 WINAPI ModuleFindHandle( MODULEENTRY *lpme, HMODULE16 hModule )
1117 hModule = GetExePtr( hModule );
1118 lpme->wNext = hModule;
1119 return ModuleNext( lpme );
1122 /***************************************************************************
1123 * MapHModuleLS (KERNEL32.520)
1125 HMODULE16 WINAPI MapHModuleLS(HMODULE32 hmod) {
1126 NE_MODULE *pModule;
1128 if (!hmod)
1129 return ((TDB*)GlobalLock16(GetCurrentTask()))->hInstance;
1130 if (!HIWORD(hmod))
1131 return hmod; /* we already have a 16 bit module handle */
1132 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1133 while (pModule) {
1134 if (pModule->module32 == hmod)
1135 return pModule->self;
1136 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1138 return 0;
1141 /***************************************************************************
1142 * MapHModuleSL (KERNEL32.521)
1144 HMODULE32 WINAPI MapHModuleSL(HMODULE16 hmod) {
1145 NE_MODULE *pModule;
1147 if (!hmod) {
1148 TDB *pTask = (TDB*)GlobalLock16(GetCurrentTask());
1150 hmod = pTask->hInstance;
1152 pModule = (NE_MODULE*)GlobalLock16(hmod);
1153 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1154 !(pModule->flags & NE_FFLAGS_WIN32)
1156 return 0;
1157 return pModule->module32;
1160 /***************************************************************************
1161 * MapHInstLS (KERNEL32.516)
1163 REGS_ENTRYPOINT(MapHInstLS) {
1164 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1167 /***************************************************************************
1168 * MapHInstLS (KERNEL32.518)
1170 REGS_ENTRYPOINT(MapHInstSL) {
1171 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1174 /***************************************************************************
1175 * WIN16_MapHInstLS (KERNEL.472)
1177 VOID WINAPI WIN16_MapHInstLS( CONTEXT *context ) {
1178 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1181 /***************************************************************************
1182 * WIN16_MapHInstSL (KERNEL.473)
1184 VOID WINAPI WIN16_MapHInstSL( CONTEXT *context ) {
1185 EAX_reg(context) = MapHModuleSL(EAX_reg(context));