Release 940510
[wine/multimedia.git] / loader / wine.c
blob896ed0608ad201f6b8408d76884e1f349cc7bdee
1 static char RCSId[] = "$Id: wine.c,v 1.2 1993/07/04 04:04:21 root Exp root $";
2 static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #ifdef linux
11 #include <linux/unistd.h>
12 #include <linux/head.h>
13 #include <linux/ldt.h>
14 #include <linux/segment.h>
15 #endif
16 #include <string.h>
17 #include <errno.h>
18 #include "neexe.h"
19 #include "segmem.h"
20 #include "prototypes.h"
21 #include "dlls.h"
22 #include "wine.h"
23 #include "windows.h"
24 #include "wineopts.h"
25 #include "arch.h"
26 #include "options.h"
28 /* #define DEBUG_FIXUP */
30 extern int CallToInit16(unsigned long csip, unsigned long sssp,
31 unsigned short ds);
32 extern void CallTo32();
34 char * GetModuleName(struct w_files * wpnt, int index, char *buffer);
35 extern unsigned char ran_out;
36 extern char WindowsPath[256];
37 unsigned short WIN_StackSize;
38 unsigned short WIN_HeapSize;
40 struct w_files * wine_files = NULL;
42 int WineForceFail = 0;
44 char **Argv;
45 int Argc;
46 struct mz_header_s *CurrentMZHeader;
47 struct ne_header_s *CurrentNEHeader;
48 int CurrentNEFile;
49 HINSTANCE hSysRes;
51 static char *DLL_Extensions[] = { "dll", "exe", NULL };
52 static char *EXE_Extensions[] = { "exe", NULL };
53 static char *WinePath = NULL;
55 FILE *SpyFp = NULL;
57 /**********************************************************************
58 * myerror
60 void
61 myerror(const char *s)
63 if (s == NULL)
64 perror("wine");
65 else
66 fprintf(stderr, "wine: %s\n", s);
68 exit(1);
71 /**********************************************************************
72 * GetFilenameFromInstance
74 char *
75 GetFilenameFromInstance(unsigned short instance)
77 register struct w_files *w = wine_files;
79 while (w && w->hinstance != instance)
80 w = w->next;
82 if (w)
83 return w->filename;
84 else
85 return NULL;
88 struct w_files *
89 GetFileInfo(unsigned short instance)
91 register struct w_files *w = wine_files;
93 while (w && w->hinstance != instance)
94 w = w->next;
96 return w;
99 #ifndef WINELIB
100 /**********************************************************************
102 * Load MZ Header
104 void load_mz_header(int fd, struct mz_header_s *mz_header)
106 if (read(fd, mz_header, sizeof(struct mz_header_s)) !=
107 sizeof(struct mz_header_s))
109 myerror("Unable to read MZ header from file");
113 void load_ne_header (int fd, struct ne_header_s *ne_header)
115 if (read(fd, ne_header, sizeof(struct ne_header_s))
116 != sizeof(struct ne_header_s))
118 myerror("Unable to read NE header from file");
121 #endif
123 /**********************************************************************
124 * LoadImage
125 * Load one NE format executable into memory
127 HINSTANCE LoadImage(char *modulename, int filetype)
129 unsigned int read_size;
130 int i;
131 struct w_files * wpnt, *wpnt1;
132 unsigned int status;
133 char buffer[256];
136 * search file
138 if (FindFile(buffer, sizeof(buffer), modulename, (filetype == EXE ?
139 EXE_Extensions : DLL_Extensions), WindowsPath) ==NULL)
141 fprintf(stderr, "LoadImage: I can't find %s.dll | %s.exe !\n",modulename, modulename);
142 return (HINSTANCE) NULL;
144 fprintf(stderr,"LoadImage: loading %s (%s)\n", modulename, buffer);
146 /* First allocate a spot to store the info we collect, and add it to
147 * our linked list.
150 wpnt = (struct w_files *) malloc(sizeof(struct w_files));
151 if(wine_files == NULL)
152 wine_files = wpnt;
153 else {
154 wpnt1 = wine_files;
155 while(wpnt1->next) wpnt1 = wpnt1->next;
156 wpnt1->next = wpnt;
158 wpnt->next = NULL;
161 * Open file for reading.
163 wpnt->fd = open(buffer, O_RDONLY);
164 if (wpnt->fd < 0)
166 myerror(NULL);
169 * Establish header pointers.
171 wpnt->filename = strdup(buffer);
172 wpnt->name = NULL;
173 if(modulename) wpnt->name = strdup(modulename);
175 wpnt->mz_header = (struct mz_header_s *) malloc(sizeof(struct mz_header_s));;
176 status = lseek(wpnt->fd, 0, SEEK_SET);
177 load_mz_header (wpnt->fd, wpnt->mz_header);
178 if (wpnt->mz_header->must_be_0x40 != 0x40)
179 myerror("This is not a Windows program");
181 wpnt->ne_header = (struct ne_header_s *) malloc(sizeof(struct ne_header_s));
182 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset, SEEK_SET);
183 load_ne_header (wpnt->fd, wpnt->ne_header);
184 if (wpnt->ne_header->header_type[0] != 'N' ||
185 wpnt->ne_header->header_type[1] != 'E')
186 myerror("This is not a Windows program");
188 if(wine_files == wpnt){
189 CurrentMZHeader = wpnt->mz_header;
190 CurrentNEHeader = wpnt->ne_header;
191 CurrentNEFile = wpnt->fd;
193 WIN_StackSize = wpnt->ne_header->stack_length;
194 WIN_HeapSize = wpnt->ne_header->local_heap_length;
198 * Create segment selectors.
200 #ifndef WINELIB
201 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
202 wpnt->ne_header->segment_tab_offset,
203 SEEK_SET);
204 read_size = wpnt->ne_header->n_segment_tab *
205 sizeof(struct ne_segment_table_entry_s);
206 wpnt->seg_table = (struct ne_segment_table_entry_s *) malloc(read_size);
207 if (read(wpnt->fd, wpnt->seg_table, read_size) != read_size)
208 myerror("Unable to read segment table header from file");
209 wpnt->selector_table = CreateSelectors(wpnt);
210 wpnt->hinstance
211 = wpnt->
212 selector_table[wpnt->ne_header->auto_data_seg-1].selector;
214 /* Get the lookup table. This is used for looking up the addresses
215 of functions that are exported */
217 read_size = wpnt->ne_header->entry_tab_length;
218 wpnt->lookup_table = (char *) malloc(read_size);
219 lseek(wpnt->fd, wpnt->mz_header->ne_offset +
220 wpnt->ne_header->entry_tab_offset, SEEK_SET);
221 if (read(wpnt->fd, wpnt->lookup_table, read_size) != read_size)
222 myerror("Unable to read lookup table header from file");
224 /* Get the iname table. This is used for looking up the names
225 of functions that are exported */
227 status = lseek(wpnt->fd, wpnt->ne_header->nrname_tab_offset, SEEK_SET);
228 read_size = wpnt->ne_header->nrname_tab_length;
229 wpnt->nrname_table = (char *) malloc(read_size);
230 if (read(wpnt->fd, wpnt->nrname_table, read_size) != read_size)
231 myerror("Unable to read nrname table header from file");
233 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
234 wpnt->ne_header->rname_tab_offset, SEEK_SET);
235 read_size = wpnt->ne_header->moduleref_tab_offset -
236 wpnt->ne_header->rname_tab_offset;
237 wpnt->rname_table = (char *) malloc(read_size);
238 if (read(wpnt->fd, wpnt->rname_table, read_size) != read_size)
239 myerror("Unable to read rname table header from file");
241 /* Now get the module name */
243 wpnt->name = (char*) malloc(*wpnt->rname_table + 1);
244 memcpy(wpnt->name, wpnt->rname_table+1, *wpnt->rname_table);
245 wpnt->name[*wpnt->rname_table] = 0;
246 #endif
248 * Now load any DLLs that this module refers to.
250 for(i=0; i<wpnt->ne_header->n_mod_ref_tab; i++){
251 char buff[14];
252 char buff2[256];
253 int fd, j;
254 GetModuleName(wpnt, i + 1, buff);
256 #ifndef WINELIB
257 if(FindDLLTable(buff)) continue; /* This module already loaded */
258 #endif
260 LoadImage(buff, DLL);
262 fprintf(stderr,"Unable to load:%s\n", buff);
265 return(wpnt->hinstance);
269 /**********************************************************************
270 * main
272 _WinMain(int argc, char **argv)
274 int segment;
275 char *p;
276 char *sysresname;
277 char filename[100];
278 char syspath[256];
279 char exe_path[256];
280 #ifdef WINESTAT
281 char * cp;
282 #endif
283 struct w_files * wpnt;
284 int cs_reg, ds_reg, ss_reg, ip_reg, sp_reg;
285 int i;
286 int rv;
288 Argc = argc - 1;
289 Argv = argv + 1;
291 WinePath = malloc(1024);
293 getcwd(WinePath, 512);
295 if ((p = getenv("WINEPATH")) != NULL) {
296 strcat(WinePath, ";");
297 strcat(WinePath, p);
300 if ((hInstMain = LoadImage(Argv[0], EXE)) == (HINSTANCE) NULL ) {
301 fprintf(stderr, "wine: can't find %s!.\n", Argv[0]);
302 exit(1);
305 GetPrivateProfileString("wine", "SystemResources", "sysres.dll",
306 filename, sizeof(filename), WINE_INI);
308 hSysRes = LoadImage(filename, DLL);
309 if (hSysRes == (HINSTANCE)NULL)
310 fprintf(stderr, "wine: can't find %s!\n", filename);
311 else
312 printf("System Resources Loaded // hSysRes='%04X'\n", hSysRes);
315 * Fixup references.
317 wpnt = wine_files;
318 for(wpnt = wine_files; wpnt; wpnt = wpnt->next)
320 for (segment = 0; segment < wpnt->ne_header->n_segment_tab; segment++)
322 if (FixupSegment(wpnt, segment) < 0)
324 myerror("fixup failed.");
329 #ifdef WINESTAT
330 cp = strrchr(argv[0], '/');
331 if(!cp) cp = argv[0];
332 else cp++;
333 if(strcmp(cp,"winestat") == 0) {
334 winestat();
335 exit(0);
337 #endif
340 * Initialize signal handling.
342 init_wine_signals();
345 * Fixup stack and jump to start.
347 ds_reg = (wine_files->
348 selector_table[wine_files->ne_header->auto_data_seg-1].selector);
349 cs_reg = wine_files->selector_table[wine_files->ne_header->cs-1].selector;
350 ip_reg = wine_files->ne_header->ip;
351 ss_reg = wine_files->selector_table[wine_files->ne_header->ss-1].selector;
352 sp_reg = wine_files->ne_header->sp;
354 if (Options.debug) wine_debug(0, NULL);
356 rv = CallToInit16(cs_reg << 16 | ip_reg, ss_reg << 16 | sp_reg, ds_reg);
357 printf ("rv = %x\n", rv);
360 void InitializeLoadedDLLs()
362 struct w_files * wpnt;
363 int cs_reg, ds_reg, ip_reg;
364 int rv;
366 fprintf(stderr, "Initializing DLLs\n");
369 * Initialize libraries
371 wpnt = wine_files;
372 for(wpnt = wine_files; wpnt; wpnt = wpnt->next)
375 * Is this a library?
377 if (wpnt->ne_header->format_flags & 0x8000)
379 if (!(wpnt->ne_header->format_flags & 0x0001))
381 /* Not SINGLEDATA */
382 fprintf(stderr, "Library is not marked SINGLEDATA\n");
383 exit(1);
386 ds_reg = wpnt->selector_table[wpnt->
387 ne_header->auto_data_seg-1].selector;
388 cs_reg = wpnt->selector_table[wpnt->ne_header->cs-1].selector;
389 ip_reg = wpnt->ne_header->ip;
391 fprintf(stderr, "Initializing %s, cs:ip %04x:%04x, ds %04x\n",
392 wpnt->name, cs_reg, ip_reg, ds_reg);
394 rv = CallTo16(cs_reg << 16 | ip_reg, ds_reg);
395 printf ("rv = %x\n", rv);
401 /**********************************************************************
402 * GetImportedName
404 char *
405 GetImportedName(int fd, struct mz_header_s *mz_header,
406 struct ne_header_s *ne_header, int name_offset, char *buffer)
408 char *p;
409 int length;
410 int status;
411 int i;
413 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
414 name_offset, SEEK_SET);
415 length = 0;
416 read(fd, &length, 1); /* Get the length byte */
417 length = CONV_CHAR_TO_LONG (length);
418 read(fd, buffer, length);
419 buffer[length] = 0;
420 return buffer;
423 /**********************************************************************
424 * GetModuleName
426 char *
427 GetModuleName(struct w_files * wpnt, int index, char *buffer)
429 int fd = wpnt->fd;
430 struct mz_header_s *mz_header = wpnt->mz_header;
431 struct ne_header_s *ne_header = wpnt->ne_header;
432 char *p;
433 int length;
434 WORD name_offset, status;
435 int i;
437 status = lseek(fd, mz_header->ne_offset + ne_header->moduleref_tab_offset +
438 2*(index - 1), SEEK_SET);
439 name_offset = 0;
440 read(fd, &name_offset, 2);
441 name_offset = CONV_SHORT (name_offset);
442 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
443 name_offset, SEEK_SET);
444 length = 0;
445 read(fd, &length, 1); /* Get the length byte */
446 length = CONV_CHAR_TO_LONG (length);
447 read(fd, buffer, length);
448 buffer[length] = 0;
450 /* Module names are always upper case */
451 for(i=0; i<length; i++)
452 if(buffer[i] >= 'a' && buffer[i] <= 'z') buffer[i] &= ~0x20;
454 return buffer;
458 #ifndef WINELIB
459 /**********************************************************************
460 * FixupSegment
463 FixupSegment(struct w_files * wpnt, int segment_num)
465 int fd = wpnt->fd;
466 struct mz_header_s * mz_header = wpnt->mz_header;
467 struct ne_header_s *ne_header = wpnt->ne_header;
468 struct ne_segment_table_entry_s *seg_table = wpnt->seg_table;
469 struct segment_descriptor_s *selector_table = wpnt->selector_table;
470 struct relocation_entry_s *rep, *rep1;
471 struct ne_segment_table_entry_s *seg;
472 struct segment_descriptor_s *sel;
473 struct dll_table_entry_s *dll_table;
474 int status;
475 unsigned short *sp;
476 unsigned int selector, address;
477 unsigned int next_addr;
478 int ordinal;
479 char dll_name[257];
480 char func_name[257];
481 int i, n_entries;
482 int additive;
484 seg = &seg_table[segment_num];
485 sel = &selector_table[segment_num];
487 #ifdef DEBUG_FIXUP
488 printf("Segment fixups for %s, segment %d, selector %x\n",
489 wpnt->name, segment_num, (int) sel->base_addr >> 16);
490 #endif
492 if ((seg->seg_data_offset == 0) ||
493 !(seg->seg_flags & NE_SEGFLAGS_RELOC_DATA))
494 return 0;
497 * Go through the relocation table on entry at a time.
499 i = seg->seg_data_length;
500 if (i == 0)
501 i = 0x10000;
503 status = lseek(fd, seg->seg_data_offset *
504 (1 << ne_header->align_shift_count) + i, SEEK_SET);
505 n_entries = 0;
506 read(fd, &n_entries, sizeof(short int));
507 rep = (struct relocation_entry_s *)
508 malloc(n_entries * sizeof(struct relocation_entry_s));
510 if (read(fd,rep, n_entries * sizeof(struct relocation_entry_s)) !=
511 n_entries * sizeof(struct relocation_entry_s))
513 myerror("Unable to read relocation information");
516 rep1 = rep;
518 for (i = 0; i < n_entries; i++, rep++)
521 * Get the target address corresponding to this entry.
523 additive = 0;
525 switch (rep->relocation_type)
527 case NE_RELTYPE_ORDINALADD:
528 additive = 1;
530 case NE_RELTYPE_ORDINAL:
531 if (GetModuleName(wpnt, rep->target1,
532 dll_name) == NULL)
534 fprintf(stderr, "NE_RELTYPE_ORDINAL failed");
535 return -1;
538 ordinal = rep->target2;
540 status = GetEntryDLLOrdinal(dll_name, ordinal, &selector,
541 &address);
542 if (status)
544 char s[80];
546 sprintf(s, "Bad DLL name '%s.%d'", dll_name, ordinal);
547 myerror(s);
548 return -1;
551 #ifdef DEBUG_FIXUP
552 printf("%d: %s.%d: %04.4x:%04.4x\n", i + 1, dll_name, ordinal,
553 selector, address);
554 #endif
555 break;
557 case NE_RELTYPE_NAMEADD:
558 additive = 1;
560 case NE_RELTYPE_NAME:
561 if (GetModuleName(wpnt, rep->target1, dll_name)
562 == NULL)
564 fprintf(stderr,"NE_RELTYPE_NAME failed");
565 return -1;
568 if (GetImportedName(fd, mz_header, ne_header,
569 rep->target2, func_name) == NULL)
571 fprintf(stderr,"getimportedname failed");
572 return -1;
575 status = GetEntryDLLName(dll_name, func_name, &selector,
576 &address);
577 if (status)
579 char s[80];
581 sprintf(s, "Bad DLL name '%s (%s)'", dll_name,func_name);
582 myerror(s);
583 return -1;
586 #ifdef DEBUG_FIXUP
587 printf("%d: %s %s.%d: %04.4x:%04.4x\n", i + 1, func_name,
588 dll_name, ordinal, selector, address);
589 #endif
590 break;
592 case NE_RELTYPE_INTERNAL:
593 case NE_RELTYPE_INT1:
594 if (rep->target1 == 0x00ff)
596 address = GetEntryPointFromOrdinal(wpnt, rep->target2);
597 selector = (address >> 16) & 0xffff;
598 address &= 0xffff;
600 else
602 selector = selector_table[rep->target1-1].selector;
603 address = rep->target2;
606 #ifdef DEBUG_FIXUP
607 printf("%d: %04.4x:%04.4x\n", i + 1, selector, address);
608 #endif
609 break;
611 case 7:
612 /* Relocation type 7:
614 * These appear to be used as fixups for the Windows
615 * floating point emulator. Let's just ignore them and
616 * try to use the hardware floating point. Linux should
617 * successfully emulate the coprocessor if it doesn't
618 * exist.
620 #ifdef DEBUG_FIXUP
621 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
622 i + 1, rep->address_type, rep->relocation_type,
623 rep->offset);
624 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
625 #endif
626 continue;
628 default:
629 fprintf(stderr,"%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
630 i + 1, rep->address_type, rep->relocation_type,
631 rep->offset);
632 fprintf(stderr,"TARGET %04.4x %04.4x\n",
633 rep->target1, rep->target2);
634 free(rep1);
635 return -1;
639 * Stuff the right size result in.
641 sp = (unsigned short *) ((char *) sel->base_addr + rep->offset);
642 if (additive)
644 if (FindDLLTable(dll_name) == NULL)
645 additive = 2;
647 fprintf(stderr,"%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
648 i + 1, rep->address_type, rep->relocation_type,
649 rep->offset);
650 fprintf(stderr,"TARGET %04.4x %04.4x\n",
651 rep->target1, rep->target2);
652 fprintf(stderr, " Additive = %d\n", additive);
655 switch (rep->address_type)
657 case NE_RADDR_OFFSET16:
658 do {
659 #ifdef DEBUG_FIXUP
660 printf(" %04.4x:%04.4x:%04.4x OFFSET16\n",
661 (unsigned long) sp >> 16, (int) sp & 0xFFFF, *sp);
662 #endif
663 next_addr = *sp;
664 *sp = (unsigned short) address;
665 if (additive == 2)
666 *sp += next_addr;
667 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
669 while (next_addr != 0xffff && !additive);
671 break;
673 case NE_RADDR_POINTER32:
674 do {
675 #ifdef DEBUG_FIXUP
676 printf(" %04.4x:%04.4x:%04.4x POINTER32\n",
677 (unsigned long) sp >> 16, (int) sp & 0xFFFF, *sp);
678 #endif
679 next_addr = *sp;
680 *sp = (unsigned short) address;
681 if (additive == 2)
682 *sp += next_addr;
683 *(sp+1) = (unsigned short) selector;
684 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
686 while (next_addr != 0xffff && !additive);
688 break;
690 case NE_RADDR_SELECTOR:
691 do {
692 #ifdef DEBUG_FIXUP
693 printf(" %04.4x:%04.4x:%04.4x SELECTOR\n",
694 (unsigned long) sp >> 16, (int) sp & 0xFFFF, *sp);
695 #endif
696 next_addr = *sp;
697 *sp = (unsigned short) selector;
698 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
699 if (rep->relocation_type == NE_RELTYPE_INT1)
700 break;
702 while (next_addr != 0xffff && !additive);
704 break;
706 default:
707 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
708 i + 1, rep->address_type, rep->relocation_type,
709 rep->offset);
710 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
711 free(rep1);
712 return -1;
716 free(rep1);
717 return 0;
720 /**********************************************************************
721 * GetProcAddress
723 FARPROC GetProcAddress(HINSTANCE hinstance, char *proc_name)
725 if ((int) proc_name & 0xffff0000)
726 printf("GetProcAddress: %#04x, '%s'\n", hinstance, proc_name);
727 else
728 printf("GetProcAddress: %#04x, %d\n", hinstance, (int) proc_name);
730 return NULL;
732 #endif