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