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('\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"),
405 &Position_dependent_options::set_whole_archive),
406 POSDEP_NOARG('\0', "no-whole-archive",
407 N_("Include only needed archive contents"),
409 &Position_dependent_options::clear_whole_archive),
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),
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,
424 SPECIAL('v', "version", N_("Report version information"), NULL,
425 TWO_DASHES, &version)
428 const int options::Command_line_options::options_size =
429 sizeof (options) / sizeof (options[0]);
433 const options::One_z_option
434 options::Command_line_options::z_options[] =
436 { "execstack", &General_options::set_execstack },
437 { "noexecstack", &General_options::set_noexecstack },
440 const int options::Command_line_options::z_options_size =
441 sizeof(z_options) / sizeof(z_options[0]);
443 // The default values for the general options.
445 General_options::General_options()
446 : export_dynamic_(false),
447 dynamic_linker_(NULL),
449 optimization_level_(0),
450 output_file_name_("a.out"),
451 is_relocatable_(false),
454 create_eh_frame_hdr_(false),
461 text_segment_address_(-1U), // -1 indicates value not set by user
463 thread_count_initial_(0),
464 thread_count_middle_(0),
465 thread_count_final_(0),
466 execstack_(EXECSTACK_FROM_INPUT)
470 // The default values for the position dependent options.
472 Position_dependent_options::Position_dependent_options()
473 : do_static_search_(false),
475 include_whole_archive_(false)
479 // Handle the -z option.
482 General_options::handle_z_option(const char* arg)
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)
489 if (strcmp(arg, z_options[i].name) == 0)
491 (this->*(z_options[i].set))();
496 fprintf(stderr, _("%s: unrecognized -z subcommand: %s\n"),
501 // Add the sysroot, if any, to the search paths.
504 General_options::add_sysroot()
506 if (this->sysroot_.empty())
508 this->sysroot_ = get_default_sysroot();
509 if (this->sysroot_.empty())
513 const char* sysroot = this->sysroot_.c_str();
514 char* canonical_sysroot = lrealpath(sysroot);
516 for (Dir_list::iterator p = this->search_path_.begin();
517 p != this->search_path_.end();
519 p->add_sysroot(sysroot, canonical_sysroot);
521 free(canonical_sysroot);
524 // Search_directory methods.
526 // This is called if we have a sysroot. Apply the sysroot if
527 // appropriate. Record whether the directory is in the sysroot.
530 Search_directory::add_sysroot(const char* sysroot,
531 const char* canonical_sysroot)
533 gold_assert(*sysroot != '\0');
534 if (this->put_in_sysroot_)
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;
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]))
553 canonical_name[canonical_sysroot_len] = '\0';
554 if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
555 this->is_in_sysroot_ = true;
557 free(canonical_name);
561 // Input_arguments methods.
563 // Add a file to the list.
566 Input_arguments::add_file(const Input_file_argument& file)
568 if (!this->in_group_)
569 this->input_argument_list_.push_back(Input_argument(file));
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);
581 Input_arguments::start_group()
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;
592 Input_arguments::end_group()
594 gold_assert(this->in_group_);
595 this->in_group_ = false;
598 // Command_line options.
600 Command_line::Command_line()
601 : options_(), position_options_(), inputs_()
605 // Process the command line options.
608 Command_line::process(int argc, char** argv)
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;
617 if (argv[i][0] != '-' || no_more_options)
619 this->add_file(argv[i], false);
624 // Option starting with '-'.
626 if (argv[i][1] == '-')
629 if (argv[i][2] == '\0')
631 no_more_options = true;
636 // Look for a long option match.
637 char* opt = argv[i] + dashes;
640 char* arg = strchr(opt, '=');
641 bool argument_with_equals = arg != NULL;
647 else if (i + 1 < argc)
654 for (j = 0; j < options_size; ++j)
656 if (options[j].long_option != NULL
659 != options::One_option::EXACTLY_TWO_DASHES))
660 && first == options[j].long_option[0]
661 && strcmp(opt, options[j].long_option) == 0)
663 if (options[j].special)
664 i += options[j].special(argc - 1, argv + i, opt, this);
667 if (!options[j].takes_argument())
669 if (argument_with_equals)
670 this->usage(_("unexpected argument"), argv[i]);
677 this->usage(_("missing argument"), argv[i]);
679 this->apply_option(options[j], arg);
685 if (j < options_size)
688 // If we saw two dashes, we need to see a long option.
690 this->usage(_("unknown option"), argv[i]);
692 // Look for a short option match. There may be more than one
693 // short option in a given argument.
695 char* s = argv[i] + 1;
697 while (*s != '\0' && !done)
701 for (j = 0; j < options_size; ++j)
703 if (options[j].short_option == opt)
705 if (options[j].special)
707 // Undo the argument skip done above.
709 i += options[j].special(argc - i, argv + i, s, this);
715 if (options[j].takes_argument())
728 this->usage(_("missing argument"), opt);
730 this->apply_option(options[j], arg);
736 if (j >= options_size)
737 this->usage(_("unknown option"), *s);
743 if (this->inputs_.in_group())
745 fprintf(stderr, _("%s: missing group end\n"), program_name);
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");
753 this->options_.add_sysroot();
755 // Ensure options don't contradict each other and are otherwise kosher.
756 this->normalize_options();
759 // Ensure options don't contradict each other and are otherwise kosher.
762 Command_line::normalize_options()
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())
768 // Clears the strip_all() status, replacing it with strip_debug().
769 this->options_.set_strip_debug();
772 // FIXME: we can/should be doing a lot more sanity checking here.
776 // Apply a command line option.
779 Command_line::apply_option(const options::One_option& opt,
784 if (opt.general_noarg)
785 (this->options_.*(opt.general_noarg))();
786 else if (opt.dependent_noarg)
787 (this->position_options_.*(opt.dependent_noarg))();
794 (this->options_.*(opt.general_arg))(arg);
795 else if (opt.dependent_arg)
796 (this->position_options_.*(opt.dependent_arg))(arg);
802 // Add an input file or library.
805 Command_line::add_file(const char* name, bool is_lib)
807 Input_file_argument file(name, is_lib, "", this->position_options_);
808 this->inputs_.add_file(file);
811 // Handle the -l option, which requires special treatment.
814 Command_line::process_l_option(int argc, char** argv, char* arg)
826 libname = argv[argc + 1];
829 this->usage(_("missing argument"), arg);
831 this->add_file(libname, true);
836 // Handle the --start-group option.
839 Command_line::start_group(const char* arg)
841 if (this->inputs_.in_group())
842 this->usage(_("may not nest groups"), arg);
843 this->inputs_.start_group();
846 // Handle the --end-group option.
849 Command_line::end_group(const char* arg)
851 if (!this->inputs_.in_group())
852 this->usage(_("group end without group start"), arg);
853 this->inputs_.end_group();
856 // Report a usage error. */
859 Command_line::usage()
862 _("%s: use the --help option for usage information\n"),
868 Command_line::usage(const char* msg, const char *opt)
872 program_name, opt, msg);
877 Command_line::usage(const char* msg, char opt)
881 program_name, opt, msg);
885 } // End namespace gold.