5d565324b8f8f10f3e4c62d5fc629cdc157aa7c7
[external/binutils.git] / gold / options.cc
1 // options.c -- handle command line options for gold
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
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
23 #include "gold.h"
24
25 #include <cstdlib>
26 #include <vector>
27 #include <iostream>
28 #include <sys/stat.h>
29 #include "filenames.h"
30 #include "libiberty.h"
31 #include "demangle.h"
32
33 #include "debug.h"
34 #include "script.h"
35 #include "target-select.h"
36 #include "options.h"
37
38 namespace gold
39 {
40
41 General_options
42 Position_dependent_options::default_options_;
43
44 namespace options
45 {
46
47 // This global variable is set up as General_options is constructed.
48 static std::vector<const One_option*> registered_options;
49
50 // These are set up at the same time -- the variables that accept one
51 // dash, two, or require -z.  A single variable may be in more than
52 // one of thes data structures.
53 typedef Unordered_map<std::string, One_option*> Option_map;
54 static Option_map* long_options = NULL;
55 static One_option* short_options[128];
56
57 void
58 One_option::register_option()
59 {
60   registered_options.push_back(this);
61
62   // We can't make long_options a static Option_map because we can't
63   // guarantee that will be initialized before register_option() is
64   // first called.
65   if (long_options == NULL)
66     long_options = new Option_map;
67
68   // TWO_DASHES means that two dashes are preferred, but one is ok too.
69   if (!this->longname.empty())
70     (*long_options)[this->longname] = this;
71
72   const int shortname_as_int = static_cast<int>(this->shortname);
73   gold_assert(shortname_as_int >= 0 && shortname_as_int < 128);
74   if (this->shortname != '\0')
75     short_options[shortname_as_int] = this;
76 }
77
78 void
79 One_option::print() const
80 {
81   bool comma = false;
82   printf("  ");
83   int len = 2;
84   if (this->shortname != '\0')
85     {
86       len += printf("-%c", this->shortname);
87       if (this->helparg)
88         {
89           // -z takes long-names only.
90           gold_assert(this->dashes != DASH_Z);
91           len += printf(" %s", gettext(this->helparg));
92         }
93       comma = true;
94     }
95   if (!this->longname.empty()
96       && !(this->longname[0] == this->shortname
97            && this->longname[1] == '\0'))
98     {
99       if (comma)
100         len += printf(", ");
101       switch (this->dashes)
102         {
103         case options::ONE_DASH: case options::EXACTLY_ONE_DASH:
104           len += printf("-");
105           break;
106         case options::TWO_DASHES: case options::EXACTLY_TWO_DASHES:
107           len += printf("--");
108           break;
109         case options::DASH_Z:
110           len += printf("-z ");
111           break;
112         default:
113           gold_unreachable();
114         }
115       len += printf("%s", this->longname.c_str());
116       if (this->helparg)
117         {
118           // For most options, we print "--frob FOO".  But for -z
119           // we print "-z frob=FOO".
120           len += printf("%c%s", this->dashes == options::DASH_Z ? '=' : ' ',
121                         gettext(this->helparg));
122         }
123     }
124
125   if (len >= 30)
126     {
127       printf("\n");
128       len = 0;
129     }
130   for (; len < 30; ++len)
131     std::putchar(' ');
132
133   // TODO: if we're boolean, add " (default)" when appropriate.
134   printf("%s\n", gettext(this->helpstring));
135 }
136
137 void
138 help()
139 {
140   printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
141
142   std::vector<const One_option*>::const_iterator it;
143   for (it = registered_options.begin(); it != registered_options.end(); ++it)
144     (*it)->print();
145
146   // config.guess and libtool.m4 look in ld --help output for the
147   // string "supported targets".
148   printf(_("%s: supported targets:"), gold::program_name);
149   std::vector<const char*> supported_names;
150   gold::supported_target_names(&supported_names);
151   for (std::vector<const char*>::const_iterator p = supported_names.begin();
152        p != supported_names.end();
153        ++p)
154     printf(" %s", *p);
155   printf("\n");
156 }
157
158 // For bool, arg will be NULL (boolean options take no argument);
159 // we always just set to true.
160 void
161 parse_bool(const char*, const char*, bool* retval)
162 {
163   *retval = true;
164 }
165
166 void
167 parse_uint(const char* option_name, const char* arg, int* retval)
168 {
169   char* endptr;
170   *retval = strtol(arg, &endptr, 0);
171   if (*endptr != '\0' || retval < 0)
172     gold_fatal(_("%s: invalid option value (expected an integer): %s"),
173                option_name, arg);
174 }
175
176 void
177 parse_uint64(const char* option_name, const char* arg, uint64_t *retval)
178 {
179   char* endptr;
180   *retval = strtoull(arg, &endptr, 0);
181   if (*endptr != '\0')
182     gold_fatal(_("%s: invalid option value (expected an integer): %s"),
183                option_name, arg);
184 }
185
186 void
187 parse_double(const char* option_name, const char* arg, double* retval)
188 {
189   char* endptr;
190   *retval = strtod(arg, &endptr);
191   if (*endptr != '\0')
192     gold_fatal(_("%s: invalid option value "
193                  "(expected a floating point number): %s"),
194                option_name, arg);
195 }
196
197 void
198 parse_string(const char* option_name, const char* arg, const char** retval)
199 {
200   if (*arg == '\0')
201     gold_fatal(_("%s: must take a non-empty argument"), option_name);
202   *retval = arg;
203 }
204
205 void
206 parse_optional_string(const char*, const char* arg, const char** retval)
207 {
208   *retval = arg;
209 }
210
211 void
212 parse_dirlist(const char*, const char* arg, Dir_list* retval)
213 {
214   retval->push_back(Search_directory(arg, false));
215 }
216
217 void
218 parse_choices(const char* option_name, const char* arg, const char** retval,
219               const char* choices[], int num_choices)
220 {
221   for (int i = 0; i < num_choices; i++)
222     if (strcmp(choices[i], arg) == 0)
223       {
224         *retval = arg;
225         return;
226       }
227
228   // If we get here, the user did not enter a valid choice, so we die.
229   std::string choices_list;
230   for (int i = 0; i < num_choices; i++)
231     {
232       choices_list += choices[i];
233       if (i != num_choices - 1)
234         choices_list += ", ";
235     }
236   gold_fatal(_("%s: must take one of the following arguments: %s"),
237              option_name, choices_list.c_str());
238 }
239
240 } // End namespace options.
241
242 // Define the handler for "special" options (set via DEFINE_special).
243
244 void
245 General_options::parse_help(const char*, const char*, Command_line*)
246 {
247   options::help();
248   ::exit(EXIT_SUCCESS);
249 }
250
251 void
252 General_options::parse_version(const char* opt, const char*, Command_line*)
253 {
254   gold::print_version(opt[0] == '-' && opt[1] == 'v');
255   ::exit(EXIT_SUCCESS);
256 }
257
258 void
259 General_options::parse_Bstatic(const char*, const char*, Command_line*)
260 {
261   this->set_Bdynamic(false);
262 }
263
264 void
265 General_options::parse_defsym(const char*, const char* arg,
266                               Command_line* cmdline)
267 {
268   cmdline->script_options().define_symbol(arg);
269 }
270
271 void
272 General_options::parse_library(const char*, const char* arg,
273                                Command_line* cmdline)
274 {
275   Input_file_argument file(arg, true, "", false, *this);
276   cmdline->inputs().add_file(file);
277 }
278
279 void
280 General_options::parse_R(const char* option, const char* arg,
281                          Command_line* cmdline)
282 {
283   struct stat s;
284   if (::stat(arg, &s) != 0 || S_ISDIR(s.st_mode))
285     this->add_to_rpath(arg);
286   else
287     this->parse_just_symbols(option, arg, cmdline);
288 }
289
290 void
291 General_options::parse_just_symbols(const char*, const char* arg,
292                                     Command_line* cmdline)
293 {
294   Input_file_argument file(arg, false, "", true, *this);
295   cmdline->inputs().add_file(file);
296 }
297
298 void
299 General_options::parse_static(const char*, const char*, Command_line*)
300 {
301   this->set_static(true);
302 }
303
304 void
305 General_options::parse_script(const char*, const char* arg,
306                               Command_line* cmdline)
307 {
308   if (!read_commandline_script(arg, cmdline))
309     gold::gold_fatal(_("unable to parse script file %s"), arg);
310 }
311
312 void
313 General_options::parse_version_script(const char*, const char* arg,
314                                       Command_line* cmdline)
315 {
316   if (!read_version_script(arg, cmdline))
317     gold::gold_fatal(_("unable to parse version script file %s"), arg);
318 }
319
320 void
321 General_options::parse_start_group(const char*, const char*,
322                                    Command_line* cmdline)
323 {
324   cmdline->inputs().start_group();
325 }
326
327 void
328 General_options::parse_end_group(const char*, const char*,
329                                  Command_line* cmdline)
330 {
331   cmdline->inputs().end_group();
332 }
333
334 } // End namespace gold.
335
336 namespace
337 {
338
339 void
340 usage()
341 {
342   fprintf(stderr,
343           _("%s: use the --help option for usage information\n"),
344           gold::program_name);
345   ::exit(EXIT_FAILURE);
346 }
347
348 void
349 usage(const char* msg, const char *opt)
350 {
351   fprintf(stderr,
352           _("%s: %s: %s\n"),
353           gold::program_name, opt, msg);
354   usage();
355 }
356
357 // Recognize input and output target names.  The GNU linker accepts
358 // these with --format and --oformat.  This code is intended to be
359 // minimally compatible.  In practice for an ELF target this would be
360 // the same target as the input files; that name always start with
361 // "elf".  Non-ELF targets would be "srec", "symbolsrec", "tekhex",
362 // "binary", "ihex".
363
364 gold::General_options::Object_format
365 string_to_object_format(const char* arg)
366 {
367   if (strncmp(arg, "elf", 3) == 0)
368     return gold::General_options::OBJECT_FORMAT_ELF;
369   else if (strcmp(arg, "binary") == 0)
370     return gold::General_options::OBJECT_FORMAT_BINARY;
371   else
372     {
373       gold::gold_error(_("format '%s' not supported; treating as elf "
374                          "(supported formats: elf, binary)"),
375                        arg);
376       return gold::General_options::OBJECT_FORMAT_ELF;
377     }
378 }
379
380 // If the default sysroot is relocatable, try relocating it based on
381 // the prefix FROM.
382
383 char*
384 get_relative_sysroot(const char* from)
385 {
386   char* path = make_relative_prefix(gold::program_name, from,
387                                     TARGET_SYSTEM_ROOT);
388   if (path != NULL)
389     {
390       struct stat s;
391       if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
392         return path;
393       free(path);
394     }
395
396   return NULL;
397 }
398
399 // Return the default sysroot.  This is set by the --with-sysroot
400 // option to configure.  Note we do not free the return value of
401 // get_relative_sysroot, which is a small memory leak, but is
402 // necessary since we store this pointer directly in General_options.
403
404 const char*
405 get_default_sysroot()
406 {
407   const char* sysroot = TARGET_SYSTEM_ROOT;
408   if (*sysroot == '\0')
409     return NULL;
410
411   if (TARGET_SYSTEM_ROOT_RELOCATABLE)
412     {
413       char* path = get_relative_sysroot(BINDIR);
414       if (path == NULL)
415         path = get_relative_sysroot(TOOLBINDIR);
416       if (path != NULL)
417         return path;
418     }
419
420   return sysroot;
421 }
422
423 // Parse a long option.  Such options have the form
424 // <-|--><option>[=arg].  If "=arg" is not present but the option
425 // takes an argument, the next word is taken to the be the argument.
426 // If equals_only is set, then only the <option>=<arg> form is
427 // accepted, not the <option><space><arg> form.  Returns a One_option
428 // struct or NULL if argv[i] cannot be parsed as a long option.  In
429 // the not-NULL case, *arg is set to the option's argument (NULL if
430 // the option takes no argument), and *i is advanced past this option.
431 // NOTE: it is safe for argv and arg to point to the same place.
432 gold::options::One_option*
433 parse_long_option(int argc, const char** argv, bool equals_only,
434                   const char** arg, int* i)
435 {
436   const char* const this_argv = argv[*i];
437
438   const char* equals = strchr(this_argv, '=');
439   const char* option_start = this_argv + strspn(this_argv, "-");
440   std::string option(option_start,
441                      equals ? equals - option_start : strlen(option_start));
442
443   gold::options::Option_map::iterator it
444       = gold::options::long_options->find(option);
445   if (it == gold::options::long_options->end())
446     return NULL;
447
448   gold::options::One_option* retval = it->second;
449
450   // If the dash-count doesn't match, we fail.
451   if (this_argv[0] != '-')  // no dashes at all: had better be "-z <longopt>"
452     {
453       if (retval->dashes != gold::options::DASH_Z)
454         return NULL;
455     }
456   else if (this_argv[1] != '-')   // one dash
457     {
458       if (retval->dashes != gold::options::ONE_DASH
459           && retval->dashes != gold::options::EXACTLY_ONE_DASH
460           && retval->dashes != gold::options::TWO_DASHES)
461         return NULL;
462     }
463   else                            // two dashes (or more!)
464     {
465       if (retval->dashes != gold::options::TWO_DASHES
466           && retval->dashes != gold::options::EXACTLY_TWO_DASHES
467           && retval->dashes != gold::options::ONE_DASH)
468         return NULL;
469     }
470
471   // Now that we know the option is good (or else bad in a way that
472   // will cause us to die), increment i to point past this argv.
473   ++(*i);
474
475   // Figure out the option's argument, if any.
476   if (!retval->takes_argument())
477     {
478       if (equals)
479         usage(_("unexpected argument"), this_argv);
480       else
481         *arg = NULL;
482     }
483   else
484     {
485       if (equals)
486         *arg = equals + 1;
487       else if (retval->takes_optional_argument())
488         *arg = retval->default_value;
489       else if (*i < argc && !equals_only)
490         *arg = argv[(*i)++];
491       else
492         usage(_("missing argument"), this_argv);
493     }
494
495   return retval;
496 }
497
498 // Parse a short option.  Such options have the form -<option>[arg].
499 // If "arg" is not present but the option takes an argument, the next
500 // word is taken to the be the argument.  If the option does not take
501 // an argument, it may be followed by another short option.  Returns a
502 // One_option struct or NULL if argv[i] cannot be parsed as a short
503 // option.  In the not-NULL case, *arg is set to the option's argument
504 // (NULL if the option takes no argument), and *i is advanced past
505 // this option.  This function keeps *i the same if we parsed a short
506 // option that does not take an argument, that looks to be followed by
507 // another short option in the same word.
508 gold::options::One_option*
509 parse_short_option(int argc, const char** argv, int pos_in_argv_i,
510                    const char** arg, int* i)
511 {
512   const char* const this_argv = argv[*i];
513
514   if (this_argv[0] != '-')
515     return NULL;
516
517   // We handle -z as a special case.
518   static gold::options::One_option dash_z("", gold::options::DASH_Z,
519                                           'z', "", "-z", "Z-OPTION", false,
520                                           NULL);
521   gold::options::One_option* retval = NULL;
522   if (this_argv[pos_in_argv_i] == 'z')
523     retval = &dash_z;
524   else
525     {
526       const int char_as_int = static_cast<int>(this_argv[pos_in_argv_i]);
527       if (char_as_int > 0 && char_as_int < 128)
528         retval = gold::options::short_options[char_as_int];
529     }
530
531   if (retval == NULL)
532     return NULL;
533
534   // Figure out the option's argument, if any.
535   if (!retval->takes_argument())
536     {
537       *arg = NULL;
538       // We only advance past this argument if it's the only one in argv.
539       if (this_argv[pos_in_argv_i + 1] == '\0')
540         ++(*i);
541     }
542   else
543     {
544       // If we take an argument, we'll eat up this entire argv entry.
545       ++(*i);
546       if (this_argv[pos_in_argv_i + 1] != '\0')
547         *arg = this_argv + pos_in_argv_i + 1;
548       else if (retval->takes_optional_argument())
549         *arg = retval->default_value;
550       else if (*i < argc)
551         *arg = argv[(*i)++];
552       else
553         usage(_("missing argument"), this_argv);
554     }
555
556   // If we're a -z option, we need to parse our argument as a
557   // long-option, e.g. "-z stacksize=8192".
558   if (retval == &dash_z)
559     {
560       int dummy_i = 0;
561       const char* dash_z_arg = *arg;
562       retval = parse_long_option(1, arg, true, arg, &dummy_i);
563       if (retval == NULL)
564         usage(_("unknown -z option"), dash_z_arg);
565     }
566
567   return retval;
568 }
569
570 } // End anonymous namespace.
571
572 namespace gold
573 {
574
575 General_options::General_options()
576   : execstack_status_(General_options::EXECSTACK_FROM_INPUT), static_(false),
577     do_demangle_(false)
578 {
579 }
580
581 General_options::Object_format
582 General_options::format_enum() const
583 {
584   return string_to_object_format(this->format());
585 }
586
587 General_options::Object_format
588 General_options::oformat_enum() const
589 {
590   return string_to_object_format(this->oformat());
591 }
592
593 // Add the sysroot, if any, to the search paths.
594
595 void
596 General_options::add_sysroot()
597 {
598   if (this->sysroot() == NULL || this->sysroot()[0] == '\0')
599     {
600       this->set_sysroot(get_default_sysroot());
601       if (this->sysroot() == NULL || this->sysroot()[0] == '\0')
602         return;
603     }
604
605   char* canonical_sysroot = lrealpath(this->sysroot());
606
607   for (Dir_list::iterator p = this->library_path_.value.begin();
608        p != this->library_path_.value.end();
609        ++p)
610     p->add_sysroot(this->sysroot(), canonical_sysroot);
611
612   free(canonical_sysroot);
613 }
614
615 // Set up variables and other state that isn't set up automatically by
616 // the parse routine, and ensure options don't contradict each other
617 // and are otherwise kosher.
618
619 void
620 General_options::finalize()
621 {
622   // Normalize the strip modifiers.  They have a total order:
623   // strip_all > strip_debug > strip_debug_gdb.  If one is true, set
624   // all beneath it to true as well.
625   if (this->strip_all())
626     this->set_strip_debug(true);
627   if (this->strip_debug())
628     this->set_strip_debug_gdb(true);
629
630   // If the user specifies both -s and -r, convert the -s to -S.
631   // -r requires us to keep externally visible symbols!
632   if (this->strip_all() && this->relocatable())
633     {
634       this->set_strip_all(false);
635       gold_assert(this->strip_debug());
636     }
637
638   // For us, -dc and -dp are synonyms for --define-common.
639   if (this->dc())
640     this->set_define_common(true);
641   if (this->dp())
642     this->set_define_common(true);
643
644   // We also set --define-common if we're not relocatable, as long as
645   // the user didn't explicitly ask for something different.
646   if (!this->user_set_define_common())
647     this->set_define_common(!this->relocatable());
648
649   // execstack_status_ is a three-state variable; update it based on
650   // -z [no]execstack.
651   if (this->execstack())
652     this->set_execstack_status(EXECSTACK_YES);
653   else if (this->noexecstack())
654     this->set_execstack_status(EXECSTACK_NO);
655
656   // Handle the optional argument for --demangle.
657   if (this->user_set_demangle())
658     {
659       this->set_do_demangle(true);
660       const char* style = this->demangle();
661       if (*style != '\0')
662         {
663           enum demangling_styles style_code;
664
665           style_code = cplus_demangle_name_to_style(style);
666           if (style_code == unknown_demangling)
667             gold_fatal("unknown demangling style '%s'", style);
668           cplus_demangle_set_style(style_code);
669         }
670     }
671   else if (this->user_set_no_demangle())
672     this->set_do_demangle(false);
673   else
674     {
675       // Testing COLLECT_NO_DEMANGLE makes our default demangling
676       // behaviour identical to that of gcc's linker wrapper.
677       this->set_do_demangle(getenv("COLLECT_NO_DEMANGLE") == NULL);
678     }
679
680   // If --thread_count is specified, it applies to
681   // --thread-count-{initial,middle,final}, though it doesn't override
682   // them.
683   if (this->thread_count() > 0 && this->thread_count_initial() == 0)
684     this->set_thread_count_initial(this->thread_count());
685   if (this->thread_count() > 0 && this->thread_count_middle() == 0)
686     this->set_thread_count_middle(this->thread_count());
687   if (this->thread_count() > 0 && this->thread_count_final() == 0)
688     this->set_thread_count_final(this->thread_count());
689
690   // Let's warn if you set the thread-count but we're going to ignore it.
691 #ifndef ENABLE_THREADS
692   if (this->threads())
693     {
694       gold_warning(_("ignoring --threads: "
695                      "%s was compiled without thread support"),
696                    program_name);
697       this->set_threads(false);
698     }
699   if (this->thread_count() > 0 || this->thread_count_initial() > 0
700       || this->thread_count_middle() > 0 || this->thread_count_final() > 0)
701     gold_warning(_("ignoring --thread-count: "
702                    "%s was compiled without thread support"),
703                  program_name);
704 #endif
705
706   // Even if they don't specify it, we add -L /lib and -L /usr/lib.
707   // FIXME: We should only do this when configured in native mode.
708   this->add_to_library_path_with_sysroot("/lib");
709   this->add_to_library_path_with_sysroot("/usr/lib");
710
711   // Normalize library_path() by adding the sysroot to all directories
712   // in the path, as appropriate.
713   this->add_sysroot();
714
715   // Now that we've normalized the options, check for contradictory ones.
716   if (this->shared() && this->relocatable())
717     gold_fatal(_("-shared and -r are incompatible"));
718
719   if (this->oformat_enum() != General_options::OBJECT_FORMAT_ELF
720       && (this->shared() || this->relocatable()))
721     gold_fatal(_("binary output format not compatible with -shared or -r"));
722
723   if (this->user_set_hash_bucket_empty_fraction()
724       && (this->hash_bucket_empty_fraction() < 0.0
725           || this->hash_bucket_empty_fraction() >= 1.0))
726     gold_fatal(_("--hash-bucket-empty-fraction value %g out of range "
727                  "[0.0, 1.0)"),
728                this->hash_bucket_empty_fraction());
729
730   // FIXME: we can/should be doing a lot more sanity checking here.
731 }
732
733 // Search_directory methods.
734
735 // This is called if we have a sysroot.  Apply the sysroot if
736 // appropriate.  Record whether the directory is in the sysroot.
737
738 void
739 Search_directory::add_sysroot(const char* sysroot,
740                               const char* canonical_sysroot)
741 {
742   gold_assert(*sysroot != '\0');
743   if (this->put_in_sysroot_)
744     {
745       if (!IS_DIR_SEPARATOR(this->name_[0])
746           && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
747         this->name_ = '/' + this->name_;
748       this->name_ = sysroot + this->name_;
749       this->is_in_sysroot_ = true;
750     }
751   else
752     {
753       // Check whether this entry is in the sysroot.  To do this
754       // correctly, we need to use canonical names.  Otherwise we will
755       // get confused by the ../../.. paths that gcc tends to use.
756       char* canonical_name = lrealpath(this->name_.c_str());
757       int canonical_name_len = strlen(canonical_name);
758       int canonical_sysroot_len = strlen(canonical_sysroot);
759       if (canonical_name_len > canonical_sysroot_len
760           && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
761         {
762           canonical_name[canonical_sysroot_len] = '\0';
763           if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
764             this->is_in_sysroot_ = true;
765         }
766       free(canonical_name);
767     }
768 }
769
770 // Input_arguments methods.
771
772 // Add a file to the list.
773
774 void
775 Input_arguments::add_file(const Input_file_argument& file)
776 {
777   if (!this->in_group_)
778     this->input_argument_list_.push_back(Input_argument(file));
779   else
780     {
781       gold_assert(!this->input_argument_list_.empty());
782       gold_assert(this->input_argument_list_.back().is_group());
783       this->input_argument_list_.back().group()->add_file(file);
784     }
785 }
786
787 // Start a group.
788
789 void
790 Input_arguments::start_group()
791 {
792   if (this->in_group_)
793     gold_fatal(_("May not nest groups"));
794   Input_file_group* group = new Input_file_group();
795   this->input_argument_list_.push_back(Input_argument(group));
796   this->in_group_ = true;
797 }
798
799 // End a group.
800
801 void
802 Input_arguments::end_group()
803 {
804   if (!this->in_group_)
805     gold_fatal(_("Group end without group start"));
806   this->in_group_ = false;
807 }
808
809 // Command_line options.
810
811 Command_line::Command_line()
812 {
813 }
814
815 // Process the command line options.  For process_one_option, i is the
816 // index of argv to process next, and must be an option (that is,
817 // start with a dash).  The return value is the index of the next
818 // option to process (i+1 or i+2, or argc to indicate processing is
819 // done).  no_more_options is set to true if (and when) "--" is seen
820 // as an option.
821
822 int
823 Command_line::process_one_option(int argc, const char** argv, int i,
824                                  bool* no_more_options)
825 {
826   gold_assert(argv[i][0] == '-' && !(*no_more_options));
827
828   // If we are reading "--", then just set no_more_options and return.
829   if (argv[i][1] == '-' && argv[i][2] == '\0')
830     {
831       *no_more_options = true;
832       return i + 1;
833     }
834
835   int new_i = i;
836   options::One_option* option = NULL;
837   const char* arg = NULL;
838
839   // First, try to process argv as a long option.
840   option = parse_long_option(argc, argv, false, &arg, &new_i);
841   if (option)
842     {
843       option->reader->parse_to_value(argv[i], arg, this, &this->options_);
844       return new_i;
845     }
846
847   // Now, try to process argv as a short option.  Since several short
848   // options can be combined in one argv, we may have to parse a lot
849   // until we're done reading this argv.
850   int pos_in_argv_i = 1;
851   while (new_i == i)
852     {
853       option = parse_short_option(argc, argv, pos_in_argv_i, &arg, &new_i);
854       if (!option)
855         break;
856       option->reader->parse_to_value(argv[i], arg, this, &this->options_);
857       ++pos_in_argv_i;
858     }
859   if (option)
860     return new_i;
861
862   // I guess it's neither a long option nor a short option.
863   usage(_("unknown option"), argv[i]);
864   return argc;
865 }
866
867
868 void
869 Command_line::process(int argc, const char** argv)
870 {
871   bool no_more_options = false;
872   int i = 0;
873   while (i < argc)
874     {
875       this->position_options_.copy_from_options(this->options());
876       if (no_more_options || argv[i][0] != '-')
877         {
878           Input_file_argument file(argv[i], false, "", false,
879                                    this->position_options_);
880           this->inputs_.add_file(file);
881           ++i;
882         }
883       else
884         i = process_one_option(argc, argv, i, &no_more_options);
885     }
886
887   if (this->inputs_.in_group())
888     {
889       fprintf(stderr, _("%s: missing group end\n"), program_name);
890       usage();
891     }
892
893   // Normalize the options and ensure they don't contradict each other.
894   this->options_.finalize();
895 }
896
897 } // End namespace gold.