]> Git Repo - binutils.git/blob - gold/options.h
* gold.h: Include <cstddef> and <sys/types.h>
[binutils.git] / gold / options.h
1 // options.h -- handle command line options for gold  -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <[email protected]>.
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
58 // The nested namespace is to contain all the global variables and
59 // structs that need to be defined in the .h file, but do not need to
60 // be used outside this class.
61 namespace options
62 {
63 typedef std::vector<Search_directory> Dir_list;
64
65 // These routines convert from a string option to various types.
66 // Each gives a fatal error if it cannot parse the argument.
67
68 extern void
69 parse_bool(const char* option_name, const char* arg, bool* retval);
70
71 extern void
72 parse_uint(const char* option_name, const char* arg, int* retval);
73
74 extern void
75 parse_uint64(const char* option_name, const char* arg, uint64_t* retval);
76
77 extern void
78 parse_double(const char* option_name, const char* arg, double* retval);
79
80 extern void
81 parse_string(const char* option_name, const char* arg, const char** retval);
82
83 extern void
84 parse_dirlist(const char* option_name, const char* arg, Dir_list* retval);
85
86 extern void
87 parse_choices(const char* option_name, const char* arg, const char** retval,
88               const char* choices[], int num_choices);
89
90 struct Struct_var;
91
92 // Most options have both a shortname (one letter) and a longname.
93 // This enum controls how many dashes are expected for longname access
94 // -- shortnames always use one dash.  Most longnames will accept
95 // either one dash or two; the only difference between ONE_DASH and
96 // TWO_DASHES is how we print the option in --help.  However, some
97 // longnames require two dashes, and some require only one.  The
98 // special value DASH_Z means that the option is preceded by "-z".
99 enum Dashes
100 {
101   ONE_DASH, TWO_DASHES, EXACTLY_ONE_DASH, EXACTLY_TWO_DASHES, DASH_Z
102 };
103
104 // LONGNAME is the long-name of the option with dashes converted to
105 //    underscores, or else the short-name if the option has no long-name.
106 //    It is never the empty string.
107 // DASHES is an instance of the Dashes enum: ONE_DASH, TWO_DASHES, etc.
108 // SHORTNAME is the short-name of the option, as a char, or '\0' if the
109 //    option has no short-name.  If the option has no long-name, you
110 //    should specify the short-name in *both* VARNAME and here.
111 // DEFAULT_VALUE is the value of the option if not specified on the
112 //    commandline, as a string.
113 // HELPSTRING is the descriptive text used with the option via --help
114 // HELPARG is how you define the argument to the option.
115 //    --help output is "-shortname HELPARG, --longname HELPARG: HELPSTRING"
116 //    HELPARG should be NULL iff the option is a bool and takes no arg.
117 // READER provides parse_to_value, which is a function that will convert
118 //    a char* argument into the proper type and store it in some variable.
119 // A One_option struct initializes itself with the global list of options
120 // at constructor time, so be careful making one of these.
121 struct One_option
122 {
123   std::string longname;
124   Dashes dashes;
125   char shortname;
126   const char* default_value;
127   const char* helpstring;
128   const char* helparg;
129   Struct_var* reader;
130
131   One_option(const char* ln, Dashes d, char sn, const char* dv,
132              const char* hs, const char* ha, Struct_var* r)
133     : longname(ln), dashes(d), shortname(sn), default_value(dv ? dv : ""),
134       helpstring(hs), helparg(ha), reader(r)
135   {
136     // In longname, we convert all underscores to dashes, since GNU
137     // style uses dashes in option names.  longname is likely to have
138     // underscores in it because it's also used to declare a C++
139     // function.
140     const char* pos = strchr(this->longname.c_str(), '_');
141     for (; pos; pos = strchr(pos, '_'))
142       this->longname[pos - this->longname.c_str()] = '-';
143
144     // We only register ourselves if our helpstring is not NULL.  This
145     // is to support the "no-VAR" boolean variables, which we
146     // conditionally turn on by defining "no-VAR" help text.
147     if (this->helpstring)
148       this->register_option();
149   }
150
151   // This option takes an argument iff helparg is not NULL.
152   bool
153   takes_argument() const
154   { return this->helparg != NULL; }
155
156   // Register this option with the global list of options.
157   void
158   register_option();
159
160   // Print this option to stdout (used with --help).
161   void
162   print() const;
163 };
164
165 // All options have a Struct_##varname that inherits from this and
166 // actually implements parse_to_value for that option.
167 struct Struct_var
168 {
169   // OPTION: the name of the option as specified on the commandline,
170   //    including leading dashes, and any text following the option:
171   //    "-O", "--defsym=mysym=0x1000", etc.
172   // ARG: the arg associated with this option, or NULL if the option
173   //    takes no argument: "2", "mysym=0x1000", etc.
174   // CMDLINE: the global Command_line object.  Used by DEFINE_special.
175   // OPTIONS: the global General_options object.  Used by DEFINE_special.
176   virtual void
177   parse_to_value(const char* option, const char* arg,
178                  Command_line* cmdline, General_options* options) = 0;
179   virtual
180   ~Struct_var()  // To make gcc happy.
181   { }
182 };
183
184 // This is for "special" options that aren't of any predefined type.
185 struct Struct_special : public Struct_var
186 {
187   // If you change this, change the parse-fn in DEFINE_special as well.
188   typedef void (General_options::*Parse_function)(const char*, const char*,
189                                                   Command_line*);
190   Struct_special(const char* varname, Dashes dashes, char shortname,
191                  Parse_function parse_function,
192                  const char* helpstring, const char* helparg)
193     : option(varname, dashes, shortname, "", helpstring, helparg, this),
194       parse(parse_function)
195   { }
196
197   void parse_to_value(const char* option, const char* arg,
198                       Command_line* cmdline, General_options* options)
199   { (options->*(this->parse))(option, arg, cmdline); }
200
201   One_option option;
202   Parse_function parse;
203 };
204
205 }  // End namespace options.
206
207
208 // These are helper macros use by DEFINE_uint64/etc below.
209 // This macro is used inside the General_options_ class, so defines
210 // var() and set_var() as General_options methods.  Arguments as are
211 // for the constructor for One_option.  param_type__ is the same as
212 // type__ for built-in types, and "const type__ &" otherwise.
213 #define DEFINE_var(varname__, dashes__, shortname__, default_value__,        \
214                    default_value_as_string__, helpstring__, helparg__,       \
215                    type__, param_type__, parse_fn__)                         \
216  public:                                                                     \
217   param_type__                                                               \
218   varname__() const                                                          \
219   { return this->varname__##_.value; }                                       \
220                                                                              \
221   bool                                                                       \
222   user_set_##varname__() const                                               \
223   { return this->varname__##_.user_set_via_option; }                         \
224                                                                              \
225  private:                                                                    \
226   struct Struct_##varname__ : public options::Struct_var                     \
227   {                                                                          \
228     Struct_##varname__()                                                     \
229       : option(#varname__, dashes__, shortname__, default_value_as_string__, \
230                helpstring__, helparg__, this),                               \
231         user_set_via_option(false), value(default_value__)                   \
232     { }                                                                      \
233                                                                              \
234     void                                                                     \
235     parse_to_value(const char* option_name, const char* arg,                 \
236                    Command_line*, General_options*)                          \
237     {                                                                        \
238       parse_fn__(option_name, arg, &this->value);                            \
239       this->user_set_via_option = true;                                      \
240     }                                                                        \
241                                                                              \
242     options::One_option option;                                              \
243     bool user_set_via_option;                                                \
244     type__ value;                                                            \
245   };                                                                         \
246   Struct_##varname__ varname__##_;                                           \
247   void                                                                       \
248   set_##varname__(param_type__ value)                                        \
249   { this->varname__##_.value = value; }
250
251 // These macros allow for easy addition of a new commandline option.
252
253 // If no_helpstring__ is not NULL, then in addition to creating
254 // VARNAME, we also create an option called no-VARNAME.
255 #define DEFINE_bool(varname__, dashes__, shortname__, default_value__,   \
256                     helpstring__, no_helpstring__)                       \
257   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
258              default_value__ ? "true" : "false", helpstring__, NULL,     \
259              bool, bool, options::parse_bool)                            \
260   struct Struct_no_##varname__ : public options::Struct_var              \
261   {                                                                      \
262     Struct_no_##varname__() : option("no-" #varname__, dashes__, '\0',   \
263                                      default_value__ ? "false" : "true", \
264                                      no_helpstring__, NULL, this)        \
265     { }                                                                  \
266                                                                          \
267     void                                                                 \
268     parse_to_value(const char*, const char*,                             \
269                    Command_line*, General_options* options)              \
270     { options->set_##varname__(false); }                                 \
271                                                                          \
272     options::One_option option;                                          \
273   };                                                                     \
274   Struct_no_##varname__ no_##varname__##_initializer_
275
276 #define DEFINE_uint(varname__, dashes__, shortname__, default_value__,  \
277                    helpstring__, helparg__)                             \
278   DEFINE_var(varname__, dashes__, shortname__, default_value__,         \
279              #default_value__, helpstring__, helparg__,                 \
280              int, int, options::parse_uint)
281
282 #define DEFINE_uint64(varname__, dashes__, shortname__, default_value__, \
283                       helpstring__, helparg__)                           \
284   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
285              #default_value__, helpstring__, helparg__,                  \
286              uint64_t, uint64_t, options::parse_uint64)
287
288 #define DEFINE_double(varname__, dashes__, shortname__, default_value__, \
289                       helpstring__, helparg__)                           \
290   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
291              #default_value__, helpstring__, helparg__,                  \
292              double, double, options::parse_double)
293
294 #define DEFINE_string(varname__, dashes__, shortname__, default_value__, \
295                       helpstring__, helparg__)                           \
296   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
297              default_value__, helpstring__, helparg__,                   \
298              const char*, const char*, options::parse_string)
299
300 // This is like DEFINE_string, but we convert each occurrence to a
301 // Search_directory and store it in a vector.  Thus we also have the
302 // add_to_VARNAME() method, to append to the vector.
303 #define DEFINE_dirlist(varname__, dashes__, shortname__,                  \
304                            helpstring__, helparg__)                       \
305   DEFINE_var(varname__, dashes__, shortname__, ,                          \
306              "", helpstring__, helparg__, options::Dir_list,              \
307              const options::Dir_list&, options::parse_dirlist)            \
308   void                                                                    \
309   add_to_##varname__(const char* new_value)                               \
310   { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
311   void                                                                    \
312   add_search_directory_to_##varname__(const Search_directory& dir)        \
313   { this->varname__##_.value.push_back(dir); }
314
315 // When you have a list of possible values (expressed as string)
316 // After helparg__ should come an initializer list, like
317 //   {"foo", "bar", "baz"}
318 #define DEFINE_enum(varname__, dashes__, shortname__, default_value__,   \
319                     helpstring__, helparg__, ...)                        \
320   DEFINE_var(varname__, dashes__, shortname__, default_value__,          \
321              default_value__, helpstring__, helparg__,                   \
322              const char*, const char*, parse_choices_##varname__)        \
323  private:                                                                \
324   static void parse_choices_##varname__(const char* option_name,         \
325                                         const char* arg,                 \
326                                         const char** retval) {           \
327     const char* choices[] = __VA_ARGS__;                                 \
328     options::parse_choices(option_name, arg, retval,                     \
329                            choices, sizeof(choices) / sizeof(*choices)); \
330   }
331
332 // This is used for non-standard flags.  It defines no functions; it
333 // just calls General_options::parse_VARNAME whenever the flag is
334 // seen.  We declare parse_VARNAME as a static member of
335 // General_options; you are responsible for defining it there.
336 // helparg__ should be NULL iff this special-option is a boolean.
337 #define DEFINE_special(varname__, dashes__, shortname__,                \
338                        helpstring__, helparg__)                         \
339  private:                                                               \
340   void parse_##varname__(const char* option, const char* arg,           \
341                          Command_line* inputs);                         \
342   struct Struct_##varname__ : public options::Struct_special            \
343   {                                                                     \
344     Struct_##varname__()                                                \
345       : options::Struct_special(#varname__, dashes__, shortname__,      \
346                                 &General_options::parse_##varname__,    \
347                                 helpstring__, helparg__)                \
348     { }                                                                 \
349   };                                                                    \
350   Struct_##varname__ varname__##_initializer_
351
352
353 // A directory to search.  For each directory we record whether it is
354 // in the sysroot.  We need to know this so that, if a linker script
355 // is found within the sysroot, we will apply the sysroot to any files
356 // named by that script.
357
358 class Search_directory
359 {
360  public:
361   // We need a default constructor because we put this in a
362   // std::vector.
363   Search_directory()
364     : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
365   { }
366
367   // This is the usual constructor.
368   Search_directory(const char* name, bool put_in_sysroot)
369     : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
370   {
371     if (this->name_.empty())
372       this->name_ = ".";
373   }
374
375   // This is called if we have a sysroot.  The sysroot is prefixed to
376   // any entries for which put_in_sysroot_ is true.  is_in_sysroot_ is
377   // set to true for any enries which are in the sysroot (this will
378   // naturally include any entries for which put_in_sysroot_ is true).
379   // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
380   // passing SYSROOT to lrealpath.
381   void
382   add_sysroot(const char* sysroot, const char* canonical_sysroot);
383
384   // Get the directory name.
385   const std::string&
386   name() const
387   { return this->name_; }
388
389   // Return whether this directory is in the sysroot.
390   bool
391   is_in_sysroot() const
392   { return this->is_in_sysroot_; }
393
394  private:
395   std::string name_;
396   bool put_in_sysroot_;
397   bool is_in_sysroot_;
398 };
399
400 class General_options
401 {
402  private:
403   // NOTE: For every option that you add here, also consider if you
404   // should add it to Position_dependent_options.
405   DEFINE_special(help, options::TWO_DASHES, '\0',
406                  N_("Report usage information"), NULL);
407   DEFINE_special(version, options::TWO_DASHES, 'v',
408                  N_("Report version information"), NULL);
409
410   // These options are sorted approximately so that for each letter in
411   // the alphabet, we show the option whose shortname is that letter
412   // (if any) and then every longname that starts with that letter (in
413   // alphabetical order).  For both, lowercase sorts before uppercase.
414   // The -z options come last.
415
416   DEFINE_bool(allow_shlib_undefined, options::TWO_DASHES, '\0', false,
417               N_("Allow unresolved references in shared libraries"),
418               N_("Do not allow unresolved references in shared libraries"));
419
420   DEFINE_bool(as_needed, options::TWO_DASHES, '\0', false,
421               N_("Only set DT_NEEDED for dynamic libs if used"),
422               N_("Always DT_NEEDED for dynamic libs"));
423
424   // This should really be an "enum", but it's too easy for folks to
425   // forget to update the list as they add new targets.  So we just
426   // accept any string.  We'll fail later (when the string is parsed),
427   // if the target isn't actually supported.
428   DEFINE_string(format, options::TWO_DASHES, 'b', "elf",
429                 N_("Set input format"), ("[elf,binary]"));
430
431   DEFINE_bool(Bdynamic, options::ONE_DASH, '\0', true,
432               N_("-l searches for shared libraries"), NULL);
433   // Bstatic affects the same variable as Bdynamic, so we have to use
434   // the "special" macro to make that happen.
435   DEFINE_special(Bstatic, options::ONE_DASH, '\0',
436                  N_("-l does not search for shared libraries"), NULL);
437
438   DEFINE_bool(Bsymbolic, options::ONE_DASH, '\0', false,
439               N_("Bind defined symbols locally"), NULL);
440
441 #ifdef HAVE_ZLIB_H
442   DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
443               N_("Compress .debug_* sections in the output file"),
444               ("[none,zlib]"),
445               {"none", "zlib"});
446 #else
447   DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
448               N_("Compress .debug_* sections in the output file"),
449               N_("[none]"),
450               {"none"});
451 #endif
452
453   DEFINE_bool(define_common, options::TWO_DASHES, 'd', false,
454               N_("Define common symbols"),
455               N_("Do not define common symbols"));
456   DEFINE_bool(dc, options::ONE_DASH, '\0', false,
457               N_("Alias for -d"), NULL);
458   DEFINE_bool(dp, options::ONE_DASH, '\0', false,
459               N_("Alias for -d"), NULL);
460
461   DEFINE_string(debug, options::TWO_DASHES, '\0', "",
462                 N_("Turn on debugging"),
463                 N_("[all,files,script,task][,...]"));
464
465   DEFINE_special(defsym, options::TWO_DASHES, '\0',
466                  N_("Define a symbol"), N_("SYMBOL=EXPRESSION"));
467
468   DEFINE_bool(demangle, options::TWO_DASHES, '\0',
469               getenv("COLLECT_NO_DEMANGLE") == NULL,
470               N_("Demangle C++ symbols in log messages"),
471               N_("Do not demangle C++ symbols in log messages"));
472
473   DEFINE_bool(detect_odr_violations, options::TWO_DASHES, '\0', false,
474               N_("Try to detect violations of the One Definition Rule"),
475               NULL);
476
477   DEFINE_string(entry, options::TWO_DASHES, 'e', NULL,
478                 N_("Set program start address"), N_("ADDRESS"));
479
480   DEFINE_bool(export_dynamic, options::TWO_DASHES, 'E', false,
481               N_("Export all dynamic symbols"), NULL);
482
483   DEFINE_bool(eh_frame_hdr, options::TWO_DASHES, '\0', false,
484               N_("Create exception frame header"), NULL);
485
486   DEFINE_string(soname, options::ONE_DASH, 'h', NULL,
487                 N_("Set shared library name"), N_("FILENAME"));
488
489   DEFINE_double(hash_bucket_empty_fraction, options::TWO_DASHES, '\0', 0.0,
490                 N_("Min fraction of empty buckets in dynamic hash"),
491                 N_("FRACTION"));
492
493   DEFINE_enum(hash_style, options::TWO_DASHES, '\0', "sysv",
494               N_("Dynamic hash style"), N_("[sysv,gnu,both]"),
495               {"sysv", "gnu", "both"});
496
497   DEFINE_string(dynamic_linker, options::TWO_DASHES, 'I', NULL,
498                 N_("Set dynamic linker path"), N_("PROGRAM"));
499
500   DEFINE_special(just_symbols, options::TWO_DASHES, '\0',
501                  N_("Read only symbol values from FILE"), N_("FILE"));
502
503   DEFINE_special(library, options::TWO_DASHES, 'l',
504                  N_("Search for library LIBNAME"), N_("LIBNAME"));
505
506   DEFINE_dirlist(library_path, options::TWO_DASHES, 'L',
507                  N_("Add directory to search path"), N_("DIR"));
508
509   DEFINE_string(m, options::EXACTLY_ONE_DASH, 'm', "",
510                 N_("Ignored for compatibility"), N_("EMULATION"));
511
512   DEFINE_string(output, options::TWO_DASHES, 'o', "a.out",
513                 N_("Set output file name"), N_("FILE"));
514
515   DEFINE_uint(optimize, options::EXACTLY_ONE_DASH, 'O', 0,
516               N_("Optimize output file size"), N_("LEVEL"));
517
518   DEFINE_string(oformat, options::EXACTLY_TWO_DASHES, '\0', "elf",
519                 N_("Set output format"), N_("[binary]"));
520
521   DEFINE_bool(emit_relocs, options::TWO_DASHES, 'q', false,
522               N_("Generate relocations in output"), NULL);
523
524   DEFINE_bool(relocatable, options::EXACTLY_ONE_DASH, 'r', false,
525               N_("Generate relocatable output"), NULL);
526
527   // -R really means -rpath, but can mean --just-symbols for
528   // compatibility with GNU ld.  -rpath is always -rpath, so we list
529   // it separately.
530   DEFINE_special(R, options::EXACTLY_ONE_DASH, 'R',
531                  N_("Add DIR to runtime search path"), N_("DIR"));
532
533   DEFINE_dirlist(rpath, options::ONE_DASH, '\0',
534                  N_("Add DIR to runtime search path"), N_("DIR"));
535
536   DEFINE_dirlist(rpath_link, options::TWO_DASHES, '\0',
537                  N_("Add DIR to link time shared library search path"),
538                  N_("DIR"));
539
540   DEFINE_bool(strip_all, options::TWO_DASHES, 's', false,
541               N_("Strip all symbols"), NULL);
542   DEFINE_bool(strip_debug, options::TWO_DASHES, 'S', false,
543               N_("Strip debugging information"), NULL);
544   DEFINE_bool(strip_debug_gdb, options::TWO_DASHES, '\0', false,
545               N_("Strip debug symbols that are unused by gdb "
546                  "(at least versions <= 6.7)"), NULL);
547
548   DEFINE_bool(shared, options::ONE_DASH, '\0', false,
549               N_("Generate shared library"), NULL);
550
551   // This is not actually special in any way, but I need to give it
552   // a non-standard accessor-function name because 'static' is a keyword.
553   DEFINE_special(static, options::ONE_DASH, '\0',
554                  N_("Do not link against shared libraries"), NULL);
555
556   DEFINE_bool(stats, options::TWO_DASHES, '\0', false,
557               N_("Print resource usage statistics"), NULL);
558
559   DEFINE_string(sysroot, options::TWO_DASHES, '\0', "",
560                 N_("Set target system root directory"), N_("DIR"));
561
562   DEFINE_special(script, options::TWO_DASHES, 'T',
563                  N_("Read linker script"), N_("FILE"));
564
565   DEFINE_bool(threads, options::TWO_DASHES, '\0', false,
566               N_("Run the linker multi-threaded"),
567               N_("Do not run the linker multi-threaded"));
568   DEFINE_uint(thread_count, options::TWO_DASHES, '\0', 0,
569               N_("Number of threads to use"), N_("COUNT"));
570   DEFINE_uint(thread_count_initial, options::TWO_DASHES, '\0', 0,
571               N_("Number of threads to use in initial pass"), N_("COUNT"));
572   DEFINE_uint(thread_count_middle, options::TWO_DASHES, '\0', 0,
573               N_("Number of threads to use in middle pass"), N_("COUNT"));
574   DEFINE_uint(thread_count_final, options::TWO_DASHES, '\0', 0,
575               N_("Number of threads to use in final pass"), N_("COUNT"));
576
577   DEFINE_uint64(Tbss, options::ONE_DASH, '\0', -1U,
578                 N_("Set the address of the bss segment"), N_("ADDRESS"));
579   DEFINE_uint64(Tdata, options::ONE_DASH, '\0', -1U,
580                 N_("Set the address of the data segment"), N_("ADDRESS"));
581   DEFINE_uint64(Ttext, options::ONE_DASH, '\0', -1U,
582                 N_("Set the address of the text segment"), N_("ADDRESS"));
583
584   DEFINE_bool(verbose, options::TWO_DASHES, '\0', false,
585               N_("Synonym for --debug=files"), NULL);
586
587   DEFINE_special(version_script, options::TWO_DASHES, '\0',
588                  N_("Read version script"), N_("FILE"));
589
590   DEFINE_bool(whole_archive, options::TWO_DASHES, '\0', false,
591               N_("Include all archive contents"),
592               N_("Include only needed archive contents"));
593
594   DEFINE_special(start_group, options::TWO_DASHES, '(',
595                  N_("Start a library search group"), NULL);
596   DEFINE_special(end_group, options::TWO_DASHES, ')',
597                  N_("End a library search group"), NULL);
598
599   // The -z options.
600
601   // Both execstack and noexecstack differ from the default execstack_
602   // value, so we need to use different variables for them.
603   DEFINE_uint64(common_page_size, options::DASH_Z, '\0', 0,
604                 N_("Set common page size to SIZE"), N_("SIZE"));
605   DEFINE_bool(defs, options::DASH_Z, '\0', false,
606               N_("Report undefined symbols (even with --shared)"),
607               NULL);
608   DEFINE_bool(execstack, options::DASH_Z, '\0', false,
609               N_("Mark output as requiring executable stack"), NULL);
610   DEFINE_uint64(max_page_size, options::DASH_Z, '\0', 0,
611                 N_("Set maximum page size to SIZE"), N_("SIZE"));
612   DEFINE_bool(noexecstack, options::DASH_Z, '\0', false,
613               N_("Mark output as not requiring executable stack"), NULL);
614
615  public:
616   typedef options::Dir_list Dir_list;
617
618   General_options();
619
620   // Does post-processing on flags, making sure they all have
621   // non-conflicting values.  Also converts some flags from their
622   // "standard" types (string, etc), to another type (enum, DirList),
623   // which can be accessed via a separate method.  Dies if it notices
624   // any problems.
625   void finalize();
626
627   // The macro defines output() (based on --output), but that's a
628   // generic name.  Provide this alternative name, which is clearer.
629   const char*
630   output_file_name() const
631   { return this->output(); }
632
633   // This is not defined via a flag, but combines flags to say whether
634   // the output is position-independent or not.
635   bool
636   output_is_position_independent() const
637   { return this->shared(); }
638
639   // This would normally be static(), and defined automatically, but
640   // since static is a keyword, we need to come up with our own name.
641   bool
642   is_static() const
643   { return static_; }
644
645   // In addition to getting the input and output formats as a string
646   // (via format() and oformat()), we also give access as an enum.
647   enum Object_format
648   {
649     // Ordinary ELF.
650     OBJECT_FORMAT_ELF,
651     // Straight binary format.
652     OBJECT_FORMAT_BINARY
653   };
654
655   // Note: these functions are not very fast.
656   Object_format format_enum() const;
657   Object_format oformat_enum() const;
658
659   // These are the best way to get access to the execstack state,
660   // not execstack() and noexecstack() which are hard to use properly.
661   bool
662   is_execstack_set() const
663   { return this->execstack_status_ != EXECSTACK_FROM_INPUT; }
664
665   bool
666   is_stack_executable() const
667   { return this->execstack_status_ == EXECSTACK_YES; }
668
669  private:
670   // Don't copy this structure.
671   General_options(const General_options&);
672   General_options& operator=(const General_options&);
673
674   // Whether to mark the stack as executable.
675   enum Execstack
676   {
677     // Not set on command line.
678     EXECSTACK_FROM_INPUT,
679     // Mark the stack as executable (-z execstack).
680     EXECSTACK_YES,
681     // Mark the stack as not executable (-z noexecstack).
682     EXECSTACK_NO
683   };
684
685   Execstack execstack_status_;
686   void
687   set_execstack_status(Execstack value)
688   { execstack_status_ = value; }
689
690   bool static_;
691   void
692   set_static(bool value)
693   { static_ = value; }
694
695   // These are called by finalize() to set up the search-path correctly.
696   void
697   add_to_library_path_with_sysroot(const char* arg)
698   { this->add_search_directory_to_library_path(Search_directory(arg, true)); }
699
700   // Apply any sysroot to the directory lists.
701   void
702   add_sysroot();
703 };
704
705 // The position-dependent options.  We use this to store the state of
706 // the commandline at a particular point in parsing for later
707 // reference.  For instance, if we see "ld --whole-archive foo.a
708 // --no-whole-archive," we want to store the whole-archive option with
709 // foo.a, so when the time comes to parse foo.a we know we should do
710 // it in whole-archive mode.  We could store all of General_options,
711 // but that's big, so we just pick the subset of flags that actually
712 // change in a position-dependent way.
713
714 #define DEFINE_posdep(varname__, type__)        \
715  public:                                        \
716   type__                                        \
717   varname__() const                             \
718   { return this->varname__##_; }                \
719                                                 \
720   void                                          \
721   set_##varname__(type__ value)                 \
722   { this->varname__##_ = value; }               \
723  private:                                       \
724   type__ varname__##_
725
726 class Position_dependent_options
727 {
728  public:
729   Position_dependent_options(const General_options& options
730                              = Position_dependent_options::default_options_)
731   { copy_from_options(options); }
732
733   void copy_from_options(const General_options& options)
734   {
735     this->set_as_needed(options.as_needed());
736     this->set_Bdynamic(options.Bdynamic());
737     this->set_format_enum(options.format_enum());
738     this->set_whole_archive(options.whole_archive());
739   }
740
741   DEFINE_posdep(as_needed, bool);
742   DEFINE_posdep(Bdynamic, bool);
743   DEFINE_posdep(format_enum, General_options::Object_format);
744   DEFINE_posdep(whole_archive, bool);
745
746  private:
747   // This is a General_options with everything set to its default
748   // value.  A Position_dependent_options created with no argument
749   // will take its values from here.
750   static General_options default_options_;
751 };
752
753
754 // A single file or library argument from the command line.
755
756 class Input_file_argument
757 {
758  public:
759   // name: file name or library name
760   // is_lib: true if name is a library name: that is, emits the leading
761   //         "lib" and trailing ".so"/".a" from the name
762   // extra_search_path: an extra directory to look for the file, prior
763   //         to checking the normal library search path.  If this is "",
764   //         then no extra directory is added.
765   // just_symbols: whether this file only defines symbols.
766   // options: The position dependent options at this point in the
767   //         command line, such as --whole-archive.
768   Input_file_argument()
769     : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
770       options_()
771   { }
772
773   Input_file_argument(const char* name, bool is_lib,
774                       const char* extra_search_path,
775                       bool just_symbols,
776                       const Position_dependent_options& options)
777     : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
778       just_symbols_(just_symbols), options_(options)
779   { }
780
781   // You can also pass in a General_options instance instead of a
782   // Position_dependent_options.  In that case, we extract the
783   // position-independent vars from the General_options and only store
784   // those.
785   Input_file_argument(const char* name, bool is_lib,
786                       const char* extra_search_path,
787                       bool just_symbols,
788                       const General_options& options)
789     : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
790       just_symbols_(just_symbols), options_(options)
791   { }
792
793   const char*
794   name() const
795   { return this->name_.c_str(); }
796
797   const Position_dependent_options&
798   options() const
799   { return this->options_; }
800
801   bool
802   is_lib() const
803   { return this->is_lib_; }
804
805   const char*
806   extra_search_path() const
807   {
808     return (this->extra_search_path_.empty()
809             ? NULL
810             : this->extra_search_path_.c_str());
811   }
812
813   // Return whether we should only read symbols from this file.
814   bool
815   just_symbols() const
816   { return this->just_symbols_; }
817
818   // Return whether this file may require a search using the -L
819   // options.
820   bool
821   may_need_search() const
822   { return this->is_lib_ || !this->extra_search_path_.empty(); }
823
824  private:
825   // We use std::string, not const char*, here for convenience when
826   // using script files, so that we do not have to preserve the string
827   // in that case.
828   std::string name_;
829   bool is_lib_;
830   std::string extra_search_path_;
831   bool just_symbols_;
832   Position_dependent_options options_;
833 };
834
835 // A file or library, or a group, from the command line.
836
837 class Input_argument
838 {
839  public:
840   // Create a file or library argument.
841   explicit Input_argument(Input_file_argument file)
842     : is_file_(true), file_(file), group_(NULL)
843   { }
844
845   // Create a group argument.
846   explicit Input_argument(Input_file_group* group)
847     : is_file_(false), group_(group)
848   { }
849
850   // Return whether this is a file.
851   bool
852   is_file() const
853   { return this->is_file_; }
854
855   // Return whether this is a group.
856   bool
857   is_group() const
858   { return !this->is_file_; }
859
860   // Return the information about the file.
861   const Input_file_argument&
862   file() const
863   {
864     gold_assert(this->is_file_);
865     return this->file_;
866   }
867
868   // Return the information about the group.
869   const Input_file_group*
870   group() const
871   {
872     gold_assert(!this->is_file_);
873     return this->group_;
874   }
875
876   Input_file_group*
877   group()
878   {
879     gold_assert(!this->is_file_);
880     return this->group_;
881   }
882
883  private:
884   bool is_file_;
885   Input_file_argument file_;
886   Input_file_group* group_;
887 };
888
889 // A group from the command line.  This is a set of arguments within
890 // --start-group ... --end-group.
891
892 class Input_file_group
893 {
894  public:
895   typedef std::vector<Input_argument> Files;
896   typedef Files::const_iterator const_iterator;
897
898   Input_file_group()
899     : files_()
900   { }
901
902   // Add a file to the end of the group.
903   void
904   add_file(const Input_file_argument& arg)
905   { this->files_.push_back(Input_argument(arg)); }
906
907   // Iterators to iterate over the group contents.
908
909   const_iterator
910   begin() const
911   { return this->files_.begin(); }
912
913   const_iterator
914   end() const
915   { return this->files_.end(); }
916
917  private:
918   Files files_;
919 };
920
921 // A list of files from the command line or a script.
922
923 class Input_arguments
924 {
925  public:
926   typedef std::vector<Input_argument> Input_argument_list;
927   typedef Input_argument_list::const_iterator const_iterator;
928
929   Input_arguments()
930     : input_argument_list_(), in_group_(false)
931   { }
932
933   // Add a file.
934   void
935   add_file(const Input_file_argument& arg);
936
937   // Start a group (the --start-group option).
938   void
939   start_group();
940
941   // End a group (the --end-group option).
942   void
943   end_group();
944
945   // Return whether we are currently in a group.
946   bool
947   in_group() const
948   { return this->in_group_; }
949
950   // The number of entries in the list.
951   int
952   size() const
953   { return this->input_argument_list_.size(); }
954
955   // Iterators to iterate over the list of input files.
956
957   const_iterator
958   begin() const
959   { return this->input_argument_list_.begin(); }
960
961   const_iterator
962   end() const
963   { return this->input_argument_list_.end(); }
964
965   // Return whether the list is empty.
966   bool
967   empty() const
968   { return this->input_argument_list_.empty(); }
969
970  private:
971   Input_argument_list input_argument_list_;
972   bool in_group_;
973 };
974
975
976 // All the information read from the command line.  These are held in
977 // three separate structs: one to hold the options (--foo), one to
978 // hold the filenames listed on the commandline, and one to hold
979 // linker script information.  This third is not a subset of the other
980 // two because linker scripts can be specified either as options (via
981 // -T) or as a file.
982
983 class Command_line
984 {
985  public:
986   typedef Input_arguments::const_iterator const_iterator;
987
988   Command_line();
989
990   // Process the command line options.  This will exit with an
991   // appropriate error message if an unrecognized option is seen.
992   void
993   process(int argc, const char** argv);
994
995   // Process one command-line option.  This takes the index of argv to
996   // process, and returns the index for the next option.  no_more_options
997   // is set to true if argv[i] is "--".
998   int
999   process_one_option(int argc, const char** argv, int i,
1000                      bool* no_more_options);
1001
1002   // Get the general options.
1003   const General_options&
1004   options() const
1005   { return this->options_; }
1006
1007   // Get the position dependent options.
1008   const Position_dependent_options&
1009   position_dependent_options() const
1010   { return this->position_options_; }
1011
1012   // Get the linker-script options.
1013   Script_options&
1014   script_options()
1015   { return this->script_options_; }
1016
1017   // Get the version-script options: a convenience routine.
1018   const Version_script_info&
1019   version_script() const
1020   { return *this->script_options_.version_script_info(); }
1021
1022   // Get the input files.
1023   Input_arguments&
1024   inputs()
1025   { return this->inputs_; }
1026
1027   // The number of input files.
1028   int
1029   number_of_input_files() const
1030   { return this->inputs_.size(); }
1031
1032   // Iterators to iterate over the list of input files.
1033
1034   const_iterator
1035   begin() const
1036   { return this->inputs_.begin(); }
1037
1038   const_iterator
1039   end() const
1040   { return this->inputs_.end(); }
1041
1042  private:
1043   Command_line(const Command_line&);
1044   Command_line& operator=(const Command_line&);
1045
1046   General_options options_;
1047   Position_dependent_options position_options_;
1048   Script_options script_options_;
1049   Input_arguments inputs_;
1050 };
1051
1052 } // End namespace gold.
1053
1054 #endif // !defined(GOLD_OPTIONS_H)
This page took 0.083627 seconds and 4 git commands to generate.