1 // options.c -- handle command line options for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
28 #include "filenames.h"
29 #include "libiberty.h"
36 // The information we keep for a single command line option.
38 struct options::One_option
40 // The single character option name, or '\0' if this is only a long
44 // The long option name, or NULL if this is only a short option.
45 const char* long_option;
47 // Description of the option for --help output, or NULL if there is none.
50 // How to print the option name in --help output, or NULL to use the
52 const char* help_output;
54 // Long option dash control. This is ignored if long_option is
58 // Long option normally takes one dash; two dashes are also
61 // Long option normally takes two dashes; one dash is also
64 // Long option always takes two dashes.
68 // Function for special handling, or NULL. Returns the number of
69 // arguments to skip. This will normally be at least 1, but it may
70 // be 0 if this function changes *argv. ARG points to the location
71 // in *ARGV where the option starts, which may be helpful for a
73 int (*special)(int argc, char** argv, char *arg, bool long_option,
76 // If this is a position independent option which does not take an
77 // argument, this is the member function to call to record it.
78 void (General_options::*general_noarg)();
80 // If this is a position independent function which takes an
81 // argument, this is the member function to call to record it.
82 void (General_options::*general_arg)(const char*);
84 // If this is a position dependent option which does not take an
85 // argument, this is the member function to call to record it.
86 void (Position_dependent_options::*dependent_noarg)();
88 // If this is a position dependent option which takes an argument,
89 // this is the member function to record it.
90 void (Position_dependent_options::*dependent_arg)(const char*);
92 // Return whether this option takes an argument.
94 takes_argument() const
95 { return this->general_arg != NULL || this->dependent_arg != NULL; }
98 // We have a separate table for -z options.
100 struct options::One_z_option
102 // The name of the option.
105 // The member function in General_options called to record it.
106 void (General_options::*set)();
109 class options::Command_line_options
112 static const One_option options[];
113 static const int options_size;
114 static const One_z_option z_options[];
115 static const int z_options_size;
118 } // End namespace gold.
123 // Handle the special -l option, which adds an input file.
126 library(int argc, char** argv, char* arg, bool long_option,
127 gold::Command_line* cmdline)
129 return cmdline->process_l_option(argc, argv, arg, long_option);
132 // Handle the special -T/--script option, which reads a linker script.
135 invoke_script(int argc, char** argv, char* arg, bool long_option,
136 gold::Command_line* cmdline)
139 const char* script_name = cmdline->get_special_argument("script", argc, argv,
142 if (!read_commandline_script(script_name, cmdline))
143 gold::gold_error(_("%s: unable to parse script file %s\n"),
144 gold::program_name, arg);
148 // Handle the special --start-group option.
151 start_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
153 cmdline->start_group(arg);
157 // Handle the special --end-group option.
160 end_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
162 cmdline->end_group(arg);
166 // Report usage information for ld --help, and exit.
169 help(int, char**, char*, bool, gold::Command_line*)
171 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
173 const int options_size = gold::options::Command_line_options::options_size;
174 const gold::options::One_option* options =
175 gold::options::Command_line_options::options;
176 for (int i = 0; i < options_size; ++i)
178 if (options[i].doc == NULL)
188 if (options[j].help_output != NULL)
195 printf(options[j].help_output);
196 len += std::strlen(options[i].help_output);
201 if (options[j].short_option != '\0')
208 printf("-%c", options[j].short_option);
213 if (options[j].long_option != NULL)
220 if (options[j].dash == gold::options::One_option::ONE_DASH)
230 printf("%s", options[j].long_option);
231 len += std::strlen(options[j].long_option);
237 while (j < options_size && options[j].doc == NULL);
244 for (; len < 30; ++len)
247 std::puts(options[i].doc);
255 // Report version information.
258 version(int, char**, char* opt, bool, gold::Command_line*)
260 gold::print_version(opt[0] == 'v' && opt[1] == '\0');
265 // If the default sysroot is relocatable, try relocating it based on
269 get_relative_sysroot(const char* from)
271 char* path = make_relative_prefix(gold::program_name, from,
276 if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
284 // Return the default sysroot. This is set by the --with-sysroot
285 // option to configure.
288 get_default_sysroot()
290 const char* sysroot = TARGET_SYSTEM_ROOT;
291 if (*sysroot == '\0')
294 if (TARGET_SYSTEM_ROOT_RELOCATABLE)
296 char* path = get_relative_sysroot (BINDIR);
298 path = get_relative_sysroot (TOOLBINDIR);
301 std::string ret = path;
310 } // End anonymous namespace.
315 // Helper macros used to specify the options. We could also do this
316 // using constructors, but then g++ would generate code to initialize
317 // the array. We want the array to be initialized statically so that
318 // we get better startup time.
320 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
321 { short_option, long_option, doc, help, options::One_option::dash, \
322 NULL, func, NULL, NULL, NULL }
323 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
324 { short_option, long_option, doc, help, options::One_option::dash, \
325 NULL, NULL, func, NULL, NULL }
326 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
327 { short_option, long_option, doc, help, options::One_option::dash, \
328 NULL, NULL, NULL, func, NULL }
329 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
330 { short_option, long_option, doc, help, options::One_option::dash, \
331 NULL, NULL, NULL, NULL, func }
332 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
333 { short_option, long_option, doc, help, options::One_option::dash, \
334 func, NULL, NULL, NULL, NULL }
336 // Here is the actual list of options which we accept.
338 const options::One_option
339 options::Command_line_options::options[] =
341 GENERAL_NOARG('\0', "allow-shlib-undefined",
342 N_("Allow unresolved references in shared libraries"),
344 &General_options::set_allow_shlib_undefined),
345 GENERAL_NOARG('\0', "no-allow-shlib-undefined",
346 N_("Do not allow unresolved references in shared libraries"),
348 &General_options::set_no_allow_shlib_undefined),
349 POSDEP_NOARG('\0', "as-needed",
350 N_("Only set DT_NEEDED for dynamic libs if used"),
351 NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
352 POSDEP_NOARG('\0', "no-as-needed",
353 N_("Always DT_NEEDED for dynamic libs (default)"),
354 NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed),
355 POSDEP_NOARG('\0', "Bdynamic",
356 N_("-l searches for shared libraries"),
358 &Position_dependent_options::set_dynamic_search),
359 POSDEP_NOARG('\0', "Bstatic",
360 N_("-l does not search for shared libraries"),
362 &Position_dependent_options::set_static_search),
363 GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
364 NULL, ONE_DASH, &General_options::set_symbolic),
365 GENERAL_NOARG('\0', "demangle", N_("Demangle C++ symbols in log messages"),
366 NULL, TWO_DASHES, &General_options::set_demangle),
367 GENERAL_NOARG('\0', "no-demangle",
368 N_("Do not demangle C++ symbols in log messages"),
369 NULL, TWO_DASHES, &General_options::clear_demangle),
370 GENERAL_NOARG('\0', "detect-odr-violations",
371 N_("Try to detect violations of the One Definition Rule"),
372 NULL, TWO_DASHES, &General_options::set_detect_odr_violations),
373 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
374 NULL, TWO_DASHES, &General_options::set_export_dynamic),
375 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
376 NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr),
377 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
378 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES,
379 &General_options::set_dynamic_linker),
380 SPECIAL('l', "library", N_("Search for library LIBNAME"),
381 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
383 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
384 N_("-L DIR, --library-path DIR"), TWO_DASHES,
385 &General_options::add_to_search_path),
386 GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
387 &General_options::ignore),
388 GENERAL_ARG('o', "output", N_("Set output file name"),
389 N_("-o FILE, --output FILE"), TWO_DASHES,
390 &General_options::set_output_file_name),
391 GENERAL_ARG('O', NULL, N_("Optimize output file size"),
392 N_("-O level"), ONE_DASH,
393 &General_options::set_optimization_level),
394 GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
395 ONE_DASH, &General_options::set_relocatable),
396 GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
397 N_("-R DIR, -rpath DIR"), ONE_DASH,
398 &General_options::add_to_rpath),
399 GENERAL_ARG('\0', "rpath-link",
400 N_("Add DIR to link time shared library search path"),
401 N_("--rpath-link DIR"), TWO_DASHES,
402 &General_options::add_to_rpath_link),
403 GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL,
404 TWO_DASHES, &General_options::set_strip_all),
405 GENERAL_NOARG('\0', "strip-debug-gdb",
406 N_("Strip debug symbols that are unused by gdb "
407 "(at least versions <= 6.7)"),
408 NULL, TWO_DASHES, &General_options::set_strip_debug_gdb),
409 // This must come after -Sdebug since it's a prefix of it.
410 GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL,
411 TWO_DASHES, &General_options::set_strip_debug),
412 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
413 NULL, ONE_DASH, &General_options::set_shared),
414 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
415 NULL, ONE_DASH, &General_options::set_static),
416 GENERAL_NOARG('\0', "stats", N_("Print resource usage statistics"),
417 NULL, TWO_DASHES, &General_options::set_stats),
418 GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
419 N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot),
420 GENERAL_ARG('\0', "Ttext", N_("Set the address of the .text section"),
421 N_("-Ttext ADDRESS"), ONE_DASH,
422 &General_options::set_text_segment_address),
423 // This must come after -Ttext since it's a prefix of it.
424 SPECIAL('T', "script", N_("Read linker script"),
425 N_("-T FILE, --script FILE"), TWO_DASHES,
427 GENERAL_NOARG('\0', "threads", N_("Run the linker multi-threaded"),
428 NULL, TWO_DASHES, &General_options::set_threads),
429 GENERAL_NOARG('\0', "no-threads", N_("Do not run the linker multi-threaded"),
430 NULL, TWO_DASHES, &General_options::clear_threads),
431 GENERAL_ARG('\0', "thread-count", N_("Number of threads to use"),
432 N_("--thread-count COUNT"), TWO_DASHES,
433 &General_options::set_thread_count),
434 GENERAL_ARG('\0', "thread-count-initial",
435 N_("Number of threads to use in initial pass"),
436 N_("--thread-count-initial COUNT"), TWO_DASHES,
437 &General_options::set_thread_count_initial),
438 GENERAL_ARG('\0', "thread-count-middle",
439 N_("Number of threads to use in middle pass"),
440 N_("--thread-count-middle COUNT"), TWO_DASHES,
441 &General_options::set_thread_count_middle),
442 GENERAL_ARG('\0', "thread-count-final",
443 N_("Number of threads to use in final pass"),
444 N_("--thread-count-final COUNT"), TWO_DASHES,
445 &General_options::set_thread_count_final),
446 POSDEP_NOARG('\0', "whole-archive",
447 N_("Include all archive contents"),
449 &Position_dependent_options::set_whole_archive),
450 POSDEP_NOARG('\0', "no-whole-archive",
451 N_("Include only needed archive contents"),
453 &Position_dependent_options::clear_whole_archive),
455 GENERAL_ARG('z', NULL,
456 N_("Subcommands as follows:\n\
457 -z execstack Mark output as requiring executable stack\n\
458 -z noexecstack Mark output as not requiring executable stack"),
459 N_("-z SUBCOMMAND"), ONE_DASH,
460 &General_options::handle_z_option),
462 SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
463 TWO_DASHES, &start_group),
464 SPECIAL(')', "end-group", N_("End a library search group"), NULL,
465 TWO_DASHES, &end_group),
466 SPECIAL('\0', "help", N_("Report usage information"), NULL,
468 SPECIAL('v', "version", N_("Report version information"), NULL,
469 TWO_DASHES, &version)
472 const int options::Command_line_options::options_size =
473 sizeof (options) / sizeof (options[0]);
477 const options::One_z_option
478 options::Command_line_options::z_options[] =
480 { "execstack", &General_options::set_execstack },
481 { "noexecstack", &General_options::set_noexecstack },
484 const int options::Command_line_options::z_options_size =
485 sizeof(z_options) / sizeof(z_options[0]);
487 // The default values for the general options.
489 General_options::General_options()
490 : export_dynamic_(false),
491 dynamic_linker_(NULL),
493 optimization_level_(0),
494 output_file_name_("a.out"),
495 is_relocatable_(false),
497 allow_shlib_undefined_(false),
499 detect_odr_violations_(false),
500 create_eh_frame_hdr_(false),
507 text_segment_address_(-1U), // -1 indicates value not set by user
509 thread_count_initial_(0),
510 thread_count_middle_(0),
511 thread_count_final_(0),
512 execstack_(EXECSTACK_FROM_INPUT)
514 // We initialize demangle_ based on the environment variable
515 // COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
516 // output of the linker, unless COLLECT_NO_DEMANGLE is set in the
517 // environment. Acting the same way here lets us provide the same
518 // interface by default.
519 this->demangle_ = getenv("COLLECT_NO_DEMANGLE") == NULL;
522 // The default values for the position dependent options.
524 Position_dependent_options::Position_dependent_options()
525 : do_static_search_(false),
527 include_whole_archive_(false)
531 // Handle the -z option.
534 General_options::handle_z_option(const char* arg)
536 const int z_options_size = options::Command_line_options::z_options_size;
537 const gold::options::One_z_option* z_options =
538 gold::options::Command_line_options::z_options;
539 for (int i = 0; i < z_options_size; ++i)
541 if (strcmp(arg, z_options[i].name) == 0)
543 (this->*(z_options[i].set))();
548 fprintf(stderr, _("%s: unrecognized -z subcommand: %s\n"),
553 // Add the sysroot, if any, to the search paths.
556 General_options::add_sysroot()
558 if (this->sysroot_.empty())
560 this->sysroot_ = get_default_sysroot();
561 if (this->sysroot_.empty())
565 const char* sysroot = this->sysroot_.c_str();
566 char* canonical_sysroot = lrealpath(sysroot);
568 for (Dir_list::iterator p = this->search_path_.begin();
569 p != this->search_path_.end();
571 p->add_sysroot(sysroot, canonical_sysroot);
573 free(canonical_sysroot);
576 // Search_directory methods.
578 // This is called if we have a sysroot. Apply the sysroot if
579 // appropriate. Record whether the directory is in the sysroot.
582 Search_directory::add_sysroot(const char* sysroot,
583 const char* canonical_sysroot)
585 gold_assert(*sysroot != '\0');
586 if (this->put_in_sysroot_)
588 if (!IS_DIR_SEPARATOR(this->name_[0])
589 && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
590 this->name_ = '/' + this->name_;
591 this->name_ = sysroot + this->name_;
592 this->is_in_sysroot_ = true;
596 // Check whether this entry is in the sysroot. To do this
597 // correctly, we need to use canonical names. Otherwise we will
598 // get confused by the ../../.. paths that gcc tends to use.
599 char* canonical_name = lrealpath(this->name_.c_str());
600 int canonical_name_len = strlen(canonical_name);
601 int canonical_sysroot_len = strlen(canonical_sysroot);
602 if (canonical_name_len > canonical_sysroot_len
603 && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
605 canonical_name[canonical_sysroot_len] = '\0';
606 if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
607 this->is_in_sysroot_ = true;
609 free(canonical_name);
613 // Input_arguments methods.
615 // Add a file to the list.
618 Input_arguments::add_file(const Input_file_argument& file)
620 if (!this->in_group_)
621 this->input_argument_list_.push_back(Input_argument(file));
624 gold_assert(!this->input_argument_list_.empty());
625 gold_assert(this->input_argument_list_.back().is_group());
626 this->input_argument_list_.back().group()->add_file(file);
633 Input_arguments::start_group()
635 gold_assert(!this->in_group_);
636 Input_file_group* group = new Input_file_group();
637 this->input_argument_list_.push_back(Input_argument(group));
638 this->in_group_ = true;
644 Input_arguments::end_group()
646 gold_assert(this->in_group_);
647 this->in_group_ = false;
650 // Command_line options.
652 Command_line::Command_line()
653 : options_(), position_options_(), inputs_()
657 // Process the command line options. For process_one_option,
658 // i is the index of argv to process next, and the return value
659 // is the index of the next option to process (i+1 or i+2, or argc
660 // to indicate processing is done). no_more_options is set to true
661 // if (and when) "--" is seen as an option.
664 Command_line::process_one_option(int argc, char** argv, int i,
665 bool* no_more_options)
667 const int options_size = options::Command_line_options::options_size;
668 const options::One_option* options = options::Command_line_options::options;
669 gold_assert(i < argc);
671 if (argv[i][0] != '-' || *no_more_options)
673 this->add_file(argv[i], false);
677 // Option starting with '-'.
679 if (argv[i][1] == '-')
682 if (argv[i][2] == '\0')
684 *no_more_options = true;
689 // Look for a long option match.
690 char* opt = argv[i] + dashes;
693 char* arg = strchr(opt, '=');
694 bool argument_with_equals = arg != NULL;
700 else if (i + 1 < argc)
707 for (j = 0; j < options_size; ++j)
709 if (options[j].long_option != NULL
712 != options::One_option::EXACTLY_TWO_DASHES))
713 && first == options[j].long_option[0]
714 && strcmp(opt, options[j].long_option) == 0)
716 if (options[j].special)
718 // Restore the '=' we clobbered above.
719 if (arg != NULL && skiparg == 0)
721 i += options[j].special(argc - i, argv + i, opt, true, this);
725 if (!options[j].takes_argument())
727 if (argument_with_equals)
728 this->usage(_("unexpected argument"), argv[i]);
735 this->usage(_("missing argument"), argv[i]);
737 this->apply_option(options[j], arg);
743 if (j < options_size)
746 // If we saw two dashes, we needed to have seen a long option.
748 this->usage(_("unknown option"), argv[i]);
750 // Look for a short option match. There may be more than one
751 // short option in a given argument.
753 char* s = argv[i] + 1;
755 while (*s != '\0' && !done)
759 for (j = 0; j < options_size; ++j)
761 if (options[j].short_option == opt)
763 if (options[j].special)
765 // Undo the argument skip done above.
767 i += options[j].special(argc - i, argv + i, s, false,
774 if (options[j].takes_argument())
787 this->usage(_("missing argument"), opt);
789 this->apply_option(options[j], arg);
795 if (j >= options_size)
796 this->usage(_("unknown option"), *s);
805 Command_line::process(int argc, char** argv)
807 bool no_more_options = false;
810 i = process_one_option(argc, argv, i, &no_more_options);
812 if (this->inputs_.in_group())
814 fprintf(stderr, _("%s: missing group end\n"), program_name);
818 // FIXME: We should only do this when configured in native mode.
819 this->options_.add_to_search_path_with_sysroot("/lib");
820 this->options_.add_to_search_path_with_sysroot("/usr/lib");
822 this->options_.add_sysroot();
824 // Ensure options don't contradict each other and are otherwise kosher.
825 this->normalize_options();
828 // Extract an option argument for a special option. LONGNAME is the
829 // long name of the option. This sets *PRET to the return value for
830 // the special function handler to skip to the next option.
833 Command_line::get_special_argument(const char* longname, int argc, char** argv,
834 const char* arg, bool long_option,
839 size_t longlen = strlen(longname);
840 gold_assert(strncmp(arg, longname, longlen) == 0);
849 gold_assert(*arg == '\0');
868 this->usage(_("missing argument"), arg);
871 // Ensure options don't contradict each other and are otherwise kosher.
874 Command_line::normalize_options()
876 // If the user specifies both -s and -r, convert the -s as -S.
877 // -r requires us to keep externally visible symbols!
878 if (this->options_.strip_all() && this->options_.is_relocatable())
880 // Clears the strip_all() status, replacing it with strip_debug().
881 this->options_.set_strip_debug();
884 // FIXME: we can/should be doing a lot more sanity checking here.
888 // Apply a command line option.
891 Command_line::apply_option(const options::One_option& opt,
896 if (opt.general_noarg)
897 (this->options_.*(opt.general_noarg))();
898 else if (opt.dependent_noarg)
899 (this->position_options_.*(opt.dependent_noarg))();
906 (this->options_.*(opt.general_arg))(arg);
907 else if (opt.dependent_arg)
908 (this->position_options_.*(opt.dependent_arg))(arg);
914 // Add an input file or library.
917 Command_line::add_file(const char* name, bool is_lib)
919 Input_file_argument file(name, is_lib, "", this->position_options_);
920 this->inputs_.add_file(file);
923 // Handle the -l option, which requires special treatment.
926 Command_line::process_l_option(int argc, char** argv, char* arg,
930 const char* libname = this->get_special_argument("library", argc, argv, arg,
932 this->add_file(libname, true);
936 // Handle the --start-group option.
939 Command_line::start_group(const char* arg)
941 if (this->inputs_.in_group())
942 this->usage(_("may not nest groups"), arg);
943 this->inputs_.start_group();
946 // Handle the --end-group option.
949 Command_line::end_group(const char* arg)
951 if (!this->inputs_.in_group())
952 this->usage(_("group end without group start"), arg);
953 this->inputs_.end_group();
956 // Report a usage error. */
959 Command_line::usage()
962 _("%s: use the --help option for usage information\n"),
968 Command_line::usage(const char* msg, const char *opt)
972 program_name, opt, msg);
977 Command_line::usage(const char* msg, char opt)
981 program_name, opt, msg);
985 } // End namespace gold.