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