* lexsup.c (parse_args): Accept -h as a synonym for --soname, for
[external/binutils.git] / ld / lexsup.c
1 /* Parse options for the GNU linker.
2    Copyright (C) 1991, 92, 93, 94, 1995 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 "config.h"
28 #include "ld.h"
29 #include "ldmain.h"
30 #include "ldmisc.h"
31 #include "ldexp.h"
32 #include "ldlang.h"
33 #include "ldgram.h"
34 #include "ldlex.h"
35 #include "ldfile.h"
36 #include "ldver.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 /* WINDOWS_NT; declare additional functions */
51 static void set_subsystem PARAMS ((char *subsystem_type));
52 static void set_stack_heap PARAMS ((char *valstr, boolean for_heap));
53
54 void
55 parse_args (argc, argv)
56      int argc;
57      char **argv;
58 {
59   int i;
60   int ingroup = 0;
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::b:c:de:F::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_CALL_SHARED              150
73 #define OPTION_DEFSYM                   (OPTION_CALL_SHARED + 1)
74 #define OPTION_DYNAMIC_LINKER           (OPTION_DEFSYM + 1)
75 #define OPTION_EB                       (OPTION_DYNAMIC_LINKER + 1)
76 #define OPTION_EL                       (OPTION_EB + 1)
77 #define OPTION_HEAP                     (OPTION_EL + 1)
78 #define OPTION_EMBEDDED_RELOCS          (OPTION_HEAP + 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_OFORMAT                  (OPTION_NON_SHARED + 1)
87 #define OPTION_RELAX                    (OPTION_OFORMAT + 1)
88 #define OPTION_RETAIN_SYMBOLS_FILE      (OPTION_RELAX + 1)
89 #define OPTION_RPATH                    (OPTION_RETAIN_SYMBOLS_FILE + 1)
90 #define OPTION_SHARED                   (OPTION_RPATH + 1)
91 #define OPTION_SONAME                   (OPTION_SHARED + 1)
92 #define OPTION_SORT_COMMON              (OPTION_SONAME + 1)
93 #define OPTION_STACK                    (OPTION_SORT_COMMON + 1) /*WINDOWS_NT*/
94 #define OPTION_STATS                    (OPTION_STACK + 1)
95 #define OPTION_SUBSYSTEM                (OPTION_STATS + 1) /* WINDOWS_NT */
96 #define OPTION_SYMBOLIC                 (OPTION_SUBSYSTEM + 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_ONCE                (OPTION_WARN_CONSTRUCTORS + 1)
107 #define OPTION_SPLIT_BY_RELOC           (OPTION_WARN_ONCE + 1)
108 #define OPTION_SPLIT_BY_FILE            (OPTION_SPLIT_BY_RELOC + 1)
109 #define OPTION_WHOLE_ARCHIVE            (OPTION_SPLIT_BY_FILE + 1)
110 #define OPTION_BASE_FILE                (OPTION_WHOLE_ARCHIVE + 1)
111   static struct option longopts[] = {
112     {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
113     {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
114     {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
115     {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
116     {"dc", no_argument, NULL, 'd'},
117     {"defsym", required_argument, NULL, OPTION_DEFSYM},
118     {"dll-verbose", no_argument, NULL, OPTION_VERSION}, /* Linux.  */
119     {"dn", no_argument, NULL, OPTION_NON_SHARED},
120     {"dp", no_argument, NULL, 'd'},
121     {"dy", no_argument, NULL, OPTION_CALL_SHARED},
122     {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
123     {"EB", no_argument, NULL, OPTION_EB},
124     {"EL", no_argument, NULL, OPTION_EL},
125     {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
126     {"end-group", no_argument, NULL, ')'},
127     {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
128     {"format", required_argument, NULL, 'b'},
129     {"heap", required_argument, NULL, OPTION_HEAP}, /* WINDOWS_NT */
130     {"help", no_argument, NULL, OPTION_HELP},
131     {"Map", required_argument, NULL, OPTION_MAP},
132     {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
133     {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
134     {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
135     {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
136     {"oformat", required_argument, NULL, OPTION_OFORMAT},
137     {"Qy", no_argument, NULL, OPTION_IGNORE},
138     {"qmagic", no_argument, NULL, OPTION_IGNORE}, /* Linux compatibility.  */
139     {"relax", no_argument, NULL, OPTION_RELAX},
140     {"retain-symbols-file", required_argument, NULL, OPTION_RETAIN_SYMBOLS_FILE},
141     {"rpath", required_argument, NULL, OPTION_RPATH},
142     {"shared", no_argument, NULL, OPTION_SHARED},
143     {"soname", required_argument, NULL, OPTION_SONAME},
144     {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
145     {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
146     {"stack", required_argument, NULL, OPTION_STACK}, /* WINDOWS_NT */
147     {"start-group", no_argument, NULL, '('},
148     {"stats", no_argument, NULL, OPTION_STATS},
149     {"static", no_argument, NULL, OPTION_NON_SHARED},
150     {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM}, /* WINDOWS_NT */
151     {"Tbss", required_argument, NULL, OPTION_TBSS},
152     {"Tdata", required_argument, NULL, OPTION_TDATA},
153     {"Ttext", required_argument, NULL, OPTION_TTEXT},
154     {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
155     {"Ur", no_argument, NULL, OPTION_UR},
156     {"verbose", no_argument, NULL, OPTION_VERBOSE},
157     {"version", no_argument, NULL, OPTION_VERSION},
158     {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
159     {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
160     {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
161     {"split-by-reloc", required_argument, NULL, OPTION_SPLIT_BY_RELOC},
162     {"split-by-file", no_argument, NULL, OPTION_SPLIT_BY_FILE},
163     {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
164     {"base-file", required_argument, NULL, OPTION_BASE_FILE},
165     {NULL, no_argument, NULL, 0}
166   };
167
168   /* The -G option is ambiguous on different platforms.  Sometimes it
169      specifies the largest data size to put into the small data
170      section.  Sometimes it is equivalent to --shared.  Unfortunately,
171      the first form takes an argument, while the second does not.
172
173      We need to permit the --shared form because on some platforms,
174      such as Solaris, gcc -shared will pass -G to the linker.
175
176      To permit either usage, we look through the argument list.  If we
177      find -G not followed by a number, we change it into --shared.
178      This will work for most normal cases.  */
179   for (i = 1; i < argc; i++)
180     if (strcmp (argv[i], "-G") == 0
181         && (i + 1 >= argc
182             || ! isdigit (argv[i + 1][0])))
183       argv[i] = (char *) "--shared";
184
185   while (1)
186     {
187       /* getopt_long_only is like getopt_long, but '-' as well as '--' can
188          indicate a long option.  */
189       int longind;
190       int optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
191
192       if (optc == -1)
193         break;
194
195       switch (optc)
196         {
197         default:
198           xexit (1);
199         case 1:                 /* File name.  */
200           lang_add_input_file (optarg, lang_input_file_is_file_enum,
201                                (char *) NULL);
202           break;
203
204         case OPTION_IGNORE:
205           break;
206         case 'a':
207           /* For HP/UX compatibility.  Actually -a shared should mean
208              ``use only shared libraries'' but, then, we don't
209              currently support shared libraries on HP/UX anyhow.  */
210           if (strcmp (optarg, "archive") == 0)
211             config.dynamic_link = false;
212           else if (strcmp (optarg, "shared") == 0
213                    || strcmp (optarg, "default") == 0)
214             config.dynamic_link = true;
215           else
216             einfo ("%P%F: unrecognized -a option `%s'\n", optarg);
217           break;
218         case 'A':
219           ldfile_add_arch (optarg);
220           break;
221         case 'b':
222           lang_add_target (optarg);
223           break;
224         case 'c':
225           ldfile_open_command_file (optarg);
226           parser_input = input_mri_script;
227           yyparse ();
228           break;
229         case OPTION_CALL_SHARED:
230           config.dynamic_link = true;
231           break;
232         case OPTION_NON_SHARED:
233           config.dynamic_link = false;
234           break;
235         case 'd':
236           command_line.force_common_definition = true;
237           break;
238         case OPTION_DEFSYM:
239           lex_redirect (optarg);
240           parser_input = input_defsym;
241           yyparse ();
242           break;
243         case OPTION_DYNAMIC_LINKER:
244           command_line.interpreter = optarg;
245           break;
246         case OPTION_EB:
247           command_line.endian = ENDIAN_BIG;
248           break;
249         case OPTION_EL:
250           command_line.endian = ENDIAN_LITTLE;
251           break;
252         case OPTION_EMBEDDED_RELOCS:
253           command_line.embedded_relocs = true;
254           break;
255         case OPTION_EXPORT_DYNAMIC:
256           command_line.export_dynamic = true;
257           break;
258         case 'e':
259           lang_add_entry (optarg, 1);
260           break;
261         case 'F':
262           /* Ignore.  */
263           break;
264         case 'G':
265           {
266             char *end;
267             g_switch_value = strtoul (optarg, &end, 0);
268             if (*end)
269               einfo ("%P%F: invalid number `%s'\n", optarg);
270           }
271           break;
272         case 'g':
273           /* Ignore.  */
274           break;
275         case OPTION_HEAP:  /* WINDOWS_NT */
276           link_info.stack_heap_parameters.heap_defined = true;
277           set_stack_heap (optarg, true);
278           break;
279         case OPTION_HELP:
280           help ();
281           xexit (0);
282           break;
283         case 'L':
284           ldfile_add_library_path (optarg, true);
285           break;
286         case 'l':
287           lang_add_input_file (optarg, lang_input_file_is_l_enum,
288                                (char *) NULL);
289           break;
290         case 'M':
291           config.map_filename = "-";
292           break;
293         case 'm':
294           /* Ignore.  Was handled in a pre-parse.   */
295           break;
296         case OPTION_MAP:
297           config.map_filename = optarg;
298           break;
299         case 'N':
300           config.text_read_only = false;
301           config.magic_demand_paged = false;
302           break;
303         case 'n':
304           config.magic_demand_paged = false;
305           break;
306         case OPTION_NO_KEEP_MEMORY:
307           link_info.keep_memory = false;
308           break;
309         case OPTION_NOINHIBIT_EXEC:
310           force_make_executable = true;
311           break;
312         case 'O':
313           /* FIXME "-O<non-digits> <value>" used to set the address of
314              section <non-digits>.  Was this for compatibility with
315              something, or can we create a new option to do that
316              (with a syntax similar to -defsym)?
317              getopt can't handle two args to an option without kludges.  */
318           set_default_dirlist (optarg);
319           break;
320         case 'o':
321           lang_add_output (optarg, 0); 
322           break;
323         case OPTION_OFORMAT:
324           lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
325           break;
326         case 'i':
327         case 'r':
328           link_info.relocateable = true;
329           config.build_constructors = false;
330           config.magic_demand_paged = false;
331           config.text_read_only = false;
332           config.dynamic_link = false;
333           break;
334         case 'R':
335           /* The GNU linker traditionally uses -R to mean to include
336              only the symbols from a file.  The Solaris linker uses -R
337              to set the path used by the runtime linker to find
338              libraries.  This is the GNU linker -rpath argument.  We
339              try to support both simultaneously by checking the file
340              named.  If it is a directory, rather than a regular file,
341              we assume -rpath was meant.  */
342           {
343             struct stat s;
344
345             if (stat (optarg, &s) >= 0
346                 && ! S_ISDIR (s.st_mode))
347               {
348                 lang_add_input_file (optarg,
349                                      lang_input_file_is_symbols_only_enum,
350                                      (char *) NULL);
351                 break;
352               }
353           }
354           /* Fall through.  */
355         case OPTION_RPATH:
356           if (command_line.rpath == NULL)
357             command_line.rpath = buystring (optarg);
358           else
359             {
360               char *buf;
361
362               buf = xmalloc (strlen (command_line.rpath)
363                              + strlen (optarg)
364                              + 2);
365               sprintf (buf, "%s:%s", command_line.rpath, optarg);
366               free (command_line.rpath);
367               command_line.rpath = buf;
368             }
369           break;
370         case OPTION_RELAX:
371           command_line.relax = true;
372           break;
373         case OPTION_RETAIN_SYMBOLS_FILE:
374           add_keepsyms_file (optarg);
375           break;
376         case 'S':
377           link_info.strip = strip_debugger;
378           break;
379         case 's':
380           link_info.strip = strip_all;
381           break;
382         case OPTION_SHARED:
383           link_info.shared = true;
384           break;
385         case 'h': /* Used on Solaris.  */
386         case OPTION_SONAME:
387           command_line.soname = optarg;
388           break;
389         case OPTION_SORT_COMMON:
390           config.sort_common = true;
391           break;
392         case OPTION_STACK:  /* WINDOWS_NT */
393           link_info.stack_heap_parameters.stack_defined = true;
394           set_stack_heap (optarg, false);
395           break;
396         case OPTION_STATS:
397           config.stats = true;
398           break;
399         case OPTION_SUBSYSTEM:  /* WINDOWS_NT */
400           set_subsystem (optarg);
401           break;
402         case OPTION_SYMBOLIC:
403           link_info.symbolic = true;
404           break;
405         case 't':
406           trace_files = true;
407           break;
408         case 'T':
409           ldfile_open_command_file (optarg);
410           parser_input = input_script;
411           yyparse ();
412           break;
413         case OPTION_TBSS:
414           set_section_start (".bss", optarg);
415           break;
416         case OPTION_TDATA:
417           set_section_start (".data", optarg);
418           break;
419         case OPTION_TTEXT:
420           set_section_start (".text", optarg);
421           break;
422         case OPTION_TRADITIONAL_FORMAT:
423           config.traditional_format = true;
424           break;
425         case OPTION_UR:
426           link_info.relocateable = true;
427           config.build_constructors = true;
428           config.magic_demand_paged = false;
429           config.text_read_only = false;
430           config.dynamic_link = false;
431           break;
432         case 'u':
433           ldlang_add_undef (optarg);
434           break;
435         case OPTION_VERBOSE:
436           ldversion (1);
437           version_printed = true;
438           trace_file_tries = true;
439           break;
440         case 'v':
441           ldversion (0);
442           version_printed = true;
443           break;
444         case 'V':
445           ldversion (1);
446           version_printed = true;
447           break;
448         case OPTION_VERSION:
449           ldversion (0);
450           version_printed = true;
451           break;
452         case OPTION_WARN_COMMON:
453           config.warn_common = true;
454           break;
455         case OPTION_WARN_CONSTRUCTORS:
456           config.warn_constructors = true;
457           break;
458         case OPTION_WARN_ONCE:
459           config.warn_once = true;
460           break;
461         case OPTION_WHOLE_ARCHIVE:
462           whole_archive = true;
463           break;
464         case OPTION_BASE_FILE:
465           link_info.base_file = (PTR) fopen (optarg,"w");
466           if (link_info.base_file == NULL)
467             {
468               fprintf (stderr, "%s: Can't open base file %s\n",
469                        program_name, optarg);
470               xexit (1);
471             }
472           break;
473         case 'X':
474           link_info.discard = discard_l;
475           break;
476         case 'x':
477           link_info.discard = discard_all;
478           break;
479         case 'Y':
480           set_default_dirlist (optarg);
481           break;
482         case 'y':
483           add_ysym (optarg);
484           break;
485         case 'z':
486           /* We accept and ignore this option for Solaris
487              compatibility.  Actually, on Solaris, optarg is not
488              ignored.  Someday we should handle it correctly.  FIXME.  */
489           break;
490         case OPTION_SPLIT_BY_RELOC:
491           config.split_by_reloc = atoi (optarg);
492           break; 
493         case OPTION_SPLIT_BY_FILE:
494           config.split_by_file = true;
495           break; 
496         case '(':
497           if (ingroup)
498             {
499               fprintf (stderr,
500                        "%s: may not nest groups (--help for usage)\n",
501                        program_name);
502               xexit (1);
503             }
504           lang_enter_group ();
505           ingroup = 1;
506           break;
507         case ')':
508           if (! ingroup)
509             {
510               fprintf (stderr,
511                        "%s: group ended before it began (--help for usage)\n",
512                        program_name);
513               xexit (1);
514             }
515           lang_leave_group ();
516           ingroup = 0;
517           break;
518         }
519     }
520
521   if (ingroup)
522     lang_leave_group ();
523 }
524
525 /* Add the (colon-separated) elements of DIRLIST_PTR to the
526    library search path.  */
527
528 static void
529 set_default_dirlist (dirlist_ptr)
530      char *dirlist_ptr;
531 {
532   char *p;
533
534   while (1)
535     {
536       p = strchr (dirlist_ptr, ':');
537       if (p != NULL)
538         *p = 0;
539       if (*dirlist_ptr)
540         ldfile_add_library_path (dirlist_ptr, true);
541       if (p == NULL)
542         break;
543       *p = ':';
544       dirlist_ptr = p + 1;
545     }
546 }
547
548 static void
549 set_section_start (sect, valstr)
550      char *sect, *valstr;
551 {
552   char *end;
553   unsigned long val = strtoul (valstr, &end, 16);
554   if (*end)
555     einfo ("%P%F: invalid hex number `%s'\n", valstr);
556   lang_section_start (sect, exp_intop (val));
557 }
558
559 /* WINDOWS_NT; added routines to get the subsystem type, heap and/or stack
560    parameters which may be input from the command line */
561 static void
562 set_subsystem (subsystem_type)
563   char *subsystem_type;
564 {
565   if (strcmp (subsystem_type, "native") == 0)
566     {
567       link_info.subsystem = native;
568     }
569   else if (strcmp (subsystem_type, "windows") == 0)
570     {
571       link_info.subsystem = windows;
572     }
573   else if (strcmp (subsystem_type, "console") == 0)
574     {
575       link_info.subsystem = console;
576     }
577   else if (strcmp (subsystem_type, "os2") == 0)
578     {
579       link_info.subsystem = os2;
580     }
581   else if (strcmp (subsystem_type, "posix") == 0)
582     {
583       link_info.subsystem = posix;
584     }
585   else
586     einfo ("%P%F: invalid subsystem type `%s'\n", subsystem_type);
587
588 }
589
590 static void
591 set_stack_heap (valstr, for_heap)
592   char *valstr;
593   boolean for_heap;
594 {
595   char *begin_commit, *end;
596   bfd_vma reserve = 0;
597   bfd_vma commit  = 0;
598
599   /* There may be two values passed in to the -stack or -heap switches like
600      this:
601        -stack 0x10000,0x100
602      where the first parameter is the stack (or heap) reserve and the second
603      is commit.  The second parameter is optional. */
604
605   /* get the reserve value */
606   reserve = strtoul (valstr, &begin_commit, 16);
607
608   if (strcmp (valstr, begin_commit) == 0)  
609     /* the reserve value couldn't be read */
610     einfo ("%P%F: invalid hex number for reserve[,commit] '%s'\n", valstr); 
611   else if (strcmp (begin_commit, "\0") != 0)  /* check for a commit value */
612     {
613      begin_commit += 1;  /* increment begin_commit to point past ',' */
614      commit = strtoul (begin_commit, &end, 16);
615      if (strcmp (end, begin_commit) == 0)
616        einfo ("%P%F: invalid hex number for commit '%s'\n", begin_commit);
617     }
618    
619   if (for_heap)
620    {
621     link_info.stack_heap_parameters.heap_reserve = reserve;     
622     link_info.stack_heap_parameters.heap_commit  = commit;     
623    }
624   else
625    {
626     link_info.stack_heap_parameters.stack_reserve = reserve;     
627     link_info.stack_heap_parameters.stack_commit  = commit;     
628    }
629
630 #if DUMP_INFO
631   printf ("reserve = %8x  commit = %8x\n", reserve, commit);
632 #endif
633 }