1 /* dllwrap.c -- wrapper for DLLTOOL and GCC to generate PE style DLLs
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Mumit Khan (khan@xraylith.wisc.edu).
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 2 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., 59 Temple Place - Suite 330, Boston, MA
22 /* AIX requires this to be the first thing in the file. */
34 #include "libiberty.h"
37 #include "dyn-string.h"
43 #ifdef ANSI_PROTOTYPES
49 #ifdef HAVE_SYS_WAIT_H
51 #else /* ! HAVE_SYS_WAIT_H */
52 #if ! defined (_WIN32) || defined (__CYGWIN32__)
54 #define WIFEXITED(w) (((w)&0377) == 0)
57 #define WIFSIGNALED(w) (((w)&0377) != 0177 && ((w)&~0377) == 0)
60 #define WTERMSIG(w) ((w) & 0177)
63 #define WEXITSTATUS(w) (((w) >> 8) & 0377)
65 #else /* defined (_WIN32) && ! defined (__CYGWIN32__) */
67 #define WIFEXITED(w) (((w) & 0xff) == 0)
70 #define WIFSIGNALED(w) (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f)
73 #define WTERMSIG(w) ((w) & 0x7f)
76 #define WEXITSTATUS(w) (((w) & 0xff00) >> 8)
78 #endif /* defined (_WIN32) && ! defined (__CYGWIN32__) */
79 #endif /* ! HAVE_SYS_WAIT_H */
81 static char *program_version
= "0.2.4";
82 static char *driver_name
= NULL
;
83 static char *cygwin_driver_flags
=
84 "-Wl,--dll -nostartfiles";
85 static char *mingw32_driver_flags
= "-mdll";
86 static char *generic_driver_flags
= "-Wl,--dll";
88 static char *entry_point
;
90 static char *dlltool_name
= NULL
;
92 static char *target
= TARGET
;
101 static target_type which_target
= UNKNOWN_TARGET
;
103 static int dontdeltemps
= 0;
104 static int dry_run
= 0;
106 static char *program_name
;
108 static int verbose
= 0;
110 static char *dll_file_name
;
111 static char *dll_name
;
112 static char *base_file_name
;
113 static char *exp_file_name
;
114 static char *def_file_name
;
115 static int delete_base_file
= 1;
116 static int delete_exp_file
= 1;
117 static int delete_def_file
= 1;
119 static int run
PARAMS ((const char *, char *));
120 static void usage
PARAMS ((FILE *, int));
121 static void display
PARAMS ((const char *, va_list));
122 static void inform
PARAMS ((const char *, ...));
123 static char *look_for_prog
PARAMS ((const char *, const char *, int));
124 static char *deduce_name
PARAMS ((const char *));
125 static void delete_temp_files
PARAMS ((void));
126 static void cleanup_and_exit
PARAMS ((int status
));
128 /**********************************************************************/
130 /* Please keep the following 4 routines in sync with dlltool.c:
135 It's not worth the hassle to break these out since dllwrap will
136 (hopefully) soon be retired in favor of `ld --shared. */
139 display (message
, args
)
140 const char * message
;
143 if (program_name
!= NULL
)
144 fprintf (stderr
, "%s: ", program_name
);
146 vfprintf (stderr
, message
, args
);
148 if (message
[strlen (message
) - 1] != '\n')
149 fputc ('\n', stderr
);
155 inform (const char * message
, ...)
157 inform (message
, va_alist
)
158 const char * message
;
168 va_start (args
, message
);
173 display (message
, args
);
178 /* Look for the program formed by concatenating PROG_NAME and the
179 string running from PREFIX to END_PREFIX. If the concatenated
180 string contains a '/', try appending EXECUTABLE_SUFFIX if it is
184 look_for_prog (prog_name
, prefix
, end_prefix
)
185 const char *prog_name
;
192 cmd
= xmalloc (strlen (prefix
)
194 #ifdef HAVE_EXECUTABLE_SUFFIX
195 + strlen (EXECUTABLE_SUFFIX
)
198 strcpy (cmd
, prefix
);
200 sprintf (cmd
+ end_prefix
, "%s", prog_name
);
202 if (strchr (cmd
, '/') != NULL
)
206 found
= (stat (cmd
, &s
) == 0
207 #ifdef HAVE_EXECUTABLE_SUFFIX
208 || stat (strcat (cmd
, EXECUTABLE_SUFFIX
), &s
) == 0
214 /* xgettext:c-format */
215 inform (_("Tried file: %s"), cmd
);
221 /* xgettext:c-format */
222 inform (_("Using file: %s"), cmd
);
227 /* Deduce the name of the program we are want to invoke.
228 PROG_NAME is the basic name of the program we want to run,
229 eg "as" or "ld". The catch is that we might want actually
230 run "i386-pe-as" or "ppc-pe-ld".
232 If argv[0] contains the full path, then try to find the program
233 in the same place, with and then without a target-like prefix.
235 Given, argv[0] = /usr/local/bin/i586-cygwin32-dlltool,
236 deduce_name("as") uses the following search order:
238 /usr/local/bin/i586-cygwin32-as
242 If there's an EXECUTABLE_SUFFIX, it'll use that as well; for each
243 name, it'll try without and then with EXECUTABLE_SUFFIX.
245 Given, argv[0] = i586-cygwin32-dlltool, it will not even try "as"
246 as the fallback, but rather return i586-cygwin32-as.
248 Oh, and given, argv[0] = dlltool, it'll return "as".
250 Returns a dynamically allocated string. */
253 deduce_name (prog_name
)
254 const char *prog_name
;
257 char *dash
, *slash
, *cp
;
261 for (cp
= program_name
; *cp
!= '\0'; ++cp
)
266 #if defined(__DJGPP__) || defined (__CYGWIN__) || defined(__WIN32__)
267 *cp
== ':' || *cp
== '\\' ||
280 /* First, try looking for a prefixed PROG_NAME in the
281 PROGRAM_NAME directory, with the same prefix as PROGRAM_NAME. */
282 cmd
= look_for_prog (prog_name
, program_name
, dash
- program_name
+ 1);
285 if (slash
!= NULL
&& cmd
== NULL
)
287 /* Next, try looking for a PROG_NAME in the same directory as
288 that of this program. */
289 cmd
= look_for_prog (prog_name
, program_name
, slash
- program_name
+ 1);
294 /* Just return PROG_NAME as is. */
295 cmd
= xstrdup (prog_name
);
304 if (delete_base_file
&& base_file_name
)
307 fprintf (stderr
, "%s temporary base file %s\n",
308 dontdeltemps
? "Keeping" : "Deleting",
312 unlink (base_file_name
);
313 free (base_file_name
);
317 if (delete_exp_file
&& exp_file_name
)
320 fprintf (stderr
, "%s temporary exp file %s\n",
321 dontdeltemps
? "Keeping" : "Deleting",
325 unlink (exp_file_name
);
326 free (exp_file_name
);
329 if (delete_def_file
&& def_file_name
)
332 fprintf (stderr
, "%s temporary def file %s\n",
333 dontdeltemps
? "Keeping" : "Deleting",
337 unlink (def_file_name
);
338 free (def_file_name
);
344 cleanup_and_exit (int status
)
346 delete_temp_files ();
356 int pid
, wait_status
, retcode
;
359 char *errmsg_fmt
, *errmsg_arg
;
360 char *temp_base
= choose_temp_base ();
364 if (verbose
|| dry_run
)
365 fprintf (stderr
, "%s %s\n", what
, args
);
369 for (s
= args
; *s
; s
++)
373 argv
= alloca (sizeof (char *) * (i
+ 3));
379 while (*s
== ' ' && *s
!= 0)
383 in_quote
= (*s
== '\'' || *s
== '"');
384 sep
= (in_quote
) ? *s
++ : ' ';
386 while (*s
!= sep
&& *s
!= 0)
399 pid
= pexecute (argv
[0], (char * const *) argv
, program_name
, temp_base
,
400 &errmsg_fmt
, &errmsg_arg
, PEXECUTE_ONE
| PEXECUTE_SEARCH
);
404 int errno_val
= errno
;
406 fprintf (stderr
, "%s: ", program_name
);
407 fprintf (stderr
, errmsg_fmt
, errmsg_arg
);
408 fprintf (stderr
, ": %s\n", strerror (errno_val
));
413 pid
= pwait (pid
, &wait_status
, 0);
416 fprintf (stderr
, "%s: wait: %s\n", program_name
, strerror (errno
));
419 else if (WIFSIGNALED (wait_status
))
421 fprintf (stderr
, "%s: subprocess got fatal signal %d\n",
422 program_name
, WTERMSIG (wait_status
));
425 else if (WIFEXITED (wait_status
))
427 if (WEXITSTATUS (wait_status
) != 0)
429 fprintf (stderr
, "%s: %s exited with status %d\n",
430 program_name
, what
, WEXITSTATUS (wait_status
));
444 const char *base
= name
;
448 if (*name
== '/' || *name
== '\\')
454 return (char *) base
;
458 strhash (const char *str
)
460 const unsigned char *s
;
467 s
= (const unsigned char *) str
;
468 while ((c
= *s
++) != '\0')
470 hash
+= c
+ (c
<< 17);
474 hash
+= len
+ (len
<< 17);
480 /**********************************************************************/
486 /* This output is intended to follow the GNU standards document. */
487 /* xgettext:c-format */
488 printf ("GNU %s %s\n", name
, program_version
);
489 printf ("Copyright 1998 Free Software Foundation, Inc.\n");
491 This program is free software; you may redistribute it under the terms of\n\
492 the GNU General Public License. This program has absolutely no warranty.\n");
501 fprintf (file
, "Usage %s <options> <object-files>\n", program_name
);
502 fprintf (file
, " Generic options:\n");
503 fprintf (file
, " --quiet, -q Work quietly\n");
504 fprintf (file
, " --verbose, -v Verbose\n");
505 fprintf (file
, " --version Print dllwrap version\n");
506 fprintf (file
, " --implib <outname> Synonym for --output-lib\n");
507 fprintf (file
, " Options for %s:\n", program_name
);
508 fprintf (file
, " --driver-name <driver> Defaults to \"gcc\"\n");
509 fprintf (file
, " --driver-flags <flags> Override default ld flags\n");
510 fprintf (file
, " --dlltool-name <dlltool> Defaults to \"dlltool\"\n");
511 fprintf (file
, " --entry <entry> Specify alternate DLL entry point\n");
512 fprintf (file
, " --image-base <base> Specify image base address\n");
513 fprintf (file
, " --target <machine> i386-cygwin32 or i386-mingw32\n");
514 fprintf (file
, " --dry-run Show what needs to be run\n");
515 fprintf (file
, " --mno-cygwin Create Mingw DLL\n");
516 fprintf (file
, " Options passed to DLLTOOL:\n");
517 fprintf (file
, " --machine <machine>\n");
518 fprintf (file
, " --output-exp <outname> Generate export file.\n");
519 fprintf (file
, " --output-lib <outname> Generate input library.\n");
520 fprintf (file
, " --add-indirect Add dll indirects to export file.\n");
521 fprintf (file
, " --dllname <name> Name of input dll to put into output lib.\n");
522 fprintf (file
, " --def <deffile> Name input .def file\n");
523 fprintf (file
, " --output-def <deffile> Name output .def file\n");
524 fprintf (file
, " --export-all-symbols Export all symbols to .def\n");
525 fprintf (file
, " --no-export-all-symbols Only export .drectve symbols\n");
526 fprintf (file
, " --exclude-symbols <list> Exclude <list> from .def\n");
527 fprintf (file
, " --no-default-excludes Zap default exclude symbols\n");
528 fprintf (file
, " --base-file <basefile> Read linker generated base file\n");
529 fprintf (file
, " --no-idata4 Don't generate idata$4 section\n");
530 fprintf (file
, " --no-idata5 Don't generate idata$5 section\n");
531 fprintf (file
, " -U Add underscores to .lib\n");
532 fprintf (file
, " -k Kill @<n> from exported names\n");
533 fprintf (file
, " --add-stdcall-alias Add aliases without @<n>\n");
534 fprintf (file
, " --as <name> Use <name> for assembler\n");
535 fprintf (file
, " --nodelete Keep temp files.\n");
536 fprintf (file
, " Rest are passed unmodified to the language driver\n");
537 fprintf (file
, "\n\n");
541 #define OPTION_START 149
543 /* GENERIC options. */
544 #define OPTION_QUIET (OPTION_START + 1)
545 #define OPTION_VERBOSE (OPTION_QUIET + 1)
546 #define OPTION_VERSION (OPTION_VERBOSE + 1)
548 /* DLLWRAP options. */
549 #define OPTION_DRY_RUN (OPTION_VERSION + 1)
550 #define OPTION_DRIVER_NAME (OPTION_DRY_RUN + 1)
551 #define OPTION_DRIVER_FLAGS (OPTION_DRIVER_NAME + 1)
552 #define OPTION_DLLTOOL_NAME (OPTION_DRIVER_FLAGS + 1)
553 #define OPTION_ENTRY (OPTION_DLLTOOL_NAME + 1)
554 #define OPTION_IMAGE_BASE (OPTION_ENTRY + 1)
555 #define OPTION_TARGET (OPTION_IMAGE_BASE + 1)
556 #define OPTION_MNO_CYGWIN (OPTION_TARGET + 1)
558 /* DLLTOOL options. */
559 #define OPTION_NODELETE (OPTION_MNO_CYGWIN + 1)
560 #define OPTION_DLLNAME (OPTION_NODELETE + 1)
561 #define OPTION_NO_IDATA4 (OPTION_DLLNAME + 1)
562 #define OPTION_NO_IDATA5 (OPTION_NO_IDATA4 + 1)
563 #define OPTION_OUTPUT_EXP (OPTION_NO_IDATA5 + 1)
564 #define OPTION_OUTPUT_DEF (OPTION_OUTPUT_EXP + 1)
565 #define OPTION_EXPORT_ALL_SYMS (OPTION_OUTPUT_DEF + 1)
566 #define OPTION_NO_EXPORT_ALL_SYMS (OPTION_EXPORT_ALL_SYMS + 1)
567 #define OPTION_EXCLUDE_SYMS (OPTION_NO_EXPORT_ALL_SYMS + 1)
568 #define OPTION_NO_DEFAULT_EXCLUDES (OPTION_EXCLUDE_SYMS + 1)
569 #define OPTION_OUTPUT_LIB (OPTION_NO_DEFAULT_EXCLUDES + 1)
570 #define OPTION_DEF (OPTION_OUTPUT_LIB + 1)
571 #define OPTION_ADD_UNDERSCORE (OPTION_DEF + 1)
572 #define OPTION_KILLAT (OPTION_ADD_UNDERSCORE + 1)
573 #define OPTION_HELP (OPTION_KILLAT + 1)
574 #define OPTION_MACHINE (OPTION_HELP + 1)
575 #define OPTION_ADD_INDIRECT (OPTION_MACHINE + 1)
576 #define OPTION_BASE_FILE (OPTION_ADD_INDIRECT + 1)
577 #define OPTION_AS (OPTION_BASE_FILE + 1)
579 static const struct option long_options
[] =
581 /* generic options. */
582 {"quiet", no_argument
, NULL
, 'q'},
583 {"verbose", no_argument
, NULL
, 'v'},
584 {"version", no_argument
, NULL
, OPTION_VERSION
},
585 {"implib", required_argument
, NULL
, OPTION_OUTPUT_LIB
},
587 /* dllwrap options. */
588 {"dry-run", no_argument
, NULL
, OPTION_DRY_RUN
},
589 {"driver-name", required_argument
, NULL
, OPTION_DRIVER_NAME
},
590 {"driver-flags", required_argument
, NULL
, OPTION_DRIVER_FLAGS
},
591 {"dlltool-name", required_argument
, NULL
, OPTION_DLLTOOL_NAME
},
592 {"entry", required_argument
, NULL
, 'e'},
593 {"image-base", required_argument
, NULL
, OPTION_IMAGE_BASE
},
594 {"target", required_argument
, NULL
, OPTION_TARGET
},
596 /* dlltool options. */
597 {"no-delete", no_argument
, NULL
, 'n'},
598 {"dllname", required_argument
, NULL
, OPTION_DLLNAME
},
599 {"no-idata4", no_argument
, NULL
, OPTION_NO_IDATA4
},
600 {"no-idata5", no_argument
, NULL
, OPTION_NO_IDATA5
},
601 {"output-exp", required_argument
, NULL
, OPTION_OUTPUT_EXP
},
602 {"output-def", required_argument
, NULL
, OPTION_OUTPUT_DEF
},
603 {"export-all-symbols", no_argument
, NULL
, OPTION_EXPORT_ALL_SYMS
},
604 {"no-export-all-symbols", no_argument
, NULL
, OPTION_NO_EXPORT_ALL_SYMS
},
605 {"exclude-symbols", required_argument
, NULL
, OPTION_EXCLUDE_SYMS
},
606 {"no-default-excludes", no_argument
, NULL
, OPTION_NO_DEFAULT_EXCLUDES
},
607 {"output-lib", required_argument
, NULL
, OPTION_OUTPUT_LIB
},
608 {"def", required_argument
, NULL
, OPTION_DEF
},
609 {"add-underscore", no_argument
, NULL
, 'U'},
610 {"killat", no_argument
, NULL
, 'k'},
611 {"add-stdcall-alias", no_argument
, NULL
, 'A'},
612 {"help", no_argument
, NULL
, 'h'},
613 {"machine", required_argument
, NULL
, OPTION_MACHINE
},
614 {"add-indirect", no_argument
, NULL
, OPTION_ADD_INDIRECT
},
615 {"base-file", required_argument
, NULL
, OPTION_BASE_FILE
},
616 {"as", required_argument
, NULL
, OPTION_AS
},
628 char **saved_argv
= 0;
633 int *dlltool_arg_indices
;
634 int *driver_arg_indices
;
636 char *driver_flags
= 0;
637 char *output_lib_file_name
= 0;
639 dyn_string_t dlltool_cmdline
;
640 dyn_string_t driver_cmdline
;
642 int def_file_seen
= 0;
644 char *image_base_str
= 0;
646 program_name
= argv
[0];
648 saved_argv
= (char **) xmalloc (argc
* sizeof (char*));
649 dlltool_arg_indices
= (int *) xmalloc (argc
* sizeof (int));
650 driver_arg_indices
= (int *) xmalloc (argc
* sizeof (int));
651 for (i
= 0; i
< argc
; ++i
)
653 size_t len
= strlen (argv
[i
]);
654 char *arg
= (char *) xmalloc (len
+ 1);
655 strcpy (arg
, argv
[i
]);
658 dlltool_arg_indices
[i
] = 0;
659 driver_arg_indices
[i
] = 1;
663 /* We recognize dllwrap and dlltool options, and everything else is
664 passed onto the language driver (eg., to GCC). We collect options
665 to dlltool and driver in dlltool_args and driver_args. */
668 while ((c
= getopt_long_only (argc
, argv
, "nkAqve:Uho:l:L:I:",
669 long_options
, (int *) 0)) != EOF
)
673 int single_word_option_value_pair
;
677 single_word_option_value_pair
= 0;
681 /* We recognize this option, so it has to be either dllwrap or
682 dlltool option. Do not pass to driver unless it's one of the
683 generic options that are passed to all the tools (such as -v)
684 which are dealt with later. */
688 /* deal with generic and dllwrap options first. */
701 print_version (program_name
);
704 entry_point
= optarg
;
706 case OPTION_IMAGE_BASE
:
707 image_base_str
= optarg
;
710 def_file_name
= optarg
;
719 dll_file_name
= optarg
;
732 case OPTION_DRIVER_NAME
:
733 driver_name
= optarg
;
735 case OPTION_DRIVER_FLAGS
:
736 driver_flags
= optarg
;
738 case OPTION_DLLTOOL_NAME
:
739 dlltool_name
= optarg
;
744 case OPTION_MNO_CYGWIN
:
745 target
= "i386-mingw32";
747 case OPTION_BASE_FILE
:
748 base_file_name
= optarg
;
749 delete_base_file
= 0;
751 case OPTION_OUTPUT_EXP
:
752 exp_file_name
= optarg
;
755 case OPTION_EXPORT_ALL_SYMS
:
758 case OPTION_OUTPUT_LIB
:
759 output_lib_file_name
= optarg
;
768 /* Handle passing through --option=value case. */
770 && saved_argv
[optind
-1][0] == '-'
771 && saved_argv
[optind
-1][1] == '-'
772 && strchr (saved_argv
[optind
-1], '='))
773 single_word_option_value_pair
= 1;
777 dlltool_arg_indices
[optind
-1] = 1;
778 if (optarg
&& ! single_word_option_value_pair
)
780 dlltool_arg_indices
[optind
-2] = 1;
786 driver_arg_indices
[optind
-1] = 0;
787 if (optarg
&& ! single_word_option_value_pair
)
789 driver_arg_indices
[optind
-2] = 0;
795 if (! dll_name
&& ! dll_file_name
)
798 "%s: Must provide at least one of -o or --dllname options\n",
804 dll_name
= xstrdup (mybasename (dll_file_name
));
806 else if (! dll_file_name
)
808 dll_file_name
= xstrdup (dll_name
);
811 /* Deduce driver-name and dlltool-name from our own. */
812 if (driver_name
== NULL
)
813 driver_name
= deduce_name ("gcc");
815 if (dlltool_name
== NULL
)
816 dlltool_name
= deduce_name ("dlltool");
820 char *fileprefix
= choose_temp_base ();
821 def_file_name
= (char *) xmalloc (strlen (fileprefix
) + 5);
822 sprintf (def_file_name
, "%s.def",
823 (dontdeltemps
) ? mybasename (fileprefix
) : fileprefix
);
827 fprintf (stderr
, "Warning: no export definition file provided\n");
829 "dllwrap will create one, but may not be what you want\n");
832 /* set the target platform. */
833 if (strstr (target
, "cygwin32"))
834 which_target
= CYGWIN_TARGET
;
835 else if (strstr (target
, "mingw32"))
836 which_target
= MINGW32_TARGET
;
838 which_target
= UNKNOWN_TARGET
;
840 /* re-create the command lines as a string, taking care to quote stuff. */
841 dlltool_cmdline
= dyn_string_new (cmdline_len
);
844 dyn_string_append (dlltool_cmdline
, " -v");
846 dyn_string_append (dlltool_cmdline
, " --dllname ");
847 dyn_string_append (dlltool_cmdline
, dll_name
);
849 for (i
= 1; i
< argc
; ++i
)
851 if (dlltool_arg_indices
[i
])
853 char *arg
= saved_argv
[i
];
854 int quote
= (strchr (arg
, ' ') || strchr (arg
, '\t'));
855 dyn_string_append (dlltool_cmdline
,
856 (quote
) ? " \"" : " ");
857 dyn_string_append (dlltool_cmdline
, arg
);
858 dyn_string_append (dlltool_cmdline
,
859 (quote
) ? "\"" : "");
863 driver_cmdline
= dyn_string_new (cmdline_len
);
864 if (! driver_flags
|| strlen (driver_flags
) == 0)
866 switch (which_target
)
869 driver_flags
= cygwin_driver_flags
;
873 driver_flags
= mingw32_driver_flags
;
877 driver_flags
= generic_driver_flags
;
881 dyn_string_append (driver_cmdline
, driver_flags
);
882 dyn_string_append (driver_cmdline
, " -o ");
883 dyn_string_append (driver_cmdline
, dll_file_name
);
885 if (! entry_point
|| strlen (entry_point
) == 0)
887 switch (which_target
)
890 entry_point
= "__cygwin_dll_entry@12";
894 entry_point
= "_DllMainCRTStartup@12";
898 entry_point
= "_DllMain@12";
902 dyn_string_append (driver_cmdline
, " -Wl,-e,");
903 dyn_string_append (driver_cmdline
, entry_point
);
904 dyn_string_append (dlltool_cmdline
, " --exclude-symbol=");
905 dyn_string_append (dlltool_cmdline
,
906 (entry_point
[0] == '_') ? entry_point
+1 : entry_point
);
908 if (! image_base_str
|| strlen (image_base_str
) == 0)
910 char *tmpbuf
= (char *) xmalloc (sizeof ("0x12345678") + 1);
911 unsigned long hash
= strhash (dll_file_name
);
912 sprintf (tmpbuf
, "0x%.8lX", 0x60000000|((hash
<<16)&0xFFC0000));
913 image_base_str
= tmpbuf
;
916 dyn_string_append (driver_cmdline
, " -Wl,--image-base,");
917 dyn_string_append (driver_cmdline
, image_base_str
);
921 dyn_string_append (driver_cmdline
, " -v");
924 for (i
= 1; i
< argc
; ++i
)
926 if (driver_arg_indices
[i
])
928 char *arg
= saved_argv
[i
];
929 int quote
= (strchr (arg
, ' ') || strchr (arg
, '\t'));
930 dyn_string_append (driver_cmdline
,
931 (quote
) ? " \"" : " ");
932 dyn_string_append (driver_cmdline
, arg
);
933 dyn_string_append (driver_cmdline
,
934 (quote
) ? "\"" : "");
939 * Step pre-1. If no --def <EXPORT_DEF> is specified, then create it
940 * and then pass it on.
946 dyn_string_t step_pre1
;
948 step_pre1
= dyn_string_new (1024);
950 dyn_string_append (step_pre1
, dlltool_cmdline
->s
);
953 dyn_string_append (step_pre1
, " --export-all --exclude-symbol=");
954 dyn_string_append (step_pre1
,
955 "_cygwin_dll_entry@12,DllMainCRTStartup@12,DllMain@12,DllEntryPoint@12");
957 dyn_string_append (step_pre1
, " --output-def ");
958 dyn_string_append (step_pre1
, def_file_name
);
960 for (i
= 1; i
< argc
; ++i
)
962 if (driver_arg_indices
[i
])
964 char *arg
= saved_argv
[i
];
965 size_t len
= strlen (arg
);
966 if (len
>= 2 && arg
[len
-2] == '.'
967 && (arg
[len
-1] == 'o' || arg
[len
-1] == 'a'))
969 int quote
= (strchr (arg
, ' ') || strchr (arg
, '\t'));
970 dyn_string_append (step_pre1
,
971 (quote
) ? " \"" : " ");
972 dyn_string_append (step_pre1
, arg
);
973 dyn_string_append (step_pre1
,
974 (quote
) ? "\"" : "");
979 if (run (dlltool_name
, step_pre1
->s
))
980 cleanup_and_exit (1);
982 dyn_string_delete (step_pre1
);
985 dyn_string_append (dlltool_cmdline
, " --def ");
986 dyn_string_append (dlltool_cmdline
, def_file_name
);
990 fprintf (stderr
, "DLLTOOL name : %s\n", dlltool_name
);
991 fprintf (stderr
, "DLLTOOL options : %s\n", dlltool_cmdline
->s
);
992 fprintf (stderr
, "DRIVER name : %s\n", driver_name
);
993 fprintf (stderr
, "DRIVER options : %s\n", driver_cmdline
->s
);
997 * Step 1. Call GCC/LD to create base relocation file. If using GCC, the
998 * driver command line will look like the following:
1000 * % gcc -Wl,--dll --Wl,--base-file,foo.base [rest of command line]
1002 * If the user does not specify a base name, create temporary one that
1003 * is deleted at exit.
1007 if (! base_file_name
)
1009 char *fileprefix
= choose_temp_base ();
1010 base_file_name
= (char *) xmalloc (strlen (fileprefix
) + 6);
1011 sprintf (base_file_name
, "%s.base",
1012 (dontdeltemps
) ? mybasename (fileprefix
) : fileprefix
);
1013 delete_base_file
= 1;
1020 dyn_string_t step1
= dyn_string_new (driver_cmdline
->length
1021 + strlen (base_file_name
)
1023 dyn_string_append (step1
, "-Wl,--base-file,");
1024 quote
= (strchr (base_file_name
, ' ')
1025 || strchr (base_file_name
, '\t'));
1026 dyn_string_append (step1
,
1027 (quote
) ? "\"" : "");
1028 dyn_string_append (step1
, base_file_name
);
1029 dyn_string_append (step1
,
1030 (quote
) ? "\"" : "");
1031 if (driver_cmdline
->length
)
1033 dyn_string_append (step1
, " ");
1034 dyn_string_append (step1
, driver_cmdline
->s
);
1037 if (run (driver_name
, step1
->s
))
1038 cleanup_and_exit (1);
1040 dyn_string_delete (step1
);
1046 * Step 2. generate the exp file by running dlltool.
1047 * dlltool command line will look like the following:
1049 * % dlltool -Wl,--dll --Wl,--base-file,foo.base [rest of command line]
1051 * If the user does not specify a base name, create temporary one that
1052 * is deleted at exit.
1056 if (! exp_file_name
)
1058 char *p
= strrchr (dll_name
, '.');
1059 size_t prefix_len
= (p
) ? p
- dll_name
: strlen (dll_name
);
1060 exp_file_name
= (char *) xmalloc (prefix_len
+ 4 + 1);
1061 strncpy (exp_file_name
, dll_name
, prefix_len
);
1062 exp_file_name
[prefix_len
] = '\0';
1063 strcat (exp_file_name
, ".exp");
1064 delete_exp_file
= 1;
1069 dyn_string_t step2
= dyn_string_new (dlltool_cmdline
->length
1070 + strlen (base_file_name
)
1071 + strlen (exp_file_name
)
1074 dyn_string_append (step2
, "--base-file ");
1075 quote
= (strchr (base_file_name
, ' ')
1076 || strchr (base_file_name
, '\t'));
1077 dyn_string_append (step2
,
1078 (quote
) ? "\"" : "");
1079 dyn_string_append (step2
, base_file_name
);
1080 dyn_string_append (step2
,
1081 (quote
) ? "\" " : " ");
1083 dyn_string_append (step2
, "--output-exp ");
1084 quote
= (strchr (exp_file_name
, ' ')
1085 || strchr (exp_file_name
, '\t'));
1086 dyn_string_append (step2
,
1087 (quote
) ? "\"" : "");
1088 dyn_string_append (step2
, exp_file_name
);
1089 dyn_string_append (step2
,
1090 (quote
) ? "\"" : "");
1092 if (dlltool_cmdline
->length
)
1094 dyn_string_append (step2
, " ");
1095 dyn_string_append (step2
, dlltool_cmdline
->s
);
1098 if (run (dlltool_name
, step2
->s
))
1099 cleanup_and_exit (1);
1101 dyn_string_delete (step2
);
1105 * Step 3. Call GCC/LD to again, adding the exp file this time.
1106 * driver command line will look like the following:
1108 * % gcc -Wl,--dll --Wl,--base-file,foo.base foo.exp [rest ...]
1114 dyn_string_t step3
= dyn_string_new (driver_cmdline
->length
1115 + strlen (exp_file_name
)
1116 + strlen (base_file_name
)
1118 dyn_string_append (step3
, "-Wl,--base-file,");
1119 quote
= (strchr (base_file_name
, ' ')
1120 || strchr (base_file_name
, '\t'));
1121 dyn_string_append (step3
,
1122 (quote
) ? "\"" : "");
1123 dyn_string_append (step3
, base_file_name
);
1124 dyn_string_append (step3
,
1125 (quote
) ? "\" " : " ");
1127 quote
= (strchr (exp_file_name
, ' ')
1128 || strchr (exp_file_name
, '\t'));
1129 dyn_string_append (step3
,
1130 (quote
) ? "\"" : "");
1131 dyn_string_append (step3
, exp_file_name
);
1132 dyn_string_append (step3
,
1133 (quote
) ? "\"" : "");
1135 if (driver_cmdline
->length
)
1137 dyn_string_append (step3
, " ");
1138 dyn_string_append (step3
, driver_cmdline
->s
);
1141 if (run (driver_name
, step3
->s
))
1142 cleanup_and_exit (1);
1144 dyn_string_delete (step3
);
1149 * Step 4. Run DLLTOOL again using the same command line.
1154 dyn_string_t step4
= dyn_string_new (dlltool_cmdline
->length
1155 + strlen (base_file_name
)
1156 + strlen (exp_file_name
)
1159 dyn_string_append (step4
, "--base-file ");
1160 quote
= (strchr (base_file_name
, ' ')
1161 || strchr (base_file_name
, '\t'));
1162 dyn_string_append (step4
,
1163 (quote
) ? "\"" : "");
1164 dyn_string_append (step4
, base_file_name
);
1165 dyn_string_append (step4
,
1166 (quote
) ? "\" " : " ");
1168 dyn_string_append (step4
, "--output-exp ");
1169 quote
= (strchr (exp_file_name
, ' ')
1170 || strchr (exp_file_name
, '\t'));
1171 dyn_string_append (step4
,
1172 (quote
) ? "\"" : "");
1173 dyn_string_append (step4
, exp_file_name
);
1174 dyn_string_append (step4
,
1175 (quote
) ? "\"" : "");
1177 if (dlltool_cmdline
->length
)
1179 dyn_string_append (step4
, " ");
1180 dyn_string_append (step4
, dlltool_cmdline
->s
);
1183 if (output_lib_file_name
)
1185 dyn_string_append (step4
, " --output-lib ");
1186 dyn_string_append (step4
, output_lib_file_name
);
1189 if (run (dlltool_name
, step4
->s
))
1190 cleanup_and_exit (1);
1192 dyn_string_delete (step4
);
1197 * Step 5. Link it all together and be done with it.
1198 * driver command line will look like the following:
1200 * % gcc -Wl,--dll foo.exp [rest ...]
1207 dyn_string_t step5
= dyn_string_new (driver_cmdline
->length
1208 + strlen (exp_file_name
)
1210 quote
= (strchr (exp_file_name
, ' ')
1211 || strchr (exp_file_name
, '\t'));
1212 dyn_string_append (step5
,
1213 (quote
) ? "\"" : "");
1214 dyn_string_append (step5
, exp_file_name
);
1215 dyn_string_append (step5
,
1216 (quote
) ? "\"" : "");
1218 if (driver_cmdline
->length
)
1220 dyn_string_append (step5
, " ");
1221 dyn_string_append (step5
, driver_cmdline
->s
);
1224 if (run (driver_name
, step5
->s
))
1225 cleanup_and_exit (1);
1227 dyn_string_delete (step5
);
1230 cleanup_and_exit (0);