Added stdcall64 entry point type to allow correct relay debugging
[wine] / tools / winebuild / import.c
1 /*
2  * DLL imports support
3  *
4  * Copyright 2000 Alexandre Julliard
5  */
6
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <unistd.h>
10
11 #include "winnt.h"
12 #include "build.h"
13
14 struct import
15 {
16     char        *dll;         /* dll name */
17     char       **imports;     /* functions we want to import from this dll */
18     int          nb_imports;  /* number of imported functions */
19 };
20
21
22 static struct import **dll_imports = NULL;
23 static int nb_imports = 0;  /* number of imported dlls */
24 static int total_imports = 0;  /* total number of imported functions */
25
26
27 /* add a dll to the list of imports */
28 void add_import_dll( const char *name )
29 {
30     struct import *imp = xmalloc( sizeof(*imp) );
31     imp->dll        = xstrdup( name );
32     imp->imports    = NULL;
33     imp->nb_imports = 0;
34
35     dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
36     dll_imports[nb_imports++] = imp;
37 }
38
39 #ifdef notyet
40 /* add a function to the list of imports from a given dll */
41 static void add_import_func( struct import *imp, const char *name )
42 {
43     imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
44     imp->imports[imp->nb_imports++] = xstrdup( name );
45     total_imports++;
46 }
47 #endif
48
49 /* output the import table of a Win32 module */
50 int output_imports( FILE *outfile )
51 {
52     int i, j, pos;
53
54     if (!nb_imports) goto done;
55
56     /* main import header */
57
58     fprintf( outfile, "\n\n/* imports */\n\n" );
59     fprintf( outfile, "static struct {\n" );
60     fprintf( outfile, "  struct {\n" );
61     fprintf( outfile, "    void        *OriginalFirstThunk;\n" );
62     fprintf( outfile, "    unsigned int TimeDateStamp;\n" );
63     fprintf( outfile, "    unsigned int ForwarderChain;\n" );
64     fprintf( outfile, "    const char  *Name;\n" );
65     fprintf( outfile, "    void        *FirstThunk;\n" );
66     fprintf( outfile, "  } imp[%d];\n", nb_imports+1 );
67     fprintf( outfile, "  const char *data[%d];\n", total_imports + nb_imports );
68     fprintf( outfile, "} imports = {\n  {\n" );
69
70     /* list of dlls */
71
72     for (i = j = 0; i < nb_imports; i++)
73     {
74         fprintf( outfile, "    { 0, 0, 0, \"%s\", &imports.data[%d] },\n",
75                  dll_imports[i]->dll, j );
76         j += dll_imports[i]->nb_imports + 1;
77     }
78     fprintf( outfile, "    { 0, 0, 0, 0, 0 },\n" );
79     fprintf( outfile, "  },\n  {\n" );
80
81     /* list of imported functions */
82
83     for (i = 0; i < nb_imports; i++)
84     {
85         fprintf( outfile, "    /* %s */\n", dll_imports[i]->dll );
86         for (j = 0; j < dll_imports[i]->nb_imports; j++)
87             fprintf( outfile, "    \"\\0\\0%s\",\n", dll_imports[i]->imports[j] );
88         fprintf( outfile, "    0,\n" );
89     }
90     fprintf( outfile, "  }\n};\n\n" );
91
92     /* thunks for imported functions */
93
94     fprintf( outfile, "#ifndef __GNUC__\nstatic void __asm__dummy_import(void) {\n#endif\n\n" );
95     pos = 20 * (nb_imports + 1);  /* offset of imports.data from start of imports */
96     fprintf( outfile, "asm(\".align 4\");\n" );
97     for (i = 0; i < nb_imports; i++, pos += 4)
98     {
99         for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
100         {
101             fprintf( outfile,
102                      "asm(\".type " PREFIX "%s,@function\\n\\t"
103                      ".globl " PREFIX "%s\\n"
104                      PREFIX "%s:\\tjmp *(imports+%d)\\n\\t"
105                      "movl %%esi,%%esi\");\n",
106                      dll_imports[i]->imports[j], dll_imports[i]->imports[j],
107                      dll_imports[i]->imports[j], pos );
108         }
109     }
110     fprintf( outfile, "#ifndef __GNUC__\n}\n#endif\n\n" );
111
112  done:
113     return nb_imports;
114 }