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