]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* dlltool.c -- tool to generate stuff for PE style DLLs |
a2c58332 | 2 | Copyright (C) 1995-2022 Free Software Foundation, Inc. |
252b5132 RH |
3 | |
4 | This file is part of GNU Binutils. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
32866df7 | 8 | the Free Software Foundation; either version 3 of the License, or |
252b5132 RH |
9 | (at your option) any later version. |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
b43b5d5f NC |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA |
19 | 02110-1301, USA. */ | |
252b5132 RH |
20 | |
21 | ||
c9e38879 | 22 | /* This program allows you to build the files necessary to create |
252b5132 RH |
23 | DLLs to run on a system which understands PE format image files. |
24 | (eg, Windows NT) | |
25 | ||
26 | See "Peering Inside the PE: A Tour of the Win32 Portable Executable | |
27 | File Format", MSJ 1994, Volume 9 for more information. | |
28 | Also see "Microsoft Portable Executable and Common Object File Format, | |
29 | Specification 4.1" for more information. | |
30 | ||
31 | A DLL contains an export table which contains the information | |
32 | which the runtime loader needs to tie up references from a | |
33 | referencing program. | |
34 | ||
35 | The export table is generated by this program by reading | |
36 | in a .DEF file or scanning the .a and .o files which will be in the | |
37 | DLL. A .o file can contain information in special ".drectve" sections | |
38 | with export information. | |
39 | ||
40 | A DEF file contains any number of the following commands: | |
41 | ||
42 | ||
43 | NAME <name> [ , <base> ] | |
44 | The result is going to be <name>.EXE | |
45 | ||
46 | LIBRARY <name> [ , <base> ] | |
47 | The result is going to be <name>.DLL | |
48 | ||
04847a4d CF |
49 | EXPORTS ( ( ( <name1> [ = <name2> ] ) |
50 | | ( <name1> = <module-name> . <external-name>)) | |
7aa52b1f | 51 | [ @ <integer> ] [ NONAME ] [CONSTANT] [DATA] [PRIVATE] ) * |
252b5132 | 52 | Declares name1 as an exported symbol from the |
04847a4d CF |
53 | DLL, with optional ordinal number <integer>. |
54 | Or declares name1 as an alias (forward) of the function <external-name> | |
55 | in the DLL <module-name>. | |
252b5132 RH |
56 | |
57 | IMPORTS ( ( <internal-name> = <module-name> . <integer> ) | |
58 | | ( [ <internal-name> = ] <module-name> . <external-name> )) * | |
c7de9216 | 59 | Declares that <external-name> or the exported function whose ordinal number |
252b5132 RH |
60 | is <integer> is to be imported from the file <module-name>. If |
61 | <internal-name> is specified then this is the name that the imported | |
50c2245b | 62 | function will be refereed to in the body of the DLL. |
252b5132 RH |
63 | |
64 | DESCRIPTION <string> | |
65 | Puts <string> into output .exp file in the .rdata section | |
66 | ||
67 | [STACKSIZE|HEAPSIZE] <number-reserve> [ , <number-commit> ] | |
68 | Generates --stack|--heap <number-reserve>,<number-commit> | |
69 | in the output .drectve section. The linker will | |
70 | see this and act upon it. | |
71 | ||
72 | [CODE|DATA] <attr>+ | |
73 | SECTIONS ( <sectionname> <attr>+ )* | |
74 | <attr> = READ | WRITE | EXECUTE | SHARED | |
75 | Generates --attr <sectionname> <attr> in the output | |
76 | .drectve section. The linker will see this and act | |
77 | upon it. | |
78 | ||
79 | ||
80 | A -export:<name> in a .drectve section in an input .o or .a | |
81 | file to this program is equivalent to a EXPORTS <name> | |
82 | in a .DEF file. | |
83 | ||
84 | ||
85 | ||
86 | The program generates output files with the prefix supplied | |
87 | on the command line, or in the def file, or taken from the first | |
88 | supplied argument. | |
89 | ||
90 | The .exp.s file contains the information necessary to export | |
91 | the routines in the DLL. The .lib.s file contains the information | |
92 | necessary to use the DLL's routines from a referencing program. | |
93 | ||
94 | ||
95 | ||
96 | Example: | |
97 | ||
98 | file1.c: | |
99 | asm (".section .drectve"); | |
100 | asm (".ascii \"-export:adef\""); | |
101 | ||
102 | void adef (char * s) | |
103 | { | |
104 | printf ("hello from the dll %s\n", s); | |
105 | } | |
106 | ||
107 | void bdef (char * s) | |
108 | { | |
109 | printf ("hello from the dll and the other entry point %s\n", s); | |
110 | } | |
111 | ||
112 | file2.c: | |
113 | asm (".section .drectve"); | |
114 | asm (".ascii \"-export:cdef\""); | |
115 | asm (".ascii \"-export:ddef\""); | |
26044998 | 116 | |
252b5132 RH |
117 | void cdef (char * s) |
118 | { | |
119 | printf ("hello from the dll %s\n", s); | |
120 | } | |
121 | ||
122 | void ddef (char * s) | |
123 | { | |
124 | printf ("hello from the dll and the other entry point %s\n", s); | |
125 | } | |
126 | ||
661016bb | 127 | int printf (void) |
252b5132 RH |
128 | { |
129 | return 9; | |
130 | } | |
131 | ||
661016bb NC |
132 | themain.c: |
133 | int main (void) | |
252b5132 | 134 | { |
661016bb NC |
135 | cdef (); |
136 | return 0; | |
252b5132 RH |
137 | } |
138 | ||
139 | thedll.def | |
140 | ||
141 | LIBRARY thedll | |
142 | HEAPSIZE 0x40000, 0x2000 | |
143 | EXPORTS bdef @ 20 | |
144 | cdef @ 30 NONAME | |
145 | ||
146 | SECTIONS donkey READ WRITE | |
147 | aardvark EXECUTE | |
148 | ||
6e7d8205 | 149 | # Compile up the parts of the dll and the program |
252b5132 | 150 | |
6e7d8205 | 151 | gcc -c file1.c file2.c themain.c |
252b5132 | 152 | |
6e7d8205 NC |
153 | # Optional: put the dll objects into a library |
154 | # (you don't have to, you could name all the object | |
155 | # files on the dlltool line) | |
252b5132 RH |
156 | |
157 | ar qcv thedll.in file1.o file2.o | |
158 | ranlib thedll.in | |
159 | ||
6e7d8205 NC |
160 | # Run this tool over the DLL's .def file and generate an exports |
161 | # file (thedll.o) and an imports file (thedll.a). | |
162 | # (You may have to use -S to tell dlltool where to find the assembler). | |
26044998 | 163 | |
aff05906 | 164 | dlltool --def thedll.def --output-exp thedll.o --output-lib thedll.a |
252b5132 | 165 | |
aff05906 | 166 | # Build the dll with the library and the export table |
26044998 | 167 | |
252b5132 RH |
168 | ld -o thedll.dll thedll.o thedll.in |
169 | ||
6e7d8205 | 170 | # Link the executable with the import library |
26044998 | 171 | |
661016bb | 172 | gcc -o themain.exe themain.o thedll.a |
252b5132 | 173 | |
aff05906 NC |
174 | This example can be extended if relocations are needed in the DLL: |
175 | ||
176 | # Compile up the parts of the dll and the program | |
177 | ||
178 | gcc -c file1.c file2.c themain.c | |
179 | ||
180 | # Run this tool over the DLL's .def file and generate an imports file. | |
26044998 | 181 | |
aff05906 NC |
182 | dlltool --def thedll.def --output-lib thedll.lib |
183 | ||
184 | # Link the executable with the import library and generate a base file | |
185 | # at the same time | |
26044998 | 186 | |
aff05906 NC |
187 | gcc -o themain.exe themain.o thedll.lib -Wl,--base-file -Wl,themain.base |
188 | ||
189 | # Run this tool over the DLL's .def file and generate an exports file | |
190 | # which includes the relocations from the base file. | |
26044998 | 191 | |
aff05906 NC |
192 | dlltool --def thedll.def --base-file themain.base --output-exp thedll.exp |
193 | ||
194 | # Build the dll with file1.o, file2.o and the export table | |
26044998 | 195 | |
c9e38879 | 196 | ld -o thedll.dll thedll.exp file1.o file2.o */ |
252b5132 RH |
197 | |
198 | /* .idata section description | |
199 | ||
200 | The .idata section is the import table. It is a collection of several | |
201 | subsections used to keep the pieces for each dll together: .idata$[234567]. | |
202 | IE: Each dll's .idata$2's are catenated together, each .idata$3's, etc. | |
203 | ||
204 | .idata$2 = Import Directory Table | |
205 | = array of IMAGE_IMPORT_DESCRIPTOR's. | |
206 | ||
207 | DWORD Import Lookup Table; - pointer to .idata$4 | |
208 | DWORD TimeDateStamp; - currently always 0 | |
209 | DWORD ForwarderChain; - currently always 0 | |
210 | DWORD Name; - pointer to dll's name | |
211 | PIMAGE_THUNK_DATA FirstThunk; - pointer to .idata$5 | |
212 | ||
213 | .idata$3 = null terminating entry for .idata$2. | |
214 | ||
215 | .idata$4 = Import Lookup Table | |
216 | = array of array of pointers to hint name table. | |
217 | There is one for each dll being imported from, and each dll's set is | |
218 | terminated by a trailing NULL. | |
219 | ||
220 | .idata$5 = Import Address Table | |
221 | = array of array of pointers to hint name table. | |
222 | There is one for each dll being imported from, and each dll's set is | |
223 | terminated by a trailing NULL. | |
224 | Initially, this table is identical to the Import Lookup Table. However, | |
225 | at load time, the loader overwrites the entries with the address of the | |
226 | function. | |
227 | ||
228 | .idata$6 = Hint Name Table | |
229 | = Array of { short, asciz } entries, one for each imported function. | |
230 | The `short' is the function's ordinal number. | |
231 | ||
fe49679d | 232 | .idata$7 = dll name (eg: "kernel32.dll"). */ |
252b5132 | 233 | |
3db64b00 | 234 | #include "sysdep.h" |
252b5132 RH |
235 | #include "bfd.h" |
236 | #include "libiberty.h" | |
252b5132 RH |
237 | #include "getopt.h" |
238 | #include "demangle.h" | |
bb0cb4db | 239 | #include "dyn-string.h" |
3db64b00 | 240 | #include "bucomm.h" |
252b5132 | 241 | #include "dlltool.h" |
3882b010 | 242 | #include "safe-ctype.h" |
aa739c59 | 243 | #include "coff-bfd.h" |
252b5132 | 244 | |
252b5132 | 245 | #include <time.h> |
0fd555c4 NC |
246 | #include <assert.h> |
247 | ||
252b5132 RH |
248 | #ifdef DLLTOOL_ARM |
249 | #include "coff/arm.h" | |
250 | #include "coff/internal.h" | |
251 | #endif | |
e05da72d | 252 | #ifdef DLLTOOL_DEFAULT_MX86_64 |
99ad8390 NC |
253 | #include "coff/x86_64.h" |
254 | #endif | |
e05da72d NC |
255 | #ifdef DLLTOOL_DEFAULT_I386 |
256 | #include "coff/i386.h" | |
257 | #endif | |
258 | ||
259 | #ifndef COFF_PAGE_SIZE | |
260 | #define COFF_PAGE_SIZE ((bfd_vma) 4096) | |
261 | #endif | |
262 | ||
263 | #ifndef PAGE_MASK | |
264 | #define PAGE_MASK ((bfd_vma) (- COFF_PAGE_SIZE)) | |
265 | #endif | |
252b5132 | 266 | |
e05da72d | 267 | /* Get current BFD error message. */ |
dec87289 NC |
268 | #define bfd_get_errmsg() (bfd_errmsg (bfd_get_error ())) |
269 | ||
49e315b1 | 270 | /* Forward references. */ |
2da42df6 AJ |
271 | static char *look_for_prog (const char *, const char *, int); |
272 | static char *deduce_name (const char *); | |
49e315b1 NC |
273 | |
274 | #ifdef DLLTOOL_MCORE_ELF | |
fd64a958 | 275 | static void mcore_elf_cache_filename (const char *); |
2da42df6 | 276 | static void mcore_elf_gen_out_file (void); |
49e315b1 | 277 | #endif |
26044998 | 278 | |
252b5132 RH |
279 | #ifdef HAVE_SYS_WAIT_H |
280 | #include <sys/wait.h> | |
281 | #else /* ! HAVE_SYS_WAIT_H */ | |
282 | #if ! defined (_WIN32) || defined (__CYGWIN32__) | |
283 | #ifndef WIFEXITED | |
c9e38879 | 284 | #define WIFEXITED(w) (((w) & 0377) == 0) |
252b5132 RH |
285 | #endif |
286 | #ifndef WIFSIGNALED | |
c9e38879 | 287 | #define WIFSIGNALED(w) (((w) & 0377) != 0177 && ((w) & ~0377) == 0) |
252b5132 RH |
288 | #endif |
289 | #ifndef WTERMSIG | |
290 | #define WTERMSIG(w) ((w) & 0177) | |
291 | #endif | |
292 | #ifndef WEXITSTATUS | |
293 | #define WEXITSTATUS(w) (((w) >> 8) & 0377) | |
294 | #endif | |
295 | #else /* defined (_WIN32) && ! defined (__CYGWIN32__) */ | |
296 | #ifndef WIFEXITED | |
297 | #define WIFEXITED(w) (((w) & 0xff) == 0) | |
298 | #endif | |
299 | #ifndef WIFSIGNALED | |
300 | #define WIFSIGNALED(w) (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f) | |
301 | #endif | |
302 | #ifndef WTERMSIG | |
303 | #define WTERMSIG(w) ((w) & 0x7f) | |
304 | #endif | |
305 | #ifndef WEXITSTATUS | |
306 | #define WEXITSTATUS(w) (((w) & 0xff00) >> 8) | |
307 | #endif | |
308 | #endif /* defined (_WIN32) && ! defined (__CYGWIN32__) */ | |
309 | #endif /* ! HAVE_SYS_WAIT_H */ | |
310 | ||
dbb7c441 AM |
311 | #define show_allnames 0 |
312 | ||
252b5132 RH |
313 | /* ifunc and ihead data structures: [email protected] 1997 |
314 | ||
315 | When IMPORT declarations are encountered in a .def file the | |
316 | function import information is stored in a structure referenced by | |
317 | the global variable IMPORT_LIST. The structure is a linked list | |
318 | containing the names of the dll files each function is imported | |
319 | from and a linked list of functions being imported from that dll | |
320 | file. This roughly parallels the structure of the .idata section | |
321 | in the PE object file. | |
322 | ||
323 | The contents of .def file are interpreted from within the | |
324 | process_def_file function. Every time an IMPORT declaration is | |
325 | encountered, it is broken up into its component parts and passed to | |
326 | def_import. IMPORT_LIST is initialized to NULL in function main. */ | |
327 | ||
328 | typedef struct ifunct | |
329 | { | |
c9e38879 | 330 | char * name; /* Name of function being imported. */ |
bf201fdd | 331 | char * its_name; /* Optional import table symbol name. */ |
c9e38879 | 332 | int ord; /* Two-byte ordinal value associated with function. */ |
252b5132 RH |
333 | struct ifunct *next; |
334 | } ifunctype; | |
335 | ||
336 | typedef struct iheadt | |
337 | { | |
dec87289 | 338 | char * dllname; /* Name of dll file imported from. */ |
c9e38879 NC |
339 | long nfuncs; /* Number of functions in list. */ |
340 | struct ifunct *funchead; /* First function in list. */ | |
341 | struct ifunct *functail; /* Last function in list. */ | |
342 | struct iheadt *next; /* Next dll file in list. */ | |
252b5132 RH |
343 | } iheadtype; |
344 | ||
345 | /* Structure containing all import information as defined in .def file | |
346 | (qv "ihead structure"). */ | |
347 | ||
348 | static iheadtype *import_list = NULL; | |
49e315b1 | 349 | static char *as_name = NULL; |
252b5132 | 350 | static char * as_flags = ""; |
c4a8df19 | 351 | static char *tmp_prefix = NULL; |
252b5132 RH |
352 | static int no_idata4; |
353 | static int no_idata5; | |
354 | static char *exp_name; | |
355 | static char *imp_name; | |
10e636d2 | 356 | static char *delayimp_name; |
d4732f7c | 357 | static char *identify_imp_name; |
015dc7e1 | 358 | static bool identify_strict; |
25ee24d9 | 359 | static bool deterministic = DEFAULT_AR_DETERMINISTIC; |
71c57c16 | 360 | |
25893672 NC |
361 | /* Types used to implement a linked list of dllnames associated |
362 | with the specified import lib. Used by the identify_* code. | |
363 | The head entry is acts as a sentinal node and is always empty | |
364 | (head->dllname is NULL). */ | |
365 | typedef struct dll_name_list_node_t | |
366 | { | |
367 | char * dllname; | |
368 | struct dll_name_list_node_t * next; | |
369 | } dll_name_list_node_type; | |
dec87289 | 370 | |
71c57c16 NC |
371 | typedef struct dll_name_list_t |
372 | { | |
25893672 NC |
373 | dll_name_list_node_type * head; |
374 | dll_name_list_node_type * tail; | |
3aade688 | 375 | } dll_name_list_type; |
25893672 NC |
376 | |
377 | /* Types used to pass data to iterator functions. */ | |
378 | typedef struct symname_search_data_t | |
379 | { | |
015dc7e1 AM |
380 | const char *symname; |
381 | bool found; | |
25893672 | 382 | } symname_search_data_type; |
dec87289 | 383 | |
25893672 NC |
384 | typedef struct identify_data_t |
385 | { | |
015dc7e1 AM |
386 | dll_name_list_type *list; |
387 | bool ms_style_implib; | |
3aade688 | 388 | } identify_data_type; |
71c57c16 | 389 | |
71c57c16 | 390 | |
252b5132 RH |
391 | static char *head_label; |
392 | static char *imp_name_lab; | |
393 | static char *dll_name; | |
04276a0c | 394 | static int dll_name_set_by_exp_name; |
252b5132 RH |
395 | static int add_indirect = 0; |
396 | static int add_underscore = 0; | |
14288fdc | 397 | static int add_stdcall_underscore = 0; |
36d21de5 KT |
398 | /* This variable can hold three different values. The value |
399 | -1 (default) means that default underscoring should be used, | |
400 | zero means that no underscoring should be done, and one | |
401 | indicates that underscoring should be done. */ | |
402 | static int leading_underscore = -1; | |
252b5132 RH |
403 | static int dontdeltemps = 0; |
404 | ||
b34976b6 | 405 | /* TRUE if we should export all symbols. Otherwise, we only export |
252b5132 | 406 | symbols listed in .drectve sections or in the def file. */ |
015dc7e1 | 407 | static bool export_all_symbols; |
252b5132 | 408 | |
b34976b6 | 409 | /* TRUE if we should exclude the symbols in DEFAULT_EXCLUDES when |
252b5132 | 410 | exporting all symbols. */ |
015dc7e1 | 411 | static bool do_default_excludes = true; |
252b5132 | 412 | |
015dc7e1 | 413 | static bool use_nul_prefixed_import_tables = false; |
e77b97d4 | 414 | |
252b5132 RH |
415 | /* Default symbols to exclude when exporting all the symbols. */ |
416 | static const char *default_excludes = "DllMain@12,DllEntryPoint@0,impure_ptr"; | |
417 | ||
b34976b6 | 418 | /* TRUE if we should add __imp_<SYMBOL> to import libraries for backward |
5f0f29c3 | 419 | compatibility to old Cygwin releases. */ |
015dc7e1 | 420 | static bool create_compat_implib; |
5f0f29c3 | 421 | |
2ea2f3c6 | 422 | /* TRUE if we have to write PE+ import libraries. */ |
015dc7e1 | 423 | static bool create_for_pep; |
2ea2f3c6 | 424 | |
252b5132 RH |
425 | static char *def_file; |
426 | ||
427 | extern char * program_name; | |
428 | ||
429 | static int machine; | |
430 | static int killat; | |
431 | static int add_stdcall_alias; | |
607dea97 | 432 | static const char *ext_prefix_alias; |
252b5132 RH |
433 | static int verbose; |
434 | static FILE *output_def; | |
435 | static FILE *base_file; | |
436 | ||
7aad4c3d | 437 | #ifdef DLLTOOL_DEFAULT_ARM |
252b5132 RH |
438 | static const char *mname = "arm"; |
439 | #endif | |
7aad4c3d | 440 | |
7aad4c3d L |
441 | #ifdef DLLTOOL_DEFAULT_ARM_WINCE |
442 | static const char *mname = "arm-wince"; | |
a8c548cb | 443 | #endif |
252b5132 | 444 | |
7aad4c3d | 445 | #ifdef DLLTOOL_DEFAULT_I386 |
252b5132 RH |
446 | static const char *mname = "i386"; |
447 | #endif | |
448 | ||
7aad4c3d | 449 | #ifdef DLLTOOL_DEFAULT_MX86_64 |
99ad8390 NC |
450 | static const char *mname = "i386:x86-64"; |
451 | #endif | |
452 | ||
7aad4c3d | 453 | #ifdef DLLTOOL_DEFAULT_SH |
8a0e0f38 NC |
454 | static const char *mname = "sh"; |
455 | #endif | |
456 | ||
7aad4c3d | 457 | #ifdef DLLTOOL_DEFAULT_MIPS |
8a0e0f38 NC |
458 | static const char *mname = "mips"; |
459 | #endif | |
460 | ||
7aad4c3d | 461 | #ifdef DLLTOOL_DEFAULT_MCORE |
7e301c9c | 462 | static const char * mname = "mcore-le"; |
661016bb NC |
463 | #endif |
464 | ||
7aad4c3d | 465 | #ifdef DLLTOOL_DEFAULT_MCORE_ELF |
661016bb | 466 | static const char * mname = "mcore-elf"; |
49e315b1 NC |
467 | static char * mcore_elf_out_file = NULL; |
468 | static char * mcore_elf_linker = NULL; | |
469 | static char * mcore_elf_linker_flags = NULL; | |
470 | ||
661016bb NC |
471 | #define DRECTVE_SECTION_NAME ((machine == MMCORE_ELF || machine == MMCORE_ELF_LE) ? ".exports" : ".drectve") |
472 | #endif | |
473 | ||
474 | #ifndef DRECTVE_SECTION_NAME | |
475 | #define DRECTVE_SECTION_NAME ".drectve" | |
476 | #endif | |
477 | ||
0fd555c4 | 478 | /* What's the right name for this ? */ |
3aade688 | 479 | #define PATHMAX 250 |
0fd555c4 NC |
480 | |
481 | /* External name alias numbering starts here. */ | |
482 | #define PREFIX_ALIAS_BASE 20000 | |
252b5132 | 483 | |
0e11a9e9 CF |
484 | char *tmp_asm_buf; |
485 | char *tmp_head_s_buf; | |
486 | char *tmp_head_o_buf; | |
487 | char *tmp_tail_s_buf; | |
488 | char *tmp_tail_o_buf; | |
489 | char *tmp_stub_buf; | |
490 | ||
bf7a6389 CF |
491 | #define TMP_ASM dlltmp (&tmp_asm_buf, "%sc.s") |
492 | #define TMP_HEAD_S dlltmp (&tmp_head_s_buf, "%sh.s") | |
493 | #define TMP_HEAD_O dlltmp (&tmp_head_o_buf, "%sh.o") | |
494 | #define TMP_TAIL_S dlltmp (&tmp_tail_s_buf, "%st.s") | |
495 | #define TMP_TAIL_O dlltmp (&tmp_tail_o_buf, "%st.o") | |
496 | #define TMP_STUB dlltmp (&tmp_stub_buf, "%ss") | |
252b5132 | 497 | |
50c2245b | 498 | /* This bit of assembly does jmp * .... */ |
252b5132 RH |
499 | static const unsigned char i386_jtab[] = |
500 | { | |
501 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 | |
502 | }; | |
503 | ||
10e636d2 DK |
504 | static const unsigned char i386_dljtab[] = |
505 | { | |
506 | 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, /* jmp __imp__function */ | |
507 | 0xB8, 0x00, 0x00, 0x00, 0x00, /* mov eax, offset __imp__function */ | |
508 | 0xE9, 0x00, 0x00, 0x00, 0x00 /* jmp __tailMerge__dllname */ | |
509 | }; | |
510 | ||
9a30f236 KT |
511 | static const unsigned char i386_x64_dljtab[] = |
512 | { | |
513 | 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, /* jmp __imp__function */ | |
514 | 0x48, 0x8d, 0x05, /* leaq rax, (__imp__function) */ | |
515 | 0x00, 0x00, 0x00, 0x00, | |
516 | 0xE9, 0x00, 0x00, 0x00, 0x00 /* jmp __tailMerge__dllname */ | |
517 | }; | |
518 | ||
252b5132 RH |
519 | static const unsigned char arm_jtab[] = |
520 | { | |
b890a735 CM |
521 | 0x00, 0xc0, 0x9f, 0xe5, /* ldr ip, [pc] */ |
522 | 0x00, 0xf0, 0x9c, 0xe5, /* ldr pc, [ip] */ | |
523 | 0, 0, 0, 0 | |
524 | }; | |
525 | ||
526 | static const unsigned char arm_interwork_jtab[] = | |
527 | { | |
528 | 0x04, 0xc0, 0x9f, 0xe5, /* ldr ip, [pc] */ | |
529 | 0x00, 0xc0, 0x9c, 0xe5, /* ldr ip, [ip] */ | |
530 | 0x1c, 0xff, 0x2f, 0xe1, /* bx ip */ | |
252b5132 RH |
531 | 0, 0, 0, 0 |
532 | }; | |
533 | ||
534 | static const unsigned char thumb_jtab[] = | |
535 | { | |
b890a735 CM |
536 | 0x40, 0xb4, /* push {r6} */ |
537 | 0x02, 0x4e, /* ldr r6, [pc, #8] */ | |
538 | 0x36, 0x68, /* ldr r6, [r6] */ | |
539 | 0xb4, 0x46, /* mov ip, r6 */ | |
540 | 0x40, 0xbc, /* pop {r6} */ | |
541 | 0x60, 0x47, /* bx ip */ | |
252b5132 RH |
542 | 0, 0, 0, 0 |
543 | }; | |
544 | ||
661016bb NC |
545 | static const unsigned char mcore_be_jtab[] = |
546 | { | |
ba8c44fc | 547 | 0x71, 0x02, /* lrw r1,2 */ |
26044998 | 548 | 0x81, 0x01, /* ld.w r1,(r1,0) */ |
ba8c44fc NC |
549 | 0x00, 0xC1, /* jmp r1 */ |
550 | 0x12, 0x00, /* nop */ | |
26044998 | 551 | 0x00, 0x00, 0x00, 0x00 /* <address> */ |
661016bb NC |
552 | }; |
553 | ||
554 | static const unsigned char mcore_le_jtab[] = | |
555 | { | |
ba8c44fc | 556 | 0x02, 0x71, /* lrw r1,2 */ |
26044998 | 557 | 0x01, 0x81, /* ld.w r1,(r1,0) */ |
ba8c44fc NC |
558 | 0xC1, 0x00, /* jmp r1 */ |
559 | 0x00, 0x12, /* nop */ | |
26044998 | 560 | 0x00, 0x00, 0x00, 0x00 /* <address> */ |
661016bb NC |
561 | }; |
562 | ||
3aade688 | 563 | static const char i386_trampoline[] = |
10e636d2 DK |
564 | "\tpushl %%ecx\n" |
565 | "\tpushl %%edx\n" | |
566 | "\tpushl %%eax\n" | |
567 | "\tpushl $__DELAY_IMPORT_DESCRIPTOR_%s\n" | |
568 | "\tcall ___delayLoadHelper2@8\n" | |
569 | "\tpopl %%edx\n" | |
570 | "\tpopl %%ecx\n" | |
571 | "\tjmp *%%eax\n"; | |
572 | ||
3aade688 | 573 | static const char i386_x64_trampoline[] = |
2ce40d1a ZF |
574 | "\tsubq $72, %%rsp\n" |
575 | "\t.seh_stackalloc 72\n" | |
576 | "\t.seh_endprologue\n" | |
577 | "\tmovq %%rcx, 64(%%rsp)\n" | |
578 | "\tmovq %%rdx, 56(%%rsp)\n" | |
579 | "\tmovq %%r8, 48(%%rsp)\n" | |
580 | "\tmovq %%r9, 40(%%rsp)\n" | |
9a30f236 KT |
581 | "\tmovq %%rax, %%rdx\n" |
582 | "\tleaq __DELAY_IMPORT_DESCRIPTOR_%s(%%rip), %%rcx\n" | |
583 | "\tcall __delayLoadHelper2\n" | |
2ce40d1a ZF |
584 | "\tmovq 40(%%rsp), %%r9\n" |
585 | "\tmovq 48(%%rsp), %%r8\n" | |
586 | "\tmovq 56(%%rsp), %%rdx\n" | |
587 | "\tmovq 64(%%rsp), %%rcx\n" | |
588 | "\taddq $72, %%rsp\n" | |
9a30f236 KT |
589 | "\tjmp *%%rax\n"; |
590 | ||
252b5132 | 591 | struct mac |
dec87289 NC |
592 | { |
593 | const char *type; | |
594 | const char *how_byte; | |
595 | const char *how_short; | |
596 | const char *how_long; | |
597 | const char *how_asciz; | |
598 | const char *how_comment; | |
599 | const char *how_jump; | |
600 | const char *how_global; | |
601 | const char *how_space; | |
602 | const char *how_align_short; | |
603 | const char *how_align_long; | |
604 | const char *how_default_as_switches; | |
605 | const char *how_bfd_target; | |
606 | enum bfd_architecture how_bfd_arch; | |
607 | const unsigned char *how_jtab; | |
608 | int how_jtab_size; /* Size of the jtab entry. */ | |
609 | int how_jtab_roff; /* Offset into it for the ind 32 reloc into idata 5. */ | |
610 | const unsigned char *how_dljtab; | |
611 | int how_dljtab_size; /* Size of the dljtab entry. */ | |
612 | int how_dljtab_roff1; /* Offset for the ind 32 reloc into idata 5. */ | |
613 | int how_dljtab_roff2; /* Offset for the ind 32 reloc into idata 5. */ | |
614 | int how_dljtab_roff3; /* Offset for the ind 32 reloc into idata 5. */ | |
015dc7e1 | 615 | bool how_seh; |
dec87289 NC |
616 | const char *trampoline; |
617 | }; | |
252b5132 RH |
618 | |
619 | static const struct mac | |
620 | mtable[] = | |
621 | { | |
622 | { | |
623 | #define MARM 0 | |
624 | "arm", ".byte", ".short", ".long", ".asciz", "@", | |
625 | "ldr\tip,[pc]\n\tldr\tpc,[ip]\n\t.long", | |
eaeaa15c | 626 | ".global", ".space", ".align\t2",".align\t4", "-mapcs-32", |
49c24507 | 627 | "pe-arm-little", bfd_arch_arm, |
10e636d2 | 628 | arm_jtab, sizeof (arm_jtab), 8, |
015dc7e1 | 629 | 0, 0, 0, 0, 0, false, 0 |
252b5132 RH |
630 | } |
631 | , | |
632 | { | |
633 | #define M386 1 | |
49c24507 NC |
634 | "i386", ".byte", ".short", ".long", ".asciz", "#", |
635 | "jmp *", ".global", ".space", ".align\t2",".align\t4", "", | |
636 | "pe-i386",bfd_arch_i386, | |
10e636d2 | 637 | i386_jtab, sizeof (i386_jtab), 2, |
015dc7e1 | 638 | i386_dljtab, sizeof (i386_dljtab), 2, 7, 12, false, i386_trampoline |
252b5132 RH |
639 | } |
640 | , | |
641 | { | |
fe49679d | 642 | #define MTHUMB 2 |
252b5132 | 643 | "thumb", ".byte", ".short", ".long", ".asciz", "@", |
b890a735 | 644 | "push\t{r6}\n\tldr\tr6, [pc, #8]\n\tldr\tr6, [r6]\n\tmov\tip, r6\n\tpop\t{r6}\n\tbx\tip", |
a2186dfe | 645 | ".global", ".space", ".align\t2",".align\t4", "-mthumb-interwork", |
49c24507 | 646 | "pe-arm-little", bfd_arch_arm, |
10e636d2 | 647 | thumb_jtab, sizeof (thumb_jtab), 12, |
015dc7e1 | 648 | 0, 0, 0, 0, 0, false, 0 |
252b5132 RH |
649 | } |
650 | , | |
fe49679d | 651 | #define MARM_INTERWORK 3 |
b890a735 CM |
652 | { |
653 | "arm_interwork", ".byte", ".short", ".long", ".asciz", "@", | |
654 | "ldr\tip,[pc]\n\tldr\tip,[ip]\n\tbx\tip\n\t.long", | |
49c24507 NC |
655 | ".global", ".space", ".align\t2",".align\t4", "-mthumb-interwork", |
656 | "pe-arm-little", bfd_arch_arm, | |
10e636d2 | 657 | arm_interwork_jtab, sizeof (arm_interwork_jtab), 12, |
015dc7e1 | 658 | 0, 0, 0, 0, 0, false, 0 |
b890a735 CM |
659 | } |
660 | , | |
661016bb | 661 | { |
fe49679d | 662 | #define MMCORE_BE 4 |
7e301c9c | 663 | "mcore-be", ".byte", ".short", ".long", ".asciz", "//", |
ba8c44fc | 664 | "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long", |
49c24507 NC |
665 | ".global", ".space", ".align\t2",".align\t4", "", |
666 | "pe-mcore-big", bfd_arch_mcore, | |
10e636d2 | 667 | mcore_be_jtab, sizeof (mcore_be_jtab), 8, |
015dc7e1 | 668 | 0, 0, 0, 0, 0, false, 0 |
661016bb NC |
669 | } |
670 | , | |
671 | { | |
fe49679d | 672 | #define MMCORE_LE 5 |
661016bb | 673 | "mcore-le", ".byte", ".short", ".long", ".asciz", "//", |
ba8c44fc | 674 | "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long", |
49c24507 NC |
675 | ".global", ".space", ".align\t2",".align\t4", "-EL", |
676 | "pe-mcore-little", bfd_arch_mcore, | |
10e636d2 | 677 | mcore_le_jtab, sizeof (mcore_le_jtab), 8, |
015dc7e1 | 678 | 0, 0, 0, 0, 0, false, 0 |
661016bb NC |
679 | } |
680 | , | |
681 | { | |
fe49679d | 682 | #define MMCORE_ELF 6 |
7e301c9c | 683 | "mcore-elf-be", ".byte", ".short", ".long", ".asciz", "//", |
ba8c44fc | 684 | "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long", |
49c24507 NC |
685 | ".global", ".space", ".align\t2",".align\t4", "", |
686 | "elf32-mcore-big", bfd_arch_mcore, | |
10e636d2 | 687 | mcore_be_jtab, sizeof (mcore_be_jtab), 8, |
015dc7e1 | 688 | 0, 0, 0, 0, 0, false, 0 |
661016bb NC |
689 | } |
690 | , | |
691 | { | |
fe49679d | 692 | #define MMCORE_ELF_LE 7 |
661016bb | 693 | "mcore-elf-le", ".byte", ".short", ".long", ".asciz", "//", |
ba8c44fc | 694 | "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long", |
49c24507 NC |
695 | ".global", ".space", ".align\t2",".align\t4", "-EL", |
696 | "elf32-mcore-little", bfd_arch_mcore, | |
10e636d2 | 697 | mcore_le_jtab, sizeof (mcore_le_jtab), 8, |
015dc7e1 | 698 | 0, 0, 0, 0, 0, false, 0 |
661016bb NC |
699 | } |
700 | , | |
a2186dfe | 701 | { |
fe49679d | 702 | #define MARM_WINCE 8 |
7148cc28 NC |
703 | "arm-wince", ".byte", ".short", ".long", ".asciz", "@", |
704 | "ldr\tip,[pc]\n\tldr\tpc,[ip]\n\t.long", | |
705 | ".global", ".space", ".align\t2",".align\t4", "-mapcs-32", | |
706 | "pe-arm-wince-little", bfd_arch_arm, | |
10e636d2 | 707 | arm_jtab, sizeof (arm_jtab), 8, |
015dc7e1 | 708 | 0, 0, 0, 0, 0, false, 0 |
7148cc28 NC |
709 | } |
710 | , | |
99ad8390 | 711 | { |
fe49679d | 712 | #define MX86 9 |
99ad8390 NC |
713 | "i386:x86-64", ".byte", ".short", ".long", ".asciz", "#", |
714 | "jmp *", ".global", ".space", ".align\t2",".align\t4", "", | |
715 | "pe-x86-64",bfd_arch_i386, | |
10e636d2 | 716 | i386_jtab, sizeof (i386_jtab), 2, |
015dc7e1 | 717 | i386_x64_dljtab, sizeof (i386_x64_dljtab), 2, 9, 14, true, i386_x64_trampoline |
99ad8390 NC |
718 | } |
719 | , | |
2ce40d1a | 720 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } |
252b5132 RH |
721 | }; |
722 | ||
723 | typedef struct dlist | |
724 | { | |
725 | char *text; | |
726 | struct dlist *next; | |
727 | } | |
728 | dlist_type; | |
729 | ||
730 | typedef struct export | |
dec87289 NC |
731 | { |
732 | const char *name; | |
733 | const char *internal_name; | |
734 | const char *import_name; | |
bf201fdd | 735 | const char *its_name; |
dec87289 NC |
736 | int ordinal; |
737 | int constant; | |
738 | int noname; /* Don't put name in image file. */ | |
35fd2dde | 739 | int private; /* Don't put reference in import lib. */ |
dec87289 | 740 | int data; |
35fd2dde | 741 | int forward; /* Number of forward label, 0 means no forward. */ |
dec87289 NC |
742 | struct export *next; |
743 | } | |
252b5132 RH |
744 | export_type; |
745 | ||
746 | /* A list of symbols which we should not export. */ | |
26044998 | 747 | |
252b5132 RH |
748 | struct string_list |
749 | { | |
750 | struct string_list *next; | |
751 | char *string; | |
752 | }; | |
753 | ||
754 | static struct string_list *excludes; | |
755 | ||
2da42df6 AJ |
756 | static const char *rvaafter (int); |
757 | static const char *rvabefore (int); | |
2758961a | 758 | static const char *asm_prefix (int, const char *); |
2da42df6 AJ |
759 | static void process_def_file (const char *); |
760 | static void new_directive (char *); | |
bf201fdd | 761 | static void append_import (const char *, const char *, int, const char *); |
2da42df6 AJ |
762 | static void run (const char *, char *); |
763 | static void scan_drectve_symbols (bfd *); | |
764 | static void scan_filtered_symbols (bfd *, void *, long, unsigned int); | |
765 | static void add_excludes (const char *); | |
015dc7e1 | 766 | static bool match_exclude (const char *); |
2da42df6 AJ |
767 | static void set_default_excludes (void); |
768 | static long filter_symbols (bfd *, void *, long, unsigned int); | |
769 | static void scan_all_symbols (bfd *); | |
770 | static void scan_open_obj_file (bfd *); | |
771 | static void scan_obj_file (const char *); | |
772 | static void dump_def_info (FILE *); | |
773 | static int sfunc (const void *, const void *); | |
d078078d | 774 | static void flush_page (FILE *, bfd_vma *, bfd_vma, int); |
2da42df6 AJ |
775 | static void gen_def_file (void); |
776 | static void generate_idata_ofile (FILE *); | |
777 | static void assemble_file (const char *, const char *); | |
778 | static void gen_exp_file (void); | |
779 | static const char *xlate (const char *); | |
2da42df6 AJ |
780 | static char *make_label (const char *, const char *); |
781 | static char *make_imp_label (const char *, const char *); | |
10e636d2 | 782 | static bfd *make_one_lib_file (export_type *, int, int); |
2da42df6 AJ |
783 | static bfd *make_head (void); |
784 | static bfd *make_tail (void); | |
10e636d2 DK |
785 | static bfd *make_delay_head (void); |
786 | static void gen_lib_file (int); | |
25893672 NC |
787 | static void dll_name_list_append (dll_name_list_type *, bfd_byte *); |
788 | static int dll_name_list_count (dll_name_list_type *); | |
789 | static void dll_name_list_print (dll_name_list_type *); | |
790 | static void dll_name_list_free_contents (dll_name_list_node_type *); | |
791 | static void dll_name_list_free (dll_name_list_type *); | |
792 | static dll_name_list_type * dll_name_list_create (void); | |
d4732f7c | 793 | static void identify_dll_for_implib (void); |
71c57c16 NC |
794 | static void identify_search_archive |
795 | (bfd *, void (*) (bfd *, bfd *, void *), void *); | |
796 | static void identify_search_member (bfd *, bfd *, void *); | |
015dc7e1 | 797 | static bool identify_process_section_p (asection *, bool); |
d4732f7c | 798 | static void identify_search_section (bfd *, asection *, void *); |
71c57c16 NC |
799 | static void identify_member_contains_symname (bfd *, bfd *, void *); |
800 | ||
2da42df6 AJ |
801 | static int pfunc (const void *, const void *); |
802 | static int nfunc (const void *, const void *); | |
803 | static void remove_null_names (export_type **); | |
2da42df6 AJ |
804 | static void process_duplicates (export_type **); |
805 | static void fill_ordinals (export_type **); | |
2da42df6 AJ |
806 | static void mangle_defs (void); |
807 | static void usage (FILE *, int); | |
0fd3a477 | 808 | static void inform (const char *, ...) ATTRIBUTE_PRINTF_1; |
2fe50fe3 | 809 | static void set_dll_name_from_def (const char *name, char is_dll); |
252b5132 | 810 | |
0e11a9e9 | 811 | static char * |
739fea7b | 812 | prefix_encode (char *start, unsigned code) |
bf7a6389 | 813 | { |
ff6b6222 | 814 | static char alpha[26] = "abcdefghijklmnopqrstuvwxyz"; |
bf7a6389 CF |
815 | static char buf[32]; |
816 | char *p; | |
739fea7b | 817 | strcpy (buf, start); |
bf7a6389 CF |
818 | p = strchr (buf, '\0'); |
819 | do | |
820 | *p++ = alpha[code % sizeof (alpha)]; | |
821 | while ((code /= sizeof (alpha)) != 0); | |
822 | *p = '\0'; | |
0e11a9e9 CF |
823 | return buf; |
824 | } | |
252b5132 | 825 | |
bf7a6389 | 826 | static char * |
739fea7b | 827 | dlltmp (char **buf, const char *fmt) |
bf7a6389 CF |
828 | { |
829 | if (!*buf) | |
830 | { | |
739fea7b | 831 | *buf = malloc (strlen (tmp_prefix) + 64); |
bf7a6389 CF |
832 | sprintf (*buf, fmt, tmp_prefix); |
833 | } | |
834 | return *buf; | |
835 | } | |
836 | ||
252b5132 | 837 | static void |
1651e569 | 838 | inform (const char * message, ...) |
252b5132 | 839 | { |
1651e569 TT |
840 | va_list args; |
841 | ||
842 | va_start (args, message); | |
e80ff7de | 843 | |
252b5132 RH |
844 | if (!verbose) |
845 | return; | |
846 | ||
37cc8ec1 | 847 | report (message, args); |
e80ff7de | 848 | |
1651e569 | 849 | va_end (args); |
252b5132 RH |
850 | } |
851 | ||
252b5132 | 852 | static const char * |
91d6fa6a | 853 | rvaafter (int mach) |
252b5132 | 854 | { |
91d6fa6a | 855 | switch (mach) |
252b5132 RH |
856 | { |
857 | case MARM: | |
858 | case M386: | |
99ad8390 | 859 | case MX86: |
252b5132 | 860 | case MTHUMB: |
b890a735 | 861 | case MARM_INTERWORK: |
661016bb NC |
862 | case MMCORE_BE: |
863 | case MMCORE_LE: | |
864 | case MMCORE_ELF: | |
865 | case MMCORE_ELF_LE: | |
7148cc28 | 866 | case MARM_WINCE: |
252b5132 RH |
867 | break; |
868 | default: | |
869 | /* xgettext:c-format */ | |
91d6fa6a | 870 | fatal (_("Internal error: Unknown machine type: %d"), mach); |
252b5132 RH |
871 | break; |
872 | } | |
873 | return ""; | |
874 | } | |
875 | ||
876 | static const char * | |
91d6fa6a | 877 | rvabefore (int mach) |
252b5132 | 878 | { |
91d6fa6a | 879 | switch (mach) |
252b5132 RH |
880 | { |
881 | case MARM: | |
882 | case M386: | |
99ad8390 | 883 | case MX86: |
252b5132 | 884 | case MTHUMB: |
b890a735 | 885 | case MARM_INTERWORK: |
661016bb NC |
886 | case MMCORE_BE: |
887 | case MMCORE_LE: | |
888 | case MMCORE_ELF: | |
889 | case MMCORE_ELF_LE: | |
7148cc28 | 890 | case MARM_WINCE: |
252b5132 RH |
891 | return ".rva\t"; |
892 | default: | |
893 | /* xgettext:c-format */ | |
91d6fa6a | 894 | fatal (_("Internal error: Unknown machine type: %d"), mach); |
252b5132 RH |
895 | break; |
896 | } | |
897 | return ""; | |
898 | } | |
899 | ||
900 | static const char * | |
91d6fa6a | 901 | asm_prefix (int mach, const char *name) |
252b5132 | 902 | { |
91d6fa6a | 903 | switch (mach) |
252b5132 RH |
904 | { |
905 | case MARM: | |
252b5132 | 906 | case MTHUMB: |
b890a735 | 907 | case MARM_INTERWORK: |
661016bb NC |
908 | case MMCORE_BE: |
909 | case MMCORE_LE: | |
910 | case MMCORE_ELF: | |
911 | case MMCORE_ELF_LE: | |
7148cc28 | 912 | case MARM_WINCE: |
252b5132 RH |
913 | break; |
914 | case M386: | |
99ad8390 | 915 | case MX86: |
2758961a | 916 | /* Symbol names starting with ? do not have a leading underscore. */ |
36d21de5 | 917 | if ((name && *name == '?') || leading_underscore == 0) |
2758961a NC |
918 | break; |
919 | else | |
920 | return "_"; | |
252b5132 RH |
921 | default: |
922 | /* xgettext:c-format */ | |
91d6fa6a | 923 | fatal (_("Internal error: Unknown machine type: %d"), mach); |
252b5132 RH |
924 | break; |
925 | } | |
926 | return ""; | |
927 | } | |
928 | ||
2758961a NC |
929 | #define ASM_BYTE mtable[machine].how_byte |
930 | #define ASM_SHORT mtable[machine].how_short | |
931 | #define ASM_LONG mtable[machine].how_long | |
932 | #define ASM_TEXT mtable[machine].how_asciz | |
933 | #define ASM_C mtable[machine].how_comment | |
934 | #define ASM_JUMP mtable[machine].how_jump | |
935 | #define ASM_GLOBAL mtable[machine].how_global | |
936 | #define ASM_SPACE mtable[machine].how_space | |
937 | #define ASM_ALIGN_SHORT mtable[machine].how_align_short | |
938 | #define ASM_RVA_BEFORE rvabefore (machine) | |
939 | #define ASM_RVA_AFTER rvaafter (machine) | |
940 | #define ASM_PREFIX(NAME) asm_prefix (machine, (NAME)) | |
941 | #define ASM_ALIGN_LONG mtable[machine].how_align_long | |
942 | #define HOW_BFD_READ_TARGET 0 /* Always default. */ | |
943 | #define HOW_BFD_WRITE_TARGET mtable[machine].how_bfd_target | |
944 | #define HOW_BFD_ARCH mtable[machine].how_bfd_arch | |
10e636d2 DK |
945 | #define HOW_JTAB (delay ? mtable[machine].how_dljtab \ |
946 | : mtable[machine].how_jtab) | |
947 | #define HOW_JTAB_SIZE (delay ? mtable[machine].how_dljtab_size \ | |
948 | : mtable[machine].how_jtab_size) | |
949 | #define HOW_JTAB_ROFF (delay ? mtable[machine].how_dljtab_roff1 \ | |
950 | : mtable[machine].how_jtab_roff) | |
951 | #define HOW_JTAB_ROFF2 (delay ? mtable[machine].how_dljtab_roff2 : 0) | |
952 | #define HOW_JTAB_ROFF3 (delay ? mtable[machine].how_dljtab_roff3 : 0) | |
2758961a | 953 | #define ASM_SWITCHES mtable[machine].how_default_as_switches |
2ce40d1a | 954 | #define HOW_SEH mtable[machine].how_seh |
49c24507 | 955 | |
252b5132 RH |
956 | static char **oav; |
957 | ||
e80ff7de | 958 | static void |
2da42df6 | 959 | process_def_file (const char *name) |
252b5132 RH |
960 | { |
961 | FILE *f = fopen (name, FOPEN_RT); | |
26044998 | 962 | |
252b5132 RH |
963 | if (!f) |
964 | /* xgettext:c-format */ | |
965 | fatal (_("Can't open def file: %s"), name); | |
966 | ||
967 | yyin = f; | |
968 | ||
969 | /* xgettext:c-format */ | |
970 | inform (_("Processing def file: %s"), name); | |
26044998 | 971 | |
252b5132 RH |
972 | yyparse (); |
973 | ||
974 | inform (_("Processed def file")); | |
975 | } | |
976 | ||
977 | /**********************************************************************/ | |
978 | ||
c9e38879 | 979 | /* Communications with the parser. */ |
252b5132 | 980 | |
c9e38879 NC |
981 | static int d_nfuncs; /* Number of functions exported. */ |
982 | static int d_named_nfuncs; /* Number of named functions exported. */ | |
983 | static int d_low_ord; /* Lowest ordinal index. */ | |
984 | static int d_high_ord; /* Highest ordinal index. */ | |
985 | static export_type *d_exports; /* List of exported functions. */ | |
986 | static export_type **d_exports_lexically; /* Vector of exported functions in alpha order. */ | |
987 | static dlist_type *d_list; /* Descriptions. */ | |
988 | static dlist_type *a_list; /* Stuff to go in directives. */ | |
989 | static int d_nforwards = 0; /* Number of forwarded exports. */ | |
252b5132 RH |
990 | |
991 | static int d_is_dll; | |
992 | static int d_is_exe; | |
993 | ||
314ec7ae | 994 | void |
2da42df6 | 995 | yyerror (const char * err ATTRIBUTE_UNUSED) |
252b5132 RH |
996 | { |
997 | /* xgettext:c-format */ | |
37cc8ec1 | 998 | non_fatal (_("Syntax error in def file %s:%d"), def_file, linenumber); |
252b5132 RH |
999 | } |
1000 | ||
1001 | void | |
2da42df6 | 1002 | def_exports (const char *name, const char *internal_name, int ordinal, |
bf201fdd KT |
1003 | int noname, int constant, int data, int private, |
1004 | const char *its_name) | |
252b5132 RH |
1005 | { |
1006 | struct export *p = (struct export *) xmalloc (sizeof (*p)); | |
1007 | ||
1008 | p->name = name; | |
1009 | p->internal_name = internal_name ? internal_name : name; | |
bf201fdd | 1010 | p->its_name = its_name; |
0fd555c4 | 1011 | p->import_name = name; |
252b5132 RH |
1012 | p->ordinal = ordinal; |
1013 | p->constant = constant; | |
1014 | p->noname = noname; | |
7aa52b1f | 1015 | p->private = private; |
252b5132 RH |
1016 | p->data = data; |
1017 | p->next = d_exports; | |
1018 | d_exports = p; | |
1019 | d_nfuncs++; | |
26044998 KH |
1020 | |
1021 | if ((internal_name != NULL) | |
04847a4d CF |
1022 | && (strchr (internal_name, '.') != NULL)) |
1023 | p->forward = ++d_nforwards; | |
1024 | else | |
1025 | p->forward = 0; /* no forward */ | |
252b5132 RH |
1026 | } |
1027 | ||
a0ce7f12 | 1028 | static void |
2fe50fe3 | 1029 | set_dll_name_from_def (const char *name, char is_dll) |
a0ce7f12 | 1030 | { |
2fe50fe3 | 1031 | const char *image_basename = lbasename (name); |
a0ce7f12 DS |
1032 | if (image_basename != name) |
1033 | non_fatal (_("%s: Path components stripped from image name, '%s'."), | |
1034 | def_file, name); | |
3aade688 | 1035 | /* Append the default suffix, if none specified. */ |
2fe50fe3 DK |
1036 | if (strchr (image_basename, '.') == 0) |
1037 | { | |
1038 | const char * suffix = is_dll ? ".dll" : ".exe"; | |
1039 | ||
1040 | dll_name = xmalloc (strlen (image_basename) + strlen (suffix) + 1); | |
1041 | sprintf (dll_name, "%s%s", image_basename, suffix); | |
1042 | } | |
1043 | else | |
1044 | dll_name = xstrdup (image_basename); | |
a0ce7f12 DS |
1045 | } |
1046 | ||
252b5132 | 1047 | void |
2da42df6 | 1048 | def_name (const char *name, int base) |
252b5132 RH |
1049 | { |
1050 | /* xgettext:c-format */ | |
1051 | inform (_("NAME: %s base: %x"), name, base); | |
26044998 | 1052 | |
252b5132 | 1053 | if (d_is_dll) |
37cc8ec1 | 1054 | non_fatal (_("Can't have LIBRARY and NAME")); |
26044998 | 1055 | |
04276a0c KT |
1056 | if (dll_name_set_by_exp_name && name && *name != 0) |
1057 | { | |
1058 | dll_name = NULL; | |
1059 | dll_name_set_by_exp_name = 0; | |
1060 | } | |
c9e38879 NC |
1061 | /* If --dllname not provided, use the one in the DEF file. |
1062 | FIXME: Is this appropriate for executables? */ | |
2fe50fe3 DK |
1063 | if (!dll_name) |
1064 | set_dll_name_from_def (name, 0); | |
252b5132 RH |
1065 | d_is_exe = 1; |
1066 | } | |
1067 | ||
1068 | void | |
2da42df6 | 1069 | def_library (const char *name, int base) |
252b5132 RH |
1070 | { |
1071 | /* xgettext:c-format */ | |
1072 | inform (_("LIBRARY: %s base: %x"), name, base); | |
26044998 | 1073 | |
252b5132 | 1074 | if (d_is_exe) |
37cc8ec1 | 1075 | non_fatal (_("Can't have LIBRARY and NAME")); |
26044998 | 1076 | |
04276a0c KT |
1077 | if (dll_name_set_by_exp_name && name && *name != 0) |
1078 | { | |
1079 | dll_name = NULL; | |
1080 | dll_name_set_by_exp_name = 0; | |
1081 | } | |
1082 | ||
c9e38879 | 1083 | /* If --dllname not provided, use the one in the DEF file. */ |
2fe50fe3 DK |
1084 | if (!dll_name) |
1085 | set_dll_name_from_def (name, 1); | |
252b5132 RH |
1086 | d_is_dll = 1; |
1087 | } | |
1088 | ||
1089 | void | |
2da42df6 | 1090 | def_description (const char *desc) |
252b5132 RH |
1091 | { |
1092 | dlist_type *d = (dlist_type *) xmalloc (sizeof (dlist_type)); | |
1093 | d->text = xstrdup (desc); | |
1094 | d->next = d_list; | |
1095 | d_list = d; | |
1096 | } | |
1097 | ||
e80ff7de | 1098 | static void |
2da42df6 | 1099 | new_directive (char *dir) |
252b5132 RH |
1100 | { |
1101 | dlist_type *d = (dlist_type *) xmalloc (sizeof (dlist_type)); | |
1102 | d->text = xstrdup (dir); | |
1103 | d->next = a_list; | |
1104 | a_list = d; | |
1105 | } | |
1106 | ||
1107 | void | |
2da42df6 | 1108 | def_heapsize (int reserve, int commit) |
252b5132 RH |
1109 | { |
1110 | char b[200]; | |
1111 | if (commit > 0) | |
1112 | sprintf (b, "-heap 0x%x,0x%x ", reserve, commit); | |
1113 | else | |
1114 | sprintf (b, "-heap 0x%x ", reserve); | |
1115 | new_directive (xstrdup (b)); | |
1116 | } | |
1117 | ||
1118 | void | |
2da42df6 | 1119 | def_stacksize (int reserve, int commit) |
252b5132 RH |
1120 | { |
1121 | char b[200]; | |
1122 | if (commit > 0) | |
1123 | sprintf (b, "-stack 0x%x,0x%x ", reserve, commit); | |
1124 | else | |
1125 | sprintf (b, "-stack 0x%x ", reserve); | |
1126 | new_directive (xstrdup (b)); | |
1127 | } | |
1128 | ||
1129 | /* append_import simply adds the given import definition to the global | |
1130 | import_list. It is used by def_import. */ | |
1131 | ||
1132 | static void | |
91d6fa6a | 1133 | append_import (const char *symbol_name, const char *dllname, int func_ordinal, |
bf201fdd | 1134 | const char *its_name) |
252b5132 RH |
1135 | { |
1136 | iheadtype **pq; | |
1137 | iheadtype *q; | |
1138 | ||
1139 | for (pq = &import_list; *pq != NULL; pq = &(*pq)->next) | |
1140 | { | |
91d6fa6a | 1141 | if (strcmp ((*pq)->dllname, dllname) == 0) |
252b5132 RH |
1142 | { |
1143 | q = *pq; | |
1144 | q->functail->next = xmalloc (sizeof (ifunctype)); | |
1145 | q->functail = q->functail->next; | |
1146 | q->functail->ord = func_ordinal; | |
1147 | q->functail->name = xstrdup (symbol_name); | |
bf201fdd | 1148 | q->functail->its_name = (its_name ? xstrdup (its_name) : NULL); |
252b5132 RH |
1149 | q->functail->next = NULL; |
1150 | q->nfuncs++; | |
1151 | return; | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | q = xmalloc (sizeof (iheadtype)); | |
91d6fa6a | 1156 | q->dllname = xstrdup (dllname); |
252b5132 RH |
1157 | q->nfuncs = 1; |
1158 | q->funchead = xmalloc (sizeof (ifunctype)); | |
1159 | q->functail = q->funchead; | |
1160 | q->next = NULL; | |
1161 | q->functail->name = xstrdup (symbol_name); | |
bf201fdd | 1162 | q->functail->its_name = (its_name ? xstrdup (its_name) : NULL); |
252b5132 RH |
1163 | q->functail->ord = func_ordinal; |
1164 | q->functail->next = NULL; | |
1165 | ||
1166 | *pq = q; | |
1167 | } | |
1168 | ||
1169 | /* def_import is called from within defparse.y when an IMPORT | |
1170 | declaration is encountered. Depending on the form of the | |
1171 | declaration, the module name may or may not need ".dll" to be | |
1172 | appended to it, the name of the function may be stored in internal | |
1173 | or entry, and there may or may not be an ordinal value associated | |
1174 | with it. */ | |
1175 | ||
1176 | /* A note regarding the parse modes: | |
1177 | In defparse.y we have to accept import declarations which follow | |
1178 | any one of the following forms: | |
1179 | <func_name_in_app> = <dll_name>.<func_name_in_dll> | |
1180 | <func_name_in_app> = <dll_name>.<number> | |
1181 | <dll_name>.<func_name_in_dll> | |
1182 | <dll_name>.<number> | |
1183 | Furthermore, the dll's name may or may not end with ".dll", which | |
1184 | complicates the parsing a little. Normally the dll's name is | |
1185 | passed to def_import() in the "module" parameter, but when it ends | |
1186 | with ".dll" it gets passed in "module" sans ".dll" and that needs | |
1187 | to be reappended. | |
1188 | ||
1189 | def_import gets five parameters: | |
1190 | APP_NAME - the name of the function in the application, if | |
1191 | present, or NULL if not present. | |
1192 | MODULE - the name of the dll, possibly sans extension (ie, '.dll'). | |
1193 | DLLEXT - the extension of the dll, if present, NULL if not present. | |
1194 | ENTRY - the name of the function in the dll, if present, or NULL. | |
1195 | ORD_VAL - the numerical tag of the function in the dll, if present, | |
1196 | or NULL. Exactly one of <entry> or <ord_val> must be | |
1197 | present (i.e., not NULL). */ | |
1198 | ||
1199 | void | |
2da42df6 | 1200 | def_import (const char *app_name, const char *module, const char *dllext, |
bf201fdd | 1201 | const char *entry, int ord_val, const char *its_name) |
252b5132 RH |
1202 | { |
1203 | const char *application_name; | |
e1fa0163 | 1204 | char *buf = NULL; |
252b5132 RH |
1205 | |
1206 | if (entry != NULL) | |
1207 | application_name = entry; | |
1208 | else | |
1209 | { | |
1210 | if (app_name != NULL) | |
1211 | application_name = app_name; | |
1212 | else | |
1213 | application_name = ""; | |
1214 | } | |
26044998 | 1215 | |
252b5132 | 1216 | if (dllext != NULL) |
e1fa0163 | 1217 | module = buf = concat (module, ".", dllext, NULL); |
252b5132 | 1218 | |
bf201fdd | 1219 | append_import (application_name, module, ord_val, its_name); |
e1fa0163 | 1220 | |
9db70fc3 | 1221 | free (buf); |
252b5132 RH |
1222 | } |
1223 | ||
1224 | void | |
2da42df6 | 1225 | def_version (int major, int minor) |
252b5132 | 1226 | { |
9cf03b7e | 1227 | printf (_("VERSION %d.%d\n"), major, minor); |
252b5132 RH |
1228 | } |
1229 | ||
1230 | void | |
2da42df6 | 1231 | def_section (const char *name, int attr) |
252b5132 RH |
1232 | { |
1233 | char buf[200]; | |
1234 | char atts[5]; | |
1235 | char *d = atts; | |
1236 | if (attr & 1) | |
1237 | *d++ = 'R'; | |
1238 | ||
1239 | if (attr & 2) | |
1240 | *d++ = 'W'; | |
1241 | if (attr & 4) | |
1242 | *d++ = 'X'; | |
1243 | if (attr & 8) | |
1244 | *d++ = 'S'; | |
1245 | *d++ = 0; | |
1246 | sprintf (buf, "-attr %s %s", name, atts); | |
1247 | new_directive (xstrdup (buf)); | |
1248 | } | |
1249 | ||
1250 | void | |
2da42df6 | 1251 | def_code (int attr) |
252b5132 RH |
1252 | { |
1253 | ||
1254 | def_section ("CODE", attr); | |
1255 | } | |
1256 | ||
1257 | void | |
2da42df6 | 1258 | def_data (int attr) |
252b5132 RH |
1259 | { |
1260 | def_section ("DATA", attr); | |
1261 | } | |
1262 | ||
1263 | /**********************************************************************/ | |
1264 | ||
1265 | static void | |
2da42df6 | 1266 | run (const char *what, char *args) |
252b5132 RH |
1267 | { |
1268 | char *s; | |
1269 | int pid, wait_status; | |
1270 | int i; | |
1271 | const char **argv; | |
95086e1e | 1272 | char *errmsg_fmt = NULL, *errmsg_arg = NULL; |
f4492fb6 | 1273 | char *temp_base = make_temp_file (""); |
252b5132 | 1274 | |
9cf03b7e | 1275 | inform (_("run: %s %s"), what, args); |
252b5132 RH |
1276 | |
1277 | /* Count the args */ | |
1278 | i = 0; | |
1279 | for (s = args; *s; s++) | |
1280 | if (*s == ' ') | |
1281 | i++; | |
1282 | i++; | |
e1fa0163 | 1283 | argv = xmalloc (sizeof (char *) * (i + 3)); |
252b5132 RH |
1284 | i = 0; |
1285 | argv[i++] = what; | |
1286 | s = args; | |
1287 | while (1) | |
1288 | { | |
1289 | while (*s == ' ') | |
1290 | ++s; | |
1291 | argv[i++] = s; | |
1292 | while (*s != ' ' && *s != 0) | |
1293 | s++; | |
1294 | if (*s == 0) | |
1295 | break; | |
1296 | *s++ = 0; | |
1297 | } | |
1298 | argv[i++] = NULL; | |
1299 | ||
1300 | pid = pexecute (argv[0], (char * const *) argv, program_name, temp_base, | |
1301 | &errmsg_fmt, &errmsg_arg, PEXECUTE_ONE | PEXECUTE_SEARCH); | |
9db70fc3 | 1302 | free (argv); |
252b5132 RH |
1303 | |
1304 | if (pid == -1) | |
1305 | { | |
20359e08 | 1306 | inform ("%s", strerror (errno)); |
26044998 | 1307 | |
252b5132 RH |
1308 | fatal (errmsg_fmt, errmsg_arg); |
1309 | } | |
1310 | ||
1311 | pid = pwait (pid, & wait_status, 0); | |
26044998 | 1312 | |
252b5132 RH |
1313 | if (pid == -1) |
1314 | { | |
1315 | /* xgettext:c-format */ | |
1316 | fatal (_("wait: %s"), strerror (errno)); | |
1317 | } | |
1318 | else if (WIFSIGNALED (wait_status)) | |
1319 | { | |
1320 | /* xgettext:c-format */ | |
1321 | fatal (_("subprocess got fatal signal %d"), WTERMSIG (wait_status)); | |
1322 | } | |
1323 | else if (WIFEXITED (wait_status)) | |
1324 | { | |
1325 | if (WEXITSTATUS (wait_status) != 0) | |
1326 | /* xgettext:c-format */ | |
37cc8ec1 AM |
1327 | non_fatal (_("%s exited with status %d"), |
1328 | what, WEXITSTATUS (wait_status)); | |
252b5132 RH |
1329 | } |
1330 | else | |
1331 | abort (); | |
1332 | } | |
1333 | ||
1334 | /* Look for a list of symbols to export in the .drectve section of | |
1335 | ABFD. Pass each one to def_exports. */ | |
1336 | ||
1337 | static void | |
2da42df6 | 1338 | scan_drectve_symbols (bfd *abfd) |
252b5132 RH |
1339 | { |
1340 | asection * s; | |
1341 | int size; | |
1342 | char * buf; | |
1343 | char * p; | |
1344 | char * e; | |
661016bb | 1345 | |
252b5132 | 1346 | /* Look for .drectve's */ |
661016bb | 1347 | s = bfd_get_section_by_name (abfd, DRECTVE_SECTION_NAME); |
26044998 | 1348 | |
252b5132 RH |
1349 | if (s == NULL) |
1350 | return; | |
26044998 | 1351 | |
fd361982 | 1352 | size = bfd_section_size (s); |
252b5132 RH |
1353 | buf = xmalloc (size); |
1354 | ||
1355 | bfd_get_section_contents (abfd, s, buf, 0, size); | |
26044998 | 1356 | |
252b5132 | 1357 | /* xgettext:c-format */ |
37cc8ec1 | 1358 | inform (_("Sucking in info from %s section in %s"), |
661016bb | 1359 | DRECTVE_SECTION_NAME, bfd_get_filename (abfd)); |
252b5132 | 1360 | |
ce195b42 DD |
1361 | /* Search for -export: strings. The exported symbols can optionally |
1362 | have type tags (eg., -export:foo,data), so handle those as well. | |
5f0f29c3 | 1363 | Currently only data tag is supported. */ |
252b5132 RH |
1364 | p = buf; |
1365 | e = buf + size; | |
1366 | while (p < e) | |
1367 | { | |
1368 | if (p[0] == '-' | |
08dedd66 | 1369 | && startswith (p, "-export:")) |
252b5132 RH |
1370 | { |
1371 | char * name; | |
1372 | char * c; | |
ce195b42 | 1373 | flagword flags = BSF_FUNCTION; |
26044998 | 1374 | |
252b5132 | 1375 | p += 8; |
290c52bd KT |
1376 | /* Do we have a quoted export? */ |
1377 | if (*p == '"') | |
1378 | { | |
1379 | p++; | |
1380 | name = p; | |
1381 | while (p < e && *p != '"') | |
1382 | ++p; | |
1383 | } | |
1384 | else | |
1385 | { | |
1386 | name = p; | |
1387 | while (p < e && *p != ',' && *p != ' ' && *p != '-') | |
1388 | p++; | |
1389 | } | |
252b5132 RH |
1390 | c = xmalloc (p - name + 1); |
1391 | memcpy (c, name, p - name); | |
1392 | c[p - name] = 0; | |
290c52bd KT |
1393 | /* Advance over trailing quote. */ |
1394 | if (p < e && *p == '"') | |
1395 | ++p; | |
26044998 | 1396 | if (p < e && *p == ',') /* found type tag. */ |
ce195b42 DD |
1397 | { |
1398 | char *tag_start = ++p; | |
1399 | while (p < e && *p != ' ' && *p != '-') | |
1400 | p++; | |
08dedd66 | 1401 | if (startswith (tag_start, "data")) |
ce195b42 DD |
1402 | flags &= ~BSF_FUNCTION; |
1403 | } | |
1404 | ||
252b5132 RH |
1405 | /* FIXME: The 5th arg is for the `constant' field. |
1406 | What should it be? Not that it matters since it's not | |
1407 | currently useful. */ | |
bf201fdd | 1408 | def_exports (c, 0, -1, 0, 0, ! (flags & BSF_FUNCTION), 0, NULL); |
252b5132 RH |
1409 | |
1410 | if (add_stdcall_alias && strchr (c, '@')) | |
1411 | { | |
b34976b6 | 1412 | int lead_at = (*c == '@') ; |
c9e38879 | 1413 | char *exported_name = xstrdup (c + lead_at); |
252b5132 RH |
1414 | char *atsym = strchr (exported_name, '@'); |
1415 | *atsym = '\0'; | |
5f0f29c3 | 1416 | /* Note: stdcall alias symbols can never be data. */ |
bf201fdd | 1417 | def_exports (exported_name, xstrdup (c), -1, 0, 0, 0, 0, NULL); |
252b5132 RH |
1418 | } |
1419 | } | |
1420 | else | |
1421 | p++; | |
1422 | } | |
1423 | free (buf); | |
1424 | } | |
1425 | ||
1426 | /* Look through the symbols in MINISYMS, and add each one to list of | |
1427 | symbols to export. */ | |
1428 | ||
1429 | static void | |
2da42df6 AJ |
1430 | scan_filtered_symbols (bfd *abfd, void *minisyms, long symcount, |
1431 | unsigned int size) | |
252b5132 RH |
1432 | { |
1433 | asymbol *store; | |
1434 | bfd_byte *from, *fromend; | |
1435 | ||
1436 | store = bfd_make_empty_symbol (abfd); | |
1437 | if (store == NULL) | |
1438 | bfd_fatal (bfd_get_filename (abfd)); | |
1439 | ||
1440 | from = (bfd_byte *) minisyms; | |
1441 | fromend = from + symcount * size; | |
1442 | for (; from < fromend; from += size) | |
1443 | { | |
1444 | asymbol *sym; | |
1445 | const char *symbol_name; | |
1446 | ||
015dc7e1 | 1447 | sym = bfd_minisymbol_to_symbol (abfd, false, from, store); |
252b5132 RH |
1448 | if (sym == NULL) |
1449 | bfd_fatal (bfd_get_filename (abfd)); | |
1450 | ||
1451 | symbol_name = bfd_asymbol_name (sym); | |
1452 | if (bfd_get_symbol_leading_char (abfd) == symbol_name[0]) | |
1453 | ++symbol_name; | |
1454 | ||
ce195b42 | 1455 | def_exports (xstrdup (symbol_name) , 0, -1, 0, 0, |
bf201fdd | 1456 | ! (sym->flags & BSF_FUNCTION), 0, NULL); |
252b5132 RH |
1457 | |
1458 | if (add_stdcall_alias && strchr (symbol_name, '@')) | |
1459 | { | |
c9e38879 NC |
1460 | int lead_at = (*symbol_name == '@'); |
1461 | char *exported_name = xstrdup (symbol_name + lead_at); | |
252b5132 RH |
1462 | char *atsym = strchr (exported_name, '@'); |
1463 | *atsym = '\0'; | |
26044998 | 1464 | /* Note: stdcall alias symbols can never be data. */ |
bf201fdd | 1465 | def_exports (exported_name, xstrdup (symbol_name), -1, 0, 0, 0, 0, NULL); |
252b5132 RH |
1466 | } |
1467 | } | |
1468 | } | |
1469 | ||
1470 | /* Add a list of symbols to exclude. */ | |
1471 | ||
1472 | static void | |
2da42df6 | 1473 | add_excludes (const char *new_excludes) |
252b5132 RH |
1474 | { |
1475 | char *local_copy; | |
1476 | char *exclude_string; | |
1477 | ||
1478 | local_copy = xstrdup (new_excludes); | |
1479 | ||
1480 | exclude_string = strtok (local_copy, ",:"); | |
1481 | for (; exclude_string; exclude_string = strtok (NULL, ",:")) | |
1482 | { | |
1483 | struct string_list *new_exclude; | |
26044998 | 1484 | |
252b5132 RH |
1485 | new_exclude = ((struct string_list *) |
1486 | xmalloc (sizeof (struct string_list))); | |
1487 | new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 2); | |
c9e38879 NC |
1488 | /* Don't add a leading underscore for fastcall symbols. */ |
1489 | if (*exclude_string == '@') | |
1490 | sprintf (new_exclude->string, "%s", exclude_string); | |
1491 | else | |
36d21de5 KT |
1492 | sprintf (new_exclude->string, "%s%s", (!leading_underscore ? "" : "_"), |
1493 | exclude_string); | |
252b5132 RH |
1494 | new_exclude->next = excludes; |
1495 | excludes = new_exclude; | |
1496 | ||
1497 | /* xgettext:c-format */ | |
37cc8ec1 | 1498 | inform (_("Excluding symbol: %s"), exclude_string); |
252b5132 RH |
1499 | } |
1500 | ||
1501 | free (local_copy); | |
1502 | } | |
1503 | ||
1504 | /* See if STRING is on the list of symbols to exclude. */ | |
1505 | ||
015dc7e1 | 1506 | static bool |
2da42df6 | 1507 | match_exclude (const char *string) |
252b5132 RH |
1508 | { |
1509 | struct string_list *excl_item; | |
1510 | ||
1511 | for (excl_item = excludes; excl_item; excl_item = excl_item->next) | |
1512 | if (strcmp (string, excl_item->string) == 0) | |
015dc7e1 AM |
1513 | return true; |
1514 | return false; | |
252b5132 RH |
1515 | } |
1516 | ||
1517 | /* Add the default list of symbols to exclude. */ | |
1518 | ||
1519 | static void | |
1520 | set_default_excludes (void) | |
1521 | { | |
1522 | add_excludes (default_excludes); | |
1523 | } | |
1524 | ||
1525 | /* Choose which symbols to export. */ | |
1526 | ||
1527 | static long | |
2da42df6 | 1528 | filter_symbols (bfd *abfd, void *minisyms, long symcount, unsigned int size) |
252b5132 RH |
1529 | { |
1530 | bfd_byte *from, *fromend, *to; | |
1531 | asymbol *store; | |
1532 | ||
1533 | store = bfd_make_empty_symbol (abfd); | |
1534 | if (store == NULL) | |
1535 | bfd_fatal (bfd_get_filename (abfd)); | |
1536 | ||
1537 | from = (bfd_byte *) minisyms; | |
1538 | fromend = from + symcount * size; | |
1539 | to = (bfd_byte *) minisyms; | |
1540 | ||
1541 | for (; from < fromend; from += size) | |
1542 | { | |
1543 | int keep = 0; | |
1544 | asymbol *sym; | |
1545 | ||
015dc7e1 | 1546 | sym = bfd_minisymbol_to_symbol (abfd, false, (const void *) from, store); |
252b5132 RH |
1547 | if (sym == NULL) |
1548 | bfd_fatal (bfd_get_filename (abfd)); | |
1549 | ||
1550 | /* Check for external and defined only symbols. */ | |
1551 | keep = (((sym->flags & BSF_GLOBAL) != 0 | |
1552 | || (sym->flags & BSF_WEAK) != 0 | |
1553 | || bfd_is_com_section (sym->section)) | |
1554 | && ! bfd_is_und_section (sym->section)); | |
26044998 | 1555 | |
252b5132 RH |
1556 | keep = keep && ! match_exclude (sym->name); |
1557 | ||
1558 | if (keep) | |
1559 | { | |
1560 | memcpy (to, from, size); | |
1561 | to += size; | |
1562 | } | |
1563 | } | |
1564 | ||
1565 | return (to - (bfd_byte *) minisyms) / size; | |
1566 | } | |
1567 | ||
1568 | /* Export all symbols in ABFD, except for ones we were told not to | |
1569 | export. */ | |
1570 | ||
1571 | static void | |
2da42df6 | 1572 | scan_all_symbols (bfd *abfd) |
252b5132 RH |
1573 | { |
1574 | long symcount; | |
2da42df6 | 1575 | void *minisyms; |
252b5132 RH |
1576 | unsigned int size; |
1577 | ||
1578 | /* Ignore bfds with an import descriptor table. We assume that any | |
1579 | such BFD contains symbols which are exported from another DLL, | |
1580 | and we don't want to reexport them from here. */ | |
1581 | if (bfd_get_section_by_name (abfd, ".idata$4")) | |
1582 | return; | |
1583 | ||
1584 | if (! (bfd_get_file_flags (abfd) & HAS_SYMS)) | |
1585 | { | |
1586 | /* xgettext:c-format */ | |
37cc8ec1 | 1587 | non_fatal (_("%s: no symbols"), bfd_get_filename (abfd)); |
252b5132 RH |
1588 | return; |
1589 | } | |
1590 | ||
015dc7e1 | 1591 | symcount = bfd_read_minisymbols (abfd, false, &minisyms, &size); |
252b5132 RH |
1592 | if (symcount < 0) |
1593 | bfd_fatal (bfd_get_filename (abfd)); | |
1594 | ||
1595 | if (symcount == 0) | |
1596 | { | |
1597 | /* xgettext:c-format */ | |
37cc8ec1 | 1598 | non_fatal (_("%s: no symbols"), bfd_get_filename (abfd)); |
252b5132 RH |
1599 | return; |
1600 | } | |
1601 | ||
1602 | /* Discard the symbols we don't want to export. It's OK to do this | |
1603 | in place; we'll free the storage anyway. */ | |
1604 | ||
1605 | symcount = filter_symbols (abfd, minisyms, symcount, size); | |
1606 | scan_filtered_symbols (abfd, minisyms, symcount, size); | |
1607 | ||
1608 | free (minisyms); | |
1609 | } | |
1610 | ||
1611 | /* Look at the object file to decide which symbols to export. */ | |
1612 | ||
1613 | static void | |
2da42df6 | 1614 | scan_open_obj_file (bfd *abfd) |
252b5132 RH |
1615 | { |
1616 | if (export_all_symbols) | |
1617 | scan_all_symbols (abfd); | |
1618 | else | |
1619 | scan_drectve_symbols (abfd); | |
26044998 | 1620 | |
c9e38879 | 1621 | /* FIXME: we ought to read in and block out the base relocations. */ |
252b5132 RH |
1622 | |
1623 | /* xgettext:c-format */ | |
37cc8ec1 | 1624 | inform (_("Done reading %s"), bfd_get_filename (abfd)); |
252b5132 RH |
1625 | } |
1626 | ||
1627 | static void | |
2da42df6 | 1628 | scan_obj_file (const char *filename) |
252b5132 RH |
1629 | { |
1630 | bfd * f = bfd_openr (filename, 0); | |
1631 | ||
1632 | if (!f) | |
1633 | /* xgettext:c-format */ | |
dec87289 | 1634 | fatal (_("Unable to open object file: %s: %s"), filename, bfd_get_errmsg ()); |
252b5132 RH |
1635 | |
1636 | /* xgettext:c-format */ | |
1637 | inform (_("Scanning object file %s"), filename); | |
26044998 | 1638 | |
252b5132 RH |
1639 | if (bfd_check_format (f, bfd_archive)) |
1640 | { | |
1641 | bfd *arfile = bfd_openr_next_archived_file (f, 0); | |
1642 | while (arfile) | |
1643 | { | |
a442b35f | 1644 | bfd *next; |
252b5132 RH |
1645 | if (bfd_check_format (arfile, bfd_object)) |
1646 | scan_open_obj_file (arfile); | |
a442b35f | 1647 | next = bfd_openr_next_archived_file (f, arfile); |
252b5132 | 1648 | bfd_close (arfile); |
d7b24d29 NC |
1649 | /* PR 17512: file: 58715298. */ |
1650 | if (next == arfile) | |
1651 | break; | |
a442b35f | 1652 | arfile = next; |
252b5132 | 1653 | } |
26044998 | 1654 | |
49e315b1 NC |
1655 | #ifdef DLLTOOL_MCORE_ELF |
1656 | if (mcore_elf_out_file) | |
1657 | inform (_("Cannot produce mcore-elf dll from archive file: %s"), filename); | |
1658 | #endif | |
252b5132 RH |
1659 | } |
1660 | else if (bfd_check_format (f, bfd_object)) | |
1661 | { | |
1662 | scan_open_obj_file (f); | |
49e315b1 NC |
1663 | |
1664 | #ifdef DLLTOOL_MCORE_ELF | |
1665 | if (mcore_elf_out_file) | |
fd64a958 | 1666 | mcore_elf_cache_filename (filename); |
49e315b1 | 1667 | #endif |
252b5132 RH |
1668 | } |
1669 | ||
1670 | bfd_close (f); | |
1671 | } | |
1672 | ||
dec87289 | 1673 | \f |
252b5132 RH |
1674 | |
1675 | static void | |
2da42df6 | 1676 | dump_def_info (FILE *f) |
252b5132 RH |
1677 | { |
1678 | int i; | |
1679 | export_type *exp; | |
1680 | fprintf (f, "%s ", ASM_C); | |
1681 | for (i = 0; oav[i]; i++) | |
1682 | fprintf (f, "%s ", oav[i]); | |
1683 | fprintf (f, "\n"); | |
1684 | for (i = 0, exp = d_exports; exp; i++, exp = exp->next) | |
1685 | { | |
bf201fdd | 1686 | fprintf (f, "%s %d = %s %s @ %d %s%s%s%s%s%s\n", |
252b5132 RH |
1687 | ASM_C, |
1688 | i, | |
1689 | exp->name, | |
1690 | exp->internal_name, | |
1691 | exp->ordinal, | |
1692 | exp->noname ? "NONAME " : "", | |
7aa52b1f | 1693 | exp->private ? "PRIVATE " : "", |
252b5132 | 1694 | exp->constant ? "CONSTANT" : "", |
bf201fdd KT |
1695 | exp->data ? "DATA" : "", |
1696 | exp->its_name ? " ==" : "", | |
1697 | exp->its_name ? exp->its_name : ""); | |
252b5132 RH |
1698 | } |
1699 | } | |
1700 | ||
c9e38879 | 1701 | /* Generate the .exp file. */ |
252b5132 RH |
1702 | |
1703 | static int | |
2da42df6 | 1704 | sfunc (const void *a, const void *b) |
252b5132 | 1705 | { |
d078078d KT |
1706 | if (*(const bfd_vma *) a == *(const bfd_vma *) b) |
1707 | return 0; | |
1708 | ||
1709 | return ((*(const bfd_vma *) a > *(const bfd_vma *) b) ? 1 : -1); | |
252b5132 RH |
1710 | } |
1711 | ||
1712 | static void | |
d078078d | 1713 | flush_page (FILE *f, bfd_vma *need, bfd_vma page_addr, int on_page) |
252b5132 RH |
1714 | { |
1715 | int i; | |
1716 | ||
c9e38879 | 1717 | /* Flush this page. */ |
252b5132 RH |
1718 | fprintf (f, "\t%s\t0x%08x\t%s Starting RVA for chunk\n", |
1719 | ASM_LONG, | |
d078078d | 1720 | (int) page_addr, |
252b5132 RH |
1721 | ASM_C); |
1722 | fprintf (f, "\t%s\t0x%x\t%s Size of block\n", | |
1723 | ASM_LONG, | |
1724 | (on_page * 2) + (on_page & 1) * 2 + 8, | |
1725 | ASM_C); | |
26044998 | 1726 | |
252b5132 | 1727 | for (i = 0; i < on_page; i++) |
a2186dfe | 1728 | { |
d078078d | 1729 | bfd_vma needed = need[i]; |
26044998 | 1730 | |
a2186dfe | 1731 | if (needed) |
d078078d | 1732 | { |
2ea2f3c6 KT |
1733 | if (!create_for_pep) |
1734 | { | |
1735 | /* Relocation via HIGHLOW. */ | |
1736 | needed = ((needed - page_addr) | 0x3000) & 0xffff; | |
1737 | } | |
1738 | else | |
1739 | { | |
1740 | /* Relocation via DIR64. */ | |
1741 | needed = ((needed - page_addr) | 0xa000) & 0xffff; | |
1742 | } | |
d078078d | 1743 | } |
26044998 | 1744 | |
d078078d | 1745 | fprintf (f, "\t%s\t0x%lx\n", ASM_SHORT, (long) needed); |
a2186dfe | 1746 | } |
26044998 | 1747 | |
252b5132 RH |
1748 | /* And padding */ |
1749 | if (on_page & 1) | |
1750 | fprintf (f, "\t%s\t0x%x\n", ASM_SHORT, 0 | 0x0000); | |
1751 | } | |
1752 | ||
1753 | static void | |
2da42df6 | 1754 | gen_def_file (void) |
252b5132 RH |
1755 | { |
1756 | int i; | |
1757 | export_type *exp; | |
1758 | ||
1759 | inform (_("Adding exports to output file")); | |
26044998 | 1760 | |
252b5132 RH |
1761 | fprintf (output_def, ";"); |
1762 | for (i = 0; oav[i]; i++) | |
1763 | fprintf (output_def, " %s", oav[i]); | |
1764 | ||
1765 | fprintf (output_def, "\nEXPORTS\n"); | |
1766 | ||
1767 | for (i = 0, exp = d_exports; exp; i++, exp = exp->next) | |
1768 | { | |
1769 | char *quote = strchr (exp->name, '.') ? "\"" : ""; | |
1770 | char *res = cplus_demangle (exp->internal_name, DMGL_ANSI | DMGL_PARAMS); | |
1771 | ||
2630b4ca DS |
1772 | if (res) |
1773 | { | |
2da42df6 | 1774 | fprintf (output_def,";\t%s\n", res); |
2630b4ca DS |
1775 | free (res); |
1776 | } | |
1777 | ||
252b5132 | 1778 | if (strcmp (exp->name, exp->internal_name) == 0) |
26044998 | 1779 | { |
bf201fdd | 1780 | fprintf (output_def, "\t%s%s%s @ %d%s%s%s%s%s\n", |
252b5132 RH |
1781 | quote, |
1782 | exp->name, | |
1783 | quote, | |
1784 | exp->ordinal, | |
1785 | exp->noname ? " NONAME" : "", | |
7aa52b1f | 1786 | exp->private ? "PRIVATE " : "", |
bf201fdd KT |
1787 | exp->data ? " DATA" : "", |
1788 | exp->its_name ? " ==" : "", | |
1789 | exp->its_name ? exp->its_name : ""); | |
252b5132 | 1790 | } |
26044998 KH |
1791 | else |
1792 | { | |
7aa52b1f | 1793 | char * quote1 = strchr (exp->internal_name, '.') ? "\"" : ""; |
252b5132 | 1794 | /* char *alias = */ |
bf201fdd | 1795 | fprintf (output_def, "\t%s%s%s = %s%s%s @ %d%s%s%s%s%s\n", |
252b5132 RH |
1796 | quote, |
1797 | exp->name, | |
1798 | quote, | |
1799 | quote1, | |
1800 | exp->internal_name, | |
1801 | quote1, | |
1802 | exp->ordinal, | |
1803 | exp->noname ? " NONAME" : "", | |
7aa52b1f | 1804 | exp->private ? "PRIVATE " : "", |
bf201fdd KT |
1805 | exp->data ? " DATA" : "", |
1806 | exp->its_name ? " ==" : "", | |
1807 | exp->its_name ? exp->its_name : ""); | |
252b5132 | 1808 | } |
252b5132 | 1809 | } |
26044998 | 1810 | |
252b5132 RH |
1811 | inform (_("Added exports to output file")); |
1812 | } | |
1813 | ||
1814 | /* generate_idata_ofile generates the portable assembly source code | |
1815 | for the idata sections. It appends the source code to the end of | |
1816 | the file. */ | |
1817 | ||
1818 | static void | |
2da42df6 | 1819 | generate_idata_ofile (FILE *filvar) |
252b5132 RH |
1820 | { |
1821 | iheadtype *headptr; | |
1822 | ifunctype *funcptr; | |
1823 | int headindex; | |
1824 | int funcindex; | |
1825 | int nheads; | |
1826 | ||
1827 | if (import_list == NULL) | |
1828 | return; | |
1829 | ||
1830 | fprintf (filvar, "%s Import data sections\n", ASM_C); | |
1831 | fprintf (filvar, "\n\t.section\t.idata$2\n"); | |
1832 | fprintf (filvar, "\t%s\tdoi_idata\n", ASM_GLOBAL); | |
1833 | fprintf (filvar, "doi_idata:\n"); | |
1834 | ||
1835 | nheads = 0; | |
1836 | for (headptr = import_list; headptr != NULL; headptr = headptr->next) | |
1837 | { | |
1838 | fprintf (filvar, "\t%slistone%d%s\t%s %s\n", | |
1839 | ASM_RVA_BEFORE, nheads, ASM_RVA_AFTER, | |
1840 | ASM_C, headptr->dllname); | |
1841 | fprintf (filvar, "\t%s\t0\n", ASM_LONG); | |
1842 | fprintf (filvar, "\t%s\t0\n", ASM_LONG); | |
1843 | fprintf (filvar, "\t%sdllname%d%s\n", | |
1844 | ASM_RVA_BEFORE, nheads, ASM_RVA_AFTER); | |
1845 | fprintf (filvar, "\t%slisttwo%d%s\n\n", | |
1846 | ASM_RVA_BEFORE, nheads, ASM_RVA_AFTER); | |
1847 | nheads++; | |
1848 | } | |
1849 | ||
1850 | fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL record at */ | |
1851 | fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* end of idata$2 */ | |
1852 | fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* section */ | |
1853 | fprintf (filvar, "\t%s\t0\n", ASM_LONG); | |
1854 | fprintf (filvar, "\t%s\t0\n", ASM_LONG); | |
1855 | ||
1856 | fprintf (filvar, "\n\t.section\t.idata$4\n"); | |
1857 | headindex = 0; | |
1858 | for (headptr = import_list; headptr != NULL; headptr = headptr->next) | |
1859 | { | |
1860 | fprintf (filvar, "listone%d:\n", headindex); | |
99ad8390 | 1861 | for (funcindex = 0; funcindex < headptr->nfuncs; funcindex++) |
2ea2f3c6 KT |
1862 | { |
1863 | if (create_for_pep) | |
1864 | fprintf (filvar, "\t%sfuncptr%d_%d%s\n%s\t0\n", | |
1865 | ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER, | |
1866 | ASM_LONG); | |
1867 | else | |
1868 | fprintf (filvar, "\t%sfuncptr%d_%d%s\n", | |
1869 | ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER); | |
1870 | } | |
1871 | if (create_for_pep) | |
1872 | fprintf (filvar, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG); | |
1873 | else | |
1874 | fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL terminating list. */ | |
252b5132 RH |
1875 | headindex++; |
1876 | } | |
1877 | ||
1878 | fprintf (filvar, "\n\t.section\t.idata$5\n"); | |
1879 | headindex = 0; | |
1880 | for (headptr = import_list; headptr != NULL; headptr = headptr->next) | |
1881 | { | |
1882 | fprintf (filvar, "listtwo%d:\n", headindex); | |
99ad8390 | 1883 | for (funcindex = 0; funcindex < headptr->nfuncs; funcindex++) |
2ea2f3c6 KT |
1884 | { |
1885 | if (create_for_pep) | |
1886 | fprintf (filvar, "\t%sfuncptr%d_%d%s\n%s\t0\n", | |
1887 | ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER, | |
1888 | ASM_LONG); | |
1889 | else | |
1890 | fprintf (filvar, "\t%sfuncptr%d_%d%s\n", | |
1891 | ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER); | |
1892 | } | |
1893 | if (create_for_pep) | |
1894 | fprintf (filvar, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG); | |
1895 | else | |
1896 | fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL terminating list. */ | |
252b5132 RH |
1897 | headindex++; |
1898 | } | |
1899 | ||
1900 | fprintf (filvar, "\n\t.section\t.idata$6\n"); | |
1901 | headindex = 0; | |
1902 | for (headptr = import_list; headptr != NULL; headptr = headptr->next) | |
1903 | { | |
1904 | funcindex = 0; | |
1905 | for (funcptr = headptr->funchead; funcptr != NULL; | |
1906 | funcptr = funcptr->next) | |
1907 | { | |
1908 | fprintf (filvar,"funcptr%d_%d:\n", headindex, funcindex); | |
1909 | fprintf (filvar,"\t%s\t%d\n", ASM_SHORT, | |
1910 | ((funcptr->ord) & 0xFFFF)); | |
bf201fdd KT |
1911 | fprintf (filvar,"\t%s\t\"%s\"\n", ASM_TEXT, |
1912 | (funcptr->its_name ? funcptr->its_name : funcptr->name)); | |
252b5132 RH |
1913 | fprintf (filvar,"\t%s\t0\n", ASM_BYTE); |
1914 | funcindex++; | |
1915 | } | |
1916 | headindex++; | |
1917 | } | |
1918 | ||
1919 | fprintf (filvar, "\n\t.section\t.idata$7\n"); | |
1920 | headindex = 0; | |
1921 | for (headptr = import_list; headptr != NULL; headptr = headptr->next) | |
1922 | { | |
1923 | fprintf (filvar,"dllname%d:\n", headindex); | |
1924 | fprintf (filvar,"\t%s\t\"%s\"\n", ASM_TEXT, headptr->dllname); | |
1925 | fprintf (filvar,"\t%s\t0\n", ASM_BYTE); | |
1926 | headindex++; | |
1927 | } | |
1928 | } | |
1929 | ||
26044998 | 1930 | /* Assemble the specified file. */ |
49c24507 | 1931 | static void |
2da42df6 | 1932 | assemble_file (const char * source, const char * dest) |
49c24507 NC |
1933 | { |
1934 | char * cmd; | |
26044998 | 1935 | |
e1fa0163 NC |
1936 | cmd = xmalloc (strlen (ASM_SWITCHES) + strlen (as_flags) |
1937 | + strlen (source) + strlen (dest) + 50); | |
49c24507 NC |
1938 | |
1939 | sprintf (cmd, "%s %s -o %s %s", ASM_SWITCHES, as_flags, dest, source); | |
1940 | ||
1941 | run (as_name, cmd); | |
e1fa0163 | 1942 | free (cmd); |
49c24507 NC |
1943 | } |
1944 | ||
81fb971a NC |
1945 | static const char * temp_file_to_remove[5]; |
1946 | #define TEMP_EXPORT_FILE 0 | |
1947 | #define TEMP_HEAD_FILE 1 | |
1948 | #define TEMP_TAIL_FILE 2 | |
1949 | #define TEMP_HEAD_O_FILE 3 | |
1950 | #define TEMP_TAIL_O_FILE 4 | |
1951 | ||
1952 | static void | |
1953 | unlink_temp_files (void) | |
1954 | { | |
1955 | unsigned i; | |
1956 | ||
1957 | if (dontdeltemps > 0) | |
1958 | return; | |
1959 | ||
1960 | for (i = 0; i < ARRAY_SIZE (temp_file_to_remove); i++) | |
1961 | { | |
1962 | if (temp_file_to_remove[i]) | |
1963 | { | |
1964 | unlink (temp_file_to_remove[i]); | |
1965 | temp_file_to_remove[i] = NULL; | |
1966 | } | |
1967 | } | |
1968 | } | |
1969 | ||
252b5132 | 1970 | static void |
2da42df6 | 1971 | gen_exp_file (void) |
252b5132 RH |
1972 | { |
1973 | FILE *f; | |
1974 | int i; | |
1975 | export_type *exp; | |
1976 | dlist_type *dl; | |
1977 | ||
1978 | /* xgettext:c-format */ | |
37cc8ec1 | 1979 | inform (_("Generating export file: %s"), exp_name); |
26044998 | 1980 | |
252b5132 RH |
1981 | f = fopen (TMP_ASM, FOPEN_WT); |
1982 | if (!f) | |
1983 | /* xgettext:c-format */ | |
1984 | fatal (_("Unable to open temporary assembler file: %s"), TMP_ASM); | |
26044998 | 1985 | |
81fb971a | 1986 | temp_file_to_remove[TEMP_EXPORT_FILE] = TMP_ASM; |
3aade688 | 1987 | |
252b5132 RH |
1988 | /* xgettext:c-format */ |
1989 | inform (_("Opened temporary file: %s"), TMP_ASM); | |
1990 | ||
1991 | dump_def_info (f); | |
26044998 | 1992 | |
252b5132 RH |
1993 | if (d_exports) |
1994 | { | |
1995 | fprintf (f, "\t.section .edata\n\n"); | |
1996 | fprintf (f, "\t%s 0 %s Allways 0\n", ASM_LONG, ASM_C); | |
0af1713e AM |
1997 | fprintf (f, "\t%s 0x%lx %s Time and date\n", ASM_LONG, |
1998 | (unsigned long) time(0), ASM_C); | |
252b5132 RH |
1999 | fprintf (f, "\t%s 0 %s Major and Minor version\n", ASM_LONG, ASM_C); |
2000 | fprintf (f, "\t%sname%s %s Ptr to name of dll\n", ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C); | |
2001 | fprintf (f, "\t%s %d %s Starting ordinal of exports\n", ASM_LONG, d_low_ord, ASM_C); | |
2002 | ||
2003 | ||
2004 | fprintf (f, "\t%s %d %s Number of functions\n", ASM_LONG, d_high_ord - d_low_ord + 1, ASM_C); | |
2005 | fprintf(f,"\t%s named funcs %d, low ord %d, high ord %d\n", | |
2006 | ASM_C, | |
2007 | d_named_nfuncs, d_low_ord, d_high_ord); | |
2008 | fprintf (f, "\t%s %d %s Number of names\n", ASM_LONG, | |
2009 | show_allnames ? d_high_ord - d_low_ord + 1 : d_named_nfuncs, ASM_C); | |
2010 | fprintf (f, "\t%safuncs%s %s Address of functions\n", ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C); | |
2011 | ||
2012 | fprintf (f, "\t%sanames%s %s Address of Name Pointer Table\n", | |
2013 | ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C); | |
2014 | ||
2015 | fprintf (f, "\t%sanords%s %s Address of ordinals\n", ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C); | |
2016 | ||
2017 | fprintf (f, "name: %s \"%s\"\n", ASM_TEXT, dll_name); | |
2018 | ||
2019 | ||
2020 | fprintf(f,"%s Export address Table\n", ASM_C); | |
2021 | fprintf(f,"\t%s\n", ASM_ALIGN_LONG); | |
2022 | fprintf (f, "afuncs:\n"); | |
2023 | i = d_low_ord; | |
2024 | ||
2025 | for (exp = d_exports; exp; exp = exp->next) | |
2026 | { | |
2027 | if (exp->ordinal != i) | |
2028 | { | |
252b5132 RH |
2029 | while (i < exp->ordinal) |
2030 | { | |
2031 | fprintf(f,"\t%s\t0\n", ASM_LONG); | |
2032 | i++; | |
2033 | } | |
2034 | } | |
04847a4d CF |
2035 | |
2036 | if (exp->forward == 0) | |
c9e38879 NC |
2037 | { |
2038 | if (exp->internal_name[0] == '@') | |
2039 | fprintf (f, "\t%s%s%s\t%s %d\n", ASM_RVA_BEFORE, | |
2040 | exp->internal_name, ASM_RVA_AFTER, ASM_C, exp->ordinal); | |
2041 | else | |
2042 | fprintf (f, "\t%s%s%s%s\t%s %d\n", ASM_RVA_BEFORE, | |
2758961a | 2043 | ASM_PREFIX (exp->internal_name), |
c9e38879 NC |
2044 | exp->internal_name, ASM_RVA_AFTER, ASM_C, exp->ordinal); |
2045 | } | |
04847a4d CF |
2046 | else |
2047 | fprintf (f, "\t%sf%d%s\t%s %d\n", ASM_RVA_BEFORE, | |
2048 | exp->forward, ASM_RVA_AFTER, ASM_C, exp->ordinal); | |
252b5132 RH |
2049 | i++; |
2050 | } | |
2051 | ||
2052 | fprintf (f,"%s Export Name Pointer Table\n", ASM_C); | |
2053 | fprintf (f, "anames:\n"); | |
2054 | ||
2055 | for (i = 0; (exp = d_exports_lexically[i]); i++) | |
2056 | { | |
2057 | if (!exp->noname || show_allnames) | |
2058 | fprintf (f, "\t%sn%d%s\n", | |
2059 | ASM_RVA_BEFORE, exp->ordinal, ASM_RVA_AFTER); | |
2060 | } | |
2061 | ||
ea6e992c | 2062 | fprintf (f,"%s Export Ordinal Table\n", ASM_C); |
252b5132 RH |
2063 | fprintf (f, "anords:\n"); |
2064 | for (i = 0; (exp = d_exports_lexically[i]); i++) | |
2065 | { | |
2066 | if (!exp->noname || show_allnames) | |
2067 | fprintf (f, "\t%s %d\n", ASM_SHORT, exp->ordinal - d_low_ord); | |
2068 | } | |
2069 | ||
2070 | fprintf(f,"%s Export Name Table\n", ASM_C); | |
2071 | for (i = 0; (exp = d_exports_lexically[i]); i++) | |
eff21b8e CF |
2072 | { |
2073 | if (!exp->noname || show_allnames) | |
04847a4d | 2074 | fprintf (f, "n%d: %s \"%s\"\n", |
bf201fdd KT |
2075 | exp->ordinal, ASM_TEXT, |
2076 | (exp->its_name ? exp->its_name : xlate (exp->name))); | |
eff21b8e CF |
2077 | if (exp->forward != 0) |
2078 | fprintf (f, "f%d: %s \"%s\"\n", | |
2079 | exp->forward, ASM_TEXT, exp->internal_name); | |
2080 | } | |
252b5132 RH |
2081 | |
2082 | if (a_list) | |
2083 | { | |
661016bb | 2084 | fprintf (f, "\t.section %s\n", DRECTVE_SECTION_NAME); |
252b5132 RH |
2085 | for (dl = a_list; dl; dl = dl->next) |
2086 | { | |
2087 | fprintf (f, "\t%s\t\"%s\"\n", ASM_TEXT, dl->text); | |
2088 | } | |
2089 | } | |
26044998 | 2090 | |
252b5132 RH |
2091 | if (d_list) |
2092 | { | |
2093 | fprintf (f, "\t.section .rdata\n"); | |
2094 | for (dl = d_list; dl; dl = dl->next) | |
2095 | { | |
2096 | char *p; | |
2097 | int l; | |
26044998 | 2098 | |
5f0f29c3 NC |
2099 | /* We don't output as ascii because there can |
2100 | be quote characters in the string. */ | |
252b5132 RH |
2101 | l = 0; |
2102 | for (p = dl->text; *p; p++) | |
2103 | { | |
2104 | if (l == 0) | |
2105 | fprintf (f, "\t%s\t", ASM_BYTE); | |
2106 | else | |
2107 | fprintf (f, ","); | |
2108 | fprintf (f, "%d", *p); | |
2109 | if (p[1] == 0) | |
2110 | { | |
2111 | fprintf (f, ",0\n"); | |
2112 | break; | |
2113 | } | |
2114 | if (++l == 10) | |
2115 | { | |
2116 | fprintf (f, "\n"); | |
2117 | l = 0; | |
2118 | } | |
2119 | } | |
2120 | } | |
2121 | } | |
2122 | } | |
2123 | ||
252b5132 | 2124 | /* Add to the output file a way of getting to the exported names |
5f0f29c3 | 2125 | without using the import library. */ |
252b5132 RH |
2126 | if (add_indirect) |
2127 | { | |
2128 | fprintf (f, "\t.section\t.rdata\n"); | |
2129 | for (i = 0, exp = d_exports; exp; i++, exp = exp->next) | |
2130 | if (!exp->noname || show_allnames) | |
2131 | { | |
2132 | /* We use a single underscore for MS compatibility, and a | |
2133 | double underscore for backward compatibility with old | |
2134 | cygwin releases. */ | |
5f0f29c3 NC |
2135 | if (create_compat_implib) |
2136 | fprintf (f, "\t%s\t__imp_%s\n", ASM_GLOBAL, exp->name); | |
36d21de5 KT |
2137 | fprintf (f, "\t%s\t_imp_%s%s\n", ASM_GLOBAL, |
2138 | (!leading_underscore ? "" : "_"), exp->name); | |
5f0f29c3 NC |
2139 | if (create_compat_implib) |
2140 | fprintf (f, "__imp_%s:\n", exp->name); | |
36d21de5 | 2141 | fprintf (f, "_imp_%s%s:\n", (!leading_underscore ? "" : "_"), exp->name); |
252b5132 RH |
2142 | fprintf (f, "\t%s\t%s\n", ASM_LONG, exp->name); |
2143 | } | |
2144 | } | |
2145 | ||
c9e38879 | 2146 | /* Dump the reloc section if a base file is provided. */ |
252b5132 RH |
2147 | if (base_file) |
2148 | { | |
d078078d | 2149 | bfd_vma addr; |
e05da72d | 2150 | bfd_vma need[COFF_PAGE_SIZE]; |
d078078d | 2151 | bfd_vma page_addr; |
20359e08 | 2152 | bfd_size_type numbytes; |
252b5132 | 2153 | int num_entries; |
d078078d | 2154 | bfd_vma *copy; |
252b5132 RH |
2155 | int j; |
2156 | int on_page; | |
2157 | fprintf (f, "\t.section\t.init\n"); | |
2158 | fprintf (f, "lab:\n"); | |
2159 | ||
2160 | fseek (base_file, 0, SEEK_END); | |
2161 | numbytes = ftell (base_file); | |
2162 | fseek (base_file, 0, SEEK_SET); | |
2163 | copy = xmalloc (numbytes); | |
20359e08 NC |
2164 | if (fread (copy, 1, numbytes, base_file) < numbytes) |
2165 | fatal (_("failed to read the number of entries from base file")); | |
d078078d | 2166 | num_entries = numbytes / sizeof (bfd_vma); |
252b5132 RH |
2167 | |
2168 | ||
2169 | fprintf (f, "\t.section\t.reloc\n"); | |
2170 | if (num_entries) | |
2171 | { | |
2172 | int src; | |
2173 | int dst = 0; | |
d078078d KT |
2174 | bfd_vma last = (bfd_vma) -1; |
2175 | qsort (copy, num_entries, sizeof (bfd_vma), sfunc); | |
50c2245b | 2176 | /* Delete duplicates */ |
252b5132 RH |
2177 | for (src = 0; src < num_entries; src++) |
2178 | { | |
2179 | if (last != copy[src]) | |
2180 | last = copy[dst++] = copy[src]; | |
2181 | } | |
2182 | num_entries = dst; | |
2183 | addr = copy[0]; | |
2184 | page_addr = addr & PAGE_MASK; /* work out the page addr */ | |
2185 | on_page = 0; | |
2186 | for (j = 0; j < num_entries; j++) | |
2187 | { | |
252b5132 RH |
2188 | addr = copy[j]; |
2189 | if ((addr & PAGE_MASK) != page_addr) | |
2190 | { | |
252b5132 RH |
2191 | flush_page (f, need, page_addr, on_page); |
2192 | on_page = 0; | |
2193 | page_addr = addr & PAGE_MASK; | |
2194 | } | |
2195 | need[on_page++] = addr; | |
2196 | } | |
252b5132 RH |
2197 | flush_page (f, need, page_addr, on_page); |
2198 | ||
d8bcc1ac | 2199 | /* fprintf (f, "\t%s\t0,0\t%s End\n", ASM_LONG, ASM_C);*/ |
252b5132 RH |
2200 | } |
2201 | } | |
2202 | ||
2203 | generate_idata_ofile (f); | |
2204 | ||
2205 | fclose (f); | |
2206 | ||
c9e38879 | 2207 | /* Assemble the file. */ |
49c24507 | 2208 | assemble_file (TMP_ASM, exp_name); |
bb0cb4db | 2209 | |
252b5132 | 2210 | if (dontdeltemps == 0) |
81fb971a NC |
2211 | { |
2212 | temp_file_to_remove[TEMP_EXPORT_FILE] = NULL; | |
2213 | unlink (TMP_ASM); | |
2214 | } | |
26044998 | 2215 | |
252b5132 RH |
2216 | inform (_("Generated exports file")); |
2217 | } | |
2218 | ||
2219 | static const char * | |
2da42df6 | 2220 | xlate (const char *name) |
252b5132 | 2221 | { |
c9e38879 | 2222 | int lead_at = (*name == '@'); |
36d21de5 | 2223 | int is_stdcall = (!lead_at && strchr (name, '@') != NULL); |
c9e38879 | 2224 | |
14288fdc | 2225 | if (!lead_at && (add_underscore |
36d21de5 | 2226 | || (add_stdcall_underscore && is_stdcall))) |
252b5132 RH |
2227 | { |
2228 | char *copy = xmalloc (strlen (name) + 2); | |
c9e38879 | 2229 | |
252b5132 RH |
2230 | copy[0] = '_'; |
2231 | strcpy (copy + 1, name); | |
2232 | name = copy; | |
2233 | } | |
2234 | ||
2235 | if (killat) | |
2236 | { | |
2237 | char *p; | |
c9e38879 NC |
2238 | |
2239 | name += lead_at; | |
2c2ce03f NC |
2240 | /* PR 9766: Look for the last @ sign in the name. */ |
2241 | p = strrchr (name, '@'); | |
2242 | if (p && ISDIGIT (p[1])) | |
252b5132 RH |
2243 | *p = 0; |
2244 | } | |
2245 | return name; | |
2246 | } | |
2247 | ||
252b5132 RH |
2248 | typedef struct |
2249 | { | |
2250 | int id; | |
2251 | const char *name; | |
2252 | int flags; | |
2253 | int align; | |
2254 | asection *sec; | |
2255 | asymbol *sym; | |
2256 | asymbol **sympp; | |
2257 | int size; | |
c9e38879 | 2258 | unsigned char *data; |
252b5132 RH |
2259 | } sinfo; |
2260 | ||
ce23608f AM |
2261 | #define INIT_SEC_DATA(id, name, flags, align) \ |
2262 | { id, name, flags, align, NULL, NULL, NULL, 0, NULL } | |
2263 | ||
252b5132 RH |
2264 | #define TEXT 0 |
2265 | #define DATA 1 | |
2266 | #define BSS 2 | |
2267 | #define IDATA7 3 | |
2268 | #define IDATA5 4 | |
2269 | #define IDATA4 5 | |
2270 | #define IDATA6 6 | |
2271 | ||
2272 | #define NSECS 7 | |
2273 | ||
bee72332 DD |
2274 | #define TEXT_SEC_FLAGS \ |
2275 | (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS) | |
2276 | #define DATA_SEC_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_DATA) | |
2277 | #define BSS_SEC_FLAGS SEC_ALLOC | |
2278 | ||
252b5132 RH |
2279 | static sinfo secdata[NSECS] = |
2280 | { | |
bee72332 DD |
2281 | INIT_SEC_DATA (TEXT, ".text", TEXT_SEC_FLAGS, 2), |
2282 | INIT_SEC_DATA (DATA, ".data", DATA_SEC_FLAGS, 2), | |
2283 | INIT_SEC_DATA (BSS, ".bss", BSS_SEC_FLAGS, 2), | |
2284 | INIT_SEC_DATA (IDATA7, ".idata$7", SEC_HAS_CONTENTS, 2), | |
2285 | INIT_SEC_DATA (IDATA5, ".idata$5", SEC_HAS_CONTENTS, 2), | |
2286 | INIT_SEC_DATA (IDATA4, ".idata$4", SEC_HAS_CONTENTS, 2), | |
2979a883 | 2287 | INIT_SEC_DATA (IDATA6, ".idata$6", SEC_HAS_CONTENTS, 1) |
252b5132 RH |
2288 | }; |
2289 | ||
c9e38879 NC |
2290 | /* This is what we're trying to make. We generate the imp symbols with |
2291 | both single and double underscores, for compatibility. | |
252b5132 RH |
2292 | |
2293 | .text | |
2294 | .global _GetFileVersionInfoSizeW@8 | |
2295 | .global __imp_GetFileVersionInfoSizeW@8 | |
2296 | _GetFileVersionInfoSizeW@8: | |
2297 | jmp * __imp_GetFileVersionInfoSizeW@8 | |
2298 | .section .idata$7 # To force loading of head | |
2299 | .long __version_a_head | |
2300 | # Import Address Table | |
2301 | .section .idata$5 | |
2302 | __imp_GetFileVersionInfoSizeW@8: | |
2303 | .rva ID2 | |
2304 | ||
2305 | # Import Lookup Table | |
2306 | .section .idata$4 | |
2307 | .rva ID2 | |
2308 | # Hint/Name table | |
2309 | .section .idata$6 | |
2310 | ID2: .short 2 | |
fe49679d | 2311 | .asciz "GetFileVersionInfoSizeW" */ |
252b5132 RH |
2312 | |
2313 | static char * | |
2da42df6 | 2314 | make_label (const char *prefix, const char *name) |
252b5132 | 2315 | { |
2758961a NC |
2316 | int len = strlen (ASM_PREFIX (name)) + strlen (prefix) + strlen (name); |
2317 | char *copy = xmalloc (len + 1); | |
c9e38879 | 2318 | |
2758961a | 2319 | strcpy (copy, ASM_PREFIX (name)); |
252b5132 RH |
2320 | strcat (copy, prefix); |
2321 | strcat (copy, name); | |
2322 | return copy; | |
2323 | } | |
2324 | ||
c9e38879 | 2325 | static char * |
2da42df6 | 2326 | make_imp_label (const char *prefix, const char *name) |
c9e38879 NC |
2327 | { |
2328 | int len; | |
2329 | char *copy; | |
2330 | ||
2331 | if (name[0] == '@') | |
2332 | { | |
2333 | len = strlen (prefix) + strlen (name); | |
2334 | copy = xmalloc (len + 1); | |
2335 | strcpy (copy, prefix); | |
2336 | strcat (copy, name); | |
2337 | } | |
2338 | else | |
2339 | { | |
2758961a | 2340 | len = strlen (ASM_PREFIX (name)) + strlen (prefix) + strlen (name); |
c9e38879 NC |
2341 | copy = xmalloc (len + 1); |
2342 | strcpy (copy, prefix); | |
2758961a | 2343 | strcat (copy, ASM_PREFIX (name)); |
c9e38879 NC |
2344 | strcat (copy, name); |
2345 | } | |
2346 | return copy; | |
2347 | } | |
2348 | ||
252b5132 | 2349 | static bfd * |
10e636d2 | 2350 | make_one_lib_file (export_type *exp, int i, int delay) |
252b5132 | 2351 | { |
84e43642 BE |
2352 | bfd * abfd; |
2353 | asymbol * exp_label; | |
2354 | asymbol * iname = 0; | |
2355 | asymbol * iname2; | |
2356 | asymbol * iname_lab; | |
2357 | asymbol ** iname_lab_pp; | |
2358 | asymbol ** iname_pp; | |
252b5132 RH |
2359 | #ifndef EXTRA |
2360 | #define EXTRA 0 | |
2361 | #endif | |
84e43642 BE |
2362 | asymbol * ptrs[NSECS + 4 + EXTRA + 1]; |
2363 | flagword applicable; | |
2364 | char * outname = xmalloc (strlen (TMP_STUB) + 10); | |
2365 | int oidx = 0; | |
252b5132 | 2366 | |
26044998 | 2367 | |
84e43642 | 2368 | sprintf (outname, "%s%05d.o", TMP_STUB, i); |
26044998 | 2369 | |
84e43642 | 2370 | abfd = bfd_openw (outname, HOW_BFD_WRITE_TARGET); |
26044998 | 2371 | |
84e43642 BE |
2372 | if (!abfd) |
2373 | /* xgettext:c-format */ | |
dec87289 NC |
2374 | fatal (_("bfd_open failed open stub file: %s: %s"), |
2375 | outname, bfd_get_errmsg ()); | |
252b5132 | 2376 | |
84e43642 BE |
2377 | /* xgettext:c-format */ |
2378 | inform (_("Creating stub file: %s"), outname); | |
26044998 | 2379 | |
84e43642 BE |
2380 | bfd_set_format (abfd, bfd_object); |
2381 | bfd_set_arch_mach (abfd, HOW_BFD_ARCH, 0); | |
252b5132 RH |
2382 | |
2383 | #ifdef DLLTOOL_ARM | |
84e43642 BE |
2384 | if (machine == MARM_INTERWORK || machine == MTHUMB) |
2385 | bfd_set_private_flags (abfd, F_INTERWORK); | |
252b5132 | 2386 | #endif |
26044998 | 2387 | |
84e43642 | 2388 | applicable = bfd_applicable_section_flags (abfd); |
26044998 | 2389 | |
84e43642 BE |
2390 | /* First make symbols for the sections. */ |
2391 | for (i = 0; i < NSECS; i++) | |
2392 | { | |
2393 | sinfo *si = secdata + i; | |
2394 | ||
2395 | if (si->id != i) | |
dec87289 | 2396 | abort (); |
84e43642 | 2397 | si->sec = bfd_make_section_old_way (abfd, si->name); |
fd361982 | 2398 | bfd_set_section_flags (si->sec, si->flags & applicable); |
84e43642 | 2399 | |
fd361982 | 2400 | bfd_set_section_alignment (si->sec, si->align); |
84e43642 BE |
2401 | si->sec->output_section = si->sec; |
2402 | si->sym = bfd_make_empty_symbol(abfd); | |
2403 | si->sym->name = si->sec->name; | |
2404 | si->sym->section = si->sec; | |
2405 | si->sym->flags = BSF_LOCAL; | |
2406 | si->sym->value = 0; | |
2407 | ptrs[oidx] = si->sym; | |
2408 | si->sympp = ptrs + oidx; | |
2409 | si->size = 0; | |
2410 | si->data = NULL; | |
2411 | ||
2412 | oidx++; | |
2413 | } | |
252b5132 | 2414 | |
84e43642 BE |
2415 | if (! exp->data) |
2416 | { | |
2417 | exp_label = bfd_make_empty_symbol (abfd); | |
2418 | exp_label->name = make_imp_label ("", exp->name); | |
fe49679d | 2419 | exp_label->section = secdata[TEXT].sec; |
84e43642 BE |
2420 | exp_label->flags = BSF_GLOBAL; |
2421 | exp_label->value = 0; | |
252b5132 RH |
2422 | |
2423 | #ifdef DLLTOOL_ARM | |
84e43642 BE |
2424 | if (machine == MTHUMB) |
2425 | bfd_coff_set_symbol_class (abfd, exp_label, C_THUMBEXTFUNC); | |
252b5132 | 2426 | #endif |
84e43642 BE |
2427 | ptrs[oidx++] = exp_label; |
2428 | } | |
252b5132 | 2429 | |
84e43642 BE |
2430 | /* Generate imp symbols with one underscore for Microsoft |
2431 | compatibility, and with two underscores for backward | |
2432 | compatibility with old versions of cygwin. */ | |
2433 | if (create_compat_implib) | |
2434 | { | |
2435 | iname = bfd_make_empty_symbol (abfd); | |
2436 | iname->name = make_imp_label ("___imp", exp->name); | |
2437 | iname->section = secdata[IDATA5].sec; | |
2438 | iname->flags = BSF_GLOBAL; | |
2439 | iname->value = 0; | |
2440 | } | |
252b5132 | 2441 | |
84e43642 BE |
2442 | iname2 = bfd_make_empty_symbol (abfd); |
2443 | iname2->name = make_imp_label ("__imp_", exp->name); | |
2444 | iname2->section = secdata[IDATA5].sec; | |
2445 | iname2->flags = BSF_GLOBAL; | |
2446 | iname2->value = 0; | |
252b5132 | 2447 | |
84e43642 | 2448 | iname_lab = bfd_make_empty_symbol (abfd); |
252b5132 | 2449 | |
84e43642 | 2450 | iname_lab->name = head_label; |
45dfa85a | 2451 | iname_lab->section = bfd_und_section_ptr; |
84e43642 BE |
2452 | iname_lab->flags = 0; |
2453 | iname_lab->value = 0; | |
252b5132 | 2454 | |
84e43642 BE |
2455 | iname_pp = ptrs + oidx; |
2456 | if (create_compat_implib) | |
2457 | ptrs[oidx++] = iname; | |
2458 | ptrs[oidx++] = iname2; | |
252b5132 | 2459 | |
84e43642 BE |
2460 | iname_lab_pp = ptrs + oidx; |
2461 | ptrs[oidx++] = iname_lab; | |
252b5132 | 2462 | |
84e43642 | 2463 | ptrs[oidx] = 0; |
252b5132 | 2464 | |
84e43642 BE |
2465 | for (i = 0; i < NSECS; i++) |
2466 | { | |
2467 | sinfo *si = secdata + i; | |
2468 | asection *sec = si->sec; | |
10e636d2 | 2469 | arelent *rel, *rel2 = 0, *rel3 = 0; |
84e43642 | 2470 | arelent **rpp; |
252b5132 | 2471 | |
84e43642 BE |
2472 | switch (i) |
2473 | { | |
2474 | case TEXT: | |
2475 | if (! exp->data) | |
252b5132 | 2476 | { |
84e43642 BE |
2477 | si->size = HOW_JTAB_SIZE; |
2478 | si->data = xmalloc (HOW_JTAB_SIZE); | |
2479 | memcpy (si->data, HOW_JTAB, HOW_JTAB_SIZE); | |
26044998 | 2480 | |
99ad8390 | 2481 | /* Add the reloc into idata$5. */ |
84e43642 | 2482 | rel = xmalloc (sizeof (arelent)); |
252b5132 | 2483 | |
10e636d2 | 2484 | rpp = xmalloc (sizeof (arelent *) * (delay ? 4 : 2)); |
84e43642 BE |
2485 | rpp[0] = rel; |
2486 | rpp[1] = 0; | |
252b5132 | 2487 | |
84e43642 BE |
2488 | rel->address = HOW_JTAB_ROFF; |
2489 | rel->addend = 0; | |
252b5132 | 2490 | |
10e636d2 DK |
2491 | if (delay) |
2492 | { | |
2493 | rel2 = xmalloc (sizeof (arelent)); | |
2494 | rpp[1] = rel2; | |
2495 | rel2->address = HOW_JTAB_ROFF2; | |
2496 | rel2->addend = 0; | |
2497 | rel3 = xmalloc (sizeof (arelent)); | |
2498 | rpp[2] = rel3; | |
2499 | rel3->address = HOW_JTAB_ROFF3; | |
2500 | rel3->addend = 0; | |
2501 | rpp[3] = 0; | |
2502 | } | |
2503 | ||
fe49679d | 2504 | if (machine == MX86) |
591a748a NC |
2505 | { |
2506 | rel->howto = bfd_reloc_type_lookup (abfd, | |
2507 | BFD_RELOC_32_PCREL); | |
2508 | rel->sym_ptr_ptr = iname_pp; | |
252b5132 RH |
2509 | } |
2510 | else | |
2511 | { | |
84e43642 BE |
2512 | rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32); |
2513 | rel->sym_ptr_ptr = secdata[IDATA5].sympp; | |
252b5132 | 2514 | } |
10e636d2 DK |
2515 | |
2516 | if (delay) | |
2517 | { | |
9a30f236 KT |
2518 | if (machine == MX86) |
2519 | rel2->howto = bfd_reloc_type_lookup (abfd, | |
2520 | BFD_RELOC_32_PCREL); | |
2521 | else | |
2522 | rel2->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32); | |
10e636d2 | 2523 | rel2->sym_ptr_ptr = rel->sym_ptr_ptr; |
9a30f236 KT |
2524 | rel3->howto = bfd_reloc_type_lookup (abfd, |
2525 | BFD_RELOC_32_PCREL); | |
10e636d2 DK |
2526 | rel3->sym_ptr_ptr = iname_lab_pp; |
2527 | } | |
2528 | ||
84e43642 | 2529 | sec->orelocation = rpp; |
10e636d2 | 2530 | sec->reloc_count = delay ? 3 : 1; |
84e43642 BE |
2531 | } |
2532 | break; | |
10e636d2 | 2533 | |
84e43642 | 2534 | case IDATA5: |
10e636d2 DK |
2535 | if (delay) |
2536 | { | |
9a30f236 KT |
2537 | si->size = create_for_pep ? 8 : 4; |
2538 | si->data = xmalloc (si->size); | |
10e636d2 DK |
2539 | sec->reloc_count = 1; |
2540 | memset (si->data, 0, si->size); | |
9a30f236 | 2541 | /* Point after jmp [__imp_...] instruction. */ |
10e636d2 DK |
2542 | si->data[0] = 6; |
2543 | rel = xmalloc (sizeof (arelent)); | |
2544 | rpp = xmalloc (sizeof (arelent *) * 2); | |
2545 | rpp[0] = rel; | |
2546 | rpp[1] = 0; | |
2547 | rel->address = 0; | |
2548 | rel->addend = 0; | |
9a30f236 KT |
2549 | if (create_for_pep) |
2550 | rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_64); | |
2551 | else | |
2552 | rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32); | |
10e636d2 DK |
2553 | rel->sym_ptr_ptr = secdata[TEXT].sympp; |
2554 | sec->orelocation = rpp; | |
2555 | break; | |
2556 | } | |
1a0670f3 AM |
2557 | /* Fall through. */ |
2558 | ||
10e636d2 | 2559 | case IDATA4: |
84e43642 BE |
2560 | /* An idata$4 or idata$5 is one word long, and has an |
2561 | rva to idata$6. */ | |
252b5132 | 2562 | |
2ea2f3c6 | 2563 | if (create_for_pep) |
84e43642 | 2564 | { |
2ea2f3c6 KT |
2565 | si->data = xmalloc (8); |
2566 | si->size = 8; | |
2567 | if (exp->noname) | |
2568 | { | |
2569 | si->data[0] = exp->ordinal ; | |
2570 | si->data[1] = exp->ordinal >> 8; | |
2571 | si->data[2] = exp->ordinal >> 16; | |
2572 | si->data[3] = exp->ordinal >> 24; | |
2573 | si->data[4] = 0; | |
2574 | si->data[5] = 0; | |
2575 | si->data[6] = 0; | |
2576 | si->data[7] = 0x80; | |
2577 | } | |
2578 | else | |
2579 | { | |
2580 | sec->reloc_count = 1; | |
2581 | memset (si->data, 0, si->size); | |
2582 | rel = xmalloc (sizeof (arelent)); | |
2583 | rpp = xmalloc (sizeof (arelent *) * 2); | |
2584 | rpp[0] = rel; | |
2585 | rpp[1] = 0; | |
2586 | rel->address = 0; | |
2587 | rel->addend = 0; | |
2588 | rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA); | |
2589 | rel->sym_ptr_ptr = secdata[IDATA6].sympp; | |
2590 | sec->orelocation = rpp; | |
2591 | } | |
84e43642 BE |
2592 | } |
2593 | else | |
2594 | { | |
2ea2f3c6 KT |
2595 | si->data = xmalloc (4); |
2596 | si->size = 4; | |
3aade688 | 2597 | |
2ea2f3c6 KT |
2598 | if (exp->noname) |
2599 | { | |
2600 | si->data[0] = exp->ordinal ; | |
2601 | si->data[1] = exp->ordinal >> 8; | |
2602 | si->data[2] = exp->ordinal >> 16; | |
2603 | si->data[3] = 0x80; | |
2604 | } | |
2605 | else | |
2606 | { | |
2607 | sec->reloc_count = 1; | |
2608 | memset (si->data, 0, si->size); | |
2609 | rel = xmalloc (sizeof (arelent)); | |
2610 | rpp = xmalloc (sizeof (arelent *) * 2); | |
2611 | rpp[0] = rel; | |
2612 | rpp[1] = 0; | |
2613 | rel->address = 0; | |
2614 | rel->addend = 0; | |
2615 | rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA); | |
2616 | rel->sym_ptr_ptr = secdata[IDATA6].sympp; | |
2617 | sec->orelocation = rpp; | |
2618 | } | |
84e43642 | 2619 | } |
84e43642 | 2620 | break; |
252b5132 | 2621 | |
84e43642 BE |
2622 | case IDATA6: |
2623 | if (!exp->noname) | |
2624 | { | |
35fd2dde R |
2625 | int idx = exp->ordinal; |
2626 | ||
bf201fdd KT |
2627 | if (exp->its_name) |
2628 | si->size = strlen (exp->its_name) + 3; | |
2629 | else | |
2630 | si->size = strlen (xlate (exp->import_name)) + 3; | |
84e43642 | 2631 | si->data = xmalloc (si->size); |
030f4c7f | 2632 | memset (si->data, 0, si->size); |
84e43642 BE |
2633 | si->data[0] = idx & 0xff; |
2634 | si->data[1] = idx >> 8; | |
bf201fdd KT |
2635 | if (exp->its_name) |
2636 | strcpy ((char *) si->data + 2, exp->its_name); | |
2637 | else | |
2638 | strcpy ((char *) si->data + 2, xlate (exp->import_name)); | |
84e43642 BE |
2639 | } |
2640 | break; | |
2641 | case IDATA7: | |
10e636d2 DK |
2642 | if (delay) |
2643 | break; | |
84e43642 BE |
2644 | si->size = 4; |
2645 | si->data = xmalloc (4); | |
2646 | memset (si->data, 0, si->size); | |
2647 | rel = xmalloc (sizeof (arelent)); | |
2648 | rpp = xmalloc (sizeof (arelent *) * 2); | |
2649 | rpp[0] = rel; | |
2650 | rel->address = 0; | |
2651 | rel->addend = 0; | |
2652 | rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA); | |
2653 | rel->sym_ptr_ptr = iname_lab_pp; | |
2654 | sec->orelocation = rpp; | |
2655 | sec->reloc_count = 1; | |
2656 | break; | |
252b5132 | 2657 | } |
84e43642 | 2658 | } |
252b5132 | 2659 | |
84e43642 BE |
2660 | { |
2661 | bfd_vma vma = 0; | |
2662 | /* Size up all the sections. */ | |
2663 | for (i = 0; i < NSECS; i++) | |
252b5132 | 2664 | { |
84e43642 | 2665 | sinfo *si = secdata + i; |
252b5132 | 2666 | |
fd361982 AM |
2667 | bfd_set_section_size (si->sec, si->size); |
2668 | bfd_set_section_vma (si->sec, vma); | |
252b5132 | 2669 | } |
84e43642 BE |
2670 | } |
2671 | /* Write them out. */ | |
2672 | for (i = 0; i < NSECS; i++) | |
2673 | { | |
2674 | sinfo *si = secdata + i; | |
252b5132 | 2675 | |
84e43642 BE |
2676 | if (i == IDATA5 && no_idata5) |
2677 | continue; | |
252b5132 | 2678 | |
84e43642 BE |
2679 | if (i == IDATA4 && no_idata4) |
2680 | continue; | |
252b5132 | 2681 | |
84e43642 BE |
2682 | bfd_set_section_contents (abfd, si->sec, |
2683 | si->data, 0, | |
2684 | si->size); | |
252b5132 | 2685 | } |
84e43642 BE |
2686 | |
2687 | bfd_set_symtab (abfd, ptrs, oidx); | |
2688 | bfd_close (abfd); | |
2689 | abfd = bfd_openr (outname, HOW_BFD_READ_TARGET); | |
dec87289 NC |
2690 | if (!abfd) |
2691 | /* xgettext:c-format */ | |
2692 | fatal (_("bfd_open failed reopen stub file: %s: %s"), | |
2693 | outname, bfd_get_errmsg ()); | |
3aade688 | 2694 | |
84e43642 | 2695 | return abfd; |
252b5132 RH |
2696 | } |
2697 | ||
2698 | static bfd * | |
2da42df6 | 2699 | make_head (void) |
252b5132 | 2700 | { |
bb0cb4db | 2701 | FILE *f = fopen (TMP_HEAD_S, FOPEN_WT); |
dec87289 | 2702 | bfd *abfd; |
252b5132 | 2703 | |
661016bb NC |
2704 | if (f == NULL) |
2705 | { | |
2706 | fatal (_("failed to open temporary head file: %s"), TMP_HEAD_S); | |
2707 | return NULL; | |
2708 | } | |
26044998 | 2709 | |
81fb971a NC |
2710 | temp_file_to_remove[TEMP_HEAD_FILE] = TMP_HEAD_S; |
2711 | ||
252b5132 | 2712 | fprintf (f, "%s IMAGE_IMPORT_DESCRIPTOR\n", ASM_C); |
10e636d2 | 2713 | fprintf (f, "\t.section\t.idata$2\n"); |
252b5132 | 2714 | |
10e636d2 | 2715 | fprintf (f,"\t%s\t%s\n", ASM_GLOBAL, head_label); |
252b5132 RH |
2716 | |
2717 | fprintf (f, "%s:\n", head_label); | |
2718 | ||
2719 | fprintf (f, "\t%shname%s\t%sPtr to image import by name list\n", | |
2720 | ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C); | |
2721 | ||
2722 | fprintf (f, "\t%sthis should be the timestamp, but NT sometimes\n", ASM_C); | |
2723 | fprintf (f, "\t%sdoesn't load DLLs when this is set.\n", ASM_C); | |
2724 | fprintf (f, "\t%s\t0\t%s loaded time\n", ASM_LONG, ASM_C); | |
2725 | fprintf (f, "\t%s\t0\t%s Forwarder chain\n", ASM_LONG, ASM_C); | |
2726 | fprintf (f, "\t%s__%s_iname%s\t%s imported dll's name\n", | |
2727 | ASM_RVA_BEFORE, | |
2728 | imp_name_lab, | |
2729 | ASM_RVA_AFTER, | |
2730 | ASM_C); | |
2731 | fprintf (f, "\t%sfthunk%s\t%s pointer to firstthunk\n", | |
2732 | ASM_RVA_BEFORE, | |
2733 | ASM_RVA_AFTER, ASM_C); | |
2734 | ||
2735 | fprintf (f, "%sStuff for compatibility\n", ASM_C); | |
2736 | ||
2737 | if (!no_idata5) | |
2738 | { | |
2739 | fprintf (f, "\t.section\t.idata$5\n"); | |
e77b97d4 KT |
2740 | if (use_nul_prefixed_import_tables) |
2741 | { | |
2ea2f3c6 KT |
2742 | if (create_for_pep) |
2743 | fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG); | |
2744 | else | |
2745 | fprintf (f,"\t%s\t0\n", ASM_LONG); | |
e77b97d4 | 2746 | } |
252b5132 RH |
2747 | fprintf (f, "fthunk:\n"); |
2748 | } | |
26044998 | 2749 | |
252b5132 RH |
2750 | if (!no_idata4) |
2751 | { | |
2752 | fprintf (f, "\t.section\t.idata$4\n"); | |
e77b97d4 KT |
2753 | if (use_nul_prefixed_import_tables) |
2754 | { | |
2ea2f3c6 KT |
2755 | if (create_for_pep) |
2756 | fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG); | |
2757 | else | |
2758 | fprintf (f,"\t%s\t0\n", ASM_LONG); | |
e77b97d4 | 2759 | } |
252b5132 RH |
2760 | fprintf (f, "hname:\n"); |
2761 | } | |
26044998 | 2762 | |
252b5132 RH |
2763 | fclose (f); |
2764 | ||
49c24507 | 2765 | assemble_file (TMP_HEAD_S, TMP_HEAD_O); |
252b5132 | 2766 | |
dec87289 NC |
2767 | abfd = bfd_openr (TMP_HEAD_O, HOW_BFD_READ_TARGET); |
2768 | if (abfd == NULL) | |
2769 | /* xgettext:c-format */ | |
2770 | fatal (_("failed to open temporary head file: %s: %s"), | |
2771 | TMP_HEAD_O, bfd_get_errmsg ()); | |
2772 | ||
81fb971a | 2773 | temp_file_to_remove[TEMP_HEAD_O_FILE] = TMP_HEAD_O; |
dec87289 | 2774 | return abfd; |
252b5132 RH |
2775 | } |
2776 | ||
10e636d2 DK |
2777 | bfd * |
2778 | make_delay_head (void) | |
2779 | { | |
2780 | FILE *f = fopen (TMP_HEAD_S, FOPEN_WT); | |
dec87289 | 2781 | bfd *abfd; |
10e636d2 DK |
2782 | |
2783 | if (f == NULL) | |
2784 | { | |
2785 | fatal (_("failed to open temporary head file: %s"), TMP_HEAD_S); | |
2786 | return NULL; | |
2787 | } | |
2788 | ||
81fb971a NC |
2789 | temp_file_to_remove[TEMP_HEAD_FILE] = TMP_HEAD_S; |
2790 | ||
10e636d2 DK |
2791 | /* Output the __tailMerge__xxx function */ |
2792 | fprintf (f, "%s Import trampoline\n", ASM_C); | |
2793 | fprintf (f, "\t.section\t.text\n"); | |
2794 | fprintf(f,"\t%s\t%s\n", ASM_GLOBAL, head_label); | |
2ce40d1a ZF |
2795 | if (HOW_SEH) |
2796 | fprintf (f, "\t.seh_proc\t%s\n", head_label); | |
10e636d2 DK |
2797 | fprintf (f, "%s:\n", head_label); |
2798 | fprintf (f, mtable[machine].trampoline, imp_name_lab); | |
2ce40d1a ZF |
2799 | if (HOW_SEH) |
2800 | fprintf (f, "\t.seh_endproc\n"); | |
10e636d2 DK |
2801 | |
2802 | /* Output the delay import descriptor */ | |
2803 | fprintf (f, "\n%s DELAY_IMPORT_DESCRIPTOR\n", ASM_C); | |
2804 | fprintf (f, ".section\t.text$2\n"); | |
2805 | fprintf (f,"%s __DELAY_IMPORT_DESCRIPTOR_%s\n", ASM_GLOBAL,imp_name_lab); | |
2806 | fprintf (f, "__DELAY_IMPORT_DESCRIPTOR_%s:\n", imp_name_lab); | |
2807 | fprintf (f, "\t%s 1\t%s grAttrs\n", ASM_LONG, ASM_C); | |
2808 | fprintf (f, "\t%s__%s_iname%s\t%s rvaDLLName\n", | |
2809 | ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C); | |
2810 | fprintf (f, "\t%s__DLL_HANDLE_%s%s\t%s rvaHmod\n", | |
2811 | ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C); | |
2812 | fprintf (f, "\t%s__IAT_%s%s\t%s rvaIAT\n", | |
2813 | ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C); | |
2814 | fprintf (f, "\t%s__INT_%s%s\t%s rvaINT\n", | |
2815 | ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C); | |
2816 | fprintf (f, "\t%s\t0\t%s rvaBoundIAT\n", ASM_LONG, ASM_C); | |
2817 | fprintf (f, "\t%s\t0\t%s rvaUnloadIAT\n", ASM_LONG, ASM_C); | |
2818 | fprintf (f, "\t%s\t0\t%s dwTimeStamp\n", ASM_LONG, ASM_C); | |
2819 | ||
2820 | /* Output the dll_handle */ | |
2821 | fprintf (f, "\n.section .data\n"); | |
2822 | fprintf (f, "__DLL_HANDLE_%s:\n", imp_name_lab); | |
2823 | fprintf (f, "\t%s\t0\t%s Handle\n", ASM_LONG, ASM_C); | |
9a30f236 KT |
2824 | if (create_for_pep) |
2825 | fprintf (f, "\t%s\t0\n", ASM_LONG); | |
10e636d2 DK |
2826 | fprintf (f, "\n"); |
2827 | ||
2828 | fprintf (f, "%sStuff for compatibility\n", ASM_C); | |
2829 | ||
2830 | if (!no_idata5) | |
2831 | { | |
bf201fdd | 2832 | fprintf (f, "\t.section\t.idata$5\n"); |
10e636d2 | 2833 | /* NULL terminating list. */ |
9a30f236 KT |
2834 | if (create_for_pep) |
2835 | fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG); | |
2836 | else | |
2837 | fprintf (f,"\t%s\t0\n", ASM_LONG); | |
10e636d2 DK |
2838 | fprintf (f, "__IAT_%s:\n", imp_name_lab); |
2839 | } | |
2840 | ||
2841 | if (!no_idata4) | |
2842 | { | |
2843 | fprintf (f, "\t.section\t.idata$4\n"); | |
2844 | fprintf (f, "\t%s\t0\n", ASM_LONG); | |
9a30f236 KT |
2845 | if (create_for_pep) |
2846 | fprintf (f, "\t%s\t0\n", ASM_LONG); | |
10e636d2 DK |
2847 | fprintf (f, "\t.section\t.idata$4\n"); |
2848 | fprintf (f, "__INT_%s:\n", imp_name_lab); | |
2849 | } | |
2850 | ||
2851 | fprintf (f, "\t.section\t.idata$2\n"); | |
2852 | ||
2853 | fclose (f); | |
2854 | ||
2855 | assemble_file (TMP_HEAD_S, TMP_HEAD_O); | |
2856 | ||
dec87289 NC |
2857 | abfd = bfd_openr (TMP_HEAD_O, HOW_BFD_READ_TARGET); |
2858 | if (abfd == NULL) | |
2859 | /* xgettext:c-format */ | |
2860 | fatal (_("failed to open temporary head file: %s: %s"), | |
2861 | TMP_HEAD_O, bfd_get_errmsg ()); | |
2862 | ||
81fb971a | 2863 | temp_file_to_remove[TEMP_HEAD_O_FILE] = TMP_HEAD_O; |
dec87289 | 2864 | return abfd; |
10e636d2 DK |
2865 | } |
2866 | ||
252b5132 | 2867 | static bfd * |
2da42df6 | 2868 | make_tail (void) |
252b5132 | 2869 | { |
bb0cb4db | 2870 | FILE *f = fopen (TMP_TAIL_S, FOPEN_WT); |
dec87289 | 2871 | bfd *abfd; |
252b5132 | 2872 | |
661016bb NC |
2873 | if (f == NULL) |
2874 | { | |
2875 | fatal (_("failed to open temporary tail file: %s"), TMP_TAIL_S); | |
2876 | return NULL; | |
2877 | } | |
26044998 | 2878 | |
81fb971a NC |
2879 | temp_file_to_remove[TEMP_TAIL_FILE] = TMP_TAIL_S; |
2880 | ||
252b5132 RH |
2881 | if (!no_idata4) |
2882 | { | |
10e636d2 | 2883 | fprintf (f, "\t.section\t.idata$4\n"); |
2ea2f3c6 KT |
2884 | if (create_for_pep) |
2885 | fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG); | |
2886 | else | |
2887 | fprintf (f,"\t%s\t0\n", ASM_LONG); /* NULL terminating list. */ | |
252b5132 | 2888 | } |
26044998 | 2889 | |
252b5132 RH |
2890 | if (!no_idata5) |
2891 | { | |
10e636d2 | 2892 | fprintf (f, "\t.section\t.idata$5\n"); |
2ea2f3c6 KT |
2893 | if (create_for_pep) |
2894 | fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG); | |
2895 | else | |
2896 | fprintf (f,"\t%s\t0\n", ASM_LONG); /* NULL terminating list. */ | |
252b5132 RH |
2897 | } |
2898 | ||
10e636d2 | 2899 | fprintf (f, "\t.section\t.idata$7\n"); |
252b5132 RH |
2900 | fprintf (f, "\t%s\t__%s_iname\n", ASM_GLOBAL, imp_name_lab); |
2901 | fprintf (f, "__%s_iname:\t%s\t\"%s\"\n", | |
2902 | imp_name_lab, ASM_TEXT, dll_name); | |
2903 | ||
2904 | fclose (f); | |
2905 | ||
49c24507 | 2906 | assemble_file (TMP_TAIL_S, TMP_TAIL_O); |
26044998 | 2907 | |
dec87289 NC |
2908 | abfd = bfd_openr (TMP_TAIL_O, HOW_BFD_READ_TARGET); |
2909 | if (abfd == NULL) | |
2910 | /* xgettext:c-format */ | |
2911 | fatal (_("failed to open temporary tail file: %s: %s"), | |
2912 | TMP_TAIL_O, bfd_get_errmsg ()); | |
2913 | ||
81fb971a | 2914 | temp_file_to_remove[TEMP_TAIL_O_FILE] = TMP_TAIL_O; |
dec87289 | 2915 | return abfd; |
252b5132 RH |
2916 | } |
2917 | ||
2918 | static void | |
10e636d2 | 2919 | gen_lib_file (int delay) |
252b5132 RH |
2920 | { |
2921 | int i; | |
2922 | export_type *exp; | |
2923 | bfd *ar_head; | |
2924 | bfd *ar_tail; | |
2925 | bfd *outarch; | |
2926 | bfd * head = 0; | |
2927 | ||
2928 | unlink (imp_name); | |
2929 | ||
49c24507 | 2930 | outarch = bfd_openw (imp_name, HOW_BFD_WRITE_TARGET); |
252b5132 RH |
2931 | |
2932 | if (!outarch) | |
2933 | /* xgettext:c-format */ | |
dec87289 NC |
2934 | fatal (_("Can't create .lib file: %s: %s"), |
2935 | imp_name, bfd_get_errmsg ()); | |
252b5132 RH |
2936 | |
2937 | /* xgettext:c-format */ | |
37cc8ec1 | 2938 | inform (_("Creating library file: %s"), imp_name); |
26044998 | 2939 | |
81fb971a | 2940 | xatexit (unlink_temp_files); |
3aade688 | 2941 | |
252b5132 RH |
2942 | bfd_set_format (outarch, bfd_archive); |
2943 | outarch->has_armap = 1; | |
a8da6403 | 2944 | outarch->is_thin_archive = 0; |
252b5132 | 2945 | |
25ee24d9 NC |
2946 | if (deterministic) |
2947 | outarch->flags |= BFD_DETERMINISTIC_OUTPUT; | |
2948 | ||
26044998 | 2949 | /* Work out a reasonable size of things to put onto one line. */ |
10e636d2 DK |
2950 | if (delay) |
2951 | { | |
2952 | ar_head = make_delay_head (); | |
2953 | } | |
2954 | else | |
2955 | { | |
2956 | ar_head = make_head (); | |
2957 | } | |
252b5132 RH |
2958 | ar_tail = make_tail(); |
2959 | ||
2960 | if (ar_head == NULL || ar_tail == NULL) | |
2961 | return; | |
26044998 | 2962 | |
252b5132 RH |
2963 | for (i = 0; (exp = d_exports_lexically[i]); i++) |
2964 | { | |
7aa52b1f NC |
2965 | bfd *n; |
2966 | /* Don't add PRIVATE entries to import lib. */ | |
2967 | if (exp->private) | |
2968 | continue; | |
10e636d2 | 2969 | n = make_one_lib_file (exp, i, delay); |
cc481421 | 2970 | n->archive_next = head; |
252b5132 | 2971 | head = n; |
0fd555c4 NC |
2972 | if (ext_prefix_alias) |
2973 | { | |
2974 | export_type alias_exp; | |
2975 | ||
2976 | assert (i < PREFIX_ALIAS_BASE); | |
2977 | alias_exp.name = make_imp_label (ext_prefix_alias, exp->name); | |
2978 | alias_exp.internal_name = exp->internal_name; | |
bf201fdd | 2979 | alias_exp.its_name = exp->its_name; |
0fd555c4 NC |
2980 | alias_exp.import_name = exp->name; |
2981 | alias_exp.ordinal = exp->ordinal; | |
2982 | alias_exp.constant = exp->constant; | |
2983 | alias_exp.noname = exp->noname; | |
2984 | alias_exp.private = exp->private; | |
2985 | alias_exp.data = exp->data; | |
0fd555c4 NC |
2986 | alias_exp.forward = exp->forward; |
2987 | alias_exp.next = exp->next; | |
10e636d2 | 2988 | n = make_one_lib_file (&alias_exp, i + PREFIX_ALIAS_BASE, delay); |
cc481421 | 2989 | n->archive_next = head; |
0fd555c4 NC |
2990 | head = n; |
2991 | } | |
252b5132 RH |
2992 | } |
2993 | ||
c9e38879 | 2994 | /* Now stick them all into the archive. */ |
cc481421 AM |
2995 | ar_head->archive_next = head; |
2996 | ar_tail->archive_next = ar_head; | |
252b5132 RH |
2997 | head = ar_tail; |
2998 | ||
2999 | if (! bfd_set_archive_head (outarch, head)) | |
3000 | bfd_fatal ("bfd_set_archive_head"); | |
26044998 | 3001 | |
252b5132 RH |
3002 | if (! bfd_close (outarch)) |
3003 | bfd_fatal (imp_name); | |
3004 | ||
3005 | while (head != NULL) | |
3006 | { | |
cc481421 | 3007 | bfd *n = head->archive_next; |
252b5132 RH |
3008 | bfd_close (head); |
3009 | head = n; | |
3010 | } | |
3011 | ||
c9e38879 | 3012 | /* Delete all the temp files. */ |
81fb971a | 3013 | unlink_temp_files (); |
252b5132 RH |
3014 | |
3015 | if (dontdeltemps < 2) | |
3016 | { | |
bb0cb4db ILT |
3017 | char *name; |
3018 | ||
e1fa0163 | 3019 | name = xmalloc (strlen (TMP_STUB) + 10); |
7aa52b1f | 3020 | for (i = 0; (exp = d_exports_lexically[i]); i++) |
252b5132 | 3021 | { |
7aa52b1f NC |
3022 | /* Don't delete non-existent stubs for PRIVATE entries. */ |
3023 | if (exp->private) | |
3024 | continue; | |
bb0cb4db ILT |
3025 | sprintf (name, "%s%05d.o", TMP_STUB, i); |
3026 | if (unlink (name) < 0) | |
252b5132 | 3027 | /* xgettext:c-format */ |
37cc8ec1 | 3028 | non_fatal (_("cannot delete %s: %s"), name, strerror (errno)); |
0fd555c4 NC |
3029 | if (ext_prefix_alias) |
3030 | { | |
3031 | sprintf (name, "%s%05d.o", TMP_STUB, i + PREFIX_ALIAS_BASE); | |
3032 | if (unlink (name) < 0) | |
3033 | /* xgettext:c-format */ | |
3034 | non_fatal (_("cannot delete %s: %s"), name, strerror (errno)); | |
3035 | } | |
252b5132 | 3036 | } |
e1fa0163 | 3037 | free (name); |
252b5132 | 3038 | } |
26044998 | 3039 | |
252b5132 RH |
3040 | inform (_("Created lib file")); |
3041 | } | |
3042 | ||
25893672 | 3043 | /* Append a copy of data (cast to char *) to list. */ |
71c57c16 NC |
3044 | |
3045 | static void | |
25893672 | 3046 | dll_name_list_append (dll_name_list_type * list, bfd_byte * data) |
71c57c16 | 3047 | { |
dabf2221 AM |
3048 | dll_name_list_node_type * entry; |
3049 | ||
25893672 NC |
3050 | /* Error checking. */ |
3051 | if (! list || ! list->tail) | |
3052 | return; | |
3053 | ||
71c57c16 | 3054 | /* Allocate new node. */ |
dabf2221 AM |
3055 | entry = ((dll_name_list_node_type *) |
3056 | xmalloc (sizeof (dll_name_list_node_type))); | |
71c57c16 NC |
3057 | |
3058 | /* Initialize its values. */ | |
3059 | entry->dllname = xstrdup ((char *) data); | |
3060 | entry->next = NULL; | |
3061 | ||
3062 | /* Add to tail, and move tail. */ | |
25893672 NC |
3063 | list->tail->next = entry; |
3064 | list->tail = entry; | |
71c57c16 NC |
3065 | } |
3066 | ||
25893672 NC |
3067 | /* Count the number of entries in list. */ |
3068 | ||
3aade688 | 3069 | static int |
25893672 | 3070 | dll_name_list_count (dll_name_list_type * list) |
71c57c16 | 3071 | { |
dabf2221 AM |
3072 | dll_name_list_node_type * p; |
3073 | int count = 0; | |
3074 | ||
25893672 NC |
3075 | /* Error checking. */ |
3076 | if (! list || ! list->head) | |
3077 | return 0; | |
3078 | ||
dabf2221 | 3079 | p = list->head; |
71c57c16 NC |
3080 | |
3081 | while (p && p->next) | |
3082 | { | |
3083 | count++; | |
3084 | p = p->next; | |
3085 | } | |
3086 | return count; | |
3087 | } | |
3088 | ||
25893672 NC |
3089 | /* Print each entry in list to stdout. */ |
3090 | ||
3aade688 | 3091 | static void |
25893672 | 3092 | dll_name_list_print (dll_name_list_type * list) |
71c57c16 | 3093 | { |
dabf2221 AM |
3094 | dll_name_list_node_type * p; |
3095 | ||
25893672 NC |
3096 | /* Error checking. */ |
3097 | if (! list || ! list->head) | |
3098 | return; | |
3099 | ||
dabf2221 | 3100 | p = list->head; |
71c57c16 NC |
3101 | |
3102 | while (p && p->next && p->next->dllname && *(p->next->dllname)) | |
3103 | { | |
3104 | printf ("%s\n", p->next->dllname); | |
3105 | p = p->next; | |
3106 | } | |
3107 | } | |
3108 | ||
25893672 NC |
3109 | /* Free all entries in list, and list itself. */ |
3110 | ||
3111 | static void | |
3112 | dll_name_list_free (dll_name_list_type * list) | |
3113 | { | |
3114 | if (list) | |
3115 | { | |
3116 | dll_name_list_free_contents (list->head); | |
3117 | list->head = NULL; | |
3118 | list->tail = NULL; | |
3119 | free (list); | |
3120 | } | |
3121 | } | |
3122 | ||
3123 | /* Recursive function to free all nodes entry->next->next... | |
3124 | as well as entry itself. */ | |
3125 | ||
3aade688 | 3126 | static void |
25893672 | 3127 | dll_name_list_free_contents (dll_name_list_node_type * entry) |
71c57c16 NC |
3128 | { |
3129 | if (entry) | |
3130 | { | |
3131 | if (entry->next) | |
9db70fc3 AM |
3132 | dll_name_list_free_contents (entry->next); |
3133 | free (entry->dllname); | |
71c57c16 NC |
3134 | free (entry); |
3135 | } | |
3136 | } | |
3137 | ||
25893672 NC |
3138 | /* Allocate and initialize a dll_name_list_type object, |
3139 | including its sentinel node. Caller is responsible | |
3aade688 | 3140 | for calling dll_name_list_free when finished with |
25893672 NC |
3141 | the list. */ |
3142 | ||
3143 | static dll_name_list_type * | |
3144 | dll_name_list_create (void) | |
3145 | { | |
3146 | /* Allocate list. */ | |
3147 | dll_name_list_type * list = xmalloc (sizeof (dll_name_list_type)); | |
3148 | ||
3149 | /* Allocate and initialize sentinel node. */ | |
3150 | list->head = xmalloc (sizeof (dll_name_list_node_type)); | |
3151 | list->head->dllname = NULL; | |
3152 | list->head->next = NULL; | |
3153 | ||
3154 | /* Bookkeeping for empty list. */ | |
3155 | list->tail = list->head; | |
3156 | ||
3157 | return list; | |
3158 | } | |
3159 | ||
71c57c16 NC |
3160 | /* Search the symbol table of the suppled BFD for a symbol whose name matches |
3161 | OBJ (where obj is cast to const char *). If found, set global variable | |
3162 | identify_member_contains_symname_result TRUE. It is the caller's | |
3163 | responsibility to set the result variable FALSE before iterating with | |
3aade688 | 3164 | this function. */ |
71c57c16 | 3165 | |
3aade688 | 3166 | static void |
71c57c16 NC |
3167 | identify_member_contains_symname (bfd * abfd, |
3168 | bfd * archive_bfd ATTRIBUTE_UNUSED, | |
3169 | void * obj) | |
3170 | { | |
3171 | long storage_needed; | |
3172 | asymbol ** symbol_table; | |
3173 | long number_of_symbols; | |
3174 | long i; | |
25893672 | 3175 | symname_search_data_type * search_data = (symname_search_data_type *) obj; |
71c57c16 NC |
3176 | |
3177 | /* If we already found the symbol in a different member, | |
3178 | short circuit. */ | |
25893672 | 3179 | if (search_data->found) |
71c57c16 NC |
3180 | return; |
3181 | ||
3182 | storage_needed = bfd_get_symtab_upper_bound (abfd); | |
3183 | if (storage_needed <= 0) | |
3184 | return; | |
3185 | ||
3186 | symbol_table = xmalloc (storage_needed); | |
3187 | number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); | |
3188 | if (number_of_symbols < 0) | |
3189 | { | |
3190 | free (symbol_table); | |
3191 | return; | |
3192 | } | |
3193 | ||
3194 | for (i = 0; i < number_of_symbols; i++) | |
3195 | { | |
25893672 NC |
3196 | if (strncmp (symbol_table[i]->name, |
3197 | search_data->symname, | |
3198 | strlen (search_data->symname)) == 0) | |
71c57c16 | 3199 | { |
015dc7e1 | 3200 | search_data->found = true; |
71c57c16 NC |
3201 | break; |
3202 | } | |
3203 | } | |
3204 | free (symbol_table); | |
3205 | } | |
3206 | ||
3207 | /* This is the main implementation for the --identify option. | |
fe49679d AM |
3208 | Given the name of an import library in identify_imp_name, first |
3209 | determine if the import library is a GNU binutils-style one (where | |
3210 | the DLL name is stored in an .idata$7 section), or if it is a | |
3211 | MS-style one (where the DLL name, along with much other data, is | |
3212 | stored in the .idata$6 section). We determine the style of import | |
3213 | library by searching for the DLL-structure symbol inserted by MS | |
3214 | tools: __NULL_IMPORT_DESCRIPTOR. | |
71c57c16 NC |
3215 | |
3216 | Once we know which section to search, evaluate each section for the | |
3217 | appropriate properties that indicate it may contain the name of the | |
3218 | associated DLL (this differs depending on the style). Add the contents | |
3219 | of all sections which meet the criteria to a linked list of dll names. | |
d4732f7c | 3220 | |
71c57c16 | 3221 | Finally, print them all to stdout. (If --identify-strict, an error is |
3aade688 | 3222 | reported if more than one match was found). */ |
d4732f7c | 3223 | |
3aade688 | 3224 | static void |
d4732f7c CW |
3225 | identify_dll_for_implib (void) |
3226 | { | |
71c57c16 NC |
3227 | bfd * abfd = NULL; |
3228 | int count = 0; | |
25893672 NC |
3229 | identify_data_type identify_data; |
3230 | symname_search_data_type search_data; | |
71c57c16 | 3231 | |
25893672 NC |
3232 | /* Initialize identify_data. */ |
3233 | identify_data.list = dll_name_list_create (); | |
015dc7e1 | 3234 | identify_data.ms_style_implib = false; |
25893672 NC |
3235 | |
3236 | /* Initialize search_data. */ | |
3237 | search_data.symname = "__NULL_IMPORT_DESCRIPTOR"; | |
015dc7e1 | 3238 | search_data.found = false; |
d4732f7c | 3239 | |
bf2dd8d7 AM |
3240 | if (bfd_init () != BFD_INIT_MAGIC) |
3241 | fatal (_("fatal error: libbfd ABI mismatch")); | |
d4732f7c CW |
3242 | |
3243 | abfd = bfd_openr (identify_imp_name, 0); | |
3244 | if (abfd == NULL) | |
dec87289 NC |
3245 | /* xgettext:c-format */ |
3246 | fatal (_("Can't open .lib file: %s: %s"), | |
3247 | identify_imp_name, bfd_get_errmsg ()); | |
71c57c16 NC |
3248 | |
3249 | if (! bfd_check_format (abfd, bfd_archive)) | |
d4732f7c | 3250 | { |
71c57c16 NC |
3251 | if (! bfd_close (abfd)) |
3252 | bfd_fatal (identify_imp_name); | |
3253 | ||
3254 | fatal (_("%s is not a library"), identify_imp_name); | |
d4732f7c | 3255 | } |
71c57c16 NC |
3256 | |
3257 | /* Detect if this a Microsoft import library. */ | |
25893672 NC |
3258 | identify_search_archive (abfd, |
3259 | identify_member_contains_symname, | |
3260 | (void *)(& search_data)); | |
3261 | if (search_data.found) | |
015dc7e1 | 3262 | identify_data.ms_style_implib = true; |
3aade688 | 3263 | |
71c57c16 NC |
3264 | /* Rewind the bfd. */ |
3265 | if (! bfd_close (abfd)) | |
3266 | bfd_fatal (identify_imp_name); | |
3267 | abfd = bfd_openr (identify_imp_name, 0); | |
3268 | if (abfd == NULL) | |
3269 | bfd_fatal (identify_imp_name); | |
3270 | ||
d4732f7c CW |
3271 | if (!bfd_check_format (abfd, bfd_archive)) |
3272 | { | |
3273 | if (!bfd_close (abfd)) | |
3274 | bfd_fatal (identify_imp_name); | |
3275 | ||
71c57c16 | 3276 | fatal (_("%s is not a library"), identify_imp_name); |
d4732f7c | 3277 | } |
3aade688 | 3278 | |
71c57c16 | 3279 | /* Now search for the dll name. */ |
25893672 NC |
3280 | identify_search_archive (abfd, |
3281 | identify_search_member, | |
3282 | (void *)(& identify_data)); | |
d4732f7c | 3283 | |
71c57c16 | 3284 | if (! bfd_close (abfd)) |
d4732f7c CW |
3285 | bfd_fatal (identify_imp_name); |
3286 | ||
25893672 | 3287 | count = dll_name_list_count (identify_data.list); |
71c57c16 | 3288 | if (count > 0) |
d4732f7c | 3289 | { |
71c57c16 NC |
3290 | if (identify_strict && count > 1) |
3291 | { | |
25893672 NC |
3292 | dll_name_list_free (identify_data.list); |
3293 | identify_data.list = NULL; | |
71c57c16 NC |
3294 | fatal (_("Import library `%s' specifies two or more dlls"), |
3295 | identify_imp_name); | |
3296 | } | |
25893672 NC |
3297 | dll_name_list_print (identify_data.list); |
3298 | dll_name_list_free (identify_data.list); | |
3299 | identify_data.list = NULL; | |
d4732f7c CW |
3300 | } |
3301 | else | |
3302 | { | |
25893672 NC |
3303 | dll_name_list_free (identify_data.list); |
3304 | identify_data.list = NULL; | |
71c57c16 NC |
3305 | fatal (_("Unable to determine dll name for `%s' (not an import library?)"), |
3306 | identify_imp_name); | |
d4732f7c CW |
3307 | } |
3308 | } | |
3309 | ||
71c57c16 NC |
3310 | /* Loop over all members of the archive, applying the supplied function to |
3311 | each member that is a bfd_object. The function will be called as if: | |
3aade688 | 3312 | func (member_bfd, abfd, user_storage) */ |
d4732f7c | 3313 | |
d4732f7c | 3314 | static void |
3aade688 | 3315 | identify_search_archive (bfd * abfd, |
71c57c16 NC |
3316 | void (* operation) (bfd *, bfd *, void *), |
3317 | void * user_storage) | |
d4732f7c | 3318 | { |
71c57c16 NC |
3319 | bfd * arfile = NULL; |
3320 | bfd * last_arfile = NULL; | |
3321 | char ** matching; | |
d4732f7c CW |
3322 | |
3323 | while (1) | |
3324 | { | |
3325 | arfile = bfd_openr_next_archived_file (abfd, arfile); | |
3326 | ||
3327 | if (arfile == NULL) | |
3328 | { | |
3329 | if (bfd_get_error () != bfd_error_no_more_archived_files) | |
3330 | bfd_fatal (bfd_get_filename (abfd)); | |
3331 | break; | |
3332 | } | |
71c57c16 | 3333 | |
d4732f7c | 3334 | if (bfd_check_format_matches (arfile, bfd_object, &matching)) |
71c57c16 | 3335 | (*operation) (arfile, abfd, user_storage); |
d4732f7c CW |
3336 | else |
3337 | { | |
3338 | bfd_nonfatal (bfd_get_filename (arfile)); | |
3339 | free (matching); | |
3340 | } | |
71c57c16 | 3341 | |
d4732f7c | 3342 | if (last_arfile != NULL) |
37e3922e NC |
3343 | { |
3344 | bfd_close (last_arfile); | |
3345 | /* PR 17512: file: 8b2168d4. */ | |
3346 | if (last_arfile == arfile) | |
3347 | { | |
3348 | last_arfile = NULL; | |
3349 | break; | |
3350 | } | |
3351 | } | |
71c57c16 | 3352 | |
d4732f7c CW |
3353 | last_arfile = arfile; |
3354 | } | |
3355 | ||
3356 | if (last_arfile != NULL) | |
3357 | { | |
3358 | bfd_close (last_arfile); | |
3359 | } | |
3360 | } | |
3361 | ||
71c57c16 | 3362 | /* Call the identify_search_section() function for each section of this |
3aade688 | 3363 | archive member. */ |
d4732f7c | 3364 | |
d4732f7c | 3365 | static void |
71c57c16 NC |
3366 | identify_search_member (bfd *abfd, |
3367 | bfd *archive_bfd ATTRIBUTE_UNUSED, | |
3368 | void *obj) | |
d4732f7c | 3369 | { |
71c57c16 | 3370 | bfd_map_over_sections (abfd, identify_search_section, obj); |
d4732f7c CW |
3371 | } |
3372 | ||
71c57c16 | 3373 | /* This predicate returns true if section->name matches the desired value. |
fe49679d AM |
3374 | By default, this is .idata$7 (.idata$6 if the import library is |
3375 | ms-style). */ | |
d4732f7c | 3376 | |
015dc7e1 AM |
3377 | static bool |
3378 | identify_process_section_p (asection * section, bool ms_style_implib) | |
d4732f7c | 3379 | { |
fe49679d | 3380 | static const char * SECTION_NAME = ".idata$7"; |
71c57c16 | 3381 | static const char * MS_SECTION_NAME = ".idata$6"; |
3aade688 | 3382 | |
71c57c16 | 3383 | const char * section_name = |
25893672 | 3384 | (ms_style_implib ? MS_SECTION_NAME : SECTION_NAME); |
3aade688 | 3385 | |
71c57c16 | 3386 | if (strcmp (section_name, section->name) == 0) |
015dc7e1 AM |
3387 | return true; |
3388 | return false; | |
d4732f7c CW |
3389 | } |
3390 | ||
fe49679d | 3391 | /* If *section has contents and its name is .idata$7 (.idata$6 if |
71c57c16 | 3392 | import lib ms-generated) -- and it satisfies several other constraints |
25893672 | 3393 | -- then add the contents of the section to obj->list. */ |
d4732f7c | 3394 | |
d4732f7c | 3395 | static void |
25893672 | 3396 | identify_search_section (bfd * abfd, asection * section, void * obj) |
d4732f7c CW |
3397 | { |
3398 | bfd_byte *data = 0; | |
3399 | bfd_size_type datasize; | |
25893672 | 3400 | identify_data_type * identify_data = (identify_data_type *)obj; |
015dc7e1 | 3401 | bool ms_style = identify_data->ms_style_implib; |
d4732f7c CW |
3402 | |
3403 | if ((section->flags & SEC_HAS_CONTENTS) == 0) | |
3404 | return; | |
3405 | ||
25893672 | 3406 | if (! identify_process_section_p (section, ms_style)) |
d4732f7c CW |
3407 | return; |
3408 | ||
71c57c16 NC |
3409 | /* Binutils import libs seem distinguish the .idata$7 section that contains |
3410 | the DLL name from other .idata$7 sections by the absence of the | |
3411 | SEC_RELOC flag. */ | |
25893672 | 3412 | if (!ms_style && ((section->flags & SEC_RELOC) == SEC_RELOC)) |
71c57c16 NC |
3413 | return; |
3414 | ||
3415 | /* MS import libs seem to distinguish the .idata$6 section | |
3416 | that contains the DLL name from other .idata$6 sections | |
3417 | by the presence of the SEC_DATA flag. */ | |
25893672 | 3418 | if (ms_style && ((section->flags & SEC_DATA) == 0)) |
71c57c16 NC |
3419 | return; |
3420 | ||
fd361982 | 3421 | if ((datasize = bfd_section_size (section)) == 0) |
d4732f7c CW |
3422 | return; |
3423 | ||
71c57c16 | 3424 | data = (bfd_byte *) xmalloc (datasize + 1); |
d4732f7c CW |
3425 | data[0] = '\0'; |
3426 | ||
3427 | bfd_get_section_contents (abfd, section, data, 0, datasize); | |
3428 | data[datasize] = '\0'; | |
3429 | ||
71c57c16 NC |
3430 | /* Use a heuristic to determine if data is a dll name. |
3431 | Possible to defeat this if (a) the library has MANY | |
3aade688 | 3432 | (more than 0x302f) imports, (b) it is an ms-style |
71c57c16 NC |
3433 | import library, but (c) it is buggy, in that the SEC_DATA |
3434 | flag is set on the "wrong" sections. This heuristic might | |
25893672 NC |
3435 | also fail to record a valid dll name if the dllname uses |
3436 | a multibyte or unicode character set (is that valid?). | |
71c57c16 NC |
3437 | |
3438 | This heuristic is based on the fact that symbols names in | |
3439 | the chosen section -- as opposed to the dll name -- begin | |
3440 | at offset 2 in the data. The first two bytes are a 16bit | |
3441 | little-endian count, and start at 0x0000. However, the dll | |
3442 | name begins at offset 0 in the data. We assume that the | |
3443 | dll name does not contain unprintable characters. */ | |
3444 | if (data[0] != '\0' && ISPRINT (data[0]) | |
3445 | && ((datasize < 2) || ISPRINT (data[1]))) | |
25893672 | 3446 | dll_name_list_append (identify_data->list, data); |
d4732f7c CW |
3447 | |
3448 | free (data); | |
3449 | } | |
3450 | ||
252b5132 | 3451 | /* Run through the information gathered from the .o files and the |
c9e38879 | 3452 | .def file and work out the best stuff. */ |
7aa52b1f | 3453 | |
252b5132 | 3454 | static int |
2da42df6 | 3455 | pfunc (const void *a, const void *b) |
252b5132 RH |
3456 | { |
3457 | export_type *ap = *(export_type **) a; | |
3458 | export_type *bp = *(export_type **) b; | |
71c57c16 | 3459 | |
252b5132 RH |
3460 | if (ap->ordinal == bp->ordinal) |
3461 | return 0; | |
3462 | ||
c9e38879 | 3463 | /* Unset ordinals go to the bottom. */ |
252b5132 RH |
3464 | if (ap->ordinal == -1) |
3465 | return 1; | |
3466 | if (bp->ordinal == -1) | |
3467 | return -1; | |
3468 | return (ap->ordinal - bp->ordinal); | |
3469 | } | |
3470 | ||
3471 | static int | |
2da42df6 | 3472 | nfunc (const void *a, const void *b) |
252b5132 RH |
3473 | { |
3474 | export_type *ap = *(export_type **) a; | |
3475 | export_type *bp = *(export_type **) b; | |
c6972290 NC |
3476 | const char *an = ap->name; |
3477 | const char *bn = bp->name; | |
bf201fdd KT |
3478 | if (ap->its_name) |
3479 | an = ap->its_name; | |
3480 | if (bp->its_name) | |
3481 | an = bp->its_name; | |
c6972290 NC |
3482 | if (killat) |
3483 | { | |
3484 | an = (an[0] == '@') ? an + 1 : an; | |
3485 | bn = (bn[0] == '@') ? bn + 1 : bn; | |
3486 | } | |
3487 | ||
3488 | return (strcmp (an, bn)); | |
252b5132 RH |
3489 | } |
3490 | ||
3491 | static void | |
2da42df6 | 3492 | remove_null_names (export_type **ptr) |
252b5132 RH |
3493 | { |
3494 | int src; | |
3495 | int dst; | |
c9e38879 | 3496 | |
252b5132 RH |
3497 | for (dst = src = 0; src < d_nfuncs; src++) |
3498 | { | |
3499 | if (ptr[src]) | |
3500 | { | |
3501 | ptr[dst] = ptr[src]; | |
3502 | dst++; | |
3503 | } | |
3504 | } | |
3505 | d_nfuncs = dst; | |
3506 | } | |
3507 | ||
252b5132 | 3508 | static void |
2da42df6 | 3509 | process_duplicates (export_type **d_export_vec) |
252b5132 RH |
3510 | { |
3511 | int more = 1; | |
3512 | int i; | |
c9e38879 | 3513 | |
252b5132 RH |
3514 | while (more) |
3515 | { | |
252b5132 | 3516 | more = 0; |
c9e38879 | 3517 | /* Remove duplicates. */ |
252b5132 RH |
3518 | qsort (d_export_vec, d_nfuncs, sizeof (export_type *), nfunc); |
3519 | ||
252b5132 RH |
3520 | for (i = 0; i < d_nfuncs - 1; i++) |
3521 | { | |
3522 | if (strcmp (d_export_vec[i]->name, | |
3523 | d_export_vec[i + 1]->name) == 0) | |
3524 | { | |
252b5132 RH |
3525 | export_type *a = d_export_vec[i]; |
3526 | export_type *b = d_export_vec[i + 1]; | |
3527 | ||
3528 | more = 1; | |
26044998 | 3529 | |
252b5132 | 3530 | /* xgettext:c-format */ |
37cc8ec1 | 3531 | inform (_("Warning, ignoring duplicate EXPORT %s %d,%d"), |
252b5132 | 3532 | a->name, a->ordinal, b->ordinal); |
26044998 | 3533 | |
252b5132 RH |
3534 | if (a->ordinal != -1 |
3535 | && b->ordinal != -1) | |
3536 | /* xgettext:c-format */ | |
ea6e992c | 3537 | fatal (_("Error, duplicate EXPORT with ordinals: %s"), |
252b5132 RH |
3538 | a->name); |
3539 | ||
c9e38879 | 3540 | /* Merge attributes. */ |
252b5132 RH |
3541 | b->ordinal = a->ordinal > 0 ? a->ordinal : b->ordinal; |
3542 | b->constant |= a->constant; | |
3543 | b->noname |= a->noname; | |
3544 | b->data |= a->data; | |
3545 | d_export_vec[i] = 0; | |
3546 | } | |
3547 | ||
252b5132 | 3548 | remove_null_names (d_export_vec); |
252b5132 RH |
3549 | } |
3550 | } | |
3551 | ||
c9e38879 | 3552 | /* Count the names. */ |
252b5132 | 3553 | for (i = 0; i < d_nfuncs; i++) |
7aa52b1f NC |
3554 | if (!d_export_vec[i]->noname) |
3555 | d_named_nfuncs++; | |
252b5132 RH |
3556 | } |
3557 | ||
3558 | static void | |
2da42df6 | 3559 | fill_ordinals (export_type **d_export_vec) |
252b5132 RH |
3560 | { |
3561 | int lowest = -1; | |
3562 | int i; | |
3563 | char *ptr; | |
3564 | int size = 65536; | |
3565 | ||
3566 | qsort (d_export_vec, d_nfuncs, sizeof (export_type *), pfunc); | |
3567 | ||
c9e38879 | 3568 | /* Fill in the unset ordinals with ones from our range. */ |
252b5132 RH |
3569 | ptr = (char *) xmalloc (size); |
3570 | ||
3571 | memset (ptr, 0, size); | |
3572 | ||
c9e38879 | 3573 | /* Mark in our large vector all the numbers that are taken. */ |
252b5132 RH |
3574 | for (i = 0; i < d_nfuncs; i++) |
3575 | { | |
3576 | if (d_export_vec[i]->ordinal != -1) | |
3577 | { | |
3578 | ptr[d_export_vec[i]->ordinal] = 1; | |
c9e38879 | 3579 | |
252b5132 | 3580 | if (lowest == -1 || d_export_vec[i]->ordinal < lowest) |
c9e38879 | 3581 | lowest = d_export_vec[i]->ordinal; |
252b5132 RH |
3582 | } |
3583 | } | |
3584 | ||
3585 | /* Start at 1 for compatibility with MS toolchain. */ | |
3586 | if (lowest == -1) | |
3587 | lowest = 1; | |
3588 | ||
26044998 | 3589 | /* Now fill in ordinals where the user wants us to choose. */ |
252b5132 RH |
3590 | for (i = 0; i < d_nfuncs; i++) |
3591 | { | |
3592 | if (d_export_vec[i]->ordinal == -1) | |
3593 | { | |
7aa52b1f | 3594 | int j; |
252b5132 | 3595 | |
26044998 | 3596 | /* First try within or after any user supplied range. */ |
252b5132 RH |
3597 | for (j = lowest; j < size; j++) |
3598 | if (ptr[j] == 0) | |
3599 | { | |
3600 | ptr[j] = 1; | |
3601 | d_export_vec[i]->ordinal = j; | |
3602 | goto done; | |
3603 | } | |
3604 | ||
26044998 | 3605 | /* Then try before the range. */ |
252b5132 RH |
3606 | for (j = lowest; j >0; j--) |
3607 | if (ptr[j] == 0) | |
3608 | { | |
3609 | ptr[j] = 1; | |
3610 | d_export_vec[i]->ordinal = j; | |
3611 | goto done; | |
3612 | } | |
3613 | done:; | |
3614 | } | |
3615 | } | |
3616 | ||
3617 | free (ptr); | |
3618 | ||
c9e38879 | 3619 | /* And resort. */ |
252b5132 RH |
3620 | qsort (d_export_vec, d_nfuncs, sizeof (export_type *), pfunc); |
3621 | ||
3622 | /* Work out the lowest and highest ordinal numbers. */ | |
3623 | if (d_nfuncs) | |
3624 | { | |
3625 | if (d_export_vec[0]) | |
3626 | d_low_ord = d_export_vec[0]->ordinal; | |
3627 | if (d_export_vec[d_nfuncs-1]) | |
3628 | d_high_ord = d_export_vec[d_nfuncs-1]->ordinal; | |
3629 | } | |
3630 | } | |
3631 | ||
252b5132 | 3632 | static void |
2da42df6 | 3633 | mangle_defs (void) |
252b5132 | 3634 | { |
c9e38879 | 3635 | /* First work out the minimum ordinal chosen. */ |
252b5132 | 3636 | export_type *exp; |
7aa52b1f | 3637 | export_type **d_export_vec = xmalloc (sizeof (export_type *) * d_nfuncs); |
35fd2dde | 3638 | int i; |
252b5132 RH |
3639 | |
3640 | inform (_("Processing definitions")); | |
26044998 | 3641 | |
252b5132 | 3642 | for (i = 0, exp = d_exports; exp; i++, exp = exp->next) |
c9e38879 | 3643 | d_export_vec[i] = exp; |
252b5132 RH |
3644 | |
3645 | process_duplicates (d_export_vec); | |
3646 | fill_ordinals (d_export_vec); | |
3647 | ||
c9e38879 | 3648 | /* Put back the list in the new order. */ |
252b5132 RH |
3649 | d_exports = 0; |
3650 | for (i = d_nfuncs - 1; i >= 0; i--) | |
3651 | { | |
3652 | d_export_vec[i]->next = d_exports; | |
3653 | d_exports = d_export_vec[i]; | |
3654 | } | |
3655 | ||
c9e38879 | 3656 | /* Build list in alpha order. */ |
252b5132 RH |
3657 | d_exports_lexically = (export_type **) |
3658 | xmalloc (sizeof (export_type *) * (d_nfuncs + 1)); | |
3659 | ||
3660 | for (i = 0, exp = d_exports; exp; i++, exp = exp->next) | |
c9e38879 NC |
3661 | d_exports_lexically[i] = exp; |
3662 | ||
252b5132 RH |
3663 | d_exports_lexically[i] = 0; |
3664 | ||
c6972290 | 3665 | qsort (d_exports_lexically, i, sizeof (export_type *), nfunc); |
252b5132 | 3666 | |
252b5132 RH |
3667 | inform (_("Processed definitions")); |
3668 | } | |
3669 | ||
252b5132 | 3670 | static void |
2da42df6 | 3671 | usage (FILE *file, int status) |
252b5132 RH |
3672 | { |
3673 | /* xgetext:c-format */ | |
8b53311e | 3674 | fprintf (file, _("Usage %s <option(s)> <object-file(s)>\n"), program_name); |
252b5132 | 3675 | /* xgetext:c-format */ |
661016bb | 3676 | fprintf (file, _(" -m --machine <machine> Create as DLL for <machine>. [default: %s]\n"), mname); |
fe49679d | 3677 | fprintf (file, _(" possible <machine>: arm[_interwork], i386, mcore[-elf]{-le|-be}, thumb\n")); |
252b5132 RH |
3678 | fprintf (file, _(" -e --output-exp <outname> Generate an export file.\n")); |
3679 | fprintf (file, _(" -l --output-lib <outname> Generate an interface library.\n")); | |
10e636d2 | 3680 | fprintf (file, _(" -y --output-delaylib <outname> Create a delay-import library.\n")); |
25ee24d9 NC |
3681 | fprintf (file, _(" --deterministic-libraries\n")); |
3682 | if (DEFAULT_AR_DETERMINISTIC) | |
3683 | fprintf (file, _(" Use zero for timestamps and uids/gids in output libraries (default)\n")); | |
3684 | else | |
3685 | fprintf (file, _(" Use zero for timestamps and uids/gids in output libraries\n")); | |
3686 | fprintf (file, _(" --non-deterministic-libraries\n")); | |
3687 | if (DEFAULT_AR_DETERMINISTIC) | |
3688 | fprintf (file, _(" Use actual timestamps and uids/gids in output libraries\n")); | |
3689 | else | |
3690 | fprintf (file, _(" Use actual timestamps and uids/gids in output libraries (default)\n")); | |
252b5132 RH |
3691 | fprintf (file, _(" -a --add-indirect Add dll indirects to export file.\n")); |
3692 | fprintf (file, _(" -D --dllname <name> Name of input dll to put into interface lib.\n")); | |
3693 | fprintf (file, _(" -d --input-def <deffile> Name of .def file to be read in.\n")); | |
3694 | fprintf (file, _(" -z --output-def <deffile> Name of .def file to be created.\n")); | |
661016bb NC |
3695 | fprintf (file, _(" --export-all-symbols Export all symbols to .def\n")); |
3696 | fprintf (file, _(" --no-export-all-symbols Only export listed symbols\n")); | |
3697 | fprintf (file, _(" --exclude-symbols <list> Don't export <list>\n")); | |
3698 | fprintf (file, _(" --no-default-excludes Clear default exclude symbols\n")); | |
252b5132 RH |
3699 | fprintf (file, _(" -b --base-file <basefile> Read linker generated base file.\n")); |
3700 | fprintf (file, _(" -x --no-idata4 Don't generate idata$4 section.\n")); | |
3701 | fprintf (file, _(" -c --no-idata5 Don't generate idata$5 section.\n")); | |
e77b97d4 | 3702 | fprintf (file, _(" --use-nul-prefixed-import-tables Use zero prefixed idata$4 and idata$5.\n")); |
14288fdc DS |
3703 | fprintf (file, _(" -U --add-underscore Add underscores to all symbols in interface library.\n")); |
3704 | fprintf (file, _(" --add-stdcall-underscore Add underscores to stdcall symbols in interface library.\n")); | |
36d21de5 KT |
3705 | fprintf (file, _(" --no-leading-underscore All symbols shouldn't be prefixed by an underscore.\n")); |
3706 | fprintf (file, _(" --leading-underscore All symbols should be prefixed by an underscore.\n")); | |
252b5132 RH |
3707 | fprintf (file, _(" -k --kill-at Kill @<n> from exported names.\n")); |
3708 | fprintf (file, _(" -A --add-stdcall-alias Add aliases without @<n>.\n")); | |
607dea97 | 3709 | fprintf (file, _(" -p --ext-prefix-alias <prefix> Add aliases with <prefix>.\n")); |
252b5132 RH |
3710 | fprintf (file, _(" -S --as <name> Use <name> for assembler.\n")); |
3711 | fprintf (file, _(" -f --as-flags <flags> Pass <flags> to the assembler.\n")); | |
5f0f29c3 | 3712 | fprintf (file, _(" -C --compat-implib Create backward compatible import library.\n")); |
252b5132 | 3713 | fprintf (file, _(" -n --no-delete Keep temp files (repeat for extra preservation).\n")); |
f9346411 | 3714 | fprintf (file, _(" -t --temp-prefix <prefix> Use <prefix> to construct temp file names.\n")); |
d4732f7c | 3715 | fprintf (file, _(" -I --identify <implib> Report the name of the DLL associated with <implib>.\n")); |
71c57c16 | 3716 | fprintf (file, _(" --identify-strict Causes --identify to report error when multiple DLLs.\n")); |
252b5132 RH |
3717 | fprintf (file, _(" -v --verbose Be verbose.\n")); |
3718 | fprintf (file, _(" -V --version Display the program version.\n")); | |
3719 | fprintf (file, _(" -h --help Display this information.\n")); | |
07012eee | 3720 | fprintf (file, _(" @<file> Read options from <file>.\n")); |
49e315b1 NC |
3721 | #ifdef DLLTOOL_MCORE_ELF |
3722 | fprintf (file, _(" -M --mcore-elf <outname> Process mcore-elf object files into <outname>.\n")); | |
3723 | fprintf (file, _(" -L --linker <name> Use <name> as the linker.\n")); | |
3724 | fprintf (file, _(" -F --linker-flags <flags> Pass <flags> to the linker.\n")); | |
3725 | #endif | |
92f01d61 JM |
3726 | if (REPORT_BUGS_TO[0] && status == 0) |
3727 | fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO); | |
252b5132 RH |
3728 | exit (status); |
3729 | } | |
3730 | ||
25ee24d9 NC |
3731 | /* 150 isn't special; it's just an arbitrary non-ASCII char value. */ |
3732 | enum command_line_switch | |
3733 | { | |
3734 | OPTION_EXPORT_ALL_SYMS = 150, | |
3735 | OPTION_NO_EXPORT_ALL_SYMS, | |
3736 | OPTION_EXCLUDE_SYMS, | |
3737 | OPTION_NO_DEFAULT_EXCLUDES, | |
3738 | OPTION_ADD_STDCALL_UNDERSCORE, | |
3739 | OPTION_USE_NUL_PREFIXED_IMPORT_TABLES, | |
3740 | OPTION_IDENTIFY_STRICT, | |
3741 | OPTION_NO_LEADING_UNDERSCORE, | |
3742 | OPTION_LEADING_UNDERSCORE, | |
3743 | OPTION_DETERMINISTIC_LIBRARIES, | |
3744 | OPTION_NON_DETERMINISTIC_LIBRARIES | |
3745 | }; | |
252b5132 RH |
3746 | |
3747 | static const struct option long_options[] = | |
3748 | { | |
25ee24d9 NC |
3749 | {"add-indirect", no_argument, NULL, 'a'}, |
3750 | {"add-stdcall-alias", no_argument, NULL, 'A'}, | |
3751 | {"add-stdcall-underscore", no_argument, NULL, OPTION_ADD_STDCALL_UNDERSCORE}, | |
3752 | {"add-underscore", no_argument, NULL, 'U'}, | |
3753 | {"as", required_argument, NULL, 'S'}, | |
3754 | {"as-flags", required_argument, NULL, 'f'}, | |
3755 | {"base-file", required_argument, NULL, 'b'}, | |
3756 | {"compat-implib", no_argument, NULL, 'C'}, | |
3757 | {"def", required_argument, NULL, 'd'}, /* For compatibility with older versions. */ | |
3758 | {"deterministic-libraries", no_argument, NULL, OPTION_DETERMINISTIC_LIBRARIES}, | |
252b5132 | 3759 | {"dllname", required_argument, NULL, 'D'}, |
252b5132 | 3760 | {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMS}, |
25ee24d9 | 3761 | {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL_SYMS}, |
607dea97 | 3762 | {"ext-prefix-alias", required_argument, NULL, 'p'}, |
25ee24d9 | 3763 | {"help", no_argument, NULL, 'h'}, |
d4732f7c | 3764 | {"identify", required_argument, NULL, 'I'}, |
71c57c16 | 3765 | {"identify-strict", no_argument, NULL, OPTION_IDENTIFY_STRICT}, |
25ee24d9 NC |
3766 | {"input-def", required_argument, NULL, 'd'}, |
3767 | {"kill-at", no_argument, NULL, 'k'}, | |
3768 | {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE}, | |
252b5132 | 3769 | {"machine", required_argument, NULL, 'm'}, |
49e315b1 | 3770 | {"mcore-elf", required_argument, NULL, 'M'}, |
25ee24d9 NC |
3771 | {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES}, |
3772 | {"no-delete", no_argument, NULL, 'n'}, | |
3773 | {"no-export-all-symbols", no_argument, NULL, OPTION_NO_EXPORT_ALL_SYMS}, | |
3774 | {"no-idata4", no_argument, NULL, 'x'}, | |
3775 | {"no-idata5", no_argument, NULL, 'c'}, | |
3776 | {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE}, | |
3777 | {"non-deterministic-libraries", no_argument, NULL, OPTION_NON_DETERMINISTIC_LIBRARIES}, | |
3778 | {"output-def", required_argument, NULL, 'z'}, | |
10e636d2 | 3779 | {"output-delaylib", required_argument, NULL, 'y'}, |
25ee24d9 NC |
3780 | {"output-exp", required_argument, NULL, 'e'}, |
3781 | {"output-lib", required_argument, NULL, 'l'}, | |
3782 | {"temp-prefix", required_argument, NULL, 't'}, | |
3783 | {"use-nul-prefixed-import-tables", no_argument, NULL, OPTION_USE_NUL_PREFIXED_IMPORT_TABLES}, | |
3784 | {"verbose", no_argument, NULL, 'v'}, | |
3785 | {"version", no_argument, NULL, 'V'}, | |
5ea695ed | 3786 | {NULL,0,NULL,0} |
252b5132 RH |
3787 | }; |
3788 | ||
2da42df6 | 3789 | int main (int, char **); |
e80ff7de | 3790 | |
252b5132 | 3791 | int |
2da42df6 | 3792 | main (int ac, char **av) |
252b5132 RH |
3793 | { |
3794 | int c; | |
3795 | int i; | |
3796 | char *firstarg = 0; | |
3797 | program_name = av[0]; | |
3798 | oav = av; | |
3799 | ||
87b9f255 | 3800 | #ifdef HAVE_LC_MESSAGES |
252b5132 | 3801 | setlocale (LC_MESSAGES, ""); |
3882b010 | 3802 | #endif |
3882b010 | 3803 | setlocale (LC_CTYPE, ""); |
252b5132 RH |
3804 | bindtextdomain (PACKAGE, LOCALEDIR); |
3805 | textdomain (PACKAGE); | |
3806 | ||
86eafac0 | 3807 | bfd_set_error_program_name (program_name); |
c843b1bb | 3808 | expandargv (&ac, &av); |
869b9d07 | 3809 | |
49e315b1 | 3810 | while ((c = getopt_long (ac, av, |
26044998 | 3811 | #ifdef DLLTOOL_MCORE_ELF |
5b155b95 | 3812 | "m:e:l:aD:d:z:b:xp:cCuUkAS:t:f:nI:vVHhM:L:F:", |
49e315b1 | 3813 | #else |
5b155b95 | 3814 | "m:e:l:y:aD:d:z:b:xp:cCuUkAS:t:f:nI:vVHh", |
49e315b1 | 3815 | #endif |
252b5132 RH |
3816 | long_options, 0)) |
3817 | != EOF) | |
3818 | { | |
3819 | switch (c) | |
3820 | { | |
252b5132 | 3821 | case OPTION_EXPORT_ALL_SYMS: |
015dc7e1 | 3822 | export_all_symbols = true; |
252b5132 RH |
3823 | break; |
3824 | case OPTION_NO_EXPORT_ALL_SYMS: | |
015dc7e1 | 3825 | export_all_symbols = false; |
252b5132 RH |
3826 | break; |
3827 | case OPTION_EXCLUDE_SYMS: | |
3828 | add_excludes (optarg); | |
3829 | break; | |
3830 | case OPTION_NO_DEFAULT_EXCLUDES: | |
015dc7e1 | 3831 | do_default_excludes = false; |
252b5132 | 3832 | break; |
e77b97d4 | 3833 | case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES: |
015dc7e1 | 3834 | use_nul_prefixed_import_tables = true; |
e77b97d4 | 3835 | break; |
14288fdc DS |
3836 | case OPTION_ADD_STDCALL_UNDERSCORE: |
3837 | add_stdcall_underscore = 1; | |
3838 | break; | |
36d21de5 KT |
3839 | case OPTION_NO_LEADING_UNDERSCORE: |
3840 | leading_underscore = 0; | |
3841 | break; | |
3842 | case OPTION_LEADING_UNDERSCORE: | |
3843 | leading_underscore = 1; | |
3844 | break; | |
71c57c16 NC |
3845 | case OPTION_IDENTIFY_STRICT: |
3846 | identify_strict = 1; | |
3847 | break; | |
49e315b1 NC |
3848 | case 'x': |
3849 | no_idata4 = 1; | |
3850 | break; | |
3851 | case 'c': | |
3852 | no_idata5 = 1; | |
3853 | break; | |
252b5132 RH |
3854 | case 'S': |
3855 | as_name = optarg; | |
3856 | break; | |
0e11a9e9 CF |
3857 | case 't': |
3858 | tmp_prefix = optarg; | |
3859 | break; | |
252b5132 RH |
3860 | case 'f': |
3861 | as_flags = optarg; | |
3862 | break; | |
3863 | ||
7aa52b1f | 3864 | /* Ignored for compatibility. */ |
252b5132 RH |
3865 | case 'u': |
3866 | break; | |
3867 | case 'a': | |
3868 | add_indirect = 1; | |
3869 | break; | |
3870 | case 'z': | |
3871 | output_def = fopen (optarg, FOPEN_WT); | |
030f4c7f SK |
3872 | if (!output_def) |
3873 | /* xgettext:c-format */ | |
3874 | fatal (_("Unable to open def-file: %s"), optarg); | |
252b5132 RH |
3875 | break; |
3876 | case 'D': | |
a0ce7f12 DS |
3877 | dll_name = (char*) lbasename (optarg); |
3878 | if (dll_name != optarg) | |
3879 | non_fatal (_("Path components stripped from dllname, '%s'."), | |
3880 | optarg); | |
252b5132 RH |
3881 | break; |
3882 | case 'l': | |
3883 | imp_name = optarg; | |
3884 | break; | |
3885 | case 'e': | |
3886 | exp_name = optarg; | |
3887 | break; | |
8b53311e | 3888 | case 'H': |
252b5132 RH |
3889 | case 'h': |
3890 | usage (stdout, 0); | |
3891 | break; | |
3892 | case 'm': | |
3893 | mname = optarg; | |
3894 | break; | |
d4732f7c CW |
3895 | case 'I': |
3896 | identify_imp_name = optarg; | |
3897 | break; | |
252b5132 RH |
3898 | case 'v': |
3899 | verbose = 1; | |
3900 | break; | |
3901 | case 'V': | |
3902 | print_version (program_name); | |
3903 | break; | |
252b5132 RH |
3904 | case 'U': |
3905 | add_underscore = 1; | |
3906 | break; | |
3907 | case 'k': | |
3908 | killat = 1; | |
3909 | break; | |
3910 | case 'A': | |
3911 | add_stdcall_alias = 1; | |
3912 | break; | |
607dea97 NC |
3913 | case 'p': |
3914 | ext_prefix_alias = optarg; | |
3915 | break; | |
252b5132 RH |
3916 | case 'd': |
3917 | def_file = optarg; | |
3918 | break; | |
3919 | case 'n': | |
3920 | dontdeltemps++; | |
3921 | break; | |
3922 | case 'b': | |
3923 | base_file = fopen (optarg, FOPEN_RB); | |
26044998 | 3924 | |
252b5132 RH |
3925 | if (!base_file) |
3926 | /* xgettext:c-format */ | |
3927 | fatal (_("Unable to open base-file: %s"), optarg); | |
3928 | ||
3929 | break; | |
49e315b1 NC |
3930 | #ifdef DLLTOOL_MCORE_ELF |
3931 | case 'M': | |
3932 | mcore_elf_out_file = optarg; | |
3933 | break; | |
3934 | case 'L': | |
3935 | mcore_elf_linker = optarg; | |
3936 | break; | |
3937 | case 'F': | |
3938 | mcore_elf_linker_flags = optarg; | |
3939 | break; | |
3940 | #endif | |
5f0f29c3 NC |
3941 | case 'C': |
3942 | create_compat_implib = 1; | |
3943 | break; | |
10e636d2 DK |
3944 | case 'y': |
3945 | delayimp_name = optarg; | |
3946 | break; | |
25ee24d9 NC |
3947 | case OPTION_DETERMINISTIC_LIBRARIES: |
3948 | deterministic = true; | |
3949 | break; | |
3950 | case OPTION_NON_DETERMINISTIC_LIBRARIES: | |
3951 | deterministic = false; | |
3952 | break; | |
252b5132 RH |
3953 | default: |
3954 | usage (stderr, 1); | |
3955 | break; | |
3956 | } | |
3957 | } | |
3958 | ||
3959 | for (i = 0; mtable[i].type; i++) | |
762100ed NC |
3960 | if (strcmp (mtable[i].type, mname) == 0) |
3961 | break; | |
252b5132 RH |
3962 | |
3963 | if (!mtable[i].type) | |
3964 | /* xgettext:c-format */ | |
3965 | fatal (_("Machine '%s' not supported"), mname); | |
3966 | ||
3967 | machine = i; | |
3968 | ||
2ea2f3c6 KT |
3969 | /* Check if we generated PE+. */ |
3970 | create_for_pep = strcmp (mname, "i386:x86-64") == 0; | |
3971 | ||
1d2ca237 KT |
3972 | { |
3973 | /* Check the default underscore */ | |
3974 | int u = leading_underscore; /* Underscoring mode. -1 for use default. */ | |
3975 | if (u == -1) | |
3976 | bfd_get_target_info (mtable[machine].how_bfd_target, NULL, | |
3977 | NULL, &u, NULL); | |
3978 | if (u != -1) | |
63b4cc53 | 3979 | leading_underscore = u != 0; |
1d2ca237 KT |
3980 | } |
3981 | ||
252b5132 RH |
3982 | if (!dll_name && exp_name) |
3983 | { | |
a0ce7f12 DS |
3984 | /* If we are inferring dll_name from exp_name, |
3985 | strip off any path components, without emitting | |
3aade688 L |
3986 | a warning. */ |
3987 | const char* exp_basename = lbasename (exp_name); | |
a0ce7f12 | 3988 | const int len = strlen (exp_basename) + 5; |
252b5132 | 3989 | dll_name = xmalloc (len); |
a0ce7f12 | 3990 | strcpy (dll_name, exp_basename); |
252b5132 | 3991 | strcat (dll_name, ".dll"); |
04276a0c | 3992 | dll_name_set_by_exp_name = 1; |
252b5132 RH |
3993 | } |
3994 | ||
49e315b1 NC |
3995 | if (as_name == NULL) |
3996 | as_name = deduce_name ("as"); | |
26044998 | 3997 | |
252b5132 RH |
3998 | /* Don't use the default exclude list if we're reading only the |
3999 | symbols in the .drectve section. The default excludes are meant | |
4000 | to avoid exporting DLL entry point and Cygwin32 impure_ptr. */ | |
4001 | if (! export_all_symbols) | |
015dc7e1 | 4002 | do_default_excludes = false; |
26044998 | 4003 | |
252b5132 RH |
4004 | if (do_default_excludes) |
4005 | set_default_excludes (); | |
4006 | ||
4007 | if (def_file) | |
4008 | process_def_file (def_file); | |
4009 | ||
4010 | while (optind < ac) | |
4011 | { | |
4012 | if (!firstarg) | |
4013 | firstarg = av[optind]; | |
4014 | scan_obj_file (av[optind]); | |
4015 | optind++; | |
4016 | } | |
4017 | ||
58de646b MS |
4018 | if (tmp_prefix == NULL) |
4019 | { | |
4020 | /* If possible use a deterministic prefix. */ | |
d65c0ddd | 4021 | if (imp_name || delayimp_name) |
58de646b | 4022 | { |
d65c0ddd MS |
4023 | const char *input = imp_name ? imp_name : delayimp_name; |
4024 | tmp_prefix = xmalloc (strlen (input) + 2); | |
4025 | sprintf (tmp_prefix, "%s_", input); | |
58de646b MS |
4026 | for (i = 0; tmp_prefix[i]; i++) |
4027 | if (!ISALNUM (tmp_prefix[i])) | |
4028 | tmp_prefix[i] = '_'; | |
4029 | } | |
4030 | else | |
4031 | { | |
4032 | tmp_prefix = prefix_encode ("d", getpid ()); | |
4033 | } | |
4034 | } | |
4035 | ||
252b5132 RH |
4036 | mangle_defs (); |
4037 | ||
4038 | if (exp_name) | |
4039 | gen_exp_file (); | |
26044998 | 4040 | |
252b5132 RH |
4041 | if (imp_name) |
4042 | { | |
26044998 | 4043 | /* Make imp_name safe for use as a label. */ |
252b5132 RH |
4044 | char *p; |
4045 | ||
4046 | imp_name_lab = xstrdup (imp_name); | |
4047 | for (p = imp_name_lab; *p; p++) | |
4048 | { | |
3882b010 | 4049 | if (!ISALNUM (*p)) |
252b5132 RH |
4050 | *p = '_'; |
4051 | } | |
4052 | head_label = make_label("_head_", imp_name_lab); | |
10e636d2 DK |
4053 | gen_lib_file (0); |
4054 | } | |
4055 | ||
4056 | if (delayimp_name) | |
4057 | { | |
4058 | /* Make delayimp_name safe for use as a label. */ | |
4059 | char *p; | |
4060 | ||
4061 | if (mtable[machine].how_dljtab == 0) | |
4062 | { | |
bf201fdd | 4063 | inform (_("Warning, machine type (%d) not supported for " |
10e636d2 DK |
4064 | "delayimport."), machine); |
4065 | } | |
4066 | else | |
4067 | { | |
4068 | killat = 1; | |
4069 | imp_name = delayimp_name; | |
4070 | imp_name_lab = xstrdup (imp_name); | |
4071 | for (p = imp_name_lab; *p; p++) | |
4072 | { | |
4073 | if (!ISALNUM (*p)) | |
4074 | *p = '_'; | |
4075 | } | |
4076 | head_label = make_label("__tailMerge_", imp_name_lab); | |
4077 | gen_lib_file (1); | |
4078 | } | |
252b5132 | 4079 | } |
26044998 | 4080 | |
252b5132 RH |
4081 | if (output_def) |
4082 | gen_def_file (); | |
26044998 | 4083 | |
d4732f7c CW |
4084 | if (identify_imp_name) |
4085 | { | |
4086 | identify_dll_for_implib (); | |
4087 | } | |
4088 | ||
49e315b1 NC |
4089 | #ifdef DLLTOOL_MCORE_ELF |
4090 | if (mcore_elf_out_file) | |
4091 | mcore_elf_gen_out_file (); | |
4092 | #endif | |
26044998 | 4093 | |
252b5132 RH |
4094 | return 0; |
4095 | } | |
49e315b1 | 4096 | |
bb0cb4db ILT |
4097 | /* Look for the program formed by concatenating PROG_NAME and the |
4098 | string running from PREFIX to END_PREFIX. If the concatenated | |
4099 | string contains a '/', try appending EXECUTABLE_SUFFIX if it is | |
2481e6a2 | 4100 | appropriate. */ |
bb0cb4db ILT |
4101 | |
4102 | static char * | |
2da42df6 | 4103 | look_for_prog (const char *prog_name, const char *prefix, int end_prefix) |
bb0cb4db ILT |
4104 | { |
4105 | struct stat s; | |
4106 | char *cmd; | |
4107 | ||
26044998 KH |
4108 | cmd = xmalloc (strlen (prefix) |
4109 | + strlen (prog_name) | |
2481e6a2 | 4110 | #ifdef HAVE_EXECUTABLE_SUFFIX |
26044998 | 4111 | + strlen (EXECUTABLE_SUFFIX) |
bb0cb4db ILT |
4112 | #endif |
4113 | + 10); | |
4114 | strcpy (cmd, prefix); | |
4115 | ||
4116 | sprintf (cmd + end_prefix, "%s", prog_name); | |
4117 | ||
4118 | if (strchr (cmd, '/') != NULL) | |
4119 | { | |
4120 | int found; | |
4121 | ||
4122 | found = (stat (cmd, &s) == 0 | |
2481e6a2 | 4123 | #ifdef HAVE_EXECUTABLE_SUFFIX |
26044998 | 4124 | || stat (strcat (cmd, EXECUTABLE_SUFFIX), &s) == 0 |
bb0cb4db ILT |
4125 | #endif |
4126 | ); | |
4127 | ||
4128 | if (! found) | |
26044998 | 4129 | { |
bb0cb4db ILT |
4130 | /* xgettext:c-format */ |
4131 | inform (_("Tried file: %s"), cmd); | |
4132 | free (cmd); | |
4133 | return NULL; | |
4134 | } | |
4135 | } | |
4136 | ||
4137 | /* xgettext:c-format */ | |
4138 | inform (_("Using file: %s"), cmd); | |
4139 | ||
4140 | return cmd; | |
4141 | } | |
4142 | ||
49e315b1 NC |
4143 | /* Deduce the name of the program we are want to invoke. |
4144 | PROG_NAME is the basic name of the program we want to run, | |
4145 | eg "as" or "ld". The catch is that we might want actually | |
fe49679d | 4146 | run "i386-pe-as". |
bb0cb4db ILT |
4147 | |
4148 | If argv[0] contains the full path, then try to find the program | |
4149 | in the same place, with and then without a target-like prefix. | |
4150 | ||
4151 | Given, argv[0] = /usr/local/bin/i586-cygwin32-dlltool, | |
26044998 | 4152 | deduce_name("as") uses the following search order: |
bb0cb4db ILT |
4153 | |
4154 | /usr/local/bin/i586-cygwin32-as | |
4155 | /usr/local/bin/as | |
4156 | as | |
26044998 | 4157 | |
bb0cb4db ILT |
4158 | If there's an EXECUTABLE_SUFFIX, it'll use that as well; for each |
4159 | name, it'll try without and then with EXECUTABLE_SUFFIX. | |
4160 | ||
4161 | Given, argv[0] = i586-cygwin32-dlltool, it will not even try "as" | |
4162 | as the fallback, but rather return i586-cygwin32-as. | |
26044998 | 4163 | |
bb0cb4db ILT |
4164 | Oh, and given, argv[0] = dlltool, it'll return "as". |
4165 | ||
4166 | Returns a dynamically allocated string. */ | |
4167 | ||
49e315b1 | 4168 | static char * |
2da42df6 | 4169 | deduce_name (const char *prog_name) |
49e315b1 | 4170 | { |
bb0cb4db ILT |
4171 | char *cmd; |
4172 | char *dash, *slash, *cp; | |
49e315b1 | 4173 | |
bb0cb4db ILT |
4174 | dash = NULL; |
4175 | slash = NULL; | |
4176 | for (cp = program_name; *cp != '\0'; ++cp) | |
4177 | { | |
4178 | if (*cp == '-') | |
4179 | dash = cp; | |
4180 | if ( | |
4181 | #if defined(__DJGPP__) || defined (__CYGWIN__) || defined(__WIN32__) | |
4182 | *cp == ':' || *cp == '\\' || | |
4183 | #endif | |
4184 | *cp == '/') | |
4185 | { | |
4186 | slash = cp; | |
4187 | dash = NULL; | |
4188 | } | |
4189 | } | |
49e315b1 | 4190 | |
bb0cb4db | 4191 | cmd = NULL; |
49e315b1 | 4192 | |
bb0cb4db ILT |
4193 | if (dash != NULL) |
4194 | { | |
4195 | /* First, try looking for a prefixed PROG_NAME in the | |
4196 | PROGRAM_NAME directory, with the same prefix as PROGRAM_NAME. */ | |
4197 | cmd = look_for_prog (prog_name, program_name, dash - program_name + 1); | |
4198 | } | |
49e315b1 | 4199 | |
bb0cb4db ILT |
4200 | if (slash != NULL && cmd == NULL) |
4201 | { | |
4202 | /* Next, try looking for a PROG_NAME in the same directory as | |
4203 | that of this program. */ | |
4204 | cmd = look_for_prog (prog_name, program_name, slash - program_name + 1); | |
4205 | } | |
4206 | ||
4207 | if (cmd == NULL) | |
4208 | { | |
4209 | /* Just return PROG_NAME as is. */ | |
4210 | cmd = xstrdup (prog_name); | |
4211 | } | |
4212 | ||
4213 | return cmd; | |
49e315b1 NC |
4214 | } |
4215 | ||
4216 | #ifdef DLLTOOL_MCORE_ELF | |
4217 | typedef struct fname_cache | |
4218 | { | |
fd64a958 | 4219 | const char * filename; |
49e315b1 NC |
4220 | struct fname_cache * next; |
4221 | } | |
4222 | fname_cache; | |
4223 | ||
4224 | static fname_cache fnames; | |
4225 | ||
4226 | static void | |
fd64a958 | 4227 | mcore_elf_cache_filename (const char * filename) |
49e315b1 NC |
4228 | { |
4229 | fname_cache * ptr; | |
4230 | ||
4231 | ptr = & fnames; | |
4232 | ||
4233 | while (ptr->next != NULL) | |
4234 | ptr = ptr->next; | |
4235 | ||
4236 | ptr->filename = filename; | |
4237 | ptr->next = (fname_cache *) malloc (sizeof (fname_cache)); | |
4238 | if (ptr->next != NULL) | |
4239 | ptr->next->next = NULL; | |
4240 | } | |
4241 | ||
762100ed NC |
4242 | #define MCORE_ELF_TMP_OBJ "mcoreelf.o" |
4243 | #define MCORE_ELF_TMP_EXP "mcoreelf.exp" | |
4244 | #define MCORE_ELF_TMP_LIB "mcoreelf.lib" | |
4245 | ||
49e315b1 NC |
4246 | static void |
4247 | mcore_elf_gen_out_file (void) | |
4248 | { | |
4249 | fname_cache * ptr; | |
bb0cb4db | 4250 | dyn_string_t ds; |
49e315b1 NC |
4251 | |
4252 | /* Step one. Run 'ld -r' on the input object files in order to resolve | |
4253 | any internal references and to generate a single .exports section. */ | |
4254 | ptr = & fnames; | |
4255 | ||
bb0cb4db | 4256 | ds = dyn_string_new (100); |
55b9cdf1 | 4257 | dyn_string_append_cstr (ds, "-r "); |
49e315b1 NC |
4258 | |
4259 | if (mcore_elf_linker_flags != NULL) | |
55b9cdf1 | 4260 | dyn_string_append_cstr (ds, mcore_elf_linker_flags); |
26044998 | 4261 | |
49e315b1 NC |
4262 | while (ptr->next != NULL) |
4263 | { | |
55b9cdf1 AM |
4264 | dyn_string_append_cstr (ds, ptr->filename); |
4265 | dyn_string_append_cstr (ds, " "); | |
49e315b1 NC |
4266 | |
4267 | ptr = ptr->next; | |
4268 | } | |
4269 | ||
55b9cdf1 AM |
4270 | dyn_string_append_cstr (ds, "-o "); |
4271 | dyn_string_append_cstr (ds, MCORE_ELF_TMP_OBJ); | |
49e315b1 NC |
4272 | |
4273 | if (mcore_elf_linker == NULL) | |
4274 | mcore_elf_linker = deduce_name ("ld"); | |
26044998 | 4275 | |
bb0cb4db ILT |
4276 | run (mcore_elf_linker, ds->s); |
4277 | ||
4278 | dyn_string_delete (ds); | |
49e315b1 | 4279 | |
26044998 | 4280 | /* Step two. Create a .exp file and a .lib file from the temporary file. |
c9e38879 | 4281 | Do this by recursively invoking dlltool... */ |
bb0cb4db ILT |
4282 | ds = dyn_string_new (100); |
4283 | ||
55b9cdf1 AM |
4284 | dyn_string_append_cstr (ds, "-S "); |
4285 | dyn_string_append_cstr (ds, as_name); | |
26044998 | 4286 | |
55b9cdf1 AM |
4287 | dyn_string_append_cstr (ds, " -e "); |
4288 | dyn_string_append_cstr (ds, MCORE_ELF_TMP_EXP); | |
4289 | dyn_string_append_cstr (ds, " -l "); | |
4290 | dyn_string_append_cstr (ds, MCORE_ELF_TMP_LIB); | |
4291 | dyn_string_append_cstr (ds, " " ); | |
4292 | dyn_string_append_cstr (ds, MCORE_ELF_TMP_OBJ); | |
49e315b1 NC |
4293 | |
4294 | if (verbose) | |
55b9cdf1 | 4295 | dyn_string_append_cstr (ds, " -v"); |
26044998 | 4296 | |
49e315b1 | 4297 | if (dontdeltemps) |
762100ed | 4298 | { |
55b9cdf1 | 4299 | dyn_string_append_cstr (ds, " -n"); |
26044998 | 4300 | |
762100ed | 4301 | if (dontdeltemps > 1) |
55b9cdf1 | 4302 | dyn_string_append_cstr (ds, " -n"); |
762100ed | 4303 | } |
49e315b1 NC |
4304 | |
4305 | /* XXX - FIME: ought to check/copy other command line options as well. */ | |
bb0cb4db ILT |
4306 | run (program_name, ds->s); |
4307 | ||
4308 | dyn_string_delete (ds); | |
49e315b1 | 4309 | |
74479bd3 | 4310 | /* Step four. Feed the .exp and object files to ld -shared to create the dll. */ |
bb0cb4db ILT |
4311 | ds = dyn_string_new (100); |
4312 | ||
55b9cdf1 | 4313 | dyn_string_append_cstr (ds, "-shared "); |
49e315b1 NC |
4314 | |
4315 | if (mcore_elf_linker_flags) | |
55b9cdf1 AM |
4316 | dyn_string_append_cstr (ds, mcore_elf_linker_flags); |
4317 | ||
4318 | dyn_string_append_cstr (ds, " "); | |
4319 | dyn_string_append_cstr (ds, MCORE_ELF_TMP_EXP); | |
4320 | dyn_string_append_cstr (ds, " "); | |
4321 | dyn_string_append_cstr (ds, MCORE_ELF_TMP_OBJ); | |
4322 | dyn_string_append_cstr (ds, " -o "); | |
4323 | dyn_string_append_cstr (ds, mcore_elf_out_file); | |
49e315b1 | 4324 | |
bb0cb4db | 4325 | run (mcore_elf_linker, ds->s); |
49e315b1 | 4326 | |
bb0cb4db | 4327 | dyn_string_delete (ds); |
762100ed NC |
4328 | |
4329 | if (dontdeltemps == 0) | |
74479bd3 | 4330 | unlink (MCORE_ELF_TMP_EXP); |
762100ed NC |
4331 | |
4332 | if (dontdeltemps < 2) | |
4333 | unlink (MCORE_ELF_TMP_OBJ); | |
49e315b1 NC |
4334 | } |
4335 | #endif /* DLLTOOL_MCORE_ELF */ |