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