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