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"
26 /* #define DEBUG_FIXUP */
28 extern int CallToInit16(unsigned long csip, unsigned long sssp,
30 extern void CallTo32();
32 char * GetModuleName(struct w_files * wpnt, int index, char *buffer);
33 extern unsigned char ran_out;
34 extern char WindowsPath[256];
35 unsigned short WIN_StackSize;
36 unsigned short WIN_HeapSize;
38 struct w_files * wine_files = NULL;
40 int WineForceFail = 0;
44 struct mz_header_s *CurrentMZHeader;
45 struct ne_header_s *CurrentNEHeader;
49 static char *Extensions[] = { "dll", "exe", NULL };
50 static char *WinePath = NULL;
54 /**********************************************************************
58 DebugPrintString(char *str)
64 /**********************************************************************
68 myerror(const char *s)
73 fprintf(stderr, "wine: %s\n", s);
78 /**********************************************************************
79 * GetFilenameFromInstance
82 GetFilenameFromInstance(unsigned short instance)
84 register struct w_files *w = wine_files;
86 while (w && w->hinstance != instance)
96 GetFileInfo(unsigned short instance)
98 register struct w_files *w = wine_files;
100 while (w && w->hinstance != instance)
106 /**********************************************************************
108 * Load one NE format executable into memory
110 HINSTANCE LoadImage(char *modulename)
112 unsigned int read_size;
114 struct w_files * wpnt, *wpnt1;
121 if (FindFile(buffer, sizeof(buffer), modulename, Extensions, WindowsPath)
126 fprintf(stderr,"LoadImage: I can't find %s !\n",modulename);
127 return (HINSTANCE) NULL;
129 fprintf(stderr,"LoadImage: loading %s (%s)\n", modulename, buffer);
131 /* First allocate a spot to store the info we collect, and add it to
135 wpnt = (struct w_files *) malloc(sizeof(struct w_files));
136 if(wine_files == NULL)
140 while(wpnt1->next) wpnt1 = wpnt1->next;
146 * Open file for reading.
148 wpnt->fd = open(buffer, O_RDONLY);
154 * Establish header pointers.
156 wpnt->filename = strdup(buffer);
158 if(modulename) wpnt->name = strdup(modulename);
160 wpnt->mz_header = (struct mz_header_s *) malloc(sizeof(struct mz_header_s));;
161 status = lseek(wpnt->fd, 0, SEEK_SET);
162 if (read(wpnt->fd, wpnt->mz_header, sizeof(struct mz_header_s)) !=
163 sizeof(struct mz_header_s))
165 myerror("Unable to read MZ header from file");
167 if (wpnt->mz_header->must_be_0x40 != 0x40)
168 myerror("This is not a Windows program");
170 wpnt->ne_header = (struct ne_header_s *) malloc(sizeof(struct ne_header_s));
171 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset, SEEK_SET);
172 if (read(wpnt->fd, wpnt->ne_header, sizeof(struct ne_header_s))
173 != sizeof(struct ne_header_s))
175 myerror("Unable to read NE header from file");
177 if (wpnt->ne_header->header_type[0] != 'N' ||
178 wpnt->ne_header->header_type[1] != 'E')
179 myerror("This is not a Windows program");
181 if(wine_files == wpnt){
182 CurrentMZHeader = wpnt->mz_header;
183 CurrentNEHeader = wpnt->ne_header;
184 CurrentNEFile = wpnt->fd;
186 WIN_StackSize = wpnt->ne_header->stack_length;
187 WIN_HeapSize = wpnt->ne_header->local_heap_length;
191 * Create segment selectors.
193 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
194 wpnt->ne_header->segment_tab_offset,
196 read_size = wpnt->ne_header->n_segment_tab *
197 sizeof(struct ne_segment_table_entry_s);
198 wpnt->seg_table = (struct ne_segment_table_entry_s *) malloc(read_size);
199 if (read(wpnt->fd, wpnt->seg_table, read_size) != read_size)
200 myerror("Unable to read segment table header from file");
201 wpnt->selector_table = CreateSelectors(wpnt);
204 selector_table[wpnt->ne_header->auto_data_seg-1].selector;
206 /* Get the lookup table. This is used for looking up the addresses
207 of functions that are exported */
209 read_size = wpnt->ne_header->entry_tab_length;
210 wpnt->lookup_table = (char *) malloc(read_size);
211 lseek(wpnt->fd, wpnt->mz_header->ne_offset +
212 wpnt->ne_header->entry_tab_offset, SEEK_SET);
213 if (read(wpnt->fd, wpnt->lookup_table, read_size) != read_size)
214 myerror("Unable to read lookup table header from file");
216 /* Get the iname table. This is used for looking up the names
217 of functions that are exported */
219 status = lseek(wpnt->fd, wpnt->ne_header->nrname_tab_offset, SEEK_SET);
220 read_size = wpnt->ne_header->nrname_tab_length;
221 wpnt->nrname_table = (char *) malloc(read_size);
222 if (read(wpnt->fd, wpnt->nrname_table, read_size) != read_size)
223 myerror("Unable to read nrname table header from file");
225 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
226 wpnt->ne_header->rname_tab_offset, SEEK_SET);
227 read_size = wpnt->ne_header->moduleref_tab_offset -
228 wpnt->ne_header->rname_tab_offset;
229 wpnt->rname_table = (char *) malloc(read_size);
230 if (read(wpnt->fd, wpnt->rname_table, read_size) != read_size)
231 myerror("Unable to read rname table header from file");
233 /* Now get the module name */
235 wpnt->name = (char*) malloc(*wpnt->rname_table + 1);
236 memcpy(wpnt->name, wpnt->rname_table+1, *wpnt->rname_table);
237 wpnt->name[*wpnt->rname_table] = 0;
240 * Now load any DLLs that this module refers to.
242 for(i=0; i<wpnt->ne_header->n_mod_ref_tab; i++){
246 GetModuleName(wpnt, i + 1, buff);
248 if(FindDLLTable(buff)) continue; /* This module already loaded */
252 fprintf(stderr,"Unable to load:%s\n", buff);
255 return(wpnt->hinstance);
258 /**********************************************************************
262 ParseArgs(int argc, char **argv)
266 fprintf(stderr, "usage: %s [-spy FILENAME] FILENAME\n", argv[0]);
272 for (Argv = argv + 1; **Argv == '-' && Argc > 0; Argv++)
274 if (strcmp(*Argv, "-spy") == 0)
276 if (strcmp(*(++Argv), "-") == 0)
279 SpyFp = fopen(*Argv, "a");
284 /**********************************************************************
287 _WinMain(int argc, char **argv)
298 struct w_files * wpnt;
299 int cs_reg, ds_reg, ss_reg, ip_reg, sp_reg;
303 ParseArgs(argc, argv);
305 p = getenv("WINEPATH");
306 WinePath = malloc(256 + strlen(p));
307 getcwd(WinePath, 256);
308 strcat(WinePath, ";");
313 GetPrivateProfileString("wine", "SystemResources", "sysres.dll",
314 filename, sizeof(filename),
316 hSysRes = LoadImage(filename);
317 if (hSysRes == (HINSTANCE)NULL)
318 printf("Error Loading System Resources !!!\n");
320 printf("System Resources Loaded // hSysRes='%04X'\n", hSysRes);
326 for(wpnt = wine_files; wpnt; wpnt = wpnt->next)
327 for (segment = 0; segment < wpnt->ne_header->n_segment_tab; segment++)
329 if (FixupSegment(wpnt, segment) < 0)
331 myerror("fixup failed.");
336 * Fixup stack and jump to start.
338 ds_reg = wine_files->selector_table[wine_files->ne_header->auto_data_seg-1].selector;
339 cs_reg = wine_files->selector_table[wine_files->ne_header->cs-1].selector;
340 ip_reg = wine_files->ne_header->ip;
341 ss_reg = wine_files->selector_table[wine_files->ne_header->ss-1].selector;
342 sp_reg = wine_files->ne_header->sp;
345 cp = strrchr(argv[0], '/');
346 if(!cp) cp = argv[0];
348 if(strcmp(cp,"winestat") == 0) {
358 p = (char *) ((cs_reg << 16) | ip_reg);
366 fprintf(stderr, "SS is 0\n");
371 rv = CallToInit16(cs_reg << 16 | ip_reg, ss_reg << 16 | sp_reg, ds_reg);
372 printf ("rv = %x\n", rv);
376 /**********************************************************************
380 GetImportedName(int fd, struct mz_header_s *mz_header,
381 struct ne_header_s *ne_header, int name_offset, char *buffer)
388 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
389 name_offset, SEEK_SET);
391 read(fd, &length, 1); /* Get the length byte */
392 read(fd, buffer, length);
397 /**********************************************************************
401 GetModuleName(struct w_files * wpnt, int index, char *buffer)
404 struct mz_header_s *mz_header = wpnt->mz_header;
405 struct ne_header_s *ne_header = wpnt->ne_header;
408 int name_offset, status;
411 status = lseek(fd, mz_header->ne_offset + ne_header->moduleref_tab_offset +
412 2*(index - 1), SEEK_SET);
414 read(fd, &name_offset, 2);
415 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
416 name_offset, SEEK_SET);
418 read(fd, &length, 1); /* Get the length byte */
419 read(fd, buffer, length);
422 /* Module names are always upper case */
423 for(i=0; i<length; i++)
424 if(buffer[i] >= 'a' && buffer[i] <= 'z') buffer[i] &= ~0x20;
430 /**********************************************************************
434 FixupSegment(struct w_files * wpnt, int segment_num)
437 struct mz_header_s * mz_header = wpnt->mz_header;
438 struct ne_header_s *ne_header = wpnt->ne_header;
439 struct ne_segment_table_entry_s *seg_table = wpnt->seg_table;
440 struct segment_descriptor_s *selector_table = wpnt->selector_table;
441 struct relocation_entry_s *rep, *rep1;
442 struct ne_segment_table_entry_s *seg;
443 struct segment_descriptor_s *sel;
444 struct dll_table_entry_s *dll_table;
447 unsigned int selector, address;
448 unsigned int next_addr;
455 seg = &seg_table[segment_num];
456 sel = &selector_table[segment_num];
459 printf("Segment fixups for %s, segment %d, selector %x\n",
460 wpnt->name, segment_num, (int) sel->base_addr >> 16);
463 if ((seg->seg_data_offset == 0) ||
464 !(seg->seg_flags & NE_SEGFLAGS_RELOC_DATA))
468 * Go through the relocation table on entry at a time.
470 i = seg->seg_data_length;
474 status = lseek(fd, seg->seg_data_offset *
475 (1 << ne_header->align_shift_count) + i, SEEK_SET);
477 read(fd, &n_entries, sizeof(short int));
478 rep = (struct relocation_entry_s *)
479 malloc(n_entries * sizeof(struct relocation_entry_s));
481 if (read(fd,rep, n_entries * sizeof(struct relocation_entry_s)) !=
482 n_entries * sizeof(struct relocation_entry_s))
484 myerror("Unable to read relocation information");
489 for (i = 0; i < n_entries; i++, rep++)
492 * Get the target address corresponding to this entry.
496 switch (rep->relocation_type)
498 case NE_RELTYPE_ORDINALADD:
501 case NE_RELTYPE_ORDINAL:
502 if (GetModuleName(wpnt, rep->target1,
505 fprintf(stderr, "NE_RELTYPE_ORDINAL failed");
509 ordinal = rep->target2;
511 status = GetEntryDLLOrdinal(dll_name, ordinal, &selector,
517 sprintf(s, "Bad DLL name '%s.%d'", dll_name, ordinal);
523 printf("%d: %s.%d: %04.4x:%04.4x\n", i + 1, dll_name, ordinal,
528 case NE_RELTYPE_NAMEADD:
531 case NE_RELTYPE_NAME:
532 if (GetModuleName(wpnt, rep->target1, dll_name)
535 fprintf(stderr,"NE_RELTYPE_NAME failed");
539 if (GetImportedName(fd, mz_header, ne_header,
540 rep->target2, func_name) == NULL)
542 fprintf(stderr,"getimportedname failed");
546 status = GetEntryDLLName(dll_name, func_name, &selector,
552 sprintf(s, "Bad DLL name '%s (%s)'", dll_name,func_name);
558 printf("%d: %s %s.%d: %04.4x:%04.4x\n", i + 1, func_name,
559 dll_name, ordinal, selector, address);
563 case NE_RELTYPE_INTERNAL:
564 case NE_RELTYPE_INT1:
565 if (rep->target1 == 0x00ff)
567 address = GetEntryPointFromOrdinal(wpnt, rep->target2);
568 selector = (address >> 16) & 0xffff;
573 selector = selector_table[rep->target1-1].selector;
574 address = rep->target2;
578 printf("%d: %04.4x:%04.4x\n", i + 1, selector, address);
583 /* Relocation type 7:
585 * These appear to be used as fixups for the Windows
586 * floating point emulator. Let's just ignore them and
587 * try to use the hardware floating point. Linux should
588 * successfully emulate the coprocessor if it doesn't
592 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
593 i + 1, rep->address_type, rep->relocation_type,
595 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
600 fprintf(stderr,"%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
601 i + 1, rep->address_type, rep->relocation_type,
603 fprintf(stderr,"TARGET %04.4x %04.4x\n",
604 rep->target1, rep->target2);
610 * Stuff the right size result in.
612 sp = (unsigned short *) ((char *) sel->base_addr + rep->offset);
615 if (FindDLLTable(dll_name) == NULL)
618 fprintf(stderr,"%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
619 i + 1, rep->address_type, rep->relocation_type,
621 fprintf(stderr,"TARGET %04.4x %04.4x\n",
622 rep->target1, rep->target2);
623 fprintf(stderr, " Additive = %d\n", additive);
626 switch (rep->address_type)
628 case NE_RADDR_OFFSET16:
631 printf(" %04.4x:%04.4x:%04.4x OFFSET16\n",
632 (unsigned long) sp >> 16, (int) sp & 0xFFFF, *sp);
635 *sp = (unsigned short) address;
638 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
640 while (next_addr != 0xffff && !additive);
644 case NE_RADDR_POINTER32:
647 printf(" %04.4x:%04.4x:%04.4x POINTER32\n",
648 (unsigned long) sp >> 16, (int) sp & 0xFFFF, *sp);
651 *sp = (unsigned short) address;
654 *(sp+1) = (unsigned short) selector;
655 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
657 while (next_addr != 0xffff && !additive);
661 case NE_RADDR_SELECTOR:
664 printf(" %04.4x:%04.4x:%04.4x SELECTOR\n",
665 (unsigned long) sp >> 16, (int) sp & 0xFFFF, *sp);
668 *sp = (unsigned short) selector;
669 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
670 if (rep->relocation_type == NE_RELTYPE_INT1)
673 while (next_addr != 0xffff && !additive);
678 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
679 i + 1, rep->address_type, rep->relocation_type,
681 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
691 /**********************************************************************
694 FARPROC GetProcAddress(HINSTANCE hinstance, char *proc_name)
696 if ((int) proc_name & 0xffff0000)
697 printf("GetProcAddress: %#04x, '%s'\n", hinstance, proc_name);
699 printf("GetProcAddress: %#04x, %d\n", hinstance, (int) proc_name);