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