2007-10-04 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / options.cc
blobf49b9f57f110cfa07fa640c14ee1d1149c540920
1 // options.c -- handle command line options for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <iostream>
26 #include <sys/stat.h>
27 #include "filenames.h"
28 #include "libiberty.h"
30 #include "options.h"
32 namespace gold
35 // The information we keep for a single command line option.
37 struct options::One_option
39 // The single character option name, or '\0' if this is only a long
40 // option.
41 char short_option;
43 // The long option name, or NULL if this is only a short option.
44 const char* long_option;
46 // Description of the option for --help output, or NULL if there is none.
47 const char* doc;
49 // How to print the option name in --help output, or NULL to use the
50 // default.
51 const char* help_output;
53 // Long option dash control. This is ignored if long_option is
54 // NULL.
55 enum
57 // Long option normally takes one dash; two dashes are also
58 // accepted.
59 ONE_DASH,
60 // Long option normally takes two dashes; one dash is also
61 // accepted.
62 TWO_DASHES,
63 // Long option always takes two dashes.
64 EXACTLY_TWO_DASHES
65 } dash;
67 // Function for special handling, or NULL. Returns the number of
68 // arguments to skip. This will normally be at least 1, but it may
69 // be 0 if this function changes *argv. ARG points to the location
70 // in *ARGV where the option starts, which may be helpful for a
71 // short option.
72 int (*special)(int argc, char** argv, char *arg, Command_line*);
74 // If this is a position independent option which does not take an
75 // argument, this is the member function to call to record it.
76 void (General_options::*general_noarg)();
78 // If this is a position independent function which takes an
79 // argument, this is the member function to call to record it.
80 void (General_options::*general_arg)(const char*);
82 // If this is a position dependent option which does not take an
83 // argument, this is the member function to call to record it.
84 void (Position_dependent_options::*dependent_noarg)();
86 // If this is a position dependent option which takes an argument,
87 // this is the member function to record it.
88 void (Position_dependent_options::*dependent_arg)(const char*);
90 // Return whether this option takes an argument.
91 bool
92 takes_argument() const
93 { return this->general_arg != NULL || this->dependent_arg != NULL; }
96 class options::Command_line_options
98 public:
99 static const One_option options[];
100 static const int options_size;
103 } // End namespace gold.
105 namespace
108 // Handle the special -l option, which adds an input file.
111 library(int argc, char** argv, char* arg, gold::Command_line* cmdline)
113 return cmdline->process_l_option(argc, argv, arg);
116 // Handle the special --start-group option.
119 start_group(int, char**, char* arg, gold::Command_line* cmdline)
121 cmdline->start_group(arg);
122 return 1;
125 // Handle the special --end-group option.
128 end_group(int, char**, char* arg, gold::Command_line* cmdline)
130 cmdline->end_group(arg);
131 return 1;
134 // Report usage information for ld --help, and exit.
137 help(int, char**, char*, gold::Command_line*)
139 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
141 const int options_size = gold::options::Command_line_options::options_size;
142 const gold::options::One_option* options =
143 gold::options::Command_line_options::options;
144 for (int i = 0; i < options_size; ++i)
146 if (options[i].doc == NULL)
147 continue;
149 printf(" ");
150 int len = 2;
151 bool comma = false;
153 int j = i;
156 if (options[j].help_output != NULL)
158 if (comma)
160 printf(", ");
161 len += 2;
163 printf(options[j].help_output);
164 len += std::strlen(options[i].help_output);
165 comma = true;
167 else
169 if (options[j].short_option != '\0')
171 if (comma)
173 printf(", ");
174 len += 2;
176 printf("-%c", options[j].short_option);
177 len += 2;
178 comma = true;
181 if (options[j].long_option != NULL)
183 if (comma)
185 printf(", ");
186 len += 2;
188 if (options[j].dash == gold::options::One_option::ONE_DASH)
190 printf("-");
191 ++len;
193 else
195 printf("--");
196 len += 2;
198 printf("%s", options[j].long_option);
199 len += std::strlen(options[j].long_option);
200 comma = true;
203 ++j;
205 while (j < options_size && options[j].doc == NULL);
207 if (len >= 30)
209 printf("\n");
210 len = 0;
212 for (; len < 30; ++len)
213 std::putchar(' ');
215 std::puts(options[i].doc);
218 gold::gold_exit(true);
220 return 0;
223 // If the default sysroot is relocatable, try relocating it based on
224 // the prefix FROM.
226 char*
227 get_relative_sysroot(const char* from)
229 char* path = make_relative_prefix(gold::program_name, from,
230 TARGET_SYSTEM_ROOT);
231 if (path != NULL)
233 struct stat s;
234 if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
235 return path;
236 free(path);
239 return NULL;
242 // Return the default sysroot. This is set by the --with-sysroot
243 // option to configure.
245 std::string
246 get_default_sysroot()
248 const char* sysroot = TARGET_SYSTEM_ROOT;
249 if (*sysroot == '\0')
250 return "";
252 if (TARGET_SYSTEM_ROOT_RELOCATABLE)
254 char* path = get_relative_sysroot (BINDIR);
255 if (path == NULL)
256 path = get_relative_sysroot (TOOLBINDIR);
257 if (path != NULL)
259 std::string ret = path;
260 free(path);
261 return ret;
265 return sysroot;
268 } // End anonymous namespace.
270 namespace gold
273 // Helper macros used to specify the options. We could also do this
274 // using constructors, but then g++ would generate code to initialize
275 // the array. We want the array to be initialized statically so that
276 // we get better startup time.
278 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
279 { short_option, long_option, doc, help, options::One_option::dash, \
280 NULL, func, NULL, NULL, NULL }
281 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
282 { short_option, long_option, doc, help, options::One_option::dash, \
283 NULL, NULL, func, NULL, NULL }
284 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
285 { short_option, long_option, doc, help, options::One_option::dash, \
286 NULL, NULL, NULL, func, NULL }
287 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
288 { short_option, long_option, doc, help, options::One_option::dash, \
289 NULL, NULL, NULL, NULL, func }
290 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
291 { short_option, long_option, doc, help, options::One_option::dash, \
292 func, NULL, NULL, NULL, NULL }
294 // Here is the actual list of options which we accept.
296 const options::One_option
297 options::Command_line_options::options[] =
299 SPECIAL('l', "library", N_("Search for library LIBNAME"),
300 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
301 &library),
302 SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
303 TWO_DASHES, &start_group),
304 SPECIAL(')', "end-group", N_("End a library search group"), NULL,
305 TWO_DASHES, &end_group),
306 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
307 NULL, TWO_DASHES, &General_options::set_export_dynamic),
308 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
309 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES,
310 &General_options::set_dynamic_linker),
311 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
312 N_("-L DIR, --library-path DIR"), TWO_DASHES,
313 &General_options::add_to_search_path),
314 GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
315 &General_options::ignore),
316 GENERAL_ARG('O', NULL, N_("Optimize output file size"),
317 N_("-O level"), ONE_DASH,
318 &General_options::set_optimization_level),
319 GENERAL_ARG('o', "output", N_("Set output file name"),
320 N_("-o FILE, --output FILE"), TWO_DASHES,
321 &General_options::set_output_file_name),
322 GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
323 ONE_DASH, &General_options::set_relocatable),
324 GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
325 N_("-R DIR, -rpath DIR"), ONE_DASH,
326 &General_options::add_to_rpath),
327 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
328 NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr),
329 GENERAL_ARG('\0', "rpath-link",
330 N_("Add DIR to link time shared library search path"),
331 N_("--rpath-link DIR"), TWO_DASHES,
332 &General_options::add_to_rpath_link),
333 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
334 NULL, ONE_DASH, &General_options::set_shared),
335 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
336 NULL, ONE_DASH, &General_options::set_static),
337 GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
338 N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot),
339 POSDEP_NOARG('\0', "as-needed",
340 N_("Only set DT_NEEDED for dynamic libs if used"),
341 NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
342 POSDEP_NOARG('\0', "no-as-needed",
343 N_("Always DT_NEEDED for dynamic libs (default)"),
344 NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed),
345 POSDEP_NOARG('\0', "whole-archive",
346 N_("Include all archive contents"),
347 NULL, TWO_DASHES,
348 &Position_dependent_options::set_whole_archive),
349 POSDEP_NOARG('\0', "no-whole-archive",
350 N_("Include only needed archive contents"),
351 NULL, TWO_DASHES,
352 &Position_dependent_options::clear_whole_archive),
353 SPECIAL('\0', "help", N_("Report usage information"), NULL,
354 TWO_DASHES, &help)
357 const int options::Command_line_options::options_size =
358 sizeof (options) / sizeof (options[0]);
360 // The default values for the general options.
362 General_options::General_options()
363 : export_dynamic_(false),
364 dynamic_linker_(NULL),
365 search_path_(),
366 optimization_level_(0),
367 output_file_name_("a.out"),
368 is_relocatable_(false),
369 create_eh_frame_hdr_(false),
370 rpath_(),
371 rpath_link_(),
372 is_shared_(false),
373 is_static_(false),
374 sysroot_()
378 // The default values for the position dependent options.
380 Position_dependent_options::Position_dependent_options()
381 : do_static_search_(false),
382 as_needed_(false),
383 include_whole_archive_(false)
387 // Add the sysroot, if any, to the search paths.
389 void
390 General_options::add_sysroot()
392 if (this->sysroot_.empty())
394 this->sysroot_ = get_default_sysroot();
395 if (this->sysroot_.empty())
396 return;
399 const char* sysroot = this->sysroot_.c_str();
400 char* canonical_sysroot = lrealpath(sysroot);
402 for (Dir_list::iterator p = this->search_path_.begin();
403 p != this->search_path_.end();
404 ++p)
405 p->add_sysroot(sysroot, canonical_sysroot);
407 free(canonical_sysroot);
410 // Search_directory methods.
412 // This is called if we have a sysroot. Apply the sysroot if
413 // appropriate. Record whether the directory is in the sysroot.
415 void
416 Search_directory::add_sysroot(const char* sysroot,
417 const char* canonical_sysroot)
419 gold_assert(*sysroot != '\0');
420 if (this->put_in_sysroot_)
422 if (!IS_DIR_SEPARATOR(this->name_[0])
423 && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
424 this->name_ = '/' + this->name_;
425 this->name_ = sysroot + this->name_;
426 this->is_in_sysroot_ = true;
428 else
430 // Check whether this entry is in the sysroot. To do this
431 // correctly, we need to use canonical names. Otherwise we will
432 // get confused by the ../../.. paths that gcc tends to use.
433 char* canonical_name = lrealpath(this->name_.c_str());
434 int canonical_name_len = strlen(canonical_name);
435 int canonical_sysroot_len = strlen(canonical_sysroot);
436 if (canonical_name_len > canonical_sysroot_len
437 && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
439 canonical_name[canonical_sysroot_len] = '\0';
440 if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
441 this->is_in_sysroot_ = true;
443 free(canonical_name);
447 // Input_arguments methods.
449 // Add a file to the list.
451 void
452 Input_arguments::add_file(const Input_file_argument& file)
454 if (!this->in_group_)
455 this->input_argument_list_.push_back(Input_argument(file));
456 else
458 gold_assert(!this->input_argument_list_.empty());
459 gold_assert(this->input_argument_list_.back().is_group());
460 this->input_argument_list_.back().group()->add_file(file);
464 // Start a group.
466 void
467 Input_arguments::start_group()
469 gold_assert(!this->in_group_);
470 Input_file_group* group = new Input_file_group();
471 this->input_argument_list_.push_back(Input_argument(group));
472 this->in_group_ = true;
475 // End a group.
477 void
478 Input_arguments::end_group()
480 gold_assert(this->in_group_);
481 this->in_group_ = false;
484 // Command_line options.
486 Command_line::Command_line()
487 : options_(), position_options_(), inputs_()
491 // Process the command line options.
493 void
494 Command_line::process(int argc, char** argv)
496 const int options_size = options::Command_line_options::options_size;
497 const options::One_option* options =
498 options::Command_line_options::options;
499 bool no_more_options = false;
500 int i = 0;
501 while (i < argc)
503 if (argv[i][0] != '-' || no_more_options)
505 this->add_file(argv[i], false);
506 ++i;
507 continue;
510 // Option starting with '-'.
511 int dashes = 1;
512 if (argv[i][1] == '-')
514 dashes = 2;
515 if (argv[i][2] == '\0')
517 no_more_options = true;
518 continue;
522 // Look for a long option match.
523 char* opt = argv[i] + dashes;
524 char first = opt[0];
525 int skiparg = 0;
526 char* arg = strchr(opt, '=');
527 bool argument_with_equals = arg != NULL;
528 if (arg != NULL)
530 *arg = '\0';
531 ++arg;
533 else if (i + 1 < argc)
535 arg = argv[i + 1];
536 skiparg = 1;
539 int j;
540 for (j = 0; j < options_size; ++j)
542 if (options[j].long_option != NULL
543 && (dashes == 2
544 || (options[j].dash
545 != options::One_option::EXACTLY_TWO_DASHES))
546 && first == options[j].long_option[0]
547 && strcmp(opt, options[j].long_option) == 0)
549 if (options[j].special)
550 i += options[j].special(argc - 1, argv + i, opt, this);
551 else
553 if (!options[j].takes_argument())
555 if (argument_with_equals)
556 this->usage(_("unexpected argument"), argv[i]);
557 arg = NULL;
558 skiparg = 0;
560 else
562 if (arg == NULL)
563 this->usage(_("missing argument"), argv[i]);
565 this->apply_option(options[j], arg);
566 i += skiparg + 1;
568 break;
571 if (j < options_size)
572 continue;
574 // If we saw two dashes, we need to see a long option.
575 if (dashes == 2)
576 this->usage(_("unknown option"), argv[i]);
578 // Look for a short option match. There may be more than one
579 // short option in a given argument.
580 bool done = false;
581 char* s = argv[i] + 1;
582 ++i;
583 while (*s != '\0' && !done)
585 char opt = *s;
586 int j;
587 for (j = 0; j < options_size; ++j)
589 if (options[j].short_option == opt)
591 if (options[j].special)
593 // Undo the argument skip done above.
594 --i;
595 i += options[j].special(argc - i, argv + i, s, this);
596 done = true;
598 else
600 arg = NULL;
601 if (options[j].takes_argument())
603 if (s[1] != '\0')
605 arg = s + 1;
606 done = true;
608 else if (i < argc)
610 arg = argv[i];
611 ++i;
613 else
614 this->usage(_("missing argument"), opt);
616 this->apply_option(options[j], arg);
618 break;
622 if (j >= options_size)
623 this->usage(_("unknown option"), *s);
625 ++s;
629 if (this->inputs_.in_group())
631 fprintf(stderr, _("%s: missing group end"), program_name);
632 this->usage();
635 // FIXME: We should only do this when configured in native mode.
636 this->options_.add_to_search_path_with_sysroot("/lib");
637 this->options_.add_to_search_path_with_sysroot("/usr/lib");
639 this->options_.add_sysroot();
642 // Apply a command line option.
644 void
645 Command_line::apply_option(const options::One_option& opt,
646 const char* arg)
648 if (arg == NULL)
650 if (opt.general_noarg)
651 (this->options_.*(opt.general_noarg))();
652 else if (opt.dependent_noarg)
653 (this->position_options_.*(opt.dependent_noarg))();
654 else
655 gold_unreachable();
657 else
659 if (opt.general_arg)
660 (this->options_.*(opt.general_arg))(arg);
661 else if (opt.dependent_arg)
662 (this->position_options_.*(opt.dependent_arg))(arg);
663 else
664 gold_unreachable();
668 // Add an input file or library.
670 void
671 Command_line::add_file(const char* name, bool is_lib)
673 Input_file_argument file(name, is_lib, "", this->position_options_);
674 this->inputs_.add_file(file);
677 // Handle the -l option, which requires special treatment.
680 Command_line::process_l_option(int argc, char** argv, char* arg)
682 int ret;
683 const char* libname;
684 if (arg[1] != '\0')
686 ret = 1;
687 libname = arg + 1;
689 else if (argc > 1)
691 ret = 2;
692 libname = argv[argc + 1];
694 else
695 this->usage(_("missing argument"), arg);
697 this->add_file(libname, true);
699 return ret;
702 // Handle the --start-group option.
704 void
705 Command_line::start_group(const char* arg)
707 if (this->inputs_.in_group())
708 this->usage(_("may not nest groups"), arg);
709 this->inputs_.start_group();
712 // Handle the --end-group option.
714 void
715 Command_line::end_group(const char* arg)
717 if (!this->inputs_.in_group())
718 this->usage(_("group end without group start"), arg);
719 this->inputs_.end_group();
722 // Report a usage error. */
724 void
725 Command_line::usage()
727 fprintf(stderr,
728 _("%s: use the --help option for usage information\n"),
729 program_name);
730 gold_exit(false);
733 void
734 Command_line::usage(const char* msg, const char *opt)
736 fprintf(stderr,
737 _("%s: %s: %s\n"),
738 program_name, opt, msg);
739 this->usage();
742 void
743 Command_line::usage(const char* msg, char opt)
745 fprintf(stderr,
746 _("%s: -%c: %s\n"),
747 program_name, opt, msg);
748 this->usage();
751 } // End namespace gold.