2009-10-10 Chris Demetriou <cgd@google.com>
[platform/upstream/binutils.git] / gold / options.h
1 // options.h -- handle command line options for gold  -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009 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 <string>
43 #include <vector>
44
45 #include "elfcpp.h"
46 #include "script.h"
47
48 namespace gold
49 {
50
51 class Command_line;
52 class General_options;
53 class Search_directory;
54 class Input_file_group;
55 class Position_dependent_options;
56 class Target;
57 class Plugin_manager;
58
59 // Incremental build action for a specific file, as selected by the user.
60
61 enum Incremental_disposition
62 {
63   // Determine the status from the timestamp (default).
64   INCREMENTAL_CHECK,
65   // Assume the file changed from the previous build.
66   INCREMENTAL_CHANGED,
67   // Assume the file didn't change from the previous build.
68   INCREMENTAL_UNCHANGED
69 };
70
71 // The nested namespace is to contain all the global variables and
72 // structs that need to be defined in the .h file, but do not need to
73 // be used outside this class.
74 namespace options
75 {
76 typedef std::vector<Search_directory> Dir_list;
77 typedef Unordered_set<std::string> String_set;
78
79 // These routines convert from a string option to various types.
80 // Each gives a fatal error if it cannot parse the argument.
81
82 extern void
83 parse_bool(const char* option_name, const char* arg, bool* retval);
84
85 extern void
86 parse_int(const char* option_name, const char* arg, int* retval);
87
88 extern void
89 parse_uint(const char* option_name, const char* arg, int* retval);
90
91 extern void
92 parse_uint64(const char* option_name, const char* arg, uint64_t* retval);
93
94 extern void
95 parse_double(const char* option_name, const char* arg, double* retval);
96
97 extern void
98 parse_string(const char* option_name, const char* arg, const char** retval);
99
100 extern void
101 parse_optional_string(const char* option_name, const char* arg,
102                       const char** retval);
103
104 extern void
105 parse_dirlist(const char* option_name, const char* arg, Dir_list* retval);
106
107 extern void
108 parse_set(const char* option_name, const char* arg, String_set* retval);
109
110 extern void
111 parse_choices(const char* option_name, const char* arg, const char** retval,
112               const char* choices[], int num_choices);
113
114 struct Struct_var;
115
116 // Most options have both a shortname (one letter) and a longname.
117 // This enum controls how many dashes are expected for longname access
118 // -- shortnames always use one dash.  Most longnames will accept
119 // either one dash or two; the only difference between ONE_DASH and
120 // TWO_DASHES is how we print the option in --help.  However, some
121 // longnames require two dashes, and some require only one.  The
122 // special value DASH_Z means that the option is preceded by "-z".
123 enum Dashes
124 {
125   ONE_DASH, TWO_DASHES, EXACTLY_ONE_DASH, EXACTLY_TWO_DASHES, DASH_Z
126 };
127
128 // LONGNAME is the long-name of the option with dashes converted to
129 //    underscores, or else the short-name if the option has no long-name.
130 //    It is never the empty string.
131 // DASHES is an instance of the Dashes enum: ONE_DASH, TWO_DASHES, etc.
132 // SHORTNAME is the short-name of the option, as a char, or '\0' if the
133 //    option has no short-name.  If the option has no long-name, you
134 //    should specify the short-name in *both* VARNAME and here.
135 // DEFAULT_VALUE is the value of the option if not specified on the
136 //    commandline, as a string.
137 // HELPSTRING is the descriptive text used with the option via --help
138 // HELPARG is how you define the argument to the option.
139 //    --help output is "-shortname HELPARG, --longname HELPARG: HELPSTRING"
140 //    HELPARG should be NULL iff the option is a bool and takes no arg.
141 // OPTIONAL_ARG is true if this option takes an optional argument.  An
142 //    optional argument must be specifid as --OPTION=VALUE, not
143 //    --OPTION VALUE.
144 // READER provides parse_to_value, which is a function that will convert
145 //    a char* argument into the proper type and store it in some variable.
146 // A One_option struct initializes itself with the global list of options
147 // at constructor time, so be careful making one of these.
148 struct One_option
149 {
150   std::string longname;
151   Dashes dashes;
152   char shortname;
153   const char* default_value;
154   const char* helpstring;
155   const char* helparg;
156   bool optional_arg;
157   Struct_var* reader;
158
159   One_option(const char* ln, Dashes d, char sn, const char* dv,
160              const char* hs, const char* ha, bool oa, Struct_var* r)
161     : longname(ln), dashes(d), shortname(sn), default_value(dv ? dv : ""),
162       helpstring(hs), helparg(ha), optional_arg(oa), reader(r)
163   {
164     // In longname, we convert all underscores to dashes, since GNU
165     // style uses dashes in option names.  longname is likely to have
166     // underscores in it because it's also used to declare a C++
167     // function.
168     const char* pos = strchr(this->longname.c_str(), '_');
169     for (; pos; pos = strchr(pos, '_'))
170       this->longname[pos - this->longname.c_str()] = '-';
171
172     // We only register ourselves if our helpstring is not NULL.  This
173     // is to support the "no-VAR" boolean variables, which we
174     // conditionally turn on by defining "no-VAR" help text.
175     if (this->helpstring)
176       this->register_option();
177   }
178
179   // This option takes an argument iff helparg is not NULL.
180   bool
181   takes_argument() const
182   { return this->helparg != NULL; }
183
184   // Whether the argument is optional.
185   bool
186   takes_optional_argument() const
187   { return this->optional_arg; }
188
189   // Register this option with the global list of options.
190   void
191   register_option();
192
193   // Print this option to stdout (used with --help).
194   void
195   print() const;
196 };
197
198 // All options have a Struct_##varname that inherits from this and
199 // actually implements parse_to_value for that option.
200 struct Struct_var
201 {
202   // OPTION: the name of the option as specified on the commandline,
203   //    including leading dashes, and any text following the option:
204   //    "-O", "--defsym=mysym=0x1000", etc.
205   // ARG: the arg associated with this option, or NULL if the option
206   //    takes no argument: "2", "mysym=0x1000", etc.
207   // CMDLINE: the global Command_line object.  Used by DEFINE_special.
208   // OPTIONS: the global General_options object.  Used by DEFINE_special.
209   virtual void
210   parse_to_value(const char* option, const char* arg,
211                  Command_line* cmdline, General_options* options) = 0;
212   virtual
213   ~Struct_var()  // To make gcc happy.
214   { }
215 };
216
217 // This is for "special" options that aren't of any predefined type.
218 struct Struct_special : public Struct_var
219 {
220   // If you change this, change the parse-fn in DEFINE_special as well.
221   typedef void (General_options::*Parse_function)(const char*, const char*,
222                                                   Command_line*);
223   Struct_special(const char* varname, Dashes dashes, char shortname,
224                  Parse_function parse_function,
225                  const char* helpstring, const char* helparg)
226     : option(varname, dashes, shortname, "", helpstring, helparg, false, this),
227       parse(parse_function)
228   { }
229
230   void parse_to_value(const char* option, const char* arg,
231                       Command_line* cmdline, General_options* options)
232   { (options->*(this->parse))(option, arg, cmdline); }
233
234   One_option option;
235   Parse_function parse;
236 };
237
238 }  // End namespace options.
239
240
241 // These are helper macros use by DEFINE_uint64/etc below.
242 // This macro is used inside the General_options_ class, so defines
243 // var() and set_var() as General_options methods.  Arguments as are
244 // for the constructor for One_option.  param_type__ is the same as
245 // type__ for built-in types, and "const type__ &" otherwise.
246 #define DEFINE_var(varname__, dashes__, shortname__, default_value__,        \
247                    default_value_as_string__, helpstring__, helparg__,       \
248                    optional_arg__, type__, param_type__, parse_fn__)         \
249  public:                                                                     \
250   param_type__                                                               \
251   varname__() const                                                          \
252   { return this->varname__##_.value; }                                       \
253                                                                              \
254   bool                                                                       \
255   user_set_##varname__() const                                               \
256   { return this->varname__##_.user_set_via_option; }                         \
257                                                                              \
258   void                                                                       \
259   set_user_set_##varname__()                                                 \
260   { this->varname__##_.user_set_via_option = true; }                         \
261                                                                              \
262  private:                                                                    \
263   struct Struct_##varname__ : public options::Struct_var                     \
264   {                                                                          \
265     Struct_##varname__()                                                     \
266       : option(#varname__, dashes__, shortname__, default_value_as_string__, \
267                helpstring__, helparg__, optional_arg__, this),               \
268         user_set_via_option(false), value(default_value__)                   \
269     { }                                                                      \
270                                                                              \
271     void                                                                     \
272     parse_to_value(const char* option_name, const char* arg,                 \
273                    Command_line*, General_options*)                          \
274     {                                                                        \
275       parse_fn__(option_name, arg, &this->value);                            \
276       this->user_set_via_option = true;                                      \
277     }                                                                        \
278                                                                              \
279     options::One_option option;                                              \
280     bool user_set_via_option;                                                \
281     type__ value;                                                            \
282   };                                                                         \
283   Struct_##varname__ varname__##_;                                           \
284   void                                                                       \
285   set_##varname__(param_type__ value)                                        \
286   { this->varname__##_.value = value; }
287
288 // These macros allow for easy addition of a new commandline option.
289
290 // If no_helpstring__ is not NULL, then in addition to creating
291 // VARNAME, we also create an option called no-VARNAME (or, for a -z
292 // option, noVARNAME).
293 #define DEFINE_bool(varname__, dashes__, shortname__, default_value__,   \
294                     helpstring__, no_helpstring__)                       \
295   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
296              default_value__ ? "true" : "false", helpstring__, NULL,     \
297              false, bool, bool, options::parse_bool)                     \
298   struct Struct_no_##varname__ : public options::Struct_var              \
299   {                                                                      \
300     Struct_no_##varname__() : option((dashes__ == options::DASH_Z        \
301                                       ? "no" #varname__                  \
302                                       : "no-" #varname__),               \
303                                      dashes__, '\0',                     \
304                                      default_value__ ? "false" : "true", \
305                                      no_helpstring__, NULL, false, this) \
306     { }                                                                  \
307                                                                          \
308     void                                                                 \
309     parse_to_value(const char*, const char*,                             \
310                    Command_line*, General_options* options)              \
311     { options->set_##varname__(false); }                                 \
312                                                                          \
313     options::One_option option;                                          \
314   };                                                                     \
315   Struct_no_##varname__ no_##varname__##_initializer_
316
317 #define DEFINE_enable(varname__, dashes__, shortname__, default_value__, \
318                       helpstring__, no_helpstring__)                     \
319   DEFINE_var(enable_##varname__, dashes__, shortname__, default_value__, \
320              default_value__ ? "true" : "false", helpstring__, NULL,     \
321              false, bool, bool, options::parse_bool)                     \
322   struct Struct_disable_##varname__ : public options::Struct_var         \
323   {                                                                      \
324     Struct_disable_##varname__() : option("disable-" #varname__,         \
325                                      dashes__, '\0',                     \
326                                      default_value__ ? "false" : "true", \
327                                      no_helpstring__, NULL, false, this) \
328     { }                                                                  \
329                                                                          \
330     void                                                                 \
331     parse_to_value(const char*, const char*,                             \
332                    Command_line*, General_options* options)              \
333     { options->set_enable_##varname__(false); }                          \
334                                                                          \
335     options::One_option option;                                          \
336   };                                                                     \
337   Struct_disable_##varname__ disable_##varname__##_initializer_
338
339 #define DEFINE_int(varname__, dashes__, shortname__, default_value__,   \
340                    helpstring__, helparg__)                             \
341   DEFINE_var(varname__, dashes__, shortname__, default_value__,         \
342              #default_value__, helpstring__, helparg__, false,          \
343              int, int, options::parse_int)
344
345 #define DEFINE_uint(varname__, dashes__, shortname__, default_value__,  \
346                    helpstring__, helparg__)                             \
347   DEFINE_var(varname__, dashes__, shortname__, default_value__,         \
348              #default_value__, helpstring__, helparg__, false,          \
349              int, int, options::parse_uint)
350
351 #define DEFINE_uint64(varname__, dashes__, shortname__, default_value__, \
352                       helpstring__, helparg__)                           \
353   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
354              #default_value__, helpstring__, helparg__, false,           \
355              uint64_t, uint64_t, options::parse_uint64)
356
357 #define DEFINE_double(varname__, dashes__, shortname__, default_value__, \
358                       helpstring__, helparg__)                           \
359   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
360              #default_value__, helpstring__, helparg__, false,           \
361              double, double, options::parse_double)
362
363 #define DEFINE_string(varname__, dashes__, shortname__, default_value__, \
364                       helpstring__, helparg__)                           \
365   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
366              default_value__, helpstring__, helparg__, false,            \
367              const char*, const char*, options::parse_string)
368
369 // This is like DEFINE_string, but we convert each occurrence to a
370 // Search_directory and store it in a vector.  Thus we also have the
371 // add_to_VARNAME() method, to append to the vector.
372 #define DEFINE_dirlist(varname__, dashes__, shortname__,                  \
373                            helpstring__, helparg__)                       \
374   DEFINE_var(varname__, dashes__, shortname__, ,                          \
375              "", helpstring__, helparg__, false, options::Dir_list,       \
376              const options::Dir_list&, options::parse_dirlist)            \
377   void                                                                    \
378   add_to_##varname__(const char* new_value)                               \
379   { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
380   void                                                                    \
381   add_search_directory_to_##varname__(const Search_directory& dir)        \
382   { this->varname__##_.value.push_back(dir); }
383
384 // This is like DEFINE_string, but we store a set of strings.
385 #define DEFINE_set(varname__, dashes__, shortname__,                      \
386                    helpstring__, helparg__)                               \
387   DEFINE_var(varname__, dashes__, shortname__, ,                          \
388              "", helpstring__, helparg__, false, options::String_set,     \
389              const options::String_set&, options::parse_set)              \
390  public:                                                                  \
391   bool                                                                    \
392   any_##varname__() const                                                 \
393   { return !this->varname__##_.value.empty(); }                           \
394                                                                           \
395   bool                                                                    \
396   is_##varname__(const char* symbol) const                                \
397   {                                                                       \
398     return (!this->varname__##_.value.empty()                             \
399             && (this->varname__##_.value.find(std::string(symbol))        \
400                 != this->varname__##_.value.end()));                      \
401   }                                                                       \
402                                                                           \
403   options::String_set::const_iterator                                     \
404   varname__##_begin() const                                               \
405   { return this->varname__##_.value.begin(); }                            \
406                                                                           \
407   options::String_set::const_iterator                                     \
408   varname__##_end() const                                                 \
409   { return this->varname__##_.value.end(); }
410
411 // When you have a list of possible values (expressed as string)
412 // After helparg__ should come an initializer list, like
413 //   {"foo", "bar", "baz"}
414 #define DEFINE_enum(varname__, dashes__, shortname__, default_value__,   \
415                     helpstring__, helparg__, ...)                        \
416   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
417              default_value__, helpstring__, helparg__, false,            \
418              const char*, const char*, parse_choices_##varname__)        \
419  private:                                                                \
420   static void parse_choices_##varname__(const char* option_name,         \
421                                         const char* arg,                 \
422                                         const char** retval) {           \
423     const char* choices[] = __VA_ARGS__;                                 \
424     options::parse_choices(option_name, arg, retval,                     \
425                            choices, sizeof(choices) / sizeof(*choices)); \
426   }
427
428 // This is like DEFINE_bool, but VARNAME is the name of a different
429 // option.  This option becomes an alias for that one.  INVERT is true
430 // if this option is an inversion of the other one.
431 #define DEFINE_bool_alias(option__, varname__, dashes__, shortname__,   \
432                           helpstring__, no_helpstring__, invert__)      \
433  private:                                                               \
434   struct Struct_##option__ : public options::Struct_var                 \
435   {                                                                     \
436     Struct_##option__()                                                 \
437       : option(#option__, dashes__, shortname__, "", helpstring__,      \
438                NULL, false, this)                                       \
439     { }                                                                 \
440                                                                         \
441     void                                                                \
442     parse_to_value(const char*, const char*,                            \
443                    Command_line*, General_options* options)             \
444     {                                                                   \
445       options->set_##varname__(!invert__);                              \
446       options->set_user_set_##varname__();                              \
447     }                                                                   \
448                                                                         \
449     options::One_option option;                                         \
450   };                                                                    \
451   Struct_##option__ option__##_;                                        \
452                                                                         \
453   struct Struct_no_##option__ : public options::Struct_var              \
454   {                                                                     \
455     Struct_no_##option__()                                              \
456       : option((dashes__ == options::DASH_Z                             \
457                 ? "no" #option__                                        \
458                 : "no-" #option__),                                     \
459                dashes__, '\0', "", no_helpstring__,                     \
460                NULL, false, this)                                       \
461     { }                                                                 \
462                                                                         \
463     void                                                                \
464     parse_to_value(const char*, const char*,                            \
465                    Command_line*, General_options* options)             \
466     {                                                                   \
467       options->set_##varname__(invert__);                               \
468       options->set_user_set_##varname__();                              \
469     }                                                                   \
470                                                                         \
471     options::One_option option;                                         \
472   };                                                                    \
473   Struct_no_##option__ no_##option__##_initializer_
474
475 // This is used for non-standard flags.  It defines no functions; it
476 // just calls General_options::parse_VARNAME whenever the flag is
477 // seen.  We declare parse_VARNAME as a static member of
478 // General_options; you are responsible for defining it there.
479 // helparg__ should be NULL iff this special-option is a boolean.
480 #define DEFINE_special(varname__, dashes__, shortname__,                \
481                        helpstring__, helparg__)                         \
482  private:                                                               \
483   void parse_##varname__(const char* option, const char* arg,           \
484                          Command_line* inputs);                         \
485   struct Struct_##varname__ : public options::Struct_special            \
486   {                                                                     \
487     Struct_##varname__()                                                \
488       : options::Struct_special(#varname__, dashes__, shortname__,      \
489                                 &General_options::parse_##varname__,    \
490                                 helpstring__, helparg__)                \
491     { }                                                                 \
492   };                                                                    \
493   Struct_##varname__ varname__##_initializer_
494
495 // An option that takes an optional string argument.  If the option is
496 // used with no argument, the value will be the default, and
497 // user_set_via_option will be true.
498 #define DEFINE_optional_string(varname__, dashes__, shortname__,        \
499                                default_value__,                         \
500                                helpstring__, helparg__)                 \
501   DEFINE_var(varname__, dashes__, shortname__, default_value__,         \
502              default_value__, helpstring__, helparg__, true,            \
503              const char*, const char*, options::parse_optional_string)
504
505 // A directory to search.  For each directory we record whether it is
506 // in the sysroot.  We need to know this so that, if a linker script
507 // is found within the sysroot, we will apply the sysroot to any files
508 // named by that script.
509
510 class Search_directory
511 {
512  public:
513   // We need a default constructor because we put this in a
514   // std::vector.
515   Search_directory()
516     : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
517   { }
518
519   // This is the usual constructor.
520   Search_directory(const char* name, bool put_in_sysroot)
521     : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
522   {
523     if (this->name_.empty())
524       this->name_ = ".";
525   }
526
527   // This is called if we have a sysroot.  The sysroot is prefixed to
528   // any entries for which put_in_sysroot_ is true.  is_in_sysroot_ is
529   // set to true for any enries which are in the sysroot (this will
530   // naturally include any entries for which put_in_sysroot_ is true).
531   // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
532   // passing SYSROOT to lrealpath.
533   void
534   add_sysroot(const char* sysroot, const char* canonical_sysroot);
535
536   // Get the directory name.
537   const std::string&
538   name() const
539   { return this->name_; }
540
541   // Return whether this directory is in the sysroot.
542   bool
543   is_in_sysroot() const
544   { return this->is_in_sysroot_; }
545
546   // Return whether this is considered a system directory.
547   bool
548   is_system_directory() const
549   { return this->put_in_sysroot_ || this->is_in_sysroot_; }
550
551  private:
552   // The directory name.
553   std::string name_;
554   // True if the sysroot should be added as a prefix for this
555   // directory (if there is a sysroot).  This is true for system
556   // directories that we search by default.
557   bool put_in_sysroot_;
558   // True if this directory is in the sysroot (if there is a sysroot).
559   // This is true if there is a sysroot and either 1) put_in_sysroot_
560   // is true, or 2) the directory happens to be in the sysroot based
561   // on a pathname comparison.
562   bool is_in_sysroot_;
563 };
564
565 class General_options
566 {
567  private:
568   // NOTE: For every option that you add here, also consider if you
569   // should add it to Position_dependent_options.
570   DEFINE_special(help, options::TWO_DASHES, '\0',
571                  N_("Report usage information"), NULL);
572   DEFINE_special(version, options::TWO_DASHES, 'v',
573                  N_("Report version information"), NULL);
574   DEFINE_special(V, options::EXACTLY_ONE_DASH, '\0',
575                  N_("Report version and target information"), NULL);
576
577   // These options are sorted approximately so that for each letter in
578   // the alphabet, we show the option whose shortname is that letter
579   // (if any) and then every longname that starts with that letter (in
580   // alphabetical order).  For both, lowercase sorts before uppercase.
581   // The -z options come last.
582
583   DEFINE_bool(allow_shlib_undefined, options::TWO_DASHES, '\0', false,
584               N_("Allow unresolved references in shared libraries"),
585               N_("Do not allow unresolved references in shared libraries"));
586
587   DEFINE_bool(as_needed, options::TWO_DASHES, '\0', false,
588               N_("Only set DT_NEEDED for dynamic libs if used"),
589               N_("Always DT_NEEDED for dynamic libs"));
590
591   // This should really be an "enum", but it's too easy for folks to
592   // forget to update the list as they add new targets.  So we just
593   // accept any string.  We'll fail later (when the string is parsed),
594   // if the target isn't actually supported.
595   DEFINE_string(format, options::TWO_DASHES, 'b', "elf",
596                 N_("Set input format"), ("[elf,binary]"));
597
598   DEFINE_bool(Bdynamic, options::ONE_DASH, '\0', true,
599               N_("-l searches for shared libraries"), NULL);
600   DEFINE_bool_alias(Bstatic, Bdynamic, options::ONE_DASH, '\0',
601                     N_("-l does not search for shared libraries"), NULL,
602                     true);
603
604   DEFINE_bool(Bsymbolic, options::ONE_DASH, '\0', false,
605               N_("Bind defined symbols locally"), NULL);
606
607   DEFINE_bool(Bsymbolic_functions, options::ONE_DASH, '\0', false,
608               N_("Bind defined function symbols locally"), NULL);
609
610   DEFINE_optional_string(build_id, options::TWO_DASHES, '\0', "sha1",
611                          N_("Generate build ID note"),
612                          N_("[=STYLE]"));
613
614   DEFINE_bool(check_sections, options::TWO_DASHES, '\0', true,
615               N_("Check segment addresses for overlaps (default)"),
616               N_("Do not check segment addresses for overlaps"));
617
618 #ifdef HAVE_ZLIB_H
619   DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
620               N_("Compress .debug_* sections in the output file"),
621               ("[none,zlib]"),
622               {"none", "zlib"});
623 #else
624   DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
625               N_("Compress .debug_* sections in the output file"),
626               N_("[none]"),
627               {"none"});
628 #endif
629
630   DEFINE_bool(define_common, options::TWO_DASHES, 'd', false,
631               N_("Define common symbols"),
632               N_("Do not define common symbols"));
633   DEFINE_bool(dc, options::ONE_DASH, '\0', false,
634               N_("Alias for -d"), NULL);
635   DEFINE_bool(dp, options::ONE_DASH, '\0', false,
636               N_("Alias for -d"), NULL);
637
638   DEFINE_string(debug, options::TWO_DASHES, '\0', "",
639                 N_("Turn on debugging"),
640                 N_("[all,files,script,task][,...]"));
641
642   DEFINE_special(defsym, options::TWO_DASHES, '\0',
643                  N_("Define a symbol"), N_("SYMBOL=EXPRESSION"));
644
645   DEFINE_optional_string(demangle, options::TWO_DASHES, '\0', NULL,
646                          N_("Demangle C++ symbols in log messages"),
647                          N_("[=STYLE]"));
648
649   DEFINE_bool(no_demangle, options::TWO_DASHES, '\0', false,
650               N_("Do not demangle C++ symbols in log messages"),
651               NULL);
652
653   DEFINE_bool(detect_odr_violations, options::TWO_DASHES, '\0', false,
654               N_("Try to detect violations of the One Definition Rule"),
655               NULL);
656
657   DEFINE_bool(discard_locals, options::TWO_DASHES, 'X', false,
658               N_("Delete all temporary local symbols"), NULL);
659
660   DEFINE_bool(dynamic_list_data, options::TWO_DASHES, '\0', false,
661               N_("Add data symbols to dynamic symbols"), NULL);
662
663   DEFINE_bool(dynamic_list_cpp_new, options::TWO_DASHES, '\0', false,
664               N_("Add C++ operator new/delete to dynamic symbols"), NULL);
665
666   DEFINE_bool(dynamic_list_cpp_typeinfo, options::TWO_DASHES, '\0', false,
667               N_("Add C++ typeinfo to dynamic symbols"), NULL);
668
669   DEFINE_special(dynamic_list, options::TWO_DASHES, '\0',
670                  N_("Read a list of dynamic symbols"), N_("FILE"));
671
672   DEFINE_string(entry, options::TWO_DASHES, 'e', NULL,
673                 N_("Set program start address"), N_("ADDRESS"));
674
675   DEFINE_special(exclude_libs, options::TWO_DASHES, '\0',
676                  N_("Exclude libraries from automatic export"),
677                  N_(("lib,lib ...")));
678
679   DEFINE_bool(export_dynamic, options::TWO_DASHES, 'E', false,
680               N_("Export all dynamic symbols"),
681               N_("Do not export all dynamic symbols (default)"));
682
683   DEFINE_bool(eh_frame_hdr, options::TWO_DASHES, '\0', false,
684               N_("Create exception frame header"), NULL);
685
686   DEFINE_bool(fatal_warnings, options::TWO_DASHES, '\0', false,
687               N_("Treat warnings as errors"),
688               N_("Do not treat warnings as errors"));
689
690   DEFINE_string(soname, options::ONE_DASH, 'h', NULL,
691                 N_("Set shared library name"), N_("FILENAME"));
692
693   DEFINE_double(hash_bucket_empty_fraction, options::TWO_DASHES, '\0', 0.0,
694                 N_("Min fraction of empty buckets in dynamic hash"),
695                 N_("FRACTION"));
696
697   DEFINE_enum(hash_style, options::TWO_DASHES, '\0', "sysv",
698               N_("Dynamic hash style"), N_("[sysv,gnu,both]"),
699               {"sysv", "gnu", "both"});
700
701   DEFINE_string(dynamic_linker, options::TWO_DASHES, 'I', NULL,
702                 N_("Set dynamic linker path"), N_("PROGRAM"));
703
704   DEFINE_bool(incremental, options::TWO_DASHES, '\0', false,
705               N_("Work in progress; do not use"),
706               N_("Do a full build"));
707
708   DEFINE_special(incremental_changed, options::TWO_DASHES, '\0',
709                  N_("Assume files changed"), NULL);
710
711   DEFINE_special(incremental_unchanged, options::TWO_DASHES, '\0',
712                  N_("Assume files didn't change"), NULL);
713
714   DEFINE_special(incremental_unknown, options::TWO_DASHES, '\0',
715                  N_("Use timestamps to check files (default)"), NULL);
716
717   DEFINE_special(just_symbols, options::TWO_DASHES, '\0',
718                  N_("Read only symbol values from FILE"), N_("FILE"));
719
720   DEFINE_special(library, options::TWO_DASHES, 'l',
721                  N_("Search for library LIBNAME"), N_("LIBNAME"));
722
723   DEFINE_dirlist(library_path, options::TWO_DASHES, 'L',
724                  N_("Add directory to search path"), N_("DIR"));
725
726   DEFINE_string(m, options::EXACTLY_ONE_DASH, 'm', "",
727                 N_("Ignored for compatibility"), N_("EMULATION"));
728
729   DEFINE_bool(print_map, options::TWO_DASHES, 'M', false,
730               N_("Write map file on standard output"), NULL);
731   DEFINE_string(Map, options::ONE_DASH, '\0', NULL, N_("Write map file"),
732                 N_("MAPFILENAME"));
733
734   DEFINE_bool(nmagic, options::TWO_DASHES, 'n', false,
735               N_("Do not page align data"), NULL);
736   DEFINE_bool(omagic, options::EXACTLY_TWO_DASHES, 'N', false,
737               N_("Do not page align data, do not make text readonly"),
738               N_("Page align data, make text readonly"));
739
740   DEFINE_enable(new_dtags, options::EXACTLY_TWO_DASHES, '\0', false,
741                 N_("Enable use of DT_RUNPATH and DT_FLAGS"),
742                 N_("Disable use of DT_RUNPATH and DT_FLAGS"));
743
744   DEFINE_bool(noinhibit_exec, options::TWO_DASHES, '\0', false,
745               N_("Create an output file even if errors occur"), NULL);
746
747   DEFINE_bool_alias(no_undefined, defs, options::TWO_DASHES, '\0',
748                     N_("Report undefined symbols (even with --shared)"),
749                     NULL, false);
750
751   DEFINE_string(output, options::TWO_DASHES, 'o', "a.out",
752                 N_("Set output file name"), N_("FILE"));
753
754   DEFINE_uint(optimize, options::EXACTLY_ONE_DASH, 'O', 0,
755               N_("Optimize output file size"), N_("LEVEL"));
756
757   DEFINE_string(oformat, options::EXACTLY_TWO_DASHES, '\0', "elf",
758                 N_("Set output format"), N_("[binary]"));
759
760 #ifdef ENABLE_PLUGINS
761   DEFINE_special(plugin, options::TWO_DASHES, '\0',
762                  N_("Load a plugin library"), N_("PLUGIN"));
763   DEFINE_special(plugin_opt, options::TWO_DASHES, '\0',
764                  N_("Pass an option to the plugin"), N_("OPTION"));
765 #endif
766
767   DEFINE_bool(preread_archive_symbols, options::TWO_DASHES, '\0', false,
768               N_("Preread archive symbols when multi-threaded"), NULL);
769
770   DEFINE_string(print_symbol_counts, options::TWO_DASHES, '\0', NULL,
771                 N_("Print symbols defined and used for each input"),
772                 N_("FILENAME"));
773
774   DEFINE_bool(Qy, options::EXACTLY_ONE_DASH, '\0', false,
775               N_("Ignored for SVR4 compatibility"), NULL);
776
777   DEFINE_bool(emit_relocs, options::TWO_DASHES, 'q', false,
778               N_("Generate relocations in output"), NULL);
779
780   DEFINE_bool(relocatable, options::EXACTLY_ONE_DASH, 'r', false,
781               N_("Generate relocatable output"), NULL);
782
783   DEFINE_bool(relax, options::TWO_DASHES, '\0', false,
784               N_("Relax branches on certain targets"), NULL);
785
786   DEFINE_string(retain_symbols_file, options::EXACTLY_ONE_DASH, '\0', NULL,
787                 N_("keep only symbols listed in this file"), N_("[file]"));
788
789   // -R really means -rpath, but can mean --just-symbols for
790   // compatibility with GNU ld.  -rpath is always -rpath, so we list
791   // it separately.
792   DEFINE_special(R, options::EXACTLY_ONE_DASH, 'R',
793                  N_("Add DIR to runtime search path"), N_("DIR"));
794
795   DEFINE_dirlist(rpath, options::ONE_DASH, '\0',
796                  N_("Add DIR to runtime search path"), N_("DIR"));
797
798   DEFINE_dirlist(rpath_link, options::TWO_DASHES, '\0',
799                  N_("Add DIR to link time shared library search path"),
800                  N_("DIR"));
801
802   DEFINE_bool(strip_all, options::TWO_DASHES, 's', false,
803               N_("Strip all symbols"), NULL);
804   DEFINE_bool(strip_debug, options::TWO_DASHES, 'S', false,
805               N_("Strip debugging information"), NULL);
806   DEFINE_bool(strip_debug_non_line, options::TWO_DASHES, '\0', false,
807               N_("Emit only debug line number information"), NULL);
808   DEFINE_bool(strip_debug_gdb, options::TWO_DASHES, '\0', false,
809               N_("Strip debug symbols that are unused by gdb "
810                  "(at least versions <= 6.7)"), NULL);
811   DEFINE_bool(strip_lto_sections, options::TWO_DASHES, '\0', true,
812               N_("Strip LTO intermediate code sections"), NULL);
813
814   DEFINE_int(stub_group_size, options::TWO_DASHES , '\0', 1,
815              N_("(ARM only) The maximum distance from instructions in a group "
816                 "of sections to their stubs.  Negative values mean stubs "
817                 "are always after the group. 1 means using default size.\n"),
818              N_("SIZE"));
819
820   DEFINE_bool(no_keep_memory, options::TWO_DASHES, '\0', false,
821               N_("Use less memory and more disk I/O "
822                  "(included only for compatibility with GNU ld)"), NULL);
823
824   DEFINE_bool(shared, options::ONE_DASH, '\0', false,
825               N_("Generate shared library"), NULL);
826
827   DEFINE_bool(Bshareable, options::ONE_DASH, '\0', false,
828               N_("Generate shared library"), NULL);
829
830   DEFINE_uint(split_stack_adjust_size, options::TWO_DASHES, '\0', 0x4000,
831               N_("Stack size when -fsplit-stack function calls non-split"),
832               N_("SIZE"));
833
834   // This is not actually special in any way, but I need to give it
835   // a non-standard accessor-function name because 'static' is a keyword.
836   DEFINE_special(static, options::ONE_DASH, '\0',
837                  N_("Do not link against shared libraries"), NULL);
838
839   DEFINE_bool(icf, options::TWO_DASHES, '\0', false,
840               N_("Identical Code Folding (Fold identical functions)"),
841               N_("Don't fold identical functions (default)"));
842
843   DEFINE_uint(icf_iterations, options::TWO_DASHES , '\0', 0,
844               N_("Number of iterations of ICF (default 2)"), N_("COUNT"));
845
846   DEFINE_bool(print_icf_sections, options::TWO_DASHES, '\0', false,
847               N_("List folded identical sections on stderr"),
848               N_("Do not list folded identical sections"));
849
850   DEFINE_set(keep_unique, options::TWO_DASHES, '\0',
851              N_("Do not fold this symbol during ICF"), N_("SYMBOL"));
852
853   DEFINE_bool(gc_sections, options::TWO_DASHES, '\0', false,
854               N_("Remove unused sections"),
855               N_("Don't remove unused sections (default)"));
856
857   DEFINE_bool(print_gc_sections, options::TWO_DASHES, '\0', false,
858               N_("List removed unused sections on stderr"),
859               N_("Do not list removed unused sections"));
860
861   DEFINE_bool(stats, options::TWO_DASHES, '\0', false,
862               N_("Print resource usage statistics"), NULL);
863
864   DEFINE_string(sysroot, options::TWO_DASHES, '\0', "",
865                 N_("Set target system root directory"), N_("DIR"));
866
867   DEFINE_bool(trace, options::TWO_DASHES, 't', false,
868               N_("Print the name of each input file"), NULL);
869
870   DEFINE_special(script, options::TWO_DASHES, 'T',
871                  N_("Read linker script"), N_("FILE"));
872
873   DEFINE_bool(threads, options::TWO_DASHES, '\0', false,
874               N_("Run the linker multi-threaded"),
875               N_("Do not run the linker multi-threaded"));
876   DEFINE_uint(thread_count, options::TWO_DASHES, '\0', 0,
877               N_("Number of threads to use"), N_("COUNT"));
878   DEFINE_uint(thread_count_initial, options::TWO_DASHES, '\0', 0,
879               N_("Number of threads to use in initial pass"), N_("COUNT"));
880   DEFINE_uint(thread_count_middle, options::TWO_DASHES, '\0', 0,
881               N_("Number of threads to use in middle pass"), N_("COUNT"));
882   DEFINE_uint(thread_count_final, options::TWO_DASHES, '\0', 0,
883               N_("Number of threads to use in final pass"), N_("COUNT"));
884
885   DEFINE_uint64(Tbss, options::ONE_DASH, '\0', -1U,
886                 N_("Set the address of the bss segment"), N_("ADDRESS"));
887   DEFINE_uint64(Tdata, options::ONE_DASH, '\0', -1U,
888                 N_("Set the address of the data segment"), N_("ADDRESS"));
889   DEFINE_uint64(Ttext, options::ONE_DASH, '\0', -1U,
890                 N_("Set the address of the text segment"), N_("ADDRESS"));
891
892   DEFINE_set(undefined, options::TWO_DASHES, 'u',
893              N_("Create undefined reference to SYMBOL"), N_("SYMBOL"));
894
895   DEFINE_bool(verbose, options::TWO_DASHES, '\0', false,
896               N_("Synonym for --debug=files"), NULL);
897
898   DEFINE_special(version_script, options::TWO_DASHES, '\0',
899                  N_("Read version script"), N_("FILE"));
900
901   DEFINE_bool(warn_search_mismatch, options::TWO_DASHES, '\0', true,
902               N_("Warn when skipping an incompatible library"),
903               N_("Don't warn when skipping an incompatible library"));
904
905   DEFINE_bool(whole_archive, options::TWO_DASHES, '\0', false,
906               N_("Include all archive contents"),
907               N_("Include only needed archive contents"));
908
909   DEFINE_set(wrap, options::TWO_DASHES, '\0',
910              N_("Use wrapper functions for SYMBOL"), N_("SYMBOL"));
911
912   DEFINE_set(trace_symbol, options::TWO_DASHES, 'y',
913              N_("Trace references to symbol"), N_("SYMBOL"));
914
915   DEFINE_string(Y, options::EXACTLY_ONE_DASH, 'Y', "",
916                 N_("Default search path for Solaris compatibility"),
917                 N_("PATH"));
918
919   DEFINE_special(start_group, options::TWO_DASHES, '(',
920                  N_("Start a library search group"), NULL);
921   DEFINE_special(end_group, options::TWO_DASHES, ')',
922                  N_("End a library search group"), NULL);
923
924   // The -z options.
925
926   DEFINE_bool(combreloc, options::DASH_Z, '\0', true,
927               N_("Sort dynamic relocs"),
928               N_("Do not sort dynamic relocs"));
929   DEFINE_uint64(common_page_size, options::DASH_Z, '\0', 0,
930                 N_("Set common page size to SIZE"), N_("SIZE"));
931   DEFINE_bool(defs, options::DASH_Z, '\0', false,
932               N_("Report undefined symbols (even with --shared)"),
933               NULL);
934   DEFINE_bool(execstack, options::DASH_Z, '\0', false,
935               N_("Mark output as requiring executable stack"), NULL);
936   DEFINE_bool(initfirst, options::DASH_Z, '\0', false,
937               N_("Mark DSO to be initialized first at runtime"),
938               NULL);
939   DEFINE_bool(interpose, options::DASH_Z, '\0', false,
940               N_("Mark object to interpose all DSOs but executable"),
941               NULL);
942   DEFINE_bool(lazy, options::DASH_Z, '\0', false,
943               N_("Mark object for lazy runtime binding (default)"),
944               NULL);
945   DEFINE_bool(loadfltr, options::DASH_Z, '\0', false,
946               N_("Mark object requiring immediate process"),
947               NULL);
948   DEFINE_uint64(max_page_size, options::DASH_Z, '\0', 0,
949                 N_("Set maximum page size to SIZE"), N_("SIZE"));
950   DEFINE_bool(copyreloc, options::DASH_Z, '\0', true,
951               NULL,
952               N_("Do not create copy relocs"));
953   DEFINE_bool(nodefaultlib, options::DASH_Z, '\0', false,
954               N_("Mark object not to use default search paths"),
955               NULL);
956   DEFINE_bool(nodelete, options::DASH_Z, '\0', false,
957               N_("Mark DSO non-deletable at runtime"),
958               NULL);
959   DEFINE_bool(nodlopen, options::DASH_Z, '\0', false,
960               N_("Mark DSO not available to dlopen"),
961               NULL);
962   DEFINE_bool(nodump, options::DASH_Z, '\0', false,
963               N_("Mark DSO not available to dldump"),
964               NULL);
965   DEFINE_bool(noexecstack, options::DASH_Z, '\0', false,
966               N_("Mark output as not requiring executable stack"), NULL);
967   DEFINE_bool(now, options::DASH_Z, '\0', false,
968               N_("Mark object for immediate function binding"),
969               NULL);
970   DEFINE_bool(origin, options::DASH_Z, '\0', false,
971               N_("Mark DSO to indicate that needs immediate $ORIGIN "
972                  "processing at runtime"), NULL);
973   DEFINE_bool(relro, options::DASH_Z, '\0', false,
974               N_("Where possible mark variables read-only after relocation"),
975               N_("Don't mark variables read-only after relocation"));
976
977  public:
978   typedef options::Dir_list Dir_list;
979
980   General_options();
981
982   // Does post-processing on flags, making sure they all have
983   // non-conflicting values.  Also converts some flags from their
984   // "standard" types (string, etc), to another type (enum, DirList),
985   // which can be accessed via a separate method.  Dies if it notices
986   // any problems.
987   void finalize();
988
989   // True if we printed the version information.
990   bool
991   printed_version() const
992   { return this->printed_version_; }
993
994   // The macro defines output() (based on --output), but that's a
995   // generic name.  Provide this alternative name, which is clearer.
996   const char*
997   output_file_name() const
998   { return this->output(); }
999
1000   // This is not defined via a flag, but combines flags to say whether
1001   // the output is position-independent or not.
1002   bool
1003   output_is_position_independent() const
1004   { return this->shared(); }
1005
1006   // Return true if the output is something that can be exec()ed, such
1007   // as a static executable, or a position-dependent or
1008   // position-independent executable, but not a dynamic library or an
1009   // object file.
1010   bool
1011   output_is_executable() const
1012   { return !this->shared() || this->output_is_pie(); }
1013
1014   // Return true if the output is a position-independent executable.
1015   // This is currently not supported.
1016   bool
1017   output_is_pie() const
1018   { return false; }
1019
1020   // This would normally be static(), and defined automatically, but
1021   // since static is a keyword, we need to come up with our own name.
1022   bool
1023   is_static() const
1024   { return static_; }
1025
1026   // In addition to getting the input and output formats as a string
1027   // (via format() and oformat()), we also give access as an enum.
1028   enum Object_format
1029   {
1030     // Ordinary ELF.
1031     OBJECT_FORMAT_ELF,
1032     // Straight binary format.
1033     OBJECT_FORMAT_BINARY
1034   };
1035
1036   // Convert a string to an Object_format.  Gives an error if the
1037   // string is not recognized.
1038   static Object_format
1039   string_to_object_format(const char* arg);
1040
1041   // Note: these functions are not very fast.
1042   Object_format format_enum() const;
1043   Object_format oformat_enum() const;
1044
1045   // Return whether FILENAME is in a system directory.
1046   bool
1047   is_in_system_directory(const std::string& name) const;
1048
1049   // RETURN whether SYMBOL_NAME should be kept, according to symbols_to_retain_.
1050   bool
1051   should_retain_symbol(const char* symbol_name) const
1052     {
1053       if (symbols_to_retain_.empty())    // means flag wasn't specified
1054         return true;
1055       return symbols_to_retain_.find(symbol_name) != symbols_to_retain_.end();
1056     }
1057
1058   // These are the best way to get access to the execstack state,
1059   // not execstack() and noexecstack() which are hard to use properly.
1060   bool
1061   is_execstack_set() const
1062   { return this->execstack_status_ != EXECSTACK_FROM_INPUT; }
1063
1064   bool
1065   is_stack_executable() const
1066   { return this->execstack_status_ == EXECSTACK_YES; }
1067
1068   // The --demangle option takes an optional string, and there is also
1069   // a --no-demangle option.  This is the best way to decide whether
1070   // to demangle or not.
1071   bool
1072   do_demangle() const
1073   { return this->do_demangle_; }
1074
1075   // Returns TRUE if any plugin libraries have been loaded.
1076   bool
1077   has_plugins() const
1078   { return this->plugins_ != NULL; }
1079
1080   // Return a pointer to the plugin manager.
1081   Plugin_manager*
1082   plugins() const
1083   { return this->plugins_; }
1084
1085   // True iff SYMBOL was found in the file specified by dynamic-list.
1086   bool
1087   in_dynamic_list(const char* symbol) const
1088   { return this->dynamic_list_.version_script_info()->symbol_is_local(symbol); }
1089
1090   // The disposition given by the --incremental-changed,
1091   // --incremental-unchanged or --incremental-unknown option.  The
1092   // value may change as we proceed parsing the command line flags.
1093   Incremental_disposition
1094   incremental_disposition() const
1095   { return this->incremental_disposition_; }
1096
1097   // Return true if S is the name of a library excluded from automatic
1098   // symbol export.
1099   bool
1100   check_excluded_libs (const std::string &s) const;
1101
1102  private:
1103   // Don't copy this structure.
1104   General_options(const General_options&);
1105   General_options& operator=(const General_options&);
1106
1107   // Whether to mark the stack as executable.
1108   enum Execstack
1109   {
1110     // Not set on command line.
1111     EXECSTACK_FROM_INPUT,
1112     // Mark the stack as executable (-z execstack).
1113     EXECSTACK_YES,
1114     // Mark the stack as not executable (-z noexecstack).
1115     EXECSTACK_NO
1116   };
1117
1118   void
1119   set_execstack_status(Execstack value)
1120   { this->execstack_status_ = value; }
1121
1122   void
1123   set_do_demangle(bool value)
1124   { this->do_demangle_ = value; }
1125
1126   void
1127   set_static(bool value)
1128   { static_ = value; }
1129
1130   // These are called by finalize() to set up the search-path correctly.
1131   void
1132   add_to_library_path_with_sysroot(const char* arg)
1133   { this->add_search_directory_to_library_path(Search_directory(arg, true)); }
1134
1135   // Apply any sysroot to the directory lists.
1136   void
1137   add_sysroot();
1138
1139   // Add a plugin and its arguments to the list of plugins.
1140   void
1141   add_plugin(const char *filename);
1142
1143   // Add a plugin option.
1144   void
1145   add_plugin_option(const char* opt);
1146
1147   // Whether we printed version information.
1148   bool printed_version_;
1149   // Whether to mark the stack as executable.
1150   Execstack execstack_status_;
1151   // Whether to do a static link.
1152   bool static_;
1153   // Whether to do demangling.
1154   bool do_demangle_;
1155   // List of plugin libraries.
1156   Plugin_manager* plugins_;
1157   // The parsed output of --dynamic-list files.  For convenience in
1158   // script.cc, we store this as a Script_options object, even though
1159   // we only use a single Version_tree from it.
1160   Script_options dynamic_list_;
1161   // The disposition given by the --incremental-changed,
1162   // --incremental-unchanged or --incremental-unknown option.  The
1163   // value may change as we proceed parsing the command line flags.
1164   Incremental_disposition incremental_disposition_;
1165   // Whether we have seen one of the options that require incremental
1166   // build (--incremental-changed, --incremental-unchanged or
1167   // --incremental-unknown)
1168   bool implicit_incremental_;
1169   // Libraries excluded from automatic export, via --exclude-libs.
1170   Unordered_set<std::string> excluded_libs_;
1171   // List of symbol-names to keep, via -retain-symbol-info.
1172   Unordered_set<std::string> symbols_to_retain_;
1173 };
1174
1175 // The position-dependent options.  We use this to store the state of
1176 // the commandline at a particular point in parsing for later
1177 // reference.  For instance, if we see "ld --whole-archive foo.a
1178 // --no-whole-archive," we want to store the whole-archive option with
1179 // foo.a, so when the time comes to parse foo.a we know we should do
1180 // it in whole-archive mode.  We could store all of General_options,
1181 // but that's big, so we just pick the subset of flags that actually
1182 // change in a position-dependent way.
1183
1184 #define DEFINE_posdep(varname__, type__)        \
1185  public:                                        \
1186   type__                                        \
1187   varname__() const                             \
1188   { return this->varname__##_; }                \
1189                                                 \
1190   void                                          \
1191   set_##varname__(type__ value)                 \
1192   { this->varname__##_ = value; }               \
1193  private:                                       \
1194   type__ varname__##_
1195
1196 class Position_dependent_options
1197 {
1198  public:
1199   Position_dependent_options(const General_options& options
1200                              = Position_dependent_options::default_options_)
1201   { copy_from_options(options); }
1202
1203   void copy_from_options(const General_options& options)
1204   {
1205     this->set_as_needed(options.as_needed());
1206     this->set_Bdynamic(options.Bdynamic());
1207     this->set_format_enum(options.format_enum());
1208     this->set_whole_archive(options.whole_archive());
1209     this->set_incremental_disposition(options.incremental_disposition());
1210   }
1211
1212   DEFINE_posdep(as_needed, bool);
1213   DEFINE_posdep(Bdynamic, bool);
1214   DEFINE_posdep(format_enum, General_options::Object_format);
1215   DEFINE_posdep(whole_archive, bool);
1216   DEFINE_posdep(incremental_disposition, Incremental_disposition);
1217
1218  private:
1219   // This is a General_options with everything set to its default
1220   // value.  A Position_dependent_options created with no argument
1221   // will take its values from here.
1222   static General_options default_options_;
1223 };
1224
1225
1226 // A single file or library argument from the command line.
1227
1228 class Input_file_argument
1229 {
1230  public:
1231   enum Input_file_type
1232   {
1233     // A regular file, name used as-is, not searched.
1234     INPUT_FILE_TYPE_FILE,
1235     // A library name.  When used, "lib" will be prepended and ".so" or
1236     // ".a" appended to make a filename, and that filename will be searched
1237     // for using the -L paths.
1238     INPUT_FILE_TYPE_LIBRARY,
1239     // A regular file, name used as-is, but searched using the -L paths.
1240     INPUT_FILE_TYPE_SEARCHED_FILE
1241   };
1242
1243   // name: file name or library name
1244   // type: the type of this input file.
1245   // extra_search_path: an extra directory to look for the file, prior
1246   //         to checking the normal library search path.  If this is "",
1247   //         then no extra directory is added.
1248   // just_symbols: whether this file only defines symbols.
1249   // options: The position dependent options at this point in the
1250   //         command line, such as --whole-archive.
1251   Input_file_argument()
1252     : name_(), type_(INPUT_FILE_TYPE_FILE), extra_search_path_(""),
1253       just_symbols_(false), options_()
1254   { }
1255
1256   Input_file_argument(const char* name, Input_file_type type,
1257                       const char* extra_search_path,
1258                       bool just_symbols,
1259                       const Position_dependent_options& options)
1260     : name_(name), type_(type), extra_search_path_(extra_search_path),
1261       just_symbols_(just_symbols), options_(options)
1262   { }
1263
1264   // You can also pass in a General_options instance instead of a
1265   // Position_dependent_options.  In that case, we extract the
1266   // position-independent vars from the General_options and only store
1267   // those.
1268   Input_file_argument(const char* name, Input_file_type type,
1269                       const char* extra_search_path,
1270                       bool just_symbols,
1271                       const General_options& options)
1272     : name_(name), type_(type), extra_search_path_(extra_search_path),
1273       just_symbols_(just_symbols), options_(options)
1274   { }
1275
1276   const char*
1277   name() const
1278   { return this->name_.c_str(); }
1279
1280   const Position_dependent_options&
1281   options() const
1282   { return this->options_; }
1283
1284   bool
1285   is_lib() const
1286   { return type_ == INPUT_FILE_TYPE_LIBRARY; }
1287
1288   bool
1289   is_searched_file() const
1290   { return type_ == INPUT_FILE_TYPE_SEARCHED_FILE; }
1291
1292   const char*
1293   extra_search_path() const
1294   {
1295     return (this->extra_search_path_.empty()
1296             ? NULL
1297             : this->extra_search_path_.c_str());
1298   }
1299
1300   // Return whether we should only read symbols from this file.
1301   bool
1302   just_symbols() const
1303   { return this->just_symbols_; }
1304
1305   // Return whether this file may require a search using the -L
1306   // options.
1307   bool
1308   may_need_search() const
1309   {
1310     return (this->is_lib()
1311             || this->is_searched_file()
1312             || !this->extra_search_path_.empty());
1313   }
1314
1315  private:
1316   // We use std::string, not const char*, here for convenience when
1317   // using script files, so that we do not have to preserve the string
1318   // in that case.
1319   std::string name_;
1320   Input_file_type type_;
1321   std::string extra_search_path_;
1322   bool just_symbols_;
1323   Position_dependent_options options_;
1324 };
1325
1326 // A file or library, or a group, from the command line.
1327
1328 class Input_argument
1329 {
1330  public:
1331   // Create a file or library argument.
1332   explicit Input_argument(Input_file_argument file)
1333     : is_file_(true), file_(file), group_(NULL)
1334   { }
1335
1336   // Create a group argument.
1337   explicit Input_argument(Input_file_group* group)
1338     : is_file_(false), group_(group)
1339   { }
1340
1341   // Return whether this is a file.
1342   bool
1343   is_file() const
1344   { return this->is_file_; }
1345
1346   // Return whether this is a group.
1347   bool
1348   is_group() const
1349   { return !this->is_file_; }
1350
1351   // Return the information about the file.
1352   const Input_file_argument&
1353   file() const
1354   {
1355     gold_assert(this->is_file_);
1356     return this->file_;
1357   }
1358
1359   // Return the information about the group.
1360   const Input_file_group*
1361   group() const
1362   {
1363     gold_assert(!this->is_file_);
1364     return this->group_;
1365   }
1366
1367   Input_file_group*
1368   group()
1369   {
1370     gold_assert(!this->is_file_);
1371     return this->group_;
1372   }
1373
1374  private:
1375   bool is_file_;
1376   Input_file_argument file_;
1377   Input_file_group* group_;
1378 };
1379
1380 typedef std::vector<Input_argument> Input_argument_list;
1381
1382 // A group from the command line.  This is a set of arguments within
1383 // --start-group ... --end-group.
1384
1385 class Input_file_group
1386 {
1387  public:
1388   typedef Input_argument_list::const_iterator const_iterator;
1389
1390   Input_file_group()
1391     : files_()
1392   { }
1393
1394   // Add a file to the end of the group.
1395   void
1396   add_file(const Input_file_argument& arg)
1397   { this->files_.push_back(Input_argument(arg)); }
1398
1399   // Iterators to iterate over the group contents.
1400
1401   const_iterator
1402   begin() const
1403   { return this->files_.begin(); }
1404
1405   const_iterator
1406   end() const
1407   { return this->files_.end(); }
1408
1409  private:
1410   Input_argument_list files_;
1411 };
1412
1413 // A list of files from the command line or a script.
1414
1415 class Input_arguments
1416 {
1417  public:
1418   typedef Input_argument_list::const_iterator const_iterator;
1419
1420   Input_arguments()
1421     : input_argument_list_(), in_group_(false)
1422   { }
1423
1424   // Add a file.
1425   void
1426   add_file(const Input_file_argument& arg);
1427
1428   // Start a group (the --start-group option).
1429   void
1430   start_group();
1431
1432   // End a group (the --end-group option).
1433   void
1434   end_group();
1435
1436   // Return whether we are currently in a group.
1437   bool
1438   in_group() const
1439   { return this->in_group_; }
1440
1441   // The number of entries in the list.
1442   int
1443   size() const
1444   { return this->input_argument_list_.size(); }
1445
1446   // Iterators to iterate over the list of input files.
1447
1448   const_iterator
1449   begin() const
1450   { return this->input_argument_list_.begin(); }
1451
1452   const_iterator
1453   end() const
1454   { return this->input_argument_list_.end(); }
1455
1456   // Return whether the list is empty.
1457   bool
1458   empty() const
1459   { return this->input_argument_list_.empty(); }
1460
1461  private:
1462   Input_argument_list input_argument_list_;
1463   bool in_group_;
1464 };
1465
1466
1467 // All the information read from the command line.  These are held in
1468 // three separate structs: one to hold the options (--foo), one to
1469 // hold the filenames listed on the commandline, and one to hold
1470 // linker script information.  This third is not a subset of the other
1471 // two because linker scripts can be specified either as options (via
1472 // -T) or as a file.
1473
1474 class Command_line
1475 {
1476  public:
1477   typedef Input_arguments::const_iterator const_iterator;
1478
1479   Command_line();
1480
1481   // Process the command line options.  This will exit with an
1482   // appropriate error message if an unrecognized option is seen.
1483   void
1484   process(int argc, const char** argv);
1485
1486   // Process one command-line option.  This takes the index of argv to
1487   // process, and returns the index for the next option.  no_more_options
1488   // is set to true if argv[i] is "--".
1489   int
1490   process_one_option(int argc, const char** argv, int i,
1491                      bool* no_more_options);
1492
1493   // Get the general options.
1494   const General_options&
1495   options() const
1496   { return this->options_; }
1497
1498   // Get the position dependent options.
1499   const Position_dependent_options&
1500   position_dependent_options() const
1501   { return this->position_options_; }
1502
1503   // Get the linker-script options.
1504   Script_options&
1505   script_options()
1506   { return this->script_options_; }
1507
1508   // Get the version-script options: a convenience routine.
1509   const Version_script_info&
1510   version_script() const
1511   { return *this->script_options_.version_script_info(); }
1512
1513   // Get the input files.
1514   Input_arguments&
1515   inputs()
1516   { return this->inputs_; }
1517
1518   // The number of input files.
1519   int
1520   number_of_input_files() const
1521   { return this->inputs_.size(); }
1522
1523   // Iterators to iterate over the list of input files.
1524
1525   const_iterator
1526   begin() const
1527   { return this->inputs_.begin(); }
1528
1529   const_iterator
1530   end() const
1531   { return this->inputs_.end(); }
1532
1533  private:
1534   Command_line(const Command_line&);
1535   Command_line& operator=(const Command_line&);
1536
1537   // This is a dummy class to provide a constructor that runs before
1538   // the constructor for the General_options.  The Pre_options constructor
1539   // is used as a hook to set the flag enabling the options to register
1540   // themselves.
1541   struct Pre_options {
1542     Pre_options();
1543   };
1544
1545   // This must come before options_!
1546   Pre_options pre_options_;
1547   General_options options_;
1548   Position_dependent_options position_options_;
1549   Script_options script_options_;
1550   Input_arguments inputs_;
1551 };
1552
1553 } // End namespace gold.
1554
1555 #endif // !defined(GOLD_OPTIONS_H)