* pe-dll.h: New file.
[binutils.git] / binutils / dllwrap.c
blob84606fb2b8ea9c743c3577dcd1f85a1a953324f6
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
20 02111-1307, USA. */
22 /* AIX requires this to be the first thing in the file. */
23 #ifndef __GNUC__
24 # ifdef _AIX
25 #pragma alloca
26 #endif
27 #endif
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include "bfd.h"
34 #include "libiberty.h"
35 #include "bucomm.h"
36 #include "getopt.h"
37 #include "dyn-string.h"
39 #include <ctype.h>
40 #include <time.h>
41 #include <sys/stat.h>
43 #ifdef ANSI_PROTOTYPES
44 #include <stdarg.h>
45 #else
46 #include <varargs.h>
47 #endif
49 #ifdef HAVE_SYS_WAIT_H
50 #include <sys/wait.h>
51 #else /* ! HAVE_SYS_WAIT_H */
52 #if ! defined (_WIN32) || defined (__CYGWIN32__)
53 #ifndef WIFEXITED
54 #define WIFEXITED(w) (((w)&0377) == 0)
55 #endif
56 #ifndef WIFSIGNALED
57 #define WIFSIGNALED(w) (((w)&0377) != 0177 && ((w)&~0377) == 0)
58 #endif
59 #ifndef WTERMSIG
60 #define WTERMSIG(w) ((w) & 0177)
61 #endif
62 #ifndef WEXITSTATUS
63 #define WEXITSTATUS(w) (((w) >> 8) & 0377)
64 #endif
65 #else /* defined (_WIN32) && ! defined (__CYGWIN32__) */
66 #ifndef WIFEXITED
67 #define WIFEXITED(w) (((w) & 0xff) == 0)
68 #endif
69 #ifndef WIFSIGNALED
70 #define WIFSIGNALED(w) (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f)
71 #endif
72 #ifndef WTERMSIG
73 #define WTERMSIG(w) ((w) & 0x7f)
74 #endif
75 #ifndef WEXITSTATUS
76 #define WEXITSTATUS(w) (((w) & 0xff00) >> 8)
77 #endif
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;
94 typedef enum {
95 UNKNOWN_TARGET,
96 CYGWIN_TARGET,
97 MINGW32_TARGET
99 target_type;
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:
131 display ()
132 inform ()
133 look_for_prog ()
134 deduce_name ()
135 It's not worth the hassle to break these out since dllwrap will
136 (hopefully) soon be retired in favor of `ld --shared. */
138 static void
139 display (message, args)
140 const char * message;
141 va_list args;
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);
153 static void
154 #ifdef __STDC__
155 inform (const char * message, ...)
156 #else
157 inform (message, va_alist)
158 const char * message;
159 va_dcl
160 #endif
162 va_list args;
164 if (!verbose)
165 return;
167 #ifdef __STDC__
168 va_start (args, message);
169 #else
170 va_start (args);
171 #endif
173 display (message, args);
175 va_end (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
181 appropriate. */
183 static char *
184 look_for_prog (prog_name, prefix, end_prefix)
185 const char *prog_name;
186 const char *prefix;
187 int end_prefix;
189 struct stat s;
190 char *cmd;
192 cmd = xmalloc (strlen (prefix)
193 + strlen (prog_name)
194 #ifdef HAVE_EXECUTABLE_SUFFIX
195 + strlen (EXECUTABLE_SUFFIX)
196 #endif
197 + 10);
198 strcpy (cmd, prefix);
200 sprintf (cmd + end_prefix, "%s", prog_name);
202 if (strchr (cmd, '/') != NULL)
204 int found;
206 found = (stat (cmd, &s) == 0
207 #ifdef HAVE_EXECUTABLE_SUFFIX
208 || stat (strcat (cmd, EXECUTABLE_SUFFIX), &s) == 0
209 #endif
212 if (! found)
214 /* xgettext:c-format */
215 inform (_("Tried file: %s"), cmd);
216 free (cmd);
217 return NULL;
221 /* xgettext:c-format */
222 inform (_("Using file: %s"), cmd);
224 return 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
239 /usr/local/bin/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. */
252 static char *
253 deduce_name (prog_name)
254 const char *prog_name;
256 char *cmd;
257 char *dash, *slash, *cp;
259 dash = NULL;
260 slash = NULL;
261 for (cp = program_name; *cp != '\0'; ++cp)
263 if (*cp == '-')
264 dash = cp;
265 if (
266 #if defined(__DJGPP__) || defined (__CYGWIN__) || defined(__WIN32__)
267 *cp == ':' || *cp == '\\' ||
268 #endif
269 *cp == '/')
271 slash = cp;
272 dash = NULL;
276 cmd = NULL;
278 if (dash != NULL)
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);
292 if (cmd == NULL)
294 /* Just return PROG_NAME as is. */
295 cmd = xstrdup (prog_name);
298 return cmd;
301 static void
302 delete_temp_files ()
304 if (delete_base_file && base_file_name)
306 if (verbose)
307 fprintf (stderr, "%s temporary base file %s\n",
308 dontdeltemps ? "Keeping" : "Deleting",
309 base_file_name);
310 if (! dontdeltemps)
312 unlink (base_file_name);
313 free (base_file_name);
317 if (delete_exp_file && exp_file_name)
319 if (verbose)
320 fprintf (stderr, "%s temporary exp file %s\n",
321 dontdeltemps ? "Keeping" : "Deleting",
322 exp_file_name);
323 if (! dontdeltemps)
325 unlink (exp_file_name);
326 free (exp_file_name);
329 if (delete_def_file && def_file_name)
331 if (verbose)
332 fprintf (stderr, "%s temporary def file %s\n",
333 dontdeltemps ? "Keeping" : "Deleting",
334 def_file_name);
335 if (! dontdeltemps)
337 unlink (def_file_name);
338 free (def_file_name);
343 static void
344 cleanup_and_exit (int status)
346 delete_temp_files ();
347 exit (status);
350 static int
351 run (what, args)
352 const char *what;
353 char *args;
355 char *s;
356 int pid, wait_status, retcode;
357 int i;
358 const char **argv;
359 char *errmsg_fmt, *errmsg_arg;
360 char *temp_base = choose_temp_base ();
361 int in_quote;
362 char sep;
364 if (verbose || dry_run)
365 fprintf (stderr, "%s %s\n", what, args);
367 /* Count the args */
368 i = 0;
369 for (s = args; *s; s++)
370 if (*s == ' ')
371 i++;
372 i++;
373 argv = alloca (sizeof (char *) * (i + 3));
374 i = 0;
375 argv[i++] = what;
376 s = args;
377 while (1)
379 while (*s == ' ' && *s != 0)
380 s++;
381 if (*s == 0)
382 break;
383 in_quote = (*s == '\'' || *s == '"');
384 sep = (in_quote) ? *s++ : ' ';
385 argv[i++] = s;
386 while (*s != sep && *s != 0)
387 s++;
388 if (*s == 0)
389 break;
390 *s++ = 0;
391 if (in_quote)
392 s++;
394 argv[i++] = NULL;
396 if (dry_run)
397 return 0;
399 pid = pexecute (argv[0], (char * const *) argv, program_name, temp_base,
400 &errmsg_fmt, &errmsg_arg, PEXECUTE_ONE | PEXECUTE_SEARCH);
402 if (pid == -1)
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));
409 return 1;
412 retcode = 0;
413 pid = pwait (pid, &wait_status, 0);
414 if (pid == -1)
416 fprintf (stderr, "%s: wait: %s\n", program_name, strerror (errno));
417 retcode = 1;
419 else if (WIFSIGNALED (wait_status))
421 fprintf (stderr, "%s: subprocess got fatal signal %d\n",
422 program_name, WTERMSIG (wait_status));
423 retcode = 1;
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));
431 retcode = 1;
434 else
435 retcode = 1;
437 return retcode;
440 static char *
441 mybasename (name)
442 const char *name;
444 const char *base = name;
446 while (*name)
448 if (*name == '/' || *name == '\\')
450 base = name + 1;
452 ++name;
454 return (char *) base;
457 static int
458 strhash (const char *str)
460 const unsigned char *s;
461 unsigned long hash;
462 unsigned int c;
463 unsigned int len;
465 hash = 0;
466 len = 0;
467 s = (const unsigned char *) str;
468 while ((c = *s++) != '\0')
470 hash += c + (c << 17);
471 hash ^= hash >> 2;
472 ++len;
474 hash += len + (len << 17);
475 hash ^= hash >> 2;
477 return hash;
480 /**********************************************************************/
482 void
483 print_version (name)
484 const char *name;
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");
490 printf ("\
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");
493 exit (0);
496 static void
497 usage (file, status)
498 FILE *file;
499 int status;
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");
538 exit (status);
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},
621 main (argc, argv)
622 int argc;
623 char **argv;
625 int c;
626 int i;
628 char **saved_argv = 0;
629 int cmdline_len = 0;
631 int export_all = 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]);
656 cmdline_len += len;
657 saved_argv[i] = arg;
658 dlltool_arg_indices[i] = 0;
659 driver_arg_indices[i] = 1;
661 cmdline_len++;
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. */
667 opterr = 0;
668 while ((c = getopt_long_only (argc, argv, "nkAqve:Uho:l:L:I:",
669 long_options, (int *) 0)) != EOF)
671 int dlltool_arg;
672 int driver_arg;
673 int single_word_option_value_pair;
675 dlltool_arg = 0;
676 driver_arg = 1;
677 single_word_option_value_pair = 0;
679 if (c != '?')
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. */
685 driver_arg = 0;
688 /* deal with generic and dllwrap options first. */
689 switch (c)
691 case 'h':
692 usage (stdout, 0);
693 break;
694 case 'q':
695 verbose = 0;
696 break;
697 case 'v':
698 verbose = 1;
699 break;
700 case OPTION_VERSION:
701 print_version (program_name);
702 break;
703 case 'e':
704 entry_point = optarg;
705 break;
706 case OPTION_IMAGE_BASE:
707 image_base_str = optarg;
708 break;
709 case OPTION_DEF:
710 def_file_name = optarg;
711 def_file_seen = 1;
712 delete_def_file = 0;
713 break;
714 case 'n':
715 dontdeltemps = 1;
716 dlltool_arg = 1;
717 break;
718 case 'o':
719 dll_file_name = optarg;
720 break;
721 case 'I':
722 case 'l':
723 case 'L':
724 driver_arg = 1;
725 break;
726 case OPTION_DLLNAME:
727 dll_name = optarg;
728 break;
729 case OPTION_DRY_RUN:
730 dry_run = 1;
731 break;
732 case OPTION_DRIVER_NAME:
733 driver_name = optarg;
734 break;
735 case OPTION_DRIVER_FLAGS:
736 driver_flags = optarg;
737 break;
738 case OPTION_DLLTOOL_NAME:
739 dlltool_name = optarg;
740 break;
741 case OPTION_TARGET:
742 target = optarg;
743 break;
744 case OPTION_MNO_CYGWIN:
745 target = "i386-mingw32";
746 break;
747 case OPTION_BASE_FILE:
748 base_file_name = optarg;
749 delete_base_file = 0;
750 break;
751 case OPTION_OUTPUT_EXP:
752 exp_file_name = optarg;
753 delete_exp_file = 0;
754 break;
755 case OPTION_EXPORT_ALL_SYMS:
756 export_all = 1;
757 break;
758 case OPTION_OUTPUT_LIB:
759 output_lib_file_name = optarg;
760 break;
761 case '?':
762 break;
763 default:
764 dlltool_arg = 1;
765 break;
768 /* Handle passing through --option=value case. */
769 if (optarg
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;
775 if (dlltool_arg)
777 dlltool_arg_indices[optind-1] = 1;
778 if (optarg && ! single_word_option_value_pair)
780 dlltool_arg_indices[optind-2] = 1;
784 if (! driver_arg)
786 driver_arg_indices[optind-1] = 0;
787 if (optarg && ! single_word_option_value_pair)
789 driver_arg_indices[optind-2] = 0;
794 /* sanity checks. */
795 if (! dll_name && ! dll_file_name)
797 fprintf (stderr,
798 "%s: Must provide at least one of -o or --dllname options\n",
799 program_name);
800 exit (1);
802 else if (! dll_name)
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");
818 if (! def_file_seen)
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);
824 delete_def_file = 1;
825 free (fileprefix);
826 delete_def_file = 1;
827 fprintf (stderr, "Warning: no export definition file provided\n");
828 fprintf (stderr,
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;
837 else
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);
842 if (verbose)
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)
868 case CYGWIN_TARGET:
869 driver_flags = cygwin_driver_flags;
870 break;
872 case MINGW32_TARGET:
873 driver_flags = mingw32_driver_flags;
874 break;
876 default:
877 driver_flags = generic_driver_flags;
878 break;
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)
889 case CYGWIN_TARGET:
890 entry_point = "__cygwin_dll_entry@12";
891 break;
893 case MINGW32_TARGET:
894 entry_point = "_DllMainCRTStartup@12";
895 break;
897 default:
898 entry_point = "_DllMain@12";
899 break;
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);
919 if (verbose)
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.
943 if (! def_file_seen)
945 int i;
946 dyn_string_t step_pre1;
948 step_pre1 = dyn_string_new (1024);
950 dyn_string_append (step_pre1, dlltool_cmdline->s);
951 if (export_all)
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);
988 if (verbose)
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;
1014 free (fileprefix);
1018 int quote;
1020 dyn_string_t step1 = dyn_string_new (driver_cmdline->length
1021 + strlen (base_file_name)
1022 + 20);
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;
1068 int quote;
1069 dyn_string_t step2 = dyn_string_new (dlltool_cmdline->length
1070 + strlen (base_file_name)
1071 + strlen (exp_file_name)
1072 + 20);
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 ...]
1112 int quote;
1114 dyn_string_t step3 = dyn_string_new (driver_cmdline->length
1115 + strlen (exp_file_name)
1116 + strlen (base_file_name)
1117 + 20);
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.
1153 int quote;
1154 dyn_string_t step4 = dyn_string_new (dlltool_cmdline->length
1155 + strlen (base_file_name)
1156 + strlen (exp_file_name)
1157 + 20);
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 ...]
1205 int quote;
1207 dyn_string_t step5 = dyn_string_new (driver_cmdline->length
1208 + strlen (exp_file_name)
1209 + 20);
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);
1232 return 0;