Release 0.4.7
[wine/multimedia.git] / loader / wine.c
blob82928caf75a17abacc2d30deb97e52e5b4e3dc19
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"
25 extern int CallToInit16(unsigned long csip, unsigned long sssp,
26 unsigned short ds);
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;
36 char **Argv;
37 int Argc;
38 struct mz_header_s *CurrentMZHeader;
39 struct ne_header_s *CurrentNEHeader;
40 int CurrentNEFile;
42 static char *dllExtensions[] = { "dll", "exe", NULL };
43 static char *exeExtensions[] = { "exe", NULL };
44 static char *WinePath = NULL;
46 /**********************************************************************
47 * DebugPrintString
49 int
50 DebugPrintString(char *str)
52 printf("%s", str);
53 return 0;
56 /**********************************************************************
57 * myerror
59 void
60 myerror(const char *s)
62 if (s == NULL)
63 perror("wine");
64 else
65 fprintf(stderr, "wine: %s\n", s);
67 exit(1);
70 /**********************************************************************
71 * GetFilenameFromInstance
73 char *
74 GetFilenameFromInstance(unsigned short instance)
76 register struct w_files *w = wine_files;
78 while (w && w->hinstance != instance)
79 w = w->next;
81 if (w)
82 return w->filename;
83 else
84 return NULL;
87 struct w_files *
88 GetFileInfo(unsigned short instance)
90 register struct w_files *w = wine_files;
92 while (w && w->hinstance != instance)
93 w = w->next;
95 return w;
98 /**********************************************************************
99 * LoadImage
100 * Load one NE format executable into memory
102 LoadImage(char * filename, char * modulename)
104 unsigned int read_size;
105 int i;
106 struct w_files * wpnt, *wpnt1;
107 unsigned int status;
109 /* First allocate a spot to store the info we collect, and add it to
110 * our linked list.
113 wpnt = (struct w_files *) malloc(sizeof(struct w_files));
114 if(wine_files == NULL)
115 wine_files = wpnt;
116 else {
117 wpnt1 = wine_files;
118 while(wpnt1->next) wpnt1 = wpnt1->next;
119 wpnt1->next = wpnt;
121 wpnt->next = NULL;
124 * Open file for reading.
126 wpnt->fd = open(filename, O_RDONLY);
127 if (wpnt->fd < 0)
129 myerror(NULL);
132 * Establish header pointers.
134 wpnt->filename = strdup(filename);
135 wpnt->name = NULL;
136 if(modulename) wpnt->name = strdup(modulename);
138 wpnt->mz_header = (struct mz_header_s *) malloc(sizeof(struct mz_header_s));;
139 status = lseek(wpnt->fd, 0, SEEK_SET);
140 if (read(wpnt->fd, wpnt->mz_header, sizeof(struct mz_header_s)) !=
141 sizeof(struct mz_header_s))
143 myerror("Unable to read MZ header from file");
145 if (wpnt->mz_header->must_be_0x40 != 0x40)
146 myerror("This is not a Windows program");
148 wpnt->ne_header = (struct ne_header_s *) malloc(sizeof(struct ne_header_s));
149 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset, SEEK_SET);
150 if (read(wpnt->fd, wpnt->ne_header, sizeof(struct ne_header_s))
151 != sizeof(struct ne_header_s))
153 myerror("Unable to read NE header from file");
155 if (wpnt->ne_header->header_type[0] != 'N' ||
156 wpnt->ne_header->header_type[1] != 'E')
157 myerror("This is not a Windows program");
159 if(wine_files == wpnt){
160 CurrentMZHeader = wpnt->mz_header;
161 CurrentNEHeader = wpnt->ne_header;
162 CurrentNEFile = wpnt->fd;
164 WIN_StackSize = wpnt->ne_header->stack_length;
165 WIN_HeapSize = wpnt->ne_header->local_heap_length;
169 * Create segment selectors.
171 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
172 wpnt->ne_header->segment_tab_offset,
173 SEEK_SET);
174 read_size = wpnt->ne_header->n_segment_tab *
175 sizeof(struct ne_segment_table_entry_s);
176 wpnt->seg_table = (struct ne_segment_table_entry_s *) malloc(read_size);
177 if (read(wpnt->fd, wpnt->seg_table, read_size) != read_size)
178 myerror("Unable to read segment table header from file");
179 wpnt->selector_table = CreateSelectors(wpnt);
180 wpnt->hinstance
181 = wpnt->
182 selector_table[wine_files->ne_header->auto_data_seg-1].selector;
184 /* Get the lookup table. This is used for looking up the addresses
185 of functions that are exported */
187 read_size = wpnt->ne_header->entry_tab_length;
188 wpnt->lookup_table = (char *) malloc(read_size);
189 lseek(wpnt->fd, wpnt->mz_header->ne_offset +
190 wpnt->ne_header->entry_tab_offset, SEEK_SET);
191 if (read(wpnt->fd, wpnt->lookup_table, read_size) != read_size)
192 myerror("Unable to read lookup table header from file");
194 /* Get the iname table. This is used for looking up the names
195 of functions that are exported */
197 status = lseek(wpnt->fd, wpnt->ne_header->nrname_tab_offset, SEEK_SET);
198 read_size = wpnt->ne_header->nrname_tab_length;
199 wpnt->nrname_table = (char *) malloc(read_size);
200 if (read(wpnt->fd, wpnt->nrname_table, read_size) != read_size)
201 myerror("Unable to read nrname table header from file");
203 status = lseek(wpnt->fd, wpnt->mz_header->ne_offset +
204 wpnt->ne_header->rname_tab_offset, SEEK_SET);
205 read_size = wpnt->ne_header->moduleref_tab_offset -
206 wpnt->ne_header->rname_tab_offset;
207 wpnt->rname_table = (char *) malloc(read_size);
208 if (read(wpnt->fd, wpnt->rname_table, read_size) != read_size)
209 myerror("Unable to read rname table header from file");
211 /* Now get the module name */
213 wpnt->name = (char*) malloc(*wpnt->rname_table + 1);
214 memcpy(wpnt->name, wpnt->rname_table+1, *wpnt->rname_table);
215 wpnt->name[*wpnt->rname_table] = 0;
218 * Now load any DLLs that this module refers to.
220 for(i=0; i<wpnt->ne_header->n_mod_ref_tab; i++){
221 char buff[14];
222 char buff2[256];
223 int fd, j;
224 GetModuleName(wpnt, i + 1, buff);
226 if(FindDLLTable(buff)) continue; /* This module already loaded */
228 if (FindFileInPath(buff2, sizeof(buff2),
229 buff, dllExtensions, WinePath) != NULL &&
230 (fd = open(buff2, O_RDONLY)) >= 0)
232 close(fd);
233 LoadImage(buff2, buff);
234 continue;
237 fprintf(stderr,"Unable to load:%s\n", buff);
242 /**********************************************************************
243 * main
245 _WinMain(int argc, char **argv)
247 int segment;
248 char *p;
249 char exe_path[256];
250 #ifdef WINESTAT
251 char * cp;
252 #endif
253 struct w_files * wpnt;
254 int cs_reg, ds_reg, ss_reg, ip_reg, sp_reg;
255 int i;
256 int rv;
258 Argc = argc - 1;
259 Argv = argv + 1;
261 if (argc < 2)
263 fprintf(stderr, "usage: %s FILENAME\n", argv[0]);
264 exit(1);
267 p = getenv("WINEPATH");
268 WinePath = malloc(256 + strlen(p));
269 getcwd(WinePath, 256);
270 strcat(WinePath, ";");
271 strcat(WinePath, p);
273 if (FindFileInPath(exe_path, 256, argv[1], exeExtensions, WinePath)
274 == NULL)
276 fprintf(stderr, "Could not find file '%s'\n", argv[1]);
277 exit(1);
280 LoadImage(exe_path, NULL);
282 if(ran_out) exit(1);
283 #ifdef DEBUG
284 GetEntryDLLName("USER", "INITAPP", 0, 0);
285 for(i=0; i<1024; i++) {
286 int j;
287 j = GetEntryPointFromOrdinal(wine_files, i);
288 if(j == 0) break;
289 fprintf(stderr," %d %x\n", i, j);
291 #endif
293 * Fixup references.
295 wpnt = wine_files;
296 for(wpnt = wine_files; wpnt; wpnt = wpnt->next)
297 for (segment = 0; segment < wpnt->ne_header->n_segment_tab; segment++)
299 if (FixupSegment(wpnt, segment) < 0)
301 myerror("fixup failed.");
306 * Fixup stack and jump to start.
308 ds_reg = wine_files->selector_table[wine_files->ne_header->auto_data_seg-1].selector;
309 cs_reg = wine_files->selector_table[wine_files->ne_header->cs-1].selector;
310 ip_reg = wine_files->ne_header->ip;
311 ss_reg = wine_files->selector_table[wine_files->ne_header->ss-1].selector;
312 sp_reg = wine_files->ne_header->sp;
314 #ifdef WINESTAT
315 cp = strrchr(argv[0], '/');
316 if(!cp) cp = argv[0];
317 else cp++;
318 if(strcmp(cp,"winestat") == 0) {
319 winestat();
320 exit(0);
322 #endif
324 init_wine_signals();
326 rv = CallToInit16(cs_reg << 16 | ip_reg, ss_reg << 16 | sp_reg, ds_reg);
327 printf ("rv = %x\n", rv);
331 /**********************************************************************
332 * GetImportedName
334 char *
335 GetImportedName(int fd, struct mz_header_s *mz_header,
336 struct ne_header_s *ne_header, int name_offset, char *buffer)
338 char *p;
339 int length;
340 int status;
341 int i;
343 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
344 name_offset, SEEK_SET);
345 length = 0;
346 read(fd, &length, 1); /* Get the length byte */
347 read(fd, buffer, length);
348 buffer[length] = 0;
349 return buffer;
352 /**********************************************************************
353 * GetModuleName
355 char *
356 GetModuleName(struct w_files * wpnt, int index, char *buffer)
358 int fd = wpnt->fd;
359 struct mz_header_s *mz_header = wpnt->mz_header;
360 struct ne_header_s *ne_header = wpnt->ne_header;
361 char *p;
362 int length;
363 int name_offset, status;
364 int i;
366 status = lseek(fd, mz_header->ne_offset + ne_header->moduleref_tab_offset +
367 2*(index - 1), SEEK_SET);
368 name_offset = 0;
369 read(fd, &name_offset, 2);
370 status = lseek(fd, mz_header->ne_offset + ne_header->iname_tab_offset +
371 name_offset, SEEK_SET);
372 length = 0;
373 read(fd, &length, 1); /* Get the length byte */
374 read(fd, buffer, length);
375 buffer[length] = 0;
377 /* Module names are always upper case */
378 for(i=0; i<length; i++)
379 if(buffer[i] >= 'a' && buffer[i] <= 'z') buffer[i] &= ~0x20;
381 return buffer;
385 /**********************************************************************
386 * FixupSegment
389 FixupSegment(struct w_files * wpnt, int segment_num)
391 int fd = wpnt->fd;
392 struct mz_header_s * mz_header = wpnt->mz_header;
393 struct ne_header_s *ne_header = wpnt->ne_header;
394 struct ne_segment_table_entry_s *seg_table = wpnt->seg_table;
395 struct segment_descriptor_s *selector_table = wpnt->selector_table;
397 struct relocation_entry_s *rep, *rep1;
398 struct ne_segment_table_entry_s *seg;
399 struct segment_descriptor_s *sel;
400 struct dll_table_entry_s *dll_table;
401 int status;
402 unsigned short *sp;
403 unsigned int selector, address;
404 unsigned int next_addr;
405 int ordinal;
406 char dll_name[257];
407 char func_name[257];
408 int i, n_entries;
410 seg = &seg_table[segment_num];
411 sel = &selector_table[segment_num];
413 if ((seg->seg_data_offset == 0) ||
414 !(seg->seg_flags & NE_SEGFLAGS_RELOC_DATA))
415 return 0;
418 * Go through the relocation table on entry at a time.
420 i = seg->seg_data_length;
421 if (i == 0)
422 i = 0x10000;
424 status = lseek(fd, seg->seg_data_offset *
425 (1 << ne_header->align_shift_count) + i, SEEK_SET);
426 n_entries = 0;
427 read(fd, &n_entries, sizeof(short int));
428 rep = (struct relocation_entry_s *)
429 malloc(n_entries * sizeof(struct relocation_entry_s));
431 if (read(fd,rep, n_entries * sizeof(struct relocation_entry_s)) !=
432 n_entries * sizeof(struct relocation_entry_s))
434 myerror("Unable to read relocation information");
437 rep1 = rep;
439 for (i = 0; i < n_entries; i++, rep++)
442 * Get the target address corresponding to this entry.
444 switch (rep->relocation_type)
446 case NE_RELTYPE_ORDINAL:
447 if (GetModuleName(wpnt, rep->target1,
448 dll_name) == NULL)
450 fprintf(stderr, "NE_RELTYPE_ORDINAL failed");
451 return -1;
454 ordinal = rep->target2;
456 status = GetEntryDLLOrdinal(dll_name, ordinal, &selector,
457 &address);
458 if (status)
460 char s[80];
462 sprintf(s, "Bad DLL name '%s.%d'", dll_name, ordinal);
463 myerror(s);
464 return -1;
467 #ifdef DEBUG_FIXUP
468 printf("%d: %s.%d: %04.4x:%04.4x\n", i + 1, dll_name, ordinal,
469 selector, address);
470 #endif
471 break;
473 case NE_RELTYPE_NAME:
474 if (GetModuleName(wpnt, rep->target1, dll_name)
475 == NULL)
477 fprintf(stderr,"NE_RELTYPE_NAME failed");
478 return -1;
481 if (GetImportedName(fd, mz_header, ne_header,
482 rep->target2, func_name) == NULL)
484 fprintf(stderr,"getimportedname failed");
485 return -1;
488 status = GetEntryDLLName(dll_name, func_name, &selector,
489 &address);
490 if (status)
492 char s[80];
494 sprintf(s, "Bad DLL name '%s (%s)'", dll_name,func_name);
495 myerror(s);
496 return -1;
499 #ifdef DEBUG_FIXUP
500 printf("%d: %s %s.%d: %04.4x:%04.4x\n", i + 1, func_name,
501 dll_name, ordinal, selector, address);
502 #endif
503 break;
505 case NE_RELTYPE_INTERNAL:
506 case NE_RELTYPE_INT1:
507 if (rep->target1 == 0x00ff)
509 address = GetEntryPointFromOrdinal(wpnt, rep->target2);
510 selector = (address >> 16) & 0xffff;
511 address &= 0xffff;
513 else
515 selector = selector_table[rep->target1-1].selector;
516 address = rep->target2;
519 #ifdef DEBUG_FIXUP
520 printf("%d: %04.4x:%04.4x\n", i + 1, selector, address);
521 #endif
522 break;
524 case 7:
525 /* Relocation type 7:
527 * These appear to be used as fixups for the Windows
528 * floating point emulator. Let's just ignore them and
529 * try to use the hardware floating point. Linux should
530 * successfully emulate the coprocessor if it doesn't
531 * exist.
533 #ifdef DEBUG_FIXUP
534 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
535 i + 1, rep->address_type, rep->relocation_type,
536 rep->offset);
537 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
538 #endif
539 continue;
541 default:
542 #ifndef DEBUG_FIXUP
543 fprintf(stderr,"%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
544 i + 1, rep->address_type, rep->relocation_type,
545 rep->offset);
546 fprintf(stderr,"TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
547 #endif
548 free(rep1);
550 return -1;
554 * Stuff the right size result in.
556 sp = (unsigned short *) ((char *) sel->base_addr + rep->offset);
557 switch (rep->address_type)
559 case NE_RADDR_OFFSET16:
560 do {
561 next_addr = *sp;
562 *sp = (unsigned short) address;
563 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
565 while (next_addr != 0xffff);
567 break;
569 case NE_RADDR_POINTER32:
570 do {
571 next_addr = *sp;
572 *sp = (unsigned short) address;
573 *(sp+1) = (unsigned short) selector;
574 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
576 while (next_addr != 0xffff);
578 break;
580 case NE_RADDR_SELECTOR:
581 do {
582 next_addr = *sp;
583 *sp = (unsigned short) selector;
584 sp = (unsigned short *) ((char *) sel->base_addr + next_addr);
585 if (rep->relocation_type == NE_RELTYPE_INT1) break;
588 while (next_addr != 0xffff);
590 break;
592 default:
593 #ifndef DEBUG_FIXUP
594 printf("%d: ADDR TYPE %d, TYPE %d, OFFSET %04.4x, ",
595 i + 1, rep->address_type, rep->relocation_type,
596 rep->offset);
597 printf("TARGET %04.4x %04.4x\n", rep->target1, rep->target2);
598 #endif
599 free(rep1);
600 return -1;
604 free(rep1);
605 return 0;
608 /**********************************************************************
609 * GetProcAddress
611 FARPROC GetProcAddress(HINSTANCE hinstance, char *proc_name)
613 if ((int) proc_name & 0xffff0000)
614 printf("GetProcAddress: %#04x, '%s'\n", hinstance, proc_name);
615 else
616 printf("GetProcAddress: %#04x, %d\n", hinstance, (int) proc_name);
618 return NULL;