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.
28 #include "filenames.h"
29 #include "libiberty.h"
38 // The information we keep for a single command line option.
40 struct options::One_option
42 // The single character option name, or '\0' if this is only a long
46 // The long option name, or NULL if this is only a short option.
47 const char* long_option
;
49 // Description of the option for --help output, or NULL if there is none.
52 // How to print the option name in --help output, or NULL to use the
54 const char* help_output
;
56 // Long option dash control. This is ignored if long_option is
60 // Long option normally takes one dash; two dashes are also
63 // Long option normally takes two dashes; one dash is also
66 // Long option always takes two dashes.
70 // Function for special handling, or NULL. Returns the number of
71 // arguments to skip. This will normally be at least 1, but it may
72 // be 0 if this function changes *argv. ARG points to the location
73 // in *ARGV where the option starts, which may be helpful for a
75 int (*special
)(int argc
, char** argv
, char *arg
, bool long_option
,
78 // If this is a position independent option which does not take an
79 // argument, this is the member function to call to record it. (In
80 // this file, the bool will always be 'true' to indicate the option
82 void (General_options::*general_noarg
)(bool);
84 // If this is a position independent function which takes an
85 // argument, this is the member function to call to record it.
86 void (General_options::*general_arg
)(const char*);
88 // If this is a position dependent option which does not take an
89 // argument, this is the member function to call to record it. (In
90 // this file, the bool will always be 'true' to indicate the option
92 void (Position_dependent_options::*dependent_noarg
)(bool);
94 // If this is a position dependent option which takes an argument,
95 // this is the member function to record it.
96 void (Position_dependent_options::*dependent_arg
)(const char*);
98 // Return whether this option takes an argument.
100 takes_argument() const
101 { return this->general_arg
!= NULL
|| this->dependent_arg
!= NULL
; }
104 // We have a separate table for -z options.
106 struct options::One_z_option
108 // The name of the option.
111 // The member function in General_options called to record an option
112 // which does not take an argument.
113 void (General_options::*set_noarg
)(bool);
115 // The member function in General_options called to record an option
116 // which does take an argument.
117 void (General_options::*set_arg
)(const char*);
120 // We have a separate table for --debug options.
122 struct options::One_debug_option
124 // The name of the option.
127 // The flags to turn on.
128 unsigned int debug_flags
;
131 class options::Command_line_options
134 static const One_option options
[];
135 static const int options_size
;
136 static const One_z_option z_options
[];
137 static const int z_options_size
;
138 static const One_debug_option debug_options
[];
139 static const int debug_options_size
;
142 } // End namespace gold.
147 // Recognize input and output target names. The GNU linker accepts
148 // these with --format and --oformat. This code is intended to be
149 // minimally compatible. In practice for an ELF target this would be
150 // the same target as the input files; that name always start with
151 // "elf". Non-ELF targets would be "srec", "symbolsrec", "tekhex",
152 // "binary", "ihex". See also
153 // General_options::default_target_settings.
155 gold::General_options::Object_format
156 string_to_object_format(const char* arg
)
158 if (strncmp(arg
, "elf", 3) == 0)
159 return gold::General_options::OBJECT_FORMAT_ELF
;
160 else if (strcmp(arg
, "binary") == 0)
161 return gold::General_options::OBJECT_FORMAT_BINARY
;
164 gold::gold_error(_("format '%s' not supported "
165 "(supported formats: elf, binary)"),
167 return gold::General_options::OBJECT_FORMAT_ELF
;
171 // Handle the special -defsym option, which defines a symbol.
174 add_to_defsym(int argc
, char** argv
, char* arg
, bool long_option
,
175 gold::Command_line
* cmdline
)
178 const char* val
= cmdline
->get_special_argument("defsym", argc
, argv
, arg
,
180 cmdline
->script_options().define_symbol(val
);
184 // Handle the special -l option, which adds an input file.
187 library(int argc
, char** argv
, char* arg
, bool long_option
,
188 gold::Command_line
* cmdline
)
190 return cmdline
->process_l_option(argc
, argv
, arg
, long_option
);
193 // Handle the -R option. Historically the GNU linker made -R a
194 // synonym for --just-symbols. ELF linkers have traditionally made -R
195 // a synonym for -rpath. When ELF support was added to the GNU
196 // linker, -R was changed to switch based on the argument: if the
197 // argument is an ordinary file, we treat it as --just-symbols,
198 // otherwise we treat it as -rpath. We need to be compatible with
199 // this, because existing build scripts rely on it.
202 handle_r_option(int argc
, char** argv
, char* arg
, bool long_option
,
203 gold::Command_line
* cmdline
)
206 const char* val
= cmdline
->get_special_argument("R", argc
, argv
, arg
,
209 if (::stat(val
, &s
) != 0 || S_ISDIR(s
.st_mode
))
210 cmdline
->add_to_rpath(val
);
212 cmdline
->add_just_symbols_file(val
);
216 // Handle the --just-symbols option.
219 handle_just_symbols_option(int argc
, char** argv
, char* arg
,
220 bool long_option
, gold::Command_line
* cmdline
)
223 const char* val
= cmdline
->get_special_argument("just-symbols", argc
, argv
,
224 arg
, long_option
, &ret
);
225 cmdline
->add_just_symbols_file(val
);
229 // Handle the special -T/--script option, which reads a linker script.
232 invoke_script(int argc
, char** argv
, char* arg
, bool long_option
,
233 gold::Command_line
* cmdline
)
236 const char* script_name
= cmdline
->get_special_argument("script", argc
, argv
,
239 if (!read_commandline_script(script_name
, cmdline
))
240 gold::gold_fatal(_("unable to parse script file %s"), script_name
);
244 // Handle the special --version-script option, which reads a version script.
247 invoke_version_script(int argc
, char** argv
, char* arg
, bool long_option
,
248 gold::Command_line
* cmdline
)
251 const char* script_name
= cmdline
->get_special_argument("version-script",
255 if (!read_version_script(script_name
, cmdline
))
256 gold::gold_fatal(_("unable to parse version script file %s"), script_name
);
260 // Handle the special --start-group option.
263 start_group(int, char**, char* arg
, bool, gold::Command_line
* cmdline
)
265 cmdline
->start_group(arg
);
269 // Handle the special --end-group option.
272 end_group(int, char**, char* arg
, bool, gold::Command_line
* cmdline
)
274 cmdline
->end_group(arg
);
278 // Report usage information for ld --help, and exit.
281 help(int, char**, char*, bool, gold::Command_line
*)
283 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name
);
285 const int options_size
= gold::options::Command_line_options::options_size
;
286 const gold::options::One_option
* options
=
287 gold::options::Command_line_options::options
;
288 for (int i
= 0; i
< options_size
; ++i
)
290 if (options
[i
].doc
== NULL
)
300 if (options
[j
].help_output
!= NULL
)
307 printf(options
[j
].help_output
);
308 len
+= std::strlen(options
[j
].help_output
);
313 if (options
[j
].short_option
!= '\0')
320 printf("-%c", options
[j
].short_option
);
325 if (options
[j
].long_option
!= NULL
)
332 if (options
[j
].dash
== gold::options::One_option::ONE_DASH
)
342 printf("%s", options
[j
].long_option
);
343 len
+= std::strlen(options
[j
].long_option
);
349 while (j
< options_size
&& options
[j
].doc
== NULL
);
356 for (; len
< 30; ++len
)
359 std::puts(options
[i
].doc
);
362 ::exit(EXIT_SUCCESS
);
367 // Report version information.
370 version(int, char**, char* opt
, bool, gold::Command_line
*)
372 gold::print_version(opt
[0] == 'v' && opt
[1] == '\0');
373 ::exit(EXIT_SUCCESS
);
377 // If the default sysroot is relocatable, try relocating it based on
381 get_relative_sysroot(const char* from
)
383 char* path
= make_relative_prefix(gold::program_name
, from
,
388 if (::stat(path
, &s
) == 0 && S_ISDIR(s
.st_mode
))
396 // Return the default sysroot. This is set by the --with-sysroot
397 // option to configure.
400 get_default_sysroot()
402 const char* sysroot
= TARGET_SYSTEM_ROOT
;
403 if (*sysroot
== '\0')
406 if (TARGET_SYSTEM_ROOT_RELOCATABLE
)
408 char* path
= get_relative_sysroot (BINDIR
);
410 path
= get_relative_sysroot (TOOLBINDIR
);
413 std::string ret
= path
;
422 } // End anonymous namespace.
427 // Helper macros used to specify the options. We could also do this
428 // using constructors, but then g++ would generate code to initialize
429 // the array. We want the array to be initialized statically so that
430 // we get better startup time.
432 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
433 { short_option, long_option, doc, help, options::One_option::dash, \
434 NULL, func, NULL, NULL, NULL }
435 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
436 { short_option, long_option, doc, help, options::One_option::dash, \
437 NULL, NULL, func, NULL, NULL }
438 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
439 { short_option, long_option, doc, help, options::One_option::dash, \
440 NULL, NULL, NULL, func, NULL }
441 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
442 { short_option, long_option, doc, help, options::One_option::dash, \
443 NULL, NULL, NULL, NULL, func }
444 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
445 { short_option, long_option, doc, help, options::One_option::dash, \
446 func, NULL, NULL, NULL, NULL }
448 // Here is the actual list of options which we accept.
450 const options::One_option
451 options::Command_line_options::options
[] =
453 GENERAL_NOARG('\0', "allow-shlib-undefined",
454 N_("Allow unresolved references in shared libraries"),
456 &General_options::set_allow_shlib_undefined
),
457 GENERAL_NOARG('\0', "no-allow-shlib-undefined",
458 N_("Do not allow unresolved references in shared libraries"),
460 &General_options::set_no_allow_shlib_undefined
),
461 POSDEP_NOARG('\0', "as-needed",
462 N_("Only set DT_NEEDED for dynamic libs if used"),
463 NULL
, TWO_DASHES
, &Position_dependent_options::set_as_needed
),
464 POSDEP_NOARG('\0', "no-as-needed",
465 N_("Always DT_NEEDED for dynamic libs (default)"),
466 NULL
, TWO_DASHES
, &Position_dependent_options::set_no_as_needed
),
467 POSDEP_NOARG('\0', "Bdynamic",
468 N_("-l searches for shared libraries"),
470 &Position_dependent_options::set_Bdynamic
),
471 POSDEP_NOARG('\0', "Bstatic",
472 N_("-l does not search for shared libraries"),
474 &Position_dependent_options::set_Bstatic
),
475 GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
476 NULL
, ONE_DASH
, &General_options::set_Bsymbolic
),
477 POSDEP_ARG('b', "format", N_("Set input format (elf, binary)"),
478 N_("-b FORMAT, --format FORMAT"), TWO_DASHES
,
479 &Position_dependent_options::set_format
),
481 # define ZLIB_STR ",zlib"
485 GENERAL_ARG('\0', "compress-debug-sections",
486 N_("Compress .debug_* sections in the output file "
487 "(default is none)"),
488 N_("--compress-debug-sections=[none" ZLIB_STR
"]"),
490 &General_options::set_compress_debug_sections
),
491 SPECIAL('\0', "defsym", N_("Define a symbol"),
492 N_("--defsym SYMBOL=EXPRESSION"), TWO_DASHES
,
494 GENERAL_NOARG('\0', "demangle", N_("Demangle C++ symbols in log messages"),
495 NULL
, TWO_DASHES
, &General_options::set_demangle
),
496 GENERAL_NOARG('\0', "no-demangle",
497 N_("Do not demangle C++ symbols in log messages"),
498 NULL
, TWO_DASHES
, &General_options::set_no_demangle
),
499 GENERAL_NOARG('\0', "detect-odr-violations",
500 N_("Try to detect violations of the One Definition Rule"),
501 NULL
, TWO_DASHES
, &General_options::set_detect_odr_violations
),
502 GENERAL_ARG('e', "entry", N_("Set program start address"),
503 N_("-e ADDRESS, --entry ADDRESS"), TWO_DASHES
,
504 &General_options::set_entry
),
505 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
506 NULL
, TWO_DASHES
, &General_options::set_export_dynamic
),
507 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
508 NULL
, TWO_DASHES
, &General_options::set_eh_frame_hdr
),
509 GENERAL_ARG('h', "soname", N_("Set shared library name"),
510 N_("-h FILENAME, -soname FILENAME"), ONE_DASH
,
511 &General_options::set_soname
),
512 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
513 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES
,
514 &General_options::set_dynamic_linker
),
515 SPECIAL('l', "library", N_("Search for library LIBNAME"),
516 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES
,
518 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
519 N_("-L DIR, --library-path DIR"), TWO_DASHES
,
520 &General_options::add_to_search_path
),
521 GENERAL_ARG('m', NULL
, N_("Ignored for compatibility"), NULL
, ONE_DASH
,
522 &General_options::ignore
),
523 GENERAL_ARG('o', "output", N_("Set output file name"),
524 N_("-o FILE, --output FILE"), TWO_DASHES
,
525 &General_options::set_output
),
526 GENERAL_ARG('O', "optimize", N_("Optimize output file size"),
527 N_("-O level"), ONE_DASH
,
528 &General_options::set_optimize
),
529 GENERAL_ARG('\0', "oformat", N_("Set output format (only binary supported)"),
530 N_("--oformat FORMAT"), EXACTLY_TWO_DASHES
,
531 &General_options::set_oformat
),
532 GENERAL_NOARG('q', "emit-relocs", N_("Generate relocations in output"),
533 NULL
, TWO_DASHES
, &General_options::set_emit_relocs
),
534 GENERAL_NOARG('r', "relocatable", N_("Generate relocatable output"), NULL
,
535 TWO_DASHES
, &General_options::set_relocatable
),
536 // -R really means -rpath, but can mean --just-symbols for
537 // compatibility with GNU ld. -rpath is always -rpath, so we list
539 SPECIAL('R', NULL
, N_("Add DIR to runtime search path"),
540 N_("-R DIR"), ONE_DASH
, &handle_r_option
),
541 GENERAL_ARG('\0', "rpath", NULL
, N_("-rpath DIR"), ONE_DASH
,
542 &General_options::add_to_rpath
),
543 SPECIAL('\0', "just-symbols", N_("Read only symbol values from file"),
544 N_("-R FILE, --just-symbols FILE"), TWO_DASHES
,
545 &handle_just_symbols_option
),
546 GENERAL_ARG('\0', "rpath-link",
547 N_("Add DIR to link time shared library search path"),
548 N_("--rpath-link DIR"), TWO_DASHES
,
549 &General_options::add_to_rpath_link
),
550 GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL
,
551 TWO_DASHES
, &General_options::set_strip_all
),
552 GENERAL_NOARG('\0', "strip-debug-gdb",
553 N_("Strip debug symbols that are unused by gdb "
554 "(at least versions <= 6.7)"),
555 NULL
, TWO_DASHES
, &General_options::set_strip_debug_gdb
),
556 // This must come after -Sdebug since it's a prefix of it.
557 GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL
,
558 TWO_DASHES
, &General_options::set_strip_debug
),
559 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
560 NULL
, ONE_DASH
, &General_options::set_shared
),
561 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
562 NULL
, ONE_DASH
, &General_options::set_static
),
563 GENERAL_NOARG('\0', "stats", N_("Print resource usage statistics"),
564 NULL
, TWO_DASHES
, &General_options::set_stats
),
565 GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
566 N_("--sysroot DIR"), TWO_DASHES
, &General_options::set_sysroot
),
567 GENERAL_ARG('\0', "Tbss", N_("Set the address of the bss segment"),
568 N_("-Tbss ADDRESS"), ONE_DASH
,
569 &General_options::set_Tbss
),
570 GENERAL_ARG('\0', "Tdata", N_("Set the address of the data segment"),
571 N_("-Tdata ADDRESS"), ONE_DASH
,
572 &General_options::set_Tdata
),
573 GENERAL_ARG('\0', "Ttext", N_("Set the address of the text segment"),
574 N_("-Ttext ADDRESS"), ONE_DASH
,
575 &General_options::set_Ttext
),
576 // This must come after -Ttext and friends since it's a prefix of
578 SPECIAL('T', "script", N_("Read linker script"),
579 N_("-T FILE, --script FILE"), TWO_DASHES
,
581 SPECIAL('\0', "version-script", N_("Read version script"),
582 N_("--version-script FILE"), TWO_DASHES
,
583 &invoke_version_script
),
584 GENERAL_NOARG('\0', "threads", N_("Run the linker multi-threaded"),
585 NULL
, TWO_DASHES
, &General_options::set_threads
),
586 GENERAL_NOARG('\0', "no-threads", N_("Do not run the linker multi-threaded"),
587 NULL
, TWO_DASHES
, &General_options::set_no_threads
),
588 GENERAL_ARG('\0', "thread-count", N_("Number of threads to use"),
589 N_("--thread-count COUNT"), TWO_DASHES
,
590 &General_options::set_thread_count
),
591 GENERAL_ARG('\0', "thread-count-initial",
592 N_("Number of threads to use in initial pass"),
593 N_("--thread-count-initial COUNT"), TWO_DASHES
,
594 &General_options::set_thread_count_initial
),
595 GENERAL_ARG('\0', "thread-count-middle",
596 N_("Number of threads to use in middle pass"),
597 N_("--thread-count-middle COUNT"), TWO_DASHES
,
598 &General_options::set_thread_count_middle
),
599 GENERAL_ARG('\0', "thread-count-final",
600 N_("Number of threads to use in final pass"),
601 N_("--thread-count-final COUNT"), TWO_DASHES
,
602 &General_options::set_thread_count_final
),
603 POSDEP_NOARG('\0', "whole-archive",
604 N_("Include all archive contents"),
606 &Position_dependent_options::set_whole_archive
),
607 POSDEP_NOARG('\0', "no-whole-archive",
608 N_("Include only needed archive contents"),
610 &Position_dependent_options::set_no_whole_archive
),
612 GENERAL_ARG('z', NULL
,
613 N_("Subcommands as follows:\n\
614 -z execstack Mark output as requiring executable stack\n\
615 -z noexecstack Mark output as not requiring executable stack\n\
616 -z max-page-size=SIZE Set maximum page size to SIZE\n\
617 -z common-page-size=SIZE Set common page size to SIZE"),
618 N_("-z SUBCOMMAND"), ONE_DASH
,
619 &General_options::handle_z_option
),
621 SPECIAL('(', "start-group", N_("Start a library search group"), NULL
,
622 TWO_DASHES
, &start_group
),
623 SPECIAL(')', "end-group", N_("End a library search group"), NULL
,
624 TWO_DASHES
, &end_group
),
625 SPECIAL('\0', "help", N_("Report usage information"), NULL
,
627 SPECIAL('v', "version", N_("Report version information"), NULL
,
628 TWO_DASHES
, &version
),
629 GENERAL_ARG('\0', "debug", N_("Turn on debugging (all,task,script)"),
630 N_("--debug=TYPE"), TWO_DASHES
,
631 &General_options::handle_debug_option
)
634 const int options::Command_line_options::options_size
=
635 sizeof (options
) / sizeof (options
[0]);
639 const options::One_z_option
640 options::Command_line_options::z_options
[] =
642 { "execstack", &General_options::set_execstack
, NULL
},
643 { "noexecstack", &General_options::set_noexecstack
, NULL
},
644 { "max-page-size", NULL
, &General_options::set_max_page_size
},
645 { "common-page-size", NULL
, &General_options::set_common_page_size
}
648 const int options::Command_line_options::z_options_size
=
649 sizeof(z_options
) / sizeof(z_options
[0]);
651 // The --debug options.
653 const options::One_debug_option
654 options::Command_line_options::debug_options
[] =
656 { "all", DEBUG_ALL
},
657 { "task", DEBUG_TASK
},
658 { "script", DEBUG_SCRIPT
}
661 const int options::Command_line_options::debug_options_size
=
662 sizeof(debug_options
) / sizeof(debug_options
[0]);
664 // The default values for the general options.
666 General_options::General_options()
668 export_dynamic_(false),
670 dynamic_linker_(NULL
),
672 optimization_level_(0),
673 output_file_name_("a.out"),
674 oformat_(OBJECT_FORMAT_ELF
),
675 oformat_string_(NULL
),
677 is_relocatable_(false),
679 allow_shlib_undefined_(false),
681 compress_debug_sections_(NO_COMPRESSION
),
682 detect_odr_violations_(false),
683 create_eh_frame_hdr_(false),
690 bss_segment_address_(-1U), // -1 indicates value not set by user
691 data_segment_address_(-1U),
692 text_segment_address_(-1U),
694 thread_count_initial_(0),
695 thread_count_middle_(0),
696 thread_count_final_(0),
697 execstack_(EXECSTACK_FROM_INPUT
),
699 common_page_size_(0),
702 // We initialize demangle_ based on the environment variable
703 // COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
704 // output of the linker, unless COLLECT_NO_DEMANGLE is set in the
705 // environment. Acting the same way here lets us provide the same
706 // interface by default.
707 this->demangle_
= getenv("COLLECT_NO_DEMANGLE") == NULL
;
710 // Handle the --oformat option.
713 General_options::set_oformat(const char* arg
)
715 this->oformat_string_
= arg
;
716 this->oformat_
= string_to_object_format(arg
);
719 // Handle the -z option.
722 General_options::handle_z_option(const char* arg
)
724 // ARG may be a word, like "noexec", or it may be an option in its
725 // own right, like "max-page-size=SIZE".
726 const char* argarg
= strchr(arg
, '='); // the argument to the -z argument
730 arglen
= argarg
- arg
;
734 arglen
= strlen(arg
);
736 const int z_options_size
= options::Command_line_options::z_options_size
;
737 const gold::options::One_z_option
* z_options
=
738 gold::options::Command_line_options::z_options
;
739 for (int i
= 0; i
< z_options_size
; ++i
)
741 if (memcmp(arg
, z_options
[i
].name
, arglen
) == 0
742 && z_options
[i
].name
[arglen
] == '\0')
744 if (z_options
[i
].set_noarg
&& argarg
)
745 gold::gold_fatal(_("-z subcommand does not take an argument: %s\n"),
747 else if (z_options
[i
].set_arg
&& !argarg
)
748 gold::gold_fatal(_("-z subcommand requires an argument: %s\n"),
750 else if (z_options
[i
].set_arg
)
751 (this->*(z_options
[i
].set_arg
))(argarg
);
753 (this->*(z_options
[i
].set_noarg
))(true);
758 gold::gold_fatal(_("%s: unrecognized -z subcommand: %s\n"),
762 // Handle the --debug option.
765 General_options::handle_debug_option(const char* arg
)
767 const int debug_options_size
=
768 options::Command_line_options::debug_options_size
;
769 const gold::options::One_debug_option
* debug_options
=
770 options::Command_line_options::debug_options
;
771 for (int i
= 0; i
< debug_options_size
; ++i
)
773 if (strcmp(arg
, debug_options
[i
].name
) == 0)
775 this->set_debug(debug_options
[i
].debug_flags
);
780 fprintf(stderr
, _("%s: unrecognized --debug subcommand: %s\n"),
782 ::exit(EXIT_FAILURE
);
785 // Add the sysroot, if any, to the search paths.
788 General_options::add_sysroot()
790 if (this->sysroot_
.empty())
792 this->sysroot_
= get_default_sysroot();
793 if (this->sysroot_
.empty())
797 const char* sysroot
= this->sysroot_
.c_str();
798 char* canonical_sysroot
= lrealpath(sysroot
);
800 for (Dir_list::iterator p
= this->search_path_
.begin();
801 p
!= this->search_path_
.end();
803 p
->add_sysroot(sysroot
, canonical_sysroot
);
805 free(canonical_sysroot
);
808 // The default values for the position dependent options.
810 Position_dependent_options::Position_dependent_options()
811 : do_static_search_(false),
813 include_whole_archive_(false),
814 input_format_(General_options::OBJECT_FORMAT_ELF
)
818 // Set the input format.
821 Position_dependent_options::set_format(const char* arg
)
823 this->input_format_
= string_to_object_format(arg
);
826 // Search_directory methods.
828 // This is called if we have a sysroot. Apply the sysroot if
829 // appropriate. Record whether the directory is in the sysroot.
832 Search_directory::add_sysroot(const char* sysroot
,
833 const char* canonical_sysroot
)
835 gold_assert(*sysroot
!= '\0');
836 if (this->put_in_sysroot_
)
838 if (!IS_DIR_SEPARATOR(this->name_
[0])
839 && !IS_DIR_SEPARATOR(sysroot
[strlen(sysroot
) - 1]))
840 this->name_
= '/' + this->name_
;
841 this->name_
= sysroot
+ this->name_
;
842 this->is_in_sysroot_
= true;
846 // Check whether this entry is in the sysroot. To do this
847 // correctly, we need to use canonical names. Otherwise we will
848 // get confused by the ../../.. paths that gcc tends to use.
849 char* canonical_name
= lrealpath(this->name_
.c_str());
850 int canonical_name_len
= strlen(canonical_name
);
851 int canonical_sysroot_len
= strlen(canonical_sysroot
);
852 if (canonical_name_len
> canonical_sysroot_len
853 && IS_DIR_SEPARATOR(canonical_name
[canonical_sysroot_len
]))
855 canonical_name
[canonical_sysroot_len
] = '\0';
856 if (FILENAME_CMP(canonical_name
, canonical_sysroot
) == 0)
857 this->is_in_sysroot_
= true;
859 free(canonical_name
);
863 // Input_arguments methods.
865 // Add a file to the list.
868 Input_arguments::add_file(const Input_file_argument
& file
)
870 if (!this->in_group_
)
871 this->input_argument_list_
.push_back(Input_argument(file
));
874 gold_assert(!this->input_argument_list_
.empty());
875 gold_assert(this->input_argument_list_
.back().is_group());
876 this->input_argument_list_
.back().group()->add_file(file
);
883 Input_arguments::start_group()
885 gold_assert(!this->in_group_
);
886 Input_file_group
* group
= new Input_file_group();
887 this->input_argument_list_
.push_back(Input_argument(group
));
888 this->in_group_
= true;
894 Input_arguments::end_group()
896 gold_assert(this->in_group_
);
897 this->in_group_
= false;
900 // Command_line options.
902 Command_line::Command_line()
903 : options_(), position_options_(), script_options_(), inputs_()
907 // Process the command line options. For process_one_option,
908 // i is the index of argv to process next, and the return value
909 // is the index of the next option to process (i+1 or i+2, or argc
910 // to indicate processing is done). no_more_options is set to true
911 // if (and when) "--" is seen as an option.
914 Command_line::process_one_option(int argc
, char** argv
, int i
,
915 bool* no_more_options
)
917 const int options_size
= options::Command_line_options::options_size
;
918 const options::One_option
* options
= options::Command_line_options::options
;
919 gold_assert(i
< argc
);
921 if (argv
[i
][0] != '-' || *no_more_options
)
923 this->add_file(argv
[i
], false);
927 // Option starting with '-'.
929 if (argv
[i
][1] == '-')
932 if (argv
[i
][2] == '\0')
934 *no_more_options
= true;
939 // Look for a long option match.
940 char* opt
= argv
[i
] + dashes
;
943 char* arg
= strchr(opt
, '=');
944 bool argument_with_equals
= arg
!= NULL
;
950 else if (i
+ 1 < argc
)
957 for (j
= 0; j
< options_size
; ++j
)
959 if (options
[j
].long_option
!= NULL
962 != options::One_option::EXACTLY_TWO_DASHES
))
963 && first
== options
[j
].long_option
[0]
964 && strcmp(opt
, options
[j
].long_option
) == 0)
966 if (options
[j
].special
)
968 // Restore the '=' we clobbered above.
969 if (arg
!= NULL
&& skiparg
== 0)
971 i
+= options
[j
].special(argc
- i
, argv
+ i
, opt
, true, this);
975 if (!options
[j
].takes_argument())
977 if (argument_with_equals
)
978 this->usage(_("unexpected argument"), argv
[i
]);
985 this->usage(_("missing argument"), argv
[i
]);
987 this->apply_option(options
[j
], arg
);
993 if (j
< options_size
)
996 // If we saw two dashes, we needed to have seen a long option.
998 this->usage(_("unknown option"), argv
[i
]);
1000 // Look for a short option match. There may be more than one
1001 // short option in a given argument.
1003 char* s
= argv
[i
] + 1;
1005 while (*s
!= '\0' && !done
)
1009 for (j
= 0; j
< options_size
; ++j
)
1011 if (options
[j
].short_option
== opt
)
1013 if (options
[j
].special
)
1015 // Undo the argument skip done above.
1017 i
+= options
[j
].special(argc
- i
, argv
+ i
, s
, false,
1024 if (options
[j
].takes_argument())
1037 this->usage(_("missing argument"), opt
);
1039 this->apply_option(options
[j
], arg
);
1045 if (j
>= options_size
)
1046 this->usage(_("unknown option"), *s
);
1055 Command_line::process(int argc
, char** argv
)
1057 bool no_more_options
= false;
1060 i
= process_one_option(argc
, argv
, i
, &no_more_options
);
1062 if (this->inputs_
.in_group())
1064 fprintf(stderr
, _("%s: missing group end\n"), program_name
);
1068 // FIXME: We should only do this when configured in native mode.
1069 this->options_
.add_to_search_path_with_sysroot("/lib");
1070 this->options_
.add_to_search_path_with_sysroot("/usr/lib");
1072 this->options_
.add_sysroot();
1074 // Ensure options don't contradict each other and are otherwise kosher.
1075 this->normalize_options();
1078 // Extract an option argument for a special option. LONGNAME is the
1079 // long name of the option. This sets *PRET to the return value for
1080 // the special function handler to skip to the next option.
1083 Command_line::get_special_argument(const char* longname
, int argc
, char** argv
,
1084 const char* arg
, bool long_option
,
1089 size_t longlen
= strlen(longname
);
1090 gold_assert(strncmp(arg
, longname
, longlen
) == 0);
1099 gold_assert(*arg
== '\0');
1118 this->usage(_("missing argument"), arg
);
1121 // Ensure options don't contradict each other and are otherwise kosher.
1124 Command_line::normalize_options()
1126 if (this->options_
.shared() && this->options_
.relocatable())
1127 gold_fatal(_("-shared and -r are incompatible"));
1129 if (this->options_
.oformat() != General_options::OBJECT_FORMAT_ELF
1130 && (this->options_
.shared() || this->options_
.relocatable()))
1131 gold_fatal(_("binary output format not compatible with -shared or -r"));
1133 // If the user specifies both -s and -r, convert the -s as -S.
1134 // -r requires us to keep externally visible symbols!
1135 if (this->options_
.strip_all() && this->options_
.relocatable())
1137 // Clears the strip_all() status, replacing it with strip_debug().
1138 this->options_
.set_strip_debug(true);
1141 // FIXME: we can/should be doing a lot more sanity checking here.
1145 // Apply a command line option.
1148 Command_line::apply_option(const options::One_option
& opt
,
1153 if (opt
.general_noarg
)
1154 (this->options_
.*(opt
.general_noarg
))(true);
1155 else if (opt
.dependent_noarg
)
1156 (this->position_options_
.*(opt
.dependent_noarg
))(true);
1162 if (opt
.general_arg
)
1163 (this->options_
.*(opt
.general_arg
))(arg
);
1164 else if (opt
.dependent_arg
)
1165 (this->position_options_
.*(opt
.dependent_arg
))(arg
);
1171 // Add an input file or library.
1174 Command_line::add_file(const char* name
, bool is_lib
)
1176 Input_file_argument
file(name
, is_lib
, "", false, this->position_options_
);
1177 this->inputs_
.add_file(file
);
1180 // Handle the -l option, which requires special treatment.
1183 Command_line::process_l_option(int argc
, char** argv
, char* arg
,
1187 const char* libname
= this->get_special_argument("library", argc
, argv
, arg
,
1189 this->add_file(libname
, true);
1193 // Handle the --start-group option.
1196 Command_line::start_group(const char* arg
)
1198 if (this->inputs_
.in_group())
1199 this->usage(_("may not nest groups"), arg
);
1200 this->inputs_
.start_group();
1203 // Handle the --end-group option.
1206 Command_line::end_group(const char* arg
)
1208 if (!this->inputs_
.in_group())
1209 this->usage(_("group end without group start"), arg
);
1210 this->inputs_
.end_group();
1213 // Report a usage error. */
1216 Command_line::usage()
1219 _("%s: use the --help option for usage information\n"),
1221 ::exit(EXIT_FAILURE
);
1225 Command_line::usage(const char* msg
, const char *opt
)
1229 program_name
, opt
, msg
);
1234 Command_line::usage(const char* msg
, char opt
)
1238 program_name
, opt
, msg
);
1242 } // End namespace gold.