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