* elf32-ppc.c (ppc_elf_check_relocs): Set pointer_equality_needed
[binutils.git] / gold / options.cc
blob4d8f0b9f68f8b607768cc97c9e2a6c5fe0daa9ee
1 // options.c -- handle command line options for gold
3 // Copyright 2006, 2007, 2008 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.
23 #include "gold.h"
25 #include <cstdlib>
26 #include <iostream>
27 #include <sys/stat.h>
28 #include "filenames.h"
29 #include "libiberty.h"
31 #include "debug.h"
32 #include "script.h"
33 #include "target-select.h"
34 #include "options.h"
36 namespace gold
39 // The information we keep for a single command line option.
41 struct options::One_option
43 // The single character option name, or '\0' if this is only a long
44 // option.
45 char short_option;
47 // The long option name, or NULL if this is only a short option.
48 const char* long_option;
50 // Description of the option for --help output, or NULL if there is none.
51 const char* doc;
53 // How to print the option name in --help output, or NULL to use the
54 // default.
55 const char* help_output;
57 // Long option dash control. This is ignored if long_option is
58 // NULL.
59 enum
61 // Long option normally takes one dash; two dashes are also
62 // accepted.
63 ONE_DASH,
64 // Long option normally takes two dashes; one dash is also
65 // accepted.
66 TWO_DASHES,
67 // Long option always takes two dashes.
68 EXACTLY_TWO_DASHES
69 } dash;
71 // Function for special handling, or NULL. Returns the number of
72 // arguments to skip. This will normally be at least 1, but it may
73 // be 0 if this function changes *argv. ARG points to the location
74 // in *ARGV where the option starts, which may be helpful for a
75 // short option.
76 int (*special)(int argc, char** argv, char *arg, bool long_option,
77 Command_line*);
79 // If this is a position independent option which does not take an
80 // argument, this is the member function to call to record it.
81 void (General_options::*general_noarg)();
83 // If this is a position independent function which takes an
84 // argument, this is the member function to call to record it.
85 void (General_options::*general_arg)(const char*);
87 // If this is a position dependent option which does not take an
88 // argument, this is the member function to call to record it.
89 void (Position_dependent_options::*dependent_noarg)();
91 // If this is a position dependent option which takes an argument,
92 // this is the member function to record it.
93 void (Position_dependent_options::*dependent_arg)(const char*);
95 // Return whether this option takes an argument.
96 bool
97 takes_argument() const
98 { return this->general_arg != NULL || this->dependent_arg != NULL; }
101 // We have a separate table for -z options.
103 struct options::One_z_option
105 // The name of the option.
106 const char* name;
108 // The member function in General_options called to record it.
109 void (General_options::*set)();
112 // We have a separate table for --debug options.
114 struct options::One_debug_option
116 // The name of the option.
117 const char* name;
119 // The flags to turn on.
120 unsigned int debug_flags;
123 class options::Command_line_options
125 public:
126 static const One_option options[];
127 static const int options_size;
128 static const One_z_option z_options[];
129 static const int z_options_size;
130 static const One_debug_option debug_options[];
131 static const int debug_options_size;
134 } // End namespace gold.
136 namespace
139 // Recognize input and output target names. The GNU linker accepts
140 // these with --format and --oformat. This code is intended to be
141 // minimally compatible. In practice for an ELF target this would be
142 // the same target as the input files; that name always start with
143 // "elf". Non-ELF targets would be "srec", "symbolsrec", "tekhex",
144 // "binary", "ihex". See also
145 // General_options::default_target_settings.
147 gold::General_options::Object_format
148 string_to_object_format(const char* arg)
150 if (strncmp(arg, "elf", 3) == 0)
151 return gold::General_options::OBJECT_FORMAT_ELF;
152 else if (strcmp(arg, "binary") == 0)
153 return gold::General_options::OBJECT_FORMAT_BINARY;
154 else
156 gold::gold_error(_("format '%s' not supported "
157 "(supported formats: elf, binary)"),
158 arg);
159 return gold::General_options::OBJECT_FORMAT_ELF;
163 // Handle the special -l option, which adds an input file.
166 library(int argc, char** argv, char* arg, bool long_option,
167 gold::Command_line* cmdline)
169 return cmdline->process_l_option(argc, argv, arg, long_option);
172 // Handle the -R option. Historically the GNU linker made -R a
173 // synonym for --just-symbols. ELF linkers have traditionally made -R
174 // a synonym for -rpath. When ELF support was added to the GNU
175 // linker, -R was changed to switch based on the argument: if the
176 // argument is an ordinary file, we treat it as --just-symbols,
177 // otherwise we treat it as -rpath. We need to be compatible with
178 // this, because existing build scripts rely on it.
181 handle_r_option(int argc, char** argv, char* arg, bool long_option,
182 gold::Command_line* cmdline)
184 int ret;
185 const char* val = cmdline->get_special_argument("R", argc, argv, arg,
186 long_option, &ret);
187 struct stat s;
188 if (::stat(val, &s) != 0 || S_ISDIR(s.st_mode))
189 cmdline->add_to_rpath(val);
190 else
191 cmdline->add_just_symbols_file(val);
192 return ret;
195 // Handle the --just-symbols option.
198 handle_just_symbols_option(int argc, char** argv, char* arg,
199 bool long_option, gold::Command_line* cmdline)
201 int ret;
202 const char* val = cmdline->get_special_argument("just-symbols", argc, argv,
203 arg, long_option, &ret);
204 cmdline->add_just_symbols_file(val);
205 return ret;
208 // Handle the special -T/--script option, which reads a linker script.
211 invoke_script(int argc, char** argv, char* arg, bool long_option,
212 gold::Command_line* cmdline)
214 int ret;
215 const char* script_name = cmdline->get_special_argument("script", argc, argv,
216 arg, long_option,
217 &ret);
218 if (!read_commandline_script(script_name, cmdline))
219 gold::gold_fatal(_("unable to parse script file %s"), script_name);
220 return ret;
223 // Handle the special --version-script option, which reads a version script.
226 invoke_version_script(int argc, char** argv, char* arg, bool long_option,
227 gold::Command_line* cmdline)
229 int ret;
230 const char* script_name = cmdline->get_special_argument("version-script",
231 argc, argv,
232 arg, long_option,
233 &ret);
234 if (!read_version_script(script_name, cmdline))
235 gold::gold_fatal(_("unable to parse version script file %s"), script_name);
236 return ret;
239 // Handle the special --start-group option.
242 start_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
244 cmdline->start_group(arg);
245 return 1;
248 // Handle the special --end-group option.
251 end_group(int, char**, char* arg, bool, gold::Command_line* cmdline)
253 cmdline->end_group(arg);
254 return 1;
257 // Report usage information for ld --help, and exit.
260 help(int, char**, char*, bool, gold::Command_line*)
262 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
264 const int options_size = gold::options::Command_line_options::options_size;
265 const gold::options::One_option* options =
266 gold::options::Command_line_options::options;
267 for (int i = 0; i < options_size; ++i)
269 if (options[i].doc == NULL)
270 continue;
272 printf(" ");
273 int len = 2;
274 bool comma = false;
276 int j = i;
279 if (options[j].help_output != NULL)
281 if (comma)
283 printf(", ");
284 len += 2;
286 printf(options[j].help_output);
287 len += std::strlen(options[j].help_output);
288 comma = true;
290 else
292 if (options[j].short_option != '\0')
294 if (comma)
296 printf(", ");
297 len += 2;
299 printf("-%c", options[j].short_option);
300 len += 2;
301 comma = true;
304 if (options[j].long_option != NULL)
306 if (comma)
308 printf(", ");
309 len += 2;
311 if (options[j].dash == gold::options::One_option::ONE_DASH)
313 printf("-");
314 ++len;
316 else
318 printf("--");
319 len += 2;
321 printf("%s", options[j].long_option);
322 len += std::strlen(options[j].long_option);
323 comma = true;
326 ++j;
328 while (j < options_size && options[j].doc == NULL);
330 if (len >= 30)
332 printf("\n");
333 len = 0;
335 for (; len < 30; ++len)
336 std::putchar(' ');
338 std::puts(options[i].doc);
341 ::exit(EXIT_SUCCESS);
343 return 0;
346 // Report version information.
349 version(int, char**, char* opt, bool, gold::Command_line*)
351 gold::print_version(opt[0] == 'v' && opt[1] == '\0');
352 ::exit(EXIT_SUCCESS);
353 return 0;
356 // If the default sysroot is relocatable, try relocating it based on
357 // the prefix FROM.
359 char*
360 get_relative_sysroot(const char* from)
362 char* path = make_relative_prefix(gold::program_name, from,
363 TARGET_SYSTEM_ROOT);
364 if (path != NULL)
366 struct stat s;
367 if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
368 return path;
369 free(path);
372 return NULL;
375 // Return the default sysroot. This is set by the --with-sysroot
376 // option to configure.
378 std::string
379 get_default_sysroot()
381 const char* sysroot = TARGET_SYSTEM_ROOT;
382 if (*sysroot == '\0')
383 return "";
385 if (TARGET_SYSTEM_ROOT_RELOCATABLE)
387 char* path = get_relative_sysroot (BINDIR);
388 if (path == NULL)
389 path = get_relative_sysroot (TOOLBINDIR);
390 if (path != NULL)
392 std::string ret = path;
393 free(path);
394 return ret;
398 return sysroot;
401 } // End anonymous namespace.
403 namespace gold
406 // Helper macros used to specify the options. We could also do this
407 // using constructors, but then g++ would generate code to initialize
408 // the array. We want the array to be initialized statically so that
409 // we get better startup time.
411 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
412 { short_option, long_option, doc, help, options::One_option::dash, \
413 NULL, func, NULL, NULL, NULL }
414 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
415 { short_option, long_option, doc, help, options::One_option::dash, \
416 NULL, NULL, func, NULL, NULL }
417 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
418 { short_option, long_option, doc, help, options::One_option::dash, \
419 NULL, NULL, NULL, func, NULL }
420 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
421 { short_option, long_option, doc, help, options::One_option::dash, \
422 NULL, NULL, NULL, NULL, func }
423 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
424 { short_option, long_option, doc, help, options::One_option::dash, \
425 func, NULL, NULL, NULL, NULL }
427 // Here is the actual list of options which we accept.
429 const options::One_option
430 options::Command_line_options::options[] =
432 GENERAL_NOARG('\0', "allow-shlib-undefined",
433 N_("Allow unresolved references in shared libraries"),
434 NULL, TWO_DASHES,
435 &General_options::set_allow_shlib_undefined),
436 GENERAL_NOARG('\0', "no-allow-shlib-undefined",
437 N_("Do not allow unresolved references in shared libraries"),
438 NULL, TWO_DASHES,
439 &General_options::set_no_allow_shlib_undefined),
440 POSDEP_NOARG('\0', "as-needed",
441 N_("Only set DT_NEEDED for dynamic libs if used"),
442 NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
443 POSDEP_NOARG('\0', "no-as-needed",
444 N_("Always DT_NEEDED for dynamic libs (default)"),
445 NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed),
446 POSDEP_NOARG('\0', "Bdynamic",
447 N_("-l searches for shared libraries"),
448 NULL, ONE_DASH,
449 &Position_dependent_options::set_dynamic_search),
450 POSDEP_NOARG('\0', "Bstatic",
451 N_("-l does not search for shared libraries"),
452 NULL, ONE_DASH,
453 &Position_dependent_options::set_static_search),
454 GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
455 NULL, ONE_DASH, &General_options::set_symbolic),
456 POSDEP_ARG('b', "format", N_("Set input format (elf, binary)"),
457 N_("-b FORMAT, --format FORMAT"), TWO_DASHES,
458 &Position_dependent_options::set_input_format),
459 #ifdef HAVE_ZLIB_H
460 # define ZLIB_STR ",zlib"
461 #else
462 # define ZLIB_STR ""
463 #endif
464 GENERAL_ARG('\0', "compress-debug-sections",
465 N_("Compress .debug_* sections in the output file "
466 "(default is none)"),
467 N_("--compress-debug-sections=[none" ZLIB_STR "]"),
468 TWO_DASHES,
469 &General_options::set_compress_debug_sections),
470 GENERAL_ARG('\0', "defsym", N_("Define a symbol"),
471 N_("--defsym SYMBOL=EXPRESSION"), TWO_DASHES,
472 &General_options::define_symbol),
473 GENERAL_NOARG('\0', "demangle", N_("Demangle C++ symbols in log messages"),
474 NULL, TWO_DASHES, &General_options::set_demangle),
475 GENERAL_NOARG('\0', "no-demangle",
476 N_("Do not demangle C++ symbols in log messages"),
477 NULL, TWO_DASHES, &General_options::clear_demangle),
478 GENERAL_NOARG('\0', "detect-odr-violations",
479 N_("Try to detect violations of the One Definition Rule"),
480 NULL, TWO_DASHES, &General_options::set_detect_odr_violations),
481 GENERAL_ARG('e', "entry", N_("Set program start address"),
482 N_("-e ADDRESS, --entry ADDRESS"), TWO_DASHES,
483 &General_options::set_entry),
484 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
485 NULL, TWO_DASHES, &General_options::set_export_dynamic),
486 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
487 NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr),
488 GENERAL_ARG('h', "soname", N_("Set shared library name"),
489 N_("-h FILENAME, -soname FILENAME"), ONE_DASH,
490 &General_options::set_soname),
491 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
492 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES,
493 &General_options::set_dynamic_linker),
494 SPECIAL('l', "library", N_("Search for library LIBNAME"),
495 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
496 &library),
497 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
498 N_("-L DIR, --library-path DIR"), TWO_DASHES,
499 &General_options::add_to_search_path),
500 GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
501 &General_options::ignore),
502 GENERAL_ARG('o', "output", N_("Set output file name"),
503 N_("-o FILE, --output FILE"), TWO_DASHES,
504 &General_options::set_output_file_name),
505 GENERAL_ARG('O', NULL, N_("Optimize output file size"),
506 N_("-O level"), ONE_DASH,
507 &General_options::set_optimization_level),
508 GENERAL_ARG('\0', "oformat", N_("Set output format (only binary supported)"),
509 N_("--oformat FORMAT"), EXACTLY_TWO_DASHES,
510 &General_options::set_output_format),
511 GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
512 ONE_DASH, &General_options::set_relocatable),
513 // -R really means -rpath, but can mean --just-symbols for
514 // compatibility with GNU ld. -rpath is always -rpath, so we list
515 // it separately.
516 SPECIAL('R', NULL, N_("Add DIR to runtime search path"),
517 N_("-R DIR"), ONE_DASH, &handle_r_option),
518 GENERAL_ARG('\0', "rpath", NULL, N_("-rpath DIR"), ONE_DASH,
519 &General_options::add_to_rpath),
520 SPECIAL('\0', "just-symbols", N_("Read only symbol values from file"),
521 N_("-R FILE, --just-symbols FILE"), TWO_DASHES,
522 &handle_just_symbols_option),
523 GENERAL_ARG('\0', "rpath-link",
524 N_("Add DIR to link time shared library search path"),
525 N_("--rpath-link DIR"), TWO_DASHES,
526 &General_options::add_to_rpath_link),
527 GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL,
528 TWO_DASHES, &General_options::set_strip_all),
529 GENERAL_NOARG('\0', "strip-debug-gdb",
530 N_("Strip debug symbols that are unused by gdb "
531 "(at least versions <= 6.7)"),
532 NULL, TWO_DASHES, &General_options::set_strip_debug_gdb),
533 // This must come after -Sdebug since it's a prefix of it.
534 GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL,
535 TWO_DASHES, &General_options::set_strip_debug),
536 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
537 NULL, ONE_DASH, &General_options::set_shared),
538 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
539 NULL, ONE_DASH, &General_options::set_static),
540 GENERAL_NOARG('\0', "stats", N_("Print resource usage statistics"),
541 NULL, TWO_DASHES, &General_options::set_stats),
542 GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
543 N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot),
544 GENERAL_ARG('\0', "Tbss", N_("Set the address of the bss segment"),
545 N_("-Tbss ADDRESS"), ONE_DASH,
546 &General_options::set_bss_segment_address),
547 GENERAL_ARG('\0', "Tdata", N_("Set the address of the data segment"),
548 N_("-Tdata ADDRESS"), ONE_DASH,
549 &General_options::set_data_segment_address),
550 GENERAL_ARG('\0', "Ttext", N_("Set the address of the text segment"),
551 N_("-Ttext ADDRESS"), ONE_DASH,
552 &General_options::set_text_segment_address),
553 // This must come after -Ttext and friends since it's a prefix of
554 // them.
555 SPECIAL('T', "script", N_("Read linker script"),
556 N_("-T FILE, --script FILE"), TWO_DASHES,
557 &invoke_script),
558 SPECIAL('\0', "version-script", N_("Read version script"),
559 N_("--version-script FILE"), TWO_DASHES,
560 &invoke_version_script),
561 GENERAL_NOARG('\0', "threads", N_("Run the linker multi-threaded"),
562 NULL, TWO_DASHES, &General_options::set_threads),
563 GENERAL_NOARG('\0', "no-threads", N_("Do not run the linker multi-threaded"),
564 NULL, TWO_DASHES, &General_options::clear_threads),
565 GENERAL_ARG('\0', "thread-count", N_("Number of threads to use"),
566 N_("--thread-count COUNT"), TWO_DASHES,
567 &General_options::set_thread_count),
568 GENERAL_ARG('\0', "thread-count-initial",
569 N_("Number of threads to use in initial pass"),
570 N_("--thread-count-initial COUNT"), TWO_DASHES,
571 &General_options::set_thread_count_initial),
572 GENERAL_ARG('\0', "thread-count-middle",
573 N_("Number of threads to use in middle pass"),
574 N_("--thread-count-middle COUNT"), TWO_DASHES,
575 &General_options::set_thread_count_middle),
576 GENERAL_ARG('\0', "thread-count-final",
577 N_("Number of threads to use in final pass"),
578 N_("--thread-count-final COUNT"), TWO_DASHES,
579 &General_options::set_thread_count_final),
580 POSDEP_NOARG('\0', "whole-archive",
581 N_("Include all archive contents"),
582 NULL, TWO_DASHES,
583 &Position_dependent_options::set_whole_archive),
584 POSDEP_NOARG('\0', "no-whole-archive",
585 N_("Include only needed archive contents"),
586 NULL, TWO_DASHES,
587 &Position_dependent_options::clear_whole_archive),
589 GENERAL_ARG('z', NULL,
590 N_("Subcommands as follows:\n\
591 -z execstack Mark output as requiring executable stack\n\
592 -z noexecstack Mark output as not requiring executable stack"),
593 N_("-z SUBCOMMAND"), ONE_DASH,
594 &General_options::handle_z_option),
596 SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
597 TWO_DASHES, &start_group),
598 SPECIAL(')', "end-group", N_("End a library search group"), NULL,
599 TWO_DASHES, &end_group),
600 SPECIAL('\0', "help", N_("Report usage information"), NULL,
601 TWO_DASHES, &help),
602 SPECIAL('v', "version", N_("Report version information"), NULL,
603 TWO_DASHES, &version),
604 GENERAL_ARG('\0', "debug", N_("Turn on debugging (all,task,script)"),
605 N_("--debug=TYPE"), TWO_DASHES,
606 &General_options::handle_debug_option)
609 const int options::Command_line_options::options_size =
610 sizeof (options) / sizeof (options[0]);
612 // The -z options.
614 const options::One_z_option
615 options::Command_line_options::z_options[] =
617 { "execstack", &General_options::set_execstack },
618 { "noexecstack", &General_options::set_noexecstack },
621 const int options::Command_line_options::z_options_size =
622 sizeof(z_options) / sizeof(z_options[0]);
624 // The --debug options.
626 const options::One_debug_option
627 options::Command_line_options::debug_options[] =
629 { "all", DEBUG_ALL },
630 { "task", DEBUG_TASK },
631 { "script", DEBUG_SCRIPT }
634 const int options::Command_line_options::debug_options_size =
635 sizeof(debug_options) / sizeof(debug_options[0]);
637 // The default values for the general options.
639 General_options::General_options(Script_options* script_options)
640 : export_dynamic_(false),
641 soname_(NULL),
642 dynamic_linker_(NULL),
643 search_path_(),
644 optimization_level_(0),
645 output_file_name_("a.out"),
646 output_format_(OBJECT_FORMAT_ELF),
647 output_format_string_(NULL),
648 is_relocatable_(false),
649 strip_(STRIP_NONE),
650 allow_shlib_undefined_(false),
651 symbolic_(false),
652 compress_debug_sections_(NO_COMPRESSION),
653 detect_odr_violations_(false),
654 create_eh_frame_hdr_(false),
655 rpath_(),
656 rpath_link_(),
657 is_shared_(false),
658 is_static_(false),
659 print_stats_(false),
660 sysroot_(),
661 bss_segment_address_(-1U), // -1 indicates value not set by user
662 data_segment_address_(-1U),
663 text_segment_address_(-1U),
664 threads_(false),
665 thread_count_initial_(0),
666 thread_count_middle_(0),
667 thread_count_final_(0),
668 execstack_(EXECSTACK_FROM_INPUT),
669 debug_(0),
670 script_options_(script_options)
672 // We initialize demangle_ based on the environment variable
673 // COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
674 // output of the linker, unless COLLECT_NO_DEMANGLE is set in the
675 // environment. Acting the same way here lets us provide the same
676 // interface by default.
677 this->demangle_ = getenv("COLLECT_NO_DEMANGLE") == NULL;
680 // Handle the --defsym option.
682 void
683 General_options::define_symbol(const char* arg)
685 this->script_options_->define_symbol(arg);
688 // Handle the --oformat option.
690 void
691 General_options::set_output_format(const char* arg)
693 this->output_format_string_ = arg;
694 this->output_format_ = string_to_object_format(arg);
697 // The x86_64 kernel build converts a binary file to an object file
698 // using -r --format binary --oformat elf32-i386 foo.o. In order to
699 // support that for gold we support determining the default target
700 // choice from the output format. We recognize names that the GNU
701 // linker uses.
703 Target*
704 General_options::default_target() const
706 if (this->output_format_string_ != NULL)
708 Target* target = select_target_by_name(this->output_format_string_);
709 if (target != NULL)
710 return target;
712 gold_error(_("unrecognized output format %s"),
713 this->output_format_string_);
716 // The GOLD_DEFAULT_xx macros are defined by the configure script.
717 Target* target = select_target(elfcpp::GOLD_DEFAULT_MACHINE,
718 GOLD_DEFAULT_SIZE,
719 GOLD_DEFAULT_BIG_ENDIAN,
720 0, 0);
721 gold_assert(target != NULL);
722 return target;
725 // Handle the -z option.
727 void
728 General_options::handle_z_option(const char* arg)
730 const int z_options_size = options::Command_line_options::z_options_size;
731 const gold::options::One_z_option* z_options =
732 gold::options::Command_line_options::z_options;
733 for (int i = 0; i < z_options_size; ++i)
735 if (strcmp(arg, z_options[i].name) == 0)
737 (this->*(z_options[i].set))();
738 return;
742 fprintf(stderr, _("%s: unrecognized -z subcommand: %s\n"),
743 program_name, arg);
744 ::exit(EXIT_FAILURE);
747 // Handle the --debug option.
749 void
750 General_options::handle_debug_option(const char* arg)
752 const int debug_options_size =
753 options::Command_line_options::debug_options_size;
754 const gold::options::One_debug_option* debug_options =
755 options::Command_line_options::debug_options;
756 for (int i = 0; i < debug_options_size; ++i)
758 if (strcmp(arg, debug_options[i].name) == 0)
760 this->set_debug(debug_options[i].debug_flags);
761 return;
765 fprintf(stderr, _("%s: unrecognized --debug subcommand: %s\n"),
766 program_name, arg);
767 ::exit(EXIT_FAILURE);
770 // Add the sysroot, if any, to the search paths.
772 void
773 General_options::add_sysroot()
775 if (this->sysroot_.empty())
777 this->sysroot_ = get_default_sysroot();
778 if (this->sysroot_.empty())
779 return;
782 const char* sysroot = this->sysroot_.c_str();
783 char* canonical_sysroot = lrealpath(sysroot);
785 for (Dir_list::iterator p = this->search_path_.begin();
786 p != this->search_path_.end();
787 ++p)
788 p->add_sysroot(sysroot, canonical_sysroot);
790 free(canonical_sysroot);
793 // The default values for the position dependent options.
795 Position_dependent_options::Position_dependent_options()
796 : do_static_search_(false),
797 as_needed_(false),
798 include_whole_archive_(false),
799 input_format_(General_options::OBJECT_FORMAT_ELF)
803 // Set the input format.
805 void
806 Position_dependent_options::set_input_format(const char* arg)
808 this->input_format_ = string_to_object_format(arg);
811 // Search_directory methods.
813 // This is called if we have a sysroot. Apply the sysroot if
814 // appropriate. Record whether the directory is in the sysroot.
816 void
817 Search_directory::add_sysroot(const char* sysroot,
818 const char* canonical_sysroot)
820 gold_assert(*sysroot != '\0');
821 if (this->put_in_sysroot_)
823 if (!IS_DIR_SEPARATOR(this->name_[0])
824 && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
825 this->name_ = '/' + this->name_;
826 this->name_ = sysroot + this->name_;
827 this->is_in_sysroot_ = true;
829 else
831 // Check whether this entry is in the sysroot. To do this
832 // correctly, we need to use canonical names. Otherwise we will
833 // get confused by the ../../.. paths that gcc tends to use.
834 char* canonical_name = lrealpath(this->name_.c_str());
835 int canonical_name_len = strlen(canonical_name);
836 int canonical_sysroot_len = strlen(canonical_sysroot);
837 if (canonical_name_len > canonical_sysroot_len
838 && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
840 canonical_name[canonical_sysroot_len] = '\0';
841 if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
842 this->is_in_sysroot_ = true;
844 free(canonical_name);
848 // Input_arguments methods.
850 // Add a file to the list.
852 void
853 Input_arguments::add_file(const Input_file_argument& file)
855 if (!this->in_group_)
856 this->input_argument_list_.push_back(Input_argument(file));
857 else
859 gold_assert(!this->input_argument_list_.empty());
860 gold_assert(this->input_argument_list_.back().is_group());
861 this->input_argument_list_.back().group()->add_file(file);
865 // Start a group.
867 void
868 Input_arguments::start_group()
870 gold_assert(!this->in_group_);
871 Input_file_group* group = new Input_file_group();
872 this->input_argument_list_.push_back(Input_argument(group));
873 this->in_group_ = true;
876 // End a group.
878 void
879 Input_arguments::end_group()
881 gold_assert(this->in_group_);
882 this->in_group_ = false;
885 // Command_line options.
887 Command_line::Command_line(Script_options* script_options)
888 : options_(script_options), position_options_(), inputs_()
892 // Process the command line options. For process_one_option,
893 // i is the index of argv to process next, and the return value
894 // is the index of the next option to process (i+1 or i+2, or argc
895 // to indicate processing is done). no_more_options is set to true
896 // if (and when) "--" is seen as an option.
899 Command_line::process_one_option(int argc, char** argv, int i,
900 bool* no_more_options)
902 const int options_size = options::Command_line_options::options_size;
903 const options::One_option* options = options::Command_line_options::options;
904 gold_assert(i < argc);
906 if (argv[i][0] != '-' || *no_more_options)
908 this->add_file(argv[i], false);
909 return i + 1;
912 // Option starting with '-'.
913 int dashes = 1;
914 if (argv[i][1] == '-')
916 dashes = 2;
917 if (argv[i][2] == '\0')
919 *no_more_options = true;
920 return i + 1;
924 // Look for a long option match.
925 char* opt = argv[i] + dashes;
926 char first = opt[0];
927 int skiparg = 0;
928 char* arg = strchr(opt, '=');
929 bool argument_with_equals = arg != NULL;
930 if (arg != NULL)
932 *arg = '\0';
933 ++arg;
935 else if (i + 1 < argc)
937 arg = argv[i + 1];
938 skiparg = 1;
941 int j;
942 for (j = 0; j < options_size; ++j)
944 if (options[j].long_option != NULL
945 && (dashes == 2
946 || (options[j].dash
947 != options::One_option::EXACTLY_TWO_DASHES))
948 && first == options[j].long_option[0]
949 && strcmp(opt, options[j].long_option) == 0)
951 if (options[j].special)
953 // Restore the '=' we clobbered above.
954 if (arg != NULL && skiparg == 0)
955 arg[-1] = '=';
956 i += options[j].special(argc - i, argv + i, opt, true, this);
958 else
960 if (!options[j].takes_argument())
962 if (argument_with_equals)
963 this->usage(_("unexpected argument"), argv[i]);
964 arg = NULL;
965 skiparg = 0;
967 else
969 if (arg == NULL)
970 this->usage(_("missing argument"), argv[i]);
972 this->apply_option(options[j], arg);
973 i += skiparg + 1;
975 break;
978 if (j < options_size)
979 return i;
981 // If we saw two dashes, we needed to have seen a long option.
982 if (dashes == 2)
983 this->usage(_("unknown option"), argv[i]);
985 // Look for a short option match. There may be more than one
986 // short option in a given argument.
987 bool done = false;
988 char* s = argv[i] + 1;
989 ++i;
990 while (*s != '\0' && !done)
992 char opt = *s;
993 int j;
994 for (j = 0; j < options_size; ++j)
996 if (options[j].short_option == opt)
998 if (options[j].special)
1000 // Undo the argument skip done above.
1001 --i;
1002 i += options[j].special(argc - i, argv + i, s, false,
1003 this);
1004 done = true;
1006 else
1008 arg = NULL;
1009 if (options[j].takes_argument())
1011 if (s[1] != '\0')
1013 arg = s + 1;
1014 done = true;
1016 else if (i < argc)
1018 arg = argv[i];
1019 ++i;
1021 else
1022 this->usage(_("missing argument"), opt);
1024 this->apply_option(options[j], arg);
1026 break;
1030 if (j >= options_size)
1031 this->usage(_("unknown option"), *s);
1033 ++s;
1035 return i;
1039 void
1040 Command_line::process(int argc, char** argv)
1042 bool no_more_options = false;
1043 int i = 0;
1044 while (i < argc)
1045 i = process_one_option(argc, argv, i, &no_more_options);
1047 if (this->inputs_.in_group())
1049 fprintf(stderr, _("%s: missing group end\n"), program_name);
1050 this->usage();
1053 // FIXME: We should only do this when configured in native mode.
1054 this->options_.add_to_search_path_with_sysroot("/lib");
1055 this->options_.add_to_search_path_with_sysroot("/usr/lib");
1057 this->options_.add_sysroot();
1059 // Ensure options don't contradict each other and are otherwise kosher.
1060 this->normalize_options();
1063 // Extract an option argument for a special option. LONGNAME is the
1064 // long name of the option. This sets *PRET to the return value for
1065 // the special function handler to skip to the next option.
1067 const char*
1068 Command_line::get_special_argument(const char* longname, int argc, char** argv,
1069 const char* arg, bool long_option,
1070 int *pret)
1072 if (long_option)
1074 size_t longlen = strlen(longname);
1075 gold_assert(strncmp(arg, longname, longlen) == 0);
1076 arg += longlen;
1077 if (*arg == '=')
1079 *pret = 1;
1080 return arg + 1;
1082 else if (argc > 1)
1084 gold_assert(*arg == '\0');
1085 *pret = 2;
1086 return argv[1];
1089 else
1091 if (arg[1] != '\0')
1093 *pret = 1;
1094 return arg + 1;
1096 else if (argc > 1)
1098 *pret = 2;
1099 return argv[1];
1103 this->usage(_("missing argument"), arg);
1106 // Ensure options don't contradict each other and are otherwise kosher.
1108 void
1109 Command_line::normalize_options()
1111 if (this->options_.is_shared() && this->options_.is_relocatable())
1112 gold_fatal(_("-shared and -r are incompatible"));
1114 if (this->options_.output_format() != General_options::OBJECT_FORMAT_ELF
1115 && (this->options_.is_shared() || this->options_.is_relocatable()))
1116 gold_fatal(_("binary output format not compatible with -shared or -r"));
1118 // If the user specifies both -s and -r, convert the -s as -S.
1119 // -r requires us to keep externally visible symbols!
1120 if (this->options_.strip_all() && this->options_.is_relocatable())
1122 // Clears the strip_all() status, replacing it with strip_debug().
1123 this->options_.set_strip_debug();
1126 // FIXME: we can/should be doing a lot more sanity checking here.
1130 // Apply a command line option.
1132 void
1133 Command_line::apply_option(const options::One_option& opt,
1134 const char* arg)
1136 if (arg == NULL)
1138 if (opt.general_noarg)
1139 (this->options_.*(opt.general_noarg))();
1140 else if (opt.dependent_noarg)
1141 (this->position_options_.*(opt.dependent_noarg))();
1142 else
1143 gold_unreachable();
1145 else
1147 if (opt.general_arg)
1148 (this->options_.*(opt.general_arg))(arg);
1149 else if (opt.dependent_arg)
1150 (this->position_options_.*(opt.dependent_arg))(arg);
1151 else
1152 gold_unreachable();
1156 // Add an input file or library.
1158 void
1159 Command_line::add_file(const char* name, bool is_lib)
1161 Input_file_argument file(name, is_lib, "", false, this->position_options_);
1162 this->inputs_.add_file(file);
1165 // Handle the -l option, which requires special treatment.
1168 Command_line::process_l_option(int argc, char** argv, char* arg,
1169 bool long_option)
1171 int ret;
1172 const char* libname = this->get_special_argument("library", argc, argv, arg,
1173 long_option, &ret);
1174 this->add_file(libname, true);
1175 return ret;
1178 // Handle the --start-group option.
1180 void
1181 Command_line::start_group(const char* arg)
1183 if (this->inputs_.in_group())
1184 this->usage(_("may not nest groups"), arg);
1185 this->inputs_.start_group();
1188 // Handle the --end-group option.
1190 void
1191 Command_line::end_group(const char* arg)
1193 if (!this->inputs_.in_group())
1194 this->usage(_("group end without group start"), arg);
1195 this->inputs_.end_group();
1198 // Report a usage error. */
1200 void
1201 Command_line::usage()
1203 fprintf(stderr,
1204 _("%s: use the --help option for usage information\n"),
1205 program_name);
1206 ::exit(EXIT_FAILURE);
1209 void
1210 Command_line::usage(const char* msg, const char *opt)
1212 fprintf(stderr,
1213 _("%s: %s: %s\n"),
1214 program_name, opt, msg);
1215 this->usage();
1218 void
1219 Command_line::usage(const char* msg, char opt)
1221 fprintf(stderr,
1222 _("%s: -%c: %s\n"),
1223 program_name, opt, msg);
1224 this->usage();
1227 } // End namespace gold.