Added HOST=mingw32-linux64 setting.
[reactos.git] / reactos / tools / winebuild / import.c
1 /*
2 * DLL imports support
3 *
4 * Copyright 2000, 2004 Alexandre Julliard
5 * Copyright 2000 Eric Pouech
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "config.h"
23
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32
33 #include "winglue.h"
34 #include "build.h"
35
36 struct import
37 {
38 DLLSPEC *spec; /* description of the imported dll */
39 int delay; /* delay or not dll loading ? */
40 ORDDEF **exports; /* functions exported from this dll */
41 int nb_exports; /* number of exported functions */
42 ORDDEF **imports; /* functions we want to import from this dll */
43 int nb_imports; /* number of imported functions */
44 };
45
46 static char **undef_symbols; /* list of undefined symbols */
47 static int nb_undef_symbols = -1;
48 static int undef_size;
49
50 static char **ignore_symbols; /* list of symbols to ignore */
51 static int nb_ignore_symbols;
52 static int ignore_size;
53
54 static char *ld_tmp_file; /* ld temp file name */
55
56 static struct import **dll_imports = NULL;
57 static int nb_imports = 0; /* number of imported dlls (delayed or not) */
58 static int nb_delayed = 0; /* number of delayed dlls */
59 static int total_imports = 0; /* total number of imported functions */
60 static int total_delayed = 0; /* total number of imported functions in delayed DLLs */
61
62 /* list of symbols that are ignored by default */
63 static const char * const default_ignored_symbols[] =
64 {
65 "abs",
66 "acos",
67 "asin",
68 "atan",
69 "atan2",
70 "atof",
71 "atoi",
72 "atol",
73 "bsearch",
74 "ceil",
75 "cos",
76 "cosh",
77 "exp",
78 "fabs",
79 "floor",
80 "fmod",
81 "frexp",
82 "labs",
83 "log",
84 "log10",
85 "memchr",
86 "memcmp",
87 "memcpy",
88 "memmove",
89 "memset",
90 "modf",
91 "pow",
92 "qsort",
93 "sin",
94 "sinh",
95 "sqrt",
96 "strcat",
97 "strchr",
98 "strcmp",
99 "strcpy",
100 "strcspn",
101 "strlen",
102 "strncat",
103 "strncmp",
104 "strncpy",
105 "strpbrk",
106 "strrchr",
107 "strspn",
108 "strstr",
109 "tan",
110 "tanh"
111 };
112
113 #ifdef __powerpc__
114 # ifdef __APPLE__
115 # define ppc_high(mem) "ha16(" mem ")"
116 # define ppc_low(mem) "lo16(" mem ")"
117 static const char * const ppc_reg[32] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
118 "r8", "r9", "r10","r11","r12","r13","r14","r15",
119 "r16","r17","r18","r19","r20","r21","r22","r23",
120 "r24","r25","r26","r27","r28","r29","r30","r31" };
121 # else /* __APPLE__ */
122 # define ppc_high(mem) "(" mem ")@hi"
123 # define ppc_low(mem) "(" mem ")@l"
124 static const char * const ppc_reg[32] = { "0", "1", "2", "3", "4", "5", "6", "7",
125 "8", "9", "10","11","12","13","14","15",
126 "16","17","18","19","20","21","22","23",
127 "24","25","26","27","28","29","30","31" };
128 # endif /* __APPLE__ */
129 #endif /* __powerpc__ */
130
131 /* compare function names; helper for resolve_imports */
132 static int name_cmp( const void *name, const void *entry )
133 {
134 return strcmp( *(const char* const *)name, *(const char* const *)entry );
135 }
136
137 /* compare function names; helper for resolve_imports */
138 static int func_cmp( const void *func1, const void *func2 )
139 {
140 const ORDDEF *odp1 = *(const ORDDEF * const *)func1;
141 const ORDDEF *odp2 = *(const ORDDEF * const *)func2;
142 return strcmp( odp1->name ? odp1->name : odp1->export_name,
143 odp2->name ? odp2->name : odp2->export_name );
144 }
145
146 /* locate a symbol in a (sorted) list */
147 inline static const char *find_symbol( const char *name, char **table, int size )
148 {
149 char **res = NULL;
150
151 if (table) {
152 res = bsearch( &name, table, size, sizeof(*table), name_cmp );
153 }
154
155 return res ? *res : NULL;
156 }
157
158 /* locate an export in a (sorted) export list */
159 inline static ORDDEF *find_export( const char *name, ORDDEF **table, int size )
160 {
161 ORDDEF func, *odp, **res = NULL;
162
163 func.name = (char *)name;
164 func.ordinal = -1;
165 odp = &func;
166 if (table) res = bsearch( &odp, table, size, sizeof(*table), func_cmp );
167 return res ? *res : NULL;
168 }
169
170 /* sort a symbol table */
171 inline static void sort_symbols( char **table, int size )
172 {
173 if (table )
174 qsort( table, size, sizeof(*table), name_cmp );
175 }
176
177 inline static void output_function_size( FILE *outfile, const char *name )
178 {
179 #ifdef HAVE_ASM_DOT_SIZE
180 fprintf( outfile, " \"\\t.size " __ASM_NAME("%s") ", . - " __ASM_NAME("%s") "\\n\"\n", name, name);
181 #endif
182 }
183
184 /* free an import structure */
185 static void free_imports( struct import *imp )
186 {
187 free( imp->exports );
188 free( imp->imports );
189 free_dll_spec( imp->spec );
190 free( imp );
191 }
192
193 /* remove the temp file at exit */
194 static void remove_ld_tmp_file(void)
195 {
196 if (ld_tmp_file) unlink( ld_tmp_file );
197 }
198
199 /* check whether a given dll has already been imported */
200 static int is_already_imported( const char *name )
201 {
202 int i;
203
204 for (i = 0; i < nb_imports; i++)
205 {
206 if (!strcmp( dll_imports[i]->spec->file_name, name )) return 1;
207 }
208 return 0;
209 }
210
211 /* open the .so library for a given dll in a specified path */
212 static char *try_library_path( const char *path, const char *name )
213 {
214 char *buffer;
215 int fd;
216
217 buffer = xmalloc( strlen(path) + strlen(name) + 9 );
218 sprintf( buffer, "%s/lib%s.def", path, name );
219
220 /* check if the file exists */
221 if ((fd = open( buffer, O_RDONLY )) != -1)
222 {
223 close( fd );
224 return buffer;
225 }
226 free( buffer );
227 return NULL;
228 }
229
230 /* open the .so library for a given dll */
231 static char *open_library( const char *name )
232 {
233 char *fullname;
234 int i;
235
236 for (i = 0; i < nb_lib_paths; i++)
237 {
238 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
239 }
240 fatal_error( "could not open .def file for %s\n", name );
241 return NULL;
242 }
243
244 /* read in the list of exported symbols of an import library */
245 static int read_import_lib( const char *name, struct import *imp )
246 {
247 FILE *f;
248 char *fullname;
249 int i, ret;
250 DLLSPEC *spec = imp->spec;
251
252 imp->exports = NULL;
253 imp->nb_exports = 0;
254
255 fullname = open_library( name );
256 f = open_input_file( NULL, fullname );
257 free( fullname );
258
259 ret = parse_def_file( f, spec );
260 close_input_file( f );
261 if (!ret) return 0;
262 if (is_already_imported( spec->file_name )) return 0;
263
264 imp->exports = xmalloc( spec->nb_entry_points * sizeof(*imp->exports) );
265
266 for (i = 0; i < spec->nb_entry_points; i++)
267 {
268 ORDDEF *odp = &spec->entry_points[i];
269
270 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
271 if (odp->flags & FLAG_PRIVATE) continue;
272 imp->exports[imp->nb_exports++] = odp;
273 }
274 imp->exports = xrealloc( imp->exports, imp->nb_exports * sizeof(*imp->exports) );
275 if (imp->nb_exports)
276 qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
277 return 1;
278 }
279
280 /* add a dll to the list of imports */
281 void add_import_dll( const char *name, int delay )
282 {
283 struct import *imp;
284 char *fullname;
285
286 fullname = xmalloc( strlen(name) + 5 );
287 strcpy( fullname, name );
288 if (!strchr( fullname, '.' )) strcat( fullname, ".dll" );
289
290 /* check if we already imported it */
291 if (is_already_imported( fullname ))
292 {
293 free( fullname );
294 return;
295 }
296
297 imp = xmalloc( sizeof(*imp) );
298 imp->spec = alloc_dll_spec();
299 imp->spec->file_name = fullname;
300 imp->delay = delay;
301 imp->imports = NULL;
302 imp->nb_imports = 0;
303 if (delay) nb_delayed++;
304
305 if (read_import_lib( name, imp ))
306 {
307 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
308 dll_imports[nb_imports++] = imp;
309 }
310 else
311 {
312 free_imports( imp );
313 if (nb_errors) exit(1);
314 }
315 }
316
317 /* remove an imported dll, based on its index in the dll_imports array */
318 static void remove_import_dll( int index )
319 {
320 struct import *imp = dll_imports[index];
321
322 memmove( &dll_imports[index], &dll_imports[index+1], sizeof(imp) * (nb_imports - index - 1) );
323 nb_imports--;
324 if (imp->delay) nb_delayed--;
325 free_imports( imp );
326 }
327
328 /* initialize the list of ignored symbols */
329 static void init_ignored_symbols(void)
330 {
331 int i;
332
333 nb_ignore_symbols = sizeof(default_ignored_symbols)/sizeof(default_ignored_symbols[0]);
334 ignore_size = nb_ignore_symbols + 32;
335 ignore_symbols = xmalloc( ignore_size * sizeof(*ignore_symbols) );
336 for (i = 0; i < nb_ignore_symbols; i++)
337 ignore_symbols[i] = xstrdup( default_ignored_symbols[i] );
338 }
339
340 /* add a symbol to the ignored symbol list */
341 /* if the name starts with '-' the symbol is removed instead */
342 void add_ignore_symbol( const char *name )
343 {
344 int i;
345
346 if (!ignore_symbols) init_ignored_symbols(); /* first time around, fill list with defaults */
347
348 if (name[0] == '-') /* remove it */
349 {
350 if (!name[1]) /* remove everything */
351 {
352 for (i = 0; i < nb_ignore_symbols; i++) free( ignore_symbols[i] );
353 nb_ignore_symbols = 0;
354 }
355 else
356 {
357 for (i = 0; i < nb_ignore_symbols; i++)
358 {
359 if (!strcmp( ignore_symbols[i], name+1 ))
360 {
361 free( ignore_symbols[i] );
362 memmove( &ignore_symbols[i], &ignore_symbols[i+1], nb_ignore_symbols - i - 1 );
363 nb_ignore_symbols--;
364 }
365 }
366 }
367 }
368 else
369 {
370 if (nb_ignore_symbols == ignore_size)
371 {
372 ignore_size += 32;
373 ignore_symbols = xrealloc( ignore_symbols, ignore_size * sizeof(*ignore_symbols) );
374 }
375 ignore_symbols[nb_ignore_symbols++] = xstrdup( name );
376 }
377 }
378
379 /* add a function to the list of imports from a given dll */
380 static void add_import_func( struct import *imp, ORDDEF *func )
381 {
382 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
383 imp->imports[imp->nb_imports++] = func;
384 total_imports++;
385 if (imp->delay) total_delayed++;
386 }
387
388 /* add a symbol to the undef list */
389 inline static void add_undef_symbol( const char *name )
390 {
391 if (nb_undef_symbols == undef_size)
392 {
393 undef_size += 128;
394 undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
395 }
396 undef_symbols[nb_undef_symbols++] = xstrdup( name );
397 }
398
399 /* remove all the holes in the undefined symbol list; return the number of removed symbols */
400 static int remove_symbol_holes(void)
401 {
402 int i, off;
403 for (i = off = 0; i < nb_undef_symbols; i++)
404 {
405 if (!undef_symbols[i]) off++;
406 else undef_symbols[i - off] = undef_symbols[i];
407 }
408 nb_undef_symbols -= off;
409 return off;
410 }
411
412 /* add a symbol to the extra list, but only if needed */
413 static int add_extra_symbol( const char **extras, int *count, const char *name, const DLLSPEC *spec )
414 {
415 int i;
416
417 if (!find_symbol( name, undef_symbols, nb_undef_symbols ))
418 {
419 /* check if the symbol is being exported by this dll */
420 for (i = 0; i < spec->nb_entry_points; i++)
421 {
422 ORDDEF *odp = &spec->entry_points[i];
423 if (odp->type == TYPE_STDCALL ||
424 odp->type == TYPE_CDECL ||
425 odp->type == TYPE_VARARGS ||
426 odp->type == TYPE_EXTERN)
427 {
428 if (odp->name && !strcmp( odp->name, name )) return 0;
429 }
430 }
431 extras[*count] = name;
432 (*count)++;
433 }
434 return 1;
435 }
436
437 /* add the extra undefined symbols that will be contained in the generated spec file itself */
438 static void add_extra_undef_symbols( const DLLSPEC *spec )
439 {
440 const char *extras[10];
441 int i, count = 0, nb_stubs = 0, nb_regs = 0;
442 int kernel_imports = 0, ntdll_imports = 0;
443
444 sort_symbols( undef_symbols, nb_undef_symbols );
445
446 for (i = 0; i < spec->nb_entry_points; i++)
447 {
448 ORDDEF *odp = &spec->entry_points[i];
449 if (odp->type == TYPE_STUB) nb_stubs++;
450 if (odp->flags & FLAG_REGISTER) nb_regs++;
451 }
452
453 /* add symbols that will be contained in the spec file itself */
454 if (!(spec->characteristics & IMAGE_FILE_DLL))
455 {
456 switch (spec->subsystem)
457 {
458 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
459 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
460 kernel_imports += add_extra_symbol( extras, &count, "GetCommandLineA", spec );
461 kernel_imports += add_extra_symbol( extras, &count, "GetStartupInfoA", spec );
462 kernel_imports += add_extra_symbol( extras, &count, "GetModuleHandleA", spec );
463 kernel_imports += add_extra_symbol( extras, &count, "ExitProcess", spec );
464 break;
465 }
466 }
467 if (nb_delayed)
468 {
469 kernel_imports += add_extra_symbol( extras, &count, "LoadLibraryA", spec );
470 kernel_imports += add_extra_symbol( extras, &count, "GetProcAddress", spec );
471 }
472 if (nb_regs)
473 ntdll_imports += add_extra_symbol( extras, &count, "__wine_call_from_32_regs", spec );
474 if (nb_delayed || nb_stubs)
475 ntdll_imports += add_extra_symbol( extras, &count, "RtlRaiseException", spec );
476
477 /* make sure we import the dlls that contain these functions */
478 if (kernel_imports) add_import_dll( "kernel32", 0 );
479 if (ntdll_imports) add_import_dll( "ntdll", 0 );
480
481 if (count)
482 {
483 for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
484 sort_symbols( undef_symbols, nb_undef_symbols );
485 }
486 }
487
488 /* check if a given imported dll is not needed, taking forwards into account */
489 static int check_unused( const struct import* imp, const DLLSPEC *spec )
490 {
491 int i;
492 const char *file_name = imp->spec->file_name;
493 size_t len = strlen( file_name );
494 const char *p = strchr( file_name, '.' );
495 if (p && !strcasecmp( p, ".dll" )) len = p - file_name;
496
497 for (i = spec->base; i <= spec->limit; i++)
498 {
499 ORDDEF *odp = spec->ordinals[i];
500 if (!odp || !(odp->flags & FLAG_FORWARD)) continue;
501 if (!strncasecmp( odp->link_name, file_name, len ) &&
502 odp->link_name[len] == '.')
503 return 0; /* found a forward, it is used */
504 }
505 return 1;
506 }
507
508 /* combine a list of object files with ld into a single object file */
509 /* returns the name of the combined file */
510 static const char *ldcombine_files( char **argv )
511 {
512 int i, len = 0;
513 char *cmd;
514 int fd, err;
515
516 if (output_file_name && output_file_name[0])
517 {
518 ld_tmp_file = xmalloc( strlen(output_file_name) + 10 );
519 strcpy( ld_tmp_file, output_file_name );
520 strcat( ld_tmp_file, ".XXXXXX.o" );
521 }
522 else ld_tmp_file = xstrdup( "/tmp/winebuild.tmp.XXXXXX.o" );
523
524 if ((fd = mkstemps( ld_tmp_file, 2 ) == -1)) fatal_error( "could not generate a temp file\n" );
525 close( fd );
526 atexit( remove_ld_tmp_file );
527
528 for (i = 0; argv[i]; i++) len += strlen(argv[i]) + 1;
529 cmd = xmalloc( len + strlen(ld_tmp_file) + 8 + strlen(ld_command) );
530 sprintf( cmd, "%s -r -o %s", ld_command, ld_tmp_file );
531 for (i = 0; argv[i]; i++) sprintf( cmd + strlen(cmd), " %s", argv[i] );
532 err = system( cmd );
533 if (err) fatal_error( "%s -r failed with status %d\n", ld_command, err );
534 free( cmd );
535 return ld_tmp_file;
536 }
537
538 /* read in the list of undefined symbols */
539 void read_undef_symbols( char **argv )
540 {
541 static const char name_prefix[] = __ASM_NAME("");
542 static const int prefix_len = sizeof(name_prefix) - 1;
543 FILE *f;
544 char *cmd, buffer[1024];
545 int err;
546 const char *name;
547
548 if (!argv[0]) return;
549
550 undef_size = nb_undef_symbols = 0;
551
552 /* if we have multiple object files, link them together */
553 if (argv[1]) name = ldcombine_files( argv );
554 else name = argv[0];
555
556 cmd = xmalloc( strlen(nm_command) + strlen(name) + 5 );
557 sprintf( cmd, "%s -u %s", nm_command, name );
558 if (!(f = popen( cmd, "r" )))
559 fatal_error( "Cannot execute '%s'\n", cmd );
560
561 while (fgets( buffer, sizeof(buffer), f ))
562 {
563 char *p = buffer + strlen(buffer) - 1;
564 if (p < buffer) continue;
565 if (*p == '\n') *p-- = 0;
566 p = buffer;
567 while (*p == ' ') p++;
568 if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
569 if (prefix_len && !strncmp( p, name_prefix, prefix_len )) p += prefix_len;
570 add_undef_symbol( p );
571 }
572 if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
573 free( cmd );
574 }
575
576 static void remove_ignored_symbols(void)
577 {
578 int i;
579
580 if (!ignore_symbols) init_ignored_symbols();
581 sort_symbols( ignore_symbols, nb_ignore_symbols );
582 for (i = 0; i < nb_undef_symbols; i++)
583 {
584 if (find_symbol( undef_symbols[i], ignore_symbols, nb_ignore_symbols ))
585 {
586 free( undef_symbols[i] );
587 undef_symbols[i] = NULL;
588 }
589 }
590 remove_symbol_holes();
591 }
592
593 /* resolve the imports for a Win32 module */
594 int resolve_imports( DLLSPEC *spec )
595 {
596 int i, j;
597
598 if (nb_undef_symbols == -1) return 0; /* no symbol file specified */
599
600 add_extra_undef_symbols( spec );
601 remove_ignored_symbols();
602
603 for (i = 0; i < nb_imports; i++)
604 {
605 struct import *imp = dll_imports[i];
606
607 for (j = 0; j < nb_undef_symbols; j++)
608 {
609 ORDDEF *odp = find_export( undef_symbols[j], imp->exports, imp->nb_exports );
610 if (odp)
611 {
612 add_import_func( imp, odp );
613 free( undef_symbols[j] );
614 undef_symbols[j] = NULL;
615 }
616 }
617 /* remove all the holes in the undef symbols list */
618 if (!remove_symbol_holes() && check_unused( imp, spec ))
619 {
620 /* the dll is not used, get rid of it */
621 warning( "%s imported but no symbols used\n", imp->spec->file_name );
622 remove_import_dll( i );
623 i--;
624 }
625 }
626 return 1;
627 }
628
629 /* output the import table of a Win32 module */
630 static int output_immediate_imports( FILE *outfile )
631 {
632 int i, j, pos;
633 int nb_imm = nb_imports - nb_delayed;
634 static const char import_thunks[] = "__wine_spec_import_thunks";
635
636 if (!nb_imm) goto done;
637
638 /* main import header */
639
640 fprintf( outfile, "\nstatic struct {\n" );
641 fprintf( outfile, " struct {\n" );
642 fprintf( outfile, " void *OriginalFirstThunk;\n" );
643 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
644 fprintf( outfile, " unsigned int ForwarderChain;\n" );
645 fprintf( outfile, " const char *Name;\n" );
646 fprintf( outfile, " void *FirstThunk;\n" );
647 fprintf( outfile, " } imp[%d];\n", nb_imm+1 );
648 fprintf( outfile, " const char *data[%d];\n",
649 total_imports - total_delayed + nb_imm );
650 fprintf( outfile, "} imports = {\n {\n" );
651
652 /* list of dlls */
653
654 for (i = j = 0; i < nb_imports; i++)
655 {
656 if (dll_imports[i]->delay) continue;
657 fprintf( outfile, " { 0, 0, 0, \"%s\", &imports.data[%d] },\n",
658 dll_imports[i]->spec->file_name, j );
659 j += dll_imports[i]->nb_imports + 1;
660 }
661
662 fprintf( outfile, " { 0, 0, 0, 0, 0 },\n" );
663 fprintf( outfile, " },\n {\n" );
664
665 /* list of imported functions */
666
667 for (i = 0; i < nb_imports; i++)
668 {
669 if (dll_imports[i]->delay) continue;
670 fprintf( outfile, " /* %s */\n", dll_imports[i]->spec->file_name );
671 for (j = 0; j < dll_imports[i]->nb_imports; j++)
672 {
673 ORDDEF *odp = dll_imports[i]->imports[j];
674 if (!(odp->flags & FLAG_NONAME))
675 {
676 unsigned short ord = odp->ordinal;
677 fprintf( outfile, " \"\\%03o\\%03o%s\",\n",
678 *(unsigned char *)&ord, *((unsigned char *)&ord + 1), odp->name );
679 }
680 else
681 fprintf( outfile, " (char *)%d,\n", odp->ordinal );
682 }
683 fprintf( outfile, " 0,\n" );
684 }
685 fprintf( outfile, " }\n};\n\n" );
686
687 /* thunks for imported functions */
688
689 fprintf( outfile, "#ifndef __GNUC__\nstatic void __asm__dummy_import(void) {\n#endif\n\n" );
690 pos = (sizeof(void *) + 2*sizeof(unsigned int) + sizeof(const char *) + sizeof(void *)) *
691 (nb_imm + 1); /* offset of imports.data from start of imports */
692 fprintf( outfile, "asm(\".text\\n\\t.align %d\\n\"\n", get_alignment(8) );
693 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\"\n", import_thunks);
694
695 for (i = 0; i < nb_imports; i++)
696 {
697 if (dll_imports[i]->delay) continue;
698 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += sizeof(const char *))
699 {
700 ORDDEF *odp = dll_imports[i]->imports[j];
701 const char *name = odp->name ? odp->name : odp->export_name;
702 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", name );
703 fprintf( outfile, " \"\\t.globl " __ASM_NAME("%s") "\\n\"\n", name );
704 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\\t", name);
705
706 #ifndef __REACTOS__
707 #if defined(__i386__)
708 if (strstr( name, "__wine_call_from_16" ))
709 fprintf( outfile, ".byte 0x2e\\n\\tjmp *(imports+%d)\\n\\tnop\\n", pos );
710 else
711 fprintf( outfile, "jmp *(imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos );
712 #elif defined(__sparc__)
713 if ( !UsePIC )
714 {
715 fprintf( outfile, "sethi %%hi(imports+%d), %%g1\\n\\t", pos );
716 fprintf( outfile, "ld [%%g1+%%lo(imports+%d)], %%g1\\n\\t", pos );
717 fprintf( outfile, "jmp %%g1\\n\\tnop\\n" );
718 }
719 else
720 {
721 /* Hmpf. Stupid sparc assembler always interprets global variable
722 names as GOT offsets, so we have to do it the long way ... */
723 fprintf( outfile, "save %%sp, -96, %%sp\\n" );
724 fprintf( outfile, "0:\\tcall 1f\\n\\tnop\\n" );
725 fprintf( outfile, "1:\\tsethi %%hi(imports+%d-0b), %%g1\\n\\t", pos );
726 fprintf( outfile, "or %%g1, %%lo(imports+%d-0b), %%g1\\n\\t", pos );
727 fprintf( outfile, "ld [%%g1+%%o7], %%g1\\n\\t" );
728 fprintf( outfile, "jmp %%g1\\n\\trestore\\n" );
729 }
730
731 #elif defined(__powerpc__)
732 fprintf(outfile, "\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
733 fprintf(outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[9], ppc_reg[1]);
734 fprintf(outfile, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
735 fprintf(outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[8], ppc_reg[1]);
736 fprintf(outfile, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
737 fprintf(outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[1]);
738
739 fprintf(outfile, "\t\"\\tlis %s, " ppc_high(__ASM_NAME("imports") "+ %d") "\\n\"\n", ppc_reg[9], pos);
740 fprintf(outfile, "\t\"\\tla %s, " ppc_low (__ASM_NAME("imports") "+ %d") "(%s)\\n\"\n", ppc_reg[8], pos, ppc_reg[9]);
741 fprintf(outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[8]);
742 fprintf(outfile, "\t\"\\tmtctr %s\\n\"\n", ppc_reg[7]);
743
744 fprintf(outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[1]);
745 fprintf(outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
746 fprintf(outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[8], ppc_reg[1]);
747 fprintf(outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
748 fprintf(outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[9], ppc_reg[1]);
749 fprintf(outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
750 fprintf(outfile, "\t\"\\tbctr\\n");
751 #elif defined(__ALPHA__)
752 fprintf( outfile, "\tlda $0,imports\\n\"\n" );
753 fprintf( outfile, "\t\"\\tlda $0,%d($0)\\n\"\n", pos);
754 fprintf( outfile, "\t\"\\tjmp $31,($0)\\n" );
755 #else
756 #error You need to define import thunks for your architecture!
757 #endif
758 #endif /*__REACTOS__*/
759 fprintf( outfile, "\"\n" );
760 output_function_size( outfile, name );
761 }
762 pos += 4;
763 }
764 output_function_size( outfile, import_thunks );
765 fprintf( outfile, " \".text\");\n#ifndef __GNUC__\n}\n#endif\n\n" );
766
767 done:
768 return nb_imm;
769 }
770
771 /* output the delayed import table of a Win32 module */
772 static int output_delayed_imports( FILE *outfile, const DLLSPEC *spec )
773 {
774 int i, idx, j, pos;
775 static const char delayed_import_loaders[] = "__wine_spec_delayed_import_loaders";
776 static const char delayed_import_thunks[] = "__wine_spec_delayed_import_thunks";
777
778 if (!nb_delayed) goto done;
779
780 for (i = 0; i < nb_imports; i++)
781 {
782 if (!dll_imports[i]->delay) continue;
783 fprintf( outfile, "static void *__wine_delay_imp_%d_hmod;\n", i);
784 for (j = 0; j < dll_imports[i]->nb_imports; j++)
785 {
786 ORDDEF *odp = dll_imports[i]->imports[j];
787 const char *name = odp->name ? odp->name : odp->export_name;
788 fprintf( outfile, "void __wine_delay_imp_%d_%s();\n", i, name );
789 }
790 }
791 fprintf( outfile, "\n" );
792 fprintf( outfile, "static struct {\n" );
793 fprintf( outfile, " struct ImgDelayDescr {\n" );
794 fprintf( outfile, " unsigned int grAttrs;\n" );
795 fprintf( outfile, " const char *szName;\n" );
796 fprintf( outfile, " void **phmod;\n" );
797 fprintf( outfile, " void **pIAT;\n" );
798 fprintf( outfile, " const char **pINT;\n" );
799 fprintf( outfile, " void* pBoundIAT;\n" );
800 fprintf( outfile, " void* pUnloadIAT;\n" );
801 fprintf( outfile, " unsigned long dwTimeStamp;\n" );
802 fprintf( outfile, " } imp[%d];\n", nb_delayed );
803 fprintf( outfile, " void *IAT[%d];\n", total_delayed );
804 fprintf( outfile, " const char *INT[%d];\n", total_delayed );
805 fprintf( outfile, "} delay_imports = {\n" );
806 fprintf( outfile, " {\n" );
807 for (i = j = 0; i < nb_imports; i++)
808 {
809 if (!dll_imports[i]->delay) continue;
810 fprintf( outfile, " { 0, \"%s\", &__wine_delay_imp_%d_hmod, &delay_imports.IAT[%d], &delay_imports.INT[%d], 0, 0, 0 },\n",
811 dll_imports[i]->spec->file_name, i, j, j );
812 j += dll_imports[i]->nb_imports;
813 }
814 fprintf( outfile, " },\n {\n" );
815 for (i = 0; i < nb_imports; i++)
816 {
817 if (!dll_imports[i]->delay) continue;
818 fprintf( outfile, " /* %s */\n", dll_imports[i]->spec->file_name );
819 for (j = 0; j < dll_imports[i]->nb_imports; j++)
820 {
821 ORDDEF *odp = dll_imports[i]->imports[j];
822 const char *name = odp->name ? odp->name : odp->export_name;
823 fprintf( outfile, " &__wine_delay_imp_%d_%s,\n", i, name );
824 }
825 }
826 fprintf( outfile, " },\n {\n" );
827 for (i = 0; i < nb_imports; i++)
828 {
829 if (!dll_imports[i]->delay) continue;
830 fprintf( outfile, " /* %s */\n", dll_imports[i]->spec->file_name );
831 for (j = 0; j < dll_imports[i]->nb_imports; j++)
832 {
833 ORDDEF *odp = dll_imports[i]->imports[j];
834 if (!odp->name)
835 fprintf( outfile, " (char *)%d,\n", odp->ordinal );
836 else
837 fprintf( outfile, " \"%s\",\n", odp->name );
838 }
839 }
840 fprintf( outfile, " }\n};\n\n" );
841
842 /* check if there's some stub defined. if so, exception struct
843 * is already defined, so don't emit it twice
844 */
845 for (i = 0; i < spec->nb_entry_points; i++) if (spec->entry_points[i].type == TYPE_STUB) break;
846
847 if (i == spec->nb_entry_points) {
848 fprintf( outfile, "struct exc_record {\n" );
849 fprintf( outfile, " unsigned int code, flags;\n" );
850 fprintf( outfile, " void *rec, *addr;\n" );
851 fprintf( outfile, " unsigned int params;\n" );
852 fprintf( outfile, " const void *info[15];\n" );
853 fprintf( outfile, "};\n\n" );
854 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n" );
855 }
856
857 fprintf( outfile, "extern void * __stdcall LoadLibraryA(const char*);\n");
858 fprintf( outfile, "extern void * __stdcall GetProcAddress(void *, const char*);\n");
859 fprintf( outfile, "\n" );
860
861 fprintf( outfile, "void *__stdcall __wine_delay_load( int idx_nr )\n" );
862 fprintf( outfile, "{\n" );
863 fprintf( outfile, " int idx = idx_nr >> 16, nr = idx_nr & 0xffff;\n" );
864 fprintf( outfile, " struct ImgDelayDescr *imd = delay_imports.imp + idx;\n" );
865 fprintf( outfile, " void **pIAT = imd->pIAT + nr;\n" );
866 fprintf( outfile, " const char** pINT = imd->pINT + nr;\n" );
867 fprintf( outfile, " void *fn;\n\n" );
868
869 fprintf( outfile, " if (!*imd->phmod) *imd->phmod = LoadLibraryA(imd->szName);\n" );
870 fprintf( outfile, " if (*imd->phmod && (fn = GetProcAddress(*imd->phmod, *pINT)))\n");
871 fprintf( outfile, " /* patch IAT with final value */\n" );
872 fprintf( outfile, " return *pIAT = fn;\n" );
873 fprintf( outfile, " else {\n");
874 fprintf( outfile, " struct exc_record rec;\n" );
875 fprintf( outfile, " rec.code = 0x80000100;\n" );
876 fprintf( outfile, " rec.flags = 1;\n" );
877 fprintf( outfile, " rec.rec = 0;\n" );
878 fprintf( outfile, " rec.params = 2;\n" );
879 fprintf( outfile, " rec.info[0] = imd->szName;\n" );
880 fprintf( outfile, " rec.info[1] = *pINT;\n" );
881 fprintf( outfile, "#ifdef __GNUC__\n" );
882 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
883 fprintf( outfile, "#else\n" );
884 fprintf( outfile, " rec.addr = 0;\n" );
885 fprintf( outfile, "#endif\n" );
886 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n" );
887 fprintf( outfile, " return 0; /* shouldn't go here */\n" );
888 fprintf( outfile, " }\n}\n\n" );
889
890 fprintf( outfile, "#ifndef __GNUC__\n" );
891 fprintf( outfile, "static void __asm__dummy_delay_import(void) {\n" );
892 fprintf( outfile, "#endif\n" );
893
894 fprintf( outfile, "asm(\".align %d\\n\"\n", get_alignment(8) );
895 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\"\n", delayed_import_loaders);
896 fprintf( outfile, " \"\\t" __ASM_FUNC("__wine_delay_load_asm") "\\n\"\n" );
897 fprintf( outfile, " \"" __ASM_NAME("__wine_delay_load_asm") ":\\n\"\n" );
898 #if defined(__i386__)
899 fprintf( outfile, " \"\\tpushl %%ecx\\n\\tpushl %%edx\\n\\tpushl %%eax\\n\"\n" );
900 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
901 fprintf( outfile, " \"\\tpopl %%edx\\n\\tpopl %%ecx\\n\\tjmp *%%eax\\n\"\n" );
902 #elif defined(__sparc__)
903 fprintf( outfile, " \"\\tsave %%sp, -96, %%sp\\n\"\n" );
904 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
905 fprintf( outfile, " \"\\tmov %%g1, %%o0\\n\"\n" );
906 fprintf( outfile, " \"\\tjmp %%o0\\n\\trestore\\n\"\n" );
907 #elif defined(__powerpc__)
908 # if defined(__APPLE__)
909 /* On darwin an extra 56 bytes must be allowed for the linkage area+param area */
910 # define extra_stack_storage 56
911 # else
912 # define extra_stack_storage 0
913 # endif
914 /* Save all callee saved registers into a stackframe. */
915 fprintf( outfile, " \"\\tstwu %s, -%d(%s)\\n\"\n",ppc_reg[1], 48+extra_stack_storage, ppc_reg[1]);
916 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[3], 4+extra_stack_storage, ppc_reg[1]);
917 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[4], 8+extra_stack_storage, ppc_reg[1]);
918 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[5], 12+extra_stack_storage, ppc_reg[1]);
919 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[6], 16+extra_stack_storage, ppc_reg[1]);
920 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[7], 20+extra_stack_storage, ppc_reg[1]);
921 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[8], 24+extra_stack_storage, ppc_reg[1]);
922 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[9], 28+extra_stack_storage, ppc_reg[1]);
923 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[10],32+extra_stack_storage, ppc_reg[1]);
924 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[11],36+extra_stack_storage, ppc_reg[1]);
925 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[12],40+extra_stack_storage, ppc_reg[1]);
926
927 /* r0 -> r3 (arg1) */
928 fprintf( outfile, " \"\\tmr %s, %s\\n\"\n", ppc_reg[3], ppc_reg[0]);
929
930 /* save return address */
931 fprintf( outfile, " \"\\tmflr %s\\n\"\n", ppc_reg[0]);
932 fprintf( outfile, " \"\\tstw %s, %d(%s)\\n\"\n", ppc_reg[0], 44+extra_stack_storage, ppc_reg[1]);
933
934 /* Call the __wine_delay_load function, arg1 is arg1. */
935 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_delay_load") "\\n\"\n");
936
937 /* Load return value from call into ctr register */
938 fprintf( outfile, " \"\\tmtctr %s\\n\"\n", ppc_reg[3]);
939
940 /* restore all saved registers and drop stackframe. */
941 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[3], 4+extra_stack_storage, ppc_reg[1]);
942 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[4], 8+extra_stack_storage, ppc_reg[1]);
943 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[5], 12+extra_stack_storage, ppc_reg[1]);
944 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[6], 16+extra_stack_storage, ppc_reg[1]);
945 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[7], 20+extra_stack_storage, ppc_reg[1]);
946 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[8], 24+extra_stack_storage, ppc_reg[1]);
947 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[9], 28+extra_stack_storage, ppc_reg[1]);
948 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[10],32+extra_stack_storage, ppc_reg[1]);
949 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[11],36+extra_stack_storage, ppc_reg[1]);
950 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[12],40+extra_stack_storage, ppc_reg[1]);
951
952 /* Load return value from call into return register */
953 fprintf( outfile, " \"\\tlwz %s, %d(%s)\\n\"\n", ppc_reg[0], 44+extra_stack_storage, ppc_reg[1]);
954 fprintf( outfile, " \"\\tmtlr %s\\n\"\n", ppc_reg[0]);
955 fprintf( outfile, " \"\\taddi %s, %s, %d\\n\"\n", ppc_reg[1], ppc_reg[1], 48+extra_stack_storage);
956
957 /* branch to ctr register. */
958 fprintf( outfile, " \"bctr\\n\"\n");
959 #elif defined(__ALPHA__)
960 fprintf( outfile, " \"\\tjsr $26,__wine_delay_load\\n\"\n" );
961 fprintf( outfile, " \"\\tjmp $31,($0)\\n\"\n" );
962 #else
963 #error You need to defined delayed import thunks for your architecture!
964 #endif
965 output_function_size( outfile, "__wine_delay_load_asm" );
966
967 for (i = idx = 0; i < nb_imports; i++)
968 {
969 if (!dll_imports[i]->delay) continue;
970 for (j = 0; j < dll_imports[i]->nb_imports; j++)
971 {
972 char buffer[128];
973 ORDDEF *odp = dll_imports[i]->imports[j];
974 const char *name = odp->name ? odp->name : odp->export_name;
975
976 sprintf( buffer, "__wine_delay_imp_%d_%s", i, name );
977 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", buffer );
978 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\"\n", buffer );
979 #if defined(__i386__)
980 fprintf( outfile, " \"\\tmovl $%d, %%eax\\n\"\n", (idx << 16) | j );
981 fprintf( outfile, " \"\\tjmp __wine_delay_load_asm\\n\"\n" );
982 #elif defined(__sparc__)
983 fprintf( outfile, " \"\\tset %d, %%g1\\n\"\n", (idx << 16) | j );
984 fprintf( outfile, " \"\\tb,a __wine_delay_load_asm\\n\"\n" );
985 #elif defined(__powerpc__)
986 #ifdef __APPLE__
987 /* On Darwin we can use r0 and r2 */
988 /* Upper part in r2 */
989 fprintf( outfile, " \"\\tlis %s, %d\\n\"\n", ppc_reg[2], idx);
990 /* Lower part + r2 -> r0, Note we can't use r0 directly */
991 fprintf( outfile, " \"\\taddi %s, %s, %d\\n\"\n", ppc_reg[0], ppc_reg[2], j);
992 fprintf( outfile, " \"\\tb " __ASM_NAME("__wine_delay_load_asm") "\\n\"\n");
993 #else /* __APPLE__ */
994 /* On linux we can't use r2 since r2 is not a scratch register (hold the TOC) */
995 /* Save r13 on the stack */
996 fprintf( outfile, " \"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
997 fprintf( outfile, " \"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[13], ppc_reg[1]);
998 /* Upper part in r13 */
999 fprintf( outfile, " \"\\tlis %s, %d\\n\"\n", ppc_reg[13], idx);
1000 /* Lower part + r13 -> r0, Note we can't use r0 directly */
1001 fprintf( outfile, " \"\\taddi %s, %s, %d\\n\"\n", ppc_reg[0], ppc_reg[13], j);
1002 /* Restore r13 */
1003 fprintf( outfile, " \"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[13], ppc_reg[1]);
1004 fprintf( outfile, " \"\\taddic %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1005 fprintf( outfile, " \"\\tb " __ASM_NAME("__wine_delay_load_asm") "\\n\"\n");
1006 #endif /* __APPLE__ */
1007 #elif defined(__ALPHA__)
1008 fprintf( outfile, " \"\\tlda $0,%d($31)\\n\"\n", j);
1009 fprintf( outfile, " \"\\tldah $0,%d($0)\\n\"\n", idx);
1010 fprintf( outfile, " \"\\tjmp $31,__wine_delay_load_asm\\n\"\n" );
1011 #else
1012 #error You need to defined delayed import thunks for your architecture!
1013 #endif
1014 output_function_size( outfile, name );
1015 }
1016 idx++;
1017 }
1018 output_function_size( outfile, delayed_import_loaders );
1019
1020 fprintf( outfile, "\n \".align %d\\n\"\n", get_alignment(8) );
1021 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\"\n", delayed_import_thunks);
1022 pos = nb_delayed * 32;
1023 for (i = 0; i < nb_imports; i++)
1024 {
1025 if (!dll_imports[i]->delay) continue;
1026 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
1027 {
1028 ORDDEF *odp = dll_imports[i]->imports[j];
1029 const char *name = odp->name ? odp->name : odp->export_name;
1030
1031 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", name );
1032 fprintf( outfile, " \"\\t.globl " __ASM_NAME("%s") "\\n\"\n", name );
1033 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\\t\"", name );
1034 #if defined(__i386__)
1035 if (strstr( name, "__wine_call_from_16" ))
1036 fprintf( outfile, "\".byte 0x2e\\n\\tjmp *(delay_imports+%d)\\n\\tnop\\n\"", pos );
1037 else
1038 fprintf( outfile, "\"jmp *(delay_imports+%d)\\n\\tmovl %%esi,%%esi\\n\"", pos );
1039 #elif defined(__sparc__)
1040 if ( !UsePIC )
1041 {
1042 fprintf( outfile, "\"sethi %%hi(delay_imports+%d), %%g1\\n\\t\"", pos );
1043 fprintf( outfile, "\"ld [%%g1+%%lo(delay_imports+%d)], %%g1\\n\\t\"", pos );
1044 fprintf( outfile, "\"jmp %%g1\\n\\tnop\\n\"" );
1045 }
1046 else
1047 {
1048 /* Hmpf. Stupid sparc assembler always interprets global variable
1049 names as GOT offsets, so we have to do it the long way ... */
1050 fprintf( outfile, "\"save %%sp, -96, %%sp\\n\"" );
1051 fprintf( outfile, "\"0:\\tcall 1f\\n\\tnop\\n\"" );
1052 fprintf( outfile, "\"1:\\tsethi %%hi(delay_imports+%d-0b), %%g1\\n\\t\"", pos );
1053 fprintf( outfile, "\"or %%g1, %%lo(delay_imports+%d-0b), %%g1\\n\\t\"", pos );
1054 fprintf( outfile, "\"ld [%%g1+%%o7], %%g1\\n\\t\"" );
1055 fprintf( outfile, "\"jmp %%g1\\n\\trestore\\n\"" );
1056 }
1057
1058 #elif defined(__powerpc__)
1059 fprintf( outfile, "\t\"addi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1060 fprintf( outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[9], ppc_reg[1]);
1061 fprintf( outfile, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1062 fprintf( outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[8], ppc_reg[1]);
1063 fprintf( outfile, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1064 fprintf( outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[1]);
1065
1066 fprintf( outfile, "\t\"\\tlis %s, " ppc_high(__ASM_NAME("delay_imports") "+ %d") "\\n\"\n", ppc_reg[9], pos);
1067 fprintf( outfile, "\t\"\\tla %s, " ppc_low (__ASM_NAME("delay_imports") "+ %d") "(%s)\\n\"\n", ppc_reg[8], pos, ppc_reg[9]);
1068 fprintf( outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[8]);
1069 fprintf( outfile, "\t\"\\tmtctr %s\\n\"\n", ppc_reg[7]);
1070
1071 fprintf( outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[1]);
1072 fprintf( outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1073 fprintf( outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[8], ppc_reg[1]);
1074 fprintf( outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1075 fprintf( outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[9], ppc_reg[1]);
1076 fprintf( outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1077 fprintf( outfile, "\t\"\\tbctr\\n\"");
1078 #elif defined(__ALPHA__)
1079 fprintf( outfile, "\t\"lda $0,delay_imports\\n\"\n" );
1080 fprintf( outfile, "\t\"\\tlda $0,%d($0)\\n\"\n", pos);
1081 fprintf( outfile, "\t\"\\tjmp $31,($0)\\n\"" );
1082 #else
1083 #error You need to define delayed import thunks for your architecture!
1084 #endif
1085 fprintf( outfile, "\n" );
1086 output_function_size( outfile, name );
1087 }
1088 }
1089 output_function_size( outfile, delayed_import_thunks );
1090 fprintf( outfile, ");\n" );
1091 fprintf( outfile, "#ifndef __GNUC__\n" );
1092 fprintf( outfile, "}\n" );
1093 fprintf( outfile, "#endif\n" );
1094 fprintf( outfile, "\n" );
1095
1096 done:
1097 return nb_delayed;
1098 }
1099
1100 /* output the import and delayed import tables of a Win32 module
1101 * returns number of DLLs exported in 'immediate' mode
1102 */
1103 int output_imports( FILE *outfile, DLLSPEC *spec )
1104 {
1105 output_delayed_imports( outfile, spec );
1106 return output_immediate_imports( outfile );
1107 }