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
;
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.
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.
70 parse_bool(const char* option_name
, const char* arg
, bool* retval
);
73 parse_uint(const char* option_name
, const char* arg
, int* retval
);
76 parse_uint64(const char* option_name
, const char* arg
, uint64_t* retval
);
79 parse_double(const char* option_name
, const char* arg
, double* retval
);
82 parse_string(const char* option_name
, const char* arg
, const char** retval
);
85 parse_optional_string(const char* option_name
, const char* arg
,
89 parse_dirlist(const char* option_name
, const char* arg
, Dir_list
* retval
);
92 parse_set(const char* option_name
, const char* arg
, String_set
* retval
);
95 parse_choices(const char* option_name
, const char* arg
, const char** retval
,
96 const char* choices
[], int num_choices
);
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".
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
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.
134 std::string longname
;
137 const char* default_value
;
138 const char* helpstring
;
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++
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.
165 takes_argument() const
166 { return this->helparg
!= NULL
; }
168 // Whether the argument is optional.
170 takes_optional_argument() const
171 { return this->optional_arg
; }
173 // Register this option with the global list of options.
177 // Print this option to stdout (used with --help).
182 // All options have a Struct_##varname that inherits from this and
183 // actually implements parse_to_value for that option.
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.
194 parse_to_value(const char* option
, const char* arg
,
195 Command_line
* cmdline
, General_options
* options
) = 0;
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*,
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
); }
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__) \
236 { return this->varname__##_.value; } \
239 user_set_##varname__() const \
240 { return this->varname__##_.user_set_via_option; } \
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__) \
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; \
263 Struct_##varname__ varname__##_; \
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 (or, for a -z
272 // option, noVARNAME).
273 #define DEFINE_bool(varname__, dashes__, shortname__, default_value__, \
274 helpstring__, no_helpstring__) \
275 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
276 default_value__ ? "true" : "false", helpstring__, NULL, \
277 false, bool, bool, options::parse_bool) \
278 struct Struct_no_##varname__ : public options::Struct_var \
280 Struct_no_##varname__() : option((dashes__ == options::DASH_Z \
282 : "no-" #varname__), \
284 default_value__ ? "false" : "true", \
285 no_helpstring__, NULL, false, this) \
289 parse_to_value(const char*, const char*, \
290 Command_line*, General_options* options) \
291 { options->set_##varname__(false); } \
293 options::One_option option; \
295 Struct_no_##varname__ no_##varname__##_initializer_
297 #define DEFINE_enable(varname__, dashes__, shortname__, default_value__, \
298 helpstring__, no_helpstring__) \
299 DEFINE_var(enable_##varname__, dashes__, shortname__, default_value__, \
300 default_value__ ? "true" : "false", helpstring__, NULL, \
301 false, bool, bool, options::parse_bool) \
302 struct Struct_disable_##varname__ : public options::Struct_var \
304 Struct_disable_##varname__() : option("disable-" #varname__, \
306 default_value__ ? "false" : "true", \
307 no_helpstring__, NULL, false, this) \
311 parse_to_value(const char*, const char*, \
312 Command_line*, General_options* options) \
313 { options->set_enable_##varname__(false); } \
315 options::One_option option; \
317 Struct_disable_##varname__ disable_##varname__##_initializer_
319 #define DEFINE_uint(varname__, dashes__, shortname__, default_value__, \
320 helpstring__, helparg__) \
321 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
322 #default_value__, helpstring__, helparg__, false, \
323 int, int, options::parse_uint)
325 #define DEFINE_uint64(varname__, dashes__, shortname__, default_value__, \
326 helpstring__, helparg__) \
327 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
328 #default_value__, helpstring__, helparg__, false, \
329 uint64_t, uint64_t, options::parse_uint64)
331 #define DEFINE_double(varname__, dashes__, shortname__, default_value__, \
332 helpstring__, helparg__) \
333 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
334 #default_value__, helpstring__, helparg__, false, \
335 double, double, options::parse_double)
337 #define DEFINE_string(varname__, dashes__, shortname__, default_value__, \
338 helpstring__, helparg__) \
339 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
340 default_value__, helpstring__, helparg__, false, \
341 const char*, const char*, options::parse_string)
343 // This is like DEFINE_string, but we convert each occurrence to a
344 // Search_directory and store it in a vector. Thus we also have the
345 // add_to_VARNAME() method, to append to the vector.
346 #define DEFINE_dirlist(varname__, dashes__, shortname__, \
347 helpstring__, helparg__) \
348 DEFINE_var(varname__, dashes__, shortname__, , \
349 "", helpstring__, helparg__, false, options::Dir_list, \
350 const options::Dir_list&, options::parse_dirlist) \
352 add_to_##varname__(const char* new_value) \
353 { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
355 add_search_directory_to_##varname__(const Search_directory& dir) \
356 { this->varname__##_.value.push_back(dir); }
358 // This is like DEFINE_string, but we store a set of strings.
359 #define DEFINE_set(varname__, dashes__, shortname__, \
360 helpstring__, helparg__) \
361 DEFINE_var(varname__, dashes__, shortname__, , \
362 "", helpstring__, helparg__, false, options::String_set, \
363 const options::String_set&, options::parse_set) \
366 any_##varname__() const \
367 { return !this->varname__##_.value.empty(); } \
369 is_##varname__(const char* symbol) const \
371 return (!this->varname__##_.value.empty() \
372 && (this->varname__##_.value.find(std::string(symbol)) \
373 != this->varname__##_.value.end())); \
376 // When you have a list of possible values (expressed as string)
377 // After helparg__ should come an initializer list, like
378 // {"foo", "bar", "baz"}
379 #define DEFINE_enum(varname__, dashes__, shortname__, default_value__, \
380 helpstring__, helparg__, ...) \
381 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
382 default_value__, helpstring__, helparg__, false, \
383 const char*, const char*, parse_choices_##varname__) \
385 static void parse_choices_##varname__(const char* option_name, \
387 const char** retval) { \
388 const char* choices[] = __VA_ARGS__; \
389 options::parse_choices(option_name, arg, retval, \
390 choices, sizeof(choices) / sizeof(*choices)); \
393 // This is used for non-standard flags. It defines no functions; it
394 // just calls General_options::parse_VARNAME whenever the flag is
395 // seen. We declare parse_VARNAME as a static member of
396 // General_options; you are responsible for defining it there.
397 // helparg__ should be NULL iff this special-option is a boolean.
398 #define DEFINE_special(varname__, dashes__, shortname__, \
399 helpstring__, helparg__) \
401 void parse_##varname__(const char* option, const char* arg, \
402 Command_line* inputs); \
403 struct Struct_##varname__ : public options::Struct_special \
405 Struct_##varname__() \
406 : options::Struct_special(#varname__, dashes__, shortname__, \
407 &General_options::parse_##varname__, \
408 helpstring__, helparg__) \
411 Struct_##varname__ varname__##_initializer_
413 // An option that takes an optional string argument. If the option is
414 // used with no argument, the value will be the default, and
415 // user_set_via_option will be true.
416 #define DEFINE_optional_string(varname__, dashes__, shortname__, \
418 helpstring__, helparg__) \
419 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
420 default_value__, helpstring__, helparg__, true, \
421 const char*, const char*, options::parse_optional_string)
423 // A directory to search. For each directory we record whether it is
424 // in the sysroot. We need to know this so that, if a linker script
425 // is found within the sysroot, we will apply the sysroot to any files
426 // named by that script.
428 class Search_directory
431 // We need a default constructor because we put this in a
434 : name_(NULL
), put_in_sysroot_(false), is_in_sysroot_(false)
437 // This is the usual constructor.
438 Search_directory(const char* name
, bool put_in_sysroot
)
439 : name_(name
), put_in_sysroot_(put_in_sysroot
), is_in_sysroot_(false)
441 if (this->name_
.empty())
445 // This is called if we have a sysroot. The sysroot is prefixed to
446 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
447 // set to true for any enries which are in the sysroot (this will
448 // naturally include any entries for which put_in_sysroot_ is true).
449 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
450 // passing SYSROOT to lrealpath.
452 add_sysroot(const char* sysroot
, const char* canonical_sysroot
);
454 // Get the directory name.
457 { return this->name_
; }
459 // Return whether this directory is in the sysroot.
461 is_in_sysroot() const
462 { return this->is_in_sysroot_
; }
466 bool put_in_sysroot_
;
470 class General_options
473 // NOTE: For every option that you add here, also consider if you
474 // should add it to Position_dependent_options.
475 DEFINE_special(help
, options::TWO_DASHES
, '\0',
476 N_("Report usage information"), NULL
);
477 DEFINE_special(version
, options::TWO_DASHES
, 'v',
478 N_("Report version information"), NULL
);
479 DEFINE_special(V
, options::EXACTLY_ONE_DASH
, '\0',
480 N_("Report version and target information"), NULL
);
482 // These options are sorted approximately so that for each letter in
483 // the alphabet, we show the option whose shortname is that letter
484 // (if any) and then every longname that starts with that letter (in
485 // alphabetical order). For both, lowercase sorts before uppercase.
486 // The -z options come last.
488 DEFINE_bool(allow_shlib_undefined
, options::TWO_DASHES
, '\0', false,
489 N_("Allow unresolved references in shared libraries"),
490 N_("Do not allow unresolved references in shared libraries"));
492 DEFINE_bool(as_needed
, options::TWO_DASHES
, '\0', false,
493 N_("Only set DT_NEEDED for dynamic libs if used"),
494 N_("Always DT_NEEDED for dynamic libs"));
496 // This should really be an "enum", but it's too easy for folks to
497 // forget to update the list as they add new targets. So we just
498 // accept any string. We'll fail later (when the string is parsed),
499 // if the target isn't actually supported.
500 DEFINE_string(format
, options::TWO_DASHES
, 'b', "elf",
501 N_("Set input format"), ("[elf,binary]"));
503 DEFINE_bool(Bdynamic
, options::ONE_DASH
, '\0', true,
504 N_("-l searches for shared libraries"), NULL
);
505 // Bstatic affects the same variable as Bdynamic, so we have to use
506 // the "special" macro to make that happen.
507 DEFINE_special(Bstatic
, options::ONE_DASH
, '\0',
508 N_("-l does not search for shared libraries"), NULL
);
510 DEFINE_bool(Bsymbolic
, options::ONE_DASH
, '\0', false,
511 N_("Bind defined symbols locally"), NULL
);
513 DEFINE_bool(Bsymbolic_functions
, options::ONE_DASH
, '\0', false,
514 N_("Bind defined function symbols locally"), NULL
);
516 DEFINE_optional_string(build_id
, options::TWO_DASHES
, '\0', "sha1",
517 N_("Generate build ID note"),
521 DEFINE_enum(compress_debug_sections
, options::TWO_DASHES
, '\0', "none",
522 N_("Compress .debug_* sections in the output file"),
526 DEFINE_enum(compress_debug_sections
, options::TWO_DASHES
, '\0', "none",
527 N_("Compress .debug_* sections in the output file"),
532 DEFINE_bool(define_common
, options::TWO_DASHES
, 'd', false,
533 N_("Define common symbols"),
534 N_("Do not define common symbols"));
535 DEFINE_bool(dc
, options::ONE_DASH
, '\0', false,
536 N_("Alias for -d"), NULL
);
537 DEFINE_bool(dp
, options::ONE_DASH
, '\0', false,
538 N_("Alias for -d"), NULL
);
540 DEFINE_string(debug
, options::TWO_DASHES
, '\0', "",
541 N_("Turn on debugging"),
542 N_("[all,files,script,task][,...]"));
544 DEFINE_special(defsym
, options::TWO_DASHES
, '\0',
545 N_("Define a symbol"), N_("SYMBOL=EXPRESSION"));
547 DEFINE_optional_string(demangle
, options::TWO_DASHES
, '\0', NULL
,
548 N_("Demangle C++ symbols in log messages"),
551 DEFINE_bool(no_demangle
, options::TWO_DASHES
, '\0', false,
552 N_("Do not demangle C++ symbols in log messages"),
555 DEFINE_bool(detect_odr_violations
, options::TWO_DASHES
, '\0', false,
556 N_("Try to detect violations of the One Definition Rule"),
559 DEFINE_string(entry
, options::TWO_DASHES
, 'e', NULL
,
560 N_("Set program start address"), N_("ADDRESS"));
562 DEFINE_bool(export_dynamic
, options::TWO_DASHES
, 'E', false,
563 N_("Export all dynamic symbols"), NULL
);
565 DEFINE_bool(eh_frame_hdr
, options::TWO_DASHES
, '\0', false,
566 N_("Create exception frame header"), NULL
);
568 DEFINE_bool(fatal_warnings
, options::TWO_DASHES
, '\0', false,
569 N_("Treat warnings as errors"),
570 N_("Do not treat warnings as errors"));
572 DEFINE_string(soname
, options::ONE_DASH
, 'h', NULL
,
573 N_("Set shared library name"), N_("FILENAME"));
575 DEFINE_double(hash_bucket_empty_fraction
, options::TWO_DASHES
, '\0', 0.0,
576 N_("Min fraction of empty buckets in dynamic hash"),
579 DEFINE_enum(hash_style
, options::TWO_DASHES
, '\0', "sysv",
580 N_("Dynamic hash style"), N_("[sysv,gnu,both]"),
581 {"sysv", "gnu", "both"});
583 DEFINE_string(dynamic_linker
, options::TWO_DASHES
, 'I', NULL
,
584 N_("Set dynamic linker path"), N_("PROGRAM"));
586 DEFINE_special(just_symbols
, options::TWO_DASHES
, '\0',
587 N_("Read only symbol values from FILE"), N_("FILE"));
589 DEFINE_special(library
, options::TWO_DASHES
, 'l',
590 N_("Search for library LIBNAME"), N_("LIBNAME"));
592 DEFINE_dirlist(library_path
, options::TWO_DASHES
, 'L',
593 N_("Add directory to search path"), N_("DIR"));
595 DEFINE_string(m
, options::EXACTLY_ONE_DASH
, 'm', "",
596 N_("Ignored for compatibility"), N_("EMULATION"));
598 DEFINE_enable(new_dtags
, options::EXACTLY_TWO_DASHES
, '\0', false,
599 N_("Enable use of DT_RUNPATH and DT_FLAGS"),
600 N_("Disable use of DT_RUNPATH and DT_FLAGS"));
602 DEFINE_bool(noinhibit_exec
, options::TWO_DASHES
, '\0', false,
603 N_("Create an output file even if errors occur"), NULL
);
605 DEFINE_string(output
, options::TWO_DASHES
, 'o', "a.out",
606 N_("Set output file name"), N_("FILE"));
608 DEFINE_uint(optimize
, options::EXACTLY_ONE_DASH
, 'O', 0,
609 N_("Optimize output file size"), N_("LEVEL"));
611 DEFINE_string(oformat
, options::EXACTLY_TWO_DASHES
, '\0', "elf",
612 N_("Set output format"), N_("[binary]"));
614 DEFINE_bool(Qy
, options::EXACTLY_ONE_DASH
, '\0', false,
615 N_("Ignored for SVR4 compatibility"), NULL
);
617 DEFINE_bool(emit_relocs
, options::TWO_DASHES
, 'q', false,
618 N_("Generate relocations in output"), NULL
);
620 DEFINE_bool(relocatable
, options::EXACTLY_ONE_DASH
, 'r', false,
621 N_("Generate relocatable output"), NULL
);
623 DEFINE_bool(relax
, options::TWO_DASHES
, '\0', false,
624 N_("Relax branches on certain targets"), NULL
);
626 // -R really means -rpath, but can mean --just-symbols for
627 // compatibility with GNU ld. -rpath is always -rpath, so we list
629 DEFINE_special(R
, options::EXACTLY_ONE_DASH
, 'R',
630 N_("Add DIR to runtime search path"), N_("DIR"));
632 DEFINE_dirlist(rpath
, options::ONE_DASH
, '\0',
633 N_("Add DIR to runtime search path"), N_("DIR"));
635 DEFINE_dirlist(rpath_link
, options::TWO_DASHES
, '\0',
636 N_("Add DIR to link time shared library search path"),
639 DEFINE_bool(strip_all
, options::TWO_DASHES
, 's', false,
640 N_("Strip all symbols"), NULL
);
641 DEFINE_bool(strip_debug
, options::TWO_DASHES
, 'S', false,
642 N_("Strip debugging information"), NULL
);
643 DEFINE_bool(strip_debug_gdb
, options::TWO_DASHES
, '\0', false,
644 N_("Strip debug symbols that are unused by gdb "
645 "(at least versions <= 6.7)"), NULL
);
647 DEFINE_bool(shared
, options::ONE_DASH
, '\0', false,
648 N_("Generate shared library"), NULL
);
650 // This is not actually special in any way, but I need to give it
651 // a non-standard accessor-function name because 'static' is a keyword.
652 DEFINE_special(static, options::ONE_DASH
, '\0',
653 N_("Do not link against shared libraries"), NULL
);
655 DEFINE_bool(stats
, options::TWO_DASHES
, '\0', false,
656 N_("Print resource usage statistics"), NULL
);
658 DEFINE_string(sysroot
, options::TWO_DASHES
, '\0', "",
659 N_("Set target system root directory"), N_("DIR"));
661 DEFINE_bool(trace
, options::TWO_DASHES
, 't', false,
662 N_("Print the name of each input file"), NULL
);
664 DEFINE_special(script
, options::TWO_DASHES
, 'T',
665 N_("Read linker script"), N_("FILE"));
667 DEFINE_bool(threads
, options::TWO_DASHES
, '\0', false,
668 N_("Run the linker multi-threaded"),
669 N_("Do not run the linker multi-threaded"));
670 DEFINE_uint(thread_count
, options::TWO_DASHES
, '\0', 0,
671 N_("Number of threads to use"), N_("COUNT"));
672 DEFINE_uint(thread_count_initial
, options::TWO_DASHES
, '\0', 0,
673 N_("Number of threads to use in initial pass"), N_("COUNT"));
674 DEFINE_uint(thread_count_middle
, options::TWO_DASHES
, '\0', 0,
675 N_("Number of threads to use in middle pass"), N_("COUNT"));
676 DEFINE_uint(thread_count_final
, options::TWO_DASHES
, '\0', 0,
677 N_("Number of threads to use in final pass"), N_("COUNT"));
679 DEFINE_uint64(Tbss
, options::ONE_DASH
, '\0', -1U,
680 N_("Set the address of the bss segment"), N_("ADDRESS"));
681 DEFINE_uint64(Tdata
, options::ONE_DASH
, '\0', -1U,
682 N_("Set the address of the data segment"), N_("ADDRESS"));
683 DEFINE_uint64(Ttext
, options::ONE_DASH
, '\0', -1U,
684 N_("Set the address of the text segment"), N_("ADDRESS"));
686 DEFINE_bool(verbose
, options::TWO_DASHES
, '\0', false,
687 N_("Synonym for --debug=files"), NULL
);
689 DEFINE_special(version_script
, options::TWO_DASHES
, '\0',
690 N_("Read version script"), N_("FILE"));
692 DEFINE_bool(whole_archive
, options::TWO_DASHES
, '\0', false,
693 N_("Include all archive contents"),
694 N_("Include only needed archive contents"));
696 DEFINE_set(wrap
, options::TWO_DASHES
, '\0',
697 N_("Use wrapper functions for SYMBOL"), N_("SYMBOL"));
699 DEFINE_set(trace_symbol
, options::TWO_DASHES
, 'y',
700 N_("Trace references to symbol"), N_("SYMBOL"));
702 DEFINE_string(Y
, options::EXACTLY_ONE_DASH
, 'Y', "",
703 N_("Default search path for Solaris compatibility"),
706 DEFINE_special(start_group
, options::TWO_DASHES
, '(',
707 N_("Start a library search group"), NULL
);
708 DEFINE_special(end_group
, options::TWO_DASHES
, ')',
709 N_("End a library search group"), NULL
);
713 DEFINE_bool(combreloc
, options::DASH_Z
, '\0', true,
714 N_("Sort dynamic relocs"),
715 N_("Do not sort dynamic relocs"));
716 DEFINE_uint64(common_page_size
, options::DASH_Z
, '\0', 0,
717 N_("Set common page size to SIZE"), N_("SIZE"));
718 DEFINE_bool(defs
, options::DASH_Z
, '\0', false,
719 N_("Report undefined symbols (even with --shared)"),
721 DEFINE_bool(execstack
, options::DASH_Z
, '\0', false,
722 N_("Mark output as requiring executable stack"), NULL
);
723 DEFINE_uint64(max_page_size
, options::DASH_Z
, '\0', 0,
724 N_("Set maximum page size to SIZE"), N_("SIZE"));
725 DEFINE_bool(noexecstack
, options::DASH_Z
, '\0', false,
726 N_("Mark output as not requiring executable stack"), NULL
);
727 DEFINE_bool(initfirst
, options::DASH_Z
, '\0', false,
728 N_("Mark DSO to be initialized first at runtime"),
730 DEFINE_bool(interpose
, options::DASH_Z
, '\0', false,
731 N_("Mark object to interpose all DSOs but executable"),
733 DEFINE_bool(loadfltr
, options::DASH_Z
, '\0', false,
734 N_("Mark object requiring immediate process"),
736 DEFINE_bool(nodefaultlib
, options::DASH_Z
, '\0', false,
737 N_("Mark object not to use default search paths"),
739 DEFINE_bool(nodelete
, options::DASH_Z
, '\0', false,
740 N_("Mark DSO non-deletable at runtime"),
742 DEFINE_bool(nodlopen
, options::DASH_Z
, '\0', false,
743 N_("Mark DSO not available to dlopen"),
745 DEFINE_bool(nodump
, options::DASH_Z
, '\0', false,
746 N_("Mark DSO not available to dldump"),
750 typedef options::Dir_list Dir_list
;
754 // Does post-processing on flags, making sure they all have
755 // non-conflicting values. Also converts some flags from their
756 // "standard" types (string, etc), to another type (enum, DirList),
757 // which can be accessed via a separate method. Dies if it notices
761 // The macro defines output() (based on --output), but that's a
762 // generic name. Provide this alternative name, which is clearer.
764 output_file_name() const
765 { return this->output(); }
767 // This is not defined via a flag, but combines flags to say whether
768 // the output is position-independent or not.
770 output_is_position_independent() const
771 { return this->shared(); }
773 // This would normally be static(), and defined automatically, but
774 // since static is a keyword, we need to come up with our own name.
779 // In addition to getting the input and output formats as a string
780 // (via format() and oformat()), we also give access as an enum.
785 // Straight binary format.
789 // Note: these functions are not very fast.
790 Object_format
format_enum() const;
791 Object_format
oformat_enum() const;
793 // These are the best way to get access to the execstack state,
794 // not execstack() and noexecstack() which are hard to use properly.
796 is_execstack_set() const
797 { return this->execstack_status_
!= EXECSTACK_FROM_INPUT
; }
800 is_stack_executable() const
801 { return this->execstack_status_
== EXECSTACK_YES
; }
803 // The --demangle option takes an optional string, and there is also
804 // a --no-demangle option. This is the best way to decide whether
805 // to demangle or not.
808 { return this->do_demangle_
; }
811 // Don't copy this structure.
812 General_options(const General_options
&);
813 General_options
& operator=(const General_options
&);
815 // Whether to mark the stack as executable.
818 // Not set on command line.
819 EXECSTACK_FROM_INPUT
,
820 // Mark the stack as executable (-z execstack).
822 // Mark the stack as not executable (-z noexecstack).
827 set_execstack_status(Execstack value
)
828 { this->execstack_status_
= value
; }
831 set_do_demangle(bool value
)
832 { this->do_demangle_
= value
; }
835 set_static(bool value
)
838 // These are called by finalize() to set up the search-path correctly.
840 add_to_library_path_with_sysroot(const char* arg
)
841 { this->add_search_directory_to_library_path(Search_directory(arg
, true)); }
843 // Apply any sysroot to the directory lists.
847 // Whether to mark the stack as executable.
848 Execstack execstack_status_
;
849 // Whether to do a static link.
851 // Whether to do demangling.
855 // The position-dependent options. We use this to store the state of
856 // the commandline at a particular point in parsing for later
857 // reference. For instance, if we see "ld --whole-archive foo.a
858 // --no-whole-archive," we want to store the whole-archive option with
859 // foo.a, so when the time comes to parse foo.a we know we should do
860 // it in whole-archive mode. We could store all of General_options,
861 // but that's big, so we just pick the subset of flags that actually
862 // change in a position-dependent way.
864 #define DEFINE_posdep(varname__, type__) \
868 { return this->varname__##_; } \
871 set_##varname__(type__ value) \
872 { this->varname__##_ = value; } \
876 class Position_dependent_options
879 Position_dependent_options(const General_options
& options
880 = Position_dependent_options::default_options_
)
881 { copy_from_options(options
); }
883 void copy_from_options(const General_options
& options
)
885 this->set_as_needed(options
.as_needed());
886 this->set_Bdynamic(options
.Bdynamic());
887 this->set_format_enum(options
.format_enum());
888 this->set_whole_archive(options
.whole_archive());
891 DEFINE_posdep(as_needed
, bool);
892 DEFINE_posdep(Bdynamic
, bool);
893 DEFINE_posdep(format_enum
, General_options::Object_format
);
894 DEFINE_posdep(whole_archive
, bool);
897 // This is a General_options with everything set to its default
898 // value. A Position_dependent_options created with no argument
899 // will take its values from here.
900 static General_options default_options_
;
904 // A single file or library argument from the command line.
906 class Input_file_argument
909 // name: file name or library name
910 // is_lib: true if name is a library name: that is, emits the leading
911 // "lib" and trailing ".so"/".a" from the name
912 // extra_search_path: an extra directory to look for the file, prior
913 // to checking the normal library search path. If this is "",
914 // then no extra directory is added.
915 // just_symbols: whether this file only defines symbols.
916 // options: The position dependent options at this point in the
917 // command line, such as --whole-archive.
918 Input_file_argument()
919 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
923 Input_file_argument(const char* name
, bool is_lib
,
924 const char* extra_search_path
,
926 const Position_dependent_options
& options
)
927 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
928 just_symbols_(just_symbols
), options_(options
)
931 // You can also pass in a General_options instance instead of a
932 // Position_dependent_options. In that case, we extract the
933 // position-independent vars from the General_options and only store
935 Input_file_argument(const char* name
, bool is_lib
,
936 const char* extra_search_path
,
938 const General_options
& options
)
939 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
940 just_symbols_(just_symbols
), options_(options
)
945 { return this->name_
.c_str(); }
947 const Position_dependent_options
&
949 { return this->options_
; }
953 { return this->is_lib_
; }
956 extra_search_path() const
958 return (this->extra_search_path_
.empty()
960 : this->extra_search_path_
.c_str());
963 // Return whether we should only read symbols from this file.
966 { return this->just_symbols_
; }
968 // Return whether this file may require a search using the -L
971 may_need_search() const
972 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
975 // We use std::string, not const char*, here for convenience when
976 // using script files, so that we do not have to preserve the string
980 std::string extra_search_path_
;
982 Position_dependent_options options_
;
985 // A file or library, or a group, from the command line.
990 // Create a file or library argument.
991 explicit Input_argument(Input_file_argument file
)
992 : is_file_(true), file_(file
), group_(NULL
)
995 // Create a group argument.
996 explicit Input_argument(Input_file_group
* group
)
997 : is_file_(false), group_(group
)
1000 // Return whether this is a file.
1003 { return this->is_file_
; }
1005 // Return whether this is a group.
1008 { return !this->is_file_
; }
1010 // Return the information about the file.
1011 const Input_file_argument
&
1014 gold_assert(this->is_file_
);
1018 // Return the information about the group.
1019 const Input_file_group
*
1022 gold_assert(!this->is_file_
);
1023 return this->group_
;
1029 gold_assert(!this->is_file_
);
1030 return this->group_
;
1035 Input_file_argument file_
;
1036 Input_file_group
* group_
;
1039 // A group from the command line. This is a set of arguments within
1040 // --start-group ... --end-group.
1042 class Input_file_group
1045 typedef std::vector
<Input_argument
> Files
;
1046 typedef Files::const_iterator const_iterator
;
1052 // Add a file to the end of the group.
1054 add_file(const Input_file_argument
& arg
)
1055 { this->files_
.push_back(Input_argument(arg
)); }
1057 // Iterators to iterate over the group contents.
1061 { return this->files_
.begin(); }
1065 { return this->files_
.end(); }
1071 // A list of files from the command line or a script.
1073 class Input_arguments
1076 typedef std::vector
<Input_argument
> Input_argument_list
;
1077 typedef Input_argument_list::const_iterator const_iterator
;
1080 : input_argument_list_(), in_group_(false)
1085 add_file(const Input_file_argument
& arg
);
1087 // Start a group (the --start-group option).
1091 // End a group (the --end-group option).
1095 // Return whether we are currently in a group.
1098 { return this->in_group_
; }
1100 // The number of entries in the list.
1103 { return this->input_argument_list_
.size(); }
1105 // Iterators to iterate over the list of input files.
1109 { return this->input_argument_list_
.begin(); }
1113 { return this->input_argument_list_
.end(); }
1115 // Return whether the list is empty.
1118 { return this->input_argument_list_
.empty(); }
1121 Input_argument_list input_argument_list_
;
1126 // All the information read from the command line. These are held in
1127 // three separate structs: one to hold the options (--foo), one to
1128 // hold the filenames listed on the commandline, and one to hold
1129 // linker script information. This third is not a subset of the other
1130 // two because linker scripts can be specified either as options (via
1131 // -T) or as a file.
1136 typedef Input_arguments::const_iterator const_iterator
;
1140 // Process the command line options. This will exit with an
1141 // appropriate error message if an unrecognized option is seen.
1143 process(int argc
, const char** argv
);
1145 // Process one command-line option. This takes the index of argv to
1146 // process, and returns the index for the next option. no_more_options
1147 // is set to true if argv[i] is "--".
1149 process_one_option(int argc
, const char** argv
, int i
,
1150 bool* no_more_options
);
1152 // Get the general options.
1153 const General_options
&
1155 { return this->options_
; }
1157 // Get the position dependent options.
1158 const Position_dependent_options
&
1159 position_dependent_options() const
1160 { return this->position_options_
; }
1162 // Get the linker-script options.
1165 { return this->script_options_
; }
1167 // Get the version-script options: a convenience routine.
1168 const Version_script_info
&
1169 version_script() const
1170 { return *this->script_options_
.version_script_info(); }
1172 // Get the input files.
1175 { return this->inputs_
; }
1177 // The number of input files.
1179 number_of_input_files() const
1180 { return this->inputs_
.size(); }
1182 // Iterators to iterate over the list of input files.
1186 { return this->inputs_
.begin(); }
1190 { return this->inputs_
.end(); }
1193 Command_line(const Command_line
&);
1194 Command_line
& operator=(const Command_line
&);
1196 General_options options_
;
1197 Position_dependent_options position_options_
;
1198 Script_options script_options_
;
1199 Input_arguments inputs_
;
1202 } // End namespace gold.
1204 #endif // !defined(GOLD_OPTIONS_H)