* m32r.opc: Formatting.
[binutils.git] / binutils / dllwrap.c
blob59b810ff6024e62e6bb7443505067bd9421b6a1b
1 /* dllwrap.c -- wrapper for DLLTOOL and GCC to generate PE style DLLs
2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
3 Free Software Foundation, Inc.
4 Contributed by Mumit Khan (khan@xraylith.wisc.edu).
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* AIX requires this to be the first thing in the file. */
24 #ifndef __GNUC__
25 # ifdef _AIX
26 #pragma alloca
27 #endif
28 #endif
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #include "bfd.h"
35 #include "libiberty.h"
36 #include "bucomm.h"
37 #include "getopt.h"
38 #include "dyn-string.h"
40 #include <time.h>
41 #include <sys/stat.h>
42 #include <stdarg.h>
44 #ifdef HAVE_SYS_WAIT_H
45 #include <sys/wait.h>
46 #else /* ! HAVE_SYS_WAIT_H */
47 #if ! defined (_WIN32) || defined (__CYGWIN32__)
48 #ifndef WIFEXITED
49 #define WIFEXITED(w) (((w)&0377) == 0)
50 #endif
51 #ifndef WIFSIGNALED
52 #define WIFSIGNALED(w) (((w)&0377) != 0177 && ((w)&~0377) == 0)
53 #endif
54 #ifndef WTERMSIG
55 #define WTERMSIG(w) ((w) & 0177)
56 #endif
57 #ifndef WEXITSTATUS
58 #define WEXITSTATUS(w) (((w) >> 8) & 0377)
59 #endif
60 #else /* defined (_WIN32) && ! defined (__CYGWIN32__) */
61 #ifndef WIFEXITED
62 #define WIFEXITED(w) (((w) & 0xff) == 0)
63 #endif
64 #ifndef WIFSIGNALED
65 #define WIFSIGNALED(w) (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f)
66 #endif
67 #ifndef WTERMSIG
68 #define WTERMSIG(w) ((w) & 0x7f)
69 #endif
70 #ifndef WEXITSTATUS
71 #define WEXITSTATUS(w) (((w) & 0xff00) >> 8)
72 #endif
73 #endif /* defined (_WIN32) && ! defined (__CYGWIN32__) */
74 #endif /* ! HAVE_SYS_WAIT_H */
76 static char *driver_name = NULL;
77 static char *cygwin_driver_flags =
78 "-Wl,--dll -nostartfiles";
79 static char *mingw32_driver_flags = "-mdll";
80 static char *generic_driver_flags = "-Wl,--dll";
82 static char *entry_point;
84 static char *dlltool_name = NULL;
86 static char *target = TARGET;
88 typedef enum {
89 UNKNOWN_TARGET,
90 CYGWIN_TARGET,
91 MINGW_TARGET
93 target_type;
95 static target_type which_target = UNKNOWN_TARGET;
97 static int dontdeltemps = 0;
98 static int dry_run = 0;
100 static char *prog_name;
102 static int verbose = 0;
104 static char *dll_file_name;
105 static char *dll_name;
106 static char *base_file_name;
107 static char *exp_file_name;
108 static char *def_file_name;
109 static int delete_base_file = 1;
110 static int delete_exp_file = 1;
111 static int delete_def_file = 1;
113 static int run (const char *, char *);
114 static char *mybasename (const char *);
115 static int strhash (const char *);
116 static void usage (FILE *, int);
117 static void display (const char *, va_list) ATTRIBUTE_PRINTF(1,0);
118 static void inform (const char *, ...) ATTRIBUTE_PRINTF_1;
119 static void warn (const char *, ...) ATTRIBUTE_PRINTF_1;
120 static char *look_for_prog (const char *, const char *, int);
121 static char *deduce_name (const char *);
122 static void delete_temp_files (void);
123 static void cleanup_and_exit (int);
125 /**********************************************************************/
127 /* Please keep the following 4 routines in sync with dlltool.c:
128 display ()
129 inform ()
130 look_for_prog ()
131 deduce_name ()
132 It's not worth the hassle to break these out since dllwrap will
133 (hopefully) soon be retired in favor of `ld --shared. */
135 static void
136 display (const char * message, va_list args)
138 if (prog_name != NULL)
139 fprintf (stderr, "%s: ", prog_name);
141 vfprintf (stderr, message, args);
142 fputc ('\n', stderr);
146 static void
147 inform VPARAMS ((const char *message, ...))
149 VA_OPEN (args, message);
150 VA_FIXEDARG (args, const char *, message);
152 if (!verbose)
153 return;
155 display (message, args);
157 VA_CLOSE (args);
160 static void
161 warn VPARAMS ((const char *format, ...))
163 VA_OPEN (args, format);
164 VA_FIXEDARG (args, const char *, format);
166 display (format, args);
168 VA_CLOSE (args);
171 /* Look for the program formed by concatenating PROG_NAME and the
172 string running from PREFIX to END_PREFIX. If the concatenated
173 string contains a '/', try appending EXECUTABLE_SUFFIX if it is
174 appropriate. */
176 static char *
177 look_for_prog (const char *prog_name, const char *prefix, int end_prefix)
179 struct stat s;
180 char *cmd;
182 cmd = xmalloc (strlen (prefix)
183 + strlen (prog_name)
184 #ifdef HAVE_EXECUTABLE_SUFFIX
185 + strlen (EXECUTABLE_SUFFIX)
186 #endif
187 + 10);
188 strcpy (cmd, prefix);
190 sprintf (cmd + end_prefix, "%s", prog_name);
192 if (strchr (cmd, '/') != NULL)
194 int found;
196 found = (stat (cmd, &s) == 0
197 #ifdef HAVE_EXECUTABLE_SUFFIX
198 || stat (strcat (cmd, EXECUTABLE_SUFFIX), &s) == 0
199 #endif
202 if (! found)
204 /* xgettext:c-format */
205 inform (_("Tried file: %s"), cmd);
206 free (cmd);
207 return NULL;
211 /* xgettext:c-format */
212 inform (_("Using file: %s"), cmd);
214 return cmd;
217 /* Deduce the name of the program we are want to invoke.
218 PROG_NAME is the basic name of the program we want to run,
219 eg "as" or "ld". The catch is that we might want actually
220 run "i386-pe-as" or "ppc-pe-ld".
222 If argv[0] contains the full path, then try to find the program
223 in the same place, with and then without a target-like prefix.
225 Given, argv[0] = /usr/local/bin/i586-cygwin32-dlltool,
226 deduce_name("as") uses the following search order:
228 /usr/local/bin/i586-cygwin32-as
229 /usr/local/bin/as
232 If there's an EXECUTABLE_SUFFIX, it'll use that as well; for each
233 name, it'll try without and then with EXECUTABLE_SUFFIX.
235 Given, argv[0] = i586-cygwin32-dlltool, it will not even try "as"
236 as the fallback, but rather return i586-cygwin32-as.
238 Oh, and given, argv[0] = dlltool, it'll return "as".
240 Returns a dynamically allocated string. */
242 static char *
243 deduce_name (const char * name)
245 char *cmd;
246 const char *dash;
247 const char *slash;
248 const char *cp;
250 dash = NULL;
251 slash = NULL;
252 for (cp = prog_name; *cp != '\0'; ++cp)
254 if (*cp == '-')
255 dash = cp;
257 if (
258 #if defined(__DJGPP__) || defined (__CYGWIN__) || defined(__WIN32__)
259 *cp == ':' || *cp == '\\' ||
260 #endif
261 *cp == '/')
263 slash = cp;
264 dash = NULL;
268 cmd = NULL;
270 if (dash != NULL)
271 /* First, try looking for a prefixed NAME in the
272 PROG_NAME directory, with the same prefix as PROG_NAME. */
273 cmd = look_for_prog (name, prog_name, dash - prog_name + 1);
275 if (slash != NULL && cmd == NULL)
276 /* Next, try looking for a NAME in the same directory as
277 that of this program. */
278 cmd = look_for_prog (name, prog_name, slash - prog_name + 1);
280 if (cmd == NULL)
281 /* Just return NAME as is. */
282 cmd = xstrdup (name);
284 return cmd;
287 static void
288 delete_temp_files (void)
290 if (delete_base_file && base_file_name)
292 if (verbose)
294 if (dontdeltemps)
295 warn (_("Keeping temporary base file %s"), base_file_name);
296 else
297 warn (_("Deleting temporary base file %s"), base_file_name);
299 if (! dontdeltemps)
301 unlink (base_file_name);
302 free (base_file_name);
306 if (delete_exp_file && exp_file_name)
308 if (verbose)
310 if (dontdeltemps)
311 warn (_("Keeping temporary exp file %s"), exp_file_name);
312 else
313 warn (_("Deleting temporary exp file %s"), exp_file_name);
315 if (! dontdeltemps)
317 unlink (exp_file_name);
318 free (exp_file_name);
321 if (delete_def_file && def_file_name)
323 if (verbose)
325 if (dontdeltemps)
326 warn (_("Keeping temporary def file %s"), def_file_name);
327 else
328 warn (_("Deleting temporary def file %s"), def_file_name);
330 if (! dontdeltemps)
332 unlink (def_file_name);
333 free (def_file_name);
338 static void
339 cleanup_and_exit (int status)
341 delete_temp_files ();
342 exit (status);
345 static int
346 run (const char *what, char *args)
348 char *s;
349 int pid, wait_status, retcode;
350 int i;
351 const char **argv;
352 char *errmsg_fmt, *errmsg_arg;
353 char *temp_base = choose_temp_base ();
354 int in_quote;
355 char sep;
357 if (verbose || dry_run)
358 fprintf (stderr, "%s %s\n", what, args);
360 /* Count the args */
361 i = 0;
362 for (s = args; *s; s++)
363 if (*s == ' ')
364 i++;
365 i++;
366 argv = alloca (sizeof (char *) * (i + 3));
367 i = 0;
368 argv[i++] = what;
369 s = args;
370 while (1)
372 while (*s == ' ' && *s != 0)
373 s++;
374 if (*s == 0)
375 break;
376 in_quote = (*s == '\'' || *s == '"');
377 sep = (in_quote) ? *s++ : ' ';
378 argv[i++] = s;
379 while (*s != sep && *s != 0)
380 s++;
381 if (*s == 0)
382 break;
383 *s++ = 0;
384 if (in_quote)
385 s++;
387 argv[i++] = NULL;
389 if (dry_run)
390 return 0;
392 pid = pexecute (argv[0], (char * const *) argv, prog_name, temp_base,
393 &errmsg_fmt, &errmsg_arg, PEXECUTE_ONE | PEXECUTE_SEARCH);
395 if (pid == -1)
397 int errno_val = errno;
399 fprintf (stderr, "%s: ", prog_name);
400 fprintf (stderr, errmsg_fmt, errmsg_arg);
401 fprintf (stderr, ": %s\n", strerror (errno_val));
402 return 1;
405 retcode = 0;
406 pid = pwait (pid, &wait_status, 0);
407 if (pid == -1)
409 warn ("wait: %s", strerror (errno));
410 retcode = 1;
412 else if (WIFSIGNALED (wait_status))
414 warn (_("subprocess got fatal signal %d"), WTERMSIG (wait_status));
415 retcode = 1;
417 else if (WIFEXITED (wait_status))
419 if (WEXITSTATUS (wait_status) != 0)
421 warn (_("%s exited with status %d"), what, WEXITSTATUS (wait_status));
422 retcode = 1;
425 else
426 retcode = 1;
428 return retcode;
431 static char *
432 mybasename (const char *name)
434 const char *base = name;
436 while (*name)
438 if (*name == '/' || *name == '\\')
440 base = name + 1;
442 ++name;
444 return (char *) base;
447 static int
448 strhash (const char *str)
450 const unsigned char *s;
451 unsigned long hash;
452 unsigned int c;
453 unsigned int len;
455 hash = 0;
456 len = 0;
457 s = (const unsigned char *) str;
458 while ((c = *s++) != '\0')
460 hash += c + (c << 17);
461 hash ^= hash >> 2;
462 ++len;
464 hash += len + (len << 17);
465 hash ^= hash >> 2;
467 return hash;
470 /**********************************************************************/
472 static void
473 usage (FILE *file, int status)
475 fprintf (file, _("Usage %s <option(s)> <object-file(s)>\n"), prog_name);
476 fprintf (file, _(" Generic options:\n"));
477 fprintf (file, _(" @<file> Read options from <file>\n"));
478 fprintf (file, _(" --quiet, -q Work quietly\n"));
479 fprintf (file, _(" --verbose, -v Verbose\n"));
480 fprintf (file, _(" --version Print dllwrap version\n"));
481 fprintf (file, _(" --implib <outname> Synonym for --output-lib\n"));
482 fprintf (file, _(" Options for %s:\n"), prog_name);
483 fprintf (file, _(" --driver-name <driver> Defaults to \"gcc\"\n"));
484 fprintf (file, _(" --driver-flags <flags> Override default ld flags\n"));
485 fprintf (file, _(" --dlltool-name <dlltool> Defaults to \"dlltool\"\n"));
486 fprintf (file, _(" --entry <entry> Specify alternate DLL entry point\n"));
487 fprintf (file, _(" --image-base <base> Specify image base address\n"));
488 fprintf (file, _(" --target <machine> i386-cygwin32 or i386-mingw32\n"));
489 fprintf (file, _(" --dry-run Show what needs to be run\n"));
490 fprintf (file, _(" --mno-cygwin Create Mingw DLL\n"));
491 fprintf (file, _(" Options passed to DLLTOOL:\n"));
492 fprintf (file, _(" --machine <machine>\n"));
493 fprintf (file, _(" --output-exp <outname> Generate export file.\n"));
494 fprintf (file, _(" --output-lib <outname> Generate input library.\n"));
495 fprintf (file, _(" --add-indirect Add dll indirects to export file.\n"));
496 fprintf (file, _(" --dllname <name> Name of input dll to put into output lib.\n"));
497 fprintf (file, _(" --def <deffile> Name input .def file\n"));
498 fprintf (file, _(" --output-def <deffile> Name output .def file\n"));
499 fprintf (file, _(" --export-all-symbols Export all symbols to .def\n"));
500 fprintf (file, _(" --no-export-all-symbols Only export .drectve symbols\n"));
501 fprintf (file, _(" --exclude-symbols <list> Exclude <list> from .def\n"));
502 fprintf (file, _(" --no-default-excludes Zap default exclude symbols\n"));
503 fprintf (file, _(" --base-file <basefile> Read linker generated base file\n"));
504 fprintf (file, _(" --no-idata4 Don't generate idata$4 section\n"));
505 fprintf (file, _(" --no-idata5 Don't generate idata$5 section\n"));
506 fprintf (file, _(" -U Add underscores to .lib\n"));
507 fprintf (file, _(" -k Kill @<n> from exported names\n"));
508 fprintf (file, _(" --add-stdcall-alias Add aliases without @<n>\n"));
509 fprintf (file, _(" --as <name> Use <name> for assembler\n"));
510 fprintf (file, _(" --nodelete Keep temp files.\n"));
511 fprintf (file, _(" Rest are passed unmodified to the language driver\n"));
512 fprintf (file, "\n\n");
513 if (REPORT_BUGS_TO[0] && status == 0)
514 fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
515 exit (status);
518 #define OPTION_START 149
520 /* GENERIC options. */
521 #define OPTION_QUIET (OPTION_START + 1)
522 #define OPTION_VERBOSE (OPTION_QUIET + 1)
523 #define OPTION_VERSION (OPTION_VERBOSE + 1)
525 /* DLLWRAP options. */
526 #define OPTION_DRY_RUN (OPTION_VERSION + 1)
527 #define OPTION_DRIVER_NAME (OPTION_DRY_RUN + 1)
528 #define OPTION_DRIVER_FLAGS (OPTION_DRIVER_NAME + 1)
529 #define OPTION_DLLTOOL_NAME (OPTION_DRIVER_FLAGS + 1)
530 #define OPTION_ENTRY (OPTION_DLLTOOL_NAME + 1)
531 #define OPTION_IMAGE_BASE (OPTION_ENTRY + 1)
532 #define OPTION_TARGET (OPTION_IMAGE_BASE + 1)
533 #define OPTION_MNO_CYGWIN (OPTION_TARGET + 1)
535 /* DLLTOOL options. */
536 #define OPTION_NODELETE (OPTION_MNO_CYGWIN + 1)
537 #define OPTION_DLLNAME (OPTION_NODELETE + 1)
538 #define OPTION_NO_IDATA4 (OPTION_DLLNAME + 1)
539 #define OPTION_NO_IDATA5 (OPTION_NO_IDATA4 + 1)
540 #define OPTION_OUTPUT_EXP (OPTION_NO_IDATA5 + 1)
541 #define OPTION_OUTPUT_DEF (OPTION_OUTPUT_EXP + 1)
542 #define OPTION_EXPORT_ALL_SYMS (OPTION_OUTPUT_DEF + 1)
543 #define OPTION_NO_EXPORT_ALL_SYMS (OPTION_EXPORT_ALL_SYMS + 1)
544 #define OPTION_EXCLUDE_SYMS (OPTION_NO_EXPORT_ALL_SYMS + 1)
545 #define OPTION_NO_DEFAULT_EXCLUDES (OPTION_EXCLUDE_SYMS + 1)
546 #define OPTION_OUTPUT_LIB (OPTION_NO_DEFAULT_EXCLUDES + 1)
547 #define OPTION_DEF (OPTION_OUTPUT_LIB + 1)
548 #define OPTION_ADD_UNDERSCORE (OPTION_DEF + 1)
549 #define OPTION_KILLAT (OPTION_ADD_UNDERSCORE + 1)
550 #define OPTION_HELP (OPTION_KILLAT + 1)
551 #define OPTION_MACHINE (OPTION_HELP + 1)
552 #define OPTION_ADD_INDIRECT (OPTION_MACHINE + 1)
553 #define OPTION_BASE_FILE (OPTION_ADD_INDIRECT + 1)
554 #define OPTION_AS (OPTION_BASE_FILE + 1)
556 static const struct option long_options[] =
558 /* generic options. */
559 {"quiet", no_argument, NULL, 'q'},
560 {"verbose", no_argument, NULL, 'v'},
561 {"version", no_argument, NULL, OPTION_VERSION},
562 {"implib", required_argument, NULL, OPTION_OUTPUT_LIB},
564 /* dllwrap options. */
565 {"dry-run", no_argument, NULL, OPTION_DRY_RUN},
566 {"driver-name", required_argument, NULL, OPTION_DRIVER_NAME},
567 {"driver-flags", required_argument, NULL, OPTION_DRIVER_FLAGS},
568 {"dlltool-name", required_argument, NULL, OPTION_DLLTOOL_NAME},
569 {"entry", required_argument, NULL, 'e'},
570 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
571 {"target", required_argument, NULL, OPTION_TARGET},
573 /* dlltool options. */
574 {"no-delete", no_argument, NULL, 'n'},
575 {"dllname", required_argument, NULL, OPTION_DLLNAME},
576 {"no-idata4", no_argument, NULL, OPTION_NO_IDATA4},
577 {"no-idata5", no_argument, NULL, OPTION_NO_IDATA5},
578 {"output-exp", required_argument, NULL, OPTION_OUTPUT_EXP},
579 {"output-def", required_argument, NULL, OPTION_OUTPUT_DEF},
580 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL_SYMS},
581 {"no-export-all-symbols", no_argument, NULL, OPTION_NO_EXPORT_ALL_SYMS},
582 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMS},
583 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
584 {"output-lib", required_argument, NULL, OPTION_OUTPUT_LIB},
585 {"def", required_argument, NULL, OPTION_DEF},
586 {"add-underscore", no_argument, NULL, 'U'},
587 {"killat", no_argument, NULL, 'k'},
588 {"add-stdcall-alias", no_argument, NULL, 'A'},
589 {"help", no_argument, NULL, 'h'},
590 {"machine", required_argument, NULL, OPTION_MACHINE},
591 {"add-indirect", no_argument, NULL, OPTION_ADD_INDIRECT},
592 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
593 {"as", required_argument, NULL, OPTION_AS},
594 {0, 0, 0, 0}
597 int main (int, char **);
600 main (int argc, char **argv)
602 int c;
603 int i;
605 char **saved_argv = 0;
606 int cmdline_len = 0;
608 int export_all = 0;
610 int *dlltool_arg_indices;
611 int *driver_arg_indices;
613 char *driver_flags = 0;
614 char *output_lib_file_name = 0;
616 dyn_string_t dlltool_cmdline;
617 dyn_string_t driver_cmdline;
619 int def_file_seen = 0;
621 char *image_base_str = 0;
623 prog_name = argv[0];
625 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
626 setlocale (LC_MESSAGES, "");
627 #endif
628 #if defined (HAVE_SETLOCALE)
629 setlocale (LC_CTYPE, "");
630 #endif
631 bindtextdomain (PACKAGE, LOCALEDIR);
632 textdomain (PACKAGE);
634 expandargv (&argc, &argv);
636 saved_argv = (char **) xmalloc (argc * sizeof (char*));
637 dlltool_arg_indices = (int *) xmalloc (argc * sizeof (int));
638 driver_arg_indices = (int *) xmalloc (argc * sizeof (int));
639 for (i = 0; i < argc; ++i)
641 size_t len = strlen (argv[i]);
642 char *arg = (char *) xmalloc (len + 1);
643 strcpy (arg, argv[i]);
644 cmdline_len += len;
645 saved_argv[i] = arg;
646 dlltool_arg_indices[i] = 0;
647 driver_arg_indices[i] = 1;
649 cmdline_len++;
651 /* We recognize dllwrap and dlltool options, and everything else is
652 passed onto the language driver (eg., to GCC). We collect options
653 to dlltool and driver in dlltool_args and driver_args. */
655 opterr = 0;
656 while ((c = getopt_long_only (argc, argv, "nkAqve:Uho:l:L:I:",
657 long_options, (int *) 0)) != EOF)
659 int dlltool_arg;
660 int driver_arg;
661 int single_word_option_value_pair;
663 dlltool_arg = 0;
664 driver_arg = 1;
665 single_word_option_value_pair = 0;
667 if (c != '?')
669 /* We recognize this option, so it has to be either dllwrap or
670 dlltool option. Do not pass to driver unless it's one of the
671 generic options that are passed to all the tools (such as -v)
672 which are dealt with later. */
673 driver_arg = 0;
676 /* deal with generic and dllwrap options first. */
677 switch (c)
679 case 'h':
680 usage (stdout, 0);
681 break;
682 case 'q':
683 verbose = 0;
684 break;
685 case 'v':
686 verbose = 1;
687 break;
688 case OPTION_VERSION:
689 print_version (prog_name);
690 break;
691 case 'e':
692 entry_point = optarg;
693 break;
694 case OPTION_IMAGE_BASE:
695 image_base_str = optarg;
696 break;
697 case OPTION_DEF:
698 def_file_name = optarg;
699 def_file_seen = 1;
700 delete_def_file = 0;
701 break;
702 case 'n':
703 dontdeltemps = 1;
704 dlltool_arg = 1;
705 break;
706 case 'o':
707 dll_file_name = optarg;
708 break;
709 case 'I':
710 case 'l':
711 case 'L':
712 driver_arg = 1;
713 break;
714 case OPTION_DLLNAME:
715 dll_name = optarg;
716 break;
717 case OPTION_DRY_RUN:
718 dry_run = 1;
719 break;
720 case OPTION_DRIVER_NAME:
721 driver_name = optarg;
722 break;
723 case OPTION_DRIVER_FLAGS:
724 driver_flags = optarg;
725 break;
726 case OPTION_DLLTOOL_NAME:
727 dlltool_name = optarg;
728 break;
729 case OPTION_TARGET:
730 target = optarg;
731 break;
732 case OPTION_MNO_CYGWIN:
733 target = "i386-mingw32";
734 break;
735 case OPTION_BASE_FILE:
736 base_file_name = optarg;
737 delete_base_file = 0;
738 break;
739 case OPTION_OUTPUT_EXP:
740 exp_file_name = optarg;
741 delete_exp_file = 0;
742 break;
743 case OPTION_EXPORT_ALL_SYMS:
744 export_all = 1;
745 break;
746 case OPTION_OUTPUT_LIB:
747 output_lib_file_name = optarg;
748 break;
749 case '?':
750 break;
751 default:
752 dlltool_arg = 1;
753 break;
756 /* Handle passing through --option=value case. */
757 if (optarg
758 && saved_argv[optind-1][0] == '-'
759 && saved_argv[optind-1][1] == '-'
760 && strchr (saved_argv[optind-1], '='))
761 single_word_option_value_pair = 1;
763 if (dlltool_arg)
765 dlltool_arg_indices[optind-1] = 1;
766 if (optarg && ! single_word_option_value_pair)
768 dlltool_arg_indices[optind-2] = 1;
772 if (! driver_arg)
774 driver_arg_indices[optind-1] = 0;
775 if (optarg && ! single_word_option_value_pair)
777 driver_arg_indices[optind-2] = 0;
782 /* Sanity checks. */
783 if (! dll_name && ! dll_file_name)
785 warn (_("Must provide at least one of -o or --dllname options"));
786 exit (1);
788 else if (! dll_name)
790 dll_name = xstrdup (mybasename (dll_file_name));
792 else if (! dll_file_name)
794 dll_file_name = xstrdup (dll_name);
797 /* Deduce driver-name and dlltool-name from our own. */
798 if (driver_name == NULL)
799 driver_name = deduce_name ("gcc");
801 if (dlltool_name == NULL)
802 dlltool_name = deduce_name ("dlltool");
804 if (! def_file_seen)
806 char *fileprefix = choose_temp_base ();
808 def_file_name = (char *) xmalloc (strlen (fileprefix) + 5);
809 sprintf (def_file_name, "%s.def",
810 (dontdeltemps) ? mybasename (fileprefix) : fileprefix);
811 delete_def_file = 1;
812 free (fileprefix);
813 delete_def_file = 1;
814 warn (_("no export definition file provided.\n\
815 Creating one, but that may not be what you want"));
818 /* Set the target platform. */
819 if (strstr (target, "cygwin"))
820 which_target = CYGWIN_TARGET;
821 else if (strstr (target, "mingw"))
822 which_target = MINGW_TARGET;
823 else
824 which_target = UNKNOWN_TARGET;
826 /* Re-create the command lines as a string, taking care to quote stuff. */
827 dlltool_cmdline = dyn_string_new (cmdline_len);
828 if (verbose)
829 dyn_string_append_cstr (dlltool_cmdline, " -v");
831 dyn_string_append_cstr (dlltool_cmdline, " --dllname ");
832 dyn_string_append_cstr (dlltool_cmdline, dll_name);
834 for (i = 1; i < argc; ++i)
836 if (dlltool_arg_indices[i])
838 char *arg = saved_argv[i];
839 int quote = (strchr (arg, ' ') || strchr (arg, '\t'));
840 dyn_string_append_cstr (dlltool_cmdline,
841 (quote) ? " \"" : " ");
842 dyn_string_append_cstr (dlltool_cmdline, arg);
843 dyn_string_append_cstr (dlltool_cmdline,
844 (quote) ? "\"" : "");
848 driver_cmdline = dyn_string_new (cmdline_len);
849 if (! driver_flags || strlen (driver_flags) == 0)
851 switch (which_target)
853 case CYGWIN_TARGET:
854 driver_flags = cygwin_driver_flags;
855 break;
857 case MINGW_TARGET:
858 driver_flags = mingw32_driver_flags;
859 break;
861 default:
862 driver_flags = generic_driver_flags;
863 break;
866 dyn_string_append_cstr (driver_cmdline, driver_flags);
867 dyn_string_append_cstr (driver_cmdline, " -o ");
868 dyn_string_append_cstr (driver_cmdline, dll_file_name);
870 if (! entry_point || strlen (entry_point) == 0)
872 switch (which_target)
874 case CYGWIN_TARGET:
875 entry_point = "__cygwin_dll_entry@12";
876 break;
878 case MINGW_TARGET:
879 entry_point = "_DllMainCRTStartup@12";
880 break;
882 default:
883 entry_point = "_DllMain@12";
884 break;
887 dyn_string_append_cstr (driver_cmdline, " -Wl,-e,");
888 dyn_string_append_cstr (driver_cmdline, entry_point);
889 dyn_string_append_cstr (dlltool_cmdline, " --exclude-symbol=");
890 dyn_string_append_cstr (dlltool_cmdline,
891 (entry_point[0] == '_') ? entry_point+1 : entry_point);
893 if (! image_base_str || strlen (image_base_str) == 0)
895 char *tmpbuf = (char *) xmalloc (sizeof ("0x12345678") + 1);
896 unsigned long hash = strhash (dll_file_name);
897 sprintf (tmpbuf, "0x%.8lX", 0x60000000|((hash<<16)&0xFFC0000));
898 image_base_str = tmpbuf;
901 dyn_string_append_cstr (driver_cmdline, " -Wl,--image-base,");
902 dyn_string_append_cstr (driver_cmdline, image_base_str);
904 if (verbose)
906 dyn_string_append_cstr (driver_cmdline, " -v");
909 for (i = 1; i < argc; ++i)
911 if (driver_arg_indices[i])
913 char *arg = saved_argv[i];
914 int quote = (strchr (arg, ' ') || strchr (arg, '\t'));
915 dyn_string_append_cstr (driver_cmdline,
916 (quote) ? " \"" : " ");
917 dyn_string_append_cstr (driver_cmdline, arg);
918 dyn_string_append_cstr (driver_cmdline,
919 (quote) ? "\"" : "");
923 /* Step pre-1. If no --def <EXPORT_DEF> is specified,
924 then create it and then pass it on. */
926 if (! def_file_seen)
928 int i;
929 dyn_string_t step_pre1;
931 step_pre1 = dyn_string_new (1024);
933 dyn_string_append_cstr (step_pre1, dlltool_cmdline->s);
934 if (export_all)
936 dyn_string_append_cstr (step_pre1, " --export-all --exclude-symbol=");
937 dyn_string_append_cstr (step_pre1,
938 "_cygwin_dll_entry@12,DllMainCRTStartup@12,DllMain@12,DllEntryPoint@12");
940 dyn_string_append_cstr (step_pre1, " --output-def ");
941 dyn_string_append_cstr (step_pre1, def_file_name);
943 for (i = 1; i < argc; ++i)
945 if (driver_arg_indices[i])
947 char *arg = saved_argv[i];
948 size_t len = strlen (arg);
949 if (len >= 2 && arg[len-2] == '.'
950 && (arg[len-1] == 'o' || arg[len-1] == 'a'))
952 int quote = (strchr (arg, ' ') || strchr (arg, '\t'));
953 dyn_string_append_cstr (step_pre1,
954 (quote) ? " \"" : " ");
955 dyn_string_append_cstr (step_pre1, arg);
956 dyn_string_append_cstr (step_pre1,
957 (quote) ? "\"" : "");
962 if (run (dlltool_name, step_pre1->s))
963 cleanup_and_exit (1);
965 dyn_string_delete (step_pre1);
968 dyn_string_append_cstr (dlltool_cmdline, " --def ");
969 dyn_string_append_cstr (dlltool_cmdline, def_file_name);
971 if (verbose)
973 fprintf (stderr, _("DLLTOOL name : %s\n"), dlltool_name);
974 fprintf (stderr, _("DLLTOOL options : %s\n"), dlltool_cmdline->s);
975 fprintf (stderr, _("DRIVER name : %s\n"), driver_name);
976 fprintf (stderr, _("DRIVER options : %s\n"), driver_cmdline->s);
979 /* Step 1. Call GCC/LD to create base relocation file. If using GCC, the
980 driver command line will look like the following:
982 % gcc -Wl,--dll --Wl,--base-file,foo.base [rest of command line]
984 If the user does not specify a base name, create temporary one that
985 is deleted at exit. */
987 if (! base_file_name)
989 char *fileprefix = choose_temp_base ();
990 base_file_name = (char *) xmalloc (strlen (fileprefix) + 6);
991 sprintf (base_file_name, "%s.base",
992 (dontdeltemps) ? mybasename (fileprefix) : fileprefix);
993 delete_base_file = 1;
994 free (fileprefix);
998 int quote;
1000 dyn_string_t step1 = dyn_string_new (driver_cmdline->length
1001 + strlen (base_file_name)
1002 + 20);
1003 dyn_string_append_cstr (step1, "-Wl,--base-file,");
1004 quote = (strchr (base_file_name, ' ')
1005 || strchr (base_file_name, '\t'));
1006 dyn_string_append_cstr (step1,
1007 (quote) ? "\"" : "");
1008 dyn_string_append_cstr (step1, base_file_name);
1009 dyn_string_append_cstr (step1,
1010 (quote) ? "\"" : "");
1011 if (driver_cmdline->length)
1013 dyn_string_append_cstr (step1, " ");
1014 dyn_string_append_cstr (step1, driver_cmdline->s);
1017 if (run (driver_name, step1->s))
1018 cleanup_and_exit (1);
1020 dyn_string_delete (step1);
1023 /* Step 2. generate the exp file by running dlltool.
1024 dlltool command line will look like the following:
1026 % dlltool -Wl,--dll --Wl,--base-file,foo.base [rest of command line]
1028 If the user does not specify a base name, create temporary one that
1029 is deleted at exit. */
1031 if (! exp_file_name)
1033 char *p = strrchr (dll_name, '.');
1034 size_t prefix_len = (p) ? (size_t) (p - dll_name) : strlen (dll_name);
1036 exp_file_name = (char *) xmalloc (prefix_len + 4 + 1);
1037 strncpy (exp_file_name, dll_name, prefix_len);
1038 exp_file_name[prefix_len] = '\0';
1039 strcat (exp_file_name, ".exp");
1040 delete_exp_file = 1;
1044 int quote;
1046 dyn_string_t step2 = dyn_string_new (dlltool_cmdline->length
1047 + strlen (base_file_name)
1048 + strlen (exp_file_name)
1049 + 20);
1051 dyn_string_append_cstr (step2, "--base-file ");
1052 quote = (strchr (base_file_name, ' ')
1053 || strchr (base_file_name, '\t'));
1054 dyn_string_append_cstr (step2,
1055 (quote) ? "\"" : "");
1056 dyn_string_append_cstr (step2, base_file_name);
1057 dyn_string_append_cstr (step2,
1058 (quote) ? "\" " : " ");
1060 dyn_string_append_cstr (step2, "--output-exp ");
1061 quote = (strchr (exp_file_name, ' ')
1062 || strchr (exp_file_name, '\t'));
1063 dyn_string_append_cstr (step2,
1064 (quote) ? "\"" : "");
1065 dyn_string_append_cstr (step2, exp_file_name);
1066 dyn_string_append_cstr (step2,
1067 (quote) ? "\"" : "");
1069 if (dlltool_cmdline->length)
1071 dyn_string_append_cstr (step2, " ");
1072 dyn_string_append_cstr (step2, dlltool_cmdline->s);
1075 if (run (dlltool_name, step2->s))
1076 cleanup_and_exit (1);
1078 dyn_string_delete (step2);
1082 * Step 3. Call GCC/LD to again, adding the exp file this time.
1083 * driver command line will look like the following:
1085 * % gcc -Wl,--dll --Wl,--base-file,foo.base foo.exp [rest ...]
1089 int quote;
1091 dyn_string_t step3 = dyn_string_new (driver_cmdline->length
1092 + strlen (exp_file_name)
1093 + strlen (base_file_name)
1094 + 20);
1095 dyn_string_append_cstr (step3, "-Wl,--base-file,");
1096 quote = (strchr (base_file_name, ' ')
1097 || strchr (base_file_name, '\t'));
1098 dyn_string_append_cstr (step3,
1099 (quote) ? "\"" : "");
1100 dyn_string_append_cstr (step3, base_file_name);
1101 dyn_string_append_cstr (step3,
1102 (quote) ? "\" " : " ");
1104 quote = (strchr (exp_file_name, ' ')
1105 || strchr (exp_file_name, '\t'));
1106 dyn_string_append_cstr (step3,
1107 (quote) ? "\"" : "");
1108 dyn_string_append_cstr (step3, exp_file_name);
1109 dyn_string_append_cstr (step3,
1110 (quote) ? "\"" : "");
1112 if (driver_cmdline->length)
1114 dyn_string_append_cstr (step3, " ");
1115 dyn_string_append_cstr (step3, driver_cmdline->s);
1118 if (run (driver_name, step3->s))
1119 cleanup_and_exit (1);
1121 dyn_string_delete (step3);
1126 * Step 4. Run DLLTOOL again using the same command line.
1130 int quote;
1131 dyn_string_t step4 = dyn_string_new (dlltool_cmdline->length
1132 + strlen (base_file_name)
1133 + strlen (exp_file_name)
1134 + 20);
1136 dyn_string_append_cstr (step4, "--base-file ");
1137 quote = (strchr (base_file_name, ' ')
1138 || strchr (base_file_name, '\t'));
1139 dyn_string_append_cstr (step4,
1140 (quote) ? "\"" : "");
1141 dyn_string_append_cstr (step4, base_file_name);
1142 dyn_string_append_cstr (step4,
1143 (quote) ? "\" " : " ");
1145 dyn_string_append_cstr (step4, "--output-exp ");
1146 quote = (strchr (exp_file_name, ' ')
1147 || strchr (exp_file_name, '\t'));
1148 dyn_string_append_cstr (step4,
1149 (quote) ? "\"" : "");
1150 dyn_string_append_cstr (step4, exp_file_name);
1151 dyn_string_append_cstr (step4,
1152 (quote) ? "\"" : "");
1154 if (dlltool_cmdline->length)
1156 dyn_string_append_cstr (step4, " ");
1157 dyn_string_append_cstr (step4, dlltool_cmdline->s);
1160 if (output_lib_file_name)
1162 dyn_string_append_cstr (step4, " --output-lib ");
1163 dyn_string_append_cstr (step4, output_lib_file_name);
1166 if (run (dlltool_name, step4->s))
1167 cleanup_and_exit (1);
1169 dyn_string_delete (step4);
1174 * Step 5. Link it all together and be done with it.
1175 * driver command line will look like the following:
1177 * % gcc -Wl,--dll foo.exp [rest ...]
1182 int quote;
1184 dyn_string_t step5 = dyn_string_new (driver_cmdline->length
1185 + strlen (exp_file_name)
1186 + 20);
1187 quote = (strchr (exp_file_name, ' ')
1188 || strchr (exp_file_name, '\t'));
1189 dyn_string_append_cstr (step5,
1190 (quote) ? "\"" : "");
1191 dyn_string_append_cstr (step5, exp_file_name);
1192 dyn_string_append_cstr (step5,
1193 (quote) ? "\"" : "");
1195 if (driver_cmdline->length)
1197 dyn_string_append_cstr (step5, " ");
1198 dyn_string_append_cstr (step5, driver_cmdline->s);
1201 if (run (driver_name, step5->s))
1202 cleanup_and_exit (1);
1204 dyn_string_delete (step5);
1207 cleanup_and_exit (0);
1209 return 0;