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_string(const char* option_name
, const char* arg
, const char** retval
);
80 parse_dirlist(const char* option_name
, const char* arg
, Dir_list
* retval
);
83 parse_choices(const char* option_name
, const char* arg
, const char** retval
,
84 const char* choices
[], int num_choices
);
88 // Most options have both a shortname (one letter) and a longname.
89 // This enum controls how many dashes are expected for longname access
90 // -- shortnames always use one dash. Most longnames will accept
91 // either one dash or two; the only difference between ONE_DASH and
92 // TWO_DASHES is how we print the option in --help. However, some
93 // longnames require two dashes, and some require only one. The
94 // special value DASH_Z means that the option is preceded by "-z".
97 ONE_DASH
, TWO_DASHES
, EXACTLY_ONE_DASH
, EXACTLY_TWO_DASHES
, DASH_Z
100 // LONGNAME is the long-name of the option with dashes converted to
101 // underscores, or else the short-name if the option has no long-name.
102 // It is never the empty string.
103 // DASHES is an instance of the Dashes enum: ONE_DASH, TWO_DASHES, etc.
104 // SHORTNAME is the short-name of the option, as a char, or '\0' if the
105 // option has no short-name. If the option has no long-name, you
106 // should specify the short-name in *both* VARNAME and here.
107 // DEFAULT_VALUE is the value of the option if not specified on the
108 // commandline, as a string.
109 // HELPSTRING is the descriptive text used with the option via --help
110 // HELPARG is how you define the argument to the option.
111 // --help output is "-shortname HELPARG, --longname HELPARG: HELPSTRING"
112 // HELPARG should be NULL iff the option is a bool and takes no arg.
113 // READER provides parse_to_value, which is a function that will convert
114 // a char* argument into the proper type and store it in some variable.
115 // A One_option struct initializes itself with the global list of options
116 // at constructor time, so be careful making one of these.
119 std::string longname
;
122 const char* default_value
;
123 const char* helpstring
;
127 One_option(const char* ln
, Dashes d
, char sn
, const char* dv
,
128 const char* hs
, const char* ha
, Struct_var
* r
)
129 : longname(ln
), dashes(d
), shortname(sn
), default_value(dv
? dv
: ""),
130 helpstring(hs
), helparg(ha
), reader(r
)
132 // In longname, we convert all underscores to dashes, since GNU
133 // style uses dashes in option names. longname is likely to have
134 // underscores in it because it's also used to declare a C++
136 const char* pos
= strchr(this->longname
.c_str(), '_');
137 for (; pos
; pos
= strchr(pos
, '_'))
138 this->longname
[pos
- this->longname
.c_str()] = '-';
140 // We only register ourselves if our helpstring is not NULL. This
141 // is to support the "no-VAR" boolean variables, which we
142 // conditionally turn on by defining "no-VAR" help text.
143 if (this->helpstring
)
144 this->register_option();
147 // This option takes an argument iff helparg is not NULL.
149 takes_argument() const
150 { return this->helparg
!= NULL
; }
152 // Register this option with the global list of options.
156 // Print this option to stdout (used with --help).
161 // All options have a Struct_##varname that inherits from this and
162 // actually implements parse_to_value for that option.
165 // OPTION: the name of the option as specified on the commandline,
166 // including leading dashes, and any text following the option:
167 // "-O", "--defsym=mysym=0x1000", etc.
168 // ARG: the arg associated with this option, or NULL if the option
169 // takes no argument: "2", "mysym=0x1000", etc.
170 // CMDLINE: the global Command_line object. Used by DEFINE_special.
171 // OPTIONS: the global General_options object. Used by DEFINE_special.
173 parse_to_value(const char* option
, const char* arg
,
174 Command_line
* cmdline
, General_options
* options
) = 0;
176 ~Struct_var() // To make gcc happy.
180 // This is for "special" options that aren't of any predefined type.
181 struct Struct_special
: public Struct_var
183 // If you change this, change the parse-fn in DEFINE_special as well.
184 typedef void (General_options::*Parse_function
)(const char*, const char*,
186 Struct_special(const char* varname
, Dashes dashes
, char shortname
,
187 Parse_function parse_function
,
188 const char* helpstring
, const char* helparg
)
189 : option(varname
, dashes
, shortname
, "", helpstring
, helparg
, this),
190 parse(parse_function
)
193 void parse_to_value(const char* option
, const char* arg
,
194 Command_line
* cmdline
, General_options
* options
)
195 { (options
->*(this->parse
))(option
, arg
, cmdline
); }
198 Parse_function parse
;
201 } // End namespace options.
204 // These are helper macros use by DEFINE_uint64/etc below.
205 // This macro is used inside the General_options_ class, so defines
206 // var() and set_var() as General_options methods. Arguments as are
207 // for the constructor for One_option. param_type__ is the same as
208 // type__ for built-in types, and "const type__ &" otherwise.
209 #define DEFINE_var(varname__, dashes__, shortname__, default_value__, \
210 default_value_as_string__, helpstring__, helparg__, \
211 type__, param_type__, parse_fn__) \
215 { return this->varname__##_.value; } \
218 user_set_##varname__() const \
219 { return this->varname__##_.user_set_via_option; } \
222 struct Struct_##varname__ : public options::Struct_var \
224 Struct_##varname__() \
225 : option(#varname__, dashes__, shortname__, default_value_as_string__, \
226 helpstring__, helparg__, this), \
227 user_set_via_option(false), value(default_value__) \
231 parse_to_value(const char* option_name, const char* arg, \
232 Command_line*, General_options*) \
234 parse_fn__(option_name, arg, &this->value); \
235 this->user_set_via_option = true; \
238 options::One_option option; \
239 bool user_set_via_option; \
242 Struct_##varname__ varname__##_; \
244 set_##varname__(param_type__ value) \
245 { this->varname__##_.value = value; }
247 // These macros allow for easy addition of a new commandline option.
249 // If no_helpstring__ is not NULL, then in addition to creating
250 // VARNAME, we also create an option called no-VARNAME.
251 #define DEFINE_bool(varname__, dashes__, shortname__, default_value__, \
252 helpstring__, no_helpstring__) \
253 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
254 default_value__ ? "true" : "false", helpstring__, NULL, \
255 bool, bool, options::parse_bool) \
256 struct Struct_no_##varname__ : public options::Struct_var \
258 Struct_no_##varname__() : option("no-" #varname__, dashes__, '\0', \
259 default_value__ ? "false" : "true", \
260 no_helpstring__, NULL, this) \
264 parse_to_value(const char*, const char*, \
265 Command_line*, General_options* options) \
266 { options->set_##varname__(false); } \
268 options::One_option option; \
270 Struct_no_##varname__ no_##varname__##_initializer_
272 #define DEFINE_uint(varname__, dashes__, shortname__, default_value__, \
273 helpstring__, helparg__) \
274 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
275 #default_value__, helpstring__, helparg__, \
276 int, int, options::parse_uint)
278 #define DEFINE_uint64(varname__, dashes__, shortname__, default_value__, \
279 helpstring__, helparg__) \
280 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
281 #default_value__, helpstring__, helparg__, \
282 uint64_t, uint64_t, options::parse_uint64)
284 #define DEFINE_string(varname__, dashes__, shortname__, default_value__, \
285 helpstring__, helparg__) \
286 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
287 default_value__, helpstring__, helparg__, \
288 const char*, const char*, options::parse_string)
290 // This is like DEFINE_string, but we convert each occurrence to a
291 // Search_directory and store it in a vector. Thus we also have the
292 // add_to_VARNAME() method, to append to the vector.
293 #define DEFINE_dirlist(varname__, dashes__, shortname__, \
294 helpstring__, helparg__) \
295 DEFINE_var(varname__, dashes__, shortname__, , \
296 "", helpstring__, helparg__, options::Dir_list, \
297 const options::Dir_list&, options::parse_dirlist) \
299 add_to_##varname__(const char* new_value) \
300 { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
302 add_search_directory_to_##varname__(const Search_directory& dir) \
303 { this->varname__##_.value.push_back(dir); }
305 // When you have a list of possible values (expressed as string)
306 // After helparg__ should come an initializer list, like
307 // {"foo", "bar", "baz"}
308 #define DEFINE_enum(varname__, dashes__, shortname__, default_value__, \
309 helpstring__, helparg__, ...) \
310 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
311 default_value__, helpstring__, helparg__, \
312 const char*, const char*, parse_choices_##varname__) \
314 static void parse_choices_##varname__(const char* option_name, \
316 const char** retval) { \
317 const char* choices[] = __VA_ARGS__; \
318 options::parse_choices(option_name, arg, retval, \
319 choices, sizeof(choices) / sizeof(*choices)); \
322 // This is used for non-standard flags. It defines no functions; it
323 // just calls General_options::parse_VARNAME whenever the flag is
324 // seen. We declare parse_VARNAME as a static member of
325 // General_options; you are responsible for defining it there.
326 // helparg__ should be NULL iff this special-option is a boolean.
327 #define DEFINE_special(varname__, dashes__, shortname__, \
328 helpstring__, helparg__) \
330 void parse_##varname__(const char* option, const char* arg, \
331 Command_line* inputs); \
332 struct Struct_##varname__ : public options::Struct_special \
334 Struct_##varname__() \
335 : options::Struct_special(#varname__, dashes__, shortname__, \
336 &General_options::parse_##varname__, \
337 helpstring__, helparg__) \
340 Struct_##varname__ varname__##_initializer_
343 // A directory to search. For each directory we record whether it is
344 // in the sysroot. We need to know this so that, if a linker script
345 // is found within the sysroot, we will apply the sysroot to any files
346 // named by that script.
348 class Search_directory
351 // We need a default constructor because we put this in a
354 : name_(NULL
), put_in_sysroot_(false), is_in_sysroot_(false)
357 // This is the usual constructor.
358 Search_directory(const char* name
, bool put_in_sysroot
)
359 : name_(name
), put_in_sysroot_(put_in_sysroot
), is_in_sysroot_(false)
361 if (this->name_
.empty())
365 // This is called if we have a sysroot. The sysroot is prefixed to
366 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
367 // set to true for any enries which are in the sysroot (this will
368 // naturally include any entries for which put_in_sysroot_ is true).
369 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
370 // passing SYSROOT to lrealpath.
372 add_sysroot(const char* sysroot
, const char* canonical_sysroot
);
374 // Get the directory name.
377 { return this->name_
; }
379 // Return whether this directory is in the sysroot.
381 is_in_sysroot() const
382 { return this->is_in_sysroot_
; }
386 bool put_in_sysroot_
;
390 class General_options
393 // NOTE: For every option that you add here, also consider if you
394 // should add it to Position_dependent_options.
395 DEFINE_special(help
, options::TWO_DASHES
, '\0',
396 _("Report usage information"), NULL
);
397 DEFINE_special(version
, options::TWO_DASHES
, 'v',
398 _("Report version information"), NULL
);
400 DEFINE_bool(allow_shlib_undefined
, options::TWO_DASHES
, '\0', false,
401 _("Allow unresolved references in shared libraries"),
402 _("Do not allow unresolved references in shared libraries"));
404 DEFINE_bool(as_needed
, options::TWO_DASHES
, '\0', false,
405 _("Only set DT_NEEDED for dynamic libs if used"),
406 _("Always DT_NEEDED for dynamic libs"));
408 DEFINE_bool(Bdynamic
, options::ONE_DASH
, '\0', true,
409 _("-l searches for shared libraries"), NULL
);
410 // Bstatic affects the same variable as Bdynamic, so we have to use
411 // the "special" macro to make that happen.
412 DEFINE_special(Bstatic
, options::ONE_DASH
, '\0',
413 _("-l does not search for shared libraries"), NULL
);
415 DEFINE_bool(Bsymbolic
, options::ONE_DASH
, '\0', false,
416 _("Bind defined symbols locally"), NULL
);
418 // This should really be an "enum", but it's too easy for folks to
419 // forget to update the list as they add new targets. So we just
420 // accept any string. We'll fail later (when the string is parsed),
421 // if the target isn't actually supported.
422 DEFINE_string(format
, options::TWO_DASHES
, 'b', "elf",
423 _("Set input format"), _("[elf,binary]"));
426 DEFINE_enum(compress_debug_sections
, options::TWO_DASHES
, '\0', "none",
427 _("Compress .debug_* sections in the output file"),
431 DEFINE_enum(compress_debug_sections
, options::TWO_DASHES
, '\0', "none",
432 _("Compress .debug_* sections in the output file"),
437 DEFINE_bool(define_common
, options::TWO_DASHES
, 'd', false,
438 _("Define common symbols"),
439 _("Do not define common symbols"));
440 DEFINE_bool(dc
, options::ONE_DASH
, '\0', false,
441 _("Alias for -d"), NULL
);
442 DEFINE_bool(dp
, options::ONE_DASH
, '\0', false,
443 _("Alias for -d"), NULL
);
445 DEFINE_special(defsym
, options::TWO_DASHES
, '\0',
446 _("Define a symbol"), _("SYMBOL=EXPRESSION"));
448 DEFINE_bool(demangle
, options::TWO_DASHES
, '\0',
449 getenv("COLLECT_NO_DEMANGLE") == NULL
,
450 _("Demangle C++ symbols in log messages"),
451 _("Do not demangle C++ symbols in log messages"));
453 DEFINE_bool(detect_odr_violations
, options::TWO_DASHES
, '\0', false,
454 _("Try to detect violations of the One Definition Rule"),
457 DEFINE_string(entry
, options::TWO_DASHES
, 'e', NULL
,
458 _("Set program start address"), _("ADDRESS"));
460 DEFINE_bool(export_dynamic
, options::TWO_DASHES
, 'E', false,
461 _("Export all dynamic symbols"), NULL
);
463 DEFINE_bool(eh_frame_hdr
, options::TWO_DASHES
, '\0', false,
464 _("Create exception frame header"), NULL
);
466 DEFINE_string(soname
, options::ONE_DASH
, 'h', NULL
,
467 _("Set shared library name"), _("FILENAME"));
469 DEFINE_enum(hash_style
, options::TWO_DASHES
, '\0', "sysv",
470 _("Dynamic hash style"), _("[sysv,gnu,both]"),
471 {"sysv", "gnu", "both"});
473 DEFINE_string(dynamic_linker
, options::TWO_DASHES
, 'I', NULL
,
474 _("Set dynamic linker path"), _("PROGRAM"));
476 DEFINE_special(library
, options::TWO_DASHES
, 'l',
477 _("Search for library LIBNAME"), _("LIBNAME"));
479 DEFINE_dirlist(library_path
, options::TWO_DASHES
, 'L',
480 _("Add directory to search path"), _("DIR"));
482 DEFINE_string(m
, options::EXACTLY_ONE_DASH
, 'm', "",
483 _("Ignored for compatibility"), _("EMULATION"));
485 DEFINE_string(output
, options::TWO_DASHES
, 'o', "a.out",
486 _("Set output file name"), _("FILE"));
488 DEFINE_uint(optimize
, options::EXACTLY_ONE_DASH
, 'O', 0,
489 _("Optimize output file size"), _("LEVEL"));
491 DEFINE_enum(oformat
, options::EXACTLY_TWO_DASHES
, '\0', "elf",
492 _("Set output format"), _("[binary]"),
495 DEFINE_bool(emit_relocs
, options::TWO_DASHES
, 'q', false,
496 _("Generate relocations in output"), NULL
);
498 DEFINE_bool(relocatable
, options::EXACTLY_ONE_DASH
, 'r', false,
499 _("Generate relocatable output"), NULL
);
501 // -R really means -rpath, but can mean --just-symbols for
502 // compatibility with GNU ld. -rpath is always -rpath, so we list
504 DEFINE_special(R
, options::EXACTLY_ONE_DASH
, 'R',
505 _("Add DIR to runtime search path"), _("DIR"));
507 DEFINE_dirlist(rpath
, options::ONE_DASH
, '\0',
508 _("Add DIR to runtime search path"), _("DIR"));
510 DEFINE_special(just_symbols
, options::TWO_DASHES
, '\0',
511 _("Read only symbol values from FILE"), _("FILE"));
513 DEFINE_dirlist(rpath_link
, options::TWO_DASHES
, '\0',
514 _("Add DIR to link time shared library search path"),
517 DEFINE_bool(strip_all
, options::TWO_DASHES
, 's', false,
518 _("Strip all symbols"), NULL
);
519 DEFINE_bool(strip_debug_gdb
, options::TWO_DASHES
, '\0', false,
520 _("Strip debug symbols that are unused by gdb "
521 "(at least versions <= 6.7)"), NULL
);
522 DEFINE_bool(strip_debug
, options::TWO_DASHES
, 'S', false,
523 _("Strip debugging information"), NULL
);
525 DEFINE_bool(shared
, options::ONE_DASH
, '\0', false,
526 _("Generate shared library"), NULL
);
528 // This is not actually special in any way, but I need to give it
529 // a non-standard accessor-function name because 'static' is a keyword.
530 DEFINE_special(static, options::ONE_DASH
, '\0',
531 _("Do not link against shared libraries"), NULL
);
533 DEFINE_bool(stats
, options::TWO_DASHES
, '\0', false,
534 _("Print resource usage statistics"), NULL
);
536 DEFINE_string(sysroot
, options::TWO_DASHES
, '\0', "",
537 _("Set target system root directory"), _("DIR"));
539 DEFINE_uint64(Tbss
, options::ONE_DASH
, '\0', -1U,
540 _("Set the address of the bss segment"), _("ADDRESS"));
541 DEFINE_uint64(Tdata
, options::ONE_DASH
, '\0', -1U,
542 _("Set the address of the data segment"), _("ADDRESS"));
543 DEFINE_uint64(Ttext
, options::ONE_DASH
, '\0', -1U,
544 _("Set the address of the text segment"), _("ADDRESS"));
546 DEFINE_special(script
, options::TWO_DASHES
, 'T',
547 _("Read linker script"), _("FILE"));
548 DEFINE_special(version_script
, options::TWO_DASHES
, '\0',
549 _("Read version script"), _("FILE"));
551 DEFINE_bool(threads
, options::TWO_DASHES
, '\0', false,
552 _("Run the linker multi-threaded"),
553 _("Do not run the linker multi-threaded"));
554 DEFINE_uint(thread_count
, options::TWO_DASHES
, '\0', 0,
555 _("Number of threads to use"), _("COUNT"));
556 DEFINE_uint(thread_count_initial
, options::TWO_DASHES
, '\0', 0,
557 _("Number of threads to use in initial pass"), _("COUNT"));
558 DEFINE_uint(thread_count_middle
, options::TWO_DASHES
, '\0', 0,
559 _("Number of threads to use in middle pass"), _("COUNT"));
560 DEFINE_uint(thread_count_final
, options::TWO_DASHES
, '\0', 0,
561 _("Number of threads to use in final pass"), _("COUNT"));
563 DEFINE_bool(whole_archive
, options::TWO_DASHES
, '\0', false,
564 _("Include all archive contents"),
565 _("Include only needed archive contents"));
567 DEFINE_special(start_group
, options::TWO_DASHES
, '(',
568 _("Start a library search group"), NULL
);
569 DEFINE_special(end_group
, options::TWO_DASHES
, ')',
570 _("End a library search group"), NULL
);
572 DEFINE_string(debug
, options::TWO_DASHES
, '\0', "",
573 _("Turn on debugging"), _("[task,script,all][,...]"));
577 // Both execstack and noexecstack differ from the default execstack_
578 // value, so we need to use different variables for them.
579 DEFINE_bool(execstack
, options::DASH_Z
, '\0', false,
580 _("Mark output as requiring executable stack"), NULL
);
581 DEFINE_bool(noexecstack
, options::DASH_Z
, '\0', false,
582 _("Mark output as not requiring executable stack"), NULL
);
583 DEFINE_uint64(max_page_size
, options::DASH_Z
, '\0', 0,
584 _("Set maximum page size to SIZE"), _("SIZE"));
585 DEFINE_uint64(common_page_size
, options::DASH_Z
, '\0', 0,
586 _("Set common page size to SIZE"), _("SIZE"));
589 typedef options::Dir_list Dir_list
;
593 // Does post-processing on flags, making sure they all have
594 // non-conflicting values. Also converts some flags from their
595 // "standard" types (string, etc), to another type (enum, DirList),
596 // which can be accessed via a separate method. Dies if it notices
600 // The macro defines output() (based on --output), but that's a
601 // generic name. Provide this alternative name, which is clearer.
603 output_file_name() const
604 { return this->output(); }
606 // This is not defined via a flag, but combines flags to say whether
607 // the output is position-independent or not.
609 output_is_position_independent() const
610 { return this->shared(); }
612 // This would normally be static(), and defined automatically, but
613 // since static is a keyword, we need to come up with our own name.
618 // In addition to getting the input and output formats as a string
619 // (via format() and oformat()), we also give access as an enum.
624 // Straight binary format.
628 // Note: these functions are not very fast.
629 Object_format
format_enum() const;
630 Object_format
oformat_enum() const;
632 // These are the best way to get access to the execstack state,
633 // not execstack() and noexecstack() which are hard to use properly.
635 is_execstack_set() const
636 { return this->execstack_status_
!= EXECSTACK_FROM_INPUT
; }
639 is_stack_executable() const
640 { return this->execstack_status_
== EXECSTACK_YES
; }
643 // Don't copy this structure.
644 General_options(const General_options
&);
645 General_options
& operator=(const General_options
&);
647 // Whether to mark the stack as executable.
650 // Not set on command line.
651 EXECSTACK_FROM_INPUT
,
652 // Mark the stack as executable (-z execstack).
654 // Mark the stack as not executable (-z noexecstack).
658 Execstack execstack_status_
;
660 set_execstack_status(Execstack value
)
661 { execstack_status_
= value
; }
665 set_static(bool value
)
668 // These are called by finalize() to set up the search-path correctly.
670 add_to_library_path_with_sysroot(const char* arg
)
671 { this->add_search_directory_to_library_path(Search_directory(arg
, true)); }
673 // Apply any sysroot to the directory lists.
678 // The position-dependent options. We use this to store the state of
679 // the commandline at a particular point in parsing for later
680 // reference. For instance, if we see "ld --whole-archive foo.a
681 // --no-whole-archive," we want to store the whole-archive option with
682 // foo.a, so when the time comes to parse foo.a we know we should do
683 // it in whole-archive mode. We could store all of General_options,
684 // but that's big, so we just pick the subset of flags that actually
685 // change in a position-dependent way.
687 #define DEFINE_posdep(varname__, type__) \
691 { return this->varname__##_; } \
694 set_##varname__(type__ value) \
695 { this->varname__##_ = value; } \
699 class Position_dependent_options
702 Position_dependent_options(const General_options
& options
703 = Position_dependent_options::default_options_
)
704 { copy_from_options(options
); }
706 void copy_from_options(const General_options
& options
)
708 this->set_as_needed(options
.as_needed());
709 this->set_Bdynamic(options
.Bdynamic());
710 this->set_format_enum(options
.format_enum());
711 this->set_whole_archive(options
.whole_archive());
714 DEFINE_posdep(as_needed
, bool);
715 DEFINE_posdep(Bdynamic
, bool);
716 DEFINE_posdep(format_enum
, General_options::Object_format
);
717 DEFINE_posdep(whole_archive
, bool);
720 // This is a General_options with everything set to its default
721 // value. A Position_dependent_options created with no argument
722 // will take its values from here.
723 static General_options default_options_
;
727 // A single file or library argument from the command line.
729 class Input_file_argument
732 // name: file name or library name
733 // is_lib: true if name is a library name: that is, emits the leading
734 // "lib" and trailing ".so"/".a" from the name
735 // extra_search_path: an extra directory to look for the file, prior
736 // to checking the normal library search path. If this is "",
737 // then no extra directory is added.
738 // just_symbols: whether this file only defines symbols.
739 // options: The position dependent options at this point in the
740 // command line, such as --whole-archive.
741 Input_file_argument()
742 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
746 Input_file_argument(const char* name
, bool is_lib
,
747 const char* extra_search_path
,
749 const Position_dependent_options
& options
)
750 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
751 just_symbols_(just_symbols
), options_(options
)
754 // You can also pass in a General_options instance instead of a
755 // Position_dependent_options. In that case, we extract the
756 // position-independent vars from the General_options and only store
758 Input_file_argument(const char* name
, bool is_lib
,
759 const char* extra_search_path
,
761 const General_options
& options
)
762 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
763 just_symbols_(just_symbols
), options_(options
)
768 { return this->name_
.c_str(); }
770 const Position_dependent_options
&
772 { return this->options_
; }
776 { return this->is_lib_
; }
779 extra_search_path() const
781 return (this->extra_search_path_
.empty()
783 : this->extra_search_path_
.c_str());
786 // Return whether we should only read symbols from this file.
789 { return this->just_symbols_
; }
791 // Return whether this file may require a search using the -L
794 may_need_search() const
795 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
798 // We use std::string, not const char*, here for convenience when
799 // using script files, so that we do not have to preserve the string
803 std::string extra_search_path_
;
805 Position_dependent_options options_
;
808 // A file or library, or a group, from the command line.
813 // Create a file or library argument.
814 explicit Input_argument(Input_file_argument file
)
815 : is_file_(true), file_(file
), group_(NULL
)
818 // Create a group argument.
819 explicit Input_argument(Input_file_group
* group
)
820 : is_file_(false), group_(group
)
823 // Return whether this is a file.
826 { return this->is_file_
; }
828 // Return whether this is a group.
831 { return !this->is_file_
; }
833 // Return the information about the file.
834 const Input_file_argument
&
837 gold_assert(this->is_file_
);
841 // Return the information about the group.
842 const Input_file_group
*
845 gold_assert(!this->is_file_
);
852 gold_assert(!this->is_file_
);
858 Input_file_argument file_
;
859 Input_file_group
* group_
;
862 // A group from the command line. This is a set of arguments within
863 // --start-group ... --end-group.
865 class Input_file_group
868 typedef std::vector
<Input_argument
> Files
;
869 typedef Files::const_iterator const_iterator
;
875 // Add a file to the end of the group.
877 add_file(const Input_file_argument
& arg
)
878 { this->files_
.push_back(Input_argument(arg
)); }
880 // Iterators to iterate over the group contents.
884 { return this->files_
.begin(); }
888 { return this->files_
.end(); }
894 // A list of files from the command line or a script.
896 class Input_arguments
899 typedef std::vector
<Input_argument
> Input_argument_list
;
900 typedef Input_argument_list::const_iterator const_iterator
;
903 : input_argument_list_(), in_group_(false)
908 add_file(const Input_file_argument
& arg
);
910 // Start a group (the --start-group option).
914 // End a group (the --end-group option).
918 // Return whether we are currently in a group.
921 { return this->in_group_
; }
923 // The number of entries in the list.
926 { return this->input_argument_list_
.size(); }
928 // Iterators to iterate over the list of input files.
932 { return this->input_argument_list_
.begin(); }
936 { return this->input_argument_list_
.end(); }
938 // Return whether the list is empty.
941 { return this->input_argument_list_
.empty(); }
944 Input_argument_list input_argument_list_
;
949 // All the information read from the command line. These are held in
950 // three separate structs: one to hold the options (--foo), one to
951 // hold the filenames listed on the commandline, and one to hold
952 // linker script information. This third is not a subset of the other
953 // two because linker scripts can be specified either as options (via
959 typedef Input_arguments::const_iterator const_iterator
;
963 // Process the command line options. This will exit with an
964 // appropriate error message if an unrecognized option is seen.
966 process(int argc
, const char** argv
);
968 // Process one command-line option. This takes the index of argv to
969 // process, and returns the index for the next option. no_more_options
970 // is set to true if argv[i] is "--".
972 process_one_option(int argc
, const char** argv
, int i
,
973 bool* no_more_options
);
975 // Get the general options.
976 const General_options
&
978 { return this->options_
; }
980 // Get the position dependent options.
981 const Position_dependent_options
&
982 position_dependent_options() const
983 { return this->position_options_
; }
985 // Get the linker-script options.
988 { return this->script_options_
; }
990 // Get the version-script options: a convenience routine.
991 const Version_script_info
&
992 version_script() const
993 { return *this->script_options_
.version_script_info(); }
995 // Get the input files.
998 { return this->inputs_
; }
1000 // The number of input files.
1002 number_of_input_files() const
1003 { return this->inputs_
.size(); }
1005 // Iterators to iterate over the list of input files.
1009 { return this->inputs_
.begin(); }
1013 { return this->inputs_
.end(); }
1016 Command_line(const Command_line
&);
1017 Command_line
& operator=(const Command_line
&);
1019 General_options options_
;
1020 Position_dependent_options position_options_
;
1021 Script_options script_options_
;
1022 Input_arguments inputs_
;
1025 } // End namespace gold.
1027 #endif // !defined(GOLD_OPTIONS_H)