1 /* dlltool.c -- tool to generate stuff for PE style DLLs
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
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
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 /* This program allows you to build the files necessary to create
24 DLLs to run on a system which understands PE format image files.
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.
32 A DLL contains an export table which contains the information
33 which the runtime loader needs to tie up references from a
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.
41 A DEF file contains any number of the following commands:
44 NAME <name> [ , <base> ]
45 The result is going to be <name>.EXE
47 LIBRARY <name> [ , <base> ]
48 The result is going to be <name>.DLL
50 EXPORTS ( ( ( <name1> [ = <name2> ] )
51 | ( <name1> = <module-name> . <external-name>))
52 [ @ <integer> ] [ NONAME ] [CONSTANT] [DATA] [PRIVATE] ) *
53 Declares name1 as an exported symbol from the
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>.
58 IMPORTS ( ( <internal-name> = <module-name> . <integer> )
59 | ( [ <internal-name> = ] <module-name> . <external-name> )) *
60 Declares that <external-name> or the exported function whose ordinal number
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
63 function will be refereed to in the body of the DLL.
66 Puts <string> into output .exp file in the .rdata section
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.
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
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>
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
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.
100 asm (".section .drectve");
101 asm (".ascii \"-export:adef\"");
105 printf ("hello from the dll %s\n", s);
110 printf ("hello from the dll and the other entry point %s\n", s);
114 asm (".section .drectve");
115 asm (".ascii \"-export:cdef\"");
116 asm (".ascii \"-export:ddef\"");
120 printf ("hello from the dll %s\n", s);
125 printf ("hello from the dll and the other entry point %s\n", s);
143 HEAPSIZE 0x40000, 0x2000
147 SECTIONS donkey READ WRITE
150 # Compile up the parts of the dll and the program
152 gcc -c file1.c file2.c themain.c
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)
158 ar qcv thedll.in file1.o file2.o
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).
165 dlltool --def thedll.def --output-exp thedll.o --output-lib thedll.a
167 # Build the dll with the library and the export table
169 ld -o thedll.dll thedll.o thedll.in
171 # Link the executable with the import library
173 gcc -o themain.exe themain.o thedll.a
175 This example can be extended if relocations are needed in the DLL:
177 # Compile up the parts of the dll and the program
179 gcc -c file1.c file2.c themain.c
181 # Run this tool over the DLL's .def file and generate an imports file.
183 dlltool --def thedll.def --output-lib thedll.lib
185 # Link the executable with the import library and generate a base file
188 gcc -o themain.exe themain.o thedll.lib -Wl,--base-file -Wl,themain.base
190 # Run this tool over the DLL's .def file and generate an exports file
191 # which includes the relocations from the base file.
193 dlltool --def thedll.def --base-file themain.base --output-exp thedll.exp
195 # Build the dll with file1.o, file2.o and the export table
197 ld -o thedll.dll thedll.exp file1.o file2.o */
199 /* .idata section description
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.
205 .idata$2 = Import Directory Table
206 = array of IMAGE_IMPORT_DESCRIPTOR's.
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
214 .idata$3 = null terminating entry for .idata$2.
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.
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
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.
233 .idata$7 = dll name (eg: "kernel32.dll"). (.idata$6 for ppc). */
235 /* AIX requires this to be the first thing in the file. */
242 #define show_allnames 0
246 #include "libiberty.h"
248 #include "demangle.h"
249 #include "dyn-string.h"
252 #include "safe-ctype.h"
255 #include <sys/stat.h>
260 #include "coff/arm.h"
261 #include "coff/internal.h"
263 #ifdef DLLTOOL_DEFAULT_MX86_64
264 #include "coff/x86_64.h"
266 #ifdef DLLTOOL_DEFAULT_I386
267 #include "coff/i386.h"
270 #ifndef COFF_PAGE_SIZE
271 #define COFF_PAGE_SIZE ((bfd_vma) 4096)
275 #define PAGE_MASK ((bfd_vma) (- COFF_PAGE_SIZE))
278 /* Get current BFD error message. */
279 #define bfd_get_errmsg() (bfd_errmsg (bfd_get_error ()))
281 /* Forward references. */
282 static char *look_for_prog (const char *, const char *, int);
283 static char *deduce_name (const char *);
285 #ifdef DLLTOOL_MCORE_ELF
286 static void mcore_elf_cache_filename (const char *);
287 static void mcore_elf_gen_out_file (void);
290 #ifdef HAVE_SYS_WAIT_H
291 #include <sys/wait.h>
292 #else /* ! HAVE_SYS_WAIT_H */
293 #if ! defined (_WIN32) || defined (__CYGWIN32__)
295 #define WIFEXITED(w) (((w) & 0377) == 0)
298 #define WIFSIGNALED(w) (((w) & 0377) != 0177 && ((w) & ~0377) == 0)
301 #define WTERMSIG(w) ((w) & 0177)
304 #define WEXITSTATUS(w) (((w) >> 8) & 0377)
306 #else /* defined (_WIN32) && ! defined (__CYGWIN32__) */
308 #define WIFEXITED(w) (((w) & 0xff) == 0)
311 #define WIFSIGNALED(w) (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f)
314 #define WTERMSIG(w) ((w) & 0x7f)
317 #define WEXITSTATUS(w) (((w) & 0xff00) >> 8)
319 #endif /* defined (_WIN32) && ! defined (__CYGWIN32__) */
320 #endif /* ! HAVE_SYS_WAIT_H */
322 /* ifunc and ihead data structures: ttk@cygnus.com 1997
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.
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. */
337 typedef struct ifunct
339 char * name; /* Name of function being imported. */
340 char * its_name; /* Optional import table symbol name. */
341 int ord; /* Two-byte ordinal value associated with function. */
345 typedef struct iheadt
347 char * dllname; /* Name of dll file imported from. */
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. */
354 /* Structure containing all import information as defined in .def file
355 (qv "ihead structure"). */
357 static iheadtype *import_list = NULL;
358 static char *as_name = NULL;
359 static char * as_flags = "";
360 static char *tmp_prefix;
361 static int no_idata4;
362 static int no_idata5;
363 static char *exp_name;
364 static char *imp_name;
365 static char *delayimp_name;
366 static char *identify_imp_name;
367 static bfd_boolean identify_strict;
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). */
373 typedef struct dll_name_list_node_t
376 struct dll_name_list_node_t * next;
377 } dll_name_list_node_type;
379 typedef struct dll_name_list_t
381 dll_name_list_node_type * head;
382 dll_name_list_node_type * tail;
383 } dll_name_list_type;
385 /* Types used to pass data to iterator functions. */
386 typedef struct symname_search_data_t
388 const char * symname;
390 } symname_search_data_type;
392 typedef struct identify_data_t
394 dll_name_list_type * list;
395 bfd_boolean ms_style_implib;
396 } identify_data_type;
399 static char *head_label;
400 static char *imp_name_lab;
401 static char *dll_name;
402 static int dll_name_set_by_exp_name;
403 static int add_indirect = 0;
404 static int add_underscore = 0;
405 static int add_stdcall_underscore = 0;
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. */
410 static int leading_underscore = -1;
411 static int dontdeltemps = 0;
413 /* TRUE if we should export all symbols. Otherwise, we only export
414 symbols listed in .drectve sections or in the def file. */
415 static bfd_boolean export_all_symbols;
417 /* TRUE if we should exclude the symbols in DEFAULT_EXCLUDES when
418 exporting all symbols. */
419 static bfd_boolean do_default_excludes = TRUE;
421 static bfd_boolean use_nul_prefixed_import_tables = FALSE;
423 /* Default symbols to exclude when exporting all the symbols. */
424 static const char *default_excludes = "DllMain@12,DllEntryPoint@0,impure_ptr";
426 /* TRUE if we should add __imp_<SYMBOL> to import libraries for backward
427 compatibility to old Cygwin releases. */
428 static bfd_boolean create_compat_implib;
430 /* TRUE if we have to write PE+ import libraries. */
431 static bfd_boolean create_for_pep;
433 static char *def_file;
435 extern char * program_name;
439 static int add_stdcall_alias;
440 static const char *ext_prefix_alias;
442 static FILE *output_def;
443 static FILE *base_file;
445 #ifdef DLLTOOL_DEFAULT_ARM
446 static const char *mname = "arm";
449 #ifdef DLLTOOL_DEFAULT_ARM_EPOC
450 static const char *mname = "arm-epoc";
453 #ifdef DLLTOOL_DEFAULT_ARM_WINCE
454 static const char *mname = "arm-wince";
457 #ifdef DLLTOOL_DEFAULT_I386
458 static const char *mname = "i386";
461 #ifdef DLLTOOL_DEFAULT_MX86_64
462 static const char *mname = "i386:x86-64";
465 #ifdef DLLTOOL_DEFAULT_PPC
466 static const char *mname = "ppc";
469 #ifdef DLLTOOL_DEFAULT_SH
470 static const char *mname = "sh";
473 #ifdef DLLTOOL_DEFAULT_MIPS
474 static const char *mname = "mips";
477 #ifdef DLLTOOL_DEFAULT_MCORE
478 static const char * mname = "mcore-le";
481 #ifdef DLLTOOL_DEFAULT_MCORE_ELF
482 static const char * mname = "mcore-elf";
483 static char * mcore_elf_out_file = NULL;
484 static char * mcore_elf_linker = NULL;
485 static char * mcore_elf_linker_flags = NULL;
487 #define DRECTVE_SECTION_NAME ((machine == MMCORE_ELF || machine == MMCORE_ELF_LE) ? ".exports" : ".drectve")
490 #ifndef DRECTVE_SECTION_NAME
491 #define DRECTVE_SECTION_NAME ".drectve"
494 /* What's the right name for this ? */
497 /* External name alias numbering starts here. */
498 #define PREFIX_ALIAS_BASE 20000
501 char *tmp_head_s_buf;
502 char *tmp_head_o_buf;
503 char *tmp_tail_s_buf;
504 char *tmp_tail_o_buf;
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")
514 /* This bit of assembly does jmp * .... */
515 static const unsigned char i386_jtab[] =
517 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
520 static const unsigned char i386_dljtab[] =
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 */
527 static const unsigned char arm_jtab[] =
529 0x00, 0xc0, 0x9f, 0xe5, /* ldr ip, [pc] */
530 0x00, 0xf0, 0x9c, 0xe5, /* ldr pc, [ip] */
534 static const unsigned char arm_interwork_jtab[] =
536 0x04, 0xc0, 0x9f, 0xe5, /* ldr ip, [pc] */
537 0x00, 0xc0, 0x9c, 0xe5, /* ldr ip, [ip] */
538 0x1c, 0xff, 0x2f, 0xe1, /* bx ip */
542 static const unsigned char thumb_jtab[] =
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 */
553 static const unsigned char mcore_be_jtab[] =
555 0x71, 0x02, /* lrw r1,2 */
556 0x81, 0x01, /* ld.w r1,(r1,0) */
557 0x00, 0xC1, /* jmp r1 */
558 0x12, 0x00, /* nop */
559 0x00, 0x00, 0x00, 0x00 /* <address> */
562 static const unsigned char mcore_le_jtab[] =
564 0x02, 0x71, /* lrw r1,2 */
565 0x01, 0x81, /* ld.w r1,(r1,0) */
566 0xC1, 0x00, /* jmp r1 */
567 0x00, 0x12, /* nop */
568 0x00, 0x00, 0x00, 0x00 /* <address> */
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
576 static const unsigned char ppc_jtab[] =
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 */
588 /* The glue instruction, picks up the toc from the stw in
589 the above code: "lwz r2,4(r1)". */
590 static bfd_vma ppc_glue_insn = 0x80410004;
593 static const char i386_trampoline[] =
597 "\tpushl $__DELAY_IMPORT_DESCRIPTOR_%s\n"
598 "\tcall ___delayLoadHelper2@8\n"
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;
630 static const struct mac
635 "arm", ".byte", ".short", ".long", ".asciz", "@",
636 "ldr\tip,[pc]\n\tldr\tpc,[ip]\n\t.long",
637 ".global", ".space", ".align\t2",".align\t4", "-mapcs-32",
638 "pe-arm-little", bfd_arch_arm,
639 arm_jtab, sizeof (arm_jtab), 8,
645 "i386", ".byte", ".short", ".long", ".asciz", "#",
646 "jmp *", ".global", ".space", ".align\t2",".align\t4", "",
647 "pe-i386",bfd_arch_i386,
648 i386_jtab, sizeof (i386_jtab), 2,
649 i386_dljtab, sizeof (i386_dljtab), 2, 7, 12, i386_trampoline
654 "ppc", ".byte", ".short", ".long", ".asciz", "#",
655 "jmp *", ".global", ".space", ".align\t2",".align\t4", "",
656 "pe-powerpcle",bfd_arch_powerpc,
657 ppc_jtab, sizeof (ppc_jtab), 0,
663 "thumb", ".byte", ".short", ".long", ".asciz", "@",
664 "push\t{r6}\n\tldr\tr6, [pc, #8]\n\tldr\tr6, [r6]\n\tmov\tip, r6\n\tpop\t{r6}\n\tbx\tip",
665 ".global", ".space", ".align\t2",".align\t4", "-mthumb-interwork",
666 "pe-arm-little", bfd_arch_arm,
667 thumb_jtab, sizeof (thumb_jtab), 12,
671 #define MARM_INTERWORK 4
673 "arm_interwork", ".byte", ".short", ".long", ".asciz", "@",
674 "ldr\tip,[pc]\n\tldr\tip,[ip]\n\tbx\tip\n\t.long",
675 ".global", ".space", ".align\t2",".align\t4", "-mthumb-interwork",
676 "pe-arm-little", bfd_arch_arm,
677 arm_interwork_jtab, sizeof (arm_interwork_jtab), 12,
683 "mcore-be", ".byte", ".short", ".long", ".asciz", "//",
684 "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
685 ".global", ".space", ".align\t2",".align\t4", "",
686 "pe-mcore-big", bfd_arch_mcore,
687 mcore_be_jtab, sizeof (mcore_be_jtab), 8,
693 "mcore-le", ".byte", ".short", ".long", ".asciz", "//",
694 "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
695 ".global", ".space", ".align\t2",".align\t4", "-EL",
696 "pe-mcore-little", bfd_arch_mcore,
697 mcore_le_jtab, sizeof (mcore_le_jtab), 8,
703 "mcore-elf-be", ".byte", ".short", ".long", ".asciz", "//",
704 "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
705 ".global", ".space", ".align\t2",".align\t4", "",
706 "elf32-mcore-big", bfd_arch_mcore,
707 mcore_be_jtab, sizeof (mcore_be_jtab), 8,
712 #define MMCORE_ELF_LE 8
713 "mcore-elf-le", ".byte", ".short", ".long", ".asciz", "//",
714 "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
715 ".global", ".space", ".align\t2",".align\t4", "-EL",
716 "elf32-mcore-little", bfd_arch_mcore,
717 mcore_le_jtab, sizeof (mcore_le_jtab), 8,
723 "arm-epoc", ".byte", ".short", ".long", ".asciz", "@",
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,
727 arm_jtab, sizeof (arm_jtab), 8,
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,
737 arm_jtab, sizeof (arm_jtab), 8,
743 "i386:x86-64", ".byte", ".short", ".long", ".asciz", "#",
744 "jmp *", ".global", ".space", ".align\t2",".align\t4", "",
745 "pe-x86-64",bfd_arch_i386,
746 i386_jtab, sizeof (i386_jtab), 2,
747 i386_dljtab, sizeof (i386_dljtab), 2, 7, 12, i386_trampoline
750 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
760 typedef struct export
763 const char *internal_name;
764 const char *import_name;
765 const char *its_name;
768 int noname; /* Don't put name in image file. */
769 int private; /* Don't put reference in import lib. */
772 int forward; /* Number of forward label, 0 means no forward. */
777 /* A list of symbols which we should not export. */
781 struct string_list *next;
785 static struct string_list *excludes;
787 static const char *rvaafter (int);
788 static const char *rvabefore (int);
789 static const char *asm_prefix (int, const char *);
790 static void process_def_file (const char *);
791 static void new_directive (char *);
792 static void append_import (const char *, const char *, int, const char *);
793 static void run (const char *, char *);
794 static void scan_drectve_symbols (bfd *);
795 static void scan_filtered_symbols (bfd *, void *, long, unsigned int);
796 static void add_excludes (const char *);
797 static bfd_boolean match_exclude (const char *);
798 static void set_default_excludes (void);
799 static long filter_symbols (bfd *, void *, long, unsigned int);
800 static void scan_all_symbols (bfd *);
801 static void scan_open_obj_file (bfd *);
802 static void scan_obj_file (const char *);
803 static void dump_def_info (FILE *);
804 static int sfunc (const void *, const void *);
805 static void flush_page (FILE *, bfd_vma *, bfd_vma, int);
806 static void gen_def_file (void);
807 static void generate_idata_ofile (FILE *);
808 static void assemble_file (const char *, const char *);
809 static void gen_exp_file (void);
810 static const char *xlate (const char *);
811 static char *make_label (const char *, const char *);
812 static char *make_imp_label (const char *, const char *);
813 static bfd *make_one_lib_file (export_type *, int, int);
814 static bfd *make_head (void);
815 static bfd *make_tail (void);
816 static bfd *make_delay_head (void);
817 static void gen_lib_file (int);
818 static void dll_name_list_append (dll_name_list_type *, bfd_byte *);
819 static int dll_name_list_count (dll_name_list_type *);
820 static void dll_name_list_print (dll_name_list_type *);
821 static void dll_name_list_free_contents (dll_name_list_node_type *);
822 static void dll_name_list_free (dll_name_list_type *);
823 static dll_name_list_type * dll_name_list_create (void);
824 static void identify_dll_for_implib (void);
825 static void identify_search_archive
826 (bfd *, void (*) (bfd *, bfd *, void *), void *);
827 static void identify_search_member (bfd *, bfd *, void *);
828 static bfd_boolean identify_process_section_p (asection *, bfd_boolean);
829 static void identify_search_section (bfd *, asection *, void *);
830 static void identify_member_contains_symname (bfd *, bfd *, void *);
832 static int pfunc (const void *, const void *);
833 static int nfunc (const void *, const void *);
834 static void remove_null_names (export_type **);
835 static void process_duplicates (export_type **);
836 static void fill_ordinals (export_type **);
837 static void mangle_defs (void);
838 static void usage (FILE *, int);
839 static void inform (const char *, ...) ATTRIBUTE_PRINTF_1;
840 static void set_dll_name_from_def (const char *name, char is_dll);
843 prefix_encode (char *start, unsigned code)
845 static char alpha[26] = "abcdefghijklmnopqrstuvwxyz";
849 p = strchr (buf, '\0');
851 *p++ = alpha[code % sizeof (alpha)];
852 while ((code /= sizeof (alpha)) != 0);
858 dlltmp (char **buf, const char *fmt)
862 *buf = malloc (strlen (tmp_prefix) + 64);
863 sprintf (*buf, fmt, tmp_prefix);
869 inform VPARAMS ((const char * message, ...))
871 VA_OPEN (args, message);
872 VA_FIXEDARG (args, const char *, message);
877 report (message, args);
901 /* xgettext:c-format */
902 fatal (_("Internal error: Unknown machine type: %d"), mach);
927 /* xgettext:c-format */
928 fatal (_("Internal error: Unknown machine type: %d"), mach);
935 asm_prefix (int mach, const char *name)
952 /* Symbol names starting with ? do not have a leading underscore. */
953 if ((name && *name == '?') || leading_underscore == 0)
958 /* xgettext:c-format */
959 fatal (_("Internal error: Unknown machine type: %d"), mach);
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
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)
989 #define ASM_SWITCHES mtable[machine].how_default_as_switches
994 process_def_file (const char *name)
996 FILE *f = fopen (name, FOPEN_RT);
999 /* xgettext:c-format */
1000 fatal (_("Can't open def file: %s"), name);
1004 /* xgettext:c-format */
1005 inform (_("Processing def file: %s"), name);
1009 inform (_("Processed def file"));
1012 /**********************************************************************/
1014 /* Communications with the parser. */
1016 static int d_nfuncs; /* Number of functions exported. */
1017 static int d_named_nfuncs; /* Number of named functions exported. */
1018 static int d_low_ord; /* Lowest ordinal index. */
1019 static int d_high_ord; /* Highest ordinal index. */
1020 static export_type *d_exports; /* List of exported functions. */
1021 static export_type **d_exports_lexically; /* Vector of exported functions in alpha order. */
1022 static dlist_type *d_list; /* Descriptions. */
1023 static dlist_type *a_list; /* Stuff to go in directives. */
1024 static int d_nforwards = 0; /* Number of forwarded exports. */
1026 static int d_is_dll;
1027 static int d_is_exe;
1030 yyerror (const char * err ATTRIBUTE_UNUSED)
1032 /* xgettext:c-format */
1033 non_fatal (_("Syntax error in def file %s:%d"), def_file, linenumber);
1039 def_exports (const char *name, const char *internal_name, int ordinal,
1040 int noname, int constant, int data, int private,
1041 const char *its_name)
1043 struct export *p = (struct export *) xmalloc (sizeof (*p));
1046 p->internal_name = internal_name ? internal_name : name;
1047 p->its_name = its_name;
1048 p->import_name = name;
1049 p->ordinal = ordinal;
1050 p->constant = constant;
1052 p->private = private;
1054 p->next = d_exports;
1058 if ((internal_name != NULL)
1059 && (strchr (internal_name, '.') != NULL))
1060 p->forward = ++d_nforwards;
1062 p->forward = 0; /* no forward */
1066 set_dll_name_from_def (const char *name, char is_dll)
1068 const char *image_basename = lbasename (name);
1069 if (image_basename != name)
1070 non_fatal (_("%s: Path components stripped from image name, '%s'."),
1072 /* Append the default suffix, if none specified. */
1073 if (strchr (image_basename, '.') == 0)
1075 const char * suffix = is_dll ? ".dll" : ".exe";
1077 dll_name = xmalloc (strlen (image_basename) + strlen (suffix) + 1);
1078 sprintf (dll_name, "%s%s", image_basename, suffix);
1081 dll_name = xstrdup (image_basename);
1085 def_name (const char *name, int base)
1087 /* xgettext:c-format */
1088 inform (_("NAME: %s base: %x"), name, base);
1091 non_fatal (_("Can't have LIBRARY and NAME"));
1093 if (dll_name_set_by_exp_name && name && *name != 0)
1096 dll_name_set_by_exp_name = 0;
1098 /* If --dllname not provided, use the one in the DEF file.
1099 FIXME: Is this appropriate for executables? */
1101 set_dll_name_from_def (name, 0);
1106 def_library (const char *name, int base)
1108 /* xgettext:c-format */
1109 inform (_("LIBRARY: %s base: %x"), name, base);
1112 non_fatal (_("Can't have LIBRARY and NAME"));
1114 if (dll_name_set_by_exp_name && name && *name != 0)
1117 dll_name_set_by_exp_name = 0;
1120 /* If --dllname not provided, use the one in the DEF file. */
1122 set_dll_name_from_def (name, 1);
1127 def_description (const char *desc)
1129 dlist_type *d = (dlist_type *) xmalloc (sizeof (dlist_type));
1130 d->text = xstrdup (desc);
1136 new_directive (char *dir)
1138 dlist_type *d = (dlist_type *) xmalloc (sizeof (dlist_type));
1139 d->text = xstrdup (dir);
1145 def_heapsize (int reserve, int commit)
1149 sprintf (b, "-heap 0x%x,0x%x ", reserve, commit);
1151 sprintf (b, "-heap 0x%x ", reserve);
1152 new_directive (xstrdup (b));
1156 def_stacksize (int reserve, int commit)
1160 sprintf (b, "-stack 0x%x,0x%x ", reserve, commit);
1162 sprintf (b, "-stack 0x%x ", reserve);
1163 new_directive (xstrdup (b));
1166 /* append_import simply adds the given import definition to the global
1167 import_list. It is used by def_import. */
1170 append_import (const char *symbol_name, const char *dllname, int func_ordinal,
1171 const char *its_name)
1176 for (pq = &import_list; *pq != NULL; pq = &(*pq)->next)
1178 if (strcmp ((*pq)->dllname, dllname) == 0)
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);
1185 q->functail->its_name = (its_name ? xstrdup (its_name) : NULL);
1186 q->functail->next = NULL;
1192 q = xmalloc (sizeof (iheadtype));
1193 q->dllname = xstrdup (dllname);
1195 q->funchead = xmalloc (sizeof (ifunctype));
1196 q->functail = q->funchead;
1198 q->functail->name = xstrdup (symbol_name);
1199 q->functail->its_name = (its_name ? xstrdup (its_name) : NULL);
1200 q->functail->ord = func_ordinal;
1201 q->functail->next = NULL;
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
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>
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
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). */
1237 def_import (const char *app_name, const char *module, const char *dllext,
1238 const char *entry, int ord_val, const char *its_name)
1240 const char *application_name;
1244 application_name = entry;
1247 if (app_name != NULL)
1248 application_name = app_name;
1250 application_name = "";
1255 buf = (char *) alloca (strlen (module) + strlen (dllext) + 2);
1256 sprintf (buf, "%s.%s", module, dllext);
1260 append_import (application_name, module, ord_val, its_name);
1264 def_version (int major, int minor)
1266 printf ("VERSION %d.%d\n", major, minor);
1270 def_section (const char *name, int attr)
1285 sprintf (buf, "-attr %s %s", name, atts);
1286 new_directive (xstrdup (buf));
1293 def_section ("CODE", attr);
1299 def_section ("DATA", attr);
1302 /**********************************************************************/
1305 run (const char *what, char *args)
1308 int pid, wait_status;
1311 char *errmsg_fmt, *errmsg_arg;
1312 char *temp_base = choose_temp_base ();
1314 inform ("run: %s %s", what, args);
1316 /* Count the args */
1318 for (s = args; *s; s++)
1322 argv = alloca (sizeof (char *) * (i + 3));
1331 while (*s != ' ' && *s != 0)
1339 pid = pexecute (argv[0], (char * const *) argv, program_name, temp_base,
1340 &errmsg_fmt, &errmsg_arg, PEXECUTE_ONE | PEXECUTE_SEARCH);
1344 inform ("%s", strerror (errno));
1346 fatal (errmsg_fmt, errmsg_arg);
1349 pid = pwait (pid, & wait_status, 0);
1353 /* xgettext:c-format */
1354 fatal (_("wait: %s"), strerror (errno));
1356 else if (WIFSIGNALED (wait_status))
1358 /* xgettext:c-format */
1359 fatal (_("subprocess got fatal signal %d"), WTERMSIG (wait_status));
1361 else if (WIFEXITED (wait_status))
1363 if (WEXITSTATUS (wait_status) != 0)
1364 /* xgettext:c-format */
1365 non_fatal (_("%s exited with status %d"),
1366 what, WEXITSTATUS (wait_status));
1372 /* Look for a list of symbols to export in the .drectve section of
1373 ABFD. Pass each one to def_exports. */
1376 scan_drectve_symbols (bfd *abfd)
1384 /* Look for .drectve's */
1385 s = bfd_get_section_by_name (abfd, DRECTVE_SECTION_NAME);
1390 size = bfd_get_section_size (s);
1391 buf = xmalloc (size);
1393 bfd_get_section_contents (abfd, s, buf, 0, size);
1395 /* xgettext:c-format */
1396 inform (_("Sucking in info from %s section in %s"),
1397 DRECTVE_SECTION_NAME, bfd_get_filename (abfd));
1399 /* Search for -export: strings. The exported symbols can optionally
1400 have type tags (eg., -export:foo,data), so handle those as well.
1401 Currently only data tag is supported. */
1407 && CONST_STRNEQ (p, "-export:"))
1411 flagword flags = BSF_FUNCTION;
1414 /* Do we have a quoted export? */
1419 while (p < e && *p != '"')
1425 while (p < e && *p != ',' && *p != ' ' && *p != '-')
1428 c = xmalloc (p - name + 1);
1429 memcpy (c, name, p - name);
1431 /* Advance over trailing quote. */
1432 if (p < e && *p == '"')
1434 if (p < e && *p == ',') /* found type tag. */
1436 char *tag_start = ++p;
1437 while (p < e && *p != ' ' && *p != '-')
1439 if (CONST_STRNEQ (tag_start, "data"))
1440 flags &= ~BSF_FUNCTION;
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. */
1446 def_exports (c, 0, -1, 0, 0, ! (flags & BSF_FUNCTION), 0, NULL);
1448 if (add_stdcall_alias && strchr (c, '@'))
1450 int lead_at = (*c == '@') ;
1451 char *exported_name = xstrdup (c + lead_at);
1452 char *atsym = strchr (exported_name, '@');
1454 /* Note: stdcall alias symbols can never be data. */
1455 def_exports (exported_name, xstrdup (c), -1, 0, 0, 0, 0, NULL);
1464 /* Look through the symbols in MINISYMS, and add each one to list of
1465 symbols to export. */
1468 scan_filtered_symbols (bfd *abfd, void *minisyms, long symcount,
1472 bfd_byte *from, *fromend;
1474 store = bfd_make_empty_symbol (abfd);
1476 bfd_fatal (bfd_get_filename (abfd));
1478 from = (bfd_byte *) minisyms;
1479 fromend = from + symcount * size;
1480 for (; from < fromend; from += size)
1483 const char *symbol_name;
1485 sym = bfd_minisymbol_to_symbol (abfd, FALSE, from, store);
1487 bfd_fatal (bfd_get_filename (abfd));
1489 symbol_name = bfd_asymbol_name (sym);
1490 if (bfd_get_symbol_leading_char (abfd) == symbol_name[0])
1493 def_exports (xstrdup (symbol_name) , 0, -1, 0, 0,
1494 ! (sym->flags & BSF_FUNCTION), 0, NULL);
1496 if (add_stdcall_alias && strchr (symbol_name, '@'))
1498 int lead_at = (*symbol_name == '@');
1499 char *exported_name = xstrdup (symbol_name + lead_at);
1500 char *atsym = strchr (exported_name, '@');
1502 /* Note: stdcall alias symbols can never be data. */
1503 def_exports (exported_name, xstrdup (symbol_name), -1, 0, 0, 0, 0, NULL);
1508 /* Add a list of symbols to exclude. */
1511 add_excludes (const char *new_excludes)
1514 char *exclude_string;
1516 local_copy = xstrdup (new_excludes);
1518 exclude_string = strtok (local_copy, ",:");
1519 for (; exclude_string; exclude_string = strtok (NULL, ",:"))
1521 struct string_list *new_exclude;
1523 new_exclude = ((struct string_list *)
1524 xmalloc (sizeof (struct string_list)));
1525 new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 2);
1526 /* Don't add a leading underscore for fastcall symbols. */
1527 if (*exclude_string == '@')
1528 sprintf (new_exclude->string, "%s", exclude_string);
1530 sprintf (new_exclude->string, "%s%s", (!leading_underscore ? "" : "_"),
1532 new_exclude->next = excludes;
1533 excludes = new_exclude;
1535 /* xgettext:c-format */
1536 inform (_("Excluding symbol: %s"), exclude_string);
1542 /* See if STRING is on the list of symbols to exclude. */
1545 match_exclude (const char *string)
1547 struct string_list *excl_item;
1549 for (excl_item = excludes; excl_item; excl_item = excl_item->next)
1550 if (strcmp (string, excl_item->string) == 0)
1555 /* Add the default list of symbols to exclude. */
1558 set_default_excludes (void)
1560 add_excludes (default_excludes);
1563 /* Choose which symbols to export. */
1566 filter_symbols (bfd *abfd, void *minisyms, long symcount, unsigned int size)
1568 bfd_byte *from, *fromend, *to;
1571 store = bfd_make_empty_symbol (abfd);
1573 bfd_fatal (bfd_get_filename (abfd));
1575 from = (bfd_byte *) minisyms;
1576 fromend = from + symcount * size;
1577 to = (bfd_byte *) minisyms;
1579 for (; from < fromend; from += size)
1584 sym = bfd_minisymbol_to_symbol (abfd, FALSE, (const void *) from, store);
1586 bfd_fatal (bfd_get_filename (abfd));
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));
1594 keep = keep && ! match_exclude (sym->name);
1598 memcpy (to, from, size);
1603 return (to - (bfd_byte *) minisyms) / size;
1606 /* Export all symbols in ABFD, except for ones we were told not to
1610 scan_all_symbols (bfd *abfd)
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"))
1622 if (! (bfd_get_file_flags (abfd) & HAS_SYMS))
1624 /* xgettext:c-format */
1625 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1629 symcount = bfd_read_minisymbols (abfd, FALSE, &minisyms, &size);
1631 bfd_fatal (bfd_get_filename (abfd));
1635 /* xgettext:c-format */
1636 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
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. */
1643 symcount = filter_symbols (abfd, minisyms, symcount, size);
1644 scan_filtered_symbols (abfd, minisyms, symcount, size);
1649 /* Look at the object file to decide which symbols to export. */
1652 scan_open_obj_file (bfd *abfd)
1654 if (export_all_symbols)
1655 scan_all_symbols (abfd);
1657 scan_drectve_symbols (abfd);
1659 /* FIXME: we ought to read in and block out the base relocations. */
1661 /* xgettext:c-format */
1662 inform (_("Done reading %s"), bfd_get_filename (abfd));
1666 scan_obj_file (const char *filename)
1668 bfd * f = bfd_openr (filename, 0);
1671 /* xgettext:c-format */
1672 fatal (_("Unable to open object file: %s: %s"), filename, bfd_get_errmsg ());
1674 /* xgettext:c-format */
1675 inform (_("Scanning object file %s"), filename);
1677 if (bfd_check_format (f, bfd_archive))
1679 bfd *arfile = bfd_openr_next_archived_file (f, 0);
1682 if (bfd_check_format (arfile, bfd_object))
1683 scan_open_obj_file (arfile);
1685 arfile = bfd_openr_next_archived_file (f, arfile);
1688 #ifdef DLLTOOL_MCORE_ELF
1689 if (mcore_elf_out_file)
1690 inform (_("Cannot produce mcore-elf dll from archive file: %s"), filename);
1693 else if (bfd_check_format (f, bfd_object))
1695 scan_open_obj_file (f);
1697 #ifdef DLLTOOL_MCORE_ELF
1698 if (mcore_elf_out_file)
1699 mcore_elf_cache_filename (filename);
1709 dump_def_info (FILE *f)
1713 fprintf (f, "%s ", ASM_C);
1714 for (i = 0; oav[i]; i++)
1715 fprintf (f, "%s ", oav[i]);
1717 for (i = 0, exp = d_exports; exp; i++, exp = exp->next)
1719 fprintf (f, "%s %d = %s %s @ %d %s%s%s%s%s%s\n",
1725 exp->noname ? "NONAME " : "",
1726 exp->private ? "PRIVATE " : "",
1727 exp->constant ? "CONSTANT" : "",
1728 exp->data ? "DATA" : "",
1729 exp->its_name ? " ==" : "",
1730 exp->its_name ? exp->its_name : "");
1734 /* Generate the .exp file. */
1737 sfunc (const void *a, const void *b)
1739 if (*(const bfd_vma *) a == *(const bfd_vma *) b)
1742 return ((*(const bfd_vma *) a > *(const bfd_vma *) b) ? 1 : -1);
1746 flush_page (FILE *f, bfd_vma *need, bfd_vma page_addr, int on_page)
1750 /* Flush this page. */
1751 fprintf (f, "\t%s\t0x%08x\t%s Starting RVA for chunk\n",
1755 fprintf (f, "\t%s\t0x%x\t%s Size of block\n",
1757 (on_page * 2) + (on_page & 1) * 2 + 8,
1760 for (i = 0; i < on_page; i++)
1762 bfd_vma needed = need[i];
1766 if (!create_for_pep)
1768 /* Relocation via HIGHLOW. */
1769 needed = ((needed - page_addr) | 0x3000) & 0xffff;
1773 /* Relocation via DIR64. */
1774 needed = ((needed - page_addr) | 0xa000) & 0xffff;
1778 fprintf (f, "\t%s\t0x%lx\n", ASM_SHORT, (long) needed);
1783 fprintf (f, "\t%s\t0x%x\n", ASM_SHORT, 0 | 0x0000);
1792 inform (_("Adding exports to output file"));
1794 fprintf (output_def, ";");
1795 for (i = 0; oav[i]; i++)
1796 fprintf (output_def, " %s", oav[i]);
1798 fprintf (output_def, "\nEXPORTS\n");
1800 for (i = 0, exp = d_exports; exp; i++, exp = exp->next)
1802 char *quote = strchr (exp->name, '.') ? "\"" : "";
1803 char *res = cplus_demangle (exp->internal_name, DMGL_ANSI | DMGL_PARAMS);
1807 fprintf (output_def,";\t%s\n", res);
1811 if (strcmp (exp->name, exp->internal_name) == 0)
1813 fprintf (output_def, "\t%s%s%s @ %d%s%s%s%s%s\n",
1818 exp->noname ? " NONAME" : "",
1819 exp->private ? "PRIVATE " : "",
1820 exp->data ? " DATA" : "",
1821 exp->its_name ? " ==" : "",
1822 exp->its_name ? exp->its_name : "");
1826 char * quote1 = strchr (exp->internal_name, '.') ? "\"" : "";
1828 fprintf (output_def, "\t%s%s%s = %s%s%s @ %d%s%s%s%s%s\n",
1836 exp->noname ? " NONAME" : "",
1837 exp->private ? "PRIVATE " : "",
1838 exp->data ? " DATA" : "",
1839 exp->its_name ? " ==" : "",
1840 exp->its_name ? exp->its_name : "");
1844 inform (_("Added exports to output file"));
1847 /* generate_idata_ofile generates the portable assembly source code
1848 for the idata sections. It appends the source code to the end of
1852 generate_idata_ofile (FILE *filvar)
1860 if (import_list == NULL)
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");
1869 for (headptr = import_list; headptr != NULL; headptr = headptr->next)
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);
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);
1889 fprintf (filvar, "\n\t.section\t.idata$4\n");
1891 for (headptr = import_list; headptr != NULL; headptr = headptr->next)
1893 fprintf (filvar, "listone%d:\n", headindex);
1894 for (funcindex = 0; funcindex < headptr->nfuncs; funcindex++)
1897 fprintf (filvar, "\t%sfuncptr%d_%d%s\n%s\t0\n",
1898 ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER,
1901 fprintf (filvar, "\t%sfuncptr%d_%d%s\n",
1902 ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER);
1905 fprintf (filvar, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
1907 fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL terminating list. */
1911 fprintf (filvar, "\n\t.section\t.idata$5\n");
1913 for (headptr = import_list; headptr != NULL; headptr = headptr->next)
1915 fprintf (filvar, "listtwo%d:\n", headindex);
1916 for (funcindex = 0; funcindex < headptr->nfuncs; funcindex++)
1919 fprintf (filvar, "\t%sfuncptr%d_%d%s\n%s\t0\n",
1920 ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER,
1923 fprintf (filvar, "\t%sfuncptr%d_%d%s\n",
1924 ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER);
1927 fprintf (filvar, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
1929 fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL terminating list. */
1933 fprintf (filvar, "\n\t.section\t.idata$6\n");
1935 for (headptr = import_list; headptr != NULL; headptr = headptr->next)
1938 for (funcptr = headptr->funchead; funcptr != NULL;
1939 funcptr = funcptr->next)
1941 fprintf (filvar,"funcptr%d_%d:\n", headindex, funcindex);
1942 fprintf (filvar,"\t%s\t%d\n", ASM_SHORT,
1943 ((funcptr->ord) & 0xFFFF));
1944 fprintf (filvar,"\t%s\t\"%s\"\n", ASM_TEXT,
1945 (funcptr->its_name ? funcptr->its_name : funcptr->name));
1946 fprintf (filvar,"\t%s\t0\n", ASM_BYTE);
1952 fprintf (filvar, "\n\t.section\t.idata$7\n");
1954 for (headptr = import_list; headptr != NULL; headptr = headptr->next)
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);
1963 /* Assemble the specified file. */
1965 assemble_file (const char * source, const char * dest)
1969 cmd = (char *) alloca (strlen (ASM_SWITCHES) + strlen (as_flags)
1970 + strlen (source) + strlen (dest) + 50);
1972 sprintf (cmd, "%s %s -o %s %s", ASM_SWITCHES, as_flags, dest, source);
1985 /* xgettext:c-format */
1986 inform (_("Generating export file: %s"), exp_name);
1988 f = fopen (TMP_ASM, FOPEN_WT);
1990 /* xgettext:c-format */
1991 fatal (_("Unable to open temporary assembler file: %s"), TMP_ASM);
1993 /* xgettext:c-format */
1994 inform (_("Opened temporary file: %s"), TMP_ASM);
2000 fprintf (f, "\t.section .edata\n\n");
2001 fprintf (f, "\t%s 0 %s Allways 0\n", ASM_LONG, ASM_C);
2002 fprintf (f, "\t%s 0x%lx %s Time and date\n", ASM_LONG,
2003 (unsigned long) time(0), ASM_C);
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);
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",
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);
2017 fprintf (f, "\t%sanames%s %s Address of Name Pointer Table\n",
2018 ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C);
2020 fprintf (f, "\t%sanords%s %s Address of ordinals\n", ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C);
2022 fprintf (f, "name: %s \"%s\"\n", ASM_TEXT, dll_name);
2025 fprintf(f,"%s Export address Table\n", ASM_C);
2026 fprintf(f,"\t%s\n", ASM_ALIGN_LONG);
2027 fprintf (f, "afuncs:\n");
2030 for (exp = d_exports; exp; exp = exp->next)
2032 if (exp->ordinal != i)
2034 while (i < exp->ordinal)
2036 fprintf(f,"\t%s\t0\n", ASM_LONG);
2041 if (exp->forward == 0)
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);
2047 fprintf (f, "\t%s%s%s%s\t%s %d\n", ASM_RVA_BEFORE,
2048 ASM_PREFIX (exp->internal_name),
2049 exp->internal_name, ASM_RVA_AFTER, ASM_C, exp->ordinal);
2052 fprintf (f, "\t%sf%d%s\t%s %d\n", ASM_RVA_BEFORE,
2053 exp->forward, ASM_RVA_AFTER, ASM_C, exp->ordinal);
2057 fprintf (f,"%s Export Name Pointer Table\n", ASM_C);
2058 fprintf (f, "anames:\n");
2060 for (i = 0; (exp = d_exports_lexically[i]); i++)
2062 if (!exp->noname || show_allnames)
2063 fprintf (f, "\t%sn%d%s\n",
2064 ASM_RVA_BEFORE, exp->ordinal, ASM_RVA_AFTER);
2067 fprintf (f,"%s Export Ordinal Table\n", ASM_C);
2068 fprintf (f, "anords:\n");
2069 for (i = 0; (exp = d_exports_lexically[i]); i++)
2071 if (!exp->noname || show_allnames)
2072 fprintf (f, "\t%s %d\n", ASM_SHORT, exp->ordinal - d_low_ord);
2075 fprintf(f,"%s Export Name Table\n", ASM_C);
2076 for (i = 0; (exp = d_exports_lexically[i]); i++)
2078 if (!exp->noname || show_allnames)
2079 fprintf (f, "n%d: %s \"%s\"\n",
2080 exp->ordinal, ASM_TEXT,
2081 (exp->its_name ? exp->its_name : xlate (exp->name)));
2082 if (exp->forward != 0)
2083 fprintf (f, "f%d: %s \"%s\"\n",
2084 exp->forward, ASM_TEXT, exp->internal_name);
2089 fprintf (f, "\t.section %s\n", DRECTVE_SECTION_NAME);
2090 for (dl = a_list; dl; dl = dl->next)
2092 fprintf (f, "\t%s\t\"%s\"\n", ASM_TEXT, dl->text);
2098 fprintf (f, "\t.section .rdata\n");
2099 for (dl = d_list; dl; dl = dl->next)
2104 /* We don't output as ascii because there can
2105 be quote characters in the string. */
2107 for (p = dl->text; *p; p++)
2110 fprintf (f, "\t%s\t", ASM_BYTE);
2113 fprintf (f, "%d", *p);
2116 fprintf (f, ",0\n");
2130 /* Add to the output file a way of getting to the exported names
2131 without using the import library. */
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)
2138 /* We use a single underscore for MS compatibility, and a
2139 double underscore for backward compatibility with old
2141 if (create_compat_implib)
2142 fprintf (f, "\t%s\t__imp_%s\n", ASM_GLOBAL, exp->name);
2143 fprintf (f, "\t%s\t_imp_%s%s\n", ASM_GLOBAL,
2144 (!leading_underscore ? "" : "_"), exp->name);
2145 if (create_compat_implib)
2146 fprintf (f, "__imp_%s:\n", exp->name);
2147 fprintf (f, "_imp_%s%s:\n", (!leading_underscore ? "" : "_"), exp->name);
2148 fprintf (f, "\t%s\t%s\n", ASM_LONG, exp->name);
2152 /* Dump the reloc section if a base file is provided. */
2156 bfd_vma need[COFF_PAGE_SIZE];
2158 bfd_size_type numbytes;
2163 fprintf (f, "\t.section\t.init\n");
2164 fprintf (f, "lab:\n");
2166 fseek (base_file, 0, SEEK_END);
2167 numbytes = ftell (base_file);
2168 fseek (base_file, 0, SEEK_SET);
2169 copy = xmalloc (numbytes);
2170 if (fread (copy, 1, numbytes, base_file) < numbytes)
2171 fatal (_("failed to read the number of entries from base file"));
2172 num_entries = numbytes / sizeof (bfd_vma);
2175 fprintf (f, "\t.section\t.reloc\n");
2180 bfd_vma last = (bfd_vma) -1;
2181 qsort (copy, num_entries, sizeof (bfd_vma), sfunc);
2182 /* Delete duplicates */
2183 for (src = 0; src < num_entries; src++)
2185 if (last != copy[src])
2186 last = copy[dst++] = copy[src];
2190 page_addr = addr & PAGE_MASK; /* work out the page addr */
2192 for (j = 0; j < num_entries; j++)
2195 if ((addr & PAGE_MASK) != page_addr)
2197 flush_page (f, need, page_addr, on_page);
2199 page_addr = addr & PAGE_MASK;
2201 need[on_page++] = addr;
2203 flush_page (f, need, page_addr, on_page);
2205 /* fprintf (f, "\t%s\t0,0\t%s End\n", ASM_LONG, ASM_C);*/
2209 generate_idata_ofile (f);
2213 /* Assemble the file. */
2214 assemble_file (TMP_ASM, exp_name);
2216 if (dontdeltemps == 0)
2219 inform (_("Generated exports file"));
2223 xlate (const char *name)
2225 int lead_at = (*name == '@');
2226 int is_stdcall = (!lead_at && strchr (name, '@') != NULL);
2228 if (!lead_at && (add_underscore
2229 || (add_stdcall_underscore && is_stdcall)))
2231 char *copy = xmalloc (strlen (name) + 2);
2234 strcpy (copy + 1, name);
2243 /* PR 9766: Look for the last @ sign in the name. */
2244 p = strrchr (name, '@');
2245 if (p && ISDIGIT (p[1]))
2261 unsigned char *data;
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
2281 #define INIT_SEC_DATA(id, name, flags, align) \
2282 { id, name, flags, align, NULL, NULL, NULL, 0, NULL }
2283 static sinfo secdata[NSECS] =
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)
2296 /* Sections numbered to make the order the same as other PowerPC NT
2297 compilers. This also keeps funny alignment thingies from happening. */
2310 static sinfo secdata[NSECS] =
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}
2325 /* This is what we're trying to make. We generate the imp symbols with
2326 both single and double underscores, for compatibility.
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
2337 __imp_GetFileVersionInfoSizeW@8:
2340 # Import Lookup Table
2346 .asciz "GetFileVersionInfoSizeW"
2349 For the PowerPC, here's the variation on the above scheme:
2351 # Rather than a simple "jmp *", the code to get to the dll function
2354 lwz r11,[tocv]__imp_function_name(r2)
2355 # RELOC: 00000000 TOCREL16,TOCDEFN __imp_function_name
2363 make_label (const char *prefix, const char *name)
2365 int len = strlen (ASM_PREFIX (name)) + strlen (prefix) + strlen (name);
2366 char *copy = xmalloc (len + 1);
2368 strcpy (copy, ASM_PREFIX (name));
2369 strcat (copy, prefix);
2370 strcat (copy, name);
2375 make_imp_label (const char *prefix, const char *name)
2382 len = strlen (prefix) + strlen (name);
2383 copy = xmalloc (len + 1);
2384 strcpy (copy, prefix);
2385 strcat (copy, name);
2389 len = strlen (ASM_PREFIX (name)) + strlen (prefix) + strlen (name);
2390 copy = xmalloc (len + 1);
2391 strcpy (copy, prefix);
2392 strcat (copy, ASM_PREFIX (name));
2393 strcat (copy, name);
2399 make_one_lib_file (export_type *exp, int i, int delay)
2402 asymbol * exp_label;
2403 asymbol * iname = 0;
2405 asymbol * iname_lab;
2406 asymbol ** iname_lab_pp;
2407 asymbol ** iname_pp;
2416 asymbol * ptrs[NSECS + 4 + EXTRA + 1];
2417 flagword applicable;
2418 char * outname = xmalloc (strlen (TMP_STUB) + 10);
2422 sprintf (outname, "%s%05d.o", TMP_STUB, i);
2424 abfd = bfd_openw (outname, HOW_BFD_WRITE_TARGET);
2427 /* xgettext:c-format */
2428 fatal (_("bfd_open failed open stub file: %s: %s"),
2429 outname, bfd_get_errmsg ());
2431 /* xgettext:c-format */
2432 inform (_("Creating stub file: %s"), outname);
2434 bfd_set_format (abfd, bfd_object);
2435 bfd_set_arch_mach (abfd, HOW_BFD_ARCH, 0);
2438 if (machine == MARM_INTERWORK || machine == MTHUMB)
2439 bfd_set_private_flags (abfd, F_INTERWORK);
2442 applicable = bfd_applicable_section_flags (abfd);
2444 /* First make symbols for the sections. */
2445 for (i = 0; i < NSECS; i++)
2447 sinfo *si = secdata + i;
2451 si->sec = bfd_make_section_old_way (abfd, si->name);
2452 bfd_set_section_flags (abfd,
2454 si->flags & applicable);
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;
2463 ptrs[oidx] = si->sym;
2464 si->sympp = ptrs + oidx;
2473 exp_label = bfd_make_empty_symbol (abfd);
2474 exp_label->name = make_imp_label ("", exp->name);
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. */
2481 if (machine == MPPC)
2482 exp_label->section = secdata[RDATA].sec;
2485 exp_label->section = secdata[TEXT].sec;
2487 exp_label->flags = BSF_GLOBAL;
2488 exp_label->value = 0;
2491 if (machine == MTHUMB)
2492 bfd_coff_set_symbol_class (abfd, exp_label, C_THUMBEXTFUNC);
2494 ptrs[oidx++] = exp_label;
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)
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;
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;
2515 iname_lab = bfd_make_empty_symbol (abfd);
2517 iname_lab->name = head_label;
2518 iname_lab->section = (asection *) &bfd_und_section;
2519 iname_lab->flags = 0;
2520 iname_lab->value = 0;
2522 iname_pp = ptrs + oidx;
2523 if (create_compat_implib)
2524 ptrs[oidx++] = iname;
2525 ptrs[oidx++] = iname2;
2527 iname_lab_pp = ptrs + oidx;
2528 ptrs[oidx++] = iname_lab;
2531 /* The symbol referring to the code (.text). */
2533 asymbol *function_name;
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;
2541 fn_pp = ptrs + oidx;
2542 ptrs[oidx++] = function_name;
2545 /* The .toc symbol. */
2547 asymbol *toc_symbol;
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;
2555 toc_pp = ptrs + oidx;
2556 ptrs[oidx++] = toc_symbol;
2562 for (i = 0; i < NSECS; i++)
2564 sinfo *si = secdata + i;
2565 asection *sec = si->sec;
2566 arelent *rel, *rel2 = 0, *rel3 = 0;
2574 si->size = HOW_JTAB_SIZE;
2575 si->data = xmalloc (HOW_JTAB_SIZE);
2576 memcpy (si->data, HOW_JTAB, HOW_JTAB_SIZE);
2578 /* Add the reloc into idata$5. */
2579 rel = xmalloc (sizeof (arelent));
2581 rpp = xmalloc (sizeof (arelent *) * (delay ? 4 : 2));
2585 rel->address = HOW_JTAB_ROFF;
2590 rel2 = xmalloc (sizeof (arelent));
2592 rel2->address = HOW_JTAB_ROFF2;
2594 rel3 = xmalloc (sizeof (arelent));
2596 rel3->address = HOW_JTAB_ROFF3;
2601 if (machine == MPPC)
2603 rel->howto = bfd_reloc_type_lookup (abfd,
2604 BFD_RELOC_16_GOTOFF);
2605 rel->sym_ptr_ptr = iname_pp;
2607 else if (machine == MX86)
2609 rel->howto = bfd_reloc_type_lookup (abfd,
2610 BFD_RELOC_32_PCREL);
2611 rel->sym_ptr_ptr = iname_pp;
2615 rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
2616 rel->sym_ptr_ptr = secdata[IDATA5].sympp;
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;
2627 sec->orelocation = rpp;
2628 sec->reloc_count = delay ? 3 : 1;
2635 si->data = xmalloc (4);
2637 sec->reloc_count = 1;
2638 memset (si->data, 0, si->size);
2640 rel = xmalloc (sizeof (arelent));
2641 rpp = xmalloc (sizeof (arelent *) * 2);
2646 rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
2647 rel->sym_ptr_ptr = secdata[TEXT].sympp;
2648 sec->orelocation = rpp;
2651 /* else fall through */
2653 /* An idata$4 or idata$5 is one word long, and has an
2658 si->data = xmalloc (8);
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;
2673 sec->reloc_count = 1;
2674 memset (si->data, 0, si->size);
2675 rel = xmalloc (sizeof (arelent));
2676 rpp = xmalloc (sizeof (arelent *) * 2);
2681 rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA);
2682 rel->sym_ptr_ptr = secdata[IDATA6].sympp;
2683 sec->orelocation = rpp;
2688 si->data = xmalloc (4);
2693 si->data[0] = exp->ordinal ;
2694 si->data[1] = exp->ordinal >> 8;
2695 si->data[2] = exp->ordinal >> 16;
2700 sec->reloc_count = 1;
2701 memset (si->data, 0, si->size);
2702 rel = xmalloc (sizeof (arelent));
2703 rpp = xmalloc (sizeof (arelent *) * 2);
2708 rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA);
2709 rel->sym_ptr_ptr = secdata[IDATA6].sympp;
2710 sec->orelocation = rpp;
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;
2723 si->size = strlen (exp->its_name) + 3;
2725 si->size = strlen (xlate (exp->import_name)) + 3;
2726 si->data = xmalloc (si->size);
2727 si->data[0] = idx & 0xff;
2728 si->data[1] = idx >> 8;
2730 strcpy ((char *) si->data + 2, exp->its_name);
2732 strcpy ((char *) si->data + 2, xlate (exp->import_name));
2739 si->data = xmalloc (4);
2740 memset (si->data, 0, si->size);
2741 rel = xmalloc (sizeof (arelent));
2742 rpp = xmalloc (sizeof (arelent *) * 2);
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;
2755 /* The .pdata section is 5 words long.
2759 bfd_vma BeginAddress, [0x00]
2761 ExceptionHandler, [0x08]
2763 PrologEndAddress; [0x10]
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
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:
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.
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;
2782 /* Alignment must be set to 2**2 or you get extra stuff. */
2783 bfd_set_section_alignment(abfd, sec, 2);
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));
2795 /* Stick the toc reload instruction in the glue reloc. */
2796 bfd_put_32(abfd, ppc_glue_insn, (char *) &imglue->address);
2799 imglue->howto = bfd_reloc_type_lookup (abfd,
2800 BFD_RELOC_32_GOTOFF);
2801 imglue->sym_ptr_ptr = fn_pp;
2803 ba_rel->address = 0;
2805 ba_rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
2806 ba_rel->sym_ptr_ptr = fn_pp;
2808 bfd_put_32 (abfd, 0x18, si->data + 0x04);
2809 ea_rel->address = 4;
2811 ea_rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
2812 ea_rel->sym_ptr_ptr = fn_pp;
2814 /* Mark it as glue. */
2815 bfd_put_32 (abfd, 0x03, si->data + 0x0c);
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;
2824 sec->orelocation = rpp;
2825 sec->reloc_count = 4;
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. */
2835 si->data = xmalloc (8);
2836 memset (si->data, 0, si->size);
2838 rpp = xmalloc (sizeof (arelent *) * 3);
2839 rpp[0] = rel = xmalloc (sizeof (arelent));
2840 rpp[1] = xmalloc (sizeof (arelent));
2845 rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
2846 rel->sym_ptr_ptr = fn_pp;
2852 rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
2853 rel->sym_ptr_ptr = toc_pp;
2855 sec->orelocation = rpp;
2856 sec->reloc_count = 2;
2858 #endif /* DLLTOOL_PPC */
2864 /* Size up all the sections. */
2865 for (i = 0; i < NSECS; i++)
2867 sinfo *si = secdata + i;
2869 bfd_set_section_size (abfd, si->sec, si->size);
2870 bfd_set_section_vma (abfd, si->sec, vma);
2873 /* Write them out. */
2874 for (i = 0; i < NSECS; i++)
2876 sinfo *si = secdata + i;
2878 if (i == IDATA5 && no_idata5)
2881 if (i == IDATA4 && no_idata4)
2884 bfd_set_section_contents (abfd, si->sec,
2889 bfd_set_symtab (abfd, ptrs, oidx);
2891 abfd = bfd_openr (outname, HOW_BFD_READ_TARGET);
2893 /* xgettext:c-format */
2894 fatal (_("bfd_open failed reopen stub file: %s: %s"),
2895 outname, bfd_get_errmsg ());
2903 FILE *f = fopen (TMP_HEAD_S, FOPEN_WT);
2908 fatal (_("failed to open temporary head file: %s"), TMP_HEAD_S);
2912 fprintf (f, "%s IMAGE_IMPORT_DESCRIPTOR\n", ASM_C);
2913 fprintf (f, "\t.section\t.idata$2\n");
2915 fprintf (f,"\t%s\t%s\n", ASM_GLOBAL, head_label);
2917 fprintf (f, "%s:\n", head_label);
2919 fprintf (f, "\t%shname%s\t%sPtr to image import by name list\n",
2920 ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C);
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",
2931 fprintf (f, "\t%sfthunk%s\t%s pointer to firstthunk\n",
2933 ASM_RVA_AFTER, ASM_C);
2935 fprintf (f, "%sStuff for compatibility\n", ASM_C);
2939 fprintf (f, "\t.section\t.idata$5\n");
2940 if (use_nul_prefixed_import_tables)
2943 fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
2945 fprintf (f,"\t%s\t0\n", ASM_LONG);
2947 fprintf (f, "fthunk:\n");
2952 fprintf (f, "\t.section\t.idata$4\n");
2953 if (use_nul_prefixed_import_tables)
2956 fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
2958 fprintf (f,"\t%s\t0\n", ASM_LONG);
2960 fprintf (f, "hname:\n");
2965 assemble_file (TMP_HEAD_S, TMP_HEAD_O);
2967 abfd = bfd_openr (TMP_HEAD_O, HOW_BFD_READ_TARGET);
2969 /* xgettext:c-format */
2970 fatal (_("failed to open temporary head file: %s: %s"),
2971 TMP_HEAD_O, bfd_get_errmsg ());
2977 make_delay_head (void)
2979 FILE *f = fopen (TMP_HEAD_S, FOPEN_WT);
2984 fatal (_("failed to open temporary head file: %s"), TMP_HEAD_S);
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);
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);
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);
3019 fprintf (f, "%sStuff for compatibility\n", ASM_C);
3023 fprintf (f, "\t.section\t.idata$5\n");
3024 /* NULL terminating list. */
3025 #ifdef DLLTOOL_MX86_64
3026 fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
3028 fprintf (f,"\t%s\t0\n", ASM_LONG);
3030 fprintf (f, "__IAT_%s:\n", imp_name_lab);
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);
3041 fprintf (f, "\t.section\t.idata$2\n");
3045 assemble_file (TMP_HEAD_S, TMP_HEAD_O);
3047 abfd = bfd_openr (TMP_HEAD_O, HOW_BFD_READ_TARGET);
3049 /* xgettext:c-format */
3050 fatal (_("failed to open temporary head file: %s: %s"),
3051 TMP_HEAD_O, bfd_get_errmsg ());
3059 FILE *f = fopen (TMP_TAIL_S, FOPEN_WT);
3064 fatal (_("failed to open temporary tail file: %s"), TMP_TAIL_S);
3070 fprintf (f, "\t.section\t.idata$4\n");
3072 fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
3074 fprintf (f,"\t%s\t0\n", ASM_LONG); /* NULL terminating list. */
3079 fprintf (f, "\t.section\t.idata$5\n");
3081 fprintf (f,"\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
3083 fprintf (f,"\t%s\t0\n", ASM_LONG); /* NULL terminating list. */
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
3094 fprintf (f, "\t.section\t.idata$3\n");
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);
3103 /* Other PowerPC NT compilers use idata$6 for the dllname, so I
3104 do too. Original, huh? */
3105 fprintf (f, "\t.section\t.idata$6\n");
3107 fprintf (f, "\t.section\t.idata$7\n");
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);
3116 assemble_file (TMP_TAIL_S, TMP_TAIL_O);
3118 abfd = bfd_openr (TMP_TAIL_O, HOW_BFD_READ_TARGET);
3120 /* xgettext:c-format */
3121 fatal (_("failed to open temporary tail file: %s: %s"),
3122 TMP_TAIL_O, bfd_get_errmsg ());
3128 gen_lib_file (int delay)
3139 outarch = bfd_openw (imp_name, HOW_BFD_WRITE_TARGET);
3142 /* xgettext:c-format */
3143 fatal (_("Can't create .lib file: %s: %s"),
3144 imp_name, bfd_get_errmsg ());
3146 /* xgettext:c-format */
3147 inform (_("Creating library file: %s"), imp_name);
3149 bfd_set_format (outarch, bfd_archive);
3150 outarch->has_armap = 1;
3151 outarch->is_thin_archive = 0;
3153 /* Work out a reasonable size of things to put onto one line. */
3156 ar_head = make_delay_head ();
3160 ar_head = make_head ();
3162 ar_tail = make_tail();
3164 if (ar_head == NULL || ar_tail == NULL)
3167 for (i = 0; (exp = d_exports_lexically[i]); i++)
3170 /* Don't add PRIVATE entries to import lib. */
3173 n = make_one_lib_file (exp, i, delay);
3174 n->archive_next = head;
3176 if (ext_prefix_alias)
3178 export_type alias_exp;
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;
3183 alias_exp.its_name = exp->its_name;
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;
3193 n = make_one_lib_file (&alias_exp, i + PREFIX_ALIAS_BASE, delay);
3194 n->archive_next = head;
3199 /* Now stick them all into the archive. */
3200 ar_head->archive_next = head;
3201 ar_tail->archive_next = ar_head;
3204 if (! bfd_set_archive_head (outarch, head))
3205 bfd_fatal ("bfd_set_archive_head");
3207 if (! bfd_close (outarch))
3208 bfd_fatal (imp_name);
3210 while (head != NULL)
3212 bfd *n = head->archive_next;
3217 /* Delete all the temp files. */
3218 if (dontdeltemps == 0)
3220 unlink (TMP_HEAD_O);
3221 unlink (TMP_HEAD_S);
3222 unlink (TMP_TAIL_O);
3223 unlink (TMP_TAIL_S);
3226 if (dontdeltemps < 2)
3230 name = (char *) alloca (strlen (TMP_STUB) + 10);
3231 for (i = 0; (exp = d_exports_lexically[i]); i++)
3233 /* Don't delete non-existent stubs for PRIVATE entries. */
3236 sprintf (name, "%s%05d.o", TMP_STUB, i);
3237 if (unlink (name) < 0)
3238 /* xgettext:c-format */
3239 non_fatal (_("cannot delete %s: %s"), name, strerror (errno));
3240 if (ext_prefix_alias)
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));
3250 inform (_("Created lib file"));
3253 /* Append a copy of data (cast to char *) to list. */
3256 dll_name_list_append (dll_name_list_type * list, bfd_byte * data)
3258 dll_name_list_node_type * entry;
3260 /* Error checking. */
3261 if (! list || ! list->tail)
3264 /* Allocate new node. */
3265 entry = ((dll_name_list_node_type *)
3266 xmalloc (sizeof (dll_name_list_node_type)));
3268 /* Initialize its values. */
3269 entry->dllname = xstrdup ((char *) data);
3272 /* Add to tail, and move tail. */
3273 list->tail->next = entry;
3277 /* Count the number of entries in list. */
3280 dll_name_list_count (dll_name_list_type * list)
3282 dll_name_list_node_type * p;
3285 /* Error checking. */
3286 if (! list || ! list->head)
3291 while (p && p->next)
3299 /* Print each entry in list to stdout. */
3302 dll_name_list_print (dll_name_list_type * list)
3304 dll_name_list_node_type * p;
3306 /* Error checking. */
3307 if (! list || ! list->head)
3312 while (p && p->next && p->next->dllname && *(p->next->dllname))
3314 printf ("%s\n", p->next->dllname);
3319 /* Free all entries in list, and list itself. */
3322 dll_name_list_free (dll_name_list_type * list)
3326 dll_name_list_free_contents (list->head);
3333 /* Recursive function to free all nodes entry->next->next...
3334 as well as entry itself. */
3337 dll_name_list_free_contents (dll_name_list_node_type * entry)
3343 dll_name_list_free_contents (entry->next);
3348 free (entry->dllname);
3349 entry->dllname = NULL;
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
3360 static dll_name_list_type *
3361 dll_name_list_create (void)
3363 /* Allocate list. */
3364 dll_name_list_type * list = xmalloc (sizeof (dll_name_list_type));
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;
3371 /* Bookkeeping for empty list. */
3372 list->tail = list->head;
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
3384 identify_member_contains_symname (bfd * abfd,
3385 bfd * archive_bfd ATTRIBUTE_UNUSED,
3388 long storage_needed;
3389 asymbol ** symbol_table;
3390 long number_of_symbols;
3392 symname_search_data_type * search_data = (symname_search_data_type *) obj;
3394 /* If we already found the symbol in a different member,
3396 if (search_data->found)
3399 storage_needed = bfd_get_symtab_upper_bound (abfd);
3400 if (storage_needed <= 0)
3403 symbol_table = xmalloc (storage_needed);
3404 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
3405 if (number_of_symbols < 0)
3407 free (symbol_table);
3411 for (i = 0; i < number_of_symbols; i++)
3413 if (strncmp (symbol_table[i]->name,
3414 search_data->symname,
3415 strlen (search_data->symname)) == 0)
3417 search_data->found = TRUE;
3421 free (symbol_table);
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.
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.
3438 Finally, print them all to stdout. (If --identify-strict, an error is
3439 reported if more than one match was found). */
3442 identify_dll_for_implib (void)
3446 identify_data_type identify_data;
3447 symname_search_data_type search_data;
3449 /* Initialize identify_data. */
3450 identify_data.list = dll_name_list_create ();
3451 identify_data.ms_style_implib = FALSE;
3453 /* Initialize search_data. */
3454 search_data.symname = "__NULL_IMPORT_DESCRIPTOR";
3455 search_data.found = FALSE;
3459 abfd = bfd_openr (identify_imp_name, 0);
3461 /* xgettext:c-format */
3462 fatal (_("Can't open .lib file: %s: %s"),
3463 identify_imp_name, bfd_get_errmsg ());
3465 if (! bfd_check_format (abfd, bfd_archive))
3467 if (! bfd_close (abfd))
3468 bfd_fatal (identify_imp_name);
3470 fatal (_("%s is not a library"), identify_imp_name);
3473 /* Detect if this a Microsoft import library. */
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;
3480 /* Rewind the bfd. */
3481 if (! bfd_close (abfd))
3482 bfd_fatal (identify_imp_name);
3483 abfd = bfd_openr (identify_imp_name, 0);
3485 bfd_fatal (identify_imp_name);
3487 if (!bfd_check_format (abfd, bfd_archive))
3489 if (!bfd_close (abfd))
3490 bfd_fatal (identify_imp_name);
3492 fatal (_("%s is not a library"), identify_imp_name);
3495 /* Now search for the dll name. */
3496 identify_search_archive (abfd,
3497 identify_search_member,
3498 (void *)(& identify_data));
3500 if (! bfd_close (abfd))
3501 bfd_fatal (identify_imp_name);
3503 count = dll_name_list_count (identify_data.list);
3506 if (identify_strict && count > 1)
3508 dll_name_list_free (identify_data.list);
3509 identify_data.list = NULL;
3510 fatal (_("Import library `%s' specifies two or more dlls"),
3513 dll_name_list_print (identify_data.list);
3514 dll_name_list_free (identify_data.list);
3515 identify_data.list = NULL;
3519 dll_name_list_free (identify_data.list);
3520 identify_data.list = NULL;
3521 fatal (_("Unable to determine dll name for `%s' (not an import library?)"),
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) */
3531 identify_search_archive (bfd * abfd,
3532 void (* operation) (bfd *, bfd *, void *),
3533 void * user_storage)
3535 bfd * arfile = NULL;
3536 bfd * last_arfile = NULL;
3541 arfile = bfd_openr_next_archived_file (abfd, arfile);
3545 if (bfd_get_error () != bfd_error_no_more_archived_files)
3546 bfd_fatal (bfd_get_filename (abfd));
3550 if (bfd_check_format_matches (arfile, bfd_object, &matching))
3551 (*operation) (arfile, abfd, user_storage);
3554 bfd_nonfatal (bfd_get_filename (arfile));
3558 if (last_arfile != NULL)
3559 bfd_close (last_arfile);
3561 last_arfile = arfile;
3564 if (last_arfile != NULL)
3566 bfd_close (last_arfile);
3570 /* Call the identify_search_section() function for each section of this
3574 identify_search_member (bfd *abfd,
3575 bfd *archive_bfd ATTRIBUTE_UNUSED,
3578 bfd_map_over_sections (abfd, identify_search_section, obj);
3581 /* This predicate returns true if section->name matches the desired value.
3582 By default, this is .idata$7 (.idata$6 on PPC, or if the import
3583 library is ms-style). */
3586 identify_process_section_p (asection * section, bfd_boolean ms_style_implib)
3588 static const char * SECTION_NAME =
3590 /* dllname is stored in idata$6 on PPC */
3595 static const char * MS_SECTION_NAME = ".idata$6";
3597 const char * section_name =
3598 (ms_style_implib ? MS_SECTION_NAME : SECTION_NAME);
3600 if (strcmp (section_name, section->name) == 0)
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
3607 -- then add the contents of the section to obj->list. */
3610 identify_search_section (bfd * abfd, asection * section, void * obj)
3613 bfd_size_type datasize;
3614 identify_data_type * identify_data = (identify_data_type *)obj;
3615 bfd_boolean ms_style = identify_data->ms_style_implib;
3617 if ((section->flags & SEC_HAS_CONTENTS) == 0)
3620 if (! identify_process_section_p (section, ms_style))
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
3626 if (!ms_style && ((section->flags & SEC_RELOC) == SEC_RELOC))
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. */
3632 if (ms_style && ((section->flags & SEC_DATA) == 0))
3635 if ((datasize = bfd_section_size (abfd, section)) == 0)
3638 data = (bfd_byte *) xmalloc (datasize + 1);
3641 bfd_get_section_contents (abfd, section, data, 0, datasize);
3642 data[datasize] = '\0';
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
3649 also fail to record a valid dll name if the dllname uses
3650 a multibyte or unicode character set (is that valid?).
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])))
3660 dll_name_list_append (identify_data->list, data);
3665 /* Run through the information gathered from the .o files and the
3666 .def file and work out the best stuff. */
3669 pfunc (const void *a, const void *b)
3671 export_type *ap = *(export_type **) a;
3672 export_type *bp = *(export_type **) b;
3674 if (ap->ordinal == bp->ordinal)
3677 /* Unset ordinals go to the bottom. */
3678 if (ap->ordinal == -1)
3680 if (bp->ordinal == -1)
3682 return (ap->ordinal - bp->ordinal);
3686 nfunc (const void *a, const void *b)
3688 export_type *ap = *(export_type **) a;
3689 export_type *bp = *(export_type **) b;
3690 const char *an = ap->name;
3691 const char *bn = bp->name;
3698 an = (an[0] == '@') ? an + 1 : an;
3699 bn = (bn[0] == '@') ? bn + 1 : bn;
3702 return (strcmp (an, bn));
3706 remove_null_names (export_type **ptr)
3711 for (dst = src = 0; src < d_nfuncs; src++)
3715 ptr[dst] = ptr[src];
3723 process_duplicates (export_type **d_export_vec)
3731 /* Remove duplicates. */
3732 qsort (d_export_vec, d_nfuncs, sizeof (export_type *), nfunc);
3734 for (i = 0; i < d_nfuncs - 1; i++)
3736 if (strcmp (d_export_vec[i]->name,
3737 d_export_vec[i + 1]->name) == 0)
3739 export_type *a = d_export_vec[i];
3740 export_type *b = d_export_vec[i + 1];
3744 /* xgettext:c-format */
3745 inform (_("Warning, ignoring duplicate EXPORT %s %d,%d"),
3746 a->name, a->ordinal, b->ordinal);
3748 if (a->ordinal != -1
3749 && b->ordinal != -1)
3750 /* xgettext:c-format */
3751 fatal (_("Error, duplicate EXPORT with ordinals: %s"),
3754 /* Merge attributes. */
3755 b->ordinal = a->ordinal > 0 ? a->ordinal : b->ordinal;
3756 b->constant |= a->constant;
3757 b->noname |= a->noname;
3759 d_export_vec[i] = 0;
3762 remove_null_names (d_export_vec);
3766 /* Count the names. */
3767 for (i = 0; i < d_nfuncs; i++)
3768 if (!d_export_vec[i]->noname)
3773 fill_ordinals (export_type **d_export_vec)
3780 qsort (d_export_vec, d_nfuncs, sizeof (export_type *), pfunc);
3782 /* Fill in the unset ordinals with ones from our range. */
3783 ptr = (char *) xmalloc (size);
3785 memset (ptr, 0, size);
3787 /* Mark in our large vector all the numbers that are taken. */
3788 for (i = 0; i < d_nfuncs; i++)
3790 if (d_export_vec[i]->ordinal != -1)
3792 ptr[d_export_vec[i]->ordinal] = 1;
3794 if (lowest == -1 || d_export_vec[i]->ordinal < lowest)
3795 lowest = d_export_vec[i]->ordinal;
3799 /* Start at 1 for compatibility with MS toolchain. */
3803 /* Now fill in ordinals where the user wants us to choose. */
3804 for (i = 0; i < d_nfuncs; i++)
3806 if (d_export_vec[i]->ordinal == -1)
3810 /* First try within or after any user supplied range. */
3811 for (j = lowest; j < size; j++)
3815 d_export_vec[i]->ordinal = j;
3819 /* Then try before the range. */
3820 for (j = lowest; j >0; j--)
3824 d_export_vec[i]->ordinal = j;
3834 qsort (d_export_vec, d_nfuncs, sizeof (export_type *), pfunc);
3836 /* Work out the lowest and highest ordinal numbers. */
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;
3849 /* First work out the minimum ordinal chosen. */
3854 export_type **d_export_vec = xmalloc (sizeof (export_type *) * d_nfuncs);
3856 inform (_("Processing definitions"));
3858 for (i = 0, exp = d_exports; exp; i++, exp = exp->next)
3859 d_export_vec[i] = exp;
3861 process_duplicates (d_export_vec);
3862 fill_ordinals (d_export_vec);
3864 /* Put back the list in the new order. */
3866 for (i = d_nfuncs - 1; i >= 0; i--)
3868 d_export_vec[i]->next = d_exports;
3869 d_exports = d_export_vec[i];
3872 /* Build list in alpha order. */
3873 d_exports_lexically = (export_type **)
3874 xmalloc (sizeof (export_type *) * (d_nfuncs + 1));
3876 for (i = 0, exp = d_exports; exp; i++, exp = exp->next)
3877 d_exports_lexically[i] = exp;
3879 d_exports_lexically[i] = 0;
3881 qsort (d_exports_lexically, i, sizeof (export_type *), nfunc);
3883 /* Fill exp entries with their hint values. */
3884 for (i = 0; i < d_nfuncs; i++)
3885 if (!d_exports_lexically[i]->noname || show_allnames)
3886 d_exports_lexically[i]->hint = hint++;
3888 inform (_("Processed definitions"));
3892 usage (FILE *file, int status)
3894 /* xgetext:c-format */
3895 fprintf (file, _("Usage %s <option(s)> <object-file(s)>\n"), program_name);
3896 /* xgetext:c-format */
3897 fprintf (file, _(" -m --machine <machine> Create as DLL for <machine>. [default: %s]\n"), mname);
3898 fprintf (file, _(" possible <machine>: arm[_interwork], i386, mcore[-elf]{-le|-be}, ppc, thumb\n"));
3899 fprintf (file, _(" -e --output-exp <outname> Generate an export file.\n"));
3900 fprintf (file, _(" -l --output-lib <outname> Generate an interface library.\n"));
3901 fprintf (file, _(" -y --output-delaylib <outname> Create a delay-import library.\n"));
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"));
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"));
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"));
3913 fprintf (file, _(" --use-nul-prefixed-import-tables Use zero prefixed idata$4 and idata$5.\n"));
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"));
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"));
3918 fprintf (file, _(" -k --kill-at Kill @<n> from exported names.\n"));
3919 fprintf (file, _(" -A --add-stdcall-alias Add aliases without @<n>.\n"));
3920 fprintf (file, _(" -p --ext-prefix-alias <prefix> Add aliases with <prefix>.\n"));
3921 fprintf (file, _(" -S --as <name> Use <name> for assembler.\n"));
3922 fprintf (file, _(" -f --as-flags <flags> Pass <flags> to the assembler.\n"));
3923 fprintf (file, _(" -C --compat-implib Create backward compatible import library.\n"));
3924 fprintf (file, _(" -n --no-delete Keep temp files (repeat for extra preservation).\n"));
3925 fprintf (file, _(" -t --temp-prefix <prefix> Use <prefix> to construct temp file names.\n"));
3926 fprintf (file, _(" -I --identify <implib> Report the name of the DLL associated with <implib>.\n"));
3927 fprintf (file, _(" --identify-strict Causes --identify to report error when multiple DLLs.\n"));
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"));
3931 fprintf (file, _(" @<file> Read options from <file>.\n"));
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"));
3937 if (REPORT_BUGS_TO[0] && status == 0)
3938 fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
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)
3946 #define OPTION_ADD_STDCALL_UNDERSCORE (OPTION_NO_DEFAULT_EXCLUDES + 1)
3947 #define OPTION_USE_NUL_PREFIXED_IMPORT_TABLES \
3948 (OPTION_ADD_STDCALL_UNDERSCORE + 1)
3949 #define OPTION_IDENTIFY_STRICT (OPTION_USE_NUL_PREFIXED_IMPORT_TABLES + 1)
3950 #define OPTION_NO_LEADING_UNDERSCORE (OPTION_IDENTIFY_STRICT + 1)
3951 #define OPTION_LEADING_UNDERSCORE (OPTION_NO_LEADING_UNDERSCORE + 1)
3953 static const struct option long_options[] =
3955 {"no-delete", no_argument, NULL, 'n'},
3956 {"dllname", required_argument, NULL, 'D'},
3957 {"no-idata4", no_argument, NULL, 'x'},
3958 {"no-idata5", no_argument, NULL, 'c'},
3959 {"use-nul-prefixed-import-tables", no_argument, NULL,
3960 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
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'},
3968 {"def", required_argument, NULL, 'd'}, /* for compatibility with older versions */
3969 {"input-def", required_argument, NULL, 'd'},
3970 {"add-underscore", no_argument, NULL, 'U'},
3971 {"add-stdcall-underscore", no_argument, NULL, OPTION_ADD_STDCALL_UNDERSCORE},
3972 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
3973 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
3974 {"kill-at", no_argument, NULL, 'k'},
3975 {"add-stdcall-alias", no_argument, NULL, 'A'},
3976 {"ext-prefix-alias", required_argument, NULL, 'p'},
3977 {"identify", required_argument, NULL, 'I'},
3978 {"identify-strict", no_argument, NULL, OPTION_IDENTIFY_STRICT},
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'},
3987 {"mcore-elf", required_argument, NULL, 'M'},
3988 {"compat-implib", no_argument, NULL, 'C'},
3989 {"temp-prefix", required_argument, NULL, 't'},
3990 {"output-delaylib", required_argument, NULL, 'y'},
3994 int main (int, char **);
3997 main (int ac, char **av)
4002 program_name = av[0];
4005 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
4006 setlocale (LC_MESSAGES, "");
4008 #if defined (HAVE_SETLOCALE)
4009 setlocale (LC_CTYPE, "");
4011 bindtextdomain (PACKAGE, LOCALEDIR);
4012 textdomain (PACKAGE);
4014 expandargv (&ac, &av);
4016 while ((c = getopt_long (ac, av,
4017 #ifdef DLLTOOL_MCORE_ELF
4018 "m:e:l:aD:d:z:b:xp:cCuUkAS:f:nI:vVHhM:L:F:",
4020 "m:e:l:y:aD:d:z:b:xp:cCuUkAS:f:nI:vVHh",
4027 case OPTION_EXPORT_ALL_SYMS:
4028 export_all_symbols = TRUE;
4030 case OPTION_NO_EXPORT_ALL_SYMS:
4031 export_all_symbols = FALSE;
4033 case OPTION_EXCLUDE_SYMS:
4034 add_excludes (optarg);
4036 case OPTION_NO_DEFAULT_EXCLUDES:
4037 do_default_excludes = FALSE;
4039 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
4040 use_nul_prefixed_import_tables = TRUE;
4042 case OPTION_ADD_STDCALL_UNDERSCORE:
4043 add_stdcall_underscore = 1;
4045 case OPTION_NO_LEADING_UNDERSCORE:
4046 leading_underscore = 0;
4048 case OPTION_LEADING_UNDERSCORE:
4049 leading_underscore = 1;
4051 case OPTION_IDENTIFY_STRICT:
4052 identify_strict = 1;
4064 tmp_prefix = optarg;
4070 /* Ignored for compatibility. */
4077 output_def = fopen (optarg, FOPEN_WT);
4080 dll_name = (char*) lbasename (optarg);
4081 if (dll_name != optarg)
4082 non_fatal (_("Path components stripped from dllname, '%s'."),
4099 identify_imp_name = optarg;
4105 print_version (program_name);
4114 add_stdcall_alias = 1;
4117 ext_prefix_alias = optarg;
4126 base_file = fopen (optarg, FOPEN_RB);
4129 /* xgettext:c-format */
4130 fatal (_("Unable to open base-file: %s"), optarg);
4133 #ifdef DLLTOOL_MCORE_ELF
4135 mcore_elf_out_file = optarg;
4138 mcore_elf_linker = optarg;
4141 mcore_elf_linker_flags = optarg;
4145 create_compat_implib = 1;
4148 delayimp_name = optarg;
4157 tmp_prefix = prefix_encode ("d", getpid ());
4159 for (i = 0; mtable[i].type; i++)
4160 if (strcmp (mtable[i].type, mname) == 0)
4163 if (!mtable[i].type)
4164 /* xgettext:c-format */
4165 fatal (_("Machine '%s' not supported"), mname);
4169 /* Check if we generated PE+. */
4170 create_for_pep = strcmp (mname, "i386:x86-64") == 0;
4173 /* Check the default underscore */
4174 int u = leading_underscore; /* Underscoring mode. -1 for use default. */
4176 bfd_get_target_info (mtable[machine].how_bfd_target, NULL,
4179 leading_underscore = (u != 0 ? TRUE : FALSE);
4182 if (!dll_name && exp_name)
4184 /* If we are inferring dll_name from exp_name,
4185 strip off any path components, without emitting
4187 const char* exp_basename = lbasename (exp_name);
4188 const int len = strlen (exp_basename) + 5;
4189 dll_name = xmalloc (len);
4190 strcpy (dll_name, exp_basename);
4191 strcat (dll_name, ".dll");
4192 dll_name_set_by_exp_name = 1;
4195 if (as_name == NULL)
4196 as_name = deduce_name ("as");
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)
4202 do_default_excludes = FALSE;
4204 if (do_default_excludes)
4205 set_default_excludes ();
4208 process_def_file (def_file);
4213 firstarg = av[optind];
4214 scan_obj_file (av[optind]);
4225 /* Make imp_name safe for use as a label. */
4228 imp_name_lab = xstrdup (imp_name);
4229 for (p = imp_name_lab; *p; p++)
4234 head_label = make_label("_head_", imp_name_lab);
4240 /* Make delayimp_name safe for use as a label. */
4243 if (mtable[machine].how_dljtab == 0)
4245 inform (_("Warning, machine type (%d) not supported for "
4246 "delayimport."), machine);
4251 imp_name = delayimp_name;
4252 imp_name_lab = xstrdup (imp_name);
4253 for (p = imp_name_lab; *p; p++)
4258 head_label = make_label("__tailMerge_", imp_name_lab);
4266 if (identify_imp_name)
4268 identify_dll_for_implib ();
4271 #ifdef DLLTOOL_MCORE_ELF
4272 if (mcore_elf_out_file)
4273 mcore_elf_gen_out_file ();
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
4285 look_for_prog (const char *prog_name, const char *prefix, int end_prefix)
4290 cmd = xmalloc (strlen (prefix)
4291 + strlen (prog_name)
4292 #ifdef HAVE_EXECUTABLE_SUFFIX
4293 + strlen (EXECUTABLE_SUFFIX)
4296 strcpy (cmd, prefix);
4298 sprintf (cmd + end_prefix, "%s", prog_name);
4300 if (strchr (cmd, '/') != NULL)
4304 found = (stat (cmd, &s) == 0
4305 #ifdef HAVE_EXECUTABLE_SUFFIX
4306 || stat (strcat (cmd, EXECUTABLE_SUFFIX), &s) == 0
4312 /* xgettext:c-format */
4313 inform (_("Tried file: %s"), cmd);
4319 /* xgettext:c-format */
4320 inform (_("Using file: %s"), cmd);
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
4328 run "i386-pe-as" or "ppc-pe-ld".
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.
4333 Given, argv[0] = /usr/local/bin/i586-cygwin32-dlltool,
4334 deduce_name("as") uses the following search order:
4336 /usr/local/bin/i586-cygwin32-as
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.
4343 Given, argv[0] = i586-cygwin32-dlltool, it will not even try "as"
4344 as the fallback, but rather return i586-cygwin32-as.
4346 Oh, and given, argv[0] = dlltool, it'll return "as".
4348 Returns a dynamically allocated string. */
4351 deduce_name (const char *prog_name)
4354 char *dash, *slash, *cp;
4358 for (cp = program_name; *cp != '\0'; ++cp)
4363 #if defined(__DJGPP__) || defined (__CYGWIN__) || defined(__WIN32__)
4364 *cp == ':' || *cp == '\\' ||
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);
4382 if (slash != NULL && cmd == NULL)
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);
4391 /* Just return PROG_NAME as is. */
4392 cmd = xstrdup (prog_name);
4398 #ifdef DLLTOOL_MCORE_ELF
4399 typedef struct fname_cache
4401 const char * filename;
4402 struct fname_cache * next;
4406 static fname_cache fnames;
4409 mcore_elf_cache_filename (const char * filename)
4415 while (ptr->next != NULL)
4418 ptr->filename = filename;
4419 ptr->next = (fname_cache *) malloc (sizeof (fname_cache));
4420 if (ptr->next != NULL)
4421 ptr->next->next = NULL;
4424 #define MCORE_ELF_TMP_OBJ "mcoreelf.o"
4425 #define MCORE_ELF_TMP_EXP "mcoreelf.exp"
4426 #define MCORE_ELF_TMP_LIB "mcoreelf.lib"
4429 mcore_elf_gen_out_file (void)
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. */
4438 ds = dyn_string_new (100);
4439 dyn_string_append_cstr (ds, "-r ");
4441 if (mcore_elf_linker_flags != NULL)
4442 dyn_string_append_cstr (ds, mcore_elf_linker_flags);
4444 while (ptr->next != NULL)
4446 dyn_string_append_cstr (ds, ptr->filename);
4447 dyn_string_append_cstr (ds, " ");
4452 dyn_string_append_cstr (ds, "-o ");
4453 dyn_string_append_cstr (ds, MCORE_ELF_TMP_OBJ);
4455 if (mcore_elf_linker == NULL)
4456 mcore_elf_linker = deduce_name ("ld");
4458 run (mcore_elf_linker, ds->s);
4460 dyn_string_delete (ds);
4462 /* Step two. Create a .exp file and a .lib file from the temporary file.
4463 Do this by recursively invoking dlltool... */
4464 ds = dyn_string_new (100);
4466 dyn_string_append_cstr (ds, "-S ");
4467 dyn_string_append_cstr (ds, as_name);
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);
4477 dyn_string_append_cstr (ds, " -v");
4481 dyn_string_append_cstr (ds, " -n");
4483 if (dontdeltemps > 1)
4484 dyn_string_append_cstr (ds, " -n");
4487 /* XXX - FIME: ought to check/copy other command line options as well. */
4488 run (program_name, ds->s);
4490 dyn_string_delete (ds);
4492 /* Step four. Feed the .exp and object files to ld -shared to create the dll. */
4493 ds = dyn_string_new (100);
4495 dyn_string_append_cstr (ds, "-shared ");
4497 if (mcore_elf_linker_flags)
4498 dyn_string_append_cstr (ds, mcore_elf_linker_flags);
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);
4507 run (mcore_elf_linker, ds->s);
4509 dyn_string_delete (ds);
4511 if (dontdeltemps == 0)
4512 unlink (MCORE_ELF_TMP_EXP);
4514 if (dontdeltemps < 2)
4515 unlink (MCORE_ELF_TMP_OBJ);
4517 #endif /* DLLTOOL_MCORE_ELF */