bfd/
[binutils.git] / gold / options.h
blob419e70bb4e1100cc743867c62373f0bf7e8af56b
1 // options.h -- handle command line options for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
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.
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).
36 #ifndef GOLD_OPTIONS_H
37 #define GOLD_OPTIONS_H
39 #include <cstdlib>
40 #include <cstring>
41 #include <list>
42 #include <string>
43 #include <vector>
45 #include "elfcpp.h"
46 #include "script.h"
48 namespace gold
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;
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
63 typedef std::vector<Search_directory> Dir_list;
64 typedef Unordered_set<std::string> String_set;
66 // These routines convert from a string option to various types.
67 // Each gives a fatal error if it cannot parse the argument.
69 extern void
70 parse_bool(const char* option_name, const char* arg, bool* retval);
72 extern void
73 parse_uint(const char* option_name, const char* arg, int* retval);
75 extern void
76 parse_uint64(const char* option_name, const char* arg, uint64_t* retval);
78 extern void
79 parse_double(const char* option_name, const char* arg, double* retval);
81 extern void
82 parse_string(const char* option_name, const char* arg, const char** retval);
84 extern void
85 parse_optional_string(const char* option_name, const char* arg,
86 const char** retval);
88 extern void
89 parse_dirlist(const char* option_name, const char* arg, Dir_list* retval);
91 extern void
92 parse_set(const char* option_name, const char* arg, String_set* retval);
94 extern void
95 parse_choices(const char* option_name, const char* arg, const char** retval,
96 const char* choices[], int num_choices);
98 struct Struct_var;
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
109 ONE_DASH, TWO_DASHES, EXACTLY_ONE_DASH, EXACTLY_TWO_DASHES, DASH_Z
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
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;
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)
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()] = '-';
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();
163 // This option takes an argument iff helparg is not NULL.
164 bool
165 takes_argument() const
166 { return this->helparg != NULL; }
168 // Whether the argument is optional.
169 bool
170 takes_optional_argument() const
171 { return this->optional_arg; }
173 // Register this option with the global list of options.
174 void
175 register_option();
177 // Print this option to stdout (used with --help).
178 void
179 print() const;
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
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.
201 // This is for "special" options that aren't of any predefined type.
202 struct Struct_special : public Struct_var
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)
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); }
218 One_option option;
219 Parse_function parse;
222 } // End namespace options.
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; } \
238 bool \
239 user_set_##varname__() const \
240 { return this->varname__##_.user_set_via_option; } \
242 private: \
243 struct Struct_##varname__ : public options::Struct_var \
245 Struct_##varname__() \
246 : option(#varname__, dashes__, shortname__, default_value_as_string__, \
247 helpstring__, helparg__, optional_arg__, this), \
248 user_set_via_option(false), value(default_value__) \
249 { } \
251 void \
252 parse_to_value(const char* option_name, const char* arg, \
253 Command_line*, General_options*) \
255 parse_fn__(option_name, arg, &this->value); \
256 this->user_set_via_option = true; \
259 options::One_option option; \
260 bool user_set_via_option; \
261 type__ value; \
262 }; \
263 Struct_##varname__ varname__##_; \
264 void \
265 set_##varname__(param_type__ value) \
266 { this->varname__##_.value = value; }
268 // These macros allow for easy addition of a new commandline option.
270 // If no_helpstring__ is not NULL, then in addition to creating
271 // VARNAME, we also create an option called no-VARNAME.
272 #define DEFINE_bool(varname__, dashes__, shortname__, default_value__, \
273 helpstring__, no_helpstring__) \
274 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
275 default_value__ ? "true" : "false", helpstring__, NULL, \
276 false, bool, bool, options::parse_bool) \
277 struct Struct_no_##varname__ : public options::Struct_var \
279 Struct_no_##varname__() : option("no-" #varname__, dashes__, '\0', \
280 default_value__ ? "false" : "true", \
281 no_helpstring__, NULL, false, this) \
282 { } \
284 void \
285 parse_to_value(const char*, const char*, \
286 Command_line*, General_options* options) \
287 { options->set_##varname__(false); } \
289 options::One_option option; \
290 }; \
291 Struct_no_##varname__ no_##varname__##_initializer_
293 #define DEFINE_enable(varname__, dashes__, shortname__, default_value__, \
294 helpstring__, no_helpstring__) \
295 DEFINE_var(enable_##varname__, dashes__, shortname__, default_value__, \
296 default_value__ ? "true" : "false", helpstring__, NULL, \
297 false, bool, bool, options::parse_bool) \
298 struct Struct_disable_##varname__ : public options::Struct_var \
300 Struct_disable_##varname__() : option("disable-" #varname__, \
301 dashes__, '\0', \
302 default_value__ ? "false" : "true", \
303 no_helpstring__, NULL, false, this) \
304 { } \
306 void \
307 parse_to_value(const char*, const char*, \
308 Command_line*, General_options* options) \
309 { options->set_enable_##varname__(false); } \
311 options::One_option option; \
312 }; \
313 Struct_disable_##varname__ disable_##varname__##_initializer_
315 #define DEFINE_uint(varname__, dashes__, shortname__, default_value__, \
316 helpstring__, helparg__) \
317 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
318 #default_value__, helpstring__, helparg__, false, \
319 int, int, options::parse_uint)
321 #define DEFINE_uint64(varname__, dashes__, shortname__, default_value__, \
322 helpstring__, helparg__) \
323 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
324 #default_value__, helpstring__, helparg__, false, \
325 uint64_t, uint64_t, options::parse_uint64)
327 #define DEFINE_double(varname__, dashes__, shortname__, default_value__, \
328 helpstring__, helparg__) \
329 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
330 #default_value__, helpstring__, helparg__, false, \
331 double, double, options::parse_double)
333 #define DEFINE_string(varname__, dashes__, shortname__, default_value__, \
334 helpstring__, helparg__) \
335 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
336 default_value__, helpstring__, helparg__, false, \
337 const char*, const char*, options::parse_string)
339 // This is like DEFINE_string, but we convert each occurrence to a
340 // Search_directory and store it in a vector. Thus we also have the
341 // add_to_VARNAME() method, to append to the vector.
342 #define DEFINE_dirlist(varname__, dashes__, shortname__, \
343 helpstring__, helparg__) \
344 DEFINE_var(varname__, dashes__, shortname__, , \
345 "", helpstring__, helparg__, false, options::Dir_list, \
346 const options::Dir_list&, options::parse_dirlist) \
347 void \
348 add_to_##varname__(const char* new_value) \
349 { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
350 void \
351 add_search_directory_to_##varname__(const Search_directory& dir) \
352 { this->varname__##_.value.push_back(dir); }
354 // This is like DEFINE_string, but we store a set of strings.
355 #define DEFINE_set(varname__, dashes__, shortname__, \
356 helpstring__, helparg__) \
357 DEFINE_var(varname__, dashes__, shortname__, , \
358 "", helpstring__, helparg__, false, options::String_set, \
359 const options::String_set&, options::parse_set) \
360 public: \
361 bool \
362 any_##varname__() const \
363 { return !this->varname__##_.value.empty(); } \
364 bool \
365 is_##varname__(const char* symbol) const \
367 return (!this->varname__##_.value.empty() \
368 && (this->varname__##_.value.find(std::string(symbol)) \
369 != this->varname__##_.value.end())); \
372 // When you have a list of possible values (expressed as string)
373 // After helparg__ should come an initializer list, like
374 // {"foo", "bar", "baz"}
375 #define DEFINE_enum(varname__, dashes__, shortname__, default_value__, \
376 helpstring__, helparg__, ...) \
377 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
378 default_value__, helpstring__, helparg__, false, \
379 const char*, const char*, parse_choices_##varname__) \
380 private: \
381 static void parse_choices_##varname__(const char* option_name, \
382 const char* arg, \
383 const char** retval) { \
384 const char* choices[] = __VA_ARGS__; \
385 options::parse_choices(option_name, arg, retval, \
386 choices, sizeof(choices) / sizeof(*choices)); \
389 // This is used for non-standard flags. It defines no functions; it
390 // just calls General_options::parse_VARNAME whenever the flag is
391 // seen. We declare parse_VARNAME as a static member of
392 // General_options; you are responsible for defining it there.
393 // helparg__ should be NULL iff this special-option is a boolean.
394 #define DEFINE_special(varname__, dashes__, shortname__, \
395 helpstring__, helparg__) \
396 private: \
397 void parse_##varname__(const char* option, const char* arg, \
398 Command_line* inputs); \
399 struct Struct_##varname__ : public options::Struct_special \
401 Struct_##varname__() \
402 : options::Struct_special(#varname__, dashes__, shortname__, \
403 &General_options::parse_##varname__, \
404 helpstring__, helparg__) \
405 { } \
406 }; \
407 Struct_##varname__ varname__##_initializer_
409 // An option that takes an optional string argument. If the option is
410 // used with no argument, the value will be the default, and
411 // user_set_via_option will be true.
412 #define DEFINE_optional_string(varname__, dashes__, shortname__, \
413 default_value__, \
414 helpstring__, helparg__) \
415 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
416 default_value__, helpstring__, helparg__, true, \
417 const char*, const char*, options::parse_optional_string)
419 // A directory to search. For each directory we record whether it is
420 // in the sysroot. We need to know this so that, if a linker script
421 // is found within the sysroot, we will apply the sysroot to any files
422 // named by that script.
424 class Search_directory
426 public:
427 // We need a default constructor because we put this in a
428 // std::vector.
429 Search_directory()
430 : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
433 // This is the usual constructor.
434 Search_directory(const char* name, bool put_in_sysroot)
435 : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
437 if (this->name_.empty())
438 this->name_ = ".";
441 // This is called if we have a sysroot. The sysroot is prefixed to
442 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
443 // set to true for any enries which are in the sysroot (this will
444 // naturally include any entries for which put_in_sysroot_ is true).
445 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
446 // passing SYSROOT to lrealpath.
447 void
448 add_sysroot(const char* sysroot, const char* canonical_sysroot);
450 // Get the directory name.
451 const std::string&
452 name() const
453 { return this->name_; }
455 // Return whether this directory is in the sysroot.
456 bool
457 is_in_sysroot() const
458 { return this->is_in_sysroot_; }
460 private:
461 std::string name_;
462 bool put_in_sysroot_;
463 bool is_in_sysroot_;
466 class General_options
468 private:
469 // NOTE: For every option that you add here, also consider if you
470 // should add it to Position_dependent_options.
471 DEFINE_special(help, options::TWO_DASHES, '\0',
472 N_("Report usage information"), NULL);
473 DEFINE_special(version, options::TWO_DASHES, 'v',
474 N_("Report version information"), NULL);
475 DEFINE_special(V, options::EXACTLY_ONE_DASH, '\0',
476 N_("Report version and target information"), NULL);
478 // These options are sorted approximately so that for each letter in
479 // the alphabet, we show the option whose shortname is that letter
480 // (if any) and then every longname that starts with that letter (in
481 // alphabetical order). For both, lowercase sorts before uppercase.
482 // The -z options come last.
484 DEFINE_bool(allow_shlib_undefined, options::TWO_DASHES, '\0', false,
485 N_("Allow unresolved references in shared libraries"),
486 N_("Do not allow unresolved references in shared libraries"));
488 DEFINE_bool(as_needed, options::TWO_DASHES, '\0', false,
489 N_("Only set DT_NEEDED for dynamic libs if used"),
490 N_("Always DT_NEEDED for dynamic libs"));
492 // This should really be an "enum", but it's too easy for folks to
493 // forget to update the list as they add new targets. So we just
494 // accept any string. We'll fail later (when the string is parsed),
495 // if the target isn't actually supported.
496 DEFINE_string(format, options::TWO_DASHES, 'b', "elf",
497 N_("Set input format"), ("[elf,binary]"));
499 DEFINE_bool(Bdynamic, options::ONE_DASH, '\0', true,
500 N_("-l searches for shared libraries"), NULL);
501 // Bstatic affects the same variable as Bdynamic, so we have to use
502 // the "special" macro to make that happen.
503 DEFINE_special(Bstatic, options::ONE_DASH, '\0',
504 N_("-l does not search for shared libraries"), NULL);
506 DEFINE_bool(Bsymbolic, options::ONE_DASH, '\0', false,
507 N_("Bind defined symbols locally"), NULL);
509 DEFINE_optional_string(build_id, options::TWO_DASHES, '\0', "sha1",
510 N_("Generate build ID note"),
511 N_("[=STYLE]"));
513 #ifdef HAVE_ZLIB_H
514 DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
515 N_("Compress .debug_* sections in the output file"),
516 ("[none,zlib]"),
517 {"none", "zlib"});
518 #else
519 DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
520 N_("Compress .debug_* sections in the output file"),
521 N_("[none]"),
522 {"none"});
523 #endif
525 DEFINE_bool(define_common, options::TWO_DASHES, 'd', false,
526 N_("Define common symbols"),
527 N_("Do not define common symbols"));
528 DEFINE_bool(dc, options::ONE_DASH, '\0', false,
529 N_("Alias for -d"), NULL);
530 DEFINE_bool(dp, options::ONE_DASH, '\0', false,
531 N_("Alias for -d"), NULL);
533 DEFINE_string(debug, options::TWO_DASHES, '\0', "",
534 N_("Turn on debugging"),
535 N_("[all,files,script,task][,...]"));
537 DEFINE_special(defsym, options::TWO_DASHES, '\0',
538 N_("Define a symbol"), N_("SYMBOL=EXPRESSION"));
540 DEFINE_optional_string(demangle, options::TWO_DASHES, '\0', NULL,
541 N_("Demangle C++ symbols in log messages"),
542 N_("[=STYLE]"));
544 DEFINE_bool(no_demangle, options::TWO_DASHES, '\0', false,
545 N_("Do not demangle C++ symbols in log messages"),
546 NULL);
548 DEFINE_bool(detect_odr_violations, options::TWO_DASHES, '\0', false,
549 N_("Try to detect violations of the One Definition Rule"),
550 NULL);
552 DEFINE_string(entry, options::TWO_DASHES, 'e', NULL,
553 N_("Set program start address"), N_("ADDRESS"));
555 DEFINE_bool(export_dynamic, options::TWO_DASHES, 'E', false,
556 N_("Export all dynamic symbols"), NULL);
558 DEFINE_bool(eh_frame_hdr, options::TWO_DASHES, '\0', false,
559 N_("Create exception frame header"), NULL);
561 DEFINE_string(soname, options::ONE_DASH, 'h', NULL,
562 N_("Set shared library name"), N_("FILENAME"));
564 DEFINE_double(hash_bucket_empty_fraction, options::TWO_DASHES, '\0', 0.0,
565 N_("Min fraction of empty buckets in dynamic hash"),
566 N_("FRACTION"));
568 DEFINE_enum(hash_style, options::TWO_DASHES, '\0', "sysv",
569 N_("Dynamic hash style"), N_("[sysv,gnu,both]"),
570 {"sysv", "gnu", "both"});
572 DEFINE_string(dynamic_linker, options::TWO_DASHES, 'I', NULL,
573 N_("Set dynamic linker path"), N_("PROGRAM"));
575 DEFINE_special(just_symbols, options::TWO_DASHES, '\0',
576 N_("Read only symbol values from FILE"), N_("FILE"));
578 DEFINE_special(library, options::TWO_DASHES, 'l',
579 N_("Search for library LIBNAME"), N_("LIBNAME"));
581 DEFINE_dirlist(library_path, options::TWO_DASHES, 'L',
582 N_("Add directory to search path"), N_("DIR"));
584 DEFINE_string(m, options::EXACTLY_ONE_DASH, 'm', "",
585 N_("Ignored for compatibility"), N_("EMULATION"));
587 DEFINE_enable(new_dtags, options::EXACTLY_TWO_DASHES, '\0', false,
588 N_("Enable use of DT_RUNPATH and DT_FLAGS"),
589 N_("Disable use of DT_RUNPATH and DT_FLAGS"));
591 DEFINE_bool(noinhibit_exec, options::TWO_DASHES, '\0', false,
592 N_("Create an output file even if errors occur"), NULL);
594 DEFINE_string(output, options::TWO_DASHES, 'o', "a.out",
595 N_("Set output file name"), N_("FILE"));
597 DEFINE_uint(optimize, options::EXACTLY_ONE_DASH, 'O', 0,
598 N_("Optimize output file size"), N_("LEVEL"));
600 DEFINE_string(oformat, options::EXACTLY_TWO_DASHES, '\0', "elf",
601 N_("Set output format"), N_("[binary]"));
603 DEFINE_bool(Qy, options::EXACTLY_ONE_DASH, '\0', false,
604 N_("Ignored for SVR4 compatibility"), NULL);
606 DEFINE_bool(emit_relocs, options::TWO_DASHES, 'q', false,
607 N_("Generate relocations in output"), NULL);
609 DEFINE_bool(relocatable, options::EXACTLY_ONE_DASH, 'r', false,
610 N_("Generate relocatable output"), NULL);
612 DEFINE_bool(relax, options::TWO_DASHES, '\0', false,
613 N_("Relax branches on certain targets"), NULL);
615 // -R really means -rpath, but can mean --just-symbols for
616 // compatibility with GNU ld. -rpath is always -rpath, so we list
617 // it separately.
618 DEFINE_special(R, options::EXACTLY_ONE_DASH, 'R',
619 N_("Add DIR to runtime search path"), N_("DIR"));
621 DEFINE_dirlist(rpath, options::ONE_DASH, '\0',
622 N_("Add DIR to runtime search path"), N_("DIR"));
624 DEFINE_dirlist(rpath_link, options::TWO_DASHES, '\0',
625 N_("Add DIR to link time shared library search path"),
626 N_("DIR"));
628 DEFINE_bool(strip_all, options::TWO_DASHES, 's', false,
629 N_("Strip all symbols"), NULL);
630 DEFINE_bool(strip_debug, options::TWO_DASHES, 'S', false,
631 N_("Strip debugging information"), NULL);
632 DEFINE_bool(strip_debug_gdb, options::TWO_DASHES, '\0', false,
633 N_("Strip debug symbols that are unused by gdb "
634 "(at least versions <= 6.7)"), NULL);
636 DEFINE_bool(shared, options::ONE_DASH, '\0', false,
637 N_("Generate shared library"), NULL);
639 // This is not actually special in any way, but I need to give it
640 // a non-standard accessor-function name because 'static' is a keyword.
641 DEFINE_special(static, options::ONE_DASH, '\0',
642 N_("Do not link against shared libraries"), NULL);
644 DEFINE_bool(stats, options::TWO_DASHES, '\0', false,
645 N_("Print resource usage statistics"), NULL);
647 DEFINE_string(sysroot, options::TWO_DASHES, '\0', "",
648 N_("Set target system root directory"), N_("DIR"));
650 DEFINE_bool(trace, options::TWO_DASHES, 't', false,
651 N_("Print the name of each input file"), NULL);
653 DEFINE_special(script, options::TWO_DASHES, 'T',
654 N_("Read linker script"), N_("FILE"));
656 DEFINE_bool(threads, options::TWO_DASHES, '\0', false,
657 N_("Run the linker multi-threaded"),
658 N_("Do not run the linker multi-threaded"));
659 DEFINE_uint(thread_count, options::TWO_DASHES, '\0', 0,
660 N_("Number of threads to use"), N_("COUNT"));
661 DEFINE_uint(thread_count_initial, options::TWO_DASHES, '\0', 0,
662 N_("Number of threads to use in initial pass"), N_("COUNT"));
663 DEFINE_uint(thread_count_middle, options::TWO_DASHES, '\0', 0,
664 N_("Number of threads to use in middle pass"), N_("COUNT"));
665 DEFINE_uint(thread_count_final, options::TWO_DASHES, '\0', 0,
666 N_("Number of threads to use in final pass"), N_("COUNT"));
668 DEFINE_uint64(Tbss, options::ONE_DASH, '\0', -1U,
669 N_("Set the address of the bss segment"), N_("ADDRESS"));
670 DEFINE_uint64(Tdata, options::ONE_DASH, '\0', -1U,
671 N_("Set the address of the data segment"), N_("ADDRESS"));
672 DEFINE_uint64(Ttext, options::ONE_DASH, '\0', -1U,
673 N_("Set the address of the text segment"), N_("ADDRESS"));
675 DEFINE_bool(verbose, options::TWO_DASHES, '\0', false,
676 N_("Synonym for --debug=files"), NULL);
678 DEFINE_special(version_script, options::TWO_DASHES, '\0',
679 N_("Read version script"), N_("FILE"));
681 DEFINE_bool(whole_archive, options::TWO_DASHES, '\0', false,
682 N_("Include all archive contents"),
683 N_("Include only needed archive contents"));
685 DEFINE_set(wrap, options::TWO_DASHES, '\0',
686 N_("Use wrapper functions for SYMBOL"), N_("SYMBOL"));
688 DEFINE_set(trace_symbol, options::TWO_DASHES, 'y',
689 N_("Trace references to symbol"), N_("SYMBOL"));
691 DEFINE_string(Y, options::EXACTLY_ONE_DASH, 'Y', "",
692 N_("Default search path for Solaris compatibility"),
693 N_("PATH"));
695 DEFINE_special(start_group, options::TWO_DASHES, '(',
696 N_("Start a library search group"), NULL);
697 DEFINE_special(end_group, options::TWO_DASHES, ')',
698 N_("End a library search group"), NULL);
700 // The -z options.
702 // Both execstack and noexecstack differ from the default execstack_
703 // value, so we need to use different variables for them.
704 DEFINE_uint64(common_page_size, options::DASH_Z, '\0', 0,
705 N_("Set common page size to SIZE"), N_("SIZE"));
706 DEFINE_bool(defs, options::DASH_Z, '\0', false,
707 N_("Report undefined symbols (even with --shared)"),
708 NULL);
709 DEFINE_bool(execstack, options::DASH_Z, '\0', false,
710 N_("Mark output as requiring executable stack"), NULL);
711 DEFINE_uint64(max_page_size, options::DASH_Z, '\0', 0,
712 N_("Set maximum page size to SIZE"), N_("SIZE"));
713 DEFINE_bool(noexecstack, options::DASH_Z, '\0', false,
714 N_("Mark output as not requiring executable stack"), NULL);
715 DEFINE_bool(initfirst, options::DASH_Z, '\0', false,
716 N_("Mark DSO to be initialized first at runtime"),
717 NULL);
718 DEFINE_bool(interpose, options::DASH_Z, '\0', false,
719 N_("Mark object to interpose all DSOs but executable"),
720 NULL);
721 DEFINE_bool(loadfltr, options::DASH_Z, '\0', false,
722 N_("Mark object requiring immediate process"),
723 NULL);
724 DEFINE_bool(nodefaultlib, options::DASH_Z, '\0', false,
725 N_("Mark object not to use default search paths"),
726 NULL);
727 DEFINE_bool(nodelete, options::DASH_Z, '\0', false,
728 N_("Mark DSO non-deletable at runtime"),
729 NULL);
730 DEFINE_bool(nodlopen, options::DASH_Z, '\0', false,
731 N_("Mark DSO not available to dlopen"),
732 NULL);
733 DEFINE_bool(nodump, options::DASH_Z, '\0', false,
734 N_("Mark DSO not available to dldump"),
735 NULL);
737 public:
738 typedef options::Dir_list Dir_list;
740 General_options();
742 // Does post-processing on flags, making sure they all have
743 // non-conflicting values. Also converts some flags from their
744 // "standard" types (string, etc), to another type (enum, DirList),
745 // which can be accessed via a separate method. Dies if it notices
746 // any problems.
747 void finalize();
749 // The macro defines output() (based on --output), but that's a
750 // generic name. Provide this alternative name, which is clearer.
751 const char*
752 output_file_name() const
753 { return this->output(); }
755 // This is not defined via a flag, but combines flags to say whether
756 // the output is position-independent or not.
757 bool
758 output_is_position_independent() const
759 { return this->shared(); }
761 // This would normally be static(), and defined automatically, but
762 // since static is a keyword, we need to come up with our own name.
763 bool
764 is_static() const
765 { return static_; }
767 // In addition to getting the input and output formats as a string
768 // (via format() and oformat()), we also give access as an enum.
769 enum Object_format
771 // Ordinary ELF.
772 OBJECT_FORMAT_ELF,
773 // Straight binary format.
774 OBJECT_FORMAT_BINARY
777 // Note: these functions are not very fast.
778 Object_format format_enum() const;
779 Object_format oformat_enum() const;
781 // These are the best way to get access to the execstack state,
782 // not execstack() and noexecstack() which are hard to use properly.
783 bool
784 is_execstack_set() const
785 { return this->execstack_status_ != EXECSTACK_FROM_INPUT; }
787 bool
788 is_stack_executable() const
789 { return this->execstack_status_ == EXECSTACK_YES; }
791 // The --demangle option takes an optional string, and there is also
792 // a --no-demangle option. This is the best way to decide whether
793 // to demangle or not.
794 bool
795 do_demangle() const
796 { return this->do_demangle_; }
798 private:
799 // Don't copy this structure.
800 General_options(const General_options&);
801 General_options& operator=(const General_options&);
803 // Whether to mark the stack as executable.
804 enum Execstack
806 // Not set on command line.
807 EXECSTACK_FROM_INPUT,
808 // Mark the stack as executable (-z execstack).
809 EXECSTACK_YES,
810 // Mark the stack as not executable (-z noexecstack).
811 EXECSTACK_NO
814 void
815 set_execstack_status(Execstack value)
816 { this->execstack_status_ = value; }
818 void
819 set_do_demangle(bool value)
820 { this->do_demangle_ = value; }
822 void
823 set_static(bool value)
824 { static_ = value; }
826 // These are called by finalize() to set up the search-path correctly.
827 void
828 add_to_library_path_with_sysroot(const char* arg)
829 { this->add_search_directory_to_library_path(Search_directory(arg, true)); }
831 // Apply any sysroot to the directory lists.
832 void
833 add_sysroot();
835 // Whether to mark the stack as executable.
836 Execstack execstack_status_;
837 // Whether to do a static link.
838 bool static_;
839 // Whether to do demangling.
840 bool do_demangle_;
843 // The position-dependent options. We use this to store the state of
844 // the commandline at a particular point in parsing for later
845 // reference. For instance, if we see "ld --whole-archive foo.a
846 // --no-whole-archive," we want to store the whole-archive option with
847 // foo.a, so when the time comes to parse foo.a we know we should do
848 // it in whole-archive mode. We could store all of General_options,
849 // but that's big, so we just pick the subset of flags that actually
850 // change in a position-dependent way.
852 #define DEFINE_posdep(varname__, type__) \
853 public: \
854 type__ \
855 varname__() const \
856 { return this->varname__##_; } \
858 void \
859 set_##varname__(type__ value) \
860 { this->varname__##_ = value; } \
861 private: \
862 type__ varname__##_
864 class Position_dependent_options
866 public:
867 Position_dependent_options(const General_options& options
868 = Position_dependent_options::default_options_)
869 { copy_from_options(options); }
871 void copy_from_options(const General_options& options)
873 this->set_as_needed(options.as_needed());
874 this->set_Bdynamic(options.Bdynamic());
875 this->set_format_enum(options.format_enum());
876 this->set_whole_archive(options.whole_archive());
879 DEFINE_posdep(as_needed, bool);
880 DEFINE_posdep(Bdynamic, bool);
881 DEFINE_posdep(format_enum, General_options::Object_format);
882 DEFINE_posdep(whole_archive, bool);
884 private:
885 // This is a General_options with everything set to its default
886 // value. A Position_dependent_options created with no argument
887 // will take its values from here.
888 static General_options default_options_;
892 // A single file or library argument from the command line.
894 class Input_file_argument
896 public:
897 // name: file name or library name
898 // is_lib: true if name is a library name: that is, emits the leading
899 // "lib" and trailing ".so"/".a" from the name
900 // extra_search_path: an extra directory to look for the file, prior
901 // to checking the normal library search path. If this is "",
902 // then no extra directory is added.
903 // just_symbols: whether this file only defines symbols.
904 // options: The position dependent options at this point in the
905 // command line, such as --whole-archive.
906 Input_file_argument()
907 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
908 options_()
911 Input_file_argument(const char* name, bool is_lib,
912 const char* extra_search_path,
913 bool just_symbols,
914 const Position_dependent_options& options)
915 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
916 just_symbols_(just_symbols), options_(options)
919 // You can also pass in a General_options instance instead of a
920 // Position_dependent_options. In that case, we extract the
921 // position-independent vars from the General_options and only store
922 // those.
923 Input_file_argument(const char* name, bool is_lib,
924 const char* extra_search_path,
925 bool just_symbols,
926 const General_options& options)
927 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
928 just_symbols_(just_symbols), options_(options)
931 const char*
932 name() const
933 { return this->name_.c_str(); }
935 const Position_dependent_options&
936 options() const
937 { return this->options_; }
939 bool
940 is_lib() const
941 { return this->is_lib_; }
943 const char*
944 extra_search_path() const
946 return (this->extra_search_path_.empty()
947 ? NULL
948 : this->extra_search_path_.c_str());
951 // Return whether we should only read symbols from this file.
952 bool
953 just_symbols() const
954 { return this->just_symbols_; }
956 // Return whether this file may require a search using the -L
957 // options.
958 bool
959 may_need_search() const
960 { return this->is_lib_ || !this->extra_search_path_.empty(); }
962 private:
963 // We use std::string, not const char*, here for convenience when
964 // using script files, so that we do not have to preserve the string
965 // in that case.
966 std::string name_;
967 bool is_lib_;
968 std::string extra_search_path_;
969 bool just_symbols_;
970 Position_dependent_options options_;
973 // A file or library, or a group, from the command line.
975 class Input_argument
977 public:
978 // Create a file or library argument.
979 explicit Input_argument(Input_file_argument file)
980 : is_file_(true), file_(file), group_(NULL)
983 // Create a group argument.
984 explicit Input_argument(Input_file_group* group)
985 : is_file_(false), group_(group)
988 // Return whether this is a file.
989 bool
990 is_file() const
991 { return this->is_file_; }
993 // Return whether this is a group.
994 bool
995 is_group() const
996 { return !this->is_file_; }
998 // Return the information about the file.
999 const Input_file_argument&
1000 file() const
1002 gold_assert(this->is_file_);
1003 return this->file_;
1006 // Return the information about the group.
1007 const Input_file_group*
1008 group() const
1010 gold_assert(!this->is_file_);
1011 return this->group_;
1014 Input_file_group*
1015 group()
1017 gold_assert(!this->is_file_);
1018 return this->group_;
1021 private:
1022 bool is_file_;
1023 Input_file_argument file_;
1024 Input_file_group* group_;
1027 // A group from the command line. This is a set of arguments within
1028 // --start-group ... --end-group.
1030 class Input_file_group
1032 public:
1033 typedef std::vector<Input_argument> Files;
1034 typedef Files::const_iterator const_iterator;
1036 Input_file_group()
1037 : files_()
1040 // Add a file to the end of the group.
1041 void
1042 add_file(const Input_file_argument& arg)
1043 { this->files_.push_back(Input_argument(arg)); }
1045 // Iterators to iterate over the group contents.
1047 const_iterator
1048 begin() const
1049 { return this->files_.begin(); }
1051 const_iterator
1052 end() const
1053 { return this->files_.end(); }
1055 private:
1056 Files files_;
1059 // A list of files from the command line or a script.
1061 class Input_arguments
1063 public:
1064 typedef std::vector<Input_argument> Input_argument_list;
1065 typedef Input_argument_list::const_iterator const_iterator;
1067 Input_arguments()
1068 : input_argument_list_(), in_group_(false)
1071 // Add a file.
1072 void
1073 add_file(const Input_file_argument& arg);
1075 // Start a group (the --start-group option).
1076 void
1077 start_group();
1079 // End a group (the --end-group option).
1080 void
1081 end_group();
1083 // Return whether we are currently in a group.
1084 bool
1085 in_group() const
1086 { return this->in_group_; }
1088 // The number of entries in the list.
1090 size() const
1091 { return this->input_argument_list_.size(); }
1093 // Iterators to iterate over the list of input files.
1095 const_iterator
1096 begin() const
1097 { return this->input_argument_list_.begin(); }
1099 const_iterator
1100 end() const
1101 { return this->input_argument_list_.end(); }
1103 // Return whether the list is empty.
1104 bool
1105 empty() const
1106 { return this->input_argument_list_.empty(); }
1108 private:
1109 Input_argument_list input_argument_list_;
1110 bool in_group_;
1114 // All the information read from the command line. These are held in
1115 // three separate structs: one to hold the options (--foo), one to
1116 // hold the filenames listed on the commandline, and one to hold
1117 // linker script information. This third is not a subset of the other
1118 // two because linker scripts can be specified either as options (via
1119 // -T) or as a file.
1121 class Command_line
1123 public:
1124 typedef Input_arguments::const_iterator const_iterator;
1126 Command_line();
1128 // Process the command line options. This will exit with an
1129 // appropriate error message if an unrecognized option is seen.
1130 void
1131 process(int argc, const char** argv);
1133 // Process one command-line option. This takes the index of argv to
1134 // process, and returns the index for the next option. no_more_options
1135 // is set to true if argv[i] is "--".
1137 process_one_option(int argc, const char** argv, int i,
1138 bool* no_more_options);
1140 // Get the general options.
1141 const General_options&
1142 options() const
1143 { return this->options_; }
1145 // Get the position dependent options.
1146 const Position_dependent_options&
1147 position_dependent_options() const
1148 { return this->position_options_; }
1150 // Get the linker-script options.
1151 Script_options&
1152 script_options()
1153 { return this->script_options_; }
1155 // Get the version-script options: a convenience routine.
1156 const Version_script_info&
1157 version_script() const
1158 { return *this->script_options_.version_script_info(); }
1160 // Get the input files.
1161 Input_arguments&
1162 inputs()
1163 { return this->inputs_; }
1165 // The number of input files.
1167 number_of_input_files() const
1168 { return this->inputs_.size(); }
1170 // Iterators to iterate over the list of input files.
1172 const_iterator
1173 begin() const
1174 { return this->inputs_.begin(); }
1176 const_iterator
1177 end() const
1178 { return this->inputs_.end(); }
1180 private:
1181 Command_line(const Command_line&);
1182 Command_line& operator=(const Command_line&);
1184 General_options options_;
1185 Position_dependent_options position_options_;
1186 Script_options script_options_;
1187 Input_arguments inputs_;
1190 } // End namespace gold.
1192 #endif // !defined(GOLD_OPTIONS_H)