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