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";
11 #include <linux/unistd.h>
12 #include <linux/head.h>
13 #include <linux/ldt.h>
14 #include <linux/segment.h>
20 #include "prototypes.h"
28 /* #define DEBUG_FIXUP */
30 extern int CallToInit16(unsigned long csip, unsigned long sssp,
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 char *WIN_ProgramName;
39 unsigned short WIN_StackSize;
40 unsigned short WIN_HeapSize;
42 struct w_files * wine_files = NULL;
44 int WineForceFail = 0;
48 struct mz_header_s *CurrentMZHeader;
49 struct ne_header_s *CurrentNEHeader;
53 static char *DLL_Extensions[] = { "dll", "exe", NULL };
54 static char *EXE_Extensions[] = { "exe", NULL };
55 static char *WinePath = NULL;
59 /**********************************************************************
63 myerror(const char *s)
68 fprintf(stderr, "wine: %s\n", s);
73 /**********************************************************************
74 * GetFilenameFromInstance
77 GetFilenameFromInstance(unsigned short instance)
79 register struct w_files *w = wine_files;
81 while (w && w->hinstance != instance)
91 GetFileInfo(unsigned short instance)
93 register struct w_files *w = wine_files;
95 while (w && w->hinstance != instance)
102 /**********************************************************************
106 void load_mz_header(int fd, struct mz_header_s *mz_header)
108 if (read(fd, mz_header, sizeof(struct mz_header_s)) !=
109 sizeof(struct mz_header_s))
111 myerror("Unable to read MZ header from file");
115 void load_ne_header (int fd, struct ne_header_s *ne_header)
117 if (read(fd, ne_header, sizeof(struct ne_header_s))
118 != sizeof(struct ne_header_s))
120 myerror("Unable to read NE header from file");
125 /**********************************************************************
127 * Load one NE format executable into memory
129 HINSTANCE LoadImage(char *modulename, int filetype, int change_dir)
131 unsigned int read_size;
133 struct w_files * wpnt, *wpnt1;
141 fullname = FindFile(buffer, sizeof(buffer), modulename,
142 (filetype == EXE ? EXE_Extensions : DLL_Extensions),
144 if (fullname == NULL)
146 fprintf(stderr, "LoadImage: I can't find %s.dll | %s.exe !\n",
147 modulename, modulename);
148 return (HINSTANCE) NULL;
151 fullname = GetDosFileName(fullname);
152 WIN_ProgramName = strdup(fullname);
154 fprintf(stderr,"LoadImage: loading %s (%s)\n [%s]\n",
155 modulename, buffer, WIN_ProgramName);
157 if (change_dir && fullname)
162 strcpy(dirname, fullname);
163 p = strrchr(dirname, '\\');
166 DOS_SetDefaultDrive(dirname[0] - 'A');
167 DOS_ChangeDir(dirname[0] - 'A', dirname + 2);
170 /* First allocate a spot to store the info we collect, and add it to
174 wpnt = (struct w_files *) malloc(sizeof(struct w_files));
175 if(wine_files == NULL)
179 while(wpnt1->next) wpnt1 = wpnt1->next;
185 * Open file for reading.
187 wpnt->fd = open(buffer, O_RDONLY);
193 * Establish header pointers.
195 wpnt->filename = strdup(buffer);
197 if(modulename) wpnt->name = strdup(modulename);
199 wpnt->mz_header = (struct mz_header_s *) malloc(sizeof(struct mz_header_s));;
200 status = lseek(wpnt->fd, 0, SEEK_SET);
201 load_mz_header (wpnt->fd, wpnt->mz_header);
202 if (wpnt->mz_header->must_be_0x40 != 0x40)
203 myerror("This is not a Windows program");
205 wpnt->ne_header = (struct ne_header_s *) malloc(sizeof(struct ne_header_s));
206 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset, SEEK_SET);
207 load_ne_header (wpnt->fd, wpnt->ne_header);
208 if (wpnt->ne_header->header_type[0] != 'N' ||
209 wpnt->ne_header->header_type[1] != 'E')
210 myerror("This is not a Windows program");
212 if(wine_files == wpnt){
213 CurrentMZHeader = wpnt->mz_header;
214 CurrentNEHeader = wpnt->ne_header;
215 CurrentNEFile = wpnt->fd;
217 WIN_StackSize = wpnt->ne_header->stack_length;
218 WIN_HeapSize = wpnt->ne_header->local_heap_length;
222 * Create segment selectors.
225 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
226 wpnt->ne_header->segment_tab_offset,
228 read_size = wpnt->ne_header->n_segment_tab *
229 sizeof(struct ne_segment_table_entry_s);
230 wpnt->seg_table = (struct ne_segment_table_entry_s *) malloc(read_size);
231 if (read(wpnt->fd, wpnt->seg_table, read_size) != read_size)
232 myerror("Unable to read segment table header from file");
233 wpnt->selector_table = CreateSelectors(wpnt);
236 selector_table[wpnt->ne_header->auto_data_seg-1].selector;
238 /* Get the lookup table. This is used for looking up the addresses
239 of functions that are exported */
241 read_size = wpnt->ne_header->entry_tab_length;
242 wpnt->lookup_table = (char *) malloc(read_size);
243 lseek(wpnt->fd, wpnt->mz_header->ne_offset +
244 wpnt->ne_header->entry_tab_offset, SEEK_SET);
245 if (read(wpnt->fd, wpnt->lookup_table, read_size) != read_size)
246 myerror("Unable to read lookup table header from file");
248 /* Get the iname table. This is used for looking up the names
249 of functions that are exported */
251 status = lseek(wpnt->fd, wpnt->ne_header->nrname_tab_offset, SEEK_SET);
252 read_size = wpnt->ne_header->nrname_tab_length;
253 wpnt->nrname_table = (char *) malloc(read_size);
254 if (read(wpnt->fd, wpnt->nrname_table, read_size) != read_size)
255 myerror("Unable to read nrname table header from file");
257 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
258 wpnt->ne_header->rname_tab_offset, SEEK_SET);
259 read_size = wpnt->ne_header->moduleref_tab_offset -
260 wpnt->ne_header->rname_tab_offset;
261 wpnt->rname_table = (char *) malloc(read_size);
262 if (read(wpnt->fd, wpnt->rname_table, read_size) != read_size)
263 myerror("Unable to read rname table header from file");
265 /* Now get the module name */
267 wpnt->name = (char*) malloc(*wpnt->rname_table + 1);
268 memcpy(wpnt->name, wpnt->rname_table+1, *wpnt->rname_table);
269 wpnt->name[*wpnt->rname_table] = 0;
272 * Now load any DLLs that this module refers to.
274 for(i=0; i<wpnt->ne_header->n_mod_ref_tab; i++){
278 GetModuleName(wpnt, i + 1, buff);
281 if(FindDLLTable(buff)) continue; /* This module already loaded */
284 LoadImage(buff, DLL, 0);
286 fprintf(stderr,"Unable to load:%s\n", buff);
289 return(wpnt->hinstance);
293 /**********************************************************************
296 _WinMain(int argc, char **argv)
307 struct w_files * wpnt;
308 int cs_reg, ds_reg, ss_reg, ip_reg, sp_reg;
315 WinePath = malloc(1024);
317 getcwd(WinePath, 512);
319 if ((p = getenv("WINEPATH")) != NULL) {
320 strcat(WinePath, ";");
324 if ((hInstMain = LoadImage(Argv[0], EXE, 1)) == (HINSTANCE) NULL ) {
325 fprintf(stderr, "wine: can't find %s!.\n", Argv[0]);
329 GetPrivateProfileString("wine", "SystemResources", "sysres.dll",
330 filename, sizeof(filename), WINE_INI);
332 hSysRes = LoadImage(filename, DLL);
333 if (hSysRes == (HINSTANCE)NULL)
334 fprintf(stderr, "wine: can't find %s!\n", filename);
336 printf("System Resources Loaded // hSysRes='%04X'\n", hSysRes);
342 for(wpnt = wine_files; wpnt; wpnt = wpnt->next)
344 for (segment = 0; segment < wpnt->ne_header->n_segment_tab; segment++)
346 if (FixupSegment(wpnt, segment) < 0)
348 myerror("fixup failed.");
354 cp = strrchr(argv[0], '/');
355 if(!cp) cp = argv[0];
357 if(strcmp(cp,"winestat") == 0) {
364 * Initialize signal handling.
369 * Fixup stack and jump to start.
371 ds_reg = (wine_files->
372 selector_table[wine_files->ne_header->auto_data_seg-1].selector);
373 cs_reg = wine_files->selector_table[wine_files->ne_header->cs-1].selector;
374 ip_reg = wine_files->ne_header->ip;
375 ss_reg = wine_files->selector_table[wine_files->ne_header->ss-1].selector;
376 sp_reg = wine_files->ne_header->sp;
378 if (Options.debug) wine_debug(0, NULL);
380 rv = CallToInit16(cs_reg << 16 | ip_reg, ss_reg << 16 | sp_reg, ds_reg);
381 printf ("rv = %x\n", rv);
384 void InitializeLoadedDLLs()
386 struct w_files * wpnt;
387 int cs_reg, ds_reg, ip_reg;
390 fprintf(stderr, "Initializing DLLs\n");
393 * Initialize libraries
396 for(wpnt = wine_files; wpnt; wpnt = wpnt->next)
401 if (wpnt->ne_header->format_flags & 0x8000)
403 if (!(wpnt->ne_header->format_flags & 0x0001))
406 fprintf(stderr, "Library is not marked SINGLEDATA\n");
410 ds_reg = wpnt->selector_table[wpnt->
411 ne_header->auto_data_seg-1].selector;
412 cs_reg = wpnt->selector_table[wpnt->ne_header->cs-1].selector;
413 ip_reg = wpnt->ne_header->ip;
415 fprintf(stderr, "Initializing %s, cs:ip %04x:%04x, ds %04x\n",
416 wpnt->name, cs_reg, ip_reg, ds_reg);
418 rv = CallTo16(cs_reg << 16 | ip_reg, ds_reg);
419 printf ("rv = %x\n", rv);
425 /**********************************************************************
429 GetImportedName(int fd, struct mz_header_s *mz_header,
430 struct ne_header_s *ne_header, int name_offset, char *buffer)
437 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
438 name_offset, SEEK_SET);
440 read(fd, &length, 1); /* Get the length byte */
441 length = CONV_CHAR_TO_LONG (length);
442 read(fd, buffer, length);
447 /**********************************************************************
451 GetModuleName(struct w_files * wpnt, int index, char *buffer)
454 struct mz_header_s *mz_header = wpnt->mz_header;
455 struct ne_header_s *ne_header = wpnt->ne_header;
458 WORD name_offset, status;
461 status = lseek(fd, mz_header->ne_offset + ne_header->moduleref_tab_offset +
462 2*(index - 1), SEEK_SET);
464 read(fd, &name_offset, 2);
465 name_offset = CONV_SHORT (name_offset);
466 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
467 name_offset, SEEK_SET);
469 read(fd, &length, 1); /* Get the length byte */
470 length = CONV_CHAR_TO_LONG (length);
471 read(fd, buffer, length);
474 /* Module names are always upper case */
475 for(i=0; i<length; i++)
476 if(buffer[i] >= 'a' && buffer[i] <= 'z') buffer[i] &= ~0x20;
483 /**********************************************************************
487 FixupSegment(struct w_files * wpnt, int segment_num)
490 struct mz_header_s * mz_header = wpnt->mz_header;
491 struct ne_header_s *ne_header = wpnt->ne_header;
492 struct ne_segment_table_entry_s *seg_table = wpnt->seg_table;
493 struct segment_descriptor_s *selector_table = wpnt->selector_table;
494 struct relocation_entry_s *rep, *rep1;
495 struct ne_segment_table_entry_s *seg;
496 struct segment_descriptor_s *sel;
497 struct dll_table_entry_s *dll_table;
500 unsigned int selector, address;
501 unsigned int next_addr;
508 seg = &seg_table[segment_num];
509 sel = &selector_table[segment_num];
512 printf("Segment fixups for %s, segment %d, selector %x\n",
513 wpnt->name, segment_num, (int) sel->base_addr >> 16);
516 if ((seg->seg_data_offset == 0) ||
517 !(seg->seg_flags & NE_SEGFLAGS_RELOC_DATA))
521 * Go through the relocation table on entry at a time.
523 i = seg->seg_data_length;
527 status = lseek(fd, seg->seg_data_offset *
528 (1 << ne_header->align_shift_count) + i, SEEK_SET);
530 read(fd, &n_entries, sizeof(short int));
531 rep = (struct relocation_entry_s *)
532 malloc(n_entries * sizeof(struct relocation_entry_s));
534 if (read(fd,rep, n_entries * sizeof(struct relocation_entry_s)) !=
535 n_entries * sizeof(struct relocation_entry_s))
537 myerror("Unable to read relocation information");
542 for (i = 0; i < n_entries; i++, rep++)
545 * Get the target address corresponding to this entry.
549 switch (rep->relocation_type)
551 case NE_RELTYPE_ORDINALADD:
554 case NE_RELTYPE_ORDINAL:
555 if (GetModuleName(wpnt, rep->target1,
558 fprintf(stderr, "NE_RELTYPE_ORDINAL failed");
562 ordinal = rep->target2;
564 status = GetEntryDLLOrdinal(dll_name, ordinal, &selector,
570 sprintf(s, "Bad DLL name '%s.%d'", dll_name, ordinal);
576 printf("%d: %s.%d: %04.4x:%04.4x\n", i + 1, dll_name, ordinal,
581 case NE_RELTYPE_NAMEADD:
584 case NE_RELTYPE_NAME:
585 if (GetModuleName(wpnt, rep->target1, dll_name)
588 fprintf(stderr,"NE_RELTYPE_NAME failed");
592 if (GetImportedName(fd, mz_header, ne_header,
593 rep->target2, func_name) == NULL)
595 fprintf(stderr,"getimportedname failed");
599 status = GetEntryDLLName(dll_name, func_name, &selector,
605 sprintf(s, "Bad DLL name '%s (%s)'", dll_name,func_name);
611 printf("%d: %s %s.%d: %04.4x:%04.4x\n", i + 1, func_name,
612 dll_name, ordinal, selector, address);
616 case NE_RELTYPE_INTERNAL:
617 case NE_RELTYPE_INT1:
618 if (rep->target1 == 0x00ff)
620 address = GetEntryPointFromOrdinal(wpnt, rep->target2);
621 selector = (address >> 16) & 0xffff;
626 selector = selector_table[rep->target1-1].selector;
627 address = rep->target2;
631 printf("%d: %04.4x:%04.4x\n", i + 1, selector, address);
636 /* Relocation type 7:
638 * These appear to be used as fixups for the Windows
639 * floating point emulator. Let's just ignore them and
640 * try to use the hardware floating point. Linux should
641 * successfully emulate the coprocessor if it doesn't
645 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
646 i + 1, rep->address_type, rep->relocation_type,
648 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
653 fprintf(stderr,"%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
654 i + 1, rep->address_type, rep->relocation_type,
656 fprintf(stderr,"TARGET %04.4x %04.4x\n",
657 rep->target1, rep->target2);
663 * Stuff the right size result in.
665 sp = (unsigned short *) ((char *) sel->base_addr + rep->offset);
668 if (FindDLLTable(dll_name) == NULL)
671 fprintf(stderr,"%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
672 i + 1, rep->address_type, rep->relocation_type,
674 fprintf(stderr,"TARGET %04.4x %04.4x\n",
675 rep->target1, rep->target2);
676 fprintf(stderr, " Additive = %d\n", additive);
679 switch (rep->address_type)
681 case NE_RADDR_OFFSET16:
684 printf(" %04.4x:%04.4x:%04.4x OFFSET16\n",
685 (unsigned long) sp >> 16, (int) sp & 0xFFFF, *sp);
688 *sp = (unsigned short) address;
691 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
693 while (next_addr != 0xffff && !additive);
697 case NE_RADDR_POINTER32:
700 printf(" %04.4x:%04.4x:%04.4x POINTER32\n",
701 (unsigned long) sp >> 16, (int) sp & 0xFFFF, *sp);
704 *sp = (unsigned short) address;
707 *(sp+1) = (unsigned short) selector;
708 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
710 while (next_addr != 0xffff && !additive);
714 case NE_RADDR_SELECTOR:
717 printf(" %04.4x:%04.4x:%04.4x SELECTOR\n",
718 (unsigned long) sp >> 16, (int) sp & 0xFFFF, *sp);
721 *sp = (unsigned short) selector;
722 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
723 if (rep->relocation_type == NE_RELTYPE_INT1)
726 while (next_addr != 0xffff && !additive);
731 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
732 i + 1, rep->address_type, rep->relocation_type,
734 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
744 /**********************************************************************
747 FARPROC GetProcAddress(HINSTANCE hinstance, char *proc_name)
749 if ((int) proc_name & 0xffff0000)
750 printf("GetProcAddress: %#04x, '%s'\n", hinstance, proc_name);
752 printf("GetProcAddress: %#04x, %d\n", hinstance, (int) proc_name);