2007-09-08 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / gold / options.cc
CommitLineData
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
8namespace gold
9{
10
bae7f79e
ILT
11// The information we keep for a single command line option.
12
61ba1cf9 13struct 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 72class 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
81namespace
82{
83
61ba1cf9
ILT
84// Handle the special -l option, which adds an input file.
85
86int
87library(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
94int
95start_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
103int
104end_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
112int
113help(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);
a6badf5a 141 comma = true;
bae7f79e
ILT
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;
a6badf5a 154 comma = true;
bae7f79e
ILT
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);
a6badf5a 176 comma = true;
bae7f79e
ILT
177 }
178 }
179 ++j;
180 }
181 while (j < options_size && options[j].doc == NULL);
182
a6badf5a 183 if (len >= 30)
bae7f79e
ILT
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
61ba1cf9
ILT
199} // End anonymous namespace.
200
201namespace gold
202{
bae7f79e
ILT
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) \
61ba1cf9 210 { short_option, long_option, doc, help, options::One_option::dash, \
bae7f79e
ILT
211 NULL, func, NULL, NULL, NULL }
212#define GENERAL_ARG(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, func, NULL, NULL }
215#define POSDEP_NOARG(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, func, NULL }
218#define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
61ba1cf9 219 { short_option, long_option, doc, help, options::One_option::dash, \
bae7f79e
ILT
220 NULL, NULL, NULL, NULL, func }
221#define SPECIAL(short_option, long_option, doc, help, dash, func) \
61ba1cf9 222 { short_option, long_option, doc, help, options::One_option::dash, \
bae7f79e
ILT
223 func, NULL, NULL, NULL, NULL }
224
225// Here is the actual list of options which we accept.
226
61ba1cf9
ILT
227const options::One_option
228options::Command_line_options::options[] =
bae7f79e 229{
61ba1cf9 230 SPECIAL('l', "library", N_("Search for library LIBNAME"),
a6badf5a 231 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
61ba1cf9 232 &library),
ead1e424
ILT
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),
a6badf5a
ILT
237 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
238 NULL, TWO_DASHES, &General_options::set_export_dynamic),
dbe717ef
ILT
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),
bae7f79e
ILT
242 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
243 N_("-L DIR, --library-path DIR"), TWO_DASHES,
61ba1cf9 244 &General_options::add_to_search_path),
652ec9bd
ILT
245 GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
246 &General_options::ignore),
61ba1cf9
ILT
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),
bae7f79e 250 GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
61ba1cf9 251 ONE_DASH, &General_options::set_relocatable),
15b3cfae 252 GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
41f542e7
ILT
253 N_("-R DIR, -rpath DIR"), ONE_DASH,
254 &General_options::add_to_rpath),
7da52175
ILT
255 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
256 NULL, TWO_DASHES, &General_options::create_eh_frame_hdr),
15b3cfae
ILT
257 GENERAL_ARG('\0', "rpath-link",
258 N_("Add DIR to link time shared library search path"),
259 N_("--rpath-link DIR"), TWO_DASHES,
260 &General_options::add_to_rpath_link),
92e059d8
ILT
261 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
262 NULL, ONE_DASH, &General_options::set_shared),
bae7f79e 263 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
61ba1cf9 264 NULL, ONE_DASH, &General_options::set_static),
dbe717ef 265 POSDEP_NOARG('\0', "as-needed",
a6badf5a 266 N_("Only set DT_NEEDED for dynamic libs if used"),
dbe717ef
ILT
267 NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
268 POSDEP_NOARG('\0', "no-as-needed",
a6badf5a 269 N_("Always DT_NEEDED for dynamic libs (default)"),
dbe717ef 270 NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed),
4973341a
ILT
271 POSDEP_NOARG('\0', "whole-archive",
272 N_("Include all archive contents"),
273 NULL, TWO_DASHES,
274 &Position_dependent_options::set_whole_archive),
275 POSDEP_NOARG('\0', "no-whole-archive",
276 N_("Include only needed archive contents"),
277 NULL, TWO_DASHES,
278 &Position_dependent_options::clear_whole_archive),
bae7f79e
ILT
279 SPECIAL('\0', "help", N_("Report usage information"), NULL,
280 TWO_DASHES, &help)
281};
282
61ba1cf9 283const int options::Command_line_options::options_size =
bae7f79e
ILT
284 sizeof (options) / sizeof (options[0]);
285
286// The default values for the general options.
287
61ba1cf9 288General_options::General_options()
a6badf5a
ILT
289 : export_dynamic_(false),
290 dynamic_linker_(NULL),
dbe717ef 291 search_path_(),
61ba1cf9
ILT
292 output_file_name_("a.out"),
293 is_relocatable_(false),
7da52175 294 create_eh_frame_hdr_(false),
4973341a 295 rpath_(),
15b3cfae 296 rpath_link_(),
92e059d8 297 is_shared_(false),
61ba1cf9 298 is_static_(false)
bae7f79e
ILT
299{
300}
301
302// The default values for the position dependent options.
303
61ba1cf9 304Position_dependent_options::Position_dependent_options()
4973341a
ILT
305 : do_static_search_(false),
306 as_needed_(false),
307 include_whole_archive_(false)
bae7f79e
ILT
308{
309}
310
dbe717ef
ILT
311// Input_arguments methods.
312
313// Add a file to the list.
314
315void
316Input_arguments::add_file(const Input_file_argument& file)
317{
318 if (!this->in_group_)
319 this->input_argument_list_.push_back(Input_argument(file));
320 else
321 {
a3ad94ed
ILT
322 gold_assert(!this->input_argument_list_.empty());
323 gold_assert(this->input_argument_list_.back().is_group());
dbe717ef
ILT
324 this->input_argument_list_.back().group()->add_file(file);
325 }
326}
327
328// Start a group.
329
330void
331Input_arguments::start_group()
332{
a3ad94ed 333 gold_assert(!this->in_group_);
dbe717ef
ILT
334 Input_file_group* group = new Input_file_group();
335 this->input_argument_list_.push_back(Input_argument(group));
336 this->in_group_ = true;
337}
338
339// End a group.
340
341void
342Input_arguments::end_group()
343{
a3ad94ed 344 gold_assert(this->in_group_);
dbe717ef
ILT
345 this->in_group_ = false;
346}
347
ead1e424 348// Command_line options.
bae7f79e 349
61ba1cf9 350Command_line::Command_line()
dbe717ef 351 : options_(), position_options_(), inputs_()
bae7f79e
ILT
352{
353}
354
355// Process the command line options.
356
357void
61ba1cf9 358Command_line::process(int argc, char** argv)
bae7f79e 359{
61ba1cf9
ILT
360 const int options_size = options::Command_line_options::options_size;
361 const options::One_option* options =
362 options::Command_line_options::options;
bae7f79e
ILT
363 bool no_more_options = false;
364 int i = 0;
365 while (i < argc)
366 {
367 if (argv[i][0] != '-' || no_more_options)
368 {
ead1e424 369 this->add_file(argv[i], false);
bae7f79e
ILT
370 ++i;
371 continue;
372 }
373
374 // Option starting with '-'.
375 int dashes = 1;
376 if (argv[i][1] == '-')
377 {
378 dashes = 2;
379 if (argv[i][2] == '\0')
380 {
381 no_more_options = true;
382 continue;
383 }
384 }
385
386 // Look for a long option match.
387 char* opt = argv[i] + dashes;
388 char first = opt[0];
389 int skiparg = 0;
390 char* arg = strchr(opt, '=');
391 if (arg != NULL)
392 *arg = '\0';
393 else if (i + 1 < argc)
394 {
395 arg = argv[i + 1];
396 skiparg = 1;
397 }
398
399 int j;
400 for (j = 0; j < options_size; ++j)
401 {
402 if (options[j].long_option != NULL
403 && (dashes == 2
404 || (options[j].dash
61ba1cf9 405 != options::One_option::EXACTLY_TWO_DASHES))
bae7f79e
ILT
406 && first == options[j].long_option[0]
407 && strcmp(opt, options[j].long_option) == 0)
408 {
409 if (options[j].special)
410 i += options[j].special(argc - 1, argv + i, opt, this);
411 else
412 {
413 if (!options[j].takes_argument())
414 {
415 arg = NULL;
416 skiparg = 0;
417 }
418 else
419 {
420 if (arg == NULL)
421 this->usage(_("missing argument"), argv[i]);
422 }
423 this->apply_option(options[j], arg);
424 i += skiparg + 1;
425 }
426 break;
427 }
428 }
429 if (j < options_size)
430 continue;
431
432 // If we saw two dashes, we need to see a long option.
433 if (dashes == 2)
434 this->usage(_("unknown option"), argv[i]);
435
436 // Look for a short option match. There may be more than one
437 // short option in a given argument.
438 bool done = false;
439 char* s = argv[i] + 1;
440 ++i;
441 while (*s != '\0' && !done)
442 {
443 char opt = *s;
444 int j;
445 for (j = 0; j < options_size; ++j)
446 {
447 if (options[j].short_option == opt)
448 {
449 if (options[j].special)
450 {
451 // Undo the argument skip done above.
452 --i;
453 i += options[j].special(argc - i, argv + i, s, this);
454 done = true;
455 }
456 else
457 {
458 arg = NULL;
459 if (options[j].takes_argument())
460 {
461 if (s[1] != '\0')
462 {
463 arg = s + 1;
464 done = true;
465 }
466 else if (i < argc)
467 {
468 arg = argv[i];
469 ++i;
470 }
471 else
472 this->usage(_("missing argument"), opt);
473 }
474 this->apply_option(options[j], arg);
475 }
476 break;
477 }
478 }
479
480 if (j >= options_size)
481 this->usage(_("unknown option"), *s);
482
483 ++s;
484 }
485 }
61ba1cf9 486
dbe717ef 487 if (this->inputs_.in_group())
ead1e424
ILT
488 {
489 fprintf(stderr, _("%s: missing group end"), program_name);
490 this->usage();
491 }
492
61ba1cf9
ILT
493 // FIXME: We should only do this when configured in native mode.
494 this->options_.add_to_search_path("/lib");
495 this->options_.add_to_search_path("/usr/lib");
bae7f79e
ILT
496}
497
498// Apply a command line option.
499
500void
61ba1cf9
ILT
501Command_line::apply_option(const options::One_option& opt,
502 const char* arg)
bae7f79e
ILT
503{
504 if (arg == NULL)
505 {
506 if (opt.general_noarg)
507 (this->options_.*(opt.general_noarg))();
508 else if (opt.dependent_noarg)
509 (this->position_options_.*(opt.dependent_noarg))();
510 else
61ba1cf9 511 gold_unreachable();
bae7f79e
ILT
512 }
513 else
514 {
515 if (opt.general_arg)
516 (this->options_.*(opt.general_arg))(arg);
517 else if (opt.dependent_arg)
518 (this->position_options_.*(opt.dependent_arg))(arg);
519 else
61ba1cf9 520 gold_unreachable();
bae7f79e
ILT
521 }
522}
523
ead1e424
ILT
524// Add an input file or library.
525
526void
527Command_line::add_file(const char* name, bool is_lib)
528{
529 Input_file_argument file(name, is_lib, this->position_options_);
dbe717ef 530 this->inputs_.add_file(file);
ead1e424
ILT
531}
532
61ba1cf9
ILT
533// Handle the -l option, which requires special treatment.
534
535int
536Command_line::process_l_option(int argc, char** argv, char* arg)
537{
538 int ret;
539 const char* libname;
540 if (arg[1] != '\0')
541 {
542 ret = 1;
543 libname = arg + 1;
544 }
545 else if (argc > 1)
546 {
547 ret = 2;
548 libname = argv[argc + 1];
549 }
550 else
551 this->usage(_("missing argument"), arg);
552
ead1e424 553 this->add_file(libname, true);
61ba1cf9
ILT
554
555 return ret;
556}
557
ead1e424
ILT
558// Handle the --start-group option.
559
560void
561Command_line::start_group(const char* arg)
562{
dbe717ef 563 if (this->inputs_.in_group())
ead1e424 564 this->usage(_("may not nest groups"), arg);
dbe717ef 565 this->inputs_.start_group();
ead1e424
ILT
566}
567
568// Handle the --end-group option.
569
570void
571Command_line::end_group(const char* arg)
572{
dbe717ef 573 if (!this->inputs_.in_group())
ead1e424 574 this->usage(_("group end without group start"), arg);
dbe717ef 575 this->inputs_.end_group();
ead1e424
ILT
576}
577
bae7f79e
ILT
578// Report a usage error. */
579
580void
61ba1cf9 581Command_line::usage()
bae7f79e
ILT
582{
583 fprintf(stderr,
584 _("%s: use the --help option for usage information\n"),
61ba1cf9
ILT
585 program_name);
586 gold_exit(false);
bae7f79e
ILT
587}
588
589void
61ba1cf9 590Command_line::usage(const char* msg, const char *opt)
bae7f79e
ILT
591{
592 fprintf(stderr,
593 _("%s: %s: %s\n"),
61ba1cf9 594 program_name, opt, msg);
bae7f79e
ILT
595 this->usage();
596}
597
598void
61ba1cf9 599Command_line::usage(const char* msg, char opt)
bae7f79e
ILT
600{
601 fprintf(stderr,
602 _("%s: -%c: %s\n"),
61ba1cf9 603 program_name, opt, msg);
bae7f79e
ILT
604 this->usage();
605}
61ba1cf9
ILT
606
607} // End namespace gold.
This page took 0.082588 seconds and 4 git commands to generate.