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"
25 extern int CallToInit16(unsigned long csip, unsigned long sssp,
27 extern void CallTo32();
29 char * GetModuleName(struct w_files * wpnt, int index, char *buffer);
30 extern unsigned char ran_out;
31 unsigned short WIN_StackSize;
32 unsigned short WIN_HeapSize;
34 struct w_files * wine_files = NULL;
38 struct mz_header_s *CurrentMZHeader;
39 struct ne_header_s *CurrentNEHeader;
43 static char *dllExtensions[] = { "dll", "exe", NULL };
44 static char *exeExtensions[] = { "exe", NULL };
45 static char *WinePath = NULL;
48 /**********************************************************************
52 DebugPrintString(char *str)
58 /**********************************************************************
62 myerror(const char *s)
67 fprintf(stderr, "wine: %s\n", s);
72 /**********************************************************************
73 * GetFilenameFromInstance
76 GetFilenameFromInstance(unsigned short instance)
78 register struct w_files *w = wine_files;
80 while (w && w->hinstance != instance)
90 GetFileInfo(unsigned short instance)
92 register struct w_files *w = wine_files;
94 while (w && w->hinstance != instance)
100 /**********************************************************************
102 * Load one NE format executable into memory
104 HINSTANCE LoadImage(char * filename, char * modulename)
106 unsigned int read_size;
108 struct w_files * wpnt, *wpnt1;
111 /* First allocate a spot to store the info we collect, and add it to
115 wpnt = (struct w_files *) malloc(sizeof(struct w_files));
116 if(wine_files == NULL)
120 while(wpnt1->next) wpnt1 = wpnt1->next;
126 * Open file for reading.
128 wpnt->fd = open(filename, O_RDONLY);
134 * Establish header pointers.
136 wpnt->filename = strdup(filename);
138 if(modulename) wpnt->name = strdup(modulename);
140 wpnt->mz_header = (struct mz_header_s *) malloc(sizeof(struct mz_header_s));;
141 status = lseek(wpnt->fd, 0, SEEK_SET);
142 if (read(wpnt->fd, wpnt->mz_header, sizeof(struct mz_header_s)) !=
143 sizeof(struct mz_header_s))
145 myerror("Unable to read MZ header from file");
147 if (wpnt->mz_header->must_be_0x40 != 0x40)
148 myerror("This is not a Windows program");
150 wpnt->ne_header = (struct ne_header_s *) malloc(sizeof(struct ne_header_s));
151 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset, SEEK_SET);
152 if (read(wpnt->fd, wpnt->ne_header, sizeof(struct ne_header_s))
153 != sizeof(struct ne_header_s))
155 myerror("Unable to read NE header from file");
157 if (wpnt->ne_header->header_type[0] != 'N' ||
158 wpnt->ne_header->header_type[1] != 'E')
159 myerror("This is not a Windows program");
161 if(wine_files == wpnt){
162 CurrentMZHeader = wpnt->mz_header;
163 CurrentNEHeader = wpnt->ne_header;
164 CurrentNEFile = wpnt->fd;
166 WIN_StackSize = wpnt->ne_header->stack_length;
167 WIN_HeapSize = wpnt->ne_header->local_heap_length;
171 * Create segment selectors.
173 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
174 wpnt->ne_header->segment_tab_offset,
176 read_size = wpnt->ne_header->n_segment_tab *
177 sizeof(struct ne_segment_table_entry_s);
178 wpnt->seg_table = (struct ne_segment_table_entry_s *) malloc(read_size);
179 if (read(wpnt->fd, wpnt->seg_table, read_size) != read_size)
180 myerror("Unable to read segment table header from file");
181 wpnt->selector_table = CreateSelectors(wpnt);
184 selector_table[wine_files->ne_header->auto_data_seg-1].selector;
186 /* Get the lookup table. This is used for looking up the addresses
187 of functions that are exported */
189 read_size = wpnt->ne_header->entry_tab_length;
190 wpnt->lookup_table = (char *) malloc(read_size);
191 lseek(wpnt->fd, wpnt->mz_header->ne_offset +
192 wpnt->ne_header->entry_tab_offset, SEEK_SET);
193 if (read(wpnt->fd, wpnt->lookup_table, read_size) != read_size)
194 myerror("Unable to read lookup table header from file");
196 /* Get the iname table. This is used for looking up the names
197 of functions that are exported */
199 status = lseek(wpnt->fd, wpnt->ne_header->nrname_tab_offset, SEEK_SET);
200 read_size = wpnt->ne_header->nrname_tab_length;
201 wpnt->nrname_table = (char *) malloc(read_size);
202 if (read(wpnt->fd, wpnt->nrname_table, read_size) != read_size)
203 myerror("Unable to read nrname table header from file");
205 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
206 wpnt->ne_header->rname_tab_offset, SEEK_SET);
207 read_size = wpnt->ne_header->moduleref_tab_offset -
208 wpnt->ne_header->rname_tab_offset;
209 wpnt->rname_table = (char *) malloc(read_size);
210 if (read(wpnt->fd, wpnt->rname_table, read_size) != read_size)
211 myerror("Unable to read rname table header from file");
213 /* Now get the module name */
215 wpnt->name = (char*) malloc(*wpnt->rname_table + 1);
216 memcpy(wpnt->name, wpnt->rname_table+1, *wpnt->rname_table);
217 wpnt->name[*wpnt->rname_table] = 0;
220 * Now load any DLLs that this module refers to.
222 for(i=0; i<wpnt->ne_header->n_mod_ref_tab; i++){
226 GetModuleName(wpnt, i + 1, buff);
228 if(FindDLLTable(buff)) continue; /* This module already loaded */
230 if (FindFileInPath(buff2, sizeof(buff2),
231 buff, dllExtensions, WinePath) != NULL &&
232 (fd = open(buff2, O_RDONLY)) >= 0)
235 LoadImage(buff2, buff);
239 fprintf(stderr,"Unable to load:%s\n", buff);
241 return(wpnt->hinstance);
245 /**********************************************************************
248 _WinMain(int argc, char **argv)
256 struct w_files * wpnt;
257 int cs_reg, ds_reg, ss_reg, ip_reg, sp_reg;
266 fprintf(stderr, "usage: %s FILENAME\n", argv[0]);
270 p = getenv("WINEPATH");
271 WinePath = malloc(256 + strlen(p));
272 getcwd(WinePath, 256);
273 strcat(WinePath, ";");
276 if (FindFileInPath(exe_path, 256, argv[1], exeExtensions, WinePath)
279 fprintf(stderr, "Could not find file '%s'\n", argv[1]);
283 LoadImage(exe_path, NULL);
284 hSysRes = LoadImage("sysres.dll", NULL);
288 GetEntryDLLName("USER", "INITAPP", 0, 0);
289 for(i=0; i<1024; i++) {
291 j = GetEntryPointFromOrdinal(wine_files, i);
293 fprintf(stderr," %d %x\n", i, j);
300 for(wpnt = wine_files; wpnt; wpnt = wpnt->next)
301 for (segment = 0; segment < wpnt->ne_header->n_segment_tab; segment++)
303 if (FixupSegment(wpnt, segment) < 0)
305 myerror("fixup failed.");
310 * Fixup stack and jump to start.
312 ds_reg = wine_files->selector_table[wine_files->ne_header->auto_data_seg-1].selector;
313 cs_reg = wine_files->selector_table[wine_files->ne_header->cs-1].selector;
314 ip_reg = wine_files->ne_header->ip;
315 ss_reg = wine_files->selector_table[wine_files->ne_header->ss-1].selector;
316 sp_reg = wine_files->ne_header->sp;
319 cp = strrchr(argv[0], '/');
320 if(!cp) cp = argv[0];
322 if(strcmp(cp,"winestat") == 0) {
330 rv = CallToInit16(cs_reg << 16 | ip_reg, ss_reg << 16 | sp_reg, ds_reg);
331 printf ("rv = %x\n", rv);
335 /**********************************************************************
339 GetImportedName(int fd, struct mz_header_s *mz_header,
340 struct ne_header_s *ne_header, int name_offset, char *buffer)
347 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
348 name_offset, SEEK_SET);
350 read(fd, &length, 1); /* Get the length byte */
351 read(fd, buffer, length);
356 /**********************************************************************
360 GetModuleName(struct w_files * wpnt, int index, char *buffer)
363 struct mz_header_s *mz_header = wpnt->mz_header;
364 struct ne_header_s *ne_header = wpnt->ne_header;
367 int name_offset, status;
370 status = lseek(fd, mz_header->ne_offset + ne_header->moduleref_tab_offset +
371 2*(index - 1), SEEK_SET);
373 read(fd, &name_offset, 2);
374 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
375 name_offset, SEEK_SET);
377 read(fd, &length, 1); /* Get the length byte */
378 read(fd, buffer, length);
381 /* Module names are always upper case */
382 for(i=0; i<length; i++)
383 if(buffer[i] >= 'a' && buffer[i] <= 'z') buffer[i] &= ~0x20;
389 /**********************************************************************
393 FixupSegment(struct w_files * wpnt, int segment_num)
396 struct mz_header_s * mz_header = wpnt->mz_header;
397 struct ne_header_s *ne_header = wpnt->ne_header;
398 struct ne_segment_table_entry_s *seg_table = wpnt->seg_table;
399 struct segment_descriptor_s *selector_table = wpnt->selector_table;
401 struct relocation_entry_s *rep, *rep1;
402 struct ne_segment_table_entry_s *seg;
403 struct segment_descriptor_s *sel;
404 struct dll_table_entry_s *dll_table;
407 unsigned int selector, address;
408 unsigned int next_addr;
414 seg = &seg_table[segment_num];
415 sel = &selector_table[segment_num];
417 if ((seg->seg_data_offset == 0) ||
418 !(seg->seg_flags & NE_SEGFLAGS_RELOC_DATA))
422 * Go through the relocation table on entry at a time.
424 i = seg->seg_data_length;
428 status = lseek(fd, seg->seg_data_offset *
429 (1 << ne_header->align_shift_count) + i, SEEK_SET);
431 read(fd, &n_entries, sizeof(short int));
432 rep = (struct relocation_entry_s *)
433 malloc(n_entries * sizeof(struct relocation_entry_s));
435 if (read(fd,rep, n_entries * sizeof(struct relocation_entry_s)) !=
436 n_entries * sizeof(struct relocation_entry_s))
438 myerror("Unable to read relocation information");
443 for (i = 0; i < n_entries; i++, rep++)
446 * Get the target address corresponding to this entry.
448 switch (rep->relocation_type)
450 case NE_RELTYPE_ORDINAL:
451 if (GetModuleName(wpnt, rep->target1,
454 fprintf(stderr, "NE_RELTYPE_ORDINAL failed");
458 ordinal = rep->target2;
460 status = GetEntryDLLOrdinal(dll_name, ordinal, &selector,
466 sprintf(s, "Bad DLL name '%s.%d'", dll_name, ordinal);
472 printf("%d: %s.%d: %04.4x:%04.4x\n", i + 1, dll_name, ordinal,
477 case NE_RELTYPE_NAME:
478 if (GetModuleName(wpnt, rep->target1, dll_name)
481 fprintf(stderr,"NE_RELTYPE_NAME failed");
485 if (GetImportedName(fd, mz_header, ne_header,
486 rep->target2, func_name) == NULL)
488 fprintf(stderr,"getimportedname failed");
492 status = GetEntryDLLName(dll_name, func_name, &selector,
498 sprintf(s, "Bad DLL name '%s (%s)'", dll_name,func_name);
504 printf("%d: %s %s.%d: %04.4x:%04.4x\n", i + 1, func_name,
505 dll_name, ordinal, selector, address);
509 case NE_RELTYPE_INTERNAL:
510 case NE_RELTYPE_INT1:
511 if (rep->target1 == 0x00ff)
513 address = GetEntryPointFromOrdinal(wpnt, rep->target2);
514 selector = (address >> 16) & 0xffff;
519 selector = selector_table[rep->target1-1].selector;
520 address = rep->target2;
524 printf("%d: %04.4x:%04.4x\n", i + 1, selector, address);
529 /* Relocation type 7:
531 * These appear to be used as fixups for the Windows
532 * floating point emulator. Let's just ignore them and
533 * try to use the hardware floating point. Linux should
534 * successfully emulate the coprocessor if it doesn't
538 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
539 i + 1, rep->address_type, rep->relocation_type,
541 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
547 fprintf(stderr,"%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
548 i + 1, rep->address_type, rep->relocation_type,
550 fprintf(stderr,"TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
558 * Stuff the right size result in.
560 sp = (unsigned short *) ((char *) sel->base_addr + rep->offset);
561 switch (rep->address_type)
563 case NE_RADDR_OFFSET16:
566 *sp = (unsigned short) address;
567 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
569 while (next_addr != 0xffff);
573 case NE_RADDR_POINTER32:
576 *sp = (unsigned short) address;
577 *(sp+1) = (unsigned short) selector;
578 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
580 while (next_addr != 0xffff);
584 case NE_RADDR_SELECTOR:
587 *sp = (unsigned short) selector;
588 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
589 if (rep->relocation_type == NE_RELTYPE_INT1) break;
592 while (next_addr != 0xffff);
598 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
599 i + 1, rep->address_type, rep->relocation_type,
601 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
612 /**********************************************************************
615 FARPROC GetProcAddress(HINSTANCE hinstance, char *proc_name)
617 if ((int) proc_name & 0xffff0000)
618 printf("GetProcAddress: %#04x, '%s'\n", hinstance, proc_name);
620 printf("GetProcAddress: %#04x, %d\n", hinstance, (int) proc_name);