reg/tests: Get rid of version check.
[wine.git] / tools / winebuild / import.c
blob01a418f3f795cf4a56b192694fe01adcceb7a442
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 "wine/list.h"
39 #include "build.h"
41 struct import_func
43 const char *name;
44 const char *export_name;
45 int ordinal;
48 struct import
50 struct list entry; /* entry in global dll list */
51 char *dll_name; /* exported file name of the dll */
52 char *c_name; /* dll name as a C-compatible identifier */
53 char *full_name; /* full name of the input file */
54 dev_t dev; /* device/inode of the input file */
55 ino_t ino;
56 ORDDEF **exports; /* functions exported from this dll */
57 int nb_exports; /* number of exported functions */
58 struct import_func *imports; /* functions we want to import from this dll */
59 int nb_imports; /* number of imported functions */
60 int max_imports; /* size of imports array */
63 static struct strarray undef_symbols; /* list of undefined symbols */
64 static struct strarray extra_ld_symbols; /* list of extra symbols that ld should resolve */
65 static struct strarray delayed_imports; /* list of delayed import dlls */
66 static struct strarray ext_link_imports; /* list of external symbols to link to */
68 static struct list dll_imports = LIST_INIT( dll_imports );
69 static struct list dll_delayed = LIST_INIT( dll_delayed );
71 static struct strarray as_files;
73 static const char import_func_prefix[] = "__wine$func$";
74 static const char import_ord_prefix[] = "__wine$ord$";
76 static inline const char *ppc_reg( int reg )
78 static const char * const ppc_regs[32] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
79 "r8", "r9", "r10","r11","r12","r13","r14","r15",
80 "r16","r17","r18","r19","r20","r21","r22","r23",
81 "r24","r25","r26","r27","r28","r29","r30","r31" };
82 if (target_platform == PLATFORM_APPLE) return ppc_regs[reg];
83 return ppc_regs[reg] + 1; /* skip the 'r' */
86 /* compare function names; helper for resolve_imports */
87 static int name_cmp( const void *name, const void *entry )
89 return strcmp( *(const char* const *)name, *(const char* const *)entry );
92 /* compare function names; helper for resolve_imports */
93 static int func_cmp( const void *func1, const void *func2 )
95 const ORDDEF *odp1 = *(const ORDDEF * const *)func1;
96 const ORDDEF *odp2 = *(const ORDDEF * const *)func2;
97 return strcmp( odp1->name ? odp1->name : odp1->export_name,
98 odp2->name ? odp2->name : odp2->export_name );
101 /* remove a name from a name table */
102 static inline void remove_name( struct strarray *table, unsigned int idx )
104 assert( idx < table->count );
105 memmove( table->str + idx, table->str + idx + 1,
106 (table->count - idx - 1) * sizeof(*table->str) );
107 table->count--;
110 /* locate a name in a (sorted) list */
111 static inline const char *find_name( const char *name, const struct strarray *table )
113 char **res = NULL;
115 if (table->count) res = bsearch( &name, table->str, table->count, sizeof(*table->str), name_cmp );
116 return res ? *res : NULL;
119 /* sort a name table */
120 static inline void sort_names( struct strarray *table )
122 if (table->count) qsort( table->str, table->count, sizeof(*table->str), name_cmp );
125 /* locate an export in a (sorted) export list */
126 static inline ORDDEF *find_export( const char *name, ORDDEF **table, int size )
128 ORDDEF func, *odp, **res = NULL;
130 func.name = xstrdup(name);
131 func.ordinal = -1;
132 odp = &func;
133 if (table) res = bsearch( &odp, table, size, sizeof(*table), func_cmp );
134 free( func.name );
135 return res ? *res : NULL;
138 /* free an import structure */
139 static void free_imports( struct import *imp )
141 free( imp->exports );
142 free( imp->imports );
143 free( imp->dll_name );
144 free( imp->c_name );
145 free( imp->full_name );
146 free( imp );
149 /* check whether a given dll is imported in delayed mode */
150 static int is_delayed_import( const char *name )
152 unsigned int i;
154 for (i = 0; i < delayed_imports.count; i++)
156 if (!strcmp( delayed_imports.str[i], name )) return 1;
158 return 0;
161 /* find an imported dll from its name */
162 static struct import *find_import_dll( const char *name )
164 struct import *import;
166 LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
167 if (!strcasecmp( import->dll_name, name )) return import;
168 LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
169 if (!strcasecmp( import->dll_name, name )) return import;
170 return NULL;
173 /* open the .so library for a given dll in a specified path */
174 static char *try_library_path( const char *path, const char *name )
176 char *buffer;
177 int fd;
179 buffer = strmake( "%s/lib%s.def", path, name );
181 /* check if the file exists */
182 if ((fd = open( buffer, O_RDONLY )) != -1)
184 close( fd );
185 return buffer;
187 free( buffer );
188 return NULL;
191 /* find the .def import library for a given dll */
192 static char *find_library( const char *name )
194 char *fullname;
195 unsigned int i;
197 for (i = 0; i < lib_path.count; i++)
199 if ((fullname = try_library_path( lib_path.str[i], name ))) return fullname;
201 fatal_error( "could not open .def file for %s\n", name );
202 return NULL;
205 /* read in the list of exported symbols of an import library */
206 static DLLSPEC *read_import_lib( struct import *imp )
208 FILE *f;
209 int i;
210 struct stat stat;
211 struct import *prev_imp;
212 DLLSPEC *spec = alloc_dll_spec();
214 f = open_input_file( NULL, imp->full_name );
215 fstat( fileno(f), &stat );
216 imp->dev = stat.st_dev;
217 imp->ino = stat.st_ino;
218 if (!parse_def_file( f, spec )) exit( 1 );
219 close_input_file( f );
221 /* check if we already imported that library from a different file */
222 if ((prev_imp = find_import_dll( spec->file_name )))
224 if (prev_imp->dev != imp->dev || prev_imp->ino != imp->ino)
225 fatal_error( "%s and %s have the same export name '%s'\n",
226 prev_imp->full_name, imp->full_name, spec->file_name );
227 free_dll_spec( spec );
228 return NULL; /* the same file was already loaded, ignore this one */
231 if (spec->nb_entry_points)
233 imp->exports = xmalloc( spec->nb_entry_points * sizeof(*imp->exports) );
234 for (i = 0; i < spec->nb_entry_points; i++)
235 imp->exports[imp->nb_exports++] = &spec->entry_points[i];
236 qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
238 return spec;
241 /* build the dll exported name from the import lib name or path */
242 static char *get_dll_name( const char *name, const char *filename )
244 char *ret;
246 if (filename)
248 const char *basename = strrchr( filename, '/' );
249 if (!basename) basename = filename;
250 else basename++;
251 if (!strncmp( basename, "lib", 3 )) basename += 3;
252 ret = xmalloc( strlen(basename) + 5 );
253 strcpy( ret, basename );
254 if (strendswith( ret, ".def" )) ret[strlen(ret)-4] = 0;
256 else
258 ret = xmalloc( strlen(name) + 5 );
259 strcpy( ret, name );
261 if (!strchr( ret, '.' )) strcat( ret, ".dll" );
262 return ret;
265 /* add a dll to the list of imports */
266 void add_import_dll( const char *name, const char *filename )
268 DLLSPEC *spec;
269 char *dll_name = get_dll_name( name, filename );
270 struct import *imp = xmalloc( sizeof(*imp) );
272 memset( imp, 0, sizeof(*imp) );
274 if (filename) imp->full_name = xstrdup( filename );
275 else imp->full_name = find_library( name );
277 if (!(spec = read_import_lib( imp )))
279 free_imports( imp );
280 return;
283 imp->dll_name = spec->file_name ? spec->file_name : dll_name;
284 imp->c_name = make_c_identifier( imp->dll_name );
286 if (is_delayed_import( dll_name ))
287 list_add_tail( &dll_delayed, &imp->entry );
288 else
289 list_add_tail( &dll_imports, &imp->entry );
292 /* add a library to the list of delayed imports */
293 void add_delayed_import( const char *name )
295 struct import *imp;
296 char *fullname = get_dll_name( name, NULL );
298 strarray_add( &delayed_imports, fullname, NULL );
299 if ((imp = find_import_dll( fullname )))
301 list_remove( &imp->entry );
302 list_add_tail( &dll_delayed, &imp->entry );
306 /* add a symbol to the list of extra symbols that ld must resolve */
307 void add_extra_ld_symbol( const char *name )
309 strarray_add( &extra_ld_symbols, name, NULL );
312 /* retrieve an imported dll, adding one if necessary */
313 struct import *add_static_import_dll( const char *name )
315 struct import *import;
316 char *dll_name = get_dll_name( name, NULL );
318 if ((import = find_import_dll( dll_name ))) return import;
320 import = xmalloc( sizeof(*import) );
321 memset( import, 0, sizeof(*import) );
323 import->dll_name = dll_name;
324 import->full_name = xstrdup( dll_name );
325 import->c_name = make_c_identifier( dll_name );
327 if (is_delayed_import( dll_name ))
328 list_add_tail( &dll_delayed, &import->entry );
329 else
330 list_add_tail( &dll_imports, &import->entry );
331 return import;
334 /* add a function to the list of imports from a given dll */
335 static void add_import_func( struct import *imp, const char *name, const char *export_name, int ordinal )
337 if (imp->nb_imports == imp->max_imports)
339 imp->max_imports *= 2;
340 if (imp->max_imports < 32) imp->max_imports = 32;
341 imp->imports = xrealloc( imp->imports, imp->max_imports * sizeof(*imp->imports) );
343 imp->imports[imp->nb_imports].name = name;
344 imp->imports[imp->nb_imports].export_name = export_name;
345 imp->imports[imp->nb_imports].ordinal = ordinal;
346 imp->nb_imports++;
349 /* add an import for an undefined function of the form __wine$func$ */
350 static void add_undef_import( const char *name, int is_ordinal )
352 char *p, *dll_name = xstrdup( name );
353 int ordinal = 0;
354 struct import *import;
356 if (!(p = strchr( dll_name, '$' ))) return;
357 *p++ = 0;
358 while (*p >= '0' && *p <= '9') ordinal = 10 * ordinal + *p++ - '0';
359 if (*p != '$') return;
360 p++;
362 import = add_static_import_dll( dll_name );
363 if (is_ordinal)
364 add_import_func( import, NULL, xstrdup( p ), ordinal );
365 else
366 add_import_func( import, xstrdup( p ), NULL, ordinal );
369 /* get the default entry point for a given spec file */
370 static const char *get_default_entry_point( const DLLSPEC *spec )
372 if (spec->characteristics & IMAGE_FILE_DLL) return "__wine_spec_dll_entry";
373 if (spec->subsystem == IMAGE_SUBSYSTEM_NATIVE) return "__wine_spec_drv_entry";
374 if (spec->type == SPEC_WIN16) return "__wine_spec_exe16_entry";
375 return "__wine_spec_exe_entry";
378 /* check if the spec file exports any stubs */
379 static int has_stubs( const DLLSPEC *spec )
381 int i;
382 for (i = 0; i < spec->nb_entry_points; i++)
384 ORDDEF *odp = &spec->entry_points[i];
385 if (odp->type == TYPE_STUB) return 1;
387 return 0;
390 /* add the extra undefined symbols that will be contained in the generated spec file itself */
391 static void add_extra_undef_symbols( DLLSPEC *spec )
393 if (!spec->init_func) spec->init_func = xstrdup( get_default_entry_point(spec) );
394 add_extra_ld_symbol( spec->init_func );
395 if (has_stubs( spec )) add_extra_ld_symbol( "__wine_spec_unimplemented_stub" );
396 if (delayed_imports.count) add_extra_ld_symbol( "__wine_spec_delay_load" );
399 /* check if a given imported dll is not needed, taking forwards into account */
400 static int check_unused( const struct import* imp, const DLLSPEC *spec )
402 int i;
403 const char *file_name = imp->dll_name;
404 size_t len = strlen( file_name );
405 const char *p = strchr( file_name, '.' );
406 if (p && !strcasecmp( p, ".dll" )) len = p - file_name;
408 for (i = spec->base; i <= spec->limit; i++)
410 ORDDEF *odp = spec->ordinals[i];
411 if (!odp || !(odp->flags & FLAG_FORWARD)) continue;
412 if (!strncasecmp( odp->link_name, file_name, len ) &&
413 odp->link_name[len] == '.')
414 return 0; /* found a forward, it is used */
416 return 1;
419 /* check if a given forward does exist in one of the imported dlls */
420 static void check_undefined_forwards( DLLSPEC *spec )
422 struct import *imp;
423 char *link_name, *api_name, *dll_name, *p;
424 int i;
426 for (i = 0; i < spec->nb_entry_points; i++)
428 ORDDEF *odp = &spec->entry_points[i];
430 if (!(odp->flags & FLAG_FORWARD)) continue;
432 link_name = xstrdup( odp->link_name );
433 p = strrchr( link_name, '.' );
434 *p = 0;
435 api_name = p + 1;
436 dll_name = get_dll_name( link_name, NULL );
438 if ((imp = find_import_dll( dll_name )))
440 if (!find_export( api_name, imp->exports, imp->nb_exports ))
441 warning( "%s:%d: forward '%s' not found in %s\n",
442 spec->src_name, odp->lineno, odp->link_name, imp->dll_name );
444 else warning( "%s:%d: forward '%s' not found in the imported dll list\n",
445 spec->src_name, odp->lineno, odp->link_name );
446 free( link_name );
447 free( dll_name );
451 /* flag the dll exports that link to an undefined symbol */
452 static void check_undefined_exports( DLLSPEC *spec )
454 int i;
456 for (i = 0; i < spec->nb_entry_points; i++)
458 ORDDEF *odp = &spec->entry_points[i];
459 if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
460 if (odp->flags & FLAG_FORWARD) continue;
461 if (find_name( odp->link_name, &undef_symbols ))
463 switch(odp->type)
465 case TYPE_PASCAL:
466 case TYPE_STDCALL:
467 case TYPE_CDECL:
468 case TYPE_VARARGS:
469 case TYPE_THISCALL:
470 if (link_ext_symbols)
472 odp->flags |= FLAG_EXT_LINK;
473 strarray_add( &ext_link_imports, odp->link_name, NULL );
475 else error( "%s:%d: function '%s' not defined\n",
476 spec->src_name, odp->lineno, odp->link_name );
477 break;
478 default:
479 error( "%s:%d: external symbol '%s' is not a function\n",
480 spec->src_name, odp->lineno, odp->link_name );
481 break;
487 /* create a .o file that references all the undefined symbols we want to resolve */
488 static char *create_undef_symbols_file( DLLSPEC *spec )
490 char *as_file, *obj_file;
491 int i;
492 unsigned int j;
493 FILE *f;
495 as_file = get_temp_file_name( output_file_name, ".s" );
496 if (!(f = fopen( as_file, "w" ))) fatal_error( "Cannot create %s\n", as_file );
497 fprintf( f, "\t.data\n" );
499 for (i = 0; i < spec->nb_entry_points; i++)
501 ORDDEF *odp = &spec->entry_points[i];
502 if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
503 if (odp->flags & FLAG_FORWARD) continue;
504 fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
506 for (j = 0; j < extra_ld_symbols.count; j++)
507 fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(extra_ld_symbols.str[j]) );
508 fclose( f );
510 obj_file = get_temp_file_name( output_file_name, ".o" );
511 assemble_file( as_file, obj_file );
512 return obj_file;
515 /* combine a list of object files with ld into a single object file */
516 /* returns the name of the combined file */
517 static const char *ldcombine_files( DLLSPEC *spec, char **argv )
519 char *ld_tmp_file, *undef_file;
520 struct strarray args = get_ld_command();
522 undef_file = create_undef_symbols_file( spec );
523 ld_tmp_file = get_temp_file_name( output_file_name, ".o" );
525 strarray_add( &args, "-r", "-o", ld_tmp_file, undef_file, NULL );
526 strarray_addv( &args, argv );
527 spawn( args );
528 return ld_tmp_file;
531 /* read in the list of undefined symbols */
532 void read_undef_symbols( DLLSPEC *spec, char **argv )
534 size_t prefix_len;
535 FILE *f;
536 const char *prog = get_nm_command();
537 char *cmd, buffer[1024], name_prefix[16];
538 int err;
539 const char *name;
541 if (!argv[0]) return;
543 add_extra_undef_symbols( spec );
545 strcpy( name_prefix, asm_name("") );
546 prefix_len = strlen( name_prefix );
548 name = ldcombine_files( spec, argv );
550 cmd = strmake( "%s -u %s", prog, name );
551 if (!(f = popen( cmd, "r" )))
552 fatal_error( "Cannot execute '%s'\n", cmd );
554 while (fgets( buffer, sizeof(buffer), f ))
556 char *p = buffer + strlen(buffer) - 1;
557 if (p < buffer) continue;
558 if (*p == '\n') *p-- = 0;
559 p = buffer;
560 while (*p == ' ') p++;
561 if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
562 if (prefix_len && !strncmp( p, name_prefix, prefix_len )) p += prefix_len;
563 if (!strncmp( p, import_func_prefix, strlen(import_func_prefix) ))
564 add_undef_import( p + strlen( import_func_prefix ), 0 );
565 else if (!strncmp( p, import_ord_prefix, strlen(import_ord_prefix) ))
566 add_undef_import( p + strlen( import_ord_prefix ), 1 );
567 else strarray_add( &undef_symbols, xstrdup( p ), NULL );
569 if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
570 free( cmd );
573 void resolve_dll_imports( DLLSPEC *spec, struct list *list )
575 unsigned int j;
576 struct import *imp, *next;
577 ORDDEF *odp;
579 LIST_FOR_EACH_ENTRY_SAFE( imp, next, list, struct import, entry )
581 for (j = 0; j < undef_symbols.count; j++)
583 odp = find_export( undef_symbols.str[j], imp->exports, imp->nb_exports );
584 if (odp)
586 if (odp->flags & FLAG_PRIVATE) continue;
587 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL)
588 warning( "winebuild: Data export '%s' cannot be imported from %s\n",
589 odp->link_name, imp->dll_name );
590 else
592 add_import_func( imp, (odp->flags & FLAG_NONAME) ? NULL : odp->name,
593 odp->export_name, odp->ordinal );
594 remove_name( &undef_symbols, j-- );
598 if (!imp->nb_imports)
600 /* the dll is not used, get rid of it */
601 if (check_unused( imp, spec ))
602 warning( "winebuild: %s imported but no symbols used\n", imp->dll_name );
603 list_remove( &imp->entry );
604 free_imports( imp );
609 /* resolve the imports for a Win32 module */
610 void resolve_imports( DLLSPEC *spec )
612 check_undefined_forwards( spec );
613 resolve_dll_imports( spec, &dll_imports );
614 resolve_dll_imports( spec, &dll_delayed );
615 sort_names( &undef_symbols );
616 check_undefined_exports( spec );
619 /* check if symbol is still undefined */
620 int is_undefined( const char *name )
622 return find_name( name, &undef_symbols ) != NULL;
625 /* output the get_pc thunk if needed */
626 void output_get_pc_thunk(void)
628 assert( target_cpu == CPU_x86 );
629 output( "\n\t.text\n" );
630 output( "\t.align %d\n", get_alignment(4) );
631 output( "\t%s\n", func_declaration("__wine_spec_get_pc_thunk_eax") );
632 output( "%s:\n", asm_name("__wine_spec_get_pc_thunk_eax") );
633 output_cfi( ".cfi_startproc" );
634 output( "\tmovl (%%esp),%%eax\n" );
635 output( "\tret\n" );
636 output_cfi( ".cfi_endproc" );
637 output_function_size( "__wine_spec_get_pc_thunk_eax" );
640 /* output a single import thunk */
641 static void output_import_thunk( const char *name, const char *table, int pos )
643 output( "\n\t.align %d\n", get_alignment(4) );
644 output( "\t%s\n", func_declaration(name) );
645 output( "%s\n", asm_globl(name) );
646 output_cfi( ".cfi_startproc" );
648 switch(target_cpu)
650 case CPU_x86:
651 if (!UsePIC)
653 output( "\tjmp *(%s+%d)\n", table, pos );
655 else
657 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
658 output( "1:\tjmp *%s+%d-1b(%%eax)\n", table, pos );
659 needs_get_pc_thunk = 1;
661 break;
662 case CPU_x86_64:
663 output( "\tjmpq *%s+%d(%%rip)\n", table, pos );
664 break;
665 case CPU_ARM:
666 output( "\tldr IP,1f\n");
667 output( "\tldr PC,[PC,IP]\n" );
668 output( "1:\t.long %s+%u-(1b+4)\n", table, pos );
669 break;
670 case CPU_ARM64:
671 output( "\tadrp x9, %s\n", table );
672 output( "\tadd x9, x9, #:lo12:%s\n", table );
673 if (pos & 0xf000) output( "\tadd x9, x9, #%u\n", pos & 0xf000 );
674 if (pos & 0x0f00) output( "\tadd x9, x9, #%u\n", pos & 0x0f00 );
675 if (pos & 0x00f0) output( "\tadd x9, x9, #%u\n", pos & 0x00f0 );
676 if (pos & 0x000f) output( "\tadd x9, x9, #%u\n", pos & 0x000f );
677 output( "\tldur x9, [x9, #0]\n" );
678 output( "\tbr x9\n" );
679 break;
680 case CPU_POWERPC:
681 output( "\tmr %s, %s\n", ppc_reg(0), ppc_reg(31) );
682 if (target_platform == PLATFORM_APPLE)
684 output( "\tlis %s, ha16(%s+%d+32768)\n", ppc_reg(31), table, pos );
685 output( "\tla %s, lo16(%s+%d)(%s)\n", ppc_reg(31), table, pos, ppc_reg(31) );
687 else
689 output( "\tlis %s, (%s+%d+32768)@h\n", ppc_reg(31), table, pos );
690 output( "\tla %s, (%s+%d)@l(%s)\n", ppc_reg(31), table, pos, ppc_reg(31) );
692 output( "\tlwz %s, 0(%s)\n", ppc_reg(31), ppc_reg(31) );
693 output( "\tmtctr %s\n", ppc_reg(31) );
694 output( "\tmr %s, %s\n", ppc_reg(31), ppc_reg(0) );
695 output( "\tbctr\n" );
696 break;
698 output_cfi( ".cfi_endproc" );
699 output_function_size( name );
702 /* check if we need an import directory */
703 int has_imports(void)
705 return !list_empty( &dll_imports );
708 /* output the import table of a Win32 module */
709 static void output_immediate_imports(void)
711 int i, j;
712 struct import *import;
714 if (list_empty( &dll_imports )) return; /* no immediate imports */
716 /* main import header */
718 output( "\n/* import table */\n" );
719 output( "\n\t.data\n" );
720 output( "\t.align %d\n", get_alignment(4) );
721 output( ".L__wine_spec_imports:\n" );
723 /* list of dlls */
725 j = 0;
726 LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
728 output( "\t.long .L__wine_spec_import_data_names+%d-.L__wine_spec_rva_base\n", /* OriginalFirstThunk */
729 j * get_ptr_size() );
730 output( "\t.long 0\n" ); /* TimeDateStamp */
731 output( "\t.long 0\n" ); /* ForwarderChain */
732 output( "\t.long .L__wine_spec_import_name_%s-.L__wine_spec_rva_base\n", /* Name */
733 import->c_name );
734 output( "\t.long .L__wine_spec_import_data_ptrs+%d-.L__wine_spec_rva_base\n", /* FirstThunk */
735 j * get_ptr_size() );
736 j += import->nb_imports + 1;
738 output( "\t.long 0\n" ); /* OriginalFirstThunk */
739 output( "\t.long 0\n" ); /* TimeDateStamp */
740 output( "\t.long 0\n" ); /* ForwarderChain */
741 output( "\t.long 0\n" ); /* Name */
742 output( "\t.long 0\n" ); /* FirstThunk */
744 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
745 /* output the names twice, once for OriginalFirstThunk and once for FirstThunk */
746 for (i = 0; i < 2; i++)
748 output( ".L__wine_spec_import_data_%s:\n", i ? "ptrs" : "names" );
749 LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
751 for (j = 0; j < import->nb_imports; j++)
753 struct import_func *func = &import->imports[j];
754 if (func->name)
755 output( "\t%s .L__wine_spec_import_data_%s_%s-.L__wine_spec_rva_base\n",
756 get_asm_ptr_keyword(), import->c_name, func->name );
757 else
759 if (get_ptr_size() == 8)
760 output( "\t.quad 0x800000000000%04x\n", func->ordinal );
761 else
762 output( "\t.long 0x8000%04x\n", func->ordinal );
765 output( "\t%s 0\n", get_asm_ptr_keyword() );
768 output( ".L__wine_spec_imports_end:\n" );
770 LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
772 for (j = 0; j < import->nb_imports; j++)
774 struct import_func *func = &import->imports[j];
775 if (!func->name) continue;
776 output( "\t.align %d\n", get_alignment(2) );
777 output( ".L__wine_spec_import_data_%s_%s:\n", import->c_name, func->name );
778 output( "\t.short %d\n", func->ordinal );
779 output( "\t%s \"%s\"\n", get_asm_string_keyword(), func->name );
783 LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
785 output( ".L__wine_spec_import_name_%s:\n\t%s \"%s\"\n",
786 import->c_name, get_asm_string_keyword(), import->dll_name );
790 /* output the import thunks of a Win32 module */
791 static void output_immediate_import_thunks(void)
793 int j, pos;
794 struct import *import;
795 static const char import_thunks[] = "__wine_spec_import_thunks";
797 if (list_empty( &dll_imports )) return;
799 output( "\n/* immediate import thunks */\n\n" );
800 output( "\t.text\n" );
801 output( "\t.align %d\n", get_alignment(8) );
802 output( "%s:\n", asm_name(import_thunks));
804 pos = 0;
805 LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
807 for (j = 0; j < import->nb_imports; j++, pos += get_ptr_size())
809 struct import_func *func = &import->imports[j];
810 output_import_thunk( func->name ? func->name : func->export_name,
811 ".L__wine_spec_import_data_ptrs", pos );
813 pos += get_ptr_size();
815 output_function_size( import_thunks );
818 /* output the delayed import table of a Win32 module */
819 static void output_delayed_imports( const DLLSPEC *spec )
821 int j, mod;
822 struct import *import;
824 if (list_empty( &dll_delayed )) return;
826 output( "\n/* delayed imports */\n\n" );
827 output( "\t.data\n" );
828 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
829 output( "%s\n", asm_globl("__wine_spec_delay_imports") );
831 /* list of dlls */
833 j = mod = 0;
834 LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
836 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
837 output( "\t%s .L__wine_delay_name_%s\n", /* szName */
838 get_asm_ptr_keyword(), import->c_name );
839 output( "\t%s .L__wine_delay_modules+%d\n", /* phmod */
840 get_asm_ptr_keyword(), mod * get_ptr_size() );
841 output( "\t%s .L__wine_delay_IAT+%d\n", /* pIAT */
842 get_asm_ptr_keyword(), j * get_ptr_size() );
843 output( "\t%s .L__wine_delay_INT+%d\n", /* pINT */
844 get_asm_ptr_keyword(), j * get_ptr_size() );
845 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
846 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
847 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
848 j += import->nb_imports;
849 mod++;
851 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
852 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* szName */
853 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* phmod */
854 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pIAT */
855 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pINT */
856 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
857 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
858 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
860 output( "\n.L__wine_delay_IAT:\n" );
861 LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
863 for (j = 0; j < import->nb_imports; j++)
865 struct import_func *func = &import->imports[j];
866 const char *name = func->name ? func->name : func->export_name;
867 output( "\t%s .L__wine_delay_imp_%s_%s\n",
868 get_asm_ptr_keyword(), import->c_name, name );
872 output( "\n.L__wine_delay_INT:\n" );
873 LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
875 for (j = 0; j < import->nb_imports; j++)
877 struct import_func *func = &import->imports[j];
878 if (!func->name)
879 output( "\t%s %d\n", get_asm_ptr_keyword(), func->ordinal );
880 else
881 output( "\t%s .L__wine_delay_data_%s_%s\n",
882 get_asm_ptr_keyword(), import->c_name, func->name );
886 output( "\n.L__wine_delay_modules:\n" );
887 LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
889 output( "\t%s 0\n", get_asm_ptr_keyword() );
892 LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
894 output( ".L__wine_delay_name_%s:\n", import->c_name );
895 output( "\t%s \"%s\"\n", get_asm_string_keyword(), import->dll_name );
898 LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
900 for (j = 0; j < import->nb_imports; j++)
902 struct import_func *func = &import->imports[j];
903 if (!func->name) continue;
904 output( ".L__wine_delay_data_%s_%s:\n", import->c_name, func->name );
905 output( "\t%s \"%s\"\n", get_asm_string_keyword(), func->name );
908 output_function_size( "__wine_spec_delay_imports" );
911 /* output the delayed import thunks of a Win32 module */
912 static void output_delayed_import_thunks( const DLLSPEC *spec )
914 int idx, j, pos, extra_stack_storage = 0;
915 struct import *import;
916 static const char delayed_import_loaders[] = "__wine_spec_delayed_import_loaders";
917 static const char delayed_import_thunks[] = "__wine_spec_delayed_import_thunks";
919 if (list_empty( &dll_delayed )) return;
921 output( "\n/* delayed import thunks */\n\n" );
922 output( "\t.text\n" );
923 output( "\t.align %d\n", get_alignment(8) );
924 output( "%s:\n", asm_name(delayed_import_loaders));
925 output( "\t%s\n", func_declaration("__wine_delay_load_asm") );
926 output( "%s:\n", asm_name("__wine_delay_load_asm") );
927 output_cfi( ".cfi_startproc" );
928 switch(target_cpu)
930 case CPU_x86:
931 output( "\tpushl %%ecx\n" );
932 output_cfi( ".cfi_adjust_cfa_offset 4" );
933 output( "\tpushl %%edx\n" );
934 output_cfi( ".cfi_adjust_cfa_offset 4" );
935 output( "\tpushl %%eax\n" );
936 output_cfi( ".cfi_adjust_cfa_offset 4" );
937 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
938 output_cfi( ".cfi_adjust_cfa_offset -4" );
939 output( "\tpopl %%edx\n" );
940 output_cfi( ".cfi_adjust_cfa_offset -4" );
941 output( "\tpopl %%ecx\n" );
942 output_cfi( ".cfi_adjust_cfa_offset -4" );
943 output( "\tjmp *%%eax\n" );
944 break;
945 case CPU_x86_64:
946 output( "\tsubq $88,%%rsp\n" );
947 output_cfi( ".cfi_adjust_cfa_offset 88" );
948 output( "\tmovq %%rdx,80(%%rsp)\n" );
949 output( "\tmovq %%rcx,72(%%rsp)\n" );
950 output( "\tmovq %%r8,64(%%rsp)\n" );
951 output( "\tmovq %%r9,56(%%rsp)\n" );
952 output( "\tmovq %%r10,48(%%rsp)\n" );
953 output( "\tmovq %%r11,40(%%rsp)\n" );
954 output( "\tmovq %%rax,%%rcx\n" );
955 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
956 output( "\tmovq 40(%%rsp),%%r11\n" );
957 output( "\tmovq 48(%%rsp),%%r10\n" );
958 output( "\tmovq 56(%%rsp),%%r9\n" );
959 output( "\tmovq 64(%%rsp),%%r8\n" );
960 output( "\tmovq 72(%%rsp),%%rcx\n" );
961 output( "\tmovq 80(%%rsp),%%rdx\n" );
962 output( "\taddq $88,%%rsp\n" );
963 output_cfi( ".cfi_adjust_cfa_offset -88" );
964 output( "\tjmp *%%rax\n" );
965 break;
966 case CPU_ARM:
967 output( "\tpush {r0-r3,FP,LR}\n" );
968 output( "\tmov r0,IP\n" );
969 output( "\tldr IP,2f\n");
970 output( "\tadd IP,PC\n");
971 output( "\tblx IP\n");
972 output( "1:\tmov IP,r0\n");
973 output( "\tpop {r0-r3,FP,LR}\n" );
974 output( "\tbx IP\n");
975 output( "2:\t.long %s-1b\n", asm_name("__wine_spec_delay_load") );
976 break;
977 case CPU_ARM64:
978 output( "\tstp x29, x30, [sp,#-16]!\n" );
979 output( "\tmov x29, sp\n" );
980 output( "\tadrp x9, %s\n", asm_name("__wine_spec_delay_load") );
981 output( "\tadd x9, x9, #:lo12:%s\n", asm_name("__wine_spec_delay_load") );
982 output( "\tblr x9\n" );
983 output( "\tmov x9, x0\n" );
984 output( "\tldp x29, x30, [sp],#16\n" );
985 output( "\tldp x0, x1, [sp,#16]\n" );
986 output( "\tldp x2, x3, [sp,#32]\n" );
987 output( "\tldp x4, x5, [sp,#48]\n" );
988 output( "\tldp x6, x7, [sp],#80\n" );
989 output( "\tbr x9\n" ); /* or "ret x9" */
990 break;
991 case CPU_POWERPC:
992 if (target_platform == PLATFORM_APPLE) extra_stack_storage = 56;
994 /* Save all callee saved registers into a stackframe. */
995 output( "\tstwu %s, -%d(%s)\n",ppc_reg(1), 48+extra_stack_storage, ppc_reg(1));
996 output( "\tstw %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage, ppc_reg(1));
997 output( "\tstw %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage, ppc_reg(1));
998 output( "\tstw %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
999 output( "\tstw %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
1000 output( "\tstw %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
1001 output( "\tstw %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
1002 output( "\tstw %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
1003 output( "\tstw %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
1004 output( "\tstw %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
1005 output( "\tstw %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
1007 /* r0 -> r3 (arg1) */
1008 output( "\tmr %s, %s\n", ppc_reg(3), ppc_reg(0));
1010 /* save return address */
1011 output( "\tmflr %s\n", ppc_reg(0));
1012 output( "\tstw %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
1014 /* Call the __wine_delay_load function, arg1 is arg1. */
1015 output( "\tbl %s\n", asm_name("__wine_spec_delay_load") );
1017 /* Load return value from call into ctr register */
1018 output( "\tmtctr %s\n", ppc_reg(3));
1020 /* restore all saved registers and drop stackframe. */
1021 output( "\tlwz %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage, ppc_reg(1));
1022 output( "\tlwz %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage, ppc_reg(1));
1023 output( "\tlwz %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
1024 output( "\tlwz %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
1025 output( "\tlwz %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
1026 output( "\tlwz %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
1027 output( "\tlwz %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
1028 output( "\tlwz %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
1029 output( "\tlwz %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
1030 output( "\tlwz %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
1032 /* Load return value from call into return register */
1033 output( "\tlwz %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
1034 output( "\tmtlr %s\n", ppc_reg(0));
1035 output( "\taddi %s, %s, %d\n", ppc_reg(1), ppc_reg(1), 48+extra_stack_storage);
1037 /* branch to ctr register. */
1038 output( "\tbctr\n");
1039 break;
1041 output_cfi( ".cfi_endproc" );
1042 output_function_size( "__wine_delay_load_asm" );
1043 output( "\n" );
1045 idx = 0;
1046 LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
1048 for (j = 0; j < import->nb_imports; j++)
1050 struct import_func *func = &import->imports[j];
1051 const char *name = func->name ? func->name : func->export_name;
1053 output( ".L__wine_delay_imp_%s_%s:\n", import->c_name, name );
1054 output_cfi( ".cfi_startproc" );
1055 switch(target_cpu)
1057 case CPU_x86:
1058 output( "\tmovl $%d, %%eax\n", (idx << 16) | j );
1059 output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1060 break;
1061 case CPU_x86_64:
1062 output( "\tmovq $%d,%%rax\n", (idx << 16) | j );
1063 output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1064 break;
1065 case CPU_ARM:
1067 unsigned int mask, count = 0, val = (idx << 16) | j;
1069 for (mask = 0xff; mask; mask <<= 8)
1070 if (val & mask) output( "\t%s IP,#%u\n", count++ ? "add" : "mov", val & mask );
1071 if (!count) output( "\tmov IP,#0\n" );
1072 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1073 break;
1075 case CPU_ARM64:
1076 output( "\tstp x6, x7, [sp,#-80]!\n" );
1077 output( "\tstp x4, x5, [sp,#48]\n" );
1078 output( "\tstp x2, x3, [sp,#32]\n" );
1079 output( "\tstp x0, x1, [sp,#16]\n" );
1080 output( "\tmov x0, #%d\n", idx );
1081 output( "\tmov x1, #16384\n" );
1082 output( "\tmul x1, x0, x1\n" );
1083 output( "\tmov x0, x1\n" );
1084 output( "\tmov x1, #4\n" );
1085 output( "\tmul x1, x0, x1\n" );
1086 output( "\tmov x0, x1\n" );
1087 output( "\tadd x0, x0, #%d\n", j );
1088 output( "\tadr x9, %s\n", asm_name("__wine_delay_load_asm") );
1089 output( "\tbr x9\n" );
1090 break;
1091 case CPU_POWERPC:
1092 switch(target_platform)
1094 case PLATFORM_APPLE:
1095 /* On Darwin we can use r0 and r2 */
1096 /* Upper part in r2 */
1097 output( "\tlis %s, %d\n", ppc_reg(2), idx);
1098 /* Lower part + r2 -> r0, Note we can't use r0 directly */
1099 output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(2), j);
1100 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1101 break;
1102 default:
1103 /* On linux we can't use r2 since r2 is not a scratch register (hold the TOC) */
1104 /* Save r13 on the stack */
1105 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1));
1106 output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
1107 /* Upper part in r13 */
1108 output( "\tlis %s, %d\n", ppc_reg(13), idx);
1109 /* Lower part + r13 -> r0, Note we can't use r0 directly */
1110 output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(13), j);
1111 /* Restore r13 */
1112 output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
1113 output( "\taddic %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1));
1114 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1115 break;
1117 break;
1119 output_cfi( ".cfi_endproc" );
1121 idx++;
1123 output_function_size( delayed_import_loaders );
1125 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
1126 output( "%s:\n", asm_name(delayed_import_thunks));
1127 pos = 0;
1128 LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
1130 for (j = 0; j < import->nb_imports; j++, pos += get_ptr_size())
1132 struct import_func *func = &import->imports[j];
1133 output_import_thunk( func->name ? func->name : func->export_name,
1134 ".L__wine_delay_IAT", pos );
1137 output_function_size( delayed_import_thunks );
1140 /* output import stubs for exported entry points that link to external symbols */
1141 static void output_external_link_imports( DLLSPEC *spec )
1143 unsigned int i, pos;
1145 if (!ext_link_imports.count) return; /* nothing to do */
1147 sort_names( &ext_link_imports );
1149 /* get rid of duplicate names */
1150 for (i = 1; i < ext_link_imports.count; i++)
1152 if (!strcmp( ext_link_imports.str[i-1], ext_link_imports.str[i] ))
1153 remove_name( &ext_link_imports, i-- );
1156 output( "\n/* external link thunks */\n\n" );
1157 output( "\t.data\n" );
1158 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
1159 output( ".L__wine_spec_external_links:\n" );
1160 for (i = 0; i < ext_link_imports.count; i++)
1161 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(ext_link_imports.str[i]) );
1163 output( "\n\t.text\n" );
1164 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
1165 output( "%s:\n", asm_name("__wine_spec_external_link_thunks") );
1167 for (i = pos = 0; i < ext_link_imports.count; i++)
1169 char *buffer = strmake( "__wine_spec_ext_link_%s", ext_link_imports.str[i] );
1170 output_import_thunk( buffer, ".L__wine_spec_external_links", pos );
1171 free( buffer );
1172 pos += get_ptr_size();
1174 output_function_size( "__wine_spec_external_link_thunks" );
1177 /*******************************************************************
1178 * output_stubs
1180 * Output the functions for stub entry points
1182 void output_stubs( DLLSPEC *spec )
1184 const char *name, *exp_name;
1185 int i, count;
1187 if (!has_stubs( spec )) return;
1189 output( "\n/* stub functions */\n\n" );
1190 output( "\t.text\n" );
1192 for (i = count = 0; i < spec->nb_entry_points; i++)
1194 ORDDEF *odp = &spec->entry_points[i];
1195 if (odp->type != TYPE_STUB) continue;
1197 name = get_stub_name( odp, spec );
1198 exp_name = odp->name ? odp->name : odp->export_name;
1199 output( "\t.align %d\n", get_alignment(4) );
1200 output( "\t%s\n", func_declaration(name) );
1201 output( "%s:\n", asm_name(name) );
1202 output_cfi( ".cfi_startproc" );
1204 switch (target_cpu)
1206 case CPU_x86:
1207 /* flesh out the stub a bit to make safedisc happy */
1208 output(" \tnop\n" );
1209 output(" \tnop\n" );
1210 output(" \tnop\n" );
1211 output(" \tnop\n" );
1212 output(" \tnop\n" );
1213 output(" \tnop\n" );
1214 output(" \tnop\n" );
1215 output(" \tnop\n" );
1216 output(" \tnop\n" );
1218 output( "\tsubl $12,%%esp\n" );
1219 output_cfi( ".cfi_adjust_cfa_offset 12" );
1220 if (UsePIC)
1222 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
1223 output( "1:" );
1224 needs_get_pc_thunk = 1;
1225 if (exp_name)
1227 output( "\tleal .L%s_string-1b(%%eax),%%ecx\n", name );
1228 output( "\tmovl %%ecx,4(%%esp)\n" );
1229 count++;
1231 else
1232 output( "\tmovl $%d,4(%%esp)\n", odp->ordinal );
1233 output( "\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
1234 output( "\tmovl %%ecx,(%%esp)\n" );
1236 else
1238 if (exp_name)
1240 output( "\tmovl $.L%s_string,4(%%esp)\n", name );
1241 count++;
1243 else
1244 output( "\tmovl $%d,4(%%esp)\n", odp->ordinal );
1245 output( "\tmovl $.L__wine_spec_file_name,(%%esp)\n" );
1247 output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
1248 break;
1249 case CPU_x86_64:
1250 output( "\tsubq $8,%%rsp\n" );
1251 output_cfi( ".cfi_adjust_cfa_offset 8" );
1252 output( "\tleaq .L__wine_spec_file_name(%%rip),%%rdi\n" );
1253 if (exp_name)
1255 output( "leaq .L%s_string(%%rip),%%rsi\n", name );
1256 count++;
1258 else
1259 output( "\tmovq $%d,%%rsi\n", odp->ordinal );
1260 output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
1261 break;
1262 case CPU_ARM:
1263 output( "\tldr r0,2f\n");
1264 output( "\tadd r0,PC\n");
1265 output( "\tldr r1,2f+4\n");
1266 output( "1:" );
1267 if (exp_name)
1269 output( "\tadd r1,PC\n");
1270 count++;
1272 output( "\tbl %s\n", asm_name("__wine_spec_unimplemented_stub") );
1273 output( "2:\t.long .L__wine_spec_file_name-1b\n" );
1274 if (exp_name) output( "\t.long .L%s_string-2b\n", name );
1275 else output( "\t.long %u\n", odp->ordinal );
1276 break;
1277 case CPU_ARM64:
1278 output( "\tadrp x0, %s\n", asm_name("__wine_spec_file_name") );
1279 output( "\tadd x0, x0, #:lo12:%s\n", asm_name("__wine_spec_file_name") );
1280 if (exp_name)
1282 output( "\tadrp x1, .L%s_string\n", name );
1283 output( "\tadd x1, x1, #:lo12:.L%s_string\n", name );
1284 count++;
1286 else
1287 output( "\tmov x1, %u\n", odp->ordinal );
1288 output( "\tadrp x2, %s\n", asm_name("__wine_spec_unimplemented_stub") );
1289 output( "\tadd x2, x2, #:lo12:%s\n", asm_name("__wine_spec_unimplemented_stub") );
1290 output( "\tblr x2\n" );
1291 break;
1292 default:
1293 assert(0);
1295 output_cfi( ".cfi_endproc" );
1296 output_function_size( name );
1299 if (count)
1301 output( "\t%s\n", get_asm_string_section() );
1302 for (i = 0; i < spec->nb_entry_points; i++)
1304 ORDDEF *odp = &spec->entry_points[i];
1305 if (odp->type != TYPE_STUB) continue;
1306 exp_name = odp->name ? odp->name : odp->export_name;
1307 if (exp_name)
1309 name = get_stub_name( odp, spec );
1310 output( ".L%s_string:\n", name );
1311 output( "\t%s \"%s\"\n", get_asm_string_keyword(), exp_name );
1317 /* output the import and delayed import tables of a Win32 module */
1318 void output_imports( DLLSPEC *spec )
1320 output_immediate_imports();
1321 output_delayed_imports( spec );
1322 output_immediate_import_thunks();
1323 output_delayed_import_thunks( spec );
1324 output_external_link_imports( spec );
1327 /* create a new asm temp file */
1328 static void new_output_as_file( const char *prefix )
1330 char *name = get_temp_file_name( prefix, ".s" );
1332 if (output_file) fclose( output_file );
1333 if (!(output_file = fopen( name, "w" )))
1334 fatal_error( "Unable to create output file '%s'\n", name );
1335 strarray_add( &as_files, name, NULL );
1338 /* assemble all the asm files */
1339 static void assemble_files( const char *prefix )
1341 unsigned int i;
1343 if (output_file) fclose( output_file );
1344 output_file = NULL;
1346 for (i = 0; i < as_files.count; i++)
1348 char *obj = get_temp_file_name( prefix, ".o" );
1349 assemble_file( as_files.str[i], obj );
1350 as_files.str[i] = obj;
1354 /* build a library from the current asm files and any additional object files in argv */
1355 static void build_library( const char *output_name, char **argv, int create )
1357 struct strarray args = find_tool( "ar", NULL );
1358 struct strarray ranlib = find_tool( "ranlib", NULL );
1360 strarray_add( &args, create ? "rc" : "r", output_name, NULL );
1361 strarray_addall( &args, as_files );
1362 strarray_addv( &args, argv );
1363 if (create) unlink( output_name );
1364 spawn( args );
1366 strarray_add( &ranlib, output_name, NULL );
1367 spawn( ranlib );
1370 /* create a Windows-style import library */
1371 static void build_windows_import_lib( DLLSPEC *spec )
1373 struct strarray args;
1374 char *def_file;
1375 const char *as_flags, *m_flag;
1377 def_file = get_temp_file_name( output_file_name, ".def" );
1378 fclose( output_file );
1379 if (!(output_file = fopen( def_file, "w" )))
1380 fatal_error( "Unable to create output file '%s'\n", def_file );
1381 output_def_file( spec, 0 );
1382 fclose( output_file );
1383 output_file = NULL;
1385 args = find_tool( "dlltool", NULL );
1386 switch (target_cpu)
1388 case CPU_x86:
1389 m_flag = "i386";
1390 as_flags = "--as-flags=--32";
1391 break;
1392 case CPU_x86_64:
1393 m_flag = "i386:x86-64";
1394 as_flags = "--as-flags=--64";
1395 break;
1396 default:
1397 m_flag = NULL;
1398 break;
1400 strarray_add( &args, "-k", "-l", output_file_name, "-d", def_file, NULL );
1401 if (m_flag)
1402 strarray_add( &args, "-m", m_flag, as_flags, NULL );
1403 spawn( args );
1406 /* create a Unix-style import library */
1407 static void build_unix_import_lib( DLLSPEC *spec )
1409 static const char valid_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._@";
1410 int i, total;
1411 const char *name, *prefix;
1412 char *dll_name = xstrdup( spec->file_name );
1414 if (strendswith( dll_name, ".dll" )) dll_name[strlen(dll_name) - 4] = 0;
1415 if (strspn( dll_name, valid_chars ) < strlen( dll_name ))
1416 fatal_error( "%s contains invalid characters\n", spec->file_name );
1418 /* entry points */
1420 for (i = total = 0; i < spec->nb_entry_points; i++)
1422 const ORDDEF *odp = &spec->entry_points[i];
1424 if (odp->name) name = odp->name;
1425 else if (odp->export_name) name = odp->export_name;
1426 else continue;
1428 if (odp->flags & FLAG_PRIVATE) continue;
1429 total++;
1431 /* C++ mangled names cannot be imported */
1432 if (strpbrk( name, "?@" )) continue;
1434 switch(odp->type)
1436 case TYPE_VARARGS:
1437 case TYPE_CDECL:
1438 case TYPE_STDCALL:
1439 case TYPE_THISCALL:
1440 prefix = (!odp->name || (odp->flags & FLAG_ORDINAL)) ? import_ord_prefix : import_func_prefix;
1441 new_output_as_file( spec->file_name );
1442 output( "\t.text\n" );
1443 output( "\n\t.align %d\n", get_alignment( get_ptr_size() ));
1444 output( "\t%s\n", func_declaration( name ) );
1445 output( "%s\n", asm_globl( name ) );
1446 output( "\t%s %s%s$%u$%s\n", get_asm_ptr_keyword(),
1447 asm_name( prefix ), dll_name, odp->ordinal, name );
1448 output_function_size( name );
1449 output_gnu_stack_note();
1450 break;
1452 default:
1453 break;
1456 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
1458 if (!as_files.count) /* create a dummy file to avoid empty import libraries */
1460 new_output_as_file( spec->file_name );
1461 output( "\t.text\n" );
1464 assemble_files( spec->file_name );
1465 free( dll_name );
1468 /* output an import library for a Win32 module and additional object files */
1469 void output_import_lib( DLLSPEC *spec, char **argv )
1471 if (target_platform == PLATFORM_WINDOWS)
1473 build_windows_import_lib( spec );
1474 if (argv[0]) build_library( output_file_name, argv, 0 );
1476 else
1478 build_unix_import_lib( spec );
1479 build_library( output_file_name, argv, 1 );
1481 output_file_name = NULL;