Implement -s and -S options which strip symbols.
[deliverable/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 class options::Command_line_options
97 {
98 public:
99 static const One_option options[];
100 static const int options_size;
101 };
102
103 } // End namespace gold.
104
105 namespace
106 {
107
108 // Handle the special -l option, which adds an input file.
109
110 int
111 library(int argc, char** argv, char* arg, gold::Command_line* cmdline)
112 {
113 return cmdline->process_l_option(argc, argv, arg);
114 }
115
116 // Handle the special --start-group option.
117
118 int
119 start_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
127 int
128 end_group(int, char**, char* arg, gold::Command_line* cmdline)
129 {
130 cmdline->end_group(arg);
131 return 1;
132 }
133
134 // Report usage information for ld --help, and exit.
135
136 int
137 help(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);
165 comma = true;
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;
178 comma = true;
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);
200 comma = true;
201 }
202 }
203 ++j;
204 }
205 while (j < options_size && options[j].doc == NULL);
206
207 if (len >= 30)
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
218 gold::gold_exit(true);
219
220 return 0;
221 }
222
223 // Report version information.
224
225 int
226 version(int, char**, char* opt, gold::Command_line*)
227 {
228 gold::print_version(opt[0] == 'v' && opt[1] == '\0');
229 gold::gold_exit(true);
230 return 0;
231 }
232
233 // If the default sysroot is relocatable, try relocating it based on
234 // the prefix FROM.
235
236 char*
237 get_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
255 std::string
256 get_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
278 } // End anonymous namespace.
279
280 namespace gold
281 {
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) \
289 { short_option, long_option, doc, help, options::One_option::dash, \
290 NULL, func, NULL, NULL, NULL }
291 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
292 { short_option, long_option, doc, help, options::One_option::dash, \
293 NULL, NULL, func, NULL, NULL }
294 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
295 { short_option, long_option, doc, help, options::One_option::dash, \
296 NULL, NULL, NULL, func, NULL }
297 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
298 { short_option, long_option, doc, help, options::One_option::dash, \
299 NULL, NULL, NULL, NULL, func }
300 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
301 { short_option, long_option, doc, help, options::One_option::dash, \
302 func, NULL, NULL, NULL, NULL }
303
304 // Here is the actual list of options which we accept.
305
306 const options::One_option
307 options::Command_line_options::options[] =
308 {
309 SPECIAL('l', "library", N_("Search for library LIBNAME"),
310 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
311 &library),
312 SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
313 TWO_DASHES, &start_group),
314 SPECIAL(')', "end-group", N_("End a library search group"), NULL,
315 TWO_DASHES, &end_group),
316 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
317 NULL, TWO_DASHES, &General_options::set_export_dynamic),
318 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
319 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES,
320 &General_options::set_dynamic_linker),
321 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
322 N_("-L DIR, --library-path DIR"), TWO_DASHES,
323 &General_options::add_to_search_path),
324 GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
325 &General_options::ignore),
326 GENERAL_ARG('O', NULL, N_("Optimize output file size"),
327 N_("-O level"), ONE_DASH,
328 &General_options::set_optimization_level),
329 GENERAL_ARG('o', "output", N_("Set output file name"),
330 N_("-o FILE, --output FILE"), TWO_DASHES,
331 &General_options::set_output_file_name),
332 GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
333 ONE_DASH, &General_options::set_relocatable),
334 GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
335 N_("-R DIR, -rpath DIR"), ONE_DASH,
336 &General_options::add_to_rpath),
337 GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL,
338 TWO_DASHES, &General_options::set_strip_all),
339 GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL,
340 TWO_DASHES, &General_options::set_strip_debug),
341 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
342 NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr),
343 GENERAL_ARG('\0', "rpath-link",
344 N_("Add DIR to link time shared library search path"),
345 N_("--rpath-link DIR"), TWO_DASHES,
346 &General_options::add_to_rpath_link),
347 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
348 NULL, ONE_DASH, &General_options::set_shared),
349 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
350 NULL, ONE_DASH, &General_options::set_static),
351 GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
352 N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot),
353 POSDEP_NOARG('\0', "as-needed",
354 N_("Only set DT_NEEDED for dynamic libs if used"),
355 NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
356 POSDEP_NOARG('\0', "no-as-needed",
357 N_("Always DT_NEEDED for dynamic libs (default)"),
358 NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed),
359 POSDEP_NOARG('\0', "whole-archive",
360 N_("Include all archive contents"),
361 NULL, TWO_DASHES,
362 &Position_dependent_options::set_whole_archive),
363 POSDEP_NOARG('\0', "no-whole-archive",
364 N_("Include only needed archive contents"),
365 NULL, TWO_DASHES,
366 &Position_dependent_options::clear_whole_archive),
367 SPECIAL('\0', "help", N_("Report usage information"), NULL,
368 TWO_DASHES, &help),
369 SPECIAL('v', "version", N_("Report version information"), NULL,
370 TWO_DASHES, &version)
371 };
372
373 const int options::Command_line_options::options_size =
374 sizeof (options) / sizeof (options[0]);
375
376 // The default values for the general options.
377
378 General_options::General_options()
379 : export_dynamic_(false),
380 dynamic_linker_(NULL),
381 search_path_(),
382 optimization_level_(0),
383 output_file_name_("a.out"),
384 is_relocatable_(false),
385 strip_(STRIP_NONE),
386 create_eh_frame_hdr_(false),
387 rpath_(),
388 rpath_link_(),
389 is_shared_(false),
390 is_static_(false),
391 sysroot_()
392 {
393 }
394
395 // The default values for the position dependent options.
396
397 Position_dependent_options::Position_dependent_options()
398 : do_static_search_(false),
399 as_needed_(false),
400 include_whole_archive_(false)
401 {
402 }
403
404 // Add the sysroot, if any, to the search paths.
405
406 void
407 General_options::add_sysroot()
408 {
409 if (this->sysroot_.empty())
410 {
411 this->sysroot_ = get_default_sysroot();
412 if (this->sysroot_.empty())
413 return;
414 }
415
416 const char* sysroot = this->sysroot_.c_str();
417 char* canonical_sysroot = lrealpath(sysroot);
418
419 for (Dir_list::iterator p = this->search_path_.begin();
420 p != this->search_path_.end();
421 ++p)
422 p->add_sysroot(sysroot, canonical_sysroot);
423
424 free(canonical_sysroot);
425 }
426
427 // Search_directory methods.
428
429 // This is called if we have a sysroot. Apply the sysroot if
430 // appropriate. Record whether the directory is in the sysroot.
431
432 void
433 Search_directory::add_sysroot(const char* sysroot,
434 const char* canonical_sysroot)
435 {
436 gold_assert(*sysroot != '\0');
437 if (this->put_in_sysroot_)
438 {
439 if (!IS_DIR_SEPARATOR(this->name_[0])
440 && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
441 this->name_ = '/' + this->name_;
442 this->name_ = sysroot + this->name_;
443 this->is_in_sysroot_ = true;
444 }
445 else
446 {
447 // Check whether this entry is in the sysroot. To do this
448 // correctly, we need to use canonical names. Otherwise we will
449 // get confused by the ../../.. paths that gcc tends to use.
450 char* canonical_name = lrealpath(this->name_.c_str());
451 int canonical_name_len = strlen(canonical_name);
452 int canonical_sysroot_len = strlen(canonical_sysroot);
453 if (canonical_name_len > canonical_sysroot_len
454 && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
455 {
456 canonical_name[canonical_sysroot_len] = '\0';
457 if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
458 this->is_in_sysroot_ = true;
459 }
460 free(canonical_name);
461 }
462 }
463
464 // Input_arguments methods.
465
466 // Add a file to the list.
467
468 void
469 Input_arguments::add_file(const Input_file_argument& file)
470 {
471 if (!this->in_group_)
472 this->input_argument_list_.push_back(Input_argument(file));
473 else
474 {
475 gold_assert(!this->input_argument_list_.empty());
476 gold_assert(this->input_argument_list_.back().is_group());
477 this->input_argument_list_.back().group()->add_file(file);
478 }
479 }
480
481 // Start a group.
482
483 void
484 Input_arguments::start_group()
485 {
486 gold_assert(!this->in_group_);
487 Input_file_group* group = new Input_file_group();
488 this->input_argument_list_.push_back(Input_argument(group));
489 this->in_group_ = true;
490 }
491
492 // End a group.
493
494 void
495 Input_arguments::end_group()
496 {
497 gold_assert(this->in_group_);
498 this->in_group_ = false;
499 }
500
501 // Command_line options.
502
503 Command_line::Command_line()
504 : options_(), position_options_(), inputs_()
505 {
506 }
507
508 // Process the command line options.
509
510 void
511 Command_line::process(int argc, char** argv)
512 {
513 const int options_size = options::Command_line_options::options_size;
514 const options::One_option* options =
515 options::Command_line_options::options;
516 bool no_more_options = false;
517 int i = 0;
518 while (i < argc)
519 {
520 if (argv[i][0] != '-' || no_more_options)
521 {
522 this->add_file(argv[i], false);
523 ++i;
524 continue;
525 }
526
527 // Option starting with '-'.
528 int dashes = 1;
529 if (argv[i][1] == '-')
530 {
531 dashes = 2;
532 if (argv[i][2] == '\0')
533 {
534 no_more_options = true;
535 continue;
536 }
537 }
538
539 // Look for a long option match.
540 char* opt = argv[i] + dashes;
541 char first = opt[0];
542 int skiparg = 0;
543 char* arg = strchr(opt, '=');
544 bool argument_with_equals = arg != NULL;
545 if (arg != NULL)
546 {
547 *arg = '\0';
548 ++arg;
549 }
550 else if (i + 1 < argc)
551 {
552 arg = argv[i + 1];
553 skiparg = 1;
554 }
555
556 int j;
557 for (j = 0; j < options_size; ++j)
558 {
559 if (options[j].long_option != NULL
560 && (dashes == 2
561 || (options[j].dash
562 != options::One_option::EXACTLY_TWO_DASHES))
563 && first == options[j].long_option[0]
564 && strcmp(opt, options[j].long_option) == 0)
565 {
566 if (options[j].special)
567 i += options[j].special(argc - 1, argv + i, opt, this);
568 else
569 {
570 if (!options[j].takes_argument())
571 {
572 if (argument_with_equals)
573 this->usage(_("unexpected argument"), argv[i]);
574 arg = NULL;
575 skiparg = 0;
576 }
577 else
578 {
579 if (arg == NULL)
580 this->usage(_("missing argument"), argv[i]);
581 }
582 this->apply_option(options[j], arg);
583 i += skiparg + 1;
584 }
585 break;
586 }
587 }
588 if (j < options_size)
589 continue;
590
591 // If we saw two dashes, we need to see a long option.
592 if (dashes == 2)
593 this->usage(_("unknown option"), argv[i]);
594
595 // Look for a short option match. There may be more than one
596 // short option in a given argument.
597 bool done = false;
598 char* s = argv[i] + 1;
599 ++i;
600 while (*s != '\0' && !done)
601 {
602 char opt = *s;
603 int j;
604 for (j = 0; j < options_size; ++j)
605 {
606 if (options[j].short_option == opt)
607 {
608 if (options[j].special)
609 {
610 // Undo the argument skip done above.
611 --i;
612 i += options[j].special(argc - i, argv + i, s, this);
613 done = true;
614 }
615 else
616 {
617 arg = NULL;
618 if (options[j].takes_argument())
619 {
620 if (s[1] != '\0')
621 {
622 arg = s + 1;
623 done = true;
624 }
625 else if (i < argc)
626 {
627 arg = argv[i];
628 ++i;
629 }
630 else
631 this->usage(_("missing argument"), opt);
632 }
633 this->apply_option(options[j], arg);
634 }
635 break;
636 }
637 }
638
639 if (j >= options_size)
640 this->usage(_("unknown option"), *s);
641
642 ++s;
643 }
644 }
645
646 if (this->inputs_.in_group())
647 {
648 fprintf(stderr, _("%s: missing group end"), program_name);
649 this->usage();
650 }
651
652 // FIXME: We should only do this when configured in native mode.
653 this->options_.add_to_search_path_with_sysroot("/lib");
654 this->options_.add_to_search_path_with_sysroot("/usr/lib");
655
656 this->options_.add_sysroot();
657 }
658
659 // Apply a command line option.
660
661 void
662 Command_line::apply_option(const options::One_option& opt,
663 const char* arg)
664 {
665 if (arg == NULL)
666 {
667 if (opt.general_noarg)
668 (this->options_.*(opt.general_noarg))();
669 else if (opt.dependent_noarg)
670 (this->position_options_.*(opt.dependent_noarg))();
671 else
672 gold_unreachable();
673 }
674 else
675 {
676 if (opt.general_arg)
677 (this->options_.*(opt.general_arg))(arg);
678 else if (opt.dependent_arg)
679 (this->position_options_.*(opt.dependent_arg))(arg);
680 else
681 gold_unreachable();
682 }
683 }
684
685 // Add an input file or library.
686
687 void
688 Command_line::add_file(const char* name, bool is_lib)
689 {
690 Input_file_argument file(name, is_lib, "", this->position_options_);
691 this->inputs_.add_file(file);
692 }
693
694 // Handle the -l option, which requires special treatment.
695
696 int
697 Command_line::process_l_option(int argc, char** argv, char* arg)
698 {
699 int ret;
700 const char* libname;
701 if (arg[1] != '\0')
702 {
703 ret = 1;
704 libname = arg + 1;
705 }
706 else if (argc > 1)
707 {
708 ret = 2;
709 libname = argv[argc + 1];
710 }
711 else
712 this->usage(_("missing argument"), arg);
713
714 this->add_file(libname, true);
715
716 return ret;
717 }
718
719 // Handle the --start-group option.
720
721 void
722 Command_line::start_group(const char* arg)
723 {
724 if (this->inputs_.in_group())
725 this->usage(_("may not nest groups"), arg);
726 this->inputs_.start_group();
727 }
728
729 // Handle the --end-group option.
730
731 void
732 Command_line::end_group(const char* arg)
733 {
734 if (!this->inputs_.in_group())
735 this->usage(_("group end without group start"), arg);
736 this->inputs_.end_group();
737 }
738
739 // Report a usage error. */
740
741 void
742 Command_line::usage()
743 {
744 fprintf(stderr,
745 _("%s: use the --help option for usage information\n"),
746 program_name);
747 gold_exit(false);
748 }
749
750 void
751 Command_line::usage(const char* msg, const char *opt)
752 {
753 fprintf(stderr,
754 _("%s: %s: %s\n"),
755 program_name, opt, msg);
756 this->usage();
757 }
758
759 void
760 Command_line::usage(const char* msg, char opt)
761 {
762 fprintf(stderr,
763 _("%s: -%c: %s\n"),
764 program_name, opt, msg);
765 this->usage();
766 }
767
768 } // End namespace gold.
This page took 0.046727 seconds and 5 git commands to generate.