OSDN Git Service

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