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 class options::Command_line_options
99 static const One_option options[];
100 static const int options_size;
103 } // End namespace gold.
108 // Handle the special -l option, which adds an input file.
111 library(int argc, char** argv, char* arg, gold::Command_line* cmdline)
113 return cmdline->process_l_option(argc, argv, arg);
116 // Handle the special --start-group option.
119 start_group(int, char**, char* arg, gold::Command_line* cmdline)
121 cmdline->start_group(arg);
125 // Handle the special --end-group option.
128 end_group(int, char**, char* arg, gold::Command_line* cmdline)
130 cmdline->end_group(arg);
134 // Report usage information for ld --help, and exit.
137 help(int, char**, char*, gold::Command_line*)
139 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
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)
146 if (options[i].doc == NULL)
156 if (options[j].help_output != NULL)
163 printf(options[j].help_output);
164 len += std::strlen(options[i].help_output);
169 if (options[j].short_option != '\0')
176 printf("-%c", options[j].short_option);
181 if (options[j].long_option != NULL)
188 if (options[j].dash == gold::options::One_option::ONE_DASH)
198 printf("%s", options[j].long_option);
199 len += std::strlen(options[j].long_option);
205 while (j < options_size && options[j].doc == NULL);
212 for (; len < 30; ++len)
215 std::puts(options[i].doc);
223 // Report version information.
226 version(int, char**, char* opt, gold::Command_line*)
228 gold::print_version(opt[0] == 'v' && opt[1] == '\0');
233 // If the default sysroot is relocatable, try relocating it based on
237 get_relative_sysroot(const char* from)
239 char* path = make_relative_prefix(gold::program_name, from,
244 if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
252 // Return the default sysroot. This is set by the --with-sysroot
253 // option to configure.
256 get_default_sysroot()
258 const char* sysroot = TARGET_SYSTEM_ROOT;
259 if (*sysroot == '\0')
262 if (TARGET_SYSTEM_ROOT_RELOCATABLE)
264 char* path = get_relative_sysroot (BINDIR);
266 path = get_relative_sysroot (TOOLBINDIR);
269 std::string ret = path;
278 } // End anonymous namespace.
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.
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 }
304 // Here is the actual list of options which we accept.
306 const options::One_option
307 options::Command_line_options::options[] =
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),
315 GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
316 NULL, ONE_DASH, &General_options::set_symbolic),
317 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
318 NULL, TWO_DASHES, &General_options::set_export_dynamic),
319 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
320 NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr),
321 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
322 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES,
323 &General_options::set_dynamic_linker),
324 SPECIAL('l', "library", N_("Search for library LIBNAME"),
325 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
327 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
328 N_("-L DIR, --library-path DIR"), TWO_DASHES,
329 &General_options::add_to_search_path),
330 GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
331 &General_options::ignore),
332 GENERAL_ARG('o', "output", N_("Set output file name"),
333 N_("-o FILE, --output FILE"), TWO_DASHES,
334 &General_options::set_output_file_name),
335 GENERAL_ARG('O', NULL, N_("Optimize output file size"),
336 N_("-O level"), ONE_DASH,
337 &General_options::set_optimization_level),
338 GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
339 ONE_DASH, &General_options::set_relocatable),
340 GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
341 N_("-R DIR, -rpath DIR"), ONE_DASH,
342 &General_options::add_to_rpath),
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('s', "strip-all", N_("Strip all symbols"), NULL,
348 TWO_DASHES, &General_options::set_strip_all),
349 GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL,
350 TWO_DASHES, &General_options::set_strip_debug),
351 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
352 NULL, ONE_DASH, &General_options::set_shared),
353 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
354 NULL, ONE_DASH, &General_options::set_static),
355 GENERAL_NOARG('\0', "stats", N_("Print resource usage statistics"),
356 NULL, TWO_DASHES, &General_options::set_stats),
357 GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
358 N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot),
359 GENERAL_ARG('\0', "Ttext", N_("Set the address of the .text section"),
360 N_("-Ttext ADDRESS"), ONE_DASH,
361 &General_options::set_text_segment_address),
362 GENERAL_NOARG('\0', "threads", N_("Run the linker multi-threaded"),
363 NULL, TWO_DASHES, &General_options::set_threads),
364 GENERAL_NOARG('\0', "no-threads", N_("Do not run the linker multi-threaded"),
365 NULL, TWO_DASHES, &General_options::clear_threads),
366 GENERAL_ARG('\0', "thread-count", N_("Number of threads to use"),
367 N_("--thread-count COUNT"), TWO_DASHES,
368 &General_options::set_thread_count),
369 GENERAL_ARG('\0', "thread-count-initial",
370 N_("Number of threads to use in initial pass"),
371 N_("--thread-count-initial COUNT"), TWO_DASHES,
372 &General_options::set_thread_count_initial),
373 GENERAL_ARG('\0', "thread-count-middle",
374 N_("Number of threads to use in middle pass"),
375 N_("--thread-count-middle COUNT"), TWO_DASHES,
376 &General_options::set_thread_count_middle),
377 GENERAL_ARG('\0', "thread-count-final",
378 N_("Number of threads to use in final pass"),
379 N_("--thread-count-final COUNT"), TWO_DASHES,
380 &General_options::set_thread_count_final),
381 POSDEP_NOARG('\0', "whole-archive",
382 N_("Include all archive contents"),
384 &Position_dependent_options::set_whole_archive),
385 POSDEP_NOARG('\0', "no-whole-archive",
386 N_("Include only needed archive contents"),
388 &Position_dependent_options::clear_whole_archive),
389 SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
390 TWO_DASHES, &start_group),
391 SPECIAL(')', "end-group", N_("End a library search group"), NULL,
392 TWO_DASHES, &end_group),
393 SPECIAL('\0', "help", N_("Report usage information"), NULL,
395 SPECIAL('v', "version", N_("Report version information"), NULL,
396 TWO_DASHES, &version)
399 const int options::Command_line_options::options_size =
400 sizeof (options) / sizeof (options[0]);
402 // The default values for the general options.
404 General_options::General_options()
405 : export_dynamic_(false),
406 dynamic_linker_(NULL),
408 optimization_level_(0),
409 output_file_name_("a.out"),
410 is_relocatable_(false),
413 create_eh_frame_hdr_(false),
420 text_segment_address_(-1U), // -1 indicates value not set by user
422 thread_count_initial_(0),
423 thread_count_middle_(0),
424 thread_count_final_(0)
428 // The default values for the position dependent options.
430 Position_dependent_options::Position_dependent_options()
431 : do_static_search_(false),
433 include_whole_archive_(false)
437 // Add the sysroot, if any, to the search paths.
440 General_options::add_sysroot()
442 if (this->sysroot_.empty())
444 this->sysroot_ = get_default_sysroot();
445 if (this->sysroot_.empty())
449 const char* sysroot = this->sysroot_.c_str();
450 char* canonical_sysroot = lrealpath(sysroot);
452 for (Dir_list::iterator p = this->search_path_.begin();
453 p != this->search_path_.end();
455 p->add_sysroot(sysroot, canonical_sysroot);
457 free(canonical_sysroot);
460 // Search_directory methods.
462 // This is called if we have a sysroot. Apply the sysroot if
463 // appropriate. Record whether the directory is in the sysroot.
466 Search_directory::add_sysroot(const char* sysroot,
467 const char* canonical_sysroot)
469 gold_assert(*sysroot != '\0');
470 if (this->put_in_sysroot_)
472 if (!IS_DIR_SEPARATOR(this->name_[0])
473 && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
474 this->name_ = '/' + this->name_;
475 this->name_ = sysroot + this->name_;
476 this->is_in_sysroot_ = true;
480 // Check whether this entry is in the sysroot. To do this
481 // correctly, we need to use canonical names. Otherwise we will
482 // get confused by the ../../.. paths that gcc tends to use.
483 char* canonical_name = lrealpath(this->name_.c_str());
484 int canonical_name_len = strlen(canonical_name);
485 int canonical_sysroot_len = strlen(canonical_sysroot);
486 if (canonical_name_len > canonical_sysroot_len
487 && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
489 canonical_name[canonical_sysroot_len] = '\0';
490 if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
491 this->is_in_sysroot_ = true;
493 free(canonical_name);
497 // Input_arguments methods.
499 // Add a file to the list.
502 Input_arguments::add_file(const Input_file_argument& file)
504 if (!this->in_group_)
505 this->input_argument_list_.push_back(Input_argument(file));
508 gold_assert(!this->input_argument_list_.empty());
509 gold_assert(this->input_argument_list_.back().is_group());
510 this->input_argument_list_.back().group()->add_file(file);
517 Input_arguments::start_group()
519 gold_assert(!this->in_group_);
520 Input_file_group* group = new Input_file_group();
521 this->input_argument_list_.push_back(Input_argument(group));
522 this->in_group_ = true;
528 Input_arguments::end_group()
530 gold_assert(this->in_group_);
531 this->in_group_ = false;
534 // Command_line options.
536 Command_line::Command_line()
537 : options_(), position_options_(), inputs_()
541 // Process the command line options.
544 Command_line::process(int argc, char** argv)
546 const int options_size = options::Command_line_options::options_size;
547 const options::One_option* options =
548 options::Command_line_options::options;
549 bool no_more_options = false;
553 if (argv[i][0] != '-' || no_more_options)
555 this->add_file(argv[i], false);
560 // Option starting with '-'.
562 if (argv[i][1] == '-')
565 if (argv[i][2] == '\0')
567 no_more_options = true;
572 // Look for a long option match.
573 char* opt = argv[i] + dashes;
576 char* arg = strchr(opt, '=');
577 bool argument_with_equals = arg != NULL;
583 else if (i + 1 < argc)
590 for (j = 0; j < options_size; ++j)
592 if (options[j].long_option != NULL
595 != options::One_option::EXACTLY_TWO_DASHES))
596 && first == options[j].long_option[0]
597 && strcmp(opt, options[j].long_option) == 0)
599 if (options[j].special)
600 i += options[j].special(argc - 1, argv + i, opt, this);
603 if (!options[j].takes_argument())
605 if (argument_with_equals)
606 this->usage(_("unexpected argument"), argv[i]);
613 this->usage(_("missing argument"), argv[i]);
615 this->apply_option(options[j], arg);
621 if (j < options_size)
624 // If we saw two dashes, we need to see a long option.
626 this->usage(_("unknown option"), argv[i]);
628 // Look for a short option match. There may be more than one
629 // short option in a given argument.
631 char* s = argv[i] + 1;
633 while (*s != '\0' && !done)
637 for (j = 0; j < options_size; ++j)
639 if (options[j].short_option == opt)
641 if (options[j].special)
643 // Undo the argument skip done above.
645 i += options[j].special(argc - i, argv + i, s, this);
651 if (options[j].takes_argument())
664 this->usage(_("missing argument"), opt);
666 this->apply_option(options[j], arg);
672 if (j >= options_size)
673 this->usage(_("unknown option"), *s);
679 if (this->inputs_.in_group())
681 fprintf(stderr, _("%s: missing group end"), program_name);
685 // FIXME: We should only do this when configured in native mode.
686 this->options_.add_to_search_path_with_sysroot("/lib");
687 this->options_.add_to_search_path_with_sysroot("/usr/lib");
689 this->options_.add_sysroot();
691 // Ensure options don't contradict each other and are otherwise kosher.
692 this->normalize_options();
695 // Ensure options don't contradict each other and are otherwise kosher.
698 Command_line::normalize_options()
700 // If the user specifies both -s and -r, convert the -s as -S.
701 // -r requires us to keep externally visible symbols!
702 if (this->options_.strip_all() && this->options_.is_relocatable())
704 // Clears the strip_all() status, replacing it with strip_debug().
705 this->options_.set_strip_debug();
708 // FIXME: we can/should be doing a lot more sanity checking here.
712 // Apply a command line option.
715 Command_line::apply_option(const options::One_option& opt,
720 if (opt.general_noarg)
721 (this->options_.*(opt.general_noarg))();
722 else if (opt.dependent_noarg)
723 (this->position_options_.*(opt.dependent_noarg))();
730 (this->options_.*(opt.general_arg))(arg);
731 else if (opt.dependent_arg)
732 (this->position_options_.*(opt.dependent_arg))(arg);
738 // Add an input file or library.
741 Command_line::add_file(const char* name, bool is_lib)
743 Input_file_argument file(name, is_lib, "", this->position_options_);
744 this->inputs_.add_file(file);
747 // Handle the -l option, which requires special treatment.
750 Command_line::process_l_option(int argc, char** argv, char* arg)
762 libname = argv[argc + 1];
765 this->usage(_("missing argument"), arg);
767 this->add_file(libname, true);
772 // Handle the --start-group option.
775 Command_line::start_group(const char* arg)
777 if (this->inputs_.in_group())
778 this->usage(_("may not nest groups"), arg);
779 this->inputs_.start_group();
782 // Handle the --end-group option.
785 Command_line::end_group(const char* arg)
787 if (!this->inputs_.in_group())
788 this->usage(_("group end without group start"), arg);
789 this->inputs_.end_group();
792 // Report a usage error. */
795 Command_line::usage()
798 _("%s: use the --help option for usage information\n"),
804 Command_line::usage(const char* msg, const char *opt)
808 program_name, opt, msg);
813 Command_line::usage(const char* msg, char opt)
817 program_name, opt, msg);
821 } // End namespace gold.