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"
33 #include "target-select.h"
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
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.
53 // How to print the option name in --help output, or NULL to use the
55 const char* help_output
;
57 // Long option dash control. This is ignored if long_option is
61 // Long option normally takes one dash; two dashes are also
64 // Long option normally takes two dashes; one dash is also
67 // Long option always takes two dashes.
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
76 int (*special
)(int argc
, char** argv
, char *arg
, bool long_option
,
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.
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.
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.
119 // The flags to turn on.
120 unsigned int debug_flags
;
123 class options::Command_line_options
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.
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
;
156 gold::gold_error(_("format '%s' not supported "
157 "(supported formats: elf, binary)"),
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
)
185 const char* val
= cmdline
->get_special_argument("R", argc
, argv
, arg
,
188 if (::stat(val
, &s
) != 0 || S_ISDIR(s
.st_mode
))
189 cmdline
->add_to_rpath(val
);
191 cmdline
->add_just_symbols_file(val
);
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
)
202 const char* val
= cmdline
->get_special_argument("just-symbols", argc
, argv
,
203 arg
, long_option
, &ret
);
204 cmdline
->add_just_symbols_file(val
);
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
)
215 const char* script_name
= cmdline
->get_special_argument("script", argc
, argv
,
218 if (!read_commandline_script(script_name
, cmdline
))
219 gold::gold_fatal(_("unable to parse script file %s"), script_name
);
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
)
230 const char* script_name
= cmdline
->get_special_argument("version-script",
234 if (!read_version_script(script_name
, cmdline
))
235 gold::gold_fatal(_("unable to parse version script file %s"), script_name
);
239 // Handle the special --start-group option.
242 start_group(int, char**, char* arg
, bool, gold::Command_line
* cmdline
)
244 cmdline
->start_group(arg
);
248 // Handle the special --end-group option.
251 end_group(int, char**, char* arg
, bool, gold::Command_line
* cmdline
)
253 cmdline
->end_group(arg
);
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
)
279 if (options
[j
].help_output
!= NULL
)
286 printf(options
[j
].help_output
);
287 len
+= std::strlen(options
[j
].help_output
);
292 if (options
[j
].short_option
!= '\0')
299 printf("-%c", options
[j
].short_option
);
304 if (options
[j
].long_option
!= NULL
)
311 if (options
[j
].dash
== gold::options::One_option::ONE_DASH
)
321 printf("%s", options
[j
].long_option
);
322 len
+= std::strlen(options
[j
].long_option
);
328 while (j
< options_size
&& options
[j
].doc
== NULL
);
335 for (; len
< 30; ++len
)
338 std::puts(options
[i
].doc
);
341 ::exit(EXIT_SUCCESS
);
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
);
356 // If the default sysroot is relocatable, try relocating it based on
360 get_relative_sysroot(const char* from
)
362 char* path
= make_relative_prefix(gold::program_name
, from
,
367 if (::stat(path
, &s
) == 0 && S_ISDIR(s
.st_mode
))
375 // Return the default sysroot. This is set by the --with-sysroot
376 // option to configure.
379 get_default_sysroot()
381 const char* sysroot
= TARGET_SYSTEM_ROOT
;
382 if (*sysroot
== '\0')
385 if (TARGET_SYSTEM_ROOT_RELOCATABLE
)
387 char* path
= get_relative_sysroot (BINDIR
);
389 path
= get_relative_sysroot (TOOLBINDIR
);
392 std::string ret
= path
;
401 } // End anonymous namespace.
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"),
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"),
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"),
449 &Position_dependent_options::set_dynamic_search
),
450 POSDEP_NOARG('\0', "Bstatic",
451 N_("-l does not search for shared libraries"),
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
),
460 # define ZLIB_STR ",zlib"
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
"]"),
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
,
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
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
555 SPECIAL('T', "script", N_("Read linker script"),
556 N_("-T FILE, --script FILE"), TWO_DASHES
,
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"),
583 &Position_dependent_options::set_whole_archive
),
584 POSDEP_NOARG('\0', "no-whole-archive",
585 N_("Include only needed archive contents"),
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
,
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]);
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),
642 dynamic_linker_(NULL
),
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),
650 allow_shlib_undefined_(false),
652 compress_debug_sections_(NO_COMPRESSION
),
653 detect_odr_violations_(false),
654 create_eh_frame_hdr_(false),
661 bss_segment_address_(-1U), // -1 indicates value not set by user
662 data_segment_address_(-1U),
663 text_segment_address_(-1U),
665 thread_count_initial_(0),
666 thread_count_middle_(0),
667 thread_count_final_(0),
668 execstack_(EXECSTACK_FROM_INPUT
),
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.
683 General_options::define_symbol(const char* arg
)
685 this->script_options_
->define_symbol(arg
);
688 // Handle the --oformat option.
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
704 General_options::default_target() const
706 if (this->output_format_string_
!= NULL
)
708 Target
* target
= select_target_by_name(this->output_format_string_
);
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
,
719 GOLD_DEFAULT_BIG_ENDIAN
,
721 gold_assert(target
!= NULL
);
725 // Handle the -z option.
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
))();
742 fprintf(stderr
, _("%s: unrecognized -z subcommand: %s\n"),
744 ::exit(EXIT_FAILURE
);
747 // Handle the --debug option.
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
);
765 fprintf(stderr
, _("%s: unrecognized --debug subcommand: %s\n"),
767 ::exit(EXIT_FAILURE
);
770 // Add the sysroot, if any, to the search paths.
773 General_options::add_sysroot()
775 if (this->sysroot_
.empty())
777 this->sysroot_
= get_default_sysroot();
778 if (this->sysroot_
.empty())
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();
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),
798 include_whole_archive_(false),
799 input_format_(General_options::OBJECT_FORMAT_ELF
)
803 // Set the input format.
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.
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;
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.
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
));
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
);
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;
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);
912 // Option starting with '-'.
914 if (argv
[i
][1] == '-')
917 if (argv
[i
][2] == '\0')
919 *no_more_options
= true;
924 // Look for a long option match.
925 char* opt
= argv
[i
] + dashes
;
928 char* arg
= strchr(opt
, '=');
929 bool argument_with_equals
= arg
!= NULL
;
935 else if (i
+ 1 < argc
)
942 for (j
= 0; j
< options_size
; ++j
)
944 if (options
[j
].long_option
!= NULL
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)
956 i
+= options
[j
].special(argc
- i
, argv
+ i
, opt
, true, this);
960 if (!options
[j
].takes_argument())
962 if (argument_with_equals
)
963 this->usage(_("unexpected argument"), argv
[i
]);
970 this->usage(_("missing argument"), argv
[i
]);
972 this->apply_option(options
[j
], arg
);
978 if (j
< options_size
)
981 // If we saw two dashes, we needed to have seen a long option.
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.
988 char* s
= argv
[i
] + 1;
990 while (*s
!= '\0' && !done
)
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.
1002 i
+= options
[j
].special(argc
- i
, argv
+ i
, s
, false,
1009 if (options
[j
].takes_argument())
1022 this->usage(_("missing argument"), opt
);
1024 this->apply_option(options
[j
], arg
);
1030 if (j
>= options_size
)
1031 this->usage(_("unknown option"), *s
);
1040 Command_line::process(int argc
, char** argv
)
1042 bool no_more_options
= false;
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
);
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.
1068 Command_line::get_special_argument(const char* longname
, int argc
, char** argv
,
1069 const char* arg
, bool long_option
,
1074 size_t longlen
= strlen(longname
);
1075 gold_assert(strncmp(arg
, longname
, longlen
) == 0);
1084 gold_assert(*arg
== '\0');
1103 this->usage(_("missing argument"), arg
);
1106 // Ensure options don't contradict each other and are otherwise kosher.
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.
1133 Command_line::apply_option(const options::One_option
& opt
,
1138 if (opt
.general_noarg
)
1139 (this->options_
.*(opt
.general_noarg
))();
1140 else if (opt
.dependent_noarg
)
1141 (this->position_options_
.*(opt
.dependent_noarg
))();
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
);
1156 // Add an input file or library.
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
,
1172 const char* libname
= this->get_special_argument("library", argc
, argv
, arg
,
1174 this->add_file(libname
, true);
1178 // Handle the --start-group option.
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.
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. */
1201 Command_line::usage()
1204 _("%s: use the --help option for usage information\n"),
1206 ::exit(EXIT_FAILURE
);
1210 Command_line::usage(const char* msg
, const char *opt
)
1214 program_name
, opt
, msg
);
1219 Command_line::usage(const char* msg
, char opt
)
1223 program_name
, opt
, msg
);
1227 } // End namespace gold.