OSDN Git Service

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