4 * Copyright 2000, 2004 Alexandre Julliard
5 * Copyright 2000 Eric Pouech
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.
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.
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
23 #include "wine/port.h"
37 DLLSPEC
*spec
; /* description of the imported dll */
38 int delay
; /* delay or not dll loading ? */
39 ORDDEF
**exports
; /* functions exported from this dll */
40 int nb_exports
; /* number of exported functions */
41 ORDDEF
**imports
; /* functions we want to import from this dll */
42 int nb_imports
; /* number of imported functions */
45 static char **undef_symbols
; /* list of undefined symbols */
46 static int nb_undef_symbols
= -1;
47 static int undef_size
;
49 static char **ignore_symbols
; /* list of symbols to ignore */
50 static int nb_ignore_symbols
;
51 static int ignore_size
;
53 static char *ld_tmp_file
; /* ld temp file name */
55 static struct import
**dll_imports
= NULL
;
56 static int nb_imports
= 0; /* number of imported dlls (delayed or not) */
57 static int nb_delayed
= 0; /* number of delayed dlls */
58 static int total_imports
= 0; /* total number of imported functions */
59 static int total_delayed
= 0; /* total number of imported functions in delayed DLLs */
61 /* list of symbols that are ignored by default */
62 static const char * const default_ignored_symbols
[] =
114 # define ppc_high(mem) "ha16(" mem ")"
115 # define ppc_low(mem) "lo16(" mem ")"
116 static const char * const ppc_reg
[32] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
117 "r8", "r9", "r10","r11","r12","r13","r14","r15",
118 "r16","r17","r18","r19","r20","r21","r22","r23",
119 "r24","r25","r26","r27","r28","r29","r30","r31" };
120 # else /* __APPLE__ */
121 # define ppc_high(mem) "(" mem ")@hi"
122 # define ppc_low(mem) "(" mem ")@l"
123 static const char * const ppc_reg
[32] = { "0", "1", "2", "3", "4", "5", "6", "7",
124 "8", "9", "10","11","12","13","14","15",
125 "16","17","18","19","20","21","22","23",
126 "24","25","26","27","28","29","30","31" };
127 # endif /* __APPLE__ */
128 #endif /* __powerpc__ */
130 /* compare function names; helper for resolve_imports */
131 static int name_cmp( const void *name
, const void *entry
)
133 return strcmp( *(char **)name
, *(char **)entry
);
136 /* compare function names; helper for resolve_imports */
137 static int func_cmp( const void *func1
, const void *func2
)
139 const ORDDEF
*odp1
= *(const ORDDEF
**)func1
;
140 const ORDDEF
*odp2
= *(const ORDDEF
**)func2
;
141 return strcmp( odp1
->name
? odp1
->name
: odp1
->export_name
,
142 odp2
->name
? odp2
->name
: odp2
->export_name
);
145 /* locate a symbol in a (sorted) list */
146 inline static const char *find_symbol( const char *name
, char **table
, int size
)
151 res
= bsearch( &name
, table
, size
, sizeof(*table
), name_cmp
);
154 return res
? *res
: NULL
;
157 /* locate an export in a (sorted) export list */
158 inline static ORDDEF
*find_export( const char *name
, ORDDEF
**table
, int size
)
160 ORDDEF func
, *odp
, **res
= NULL
;
162 func
.name
= (char *)name
;
165 if (table
) res
= bsearch( &odp
, table
, size
, sizeof(*table
), func_cmp
);
166 return res
? *res
: NULL
;
169 /* sort a symbol table */
170 inline static void sort_symbols( char **table
, int size
)
173 qsort( table
, size
, sizeof(*table
), name_cmp
);
176 /* free an import structure */
177 static void free_imports( struct import
*imp
)
179 free( imp
->exports
);
180 free( imp
->imports
);
181 free_dll_spec( imp
->spec
);
185 /* remove the temp file at exit */
186 static void remove_ld_tmp_file(void)
188 if (ld_tmp_file
) unlink( ld_tmp_file
);
191 /* check whether a given dll has already been imported */
192 static int is_already_imported( const char *name
)
196 for (i
= 0; i
< nb_imports
; i
++)
198 if (!strcmp( dll_imports
[i
]->spec
->file_name
, name
)) return 1;
203 /* open the .so library for a given dll in a specified path */
204 static char *try_library_path( const char *path
, const char *name
)
209 buffer
= xmalloc( strlen(path
) + strlen(name
) + 9 );
210 sprintf( buffer
, "%s/lib%s.def", path
, name
);
212 /* check if the file exists */
213 if ((fd
= open( buffer
, O_RDONLY
)) != -1)
222 /* open the .so library for a given dll */
223 static char *open_library( const char *name
)
228 for (i
= 0; i
< nb_lib_paths
; i
++)
230 if ((fullname
= try_library_path( lib_path
[i
], name
))) return fullname
;
232 if (!(fullname
= try_library_path( ".", name
)))
233 fatal_error( "could not open .def file for %s\n", name
);
237 /* read in the list of exported symbols of an import library */
238 static int read_import_lib( const char *name
, struct import
*imp
)
243 DLLSPEC
*spec
= imp
->spec
;
248 fullname
= open_library( name
);
249 f
= open_input_file( NULL
, fullname
);
252 ret
= parse_def_file( f
, spec
);
253 close_input_file( f
);
255 if (is_already_imported( spec
->file_name
)) return 0;
257 imp
->exports
= xmalloc( spec
->nb_entry_points
* sizeof(*imp
->exports
) );
259 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
261 ORDDEF
*odp
= &spec
->entry_points
[i
];
263 if (odp
->type
!= TYPE_STDCALL
&& odp
->type
!= TYPE_CDECL
) continue;
264 if (odp
->flags
& FLAG_PRIVATE
) continue;
265 imp
->exports
[imp
->nb_exports
++] = odp
;
267 imp
->exports
= xrealloc( imp
->exports
, imp
->nb_exports
* sizeof(*imp
->exports
) );
269 qsort( imp
->exports
, imp
->nb_exports
, sizeof(*imp
->exports
), func_cmp
);
273 /* add a dll to the list of imports */
274 void add_import_dll( const char *name
, int delay
)
279 fullname
= xmalloc( strlen(name
) + 5 );
280 strcpy( fullname
, name
);
281 if (!strchr( fullname
, '.' )) strcat( fullname
, ".dll" );
283 /* check if we already imported it */
284 if (is_already_imported( fullname
))
290 imp
= xmalloc( sizeof(*imp
) );
291 imp
->spec
= alloc_dll_spec();
292 imp
->spec
->file_name
= fullname
;
296 if (delay
) nb_delayed
++;
298 if (read_import_lib( name
, imp
))
300 dll_imports
= xrealloc( dll_imports
, (nb_imports
+1) * sizeof(*dll_imports
) );
301 dll_imports
[nb_imports
++] = imp
;
306 if (nb_errors
) exit(1);
310 /* remove an imported dll, based on its index in the dll_imports array */
311 static void remove_import_dll( int index
)
313 struct import
*imp
= dll_imports
[index
];
315 memmove( &dll_imports
[index
], &dll_imports
[index
+1], sizeof(imp
) * (nb_imports
- index
- 1) );
317 if (imp
->delay
) nb_delayed
--;
321 /* initialize the list of ignored symbols */
322 static void init_ignored_symbols(void)
326 nb_ignore_symbols
= sizeof(default_ignored_symbols
)/sizeof(default_ignored_symbols
[0]);
327 ignore_size
= nb_ignore_symbols
+ 32;
328 ignore_symbols
= xmalloc( ignore_size
* sizeof(*ignore_symbols
) );
329 for (i
= 0; i
< nb_ignore_symbols
; i
++)
330 ignore_symbols
[i
] = xstrdup( default_ignored_symbols
[i
] );
333 /* add a symbol to the ignored symbol list */
334 /* if the name starts with '-' the symbol is removed instead */
335 void add_ignore_symbol( const char *name
)
339 if (!ignore_symbols
) init_ignored_symbols(); /* first time around, fill list with defaults */
341 if (name
[0] == '-') /* remove it */
343 if (!name
[1]) /* remove everything */
345 for (i
= 0; i
< nb_ignore_symbols
; i
++) free( ignore_symbols
[i
] );
346 nb_ignore_symbols
= 0;
350 for (i
= 0; i
< nb_ignore_symbols
; i
++)
352 if (!strcmp( ignore_symbols
[i
], name
+1 ))
354 free( ignore_symbols
[i
] );
355 memmove( &ignore_symbols
[i
], &ignore_symbols
[i
+1], nb_ignore_symbols
- i
- 1 );
363 if (nb_ignore_symbols
== ignore_size
)
366 ignore_symbols
= xrealloc( ignore_symbols
, ignore_size
* sizeof(*ignore_symbols
) );
368 ignore_symbols
[nb_ignore_symbols
++] = xstrdup( name
);
372 /* add a function to the list of imports from a given dll */
373 static void add_import_func( struct import
*imp
, ORDDEF
*func
)
375 imp
->imports
= xrealloc( imp
->imports
, (imp
->nb_imports
+1) * sizeof(*imp
->imports
) );
376 imp
->imports
[imp
->nb_imports
++] = func
;
378 if (imp
->delay
) total_delayed
++;
381 /* add a symbol to the undef list */
382 inline static void add_undef_symbol( const char *name
)
384 if (nb_undef_symbols
== undef_size
)
387 undef_symbols
= xrealloc( undef_symbols
, undef_size
* sizeof(*undef_symbols
) );
389 undef_symbols
[nb_undef_symbols
++] = xstrdup( name
);
392 /* remove all the holes in the undefined symbol list; return the number of removed symbols */
393 static int remove_symbol_holes(void)
396 for (i
= off
= 0; i
< nb_undef_symbols
; i
++)
398 if (!undef_symbols
[i
]) off
++;
399 else undef_symbols
[i
- off
] = undef_symbols
[i
];
401 nb_undef_symbols
-= off
;
405 /* add a symbol to the extra list, but only if needed */
406 static int add_extra_symbol( const char **extras
, int *count
, const char *name
, const DLLSPEC
*spec
)
410 if (!find_symbol( name
, undef_symbols
, nb_undef_symbols
))
412 /* check if the symbol is being exported by this dll */
413 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
415 ORDDEF
*odp
= &spec
->entry_points
[i
];
416 if (odp
->type
== TYPE_STDCALL
||
417 odp
->type
== TYPE_CDECL
||
418 odp
->type
== TYPE_VARARGS
||
419 odp
->type
== TYPE_EXTERN
)
421 if (odp
->name
&& !strcmp( odp
->name
, name
)) return 0;
424 extras
[*count
] = name
;
430 /* add the extra undefined symbols that will be contained in the generated spec file itself */
431 static void add_extra_undef_symbols( const DLLSPEC
*spec
)
433 const char *extras
[10];
434 int i
, count
= 0, nb_stubs
= 0, nb_regs
= 0;
435 int kernel_imports
= 0, ntdll_imports
= 0;
437 sort_symbols( undef_symbols
, nb_undef_symbols
);
439 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
441 ORDDEF
*odp
= &spec
->entry_points
[i
];
442 if (odp
->type
== TYPE_STUB
) nb_stubs
++;
443 if (odp
->flags
& FLAG_REGISTER
) nb_regs
++;
446 /* add symbols that will be contained in the spec file itself */
450 case SPEC_MODE_NATIVE
:
452 case SPEC_MODE_GUIEXE
:
453 kernel_imports
+= add_extra_symbol( extras
, &count
, "GetCommandLineA", spec
);
454 kernel_imports
+= add_extra_symbol( extras
, &count
, "GetStartupInfoA", spec
);
455 kernel_imports
+= add_extra_symbol( extras
, &count
, "GetModuleHandleA", spec
);
457 case SPEC_MODE_CUIEXE
:
458 kernel_imports
+= add_extra_symbol( extras
, &count
, "ExitProcess", spec
);
460 case SPEC_MODE_GUIEXE_UNICODE
:
461 kernel_imports
+= add_extra_symbol( extras
, &count
, "GetCommandLineA", spec
);
462 kernel_imports
+= add_extra_symbol( extras
, &count
, "GetStartupInfoA", spec
);
463 kernel_imports
+= add_extra_symbol( extras
, &count
, "GetModuleHandleA", spec
);
465 case SPEC_MODE_CUIEXE_UNICODE
:
466 kernel_imports
+= add_extra_symbol( extras
, &count
, "ExitProcess", spec
);
471 kernel_imports
+= add_extra_symbol( extras
, &count
, "LoadLibraryA", spec
);
472 kernel_imports
+= add_extra_symbol( extras
, &count
, "GetProcAddress", spec
);
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
);
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 );
485 for (i
= 0; i
< count
; i
++) add_undef_symbol( extras
[i
] );
486 sort_symbols( undef_symbols
, nb_undef_symbols
);
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
)
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
;
499 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
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 */
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
)
518 if (output_file_name
&& output_file_name
[0])
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" );
524 else ld_tmp_file
= xstrdup( "/tmp/winebuild.tmp.XXXXXX.o" );
526 if ((fd
= mkstemps( ld_tmp_file
, 2 ) == -1)) fatal_error( "could not generate a temp file\n" );
528 atexit( remove_ld_tmp_file
);
530 for (i
= 0; argv
[i
]; i
++) len
+= strlen(argv
[i
]) + 1;
531 cmd
= xmalloc( len
+ strlen(ld_tmp_file
) + 10 );
532 sprintf( cmd
, "ld -r -o %s", ld_tmp_file
);
533 for (i
= 0; argv
[i
]; i
++) sprintf( cmd
+ strlen(cmd
), " %s", argv
[i
] );
535 if (err
) fatal_error( "ld -r failed with status %d\n", err
);
540 /* read in the list of undefined symbols */
541 void read_undef_symbols( char **argv
)
548 if (!argv
[0]) return;
550 undef_size
= nb_undef_symbols
= 0;
552 /* if we have multiple object files, link them together */
553 if (argv
[1]) name
= ldcombine_files( argv
);
556 sprintf( buffer
, "nm -u %s", name
);
557 if (!(f
= popen( buffer
, "r" )))
558 fatal_error( "Cannot execute '%s'\n", buffer
);
560 while (fgets( buffer
, sizeof(buffer
), f
))
562 char *p
= buffer
+ strlen(buffer
) - 1;
563 if (p
< buffer
) continue;
564 if (*p
== '\n') *p
-- = 0;
566 while (*p
== ' ') p
++;
567 if (p
[0] == 'U' && p
[1] == ' ' && p
[2]) p
+= 2;
568 add_undef_symbol( p
);
570 if ((err
= pclose( f
))) warning( "nm -u %s error %d\n", name
, err
);
573 static void remove_ignored_symbols(void)
577 if (!ignore_symbols
) init_ignored_symbols();
578 sort_symbols( ignore_symbols
, nb_ignore_symbols
);
579 for (i
= 0; i
< nb_undef_symbols
; i
++)
581 if (find_symbol( undef_symbols
[i
], ignore_symbols
, nb_ignore_symbols
))
583 free( undef_symbols
[i
] );
584 undef_symbols
[i
] = NULL
;
587 remove_symbol_holes();
590 /* resolve the imports for a Win32 module */
591 int resolve_imports( DLLSPEC
*spec
)
595 if (nb_undef_symbols
== -1) return 0; /* no symbol file specified */
597 add_extra_undef_symbols( spec
);
598 remove_ignored_symbols();
600 for (i
= 0; i
< nb_imports
; i
++)
602 struct import
*imp
= dll_imports
[i
];
604 for (j
= 0; j
< nb_undef_symbols
; j
++)
606 ORDDEF
*odp
= find_export( undef_symbols
[j
], imp
->exports
, imp
->nb_exports
);
609 add_import_func( imp
, odp
);
610 free( undef_symbols
[j
] );
611 undef_symbols
[j
] = NULL
;
614 /* remove all the holes in the undef symbols list */
615 if (!remove_symbol_holes() && check_unused( imp
, spec
))
617 /* the dll is not used, get rid of it */
618 warning( "%s imported but no symbols used\n", imp
->spec
->file_name
);
619 remove_import_dll( i
);
626 /* output the import table of a Win32 module */
627 static int output_immediate_imports( FILE *outfile
)
630 int nb_imm
= nb_imports
- nb_delayed
;
632 if (!nb_imm
) goto done
;
634 /* main import header */
636 fprintf( outfile
, "\nstatic struct {\n" );
637 fprintf( outfile
, " struct {\n" );
638 fprintf( outfile
, " void *OriginalFirstThunk;\n" );
639 fprintf( outfile
, " unsigned int TimeDateStamp;\n" );
640 fprintf( outfile
, " unsigned int ForwarderChain;\n" );
641 fprintf( outfile
, " const char *Name;\n" );
642 fprintf( outfile
, " void *FirstThunk;\n" );
643 fprintf( outfile
, " } imp[%d];\n", nb_imm
+1 );
644 fprintf( outfile
, " const char *data[%d];\n",
645 total_imports
- total_delayed
+ nb_imm
);
646 fprintf( outfile
, "} imports = {\n {\n" );
650 for (i
= j
= 0; i
< nb_imports
; i
++)
652 if (dll_imports
[i
]->delay
) continue;
653 fprintf( outfile
, " { 0, 0, 0, \"%s\", &imports.data[%d] },\n",
654 dll_imports
[i
]->spec
->file_name
, j
);
655 j
+= dll_imports
[i
]->nb_imports
+ 1;
658 fprintf( outfile
, " { 0, 0, 0, 0, 0 },\n" );
659 fprintf( outfile
, " },\n {\n" );
661 /* list of imported functions */
663 for (i
= 0; i
< nb_imports
; i
++)
665 if (dll_imports
[i
]->delay
) continue;
666 fprintf( outfile
, " /* %s */\n", dll_imports
[i
]->spec
->file_name
);
667 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++)
669 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
670 if (!(odp
->flags
& FLAG_NONAME
))
672 unsigned short ord
= odp
->ordinal
;
673 fprintf( outfile
, " \"\\%03o\\%03o%s\",\n",
674 *(unsigned char *)&ord
, *((unsigned char *)&ord
+ 1), odp
->name
);
677 fprintf( outfile
, " (char *)%d,\n", odp
->ordinal
);
679 fprintf( outfile
, " 0,\n" );
681 fprintf( outfile
, " }\n};\n\n" );
683 /* thunks for imported functions */
685 fprintf( outfile
, "#ifndef __GNUC__\nstatic void __asm__dummy_import(void) {\n#endif\n\n" );
686 pos
= 20 * (nb_imm
+ 1); /* offset of imports.data from start of imports */
687 fprintf( outfile
, "asm(\".data\\n\\t.align %d\\n\"\n", get_alignment(8) );
688 for (i
= 0; i
< nb_imports
; i
++)
690 if (dll_imports
[i
]->delay
) continue;
691 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++, pos
+= 4)
693 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
694 const char *name
= odp
->name
? odp
->name
: odp
->export_name
;
695 fprintf( outfile
, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", name
);
696 fprintf( outfile
, " \"\\t.globl " __ASM_NAME("%s") "\\n\"\n", name
);
697 fprintf( outfile
, " \"" __ASM_NAME("%s") ":\\n\\t", name
);
699 #if defined(__i386__)
700 if (strstr( name
, "__wine_call_from_16" ))
701 fprintf( outfile
, ".byte 0x2e\\n\\tjmp *(imports+%d)\\n\\tnop\\n", pos
);
703 fprintf( outfile
, "jmp *(imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos
);
704 #elif defined(__sparc__)
707 fprintf( outfile
, "sethi %%hi(imports+%d), %%g1\\n\\t", pos
);
708 fprintf( outfile
, "ld [%%g1+%%lo(imports+%d)], %%g1\\n\\t", pos
);
709 fprintf( outfile
, "jmp %%g1\\n\\tnop\\n" );
713 /* Hmpf. Stupid sparc assembler always interprets global variable
714 names as GOT offsets, so we have to do it the long way ... */
715 fprintf( outfile
, "save %%sp, -96, %%sp\\n" );
716 fprintf( outfile
, "0:\\tcall 1f\\n\\tnop\\n" );
717 fprintf( outfile
, "1:\\tsethi %%hi(imports+%d-0b), %%g1\\n\\t", pos
);
718 fprintf( outfile
, "or %%g1, %%lo(imports+%d-0b), %%g1\\n\\t", pos
);
719 fprintf( outfile
, "ld [%%g1+%%o7], %%g1\\n\\t" );
720 fprintf( outfile
, "jmp %%g1\\n\\trestore\\n" );
723 #elif defined(__powerpc__)
724 fprintf(outfile
, "\taddi %s, %s, -0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
725 fprintf(outfile
, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg
[9], ppc_reg
[1]);
726 fprintf(outfile
, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
727 fprintf(outfile
, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg
[8], ppc_reg
[1]);
728 fprintf(outfile
, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
729 fprintf(outfile
, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg
[7], ppc_reg
[1]);
731 fprintf(outfile
, "\t\"\\tlis %s, " ppc_high(__ASM_NAME("imports") "+ %d") "\\n\"\n", ppc_reg
[9], pos
);
732 fprintf(outfile
, "\t\"\\tla %s, " ppc_low (__ASM_NAME("imports") "+ %d") "(%s)\\n\"\n", ppc_reg
[8], pos
, ppc_reg
[9]);
733 fprintf(outfile
, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg
[7], ppc_reg
[8]);
734 fprintf(outfile
, "\t\"\\tmtctr %s\\n\"\n", ppc_reg
[7]);
736 fprintf(outfile
, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg
[7], ppc_reg
[1]);
737 fprintf(outfile
, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
738 fprintf(outfile
, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg
[8], ppc_reg
[1]);
739 fprintf(outfile
, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
740 fprintf(outfile
, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg
[9], ppc_reg
[1]);
741 fprintf(outfile
, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
742 fprintf(outfile
, "\t\"\\tbctr\\n");
744 #error You need to define import thunks for your architecture!
746 fprintf( outfile
, "\"\n" );
750 fprintf( outfile
, "\".text\");\n#ifndef __GNUC__\n}\n#endif\n\n" );
756 /* output the delayed import table of a Win32 module */
757 static int output_delayed_imports( FILE *outfile
, const DLLSPEC
*spec
)
761 if (!nb_delayed
) goto done
;
763 for (i
= 0; i
< nb_imports
; i
++)
765 if (!dll_imports
[i
]->delay
) continue;
766 fprintf( outfile
, "static void *__wine_delay_imp_%d_hmod;\n", i
);
767 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++)
769 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
770 const char *name
= odp
->name
? odp
->name
: odp
->export_name
;
771 fprintf( outfile
, "void __wine_delay_imp_%d_%s();\n", i
, name
);
774 fprintf( outfile
, "\n" );
775 fprintf( outfile
, "static struct {\n" );
776 fprintf( outfile
, " struct ImgDelayDescr {\n" );
777 fprintf( outfile
, " unsigned int grAttrs;\n" );
778 fprintf( outfile
, " const char *szName;\n" );
779 fprintf( outfile
, " void **phmod;\n" );
780 fprintf( outfile
, " void **pIAT;\n" );
781 fprintf( outfile
, " const char **pINT;\n" );
782 fprintf( outfile
, " void* pBoundIAT;\n" );
783 fprintf( outfile
, " void* pUnloadIAT;\n" );
784 fprintf( outfile
, " unsigned long dwTimeStamp;\n" );
785 fprintf( outfile
, " } imp[%d];\n", nb_delayed
);
786 fprintf( outfile
, " void *IAT[%d];\n", total_delayed
);
787 fprintf( outfile
, " const char *INT[%d];\n", total_delayed
);
788 fprintf( outfile
, "} delay_imports = {\n" );
789 fprintf( outfile
, " {\n" );
790 for (i
= j
= 0; i
< nb_imports
; i
++)
792 if (!dll_imports
[i
]->delay
) continue;
793 fprintf( outfile
, " { 0, \"%s\", &__wine_delay_imp_%d_hmod, &delay_imports.IAT[%d], &delay_imports.INT[%d], 0, 0, 0 },\n",
794 dll_imports
[i
]->spec
->file_name
, i
, j
, j
);
795 j
+= dll_imports
[i
]->nb_imports
;
797 fprintf( outfile
, " },\n {\n" );
798 for (i
= 0; i
< nb_imports
; i
++)
800 if (!dll_imports
[i
]->delay
) continue;
801 fprintf( outfile
, " /* %s */\n", dll_imports
[i
]->spec
->file_name
);
802 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++)
804 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
805 const char *name
= odp
->name
? odp
->name
: odp
->export_name
;
806 fprintf( outfile
, " &__wine_delay_imp_%d_%s,\n", i
, name
);
809 fprintf( outfile
, " },\n {\n" );
810 for (i
= 0; i
< nb_imports
; i
++)
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
++)
816 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
818 fprintf( outfile
, " (char *)%d,\n", odp
->ordinal
);
820 fprintf( outfile
, " \"%s\",\n", odp
->name
);
823 fprintf( outfile
, " }\n};\n\n" );
825 /* check if there's some stub defined. if so, exception struct
826 * is already defined, so don't emit it twice
828 for (i
= 0; i
< spec
->nb_entry_points
; i
++) if (spec
->entry_points
[i
].type
== TYPE_STUB
) break;
830 if (i
== spec
->nb_entry_points
) {
831 fprintf( outfile
, "struct exc_record {\n" );
832 fprintf( outfile
, " unsigned int code, flags;\n" );
833 fprintf( outfile
, " void *rec, *addr;\n" );
834 fprintf( outfile
, " unsigned int params;\n" );
835 fprintf( outfile
, " const void *info[15];\n" );
836 fprintf( outfile
, "};\n\n" );
837 fprintf( outfile
, "extern void __stdcall RtlRaiseException( struct exc_record * );\n" );
840 fprintf( outfile
, "extern void * __stdcall LoadLibraryA(const char*);\n");
841 fprintf( outfile
, "extern void * __stdcall GetProcAddress(void *, const char*);\n");
842 fprintf( outfile
, "\n" );
844 fprintf( outfile
, "void *__stdcall __wine_delay_load( int idx_nr )\n" );
845 fprintf( outfile
, "{\n" );
846 fprintf( outfile
, " int idx = idx_nr >> 16, nr = idx_nr & 0xffff;\n" );
847 fprintf( outfile
, " struct ImgDelayDescr *imd = delay_imports.imp + idx;\n" );
848 fprintf( outfile
, " void **pIAT = imd->pIAT + nr;\n" );
849 fprintf( outfile
, " const char** pINT = imd->pINT + nr;\n" );
850 fprintf( outfile
, " void *fn;\n\n" );
852 fprintf( outfile
, " if (!*imd->phmod) *imd->phmod = LoadLibraryA(imd->szName);\n" );
853 fprintf( outfile
, " if (*imd->phmod && (fn = GetProcAddress(*imd->phmod, *pINT)))\n");
854 fprintf( outfile
, " /* patch IAT with final value */\n" );
855 fprintf( outfile
, " return *pIAT = fn;\n" );
856 fprintf( outfile
, " else {\n");
857 fprintf( outfile
, " struct exc_record rec;\n" );
858 fprintf( outfile
, " rec.code = 0x80000100;\n" );
859 fprintf( outfile
, " rec.flags = 1;\n" );
860 fprintf( outfile
, " rec.rec = 0;\n" );
861 fprintf( outfile
, " rec.params = 2;\n" );
862 fprintf( outfile
, " rec.info[0] = imd->szName;\n" );
863 fprintf( outfile
, " rec.info[1] = *pINT;\n" );
864 fprintf( outfile
, "#ifdef __GNUC__\n" );
865 fprintf( outfile
, " rec.addr = __builtin_return_address(1);\n" );
866 fprintf( outfile
, "#else\n" );
867 fprintf( outfile
, " rec.addr = 0;\n" );
868 fprintf( outfile
, "#endif\n" );
869 fprintf( outfile
, " for (;;) RtlRaiseException( &rec );\n" );
870 fprintf( outfile
, " return 0; /* shouldn't go here */\n" );
871 fprintf( outfile
, " }\n}\n\n" );
873 fprintf( outfile
, "#ifndef __GNUC__\n" );
874 fprintf( outfile
, "static void __asm__dummy_delay_import(void) {\n" );
875 fprintf( outfile
, "#endif\n" );
877 fprintf( outfile
, "asm(\".align %d\\n\"\n", get_alignment(8) );
878 fprintf( outfile
, " \"\\t" __ASM_FUNC("__wine_delay_load_asm") "\\n\"\n" );
879 fprintf( outfile
, " \"" __ASM_NAME("__wine_delay_load_asm") ":\\n\"\n" );
880 #if defined(__i386__)
881 fprintf( outfile
, " \"\\tpushl %%ecx\\n\\tpushl %%edx\\n\\tpushl %%eax\\n\"\n" );
882 fprintf( outfile
, " \"\\tcall __wine_delay_load\\n\"\n" );
883 fprintf( outfile
, " \"\\tpopl %%edx\\n\\tpopl %%ecx\\n\\tjmp *%%eax\\n\"\n" );
884 #elif defined(__sparc__)
885 fprintf( outfile
, " \"\\tsave %%sp, -96, %%sp\\n\"\n" );
886 fprintf( outfile
, " \"\\tcall __wine_delay_load\\n\"\n" );
887 fprintf( outfile
, " \"\\tmov %%g1, %%o0\\n\"\n" );
888 fprintf( outfile
, " \"\\tjmp %%o0\\n\\trestore\\n\"\n" );
889 #elif defined(__powerpc__)
890 /* Save all callee saved registers into a stackframe. */
891 fprintf( outfile
, " \"\\tstwu %s, -48(%s)\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
892 fprintf( outfile
, " \"\\tstw %s, 4(%s)\\n\"\n", ppc_reg
[3], ppc_reg
[1]);
893 fprintf( outfile
, " \"\\tstw %s, 8(%s)\\n\"\n", ppc_reg
[4], ppc_reg
[1]);
894 fprintf( outfile
, " \"\\tstw %s, 12(%s)\\n\"\n", ppc_reg
[5], ppc_reg
[1]);
895 fprintf( outfile
, " \"\\tstw %s, 16(%s)\\n\"\n", ppc_reg
[6], ppc_reg
[1]);
896 fprintf( outfile
, " \"\\tstw %s, 20(%s)\\n\"\n", ppc_reg
[7], ppc_reg
[1]);
897 fprintf( outfile
, " \"\\tstw %s, 24(%s)\\n\"\n", ppc_reg
[8], ppc_reg
[1]);
898 fprintf( outfile
, " \"\\tstw %s, 28(%s)\\n\"\n", ppc_reg
[9], ppc_reg
[1]);
899 fprintf( outfile
, " \"\\tstw %s, 32(%s)\\n\"\n", ppc_reg
[10], ppc_reg
[1]);
900 fprintf( outfile
, " \"\\tstw %s, 36(%s)\\n\"\n", ppc_reg
[11], ppc_reg
[1]);
901 fprintf( outfile
, " \"\\tstw %s, 40(%s)\\n\"\n", ppc_reg
[12], ppc_reg
[1]);
903 /* r0 -> r3 (arg1) */
904 fprintf( outfile
, " \"\\tmr %s, %s\\n\"\n", ppc_reg
[3], ppc_reg
[0]);
906 /* save return address */
907 fprintf( outfile
, " \"\\tmflr %s\\n\"\n", ppc_reg
[0]);
908 fprintf( outfile
, " \"\\tstw %s, 44(%s)\\n\"\n", ppc_reg
[0], ppc_reg
[1]);
910 /* Call the __wine_delay_load function, arg1 is arg1. */
911 fprintf( outfile
, " \"\\tbl " __ASM_NAME("__wine_delay_load") "\\n\"\n");
913 /* Load return value from call into ctr register */
914 fprintf( outfile
, " \"\\tmtctr %s\\n\"\n", ppc_reg
[3]);
916 /* restore all saved registers and drop stackframe. */
917 fprintf( outfile
, " \"\\tlwz %s, 4(%s)\\n\"\n", ppc_reg
[3], ppc_reg
[1]);
918 fprintf( outfile
, " \"\\tlwz %s, 8(%s)\\n\"\n", ppc_reg
[4], ppc_reg
[1]);
919 fprintf( outfile
, " \"\\tlwz %s, 12(%s)\\n\"\n", ppc_reg
[5], ppc_reg
[1]);
920 fprintf( outfile
, " \"\\tlwz %s, 16(%s)\\n\"\n", ppc_reg
[6], ppc_reg
[1]);
921 fprintf( outfile
, " \"\\tlwz %s, 20(%s)\\n\"\n", ppc_reg
[7], ppc_reg
[1]);
922 fprintf( outfile
, " \"\\tlwz %s, 24(%s)\\n\"\n", ppc_reg
[8], ppc_reg
[1]);
923 fprintf( outfile
, " \"\\tlwz %s, 28(%s)\\n\"\n", ppc_reg
[9], ppc_reg
[1]);
924 fprintf( outfile
, " \"\\tlwz %s, 32(%s)\\n\"\n", ppc_reg
[10], ppc_reg
[1]);
925 fprintf( outfile
, " \"\\tlwz %s, 36(%s)\\n\"\n", ppc_reg
[11], ppc_reg
[1]);
926 fprintf( outfile
, " \"\\tlwz %s, 40(%s)\\n\"\n", ppc_reg
[12], ppc_reg
[1]);
928 /* Load return value from call into return register */
929 fprintf( outfile
, " \"\\tlwz %s, 44(%s)\\n\"\n", ppc_reg
[0], ppc_reg
[1]);
930 fprintf( outfile
, " \"\\tmtlr %s\\n\"\n", ppc_reg
[0]);
931 fprintf( outfile
, " \"\\taddi %s, %s, 48\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
933 /* branch to ctr register. */
934 fprintf( outfile
, "\"bctr\\n\"\n");
936 #error You need to defined delayed import thunks for your architecture!
939 for (i
= idx
= 0; i
< nb_imports
; i
++)
941 if (!dll_imports
[i
]->delay
) continue;
942 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++)
945 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
946 const char *name
= odp
->name
? odp
->name
: odp
->export_name
;
948 sprintf( buffer
, "__wine_delay_imp_%d_%s", i
, name
);
949 fprintf( outfile
, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", buffer
);
950 fprintf( outfile
, " \"" __ASM_NAME("%s") ":\\n\"\n", buffer
);
951 #if defined(__i386__)
952 fprintf( outfile
, " \"\\tmovl $%d, %%eax\\n\"\n", (idx
<< 16) | j
);
953 fprintf( outfile
, " \"\\tjmp __wine_delay_load_asm\\n\"\n" );
954 #elif defined(__sparc__)
955 fprintf( outfile
, " \"\\tset %d, %%g1\\n\"\n", (idx
<< 16) | j
);
956 fprintf( outfile
, " \"\\tb,a __wine_delay_load_asm\\n\"\n" );
957 #elif defined(__powerpc__)
958 /* g0 is a function scratch register or so I understand. */
959 /* First load the upper half-word, and then the lower part */
960 fprintf( outfile
, " \"\\tlis %s, %d\\n\"\n", ppc_reg
[0], idx
);
961 fprintf( outfile
, " \"\\tli %s, %d\\n\"\n", ppc_reg
[0], j
);
962 fprintf( outfile
, " \"\\tb " __ASM_NAME("__wine_delay_load_asm") "\\n\"\n");
964 #error You need to defined delayed import thunks for your architecture!
970 fprintf( outfile
, "\n \".data\\n\\t.align %d\\n\"\n", get_alignment(8) );
971 pos
= nb_delayed
* 32;
972 for (i
= 0; i
< nb_imports
; i
++)
974 if (!dll_imports
[i
]->delay
) continue;
975 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++, pos
+= 4)
977 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
978 const char *name
= odp
->name
? odp
->name
: odp
->export_name
;
980 fprintf( outfile
, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", name
);
981 fprintf( outfile
, " \"\\t.globl " __ASM_NAME("%s") "\\n\"\n", name
);
982 fprintf( outfile
, " \"" __ASM_NAME("%s") ":\\n\\t\"", name
);
983 #if defined(__i386__)
984 if (strstr( name
, "__wine_call_from_16" ))
985 fprintf( outfile
, "\".byte 0x2e\\n\\tjmp *(delay_imports+%d)\\n\\tnop\\n\"", pos
);
987 fprintf( outfile
, "\"jmp *(delay_imports+%d)\\n\\tmovl %%esi,%%esi\\n\"", pos
);
988 #elif defined(__sparc__)
991 fprintf( outfile
, "\"sethi %%hi(delay_imports+%d), %%g1\\n\\t\"", pos
);
992 fprintf( outfile
, "\"ld [%%g1+%%lo(delay_imports+%d)], %%g1\\n\\t\"", pos
);
993 fprintf( outfile
, "\"jmp %%g1\\n\\tnop\\n\"" );
997 /* Hmpf. Stupid sparc assembler always interprets global variable
998 names as GOT offsets, so we have to do it the long way ... */
999 fprintf( outfile
, "\"save %%sp, -96, %%sp\\n\"" );
1000 fprintf( outfile
, "\"0:\\tcall 1f\\n\\tnop\\n\"" );
1001 fprintf( outfile
, "\"1:\\tsethi %%hi(delay_imports+%d-0b), %%g1\\n\\t\"", pos
);
1002 fprintf( outfile
, "\"or %%g1, %%lo(delay_imports+%d-0b), %%g1\\n\\t\"", pos
);
1003 fprintf( outfile
, "\"ld [%%g1+%%o7], %%g1\\n\\t\"" );
1004 fprintf( outfile
, "\"jmp %%g1\\n\\trestore\\n\"" );
1007 #elif defined(__powerpc__)
1008 fprintf( outfile
, "\t\"addi %s, %s, -0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
1009 fprintf( outfile
, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg
[9], ppc_reg
[1]);
1010 fprintf( outfile
, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
1011 fprintf( outfile
, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg
[8], ppc_reg
[1]);
1012 fprintf( outfile
, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
1013 fprintf( outfile
, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg
[7], ppc_reg
[1]);
1014 fprintf( outfile
, "\t\"\\tlis %s, " ppc_high(__ASM_NAME("imports") "+ %d") "\\n\"\n", ppc_reg
[9], pos
);
1015 fprintf( outfile
, "\t\"\\tla %s, " ppc_low (__ASM_NAME("imports") "+ %d") "(%s)\\n\"\n", ppc_reg
[8], pos
, ppc_reg
[9]);
1016 fprintf( outfile
, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg
[7], ppc_reg
[8]);
1017 fprintf( outfile
, "\t\"\\tmtctr %s\\n\"\n", ppc_reg
[7]);
1019 fprintf( outfile
, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg
[7], ppc_reg
[1]);
1020 fprintf( outfile
, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
1021 fprintf( outfile
, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg
[8], ppc_reg
[1]);
1022 fprintf( outfile
, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
1023 fprintf( outfile
, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg
[9], ppc_reg
[1]);
1024 fprintf( outfile
, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg
[1], ppc_reg
[1]);
1025 fprintf( outfile
, "\t\"\\tbctr\\n\"");
1027 #error You need to define delayed import thunks for your architecture!
1029 fprintf( outfile
, "\n" );
1032 fprintf( outfile
, "\".text\");\n" );
1033 fprintf( outfile
, "#ifndef __GNUC__\n" );
1034 fprintf( outfile
, "}\n" );
1035 fprintf( outfile
, "#endif\n" );
1036 fprintf( outfile
, "\n" );
1042 /* output the import and delayed import tables of a Win32 module
1043 * returns number of DLLs exported in 'immediate' mode
1045 int output_imports( FILE *outfile
, DLLSPEC
*spec
)
1047 output_delayed_imports( outfile
, spec
);
1048 return output_immediate_imports( outfile
);