]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/dllwrap.c
2009-12-10 Michael Snyder <msnyder@vmware.com>
[thirdparty/binutils-gdb.git] / binutils / dllwrap.c
CommitLineData
252b5132 1/* dllwrap.c -- wrapper for DLLTOOL and GCC to generate PE style DLLs
92f01d61
JM
2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
3 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by Mumit Khan (khan@xraylith.wisc.edu).
5
6 This file is part of GNU Binutils.
7
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
32866df7 10 the Free Software Foundation; either version 3 of the License, or
252b5132
RH
11 (at your option) any later version.
12
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.
17
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
b43b5d5f
NC
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
252b5132
RH
22
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
29
3db64b00 30#include "sysdep.h"
252b5132
RH
31#include "bfd.h"
32#include "libiberty.h"
252b5132
RH
33#include "getopt.h"
34#include "dyn-string.h"
3db64b00 35#include "bucomm.h"
252b5132 36
252b5132 37#include <time.h>
bb0cb4db 38#include <sys/stat.h>
252b5132
RH
39
40#ifdef HAVE_SYS_WAIT_H
41#include <sys/wait.h>
42#else /* ! HAVE_SYS_WAIT_H */
43#if ! defined (_WIN32) || defined (__CYGWIN32__)
44#ifndef WIFEXITED
45#define WIFEXITED(w) (((w)&0377) == 0)
46#endif
47#ifndef WIFSIGNALED
48#define WIFSIGNALED(w) (((w)&0377) != 0177 && ((w)&~0377) == 0)
49#endif
50#ifndef WTERMSIG
51#define WTERMSIG(w) ((w) & 0177)
52#endif
53#ifndef WEXITSTATUS
54#define WEXITSTATUS(w) (((w) >> 8) & 0377)
55#endif
56#else /* defined (_WIN32) && ! defined (__CYGWIN32__) */
57#ifndef WIFEXITED
58#define WIFEXITED(w) (((w) & 0xff) == 0)
59#endif
60#ifndef WIFSIGNALED
61#define WIFSIGNALED(w) (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f)
62#endif
63#ifndef WTERMSIG
64#define WTERMSIG(w) ((w) & 0x7f)
65#endif
66#ifndef WEXITSTATUS
67#define WEXITSTATUS(w) (((w) & 0xff00) >> 8)
68#endif
69#endif /* defined (_WIN32) && ! defined (__CYGWIN32__) */
70#endif /* ! HAVE_SYS_WAIT_H */
71
bb0cb4db 72static char *driver_name = NULL;
26044998 73static char *cygwin_driver_flags =
252b5132
RH
74 "-Wl,--dll -nostartfiles";
75static char *mingw32_driver_flags = "-mdll";
76static char *generic_driver_flags = "-Wl,--dll";
77
78static char *entry_point;
79
bb0cb4db 80static char *dlltool_name = NULL;
252b5132
RH
81
82static char *target = TARGET;
83
fa0d16be
KT
84/* -1: use default, 0: no underscoring, 1: underscore. */
85static int is_leading_underscore = -1;
86
252b5132 87typedef enum {
26044998
KH
88 UNKNOWN_TARGET,
89 CYGWIN_TARGET,
087f88b2 90 MINGW_TARGET
26044998 91}
252b5132
RH
92target_type;
93
fa0d16be
KT
94typedef enum {
95 UNKNOWN_CPU,
96 X86_CPU,
97 X64_CPU,
98 ARM_CPU
99}
100target_cpu;
101
252b5132 102static target_type which_target = UNKNOWN_TARGET;
fa0d16be 103static target_cpu which_cpu = UNKNOWN_CPU;
252b5132
RH
104
105static int dontdeltemps = 0;
106static int dry_run = 0;
107
61513dc1 108static char *prog_name;
252b5132
RH
109
110static int verbose = 0;
111
112static char *dll_file_name;
113static char *dll_name;
114static char *base_file_name;
115static char *exp_file_name;
116static char *def_file_name;
117static int delete_base_file = 1;
118static int delete_exp_file = 1;
119static int delete_def_file = 1;
120
2da42df6
AJ
121static int run (const char *, char *);
122static char *mybasename (const char *);
123static int strhash (const char *);
124static void usage (FILE *, int);
0fd3a477
JW
125static void display (const char *, va_list) ATTRIBUTE_PRINTF(1,0);
126static void inform (const char *, ...) ATTRIBUTE_PRINTF_1;
127static void warn (const char *, ...) ATTRIBUTE_PRINTF_1;
2da42df6
AJ
128static char *look_for_prog (const char *, const char *, int);
129static char *deduce_name (const char *);
130static void delete_temp_files (void);
131static void cleanup_and_exit (int);
252b5132
RH
132
133/**********************************************************************/
134
bb0cb4db
ILT
135/* Please keep the following 4 routines in sync with dlltool.c:
136 display ()
137 inform ()
138 look_for_prog ()
139 deduce_name ()
140 It's not worth the hassle to break these out since dllwrap will
141 (hopefully) soon be retired in favor of `ld --shared. */
142
143static void
2da42df6 144display (const char * message, va_list args)
bb0cb4db 145{
61513dc1
NC
146 if (prog_name != NULL)
147 fprintf (stderr, "%s: ", prog_name);
bb0cb4db
ILT
148
149 vfprintf (stderr, message, args);
37cc8ec1
AM
150 fputc ('\n', stderr);
151}
bb0cb4db
ILT
152
153
37cc8ec1 154static void
e80ff7de 155inform VPARAMS ((const char *message, ...))
37cc8ec1 156{
e80ff7de
AM
157 VA_OPEN (args, message);
158 VA_FIXEDARG (args, const char *, message);
37cc8ec1
AM
159
160 if (!verbose)
161 return;
162
37cc8ec1 163 display (message, args);
bb0cb4db 164
e80ff7de 165 VA_CLOSE (args);
bb0cb4db
ILT
166}
167
37cc8ec1 168static void
e80ff7de 169warn VPARAMS ((const char *format, ...))
37cc8ec1 170{
e80ff7de
AM
171 VA_OPEN (args, format);
172 VA_FIXEDARG (args, const char *, format);
37cc8ec1 173
37cc8ec1 174 display (format, args);
e80ff7de
AM
175
176 VA_CLOSE (args);
37cc8ec1 177}
37cc8ec1 178
bb0cb4db
ILT
179/* Look for the program formed by concatenating PROG_NAME and the
180 string running from PREFIX to END_PREFIX. If the concatenated
181 string contains a '/', try appending EXECUTABLE_SUFFIX if it is
2481e6a2 182 appropriate. */
bb0cb4db
ILT
183
184static char *
2da42df6 185look_for_prog (const char *prog_name, const char *prefix, int end_prefix)
bb0cb4db
ILT
186{
187 struct stat s;
188 char *cmd;
189
26044998
KH
190 cmd = xmalloc (strlen (prefix)
191 + strlen (prog_name)
2481e6a2 192#ifdef HAVE_EXECUTABLE_SUFFIX
26044998 193 + strlen (EXECUTABLE_SUFFIX)
bb0cb4db
ILT
194#endif
195 + 10);
196 strcpy (cmd, prefix);
197
198 sprintf (cmd + end_prefix, "%s", prog_name);
199
200 if (strchr (cmd, '/') != NULL)
201 {
202 int found;
203
204 found = (stat (cmd, &s) == 0
2481e6a2 205#ifdef HAVE_EXECUTABLE_SUFFIX
26044998 206 || stat (strcat (cmd, EXECUTABLE_SUFFIX), &s) == 0
bb0cb4db
ILT
207#endif
208 );
209
210 if (! found)
26044998 211 {
bb0cb4db
ILT
212 /* xgettext:c-format */
213 inform (_("Tried file: %s"), cmd);
214 free (cmd);
215 return NULL;
216 }
217 }
218
219 /* xgettext:c-format */
220 inform (_("Using file: %s"), cmd);
221
222 return cmd;
223}
224
225/* Deduce the name of the program we are want to invoke.
226 PROG_NAME is the basic name of the program we want to run,
227 eg "as" or "ld". The catch is that we might want actually
26044998 228 run "i386-pe-as" or "ppc-pe-ld".
bb0cb4db
ILT
229
230 If argv[0] contains the full path, then try to find the program
231 in the same place, with and then without a target-like prefix.
232
233 Given, argv[0] = /usr/local/bin/i586-cygwin32-dlltool,
26044998 234 deduce_name("as") uses the following search order:
bb0cb4db
ILT
235
236 /usr/local/bin/i586-cygwin32-as
237 /usr/local/bin/as
238 as
26044998 239
bb0cb4db
ILT
240 If there's an EXECUTABLE_SUFFIX, it'll use that as well; for each
241 name, it'll try without and then with EXECUTABLE_SUFFIX.
242
243 Given, argv[0] = i586-cygwin32-dlltool, it will not even try "as"
244 as the fallback, but rather return i586-cygwin32-as.
26044998 245
bb0cb4db
ILT
246 Oh, and given, argv[0] = dlltool, it'll return "as".
247
248 Returns a dynamically allocated string. */
249
250static char *
61513dc1 251deduce_name (const char * name)
bb0cb4db
ILT
252{
253 char *cmd;
61513dc1
NC
254 const char *dash;
255 const char *slash;
256 const char *cp;
bb0cb4db
ILT
257
258 dash = NULL;
259 slash = NULL;
d3cde3af 260 for (cp = prog_name; *cp != '\0'; ++cp)
bb0cb4db
ILT
261 {
262 if (*cp == '-')
263 dash = cp;
61513dc1 264
bb0cb4db
ILT
265 if (
266#if defined(__DJGPP__) || defined (__CYGWIN__) || defined(__WIN32__)
267 *cp == ':' || *cp == '\\' ||
268#endif
269 *cp == '/')
270 {
271 slash = cp;
272 dash = NULL;
273 }
274 }
275
276 cmd = NULL;
277
278 if (dash != NULL)
d3cde3af
NC
279 /* First, try looking for a prefixed NAME in the
280 PROG_NAME directory, with the same prefix as PROG_NAME. */
281 cmd = look_for_prog (name, prog_name, dash - prog_name + 1);
bb0cb4db
ILT
282
283 if (slash != NULL && cmd == NULL)
d3cde3af 284 /* Next, try looking for a NAME in the same directory as
61513dc1 285 that of this program. */
d3cde3af 286 cmd = look_for_prog (name, prog_name, slash - prog_name + 1);
bb0cb4db
ILT
287
288 if (cmd == NULL)
d3cde3af 289 /* Just return NAME as is. */
61513dc1 290 cmd = xstrdup (name);
bb0cb4db
ILT
291
292 return cmd;
293}
294
252b5132 295static void
2da42df6 296delete_temp_files (void)
252b5132
RH
297{
298 if (delete_base_file && base_file_name)
299 {
300 if (verbose)
37cc8ec1
AM
301 {
302 if (dontdeltemps)
303 warn (_("Keeping temporary base file %s"), base_file_name);
304 else
305 warn (_("Deleting temporary base file %s"), base_file_name);
306 }
252b5132 307 if (! dontdeltemps)
26044998
KH
308 {
309 unlink (base_file_name);
252b5132
RH
310 free (base_file_name);
311 }
312 }
26044998 313
252b5132
RH
314 if (delete_exp_file && exp_file_name)
315 {
316 if (verbose)
37cc8ec1
AM
317 {
318 if (dontdeltemps)
319 warn (_("Keeping temporary exp file %s"), exp_file_name);
320 else
321 warn (_("Deleting temporary exp file %s"), exp_file_name);
322 }
252b5132 323 if (! dontdeltemps)
26044998
KH
324 {
325 unlink (exp_file_name);
326 free (exp_file_name);
252b5132
RH
327 }
328 }
329 if (delete_def_file && def_file_name)
330 {
331 if (verbose)
37cc8ec1
AM
332 {
333 if (dontdeltemps)
334 warn (_("Keeping temporary def file %s"), def_file_name);
335 else
336 warn (_("Deleting temporary def file %s"), def_file_name);
337 }
252b5132 338 if (! dontdeltemps)
26044998
KH
339 {
340 unlink (def_file_name);
341 free (def_file_name);
252b5132
RH
342 }
343 }
344}
345
26044998 346static void
2da42df6 347cleanup_and_exit (int status)
252b5132
RH
348{
349 delete_temp_files ();
350 exit (status);
351}
26044998 352
252b5132 353static int
2da42df6 354run (const char *what, char *args)
252b5132
RH
355{
356 char *s;
357 int pid, wait_status, retcode;
358 int i;
359 const char **argv;
360 char *errmsg_fmt, *errmsg_arg;
361 char *temp_base = choose_temp_base ();
362 int in_quote;
363 char sep;
364
365 if (verbose || dry_run)
366 fprintf (stderr, "%s %s\n", what, args);
367
368 /* Count the args */
369 i = 0;
370 for (s = args; *s; s++)
371 if (*s == ' ')
372 i++;
373 i++;
374 argv = alloca (sizeof (char *) * (i + 3));
375 i = 0;
376 argv[i++] = what;
377 s = args;
378 while (1)
379 {
380 while (*s == ' ' && *s != 0)
381 s++;
382 if (*s == 0)
383 break;
384 in_quote = (*s == '\'' || *s == '"');
385 sep = (in_quote) ? *s++ : ' ';
386 argv[i++] = s;
387 while (*s != sep && *s != 0)
388 s++;
389 if (*s == 0)
390 break;
391 *s++ = 0;
392 if (in_quote)
26044998 393 s++;
252b5132
RH
394 }
395 argv[i++] = NULL;
396
397 if (dry_run)
398 return 0;
399
61513dc1 400 pid = pexecute (argv[0], (char * const *) argv, prog_name, temp_base,
252b5132
RH
401 &errmsg_fmt, &errmsg_arg, PEXECUTE_ONE | PEXECUTE_SEARCH);
402
403 if (pid == -1)
404 {
405 int errno_val = errno;
406
61513dc1 407 fprintf (stderr, "%s: ", prog_name);
252b5132
RH
408 fprintf (stderr, errmsg_fmt, errmsg_arg);
409 fprintf (stderr, ": %s\n", strerror (errno_val));
410 return 1;
411 }
412
413 retcode = 0;
414 pid = pwait (pid, &wait_status, 0);
415 if (pid == -1)
416 {
37cc8ec1 417 warn ("wait: %s", strerror (errno));
252b5132
RH
418 retcode = 1;
419 }
420 else if (WIFSIGNALED (wait_status))
421 {
37cc8ec1 422 warn (_("subprocess got fatal signal %d"), WTERMSIG (wait_status));
252b5132
RH
423 retcode = 1;
424 }
425 else if (WIFEXITED (wait_status))
426 {
427 if (WEXITSTATUS (wait_status) != 0)
428 {
37cc8ec1 429 warn (_("%s exited with status %d"), what, WEXITSTATUS (wait_status));
252b5132
RH
430 retcode = 1;
431 }
432 }
433 else
434 retcode = 1;
26044998 435
252b5132
RH
436 return retcode;
437}
438
439static char *
2da42df6 440mybasename (const char *name)
252b5132
RH
441{
442 const char *base = name;
443
444 while (*name)
445 {
446 if (*name == '/' || *name == '\\')
447 {
448 base = name + 1;
449 }
450 ++name;
451 }
452 return (char *) base;
453}
454
26044998 455static int
2da42df6 456strhash (const char *str)
252b5132
RH
457{
458 const unsigned char *s;
459 unsigned long hash;
460 unsigned int c;
461 unsigned int len;
462
463 hash = 0;
464 len = 0;
465 s = (const unsigned char *) str;
466 while ((c = *s++) != '\0')
467 {
468 hash += c + (c << 17);
469 hash ^= hash >> 2;
470 ++len;
471 }
472 hash += len + (len << 17);
473 hash ^= hash >> 2;
474
475 return hash;
476}
477
478/**********************************************************************/
479
252b5132 480static void
2da42df6 481usage (FILE *file, int status)
252b5132 482{
61513dc1 483 fprintf (file, _("Usage %s <option(s)> <object-file(s)>\n"), prog_name);
37cc8ec1 484 fprintf (file, _(" Generic options:\n"));
07012eee 485 fprintf (file, _(" @<file> Read options from <file>\n"));
37cc8ec1
AM
486 fprintf (file, _(" --quiet, -q Work quietly\n"));
487 fprintf (file, _(" --verbose, -v Verbose\n"));
488 fprintf (file, _(" --version Print dllwrap version\n"));
489 fprintf (file, _(" --implib <outname> Synonym for --output-lib\n"));
61513dc1 490 fprintf (file, _(" Options for %s:\n"), prog_name);
37cc8ec1
AM
491 fprintf (file, _(" --driver-name <driver> Defaults to \"gcc\"\n"));
492 fprintf (file, _(" --driver-flags <flags> Override default ld flags\n"));
493 fprintf (file, _(" --dlltool-name <dlltool> Defaults to \"dlltool\"\n"));
494 fprintf (file, _(" --entry <entry> Specify alternate DLL entry point\n"));
495 fprintf (file, _(" --image-base <base> Specify image base address\n"));
496 fprintf (file, _(" --target <machine> i386-cygwin32 or i386-mingw32\n"));
497 fprintf (file, _(" --dry-run Show what needs to be run\n"));
498 fprintf (file, _(" --mno-cygwin Create Mingw DLL\n"));
499 fprintf (file, _(" Options passed to DLLTOOL:\n"));
500 fprintf (file, _(" --machine <machine>\n"));
501 fprintf (file, _(" --output-exp <outname> Generate export file.\n"));
502 fprintf (file, _(" --output-lib <outname> Generate input library.\n"));
503 fprintf (file, _(" --add-indirect Add dll indirects to export file.\n"));
504 fprintf (file, _(" --dllname <name> Name of input dll to put into output lib.\n"));
505 fprintf (file, _(" --def <deffile> Name input .def file\n"));
506 fprintf (file, _(" --output-def <deffile> Name output .def file\n"));
507 fprintf (file, _(" --export-all-symbols Export all symbols to .def\n"));
508 fprintf (file, _(" --no-export-all-symbols Only export .drectve symbols\n"));
509 fprintf (file, _(" --exclude-symbols <list> Exclude <list> from .def\n"));
510 fprintf (file, _(" --no-default-excludes Zap default exclude symbols\n"));
511 fprintf (file, _(" --base-file <basefile> Read linker generated base file\n"));
512 fprintf (file, _(" --no-idata4 Don't generate idata$4 section\n"));
513 fprintf (file, _(" --no-idata5 Don't generate idata$5 section\n"));
514 fprintf (file, _(" -U Add underscores to .lib\n"));
515 fprintf (file, _(" -k Kill @<n> from exported names\n"));
516 fprintf (file, _(" --add-stdcall-alias Add aliases without @<n>\n"));
517 fprintf (file, _(" --as <name> Use <name> for assembler\n"));
518 fprintf (file, _(" --nodelete Keep temp files.\n"));
fa0d16be
KT
519 fprintf (file, _(" --no-leading-underscore Entrypoint without underscore\n"));
520 fprintf (file, _(" --leading-underscore Entrypoint with underscore.\n"));
37cc8ec1 521 fprintf (file, _(" Rest are passed unmodified to the language driver\n"));
252b5132 522 fprintf (file, "\n\n");
92f01d61
JM
523 if (REPORT_BUGS_TO[0] && status == 0)
524 fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132
RH
525 exit (status);
526}
527
528#define OPTION_START 149
529
26044998 530/* GENERIC options. */
252b5132
RH
531#define OPTION_QUIET (OPTION_START + 1)
532#define OPTION_VERBOSE (OPTION_QUIET + 1)
533#define OPTION_VERSION (OPTION_VERBOSE + 1)
534
26044998 535/* DLLWRAP options. */
252b5132
RH
536#define OPTION_DRY_RUN (OPTION_VERSION + 1)
537#define OPTION_DRIVER_NAME (OPTION_DRY_RUN + 1)
538#define OPTION_DRIVER_FLAGS (OPTION_DRIVER_NAME + 1)
539#define OPTION_DLLTOOL_NAME (OPTION_DRIVER_FLAGS + 1)
540#define OPTION_ENTRY (OPTION_DLLTOOL_NAME + 1)
541#define OPTION_IMAGE_BASE (OPTION_ENTRY + 1)
542#define OPTION_TARGET (OPTION_IMAGE_BASE + 1)
bb0cb4db 543#define OPTION_MNO_CYGWIN (OPTION_TARGET + 1)
fa0d16be
KT
544#define OPTION_NO_LEADING_UNDERSCORE (OPTION_MNO_CYGWIN + 1)
545#define OPTION_LEADING_UNDERSCORE (OPTION_NO_LEADING_UNDERSCORE + 1)
252b5132 546
26044998 547/* DLLTOOL options. */
fa0d16be 548#define OPTION_NODELETE (OPTION_LEADING_UNDERSCORE + 1)
252b5132 549#define OPTION_DLLNAME (OPTION_NODELETE + 1)
2da42df6 550#define OPTION_NO_IDATA4 (OPTION_DLLNAME + 1)
252b5132
RH
551#define OPTION_NO_IDATA5 (OPTION_NO_IDATA4 + 1)
552#define OPTION_OUTPUT_EXP (OPTION_NO_IDATA5 + 1)
553#define OPTION_OUTPUT_DEF (OPTION_OUTPUT_EXP + 1)
554#define OPTION_EXPORT_ALL_SYMS (OPTION_OUTPUT_DEF + 1)
555#define OPTION_NO_EXPORT_ALL_SYMS (OPTION_EXPORT_ALL_SYMS + 1)
556#define OPTION_EXCLUDE_SYMS (OPTION_NO_EXPORT_ALL_SYMS + 1)
557#define OPTION_NO_DEFAULT_EXCLUDES (OPTION_EXCLUDE_SYMS + 1)
558#define OPTION_OUTPUT_LIB (OPTION_NO_DEFAULT_EXCLUDES + 1)
559#define OPTION_DEF (OPTION_OUTPUT_LIB + 1)
560#define OPTION_ADD_UNDERSCORE (OPTION_DEF + 1)
561#define OPTION_KILLAT (OPTION_ADD_UNDERSCORE + 1)
562#define OPTION_HELP (OPTION_KILLAT + 1)
563#define OPTION_MACHINE (OPTION_HELP + 1)
564#define OPTION_ADD_INDIRECT (OPTION_MACHINE + 1)
565#define OPTION_BASE_FILE (OPTION_ADD_INDIRECT + 1)
566#define OPTION_AS (OPTION_BASE_FILE + 1)
567
568static const struct option long_options[] =
569{
26044998 570 /* generic options. */
252b5132
RH
571 {"quiet", no_argument, NULL, 'q'},
572 {"verbose", no_argument, NULL, 'v'},
573 {"version", no_argument, NULL, OPTION_VERSION},
574 {"implib", required_argument, NULL, OPTION_OUTPUT_LIB},
575
26044998 576 /* dllwrap options. */
252b5132
RH
577 {"dry-run", no_argument, NULL, OPTION_DRY_RUN},
578 {"driver-name", required_argument, NULL, OPTION_DRIVER_NAME},
579 {"driver-flags", required_argument, NULL, OPTION_DRIVER_FLAGS},
580 {"dlltool-name", required_argument, NULL, OPTION_DLLTOOL_NAME},
581 {"entry", required_argument, NULL, 'e'},
582 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
583 {"target", required_argument, NULL, OPTION_TARGET},
fa0d16be
KT
584 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
585 {"leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
252b5132 586
26044998 587 /* dlltool options. */
252b5132
RH
588 {"no-delete", no_argument, NULL, 'n'},
589 {"dllname", required_argument, NULL, OPTION_DLLNAME},
590 {"no-idata4", no_argument, NULL, OPTION_NO_IDATA4},
591 {"no-idata5", no_argument, NULL, OPTION_NO_IDATA5},
592 {"output-exp", required_argument, NULL, OPTION_OUTPUT_EXP},
593 {"output-def", required_argument, NULL, OPTION_OUTPUT_DEF},
594 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL_SYMS},
595 {"no-export-all-symbols", no_argument, NULL, OPTION_NO_EXPORT_ALL_SYMS},
596 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMS},
597 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
598 {"output-lib", required_argument, NULL, OPTION_OUTPUT_LIB},
599 {"def", required_argument, NULL, OPTION_DEF},
600 {"add-underscore", no_argument, NULL, 'U'},
601 {"killat", no_argument, NULL, 'k'},
602 {"add-stdcall-alias", no_argument, NULL, 'A'},
603 {"help", no_argument, NULL, 'h'},
604 {"machine", required_argument, NULL, OPTION_MACHINE},
605 {"add-indirect", no_argument, NULL, OPTION_ADD_INDIRECT},
606 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
607 {"as", required_argument, NULL, OPTION_AS},
37cc8ec1 608 {0, 0, 0, 0}
252b5132
RH
609};
610
2da42df6 611int main (int, char **);
e80ff7de 612
252b5132 613int
2da42df6 614main (int argc, char **argv)
252b5132
RH
615{
616 int c;
617 int i;
618
619 char **saved_argv = 0;
620 int cmdline_len = 0;
621
622 int export_all = 0;
623
624 int *dlltool_arg_indices;
625 int *driver_arg_indices;
626
627 char *driver_flags = 0;
628 char *output_lib_file_name = 0;
629
630 dyn_string_t dlltool_cmdline;
631 dyn_string_t driver_cmdline;
632
633 int def_file_seen = 0;
634
635 char *image_base_str = 0;
636
61513dc1 637 prog_name = argv[0];
252b5132 638
3882b010
L
639#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
640 setlocale (LC_MESSAGES, "");
641#endif
642#if defined (HAVE_SETLOCALE)
643 setlocale (LC_CTYPE, "");
644#endif
645 bindtextdomain (PACKAGE, LOCALEDIR);
646 textdomain (PACKAGE);
647
869b9d07
MM
648 expandargv (&argc, &argv);
649
252b5132
RH
650 saved_argv = (char **) xmalloc (argc * sizeof (char*));
651 dlltool_arg_indices = (int *) xmalloc (argc * sizeof (int));
652 driver_arg_indices = (int *) xmalloc (argc * sizeof (int));
26044998 653 for (i = 0; i < argc; ++i)
252b5132
RH
654 {
655 size_t len = strlen (argv[i]);
656 char *arg = (char *) xmalloc (len + 1);
657 strcpy (arg, argv[i]);
658 cmdline_len += len;
659 saved_argv[i] = arg;
660 dlltool_arg_indices[i] = 0;
661 driver_arg_indices[i] = 1;
662 }
663 cmdline_len++;
664
665 /* We recognize dllwrap and dlltool options, and everything else is
666 passed onto the language driver (eg., to GCC). We collect options
26044998
KH
667 to dlltool and driver in dlltool_args and driver_args. */
668
252b5132 669 opterr = 0;
26044998
KH
670 while ((c = getopt_long_only (argc, argv, "nkAqve:Uho:l:L:I:",
671 long_options, (int *) 0)) != EOF)
252b5132
RH
672 {
673 int dlltool_arg;
674 int driver_arg;
675 int single_word_option_value_pair;
676
677 dlltool_arg = 0;
678 driver_arg = 1;
679 single_word_option_value_pair = 0;
680
681 if (c != '?')
26044998 682 {
252b5132
RH
683 /* We recognize this option, so it has to be either dllwrap or
684 dlltool option. Do not pass to driver unless it's one of the
685 generic options that are passed to all the tools (such as -v)
26044998 686 which are dealt with later. */
252b5132
RH
687 driver_arg = 0;
688 }
689
26044998 690 /* deal with generic and dllwrap options first. */
252b5132
RH
691 switch (c)
692 {
693 case 'h':
694 usage (stdout, 0);
695 break;
696 case 'q':
697 verbose = 0;
698 break;
699 case 'v':
700 verbose = 1;
701 break;
702 case OPTION_VERSION:
61513dc1 703 print_version (prog_name);
252b5132
RH
704 break;
705 case 'e':
706 entry_point = optarg;
707 break;
708 case OPTION_IMAGE_BASE:
709 image_base_str = optarg;
710 break;
711 case OPTION_DEF:
712 def_file_name = optarg;
713 def_file_seen = 1;
714 delete_def_file = 0;
715 break;
716 case 'n':
717 dontdeltemps = 1;
718 dlltool_arg = 1;
719 break;
720 case 'o':
721 dll_file_name = optarg;
722 break;
723 case 'I':
724 case 'l':
725 case 'L':
726 driver_arg = 1;
727 break;
728 case OPTION_DLLNAME:
729 dll_name = optarg;
730 break;
731 case OPTION_DRY_RUN:
732 dry_run = 1;
733 break;
734 case OPTION_DRIVER_NAME:
735 driver_name = optarg;
736 break;
737 case OPTION_DRIVER_FLAGS:
738 driver_flags = optarg;
739 break;
740 case OPTION_DLLTOOL_NAME:
741 dlltool_name = optarg;
742 break;
743 case OPTION_TARGET:
744 target = optarg;
745 break;
bb0cb4db
ILT
746 case OPTION_MNO_CYGWIN:
747 target = "i386-mingw32";
748 break;
fa0d16be
KT
749 case OPTION_NO_LEADING_UNDERSCORE:
750 is_leading_underscore = 0;
751 break;
752 case OPTION_LEADING_UNDERSCORE:
753 is_leading_underscore = 1;
754 break;
252b5132
RH
755 case OPTION_BASE_FILE:
756 base_file_name = optarg;
757 delete_base_file = 0;
758 break;
759 case OPTION_OUTPUT_EXP:
760 exp_file_name = optarg;
761 delete_exp_file = 0;
762 break;
763 case OPTION_EXPORT_ALL_SYMS:
764 export_all = 1;
765 break;
766 case OPTION_OUTPUT_LIB:
767 output_lib_file_name = optarg;
768 break;
769 case '?':
770 break;
771 default:
772 dlltool_arg = 1;
773 break;
774 }
26044998
KH
775
776 /* Handle passing through --option=value case. */
777 if (optarg
778 && saved_argv[optind-1][0] == '-'
779 && saved_argv[optind-1][1] == '-'
252b5132
RH
780 && strchr (saved_argv[optind-1], '='))
781 single_word_option_value_pair = 1;
782
783 if (dlltool_arg)
26044998 784 {
252b5132
RH
785 dlltool_arg_indices[optind-1] = 1;
786 if (optarg && ! single_word_option_value_pair)
787 {
788 dlltool_arg_indices[optind-2] = 1;
26044998 789 }
252b5132
RH
790 }
791
792 if (! driver_arg)
26044998 793 {
252b5132
RH
794 driver_arg_indices[optind-1] = 0;
795 if (optarg && ! single_word_option_value_pair)
796 {
797 driver_arg_indices[optind-2] = 0;
26044998 798 }
252b5132
RH
799 }
800 }
bb0cb4db 801
61513dc1 802 /* Sanity checks. */
252b5132
RH
803 if (! dll_name && ! dll_file_name)
804 {
37cc8ec1 805 warn (_("Must provide at least one of -o or --dllname options"));
252b5132
RH
806 exit (1);
807 }
808 else if (! dll_name)
809 {
810 dll_name = xstrdup (mybasename (dll_file_name));
811 }
812 else if (! dll_file_name)
813 {
814 dll_file_name = xstrdup (dll_name);
815 }
bb0cb4db 816
26044998 817 /* Deduce driver-name and dlltool-name from our own. */
bb0cb4db
ILT
818 if (driver_name == NULL)
819 driver_name = deduce_name ("gcc");
820
821 if (dlltool_name == NULL)
822 dlltool_name = deduce_name ("dlltool");
823
252b5132
RH
824 if (! def_file_seen)
825 {
826 char *fileprefix = choose_temp_base ();
61513dc1 827
252b5132 828 def_file_name = (char *) xmalloc (strlen (fileprefix) + 5);
26044998
KH
829 sprintf (def_file_name, "%s.def",
830 (dontdeltemps) ? mybasename (fileprefix) : fileprefix);
252b5132
RH
831 delete_def_file = 1;
832 free (fileprefix);
833 delete_def_file = 1;
e3c8793a
NC
834 warn (_("no export definition file provided.\n\
835Creating one, but that may not be what you want"));
252b5132 836 }
26044998 837
61513dc1 838 /* Set the target platform. */
087f88b2 839 if (strstr (target, "cygwin"))
252b5132 840 which_target = CYGWIN_TARGET;
087f88b2
NC
841 else if (strstr (target, "mingw"))
842 which_target = MINGW_TARGET;
26044998 843 else
252b5132
RH
844 which_target = UNKNOWN_TARGET;
845
fa0d16be
KT
846 if (! strncmp (target, "arm", 3))
847 which_cpu = ARM_CPU;
848 else if (!strncmp (target, "x86_64", 6)
849 || !strncmp (target, "athlon64", 8)
850 || !strncmp (target, "amd64", 5))
851 which_cpu = X64_CPU;
852 else if (target[0] == 'i' && (target[1] >= '3' && target[1] <= '6')
853 && target[2] == '8' && target[3] == '6')
854 which_cpu = X86_CPU;
855 else
856 which_cpu = UNKNOWN_CPU;
857
61513dc1 858 /* Re-create the command lines as a string, taking care to quote stuff. */
252b5132
RH
859 dlltool_cmdline = dyn_string_new (cmdline_len);
860 if (verbose)
61513dc1
NC
861 dyn_string_append_cstr (dlltool_cmdline, " -v");
862
7f143ac1
DD
863 dyn_string_append_cstr (dlltool_cmdline, " --dllname ");
864 dyn_string_append_cstr (dlltool_cmdline, dll_name);
252b5132
RH
865
866 for (i = 1; i < argc; ++i)
867 {
868 if (dlltool_arg_indices[i])
26044998 869 {
252b5132 870 char *arg = saved_argv[i];
26044998
KH
871 int quote = (strchr (arg, ' ') || strchr (arg, '\t'));
872 dyn_string_append_cstr (dlltool_cmdline,
252b5132 873 (quote) ? " \"" : " ");
7f143ac1 874 dyn_string_append_cstr (dlltool_cmdline, arg);
26044998 875 dyn_string_append_cstr (dlltool_cmdline,
252b5132
RH
876 (quote) ? "\"" : "");
877 }
878 }
879
880 driver_cmdline = dyn_string_new (cmdline_len);
881 if (! driver_flags || strlen (driver_flags) == 0)
882 {
883 switch (which_target)
26044998 884 {
252b5132 885 case CYGWIN_TARGET:
26044998 886 driver_flags = cygwin_driver_flags;
252b5132 887 break;
26044998 888
087f88b2 889 case MINGW_TARGET:
26044998 890 driver_flags = mingw32_driver_flags;
252b5132 891 break;
26044998 892
252b5132 893 default:
26044998 894 driver_flags = generic_driver_flags;
252b5132
RH
895 break;
896 }
897 }
7f143ac1
DD
898 dyn_string_append_cstr (driver_cmdline, driver_flags);
899 dyn_string_append_cstr (driver_cmdline, " -o ");
900 dyn_string_append_cstr (driver_cmdline, dll_file_name);
252b5132 901
fa0d16be
KT
902 if (is_leading_underscore == 0)
903 dyn_string_append_cstr (driver_cmdline, " --no-leading-underscore");
904 else if (is_leading_underscore == 1)
905 dyn_string_append_cstr (driver_cmdline, " --leading-underscore");
906
252b5132
RH
907 if (! entry_point || strlen (entry_point) == 0)
908 {
fa0d16be
KT
909 const char *prefix = (is_leading_underscore != 0 ? "_" : "");
910 const char *postfix = "";
911 const char *name_entry;
912
913 if (which_cpu == X86_CPU || which_cpu == UNKNOWN_CPU)
914 postfix = "@12";
915
252b5132 916 switch (which_target)
26044998 917 {
252b5132 918 case CYGWIN_TARGET:
fa0d16be 919 name_entry = "_cygwin_dll_entry";
252b5132 920 break;
26044998 921
087f88b2 922 case MINGW_TARGET:
fa0d16be 923 name_entry = "DllMainCRTStartup";
252b5132 924 break;
26044998 925
252b5132 926 default:
fa0d16be 927 name_entry = "DllMain";
252b5132
RH
928 break;
929 }
fa0d16be
KT
930 entry_point =
931 (char *) malloc (strlen (name_entry) + strlen (prefix)
932 + strlen (postfix) + 1);
933 sprintf (entry_point, "%s%s%s", prefix, name_entry, postfix);
252b5132 934 }
7f143ac1
DD
935 dyn_string_append_cstr (driver_cmdline, " -Wl,-e,");
936 dyn_string_append_cstr (driver_cmdline, entry_point);
937 dyn_string_append_cstr (dlltool_cmdline, " --exclude-symbol=");
26044998
KH
938 dyn_string_append_cstr (dlltool_cmdline,
939 (entry_point[0] == '_') ? entry_point+1 : entry_point);
252b5132
RH
940
941 if (! image_base_str || strlen (image_base_str) == 0)
942 {
943 char *tmpbuf = (char *) xmalloc (sizeof ("0x12345678") + 1);
944 unsigned long hash = strhash (dll_file_name);
945 sprintf (tmpbuf, "0x%.8lX", 0x60000000|((hash<<16)&0xFFC0000));
946 image_base_str = tmpbuf;
947 }
948
7f143ac1
DD
949 dyn_string_append_cstr (driver_cmdline, " -Wl,--image-base,");
950 dyn_string_append_cstr (driver_cmdline, image_base_str);
252b5132
RH
951
952 if (verbose)
953 {
7f143ac1 954 dyn_string_append_cstr (driver_cmdline, " -v");
252b5132
RH
955 }
956
957 for (i = 1; i < argc; ++i)
958 {
959 if (driver_arg_indices[i])
26044998 960 {
252b5132 961 char *arg = saved_argv[i];
26044998
KH
962 int quote = (strchr (arg, ' ') || strchr (arg, '\t'));
963 dyn_string_append_cstr (driver_cmdline,
252b5132 964 (quote) ? " \"" : " ");
7f143ac1 965 dyn_string_append_cstr (driver_cmdline, arg);
26044998 966 dyn_string_append_cstr (driver_cmdline,
252b5132
RH
967 (quote) ? "\"" : "");
968 }
969 }
26044998 970
61513dc1
NC
971 /* Step pre-1. If no --def <EXPORT_DEF> is specified,
972 then create it and then pass it on. */
26044998
KH
973
974 if (! def_file_seen)
252b5132
RH
975 {
976 int i;
977 dyn_string_t step_pre1;
978
979 step_pre1 = dyn_string_new (1024);
980
7f143ac1 981 dyn_string_append_cstr (step_pre1, dlltool_cmdline->s);
252b5132
RH
982 if (export_all)
983 {
26044998
KH
984 dyn_string_append_cstr (step_pre1, " --export-all --exclude-symbol=");
985 dyn_string_append_cstr (step_pre1,
252b5132
RH
986 "_cygwin_dll_entry@12,DllMainCRTStartup@12,DllMain@12,DllEntryPoint@12");
987 }
7f143ac1
DD
988 dyn_string_append_cstr (step_pre1, " --output-def ");
989 dyn_string_append_cstr (step_pre1, def_file_name);
252b5132
RH
990
991 for (i = 1; i < argc; ++i)
992 {
993 if (driver_arg_indices[i])
994 {
995 char *arg = saved_argv[i];
996 size_t len = strlen (arg);
26044998 997 if (len >= 2 && arg[len-2] == '.'
252b5132
RH
998 && (arg[len-1] == 'o' || arg[len-1] == 'a'))
999 {
1000 int quote = (strchr (arg, ' ') || strchr (arg, '\t'));
7f143ac1 1001 dyn_string_append_cstr (step_pre1,
252b5132 1002 (quote) ? " \"" : " ");
7f143ac1
DD
1003 dyn_string_append_cstr (step_pre1, arg);
1004 dyn_string_append_cstr (step_pre1,
252b5132
RH
1005 (quote) ? "\"" : "");
1006 }
1007 }
1008 }
1009
1010 if (run (dlltool_name, step_pre1->s))
1011 cleanup_and_exit (1);
26044998 1012
252b5132
RH
1013 dyn_string_delete (step_pre1);
1014 }
1015
7f143ac1
DD
1016 dyn_string_append_cstr (dlltool_cmdline, " --def ");
1017 dyn_string_append_cstr (dlltool_cmdline, def_file_name);
252b5132
RH
1018
1019 if (verbose)
1020 {
37cc8ec1
AM
1021 fprintf (stderr, _("DLLTOOL name : %s\n"), dlltool_name);
1022 fprintf (stderr, _("DLLTOOL options : %s\n"), dlltool_cmdline->s);
1023 fprintf (stderr, _("DRIVER name : %s\n"), driver_name);
1024 fprintf (stderr, _("DRIVER options : %s\n"), driver_cmdline->s);
252b5132 1025 }
26044998 1026
61513dc1
NC
1027 /* Step 1. Call GCC/LD to create base relocation file. If using GCC, the
1028 driver command line will look like the following:
1029
1030 % gcc -Wl,--dll --Wl,--base-file,foo.base [rest of command line]
1031
1032 If the user does not specify a base name, create temporary one that
1033 is deleted at exit. */
26044998 1034
252b5132
RH
1035 if (! base_file_name)
1036 {
1037 char *fileprefix = choose_temp_base ();
1038 base_file_name = (char *) xmalloc (strlen (fileprefix) + 6);
26044998
KH
1039 sprintf (base_file_name, "%s.base",
1040 (dontdeltemps) ? mybasename (fileprefix) : fileprefix);
252b5132
RH
1041 delete_base_file = 1;
1042 free (fileprefix);
1043 }
26044998 1044
252b5132
RH
1045 {
1046 int quote;
1047
26044998
KH
1048 dyn_string_t step1 = dyn_string_new (driver_cmdline->length
1049 + strlen (base_file_name)
1050 + 20);
7f143ac1 1051 dyn_string_append_cstr (step1, "-Wl,--base-file,");
26044998
KH
1052 quote = (strchr (base_file_name, ' ')
1053 || strchr (base_file_name, '\t'));
1054 dyn_string_append_cstr (step1,
252b5132 1055 (quote) ? "\"" : "");
7f143ac1 1056 dyn_string_append_cstr (step1, base_file_name);
26044998 1057 dyn_string_append_cstr (step1,
252b5132
RH
1058 (quote) ? "\"" : "");
1059 if (driver_cmdline->length)
1060 {
26044998
KH
1061 dyn_string_append_cstr (step1, " ");
1062 dyn_string_append_cstr (step1, driver_cmdline->s);
252b5132
RH
1063 }
1064
1065 if (run (driver_name, step1->s))
1066 cleanup_and_exit (1);
26044998 1067
252b5132
RH
1068 dyn_string_delete (step1);
1069 }
1070
61513dc1
NC
1071 /* Step 2. generate the exp file by running dlltool.
1072 dlltool command line will look like the following:
1073
1074 % dlltool -Wl,--dll --Wl,--base-file,foo.base [rest of command line]
1075
1076 If the user does not specify a base name, create temporary one that
1077 is deleted at exit. */
26044998 1078
252b5132
RH
1079 if (! exp_file_name)
1080 {
1081 char *p = strrchr (dll_name, '.');
61513dc1
NC
1082 size_t prefix_len = (p) ? (size_t) (p - dll_name) : strlen (dll_name);
1083
252b5132
RH
1084 exp_file_name = (char *) xmalloc (prefix_len + 4 + 1);
1085 strncpy (exp_file_name, dll_name, prefix_len);
1086 exp_file_name[prefix_len] = '\0';
1087 strcat (exp_file_name, ".exp");
1088 delete_exp_file = 1;
1089 }
26044998 1090
252b5132
RH
1091 {
1092 int quote;
61513dc1 1093
26044998
KH
1094 dyn_string_t step2 = dyn_string_new (dlltool_cmdline->length
1095 + strlen (base_file_name)
1096 + strlen (exp_file_name)
252b5132
RH
1097 + 20);
1098
7f143ac1 1099 dyn_string_append_cstr (step2, "--base-file ");
26044998
KH
1100 quote = (strchr (base_file_name, ' ')
1101 || strchr (base_file_name, '\t'));
1102 dyn_string_append_cstr (step2,
252b5132 1103 (quote) ? "\"" : "");
7f143ac1 1104 dyn_string_append_cstr (step2, base_file_name);
26044998 1105 dyn_string_append_cstr (step2,
252b5132
RH
1106 (quote) ? "\" " : " ");
1107
7f143ac1 1108 dyn_string_append_cstr (step2, "--output-exp ");
26044998
KH
1109 quote = (strchr (exp_file_name, ' ')
1110 || strchr (exp_file_name, '\t'));
1111 dyn_string_append_cstr (step2,
252b5132 1112 (quote) ? "\"" : "");
7f143ac1 1113 dyn_string_append_cstr (step2, exp_file_name);
26044998 1114 dyn_string_append_cstr (step2,
252b5132
RH
1115 (quote) ? "\"" : "");
1116
1117 if (dlltool_cmdline->length)
1118 {
26044998
KH
1119 dyn_string_append_cstr (step2, " ");
1120 dyn_string_append_cstr (step2, dlltool_cmdline->s);
252b5132
RH
1121 }
1122
1123 if (run (dlltool_name, step2->s))
1124 cleanup_and_exit (1);
26044998 1125
252b5132
RH
1126 dyn_string_delete (step2);
1127 }
1128
1129 /*
1130 * Step 3. Call GCC/LD to again, adding the exp file this time.
1131 * driver command line will look like the following:
26044998 1132 *
252b5132
RH
1133 * % gcc -Wl,--dll --Wl,--base-file,foo.base foo.exp [rest ...]
1134 */
1135
1136 {
1137 int quote;
1138
26044998
KH
1139 dyn_string_t step3 = dyn_string_new (driver_cmdline->length
1140 + strlen (exp_file_name)
1141 + strlen (base_file_name)
252b5132 1142 + 20);
7f143ac1 1143 dyn_string_append_cstr (step3, "-Wl,--base-file,");
26044998
KH
1144 quote = (strchr (base_file_name, ' ')
1145 || strchr (base_file_name, '\t'));
1146 dyn_string_append_cstr (step3,
252b5132 1147 (quote) ? "\"" : "");
7f143ac1 1148 dyn_string_append_cstr (step3, base_file_name);
26044998 1149 dyn_string_append_cstr (step3,
252b5132
RH
1150 (quote) ? "\" " : " ");
1151
26044998
KH
1152 quote = (strchr (exp_file_name, ' ')
1153 || strchr (exp_file_name, '\t'));
1154 dyn_string_append_cstr (step3,
252b5132 1155 (quote) ? "\"" : "");
7f143ac1 1156 dyn_string_append_cstr (step3, exp_file_name);
26044998 1157 dyn_string_append_cstr (step3,
252b5132
RH
1158 (quote) ? "\"" : "");
1159
1160 if (driver_cmdline->length)
1161 {
26044998
KH
1162 dyn_string_append_cstr (step3, " ");
1163 dyn_string_append_cstr (step3, driver_cmdline->s);
252b5132
RH
1164 }
1165
1166 if (run (driver_name, step3->s))
1167 cleanup_and_exit (1);
26044998 1168
252b5132
RH
1169 dyn_string_delete (step3);
1170 }
1171
1172
1173 /*
1174 * Step 4. Run DLLTOOL again using the same command line.
1175 */
1176
1177 {
1178 int quote;
26044998
KH
1179 dyn_string_t step4 = dyn_string_new (dlltool_cmdline->length
1180 + strlen (base_file_name)
1181 + strlen (exp_file_name)
252b5132
RH
1182 + 20);
1183
7f143ac1 1184 dyn_string_append_cstr (step4, "--base-file ");
26044998
KH
1185 quote = (strchr (base_file_name, ' ')
1186 || strchr (base_file_name, '\t'));
1187 dyn_string_append_cstr (step4,
252b5132 1188 (quote) ? "\"" : "");
7f143ac1 1189 dyn_string_append_cstr (step4, base_file_name);
26044998 1190 dyn_string_append_cstr (step4,
252b5132
RH
1191 (quote) ? "\" " : " ");
1192
7f143ac1 1193 dyn_string_append_cstr (step4, "--output-exp ");
26044998
KH
1194 quote = (strchr (exp_file_name, ' ')
1195 || strchr (exp_file_name, '\t'));
1196 dyn_string_append_cstr (step4,
252b5132 1197 (quote) ? "\"" : "");
7f143ac1 1198 dyn_string_append_cstr (step4, exp_file_name);
26044998 1199 dyn_string_append_cstr (step4,
252b5132
RH
1200 (quote) ? "\"" : "");
1201
1202 if (dlltool_cmdline->length)
1203 {
26044998
KH
1204 dyn_string_append_cstr (step4, " ");
1205 dyn_string_append_cstr (step4, dlltool_cmdline->s);
252b5132
RH
1206 }
1207
1208 if (output_lib_file_name)
1209 {
26044998
KH
1210 dyn_string_append_cstr (step4, " --output-lib ");
1211 dyn_string_append_cstr (step4, output_lib_file_name);
252b5132
RH
1212 }
1213
1214 if (run (dlltool_name, step4->s))
1215 cleanup_and_exit (1);
26044998 1216
252b5132
RH
1217 dyn_string_delete (step4);
1218 }
26044998 1219
252b5132
RH
1220
1221 /*
1222 * Step 5. Link it all together and be done with it.
1223 * driver command line will look like the following:
26044998 1224 *
252b5132
RH
1225 * % gcc -Wl,--dll foo.exp [rest ...]
1226 *
1227 */
1228
1229 {
1230 int quote;
1231
26044998
KH
1232 dyn_string_t step5 = dyn_string_new (driver_cmdline->length
1233 + strlen (exp_file_name)
252b5132 1234 + 20);
26044998
KH
1235 quote = (strchr (exp_file_name, ' ')
1236 || strchr (exp_file_name, '\t'));
1237 dyn_string_append_cstr (step5,
252b5132 1238 (quote) ? "\"" : "");
7f143ac1 1239 dyn_string_append_cstr (step5, exp_file_name);
26044998 1240 dyn_string_append_cstr (step5,
252b5132
RH
1241 (quote) ? "\"" : "");
1242
1243 if (driver_cmdline->length)
1244 {
26044998
KH
1245 dyn_string_append_cstr (step5, " ");
1246 dyn_string_append_cstr (step5, driver_cmdline->s);
252b5132
RH
1247 }
1248
1249 if (run (driver_name, step5->s))
1250 cleanup_and_exit (1);
26044998 1251
252b5132
RH
1252 dyn_string_delete (step5);
1253 }
1254
1255 cleanup_and_exit (0);
1256
1257 return 0;
1258}