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.
27 #include "filenames.h"
28 #include "libiberty.h"
35 // The information we keep for a single command line option.
37 struct options::One_option
39 // The single character option name, or '\0' if this is only a long
43 // The long option name, or NULL if this is only a short option.
44 const char* long_option;
46 // Description of the option for --help output, or NULL if there is none.
49 // How to print the option name in --help output, or NULL to use the
51 const char* help_output;
53 // Long option dash control. This is ignored if long_option is
57 // Long option normally takes one dash; two dashes are also
60 // Long option normally takes two dashes; one dash is also
63 // Long option always takes two dashes.
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
72 int (*special)(int argc, char** argv, char *arg, Command_line*);
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)();
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*);
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)();
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*);
90 // Return whether this option takes an argument.
92 takes_argument() const
93 { return this->general_arg != NULL || this->dependent_arg != NULL; }
96 // We have a separate table for -z options.
98 struct options::One_z_option
100 // The name of the option.
103 // The member function in General_options called to record it.
104 void (General_options::*set)();
107 class options::Command_line_options
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;
116 } // End namespace gold.
121 // Handle the special -l option, which adds an input file.
124 library(int argc, char** argv, char* arg, gold::Command_line* cmdline)
126 return cmdline->process_l_option(argc, argv, arg);
129 // Handle the special --start-group option.
132 start_group(int, char**, char* arg, gold::Command_line* cmdline)
134 cmdline->start_group(arg);
138 // Handle the special --end-group option.
141 end_group(int, char**, char* arg, gold::Command_line* cmdline)
143 cmdline->end_group(arg);
147 // Report usage information for ld --help, and exit.
150 help(int, char**, char*, gold::Command_line*)
152 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
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)
159 if (options[i].doc == NULL)
169 if (options[j].help_output != NULL)
176 printf(options[j].help_output);
177 len += std::strlen(options[i].help_output);
182 if (options[j].short_option != '\0')
189 printf("-%c", options[j].short_option);
194 if (options[j].long_option != NULL)
201 if (options[j].dash == gold::options::One_option::ONE_DASH)
211 printf("%s", options[j].long_option);
212 len += std::strlen(options[j].long_option);
218 while (j < options_size && options[j].doc == NULL);
225 for (; len < 30; ++len)
228 std::puts(options[i].doc);
236 // Report version information.
239 version(int, char**, char* opt, gold::Command_line*)
241 gold::print_version(opt[0] == 'v' && opt[1] == '\0');
246 // If the default sysroot is relocatable, try relocating it based on
250 get_relative_sysroot(const char* from)
252 char* path = make_relative_prefix(gold::program_name, from,
257 if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
265 // Return the default sysroot. This is set by the --with-sysroot
266 // option to configure.
269 get_default_sysroot()
271 const char* sysroot = TARGET_SYSTEM_ROOT;
272 if (*sysroot == '\0')
275 if (TARGET_SYSTEM_ROOT_RELOCATABLE)
277 char* path = get_relative_sysroot (BINDIR);
279 path = get_relative_sysroot (TOOLBINDIR);
282 std::string ret = path;
291 } // End anonymous namespace.
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.
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 }
317 // Here is the actual list of options which we accept.
319 const options::One_option
320 options::Command_line_options::options[] =
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"),
331 &Position_dependent_options::set_dynamic_search),
332 POSDEP_NOARG('\0', "Bstatic",
333 N_("-l does not search for shared libraries"),
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,
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('T', "script", N_("Read linker script"),
381 N_("-T FILE, --script FILE"), TWO_DASHES,
382 &General_options::set_script),
383 GENERAL_ARG('\0', "Ttext", N_("Set the address of the .text section"),
384 N_("-Ttext ADDRESS"), ONE_DASH,
385 &General_options::set_text_segment_address),
386 GENERAL_NOARG('\0', "threads", N_("Run the linker multi-threaded"),
387 NULL, TWO_DASHES, &General_options::set_threads),
388 GENERAL_NOARG('\0', "no-threads", N_("Do not run the linker multi-threaded"),
389 NULL, TWO_DASHES, &General_options::clear_threads),
390 GENERAL_ARG('\0', "thread-count", N_("Number of threads to use"),
391 N_("--thread-count COUNT"), TWO_DASHES,
392 &General_options::set_thread_count),
393 GENERAL_ARG('\0', "thread-count-initial",
394 N_("Number of threads to use in initial pass"),
395 N_("--thread-count-initial COUNT"), TWO_DASHES,
396 &General_options::set_thread_count_initial),
397 GENERAL_ARG('\0', "thread-count-middle",
398 N_("Number of threads to use in middle pass"),
399 N_("--thread-count-middle COUNT"), TWO_DASHES,
400 &General_options::set_thread_count_middle),
401 GENERAL_ARG('\0', "thread-count-final",
402 N_("Number of threads to use in final pass"),
403 N_("--thread-count-final COUNT"), TWO_DASHES,
404 &General_options::set_thread_count_final),
405 POSDEP_NOARG('\0', "whole-archive",
406 N_("Include all archive contents"),
408 &Position_dependent_options::set_whole_archive),
409 POSDEP_NOARG('\0', "no-whole-archive",
410 N_("Include only needed archive contents"),
412 &Position_dependent_options::clear_whole_archive),
414 GENERAL_ARG('z', NULL,
415 N_("Subcommands as follows:\n\
416 -z execstack Mark output as requiring executable stack\n\
417 -z noexecstack Mark output as not requiring executable stack"),
418 N_("-z SUBCOMMAND"), ONE_DASH,
419 &General_options::handle_z_option),
421 SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
422 TWO_DASHES, &start_group),
423 SPECIAL(')', "end-group", N_("End a library search group"), NULL,
424 TWO_DASHES, &end_group),
425 SPECIAL('\0', "help", N_("Report usage information"), NULL,
427 SPECIAL('v', "version", N_("Report version information"), NULL,
428 TWO_DASHES, &version)
431 const int options::Command_line_options::options_size =
432 sizeof (options) / sizeof (options[0]);
436 const options::One_z_option
437 options::Command_line_options::z_options[] =
439 { "execstack", &General_options::set_execstack },
440 { "noexecstack", &General_options::set_noexecstack },
443 const int options::Command_line_options::z_options_size =
444 sizeof(z_options) / sizeof(z_options[0]);
446 // The default values for the general options.
448 General_options::General_options()
449 : export_dynamic_(false),
450 dynamic_linker_(NULL),
452 optimization_level_(0),
453 output_file_name_("a.out"),
454 is_relocatable_(false),
457 create_eh_frame_hdr_(false),
464 text_segment_address_(-1U), // -1 indicates value not set by user
466 thread_count_initial_(0),
467 thread_count_middle_(0),
468 thread_count_final_(0),
469 execstack_(EXECSTACK_FROM_INPUT)
473 // The default values for the position dependent options.
475 Position_dependent_options::Position_dependent_options()
476 : do_static_search_(false),
478 include_whole_archive_(false)
482 // Handle the -z option.
485 General_options::handle_z_option(const char* arg)
487 const int z_options_size = options::Command_line_options::z_options_size;
488 const gold::options::One_z_option* z_options =
489 gold::options::Command_line_options::z_options;
490 for (int i = 0; i < z_options_size; ++i)
492 if (strcmp(arg, z_options[i].name) == 0)
494 (this->*(z_options[i].set))();
499 fprintf(stderr, _("%s: unrecognized -z subcommand: %s\n"),
504 // Add the sysroot, if any, to the search paths.
507 General_options::add_sysroot()
509 if (this->sysroot_.empty())
511 this->sysroot_ = get_default_sysroot();
512 if (this->sysroot_.empty())
516 const char* sysroot = this->sysroot_.c_str();
517 char* canonical_sysroot = lrealpath(sysroot);
519 for (Dir_list::iterator p = this->search_path_.begin();
520 p != this->search_path_.end();
522 p->add_sysroot(sysroot, canonical_sysroot);
524 free(canonical_sysroot);
527 // Search_directory methods.
529 // This is called if we have a sysroot. Apply the sysroot if
530 // appropriate. Record whether the directory is in the sysroot.
533 Search_directory::add_sysroot(const char* sysroot,
534 const char* canonical_sysroot)
536 gold_assert(*sysroot != '\0');
537 if (this->put_in_sysroot_)
539 if (!IS_DIR_SEPARATOR(this->name_[0])
540 && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
541 this->name_ = '/' + this->name_;
542 this->name_ = sysroot + this->name_;
543 this->is_in_sysroot_ = true;
547 // Check whether this entry is in the sysroot. To do this
548 // correctly, we need to use canonical names. Otherwise we will
549 // get confused by the ../../.. paths that gcc tends to use.
550 char* canonical_name = lrealpath(this->name_.c_str());
551 int canonical_name_len = strlen(canonical_name);
552 int canonical_sysroot_len = strlen(canonical_sysroot);
553 if (canonical_name_len > canonical_sysroot_len
554 && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
556 canonical_name[canonical_sysroot_len] = '\0';
557 if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
558 this->is_in_sysroot_ = true;
560 free(canonical_name);
564 // Input_arguments methods.
566 // Add a file to the list.
569 Input_arguments::add_file(const Input_file_argument& file)
571 if (!this->in_group_)
572 this->input_argument_list_.push_back(Input_argument(file));
575 gold_assert(!this->input_argument_list_.empty());
576 gold_assert(this->input_argument_list_.back().is_group());
577 this->input_argument_list_.back().group()->add_file(file);
584 Input_arguments::start_group()
586 gold_assert(!this->in_group_);
587 Input_file_group* group = new Input_file_group();
588 this->input_argument_list_.push_back(Input_argument(group));
589 this->in_group_ = true;
595 Input_arguments::end_group()
597 gold_assert(this->in_group_);
598 this->in_group_ = false;
601 // Command_line options.
603 Command_line::Command_line()
604 : options_(), position_options_(), inputs_()
608 // Process the command line options.
611 Command_line::process(int argc, char** argv)
613 const int options_size = options::Command_line_options::options_size;
614 const options::One_option* options =
615 options::Command_line_options::options;
616 bool no_more_options = false;
620 if (argv[i][0] != '-' || no_more_options)
622 this->add_file(argv[i], false);
627 // Option starting with '-'.
629 if (argv[i][1] == '-')
632 if (argv[i][2] == '\0')
634 no_more_options = true;
639 // Look for a long option match.
640 char* opt = argv[i] + dashes;
643 char* arg = strchr(opt, '=');
644 bool argument_with_equals = arg != NULL;
650 else if (i + 1 < argc)
657 for (j = 0; j < options_size; ++j)
659 if (options[j].long_option != NULL
662 != options::One_option::EXACTLY_TWO_DASHES))
663 && first == options[j].long_option[0]
664 && strcmp(opt, options[j].long_option) == 0)
666 if (options[j].special)
667 i += options[j].special(argc - 1, argv + i, opt, this);
670 if (!options[j].takes_argument())
672 if (argument_with_equals)
673 this->usage(_("unexpected argument"), argv[i]);
680 this->usage(_("missing argument"), argv[i]);
682 this->apply_option(options[j], arg);
688 if (j < options_size)
691 // If we saw two dashes, we need to see a long option.
693 this->usage(_("unknown option"), argv[i]);
695 // Look for a short option match. There may be more than one
696 // short option in a given argument.
698 char* s = argv[i] + 1;
700 while (*s != '\0' && !done)
704 for (j = 0; j < options_size; ++j)
706 if (options[j].short_option == opt)
708 if (options[j].special)
710 // Undo the argument skip done above.
712 i += options[j].special(argc - i, argv + i, s, this);
718 if (options[j].takes_argument())
731 this->usage(_("missing argument"), opt);
733 this->apply_option(options[j], arg);
739 if (j >= options_size)
740 this->usage(_("unknown option"), *s);
746 if (this->inputs_.in_group())
748 fprintf(stderr, _("%s: missing group end\n"), program_name);
752 // FIXME: We should only do this when configured in native mode.
753 this->options_.add_to_search_path_with_sysroot("/lib");
754 this->options_.add_to_search_path_with_sysroot("/usr/lib");
756 this->options_.add_sysroot();
758 // Ensure options don't contradict each other and are otherwise kosher.
759 this->normalize_options();
762 // Ensure options don't contradict each other and are otherwise kosher.
765 Command_line::normalize_options()
767 // If the user specifies both -s and -r, convert the -s as -S.
768 // -r requires us to keep externally visible symbols!
769 if (this->options_.strip_all() && this->options_.is_relocatable())
771 // Clears the strip_all() status, replacing it with strip_debug().
772 this->options_.set_strip_debug();
775 // FIXME: we can/should be doing a lot more sanity checking here.
779 // Apply a command line option.
782 Command_line::apply_option(const options::One_option& opt,
787 if (opt.general_noarg)
788 (this->options_.*(opt.general_noarg))();
789 else if (opt.dependent_noarg)
790 (this->position_options_.*(opt.dependent_noarg))();
797 (this->options_.*(opt.general_arg))(arg);
798 else if (opt.dependent_arg)
799 (this->position_options_.*(opt.dependent_arg))(arg);
805 // Add an input file or library.
808 Command_line::add_file(const char* name, bool is_lib)
810 Input_file_argument file(name, is_lib, "", this->position_options_);
811 this->inputs_.add_file(file);
814 // Handle the -l option, which requires special treatment.
817 Command_line::process_l_option(int argc, char** argv, char* arg)
829 libname = argv[argc + 1];
832 this->usage(_("missing argument"), arg);
834 this->add_file(libname, true);
839 // Handle the --start-group option.
842 Command_line::start_group(const char* arg)
844 if (this->inputs_.in_group())
845 this->usage(_("may not nest groups"), arg);
846 this->inputs_.start_group();
849 // Handle the --end-group option.
852 Command_line::end_group(const char* arg)
854 if (!this->inputs_.in_group())
855 this->usage(_("group end without group start"), arg);
856 this->inputs_.end_group();
859 // Report a usage error. */
862 Command_line::usage()
865 _("%s: use the --help option for usage information\n"),
871 Command_line::usage(const char* msg, const char *opt)
875 program_name, opt, msg);
880 Command_line::usage(const char* msg, char opt)
884 program_name, opt, msg);
888 } // End namespace gold.