* lexsup.c (parse_args): Add -E as a synonym for -export-dynamic,
[external/binutils.git] / ld / lexsup.c
1 /* Parse options for the GNU linker.
2    Copyright (C) 1991, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3
4 This file is part of GLD, the Gnu Linker.
5
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include "getopt.h"
26 #include "bfdlink.h"
27 #include "ld.h"
28 #include "ldmain.h"
29 #include "ldmisc.h"
30 #include "ldexp.h"
31 #include "ldlang.h"
32 #include "ldgram.h"
33 #include "ldlex.h"
34 #include "ldfile.h"
35 #include "ldver.h"
36 #include "ldemul.h"
37
38 /* Somewhere above, sys/stat.h got included . . . . */
39 #if !defined(S_ISDIR) && defined(S_IFDIR)
40 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
41 #endif
42
43 /* Omit args to avoid the possibility of clashing with a system header
44    that might disagree about consts.  */
45 unsigned long strtoul ();
46
47 static void set_default_dirlist PARAMS ((char *dirlist_ptr));
48 static void set_section_start PARAMS ((char *sect, char *valstr));
49
50 /* Non-zero if we are processing a --defsym from the command line.  */
51 int parsing_defsym = 0;
52
53 void
54 parse_args (argc, argv)
55      int argc;
56      char **argv;
57 {
58   int i;
59   int ingroup = 0;
60   char *default_dirlist = NULL;
61
62   /* Starting the short option string with '-' is for programs that
63      expect options and other ARGV-elements in any order and that care about
64      the ordering of the two.  We describe each non-option ARGV-element
65      as if it were the argument of an option with character code 1.  */
66
67   const char *shortopts =
68     "-a:A:b:c:de:EF::G:gh:iL:l:Mm:NnO:o:R:rSsT:tu:VvXxY:y:z:()";
69
70   /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
71
72 #define OPTION_ASSERT                   150
73 #define OPTION_CALL_SHARED              (OPTION_ASSERT + 1)
74 #define OPTION_DEFSYM                   (OPTION_CALL_SHARED + 1)
75 #define OPTION_DYNAMIC_LINKER           (OPTION_DEFSYM + 1)
76 #define OPTION_EB                       (OPTION_DYNAMIC_LINKER + 1)
77 #define OPTION_EL                       (OPTION_EB + 1)
78 #define OPTION_EMBEDDED_RELOCS          (OPTION_EL + 1)
79 #define OPTION_EXPORT_DYNAMIC           (OPTION_EMBEDDED_RELOCS + 1)
80 #define OPTION_HELP                     (OPTION_EXPORT_DYNAMIC + 1)
81 #define OPTION_IGNORE                   (OPTION_HELP + 1)
82 #define OPTION_MAP                      (OPTION_IGNORE + 1)
83 #define OPTION_NO_KEEP_MEMORY           (OPTION_MAP + 1)
84 #define OPTION_NOINHIBIT_EXEC           (OPTION_NO_KEEP_MEMORY + 1)
85 #define OPTION_NON_SHARED               (OPTION_NOINHIBIT_EXEC + 1)
86 #define OPTION_NO_WHOLE_ARCHIVE         (OPTION_NON_SHARED + 1)
87 #define OPTION_OFORMAT                  (OPTION_NO_WHOLE_ARCHIVE + 1)
88 #define OPTION_RELAX                    (OPTION_OFORMAT + 1)
89 #define OPTION_RETAIN_SYMBOLS_FILE      (OPTION_RELAX + 1)
90 #define OPTION_RPATH                    (OPTION_RETAIN_SYMBOLS_FILE + 1)
91 #define OPTION_RPATH_LINK               (OPTION_RPATH + 1)
92 #define OPTION_SHARED                   (OPTION_RPATH_LINK + 1)
93 #define OPTION_SONAME                   (OPTION_SHARED + 1)
94 #define OPTION_SORT_COMMON              (OPTION_SONAME + 1)
95 #define OPTION_STATS                    (OPTION_SORT_COMMON + 1)
96 #define OPTION_SYMBOLIC                 (OPTION_STATS + 1)
97 #define OPTION_TBSS                     (OPTION_SYMBOLIC + 1)
98 #define OPTION_TDATA                    (OPTION_TBSS + 1)
99 #define OPTION_TTEXT                    (OPTION_TDATA + 1)
100 #define OPTION_TRADITIONAL_FORMAT       (OPTION_TTEXT + 1)
101 #define OPTION_UR                       (OPTION_TRADITIONAL_FORMAT + 1)
102 #define OPTION_VERBOSE                  (OPTION_UR + 1)
103 #define OPTION_VERSION                  (OPTION_VERBOSE + 1)
104 #define OPTION_WARN_COMMON              (OPTION_VERSION + 1)
105 #define OPTION_WARN_CONSTRUCTORS        (OPTION_WARN_COMMON + 1)
106 #define OPTION_WARN_MULTIPLE_GP         (OPTION_WARN_CONSTRUCTORS + 1)
107 #define OPTION_WARN_ONCE                (OPTION_WARN_MULTIPLE_GP + 1)
108 #define OPTION_SPLIT_BY_RELOC           (OPTION_WARN_ONCE + 1)
109 #define OPTION_SPLIT_BY_FILE            (OPTION_SPLIT_BY_RELOC + 1)
110 #define OPTION_WHOLE_ARCHIVE            (OPTION_SPLIT_BY_FILE + 1)
111 #define OPTION_WRAP                     (OPTION_WHOLE_ARCHIVE + 1)
112
113   static struct option longopts[] = {
114   /* Sorted alphabeticaly, except for the PE options grouped at the end. */
115     {"assert", required_argument, NULL, OPTION_ASSERT},
116     {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
117     {"Bshareable", no_argument, NULL, OPTION_SHARED }, /* FreeBSD.  */
118     {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
119     {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
120     {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
121     {"dc", no_argument, NULL, 'd'},
122     {"defsym", required_argument, NULL, OPTION_DEFSYM},
123     {"dll-verbose", no_argument, NULL, OPTION_VERSION}, /* Linux.  */
124     {"dn", no_argument, NULL, OPTION_NON_SHARED},
125     {"dp", no_argument, NULL, 'd'},
126     {"dy", no_argument, NULL, OPTION_CALL_SHARED},
127     {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
128     {"EB", no_argument, NULL, OPTION_EB},
129     {"EL", no_argument, NULL, OPTION_EL},
130     {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
131     {"end-group", no_argument, NULL, ')'},
132     {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
133     {"format", required_argument, NULL, 'b'},
134     {"help", no_argument, NULL, OPTION_HELP},
135     {"Map", required_argument, NULL, OPTION_MAP},
136     {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
137     {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
138     {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
139     {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
140     {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
141     {"oformat", required_argument, NULL, OPTION_OFORMAT},
142     {"Qy", no_argument, NULL, OPTION_IGNORE},
143     {"qmagic", no_argument, NULL, OPTION_IGNORE}, /* Linux compatibility.  */
144     {"relax", no_argument, NULL, OPTION_RELAX},
145     {"retain-symbols-file", required_argument, NULL, OPTION_RETAIN_SYMBOLS_FILE},
146     {"rpath", required_argument, NULL, OPTION_RPATH},
147     {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
148     {"shared", no_argument, NULL, OPTION_SHARED},
149     {"soname", required_argument, NULL, OPTION_SONAME},
150     {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
151     {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
152     {"start-group", no_argument, NULL, '('},
153     {"stats", no_argument, NULL, OPTION_STATS},
154     {"static", no_argument, NULL, OPTION_NON_SHARED},
155     {"Tbss", required_argument, NULL, OPTION_TBSS},
156     {"Tdata", required_argument, NULL, OPTION_TDATA},
157     {"Ttext", required_argument, NULL, OPTION_TTEXT},
158     {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
159     {"Ur", no_argument, NULL, OPTION_UR},
160     {"verbose", no_argument, NULL, OPTION_VERBOSE},
161     {"version", no_argument, NULL, OPTION_VERSION},
162     {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
163     {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
164     {"warn-multiple-gp", no_argument, NULL, OPTION_WARN_MULTIPLE_GP},
165     {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
166     {"split-by-reloc", required_argument, NULL, OPTION_SPLIT_BY_RELOC},
167     {"split-by-file", no_argument, NULL, OPTION_SPLIT_BY_FILE},
168     {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
169     {"wrap", required_argument, NULL, OPTION_WRAP},
170
171     {NULL, no_argument, NULL, 0}
172   };
173
174   /* The -G option is ambiguous on different platforms.  Sometimes it
175      specifies the largest data size to put into the small data
176      section.  Sometimes it is equivalent to --shared.  Unfortunately,
177      the first form takes an argument, while the second does not.
178
179      We need to permit the --shared form because on some platforms,
180      such as Solaris, gcc -shared will pass -G to the linker.
181
182      To permit either usage, we look through the argument list.  If we
183      find -G not followed by a number, we change it into --shared.
184      This will work for most normal cases.  */
185   for (i = 1; i < argc; i++)
186     if (strcmp (argv[i], "-G") == 0
187         && (i + 1 >= argc
188             || ! isdigit (argv[i + 1][0])))
189       argv[i] = (char *) "--shared";
190
191   while (1)
192     {
193       /* getopt_long_only is like getopt_long, but '-' as well as '--' can
194          indicate a long option.  */
195       int longind;
196       int optc;
197
198       if (ldemul_parse_args (argc, argv))
199         continue;
200
201       optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
202
203       if (optc == -1)
204         break;
205       switch (optc)
206         {
207         default:
208           xexit (1);
209         case 1:                 /* File name.  */
210           lang_add_input_file (optarg, lang_input_file_is_file_enum,
211                                (char *) NULL);
212           break;
213
214         case OPTION_IGNORE:
215           break;
216         case 'a':
217           /* For HP/UX compatibility.  Actually -a shared should mean
218              ``use only shared libraries'' but, then, we don't
219              currently support shared libraries on HP/UX anyhow.  */
220           if (strcmp (optarg, "archive") == 0)
221             config.dynamic_link = false;
222           else if (strcmp (optarg, "shared") == 0
223                    || strcmp (optarg, "default") == 0)
224             config.dynamic_link = true;
225           else
226             einfo ("%P%F: unrecognized -a option `%s'\n", optarg);
227           break;
228         case OPTION_ASSERT:
229           /* FIXME: We just ignore these, but we should handle them.  */
230           if (strcmp (optarg, "definitions") == 0)
231             ;
232           else if (strcmp (optarg, "nodefinitions") == 0)
233             ;
234           else if (strcmp (optarg, "nosymbolic") == 0)
235             ;
236           else if (strcmp (optarg, "pure-text") == 0)
237             ;
238           else
239             einfo ("%P%F: unrecognized -assert option `%s'\n", optarg);
240           break;
241         case 'A':
242           ldfile_add_arch (optarg);
243           break;
244         case 'b':
245           lang_add_target (optarg);
246           break;
247         case 'c':
248           ldfile_open_command_file (optarg);
249           parser_input = input_mri_script;
250           yyparse ();
251           break;
252         case OPTION_CALL_SHARED:
253           config.dynamic_link = true;
254           break;
255         case OPTION_NON_SHARED:
256           config.dynamic_link = false;
257           break;
258         case 'd':
259           command_line.force_common_definition = true;
260           break;
261         case OPTION_DEFSYM:
262           lex_string = optarg;
263           lex_redirect (optarg);
264           parser_input = input_defsym;
265           parsing_defsym = 1;
266           yyparse ();
267           parsing_defsym = 0;
268           lex_string = NULL;
269           break;
270         case OPTION_DYNAMIC_LINKER:
271           command_line.interpreter = optarg;
272           break;
273         case OPTION_EB:
274           command_line.endian = ENDIAN_BIG;
275           break;
276         case OPTION_EL:
277           command_line.endian = ENDIAN_LITTLE;
278           break;
279         case OPTION_EMBEDDED_RELOCS:
280           command_line.embedded_relocs = true;
281           break;
282         case OPTION_EXPORT_DYNAMIC:
283         case 'E': /* HP/UX compatibility.  */
284           command_line.export_dynamic = true;
285           break;
286         case 'e':
287           lang_add_entry (optarg, true);
288           break;
289         case 'F':
290           /* Ignore.  */
291           break;
292         case 'G':
293           {
294             char *end;
295             g_switch_value = strtoul (optarg, &end, 0);
296             if (*end)
297               einfo ("%P%F: invalid number `%s'\n", optarg);
298           }
299           break;
300         case 'g':
301           /* Ignore.  */
302           break;
303         case OPTION_HELP:
304           help ();
305           xexit (0);
306           break;
307         case 'L':
308           ldfile_add_library_path (optarg, true);
309           break;
310         case 'l':
311           lang_add_input_file (optarg, lang_input_file_is_l_enum,
312                                (char *) NULL);
313           break;
314         case 'M':
315           config.map_filename = "-";
316           break;
317         case 'm':
318           /* Ignore.  Was handled in a pre-parse.   */
319           break;
320         case OPTION_MAP:
321           config.map_filename = optarg;
322           break;
323         case 'N':
324           config.text_read_only = false;
325           config.magic_demand_paged = false;
326           config.dynamic_link = false;
327           break;
328         case 'n':
329           config.magic_demand_paged = false;
330           config.dynamic_link = false;
331           break;
332         case OPTION_NO_KEEP_MEMORY:
333           link_info.keep_memory = false;
334           break;
335         case OPTION_NOINHIBIT_EXEC:
336           force_make_executable = true;
337           break;
338         case OPTION_NO_WHOLE_ARCHIVE:
339           whole_archive = false;
340           break;
341         case 'O':
342           /* FIXME "-O<non-digits> <value>" used to set the address of
343              section <non-digits>.  Was this for compatibility with
344              something, or can we create a new option to do that
345              (with a syntax similar to -defsym)?
346              getopt can't handle two args to an option without kludges.  */
347           break;
348         case 'o':
349           lang_add_output (optarg, 0); 
350           break;
351         case OPTION_OFORMAT:
352           lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
353           break;
354         case 'i':
355         case 'r':
356           link_info.relocateable = true;
357           config.build_constructors = false;
358           config.magic_demand_paged = false;
359           config.text_read_only = false;
360           config.dynamic_link = false;
361           break;
362         case 'R':
363           /* The GNU linker traditionally uses -R to mean to include
364              only the symbols from a file.  The Solaris linker uses -R
365              to set the path used by the runtime linker to find
366              libraries.  This is the GNU linker -rpath argument.  We
367              try to support both simultaneously by checking the file
368              named.  If it is a directory, rather than a regular file,
369              we assume -rpath was meant.  */
370           {
371             struct stat s;
372
373             if (stat (optarg, &s) >= 0
374                 && ! S_ISDIR (s.st_mode))
375               {
376                 lang_add_input_file (optarg,
377                                      lang_input_file_is_symbols_only_enum,
378                                      (char *) NULL);
379                 break;
380               }
381           }
382           /* Fall through.  */
383         case OPTION_RPATH:
384           if (command_line.rpath == NULL)
385             command_line.rpath = buystring (optarg);
386           else
387             {
388               char *buf;
389
390               buf = xmalloc (strlen (command_line.rpath)
391                              + strlen (optarg)
392                              + 2);
393               sprintf (buf, "%s:%s", command_line.rpath, optarg);
394               free (command_line.rpath);
395               command_line.rpath = buf;
396             }
397           break;
398         case OPTION_RPATH_LINK:
399           if (command_line.rpath_link == NULL)
400             command_line.rpath_link = buystring (optarg);
401           else
402             {
403               char *buf;
404
405               buf = xmalloc (strlen (command_line.rpath_link)
406                              + strlen (optarg)
407                              + 2);
408               sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
409               free (command_line.rpath_link);
410               command_line.rpath_link = buf;
411             }
412           break;
413         case OPTION_RELAX:
414           command_line.relax = true;
415           break;
416         case OPTION_RETAIN_SYMBOLS_FILE:
417           add_keepsyms_file (optarg);
418           break;
419         case 'S':
420           link_info.strip = strip_debugger;
421           break;
422         case 's':
423           link_info.strip = strip_all;
424           break;
425         case OPTION_SHARED:
426           link_info.shared = true;
427           break;
428         case 'h':               /* Used on Solaris.  */
429         case OPTION_SONAME:
430           command_line.soname = optarg;
431           break;
432         case OPTION_SORT_COMMON:
433           config.sort_common = true;
434           break;
435         case OPTION_STATS:
436           config.stats = true;
437           break;
438         case OPTION_SYMBOLIC:
439           link_info.symbolic = true;
440           break;
441         case 't':
442           trace_files = true;
443           break;
444         case 'T':
445           ldfile_open_command_file (optarg);
446           parser_input = input_script;
447           yyparse ();
448           break;
449         case OPTION_TBSS:
450           set_section_start (".bss", optarg);
451           break;
452         case OPTION_TDATA:
453           set_section_start (".data", optarg);
454           break;
455         case OPTION_TTEXT:
456           set_section_start (".text", optarg);
457           break;
458         case OPTION_TRADITIONAL_FORMAT:
459           config.traditional_format = true;
460           break;
461         case OPTION_UR:
462           link_info.relocateable = true;
463           config.build_constructors = true;
464           config.magic_demand_paged = false;
465           config.text_read_only = false;
466           config.dynamic_link = false;
467           break;
468         case 'u':
469           ldlang_add_undef (optarg);
470           break;
471         case OPTION_VERBOSE:
472           ldversion (1);
473           version_printed = true;
474           trace_file_tries = true;
475           break;
476         case 'v':
477           ldversion (0);
478           version_printed = true;
479           break;
480         case 'V':
481           ldversion (1);
482           version_printed = true;
483           break;
484         case OPTION_VERSION:
485           ldversion (0);
486           version_printed = true;
487           break;
488         case OPTION_WARN_COMMON:
489           config.warn_common = true;
490           break;
491         case OPTION_WARN_CONSTRUCTORS:
492           config.warn_constructors = true;
493           break;
494         case OPTION_WARN_MULTIPLE_GP:
495           config.warn_multiple_gp = true;
496           break;
497         case OPTION_WARN_ONCE:
498           config.warn_once = true;
499           break;
500         case OPTION_WHOLE_ARCHIVE:
501           whole_archive = true;
502           break;
503         case OPTION_WRAP:
504           add_wrap (optarg);
505           break;
506         case 'X':
507           link_info.discard = discard_l;
508           break;
509         case 'x':
510           link_info.discard = discard_all;
511           break;
512         case 'Y':
513           if (strncmp (optarg, "P,", 2) == 0)
514             optarg += 2;
515           default_dirlist = optarg;
516           break;
517         case 'y':
518           add_ysym (optarg);
519           break;
520         case 'z':
521           /* We accept and ignore this option for Solaris
522              compatibility.  Actually, on Solaris, optarg is not
523              ignored.  Someday we should handle it correctly.  FIXME.  */
524           break;
525         case OPTION_SPLIT_BY_RELOC:
526           config.split_by_reloc = atoi (optarg);
527           break; 
528         case OPTION_SPLIT_BY_FILE:
529           config.split_by_file = true;
530           break; 
531         case '(':
532           if (ingroup)
533             {
534               fprintf (stderr,
535                        "%s: may not nest groups (--help for usage)\n",
536                        program_name);
537               xexit (1);
538             }
539           lang_enter_group ();
540           ingroup = 1;
541           break;
542         case ')':
543           if (! ingroup)
544             {
545               fprintf (stderr,
546                        "%s: group ended before it began (--help for usage)\n",
547                        program_name);
548               xexit (1);
549             }
550           lang_leave_group ();
551           ingroup = 0;
552           break;
553
554         }
555     }
556
557   if (ingroup)
558     lang_leave_group ();
559
560   if (default_dirlist != NULL)
561     set_default_dirlist (default_dirlist);
562
563 }
564
565 /* Add the (colon-separated) elements of DIRLIST_PTR to the
566    library search path.  */
567
568 static void
569 set_default_dirlist (dirlist_ptr)
570      char *dirlist_ptr;
571 {
572   char *p;
573
574   while (1)
575     {
576       p = strchr (dirlist_ptr, ':');
577       if (p != NULL)
578         *p = 0;
579       if (*dirlist_ptr)
580         ldfile_add_library_path (dirlist_ptr, true);
581       if (p == NULL)
582         break;
583       *p = ':';
584       dirlist_ptr = p + 1;
585     }
586 }
587
588 static void
589 set_section_start (sect, valstr)
590      char *sect, *valstr;
591 {
592   char *end;
593   unsigned long val = strtoul (valstr, &end, 16);
594   if (*end)
595     einfo ("%P%F: invalid hex number `%s'\n", valstr);
596   lang_section_start (sect, exp_intop (val));
597 }
598 \f