4 * Copyright 2000 Alexandre Julliard
16 char *dll
; /* dll name */
17 char **imports
; /* functions we want to import from this dll */
18 int nb_imports
; /* number of imported functions */
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 */
27 /* add a dll to the list of imports */
28 void add_import_dll( const char *name
)
30 struct import
*imp
= xmalloc( sizeof(*imp
) );
31 imp
->dll
= xstrdup( name
);
35 dll_imports
= xrealloc( dll_imports
, (nb_imports
+1) * sizeof(*dll_imports
) );
36 dll_imports
[nb_imports
++] = imp
;
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
)
43 imp
->imports
= xrealloc( imp
->imports
, (imp
->nb_imports
+1) * sizeof(*imp
->imports
) );
44 imp
->imports
[imp
->nb_imports
++] = xstrdup( name
);
49 /* output the import table of a Win32 module */
50 int output_imports( FILE *outfile
)
54 if (!nb_imports
) goto done
;
56 /* main import header */
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" );
72 for (i
= j
= 0; i
< nb_imports
; i
++)
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;
78 fprintf( outfile
, " { 0, 0, 0, 0, 0 },\n" );
79 fprintf( outfile
, " },\n {\n" );
81 /* list of imported functions */
83 for (i
= 0; i
< nb_imports
; i
++)
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" );
90 fprintf( outfile
, " }\n};\n\n" );
92 /* thunks for imported functions */
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)
99 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++, pos
+= 4)
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
);
110 fprintf( outfile
, "#ifndef __GNUC__\n}\n#endif\n\n" );