wininet: Port resolution doesn't depend on the secure flag.
[wine.git] / tools / winebuild / import.c
blob81325e947afd7b323e766e1d4427c1ea85358d15
1 /*
2 * DLL imports support
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
38 #include "build.h"
40 struct import
42 DLLSPEC *spec; /* description of the imported dll */
43 char *full_name; /* full name of the input file */
44 dev_t dev; /* device/inode of the input file */
45 ino_t ino;
46 int delay; /* delay or not dll loading ? */
47 ORDDEF **exports; /* functions exported from this dll */
48 int nb_exports; /* number of exported functions */
49 ORDDEF **imports; /* functions we want to import from this dll */
50 int nb_imports; /* number of imported functions */
53 struct name_table
55 char **names;
56 unsigned int count, size;
59 static struct name_table undef_symbols; /* list of undefined symbols */
60 static struct name_table extra_ld_symbols; /* list of extra symbols that ld should resolve */
61 static struct name_table delayed_imports; /* list of delayed import dlls */
62 static struct name_table ext_link_imports; /* list of external symbols to link to */
64 static struct import **dll_imports = NULL;
65 static int nb_imports = 0; /* number of imported dlls (delayed or not) */
66 static int nb_delayed = 0; /* number of delayed dlls */
67 static int total_imports = 0; /* total number of imported functions */
68 static int total_delayed = 0; /* total number of imported functions in delayed DLLs */
71 static inline const char *ppc_reg( int reg )
73 static const char * const ppc_regs[32] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
74 "r8", "r9", "r10","r11","r12","r13","r14","r15",
75 "r16","r17","r18","r19","r20","r21","r22","r23",
76 "r24","r25","r26","r27","r28","r29","r30","r31" };
77 if (target_platform == PLATFORM_APPLE) return ppc_regs[reg];
78 return ppc_regs[reg] + 1; /* skip the 'r' */
81 /* compare function names; helper for resolve_imports */
82 static int name_cmp( const void *name, const void *entry )
84 return strcmp( *(const char* const *)name, *(const char* const *)entry );
87 /* compare function names; helper for resolve_imports */
88 static int func_cmp( const void *func1, const void *func2 )
90 const ORDDEF *odp1 = *(const ORDDEF * const *)func1;
91 const ORDDEF *odp2 = *(const ORDDEF * const *)func2;
92 return strcmp( odp1->name ? odp1->name : odp1->export_name,
93 odp2->name ? odp2->name : odp2->export_name );
96 /* add a name to a name table */
97 static inline void add_name( struct name_table *table, const char *name )
99 if (table->count == table->size)
101 table->size += (table->size / 2);
102 if (table->size < 32) table->size = 32;
103 table->names = xrealloc( table->names, table->size * sizeof(*table->names) );
105 table->names[table->count++] = xstrdup( name );
108 /* remove a name from a name table */
109 static inline void remove_name( struct name_table *table, unsigned int idx )
111 assert( idx < table->count );
112 free( table->names[idx] );
113 memmove( table->names + idx, table->names + idx + 1,
114 (table->count - idx - 1) * sizeof(*table->names) );
115 table->count--;
118 /* make a name table empty */
119 static inline void empty_name_table( struct name_table *table )
121 unsigned int i;
123 for (i = 0; i < table->count; i++) free( table->names[i] );
124 table->count = 0;
127 /* locate a name in a (sorted) list */
128 static inline const char *find_name( const char *name, const struct name_table *table )
130 char **res = NULL;
132 if (table->count) res = bsearch( &name, table->names, table->count, sizeof(*table->names), name_cmp );
133 return res ? *res : NULL;
136 /* sort a name table */
137 static inline void sort_names( struct name_table *table )
139 if (table->count) qsort( table->names, table->count, sizeof(*table->names), name_cmp );
142 /* locate an export in a (sorted) export list */
143 static inline ORDDEF *find_export( const char *name, ORDDEF **table, int size )
145 ORDDEF func, *odp, **res = NULL;
147 func.name = xstrdup(name);
148 func.ordinal = -1;
149 odp = &func;
150 if (table) res = bsearch( &odp, table, size, sizeof(*table), func_cmp );
151 free( func.name );
152 return res ? *res : NULL;
155 /* free an import structure */
156 static void free_imports( struct import *imp )
158 free( imp->exports );
159 free( imp->imports );
160 free_dll_spec( imp->spec );
161 free( imp->full_name );
162 free( imp );
165 /* check whether a given dll is imported in delayed mode */
166 static int is_delayed_import( const char *name )
168 unsigned int i;
170 for (i = 0; i < delayed_imports.count; i++)
172 if (!strcmp( delayed_imports.names[i], name )) return 1;
174 return 0;
177 /* check whether a given dll has already been imported */
178 static struct import *is_already_imported( const char *name )
180 int i;
182 for (i = 0; i < nb_imports; i++)
184 if (!strcmp( dll_imports[i]->spec->file_name, name )) return dll_imports[i];
186 return NULL;
189 /* open the .so library for a given dll in a specified path */
190 static char *try_library_path( const char *path, const char *name )
192 char *buffer;
193 int fd;
195 buffer = strmake( "%s/lib%s.def", path, name );
197 /* check if the file exists */
198 if ((fd = open( buffer, O_RDONLY )) != -1)
200 close( fd );
201 return buffer;
203 free( buffer );
204 return NULL;
207 /* find the .def import library for a given dll */
208 static char *find_library( const char *name )
210 char *fullname;
211 int i;
213 for (i = 0; i < nb_lib_paths; i++)
215 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
217 fatal_error( "could not open .def file for %s\n", name );
218 return NULL;
221 /* read in the list of exported symbols of an import library */
222 static int read_import_lib( struct import *imp )
224 FILE *f;
225 int i, ret;
226 struct stat stat;
227 struct import *prev_imp;
228 DLLSPEC *spec = imp->spec;
230 f = open_input_file( NULL, imp->full_name );
231 fstat( fileno(f), &stat );
232 imp->dev = stat.st_dev;
233 imp->ino = stat.st_ino;
234 ret = parse_def_file( f, spec );
235 close_input_file( f );
236 if (!ret) return 0;
238 /* check if we already imported that library from a different file */
239 if ((prev_imp = is_already_imported( spec->file_name )))
241 if (prev_imp->dev != imp->dev || prev_imp->ino != imp->ino)
242 fatal_error( "%s and %s have the same export name '%s'\n",
243 prev_imp->full_name, imp->full_name, spec->file_name );
244 return 0; /* the same file was already loaded, ignore this one */
247 if (is_delayed_import( spec->file_name ))
249 imp->delay = 1;
250 nb_delayed++;
253 if (spec->nb_entry_points)
255 imp->exports = xmalloc( spec->nb_entry_points * sizeof(*imp->exports) );
256 for (i = 0; i < spec->nb_entry_points; i++)
257 imp->exports[imp->nb_exports++] = &spec->entry_points[i];
258 qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
260 return 1;
263 /* build the dll exported name from the import lib name or path */
264 static char *get_dll_name( const char *name, const char *filename )
266 char *ret;
268 if (filename)
270 const char *basename = strrchr( filename, '/' );
271 if (!basename) basename = filename;
272 else basename++;
273 if (!strncmp( basename, "lib", 3 )) basename += 3;
274 ret = xmalloc( strlen(basename) + 5 );
275 strcpy( ret, basename );
276 if (strendswith( ret, ".def" )) ret[strlen(ret)-4] = 0;
278 else
280 ret = xmalloc( strlen(name) + 5 );
281 strcpy( ret, name );
283 if (!strchr( ret, '.' )) strcat( ret, ".dll" );
284 return ret;
287 /* add a dll to the list of imports */
288 void add_import_dll( const char *name, const char *filename )
290 struct import *imp = xmalloc( sizeof(*imp) );
292 imp->spec = alloc_dll_spec();
293 imp->spec->file_name = get_dll_name( name, filename );
294 imp->delay = 0;
295 imp->imports = NULL;
296 imp->nb_imports = 0;
297 imp->exports = NULL;
298 imp->nb_exports = 0;
300 if (filename) imp->full_name = xstrdup( filename );
301 else imp->full_name = find_library( name );
303 if (read_import_lib( imp ))
305 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
306 dll_imports[nb_imports++] = imp;
308 else
310 free_imports( imp );
311 if (nb_errors) exit(1);
315 /* add a library to the list of delayed imports */
316 void add_delayed_import( const char *name )
318 struct import *imp;
319 char *fullname = get_dll_name( name, NULL );
321 add_name( &delayed_imports, fullname );
322 if ((imp = is_already_imported( fullname )) && !imp->delay)
324 imp->delay = 1;
325 nb_delayed++;
327 free( fullname );
330 /* remove an imported dll, based on its index in the dll_imports array */
331 static void remove_import_dll( int index )
333 struct import *imp = dll_imports[index];
335 memmove( &dll_imports[index], &dll_imports[index+1], sizeof(imp) * (nb_imports - index - 1) );
336 nb_imports--;
337 if (imp->delay) nb_delayed--;
338 free_imports( imp );
341 /* add a symbol to the list of extra symbols that ld must resolve */
342 void add_extra_ld_symbol( const char *name )
344 add_name( &extra_ld_symbols, name );
347 /* add a function to the list of imports from a given dll */
348 static void add_import_func( struct import *imp, ORDDEF *func )
350 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
351 imp->imports[imp->nb_imports++] = func;
352 total_imports++;
353 if (imp->delay) total_delayed++;
356 /* get the default entry point for a given spec file */
357 static const char *get_default_entry_point( const DLLSPEC *spec )
359 if (spec->characteristics & IMAGE_FILE_DLL) return "__wine_spec_dll_entry";
360 if (spec->subsystem == IMAGE_SUBSYSTEM_NATIVE) return "__wine_spec_drv_entry";
361 if (spec->type == SPEC_WIN16) return "__wine_spec_exe16_entry";
362 return "__wine_spec_exe_entry";
365 /* check if the spec file exports any stubs */
366 static int has_stubs( const DLLSPEC *spec )
368 int i;
369 for (i = 0; i < spec->nb_entry_points; i++)
371 ORDDEF *odp = &spec->entry_points[i];
372 if (odp->type == TYPE_STUB) return 1;
374 return 0;
377 /* add the extra undefined symbols that will be contained in the generated spec file itself */
378 static void add_extra_undef_symbols( DLLSPEC *spec )
380 if (!spec->init_func) spec->init_func = xstrdup( get_default_entry_point(spec) );
381 add_extra_ld_symbol( spec->init_func );
382 if (has_stubs( spec )) add_extra_ld_symbol( "__wine_spec_unimplemented_stub" );
383 if (nb_delayed) add_extra_ld_symbol( "__wine_spec_delay_load" );
386 /* check if a given imported dll is not needed, taking forwards into account */
387 static int check_unused( const struct import* imp, const DLLSPEC *spec )
389 int i;
390 const char *file_name = imp->spec->file_name;
391 size_t len = strlen( file_name );
392 const char *p = strchr( file_name, '.' );
393 if (p && !strcasecmp( p, ".dll" )) len = p - file_name;
395 for (i = spec->base; i <= spec->limit; i++)
397 ORDDEF *odp = spec->ordinals[i];
398 if (!odp || !(odp->flags & FLAG_FORWARD)) continue;
399 if (!strncasecmp( odp->link_name, file_name, len ) &&
400 odp->link_name[len] == '.')
401 return 0; /* found a forward, it is used */
403 return 1;
406 /* check if a given forward does exist in one of the imported dlls */
407 static void check_undefined_forwards( DLLSPEC *spec )
409 char *link_name, *api_name, *dll_name, *p;
410 int i, j;
412 for (i = 0; i < spec->nb_entry_points; i++)
414 ORDDEF *odp = &spec->entry_points[i];
416 if (!(odp->flags & FLAG_FORWARD)) continue;
418 link_name = xstrdup( odp->link_name );
419 p = strrchr( link_name, '.' );
420 *p = 0;
421 api_name = p + 1;
422 dll_name = get_dll_name( link_name, NULL );
424 for (j = 0; j < nb_imports; j++)
426 struct import *imp = dll_imports[j];
428 if (strcasecmp( imp->spec->file_name, dll_name )) continue;
429 if (!find_export( api_name, imp->exports, imp->nb_exports ))
430 warning( "%s:%d: forward '%s' not found in %s\n",
431 spec->src_name, odp->lineno, odp->link_name, imp->spec->file_name );
432 break;
434 if (j == nb_imports)
435 warning( "%s:%d: forward '%s' not found in the imported dll list\n",
436 spec->src_name, odp->lineno, odp->link_name );
437 free( link_name );
438 free( dll_name );
442 /* flag the dll exports that link to an undefined symbol */
443 static void check_undefined_exports( DLLSPEC *spec )
445 int i;
447 for (i = 0; i < spec->nb_entry_points; i++)
449 ORDDEF *odp = &spec->entry_points[i];
450 if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
451 if (odp->flags & FLAG_FORWARD) continue;
452 if (find_name( odp->link_name, &undef_symbols ))
454 switch(odp->type)
456 case TYPE_PASCAL:
457 case TYPE_STDCALL:
458 case TYPE_CDECL:
459 case TYPE_VARARGS:
460 case TYPE_THISCALL:
461 if (link_ext_symbols)
463 odp->flags |= FLAG_EXT_LINK;
464 add_name( &ext_link_imports, odp->link_name );
466 else error( "%s:%d: function '%s' not defined\n",
467 spec->src_name, odp->lineno, odp->link_name );
468 break;
469 default:
470 error( "%s:%d: external symbol '%s' is not a function\n",
471 spec->src_name, odp->lineno, odp->link_name );
472 break;
478 /* create a .o file that references all the undefined symbols we want to resolve */
479 static char *create_undef_symbols_file( DLLSPEC *spec )
481 char *as_file, *obj_file;
482 int i;
483 unsigned int j;
484 FILE *f;
486 as_file = get_temp_file_name( output_file_name, ".s" );
487 if (!(f = fopen( as_file, "w" ))) fatal_error( "Cannot create %s\n", as_file );
488 fprintf( f, "\t.data\n" );
490 for (i = 0; i < spec->nb_entry_points; i++)
492 ORDDEF *odp = &spec->entry_points[i];
493 if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
494 if (odp->flags & FLAG_FORWARD) continue;
495 fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
497 for (j = 0; j < extra_ld_symbols.count; j++)
498 fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(extra_ld_symbols.names[j]) );
499 fclose( f );
501 obj_file = get_temp_file_name( output_file_name, ".o" );
502 assemble_file( as_file, obj_file );
503 return obj_file;
506 /* combine a list of object files with ld into a single object file */
507 /* returns the name of the combined file */
508 static const char *ldcombine_files( DLLSPEC *spec, char **argv )
510 char *ld_tmp_file, *undef_file;
511 struct strarray *args = get_ld_command();
513 undef_file = create_undef_symbols_file( spec );
514 ld_tmp_file = get_temp_file_name( output_file_name, ".o" );
516 strarray_add( args, "-r", "-o", ld_tmp_file, undef_file, NULL );
517 strarray_addv( args, argv );
518 spawn( args );
519 strarray_free( args );
520 return ld_tmp_file;
523 /* read in the list of undefined symbols */
524 void read_undef_symbols( DLLSPEC *spec, char **argv )
526 size_t prefix_len;
527 FILE *f;
528 const char *prog = get_nm_command();
529 char *cmd, buffer[1024], name_prefix[16];
530 int err;
531 const char *name;
533 if (!argv[0]) return;
535 add_extra_undef_symbols( spec );
537 strcpy( name_prefix, asm_name("") );
538 prefix_len = strlen( name_prefix );
540 name = ldcombine_files( spec, argv );
542 cmd = strmake( "%s -u %s", prog, name );
543 if (!(f = popen( cmd, "r" )))
544 fatal_error( "Cannot execute '%s'\n", cmd );
546 while (fgets( buffer, sizeof(buffer), f ))
548 char *p = buffer + strlen(buffer) - 1;
549 if (p < buffer) continue;
550 if (*p == '\n') *p-- = 0;
551 p = buffer;
552 while (*p == ' ') p++;
553 if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
554 if (prefix_len && !strncmp( p, name_prefix, prefix_len )) p += prefix_len;
555 add_name( &undef_symbols, p );
557 if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
558 free( cmd );
561 /* resolve the imports for a Win32 module */
562 void resolve_imports( DLLSPEC *spec )
564 int i;
565 unsigned int j, removed;
566 ORDDEF *odp;
568 check_undefined_forwards( spec );
570 for (i = 0; i < nb_imports; i++)
572 struct import *imp = dll_imports[i];
574 for (j = removed = 0; j < undef_symbols.count; j++)
576 odp = find_export( undef_symbols.names[j], imp->exports, imp->nb_exports );
577 if (odp)
579 if (odp->flags & FLAG_PRIVATE) continue;
580 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL)
581 warning( "winebuild: Data export '%s' cannot be imported from %s\n",
582 odp->link_name, imp->spec->file_name );
583 else
585 add_import_func( imp, odp );
586 remove_name( &undef_symbols, j-- );
587 removed++;
591 if (!removed)
593 /* the dll is not used, get rid of it */
594 if (check_unused( imp, spec ))
595 warning( "winebuild: %s imported but no symbols used\n", imp->spec->file_name );
596 remove_import_dll( i );
597 i--;
601 sort_names( &undef_symbols );
602 check_undefined_exports( spec );
605 /* check if symbol is still undefined */
606 int is_undefined( const char *name )
608 return find_name( name, &undef_symbols ) != NULL;
611 /* output the get_pc thunk if needed */
612 void output_get_pc_thunk(void)
614 if (target_cpu != CPU_x86) return;
615 if (!UsePIC) return;
616 output( "\n\t.text\n" );
617 output( "\t.align %d\n", get_alignment(4) );
618 output( "\t%s\n", func_declaration("__wine_spec_get_pc_thunk_eax") );
619 output( "%s:\n", asm_name("__wine_spec_get_pc_thunk_eax") );
620 output_cfi( ".cfi_startproc" );
621 output( "\tmovl (%%esp),%%eax\n" );
622 output( "\tret\n" );
623 output_cfi( ".cfi_endproc" );
624 output_function_size( "__wine_spec_get_pc_thunk_eax" );
627 /* output a single import thunk */
628 static void output_import_thunk( const char *name, const char *table, int pos )
630 output( "\n\t.align %d\n", get_alignment(4) );
631 output( "\t%s\n", func_declaration(name) );
632 output( "%s\n", asm_globl(name) );
633 output_cfi( ".cfi_startproc" );
635 switch(target_cpu)
637 case CPU_x86:
638 if (!UsePIC)
640 output( "\tjmp *(%s+%d)\n", table, pos );
642 else
644 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
645 output( "1:\tjmp *%s+%d-1b(%%eax)\n", table, pos );
647 break;
648 case CPU_x86_64:
649 output( "\tjmpq *%s+%d(%%rip)\n", table, pos );
650 break;
651 case CPU_SPARC:
652 if ( !UsePIC )
654 output( "\tsethi %%hi(%s+%d), %%g1\n", table, pos );
655 output( "\tld [%%g1+%%lo(%s+%d)], %%g1\n", table, pos );
656 output( "\tjmp %%g1\n" );
657 output( "\tnop\n" );
659 else
661 /* Hmpf. Stupid sparc assembler always interprets global variable
662 names as GOT offsets, so we have to do it the long way ... */
663 output( "\tsave %%sp, -96, %%sp\n" );
664 output( "0:\tcall 1f\n" );
665 output( "\tnop\n" );
666 output( "1:\tsethi %%hi(%s+%d-0b), %%g1\n", table, pos );
667 output( "\tor %%g1, %%lo(%s+%d-0b), %%g1\n", table, pos );
668 output( "\tld [%%g1+%%o7], %%g1\n" );
669 output( "\tjmp %%g1\n" );
670 output( "\trestore\n" );
672 break;
673 case CPU_ARM:
674 output( "\tldr IP,[PC,#0]\n");
675 output( "\tldr PC,[IP,#%d]\n", pos);
676 output( "\t.long %s\n", table );
677 break;
678 case CPU_ARM64:
679 output( "\tadr x9, 1f\n" );
680 output( "\tldur x9, [x9, #0]\n" );
681 if (pos & 0xf000) output( "\tadd x9, x9, #%u\n", pos & 0xf000 );
682 if (pos & 0x0f00) output( "\tadd x9, x9, #%u\n", pos & 0x0f00 );
683 if (pos & 0x00f0) output( "\tadd x9, x9, #%u\n", pos & 0x00f0 );
684 if (pos & 0x000f) output( "\tadd x9, x9, #%u\n", pos & 0x000f );
685 output( "\tldur x9, [x9, #0]\n" );
686 output( "\tbr x9\n" );
687 output( "1:\t.quad %s\n", table );
688 break;
689 case CPU_POWERPC:
690 output( "\tmr %s, %s\n", ppc_reg(0), ppc_reg(31) );
691 if (target_platform == PLATFORM_APPLE)
693 output( "\tlis %s, ha16(%s+%d+32768)\n", ppc_reg(31), table, pos );
694 output( "\tla %s, lo16(%s+%d)(%s)\n", ppc_reg(31), table, pos, ppc_reg(31) );
696 else
698 output( "\tlis %s, (%s+%d+32768)@h\n", ppc_reg(31), table, pos );
699 output( "\tla %s, (%s+%d)@l(%s)\n", ppc_reg(31), table, pos, ppc_reg(31) );
701 output( "\tlwz %s, 0(%s)\n", ppc_reg(31), ppc_reg(31) );
702 output( "\tmtctr %s\n", ppc_reg(31) );
703 output( "\tmr %s, %s\n", ppc_reg(31), ppc_reg(0) );
704 output( "\tbctr\n" );
705 break;
707 output_cfi( ".cfi_endproc" );
708 output_function_size( name );
711 /* check if we need an import directory */
712 int has_imports(void)
714 return (nb_imports - nb_delayed) > 0;
717 /* output the import table of a Win32 module */
718 static void output_immediate_imports(void)
720 int i, j;
721 const char *dll_name;
723 if (nb_imports == nb_delayed) return; /* no immediate imports */
725 /* main import header */
727 output( "\n/* import table */\n" );
728 output( "\n\t.data\n" );
729 output( "\t.align %d\n", get_alignment(4) );
730 output( ".L__wine_spec_imports:\n" );
732 /* list of dlls */
734 for (i = j = 0; i < nb_imports; i++)
736 if (dll_imports[i]->delay) continue;
737 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
738 output( "\t.long .L__wine_spec_import_data_names+%d-.L__wine_spec_rva_base\n", /* OriginalFirstThunk */
739 j * get_ptr_size() );
740 output( "\t.long 0\n" ); /* TimeDateStamp */
741 output( "\t.long 0\n" ); /* ForwarderChain */
742 output( "\t.long .L__wine_spec_import_name_%s-.L__wine_spec_rva_base\n", /* Name */
743 dll_name );
744 output( "\t.long .L__wine_spec_import_data_ptrs+%d-.L__wine_spec_rva_base\n", /* FirstThunk */
745 j * get_ptr_size() );
746 j += dll_imports[i]->nb_imports + 1;
748 output( "\t.long 0\n" ); /* OriginalFirstThunk */
749 output( "\t.long 0\n" ); /* TimeDateStamp */
750 output( "\t.long 0\n" ); /* ForwarderChain */
751 output( "\t.long 0\n" ); /* Name */
752 output( "\t.long 0\n" ); /* FirstThunk */
754 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
755 output( ".L__wine_spec_import_data_names:\n" );
756 for (i = 0; i < nb_imports; i++)
758 if (dll_imports[i]->delay) continue;
759 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
760 for (j = 0; j < dll_imports[i]->nb_imports; j++)
762 ORDDEF *odp = dll_imports[i]->imports[j];
763 if (!(odp->flags & FLAG_NONAME))
764 output( "\t%s .L__wine_spec_import_data_%s_%s-.L__wine_spec_rva_base\n",
765 get_asm_ptr_keyword(), dll_name, odp->name );
766 else
768 if (get_ptr_size() == 8)
769 output( "\t.quad 0x800000000000%04x\n", odp->ordinal );
770 else
771 output( "\t.long 0x8000%04x\n", odp->ordinal );
774 output( "\t%s 0\n", get_asm_ptr_keyword() );
776 output( ".L__wine_spec_import_data_ptrs:\n" );
777 for (i = 0; i < nb_imports; i++)
779 if (dll_imports[i]->delay) continue;
780 for (j = 0; j < dll_imports[i]->nb_imports; j++) output( "\t%s 0\n", get_asm_ptr_keyword() );
781 output( "\t%s 0\n", get_asm_ptr_keyword() );
783 output( ".L__wine_spec_imports_end:\n" );
785 for (i = 0; i < nb_imports; i++)
787 if (dll_imports[i]->delay) continue;
788 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
789 for (j = 0; j < dll_imports[i]->nb_imports; j++)
791 ORDDEF *odp = dll_imports[i]->imports[j];
792 if (!(odp->flags & FLAG_NONAME))
794 output( "\t.align %d\n", get_alignment(2) );
795 output( ".L__wine_spec_import_data_%s_%s:\n", dll_name, odp->name );
796 output( "\t%s %d\n", get_asm_short_keyword(), odp->ordinal );
797 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name );
802 for (i = 0; i < nb_imports; i++)
804 if (dll_imports[i]->delay) continue;
805 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
806 output( ".L__wine_spec_import_name_%s:\n\t%s \"%s\"\n",
807 dll_name, get_asm_string_keyword(), dll_imports[i]->spec->file_name );
811 /* output the import thunks of a Win32 module */
812 static void output_immediate_import_thunks(void)
814 int i, j, pos;
815 int nb_imm = nb_imports - nb_delayed;
816 static const char import_thunks[] = "__wine_spec_import_thunks";
818 if (!nb_imm) return;
820 output( "\n/* immediate import thunks */\n\n" );
821 output( "\t.text\n" );
822 output( "\t.align %d\n", get_alignment(8) );
823 output( "%s:\n", asm_name(import_thunks));
825 for (i = pos = 0; i < nb_imports; i++)
827 if (dll_imports[i]->delay) continue;
828 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += get_ptr_size())
830 ORDDEF *odp = dll_imports[i]->imports[j];
831 output_import_thunk( odp->name ? odp->name : odp->export_name,
832 ".L__wine_spec_import_data_ptrs", pos );
834 pos += get_ptr_size();
836 output_function_size( import_thunks );
839 /* output the delayed import table of a Win32 module */
840 static void output_delayed_imports( const DLLSPEC *spec )
842 int i, j, mod;
844 if (!nb_delayed) return;
846 output( "\n/* delayed imports */\n\n" );
847 output( "\t.data\n" );
848 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
849 output( "%s\n", asm_globl("__wine_spec_delay_imports") );
851 /* list of dlls */
853 for (i = j = mod = 0; i < nb_imports; i++)
855 if (!dll_imports[i]->delay) continue;
856 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
857 output( "\t%s .L__wine_delay_name_%d\n", /* szName */
858 get_asm_ptr_keyword(), i );
859 output( "\t%s .L__wine_delay_modules+%d\n", /* phmod */
860 get_asm_ptr_keyword(), mod * get_ptr_size() );
861 output( "\t%s .L__wine_delay_IAT+%d\n", /* pIAT */
862 get_asm_ptr_keyword(), j * get_ptr_size() );
863 output( "\t%s .L__wine_delay_INT+%d\n", /* pINT */
864 get_asm_ptr_keyword(), j * get_ptr_size() );
865 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
866 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
867 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
868 j += dll_imports[i]->nb_imports;
869 mod++;
871 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
872 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* szName */
873 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* phmod */
874 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pIAT */
875 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pINT */
876 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
877 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
878 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
880 output( "\n.L__wine_delay_IAT:\n" );
881 for (i = 0; i < nb_imports; i++)
883 if (!dll_imports[i]->delay) continue;
884 for (j = 0; j < dll_imports[i]->nb_imports; j++)
886 ORDDEF *odp = dll_imports[i]->imports[j];
887 const char *name = odp->name ? odp->name : odp->export_name;
888 output( "\t%s .L__wine_delay_imp_%d_%s\n",
889 get_asm_ptr_keyword(), i, name );
893 output( "\n.L__wine_delay_INT:\n" );
894 for (i = 0; i < nb_imports; i++)
896 if (!dll_imports[i]->delay) continue;
897 for (j = 0; j < dll_imports[i]->nb_imports; j++)
899 ORDDEF *odp = dll_imports[i]->imports[j];
900 if (!odp->name)
901 output( "\t%s %d\n", get_asm_ptr_keyword(), odp->ordinal );
902 else
903 output( "\t%s .L__wine_delay_data_%d_%s\n",
904 get_asm_ptr_keyword(), i, odp->name );
908 output( "\n.L__wine_delay_modules:\n" );
909 for (i = 0; i < nb_imports; i++)
911 if (dll_imports[i]->delay) output( "\t%s 0\n", get_asm_ptr_keyword() );
914 for (i = 0; i < nb_imports; i++)
916 if (!dll_imports[i]->delay) continue;
917 output( ".L__wine_delay_name_%d:\n", i );
918 output( "\t%s \"%s\"\n",
919 get_asm_string_keyword(), dll_imports[i]->spec->file_name );
922 for (i = 0; i < nb_imports; i++)
924 if (!dll_imports[i]->delay) continue;
925 for (j = 0; j < dll_imports[i]->nb_imports; j++)
927 ORDDEF *odp = dll_imports[i]->imports[j];
928 if (!odp->name) continue;
929 output( ".L__wine_delay_data_%d_%s:\n", i, odp->name );
930 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name );
933 output_function_size( "__wine_spec_delay_imports" );
936 /* output the delayed import thunks of a Win32 module */
937 static void output_delayed_import_thunks( const DLLSPEC *spec )
939 int i, idx, j, pos, extra_stack_storage = 0;
940 static const char delayed_import_loaders[] = "__wine_spec_delayed_import_loaders";
941 static const char delayed_import_thunks[] = "__wine_spec_delayed_import_thunks";
943 if (!nb_delayed) return;
945 output( "\n/* delayed import thunks */\n\n" );
946 output( "\t.text\n" );
947 output( "\t.align %d\n", get_alignment(8) );
948 output( "%s:\n", asm_name(delayed_import_loaders));
949 output( "\t%s\n", func_declaration("__wine_delay_load_asm") );
950 output( "%s:\n", asm_name("__wine_delay_load_asm") );
951 output_cfi( ".cfi_startproc" );
952 switch(target_cpu)
954 case CPU_x86:
955 output( "\tpushl %%ecx\n" );
956 output_cfi( ".cfi_adjust_cfa_offset 4" );
957 output( "\tpushl %%edx\n" );
958 output_cfi( ".cfi_adjust_cfa_offset 4" );
959 output( "\tpushl %%eax\n" );
960 output_cfi( ".cfi_adjust_cfa_offset 4" );
961 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
962 output_cfi( ".cfi_adjust_cfa_offset -4" );
963 output( "\tpopl %%edx\n" );
964 output_cfi( ".cfi_adjust_cfa_offset -4" );
965 output( "\tpopl %%ecx\n" );
966 output_cfi( ".cfi_adjust_cfa_offset -4" );
967 output( "\tjmp *%%eax\n" );
968 break;
969 case CPU_x86_64:
970 output( "\tsubq $88,%%rsp\n" );
971 output_cfi( ".cfi_adjust_cfa_offset 88" );
972 output( "\tmovq %%rdx,80(%%rsp)\n" );
973 output( "\tmovq %%rcx,72(%%rsp)\n" );
974 output( "\tmovq %%r8,64(%%rsp)\n" );
975 output( "\tmovq %%r9,56(%%rsp)\n" );
976 output( "\tmovq %%r10,48(%%rsp)\n" );
977 output( "\tmovq %%r11,40(%%rsp)\n" );
978 output( "\tmovq %%rax,%%rcx\n" );
979 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
980 output( "\tmovq 40(%%rsp),%%r11\n" );
981 output( "\tmovq 48(%%rsp),%%r10\n" );
982 output( "\tmovq 56(%%rsp),%%r9\n" );
983 output( "\tmovq 64(%%rsp),%%r8\n" );
984 output( "\tmovq 72(%%rsp),%%rcx\n" );
985 output( "\tmovq 80(%%rsp),%%rdx\n" );
986 output( "\taddq $88,%%rsp\n" );
987 output_cfi( ".cfi_adjust_cfa_offset -88" );
988 output( "\tjmp *%%rax\n" );
989 break;
990 case CPU_SPARC:
991 output( "\tsave %%sp, -96, %%sp\n" );
992 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
993 output( "\tmov %%g1, %%o0\n" );
994 output( "\tjmp %%o0\n" );
995 output( "\trestore\n" );
996 break;
997 case CPU_ARM:
998 output( "\tstmfd SP!, {r4-r10,FP,LR}\n" );
999 output( "\tmov LR,PC\n");
1000 output( "\tadd LR,LR,#8\n");
1001 output( "\tldr PC,[PC,#-4]\n");
1002 output( "\t.long %s\n", asm_name("__wine_spec_delay_load") );
1003 output( "\tmov IP,r0\n");
1004 output( "\tldmfd SP!, {r4-r10,FP,LR}\n" );
1005 output( "\tldmfd SP!, {r0-r3}\n" );
1006 output( "\tmov PC,IP\n");
1007 break;
1008 case CPU_ARM64:
1009 output( "\tstp x29, x30, [sp,#-16]!\n" );
1010 output( "\tmov x29, sp\n" );
1011 output( "\tadr x9, 1f\n" );
1012 output( "\tldur x9, [x9, #0]\n" );
1013 output( "\tblr x9\n" );
1014 output( "\tmov x9, x0\n" );
1015 output( "\tldp x29, x30, [sp],#16\n" );
1016 output( "\tldp x0, x1, [sp,#16]\n" );
1017 output( "\tldp x2, x3, [sp,#32]\n" );
1018 output( "\tldp x4, x5, [sp,#48]\n" );
1019 output( "\tldp x6, x7, [sp],#80\n" );
1020 output( "\tbr x9\n" ); /* or "ret x9" */
1021 output( "1:\t.quad %s\n", asm_name("__wine_spec_delay_load") );
1022 break;
1023 case CPU_POWERPC:
1024 if (target_platform == PLATFORM_APPLE) extra_stack_storage = 56;
1026 /* Save all callee saved registers into a stackframe. */
1027 output( "\tstwu %s, -%d(%s)\n",ppc_reg(1), 48+extra_stack_storage, ppc_reg(1));
1028 output( "\tstw %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage, ppc_reg(1));
1029 output( "\tstw %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage, ppc_reg(1));
1030 output( "\tstw %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
1031 output( "\tstw %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
1032 output( "\tstw %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
1033 output( "\tstw %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
1034 output( "\tstw %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
1035 output( "\tstw %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
1036 output( "\tstw %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
1037 output( "\tstw %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
1039 /* r0 -> r3 (arg1) */
1040 output( "\tmr %s, %s\n", ppc_reg(3), ppc_reg(0));
1042 /* save return address */
1043 output( "\tmflr %s\n", ppc_reg(0));
1044 output( "\tstw %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
1046 /* Call the __wine_delay_load function, arg1 is arg1. */
1047 output( "\tbl %s\n", asm_name("__wine_spec_delay_load") );
1049 /* Load return value from call into ctr register */
1050 output( "\tmtctr %s\n", ppc_reg(3));
1052 /* restore all saved registers and drop stackframe. */
1053 output( "\tlwz %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage, ppc_reg(1));
1054 output( "\tlwz %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage, ppc_reg(1));
1055 output( "\tlwz %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
1056 output( "\tlwz %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
1057 output( "\tlwz %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
1058 output( "\tlwz %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
1059 output( "\tlwz %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
1060 output( "\tlwz %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
1061 output( "\tlwz %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
1062 output( "\tlwz %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
1064 /* Load return value from call into return register */
1065 output( "\tlwz %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
1066 output( "\tmtlr %s\n", ppc_reg(0));
1067 output( "\taddi %s, %s, %d\n", ppc_reg(1), ppc_reg(1), 48+extra_stack_storage);
1069 /* branch to ctr register. */
1070 output( "\tbctr\n");
1071 break;
1073 output_cfi( ".cfi_endproc" );
1074 output_function_size( "__wine_delay_load_asm" );
1075 output( "\n" );
1077 for (i = idx = 0; i < nb_imports; i++)
1079 if (!dll_imports[i]->delay) continue;
1080 for (j = 0; j < dll_imports[i]->nb_imports; j++)
1082 ORDDEF *odp = dll_imports[i]->imports[j];
1083 const char *name = odp->name ? odp->name : odp->export_name;
1085 output( ".L__wine_delay_imp_%d_%s:\n", i, name );
1086 output_cfi( ".cfi_startproc" );
1087 switch(target_cpu)
1089 case CPU_x86:
1090 output( "\tmovl $%d, %%eax\n", (idx << 16) | j );
1091 output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1092 break;
1093 case CPU_x86_64:
1094 output( "\tmovq $%d,%%rax\n", (idx << 16) | j );
1095 output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1096 break;
1097 case CPU_SPARC:
1098 output( "\tset %d, %%g1\n", (idx << 16) | j );
1099 output( "\tb,a %s\n", asm_name("__wine_delay_load_asm") );
1100 output( "\tnop\n" );
1101 break;
1102 case CPU_ARM:
1103 output( "\tstmfd SP!, {r0-r3}\n" );
1104 output( "\tmov r0, #%d\n", idx );
1105 output( "\tmov r1, #16384\n" );
1106 output( "\tmul r1, r0, r1\n" );
1107 output( "\tmov r0, r1\n" );
1108 output( "\tmov r1, #4\n" );
1109 output( "\tmul r1, r0, r1\n" );
1110 output( "\tmov r0, r1\n" );
1111 output( "\tadd r0, #%d\n", j );
1112 output( "\tldr PC,[PC,#-4]\n");
1113 output( "\t.long %s\n", asm_name("__wine_delay_load_asm") );
1114 break;
1115 case CPU_ARM64:
1116 output( "\tstp x6, x7, [sp,#-80]!\n" );
1117 output( "\tstp x4, x5, [sp,#48]\n" );
1118 output( "\tstp x2, x3, [sp,#32]\n" );
1119 output( "\tstp x0, x1, [sp,#16]\n" );
1120 output( "\tmov x0, #%d\n", idx );
1121 output( "\tmov x1, #16384\n" );
1122 output( "\tmul x1, x0, x1\n" );
1123 output( "\tmov x0, x1\n" );
1124 output( "\tmov x1, #4\n" );
1125 output( "\tmul x1, x0, x1\n" );
1126 output( "\tmov x0, x1\n" );
1127 output( "\tadd x0, x0, #%d\n", j );
1128 output( "\tadr x9, 1f\n" );
1129 output( "\tldur x9, [x9, #0]\n" );
1130 output( "\tbr x9\n" );
1131 output( "1:\t.quad %s\n", asm_name("__wine_delay_load_asm") );
1132 break;
1133 case CPU_POWERPC:
1134 switch(target_platform)
1136 case PLATFORM_APPLE:
1137 /* On Darwin we can use r0 and r2 */
1138 /* Upper part in r2 */
1139 output( "\tlis %s, %d\n", ppc_reg(2), idx);
1140 /* Lower part + r2 -> r0, Note we can't use r0 directly */
1141 output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(2), j);
1142 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1143 break;
1144 default:
1145 /* On linux we can't use r2 since r2 is not a scratch register (hold the TOC) */
1146 /* Save r13 on the stack */
1147 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1));
1148 output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
1149 /* Upper part in r13 */
1150 output( "\tlis %s, %d\n", ppc_reg(13), idx);
1151 /* Lower part + r13 -> r0, Note we can't use r0 directly */
1152 output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(13), j);
1153 /* Restore r13 */
1154 output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
1155 output( "\taddic %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1));
1156 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1157 break;
1159 break;
1161 output_cfi( ".cfi_endproc" );
1163 idx++;
1165 output_function_size( delayed_import_loaders );
1167 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
1168 output( "%s:\n", asm_name(delayed_import_thunks));
1169 for (i = pos = 0; i < nb_imports; i++)
1171 if (!dll_imports[i]->delay) continue;
1172 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += get_ptr_size())
1174 ORDDEF *odp = dll_imports[i]->imports[j];
1175 output_import_thunk( odp->name ? odp->name : odp->export_name,
1176 ".L__wine_delay_IAT", pos );
1179 output_function_size( delayed_import_thunks );
1182 /* output import stubs for exported entry points that link to external symbols */
1183 static void output_external_link_imports( DLLSPEC *spec )
1185 unsigned int i, pos;
1187 if (!ext_link_imports.count) return; /* nothing to do */
1189 sort_names( &ext_link_imports );
1191 /* get rid of duplicate names */
1192 for (i = 1; i < ext_link_imports.count; i++)
1194 if (!strcmp( ext_link_imports.names[i-1], ext_link_imports.names[i] ))
1195 remove_name( &ext_link_imports, i-- );
1198 output( "\n/* external link thunks */\n\n" );
1199 output( "\t.data\n" );
1200 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
1201 output( ".L__wine_spec_external_links:\n" );
1202 for (i = 0; i < ext_link_imports.count; i++)
1203 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(ext_link_imports.names[i]) );
1205 output( "\n\t.text\n" );
1206 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
1207 output( "%s:\n", asm_name("__wine_spec_external_link_thunks") );
1209 for (i = pos = 0; i < ext_link_imports.count; i++)
1211 char *buffer = strmake( "__wine_spec_ext_link_%s", ext_link_imports.names[i] );
1212 output_import_thunk( buffer, ".L__wine_spec_external_links", pos );
1213 free( buffer );
1214 pos += get_ptr_size();
1216 output_function_size( "__wine_spec_external_link_thunks" );
1219 /*******************************************************************
1220 * output_stubs
1222 * Output the functions for stub entry points
1224 void output_stubs( DLLSPEC *spec )
1226 const char *name, *exp_name;
1227 int i, count;
1229 if (!has_stubs( spec )) return;
1231 output( "\n/* stub functions */\n\n" );
1232 output( "\t.text\n" );
1234 for (i = count = 0; i < spec->nb_entry_points; i++)
1236 ORDDEF *odp = &spec->entry_points[i];
1237 if (odp->type != TYPE_STUB) continue;
1239 name = get_stub_name( odp, spec );
1240 exp_name = odp->name ? odp->name : odp->export_name;
1241 output( "\t.align %d\n", get_alignment(4) );
1242 output( "\t%s\n", func_declaration(name) );
1243 output( "%s:\n", asm_name(name) );
1244 output_cfi( ".cfi_startproc" );
1246 switch (target_cpu)
1248 case CPU_x86:
1249 /* flesh out the stub a bit to make safedisc happy */
1250 output(" \tnop\n" );
1251 output(" \tnop\n" );
1252 output(" \tnop\n" );
1253 output(" \tnop\n" );
1254 output(" \tnop\n" );
1255 output(" \tnop\n" );
1256 output(" \tnop\n" );
1257 output(" \tnop\n" );
1258 output(" \tnop\n" );
1260 output( "\tsubl $12,%%esp\n" );
1261 output_cfi( ".cfi_adjust_cfa_offset 12" );
1262 if (UsePIC)
1264 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
1265 output( "1:" );
1266 if (exp_name)
1268 output( "\tleal .L%s_string-1b(%%eax),%%ecx\n", name );
1269 output( "\tmovl %%ecx,4(%%esp)\n" );
1270 count++;
1272 else
1273 output( "\tmovl $%d,4(%%esp)\n", odp->ordinal );
1274 output( "\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
1275 output( "\tmovl %%ecx,(%%esp)\n" );
1277 else
1279 if (exp_name)
1281 output( "\tmovl $.L%s_string,4(%%esp)\n", name );
1282 count++;
1284 else
1285 output( "\tmovl $%d,4(%%esp)\n", odp->ordinal );
1286 output( "\tmovl $.L__wine_spec_file_name,(%%esp)\n" );
1288 output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
1289 break;
1290 case CPU_x86_64:
1291 output( "\tsubq $8,%%rsp\n" );
1292 output_cfi( ".cfi_adjust_cfa_offset 8" );
1293 output( "\tleaq .L__wine_spec_file_name(%%rip),%%rdi\n" );
1294 if (exp_name)
1296 output( "leaq .L%s_string(%%rip),%%rsi\n", name );
1297 count++;
1299 else
1300 output( "\tmovq $%d,%%rsi\n", odp->ordinal );
1301 output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
1302 break;
1303 case CPU_ARM:
1304 output( "\tldr r0,[PC,#0]\n");
1305 output( "\tmov PC,PC\n");
1306 output( "\t.long .L__wine_spec_file_name\n" );
1307 output( "\tldr r1,[PC,#0]\n");
1308 output( "\tmov PC,PC\n");
1309 if (exp_name)
1311 output( "\t.long .L%s_string\n", name );
1312 count++;
1314 else
1315 output( "\t.long %d\n", odp->ordinal );
1316 output( "\tbl %s\n", asm_name("__wine_spec_unimplemented_stub") );
1317 break;
1318 default:
1319 assert(0);
1321 output_cfi( ".cfi_endproc" );
1322 output_function_size( name );
1325 if (count)
1327 output( "\t%s\n", get_asm_string_section() );
1328 for (i = 0; i < spec->nb_entry_points; i++)
1330 ORDDEF *odp = &spec->entry_points[i];
1331 if (odp->type != TYPE_STUB) continue;
1332 exp_name = odp->name ? odp->name : odp->export_name;
1333 if (exp_name)
1335 name = get_stub_name( odp, spec );
1336 output( ".L%s_string:\n", name );
1337 output( "\t%s \"%s\"\n", get_asm_string_keyword(), exp_name );
1343 /* output the import and delayed import tables of a Win32 module */
1344 void output_imports( DLLSPEC *spec )
1346 output_immediate_imports();
1347 output_delayed_imports( spec );
1348 output_immediate_import_thunks();
1349 output_delayed_import_thunks( spec );
1350 output_external_link_imports( spec );
1351 if (nb_imports || ext_link_imports.count || has_stubs(spec) || has_relays(spec))
1352 output_get_pc_thunk();
1355 /* output an import library for a Win32 module and additional object files */
1356 void output_import_lib( DLLSPEC *spec, char **argv )
1358 struct strarray *args = strarray_init();
1359 char *def_file;
1361 if (target_platform != PLATFORM_WINDOWS)
1362 fatal_error( "Unix-style import libraries not supported yet\n" );
1364 def_file = get_temp_file_name( output_file_name, ".def" );
1365 fclose( output_file );
1366 if (!(output_file = fopen( def_file, "w" )))
1367 fatal_error( "Unable to create output file '%s'\n", def_file );
1368 output_def_file( spec, 0 );
1369 fclose( output_file );
1370 output_file = NULL;
1372 strarray_add( args, find_tool( "dlltool", NULL ), "-k", "-l", output_file_name, "-d", def_file, NULL );
1373 spawn( args );
1374 strarray_free( args );
1376 if (argv[0])
1378 args = strarray_init();
1379 strarray_add( args, find_tool( "ar", NULL ), "rs", output_file_name, NULL );
1380 strarray_addv( args, argv );
1381 spawn( args );
1382 strarray_free( args );
1384 output_file_name = NULL;