Release 0.2.0
[wine/multimedia.git] / wine.c
blob09d7fc7753e8271e033ad64745f77e7eecdde384
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 #include <linux/unistd.h>
11 #include <linux/head.h>
12 #include <linux/ldt.h>
13 #include <linux/segment.h>
14 #include <string.h>
15 #include <errno.h>
16 #include "neexe.h"
17 #include "segmem.h"
18 #include "prototypes.h"
19 #include "dlls.h"
20 #include "wine.h"
22 extern int CallToInit16(unsigned long csip, unsigned long sssp,
23 unsigned short ds);
24 extern void CallTo32();
26 char * GetModuleName(struct w_files * wpnt, int index, char *buffer);
27 extern unsigned char ran_out;
28 unsigned short WIN_StackSize;
29 unsigned short WIN_HeapSize;
31 struct w_files * wine_files = NULL;
33 char **Argv;
34 int Argc;
35 struct mz_header_s *CurrentMZHeader;
36 struct ne_header_s *CurrentNEHeader;
37 int CurrentNEFile;
39 /**********************************************************************
40 * DebugPrintString
42 int
43 DebugPrintString(char *str)
45 printf("%s", str);
46 return 0;
49 /**********************************************************************
50 * myerror
52 void
53 myerror(const char *s)
55 char buffer[200];
57 sprintf(buffer, "%s", Argv[0]);
58 if (s == NULL)
59 perror(buffer);
60 else
61 fprintf(stderr, "%s: %s\n", buffer, s);
63 exit(1);
67 /* Load one NE format executable into memory */
68 LoadImage(char * filename, char * modulename)
70 unsigned int read_size;
71 int i;
72 struct w_files * wpnt, *wpnt1;
73 unsigned int status;
75 /* First allocate a spot to store the info we collect, and add it to
76 * our linked list.
79 wpnt = (struct w_files *) malloc(sizeof(struct w_files));
80 if(wine_files == NULL)
81 wine_files = wpnt;
82 else {
83 wpnt1 = wine_files;
84 while(wpnt1->next) wpnt1 = wpnt1->next;
85 wpnt1->next = wpnt;
87 wpnt->next = NULL;
90 * Open file for reading.
92 wpnt->fd = open(filename, O_RDONLY);
93 if (wpnt->fd < 0)
95 myerror(NULL);
98 * Establish header pointers.
100 wpnt->filename = strdup(filename);
101 wpnt->name = NULL;
102 if(modulename) wpnt->name = strdup(modulename);
104 wpnt->mz_header = (struct mz_header_s *) malloc(sizeof(struct mz_header_s));;
105 status = lseek(wpnt->fd, 0, SEEK_SET);
106 if (read(wpnt->fd, wpnt->mz_header, sizeof(struct mz_header_s)) !=
107 sizeof(struct mz_header_s))
109 myerror("Unable to read MZ header from file");
111 if (wpnt->mz_header->must_be_0x40 != 0x40)
112 myerror("This is not a Windows program");
114 wpnt->ne_header = (struct ne_header_s *) malloc(sizeof(struct ne_header_s));
115 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset, SEEK_SET);
116 if (read(wpnt->fd, wpnt->ne_header, sizeof(struct ne_header_s))
117 != sizeof(struct ne_header_s))
119 myerror("Unable to read NE header from file");
121 if (wpnt->ne_header->header_type[0] != 'N' ||
122 wpnt->ne_header->header_type[1] != 'E')
123 myerror("This is not a Windows program");
125 if(wine_files == wpnt){
126 CurrentMZHeader = wpnt->mz_header;
127 CurrentNEHeader = wpnt->ne_header;
128 CurrentNEFile = wpnt->fd;
130 WIN_StackSize = wpnt->ne_header->stack_length;
131 WIN_HeapSize = wpnt->ne_header->local_heap_length;
135 * Create segment selectors.
137 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
138 wpnt->ne_header->segment_tab_offset,
139 SEEK_SET);
140 read_size = wpnt->ne_header->n_segment_tab *
141 sizeof(struct ne_segment_table_entry_s);
142 wpnt->seg_table = (struct ne_segment_table_entry_s *) malloc(read_size);
143 if (read(wpnt->fd, wpnt->seg_table, read_size) != read_size)
144 myerror("Unable to read segment table header from file");
145 wpnt->selector_table = CreateSelectors(wpnt);
147 /* Get the lookup table. This is used for looking up the addresses
148 of functions that are exported */
150 read_size = wpnt->ne_header->entry_tab_length;
151 wpnt->lookup_table = (char *) malloc(read_size);
152 lseek(wpnt->fd, wpnt->mz_header->ne_offset +
153 wpnt->ne_header->entry_tab_offset, SEEK_SET);
154 if (read(wpnt->fd, wpnt->lookup_table, read_size) != read_size)
155 myerror("Unable to read lookup table header from file");
157 /* Get the iname table. This is used for looking up the names
158 of functions that are exported */
160 status = lseek(wpnt->fd, wpnt->ne_header->nrname_tab_offset, SEEK_SET);
161 read_size = wpnt->ne_header->nrname_tab_length;
162 wpnt->nrname_table = (char *) malloc(read_size);
163 if (read(wpnt->fd, wpnt->nrname_table, read_size) != read_size)
164 myerror("Unable to read nrname table header from file");
166 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
167 wpnt->ne_header->rname_tab_offset, SEEK_SET);
168 read_size = wpnt->ne_header->moduleref_tab_offset -
169 wpnt->ne_header->rname_tab_offset;
170 wpnt->rname_table = (char *) malloc(read_size);
171 if (read(wpnt->fd, wpnt->rname_table, read_size) != read_size)
172 myerror("Unable to read rname table header from file");
174 /* Now get the module name */
176 wpnt->name = (char*) malloc(*wpnt->rname_table + 1);
177 memcpy(wpnt->name, wpnt->rname_table+1, *wpnt->rname_table);
178 wpnt->name[*wpnt->rname_table] = 0;
181 * Now load any DLLs that this module refers to.
183 for(i=0; i<wpnt->ne_header->n_mod_ref_tab; i++){
184 char buff[14];
185 char buff2[14];
186 int fd, j;
187 GetModuleName(wpnt, i + 1, buff);
189 if(FindDLLTable(buff)) continue; /* This module already loaded */
191 /* The next trick is to convert the case, and add the .dll
192 * extension if required to find the actual library. We may want
193 * to use a search path at some point as well. */
195 /* First try the straight name */
196 strcpy(buff2, buff);
197 if(fd = open(buff2, O_RDONLY) >= 0) {
198 close(fd);
199 LoadImage(buff2, buff);
200 continue;
203 /* OK, that did not work, try making it lower-case, and add the .dll
204 extension */
206 for(j=0; j<strlen(buff2); j++)
207 if(buff2[j] >= 'A' && buff2[j] <= 'Z') buff2[j] |= 0x20;
208 strcat(buff2, ".dll");
210 if(fd = open(buff2, O_RDONLY) >= 0) {
211 close(fd);
212 LoadImage(buff2, buff);
213 continue;
216 fprintf(stderr,"Unable to load:%s\n", buff);
221 /**********************************************************************
222 * main
224 _WinMain(int argc, char **argv)
226 int segment;
227 struct w_files * wpnt;
228 int cs_reg, ds_reg, ss_reg, ip_reg, sp_reg;
229 int i;
230 int rv;
232 Argc = argc;
233 Argv = argv;
235 if (argc < 2)
237 fprintf(stderr, "usage: %s FILENAME\n", argv[0]);
238 exit(1);
241 LoadImage(argv[1], NULL);
243 if(ran_out) exit(1);
244 #ifdef DEBUG
245 GetEntryDLLName("USER", "INITAPP", 0, 0);
246 for(i=0; i<1024; i++) {
247 int j;
248 j = GetEntryPointFromOrdinal(wine_files, i);
249 if(j == 0) break;
250 fprintf(stderr," %d %x\n", i, j);
252 #endif
254 * Fixup references.
256 wpnt = wine_files;
257 for(wpnt = wine_files; wpnt; wpnt = wpnt->next)
258 for (segment = 0; segment < wpnt->ne_header->n_segment_tab; segment++)
260 if (FixupSegment(wpnt, segment) < 0)
262 myerror("fixup failed.");
267 * Fixup stack and jump to start.
269 ds_reg = wine_files->selector_table[wine_files->ne_header->auto_data_seg-1].selector;
270 cs_reg = wine_files->selector_table[wine_files->ne_header->cs-1].selector;
271 ip_reg = wine_files->ne_header->ip;
272 ss_reg = wine_files->selector_table[wine_files->ne_header->ss-1].selector;
273 sp_reg = wine_files->ne_header->sp;
275 rv = CallToInit16(cs_reg << 16 | ip_reg, ss_reg << 16 | sp_reg, ds_reg);
276 printf ("rv = %x\n", rv);
280 /**********************************************************************
281 * GetImportedName
283 char *
284 GetImportedName(int fd, struct mz_header_s *mz_header,
285 struct ne_header_s *ne_header, int name_offset, char *buffer)
287 char *p;
288 int length;
289 int status;
290 int i;
292 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
293 name_offset, SEEK_SET);
294 length = 0;
295 read(fd, &length, 1); /* Get the length byte */
296 read(fd, buffer, length);
297 buffer[length] = 0;
298 return buffer;
301 /**********************************************************************
302 * GetModuleName
304 char *
305 GetModuleName(struct w_files * wpnt, int index, char *buffer)
307 int fd = wpnt->fd;
308 struct mz_header_s *mz_header = wpnt->mz_header;
309 struct ne_header_s *ne_header = wpnt->ne_header;
310 char *p;
311 int length;
312 int name_offset, status;
313 int i;
315 status = lseek(fd, mz_header->ne_offset + ne_header->moduleref_tab_offset +
316 2*(index - 1), SEEK_SET);
317 name_offset = 0;
318 read(fd, &name_offset, 2);
319 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
320 name_offset, SEEK_SET);
321 length = 0;
322 read(fd, &length, 1); /* Get the length byte */
323 read(fd, buffer, length);
324 buffer[length] = 0;
326 /* Module names are always upper case */
327 for(i=0; i<length; i++)
328 if(buffer[i] >= 'a' && buffer[i] <= 'z') buffer[i] &= ~0x20;
330 return buffer;
334 /**********************************************************************
335 * FixupSegment
338 FixupSegment(struct w_files * wpnt, int segment_num)
340 int fd = wpnt->fd;
341 struct mz_header_s * mz_header = wpnt->mz_header;
342 struct ne_header_s *ne_header = wpnt->ne_header;
343 struct ne_segment_table_entry_s *seg_table = wpnt->seg_table;
344 struct segment_descriptor_s *selector_table = wpnt->selector_table;
346 struct relocation_entry_s *rep, *rep1;
347 struct ne_segment_table_entry_s *seg;
348 struct segment_descriptor_s *sel;
349 struct dll_table_entry_s *dll_table;
350 int status;
351 unsigned short *sp;
352 unsigned int selector, address;
353 unsigned int next_addr;
354 int ordinal;
355 char dll_name[257];
356 char func_name[257];
357 int i, n_entries;
359 seg = &seg_table[segment_num];
360 sel = &selector_table[segment_num];
362 if ((seg->seg_data_offset == 0) ||
363 !(seg->seg_flags & NE_SEGFLAGS_RELOC_DATA))
364 return 0;
367 * Go through the relocation table on entry at a time.
369 i = seg->seg_data_length;
370 if (i == 0)
371 i = 0x10000;
373 status = lseek(fd, seg->seg_data_offset *
374 (1 << ne_header->align_shift_count) + i, SEEK_SET);
375 n_entries = 0;
376 read(fd, &n_entries, sizeof(short int));
377 rep = (struct relocation_entry_s *)
378 malloc(n_entries * sizeof(struct relocation_entry_s));
380 if (read(fd,rep, n_entries * sizeof(struct relocation_entry_s)) !=
381 n_entries * sizeof(struct relocation_entry_s))
383 myerror("Unable to read relocation information");
386 rep1 = rep;
388 for (i = 0; i < n_entries; i++, rep++)
391 * Get the target address corresponding to this entry.
393 switch (rep->relocation_type)
395 case NE_RELTYPE_ORDINAL:
396 if (GetModuleName(wpnt, rep->target1,
397 dll_name) == NULL)
399 fprintf(stderr, "NE_RELTYPE_ORDINAL failed");
400 return -1;
403 ordinal = rep->target2;
405 status = GetEntryDLLOrdinal(dll_name, ordinal, &selector,
406 &address);
407 if (status)
409 char s[80];
411 sprintf(s, "Bad DLL name '%s.%d'", dll_name, ordinal);
412 myerror(s);
413 return -1;
416 #ifdef DEBUG_FIXUP
417 printf("%d: %s.%d: %04.4x:%04.4x\n", i + 1, dll_name, ordinal,
418 selector, address);
419 #endif
420 break;
422 case NE_RELTYPE_NAME:
423 if (GetModuleName(wpnt, rep->target1, dll_name)
424 == NULL)
426 fprintf(stderr,"NE_RELTYPE_NAME failed");
427 return -1;
430 if (GetImportedName(fd, mz_header, ne_header,
431 rep->target2, func_name) == NULL)
433 fprintf(stderr,"getimportedname failed");
434 return -1;
437 status = GetEntryDLLName(dll_name, func_name, &selector,
438 &address);
439 if (status)
441 char s[80];
443 sprintf(s, "Bad DLL name '%s (%s)'", dll_name,func_name);
444 myerror(s);
445 return -1;
448 #ifdef DEBUG_FIXUP
449 printf("%d: %s %s.%d: %04.4x:%04.4x\n", i + 1, func_name,
450 dll_name, ordinal, selector, address);
451 #endif
452 break;
454 case NE_RELTYPE_INTERNAL:
455 if (rep->target1 == 0x00ff)
457 address = GetEntryPointFromOrdinal(wpnt, rep->target2);
458 selector = (address >> 16) & 0xffff;
459 address &= 0xffff;
461 else
463 selector = selector_table[rep->target1-1].selector;
464 address = rep->target2;
467 #ifdef DEBUG_FIXUP
468 printf("%d: %04.4x:%04.4x\n", i + 1, selector, address);
469 #endif
470 break;
472 case 7:
473 /* Relocation type 7:
475 * These appear to be used as fixups for the Windows
476 * floating point emulator. Let's just ignore them and
477 * try to use the hardware floating point. Linux should
478 * successfully emulate the coprocessor if it doesn't
479 * exist.
481 #ifdef DEBUG_FIXUP
482 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
483 i + 1, rep->address_type, rep->relocation_type,
484 rep->offset);
485 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
486 #endif
487 continue;
489 default:
490 #ifndef DEBUG_FIXUP
491 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
492 i + 1, rep->address_type, rep->relocation_type,
493 rep->offset);
494 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
495 #endif
496 free(rep1);
497 return -1;
501 * Stuff the right size result in.
503 sp = (unsigned short *) ((char *) sel->base_addr + rep->offset);
504 switch (rep->address_type)
506 case NE_RADDR_OFFSET16:
507 do {
508 next_addr = *sp;
509 *sp = (unsigned short) address;
510 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
512 while (next_addr != 0xffff);
514 break;
516 case NE_RADDR_POINTER32:
517 do {
518 next_addr = *sp;
519 *sp = (unsigned short) address;
520 *(sp+1) = (unsigned short) selector;
521 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
523 while (next_addr != 0xffff);
525 break;
527 case NE_RADDR_SELECTOR:
528 do {
529 next_addr = *sp;
530 *sp = (unsigned short) selector;
531 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
533 while (next_addr != 0xffff);
535 break;
537 default:
538 #ifdef DEBUG_FIXUP
539 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
540 i + 1, rep->address_type, rep->relocation_type,
541 rep->offset);
542 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
543 #endif
544 free(rep1);
545 return -1;
549 free(rep1);
550 return 0;