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