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
51 class General_options
;
52 class Search_directory
;
53 class Input_file_group
;
54 class Position_dependent_options
;
57 // The nested namespace is to contain all the global variables and
58 // structs that need to be defined in the .h file, but do not need to
59 // be used outside this class.
62 typedef std::vector
<Search_directory
> Dir_list
;
64 // These routines convert from a string option to various types.
65 // Each gives a fatal error if it cannot parse the argument.
68 parse_bool(const char* option_name
, const char* arg
, bool* retval
);
71 parse_uint(const char* option_name
, const char* arg
, int* retval
);
74 parse_uint64(const char* option_name
, const char* arg
, uint64_t* retval
);
77 parse_double(const char* option_name
, const char* arg
, double* retval
);
80 parse_string(const char* option_name
, const char* arg
, const char** retval
);
83 parse_dirlist(const char* option_name
, const char* arg
, Dir_list
* retval
);
86 parse_choices(const char* option_name
, const char* arg
, const char** retval
,
87 const char* choices
[], int num_choices
);
91 // Most options have both a shortname (one letter) and a longname.
92 // This enum controls how many dashes are expected for longname access
93 // -- shortnames always use one dash. Most longnames will accept
94 // either one dash or two; the only difference between ONE_DASH and
95 // TWO_DASHES is how we print the option in --help. However, some
96 // longnames require two dashes, and some require only one. The
97 // special value DASH_Z means that the option is preceded by "-z".
100 ONE_DASH
, TWO_DASHES
, EXACTLY_ONE_DASH
, EXACTLY_TWO_DASHES
, DASH_Z
103 // LONGNAME is the long-name of the option with dashes converted to
104 // underscores, or else the short-name if the option has no long-name.
105 // It is never the empty string.
106 // DASHES is an instance of the Dashes enum: ONE_DASH, TWO_DASHES, etc.
107 // SHORTNAME is the short-name of the option, as a char, or '\0' if the
108 // option has no short-name. If the option has no long-name, you
109 // should specify the short-name in *both* VARNAME and here.
110 // DEFAULT_VALUE is the value of the option if not specified on the
111 // commandline, as a string.
112 // HELPSTRING is the descriptive text used with the option via --help
113 // HELPARG is how you define the argument to the option.
114 // --help output is "-shortname HELPARG, --longname HELPARG: HELPSTRING"
115 // HELPARG should be NULL iff the option is a bool and takes no arg.
116 // READER provides parse_to_value, which is a function that will convert
117 // a char* argument into the proper type and store it in some variable.
118 // A One_option struct initializes itself with the global list of options
119 // at constructor time, so be careful making one of these.
122 std::string longname
;
125 const char* default_value
;
126 const char* helpstring
;
130 One_option(const char* ln
, Dashes d
, char sn
, const char* dv
,
131 const char* hs
, const char* ha
, Struct_var
* r
)
132 : longname(ln
), dashes(d
), shortname(sn
), default_value(dv
? dv
: ""),
133 helpstring(hs
), helparg(ha
), reader(r
)
135 // In longname, we convert all underscores to dashes, since GNU
136 // style uses dashes in option names. longname is likely to have
137 // underscores in it because it's also used to declare a C++
139 const char* pos
= strchr(this->longname
.c_str(), '_');
140 for (; pos
; pos
= strchr(pos
, '_'))
141 this->longname
[pos
- this->longname
.c_str()] = '-';
143 // We only register ourselves if our helpstring is not NULL. This
144 // is to support the "no-VAR" boolean variables, which we
145 // conditionally turn on by defining "no-VAR" help text.
146 if (this->helpstring
)
147 this->register_option();
150 // This option takes an argument iff helparg is not NULL.
152 takes_argument() const
153 { return this->helparg
!= NULL
; }
155 // Register this option with the global list of options.
159 // Print this option to stdout (used with --help).
164 // All options have a Struct_##varname that inherits from this and
165 // actually implements parse_to_value for that option.
168 // OPTION: the name of the option as specified on the commandline,
169 // including leading dashes, and any text following the option:
170 // "-O", "--defsym=mysym=0x1000", etc.
171 // ARG: the arg associated with this option, or NULL if the option
172 // takes no argument: "2", "mysym=0x1000", etc.
173 // CMDLINE: the global Command_line object. Used by DEFINE_special.
174 // OPTIONS: the global General_options object. Used by DEFINE_special.
176 parse_to_value(const char* option
, const char* arg
,
177 Command_line
* cmdline
, General_options
* options
) = 0;
179 ~Struct_var() // To make gcc happy.
183 // This is for "special" options that aren't of any predefined type.
184 struct Struct_special
: public Struct_var
186 // If you change this, change the parse-fn in DEFINE_special as well.
187 typedef void (General_options::*Parse_function
)(const char*, const char*,
189 Struct_special(const char* varname
, Dashes dashes
, char shortname
,
190 Parse_function parse_function
,
191 const char* helpstring
, const char* helparg
)
192 : option(varname
, dashes
, shortname
, "", helpstring
, helparg
, this),
193 parse(parse_function
)
196 void parse_to_value(const char* option
, const char* arg
,
197 Command_line
* cmdline
, General_options
* options
)
198 { (options
->*(this->parse
))(option
, arg
, cmdline
); }
201 Parse_function parse
;
204 } // End namespace options.
207 // These are helper macros use by DEFINE_uint64/etc below.
208 // This macro is used inside the General_options_ class, so defines
209 // var() and set_var() as General_options methods. Arguments as are
210 // for the constructor for One_option. param_type__ is the same as
211 // type__ for built-in types, and "const type__ &" otherwise.
212 #define DEFINE_var(varname__, dashes__, shortname__, default_value__, \
213 default_value_as_string__, helpstring__, helparg__, \
214 type__, param_type__, parse_fn__) \
218 { return this->varname__##_.value; } \
221 user_set_##varname__() const \
222 { return this->varname__##_.user_set_via_option; } \
225 struct Struct_##varname__ : public options::Struct_var \
227 Struct_##varname__() \
228 : option(#varname__, dashes__, shortname__, default_value_as_string__, \
229 helpstring__, helparg__, this), \
230 user_set_via_option(false), value(default_value__) \
234 parse_to_value(const char* option_name, const char* arg, \
235 Command_line*, General_options*) \
237 parse_fn__(option_name, arg, &this->value); \
238 this->user_set_via_option = true; \
241 options::One_option option; \
242 bool user_set_via_option; \
245 Struct_##varname__ varname__##_; \
247 set_##varname__(param_type__ value) \
248 { this->varname__##_.value = value; }
250 // These macros allow for easy addition of a new commandline option.
252 // If no_helpstring__ is not NULL, then in addition to creating
253 // VARNAME, we also create an option called no-VARNAME.
254 #define DEFINE_bool(varname__, dashes__, shortname__, default_value__, \
255 helpstring__, no_helpstring__) \
256 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
257 default_value__ ? "true" : "false", helpstring__, NULL, \
258 bool, bool, options::parse_bool) \
259 struct Struct_no_##varname__ : public options::Struct_var \
261 Struct_no_##varname__() : option("no-" #varname__, dashes__, '\0', \
262 default_value__ ? "false" : "true", \
263 no_helpstring__, NULL, this) \
267 parse_to_value(const char*, const char*, \
268 Command_line*, General_options* options) \
269 { options->set_##varname__(false); } \
271 options::One_option option; \
273 Struct_no_##varname__ no_##varname__##_initializer_
275 #define DEFINE_uint(varname__, dashes__, shortname__, default_value__, \
276 helpstring__, helparg__) \
277 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
278 #default_value__, helpstring__, helparg__, \
279 int, int, options::parse_uint)
281 #define DEFINE_uint64(varname__, dashes__, shortname__, default_value__, \
282 helpstring__, helparg__) \
283 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
284 #default_value__, helpstring__, helparg__, \
285 uint64_t, uint64_t, options::parse_uint64)
287 #define DEFINE_double(varname__, dashes__, shortname__, default_value__, \
288 helpstring__, helparg__) \
289 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
290 #default_value__, helpstring__, helparg__, \
291 double, double, options::parse_double)
293 #define DEFINE_string(varname__, dashes__, shortname__, default_value__, \
294 helpstring__, helparg__) \
295 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
296 default_value__, helpstring__, helparg__, \
297 const char*, const char*, options::parse_string)
299 // This is like DEFINE_string, but we convert each occurrence to a
300 // Search_directory and store it in a vector. Thus we also have the
301 // add_to_VARNAME() method, to append to the vector.
302 #define DEFINE_dirlist(varname__, dashes__, shortname__, \
303 helpstring__, helparg__) \
304 DEFINE_var(varname__, dashes__, shortname__, , \
305 "", helpstring__, helparg__, options::Dir_list, \
306 const options::Dir_list&, options::parse_dirlist) \
308 add_to_##varname__(const char* new_value) \
309 { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
311 add_search_directory_to_##varname__(const Search_directory& dir) \
312 { this->varname__##_.value.push_back(dir); }
314 // When you have a list of possible values (expressed as string)
315 // After helparg__ should come an initializer list, like
316 // {"foo", "bar", "baz"}
317 #define DEFINE_enum(varname__, dashes__, shortname__, default_value__, \
318 helpstring__, helparg__, ...) \
319 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
320 default_value__, helpstring__, helparg__, \
321 const char*, const char*, parse_choices_##varname__) \
323 static void parse_choices_##varname__(const char* option_name, \
325 const char** retval) { \
326 const char* choices[] = __VA_ARGS__; \
327 options::parse_choices(option_name, arg, retval, \
328 choices, sizeof(choices) / sizeof(*choices)); \
331 // This is used for non-standard flags. It defines no functions; it
332 // just calls General_options::parse_VARNAME whenever the flag is
333 // seen. We declare parse_VARNAME as a static member of
334 // General_options; you are responsible for defining it there.
335 // helparg__ should be NULL iff this special-option is a boolean.
336 #define DEFINE_special(varname__, dashes__, shortname__, \
337 helpstring__, helparg__) \
339 void parse_##varname__(const char* option, const char* arg, \
340 Command_line* inputs); \
341 struct Struct_##varname__ : public options::Struct_special \
343 Struct_##varname__() \
344 : options::Struct_special(#varname__, dashes__, shortname__, \
345 &General_options::parse_##varname__, \
346 helpstring__, helparg__) \
349 Struct_##varname__ varname__##_initializer_
352 // A directory to search. For each directory we record whether it is
353 // in the sysroot. We need to know this so that, if a linker script
354 // is found within the sysroot, we will apply the sysroot to any files
355 // named by that script.
357 class Search_directory
360 // We need a default constructor because we put this in a
363 : name_(NULL
), put_in_sysroot_(false), is_in_sysroot_(false)
366 // This is the usual constructor.
367 Search_directory(const char* name
, bool put_in_sysroot
)
368 : name_(name
), put_in_sysroot_(put_in_sysroot
), is_in_sysroot_(false)
370 if (this->name_
.empty())
374 // This is called if we have a sysroot. The sysroot is prefixed to
375 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
376 // set to true for any enries which are in the sysroot (this will
377 // naturally include any entries for which put_in_sysroot_ is true).
378 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
379 // passing SYSROOT to lrealpath.
381 add_sysroot(const char* sysroot
, const char* canonical_sysroot
);
383 // Get the directory name.
386 { return this->name_
; }
388 // Return whether this directory is in the sysroot.
390 is_in_sysroot() const
391 { return this->is_in_sysroot_
; }
395 bool put_in_sysroot_
;
399 class General_options
402 // NOTE: For every option that you add here, also consider if you
403 // should add it to Position_dependent_options.
404 DEFINE_special(help
, options::TWO_DASHES
, '\0',
405 N_("Report usage information"), NULL
);
406 DEFINE_special(version
, options::TWO_DASHES
, 'v',
407 N_("Report version information"), NULL
);
409 // These options are sorted approximately so that for each letter in
410 // the alphabet, we show the option whose shortname is that letter
411 // (if any) and then every longname that starts with that letter (in
412 // alphabetical order). For both, lowercase sorts before uppercase.
413 // The -z options come last.
415 DEFINE_bool(allow_shlib_undefined
, options::TWO_DASHES
, '\0', false,
416 N_("Allow unresolved references in shared libraries"),
417 N_("Do not allow unresolved references in shared libraries"));
419 DEFINE_bool(as_needed
, options::TWO_DASHES
, '\0', false,
420 N_("Only set DT_NEEDED for dynamic libs if used"),
421 N_("Always DT_NEEDED for dynamic libs"));
423 // This should really be an "enum", but it's too easy for folks to
424 // forget to update the list as they add new targets. So we just
425 // accept any string. We'll fail later (when the string is parsed),
426 // if the target isn't actually supported.
427 DEFINE_string(format
, options::TWO_DASHES
, 'b', "elf",
428 N_("Set input format"), ("[elf,binary]"));
430 DEFINE_bool(Bdynamic
, options::ONE_DASH
, '\0', true,
431 N_("-l searches for shared libraries"), NULL
);
432 // Bstatic affects the same variable as Bdynamic, so we have to use
433 // the "special" macro to make that happen.
434 DEFINE_special(Bstatic
, options::ONE_DASH
, '\0',
435 N_("-l does not search for shared libraries"), NULL
);
437 DEFINE_bool(Bsymbolic
, options::ONE_DASH
, '\0', false,
438 N_("Bind defined symbols locally"), NULL
);
441 DEFINE_enum(compress_debug_sections
, options::TWO_DASHES
, '\0', "none",
442 N_("Compress .debug_* sections in the output file"),
446 DEFINE_enum(compress_debug_sections
, options::TWO_DASHES
, '\0', "none",
447 N_("Compress .debug_* sections in the output file"),
452 DEFINE_bool(define_common
, options::TWO_DASHES
, 'd', false,
453 N_("Define common symbols"),
454 N_("Do not define common symbols"));
455 DEFINE_bool(dc
, options::ONE_DASH
, '\0', false,
456 N_("Alias for -d"), NULL
);
457 DEFINE_bool(dp
, options::ONE_DASH
, '\0', false,
458 N_("Alias for -d"), NULL
);
460 DEFINE_string(debug
, options::TWO_DASHES
, '\0', "",
461 N_("Turn on debugging"), N_("[task,script,all][,...]"));
463 DEFINE_special(defsym
, options::TWO_DASHES
, '\0',
464 N_("Define a symbol"), N_("SYMBOL=EXPRESSION"));
466 DEFINE_bool(demangle
, options::TWO_DASHES
, '\0',
467 getenv("COLLECT_NO_DEMANGLE") == NULL
,
468 N_("Demangle C++ symbols in log messages"),
469 N_("Do not demangle C++ symbols in log messages"));
471 DEFINE_bool(detect_odr_violations
, options::TWO_DASHES
, '\0', false,
472 N_("Try to detect violations of the One Definition Rule"),
475 DEFINE_string(entry
, options::TWO_DASHES
, 'e', NULL
,
476 N_("Set program start address"), N_("ADDRESS"));
478 DEFINE_bool(export_dynamic
, options::TWO_DASHES
, 'E', false,
479 N_("Export all dynamic symbols"), NULL
);
481 DEFINE_bool(eh_frame_hdr
, options::TWO_DASHES
, '\0', false,
482 N_("Create exception frame header"), NULL
);
484 DEFINE_string(soname
, options::ONE_DASH
, 'h', NULL
,
485 N_("Set shared library name"), N_("FILENAME"));
487 DEFINE_double(hash_bucket_empty_fraction
, options::TWO_DASHES
, '\0', 0.0,
488 N_("Min fraction of empty buckets in dynamic hash"),
491 DEFINE_enum(hash_style
, options::TWO_DASHES
, '\0', "sysv",
492 N_("Dynamic hash style"), N_("[sysv,gnu,both]"),
493 {"sysv", "gnu", "both"});
495 DEFINE_string(dynamic_linker
, options::TWO_DASHES
, 'I', NULL
,
496 N_("Set dynamic linker path"), N_("PROGRAM"));
498 DEFINE_special(just_symbols
, options::TWO_DASHES
, '\0',
499 N_("Read only symbol values from FILE"), N_("FILE"));
501 DEFINE_special(library
, options::TWO_DASHES
, 'l',
502 N_("Search for library LIBNAME"), N_("LIBNAME"));
504 DEFINE_dirlist(library_path
, options::TWO_DASHES
, 'L',
505 N_("Add directory to search path"), N_("DIR"));
507 DEFINE_string(m
, options::EXACTLY_ONE_DASH
, 'm', "",
508 N_("Ignored for compatibility"), N_("EMULATION"));
510 DEFINE_string(output
, options::TWO_DASHES
, 'o', "a.out",
511 N_("Set output file name"), N_("FILE"));
513 DEFINE_uint(optimize
, options::EXACTLY_ONE_DASH
, 'O', 0,
514 N_("Optimize output file size"), N_("LEVEL"));
516 DEFINE_string(oformat
, options::EXACTLY_TWO_DASHES
, '\0', "elf",
517 N_("Set output format"), N_("[binary]"));
519 DEFINE_bool(emit_relocs
, options::TWO_DASHES
, 'q', false,
520 N_("Generate relocations in output"), NULL
);
522 DEFINE_bool(relocatable
, options::EXACTLY_ONE_DASH
, 'r', false,
523 N_("Generate relocatable output"), NULL
);
525 // -R really means -rpath, but can mean --just-symbols for
526 // compatibility with GNU ld. -rpath is always -rpath, so we list
528 DEFINE_special(R
, options::EXACTLY_ONE_DASH
, 'R',
529 N_("Add DIR to runtime search path"), N_("DIR"));
531 DEFINE_dirlist(rpath
, options::ONE_DASH
, '\0',
532 N_("Add DIR to runtime search path"), N_("DIR"));
534 DEFINE_dirlist(rpath_link
, options::TWO_DASHES
, '\0',
535 N_("Add DIR to link time shared library search path"),
538 DEFINE_bool(strip_all
, options::TWO_DASHES
, 's', false,
539 N_("Strip all symbols"), NULL
);
540 DEFINE_bool(strip_debug
, options::TWO_DASHES
, 'S', false,
541 N_("Strip debugging information"), NULL
);
542 DEFINE_bool(strip_debug_gdb
, options::TWO_DASHES
, '\0', false,
543 N_("Strip debug symbols that are unused by gdb "
544 "(at least versions <= 6.7)"), NULL
);
546 DEFINE_bool(shared
, options::ONE_DASH
, '\0', false,
547 N_("Generate shared library"), NULL
);
549 // This is not actually special in any way, but I need to give it
550 // a non-standard accessor-function name because 'static' is a keyword.
551 DEFINE_special(static, options::ONE_DASH
, '\0',
552 N_("Do not link against shared libraries"), NULL
);
554 DEFINE_bool(stats
, options::TWO_DASHES
, '\0', false,
555 N_("Print resource usage statistics"), NULL
);
557 DEFINE_string(sysroot
, options::TWO_DASHES
, '\0', "",
558 N_("Set target system root directory"), N_("DIR"));
560 DEFINE_special(script
, options::TWO_DASHES
, 'T',
561 N_("Read linker script"), N_("FILE"));
563 DEFINE_bool(threads
, options::TWO_DASHES
, '\0', false,
564 N_("Run the linker multi-threaded"),
565 N_("Do not run the linker multi-threaded"));
566 DEFINE_uint(thread_count
, options::TWO_DASHES
, '\0', 0,
567 N_("Number of threads to use"), N_("COUNT"));
568 DEFINE_uint(thread_count_initial
, options::TWO_DASHES
, '\0', 0,
569 N_("Number of threads to use in initial pass"), N_("COUNT"));
570 DEFINE_uint(thread_count_middle
, options::TWO_DASHES
, '\0', 0,
571 N_("Number of threads to use in middle pass"), N_("COUNT"));
572 DEFINE_uint(thread_count_final
, options::TWO_DASHES
, '\0', 0,
573 N_("Number of threads to use in final pass"), N_("COUNT"));
575 DEFINE_uint64(Tbss
, options::ONE_DASH
, '\0', -1U,
576 N_("Set the address of the bss segment"), N_("ADDRESS"));
577 DEFINE_uint64(Tdata
, options::ONE_DASH
, '\0', -1U,
578 N_("Set the address of the data segment"), N_("ADDRESS"));
579 DEFINE_uint64(Ttext
, options::ONE_DASH
, '\0', -1U,
580 N_("Set the address of the text segment"), N_("ADDRESS"));
582 DEFINE_special(version_script
, options::TWO_DASHES
, '\0',
583 N_("Read version script"), N_("FILE"));
585 DEFINE_bool(whole_archive
, options::TWO_DASHES
, '\0', false,
586 N_("Include all archive contents"),
587 N_("Include only needed archive contents"));
589 DEFINE_special(start_group
, options::TWO_DASHES
, '(',
590 N_("Start a library search group"), NULL
);
591 DEFINE_special(end_group
, options::TWO_DASHES
, ')',
592 N_("End a library search group"), NULL
);
596 // Both execstack and noexecstack differ from the default execstack_
597 // value, so we need to use different variables for them.
598 DEFINE_uint64(common_page_size
, options::DASH_Z
, '\0', 0,
599 N_("Set common page size to SIZE"), N_("SIZE"));
600 DEFINE_bool(execstack
, options::DASH_Z
, '\0', false,
601 N_("Mark output as requiring executable stack"), NULL
);
602 DEFINE_uint64(max_page_size
, options::DASH_Z
, '\0', 0,
603 N_("Set maximum page size to SIZE"), N_("SIZE"));
604 DEFINE_bool(noexecstack
, options::DASH_Z
, '\0', false,
605 N_("Mark output as not requiring executable stack"), NULL
);
608 typedef options::Dir_list Dir_list
;
612 // Does post-processing on flags, making sure they all have
613 // non-conflicting values. Also converts some flags from their
614 // "standard" types (string, etc), to another type (enum, DirList),
615 // which can be accessed via a separate method. Dies if it notices
619 // The macro defines output() (based on --output), but that's a
620 // generic name. Provide this alternative name, which is clearer.
622 output_file_name() const
623 { return this->output(); }
625 // This is not defined via a flag, but combines flags to say whether
626 // the output is position-independent or not.
628 output_is_position_independent() const
629 { return this->shared(); }
631 // This would normally be static(), and defined automatically, but
632 // since static is a keyword, we need to come up with our own name.
637 // In addition to getting the input and output formats as a string
638 // (via format() and oformat()), we also give access as an enum.
643 // Straight binary format.
647 // Note: these functions are not very fast.
648 Object_format
format_enum() const;
649 Object_format
oformat_enum() const;
651 // These are the best way to get access to the execstack state,
652 // not execstack() and noexecstack() which are hard to use properly.
654 is_execstack_set() const
655 { return this->execstack_status_
!= EXECSTACK_FROM_INPUT
; }
658 is_stack_executable() const
659 { return this->execstack_status_
== EXECSTACK_YES
; }
662 // Don't copy this structure.
663 General_options(const General_options
&);
664 General_options
& operator=(const General_options
&);
666 // Whether to mark the stack as executable.
669 // Not set on command line.
670 EXECSTACK_FROM_INPUT
,
671 // Mark the stack as executable (-z execstack).
673 // Mark the stack as not executable (-z noexecstack).
677 Execstack execstack_status_
;
679 set_execstack_status(Execstack value
)
680 { execstack_status_
= value
; }
684 set_static(bool value
)
687 // These are called by finalize() to set up the search-path correctly.
689 add_to_library_path_with_sysroot(const char* arg
)
690 { this->add_search_directory_to_library_path(Search_directory(arg
, true)); }
692 // Apply any sysroot to the directory lists.
697 // The position-dependent options. We use this to store the state of
698 // the commandline at a particular point in parsing for later
699 // reference. For instance, if we see "ld --whole-archive foo.a
700 // --no-whole-archive," we want to store the whole-archive option with
701 // foo.a, so when the time comes to parse foo.a we know we should do
702 // it in whole-archive mode. We could store all of General_options,
703 // but that's big, so we just pick the subset of flags that actually
704 // change in a position-dependent way.
706 #define DEFINE_posdep(varname__, type__) \
710 { return this->varname__##_; } \
713 set_##varname__(type__ value) \
714 { this->varname__##_ = value; } \
718 class Position_dependent_options
721 Position_dependent_options(const General_options
& options
722 = Position_dependent_options::default_options_
)
723 { copy_from_options(options
); }
725 void copy_from_options(const General_options
& options
)
727 this->set_as_needed(options
.as_needed());
728 this->set_Bdynamic(options
.Bdynamic());
729 this->set_format_enum(options
.format_enum());
730 this->set_whole_archive(options
.whole_archive());
733 DEFINE_posdep(as_needed
, bool);
734 DEFINE_posdep(Bdynamic
, bool);
735 DEFINE_posdep(format_enum
, General_options::Object_format
);
736 DEFINE_posdep(whole_archive
, bool);
739 // This is a General_options with everything set to its default
740 // value. A Position_dependent_options created with no argument
741 // will take its values from here.
742 static General_options default_options_
;
746 // A single file or library argument from the command line.
748 class Input_file_argument
751 // name: file name or library name
752 // is_lib: true if name is a library name: that is, emits the leading
753 // "lib" and trailing ".so"/".a" from the name
754 // extra_search_path: an extra directory to look for the file, prior
755 // to checking the normal library search path. If this is "",
756 // then no extra directory is added.
757 // just_symbols: whether this file only defines symbols.
758 // options: The position dependent options at this point in the
759 // command line, such as --whole-archive.
760 Input_file_argument()
761 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
765 Input_file_argument(const char* name
, bool is_lib
,
766 const char* extra_search_path
,
768 const Position_dependent_options
& options
)
769 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
770 just_symbols_(just_symbols
), options_(options
)
773 // You can also pass in a General_options instance instead of a
774 // Position_dependent_options. In that case, we extract the
775 // position-independent vars from the General_options and only store
777 Input_file_argument(const char* name
, bool is_lib
,
778 const char* extra_search_path
,
780 const General_options
& options
)
781 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
782 just_symbols_(just_symbols
), options_(options
)
787 { return this->name_
.c_str(); }
789 const Position_dependent_options
&
791 { return this->options_
; }
795 { return this->is_lib_
; }
798 extra_search_path() const
800 return (this->extra_search_path_
.empty()
802 : this->extra_search_path_
.c_str());
805 // Return whether we should only read symbols from this file.
808 { return this->just_symbols_
; }
810 // Return whether this file may require a search using the -L
813 may_need_search() const
814 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
817 // We use std::string, not const char*, here for convenience when
818 // using script files, so that we do not have to preserve the string
822 std::string extra_search_path_
;
824 Position_dependent_options options_
;
827 // A file or library, or a group, from the command line.
832 // Create a file or library argument.
833 explicit Input_argument(Input_file_argument file
)
834 : is_file_(true), file_(file
), group_(NULL
)
837 // Create a group argument.
838 explicit Input_argument(Input_file_group
* group
)
839 : is_file_(false), group_(group
)
842 // Return whether this is a file.
845 { return this->is_file_
; }
847 // Return whether this is a group.
850 { return !this->is_file_
; }
852 // Return the information about the file.
853 const Input_file_argument
&
856 gold_assert(this->is_file_
);
860 // Return the information about the group.
861 const Input_file_group
*
864 gold_assert(!this->is_file_
);
871 gold_assert(!this->is_file_
);
877 Input_file_argument file_
;
878 Input_file_group
* group_
;
881 // A group from the command line. This is a set of arguments within
882 // --start-group ... --end-group.
884 class Input_file_group
887 typedef std::vector
<Input_argument
> Files
;
888 typedef Files::const_iterator const_iterator
;
894 // Add a file to the end of the group.
896 add_file(const Input_file_argument
& arg
)
897 { this->files_
.push_back(Input_argument(arg
)); }
899 // Iterators to iterate over the group contents.
903 { return this->files_
.begin(); }
907 { return this->files_
.end(); }
913 // A list of files from the command line or a script.
915 class Input_arguments
918 typedef std::vector
<Input_argument
> Input_argument_list
;
919 typedef Input_argument_list::const_iterator const_iterator
;
922 : input_argument_list_(), in_group_(false)
927 add_file(const Input_file_argument
& arg
);
929 // Start a group (the --start-group option).
933 // End a group (the --end-group option).
937 // Return whether we are currently in a group.
940 { return this->in_group_
; }
942 // The number of entries in the list.
945 { return this->input_argument_list_
.size(); }
947 // Iterators to iterate over the list of input files.
951 { return this->input_argument_list_
.begin(); }
955 { return this->input_argument_list_
.end(); }
957 // Return whether the list is empty.
960 { return this->input_argument_list_
.empty(); }
963 Input_argument_list input_argument_list_
;
968 // All the information read from the command line. These are held in
969 // three separate structs: one to hold the options (--foo), one to
970 // hold the filenames listed on the commandline, and one to hold
971 // linker script information. This third is not a subset of the other
972 // two because linker scripts can be specified either as options (via
978 typedef Input_arguments::const_iterator const_iterator
;
982 // Process the command line options. This will exit with an
983 // appropriate error message if an unrecognized option is seen.
985 process(int argc
, const char** argv
);
987 // Process one command-line option. This takes the index of argv to
988 // process, and returns the index for the next option. no_more_options
989 // is set to true if argv[i] is "--".
991 process_one_option(int argc
, const char** argv
, int i
,
992 bool* no_more_options
);
994 // Get the general options.
995 const General_options
&
997 { return this->options_
; }
999 // Get the position dependent options.
1000 const Position_dependent_options
&
1001 position_dependent_options() const
1002 { return this->position_options_
; }
1004 // Get the linker-script options.
1007 { return this->script_options_
; }
1009 // Get the version-script options: a convenience routine.
1010 const Version_script_info
&
1011 version_script() const
1012 { return *this->script_options_
.version_script_info(); }
1014 // Get the input files.
1017 { return this->inputs_
; }
1019 // The number of input files.
1021 number_of_input_files() const
1022 { return this->inputs_
.size(); }
1024 // Iterators to iterate over the list of input files.
1028 { return this->inputs_
.begin(); }
1032 { return this->inputs_
.end(); }
1035 Command_line(const Command_line
&);
1036 Command_line
& operator=(const Command_line
&);
1038 General_options options_
;
1039 Position_dependent_options position_options_
;
1040 Script_options script_options_
;
1041 Input_arguments inputs_
;
1044 } // End namespace gold.
1046 #endif // !defined(GOLD_OPTIONS_H)