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