* ld-cris/libdso-10.d: Adjust for change in objdump output.
[deliverable/binutils-gdb.git] / gold / options.h
CommitLineData
bae7f79e
ILT
1// options.h -- handle command line options for gold -*- C++ -*-
2
e5756efb 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
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
bae7f79e 23// General_options (from Command_line::options())
ee1fe73e 24// All the options (a.k.a. command-line flags)
bae7f79e
ILT
25// Input_argument (from Command_line::inputs())
26// The list of input files, including -l options.
ee1fe73e
ILT
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).
bae7f79e
ILT
35
36#ifndef GOLD_OPTIONS_H
37#define GOLD_OPTIONS_H
38
ca3a67a5 39#include <cstdlib>
bae7f79e 40#include <list>
61ba1cf9 41#include <string>
92e059d8 42#include <vector>
bae7f79e 43
0daa6f62 44#include "elfcpp.h"
3c2fafa5
ILT
45#include "script.h"
46
bae7f79e
ILT
47namespace gold
48{
49
50class Command_line;
ee1fe73e
ILT
51class General_options;
52class Search_directory;
ead1e424 53class Input_file_group;
3c2fafa5 54class Position_dependent_options;
0daa6f62 55class Target;
bae7f79e 56
ee1fe73e
ILT
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.
c7912668
ILT
60namespace options
61{
ee1fe73e
ILT
62typedef std::vector<Search_directory> Dir_list;
63
64// These routines convert from a string option to various types.
65// Each gives a fatal error if it cannot parse the argument.
66
67extern void
68parse_bool(const char* option_name, const char* arg, bool* retval);
69
70extern void
71parse_uint(const char* option_name, const char* arg, int* retval);
72
73extern void
c18476e7
ILT
74parse_uint64(const char* option_name, const char* arg, uint64_t* retval);
75
76extern void
77parse_double(const char* option_name, const char* arg, double* retval);
ee1fe73e
ILT
78
79extern void
80parse_string(const char* option_name, const char* arg, const char** retval);
81
82extern void
83parse_dirlist(const char* option_name, const char* arg, Dir_list* retval);
84
85extern void
86parse_choices(const char* option_name, const char* arg, const char** retval,
87 const char* choices[], int num_choices);
88
89struct Struct_var;
90
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".
98enum Dashes
99{
100 ONE_DASH, TWO_DASHES, EXACTLY_ONE_DASH, EXACTLY_TWO_DASHES, DASH_Z
101};
102
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.
120struct One_option
121{
122 std::string longname;
123 Dashes dashes;
124 char shortname;
125 const char* default_value;
126 const char* helpstring;
127 const char* helparg;
128 Struct_var* reader;
129
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)
134 {
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++
138 // function.
139 const char* pos = strchr(this->longname.c_str(), '_');
140 for (; pos; pos = strchr(pos, '_'))
141 this->longname[pos - this->longname.c_str()] = '-';
142
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();
148 }
149
150 // This option takes an argument iff helparg is not NULL.
151 bool
152 takes_argument() const
153 { return this->helparg != NULL; }
154
155 // Register this option with the global list of options.
156 void
157 register_option();
158
159 // Print this option to stdout (used with --help).
160 void
161 print() const;
162};
163
164// All options have a Struct_##varname that inherits from this and
165// actually implements parse_to_value for that option.
166struct Struct_var
167{
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.
175 virtual void
176 parse_to_value(const char* option, const char* arg,
177 Command_line* cmdline, General_options* options) = 0;
178 virtual
179 ~Struct_var() // To make gcc happy.
180 { }
181};
bae7f79e 182
ee1fe73e
ILT
183// This is for "special" options that aren't of any predefined type.
184struct Struct_special : public Struct_var
185{
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*,
188 Command_line*);
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)
194 { }
195
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); }
199
200 One_option option;
201 Parse_function parse;
202};
203
204} // End namespace options.
205
206
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__) \
215 public: \
216 param_type__ \
217 varname__() const \
218 { return this->varname__##_.value; } \
219 \
220 bool \
221 user_set_##varname__() const \
222 { return this->varname__##_.user_set_via_option; } \
223 \
224 private: \
225 struct Struct_##varname__ : public options::Struct_var \
226 { \
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__) \
231 { } \
232 \
233 void \
234 parse_to_value(const char* option_name, const char* arg, \
235 Command_line*, General_options*) \
236 { \
237 parse_fn__(option_name, arg, &this->value); \
238 this->user_set_via_option = true; \
239 } \
240 \
241 options::One_option option; \
242 bool user_set_via_option; \
243 type__ value; \
244 }; \
245 Struct_##varname__ varname__##_; \
246 void \
247 set_##varname__(param_type__ value) \
248 { this->varname__##_.value = value; }
249
250// These macros allow for easy addition of a new commandline option.
251
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 \
260 { \
261 Struct_no_##varname__() : option("no-" #varname__, dashes__, '\0', \
262 default_value__ ? "false" : "true", \
263 no_helpstring__, NULL, this) \
264 { } \
265 \
266 void \
267 parse_to_value(const char*, const char*, \
268 Command_line*, General_options* options) \
269 { options->set_##varname__(false); } \
270 \
271 options::One_option option; \
272 }; \
273 Struct_no_##varname__ no_##varname__##_initializer_
274
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)
280
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)
286
c18476e7
ILT
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)
292
ee1fe73e
ILT
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)
298
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) \
307 void \
308 add_to_##varname__(const char* new_value) \
309 { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
310 void \
311 add_search_directory_to_##varname__(const Search_directory& dir) \
312 { this->varname__##_.value.push_back(dir); }
313
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__) \
322 private: \
323 static void parse_choices_##varname__(const char* option_name, \
324 const char* arg, \
325 const char** retval) { \
326 const char* choices[] = __VA_ARGS__; \
327 options::parse_choices(option_name, arg, retval, \
328 choices, sizeof(choices) / sizeof(*choices)); \
329 }
330
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__) \
338 private: \
339 void parse_##varname__(const char* option, const char* arg, \
340 Command_line* inputs); \
341 struct Struct_##varname__ : public options::Struct_special \
342 { \
343 Struct_##varname__() \
344 : options::Struct_special(#varname__, dashes__, shortname__, \
345 &General_options::parse_##varname__, \
346 helpstring__, helparg__) \
347 { } \
348 }; \
349 Struct_##varname__ varname__##_initializer_
bae7f79e 350
bae7f79e 351
ad2d6943
ILT
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.
356
357class Search_directory
358{
359 public:
360 // We need a default constructor because we put this in a
361 // std::vector.
362 Search_directory()
363 : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
364 { }
365
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)
15893b88
ILT
369 {
370 if (this->name_.empty())
371 this->name_ = ".";
372 }
ad2d6943
ILT
373
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.
380 void
381 add_sysroot(const char* sysroot, const char* canonical_sysroot);
382
383 // Get the directory name.
384 const std::string&
385 name() const
386 { return this->name_; }
387
388 // Return whether this directory is in the sysroot.
389 bool
390 is_in_sysroot() const
391 { return this->is_in_sysroot_; }
392
393 private:
394 std::string name_;
395 bool put_in_sysroot_;
396 bool is_in_sysroot_;
397};
398
bae7f79e
ILT
399class General_options
400{
ee1fe73e
ILT
401 private:
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',
a4d4b13f 405 N_("Report usage information"), NULL);
ee1fe73e 406 DEFINE_special(version, options::TWO_DASHES, 'v',
a4d4b13f 407 N_("Report version information"), NULL);
ee1fe73e 408
fee2edb1
ILT
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.
414
ee1fe73e 415 DEFINE_bool(allow_shlib_undefined, options::TWO_DASHES, '\0', false,
a4d4b13f
ILT
416 N_("Allow unresolved references in shared libraries"),
417 N_("Do not allow unresolved references in shared libraries"));
ee1fe73e
ILT
418
419 DEFINE_bool(as_needed, options::TWO_DASHES, '\0', false,
a4d4b13f
ILT
420 N_("Only set DT_NEEDED for dynamic libs if used"),
421 N_("Always DT_NEEDED for dynamic libs"));
ee1fe73e 422
fee2edb1
ILT
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]"));
429
ee1fe73e 430 DEFINE_bool(Bdynamic, options::ONE_DASH, '\0', true,
a4d4b13f 431 N_("-l searches for shared libraries"), NULL);
ee1fe73e
ILT
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',
a4d4b13f 435 N_("-l does not search for shared libraries"), NULL);
ee1fe73e
ILT
436
437 DEFINE_bool(Bsymbolic, options::ONE_DASH, '\0', false,
a4d4b13f 438 N_("Bind defined symbols locally"), NULL);
ee1fe73e 439
ee1fe73e
ILT
440#ifdef HAVE_ZLIB_H
441 DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
a4d4b13f
ILT
442 N_("Compress .debug_* sections in the output file"),
443 ("[none,zlib]"),
ee1fe73e
ILT
444 {"none", "zlib"});
445#else
446 DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
a4d4b13f
ILT
447 N_("Compress .debug_* sections in the output file"),
448 N_("[none]"),
ee1fe73e
ILT
449 {"none"});
450#endif
0dfbdef4 451
ee1fe73e 452 DEFINE_bool(define_common, options::TWO_DASHES, 'd', false,
a4d4b13f
ILT
453 N_("Define common symbols"),
454 N_("Do not define common symbols"));
ee1fe73e 455 DEFINE_bool(dc, options::ONE_DASH, '\0', false,
a4d4b13f 456 N_("Alias for -d"), NULL);
ee1fe73e 457 DEFINE_bool(dp, options::ONE_DASH, '\0', false,
a4d4b13f 458 N_("Alias for -d"), NULL);
d391083d 459
fee2edb1 460 DEFINE_string(debug, options::TWO_DASHES, '\0', "",
2285a610
ILT
461 N_("Turn on debugging"),
462 N_("[all,files,script,task][,...]"));
fee2edb1 463
ee1fe73e 464 DEFINE_special(defsym, options::TWO_DASHES, '\0',
a4d4b13f 465 N_("Define a symbol"), N_("SYMBOL=EXPRESSION"));
a6badf5a 466
ee1fe73e
ILT
467 DEFINE_bool(demangle, options::TWO_DASHES, '\0',
468 getenv("COLLECT_NO_DEMANGLE") == NULL,
a4d4b13f
ILT
469 N_("Demangle C++ symbols in log messages"),
470 N_("Do not demangle C++ symbols in log messages"));
fced7afd 471
ee1fe73e 472 DEFINE_bool(detect_odr_violations, options::TWO_DASHES, '\0', false,
a4d4b13f 473 N_("Try to detect violations of the One Definition Rule"),
ee1fe73e
ILT
474 NULL);
475
476 DEFINE_string(entry, options::TWO_DASHES, 'e', NULL,
a4d4b13f 477 N_("Set program start address"), N_("ADDRESS"));
ee1fe73e
ILT
478
479 DEFINE_bool(export_dynamic, options::TWO_DASHES, 'E', false,
a4d4b13f 480 N_("Export all dynamic symbols"), NULL);
dbe717ef 481
ee1fe73e 482 DEFINE_bool(eh_frame_hdr, options::TWO_DASHES, '\0', false,
a4d4b13f 483 N_("Create exception frame header"), NULL);
ee1fe73e
ILT
484
485 DEFINE_string(soname, options::ONE_DASH, 'h', NULL,
a4d4b13f 486 N_("Set shared library name"), N_("FILENAME"));
ee1fe73e 487
c18476e7
ILT
488 DEFINE_double(hash_bucket_empty_fraction, options::TWO_DASHES, '\0', 0.0,
489 N_("Min fraction of empty buckets in dynamic hash"),
490 N_("FRACTION"));
491
13670ee6 492 DEFINE_enum(hash_style, options::TWO_DASHES, '\0', "sysv",
a4d4b13f 493 N_("Dynamic hash style"), N_("[sysv,gnu,both]"),
13670ee6
ILT
494 {"sysv", "gnu", "both"});
495
ee1fe73e 496 DEFINE_string(dynamic_linker, options::TWO_DASHES, 'I', NULL,
a4d4b13f 497 N_("Set dynamic linker path"), N_("PROGRAM"));
ee1fe73e 498
fee2edb1
ILT
499 DEFINE_special(just_symbols, options::TWO_DASHES, '\0',
500 N_("Read only symbol values from FILE"), N_("FILE"));
501
ee1fe73e 502 DEFINE_special(library, options::TWO_DASHES, 'l',
a4d4b13f 503 N_("Search for library LIBNAME"), N_("LIBNAME"));
bae7f79e 504
ee1fe73e 505 DEFINE_dirlist(library_path, options::TWO_DASHES, 'L',
a4d4b13f 506 N_("Add directory to search path"), N_("DIR"));
ee1fe73e
ILT
507
508 DEFINE_string(m, options::EXACTLY_ONE_DASH, 'm', "",
a4d4b13f 509 N_("Ignored for compatibility"), N_("EMULATION"));
ee1fe73e
ILT
510
511 DEFINE_string(output, options::TWO_DASHES, 'o', "a.out",
a4d4b13f 512 N_("Set output file name"), N_("FILE"));
ee1fe73e
ILT
513
514 DEFINE_uint(optimize, options::EXACTLY_ONE_DASH, 'O', 0,
a4d4b13f 515 N_("Optimize output file size"), N_("LEVEL"));
ee1fe73e 516
a4d4b13f
ILT
517 DEFINE_string(oformat, options::EXACTLY_TWO_DASHES, '\0', "elf",
518 N_("Set output format"), N_("[binary]"));
ee1fe73e
ILT
519
520 DEFINE_bool(emit_relocs, options::TWO_DASHES, 'q', false,
a4d4b13f 521 N_("Generate relocations in output"), NULL);
ee1fe73e
ILT
522
523 DEFINE_bool(relocatable, options::EXACTLY_ONE_DASH, 'r', false,
a4d4b13f 524 N_("Generate relocatable output"), NULL);
ee1fe73e
ILT
525
526 // -R really means -rpath, but can mean --just-symbols for
527 // compatibility with GNU ld. -rpath is always -rpath, so we list
528 // it separately.
529 DEFINE_special(R, options::EXACTLY_ONE_DASH, 'R',
a4d4b13f 530 N_("Add DIR to runtime search path"), N_("DIR"));
ee1fe73e
ILT
531
532 DEFINE_dirlist(rpath, options::ONE_DASH, '\0',
a4d4b13f 533 N_("Add DIR to runtime search path"), N_("DIR"));
ee1fe73e 534
ee1fe73e 535 DEFINE_dirlist(rpath_link, options::TWO_DASHES, '\0',
a4d4b13f
ILT
536 N_("Add DIR to link time shared library search path"),
537 N_("DIR"));
ee1fe73e
ILT
538
539 DEFINE_bool(strip_all, options::TWO_DASHES, 's', false,
a4d4b13f 540 N_("Strip all symbols"), NULL);
fee2edb1
ILT
541 DEFINE_bool(strip_debug, options::TWO_DASHES, 'S', false,
542 N_("Strip debugging information"), NULL);
ee1fe73e 543 DEFINE_bool(strip_debug_gdb, options::TWO_DASHES, '\0', false,
a4d4b13f 544 N_("Strip debug symbols that are unused by gdb "
ee1fe73e 545 "(at least versions <= 6.7)"), NULL);
ee1fe73e
ILT
546
547 DEFINE_bool(shared, options::ONE_DASH, '\0', false,
a4d4b13f 548 N_("Generate shared library"), NULL);
ee1fe73e
ILT
549
550 // This is not actually special in any way, but I need to give it
551 // a non-standard accessor-function name because 'static' is a keyword.
552 DEFINE_special(static, options::ONE_DASH, '\0',
a4d4b13f 553 N_("Do not link against shared libraries"), NULL);
ee1fe73e
ILT
554
555 DEFINE_bool(stats, options::TWO_DASHES, '\0', false,
a4d4b13f 556 N_("Print resource usage statistics"), NULL);
ee1fe73e
ILT
557
558 DEFINE_string(sysroot, options::TWO_DASHES, '\0', "",
a4d4b13f 559 N_("Set target system root directory"), N_("DIR"));
ee1fe73e 560
ee1fe73e 561 DEFINE_special(script, options::TWO_DASHES, 'T',
a4d4b13f 562 N_("Read linker script"), N_("FILE"));
ee1fe73e
ILT
563
564 DEFINE_bool(threads, options::TWO_DASHES, '\0', false,
a4d4b13f
ILT
565 N_("Run the linker multi-threaded"),
566 N_("Do not run the linker multi-threaded"));
ee1fe73e 567 DEFINE_uint(thread_count, options::TWO_DASHES, '\0', 0,
a4d4b13f 568 N_("Number of threads to use"), N_("COUNT"));
ee1fe73e 569 DEFINE_uint(thread_count_initial, options::TWO_DASHES, '\0', 0,
a4d4b13f 570 N_("Number of threads to use in initial pass"), N_("COUNT"));
ee1fe73e 571 DEFINE_uint(thread_count_middle, options::TWO_DASHES, '\0', 0,
a4d4b13f 572 N_("Number of threads to use in middle pass"), N_("COUNT"));
ee1fe73e 573 DEFINE_uint(thread_count_final, options::TWO_DASHES, '\0', 0,
a4d4b13f 574 N_("Number of threads to use in final pass"), N_("COUNT"));
ee1fe73e 575
fee2edb1
ILT
576 DEFINE_uint64(Tbss, options::ONE_DASH, '\0', -1U,
577 N_("Set the address of the bss segment"), N_("ADDRESS"));
578 DEFINE_uint64(Tdata, options::ONE_DASH, '\0', -1U,
579 N_("Set the address of the data segment"), N_("ADDRESS"));
580 DEFINE_uint64(Ttext, options::ONE_DASH, '\0', -1U,
581 N_("Set the address of the text segment"), N_("ADDRESS"));
582
2285a610
ILT
583 DEFINE_bool(verbose, options::TWO_DASHES, '\0', false,
584 N_("Synonym for --debug=files"), NULL);
585
fee2edb1
ILT
586 DEFINE_special(version_script, options::TWO_DASHES, '\0',
587 N_("Read version script"), N_("FILE"));
588
ee1fe73e 589 DEFINE_bool(whole_archive, options::TWO_DASHES, '\0', false,
a4d4b13f
ILT
590 N_("Include all archive contents"),
591 N_("Include only needed archive contents"));
ee1fe73e
ILT
592
593 DEFINE_special(start_group, options::TWO_DASHES, '(',
a4d4b13f 594 N_("Start a library search group"), NULL);
ee1fe73e 595 DEFINE_special(end_group, options::TWO_DASHES, ')',
a4d4b13f 596 N_("End a library search group"), NULL);
ee1fe73e 597
fee2edb1 598 // The -z options.
ee1fe73e
ILT
599
600 // Both execstack and noexecstack differ from the default execstack_
601 // value, so we need to use different variables for them.
fee2edb1
ILT
602 DEFINE_uint64(common_page_size, options::DASH_Z, '\0', 0,
603 N_("Set common page size to SIZE"), N_("SIZE"));
eb42429a
ILT
604 DEFINE_bool(defs, options::DASH_Z, '\0', false,
605 N_("Report undefined symbols (even with --shared)"),
606 NULL);
ee1fe73e 607 DEFINE_bool(execstack, options::DASH_Z, '\0', false,
a4d4b13f 608 N_("Mark output as requiring executable stack"), NULL);
ee1fe73e 609 DEFINE_uint64(max_page_size, options::DASH_Z, '\0', 0,
a4d4b13f 610 N_("Set maximum page size to SIZE"), N_("SIZE"));
fee2edb1
ILT
611 DEFINE_bool(noexecstack, options::DASH_Z, '\0', false,
612 N_("Mark output as not requiring executable stack"), NULL);
bae7f79e 613
ee1fe73e
ILT
614 public:
615 typedef options::Dir_list Dir_list;
ca3a67a5 616
ee1fe73e 617 General_options();
61ba1cf9 618
ee1fe73e
ILT
619 // Does post-processing on flags, making sure they all have
620 // non-conflicting values. Also converts some flags from their
621 // "standard" types (string, etc), to another type (enum, DirList),
622 // which can be accessed via a separate method. Dies if it notices
623 // any problems.
624 void finalize();
516cb3d0 625
ee1fe73e
ILT
626 // The macro defines output() (based on --output), but that's a
627 // generic name. Provide this alternative name, which is clearer.
8851ecca 628 const char*
ee1fe73e
ILT
629 output_file_name() const
630 { return this->output(); }
92e059d8 631
8851ecca
ILT
632 // This is not defined via a flag, but combines flags to say whether
633 // the output is position-independent or not.
634 bool
635 output_is_position_independent() const
636 { return this->shared(); }
637
ee1fe73e
ILT
638 // This would normally be static(), and defined automatically, but
639 // since static is a keyword, we need to come up with our own name.
bae7f79e
ILT
640 bool
641 is_static() const
ee1fe73e 642 { return static_; }
756ac4a8 643
ee1fe73e
ILT
644 // In addition to getting the input and output formats as a string
645 // (via format() and oformat()), we also give access as an enum.
646 enum Object_format
647 {
648 // Ordinary ELF.
649 OBJECT_FORMAT_ELF,
650 // Straight binary format.
651 OBJECT_FORMAT_BINARY
652 };
fe9a4c12 653
ee1fe73e
ILT
654 // Note: these functions are not very fast.
655 Object_format format_enum() const;
656 Object_format oformat_enum() const;
fe9a4c12 657
ee1fe73e
ILT
658 // These are the best way to get access to the execstack state,
659 // not execstack() and noexecstack() which are hard to use properly.
35cdfc9a
ILT
660 bool
661 is_execstack_set() const
ee1fe73e 662 { return this->execstack_status_ != EXECSTACK_FROM_INPUT; }
35cdfc9a
ILT
663
664 bool
665 is_stack_executable() const
ee1fe73e 666 { return this->execstack_status_ == EXECSTACK_YES; }
c7912668 667
bae7f79e 668 private:
dbe717ef
ILT
669 // Don't copy this structure.
670 General_options(const General_options&);
671 General_options& operator=(const General_options&);
672
35cdfc9a
ILT
673 // Whether to mark the stack as executable.
674 enum Execstack
675 {
676 // Not set on command line.
677 EXECSTACK_FROM_INPUT,
ee1fe73e 678 // Mark the stack as executable (-z execstack).
35cdfc9a 679 EXECSTACK_YES,
ee1fe73e 680 // Mark the stack as not executable (-z noexecstack).
35cdfc9a
ILT
681 EXECSTACK_NO
682 };
683
ee1fe73e 684 Execstack execstack_status_;
92e059d8 685 void
ee1fe73e
ILT
686 set_execstack_status(Execstack value)
687 { execstack_status_ = value; }
92e059d8 688
ee1fe73e 689 bool static_;
bae7f79e 690 void
45aa233b 691 set_static(bool value)
ee1fe73e 692 { static_ = value; }
652ec9bd 693
ee1fe73e 694 // These are called by finalize() to set up the search-path correctly.
35cdfc9a 695 void
ee1fe73e
ILT
696 add_to_library_path_with_sysroot(const char* arg)
697 { this->add_search_directory_to_library_path(Search_directory(arg, true)); }
c7912668 698
ad2d6943
ILT
699 // Apply any sysroot to the directory lists.
700 void
701 add_sysroot();
bae7f79e
ILT
702};
703
ee1fe73e
ILT
704// The position-dependent options. We use this to store the state of
705// the commandline at a particular point in parsing for later
706// reference. For instance, if we see "ld --whole-archive foo.a
707// --no-whole-archive," we want to store the whole-archive option with
708// foo.a, so when the time comes to parse foo.a we know we should do
709// it in whole-archive mode. We could store all of General_options,
710// but that's big, so we just pick the subset of flags that actually
711// change in a position-dependent way.
712
713#define DEFINE_posdep(varname__, type__) \
714 public: \
715 type__ \
716 varname__() const \
717 { return this->varname__##_; } \
718 \
719 void \
720 set_##varname__(type__ value) \
721 { this->varname__##_ = value; } \
722 private: \
723 type__ varname__##_
bae7f79e
ILT
724
725class Position_dependent_options
726{
727 public:
ee1fe73e
ILT
728 Position_dependent_options(const General_options& options
729 = Position_dependent_options::default_options_)
730 { copy_from_options(options); }
bae7f79e 731
ee1fe73e
ILT
732 void copy_from_options(const General_options& options)
733 {
734 this->set_as_needed(options.as_needed());
735 this->set_Bdynamic(options.Bdynamic());
736 this->set_format_enum(options.format_enum());
737 this->set_whole_archive(options.whole_archive());
738 }
bc644c6c 739
ee1fe73e
ILT
740 DEFINE_posdep(as_needed, bool);
741 DEFINE_posdep(Bdynamic, bool);
742 DEFINE_posdep(format_enum, General_options::Object_format);
743 DEFINE_posdep(whole_archive, bool);
7cc619c3 744
dbe717ef 745 private:
ee1fe73e
ILT
746 // This is a General_options with everything set to its default
747 // value. A Position_dependent_options created with no argument
748 // will take its values from here.
749 static General_options default_options_;
bae7f79e
ILT
750};
751
ee1fe73e 752
bae7f79e
ILT
753// A single file or library argument from the command line.
754
ead1e424 755class Input_file_argument
bae7f79e
ILT
756{
757 public:
51dee2fe
ILT
758 // name: file name or library name
759 // is_lib: true if name is a library name: that is, emits the leading
760 // "lib" and trailing ".so"/".a" from the name
761 // extra_search_path: an extra directory to look for the file, prior
762 // to checking the normal library search path. If this is "",
763 // then no extra directory is added.
88dd47ac 764 // just_symbols: whether this file only defines symbols.
51dee2fe 765 // options: The position dependent options at this point in the
ad2d6943 766 // command line, such as --whole-archive.
ead1e424 767 Input_file_argument()
88dd47ac
ILT
768 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
769 options_()
ead1e424
ILT
770 { }
771
772 Input_file_argument(const char* name, bool is_lib,
51dee2fe 773 const char* extra_search_path,
ee1fe73e
ILT
774 bool just_symbols,
775 const Position_dependent_options& options)
776 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
777 just_symbols_(just_symbols), options_(options)
778 { }
779
780 // You can also pass in a General_options instance instead of a
781 // Position_dependent_options. In that case, we extract the
782 // position-independent vars from the General_options and only store
783 // those.
784 Input_file_argument(const char* name, bool is_lib,
785 const char* extra_search_path,
786 bool just_symbols,
787 const General_options& options)
51dee2fe 788 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
88dd47ac 789 just_symbols_(just_symbols), options_(options)
bae7f79e
ILT
790 { }
791
792 const char*
793 name() const
dbe717ef 794 { return this->name_.c_str(); }
bae7f79e
ILT
795
796 const Position_dependent_options&
797 options() const
798 { return this->options_; }
799
800 bool
801 is_lib() const
61ba1cf9 802 { return this->is_lib_; }
bae7f79e 803
51dee2fe
ILT
804 const char*
805 extra_search_path() const
806 {
807 return (this->extra_search_path_.empty()
808 ? NULL
ee1fe73e 809 : this->extra_search_path_.c_str());
51dee2fe
ILT
810 }
811
88dd47ac
ILT
812 // Return whether we should only read symbols from this file.
813 bool
814 just_symbols() const
815 { return this->just_symbols_; }
816
51dee2fe
ILT
817 // Return whether this file may require a search using the -L
818 // options.
819 bool
820 may_need_search() const
821 { return this->is_lib_ || !this->extra_search_path_.empty(); }
822
bae7f79e 823 private:
dbe717ef
ILT
824 // We use std::string, not const char*, here for convenience when
825 // using script files, so that we do not have to preserve the string
826 // in that case.
827 std::string name_;
61ba1cf9 828 bool is_lib_;
51dee2fe 829 std::string extra_search_path_;
88dd47ac 830 bool just_symbols_;
bae7f79e
ILT
831 Position_dependent_options options_;
832};
833
ead1e424
ILT
834// A file or library, or a group, from the command line.
835
836class Input_argument
837{
838 public:
839 // Create a file or library argument.
840 explicit Input_argument(Input_file_argument file)
841 : is_file_(true), file_(file), group_(NULL)
842 { }
843
844 // Create a group argument.
845 explicit Input_argument(Input_file_group* group)
846 : is_file_(false), group_(group)
847 { }
848
849 // Return whether this is a file.
850 bool
851 is_file() const
852 { return this->is_file_; }
853
854 // Return whether this is a group.
855 bool
856 is_group() const
857 { return !this->is_file_; }
858
859 // Return the information about the file.
860 const Input_file_argument&
861 file() const
862 {
a3ad94ed 863 gold_assert(this->is_file_);
ead1e424
ILT
864 return this->file_;
865 }
866
867 // Return the information about the group.
868 const Input_file_group*
869 group() const
870 {
a3ad94ed 871 gold_assert(!this->is_file_);
ead1e424
ILT
872 return this->group_;
873 }
874
875 Input_file_group*
876 group()
877 {
a3ad94ed 878 gold_assert(!this->is_file_);
ead1e424
ILT
879 return this->group_;
880 }
881
882 private:
883 bool is_file_;
884 Input_file_argument file_;
885 Input_file_group* group_;
886};
887
888// A group from the command line. This is a set of arguments within
889// --start-group ... --end-group.
890
891class Input_file_group
92e059d8 892{
ead1e424
ILT
893 public:
894 typedef std::vector<Input_argument> Files;
895 typedef Files::const_iterator const_iterator;
896
897 Input_file_group()
898 : files_()
899 { }
900
901 // Add a file to the end of the group.
902 void
903 add_file(const Input_file_argument& arg)
904 { this->files_.push_back(Input_argument(arg)); }
905
906 // Iterators to iterate over the group contents.
907
908 const_iterator
909 begin() const
910 { return this->files_.begin(); }
911
912 const_iterator
913 end() const
914 { return this->files_.end(); }
915
916 private:
917 Files files_;
92e059d8
ILT
918};
919
dbe717ef
ILT
920// A list of files from the command line or a script.
921
922class Input_arguments
923{
924 public:
925 typedef std::vector<Input_argument> Input_argument_list;
926 typedef Input_argument_list::const_iterator const_iterator;
927
928 Input_arguments()
929 : input_argument_list_(), in_group_(false)
930 { }
931
932 // Add a file.
933 void
934 add_file(const Input_file_argument& arg);
935
936 // Start a group (the --start-group option).
937 void
938 start_group();
939
940 // End a group (the --end-group option).
941 void
942 end_group();
943
944 // Return whether we are currently in a group.
945 bool
946 in_group() const
947 { return this->in_group_; }
948
fe9a4c12
ILT
949 // The number of entries in the list.
950 int
951 size() const
952 { return this->input_argument_list_.size(); }
953
dbe717ef
ILT
954 // Iterators to iterate over the list of input files.
955
956 const_iterator
957 begin() const
958 { return this->input_argument_list_.begin(); }
959
960 const_iterator
961 end() const
962 { return this->input_argument_list_.end(); }
963
964 // Return whether the list is empty.
965 bool
966 empty() const
967 { return this->input_argument_list_.empty(); }
968
969 private:
970 Input_argument_list input_argument_list_;
971 bool in_group_;
972};
973
ee1fe73e
ILT
974
975// All the information read from the command line. These are held in
976// three separate structs: one to hold the options (--foo), one to
977// hold the filenames listed on the commandline, and one to hold
978// linker script information. This third is not a subset of the other
979// two because linker scripts can be specified either as options (via
980// -T) or as a file.
bae7f79e
ILT
981
982class Command_line
983{
984 public:
ead1e424
ILT
985 typedef Input_arguments::const_iterator const_iterator;
986
a5dc0706 987 Command_line();
bae7f79e
ILT
988
989 // Process the command line options. This will exit with an
990 // appropriate error message if an unrecognized option is seen.
991 void
ee1fe73e 992 process(int argc, const char** argv);
bae7f79e 993
a0451b38 994 // Process one command-line option. This takes the index of argv to
ee1fe73e
ILT
995 // process, and returns the index for the next option. no_more_options
996 // is set to true if argv[i] is "--".
61ba1cf9 997 int
ee1fe73e
ILT
998 process_one_option(int argc, const char** argv, int i,
999 bool* no_more_options);
3c2fafa5 1000
61ba1cf9 1001 // Get the general options.
bae7f79e
ILT
1002 const General_options&
1003 options() const
1004 { return this->options_; }
1005
3c2fafa5
ILT
1006 // Get the position dependent options.
1007 const Position_dependent_options&
1008 position_dependent_options() const
1009 { return this->position_options_; }
1010
a5dc0706
ILT
1011 // Get the linker-script options.
1012 Script_options&
e5756efb 1013 script_options()
a5dc0706 1014 { return this->script_options_; }
e5756efb 1015
a5dc0706
ILT
1016 // Get the version-script options: a convenience routine.
1017 const Version_script_info&
1018 version_script() const
1019 { return *this->script_options_.version_script_info(); }
e5756efb 1020
ee1fe73e
ILT
1021 // Get the input files.
1022 Input_arguments&
1023 inputs()
1024 { return this->inputs_; }
1025
fe9a4c12
ILT
1026 // The number of input files.
1027 int
1028 number_of_input_files() const
1029 { return this->inputs_.size(); }
1030
ead1e424
ILT
1031 // Iterators to iterate over the list of input files.
1032
1033 const_iterator
1034 begin() const
1035 { return this->inputs_.begin(); }
1036
1037 const_iterator
1038 end() const
1039 { return this->inputs_.end(); }
bae7f79e
ILT
1040
1041 private:
ead1e424
ILT
1042 Command_line(const Command_line&);
1043 Command_line& operator=(const Command_line&);
1044
bae7f79e
ILT
1045 General_options options_;
1046 Position_dependent_options position_options_;
a5dc0706 1047 Script_options script_options_;
ead1e424 1048 Input_arguments inputs_;
bae7f79e
ILT
1049};
1050
1051} // End namespace gold.
1052
1053#endif // !defined(GOLD_OPTIONS_H)
This page took 0.195915 seconds and 4 git commands to generate.