Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // options.c -- handle command line options for gold |
2 | ||
3 | #include <iostream> | |
4 | ||
5 | #include "gold.h" | |
6 | #include "options.h" | |
7 | ||
61ba1cf9 ILT |
8 | namespace gold |
9 | { | |
10 | ||
bae7f79e ILT |
11 | // The information we keep for a single command line option. |
12 | ||
61ba1cf9 | 13 | struct options::One_option |
bae7f79e ILT |
14 | { |
15 | // The single character option name, or '\0' if this is only a long | |
16 | // option. | |
17 | char short_option; | |
18 | ||
19 | // The long option name, or NULL if this is only a short option. | |
20 | const char* long_option; | |
21 | ||
22 | // Description of the option for --help output, or NULL if there is none. | |
23 | const char* doc; | |
24 | ||
25 | // How to print the option name in --help output, or NULL to use the | |
26 | // default. | |
27 | const char* help_output; | |
28 | ||
29 | // Long option dash control. This is ignored if long_option is | |
30 | // NULL. | |
31 | enum | |
32 | { | |
33 | // Long option normally takes one dash; two dashes are also | |
34 | // accepted. | |
35 | ONE_DASH, | |
36 | // Long option normally takes two dashes; one dash is also | |
37 | // accepted. | |
38 | TWO_DASHES, | |
39 | // Long option always takes two dashes. | |
40 | EXACTLY_TWO_DASHES | |
41 | } dash; | |
42 | ||
43 | // Function for special handling, or NULL. Returns the number of | |
44 | // arguments to skip. This will normally be at least 1, but it may | |
45 | // be 0 if this function changes *argv. ARG points to the location | |
46 | // in *ARGV where the option starts, which may be helpful for a | |
47 | // short option. | |
61ba1cf9 | 48 | int (*special)(int argc, char** argv, char *arg, Command_line*); |
bae7f79e ILT |
49 | |
50 | // If this is a position independent option which does not take an | |
51 | // argument, this is the member function to call to record it. | |
61ba1cf9 | 52 | void (General_options::*general_noarg)(); |
bae7f79e ILT |
53 | |
54 | // If this is a position independent function which takes an | |
55 | // argument, this is the member function to call to record it. | |
61ba1cf9 | 56 | void (General_options::*general_arg)(const char*); |
bae7f79e ILT |
57 | |
58 | // If this is a position dependent option which does not take an | |
59 | // argument, this is the member function to call to record it. | |
61ba1cf9 | 60 | void (Position_dependent_options::*dependent_noarg)(); |
bae7f79e ILT |
61 | |
62 | // If this is a position dependent option which takes an argument, | |
63 | // this is the member function to record it. | |
61ba1cf9 | 64 | void (Position_dependent_options::*dependent_arg)(const char*); |
bae7f79e ILT |
65 | |
66 | // Return whether this option takes an argument. | |
67 | bool | |
68 | takes_argument() const | |
69 | { return this->general_arg != NULL || this->dependent_arg != NULL; } | |
70 | }; | |
71 | ||
61ba1cf9 | 72 | class options::Command_line_options |
bae7f79e ILT |
73 | { |
74 | public: | |
75 | static const One_option options[]; | |
76 | static const int options_size; | |
77 | }; | |
78 | ||
61ba1cf9 ILT |
79 | } // End namespace gold. |
80 | ||
bae7f79e ILT |
81 | namespace |
82 | { | |
83 | ||
61ba1cf9 ILT |
84 | // Handle the special -l option, which adds an input file. |
85 | ||
86 | int | |
87 | library(int argc, char** argv, char* arg, gold::Command_line* cmdline) | |
88 | { | |
89 | return cmdline->process_l_option(argc, argv, arg); | |
90 | } | |
91 | ||
ead1e424 ILT |
92 | // Handle the special --start-group option. |
93 | ||
94 | int | |
95 | start_group(int, char**, char* arg, gold::Command_line* cmdline) | |
96 | { | |
97 | cmdline->start_group(arg); | |
98 | return 1; | |
99 | } | |
100 | ||
101 | // Handle the special --end-group option. | |
102 | ||
103 | int | |
104 | end_group(int, char**, char* arg, gold::Command_line* cmdline) | |
105 | { | |
106 | cmdline->end_group(arg); | |
107 | return 1; | |
108 | } | |
109 | ||
bae7f79e ILT |
110 | // Report usage information for ld --help, and exit. |
111 | ||
112 | int | |
113 | help(int, char**, char*, gold::Command_line*) | |
114 | { | |
115 | printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name); | |
116 | ||
117 | const int options_size = gold::options::Command_line_options::options_size; | |
118 | const gold::options::One_option* options = | |
119 | gold::options::Command_line_options::options; | |
120 | for (int i = 0; i < options_size; ++i) | |
121 | { | |
122 | if (options[i].doc == NULL) | |
123 | continue; | |
124 | ||
125 | printf(" "); | |
126 | int len = 2; | |
127 | bool comma = false; | |
128 | ||
129 | int j = i; | |
130 | do | |
131 | { | |
132 | if (options[j].help_output != NULL) | |
133 | { | |
134 | if (comma) | |
135 | { | |
136 | printf(", "); | |
137 | len += 2; | |
138 | } | |
139 | printf(options[j].help_output); | |
140 | len += std::strlen(options[i].help_output); | |
141 | } | |
142 | else | |
143 | { | |
144 | if (options[j].short_option != '\0') | |
145 | { | |
146 | if (comma) | |
147 | { | |
148 | printf(", "); | |
149 | len += 2; | |
150 | } | |
151 | printf("-%c", options[j].short_option); | |
152 | len += 2; | |
153 | } | |
154 | ||
155 | if (options[j].long_option != NULL) | |
156 | { | |
157 | if (comma) | |
158 | { | |
159 | printf(", "); | |
160 | len += 2; | |
161 | } | |
162 | if (options[j].dash == gold::options::One_option::ONE_DASH) | |
163 | { | |
164 | printf("-"); | |
165 | ++len; | |
166 | } | |
167 | else | |
168 | { | |
169 | printf("--"); | |
170 | len += 2; | |
171 | } | |
172 | printf("%s", options[j].long_option); | |
173 | len += std::strlen(options[j].long_option); | |
174 | } | |
175 | } | |
176 | ++j; | |
177 | } | |
178 | while (j < options_size && options[j].doc == NULL); | |
179 | ||
180 | if (len > 30) | |
181 | { | |
182 | printf("\n"); | |
183 | len = 0; | |
184 | } | |
185 | for (; len < 30; ++len) | |
186 | std::putchar(' '); | |
187 | ||
188 | std::puts(options[i].doc); | |
189 | } | |
190 | ||
191 | gold::gold_exit(true); | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
61ba1cf9 ILT |
196 | } // End anonymous namespace. |
197 | ||
198 | namespace gold | |
199 | { | |
bae7f79e ILT |
200 | |
201 | // Helper macros used to specify the options. We could also do this | |
202 | // using constructors, but then g++ would generate code to initialize | |
203 | // the array. We want the array to be initialized statically so that | |
204 | // we get better startup time. | |
205 | ||
206 | #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 207 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
208 | NULL, func, NULL, NULL, NULL } |
209 | #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 210 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
211 | NULL, NULL, func, NULL, NULL } |
212 | #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 213 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
214 | NULL, NULL, NULL, func, NULL } |
215 | #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 216 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
217 | NULL, NULL, NULL, NULL, func } |
218 | #define SPECIAL(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 219 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
220 | func, NULL, NULL, NULL, NULL } |
221 | ||
222 | // Here is the actual list of options which we accept. | |
223 | ||
61ba1cf9 ILT |
224 | const options::One_option |
225 | options::Command_line_options::options[] = | |
bae7f79e | 226 | { |
61ba1cf9 ILT |
227 | SPECIAL('l', "library", N_("Search for library LIBNAME"), |
228 | N_("-lLIBNAME --library LIBNAME"), TWO_DASHES, | |
229 | &library), | |
ead1e424 ILT |
230 | SPECIAL('(', "start-group", N_("Start a library search group"), NULL, |
231 | TWO_DASHES, &start_group), | |
232 | SPECIAL(')', "end-group", N_("End a library search group"), NULL, | |
233 | TWO_DASHES, &end_group), | |
dbe717ef ILT |
234 | GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"), |
235 | N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES, | |
236 | &General_options::set_dynamic_linker), | |
bae7f79e ILT |
237 | GENERAL_ARG('L', "library-path", N_("Add directory to search path"), |
238 | N_("-L DIR, --library-path DIR"), TWO_DASHES, | |
61ba1cf9 | 239 | &General_options::add_to_search_path), |
652ec9bd ILT |
240 | GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH, |
241 | &General_options::ignore), | |
61ba1cf9 ILT |
242 | GENERAL_ARG('o', "output", N_("Set output file name"), |
243 | N_("-o FILE, --output FILE"), TWO_DASHES, | |
244 | &General_options::set_output_file_name), | |
bae7f79e | 245 | GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL, |
61ba1cf9 | 246 | ONE_DASH, &General_options::set_relocatable), |
41f542e7 ILT |
247 | GENERAL_ARG('R', "rpath", N_("Add directory to runtime search path"), |
248 | N_("-R DIR, -rpath DIR"), ONE_DASH, | |
249 | &General_options::add_to_rpath), | |
92e059d8 ILT |
250 | GENERAL_NOARG('\0', "shared", N_("Generate shared library"), |
251 | NULL, ONE_DASH, &General_options::set_shared), | |
bae7f79e | 252 | GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"), |
61ba1cf9 | 253 | NULL, ONE_DASH, &General_options::set_static), |
dbe717ef ILT |
254 | POSDEP_NOARG('\0', "as-needed", |
255 | N_("Only set DT_NEEDED for following dynamic libs if used"), | |
256 | NULL, TWO_DASHES, &Position_dependent_options::set_as_needed), | |
257 | POSDEP_NOARG('\0', "no-as-needed", | |
258 | N_("Always DT_NEEDED for following dynamic libs (default)"), | |
259 | NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed), | |
4973341a ILT |
260 | POSDEP_NOARG('\0', "whole-archive", |
261 | N_("Include all archive contents"), | |
262 | NULL, TWO_DASHES, | |
263 | &Position_dependent_options::set_whole_archive), | |
264 | POSDEP_NOARG('\0', "no-whole-archive", | |
265 | N_("Include only needed archive contents"), | |
266 | NULL, TWO_DASHES, | |
267 | &Position_dependent_options::clear_whole_archive), | |
bae7f79e ILT |
268 | SPECIAL('\0', "help", N_("Report usage information"), NULL, |
269 | TWO_DASHES, &help) | |
270 | }; | |
271 | ||
61ba1cf9 | 272 | const int options::Command_line_options::options_size = |
bae7f79e ILT |
273 | sizeof (options) / sizeof (options[0]); |
274 | ||
275 | // The default values for the general options. | |
276 | ||
61ba1cf9 | 277 | General_options::General_options() |
dbe717ef ILT |
278 | : dynamic_linker_(NULL), |
279 | search_path_(), | |
61ba1cf9 ILT |
280 | output_file_name_("a.out"), |
281 | is_relocatable_(false), | |
4973341a | 282 | rpath_(), |
92e059d8 | 283 | is_shared_(false), |
61ba1cf9 | 284 | is_static_(false) |
bae7f79e ILT |
285 | { |
286 | } | |
287 | ||
288 | // The default values for the position dependent options. | |
289 | ||
61ba1cf9 | 290 | Position_dependent_options::Position_dependent_options() |
4973341a ILT |
291 | : do_static_search_(false), |
292 | as_needed_(false), | |
293 | include_whole_archive_(false) | |
bae7f79e ILT |
294 | { |
295 | } | |
296 | ||
dbe717ef ILT |
297 | // Input_arguments methods. |
298 | ||
299 | // Add a file to the list. | |
300 | ||
301 | void | |
302 | Input_arguments::add_file(const Input_file_argument& file) | |
303 | { | |
304 | if (!this->in_group_) | |
305 | this->input_argument_list_.push_back(Input_argument(file)); | |
306 | else | |
307 | { | |
a3ad94ed ILT |
308 | gold_assert(!this->input_argument_list_.empty()); |
309 | gold_assert(this->input_argument_list_.back().is_group()); | |
dbe717ef ILT |
310 | this->input_argument_list_.back().group()->add_file(file); |
311 | } | |
312 | } | |
313 | ||
314 | // Start a group. | |
315 | ||
316 | void | |
317 | Input_arguments::start_group() | |
318 | { | |
a3ad94ed | 319 | gold_assert(!this->in_group_); |
dbe717ef ILT |
320 | Input_file_group* group = new Input_file_group(); |
321 | this->input_argument_list_.push_back(Input_argument(group)); | |
322 | this->in_group_ = true; | |
323 | } | |
324 | ||
325 | // End a group. | |
326 | ||
327 | void | |
328 | Input_arguments::end_group() | |
329 | { | |
a3ad94ed | 330 | gold_assert(this->in_group_); |
dbe717ef ILT |
331 | this->in_group_ = false; |
332 | } | |
333 | ||
ead1e424 | 334 | // Command_line options. |
bae7f79e | 335 | |
61ba1cf9 | 336 | Command_line::Command_line() |
dbe717ef | 337 | : options_(), position_options_(), inputs_() |
bae7f79e ILT |
338 | { |
339 | } | |
340 | ||
341 | // Process the command line options. | |
342 | ||
343 | void | |
61ba1cf9 | 344 | Command_line::process(int argc, char** argv) |
bae7f79e | 345 | { |
61ba1cf9 ILT |
346 | const int options_size = options::Command_line_options::options_size; |
347 | const options::One_option* options = | |
348 | options::Command_line_options::options; | |
bae7f79e ILT |
349 | bool no_more_options = false; |
350 | int i = 0; | |
351 | while (i < argc) | |
352 | { | |
353 | if (argv[i][0] != '-' || no_more_options) | |
354 | { | |
ead1e424 | 355 | this->add_file(argv[i], false); |
bae7f79e ILT |
356 | ++i; |
357 | continue; | |
358 | } | |
359 | ||
360 | // Option starting with '-'. | |
361 | int dashes = 1; | |
362 | if (argv[i][1] == '-') | |
363 | { | |
364 | dashes = 2; | |
365 | if (argv[i][2] == '\0') | |
366 | { | |
367 | no_more_options = true; | |
368 | continue; | |
369 | } | |
370 | } | |
371 | ||
372 | // Look for a long option match. | |
373 | char* opt = argv[i] + dashes; | |
374 | char first = opt[0]; | |
375 | int skiparg = 0; | |
376 | char* arg = strchr(opt, '='); | |
377 | if (arg != NULL) | |
378 | *arg = '\0'; | |
379 | else if (i + 1 < argc) | |
380 | { | |
381 | arg = argv[i + 1]; | |
382 | skiparg = 1; | |
383 | } | |
384 | ||
385 | int j; | |
386 | for (j = 0; j < options_size; ++j) | |
387 | { | |
388 | if (options[j].long_option != NULL | |
389 | && (dashes == 2 | |
390 | || (options[j].dash | |
61ba1cf9 | 391 | != options::One_option::EXACTLY_TWO_DASHES)) |
bae7f79e ILT |
392 | && first == options[j].long_option[0] |
393 | && strcmp(opt, options[j].long_option) == 0) | |
394 | { | |
395 | if (options[j].special) | |
396 | i += options[j].special(argc - 1, argv + i, opt, this); | |
397 | else | |
398 | { | |
399 | if (!options[j].takes_argument()) | |
400 | { | |
401 | arg = NULL; | |
402 | skiparg = 0; | |
403 | } | |
404 | else | |
405 | { | |
406 | if (arg == NULL) | |
407 | this->usage(_("missing argument"), argv[i]); | |
408 | } | |
409 | this->apply_option(options[j], arg); | |
410 | i += skiparg + 1; | |
411 | } | |
412 | break; | |
413 | } | |
414 | } | |
415 | if (j < options_size) | |
416 | continue; | |
417 | ||
418 | // If we saw two dashes, we need to see a long option. | |
419 | if (dashes == 2) | |
420 | this->usage(_("unknown option"), argv[i]); | |
421 | ||
422 | // Look for a short option match. There may be more than one | |
423 | // short option in a given argument. | |
424 | bool done = false; | |
425 | char* s = argv[i] + 1; | |
426 | ++i; | |
427 | while (*s != '\0' && !done) | |
428 | { | |
429 | char opt = *s; | |
430 | int j; | |
431 | for (j = 0; j < options_size; ++j) | |
432 | { | |
433 | if (options[j].short_option == opt) | |
434 | { | |
435 | if (options[j].special) | |
436 | { | |
437 | // Undo the argument skip done above. | |
438 | --i; | |
439 | i += options[j].special(argc - i, argv + i, s, this); | |
440 | done = true; | |
441 | } | |
442 | else | |
443 | { | |
444 | arg = NULL; | |
445 | if (options[j].takes_argument()) | |
446 | { | |
447 | if (s[1] != '\0') | |
448 | { | |
449 | arg = s + 1; | |
450 | done = true; | |
451 | } | |
452 | else if (i < argc) | |
453 | { | |
454 | arg = argv[i]; | |
455 | ++i; | |
456 | } | |
457 | else | |
458 | this->usage(_("missing argument"), opt); | |
459 | } | |
460 | this->apply_option(options[j], arg); | |
461 | } | |
462 | break; | |
463 | } | |
464 | } | |
465 | ||
466 | if (j >= options_size) | |
467 | this->usage(_("unknown option"), *s); | |
468 | ||
469 | ++s; | |
470 | } | |
471 | } | |
61ba1cf9 | 472 | |
dbe717ef | 473 | if (this->inputs_.in_group()) |
ead1e424 ILT |
474 | { |
475 | fprintf(stderr, _("%s: missing group end"), program_name); | |
476 | this->usage(); | |
477 | } | |
478 | ||
61ba1cf9 ILT |
479 | // FIXME: We should only do this when configured in native mode. |
480 | this->options_.add_to_search_path("/lib"); | |
481 | this->options_.add_to_search_path("/usr/lib"); | |
bae7f79e ILT |
482 | } |
483 | ||
484 | // Apply a command line option. | |
485 | ||
486 | void | |
61ba1cf9 ILT |
487 | Command_line::apply_option(const options::One_option& opt, |
488 | const char* arg) | |
bae7f79e ILT |
489 | { |
490 | if (arg == NULL) | |
491 | { | |
492 | if (opt.general_noarg) | |
493 | (this->options_.*(opt.general_noarg))(); | |
494 | else if (opt.dependent_noarg) | |
495 | (this->position_options_.*(opt.dependent_noarg))(); | |
496 | else | |
61ba1cf9 | 497 | gold_unreachable(); |
bae7f79e ILT |
498 | } |
499 | else | |
500 | { | |
501 | if (opt.general_arg) | |
502 | (this->options_.*(opt.general_arg))(arg); | |
503 | else if (opt.dependent_arg) | |
504 | (this->position_options_.*(opt.dependent_arg))(arg); | |
505 | else | |
61ba1cf9 | 506 | gold_unreachable(); |
bae7f79e ILT |
507 | } |
508 | } | |
509 | ||
ead1e424 ILT |
510 | // Add an input file or library. |
511 | ||
512 | void | |
513 | Command_line::add_file(const char* name, bool is_lib) | |
514 | { | |
515 | Input_file_argument file(name, is_lib, this->position_options_); | |
dbe717ef | 516 | this->inputs_.add_file(file); |
ead1e424 ILT |
517 | } |
518 | ||
61ba1cf9 ILT |
519 | // Handle the -l option, which requires special treatment. |
520 | ||
521 | int | |
522 | Command_line::process_l_option(int argc, char** argv, char* arg) | |
523 | { | |
524 | int ret; | |
525 | const char* libname; | |
526 | if (arg[1] != '\0') | |
527 | { | |
528 | ret = 1; | |
529 | libname = arg + 1; | |
530 | } | |
531 | else if (argc > 1) | |
532 | { | |
533 | ret = 2; | |
534 | libname = argv[argc + 1]; | |
535 | } | |
536 | else | |
537 | this->usage(_("missing argument"), arg); | |
538 | ||
ead1e424 | 539 | this->add_file(libname, true); |
61ba1cf9 ILT |
540 | |
541 | return ret; | |
542 | } | |
543 | ||
ead1e424 ILT |
544 | // Handle the --start-group option. |
545 | ||
546 | void | |
547 | Command_line::start_group(const char* arg) | |
548 | { | |
dbe717ef | 549 | if (this->inputs_.in_group()) |
ead1e424 | 550 | this->usage(_("may not nest groups"), arg); |
dbe717ef | 551 | this->inputs_.start_group(); |
ead1e424 ILT |
552 | } |
553 | ||
554 | // Handle the --end-group option. | |
555 | ||
556 | void | |
557 | Command_line::end_group(const char* arg) | |
558 | { | |
dbe717ef | 559 | if (!this->inputs_.in_group()) |
ead1e424 | 560 | this->usage(_("group end without group start"), arg); |
dbe717ef | 561 | this->inputs_.end_group(); |
ead1e424 ILT |
562 | } |
563 | ||
bae7f79e ILT |
564 | // Report a usage error. */ |
565 | ||
566 | void | |
61ba1cf9 | 567 | Command_line::usage() |
bae7f79e ILT |
568 | { |
569 | fprintf(stderr, | |
570 | _("%s: use the --help option for usage information\n"), | |
61ba1cf9 ILT |
571 | program_name); |
572 | gold_exit(false); | |
bae7f79e ILT |
573 | } |
574 | ||
575 | void | |
61ba1cf9 | 576 | Command_line::usage(const char* msg, const char *opt) |
bae7f79e ILT |
577 | { |
578 | fprintf(stderr, | |
579 | _("%s: %s: %s\n"), | |
61ba1cf9 | 580 | program_name, opt, msg); |
bae7f79e ILT |
581 | this->usage(); |
582 | } | |
583 | ||
584 | void | |
61ba1cf9 | 585 | Command_line::usage(const char* msg, char opt) |
bae7f79e ILT |
586 | { |
587 | fprintf(stderr, | |
588 | _("%s: -%c: %s\n"), | |
61ba1cf9 | 589 | program_name, opt, msg); |
bae7f79e ILT |
590 | this->usage(); |
591 | } | |
61ba1cf9 ILT |
592 | |
593 | } // End namespace gold. |