2013-05-10 Joel Brobecker <brobecker@adacore.com>
[external/binutils.git] / gold / options.h
1 // options.h -- handle command line options for gold  -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 // General_options (from Command_line::options())
24 //   All the options (a.k.a. command-line flags)
25 // Input_argument (from Command_line::inputs())
26 //   The list of input files, including -l options.
27 // Command_line
28 //   Everything we get from the command line -- the General_options
29 //   plus the Input_arguments.
30 //
31 // There are also some smaller classes, such as
32 // Position_dependent_options which hold a subset of General_options
33 // that change as options are parsed (as opposed to the usual behavior
34 // of the last instance of that option specified on the commandline wins).
35
36 #ifndef GOLD_OPTIONS_H
37 #define GOLD_OPTIONS_H
38
39 #include <cstdlib>
40 #include <cstring>
41 #include <list>
42 #include <map>
43 #include <string>
44 #include <vector>
45
46 #include "elfcpp.h"
47 #include "script.h"
48
49 namespace gold
50 {
51
52 class Command_line;
53 class General_options;
54 class Search_directory;
55 class Input_file_group;
56 class Input_file_lib;
57 class Position_dependent_options;
58 class Target;
59 class Plugin_manager;
60 class Script_info;
61
62 // Incremental build action for a specific file, as selected by the user.
63
64 enum Incremental_disposition
65 {
66   // Startup files that appear before the first disposition option.
67   // These will default to INCREMENTAL_CHECK unless the
68   // --incremental-startup-unchanged option is given.
69   // (For files added implicitly by gcc before any user options.)
70   INCREMENTAL_STARTUP,
71   // Determine the status from the timestamp (default).
72   INCREMENTAL_CHECK,
73   // Assume the file changed from the previous build.
74   INCREMENTAL_CHANGED,
75   // Assume the file didn't change from the previous build.
76   INCREMENTAL_UNCHANGED
77 };
78
79 // The nested namespace is to contain all the global variables and
80 // structs that need to be defined in the .h file, but do not need to
81 // be used outside this class.
82 namespace options
83 {
84 typedef std::vector<Search_directory> Dir_list;
85 typedef Unordered_set<std::string> String_set;
86
87 // These routines convert from a string option to various types.
88 // Each gives a fatal error if it cannot parse the argument.
89
90 extern void
91 parse_bool(const char* option_name, const char* arg, bool* retval);
92
93 extern void
94 parse_int(const char* option_name, const char* arg, int* retval);
95
96 extern void
97 parse_uint(const char* option_name, const char* arg, int* retval);
98
99 extern void
100 parse_uint64(const char* option_name, const char* arg, uint64_t* retval);
101
102 extern void
103 parse_double(const char* option_name, const char* arg, double* retval);
104
105 extern void
106 parse_percent(const char* option_name, const char* arg, double* retval);
107
108 extern void
109 parse_string(const char* option_name, const char* arg, const char** retval);
110
111 extern void
112 parse_optional_string(const char* option_name, const char* arg,
113                       const char** retval);
114
115 extern void
116 parse_dirlist(const char* option_name, const char* arg, Dir_list* retval);
117
118 extern void
119 parse_set(const char* option_name, const char* arg, String_set* retval);
120
121 extern void
122 parse_choices(const char* option_name, const char* arg, const char** retval,
123               const char* choices[], int num_choices);
124
125 struct Struct_var;
126
127 // Most options have both a shortname (one letter) and a longname.
128 // This enum controls how many dashes are expected for longname access
129 // -- shortnames always use one dash.  Most longnames will accept
130 // either one dash or two; the only difference between ONE_DASH and
131 // TWO_DASHES is how we print the option in --help.  However, some
132 // longnames require two dashes, and some require only one.  The
133 // special value DASH_Z means that the option is preceded by "-z".
134 enum Dashes
135 {
136   ONE_DASH, TWO_DASHES, EXACTLY_ONE_DASH, EXACTLY_TWO_DASHES, DASH_Z
137 };
138
139 // LONGNAME is the long-name of the option with dashes converted to
140 //    underscores, or else the short-name if the option has no long-name.
141 //    It is never the empty string.
142 // DASHES is an instance of the Dashes enum: ONE_DASH, TWO_DASHES, etc.
143 // SHORTNAME is the short-name of the option, as a char, or '\0' if the
144 //    option has no short-name.  If the option has no long-name, you
145 //    should specify the short-name in *both* VARNAME and here.
146 // DEFAULT_VALUE is the value of the option if not specified on the
147 //    commandline, as a string.
148 // HELPSTRING is the descriptive text used with the option via --help
149 // HELPARG is how you define the argument to the option.
150 //    --help output is "-shortname HELPARG, --longname HELPARG: HELPSTRING"
151 //    HELPARG should be NULL iff the option is a bool and takes no arg.
152 // OPTIONAL_ARG is true if this option takes an optional argument.  An
153 //    optional argument must be specifid as --OPTION=VALUE, not
154 //    --OPTION VALUE.
155 // READER provides parse_to_value, which is a function that will convert
156 //    a char* argument into the proper type and store it in some variable.
157 // A One_option struct initializes itself with the global list of options
158 // at constructor time, so be careful making one of these.
159 struct One_option
160 {
161   std::string longname;
162   Dashes dashes;
163   char shortname;
164   const char* default_value;
165   const char* helpstring;
166   const char* helparg;
167   bool optional_arg;
168   Struct_var* reader;
169
170   One_option(const char* ln, Dashes d, char sn, const char* dv,
171              const char* hs, const char* ha, bool oa, Struct_var* r)
172     : longname(ln), dashes(d), shortname(sn), default_value(dv ? dv : ""),
173       helpstring(hs), helparg(ha), optional_arg(oa), reader(r)
174   {
175     // In longname, we convert all underscores to dashes, since GNU
176     // style uses dashes in option names.  longname is likely to have
177     // underscores in it because it's also used to declare a C++
178     // function.
179     const char* pos = strchr(this->longname.c_str(), '_');
180     for (; pos; pos = strchr(pos, '_'))
181       this->longname[pos - this->longname.c_str()] = '-';
182
183     // We only register ourselves if our helpstring is not NULL.  This
184     // is to support the "no-VAR" boolean variables, which we
185     // conditionally turn on by defining "no-VAR" help text.
186     if (this->helpstring)
187       this->register_option();
188   }
189
190   // This option takes an argument iff helparg is not NULL.
191   bool
192   takes_argument() const
193   { return this->helparg != NULL; }
194
195   // Whether the argument is optional.
196   bool
197   takes_optional_argument() const
198   { return this->optional_arg; }
199
200   // Register this option with the global list of options.
201   void
202   register_option();
203
204   // Print this option to stdout (used with --help).
205   void
206   print() const;
207 };
208
209 // All options have a Struct_##varname that inherits from this and
210 // actually implements parse_to_value for that option.
211 struct Struct_var
212 {
213   // OPTION: the name of the option as specified on the commandline,
214   //    including leading dashes, and any text following the option:
215   //    "-O", "--defsym=mysym=0x1000", etc.
216   // ARG: the arg associated with this option, or NULL if the option
217   //    takes no argument: "2", "mysym=0x1000", etc.
218   // CMDLINE: the global Command_line object.  Used by DEFINE_special.
219   // OPTIONS: the global General_options object.  Used by DEFINE_special.
220   virtual void
221   parse_to_value(const char* option, const char* arg,
222                  Command_line* cmdline, General_options* options) = 0;
223   virtual
224   ~Struct_var()  // To make gcc happy.
225   { }
226 };
227
228 // This is for "special" options that aren't of any predefined type.
229 struct Struct_special : public Struct_var
230 {
231   // If you change this, change the parse-fn in DEFINE_special as well.
232   typedef void (General_options::*Parse_function)(const char*, const char*,
233                                                   Command_line*);
234   Struct_special(const char* varname, Dashes dashes, char shortname,
235                  Parse_function parse_function,
236                  const char* helpstring, const char* helparg)
237     : option(varname, dashes, shortname, "", helpstring, helparg, false, this),
238       parse(parse_function)
239   { }
240
241   void parse_to_value(const char* option, const char* arg,
242                       Command_line* cmdline, General_options* options)
243   { (options->*(this->parse))(option, arg, cmdline); }
244
245   One_option option;
246   Parse_function parse;
247 };
248
249 }  // End namespace options.
250
251
252 // These are helper macros use by DEFINE_uint64/etc below.
253 // This macro is used inside the General_options_ class, so defines
254 // var() and set_var() as General_options methods.  Arguments as are
255 // for the constructor for One_option.  param_type__ is the same as
256 // type__ for built-in types, and "const type__ &" otherwise.
257 //
258 // When we define the linker command option "assert", the macro argument
259 // varname__ of DEFINE_var below will be replaced by "assert".  On Mac OSX
260 // assert.h is included implicitly by one of the library headers we use.  To
261 // avoid unintended macro substitution of "assert()", we need to enclose
262 // varname__ with parenthese.
263 #define DEFINE_var(varname__, dashes__, shortname__, default_value__,        \
264                    default_value_as_string__, helpstring__, helparg__,       \
265                    optional_arg__, type__, param_type__, parse_fn__)         \
266  public:                                                                     \
267   param_type__                                                               \
268   (varname__)() const                                                        \
269   { return this->varname__##_.value; }                                       \
270                                                                              \
271   bool                                                                       \
272   user_set_##varname__() const                                               \
273   { return this->varname__##_.user_set_via_option; }                         \
274                                                                              \
275   void                                                                       \
276   set_user_set_##varname__()                                                 \
277   { this->varname__##_.user_set_via_option = true; }                         \
278                                                                              \
279  private:                                                                    \
280   struct Struct_##varname__ : public options::Struct_var                     \
281   {                                                                          \
282     Struct_##varname__()                                                     \
283       : option(#varname__, dashes__, shortname__, default_value_as_string__, \
284                helpstring__, helparg__, optional_arg__, this),               \
285         user_set_via_option(false), value(default_value__)                   \
286     { }                                                                      \
287                                                                              \
288     void                                                                     \
289     parse_to_value(const char* option_name, const char* arg,                 \
290                    Command_line*, General_options*)                          \
291     {                                                                        \
292       parse_fn__(option_name, arg, &this->value);                            \
293       this->user_set_via_option = true;                                      \
294     }                                                                        \
295                                                                              \
296     options::One_option option;                                              \
297     bool user_set_via_option;                                                \
298     type__ value;                                                            \
299   };                                                                         \
300   Struct_##varname__ varname__##_;                                           \
301   void                                                                       \
302   set_##varname__(param_type__ value)                                        \
303   { this->varname__##_.value = value; }
304
305 // These macros allow for easy addition of a new commandline option.
306
307 // If no_helpstring__ is not NULL, then in addition to creating
308 // VARNAME, we also create an option called no-VARNAME (or, for a -z
309 // option, noVARNAME).
310 #define DEFINE_bool(varname__, dashes__, shortname__, default_value__,   \
311                     helpstring__, no_helpstring__)                       \
312   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
313              default_value__ ? "true" : "false", helpstring__, NULL,     \
314              false, bool, bool, options::parse_bool)                     \
315   struct Struct_no_##varname__ : public options::Struct_var              \
316   {                                                                      \
317     Struct_no_##varname__() : option((dashes__ == options::DASH_Z        \
318                                       ? "no" #varname__                  \
319                                       : "no-" #varname__),               \
320                                      dashes__, '\0',                     \
321                                      default_value__ ? "false" : "true", \
322                                      no_helpstring__, NULL, false, this) \
323     { }                                                                  \
324                                                                          \
325     void                                                                 \
326     parse_to_value(const char*, const char*,                             \
327                    Command_line*, General_options* options)              \
328     {                                                                    \
329       options->set_##varname__(false);                                   \
330       options->set_user_set_##varname__();                               \
331     }                                                                    \
332                                                                          \
333     options::One_option option;                                          \
334   };                                                                     \
335   Struct_no_##varname__ no_##varname__##_initializer_
336
337 #define DEFINE_enable(varname__, dashes__, shortname__, default_value__, \
338                       helpstring__, no_helpstring__)                     \
339   DEFINE_var(enable_##varname__, dashes__, shortname__, default_value__, \
340              default_value__ ? "true" : "false", helpstring__, NULL,     \
341              false, bool, bool, options::parse_bool)                     \
342   struct Struct_disable_##varname__ : public options::Struct_var         \
343   {                                                                      \
344     Struct_disable_##varname__() : option("disable-" #varname__,         \
345                                      dashes__, '\0',                     \
346                                      default_value__ ? "false" : "true", \
347                                      no_helpstring__, NULL, false, this) \
348     { }                                                                  \
349                                                                          \
350     void                                                                 \
351     parse_to_value(const char*, const char*,                             \
352                    Command_line*, General_options* options)              \
353     { options->set_enable_##varname__(false); }                          \
354                                                                          \
355     options::One_option option;                                          \
356   };                                                                     \
357   Struct_disable_##varname__ disable_##varname__##_initializer_
358
359 #define DEFINE_int(varname__, dashes__, shortname__, default_value__,   \
360                    helpstring__, helparg__)                             \
361   DEFINE_var(varname__, dashes__, shortname__, default_value__,         \
362              #default_value__, helpstring__, helparg__, false,          \
363              int, int, options::parse_int)
364
365 #define DEFINE_uint(varname__, dashes__, shortname__, default_value__,  \
366                    helpstring__, helparg__)                             \
367   DEFINE_var(varname__, dashes__, shortname__, default_value__,         \
368              #default_value__, helpstring__, helparg__, false,          \
369              int, int, options::parse_uint)
370
371 #define DEFINE_uint64(varname__, dashes__, shortname__, default_value__, \
372                       helpstring__, helparg__)                           \
373   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
374              #default_value__, helpstring__, helparg__, false,           \
375              uint64_t, uint64_t, options::parse_uint64)
376
377 #define DEFINE_double(varname__, dashes__, shortname__, default_value__, \
378                       helpstring__, helparg__)                           \
379   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
380              #default_value__, helpstring__, helparg__, false,           \
381              double, double, options::parse_double)
382
383 #define DEFINE_percent(varname__, dashes__, shortname__, default_value__, \
384                        helpstring__, helparg__)                           \
385   DEFINE_var(varname__, dashes__, shortname__, default_value__ / 100.0,   \
386              #default_value__, helpstring__, helparg__, false,            \
387              double, double, options::parse_percent)
388
389 #define DEFINE_string(varname__, dashes__, shortname__, default_value__, \
390                       helpstring__, helparg__)                           \
391   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
392              default_value__, helpstring__, helparg__, false,            \
393              const char*, const char*, options::parse_string)
394
395 // This is like DEFINE_string, but we convert each occurrence to a
396 // Search_directory and store it in a vector.  Thus we also have the
397 // add_to_VARNAME() method, to append to the vector.
398 #define DEFINE_dirlist(varname__, dashes__, shortname__,                  \
399                            helpstring__, helparg__)                       \
400   DEFINE_var(varname__, dashes__, shortname__, ,                          \
401              "", helpstring__, helparg__, false, options::Dir_list,       \
402              const options::Dir_list&, options::parse_dirlist)            \
403   void                                                                    \
404   add_to_##varname__(const char* new_value)                               \
405   { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
406   void                                                                    \
407   add_search_directory_to_##varname__(const Search_directory& dir)        \
408   { this->varname__##_.value.push_back(dir); }
409
410 // This is like DEFINE_string, but we store a set of strings.
411 #define DEFINE_set(varname__, dashes__, shortname__,                      \
412                    helpstring__, helparg__)                               \
413   DEFINE_var(varname__, dashes__, shortname__, ,                          \
414              "", helpstring__, helparg__, false, options::String_set,     \
415              const options::String_set&, options::parse_set)              \
416  public:                                                                  \
417   bool                                                                    \
418   any_##varname__() const                                                 \
419   { return !this->varname__##_.value.empty(); }                           \
420                                                                           \
421   bool                                                                    \
422   is_##varname__(const char* symbol) const                                \
423   {                                                                       \
424     return (!this->varname__##_.value.empty()                             \
425             && (this->varname__##_.value.find(std::string(symbol))        \
426                 != this->varname__##_.value.end()));                      \
427   }                                                                       \
428                                                                           \
429   options::String_set::const_iterator                                     \
430   varname__##_begin() const                                               \
431   { return this->varname__##_.value.begin(); }                            \
432                                                                           \
433   options::String_set::const_iterator                                     \
434   varname__##_end() const                                                 \
435   { return this->varname__##_.value.end(); }
436
437 // When you have a list of possible values (expressed as string)
438 // After helparg__ should come an initializer list, like
439 //   {"foo", "bar", "baz"}
440 #define DEFINE_enum(varname__, dashes__, shortname__, default_value__,   \
441                     helpstring__, helparg__, ...)                        \
442   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
443              default_value__, helpstring__, helparg__, false,            \
444              const char*, const char*, parse_choices_##varname__)        \
445  private:                                                                \
446   static void parse_choices_##varname__(const char* option_name,         \
447                                         const char* arg,                 \
448                                         const char** retval) {           \
449     const char* choices[] = __VA_ARGS__;                                 \
450     options::parse_choices(option_name, arg, retval,                     \
451                            choices, sizeof(choices) / sizeof(*choices)); \
452   }
453
454 // This is like DEFINE_bool, but VARNAME is the name of a different
455 // option.  This option becomes an alias for that one.  INVERT is true
456 // if this option is an inversion of the other one.
457 #define DEFINE_bool_alias(option__, varname__, dashes__, shortname__,   \
458                           helpstring__, no_helpstring__, invert__)      \
459  private:                                                               \
460   struct Struct_##option__ : public options::Struct_var                 \
461   {                                                                     \
462     Struct_##option__()                                                 \
463       : option(#option__, dashes__, shortname__, "", helpstring__,      \
464                NULL, false, this)                                       \
465     { }                                                                 \
466                                                                         \
467     void                                                                \
468     parse_to_value(const char*, const char*,                            \
469                    Command_line*, General_options* options)             \
470     {                                                                   \
471       options->set_##varname__(!invert__);                              \
472       options->set_user_set_##varname__();                              \
473     }                                                                   \
474                                                                         \
475     options::One_option option;                                         \
476   };                                                                    \
477   Struct_##option__ option__##_;                                        \
478                                                                         \
479   struct Struct_no_##option__ : public options::Struct_var              \
480   {                                                                     \
481     Struct_no_##option__()                                              \
482       : option((dashes__ == options::DASH_Z                             \
483                 ? "no" #option__                                        \
484                 : "no-" #option__),                                     \
485                dashes__, '\0', "", no_helpstring__,                     \
486                NULL, false, this)                                       \
487     { }                                                                 \
488                                                                         \
489     void                                                                \
490     parse_to_value(const char*, const char*,                            \
491                    Command_line*, General_options* options)             \
492     {                                                                   \
493       options->set_##varname__(invert__);                               \
494       options->set_user_set_##varname__();                              \
495     }                                                                   \
496                                                                         \
497     options::One_option option;                                         \
498   };                                                                    \
499   Struct_no_##option__ no_##option__##_initializer_
500
501 // This is like DEFINE_uint64, but VARNAME is the name of a different
502 // option.  This option becomes an alias for that one.
503 #define DEFINE_uint64_alias(option__, varname__, dashes__, shortname__, \
504                             helpstring__, helparg__)                    \
505  private:                                                               \
506   struct Struct_##option__ : public options::Struct_var                 \
507   {                                                                     \
508     Struct_##option__()                                                 \
509       : option(#option__, dashes__, shortname__, "", helpstring__,      \
510                helparg__, false, this)                                  \
511     { }                                                                 \
512                                                                         \
513     void                                                                \
514     parse_to_value(const char* option_name, const char* arg,            \
515                    Command_line*, General_options* options)             \
516     {                                                                   \
517       uint64_t value;                                                   \
518       options::parse_uint64(option_name, arg, &value);                  \
519       options->set_##varname__(value);                                  \
520       options->set_user_set_##varname__();                              \
521     }                                                                   \
522                                                                         \
523     options::One_option option;                                         \
524   };                                                                    \
525   Struct_##option__ option__##_;
526
527 // This is used for non-standard flags.  It defines no functions; it
528 // just calls General_options::parse_VARNAME whenever the flag is
529 // seen.  We declare parse_VARNAME as a static member of
530 // General_options; you are responsible for defining it there.
531 // helparg__ should be NULL iff this special-option is a boolean.
532 #define DEFINE_special(varname__, dashes__, shortname__,                \
533                        helpstring__, helparg__)                         \
534  private:                                                               \
535   void parse_##varname__(const char* option, const char* arg,           \
536                          Command_line* inputs);                         \
537   struct Struct_##varname__ : public options::Struct_special            \
538   {                                                                     \
539     Struct_##varname__()                                                \
540       : options::Struct_special(#varname__, dashes__, shortname__,      \
541                                 &General_options::parse_##varname__,    \
542                                 helpstring__, helparg__)                \
543     { }                                                                 \
544   };                                                                    \
545   Struct_##varname__ varname__##_initializer_
546
547 // An option that takes an optional string argument.  If the option is
548 // used with no argument, the value will be the default, and
549 // user_set_via_option will be true.
550 #define DEFINE_optional_string(varname__, dashes__, shortname__,        \
551                                default_value__,                         \
552                                helpstring__, helparg__)                 \
553   DEFINE_var(varname__, dashes__, shortname__, default_value__,         \
554              default_value__, helpstring__, helparg__, true,            \
555              const char*, const char*, options::parse_optional_string)
556
557 // A directory to search.  For each directory we record whether it is
558 // in the sysroot.  We need to know this so that, if a linker script
559 // is found within the sysroot, we will apply the sysroot to any files
560 // named by that script.
561
562 class Search_directory
563 {
564  public:
565   // We need a default constructor because we put this in a
566   // std::vector.
567   Search_directory()
568     : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
569   { }
570
571   // This is the usual constructor.
572   Search_directory(const std::string& name, bool put_in_sysroot)
573     : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
574   {
575     if (this->name_.empty())
576       this->name_ = ".";
577   }
578
579   // This is called if we have a sysroot.  The sysroot is prefixed to
580   // any entries for which put_in_sysroot_ is true.  is_in_sysroot_ is
581   // set to true for any enries which are in the sysroot (this will
582   // naturally include any entries for which put_in_sysroot_ is true).
583   // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
584   // passing SYSROOT to lrealpath.
585   void
586   add_sysroot(const char* sysroot, const char* canonical_sysroot);
587
588   // Get the directory name.
589   const std::string&
590   name() const
591   { return this->name_; }
592
593   // Return whether this directory is in the sysroot.
594   bool
595   is_in_sysroot() const
596   { return this->is_in_sysroot_; }
597
598   // Return whether this is considered a system directory.
599   bool
600   is_system_directory() const
601   { return this->put_in_sysroot_ || this->is_in_sysroot_; }
602
603  private:
604   // The directory name.
605   std::string name_;
606   // True if the sysroot should be added as a prefix for this
607   // directory (if there is a sysroot).  This is true for system
608   // directories that we search by default.
609   bool put_in_sysroot_;
610   // True if this directory is in the sysroot (if there is a sysroot).
611   // This is true if there is a sysroot and either 1) put_in_sysroot_
612   // is true, or 2) the directory happens to be in the sysroot based
613   // on a pathname comparison.
614   bool is_in_sysroot_;
615 };
616
617 class General_options
618 {
619  private:
620   // NOTE: For every option that you add here, also consider if you
621   // should add it to Position_dependent_options.
622   DEFINE_special(help, options::TWO_DASHES, '\0',
623                  N_("Report usage information"), NULL);
624   DEFINE_special(version, options::TWO_DASHES, 'v',
625                  N_("Report version information"), NULL);
626   DEFINE_special(V, options::EXACTLY_ONE_DASH, '\0',
627                  N_("Report version and target information"), NULL);
628
629   // These options are sorted approximately so that for each letter in
630   // the alphabet, we show the option whose shortname is that letter
631   // (if any) and then every longname that starts with that letter (in
632   // alphabetical order).  For both, lowercase sorts before uppercase.
633   // The -z options come last.
634
635   DEFINE_bool(add_needed, options::TWO_DASHES, '\0', false,
636               N_("Not supported"),
637               N_("Do not copy DT_NEEDED tags from shared libraries"));
638
639   DEFINE_bool_alias(allow_multiple_definition, muldefs, options::TWO_DASHES,
640                     '\0', N_("Allow multiple definitions of symbols"),
641                     N_("Do not allow multiple definitions"), false);
642
643   DEFINE_bool(allow_shlib_undefined, options::TWO_DASHES, '\0', false,
644               N_("Allow unresolved references in shared libraries"),
645               N_("Do not allow unresolved references in shared libraries"));
646
647   DEFINE_bool(as_needed, options::TWO_DASHES, '\0', false,
648               N_("Only set DT_NEEDED for shared libraries if used"),
649               N_("Always DT_NEEDED for shared libraries"));
650
651   DEFINE_enum(assert, options::ONE_DASH, '\0', NULL,
652               N_("Ignored"), N_("[ignored]"),
653               {"definitions", "nodefinitions", "nosymbolic", "pure-text"});
654
655   // This should really be an "enum", but it's too easy for folks to
656   // forget to update the list as they add new targets.  So we just
657   // accept any string.  We'll fail later (when the string is parsed),
658   // if the target isn't actually supported.
659   DEFINE_string(format, options::TWO_DASHES, 'b', "elf",
660                 N_("Set input format"), ("[elf,binary]"));
661
662   DEFINE_bool(Bdynamic, options::ONE_DASH, '\0', true,
663               N_("-l searches for shared libraries"), NULL);
664   DEFINE_bool_alias(Bstatic, Bdynamic, options::ONE_DASH, '\0',
665                     N_("-l does not search for shared libraries"), NULL,
666                     true);
667   DEFINE_bool_alias(dy, Bdynamic, options::ONE_DASH, '\0',
668                     N_("alias for -Bdynamic"), NULL, false);
669   DEFINE_bool_alias(dn, Bdynamic, options::ONE_DASH, '\0',
670                     N_("alias for -Bstatic"), NULL, true);
671
672   DEFINE_bool(Bgroup, options::ONE_DASH, '\0', false,
673               N_("Use group name lookup rules for shared library"), NULL);
674
675   DEFINE_bool(Bsymbolic, options::ONE_DASH, '\0', false,
676               N_("Bind defined symbols locally"), NULL);
677
678   DEFINE_bool(Bsymbolic_functions, options::ONE_DASH, '\0', false,
679               N_("Bind defined function symbols locally"), NULL);
680
681   DEFINE_optional_string(build_id, options::TWO_DASHES, '\0', "tree",
682                          N_("Generate build ID note"),
683                          N_("[=STYLE]"));
684
685   DEFINE_uint64(build_id_chunk_size_for_treehash,
686                 options::TWO_DASHES, '\0', 2 << 20,
687                 N_("Chunk size for '--build-id=tree'"), N_("SIZE"));
688
689   DEFINE_uint64(build_id_min_file_size_for_treehash, options::TWO_DASHES,
690                 '\0', 40 << 20,
691                 N_("Minimum output file size for '--build-id=tree' to work"
692                    " differently than '--build-id=sha1'"), N_("SIZE"));
693
694   DEFINE_bool(check_sections, options::TWO_DASHES, '\0', true,
695               N_("Check segment addresses for overlaps (default)"),
696               N_("Do not check segment addresses for overlaps"));
697
698 #ifdef HAVE_ZLIB_H
699   DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
700               N_("Compress .debug_* sections in the output file"),
701               ("[none,zlib]"),
702               {"none", "zlib"});
703 #else
704   DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
705               N_("Compress .debug_* sections in the output file"),
706               N_("[none]"),
707               {"none"});
708 #endif
709
710   DEFINE_bool(copy_dt_needed_entries, options::TWO_DASHES, '\0', false,
711               N_("Not supported"),
712               N_("Do not copy DT_NEEDED tags from shared libraries"));
713
714   DEFINE_bool(cref, options::TWO_DASHES, '\0', false,
715               N_("Output cross reference table"),
716               N_("Do not output cross reference table"));
717
718   DEFINE_bool(ctors_in_init_array, options::TWO_DASHES, '\0', true,
719               N_("Use DT_INIT_ARRAY for all constructors (default)"),
720               N_("Handle constructors as directed by compiler"));
721
722   DEFINE_bool(define_common, options::TWO_DASHES, 'd', false,
723               N_("Define common symbols"),
724               N_("Do not define common symbols"));
725   DEFINE_bool(dc, options::ONE_DASH, '\0', false,
726               N_("Alias for -d"), NULL);
727   DEFINE_bool(dp, options::ONE_DASH, '\0', false,
728               N_("Alias for -d"), NULL);
729
730   DEFINE_string(debug, options::TWO_DASHES, '\0', "",
731                 N_("Turn on debugging"),
732                 N_("[all,files,script,task][,...]"));
733
734   DEFINE_special(defsym, options::TWO_DASHES, '\0',
735                  N_("Define a symbol"), N_("SYMBOL=EXPRESSION"));
736
737   DEFINE_optional_string(demangle, options::TWO_DASHES, '\0', NULL,
738                          N_("Demangle C++ symbols in log messages"),
739                          N_("[=STYLE]"));
740
741   DEFINE_bool(no_demangle, options::TWO_DASHES, '\0', false,
742               N_("Do not demangle C++ symbols in log messages"),
743               NULL);
744
745   DEFINE_bool(detect_odr_violations, options::TWO_DASHES, '\0', false,
746               N_("Look for violations of the C++ One Definition Rule"),
747               N_("Do not look for violations of the C++ One Definition Rule"));
748
749   DEFINE_bool(discard_all, options::TWO_DASHES, 'x', false,
750               N_("Delete all local symbols"), NULL);
751   DEFINE_bool(discard_locals, options::TWO_DASHES, 'X', false,
752               N_("Delete all temporary local symbols"), NULL);
753
754   DEFINE_bool(dynamic_list_data, options::TWO_DASHES, '\0', false,
755               N_("Add data symbols to dynamic symbols"), NULL);
756
757   DEFINE_bool(dynamic_list_cpp_new, options::TWO_DASHES, '\0', false,
758               N_("Add C++ operator new/delete to dynamic symbols"), NULL);
759
760   DEFINE_bool(dynamic_list_cpp_typeinfo, options::TWO_DASHES, '\0', false,
761               N_("Add C++ typeinfo to dynamic symbols"), NULL);
762
763   DEFINE_special(dynamic_list, options::TWO_DASHES, '\0',
764                  N_("Read a list of dynamic symbols"), N_("FILE"));
765
766   DEFINE_string(entry, options::TWO_DASHES, 'e', NULL,
767                 N_("Set program start address"), N_("ADDRESS"));
768
769   DEFINE_special(exclude_libs, options::TWO_DASHES, '\0',
770                  N_("Exclude libraries from automatic export"),
771                  N_(("lib,lib ...")));
772
773   DEFINE_bool(export_dynamic, options::TWO_DASHES, 'E', false,
774               N_("Export all dynamic symbols"),
775               N_("Do not export all dynamic symbols (default)"));
776
777   DEFINE_set(export_dynamic_symbol, options::TWO_DASHES, '\0',
778              N_("Export SYMBOL to dynamic symbol table"), N_("SYMBOL"));
779
780   DEFINE_special(EB, options::ONE_DASH, '\0',
781                  N_("Link big-endian objects."), NULL);
782
783   DEFINE_special(EL, options::ONE_DASH, '\0',
784                  N_("Link little-endian objects."), NULL);
785
786   DEFINE_bool(eh_frame_hdr, options::TWO_DASHES, '\0', false,
787               N_("Create exception frame header"), NULL);
788
789   DEFINE_bool(enum_size_warning, options::TWO_DASHES, '\0', true, NULL,
790               N_("(ARM only) Do not warn about objects with incompatible "
791                  "enum sizes"));
792
793   DEFINE_set(auxiliary, options::TWO_DASHES, 'f',
794              N_("Auxiliary filter for shared object symbol table"),
795              N_("SHLIB"));
796
797   DEFINE_string(filter, options::TWO_DASHES, 'F', NULL,
798                 N_("Filter for shared object symbol table"),
799                 N_("SHLIB"));
800
801   DEFINE_bool(fatal_warnings, options::TWO_DASHES, '\0', false,
802               N_("Treat warnings as errors"),
803               N_("Do not treat warnings as errors"));
804
805   DEFINE_string(fini, options::ONE_DASH, '\0', "_fini",
806                 N_("Call SYMBOL at unload-time"), N_("SYMBOL"));
807
808   DEFINE_bool(fix_cortex_a8, options::TWO_DASHES, '\0', false,
809               N_("(ARM only) Fix binaries for Cortex-A8 erratum."),
810               N_("(ARM only) Do not fix binaries for Cortex-A8 erratum."));
811
812   DEFINE_bool(fix_arm1176, options::TWO_DASHES, '\0', true,
813               N_("(ARM only) Fix binaries for ARM1176 erratum."),
814               N_("(ARM only) Do not fix binaries for ARM1176 erratum."));
815
816   DEFINE_bool(merge_exidx_entries, options::TWO_DASHES, '\0', true,
817               N_("(ARM only) Merge exidx entries in debuginfo."),
818               N_("(ARM only) Do not merge exidx entries in debuginfo."));
819
820   DEFINE_special(fix_v4bx, options::TWO_DASHES, '\0',
821                  N_("(ARM only) Rewrite BX rn as MOV pc, rn for ARMv4"),
822                  NULL);
823
824   DEFINE_special(fix_v4bx_interworking, options::TWO_DASHES, '\0',
825                  N_("(ARM only) Rewrite BX rn branch to ARMv4 interworking "
826                     "veneer"),
827                  NULL);
828
829   DEFINE_bool(g, options::EXACTLY_ONE_DASH, '\0', false,
830               N_("Ignored"), NULL);
831
832   DEFINE_bool(gdb_index, options::TWO_DASHES, '\0', false,
833               N_("Generate .gdb_index section"),
834               N_("Do not generate .gdb_index section"));
835
836   DEFINE_bool(gnu_unique, options::TWO_DASHES, '\0', true,
837               N_("Enable STB_GNU_UNIQUE symbol binding (default)"),
838               N_("Disable STB_GNU_UNIQUE symbol binding"));
839
840   DEFINE_string(soname, options::ONE_DASH, 'h', NULL,
841                 N_("Set shared library name"), N_("FILENAME"));
842
843   DEFINE_double(hash_bucket_empty_fraction, options::TWO_DASHES, '\0', 0.0,
844                 N_("Min fraction of empty buckets in dynamic hash"),
845                 N_("FRACTION"));
846
847   DEFINE_enum(hash_style, options::TWO_DASHES, '\0', "sysv",
848               N_("Dynamic hash style"), N_("[sysv,gnu,both]"),
849               {"sysv", "gnu", "both"});
850
851   DEFINE_string(dynamic_linker, options::TWO_DASHES, 'I', NULL,
852                 N_("Set dynamic linker path"), N_("PROGRAM"));
853
854   DEFINE_special(incremental, options::TWO_DASHES, '\0',
855                  N_("Do an incremental link if possible; "
856                     "otherwise, do a full link and prepare output "
857                     "for incremental linking"), NULL);
858
859   DEFINE_special(no_incremental, options::TWO_DASHES, '\0',
860                  N_("Do a full link (default)"), NULL);
861
862   DEFINE_special(incremental_full, options::TWO_DASHES, '\0',
863                  N_("Do a full link and "
864                     "prepare output for incremental linking"), NULL);
865
866   DEFINE_special(incremental_update, options::TWO_DASHES, '\0',
867                  N_("Do an incremental link; exit if not possible"), NULL);
868
869   DEFINE_string(incremental_base, options::TWO_DASHES, '\0', NULL,
870                 N_("Set base file for incremental linking"
871                    " (default is output file)"),
872                 N_("FILE"));
873
874   DEFINE_special(incremental_changed, options::TWO_DASHES, '\0',
875                  N_("Assume files changed"), NULL);
876
877   DEFINE_special(incremental_unchanged, options::TWO_DASHES, '\0',
878                  N_("Assume files didn't change"), NULL);
879
880   DEFINE_special(incremental_unknown, options::TWO_DASHES, '\0',
881                  N_("Use timestamps to check files (default)"), NULL);
882
883   DEFINE_special(incremental_startup_unchanged, options::TWO_DASHES, '\0',
884                  N_("Assume startup files unchanged "
885                     "(files preceding this option)"), NULL);
886
887   DEFINE_percent(incremental_patch, options::TWO_DASHES, '\0', 10,
888                  N_("Amount of extra space to allocate for patches"),
889                  N_("PERCENT"));
890
891   DEFINE_string(init, options::ONE_DASH, '\0', "_init",
892                 N_("Call SYMBOL at load-time"), N_("SYMBOL"));
893
894   DEFINE_special(just_symbols, options::TWO_DASHES, '\0',
895                  N_("Read only symbol values from FILE"), N_("FILE"));
896
897   DEFINE_bool(map_whole_files, options::TWO_DASHES, '\0',
898               sizeof(void*) >= 8,
899               N_("Map whole files to memory (default on 64-bit hosts)"),
900               N_("Map relevant file parts to memory (default on 32-bit "
901                  "hosts)"));
902   DEFINE_bool(keep_files_mapped, options::TWO_DASHES, '\0', true,
903               N_("Keep files mapped across passes (default)"),
904               N_("Release mapped files after each pass"));
905
906   DEFINE_bool(ld_generated_unwind_info, options::TWO_DASHES, '\0', true,
907               N_("Generate unwind information for PLT (default)"),
908               N_("Do not generate unwind information for PLT"));
909
910   DEFINE_special(library, options::TWO_DASHES, 'l',
911                  N_("Search for library LIBNAME"), N_("LIBNAME"));
912
913   DEFINE_dirlist(library_path, options::TWO_DASHES, 'L',
914                  N_("Add directory to search path"), N_("DIR"));
915
916   DEFINE_bool(text_reorder, options::TWO_DASHES, '\0', true,
917               N_("Enable text section reordering for GCC section names "
918                  "(default)"),
919               N_("Disable text section reordering for GCC section names"));
920
921   DEFINE_bool(nostdlib, options::ONE_DASH, '\0', false,
922               N_(" Only search directories specified on the command line."),
923               NULL);
924
925   DEFINE_bool(rosegment, options::TWO_DASHES, '\0', false,
926               N_(" Put read-only non-executable sections in their own segment"),
927               NULL);
928
929   DEFINE_string(m, options::EXACTLY_ONE_DASH, 'm', "",
930                 N_("Set GNU linker emulation; obsolete"), N_("EMULATION"));
931
932   DEFINE_bool(mmap_output_file, options::TWO_DASHES, '\0', true,
933               N_("Map the output file for writing (default)."),
934               N_("Do not map the output file for writing."));
935
936   DEFINE_bool(print_map, options::TWO_DASHES, 'M', false,
937               N_("Write map file on standard output"), NULL);
938   DEFINE_string(Map, options::ONE_DASH, '\0', NULL, N_("Write map file"),
939                 N_("MAPFILENAME"));
940
941   DEFINE_bool(nmagic, options::TWO_DASHES, 'n', false,
942               N_("Do not page align data"), NULL);
943   DEFINE_bool(omagic, options::EXACTLY_TWO_DASHES, 'N', false,
944               N_("Do not page align data, do not make text readonly"),
945               N_("Page align data, make text readonly"));
946
947   DEFINE_enable(new_dtags, options::EXACTLY_TWO_DASHES, '\0', true,
948                 N_("Enable use of DT_RUNPATH and DT_FLAGS"),
949                 N_("Disable use of DT_RUNPATH and DT_FLAGS"));
950
951   DEFINE_bool(noinhibit_exec, options::TWO_DASHES, '\0', false,
952               N_("Create an output file even if errors occur"), NULL);
953
954   DEFINE_bool_alias(no_undefined, defs, options::TWO_DASHES, '\0',
955                     N_("Report undefined symbols (even with --shared)"),
956                     NULL, false);
957
958   DEFINE_string(output, options::TWO_DASHES, 'o', "a.out",
959                 N_("Set output file name"), N_("FILE"));
960
961   DEFINE_uint(optimize, options::EXACTLY_ONE_DASH, 'O', 0,
962               N_("Optimize output file size"), N_("LEVEL"));
963
964   DEFINE_string(oformat, options::EXACTLY_TWO_DASHES, '\0', "elf",
965                 N_("Set output format"), N_("[binary]"));
966
967   DEFINE_bool(p, options::ONE_DASH, '\0', false,
968               N_("(ARM only) Ignore for backward compatibility"), NULL);
969
970   DEFINE_bool(pie, options::ONE_DASH, '\0', false,
971               N_("Create a position independent executable"), NULL);
972   DEFINE_bool_alias(pic_executable, pie, options::TWO_DASHES, '\0',
973                     N_("Create a position independent executable"), NULL,
974                     false);
975
976   DEFINE_bool(pipeline_knowledge, options::ONE_DASH, '\0', false,
977               NULL, N_("(ARM only) Ignore for backward compatibility"));
978
979   DEFINE_var(plt_align, options::TWO_DASHES, '\0', 0, "5",
980              N_("(PowerPC64 only) Align PLT call stubs to fit cache lines"),
981              N_("[=P2ALIGN]"), true, int, int, options::parse_uint);
982
983   DEFINE_bool(plt_static_chain, options::TWO_DASHES, '\0', false,
984               N_("(PowerPC64 only) PLT call stubs should load r11"),
985               N_("(PowerPC64 only) PLT call stubs should not load r11"));
986
987   DEFINE_bool(plt_thread_safe, options::TWO_DASHES, '\0', false,
988               N_("(PowerPC64 only) PLT call stubs with load-load barrier"),
989               N_("(PowerPC64 only) PLT call stubs without barrier"));
990
991 #ifdef ENABLE_PLUGINS
992   DEFINE_special(plugin, options::TWO_DASHES, '\0',
993                  N_("Load a plugin library"), N_("PLUGIN"));
994   DEFINE_special(plugin_opt, options::TWO_DASHES, '\0',
995                  N_("Pass an option to the plugin"), N_("OPTION"));
996 #endif
997
998   DEFINE_bool(posix_fallocate, options::TWO_DASHES, '\0', true,
999               N_("Use posix_fallocate to reserve space in the output file"
1000                  " (default)."),
1001               N_("Use fallocate or ftruncate to reserve space."));
1002
1003   DEFINE_bool(preread_archive_symbols, options::TWO_DASHES, '\0', false,
1004               N_("Preread archive symbols when multi-threaded"), NULL);
1005
1006   DEFINE_bool(print_output_format, options::TWO_DASHES, '\0', false,
1007               N_("Print default output format"), NULL);
1008
1009   DEFINE_string(print_symbol_counts, options::TWO_DASHES, '\0', NULL,
1010                 N_("Print symbols defined and used for each input"),
1011                 N_("FILENAME"));
1012
1013   DEFINE_bool(Qy, options::EXACTLY_ONE_DASH, '\0', false,
1014               N_("Ignored for SVR4 compatibility"), NULL);
1015
1016   DEFINE_bool(emit_relocs, options::TWO_DASHES, 'q', false,
1017               N_("Generate relocations in output"), NULL);
1018
1019   DEFINE_bool(relocatable, options::EXACTLY_ONE_DASH, 'r', false,
1020               N_("Generate relocatable output"), NULL);
1021   DEFINE_bool_alias(i, relocatable, options::EXACTLY_ONE_DASH, '\0',
1022                     N_("Synonym for -r"), NULL, false);
1023
1024   DEFINE_bool(relax, options::TWO_DASHES, '\0', false,
1025               N_("Relax branches on certain targets"), NULL);
1026
1027   DEFINE_string(retain_symbols_file, options::TWO_DASHES, '\0', NULL,
1028                 N_("keep only symbols listed in this file"), N_("FILE"));
1029
1030   // -R really means -rpath, but can mean --just-symbols for
1031   // compatibility with GNU ld.  -rpath is always -rpath, so we list
1032   // it separately.
1033   DEFINE_special(R, options::EXACTLY_ONE_DASH, 'R',
1034                  N_("Add DIR to runtime search path"), N_("DIR"));
1035
1036   DEFINE_dirlist(rpath, options::ONE_DASH, '\0',
1037                  N_("Add DIR to runtime search path"), N_("DIR"));
1038
1039   DEFINE_dirlist(rpath_link, options::TWO_DASHES, '\0',
1040                  N_("Add DIR to link time shared library search path"),
1041                  N_("DIR"));
1042
1043   DEFINE_string(section_ordering_file, options::TWO_DASHES, '\0', NULL,
1044                 N_("Layout sections in the order specified."),
1045                 N_("FILENAME"));
1046
1047   DEFINE_special(section_start, options::TWO_DASHES, '\0',
1048                  N_("Set address of section"), N_("SECTION=ADDRESS"));
1049
1050   DEFINE_optional_string(sort_common, options::TWO_DASHES, '\0', NULL,
1051                          N_("Sort common symbols by alignment"),
1052                          N_("[={ascending,descending}]"));
1053
1054   DEFINE_uint(spare_dynamic_tags, options::TWO_DASHES, '\0', 5,
1055               N_("Dynamic tag slots to reserve (default 5)"),
1056               N_("COUNT"));
1057
1058   DEFINE_bool(strip_all, options::TWO_DASHES, 's', false,
1059               N_("Strip all symbols"), NULL);
1060   DEFINE_bool(strip_debug, options::TWO_DASHES, 'S', false,
1061               N_("Strip debugging information"), NULL);
1062   DEFINE_bool(strip_debug_non_line, options::TWO_DASHES, '\0', false,
1063               N_("Emit only debug line number information"), NULL);
1064   DEFINE_bool(strip_debug_gdb, options::TWO_DASHES, '\0', false,
1065               N_("Strip debug symbols that are unused by gdb "
1066                  "(at least versions <= 7.4)"), NULL);
1067   DEFINE_bool(strip_lto_sections, options::TWO_DASHES, '\0', true,
1068               N_("Strip LTO intermediate code sections"), NULL);
1069
1070   DEFINE_int(stub_group_size, options::TWO_DASHES , '\0', 1,
1071              N_("(ARM, PowerPC only) The maximum distance from instructions "
1072                 "in a group of sections to their stubs.  Negative values mean "
1073                 "stubs are always after (PowerPC before) the group.  1 means "
1074                 "use default size.\n"),
1075              N_("SIZE"));
1076
1077   DEFINE_bool(no_keep_memory, options::TWO_DASHES, '\0', false,
1078               N_("Use less memory and more disk I/O "
1079                  "(included only for compatibility with GNU ld)"), NULL);
1080
1081   DEFINE_bool(shared, options::ONE_DASH, 'G', false,
1082               N_("Generate shared library"), NULL);
1083
1084   DEFINE_bool(Bshareable, options::ONE_DASH, '\0', false,
1085               N_("Generate shared library"), NULL);
1086
1087   DEFINE_uint(split_stack_adjust_size, options::TWO_DASHES, '\0', 0x4000,
1088               N_("Stack size when -fsplit-stack function calls non-split"),
1089               N_("SIZE"));
1090
1091   // This is not actually special in any way, but I need to give it
1092   // a non-standard accessor-function name because 'static' is a keyword.
1093   DEFINE_special(static, options::ONE_DASH, '\0',
1094                  N_("Do not link against shared libraries"), NULL);
1095
1096   DEFINE_enum(icf, options::TWO_DASHES, '\0', "none",
1097               N_("Identical Code Folding. "
1098                  "\'--icf=safe\' Folds ctors, dtors and functions whose"
1099                  " pointers are definitely not taken."),
1100               ("[none,all,safe]"),      
1101               {"none", "all", "safe"});
1102
1103   DEFINE_uint(icf_iterations, options::TWO_DASHES , '\0', 0,
1104               N_("Number of iterations of ICF (default 2)"), N_("COUNT"));
1105
1106   DEFINE_bool(print_icf_sections, options::TWO_DASHES, '\0', false,
1107               N_("List folded identical sections on stderr"),
1108               N_("Do not list folded identical sections"));
1109
1110   DEFINE_set(keep_unique, options::TWO_DASHES, '\0',
1111              N_("Do not fold this symbol during ICF"), N_("SYMBOL"));
1112
1113   DEFINE_bool(gc_sections, options::TWO_DASHES, '\0', false,
1114               N_("Remove unused sections"),
1115               N_("Don't remove unused sections (default)"));
1116
1117   DEFINE_bool(print_gc_sections, options::TWO_DASHES, '\0', false,
1118               N_("List removed unused sections on stderr"),
1119               N_("Do not list removed unused sections"));
1120
1121   DEFINE_bool(stats, options::TWO_DASHES, '\0', false,
1122               N_("Print resource usage statistics"), NULL);
1123
1124   DEFINE_string(sysroot, options::TWO_DASHES, '\0', "",
1125                 N_("Set target system root directory"), N_("DIR"));
1126
1127   DEFINE_bool(trace, options::TWO_DASHES, 't', false,
1128               N_("Print the name of each input file"), NULL);
1129
1130   DEFINE_special(script, options::TWO_DASHES, 'T',
1131                  N_("Read linker script"), N_("FILE"));
1132
1133   DEFINE_bool(threads, options::TWO_DASHES, '\0', false,
1134               N_("Run the linker multi-threaded"),
1135               N_("Do not run the linker multi-threaded"));
1136   DEFINE_uint(thread_count, options::TWO_DASHES, '\0', 0,
1137               N_("Number of threads to use"), N_("COUNT"));
1138   DEFINE_uint(thread_count_initial, options::TWO_DASHES, '\0', 0,
1139               N_("Number of threads to use in initial pass"), N_("COUNT"));
1140   DEFINE_uint(thread_count_middle, options::TWO_DASHES, '\0', 0,
1141               N_("Number of threads to use in middle pass"), N_("COUNT"));
1142   DEFINE_uint(thread_count_final, options::TWO_DASHES, '\0', 0,
1143               N_("Number of threads to use in final pass"), N_("COUNT"));
1144
1145   DEFINE_uint64(Tbss, options::ONE_DASH, '\0', -1U,
1146                 N_("Set the address of the bss segment"), N_("ADDRESS"));
1147   DEFINE_uint64(Tdata, options::ONE_DASH, '\0', -1U,
1148                 N_("Set the address of the data segment"), N_("ADDRESS"));
1149   DEFINE_uint64(Ttext, options::ONE_DASH, '\0', -1U,
1150                 N_("Set the address of the text segment"), N_("ADDRESS"));
1151   DEFINE_uint64_alias(Ttext_segment, Ttext, options::ONE_DASH, '\0',
1152                       N_("Set the address of the text segment"),
1153                       N_("ADDRESS"));
1154
1155   DEFINE_bool(toc_optimize, options::TWO_DASHES, '\0', true,
1156               N_("(PowerPC64 only) Optimize TOC code sequences"),
1157               N_("(PowerPC64 only) Don't optimize TOC code sequences"));
1158
1159   DEFINE_bool(toc_sort, options::TWO_DASHES, '\0', true,
1160               N_("(PowerPC64 only) Sort TOC and GOT sections"),
1161               N_("(PowerPC64 only) Don't sort TOC and GOT sections"));
1162
1163   DEFINE_set(undefined, options::TWO_DASHES, 'u',
1164              N_("Create undefined reference to SYMBOL"), N_("SYMBOL"));
1165
1166   DEFINE_enum(unresolved_symbols, options::TWO_DASHES, '\0', NULL,
1167               N_("How to handle unresolved symbols"),
1168               ("ignore-all,report-all,ignore-in-object-files,"
1169                "ignore-in-shared-libs"),
1170               {"ignore-all", "report-all", "ignore-in-object-files",
1171                   "ignore-in-shared-libs"});
1172
1173   DEFINE_bool(verbose, options::TWO_DASHES, '\0', false,
1174               N_("Synonym for --debug=files"), NULL);
1175
1176   DEFINE_special(version_script, options::TWO_DASHES, '\0',
1177                  N_("Read version script"), N_("FILE"));
1178
1179   DEFINE_bool(warn_common, options::TWO_DASHES, '\0', false,
1180               N_("Warn about duplicate common symbols"),
1181               N_("Do not warn about duplicate common symbols (default)"));
1182
1183   DEFINE_bool(warn_constructors, options::TWO_DASHES, '\0', false,
1184               N_("Ignored"), N_("Ignored"));
1185
1186   DEFINE_bool(warn_execstack, options::TWO_DASHES, '\0', false,
1187               N_("Warn if the stack is executable"),
1188               N_("Do not warn if the stack is executable (default)"));
1189
1190   DEFINE_bool(warn_mismatch, options::TWO_DASHES, '\0', true,
1191               NULL, N_("Don't warn about mismatched input files"));
1192
1193   DEFINE_bool(warn_multiple_gp, options::TWO_DASHES, '\0', false,
1194               N_("Ignored"), NULL);
1195
1196   DEFINE_bool(warn_search_mismatch, options::TWO_DASHES, '\0', true,
1197               N_("Warn when skipping an incompatible library"),
1198               N_("Don't warn when skipping an incompatible library"));
1199
1200   DEFINE_bool(warn_shared_textrel, options::TWO_DASHES, '\0', false,
1201               N_("Warn if text segment is not shareable"),
1202               N_("Do not warn if text segment is not shareable (default)"));
1203
1204   DEFINE_bool(warn_unresolved_symbols, options::TWO_DASHES, '\0', false,
1205               N_("Report unresolved symbols as warnings"),
1206               NULL);
1207   DEFINE_bool_alias(error_unresolved_symbols, warn_unresolved_symbols,
1208                     options::TWO_DASHES, '\0',
1209                     N_("Report unresolved symbols as errors"),
1210                     NULL, true);
1211
1212   DEFINE_bool(wchar_size_warning, options::TWO_DASHES, '\0', true, NULL,
1213               N_("(ARM only) Do not warn about objects with incompatible "
1214                  "wchar_t sizes"));
1215
1216   DEFINE_bool(whole_archive, options::TWO_DASHES, '\0', false,
1217               N_("Include all archive contents"),
1218               N_("Include only needed archive contents"));
1219
1220   DEFINE_set(wrap, options::TWO_DASHES, '\0',
1221              N_("Use wrapper functions for SYMBOL"), N_("SYMBOL"));
1222
1223   DEFINE_set(trace_symbol, options::TWO_DASHES, 'y',
1224              N_("Trace references to symbol"), N_("SYMBOL"));
1225
1226   DEFINE_bool(undefined_version, options::TWO_DASHES, '\0', true,
1227               N_("Allow unused version in script (default)"),
1228               N_("Do not allow unused version in script"));
1229
1230   DEFINE_string(Y, options::EXACTLY_ONE_DASH, 'Y', "",
1231                 N_("Default search path for Solaris compatibility"),
1232                 N_("PATH"));
1233
1234   DEFINE_special(start_group, options::TWO_DASHES, '(',
1235                  N_("Start a library search group"), NULL);
1236   DEFINE_special(end_group, options::TWO_DASHES, ')',
1237                  N_("End a library search group"), NULL);
1238
1239
1240   DEFINE_special(start_lib, options::TWO_DASHES, '\0',
1241                  N_("Start a library"), NULL);
1242   DEFINE_special(end_lib, options::TWO_DASHES, '\0',
1243                  N_("End a library "), NULL);
1244
1245   DEFINE_string(fuse_ld, options::ONE_DASH, '\0', "",
1246                 N_("Ignored for GCC linker option compatibility"),
1247                 "");
1248
1249   // The -z options.
1250
1251   DEFINE_bool(combreloc, options::DASH_Z, '\0', true,
1252               N_("Sort dynamic relocs"),
1253               N_("Do not sort dynamic relocs"));
1254   DEFINE_uint64(common_page_size, options::DASH_Z, '\0', 0,
1255                 N_("Set common page size to SIZE"), N_("SIZE"));
1256   DEFINE_bool(defs, options::DASH_Z, '\0', false,
1257               N_("Report undefined symbols (even with --shared)"),
1258               NULL);
1259   DEFINE_bool(execstack, options::DASH_Z, '\0', false,
1260               N_("Mark output as requiring executable stack"), NULL);
1261   DEFINE_bool(initfirst, options::DASH_Z, '\0', false,
1262               N_("Mark DSO to be initialized first at runtime"),
1263               NULL);
1264   DEFINE_bool(interpose, options::DASH_Z, '\0', false,
1265               N_("Mark object to interpose all DSOs but executable"),
1266               NULL);
1267   DEFINE_bool_alias(lazy, now, options::DASH_Z, '\0',
1268                     N_("Mark object for lazy runtime binding (default)"),
1269                     NULL, true);
1270   DEFINE_bool(loadfltr, options::DASH_Z, '\0', false,
1271               N_("Mark object requiring immediate process"),
1272               NULL);
1273   DEFINE_uint64(max_page_size, options::DASH_Z, '\0', 0,
1274                 N_("Set maximum page size to SIZE"), N_("SIZE"));
1275   DEFINE_bool(muldefs, options::DASH_Z, '\0', false,
1276               N_("Allow multiple definitions of symbols"),
1277               NULL);
1278   // copyreloc is here in the list because there is only -z
1279   // nocopyreloc, not -z copyreloc.
1280   DEFINE_bool(copyreloc, options::DASH_Z, '\0', true,
1281               NULL,
1282               N_("Do not create copy relocs"));
1283   DEFINE_bool(nodefaultlib, options::DASH_Z, '\0', false,
1284               N_("Mark object not to use default search paths"),
1285               NULL);
1286   DEFINE_bool(nodelete, options::DASH_Z, '\0', false,
1287               N_("Mark DSO non-deletable at runtime"),
1288               NULL);
1289   DEFINE_bool(nodlopen, options::DASH_Z, '\0', false,
1290               N_("Mark DSO not available to dlopen"),
1291               NULL);
1292   DEFINE_bool(nodump, options::DASH_Z, '\0', false,
1293               N_("Mark DSO not available to dldump"),
1294               NULL);
1295   DEFINE_bool(noexecstack, options::DASH_Z, '\0', false,
1296               N_("Mark output as not requiring executable stack"), NULL);
1297   DEFINE_bool(now, options::DASH_Z, '\0', false,
1298               N_("Mark object for immediate function binding"),
1299               NULL);
1300   DEFINE_bool(origin, options::DASH_Z, '\0', false,
1301               N_("Mark DSO to indicate that needs immediate $ORIGIN "
1302                  "processing at runtime"), NULL);
1303   DEFINE_bool(relro, options::DASH_Z, '\0', false,
1304               N_("Where possible mark variables read-only after relocation"),
1305               N_("Don't mark variables read-only after relocation"));
1306   DEFINE_bool(text, options::DASH_Z, '\0', false,
1307               N_("Do not permit relocations in read-only segments"),
1308               N_("Permit relocations in read-only segments (default)"));
1309   DEFINE_bool_alias(textoff, text, options::DASH_Z, '\0',
1310                     N_("Permit relocations in read-only segments (default)"),
1311                     NULL, true);
1312
1313  public:
1314   typedef options::Dir_list Dir_list;
1315
1316   General_options();
1317
1318   // Does post-processing on flags, making sure they all have
1319   // non-conflicting values.  Also converts some flags from their
1320   // "standard" types (string, etc), to another type (enum, DirList),
1321   // which can be accessed via a separate method.  Dies if it notices
1322   // any problems.
1323   void finalize();
1324
1325   // True if we printed the version information.
1326   bool
1327   printed_version() const
1328   { return this->printed_version_; }
1329
1330   // The macro defines output() (based on --output), but that's a
1331   // generic name.  Provide this alternative name, which is clearer.
1332   const char*
1333   output_file_name() const
1334   { return this->output(); }
1335
1336   // This is not defined via a flag, but combines flags to say whether
1337   // the output is position-independent or not.
1338   bool
1339   output_is_position_independent() const
1340   { return this->shared() || this->pie(); }
1341
1342   // Return true if the output is something that can be exec()ed, such
1343   // as a static executable, or a position-dependent or
1344   // position-independent executable, but not a dynamic library or an
1345   // object file.
1346   bool
1347   output_is_executable() const
1348   { return !this->shared() && !this->relocatable(); }
1349
1350   // This would normally be static(), and defined automatically, but
1351   // since static is a keyword, we need to come up with our own name.
1352   bool
1353   is_static() const
1354   { return static_; }
1355
1356   // In addition to getting the input and output formats as a string
1357   // (via format() and oformat()), we also give access as an enum.
1358   enum Object_format
1359   {
1360     // Ordinary ELF.
1361     OBJECT_FORMAT_ELF,
1362     // Straight binary format.
1363     OBJECT_FORMAT_BINARY
1364   };
1365
1366   // Convert a string to an Object_format.  Gives an error if the
1367   // string is not recognized.
1368   static Object_format
1369   string_to_object_format(const char* arg);
1370
1371   // Note: these functions are not very fast.
1372   Object_format format_enum() const;
1373   Object_format oformat_enum() const;
1374
1375   // Return whether FILENAME is in a system directory.
1376   bool
1377   is_in_system_directory(const std::string& name) const;
1378
1379   // RETURN whether SYMBOL_NAME should be kept, according to symbols_to_retain_.
1380   bool
1381   should_retain_symbol(const char* symbol_name) const
1382     {
1383       if (symbols_to_retain_.empty())    // means flag wasn't specified
1384         return true;
1385       return symbols_to_retain_.find(symbol_name) != symbols_to_retain_.end();
1386     }
1387
1388   // These are the best way to get access to the execstack state,
1389   // not execstack() and noexecstack() which are hard to use properly.
1390   bool
1391   is_execstack_set() const
1392   { return this->execstack_status_ != EXECSTACK_FROM_INPUT; }
1393
1394   bool
1395   is_stack_executable() const
1396   { return this->execstack_status_ == EXECSTACK_YES; }
1397
1398   bool
1399   icf_enabled() const
1400   { return this->icf_status_ != ICF_NONE; }
1401
1402   bool
1403   icf_safe_folding() const
1404   { return this->icf_status_ == ICF_SAFE; }
1405
1406   // The --demangle option takes an optional string, and there is also
1407   // a --no-demangle option.  This is the best way to decide whether
1408   // to demangle or not.
1409   bool
1410   do_demangle() const
1411   { return this->do_demangle_; }
1412
1413   // Returns TRUE if any plugin libraries have been loaded.
1414   bool
1415   has_plugins() const
1416   { return this->plugins_ != NULL; }
1417
1418   // Return a pointer to the plugin manager.
1419   Plugin_manager*
1420   plugins() const
1421   { return this->plugins_; }
1422
1423   // True iff SYMBOL was found in the file specified by dynamic-list.
1424   bool
1425   in_dynamic_list(const char* symbol) const
1426   { return this->dynamic_list_.version_script_info()->symbol_is_local(symbol); }
1427
1428   // Finalize the dynamic list.
1429   void
1430   finalize_dynamic_list()
1431   { this->dynamic_list_.version_script_info()->finalize(); }
1432
1433   // The mode selected by the --incremental options.
1434   enum Incremental_mode
1435   {
1436     // No incremental linking (--no-incremental).
1437     INCREMENTAL_OFF,
1438     // Incremental update only (--incremental-update).
1439     INCREMENTAL_UPDATE,
1440     // Force a full link, but prepare for subsequent incremental link
1441     // (--incremental-full).
1442     INCREMENTAL_FULL,
1443     // Incremental update if possible, fallback to full link  (--incremental).
1444     INCREMENTAL_AUTO
1445   };
1446
1447   // The incremental linking mode.
1448   Incremental_mode
1449   incremental_mode() const
1450   { return this->incremental_mode_; }
1451
1452   // The disposition given by the --incremental-changed,
1453   // --incremental-unchanged or --incremental-unknown option.  The
1454   // value may change as we proceed parsing the command line flags.
1455   Incremental_disposition
1456   incremental_disposition() const
1457   { return this->incremental_disposition_; }
1458
1459   // The disposition to use for startup files (those that precede the
1460   // first --incremental-changed, etc. option).
1461   Incremental_disposition
1462   incremental_startup_disposition() const
1463   { return this->incremental_startup_disposition_; }
1464
1465   // Return true if S is the name of a library excluded from automatic
1466   // symbol export.
1467   bool
1468   check_excluded_libs(const std::string &s) const;
1469
1470   // If an explicit start address was given for section SECNAME with
1471   // the --section-start option, return true and set *PADDR to the
1472   // address.  Otherwise return false.
1473   bool
1474   section_start(const char* secname, uint64_t* paddr) const;
1475
1476   // Return whether any --section-start option was used.
1477   bool
1478   any_section_start() const
1479   { return !this->section_starts_.empty(); }
1480
1481   enum Fix_v4bx
1482   {
1483     // Leave original instruction.
1484     FIX_V4BX_NONE,
1485     // Replace instruction.
1486     FIX_V4BX_REPLACE,
1487     // Generate an interworking veneer.
1488     FIX_V4BX_INTERWORKING
1489   };
1490
1491   Fix_v4bx
1492   fix_v4bx() const
1493   { return (this->fix_v4bx_); }
1494
1495   enum Endianness
1496   {
1497     ENDIANNESS_NOT_SET,
1498     ENDIANNESS_BIG,
1499     ENDIANNESS_LITTLE
1500   };
1501
1502   Endianness
1503   endianness() const
1504   { return this->endianness_; }
1505
1506  private:
1507   // Don't copy this structure.
1508   General_options(const General_options&);
1509   General_options& operator=(const General_options&);
1510
1511   // Whether to mark the stack as executable.
1512   enum Execstack
1513   {
1514     // Not set on command line.
1515     EXECSTACK_FROM_INPUT,
1516     // Mark the stack as executable (-z execstack).
1517     EXECSTACK_YES,
1518     // Mark the stack as not executable (-z noexecstack).
1519     EXECSTACK_NO
1520   };
1521
1522   enum Icf_status
1523   {
1524     // Do not fold any functions (Default or --icf=none).
1525     ICF_NONE,
1526     // All functions are candidates for folding. (--icf=all).
1527     ICF_ALL,    
1528     // Only ctors and dtors are candidates for folding. (--icf=safe).
1529     ICF_SAFE
1530   };
1531
1532   void
1533   set_icf_status(Icf_status value)
1534   { this->icf_status_ = value; }
1535
1536   void
1537   set_execstack_status(Execstack value)
1538   { this->execstack_status_ = value; }
1539
1540   void
1541   set_do_demangle(bool value)
1542   { this->do_demangle_ = value; }
1543
1544   void
1545   set_static(bool value)
1546   { static_ = value; }
1547
1548   // These are called by finalize() to set up the search-path correctly.
1549   void
1550   add_to_library_path_with_sysroot(const std::string& arg)
1551   { this->add_search_directory_to_library_path(Search_directory(arg, true)); }
1552
1553   // Apply any sysroot to the directory lists.
1554   void
1555   add_sysroot();
1556
1557   // Add a plugin and its arguments to the list of plugins.
1558   void
1559   add_plugin(const char* filename);
1560
1561   // Add a plugin option.
1562   void
1563   add_plugin_option(const char* opt);
1564
1565   // Whether we printed version information.
1566   bool printed_version_;
1567   // Whether to mark the stack as executable.
1568   Execstack execstack_status_;
1569   // Whether to do code folding.
1570   Icf_status icf_status_;
1571   // Whether to do a static link.
1572   bool static_;
1573   // Whether to do demangling.
1574   bool do_demangle_;
1575   // List of plugin libraries.
1576   Plugin_manager* plugins_;
1577   // The parsed output of --dynamic-list files.  For convenience in
1578   // script.cc, we store this as a Script_options object, even though
1579   // we only use a single Version_tree from it.
1580   Script_options dynamic_list_;
1581   // The incremental linking mode.
1582   Incremental_mode incremental_mode_;
1583   // The disposition given by the --incremental-changed,
1584   // --incremental-unchanged or --incremental-unknown option.  The
1585   // value may change as we proceed parsing the command line flags.
1586   Incremental_disposition incremental_disposition_;
1587   // The disposition to use for startup files (those marked
1588   // INCREMENTAL_STARTUP).
1589   Incremental_disposition incremental_startup_disposition_;
1590   // Whether we have seen one of the options that require incremental
1591   // build (--incremental-changed, --incremental-unchanged,
1592   // --incremental-unknown, or --incremental-startup-unchanged).
1593   bool implicit_incremental_;
1594   // Libraries excluded from automatic export, via --exclude-libs.
1595   Unordered_set<std::string> excluded_libs_;
1596   // List of symbol-names to keep, via -retain-symbol-info.
1597   Unordered_set<std::string> symbols_to_retain_;
1598   // Map from section name to address from --section-start.
1599   std::map<std::string, uint64_t> section_starts_;
1600   // Whether to process armv4 bx instruction relocation.
1601   Fix_v4bx fix_v4bx_;
1602   // Endianness.
1603   Endianness endianness_;
1604 };
1605
1606 // The position-dependent options.  We use this to store the state of
1607 // the commandline at a particular point in parsing for later
1608 // reference.  For instance, if we see "ld --whole-archive foo.a
1609 // --no-whole-archive," we want to store the whole-archive option with
1610 // foo.a, so when the time comes to parse foo.a we know we should do
1611 // it in whole-archive mode.  We could store all of General_options,
1612 // but that's big, so we just pick the subset of flags that actually
1613 // change in a position-dependent way.
1614
1615 #define DEFINE_posdep(varname__, type__)        \
1616  public:                                        \
1617   type__                                        \
1618   varname__() const                             \
1619   { return this->varname__##_; }                \
1620                                                 \
1621   void                                          \
1622   set_##varname__(type__ value)                 \
1623   { this->varname__##_ = value; }               \
1624  private:                                       \
1625   type__ varname__##_
1626
1627 class Position_dependent_options
1628 {
1629  public:
1630   Position_dependent_options(const General_options& options
1631                              = Position_dependent_options::default_options_)
1632   { copy_from_options(options); }
1633
1634   void copy_from_options(const General_options& options)
1635   {
1636     this->set_as_needed(options.as_needed());
1637     this->set_Bdynamic(options.Bdynamic());
1638     this->set_format_enum(options.format_enum());
1639     this->set_whole_archive(options.whole_archive());
1640     this->set_incremental_disposition(options.incremental_disposition());
1641   }
1642
1643   DEFINE_posdep(as_needed, bool);
1644   DEFINE_posdep(Bdynamic, bool);
1645   DEFINE_posdep(format_enum, General_options::Object_format);
1646   DEFINE_posdep(whole_archive, bool);
1647   DEFINE_posdep(incremental_disposition, Incremental_disposition);
1648
1649  private:
1650   // This is a General_options with everything set to its default
1651   // value.  A Position_dependent_options created with no argument
1652   // will take its values from here.
1653   static General_options default_options_;
1654 };
1655
1656
1657 // A single file or library argument from the command line.
1658
1659 class Input_file_argument
1660 {
1661  public:
1662   enum Input_file_type
1663   {
1664     // A regular file, name used as-is, not searched.
1665     INPUT_FILE_TYPE_FILE,
1666     // A library name.  When used, "lib" will be prepended and ".so" or
1667     // ".a" appended to make a filename, and that filename will be searched
1668     // for using the -L paths.
1669     INPUT_FILE_TYPE_LIBRARY,
1670     // A regular file, name used as-is, but searched using the -L paths.
1671     INPUT_FILE_TYPE_SEARCHED_FILE
1672   };
1673
1674   // name: file name or library name
1675   // type: the type of this input file.
1676   // extra_search_path: an extra directory to look for the file, prior
1677   //         to checking the normal library search path.  If this is "",
1678   //         then no extra directory is added.
1679   // just_symbols: whether this file only defines symbols.
1680   // options: The position dependent options at this point in the
1681   //         command line, such as --whole-archive.
1682   Input_file_argument()
1683     : name_(), type_(INPUT_FILE_TYPE_FILE), extra_search_path_(""),
1684       just_symbols_(false), options_(), arg_serial_(0)
1685   { }
1686
1687   Input_file_argument(const char* name, Input_file_type type,
1688                       const char* extra_search_path,
1689                       bool just_symbols,
1690                       const Position_dependent_options& options)
1691     : name_(name), type_(type), extra_search_path_(extra_search_path),
1692       just_symbols_(just_symbols), options_(options), arg_serial_(0)
1693   { }
1694
1695   // You can also pass in a General_options instance instead of a
1696   // Position_dependent_options.  In that case, we extract the
1697   // position-independent vars from the General_options and only store
1698   // those.
1699   Input_file_argument(const char* name, Input_file_type type,
1700                       const char* extra_search_path,
1701                       bool just_symbols,
1702                       const General_options& options)
1703     : name_(name), type_(type), extra_search_path_(extra_search_path),
1704       just_symbols_(just_symbols), options_(options), arg_serial_(0)
1705   { }
1706
1707   const char*
1708   name() const
1709   { return this->name_.c_str(); }
1710
1711   const Position_dependent_options&
1712   options() const
1713   { return this->options_; }
1714
1715   bool
1716   is_lib() const
1717   { return type_ == INPUT_FILE_TYPE_LIBRARY; }
1718
1719   bool
1720   is_searched_file() const
1721   { return type_ == INPUT_FILE_TYPE_SEARCHED_FILE; }
1722
1723   const char*
1724   extra_search_path() const
1725   {
1726     return (this->extra_search_path_.empty()
1727             ? NULL
1728             : this->extra_search_path_.c_str());
1729   }
1730
1731   // Return whether we should only read symbols from this file.
1732   bool
1733   just_symbols() const
1734   { return this->just_symbols_; }
1735
1736   // Return whether this file may require a search using the -L
1737   // options.
1738   bool
1739   may_need_search() const
1740   {
1741     return (this->is_lib()
1742             || this->is_searched_file()
1743             || !this->extra_search_path_.empty());
1744   }
1745
1746   // Set the serial number for this argument.
1747   void
1748   set_arg_serial(unsigned int arg_serial)
1749   { this->arg_serial_ = arg_serial; }
1750
1751   // Get the serial number.
1752   unsigned int
1753   arg_serial() const
1754   { return this->arg_serial_; }
1755
1756  private:
1757   // We use std::string, not const char*, here for convenience when
1758   // using script files, so that we do not have to preserve the string
1759   // in that case.
1760   std::string name_;
1761   Input_file_type type_;
1762   std::string extra_search_path_;
1763   bool just_symbols_;
1764   Position_dependent_options options_;
1765   // A unique index for this file argument in the argument list.
1766   unsigned int arg_serial_;
1767 };
1768
1769 // A file or library, or a group, from the command line.
1770
1771 class Input_argument
1772 {
1773  public:
1774   // Create a file or library argument.
1775   explicit Input_argument(Input_file_argument file)
1776     : is_file_(true), file_(file), group_(NULL), lib_(NULL), script_info_(NULL)
1777   { }
1778
1779   // Create a group argument.
1780   explicit Input_argument(Input_file_group* group)
1781     : is_file_(false), group_(group), lib_(NULL), script_info_(NULL)
1782   { }
1783
1784   // Create a lib argument.
1785   explicit Input_argument(Input_file_lib* lib)
1786     : is_file_(false), group_(NULL), lib_(lib), script_info_(NULL)
1787   { }
1788
1789   // Return whether this is a file.
1790   bool
1791   is_file() const
1792   { return this->is_file_; }
1793
1794   // Return whether this is a group.
1795   bool
1796   is_group() const
1797   { return !this->is_file_ && this->lib_ == NULL; }
1798
1799   // Return whether this is a lib.
1800   bool
1801   is_lib() const
1802   { return this->lib_ != NULL; }
1803
1804   // Return the information about the file.
1805   const Input_file_argument&
1806   file() const
1807   {
1808     gold_assert(this->is_file_);
1809     return this->file_;
1810   }
1811
1812   // Return the information about the group.
1813   const Input_file_group*
1814   group() const
1815   {
1816     gold_assert(!this->is_file_);
1817     return this->group_;
1818   }
1819
1820   Input_file_group*
1821   group()
1822   {
1823     gold_assert(!this->is_file_);
1824     return this->group_;
1825   }
1826
1827   // Return the information about the lib.
1828   const Input_file_lib*
1829   lib() const
1830   {
1831     gold_assert(!this->is_file_);
1832     gold_assert(this->lib_);
1833     return this->lib_;
1834   }
1835
1836   Input_file_lib*
1837   lib()
1838   {
1839     gold_assert(!this->is_file_);
1840     gold_assert(this->lib_);
1841     return this->lib_;
1842   }
1843
1844   // If a script generated this argument, store a pointer to the script info.
1845   // Currently used only for recording incremental link information.
1846   void
1847   set_script_info(Script_info* info)
1848   { this->script_info_ = info; }
1849
1850   Script_info*
1851   script_info() const
1852   { return this->script_info_; }
1853
1854  private:
1855   bool is_file_;
1856   Input_file_argument file_;
1857   Input_file_group* group_;
1858   Input_file_lib* lib_;
1859   Script_info* script_info_;
1860 };
1861
1862 typedef std::vector<Input_argument> Input_argument_list;
1863
1864 // A group from the command line.  This is a set of arguments within
1865 // --start-group ... --end-group.
1866
1867 class Input_file_group
1868 {
1869  public:
1870   typedef Input_argument_list::const_iterator const_iterator;
1871
1872   Input_file_group()
1873     : files_()
1874   { }
1875
1876   // Add a file to the end of the group.
1877   Input_argument&
1878   add_file(const Input_file_argument& arg)
1879   {
1880     this->files_.push_back(Input_argument(arg));
1881     return this->files_.back();
1882   }
1883
1884   // Iterators to iterate over the group contents.
1885
1886   const_iterator
1887   begin() const
1888   { return this->files_.begin(); }
1889
1890   const_iterator
1891   end() const
1892   { return this->files_.end(); }
1893
1894  private:
1895   Input_argument_list files_;
1896 };
1897
1898 // A lib from the command line.  This is a set of arguments within
1899 // --start-lib ... --end-lib.
1900
1901 class Input_file_lib
1902 {
1903  public:
1904   typedef Input_argument_list::const_iterator const_iterator;
1905
1906   Input_file_lib(const Position_dependent_options& options)
1907     : files_(), options_(options)
1908   { }
1909
1910   // Add a file to the end of the lib.
1911   Input_argument&
1912   add_file(const Input_file_argument& arg)
1913   {
1914     this->files_.push_back(Input_argument(arg));
1915     return this->files_.back();
1916   }
1917
1918   const Position_dependent_options&
1919   options() const
1920   { return this->options_; }
1921
1922   // Iterators to iterate over the lib contents.
1923
1924   const_iterator
1925   begin() const
1926   { return this->files_.begin(); }
1927
1928   const_iterator
1929   end() const
1930   { return this->files_.end(); }
1931
1932   size_t
1933   size() const
1934   { return this->files_.size(); }
1935
1936  private:
1937   Input_argument_list files_;
1938   Position_dependent_options options_;
1939 };
1940
1941 // A list of files from the command line or a script.
1942
1943 class Input_arguments
1944 {
1945  public:
1946   typedef Input_argument_list::const_iterator const_iterator;
1947
1948   Input_arguments()
1949     : input_argument_list_(), in_group_(false), in_lib_(false), file_count_(0)
1950   { }
1951
1952   // Add a file.
1953   Input_argument&
1954   add_file(Input_file_argument& arg);
1955
1956   // Start a group (the --start-group option).
1957   void
1958   start_group();
1959
1960   // End a group (the --end-group option).
1961   void
1962   end_group();
1963
1964   // Start a lib (the --start-lib option).
1965   void
1966   start_lib(const Position_dependent_options&);
1967
1968   // End a lib (the --end-lib option).
1969   void
1970   end_lib();
1971
1972   // Return whether we are currently in a group.
1973   bool
1974   in_group() const
1975   { return this->in_group_; }
1976
1977   // Return whether we are currently in a lib.
1978   bool
1979   in_lib() const
1980   { return this->in_lib_; }
1981
1982   // The number of entries in the list.
1983   int
1984   size() const
1985   { return this->input_argument_list_.size(); }
1986
1987   // Iterators to iterate over the list of input files.
1988
1989   const_iterator
1990   begin() const
1991   { return this->input_argument_list_.begin(); }
1992
1993   const_iterator
1994   end() const
1995   { return this->input_argument_list_.end(); }
1996
1997   // Return whether the list is empty.
1998   bool
1999   empty() const
2000   { return this->input_argument_list_.empty(); }
2001
2002   // Return the number of input files.  This may be larger than
2003   // input_argument_list_.size(), because of files that are part
2004   // of groups or libs.
2005   int
2006   number_of_input_files() const
2007   { return this->file_count_; }
2008
2009  private:
2010   Input_argument_list input_argument_list_;
2011   bool in_group_;
2012   bool in_lib_;
2013   unsigned int file_count_;
2014 };
2015
2016
2017 // All the information read from the command line.  These are held in
2018 // three separate structs: one to hold the options (--foo), one to
2019 // hold the filenames listed on the commandline, and one to hold
2020 // linker script information.  This third is not a subset of the other
2021 // two because linker scripts can be specified either as options (via
2022 // -T) or as a file.
2023
2024 class Command_line
2025 {
2026  public:
2027   typedef Input_arguments::const_iterator const_iterator;
2028
2029   Command_line();
2030
2031   // Process the command line options.  This will exit with an
2032   // appropriate error message if an unrecognized option is seen.
2033   void
2034   process(int argc, const char** argv);
2035
2036   // Process one command-line option.  This takes the index of argv to
2037   // process, and returns the index for the next option.  no_more_options
2038   // is set to true if argv[i] is "--".
2039   int
2040   process_one_option(int argc, const char** argv, int i,
2041                      bool* no_more_options);
2042
2043   // Get the general options.
2044   const General_options&
2045   options() const
2046   { return this->options_; }
2047
2048   // Get the position dependent options.
2049   const Position_dependent_options&
2050   position_dependent_options() const
2051   { return this->position_options_; }
2052
2053   // Get the linker-script options.
2054   Script_options&
2055   script_options()
2056   { return this->script_options_; }
2057
2058   // Finalize the version-script options and return them.
2059   const Version_script_info&
2060   version_script();
2061
2062   // Get the input files.
2063   Input_arguments&
2064   inputs()
2065   { return this->inputs_; }
2066
2067   // The number of input files.
2068   int
2069   number_of_input_files() const
2070   { return this->inputs_.number_of_input_files(); }
2071
2072   // Iterators to iterate over the list of input files.
2073
2074   const_iterator
2075   begin() const
2076   { return this->inputs_.begin(); }
2077
2078   const_iterator
2079   end() const
2080   { return this->inputs_.end(); }
2081
2082  private:
2083   Command_line(const Command_line&);
2084   Command_line& operator=(const Command_line&);
2085
2086   // This is a dummy class to provide a constructor that runs before
2087   // the constructor for the General_options.  The Pre_options constructor
2088   // is used as a hook to set the flag enabling the options to register
2089   // themselves.
2090   struct Pre_options {
2091     Pre_options();
2092   };
2093
2094   // This must come before options_!
2095   Pre_options pre_options_;
2096   General_options options_;
2097   Position_dependent_options position_options_;
2098   Script_options script_options_;
2099   Input_arguments inputs_;
2100 };
2101
2102 } // End namespace gold.
2103
2104 #endif // !defined(GOLD_OPTIONS_H)