* options.cc (General_options::parse_V): New function.
[deliverable/binutils-gdb.git] / gold / options.h
1 // options.h -- handle command line options for gold -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 // General_options (from Command_line::options())
24 // All the options (a.k.a. command-line flags)
25 // Input_argument (from Command_line::inputs())
26 // The list of input files, including -l options.
27 // Command_line
28 // Everything we get from the command line -- the General_options
29 // plus the Input_arguments.
30 //
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).
35
36 #ifndef GOLD_OPTIONS_H
37 #define GOLD_OPTIONS_H
38
39 #include <cstdlib>
40 #include <cstring>
41 #include <list>
42 #include <string>
43 #include <vector>
44
45 #include "elfcpp.h"
46 #include "script.h"
47
48 namespace gold
49 {
50
51 class Command_line;
52 class General_options;
53 class Search_directory;
54 class Input_file_group;
55 class Position_dependent_options;
56 class Target;
57
58 // The nested namespace is to contain all the global variables and
59 // structs that need to be defined in the .h file, but do not need to
60 // be used outside this class.
61 namespace options
62 {
63 typedef std::vector<Search_directory> Dir_list;
64
65 // These routines convert from a string option to various types.
66 // Each gives a fatal error if it cannot parse the argument.
67
68 extern void
69 parse_bool(const char* option_name, const char* arg, bool* retval);
70
71 extern void
72 parse_uint(const char* option_name, const char* arg, int* retval);
73
74 extern void
75 parse_uint64(const char* option_name, const char* arg, uint64_t* retval);
76
77 extern void
78 parse_double(const char* option_name, const char* arg, double* retval);
79
80 extern void
81 parse_string(const char* option_name, const char* arg, const char** retval);
82
83 extern void
84 parse_optional_string(const char* option_name, const char* arg,
85 const char** retval);
86
87 extern void
88 parse_dirlist(const char* option_name, const char* arg, Dir_list* retval);
89
90 extern void
91 parse_choices(const char* option_name, const char* arg, const char** retval,
92 const char* choices[], int num_choices);
93
94 struct Struct_var;
95
96 // Most options have both a shortname (one letter) and a longname.
97 // This enum controls how many dashes are expected for longname access
98 // -- shortnames always use one dash. Most longnames will accept
99 // either one dash or two; the only difference between ONE_DASH and
100 // TWO_DASHES is how we print the option in --help. However, some
101 // longnames require two dashes, and some require only one. The
102 // special value DASH_Z means that the option is preceded by "-z".
103 enum Dashes
104 {
105 ONE_DASH, TWO_DASHES, EXACTLY_ONE_DASH, EXACTLY_TWO_DASHES, DASH_Z
106 };
107
108 // LONGNAME is the long-name of the option with dashes converted to
109 // underscores, or else the short-name if the option has no long-name.
110 // It is never the empty string.
111 // DASHES is an instance of the Dashes enum: ONE_DASH, TWO_DASHES, etc.
112 // SHORTNAME is the short-name of the option, as a char, or '\0' if the
113 // option has no short-name. If the option has no long-name, you
114 // should specify the short-name in *both* VARNAME and here.
115 // DEFAULT_VALUE is the value of the option if not specified on the
116 // commandline, as a string.
117 // HELPSTRING is the descriptive text used with the option via --help
118 // HELPARG is how you define the argument to the option.
119 // --help output is "-shortname HELPARG, --longname HELPARG: HELPSTRING"
120 // HELPARG should be NULL iff the option is a bool and takes no arg.
121 // OPTIONAL_ARG is true if this option takes an optional argument. An
122 // optional argument must be specifid as --OPTION=VALUE, not
123 // --OPTION VALUE.
124 // READER provides parse_to_value, which is a function that will convert
125 // a char* argument into the proper type and store it in some variable.
126 // A One_option struct initializes itself with the global list of options
127 // at constructor time, so be careful making one of these.
128 struct One_option
129 {
130 std::string longname;
131 Dashes dashes;
132 char shortname;
133 const char* default_value;
134 const char* helpstring;
135 const char* helparg;
136 bool optional_arg;
137 Struct_var* reader;
138
139 One_option(const char* ln, Dashes d, char sn, const char* dv,
140 const char* hs, const char* ha, bool oa, Struct_var* r)
141 : longname(ln), dashes(d), shortname(sn), default_value(dv ? dv : ""),
142 helpstring(hs), helparg(ha), optional_arg(oa), reader(r)
143 {
144 // In longname, we convert all underscores to dashes, since GNU
145 // style uses dashes in option names. longname is likely to have
146 // underscores in it because it's also used to declare a C++
147 // function.
148 const char* pos = strchr(this->longname.c_str(), '_');
149 for (; pos; pos = strchr(pos, '_'))
150 this->longname[pos - this->longname.c_str()] = '-';
151
152 // We only register ourselves if our helpstring is not NULL. This
153 // is to support the "no-VAR" boolean variables, which we
154 // conditionally turn on by defining "no-VAR" help text.
155 if (this->helpstring)
156 this->register_option();
157 }
158
159 // This option takes an argument iff helparg is not NULL.
160 bool
161 takes_argument() const
162 { return this->helparg != NULL; }
163
164 // Whether the argument is optional.
165 bool
166 takes_optional_argument() const
167 { return this->optional_arg; }
168
169 // Register this option with the global list of options.
170 void
171 register_option();
172
173 // Print this option to stdout (used with --help).
174 void
175 print() const;
176 };
177
178 // All options have a Struct_##varname that inherits from this and
179 // actually implements parse_to_value for that option.
180 struct Struct_var
181 {
182 // OPTION: the name of the option as specified on the commandline,
183 // including leading dashes, and any text following the option:
184 // "-O", "--defsym=mysym=0x1000", etc.
185 // ARG: the arg associated with this option, or NULL if the option
186 // takes no argument: "2", "mysym=0x1000", etc.
187 // CMDLINE: the global Command_line object. Used by DEFINE_special.
188 // OPTIONS: the global General_options object. Used by DEFINE_special.
189 virtual void
190 parse_to_value(const char* option, const char* arg,
191 Command_line* cmdline, General_options* options) = 0;
192 virtual
193 ~Struct_var() // To make gcc happy.
194 { }
195 };
196
197 // This is for "special" options that aren't of any predefined type.
198 struct Struct_special : public Struct_var
199 {
200 // If you change this, change the parse-fn in DEFINE_special as well.
201 typedef void (General_options::*Parse_function)(const char*, const char*,
202 Command_line*);
203 Struct_special(const char* varname, Dashes dashes, char shortname,
204 Parse_function parse_function,
205 const char* helpstring, const char* helparg)
206 : option(varname, dashes, shortname, "", helpstring, helparg, false, this),
207 parse(parse_function)
208 { }
209
210 void parse_to_value(const char* option, const char* arg,
211 Command_line* cmdline, General_options* options)
212 { (options->*(this->parse))(option, arg, cmdline); }
213
214 One_option option;
215 Parse_function parse;
216 };
217
218 } // End namespace options.
219
220
221 // These are helper macros use by DEFINE_uint64/etc below.
222 // This macro is used inside the General_options_ class, so defines
223 // var() and set_var() as General_options methods. Arguments as are
224 // for the constructor for One_option. param_type__ is the same as
225 // type__ for built-in types, and "const type__ &" otherwise.
226 #define DEFINE_var(varname__, dashes__, shortname__, default_value__, \
227 default_value_as_string__, helpstring__, helparg__, \
228 optional_arg__, type__, param_type__, parse_fn__) \
229 public: \
230 param_type__ \
231 varname__() const \
232 { return this->varname__##_.value; } \
233 \
234 bool \
235 user_set_##varname__() const \
236 { return this->varname__##_.user_set_via_option; } \
237 \
238 private: \
239 struct Struct_##varname__ : public options::Struct_var \
240 { \
241 Struct_##varname__() \
242 : option(#varname__, dashes__, shortname__, default_value_as_string__, \
243 helpstring__, helparg__, optional_arg__, this), \
244 user_set_via_option(false), value(default_value__) \
245 { } \
246 \
247 void \
248 parse_to_value(const char* option_name, const char* arg, \
249 Command_line*, General_options*) \
250 { \
251 parse_fn__(option_name, arg, &this->value); \
252 this->user_set_via_option = true; \
253 } \
254 \
255 options::One_option option; \
256 bool user_set_via_option; \
257 type__ value; \
258 }; \
259 Struct_##varname__ varname__##_; \
260 void \
261 set_##varname__(param_type__ value) \
262 { this->varname__##_.value = value; }
263
264 // These macros allow for easy addition of a new commandline option.
265
266 // If no_helpstring__ is not NULL, then in addition to creating
267 // VARNAME, we also create an option called no-VARNAME.
268 #define DEFINE_bool(varname__, dashes__, shortname__, default_value__, \
269 helpstring__, no_helpstring__) \
270 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
271 default_value__ ? "true" : "false", helpstring__, NULL, \
272 false, bool, bool, options::parse_bool) \
273 struct Struct_no_##varname__ : public options::Struct_var \
274 { \
275 Struct_no_##varname__() : option("no-" #varname__, dashes__, '\0', \
276 default_value__ ? "false" : "true", \
277 no_helpstring__, NULL, false, this) \
278 { } \
279 \
280 void \
281 parse_to_value(const char*, const char*, \
282 Command_line*, General_options* options) \
283 { options->set_##varname__(false); } \
284 \
285 options::One_option option; \
286 }; \
287 Struct_no_##varname__ no_##varname__##_initializer_
288
289 #define DEFINE_enable(varname__, dashes__, shortname__, default_value__, \
290 helpstring__, no_helpstring__) \
291 DEFINE_var(enable_##varname__, dashes__, shortname__, default_value__, \
292 default_value__ ? "true" : "false", helpstring__, NULL, \
293 false, bool, bool, options::parse_bool) \
294 struct Struct_disable_##varname__ : public options::Struct_var \
295 { \
296 Struct_disable_##varname__() : option("disable-" #varname__, \
297 dashes__, '\0', \
298 default_value__ ? "false" : "true", \
299 no_helpstring__, NULL, false, this) \
300 { } \
301 \
302 void \
303 parse_to_value(const char*, const char*, \
304 Command_line*, General_options* options) \
305 { options->set_enable_##varname__(false); } \
306 \
307 options::One_option option; \
308 }; \
309 Struct_disable_##varname__ disable_##varname__##_initializer_
310
311 #define DEFINE_uint(varname__, dashes__, shortname__, default_value__, \
312 helpstring__, helparg__) \
313 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
314 #default_value__, helpstring__, helparg__, false, \
315 int, int, options::parse_uint)
316
317 #define DEFINE_uint64(varname__, dashes__, shortname__, default_value__, \
318 helpstring__, helparg__) \
319 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
320 #default_value__, helpstring__, helparg__, false, \
321 uint64_t, uint64_t, options::parse_uint64)
322
323 #define DEFINE_double(varname__, dashes__, shortname__, default_value__, \
324 helpstring__, helparg__) \
325 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
326 #default_value__, helpstring__, helparg__, false, \
327 double, double, options::parse_double)
328
329 #define DEFINE_string(varname__, dashes__, shortname__, default_value__, \
330 helpstring__, helparg__) \
331 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
332 default_value__, helpstring__, helparg__, false, \
333 const char*, const char*, options::parse_string)
334
335 // This is like DEFINE_string, but we convert each occurrence to a
336 // Search_directory and store it in a vector. Thus we also have the
337 // add_to_VARNAME() method, to append to the vector.
338 #define DEFINE_dirlist(varname__, dashes__, shortname__, \
339 helpstring__, helparg__) \
340 DEFINE_var(varname__, dashes__, shortname__, , \
341 "", helpstring__, helparg__, false, options::Dir_list, \
342 const options::Dir_list&, options::parse_dirlist) \
343 void \
344 add_to_##varname__(const char* new_value) \
345 { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
346 void \
347 add_search_directory_to_##varname__(const Search_directory& dir) \
348 { this->varname__##_.value.push_back(dir); }
349
350 // When you have a list of possible values (expressed as string)
351 // After helparg__ should come an initializer list, like
352 // {"foo", "bar", "baz"}
353 #define DEFINE_enum(varname__, dashes__, shortname__, default_value__, \
354 helpstring__, helparg__, ...) \
355 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
356 default_value__, helpstring__, helparg__, false, \
357 const char*, const char*, parse_choices_##varname__) \
358 private: \
359 static void parse_choices_##varname__(const char* option_name, \
360 const char* arg, \
361 const char** retval) { \
362 const char* choices[] = __VA_ARGS__; \
363 options::parse_choices(option_name, arg, retval, \
364 choices, sizeof(choices) / sizeof(*choices)); \
365 }
366
367 // This is used for non-standard flags. It defines no functions; it
368 // just calls General_options::parse_VARNAME whenever the flag is
369 // seen. We declare parse_VARNAME as a static member of
370 // General_options; you are responsible for defining it there.
371 // helparg__ should be NULL iff this special-option is a boolean.
372 #define DEFINE_special(varname__, dashes__, shortname__, \
373 helpstring__, helparg__) \
374 private: \
375 void parse_##varname__(const char* option, const char* arg, \
376 Command_line* inputs); \
377 struct Struct_##varname__ : public options::Struct_special \
378 { \
379 Struct_##varname__() \
380 : options::Struct_special(#varname__, dashes__, shortname__, \
381 &General_options::parse_##varname__, \
382 helpstring__, helparg__) \
383 { } \
384 }; \
385 Struct_##varname__ varname__##_initializer_
386
387 // An option that takes an optional string argument. If the option is
388 // used with no argument, the value will be the default, and
389 // user_set_via_option will be true.
390 #define DEFINE_optional_string(varname__, dashes__, shortname__, \
391 default_value__, \
392 helpstring__, helparg__) \
393 DEFINE_var(varname__, dashes__, shortname__, default_value__, \
394 default_value__, helpstring__, helparg__, true, \
395 const char*, const char*, options::parse_optional_string)
396
397 // A directory to search. For each directory we record whether it is
398 // in the sysroot. We need to know this so that, if a linker script
399 // is found within the sysroot, we will apply the sysroot to any files
400 // named by that script.
401
402 class Search_directory
403 {
404 public:
405 // We need a default constructor because we put this in a
406 // std::vector.
407 Search_directory()
408 : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
409 { }
410
411 // This is the usual constructor.
412 Search_directory(const char* name, bool put_in_sysroot)
413 : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
414 {
415 if (this->name_.empty())
416 this->name_ = ".";
417 }
418
419 // This is called if we have a sysroot. The sysroot is prefixed to
420 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
421 // set to true for any enries which are in the sysroot (this will
422 // naturally include any entries for which put_in_sysroot_ is true).
423 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
424 // passing SYSROOT to lrealpath.
425 void
426 add_sysroot(const char* sysroot, const char* canonical_sysroot);
427
428 // Get the directory name.
429 const std::string&
430 name() const
431 { return this->name_; }
432
433 // Return whether this directory is in the sysroot.
434 bool
435 is_in_sysroot() const
436 { return this->is_in_sysroot_; }
437
438 private:
439 std::string name_;
440 bool put_in_sysroot_;
441 bool is_in_sysroot_;
442 };
443
444 class General_options
445 {
446 private:
447 // NOTE: For every option that you add here, also consider if you
448 // should add it to Position_dependent_options.
449 DEFINE_special(help, options::TWO_DASHES, '\0',
450 N_("Report usage information"), NULL);
451 DEFINE_special(version, options::TWO_DASHES, 'v',
452 N_("Report version information"), NULL);
453 DEFINE_special(V, options::EXACTLY_ONE_DASH, '\0',
454 N_("Report version and target information"), NULL);
455
456 // These options are sorted approximately so that for each letter in
457 // the alphabet, we show the option whose shortname is that letter
458 // (if any) and then every longname that starts with that letter (in
459 // alphabetical order). For both, lowercase sorts before uppercase.
460 // The -z options come last.
461
462 DEFINE_bool(allow_shlib_undefined, options::TWO_DASHES, '\0', false,
463 N_("Allow unresolved references in shared libraries"),
464 N_("Do not allow unresolved references in shared libraries"));
465
466 DEFINE_bool(as_needed, options::TWO_DASHES, '\0', false,
467 N_("Only set DT_NEEDED for dynamic libs if used"),
468 N_("Always DT_NEEDED for dynamic libs"));
469
470 // This should really be an "enum", but it's too easy for folks to
471 // forget to update the list as they add new targets. So we just
472 // accept any string. We'll fail later (when the string is parsed),
473 // if the target isn't actually supported.
474 DEFINE_string(format, options::TWO_DASHES, 'b', "elf",
475 N_("Set input format"), ("[elf,binary]"));
476
477 DEFINE_bool(Bdynamic, options::ONE_DASH, '\0', true,
478 N_("-l searches for shared libraries"), NULL);
479 // Bstatic affects the same variable as Bdynamic, so we have to use
480 // the "special" macro to make that happen.
481 DEFINE_special(Bstatic, options::ONE_DASH, '\0',
482 N_("-l does not search for shared libraries"), NULL);
483
484 DEFINE_bool(Bsymbolic, options::ONE_DASH, '\0', false,
485 N_("Bind defined symbols locally"), NULL);
486
487 DEFINE_optional_string(build_id, options::TWO_DASHES, '\0', "sha1",
488 N_("Generate build ID note"),
489 N_("[=STYLE]"));
490
491 #ifdef HAVE_ZLIB_H
492 DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
493 N_("Compress .debug_* sections in the output file"),
494 ("[none,zlib]"),
495 {"none", "zlib"});
496 #else
497 DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
498 N_("Compress .debug_* sections in the output file"),
499 N_("[none]"),
500 {"none"});
501 #endif
502
503 DEFINE_bool(define_common, options::TWO_DASHES, 'd', false,
504 N_("Define common symbols"),
505 N_("Do not define common symbols"));
506 DEFINE_bool(dc, options::ONE_DASH, '\0', false,
507 N_("Alias for -d"), NULL);
508 DEFINE_bool(dp, options::ONE_DASH, '\0', false,
509 N_("Alias for -d"), NULL);
510
511 DEFINE_string(debug, options::TWO_DASHES, '\0', "",
512 N_("Turn on debugging"),
513 N_("[all,files,script,task][,...]"));
514
515 DEFINE_special(defsym, options::TWO_DASHES, '\0',
516 N_("Define a symbol"), N_("SYMBOL=EXPRESSION"));
517
518 DEFINE_optional_string(demangle, options::TWO_DASHES, '\0', NULL,
519 N_("Demangle C++ symbols in log messages"),
520 N_("[=STYLE]"));
521
522 DEFINE_bool(no_demangle, options::TWO_DASHES, '\0', false,
523 N_("Do not demangle C++ symbols in log messages"),
524 NULL);
525
526 DEFINE_bool(detect_odr_violations, options::TWO_DASHES, '\0', false,
527 N_("Try to detect violations of the One Definition Rule"),
528 NULL);
529
530 DEFINE_string(entry, options::TWO_DASHES, 'e', NULL,
531 N_("Set program start address"), N_("ADDRESS"));
532
533 DEFINE_bool(export_dynamic, options::TWO_DASHES, 'E', false,
534 N_("Export all dynamic symbols"), NULL);
535
536 DEFINE_bool(eh_frame_hdr, options::TWO_DASHES, '\0', false,
537 N_("Create exception frame header"), NULL);
538
539 DEFINE_string(soname, options::ONE_DASH, 'h', NULL,
540 N_("Set shared library name"), N_("FILENAME"));
541
542 DEFINE_double(hash_bucket_empty_fraction, options::TWO_DASHES, '\0', 0.0,
543 N_("Min fraction of empty buckets in dynamic hash"),
544 N_("FRACTION"));
545
546 DEFINE_enum(hash_style, options::TWO_DASHES, '\0', "sysv",
547 N_("Dynamic hash style"), N_("[sysv,gnu,both]"),
548 {"sysv", "gnu", "both"});
549
550 DEFINE_string(dynamic_linker, options::TWO_DASHES, 'I', NULL,
551 N_("Set dynamic linker path"), N_("PROGRAM"));
552
553 DEFINE_special(just_symbols, options::TWO_DASHES, '\0',
554 N_("Read only symbol values from FILE"), N_("FILE"));
555
556 DEFINE_special(library, options::TWO_DASHES, 'l',
557 N_("Search for library LIBNAME"), N_("LIBNAME"));
558
559 DEFINE_dirlist(library_path, options::TWO_DASHES, 'L',
560 N_("Add directory to search path"), N_("DIR"));
561
562 DEFINE_string(m, options::EXACTLY_ONE_DASH, 'm', "",
563 N_("Ignored for compatibility"), N_("EMULATION"));
564
565 DEFINE_enable(new_dtags, options::EXACTLY_TWO_DASHES, '\0', false,
566 N_("Enable use of DT_RUNPATH and DT_FLAGS"),
567 N_("Disable use of DT_RUNPATH and DT_FLAGS"));
568
569 DEFINE_bool(noinhibit_exec, options::TWO_DASHES, '\0', false,
570 N_("Create an output file even if errors occur"), NULL);
571
572 DEFINE_string(output, options::TWO_DASHES, 'o', "a.out",
573 N_("Set output file name"), N_("FILE"));
574
575 DEFINE_uint(optimize, options::EXACTLY_ONE_DASH, 'O', 0,
576 N_("Optimize output file size"), N_("LEVEL"));
577
578 DEFINE_string(oformat, options::EXACTLY_TWO_DASHES, '\0', "elf",
579 N_("Set output format"), N_("[binary]"));
580
581 DEFINE_bool(Qy, options::EXACTLY_ONE_DASH, '\0', false,
582 N_("Ignored for SVR4 compatibility"), NULL);
583
584 DEFINE_bool(emit_relocs, options::TWO_DASHES, 'q', false,
585 N_("Generate relocations in output"), NULL);
586
587 DEFINE_bool(relocatable, options::EXACTLY_ONE_DASH, 'r', false,
588 N_("Generate relocatable output"), NULL);
589
590 DEFINE_bool(relax, options::TWO_DASHES, '\0', false,
591 N_("Relax branches on certain targets"), NULL);
592
593 // -R really means -rpath, but can mean --just-symbols for
594 // compatibility with GNU ld. -rpath is always -rpath, so we list
595 // it separately.
596 DEFINE_special(R, options::EXACTLY_ONE_DASH, 'R',
597 N_("Add DIR to runtime search path"), N_("DIR"));
598
599 DEFINE_dirlist(rpath, options::ONE_DASH, '\0',
600 N_("Add DIR to runtime search path"), N_("DIR"));
601
602 DEFINE_dirlist(rpath_link, options::TWO_DASHES, '\0',
603 N_("Add DIR to link time shared library search path"),
604 N_("DIR"));
605
606 DEFINE_bool(strip_all, options::TWO_DASHES, 's', false,
607 N_("Strip all symbols"), NULL);
608 DEFINE_bool(strip_debug, options::TWO_DASHES, 'S', false,
609 N_("Strip debugging information"), NULL);
610 DEFINE_bool(strip_debug_gdb, options::TWO_DASHES, '\0', false,
611 N_("Strip debug symbols that are unused by gdb "
612 "(at least versions <= 6.7)"), NULL);
613
614 DEFINE_bool(shared, options::ONE_DASH, '\0', false,
615 N_("Generate shared library"), NULL);
616
617 // This is not actually special in any way, but I need to give it
618 // a non-standard accessor-function name because 'static' is a keyword.
619 DEFINE_special(static, options::ONE_DASH, '\0',
620 N_("Do not link against shared libraries"), NULL);
621
622 DEFINE_bool(stats, options::TWO_DASHES, '\0', false,
623 N_("Print resource usage statistics"), NULL);
624
625 DEFINE_string(sysroot, options::TWO_DASHES, '\0', "",
626 N_("Set target system root directory"), N_("DIR"));
627
628 DEFINE_special(script, options::TWO_DASHES, 'T',
629 N_("Read linker script"), N_("FILE"));
630
631 DEFINE_bool(threads, options::TWO_DASHES, '\0', false,
632 N_("Run the linker multi-threaded"),
633 N_("Do not run the linker multi-threaded"));
634 DEFINE_uint(thread_count, options::TWO_DASHES, '\0', 0,
635 N_("Number of threads to use"), N_("COUNT"));
636 DEFINE_uint(thread_count_initial, options::TWO_DASHES, '\0', 0,
637 N_("Number of threads to use in initial pass"), N_("COUNT"));
638 DEFINE_uint(thread_count_middle, options::TWO_DASHES, '\0', 0,
639 N_("Number of threads to use in middle pass"), N_("COUNT"));
640 DEFINE_uint(thread_count_final, options::TWO_DASHES, '\0', 0,
641 N_("Number of threads to use in final pass"), N_("COUNT"));
642
643 DEFINE_uint64(Tbss, options::ONE_DASH, '\0', -1U,
644 N_("Set the address of the bss segment"), N_("ADDRESS"));
645 DEFINE_uint64(Tdata, options::ONE_DASH, '\0', -1U,
646 N_("Set the address of the data segment"), N_("ADDRESS"));
647 DEFINE_uint64(Ttext, options::ONE_DASH, '\0', -1U,
648 N_("Set the address of the text segment"), N_("ADDRESS"));
649
650 DEFINE_bool(verbose, options::TWO_DASHES, '\0', false,
651 N_("Synonym for --debug=files"), NULL);
652
653 DEFINE_special(version_script, options::TWO_DASHES, '\0',
654 N_("Read version script"), N_("FILE"));
655
656 DEFINE_bool(whole_archive, options::TWO_DASHES, '\0', false,
657 N_("Include all archive contents"),
658 N_("Include only needed archive contents"));
659
660 DEFINE_special(wrap, options::TWO_DASHES, '\0',
661 N_("Use wrapper functions for SYMBOL"), N_("SYMBOL"));
662
663 DEFINE_string(Y, options::EXACTLY_ONE_DASH, 'Y', "",
664 N_("Default search path for Solaris compatibility"),
665 N_("PATH"));
666
667 DEFINE_special(start_group, options::TWO_DASHES, '(',
668 N_("Start a library search group"), NULL);
669 DEFINE_special(end_group, options::TWO_DASHES, ')',
670 N_("End a library search group"), NULL);
671
672 // The -z options.
673
674 // Both execstack and noexecstack differ from the default execstack_
675 // value, so we need to use different variables for them.
676 DEFINE_uint64(common_page_size, options::DASH_Z, '\0', 0,
677 N_("Set common page size to SIZE"), N_("SIZE"));
678 DEFINE_bool(defs, options::DASH_Z, '\0', false,
679 N_("Report undefined symbols (even with --shared)"),
680 NULL);
681 DEFINE_bool(execstack, options::DASH_Z, '\0', false,
682 N_("Mark output as requiring executable stack"), NULL);
683 DEFINE_uint64(max_page_size, options::DASH_Z, '\0', 0,
684 N_("Set maximum page size to SIZE"), N_("SIZE"));
685 DEFINE_bool(noexecstack, options::DASH_Z, '\0', false,
686 N_("Mark output as not requiring executable stack"), NULL);
687 DEFINE_bool(initfirst, options::DASH_Z, '\0', false,
688 N_("Mark DSO to be initialized first at runtime"),
689 NULL);
690 DEFINE_bool(interpose, options::DASH_Z, '\0', false,
691 N_("Mark object to interpose all DSOs but executable"),
692 NULL);
693 DEFINE_bool(loadfltr, options::DASH_Z, '\0', false,
694 N_("Mark object requiring immediate process"),
695 NULL);
696 DEFINE_bool(nodefaultlib, options::DASH_Z, '\0', false,
697 N_("Mark object not to use default search paths"),
698 NULL);
699 DEFINE_bool(nodelete, options::DASH_Z, '\0', false,
700 N_("Mark DSO non-deletable at runtime"),
701 NULL);
702 DEFINE_bool(nodlopen, options::DASH_Z, '\0', false,
703 N_("Mark DSO not available to dlopen"),
704 NULL);
705 DEFINE_bool(nodump, options::DASH_Z, '\0', false,
706 N_("Mark DSO not available to dldump"),
707 NULL);
708
709 public:
710 typedef options::Dir_list Dir_list;
711
712 General_options();
713
714 // Does post-processing on flags, making sure they all have
715 // non-conflicting values. Also converts some flags from their
716 // "standard" types (string, etc), to another type (enum, DirList),
717 // which can be accessed via a separate method. Dies if it notices
718 // any problems.
719 void finalize();
720
721 // The macro defines output() (based on --output), but that's a
722 // generic name. Provide this alternative name, which is clearer.
723 const char*
724 output_file_name() const
725 { return this->output(); }
726
727 // This is not defined via a flag, but combines flags to say whether
728 // the output is position-independent or not.
729 bool
730 output_is_position_independent() const
731 { return this->shared(); }
732
733 // This would normally be static(), and defined automatically, but
734 // since static is a keyword, we need to come up with our own name.
735 bool
736 is_static() const
737 { return static_; }
738
739 // In addition to getting the input and output formats as a string
740 // (via format() and oformat()), we also give access as an enum.
741 enum Object_format
742 {
743 // Ordinary ELF.
744 OBJECT_FORMAT_ELF,
745 // Straight binary format.
746 OBJECT_FORMAT_BINARY
747 };
748
749 // Note: these functions are not very fast.
750 Object_format format_enum() const;
751 Object_format oformat_enum() const;
752
753 // These are the best way to get access to the execstack state,
754 // not execstack() and noexecstack() which are hard to use properly.
755 bool
756 is_execstack_set() const
757 { return this->execstack_status_ != EXECSTACK_FROM_INPUT; }
758
759 bool
760 is_stack_executable() const
761 { return this->execstack_status_ == EXECSTACK_YES; }
762
763 // The --demangle option takes an optional string, and there is also
764 // a --no-demangle option. This is the best way to decide whether
765 // to demangle or not.
766 bool
767 do_demangle() const
768 { return this->do_demangle_; }
769
770 // Whether there are any symbols to wrap.
771 bool
772 any_wrap_symbols() const
773 { return !this->wrap_symbols_.empty(); }
774
775 // Whether to wrap SYMBOL.
776 bool
777 is_wrap_symbol(const char* symbol) const
778 {
779 return (this->wrap_symbols_.find(std::string(symbol))
780 != this->wrap_symbols_.end());
781 }
782
783 private:
784 // Don't copy this structure.
785 General_options(const General_options&);
786 General_options& operator=(const General_options&);
787
788 // Whether to mark the stack as executable.
789 enum Execstack
790 {
791 // Not set on command line.
792 EXECSTACK_FROM_INPUT,
793 // Mark the stack as executable (-z execstack).
794 EXECSTACK_YES,
795 // Mark the stack as not executable (-z noexecstack).
796 EXECSTACK_NO
797 };
798
799 void
800 set_execstack_status(Execstack value)
801 { this->execstack_status_ = value; }
802
803 void
804 set_do_demangle(bool value)
805 { this->do_demangle_ = value; }
806
807 void
808 set_static(bool value)
809 { static_ = value; }
810
811 // These are called by finalize() to set up the search-path correctly.
812 void
813 add_to_library_path_with_sysroot(const char* arg)
814 { this->add_search_directory_to_library_path(Search_directory(arg, true)); }
815
816 // Apply any sysroot to the directory lists.
817 void
818 add_sysroot();
819
820 // Whether to mark the stack as executable.
821 Execstack execstack_status_;
822 // Whether to do a static link.
823 bool static_;
824 // Whether to do demangling.
825 bool do_demangle_;
826 // List of symbols used with --wrap.
827 Unordered_set<std::string> wrap_symbols_;
828 };
829
830 // The position-dependent options. We use this to store the state of
831 // the commandline at a particular point in parsing for later
832 // reference. For instance, if we see "ld --whole-archive foo.a
833 // --no-whole-archive," we want to store the whole-archive option with
834 // foo.a, so when the time comes to parse foo.a we know we should do
835 // it in whole-archive mode. We could store all of General_options,
836 // but that's big, so we just pick the subset of flags that actually
837 // change in a position-dependent way.
838
839 #define DEFINE_posdep(varname__, type__) \
840 public: \
841 type__ \
842 varname__() const \
843 { return this->varname__##_; } \
844 \
845 void \
846 set_##varname__(type__ value) \
847 { this->varname__##_ = value; } \
848 private: \
849 type__ varname__##_
850
851 class Position_dependent_options
852 {
853 public:
854 Position_dependent_options(const General_options& options
855 = Position_dependent_options::default_options_)
856 { copy_from_options(options); }
857
858 void copy_from_options(const General_options& options)
859 {
860 this->set_as_needed(options.as_needed());
861 this->set_Bdynamic(options.Bdynamic());
862 this->set_format_enum(options.format_enum());
863 this->set_whole_archive(options.whole_archive());
864 }
865
866 DEFINE_posdep(as_needed, bool);
867 DEFINE_posdep(Bdynamic, bool);
868 DEFINE_posdep(format_enum, General_options::Object_format);
869 DEFINE_posdep(whole_archive, bool);
870
871 private:
872 // This is a General_options with everything set to its default
873 // value. A Position_dependent_options created with no argument
874 // will take its values from here.
875 static General_options default_options_;
876 };
877
878
879 // A single file or library argument from the command line.
880
881 class Input_file_argument
882 {
883 public:
884 // name: file name or library name
885 // is_lib: true if name is a library name: that is, emits the leading
886 // "lib" and trailing ".so"/".a" from the name
887 // extra_search_path: an extra directory to look for the file, prior
888 // to checking the normal library search path. If this is "",
889 // then no extra directory is added.
890 // just_symbols: whether this file only defines symbols.
891 // options: The position dependent options at this point in the
892 // command line, such as --whole-archive.
893 Input_file_argument()
894 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
895 options_()
896 { }
897
898 Input_file_argument(const char* name, bool is_lib,
899 const char* extra_search_path,
900 bool just_symbols,
901 const Position_dependent_options& options)
902 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
903 just_symbols_(just_symbols), options_(options)
904 { }
905
906 // You can also pass in a General_options instance instead of a
907 // Position_dependent_options. In that case, we extract the
908 // position-independent vars from the General_options and only store
909 // those.
910 Input_file_argument(const char* name, bool is_lib,
911 const char* extra_search_path,
912 bool just_symbols,
913 const General_options& options)
914 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
915 just_symbols_(just_symbols), options_(options)
916 { }
917
918 const char*
919 name() const
920 { return this->name_.c_str(); }
921
922 const Position_dependent_options&
923 options() const
924 { return this->options_; }
925
926 bool
927 is_lib() const
928 { return this->is_lib_; }
929
930 const char*
931 extra_search_path() const
932 {
933 return (this->extra_search_path_.empty()
934 ? NULL
935 : this->extra_search_path_.c_str());
936 }
937
938 // Return whether we should only read symbols from this file.
939 bool
940 just_symbols() const
941 { return this->just_symbols_; }
942
943 // Return whether this file may require a search using the -L
944 // options.
945 bool
946 may_need_search() const
947 { return this->is_lib_ || !this->extra_search_path_.empty(); }
948
949 private:
950 // We use std::string, not const char*, here for convenience when
951 // using script files, so that we do not have to preserve the string
952 // in that case.
953 std::string name_;
954 bool is_lib_;
955 std::string extra_search_path_;
956 bool just_symbols_;
957 Position_dependent_options options_;
958 };
959
960 // A file or library, or a group, from the command line.
961
962 class Input_argument
963 {
964 public:
965 // Create a file or library argument.
966 explicit Input_argument(Input_file_argument file)
967 : is_file_(true), file_(file), group_(NULL)
968 { }
969
970 // Create a group argument.
971 explicit Input_argument(Input_file_group* group)
972 : is_file_(false), group_(group)
973 { }
974
975 // Return whether this is a file.
976 bool
977 is_file() const
978 { return this->is_file_; }
979
980 // Return whether this is a group.
981 bool
982 is_group() const
983 { return !this->is_file_; }
984
985 // Return the information about the file.
986 const Input_file_argument&
987 file() const
988 {
989 gold_assert(this->is_file_);
990 return this->file_;
991 }
992
993 // Return the information about the group.
994 const Input_file_group*
995 group() const
996 {
997 gold_assert(!this->is_file_);
998 return this->group_;
999 }
1000
1001 Input_file_group*
1002 group()
1003 {
1004 gold_assert(!this->is_file_);
1005 return this->group_;
1006 }
1007
1008 private:
1009 bool is_file_;
1010 Input_file_argument file_;
1011 Input_file_group* group_;
1012 };
1013
1014 // A group from the command line. This is a set of arguments within
1015 // --start-group ... --end-group.
1016
1017 class Input_file_group
1018 {
1019 public:
1020 typedef std::vector<Input_argument> Files;
1021 typedef Files::const_iterator const_iterator;
1022
1023 Input_file_group()
1024 : files_()
1025 { }
1026
1027 // Add a file to the end of the group.
1028 void
1029 add_file(const Input_file_argument& arg)
1030 { this->files_.push_back(Input_argument(arg)); }
1031
1032 // Iterators to iterate over the group contents.
1033
1034 const_iterator
1035 begin() const
1036 { return this->files_.begin(); }
1037
1038 const_iterator
1039 end() const
1040 { return this->files_.end(); }
1041
1042 private:
1043 Files files_;
1044 };
1045
1046 // A list of files from the command line or a script.
1047
1048 class Input_arguments
1049 {
1050 public:
1051 typedef std::vector<Input_argument> Input_argument_list;
1052 typedef Input_argument_list::const_iterator const_iterator;
1053
1054 Input_arguments()
1055 : input_argument_list_(), in_group_(false)
1056 { }
1057
1058 // Add a file.
1059 void
1060 add_file(const Input_file_argument& arg);
1061
1062 // Start a group (the --start-group option).
1063 void
1064 start_group();
1065
1066 // End a group (the --end-group option).
1067 void
1068 end_group();
1069
1070 // Return whether we are currently in a group.
1071 bool
1072 in_group() const
1073 { return this->in_group_; }
1074
1075 // The number of entries in the list.
1076 int
1077 size() const
1078 { return this->input_argument_list_.size(); }
1079
1080 // Iterators to iterate over the list of input files.
1081
1082 const_iterator
1083 begin() const
1084 { return this->input_argument_list_.begin(); }
1085
1086 const_iterator
1087 end() const
1088 { return this->input_argument_list_.end(); }
1089
1090 // Return whether the list is empty.
1091 bool
1092 empty() const
1093 { return this->input_argument_list_.empty(); }
1094
1095 private:
1096 Input_argument_list input_argument_list_;
1097 bool in_group_;
1098 };
1099
1100
1101 // All the information read from the command line. These are held in
1102 // three separate structs: one to hold the options (--foo), one to
1103 // hold the filenames listed on the commandline, and one to hold
1104 // linker script information. This third is not a subset of the other
1105 // two because linker scripts can be specified either as options (via
1106 // -T) or as a file.
1107
1108 class Command_line
1109 {
1110 public:
1111 typedef Input_arguments::const_iterator const_iterator;
1112
1113 Command_line();
1114
1115 // Process the command line options. This will exit with an
1116 // appropriate error message if an unrecognized option is seen.
1117 void
1118 process(int argc, const char** argv);
1119
1120 // Process one command-line option. This takes the index of argv to
1121 // process, and returns the index for the next option. no_more_options
1122 // is set to true if argv[i] is "--".
1123 int
1124 process_one_option(int argc, const char** argv, int i,
1125 bool* no_more_options);
1126
1127 // Get the general options.
1128 const General_options&
1129 options() const
1130 { return this->options_; }
1131
1132 // Get the position dependent options.
1133 const Position_dependent_options&
1134 position_dependent_options() const
1135 { return this->position_options_; }
1136
1137 // Get the linker-script options.
1138 Script_options&
1139 script_options()
1140 { return this->script_options_; }
1141
1142 // Get the version-script options: a convenience routine.
1143 const Version_script_info&
1144 version_script() const
1145 { return *this->script_options_.version_script_info(); }
1146
1147 // Get the input files.
1148 Input_arguments&
1149 inputs()
1150 { return this->inputs_; }
1151
1152 // The number of input files.
1153 int
1154 number_of_input_files() const
1155 { return this->inputs_.size(); }
1156
1157 // Iterators to iterate over the list of input files.
1158
1159 const_iterator
1160 begin() const
1161 { return this->inputs_.begin(); }
1162
1163 const_iterator
1164 end() const
1165 { return this->inputs_.end(); }
1166
1167 private:
1168 Command_line(const Command_line&);
1169 Command_line& operator=(const Command_line&);
1170
1171 General_options options_;
1172 Position_dependent_options position_options_;
1173 Script_options script_options_;
1174 Input_arguments inputs_;
1175 };
1176
1177 } // End namespace gold.
1178
1179 #endif // !defined(GOLD_OPTIONS_H)
This page took 0.055251 seconds and 5 git commands to generate.