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.
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
52 class General_options
;
53 class Search_directory
;
54 class Input_file_group
;
55 class Position_dependent_options
;
59 // The nested namespace is to contain all the global variables and
60 // structs that need to be defined in the .h file, but do not need to
61 // be used outside this class.
64 typedef std::vector
<Search_directory
> Dir_list
;
65 typedef Unordered_set
<std::string
> String_set
;
67 // These routines convert from a string option to various types.
68 // Each gives a fatal error if it cannot parse the argument.
71 parse_bool(const char* option_name
, const char* arg
, bool* retval
);
74 parse_uint(const char* option_name
, const char* arg
, int* retval
);
77 parse_uint64(const char* option_name
, const char* arg
, uint64_t* retval
);
80 parse_double(const char* option_name
, const char* arg
, double* retval
);
83 parse_string(const char* option_name
, const char* arg
, const char** retval
);
86 parse_optional_string(const char* option_name
, const char* arg
,
90 parse_dirlist(const char* option_name
, const char* arg
, Dir_list
* retval
);
93 parse_set(const char* option_name
, const char* arg
, String_set
* retval
);
96 parse_choices(const char* option_name
, const char* arg
, const char** retval
,
97 const char* choices
[], int num_choices
);
101 // Most options have both a shortname (one letter) and a longname.
102 // This enum controls how many dashes are expected for longname access
103 // -- shortnames always use one dash. Most longnames will accept
104 // either one dash or two; the only difference between ONE_DASH and
105 // TWO_DASHES is how we print the option in --help. However, some
106 // longnames require two dashes, and some require only one. The
107 // special value DASH_Z means that the option is preceded by "-z".
110 ONE_DASH
, TWO_DASHES
, EXACTLY_ONE_DASH
, EXACTLY_TWO_DASHES
, DASH_Z
113 // LONGNAME is the long-name of the option with dashes converted to
114 // underscores, or else the short-name if the option has no long-name.
115 // It is never the empty string.
116 // DASHES is an instance of the Dashes enum: ONE_DASH, TWO_DASHES, etc.
117 // SHORTNAME is the short-name of the option, as a char, or '\0' if the
118 // option has no short-name. If the option has no long-name, you
119 // should specify the short-name in *both* VARNAME and here.
120 // DEFAULT_VALUE is the value of the option if not specified on the
121 // commandline, as a string.
122 // HELPSTRING is the descriptive text used with the option via --help
123 // HELPARG is how you define the argument to the option.
124 // --help output is "-shortname HELPARG, --longname HELPARG: HELPSTRING"
125 // HELPARG should be NULL iff the option is a bool and takes no arg.
126 // OPTIONAL_ARG is true if this option takes an optional argument. An
127 // optional argument must be specifid as --OPTION=VALUE, not
129 // READER provides parse_to_value, which is a function that will convert
130 // a char* argument into the proper type and store it in some variable.
131 // A One_option struct initializes itself with the global list of options
132 // at constructor time, so be careful making one of these.
135 std::string longname
;
138 const char* default_value
;
139 const char* helpstring
;
144 One_option(const char* ln
, Dashes d
, char sn
, const char* dv
,
145 const char* hs
, const char* ha
, bool oa
, Struct_var
* r
)
146 : longname(ln
), dashes(d
), shortname(sn
), default_value(dv
? dv
: ""),
147 helpstring(hs
), helparg(ha
), optional_arg(oa
), reader(r
)
149 // In longname, we convert all underscores to dashes, since GNU
150 // style uses dashes in option names. longname is likely to have
151 // underscores in it because it's also used to declare a C++
153 const char* pos
= strchr(this->longname
.c_str(), '_');
154 for (; pos
; pos
= strchr(pos
, '_'))
155 this->longname
[pos
- this->longname
.c_str()] = '-';
157 // We only register ourselves if our helpstring is not NULL. This
158 // is to support the "no-VAR" boolean variables, which we
159 // conditionally turn on by defining "no-VAR" help text.
160 if (this->helpstring
)
161 this->register_option();
164 // This option takes an argument iff helparg is not NULL.
166 takes_argument() const
167 { return this->helparg
!= NULL
; }
169 // Whether the argument is optional.
171 takes_optional_argument() const
172 { return this->optional_arg
; }
174 // Register this option with the global list of options.
178 // Print this option to stdout (used with --help).
183 // All options have a Struct_##varname that inherits from this and
184 // actually implements parse_to_value for that option.
187 // OPTION: the name of the option as specified on the commandline,
188 // including leading dashes, and any text following the option:
189 // "-O", "--defsym=mysym=0x1000", etc.
190 // ARG: the arg associated with this option, or NULL if the option
191 // takes no argument: "2", "mysym=0x1000", etc.
192 // CMDLINE: the global Command_line object. Used by DEFINE_special.
193 // OPTIONS: the global General_options object. Used by DEFINE_special.
195 parse_to_value(const char* option
, const char* arg
,
196 Command_line
* cmdline
, General_options
* options
) = 0;
198 ~Struct_var() // To make gcc happy.
202 // This is for "special" options that aren't of any predefined type.
203 struct Struct_special
: public Struct_var
205 // If you change this, change the parse-fn in DEFINE_special as well.
206 typedef void (General_options::*Parse_function
)(const char*, const char*,
208 Struct_special(const char* varname
, Dashes dashes
, char shortname
,
209 Parse_function parse_function
,
210 const char* helpstring
, const char* helparg
)
211 : option(varname
, dashes
, shortname
, "", helpstring
, helparg
, false, this),
212 parse(parse_function
)
215 void parse_to_value(const char* option
, const char* arg
,
216 Command_line
* cmdline
, General_options
* options
)
217 { (options
->*(this->parse
))(option
, arg
, cmdline
); }
220 Parse_function parse
;
223 } // End namespace options.
226 // These are helper macros use by DEFINE_uint64/etc below.
227 // This macro is used inside the General_options_ class, so defines
228 // var() and set_var() as General_options methods. Arguments as are
229 // for the constructor for One_option. param_type__ is the same as
230 // type__ for built-in types, and "const type__ &" otherwise.
231 #define DEFINE_var(varname__, dashes__, shortname__, default_value__, \
232 default_value_as_string__, helpstring__, helparg__, \
233 optional_arg__, type__, param_type__, parse_fn__) \
237 { return this->varname__##_.value; } \
240 user_set_##varname__() const \
241 { return this->varname__##_.user_set_via_option; } \
244 set_user_set_##varname__() \
245 { this->varname__##_.user_set_via_option = true; } \
248 struct Struct_##varname__ : public options::Struct_var \
250 Struct_##varname__() \
251 : option(#varname__, dashes__, shortname__, default_value_as_string__, \
252 helpstring__, helparg__, optional_arg__, this), \
253 user_set_via_option(false), value(default_value__) \
257 parse_to_value(const char* option_name, const char* arg, \
258 Command_line*, General_options*) \
260 parse_fn__(option_name, arg, &this->value); \
261 this->user_set_via_option = true; \
264 options::One_option option; \
265 bool user_set_via_option; \
268 Struct_##varname__ varname__##_; \
270 set_##varname__(param_type__ value) \
271 { this->varname__##_.value = value; }
273 // These macros allow for easy addition of a new commandline option.
275 // If no_helpstring__ is not NULL, then in addition to creating
276 // VARNAME, we also create an option called no-VARNAME (or, for a -z
277 // option, noVARNAME).
278 #define DEFINE_bool(varname__, dashes__, shortname__, default_value__, \
279 helpstring__, no_helpstring__) \
280 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
281 default_value__ ? "true" : "false", helpstring__, NULL, \
282 false, bool, bool, options::parse_bool) \
283 struct Struct_no_##varname__ : public options::Struct_var \
285 Struct_no_##varname__() : option((dashes__ == options::DASH_Z \
287 : "no-" #varname__), \
289 default_value__ ? "false" : "true", \
290 no_helpstring__, NULL, false, this) \
294 parse_to_value(const char*, const char*, \
295 Command_line*, General_options* options) \
296 { options->set_##varname__(false); } \
298 options::One_option option; \
300 Struct_no_##varname__ no_##varname__##_initializer_
302 #define DEFINE_enable(varname__, dashes__, shortname__, default_value__, \
303 helpstring__, no_helpstring__) \
304 DEFINE_var(enable_##varname__, dashes__, shortname__, default_value__, \
305 default_value__ ? "true" : "false", helpstring__, NULL, \
306 false, bool, bool, options::parse_bool) \
307 struct Struct_disable_##varname__ : public options::Struct_var \
309 Struct_disable_##varname__() : option("disable-" #varname__, \
311 default_value__ ? "false" : "true", \
312 no_helpstring__, NULL, false, this) \
316 parse_to_value(const char*, const char*, \
317 Command_line*, General_options* options) \
318 { options->set_enable_##varname__(false); } \
320 options::One_option option; \
322 Struct_disable_##varname__ disable_##varname__##_initializer_
324 #define DEFINE_uint(varname__, dashes__, shortname__, default_value__, \
325 helpstring__, helparg__) \
326 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
327 #default_value__, helpstring__, helparg__, false, \
328 int, int, options::parse_uint)
330 #define DEFINE_uint64(varname__, dashes__, shortname__, default_value__, \
331 helpstring__, helparg__) \
332 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
333 #default_value__, helpstring__, helparg__, false, \
334 uint64_t, uint64_t, options::parse_uint64)
336 #define DEFINE_double(varname__, dashes__, shortname__, default_value__, \
337 helpstring__, helparg__) \
338 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
339 #default_value__, helpstring__, helparg__, false, \
340 double, double, options::parse_double)
342 #define DEFINE_string(varname__, dashes__, shortname__, default_value__, \
343 helpstring__, helparg__) \
344 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
345 default_value__, helpstring__, helparg__, false, \
346 const char*, const char*, options::parse_string)
348 // This is like DEFINE_string, but we convert each occurrence to a
349 // Search_directory and store it in a vector. Thus we also have the
350 // add_to_VARNAME() method, to append to the vector.
351 #define DEFINE_dirlist(varname__, dashes__, shortname__, \
352 helpstring__, helparg__) \
353 DEFINE_var(varname__, dashes__, shortname__, , \
354 "", helpstring__, helparg__, false, options::Dir_list, \
355 const options::Dir_list&, options::parse_dirlist) \
357 add_to_##varname__(const char* new_value) \
358 { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
360 add_search_directory_to_##varname__(const Search_directory& dir) \
361 { this->varname__##_.value.push_back(dir); }
363 // This is like DEFINE_string, but we store a set of strings.
364 #define DEFINE_set(varname__, dashes__, shortname__, \
365 helpstring__, helparg__) \
366 DEFINE_var(varname__, dashes__, shortname__, , \
367 "", helpstring__, helparg__, false, options::String_set, \
368 const options::String_set&, options::parse_set) \
371 any_##varname__() const \
372 { return !this->varname__##_.value.empty(); } \
375 is_##varname__(const char* symbol) const \
377 return (!this->varname__##_.value.empty() \
378 && (this->varname__##_.value.find(std::string(symbol)) \
379 != this->varname__##_.value.end())); \
382 options::String_set::const_iterator \
383 varname__##_begin() const \
384 { return this->varname__##_.value.begin(); } \
386 options::String_set::const_iterator \
387 varname__##_end() const \
388 { return this->varname__##_.value.end(); }
390 // When you have a list of possible values (expressed as string)
391 // After helparg__ should come an initializer list, like
392 // {"foo", "bar", "baz"}
393 #define DEFINE_enum(varname__, dashes__, shortname__, default_value__, \
394 helpstring__, helparg__, ...) \
395 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
396 default_value__, helpstring__, helparg__, false, \
397 const char*, const char*, parse_choices_##varname__) \
399 static void parse_choices_##varname__(const char* option_name, \
401 const char** retval) { \
402 const char* choices[] = __VA_ARGS__; \
403 options::parse_choices(option_name, arg, retval, \
404 choices, sizeof(choices) / sizeof(*choices)); \
407 // This is like DEFINE_bool, but VARNAME is the name of a different
408 // option. This option becomes an alias for that one. INVERT is true
409 // if this option is an inversion of the other one.
410 #define DEFINE_bool_alias(option__, varname__, dashes__, shortname__, \
411 helpstring__, no_helpstring__, invert__) \
413 struct Struct_##option__ : public options::Struct_var \
415 Struct_##option__() \
416 : option(#option__, dashes__, shortname__, "", helpstring__, \
421 parse_to_value(const char*, const char*, \
422 Command_line*, General_options* options) \
424 options->set_##varname__(!invert__); \
425 options->set_user_set_##varname__(); \
428 options::One_option option; \
430 Struct_##option__ option__##_; \
432 struct Struct_no_##option__ : public options::Struct_var \
434 Struct_no_##option__() \
435 : option((dashes__ == options::DASH_Z \
437 : "no-" #option__), \
438 dashes__, '\0', "", no_helpstring__, \
443 parse_to_value(const char*, const char*, \
444 Command_line*, General_options* options) \
446 options->set_##varname__(invert__); \
447 options->set_user_set_##varname__(); \
450 options::One_option option; \
452 Struct_no_##option__ no_##option__##_initializer_
454 // This is used for non-standard flags. It defines no functions; it
455 // just calls General_options::parse_VARNAME whenever the flag is
456 // seen. We declare parse_VARNAME as a static member of
457 // General_options; you are responsible for defining it there.
458 // helparg__ should be NULL iff this special-option is a boolean.
459 #define DEFINE_special(varname__, dashes__, shortname__, \
460 helpstring__, helparg__) \
462 void parse_##varname__(const char* option, const char* arg, \
463 Command_line* inputs); \
464 struct Struct_##varname__ : public options::Struct_special \
466 Struct_##varname__() \
467 : options::Struct_special(#varname__, dashes__, shortname__, \
468 &General_options::parse_##varname__, \
469 helpstring__, helparg__) \
472 Struct_##varname__ varname__##_initializer_
474 // An option that takes an optional string argument. If the option is
475 // used with no argument, the value will be the default, and
476 // user_set_via_option will be true.
477 #define DEFINE_optional_string(varname__, dashes__, shortname__, \
479 helpstring__, helparg__) \
480 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
481 default_value__, helpstring__, helparg__, true, \
482 const char*, const char*, options::parse_optional_string)
484 // A directory to search. For each directory we record whether it is
485 // in the sysroot. We need to know this so that, if a linker script
486 // is found within the sysroot, we will apply the sysroot to any files
487 // named by that script.
489 class Search_directory
492 // We need a default constructor because we put this in a
495 : name_(NULL
), put_in_sysroot_(false), is_in_sysroot_(false)
498 // This is the usual constructor.
499 Search_directory(const char* name
, bool put_in_sysroot
)
500 : name_(name
), put_in_sysroot_(put_in_sysroot
), is_in_sysroot_(false)
502 if (this->name_
.empty())
506 // This is called if we have a sysroot. The sysroot is prefixed to
507 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
508 // set to true for any enries which are in the sysroot (this will
509 // naturally include any entries for which put_in_sysroot_ is true).
510 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
511 // passing SYSROOT to lrealpath.
513 add_sysroot(const char* sysroot
, const char* canonical_sysroot
);
515 // Get the directory name.
518 { return this->name_
; }
520 // Return whether this directory is in the sysroot.
522 is_in_sysroot() const
523 { return this->is_in_sysroot_
; }
527 bool put_in_sysroot_
;
531 class General_options
534 // NOTE: For every option that you add here, also consider if you
535 // should add it to Position_dependent_options.
536 DEFINE_special(help
, options::TWO_DASHES
, '\0',
537 N_("Report usage information"), NULL
);
538 DEFINE_special(version
, options::TWO_DASHES
, 'v',
539 N_("Report version information"), NULL
);
540 DEFINE_special(V
, options::EXACTLY_ONE_DASH
, '\0',
541 N_("Report version and target information"), NULL
);
543 // These options are sorted approximately so that for each letter in
544 // the alphabet, we show the option whose shortname is that letter
545 // (if any) and then every longname that starts with that letter (in
546 // alphabetical order). For both, lowercase sorts before uppercase.
547 // The -z options come last.
549 DEFINE_bool(allow_shlib_undefined
, options::TWO_DASHES
, '\0', false,
550 N_("Allow unresolved references in shared libraries"),
551 N_("Do not allow unresolved references in shared libraries"));
553 DEFINE_bool(as_needed
, options::TWO_DASHES
, '\0', false,
554 N_("Only set DT_NEEDED for dynamic libs if used"),
555 N_("Always DT_NEEDED for dynamic libs"));
557 // This should really be an "enum", but it's too easy for folks to
558 // forget to update the list as they add new targets. So we just
559 // accept any string. We'll fail later (when the string is parsed),
560 // if the target isn't actually supported.
561 DEFINE_string(format
, options::TWO_DASHES
, 'b', "elf",
562 N_("Set input format"), ("[elf,binary]"));
564 DEFINE_bool(Bdynamic
, options::ONE_DASH
, '\0', true,
565 N_("-l searches for shared libraries"), NULL
);
566 DEFINE_bool_alias(Bstatic
, Bdynamic
, options::ONE_DASH
, '\0',
567 N_("-l does not search for shared libraries"), NULL
,
570 DEFINE_bool(Bsymbolic
, options::ONE_DASH
, '\0', false,
571 N_("Bind defined symbols locally"), NULL
);
573 DEFINE_bool(Bsymbolic_functions
, options::ONE_DASH
, '\0', false,
574 N_("Bind defined function symbols locally"), NULL
);
576 DEFINE_optional_string(build_id
, options::TWO_DASHES
, '\0', "sha1",
577 N_("Generate build ID note"),
580 DEFINE_bool(check_sections
, options::TWO_DASHES
, '\0', true,
581 N_("Check segment addresses for overlaps (default)"),
582 N_("Do not check segment addresses for overlaps"));
585 DEFINE_enum(compress_debug_sections
, options::TWO_DASHES
, '\0', "none",
586 N_("Compress .debug_* sections in the output file"),
590 DEFINE_enum(compress_debug_sections
, options::TWO_DASHES
, '\0', "none",
591 N_("Compress .debug_* sections in the output file"),
596 DEFINE_bool(define_common
, options::TWO_DASHES
, 'd', false,
597 N_("Define common symbols"),
598 N_("Do not define common symbols"));
599 DEFINE_bool(dc
, options::ONE_DASH
, '\0', false,
600 N_("Alias for -d"), NULL
);
601 DEFINE_bool(dp
, options::ONE_DASH
, '\0', false,
602 N_("Alias for -d"), NULL
);
604 DEFINE_string(debug
, options::TWO_DASHES
, '\0', "",
605 N_("Turn on debugging"),
606 N_("[all,files,script,task][,...]"));
608 DEFINE_special(defsym
, options::TWO_DASHES
, '\0',
609 N_("Define a symbol"), N_("SYMBOL=EXPRESSION"));
611 DEFINE_optional_string(demangle
, options::TWO_DASHES
, '\0', NULL
,
612 N_("Demangle C++ symbols in log messages"),
615 DEFINE_bool(no_demangle
, options::TWO_DASHES
, '\0', false,
616 N_("Do not demangle C++ symbols in log messages"),
619 DEFINE_bool(detect_odr_violations
, options::TWO_DASHES
, '\0', false,
620 N_("Try to detect violations of the One Definition Rule"),
623 DEFINE_string(entry
, options::TWO_DASHES
, 'e', NULL
,
624 N_("Set program start address"), N_("ADDRESS"));
626 DEFINE_bool(export_dynamic
, options::TWO_DASHES
, 'E', false,
627 N_("Export all dynamic symbols"), NULL
);
629 DEFINE_bool(eh_frame_hdr
, options::TWO_DASHES
, '\0', false,
630 N_("Create exception frame header"), NULL
);
632 DEFINE_bool(fatal_warnings
, options::TWO_DASHES
, '\0', false,
633 N_("Treat warnings as errors"),
634 N_("Do not treat warnings as errors"));
636 DEFINE_string(soname
, options::ONE_DASH
, 'h', NULL
,
637 N_("Set shared library name"), N_("FILENAME"));
639 DEFINE_double(hash_bucket_empty_fraction
, options::TWO_DASHES
, '\0', 0.0,
640 N_("Min fraction of empty buckets in dynamic hash"),
643 DEFINE_enum(hash_style
, options::TWO_DASHES
, '\0', "sysv",
644 N_("Dynamic hash style"), N_("[sysv,gnu,both]"),
645 {"sysv", "gnu", "both"});
647 DEFINE_string(dynamic_linker
, options::TWO_DASHES
, 'I', NULL
,
648 N_("Set dynamic linker path"), N_("PROGRAM"));
650 DEFINE_special(just_symbols
, options::TWO_DASHES
, '\0',
651 N_("Read only symbol values from FILE"), N_("FILE"));
653 DEFINE_special(library
, options::TWO_DASHES
, 'l',
654 N_("Search for library LIBNAME"), N_("LIBNAME"));
656 DEFINE_dirlist(library_path
, options::TWO_DASHES
, 'L',
657 N_("Add directory to search path"), N_("DIR"));
659 DEFINE_string(m
, options::EXACTLY_ONE_DASH
, 'm', "",
660 N_("Ignored for compatibility"), N_("EMULATION"));
662 DEFINE_bool(print_map
, options::TWO_DASHES
, 'M', false,
663 N_("Write map file on standard output"), NULL
);
664 DEFINE_string(Map
, options::ONE_DASH
, '\0', NULL
, N_("Write map file"),
667 DEFINE_bool(nmagic
, options::TWO_DASHES
, 'n', false,
668 N_("Do not page align data"), NULL
);
669 DEFINE_bool(omagic
, options::EXACTLY_TWO_DASHES
, 'N', false,
670 N_("Do not page align data, do not make text readonly"),
671 N_("Page align data, make text readonly"));
673 DEFINE_enable(new_dtags
, options::EXACTLY_TWO_DASHES
, '\0', false,
674 N_("Enable use of DT_RUNPATH and DT_FLAGS"),
675 N_("Disable use of DT_RUNPATH and DT_FLAGS"));
677 DEFINE_bool(noinhibit_exec
, options::TWO_DASHES
, '\0', false,
678 N_("Create an output file even if errors occur"), NULL
);
680 DEFINE_bool_alias(no_undefined
, defs
, options::TWO_DASHES
, '\0',
681 N_("Report undefined symbols (even with --shared)"),
684 DEFINE_string(output
, options::TWO_DASHES
, 'o', "a.out",
685 N_("Set output file name"), N_("FILE"));
687 DEFINE_uint(optimize
, options::EXACTLY_ONE_DASH
, 'O', 0,
688 N_("Optimize output file size"), N_("LEVEL"));
690 DEFINE_string(oformat
, options::EXACTLY_TWO_DASHES
, '\0', "elf",
691 N_("Set output format"), N_("[binary]"));
693 #ifdef ENABLE_PLUGINS
694 DEFINE_special(plugin
, options::TWO_DASHES
, '\0',
695 N_("Load a plugin library"), N_("PLUGIN[,ARG,...]"));
698 DEFINE_bool(preread_archive_symbols
, options::TWO_DASHES
, '\0', false,
699 N_("Preread archive symbols when multi-threaded"), NULL
);
701 DEFINE_string(print_symbol_counts
, options::TWO_DASHES
, '\0', NULL
,
702 N_("Print symbols defined and used for each input"),
705 DEFINE_bool(Qy
, options::EXACTLY_ONE_DASH
, '\0', false,
706 N_("Ignored for SVR4 compatibility"), NULL
);
708 DEFINE_bool(emit_relocs
, options::TWO_DASHES
, 'q', false,
709 N_("Generate relocations in output"), NULL
);
711 DEFINE_bool(relocatable
, options::EXACTLY_ONE_DASH
, 'r', false,
712 N_("Generate relocatable output"), NULL
);
714 DEFINE_bool(relax
, options::TWO_DASHES
, '\0', false,
715 N_("Relax branches on certain targets"), NULL
);
717 // -R really means -rpath, but can mean --just-symbols for
718 // compatibility with GNU ld. -rpath is always -rpath, so we list
720 DEFINE_special(R
, options::EXACTLY_ONE_DASH
, 'R',
721 N_("Add DIR to runtime search path"), N_("DIR"));
723 DEFINE_dirlist(rpath
, options::ONE_DASH
, '\0',
724 N_("Add DIR to runtime search path"), N_("DIR"));
726 DEFINE_dirlist(rpath_link
, options::TWO_DASHES
, '\0',
727 N_("Add DIR to link time shared library search path"),
730 DEFINE_bool(strip_all
, options::TWO_DASHES
, 's', false,
731 N_("Strip all symbols"), NULL
);
732 DEFINE_bool(strip_debug
, options::TWO_DASHES
, 'S', false,
733 N_("Strip debugging information"), NULL
);
734 DEFINE_bool(strip_debug_non_line
, options::TWO_DASHES
, '\0', false,
735 N_("Emit only debug line number information"), NULL
);
736 DEFINE_bool(strip_debug_gdb
, options::TWO_DASHES
, '\0', false,
737 N_("Strip debug symbols that are unused by gdb "
738 "(at least versions <= 6.7)"), NULL
);
740 DEFINE_bool(shared
, options::ONE_DASH
, '\0', false,
741 N_("Generate shared library"), NULL
);
743 // This is not actually special in any way, but I need to give it
744 // a non-standard accessor-function name because 'static' is a keyword.
745 DEFINE_special(static, options::ONE_DASH
, '\0',
746 N_("Do not link against shared libraries"), NULL
);
748 DEFINE_bool(stats
, options::TWO_DASHES
, '\0', false,
749 N_("Print resource usage statistics"), NULL
);
751 DEFINE_string(sysroot
, options::TWO_DASHES
, '\0', "",
752 N_("Set target system root directory"), N_("DIR"));
754 DEFINE_bool(trace
, options::TWO_DASHES
, 't', false,
755 N_("Print the name of each input file"), NULL
);
757 DEFINE_special(script
, options::TWO_DASHES
, 'T',
758 N_("Read linker script"), N_("FILE"));
760 DEFINE_bool(threads
, options::TWO_DASHES
, '\0', false,
761 N_("Run the linker multi-threaded"),
762 N_("Do not run the linker multi-threaded"));
763 DEFINE_uint(thread_count
, options::TWO_DASHES
, '\0', 0,
764 N_("Number of threads to use"), N_("COUNT"));
765 DEFINE_uint(thread_count_initial
, options::TWO_DASHES
, '\0', 0,
766 N_("Number of threads to use in initial pass"), N_("COUNT"));
767 DEFINE_uint(thread_count_middle
, options::TWO_DASHES
, '\0', 0,
768 N_("Number of threads to use in middle pass"), N_("COUNT"));
769 DEFINE_uint(thread_count_final
, options::TWO_DASHES
, '\0', 0,
770 N_("Number of threads to use in final pass"), N_("COUNT"));
772 DEFINE_uint64(Tbss
, options::ONE_DASH
, '\0', -1U,
773 N_("Set the address of the bss segment"), N_("ADDRESS"));
774 DEFINE_uint64(Tdata
, options::ONE_DASH
, '\0', -1U,
775 N_("Set the address of the data segment"), N_("ADDRESS"));
776 DEFINE_uint64(Ttext
, options::ONE_DASH
, '\0', -1U,
777 N_("Set the address of the text segment"), N_("ADDRESS"));
779 DEFINE_set(undefined
, options::TWO_DASHES
, 'u',
780 N_("Create undefined reference to SYMBOL"), N_("SYMBOL"));
782 DEFINE_bool(verbose
, options::TWO_DASHES
, '\0', false,
783 N_("Synonym for --debug=files"), NULL
);
785 DEFINE_special(version_script
, options::TWO_DASHES
, '\0',
786 N_("Read version script"), N_("FILE"));
788 DEFINE_bool(whole_archive
, options::TWO_DASHES
, '\0', false,
789 N_("Include all archive contents"),
790 N_("Include only needed archive contents"));
792 DEFINE_set(wrap
, options::TWO_DASHES
, '\0',
793 N_("Use wrapper functions for SYMBOL"), N_("SYMBOL"));
795 DEFINE_set(trace_symbol
, options::TWO_DASHES
, 'y',
796 N_("Trace references to symbol"), N_("SYMBOL"));
798 DEFINE_string(Y
, options::EXACTLY_ONE_DASH
, 'Y', "",
799 N_("Default search path for Solaris compatibility"),
802 DEFINE_special(start_group
, options::TWO_DASHES
, '(',
803 N_("Start a library search group"), NULL
);
804 DEFINE_special(end_group
, options::TWO_DASHES
, ')',
805 N_("End a library search group"), NULL
);
809 DEFINE_bool(combreloc
, options::DASH_Z
, '\0', true,
810 N_("Sort dynamic relocs"),
811 N_("Do not sort dynamic relocs"));
812 DEFINE_uint64(common_page_size
, options::DASH_Z
, '\0', 0,
813 N_("Set common page size to SIZE"), N_("SIZE"));
814 DEFINE_bool(defs
, options::DASH_Z
, '\0', false,
815 N_("Report undefined symbols (even with --shared)"),
817 DEFINE_bool(execstack
, options::DASH_Z
, '\0', false,
818 N_("Mark output as requiring executable stack"), NULL
);
819 DEFINE_uint64(max_page_size
, options::DASH_Z
, '\0', 0,
820 N_("Set maximum page size to SIZE"), N_("SIZE"));
821 DEFINE_bool(noexecstack
, options::DASH_Z
, '\0', false,
822 N_("Mark output as not requiring executable stack"), NULL
);
823 DEFINE_bool(initfirst
, options::DASH_Z
, '\0', false,
824 N_("Mark DSO to be initialized first at runtime"),
826 DEFINE_bool(interpose
, options::DASH_Z
, '\0', false,
827 N_("Mark object to interpose all DSOs but executable"),
829 DEFINE_bool(loadfltr
, options::DASH_Z
, '\0', false,
830 N_("Mark object requiring immediate process"),
832 DEFINE_bool(nodefaultlib
, options::DASH_Z
, '\0', false,
833 N_("Mark object not to use default search paths"),
835 DEFINE_bool(nodelete
, options::DASH_Z
, '\0', false,
836 N_("Mark DSO non-deletable at runtime"),
838 DEFINE_bool(nodlopen
, options::DASH_Z
, '\0', false,
839 N_("Mark DSO not available to dlopen"),
841 DEFINE_bool(nodump
, options::DASH_Z
, '\0', false,
842 N_("Mark DSO not available to dldump"),
844 DEFINE_bool(relro
, options::DASH_Z
, '\0', false,
845 N_("Where possible mark variables read-only after relocation"),
846 N_("Don't mark variables read-only after relocation"));
847 DEFINE_bool(origin
, options::DASH_Z
, '\0', false,
848 N_("Mark DSO to indicate that needs immediate $ORIGIN "
849 "processing at runtime"), NULL
);
852 typedef options::Dir_list Dir_list
;
856 // Does post-processing on flags, making sure they all have
857 // non-conflicting values. Also converts some flags from their
858 // "standard" types (string, etc), to another type (enum, DirList),
859 // which can be accessed via a separate method. Dies if it notices
863 // The macro defines output() (based on --output), but that's a
864 // generic name. Provide this alternative name, which is clearer.
866 output_file_name() const
867 { return this->output(); }
869 // This is not defined via a flag, but combines flags to say whether
870 // the output is position-independent or not.
872 output_is_position_independent() const
873 { return this->shared(); }
875 // Return true if the output is something that can be exec()ed, such
876 // as a static executable, or a position-dependent or
877 // position-independent executable, but not a dynamic library or an
880 output_is_executable() const
881 { return !this->shared() || this->output_is_pie(); }
883 // Return true if the output is a position-independent executable.
884 // This is currently not supported.
886 output_is_pie() const
889 // This would normally be static(), and defined automatically, but
890 // since static is a keyword, we need to come up with our own name.
895 // In addition to getting the input and output formats as a string
896 // (via format() and oformat()), we also give access as an enum.
901 // Straight binary format.
905 // Note: these functions are not very fast.
906 Object_format
format_enum() const;
907 Object_format
oformat_enum() const;
909 // These are the best way to get access to the execstack state,
910 // not execstack() and noexecstack() which are hard to use properly.
912 is_execstack_set() const
913 { return this->execstack_status_
!= EXECSTACK_FROM_INPUT
; }
916 is_stack_executable() const
917 { return this->execstack_status_
== EXECSTACK_YES
; }
919 // The --demangle option takes an optional string, and there is also
920 // a --no-demangle option. This is the best way to decide whether
921 // to demangle or not.
924 { return this->do_demangle_
; }
926 // Returns TRUE if any plugin libraries have been loaded.
929 { return this->plugins_
!= NULL
; }
931 // Return a pointer to the plugin manager.
934 { return this->plugins_
; }
937 // Don't copy this structure.
938 General_options(const General_options
&);
939 General_options
& operator=(const General_options
&);
941 // Whether to mark the stack as executable.
944 // Not set on command line.
945 EXECSTACK_FROM_INPUT
,
946 // Mark the stack as executable (-z execstack).
948 // Mark the stack as not executable (-z noexecstack).
953 set_execstack_status(Execstack value
)
954 { this->execstack_status_
= value
; }
957 set_do_demangle(bool value
)
958 { this->do_demangle_
= value
; }
961 set_static(bool value
)
964 // These are called by finalize() to set up the search-path correctly.
966 add_to_library_path_with_sysroot(const char* arg
)
967 { this->add_search_directory_to_library_path(Search_directory(arg
, true)); }
969 // Apply any sysroot to the directory lists.
973 // Add a plugin and its arguments to the list of plugins.
975 add_plugin(const char* arg
);
977 // Whether to mark the stack as executable.
978 Execstack execstack_status_
;
979 // Whether to do a static link.
981 // Whether to do demangling.
983 // List of plugin libraries.
984 Plugin_manager
* plugins_
;
987 // The position-dependent options. We use this to store the state of
988 // the commandline at a particular point in parsing for later
989 // reference. For instance, if we see "ld --whole-archive foo.a
990 // --no-whole-archive," we want to store the whole-archive option with
991 // foo.a, so when the time comes to parse foo.a we know we should do
992 // it in whole-archive mode. We could store all of General_options,
993 // but that's big, so we just pick the subset of flags that actually
994 // change in a position-dependent way.
996 #define DEFINE_posdep(varname__, type__) \
1000 { return this->varname__##_; } \
1003 set_##varname__(type__ value) \
1004 { this->varname__##_ = value; } \
1008 class Position_dependent_options
1011 Position_dependent_options(const General_options
& options
1012 = Position_dependent_options::default_options_
)
1013 { copy_from_options(options
); }
1015 void copy_from_options(const General_options
& options
)
1017 this->set_as_needed(options
.as_needed());
1018 this->set_Bdynamic(options
.Bdynamic());
1019 this->set_format_enum(options
.format_enum());
1020 this->set_whole_archive(options
.whole_archive());
1023 DEFINE_posdep(as_needed
, bool);
1024 DEFINE_posdep(Bdynamic
, bool);
1025 DEFINE_posdep(format_enum
, General_options::Object_format
);
1026 DEFINE_posdep(whole_archive
, bool);
1029 // This is a General_options with everything set to its default
1030 // value. A Position_dependent_options created with no argument
1031 // will take its values from here.
1032 static General_options default_options_
;
1036 // A single file or library argument from the command line.
1038 class Input_file_argument
1041 // name: file name or library name
1042 // is_lib: true if name is a library name: that is, emits the leading
1043 // "lib" and trailing ".so"/".a" from the name
1044 // extra_search_path: an extra directory to look for the file, prior
1045 // to checking the normal library search path. If this is "",
1046 // then no extra directory is added.
1047 // just_symbols: whether this file only defines symbols.
1048 // options: The position dependent options at this point in the
1049 // command line, such as --whole-archive.
1050 Input_file_argument()
1051 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
1055 Input_file_argument(const char* name
, bool is_lib
,
1056 const char* extra_search_path
,
1058 const Position_dependent_options
& options
)
1059 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
1060 just_symbols_(just_symbols
), options_(options
)
1063 // You can also pass in a General_options instance instead of a
1064 // Position_dependent_options. In that case, we extract the
1065 // position-independent vars from the General_options and only store
1067 Input_file_argument(const char* name
, bool is_lib
,
1068 const char* extra_search_path
,
1070 const General_options
& options
)
1071 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
1072 just_symbols_(just_symbols
), options_(options
)
1077 { return this->name_
.c_str(); }
1079 const Position_dependent_options
&
1081 { return this->options_
; }
1085 { return this->is_lib_
; }
1088 extra_search_path() const
1090 return (this->extra_search_path_
.empty()
1092 : this->extra_search_path_
.c_str());
1095 // Return whether we should only read symbols from this file.
1097 just_symbols() const
1098 { return this->just_symbols_
; }
1100 // Return whether this file may require a search using the -L
1103 may_need_search() const
1104 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
1107 // We use std::string, not const char*, here for convenience when
1108 // using script files, so that we do not have to preserve the string
1112 std::string extra_search_path_
;
1114 Position_dependent_options options_
;
1117 // A file or library, or a group, from the command line.
1119 class Input_argument
1122 // Create a file or library argument.
1123 explicit Input_argument(Input_file_argument file
)
1124 : is_file_(true), file_(file
), group_(NULL
)
1127 // Create a group argument.
1128 explicit Input_argument(Input_file_group
* group
)
1129 : is_file_(false), group_(group
)
1132 // Return whether this is a file.
1135 { return this->is_file_
; }
1137 // Return whether this is a group.
1140 { return !this->is_file_
; }
1142 // Return the information about the file.
1143 const Input_file_argument
&
1146 gold_assert(this->is_file_
);
1150 // Return the information about the group.
1151 const Input_file_group
*
1154 gold_assert(!this->is_file_
);
1155 return this->group_
;
1161 gold_assert(!this->is_file_
);
1162 return this->group_
;
1167 Input_file_argument file_
;
1168 Input_file_group
* group_
;
1171 // A group from the command line. This is a set of arguments within
1172 // --start-group ... --end-group.
1174 class Input_file_group
1177 typedef std::vector
<Input_argument
> Files
;
1178 typedef Files::const_iterator const_iterator
;
1184 // Add a file to the end of the group.
1186 add_file(const Input_file_argument
& arg
)
1187 { this->files_
.push_back(Input_argument(arg
)); }
1189 // Iterators to iterate over the group contents.
1193 { return this->files_
.begin(); }
1197 { return this->files_
.end(); }
1203 // A list of files from the command line or a script.
1205 class Input_arguments
1208 typedef std::vector
<Input_argument
> Input_argument_list
;
1209 typedef Input_argument_list::const_iterator const_iterator
;
1212 : input_argument_list_(), in_group_(false)
1217 add_file(const Input_file_argument
& arg
);
1219 // Start a group (the --start-group option).
1223 // End a group (the --end-group option).
1227 // Return whether we are currently in a group.
1230 { return this->in_group_
; }
1232 // The number of entries in the list.
1235 { return this->input_argument_list_
.size(); }
1237 // Iterators to iterate over the list of input files.
1241 { return this->input_argument_list_
.begin(); }
1245 { return this->input_argument_list_
.end(); }
1247 // Return whether the list is empty.
1250 { return this->input_argument_list_
.empty(); }
1253 Input_argument_list input_argument_list_
;
1258 // All the information read from the command line. These are held in
1259 // three separate structs: one to hold the options (--foo), one to
1260 // hold the filenames listed on the commandline, and one to hold
1261 // linker script information. This third is not a subset of the other
1262 // two because linker scripts can be specified either as options (via
1263 // -T) or as a file.
1268 typedef Input_arguments::const_iterator const_iterator
;
1272 // Process the command line options. This will exit with an
1273 // appropriate error message if an unrecognized option is seen.
1275 process(int argc
, const char** argv
);
1277 // Process one command-line option. This takes the index of argv to
1278 // process, and returns the index for the next option. no_more_options
1279 // is set to true if argv[i] is "--".
1281 process_one_option(int argc
, const char** argv
, int i
,
1282 bool* no_more_options
);
1284 // Get the general options.
1285 const General_options
&
1287 { return this->options_
; }
1289 // Get the position dependent options.
1290 const Position_dependent_options
&
1291 position_dependent_options() const
1292 { return this->position_options_
; }
1294 // Get the linker-script options.
1297 { return this->script_options_
; }
1299 // Get the version-script options: a convenience routine.
1300 const Version_script_info
&
1301 version_script() const
1302 { return *this->script_options_
.version_script_info(); }
1304 // Get the input files.
1307 { return this->inputs_
; }
1309 // The number of input files.
1311 number_of_input_files() const
1312 { return this->inputs_
.size(); }
1314 // Iterators to iterate over the list of input files.
1318 { return this->inputs_
.begin(); }
1322 { return this->inputs_
.end(); }
1325 Command_line(const Command_line
&);
1326 Command_line
& operator=(const Command_line
&);
1328 General_options options_
;
1329 Position_dependent_options position_options_
;
1330 Script_options script_options_
;
1331 Input_arguments inputs_
;
1334 } // End namespace gold.
1336 #endif // !defined(GOLD_OPTIONS_H)