Release 950319
[wine/hacks.git] / loader / library.c
blobe610d3c7d7af665c34d57c9f39dfec7340843226
1 /*
2 * Module & Library functions
3 static char Copyright[] = "Copyright 1993, 1994 Martin Ayotte, Robert J. Amstadt, Erik Bos";
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include "neexe.h"
14 #include "dlls.h"
15 #include "if1632.h"
16 #include "wineopts.h"
17 #include "arch.h"
18 #include "options.h"
19 #include "dos_fs.h"
20 #include "windows.h"
21 #include "task.h"
22 #include "toolhelp.h"
23 #include "selectors.h"
24 #include "stddebug.h"
25 #include "debug.h"
26 #include "prototypes.h"
27 #include "library.h"
28 #include "ne_image.h"
29 #include "pe_image.h"
31 struct w_files *wine_files = NULL;
32 static char *DLL_Extensions[] = { "dll", NULL };
33 static char *EXE_Extensions[] = { "exe", NULL };
35 #define IS_BUILTIN_DLL(handle) ((handle >> 8) == 0xff)
37 /**********************************************************************/
39 void ExtractDLLName(char *libname, char *temp)
41 int i;
43 strcpy(temp, libname);
44 if (strchr(temp, '\\') || strchr(temp, '/'))
45 for (i = strlen(temp) - 1; i ; i--)
46 if (temp[i] == '\\' || temp[i] == '/') {
47 strcpy(temp, temp + i + 1);
48 break;
50 for (i = strlen(temp) - 1; i ; i--)
51 if (temp[i] == '.') {
52 temp[i] = 0;
53 break;
57 struct w_files *GetFileInfo(unsigned short instance)
59 register struct w_files *w = wine_files;
61 while (w && w->hinstance != instance)
62 w = w->next;
64 return w;
67 int IsDLLLoaded(char *name)
69 struct w_files *wpnt;
71 if(FindDLLTable(name))
72 return 1;
74 for(wpnt = wine_files; wpnt; wpnt = wpnt->next)
75 if(strcmp(wpnt->name, name) == 0)
76 return 1;
78 return 0;
81 void InitDLL(struct w_files *wpnt)
83 if (wpnt->ne)
84 NE_InitDLL(wpnt);
85 else
86 PE_InitDLL(wpnt);
89 void InitializeLoadedDLLs(struct w_files *wpnt)
91 static flagReadyToRun = 0;
92 struct w_files *final_wpnt;
94 dprintf_module(stddeb,"InitializeLoadedDLLs(%p)\n", wpnt);
96 if (wpnt == NULL)
98 flagReadyToRun = 1;
99 dprintf_module(stddeb,"Initializing DLLs\n");
102 if (!flagReadyToRun)
103 return;
105 #if 1
106 if (wpnt != NULL)
107 dprintf_module(stddeb,"Initializing %s\n", wpnt->name);
108 #endif
111 * Initialize libraries
113 if (!wpnt)
115 wpnt = wine_files;
116 final_wpnt = NULL;
118 else
120 final_wpnt = wpnt->next;
123 for( ; wpnt != final_wpnt; wpnt = wpnt->next)
124 InitDLL(wpnt);
127 /**********************************************************************
128 * LoadImage
129 * Load one executable into memory
131 HINSTANCE LoadImage(char *module, int filetype, int change_dir)
133 HINSTANCE handle;
134 struct w_files *wpnt, *wpnt1;
135 char buffer[256], header[2], modulename[64], *fullname;
137 ExtractDLLName(module, modulename);
138 dprintf_module(stddeb,"LoadImage [%s]\n", module);
139 /* built-in one ? */
140 if (FindDLLTable(modulename)) {
141 return GetModuleHandle(modulename);
144 /* already loaded ? */
145 for (wpnt = wine_files ; wpnt ; wpnt = wpnt->next)
146 if (strcasecmp(wpnt->name, modulename) == 0)
147 return wpnt->hinstance;
150 * search file
152 fullname = DOS_FindFile(buffer, sizeof(buffer), module,
153 (filetype == EXE ? EXE_Extensions : DLL_Extensions),
154 WindowsPath);
155 if (fullname == NULL)
157 fprintf(stderr, "LoadImage: I can't find %s.dll | %s.exe !\n",
158 module, module);
159 return 2;
162 fullname = DOS_GetDosFileName(fullname);
164 dprintf_module(stddeb,"LoadImage: loading %s (%s)\n [%s]\n",
165 module, buffer, fullname);
167 if (change_dir && fullname)
169 char dirname[256];
170 char *p;
172 strcpy(dirname, fullname);
173 p = strrchr(dirname, '\\');
174 *p = '\0';
176 DOS_SetDefaultDrive(dirname[0] - 'A');
177 DOS_ChangeDir(dirname[0] - 'A', dirname + 2);
180 /* First allocate a spot to store the info we collect, and add it to
181 * our linked list if we could load the file.
184 wpnt = (struct w_files *) malloc(sizeof(struct w_files));
187 * Open file for reading.
189 wpnt->fd = open(buffer, O_RDONLY);
190 if (wpnt->fd < 0)
191 return 2;
194 * Establish header pointers.
196 wpnt->filename = strdup(buffer);
197 wpnt->name = strdup(modulename);
199 /* read mz header */
200 wpnt->mz_header = (struct mz_header_s *) malloc(sizeof(struct mz_header_s));;
201 lseek(wpnt->fd, 0, SEEK_SET);
202 if (read(wpnt->fd, wpnt->mz_header, sizeof(struct mz_header_s)) !=
203 sizeof(struct mz_header_s))
205 fprintf(stderr, "Unable to read MZ header from file '%s'\n", buffer);
206 exit(1);
209 /* This field is ignored according to "Windows Internals", p.242 */
210 #if 0
211 if (wpnt->mz_header->must_be_0x40 != 0x40)
212 myerror("This is not a Windows program");
213 #endif
215 /* read first two bytes to determine filetype */
216 lseek(wpnt->fd, wpnt->mz_header->ne_offset, SEEK_SET);
217 read(wpnt->fd, &header, sizeof(header));
219 handle = 0;
222 * Stick this file into the list of loaded files so we don't try to reload
223 * it again if another module references this module. Do this before
224 * calling NE_LoadImage because we might get back here before NE_loadImage
225 * returns.
227 if(wine_files == NULL)
228 wine_files = wpnt;
229 else {
230 wpnt1 = wine_files;
231 while(wpnt1->next)
232 wpnt1 = wpnt1->next;
233 wpnt1->next = wpnt;
235 wpnt->next = NULL;
237 if (header[0] == 'N' && header[1] == 'E')
238 handle = NE_LoadImage(wpnt);
239 if (header[0] == 'P' && header[1] == 'E')
240 handle = PE_LoadImage(wpnt);
241 wpnt->hinstance = handle;
243 if (handle > 32) {
244 return handle;
245 } else {
246 fprintf(stderr, "wine: (%s) unknown fileformat !\n", wpnt->filename);
248 /* Remove this module from the list of loaded modules */
249 if (wine_files == wpnt)
250 wine_files = NULL;
251 else
252 wpnt1->next = NULL;
253 close(wpnt->fd);
254 free(wpnt->filename);
255 free(wpnt->name);
256 free(wpnt);
258 return 14;
262 /**********************************************************************
263 * GetModuleHandle [KERNEL.47]
265 HANDLE GetModuleHandle(LPSTR lpModuleName)
267 register struct w_files *w = wine_files;
268 int i;
269 char dllname[256];
271 if ((int) lpModuleName & 0xffff0000)
272 ExtractDLLName(lpModuleName, dllname);
274 if ((int) lpModuleName & 0xffff0000)
275 dprintf_module(stddeb,"GetModuleHandle('%s');\n", lpModuleName);
276 else
277 dprintf_module(stddeb,"GetModuleHandle('%p');\n", lpModuleName);
279 /* dprintf_module(stddeb,"GetModuleHandle // searching in builtin libraries\n");*/
280 for (i = 0; i < N_BUILTINS; i++) {
281 if (dll_builtin_table[i].dll_name == NULL) break;
282 if (!dll_builtin_table[i].dll_is_used){
283 dprintf_module(stddeb,"Skipping builtin %s\n",
284 dll_builtin_table[i].dll_name);
285 continue;
287 if (((int) lpModuleName & 0xffff0000) == 0) {
288 if (0xFF00 + i == (int) lpModuleName) {
289 dprintf_module(stddeb,"GetModuleHandle('%s') return %04X \n",
290 lpModuleName, 0xff00 + i);
291 return 0xFF00 + i;
294 else if (strcasecmp(dll_builtin_table[i].dll_name, dllname) == 0) {
295 dprintf_module(stddeb,"GetModuleHandle('%p') return %04X \n",
296 lpModuleName, 0xFF00 + i);
297 return (0xFF00 + i);
301 dprintf_module(stddeb,"GetModuleHandle // searching in loaded modules\n");
302 while (w) {
303 /* dprintf_module(stddeb,"GetModuleHandle // '%x' \n", w->name); */
304 if (((int) lpModuleName & 0xffff0000) == 0) {
305 if (w->hinstance == (int) lpModuleName) {
306 dprintf_module(stddeb,"GetModuleHandle('%p') return %04X \n",
307 lpModuleName, w->hinstance);
308 return w->hinstance;
311 else if (strcasecmp(w->name, dllname) == 0) {
312 dprintf_module(stddeb,"GetModuleHandle('%s') return %04X \n",
313 lpModuleName, w->hinstance);
314 return w->hinstance;
316 w = w->next;
318 printf("GetModuleHandle('%p') not found !\n", lpModuleName);
319 return 0;
323 /**********************************************************************
324 * GetModuleUsage [KERNEL.48]
326 int GetModuleUsage(HANDLE hModule)
328 struct w_files *w;
330 dprintf_module(stddeb,"GetModuleUsage(%04X);\n", hModule);
332 /* built-in dll ? */
333 if (IS_BUILTIN_DLL(hModule))
334 return 2;
336 w = GetFileInfo(hModule);
337 /* return w->Usage; */
338 return 1;
342 /**********************************************************************
343 * GetModuleFilename [KERNEL.49]
345 int GetModuleFileName(HANDLE hModule, LPSTR lpFileName, short nSize)
347 struct w_files *w;
348 LPSTR str;
349 char windir[256], temp[256];
351 dprintf_module(stddeb,"GetModuleFileName(%04X, %p, %d);\n", hModule, lpFileName, nSize);
353 if (lpFileName == NULL) return 0;
354 if (nSize < 1) return 0;
356 /* built-in dll ? */
357 if (IS_BUILTIN_DLL(hModule)) {
358 GetWindowsDirectory(windir, sizeof(windir));
359 sprintf(temp, "%s\\%s.DLL", windir, dll_builtin_table[hModule & 0x00ff].dll_name);
360 ToDos(temp);
361 strncpy(lpFileName, temp, nSize);
362 dprintf_module(stddeb,"GetModuleFileName copied '%s' (internal dll) return %d \n", lpFileName, nSize);
363 return strlen(lpFileName);
366 /* check loaded dlls */
367 if ((w = GetFileInfo(hModule)) == NULL)
368 return 0;
369 str = DOS_GetDosFileName(w->filename);
370 if (nSize > strlen(str)) nSize = strlen(str) + 1;
371 strncpy(lpFileName, str, nSize);
372 dprintf_module(stddeb,"GetModuleFileName copied '%s' return %d \n", lpFileName, nSize);
373 return nSize - 1;
377 /**********************************************************************
378 * LoadLibrary [KERNEL.95]
380 HANDLE LoadLibrary(LPSTR libname)
382 HANDLE h;
384 if ((h = LoadImage(libname, DLL, 0)) < 32)
385 return h;
387 if (!IS_BUILTIN_DLL(h))
388 InitDLL(GetFileInfo(h));
390 return h;
393 /**********************************************************************
394 * FreeLibrary [KERNEL.96]
396 void FreeLibrary(HANDLE hLib)
398 dprintf_module(stddeb,"FreeLibrary(%04X);\n", hLib);
400 /* built-in dll ? */
401 if (IS_BUILTIN_DLL(hLib) || hLib == 0 || hLib == hSysRes)
402 return;
405 while (lpMod != NULL) {
406 if (lpMod->hInst == hLib) {
407 if (lpMod->Count == 1) {
408 wpnt = GetFileInfo(hLib);
409 if (wpnt->ne)
410 NE_UnloadImage(wpnt);
411 else
412 PE_UnloadImage(wpnt);
413 if (hLib != (HANDLE)NULL) GlobalFree(hLib);
414 if (lpMod->ModuleName != NULL) free(lpMod->ModuleName);
415 if (lpMod->FileName != NULL) free(lpMod->FileName);
416 GlobalFree(lpMod->hModule);
417 dprintf_module(stddeb,"FreeLibrary // freed !\n");
418 return;
420 lpMod->Count--;
421 dprintf_module(stddeb,"FreeLibrary // Count decremented !\n");
422 return;
424 lpMod = lpMod->lpNextModule;
430 /**********************************************************************
431 * GetProcAddress [KERNEL.50]
433 FARPROC GetProcAddress(HANDLE hModule, char *proc_name)
435 #ifdef WINELIB
436 WINELIB_UNIMP ("GetProcAddress");
437 #else
438 int addr, ret;
439 WORD sel;
440 register struct w_files *w = wine_files;
441 int ordinal, len;
442 char * cpnt;
443 char C[128];
444 HTASK hTask;
445 LPTASKENTRY lpTask;
447 /* built-in dll ? */
448 if (IS_BUILTIN_DLL(hModule))
450 if ((int) proc_name & 0xffff0000)
452 dprintf_module(stddeb,"GetProcAddress: builtin %#04X, '%s'\n",
453 hModule, proc_name);
454 if (GetEntryDLLName(dll_builtin_table[hModule - 0xFF00].dll_name,
455 proc_name, &sel, &addr))
457 printf("Address not found !\n");
460 else
462 dprintf_module(stddeb,"GetProcAddress: builtin %#04X, %d\n",
463 hModule, (int)proc_name);
464 if (GetEntryDLLOrdinal(dll_builtin_table[hModule-0xFF00].dll_name,
465 (int)proc_name & 0x0000FFFF, &sel, &addr))
467 printf("Address not found !\n");
470 ret = MAKELONG(addr, sel);
471 dprintf_module(stddeb,"GetProcAddress // ret=%08X sel=%04X addr=%04X\n",
472 ret, sel, addr);
473 return (FARPROC)ret;
475 if (hModule == 0)
477 hTask = GetCurrentTask();
478 dprintf_module(stddeb,"GetProcAddress // GetCurrentTask()=%04X\n", hTask);
479 lpTask = (LPTASKENTRY) GlobalLock(hTask);
480 if (lpTask == NULL)
482 printf("GetProcAddress: can't find current module handle !\n");
483 return NULL;
485 hModule = lpTask->hInst;
486 dprintf_module(stddeb,"GetProcAddress: current module=%04X instance=%04X!\n",
487 lpTask->hModule, lpTask->hInst);
488 GlobalUnlock(hTask);
490 while (w && w->hinstance != hModule)
491 w = w->next;
492 if (w == NULL)
493 return NULL;
494 dprintf_module(stddeb,"GetProcAddress // Module Found ! w->filename='%s'\n", w->filename);
495 if ((int)proc_name & 0xFFFF0000)
497 AnsiUpper(proc_name);
498 dprintf_module(stddeb,"GetProcAddress: %04X, '%s'\n", hModule, proc_name);
499 cpnt = w->ne->nrname_table;
500 while(TRUE)
502 if (((int) cpnt) - ((int)w->ne->nrname_table) >
503 w->ne->ne_header->nrname_tab_length) return NULL;
504 len = *cpnt++;
505 strncpy(C, cpnt, len);
506 C[len] = '\0';
507 dprintf_module(stddeb,"pointing Function '%s' ordinal=%d !\n",
508 C, *((unsigned short *)(cpnt + len)));
509 if (strncmp(cpnt, proc_name, len) == 0)
511 ordinal = *((unsigned short *)(cpnt + len));
512 break;
514 cpnt += len + 2;
516 if (ordinal == 0)
518 printf("GetProcAddress // function '%s' not found !\n", proc_name);
519 return NULL;
522 else
524 dprintf_module(stddeb,"GetProcAddress: %#04x, %d\n", hModule, (int) proc_name);
525 ordinal = (int)proc_name;
527 ret = GetEntryPointFromOrdinal(w, ordinal);
528 if (ret == -1)
530 printf("GetProcAddress // Function #%d not found !\n", ordinal);
531 return NULL;
533 addr = ret & 0xffff;
534 sel = (ret >> 16);
535 dprintf_module(stddeb,"GetProcAddress // ret=%08X sel=%04X addr=%04X\n", ret, sel, addr);
536 return (FARPROC) ret;
537 #endif /* WINELIB */
540 /* internal dlls */
541 static void
542 FillModStructBuiltIn(MODULEENTRY *lpModule, struct dll_name_table_entry_s *dll)
544 lpModule->dwSize = dll->dll_table_length * 1024;
545 strcpy(lpModule->szModule, dll->dll_name);
546 lpModule->hModule = 0xff00 + dll->dll_number;
547 lpModule->wcUsage = GetModuleUsage(lpModule->hModule);
548 GetModuleFileName(lpModule->hModule, lpModule->szExePath, MAX_PATH + 1);
549 lpModule->wNext = 0;
552 /* loaded dlls */
553 static void
554 FillModStructLoaded(MODULEENTRY *lpModule, struct w_files *dll)
556 lpModule->dwSize = 16384;
557 strcpy(lpModule->szModule, dll->name);
558 lpModule->hModule = dll->hinstance;
559 lpModule->wcUsage = GetModuleUsage(lpModule->hModule);
560 GetModuleFileName(lpModule->hModule, lpModule->szExePath, MAX_PATH + 1);
561 lpModule->wNext = 0;
564 /**********************************************************************
565 * ModuleFirst [TOOLHELP.59]
567 BOOL ModuleFirst(MODULEENTRY *lpModule)
569 dprintf_module(stddeb,"ModuleFirst(%08X)\n", (int) lpModule);
571 FillModStructBuiltIn(lpModule, &dll_builtin_table[0]);
572 return TRUE;
575 /**********************************************************************
576 * ModuleNext [TOOLHELP.60]
578 BOOL ModuleNext(MODULEENTRY *lpModule)
580 struct w_files *w;
582 dprintf_module(stddeb,"ModuleNext(%08X)\n", (int) lpModule);
584 if (IS_BUILTIN_DLL(lpModule->hModule))
586 int builtin_no=lpModule->hModule & 0xff;
588 /* last built-in ? */
589 if (builtin_no == (N_BUILTINS - 1) ) {
590 if (wine_files) {
591 FillModStructLoaded(lpModule, wine_files);
592 return TRUE;
593 } else
594 return FALSE;
596 builtin_no++;
597 }while(!dll_builtin_table[builtin_no].dll_is_used);
599 FillModStructBuiltIn(lpModule, &dll_builtin_table[builtin_no]);
600 return TRUE;
602 w = GetFileInfo(lpModule->hModule);
603 if (w->next) {
604 FillModStructLoaded(lpModule, w->next);
605 return TRUE;
607 return FALSE;
610 /**********************************************************************
611 * ModuleFindHandle [TOOLHELP.62]
613 HMODULE ModuleFindHandle(MODULEENTRY *lpModule, HMODULE hModule)
615 struct w_files *w;
617 dprintf_module(stddeb,"ModuleFindHandle(%08X, %04X)\n", (int) lpModule, (int)hModule);
619 /* built-in dll ? */
620 if (IS_BUILTIN_DLL(hModule)) {
621 FillModStructBuiltIn(lpModule, &dll_builtin_table[hModule & 0xff]);
622 return hModule;
625 /* check loaded dlls */
626 if ((w = GetFileInfo(hModule)) == NULL)
627 return (HMODULE) NULL;
629 FillModStructLoaded(lpModule, w);
630 return w->hinstance;
633 /**********************************************************************
634 * ModuleFindName [TOOLHELP.61]
636 HMODULE ModuleFindName(MODULEENTRY *lpModule, LPCSTR lpstrName)
638 return (ModuleFindHandle(lpModule, GetModuleHandle((char*)lpstrName)));