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