* ld.h (args_type): Add new field endian.
[platform/upstream/binutils.git] / ld / lexsup.c
1 /* Parse options for the GNU linker.
2    Copyright (C) 1991, 92, 93, 94 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include <stdio.h>
23 #include <string.h>
24 #include "getopt.h"
25 #include "bfdlink.h"
26 #include "config.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
37 /* Somewhere above, sys/stat.h got included . . . . */
38 #if !defined(S_ISDIR) && defined(S_IFDIR)
39 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
40 #endif
41
42 /* Omit args to avoid the possibility of clashing with a system header
43    that might disagree about consts.  */
44 unsigned long strtoul ();
45
46 static void set_default_dirlist PARAMS ((char *dirlist_ptr));
47 static void set_section_start PARAMS ((char *sect, char *valstr));
48
49 void
50 parse_args (argc, argv)
51      int argc;
52      char **argv;
53 {
54   int ingroup = 0;
55
56   /* Starting the short option string with '-' is for programs that
57      expect options and other ARGV-elements in any order and that care about
58      the ordering of the two.  We describe each non-option ARGV-element
59      as if it were the argument of an option with character code 1.  */
60
61   const char *shortopts =
62     "-A:B::b:cde:F::G:giL:l:Mm:NnO:o:R:rSsT:tu:VvXxY:y:()";
63
64   /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
65
66 #define OPTION_CALL_SHARED              150
67 #define OPTION_DEFSYM                   (OPTION_CALL_SHARED + 1)
68 #define OPTION_DYNAMIC_LINKER           (OPTION_DEFSYM + 1)
69 #define OPTION_EB                       (OPTION_DYNAMIC_LINKER + 1)
70 #define OPTION_EL                       (OPTION_EB + 1)
71 #define OPTION_HELP                     (OPTION_EL + 1)
72 #define OPTION_IGNORE                   (OPTION_HELP + 1)
73 #define OPTION_MAP                      (OPTION_IGNORE + 1)
74 #define OPTION_NO_KEEP_MEMORY           (OPTION_MAP + 1)
75 #define OPTION_NOINHIBIT_EXEC           (OPTION_NO_KEEP_MEMORY + 1)
76 #define OPTION_NON_SHARED               (OPTION_NOINHIBIT_EXEC + 1)
77 #define OPTION_OFORMAT                  (OPTION_NON_SHARED + 1)
78 #define OPTION_RELAX                    (OPTION_OFORMAT + 1)
79 #define OPTION_RETAIN_SYMBOLS_FILE      (OPTION_RELAX + 1)
80 #define OPTION_RPATH                    (OPTION_RETAIN_SYMBOLS_FILE + 1)
81 #define OPTION_SHARED                   (OPTION_RPATH + 1)
82 #define OPTION_SONAME                   (OPTION_SHARED + 1)
83 #define OPTION_SORT_COMMON              (OPTION_SONAME + 1)
84 #define OPTION_STATS                    (OPTION_SORT_COMMON + 1)
85 #define OPTION_TBSS                     (OPTION_STATS + 1)
86 #define OPTION_TDATA                    (OPTION_TBSS + 1)
87 #define OPTION_TTEXT                    (OPTION_TDATA + 1)
88 #define OPTION_TRADITIONAL_FORMAT       (OPTION_TTEXT + 1)
89 #define OPTION_UR                       (OPTION_TRADITIONAL_FORMAT + 1)
90 #define OPTION_VERSION                  (OPTION_UR + 1)
91 #define OPTION_WARN_COMMON              (OPTION_VERSION + 1)
92 #define OPTION_WARN_ONCE                (OPTION_WARN_COMMON + 1)
93
94   static struct option longopts[] = {
95     {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
96     {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
97     {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
98     {"dc", no_argument, NULL, 'd'},
99     {"defsym", required_argument, NULL, OPTION_DEFSYM},
100     {"dll-verbose", no_argument, NULL, OPTION_VERSION}, /* Linux.  */
101     {"dn", no_argument, NULL, OPTION_NON_SHARED},
102     {"dp", no_argument, NULL, 'd'},
103     {"dy", no_argument, NULL, OPTION_CALL_SHARED},
104     {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
105     {"EB", no_argument, NULL, OPTION_EB},
106     {"EL", no_argument, NULL, OPTION_EL},
107     {"end-group", no_argument, NULL, ')'},
108     {"format", required_argument, NULL, 'b'},
109     {"help", no_argument, NULL, OPTION_HELP},
110     {"Map", required_argument, NULL, OPTION_MAP},
111     {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
112     {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
113     {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
114     {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
115     {"oformat", required_argument, NULL, OPTION_OFORMAT},
116     {"Qy", no_argument, NULL, OPTION_IGNORE},
117     {"qmagic", no_argument, NULL, OPTION_IGNORE}, /* Linux compatibility.  */
118     {"relax", no_argument, NULL, OPTION_RELAX},
119     {"retain-symbols-file", required_argument, NULL, OPTION_RETAIN_SYMBOLS_FILE},
120     {"rpath", required_argument, NULL, OPTION_RPATH},
121     {"shared", no_argument, NULL, OPTION_SHARED},
122     {"soname", required_argument, NULL, OPTION_SONAME},
123     {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
124     {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
125     {"start-group", no_argument, NULL, '('},
126     {"stats", no_argument, NULL, OPTION_STATS},
127     {"static", no_argument, NULL, OPTION_NON_SHARED},
128     {"Tbss", required_argument, NULL, OPTION_TBSS},
129     {"Tdata", required_argument, NULL, OPTION_TDATA},
130     {"Ttext", required_argument, NULL, OPTION_TTEXT},
131     {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
132     {"Ur", no_argument, NULL, OPTION_UR},
133     {"version", no_argument, NULL, OPTION_VERSION},
134     {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
135     {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
136     {NULL, no_argument, NULL, 0}
137   };
138
139   while (1)
140     {
141       /* getopt_long_only is like getopt_long, but '-' as well as '--' can
142          indicate a long option.  */
143       int longind;
144       int optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
145
146       if (optc == -1)
147         break;
148
149       switch (optc)
150         {
151         default:
152           xexit (1);
153         case 1:                 /* File name.  */
154           lang_add_input_file (optarg, lang_input_file_is_file_enum,
155                                (char *) NULL);
156           break;
157
158         case OPTION_IGNORE:
159           break;
160         case 'A':
161           ldfile_add_arch (optarg);
162           break;
163         case 'b':
164           lang_add_target (optarg);
165           break;
166         case 'c':
167           ldfile_open_command_file (optarg);
168           parser_input = input_mri_script;
169           yyparse ();
170           break;
171         case OPTION_CALL_SHARED:
172           config.dynamic_link = true;
173           break;
174         case OPTION_NON_SHARED:
175           config.dynamic_link = false;
176           break;
177         case 'd':
178           command_line.force_common_definition = true;
179           break;
180         case OPTION_DEFSYM:
181           lex_redirect (optarg);
182           parser_input = input_defsym;
183           yyparse ();
184           break;
185         case OPTION_DYNAMIC_LINKER:
186           command_line.interpreter = optarg;
187           break;
188         case OPTION_EB:
189           command_line.endian = ENDIAN_BIG;
190           break;
191         case OPTION_EL:
192           command_line.endian = ENDIAN_LITTLE;
193           break;
194         case 'e':
195           lang_add_entry (optarg, 1);
196           break;
197         case 'F':
198           /* Ignore.  */
199           break;
200         case 'G':
201           {
202             char *end;
203             g_switch_value = strtoul (optarg, &end, 0);
204             if (*end)
205               einfo ("%P%F: invalid number `%s'\n", optarg);
206           }
207           break;
208         case 'g':
209           /* Ignore.  */
210           break;
211         case OPTION_HELP:
212           help ();
213           xexit (0);
214           break;
215         case 'L':
216           ldfile_add_library_path (optarg, true);
217           break;
218         case 'l':
219           lang_add_input_file (optarg, lang_input_file_is_l_enum,
220                                (char *) NULL);
221           break;
222         case 'M':
223           config.map_filename = "-";
224           break;
225         case 'm':
226           /* Ignore.  Was handled in a pre-parse.   */
227           break;
228         case OPTION_MAP:
229           config.map_filename = optarg;
230           break;
231         case 'N':
232           config.text_read_only = false;
233           config.magic_demand_paged = false;
234           break;
235         case 'n':
236           config.magic_demand_paged = false;
237           break;
238         case OPTION_NO_KEEP_MEMORY:
239           link_info.keep_memory = false;
240           break;
241         case OPTION_NOINHIBIT_EXEC:
242           force_make_executable = true;
243           break;
244         case 'O':
245           /* FIXME "-O<non-digits> <value>" used to set the address of
246              section <non-digits>.  Was this for compatibility with
247              something, or can we create a new option to do that
248              (with a syntax similar to -defsym)?
249              getopt can't handle two args to an option without kludges.  */
250           set_default_dirlist (optarg);
251           break;
252         case 'o':
253           lang_add_output (optarg, 0); 
254           break;
255         case OPTION_OFORMAT:
256           lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
257           break;
258         case 'i':
259         case 'r':
260           link_info.relocateable = true;
261           config.build_constructors = false;
262           config.magic_demand_paged = false;
263           config.text_read_only = false;
264           config.dynamic_link = false;
265           break;
266         case 'R':
267           /* The GNU linker traditionally uses -R to mean to include
268              only the symbols from a file.  The Solaris linker uses -R
269              to set the path used by the runtime linker to find
270              libraries.  This is the GNU linker -rpath argument.  We
271              try to support both simultaneously by checking the file
272              named.  If it is a directory, rather than a regular file,
273              we assume -rpath was meant.  */
274           {
275             struct stat s;
276
277             if (stat (optarg, &s) >= 0
278                 && ! S_ISDIR (s.st_mode))
279               {
280                 lang_add_input_file (optarg,
281                                      lang_input_file_is_symbols_only_enum,
282                                      (char *) NULL);
283                 break;
284               }
285           }
286           /* Fall through.  */
287         case OPTION_RPATH:
288           if (command_line.rpath == NULL)
289             command_line.rpath = buystring (optarg);
290           else
291             {
292               char *buf;
293
294               buf = xmalloc (strlen (command_line.rpath)
295                              + strlen (optarg)
296                              + 2);
297               sprintf (buf, "%s:%s", command_line.rpath, optarg);
298               free (command_line.rpath);
299               command_line.rpath = buf;
300             }
301           break;
302         case OPTION_RELAX:
303           command_line.relax = true;
304           break;
305         case OPTION_RETAIN_SYMBOLS_FILE:
306           add_keepsyms_file (optarg);
307           break;
308         case 'S':
309           link_info.strip = strip_debugger;
310           break;
311         case 's':
312           link_info.strip = strip_all;
313           break;
314         case OPTION_SHARED:
315           link_info.shared = true;
316           break;
317         case OPTION_SONAME:
318           command_line.soname = optarg;
319           break;
320         case OPTION_SORT_COMMON:
321           config.sort_common = true;
322           break;
323         case OPTION_STATS:
324           config.stats = true;
325           break;
326         case 't':
327           trace_files = true;
328           break;
329         case 'T':
330           ldfile_open_command_file (optarg);
331           parser_input = input_script;
332           yyparse ();
333           break;
334         case OPTION_TBSS:
335           set_section_start (".bss", optarg);
336           break;
337         case OPTION_TDATA:
338           set_section_start (".data", optarg);
339           break;
340         case OPTION_TTEXT:
341           set_section_start (".text", optarg);
342           break;
343         case OPTION_TRADITIONAL_FORMAT:
344           config.traditional_format = true;
345           break;
346         case OPTION_UR:
347           link_info.relocateable = true;
348           config.build_constructors = true;
349           config.magic_demand_paged = false;
350           config.text_read_only = false;
351           config.dynamic_link = false;
352           break;
353         case 'u':
354           ldlang_add_undef (optarg);
355           break;
356         case 'V':
357           ldversion (1);
358           version_printed = true;
359           trace_file_tries = true;
360           break;
361         case 'v':
362           ldversion (0);
363           version_printed = true;
364           break;
365         case OPTION_VERSION:
366           ldversion (0);
367           version_printed = true;
368           break;
369         case OPTION_WARN_COMMON:
370           config.warn_common = true;
371           break;
372         case OPTION_WARN_ONCE:
373           config.warn_once = true;
374           break;
375         case 'X':
376           link_info.discard = discard_l;
377           break;
378         case 'x':
379           link_info.discard = discard_all;
380           break;
381         case 'Y':
382           set_default_dirlist (optarg);
383           break;
384         case 'y':
385           add_ysym (optarg);
386           break;
387         case '(':
388           if (ingroup)
389             {
390               fprintf (stderr,
391                        "%s: may not nest groups (--help for usage)\n",
392                        program_name);
393               xexit (1);
394             }
395           lang_enter_group ();
396           ingroup = 1;
397           break;
398         case ')':
399           if (! ingroup)
400             {
401               fprintf (stderr,
402                        "%s: group ended before it began (--help for usage)\n",
403                        program_name);
404               xexit (1);
405             }
406           lang_leave_group ();
407           ingroup = 0;
408           break;
409         }
410     }
411
412   if (ingroup)
413     lang_leave_group ();
414 }
415
416 /* Add the (colon-separated) elements of DIRLIST_PTR to the
417    library search path.  */
418
419 static void
420 set_default_dirlist (dirlist_ptr)
421      char *dirlist_ptr;
422 {
423   char *p;
424
425   while (1)
426     {
427       p = strchr (dirlist_ptr, ':');
428       if (p != NULL)
429         *p = 0;
430       if (*dirlist_ptr)
431         ldfile_add_library_path (dirlist_ptr, true);
432       if (p == NULL)
433         break;
434       *p = ':';
435       dirlist_ptr = p + 1;
436     }
437 }
438
439 static void
440 set_section_start (sect, valstr)
441      char *sect, *valstr;
442 {
443   char *end;
444   unsigned long val = strtoul (valstr, &end, 16);
445   if (*end)
446     einfo ("%P%F: invalid hex number `%s'\n", valstr);
447   lang_section_start (sect, exp_intop (val));
448 }