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