From Craig Silverstein: Rework debug info code a bit, add option for
[external/binutils.git] / gold / options.cc
1 // options.c -- handle command line options for gold
2
3 // Copyright 2006, 2007 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 <iostream>
26 #include <sys/stat.h>
27 #include "filenames.h"
28 #include "libiberty.h"
29
30 #include "options.h"
31
32 namespace gold
33 {
34
35 // The information we keep for a single command line option.
36
37 struct options::One_option
38 {
39   // The single character option name, or '\0' if this is only a long
40   // option.
41   char short_option;
42
43   // The long option name, or NULL if this is only a short option.
44   const char* long_option;
45
46   // Description of the option for --help output, or NULL if there is none.
47   const char* doc;
48
49   // How to print the option name in --help output, or NULL to use the
50   // default.
51   const char* help_output;
52
53   // Long option dash control.  This is ignored if long_option is
54   // NULL.
55   enum
56     {
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;
66
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, bool long_option,
73                  Command_line*);
74
75   // If this is a position independent option which does not take an
76   // argument, this is the member function to call to record it.
77   void (General_options::*general_noarg)();
78
79   // If this is a position independent function which takes an
80   // argument, this is the member function to call to record it.
81   void (General_options::*general_arg)(const char*);
82
83   // If this is a position dependent option which does not take an
84   // argument, this is the member function to call to record it.
85   void (Position_dependent_options::*dependent_noarg)();
86
87   // If this is a position dependent option which takes an argument,
88   // this is the member function to record it.
89   void (Position_dependent_options::*dependent_arg)(const char*);
90
91   // Return whether this option takes an argument.
92   bool
93   takes_argument() const
94   { return this->general_arg != NULL || this->dependent_arg != NULL; }
95 };
96
97 // We have a separate table for -z options.
98
99 struct options::One_z_option
100 {
101   // The name of the option.
102   const char* name;
103
104   // The member function in General_options called to record it.
105   void (General_options::*set)();
106 };
107
108 class options::Command_line_options
109 {
110  public:
111   static const One_option options[];
112   static const int options_size;
113   static const One_z_option z_options[];
114   static const int z_options_size;
115 };
116
117 } // End namespace gold.
118
119 namespace
120 {
121
122 // Handle the special -l option, which adds an input file.
123
124 int
125 library(int argc, char** argv, char* arg, bool long_option,
126         gold::Command_line* cmdline)
127 {
128   return cmdline->process_l_option(argc, argv, arg, long_option);
129 }
130
131 // Handle the special -T/--script option, which reads a linker script.
132
133 int
134 invoke_script(int argc, char** argv, char* arg, bool long_option,
135               gold::Command_line* cmdline)
136 {
137   int ret;
138   const char* script_name = cmdline->get_special_argument("script", argc, argv,
139                                                           arg, long_option,
140                                                           &ret);
141   if (!read_commandline_script(script_name, cmdline))
142     gold::gold_error(_("%s: unable to parse script file %s\n"),
143                      gold::program_name, arg);
144   return ret;
145 }
146
147 // Handle the special --start-group option.
148
149 int
150 start_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
151 {
152   cmdline->start_group(arg);
153   return 1;
154 }
155
156 // Handle the special --end-group option.
157
158 int
159 end_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
160 {
161   cmdline->end_group(arg);
162   return 1;
163 }
164
165 // Report usage information for ld --help, and exit.
166
167 int
168 help(int, char**, char*, bool, gold::Command_line*)
169 {
170   printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
171
172   const int options_size = gold::options::Command_line_options::options_size;
173   const gold::options::One_option* options =
174     gold::options::Command_line_options::options;
175   for (int i = 0; i < options_size; ++i)
176     {
177       if (options[i].doc == NULL)
178         continue;
179
180       printf("  ");
181       int len = 2;
182       bool comma = false;
183
184       int j = i;
185       do
186         {
187           if (options[j].help_output != NULL)
188             {
189               if (comma)
190                 {
191                   printf(", ");
192                   len += 2;
193                 }
194               printf(options[j].help_output);
195               len += std::strlen(options[i].help_output);
196               comma = true;
197             }
198           else
199             {
200               if (options[j].short_option != '\0')
201                 {
202                   if (comma)
203                     {
204                       printf(", ");
205                       len += 2;
206                     }
207                   printf("-%c", options[j].short_option);
208                   len += 2;
209                   comma = true;
210                 }
211
212               if (options[j].long_option != NULL)
213                 {
214                   if (comma)
215                     {
216                       printf(", ");
217                       len += 2;
218                     }
219                   if (options[j].dash == gold::options::One_option::ONE_DASH)
220                     {
221                       printf("-");
222                       ++len;
223                     }
224                   else
225                     {
226                       printf("--");
227                       len += 2;
228                     }
229                   printf("%s", options[j].long_option);
230                   len += std::strlen(options[j].long_option);
231                   comma = true;
232                 }
233             }
234           ++j;
235         }
236       while (j < options_size && options[j].doc == NULL);
237
238       if (len >= 30)
239         {
240           printf("\n");
241           len = 0;
242         }
243       for (; len < 30; ++len)
244         std::putchar(' ');
245
246       std::puts(options[i].doc);
247     }
248
249   ::exit(0);
250
251   return 0;
252 }
253
254 // Report version information.
255
256 int
257 version(int, char**, char* opt, bool, gold::Command_line*)
258 {
259   gold::print_version(opt[0] == 'v' && opt[1] == '\0');
260   ::exit(0);
261   return 0;
262 }
263
264 // If the default sysroot is relocatable, try relocating it based on
265 // the prefix FROM.
266
267 char*
268 get_relative_sysroot(const char* from)
269 {
270   char* path = make_relative_prefix(gold::program_name, from,
271                                     TARGET_SYSTEM_ROOT);
272   if (path != NULL)
273     {
274       struct stat s;
275       if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
276         return path;
277       free(path);
278     }
279
280   return NULL;
281 }
282
283 // Return the default sysroot.  This is set by the --with-sysroot
284 // option to configure.
285
286 std::string
287 get_default_sysroot()
288 {
289   const char* sysroot = TARGET_SYSTEM_ROOT;
290   if (*sysroot == '\0')
291     return "";
292
293   if (TARGET_SYSTEM_ROOT_RELOCATABLE)
294     {
295       char* path = get_relative_sysroot (BINDIR);
296       if (path == NULL)
297         path = get_relative_sysroot (TOOLBINDIR);
298       if (path != NULL)
299         {
300           std::string ret = path;
301           free(path);
302           return ret;
303         }
304     }
305
306   return sysroot;
307 }
308
309 } // End anonymous namespace.
310
311 namespace gold
312 {
313
314 // Helper macros used to specify the options.  We could also do this
315 // using constructors, but then g++ would generate code to initialize
316 // the array.  We want the array to be initialized statically so that
317 // we get better startup time.
318
319 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
320   { short_option, long_option, doc, help, options::One_option::dash, \
321       NULL, func, NULL, NULL, NULL }
322 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func)   \
323   { short_option, long_option, doc, help, options::One_option::dash, \
324       NULL, NULL, func, NULL, NULL }
325 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func)  \
326   { short_option, long_option, doc, help, options::One_option::dash, \
327       NULL,  NULL, NULL, func, NULL }
328 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func)    \
329   { short_option, long_option, doc, help, options::One_option::dash, \
330       NULL, NULL, NULL, NULL, func }
331 #define SPECIAL(short_option, long_option, doc, help, dash, func)       \
332   { short_option, long_option, doc, help, options::One_option::dash, \
333       func, NULL, NULL, NULL, NULL }
334
335 // Here is the actual list of options which we accept.
336
337 const options::One_option
338 options::Command_line_options::options[] =
339 {
340   POSDEP_NOARG('\0', "as-needed",
341                N_("Only set DT_NEEDED for dynamic libs if used"),
342                NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
343   POSDEP_NOARG('\0', "no-as-needed",
344                N_("Always DT_NEEDED for dynamic libs (default)"),
345                NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed),
346   POSDEP_NOARG('\0', "Bdynamic",
347                N_("-l searches for shared libraries"),
348                NULL, ONE_DASH,
349                &Position_dependent_options::set_dynamic_search),
350   POSDEP_NOARG('\0', "Bstatic",
351                N_("-l does not search for shared libraries"),
352                NULL, ONE_DASH,
353                &Position_dependent_options::set_static_search),
354   GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
355                 NULL, ONE_DASH, &General_options::set_symbolic),
356   GENERAL_NOARG('\0', "detect-odr-violations",
357                 N_("Try to detect violations of the One Definition Rule"),
358                 NULL, TWO_DASHES, &General_options::set_detect_odr_violations),
359   GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
360                 NULL, TWO_DASHES, &General_options::set_export_dynamic),
361   GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
362                 NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr),
363   GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
364               N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES,
365               &General_options::set_dynamic_linker),
366   SPECIAL('l', "library", N_("Search for library LIBNAME"),
367           N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
368           &library),
369   GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
370               N_("-L DIR, --library-path DIR"), TWO_DASHES,
371               &General_options::add_to_search_path),
372   GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
373               &General_options::ignore),
374   GENERAL_ARG('o', "output", N_("Set output file name"),
375               N_("-o FILE, --output FILE"), TWO_DASHES,
376               &General_options::set_output_file_name),
377   GENERAL_ARG('O', NULL, N_("Optimize output file size"),
378               N_("-O level"), ONE_DASH,
379               &General_options::set_optimization_level),
380   GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
381                 ONE_DASH, &General_options::set_relocatable),
382   GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
383               N_("-R DIR, -rpath DIR"), ONE_DASH,
384               &General_options::add_to_rpath),
385   GENERAL_ARG('\0', "rpath-link",
386               N_("Add DIR to link time shared library search path"),
387               N_("--rpath-link DIR"), TWO_DASHES,
388               &General_options::add_to_rpath_link),
389   GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL,
390                 TWO_DASHES, &General_options::set_strip_all),
391   GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL,
392                 TWO_DASHES, &General_options::set_strip_debug),
393   GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
394                 NULL, ONE_DASH, &General_options::set_shared),
395   GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
396                 NULL, ONE_DASH, &General_options::set_static),
397   GENERAL_NOARG('\0', "stats", N_("Print resource usage statistics"),
398                 NULL, TWO_DASHES, &General_options::set_stats),
399   GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
400               N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot),
401   GENERAL_ARG('\0', "Ttext", N_("Set the address of the .text section"),
402               N_("-Ttext ADDRESS"), ONE_DASH,
403               &General_options::set_text_segment_address),
404   // This must come after -Ttext since it's a prefix of it.
405   SPECIAL('T', "script", N_("Read linker script"),
406           N_("-T FILE, --script FILE"), TWO_DASHES,
407           &invoke_script),
408   GENERAL_NOARG('\0', "threads", N_("Run the linker multi-threaded"),
409                 NULL, TWO_DASHES, &General_options::set_threads),
410   GENERAL_NOARG('\0', "no-threads", N_("Do not run the linker multi-threaded"),
411                 NULL, TWO_DASHES, &General_options::clear_threads),
412   GENERAL_ARG('\0', "thread-count", N_("Number of threads to use"),
413               N_("--thread-count COUNT"), TWO_DASHES,
414               &General_options::set_thread_count),
415   GENERAL_ARG('\0', "thread-count-initial",
416               N_("Number of threads to use in initial pass"),
417               N_("--thread-count-initial COUNT"), TWO_DASHES,
418               &General_options::set_thread_count_initial),
419   GENERAL_ARG('\0', "thread-count-middle",
420               N_("Number of threads to use in middle pass"),
421               N_("--thread-count-middle COUNT"), TWO_DASHES,
422               &General_options::set_thread_count_middle),
423   GENERAL_ARG('\0', "thread-count-final",
424               N_("Number of threads to use in final pass"),
425               N_("--thread-count-final COUNT"), TWO_DASHES,
426               &General_options::set_thread_count_final),
427   POSDEP_NOARG('\0', "whole-archive",
428                N_("Include all archive contents"),
429                NULL, TWO_DASHES,
430                &Position_dependent_options::set_whole_archive),
431   POSDEP_NOARG('\0', "no-whole-archive",
432                N_("Include only needed archive contents"),
433                NULL, TWO_DASHES,
434                &Position_dependent_options::clear_whole_archive),
435
436   GENERAL_ARG('z', NULL,
437               N_("Subcommands as follows:\n\
438     -z execstack              Mark output as requiring executable stack\n\
439     -z noexecstack            Mark output as not requiring executable stack"),
440               N_("-z SUBCOMMAND"), ONE_DASH,
441               &General_options::handle_z_option),
442
443   SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
444           TWO_DASHES, &start_group),
445   SPECIAL(')', "end-group", N_("End a library search group"), NULL,
446           TWO_DASHES, &end_group),
447   SPECIAL('\0', "help", N_("Report usage information"), NULL,
448           TWO_DASHES, &help),
449   SPECIAL('v', "version", N_("Report version information"), NULL,
450           TWO_DASHES, &version)
451 };
452
453 const int options::Command_line_options::options_size =
454   sizeof (options) / sizeof (options[0]);
455
456 // The -z options.
457
458 const options::One_z_option
459 options::Command_line_options::z_options[] =
460 {
461   { "execstack", &General_options::set_execstack },
462   { "noexecstack", &General_options::set_noexecstack },
463 };
464
465 const int options::Command_line_options::z_options_size =
466   sizeof(z_options) / sizeof(z_options[0]);
467
468 // The default values for the general options.
469
470 General_options::General_options()
471   : export_dynamic_(false),
472     dynamic_linker_(NULL),
473     search_path_(),
474     optimization_level_(0),
475     output_file_name_("a.out"),
476     is_relocatable_(false),
477     strip_(STRIP_NONE),
478     symbolic_(false),
479     detect_odr_violations_(false),
480     create_eh_frame_hdr_(false),
481     rpath_(),
482     rpath_link_(),
483     is_shared_(false),
484     is_static_(false),
485     print_stats_(false),
486     sysroot_(),
487     text_segment_address_(-1U),   // -1 indicates value not set by user
488     threads_(false),
489     thread_count_initial_(0),
490     thread_count_middle_(0),
491     thread_count_final_(0),
492     execstack_(EXECSTACK_FROM_INPUT)
493 {
494 }
495
496 // The default values for the position dependent options.
497
498 Position_dependent_options::Position_dependent_options()
499   : do_static_search_(false),
500     as_needed_(false),
501     include_whole_archive_(false)
502 {
503 }
504
505 // Handle the -z option.
506
507 void
508 General_options::handle_z_option(const char* arg)
509 {
510   const int z_options_size = options::Command_line_options::z_options_size;
511   const gold::options::One_z_option* z_options =
512     gold::options::Command_line_options::z_options;
513   for (int i = 0; i < z_options_size; ++i)
514     {
515       if (strcmp(arg, z_options[i].name) == 0)
516         {
517           (this->*(z_options[i].set))();
518           return;
519         }
520     }
521
522   fprintf(stderr, _("%s: unrecognized -z subcommand: %s\n"),
523           program_name, arg);
524   ::exit(1);
525 }
526
527 // Add the sysroot, if any, to the search paths.
528
529 void
530 General_options::add_sysroot()
531 {
532   if (this->sysroot_.empty())
533     {
534       this->sysroot_ = get_default_sysroot();
535       if (this->sysroot_.empty())
536         return;
537     }
538
539   const char* sysroot = this->sysroot_.c_str();
540   char* canonical_sysroot = lrealpath(sysroot);
541
542   for (Dir_list::iterator p = this->search_path_.begin();
543        p != this->search_path_.end();
544        ++p)
545     p->add_sysroot(sysroot, canonical_sysroot);
546
547   free(canonical_sysroot);
548 }
549
550 // Search_directory methods.
551
552 // This is called if we have a sysroot.  Apply the sysroot if
553 // appropriate.  Record whether the directory is in the sysroot.
554
555 void
556 Search_directory::add_sysroot(const char* sysroot,
557                               const char* canonical_sysroot)
558 {
559   gold_assert(*sysroot != '\0');
560   if (this->put_in_sysroot_)
561     {
562       if (!IS_DIR_SEPARATOR(this->name_[0])
563           && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
564         this->name_ = '/' + this->name_;
565       this->name_ = sysroot + this->name_;
566       this->is_in_sysroot_ = true;
567     }
568   else
569     {
570       // Check whether this entry is in the sysroot.  To do this
571       // correctly, we need to use canonical names.  Otherwise we will
572       // get confused by the ../../.. paths that gcc tends to use.
573       char* canonical_name = lrealpath(this->name_.c_str());
574       int canonical_name_len = strlen(canonical_name);
575       int canonical_sysroot_len = strlen(canonical_sysroot);
576       if (canonical_name_len > canonical_sysroot_len
577           && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
578         {
579           canonical_name[canonical_sysroot_len] = '\0';
580           if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
581             this->is_in_sysroot_ = true;
582         }
583       free(canonical_name);
584     }
585 }
586
587 // Input_arguments methods.
588
589 // Add a file to the list.
590
591 void
592 Input_arguments::add_file(const Input_file_argument& file)
593 {
594   if (!this->in_group_)
595     this->input_argument_list_.push_back(Input_argument(file));
596   else
597     {
598       gold_assert(!this->input_argument_list_.empty());
599       gold_assert(this->input_argument_list_.back().is_group());
600       this->input_argument_list_.back().group()->add_file(file);
601     }
602 }
603
604 // Start a group.
605
606 void
607 Input_arguments::start_group()
608 {
609   gold_assert(!this->in_group_);
610   Input_file_group* group = new Input_file_group();
611   this->input_argument_list_.push_back(Input_argument(group));
612   this->in_group_ = true;
613 }
614
615 // End a group.
616
617 void
618 Input_arguments::end_group()
619 {
620   gold_assert(this->in_group_);
621   this->in_group_ = false;
622 }
623
624 // Command_line options.
625
626 Command_line::Command_line()
627   : options_(), position_options_(), inputs_()
628 {
629 }
630
631 // Process the command line options.  For process_one_option,
632 // i is the index of argv to process next, and the return value
633 // is the index of the next option to process (i+1 or i+2, or argc
634 // to indicate processing is done).  no_more_options is set to true
635 // if (and when) "--" is seen as an option.
636
637 int
638 Command_line::process_one_option(int argc, char** argv, int i,
639                                  bool* no_more_options)
640 {
641   const int options_size = options::Command_line_options::options_size;
642   const options::One_option* options = options::Command_line_options::options;
643   gold_assert(i < argc);
644
645   if (argv[i][0] != '-' || *no_more_options)
646     {
647       this->add_file(argv[i], false);
648       return i + 1;
649     }
650
651   // Option starting with '-'.
652   int dashes = 1;
653   if (argv[i][1] == '-')
654     {
655       dashes = 2;
656       if (argv[i][2] == '\0')
657         {
658           *no_more_options = true;
659           return i + 1;
660         }
661     }
662
663   // Look for a long option match.
664   char* opt = argv[i] + dashes;
665   char first = opt[0];
666   int skiparg = 0;
667   char* arg = strchr(opt, '=');
668   bool argument_with_equals = arg != NULL;
669   if (arg != NULL)
670     {
671       *arg = '\0';
672       ++arg;
673     }
674   else if (i + 1 < argc)
675     {
676       arg = argv[i + 1];
677       skiparg = 1;
678     }
679
680   int j;
681   for (j = 0; j < options_size; ++j)
682     {
683       if (options[j].long_option != NULL
684           && (dashes == 2
685           || (options[j].dash
686               != options::One_option::EXACTLY_TWO_DASHES))
687           && first == options[j].long_option[0]
688           && strcmp(opt, options[j].long_option) == 0)
689         {
690           if (options[j].special)
691             {
692               // Restore the '=' we clobbered above.
693               if (arg != NULL && skiparg == 0)
694                 arg[-1] = '=';
695               i += options[j].special(argc - i, argv + i, opt, true, this);
696             }
697           else
698             {
699               if (!options[j].takes_argument())
700                 {
701                   if (argument_with_equals)
702                     this->usage(_("unexpected argument"), argv[i]);
703                   arg = NULL;
704                   skiparg = 0;
705                 }
706               else
707                 {
708                   if (arg == NULL)
709                     this->usage(_("missing argument"), argv[i]);
710                 }
711               this->apply_option(options[j], arg);
712               i += skiparg + 1;
713             }
714           break;
715         }
716     }
717   if (j < options_size)
718     return i;
719
720   // If we saw two dashes, we needed to have seen a long option.
721   if (dashes == 2)
722     this->usage(_("unknown option"), argv[i]);
723
724   // Look for a short option match.  There may be more than one
725   // short option in a given argument.
726   bool done = false;
727   char* s = argv[i] + 1;
728   ++i;
729   while (*s != '\0' && !done)
730     {
731       char opt = *s;
732       int j;
733       for (j = 0; j < options_size; ++j)
734         {
735           if (options[j].short_option == opt)
736             {
737               if (options[j].special)
738                 {
739                   // Undo the argument skip done above.
740                   --i;
741                   i += options[j].special(argc - i, argv + i, s, false,
742                                           this);
743                   done = true;
744                 }
745               else
746                 {
747                   arg = NULL;
748                   if (options[j].takes_argument())
749                     {
750                       if (s[1] != '\0')
751                         {
752                           arg = s + 1;
753                           done = true;
754                         }
755                       else if (i < argc)
756                         {
757                           arg = argv[i];
758                           ++i;
759                         }
760                       else
761                         this->usage(_("missing argument"), opt);
762                     }
763                   this->apply_option(options[j], arg);
764                 }
765               break;
766             }
767         }
768
769       if (j >= options_size)
770         this->usage(_("unknown option"), *s);
771
772       ++s;
773     }
774   return i;
775 }
776
777
778 void
779 Command_line::process(int argc, char** argv)
780 {
781   bool no_more_options = false;
782   int i = 0;
783   while (i < argc)
784     i = process_one_option(argc, argv, i, &no_more_options);
785
786   if (this->inputs_.in_group())
787     {
788       fprintf(stderr, _("%s: missing group end\n"), program_name);
789       this->usage();
790     }
791
792   // FIXME: We should only do this when configured in native mode.
793   this->options_.add_to_search_path_with_sysroot("/lib");
794   this->options_.add_to_search_path_with_sysroot("/usr/lib");
795
796   this->options_.add_sysroot();
797
798   // Ensure options don't contradict each other and are otherwise kosher.
799   this->normalize_options();
800 }
801
802 // Extract an option argument for a special option.  LONGNAME is the
803 // long name of the option.  This sets *PRET to the return value for
804 // the special function handler to skip to the next option.
805
806 const char*
807 Command_line::get_special_argument(const char* longname, int argc, char** argv,
808                                    const char* arg, bool long_option,
809                                    int *pret)
810 {
811   if (long_option)
812     {
813       size_t longlen = strlen(longname);
814       gold_assert(strncmp(arg, longname, longlen) == 0);
815       arg += longlen;
816       if (*arg == '=')
817         {
818           *pret = 1;
819           return arg + 1;
820         }
821       else if (argc > 1)
822         {
823           gold_assert(*arg == '\0');
824           *pret = 2;
825           return argv[1];
826         }
827     }
828   else
829     {
830       if (arg[1] != '\0')
831         {
832           *pret = 1;
833           return arg + 1;
834         }
835       else if (argc > 1)
836         {
837           *pret = 2;
838           return argv[1];
839         }
840     }
841
842   this->usage(_("missing argument"), arg);
843 }
844
845 // Ensure options don't contradict each other and are otherwise kosher.
846
847 void
848 Command_line::normalize_options()
849 {
850   // If the user specifies both -s and -r, convert the -s as -S.
851   // -r requires us to keep externally visible symbols!
852   if (this->options_.strip_all() && this->options_.is_relocatable())
853     {
854       // Clears the strip_all() status, replacing it with strip_debug().
855       this->options_.set_strip_debug();
856     }
857
858   // FIXME: we can/should be doing a lot more sanity checking here.
859 }
860
861
862 // Apply a command line option.
863
864 void
865 Command_line::apply_option(const options::One_option& opt,
866                            const char* arg)
867 {
868   if (arg == NULL)
869     {
870       if (opt.general_noarg)
871         (this->options_.*(opt.general_noarg))();
872       else if (opt.dependent_noarg)
873         (this->position_options_.*(opt.dependent_noarg))();
874       else
875         gold_unreachable();
876     }
877   else
878     {
879       if (opt.general_arg)
880         (this->options_.*(opt.general_arg))(arg);
881       else if (opt.dependent_arg)
882         (this->position_options_.*(opt.dependent_arg))(arg);
883       else
884         gold_unreachable();
885     }
886 }
887
888 // Add an input file or library.
889
890 void
891 Command_line::add_file(const char* name, bool is_lib)
892 {
893   Input_file_argument file(name, is_lib, "", this->position_options_);
894   this->inputs_.add_file(file);
895 }
896
897 // Handle the -l option, which requires special treatment.
898
899 int
900 Command_line::process_l_option(int argc, char** argv, char* arg,
901                                bool long_option)
902 {
903   int ret;
904   const char* libname = this->get_special_argument("library", argc, argv, arg,
905                                                    long_option, &ret);
906   this->add_file(libname, true);
907   return ret;
908 }
909
910 // Handle the --start-group option.
911
912 void
913 Command_line::start_group(const char* arg)
914 {
915   if (this->inputs_.in_group())
916     this->usage(_("may not nest groups"), arg);
917   this->inputs_.start_group();
918 }
919
920 // Handle the --end-group option.
921
922 void
923 Command_line::end_group(const char* arg)
924 {
925   if (!this->inputs_.in_group())
926     this->usage(_("group end without group start"), arg);
927   this->inputs_.end_group();
928 }
929
930 // Report a usage error.  */
931
932 void
933 Command_line::usage()
934 {
935   fprintf(stderr,
936           _("%s: use the --help option for usage information\n"),
937           program_name);
938   ::exit(1);
939 }
940
941 void
942 Command_line::usage(const char* msg, const char *opt)
943 {
944   fprintf(stderr,
945           _("%s: %s: %s\n"),
946           program_name, opt, msg);
947   this->usage();
948 }
949
950 void
951 Command_line::usage(const char* msg, char opt)
952 {
953   fprintf(stderr,
954           _("%s: -%c: %s\n"),
955           program_name, opt, msg);
956   this->usage();
957 }
958
959 } // End namespace gold.