1 // options.h -- handle command line options for gold -*- C++ -*-
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.
24 // Holds everything we get from the command line.
25 // General_options (from Command_line::options())
26 // Options which are not position dependent.
27 // Input_argument (from Command_line::inputs())
28 // The list of input files, including -l options.
29 // Position_dependent_options (from Input_argument::options())
30 // Position dependent options which apply to this argument.
32 #ifndef GOLD_OPTIONS_H
33 #define GOLD_OPTIONS_H
47 class Input_file_group
;
48 class Position_dependent_options
;
54 class Command_line_options
;
57 struct One_debug_option
;
59 } // End namespace gold::options.
61 // A directory to search. For each directory we record whether it is
62 // in the sysroot. We need to know this so that, if a linker script
63 // is found within the sysroot, we will apply the sysroot to any files
64 // named by that script.
66 class Search_directory
69 // We need a default constructor because we put this in a
72 : name_(NULL
), put_in_sysroot_(false), is_in_sysroot_(false)
75 // This is the usual constructor.
76 Search_directory(const char* name
, bool put_in_sysroot
)
77 : name_(name
), put_in_sysroot_(put_in_sysroot
), is_in_sysroot_(false)
79 if (this->name_
.empty())
83 // This is called if we have a sysroot. The sysroot is prefixed to
84 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
85 // set to true for any enries which are in the sysroot (this will
86 // naturally include any entries for which put_in_sysroot_ is true).
87 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
88 // passing SYSROOT to lrealpath.
90 add_sysroot(const char* sysroot
, const char* canonical_sysroot
);
92 // Get the directory name.
95 { return this->name_
; }
97 // Return whether this directory is in the sysroot.
100 { return this->is_in_sysroot_
; }
104 bool put_in_sysroot_
;
108 // The position independent options which apply to the whole link.
109 // There are a lot of them.
111 class General_options
118 // Straight binary format.
124 // -d: define common symbols.
126 define_common() const
127 { return this->define_common_
; }
129 // -e: set entry address.
132 { return this->entry_
; }
134 // -E: export dynamic symbols.
136 export_dynamic() const
137 { return this->export_dynamic_
; }
139 // -h: shared library name.
142 { return this->soname_
; }
144 // -I: dynamic linker name.
146 dynamic_linker() const
147 { return this->dynamic_linker_
; }
149 // -L: Library search path.
150 typedef std::vector
<Search_directory
> Dir_list
;
154 { return this->search_path_
; }
156 // -O: optimization level (0: don't try to optimize output size).
159 { return this->optimization_level_
; }
161 // -o: Output file name.
163 output_file_name() const
164 { return this->output_file_name_
; }
166 // --oformat: Output format.
169 { return this->oformat_
; }
172 oformat_string() const
173 { return this->oformat_string_
; }
175 // Return the default target.
177 default_target() const;
179 // -q: Whether to emit relocations.
182 { return this->emit_relocs_
; }
184 // -r: Whether we are doing a relocatable link.
187 { return this->is_relocatable_
; }
189 // -s: Strip all symbols.
192 { return this->strip_
== STRIP_ALL
; }
194 // -S: Strip debugging information.
197 { return this->strip_
== STRIP_ALL
|| this->strip_
== STRIP_DEBUG
; }
199 // --strip-debug-gdb: strip only debugging information that's not
200 // used by gdb (at least, for gdb versions <= 6.7).
202 strip_debug_gdb() const
203 { return this->strip_debug() || this->strip_
== STRIP_DEBUG_UNUSED_BY_GDB
; }
205 // --allow-shlib-undefined: do not warn about unresolved symbols in
206 // --shared libraries.
208 allow_shlib_undefined() const
209 { return this->allow_shlib_undefined_
; }
211 // -Bsymbolic: bind defined symbols locally.
214 { return this->symbolic_
; }
216 // --compress-debug-sections: compress .debug_* sections in the
217 // output file using the given compression method. This is useful
218 // when the tools (such as gdb) support compressed sections.
220 compress_debug_sections() const
221 { return this->compress_debug_sections_
!= NO_COMPRESSION
; }
224 zlib_compress_debug_sections() const
225 { return this->compress_debug_sections_
== ZLIB_COMPRESSION
; }
227 // --demangle: demangle C++ symbols in our log messages.
230 { return this->demangle_
; }
232 // --detect-odr-violations: Whether to search for One Defn Rule violations.
234 detect_odr_violations() const
235 { return this->detect_odr_violations_
; }
237 // --eh-frame-hdr: Whether to generate an exception frame header.
240 { return this->create_eh_frame_hdr_
; }
242 // --rpath: The runtime search path.
245 { return this->rpath_
; }
247 // --rpath-link: The link time search patch for shared libraries.
250 { return this->rpath_link_
; }
252 // --shared: Whether generating a shared object.
255 { return this->is_shared_
; }
257 // This is not defined via a flag, but combines flags to say whether
258 // the output is position-independent or not.
260 output_is_position_independent() const
261 { return this->shared(); }
263 // --static: Whether doing a static link.
266 { return this->is_static_
; }
268 // --stats: Print resource usage statistics.
271 { return this->print_stats_
; }
273 // --sysroot: The system root of a cross-linker.
276 { return this->sysroot_
; }
278 // -Tbss: The address of the BSS segment
281 { return this->bss_segment_address_
; }
283 // Whether -Tbss was used.
285 user_set_Tbss() const
286 { return this->bss_segment_address_
!= -1U; }
288 // -Tdata: The address of the data segment
291 { return this->data_segment_address_
; }
293 // Whether -Tdata was used.
295 user_set_Tdata() const
296 { return this->data_segment_address_
!= -1U; }
298 // -Ttext: The address of the .text section
301 { return this->text_segment_address_
; }
303 // Whether -Ttext was used.
305 user_set_Ttext() const
306 { return this->text_segment_address_
!= -1U; }
308 // --threads: Whether to use threads.
311 { return this->threads_
; }
313 // --thread-count-initial: Threads to use in initial pass.
315 thread_count_initial() const
316 { return this->thread_count_initial_
; }
318 // --thread-count-middle: Threads to use in middle pass.
320 thread_count_middle() const
321 { return this->thread_count_middle_
; }
323 // --thread-count-final: Threads to use in final pass.
325 thread_count_final() const
326 { return this->thread_count_final_
; }
328 // -z execstack, -z noexecstack
330 is_execstack_set() const
331 { return this->execstack_
!= EXECSTACK_FROM_INPUT
; }
334 is_stack_executable() const
335 { return this->execstack_
== EXECSTACK_YES
; }
339 max_page_size() const
340 { return this->max_page_size_
; }
342 // -z common-page-size
344 common_page_size() const
345 { return this->common_page_size_
; }
350 { return this->debug_
; }
353 // Don't copy this structure.
354 General_options(const General_options
&);
355 General_options
& operator=(const General_options
&);
357 friend class Command_line
;
358 friend class options::Command_line_options
;
360 // Which symbols to strip.
363 // Don't strip any symbols.
365 // Strip all symbols.
367 // Strip debugging information.
369 // Strip debugging information that's not used by gdb (at least <= 6.7)
370 STRIP_DEBUG_UNUSED_BY_GDB
373 // Whether to mark the stack as executable.
376 // Not set on command line.
377 EXECSTACK_FROM_INPUT
,
378 // Mark the stack as executable.
380 // Mark the stack as not executable.
384 // What compression method to use
385 enum CompressionMethod
392 set_define_common(bool value
)
394 this->define_common_
= value
;
395 this->user_set_define_common_
= true;
399 set_no_define_common(bool value
)
400 { this->set_define_common(!value
); }
403 user_set_define_common() const
404 { return this->user_set_define_common_
; }
407 set_entry(const char* arg
)
408 { this->entry_
= arg
; }
411 set_export_dynamic(bool value
)
412 { this->export_dynamic_
= value
; }
415 set_soname(const char* arg
)
416 { this->soname_
= arg
; }
419 set_dynamic_linker(const char* arg
)
420 { this->dynamic_linker_
= arg
; }
423 add_to_search_path(const char* arg
)
424 { this->search_path_
.push_back(Search_directory(arg
, false)); }
427 add_to_search_path_with_sysroot(const char* arg
)
428 { this->search_path_
.push_back(Search_directory(arg
, true)); }
431 set_optimize(const char* arg
)
434 this->optimization_level_
= strtol(arg
, &endptr
, 0);
435 if (*endptr
!= '\0' || this->optimization_level_
< 0)
436 gold_fatal(_("invalid optimization level: %s"), arg
);
440 set_output(const char* arg
)
441 { this->output_file_name_
= arg
; }
444 set_oformat(const char*);
447 set_emit_relocs(bool value
)
448 { this->emit_relocs_
= value
; }
451 set_relocatable(bool value
)
452 { this->is_relocatable_
= value
; }
456 { this->strip_
= STRIP_ALL
; }
458 // Note: normalize_options() depends on the fact that this turns off
459 // STRIP_ALL if it were already set.
461 set_strip_debug(bool)
462 { this->strip_
= STRIP_DEBUG
; }
465 set_strip_debug_gdb(bool)
466 { this->strip_
= STRIP_DEBUG_UNUSED_BY_GDB
; }
469 set_allow_shlib_undefined(bool value
)
470 { this->allow_shlib_undefined_
= value
; }
473 set_no_allow_shlib_undefined(bool value
)
474 { this->set_allow_shlib_undefined(!value
); }
477 set_Bsymbolic(bool value
)
478 { this->symbolic_
= value
; }
480 void set_compress_debug_sections(const char* arg
)
482 if (strcmp(arg
, "none") == 0)
483 this->compress_debug_sections_
= NO_COMPRESSION
;
485 else if (strcmp(arg
, "zlib") == 0)
486 this->compress_debug_sections_
= ZLIB_COMPRESSION
;
489 gold_fatal(_("unsupported argument to --compress-debug-sections: %s"),
494 add_to_defsym(const char* arg
);
497 set_demangle(bool value
)
498 { this->demangle_
= value
; }
501 set_no_demangle(bool value
)
502 { this->set_demangle(!value
); }
505 set_detect_odr_violations(bool value
)
506 { this->detect_odr_violations_
= value
; }
509 set_eh_frame_hdr(bool value
)
510 { this->create_eh_frame_hdr_
= value
; }
513 add_to_rpath(const char* arg
)
514 { this->rpath_
.push_back(Search_directory(arg
, false)); }
517 add_to_rpath_link(const char* arg
)
518 { this->rpath_link_
.push_back(Search_directory(arg
, false)); }
521 set_shared(bool value
)
522 { this->is_shared_
= value
; }
525 set_static(bool value
)
526 { this->is_static_
= value
; }
529 set_stats(bool value
)
530 { this->print_stats_
= value
; }
533 set_sysroot(const char* arg
)
534 { this->sysroot_
= arg
; }
537 set_segment_address(const char* name
, const char* arg
, uint64_t* val
)
540 *val
= strtoull(arg
, &endptr
, 0);
541 if (*endptr
!= '\0' || *val
== -1U)
542 gold_fatal(_("invalid argument to %s: %s"), name
, arg
);
546 set_Tbss(const char* arg
)
547 { this->set_segment_address("-Tbss", arg
, &this->bss_segment_address_
); }
550 set_Tdata(const char* arg
)
551 { this->set_segment_address("-Tdata", arg
, &this->data_segment_address_
); }
554 set_Ttext(const char* arg
)
555 { this->set_segment_address("-Ttext", arg
, &this->text_segment_address_
); }
558 parse_thread_count(const char* arg
)
561 const int count
= strtol(arg
, &endptr
, 0);
562 if (*endptr
!= '\0' || count
< 0)
563 gold_fatal(_("invalid thread count: %s"), arg
);
568 set_threads(bool value
)
570 #ifndef ENABLE_THREADS
572 gold_fatal(_("--threads not supported"));
574 this->threads_
= value
;
578 set_no_threads(bool value
)
579 { this->set_threads(!value
); }
582 set_thread_count(const char* arg
)
584 int count
= this->parse_thread_count(arg
);
585 this->thread_count_initial_
= count
;
586 this->thread_count_middle_
= count
;
587 this->thread_count_final_
= count
;
591 set_thread_count_initial(const char* arg
)
592 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
595 set_thread_count_middle(const char* arg
)
596 { this->thread_count_middle_
= this->parse_thread_count(arg
); }
599 set_thread_count_final(const char* arg
)
600 { this->thread_count_final_
= this->parse_thread_count(arg
); }
608 { this->execstack_
= EXECSTACK_YES
; }
611 set_noexecstack(bool)
612 { this->execstack_
= EXECSTACK_NO
; }
615 set_max_page_size(const char* arg
)
618 this->max_page_size_
= strtoull(arg
, &endptr
, 0);
619 if (*endptr
!= '\0' || this->max_page_size_
== 0)
620 gold_fatal(_("invalid max-page-size: %s"), arg
);
624 set_common_page_size(const char* arg
)
627 this->common_page_size_
= strtoull(arg
, &endptr
, 0);
628 if (*endptr
!= '\0' || this->common_page_size_
== 0)
629 gold_fatal(_("invalid common-page-size: %s"), arg
);
633 set_debug(unsigned int flags
)
634 { this->debug_
= flags
; }
636 // Handle the -z option.
638 handle_z_option(const char*);
640 // Handle the --debug option.
642 handle_debug_option(const char*);
644 // Apply any sysroot to the directory lists.
649 bool user_set_define_common_
;
651 bool export_dynamic_
;
653 const char* dynamic_linker_
;
654 Dir_list search_path_
;
655 int optimization_level_
;
656 const char* output_file_name_
;
657 Object_format oformat_
;
658 const char* oformat_string_
;
660 bool is_relocatable_
;
662 bool allow_shlib_undefined_
;
664 CompressionMethod compress_debug_sections_
;
666 bool detect_odr_violations_
;
667 bool create_eh_frame_hdr_
;
669 Dir_list rpath_link_
;
673 std::string sysroot_
;
674 uint64_t bss_segment_address_
;
675 uint64_t data_segment_address_
;
676 uint64_t text_segment_address_
;
678 int thread_count_initial_
;
679 int thread_count_middle_
;
680 int thread_count_final_
;
681 Execstack execstack_
;
682 uint64_t max_page_size_
;
683 uint64_t common_page_size_
;
687 // The current state of the position dependent options.
689 class Position_dependent_options
692 typedef General_options::Object_format Object_format
;
694 Position_dependent_options();
696 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
697 // -rather than a shared object.
700 { return this->do_static_search_
; }
702 // --as-needed: Whether to add a DT_NEEDED argument only if the
703 // dynamic object is used.
706 { return this->as_needed_
; }
708 // --whole-archive: Whether to include the entire contents of an
711 whole_archive() const
712 { return this->include_whole_archive_
; }
714 // --format: The format of the input file.
717 { return this->input_format_
; }
720 set_Bstatic(bool value
)
721 { this->do_static_search_
= value
; }
724 set_Bdynamic(bool value
)
725 { this->set_Bstatic(!value
); }
728 set_as_needed(bool value
)
729 { this->as_needed_
= value
; }
732 set_no_as_needed(bool value
)
733 { this->set_as_needed(!value
); }
736 set_whole_archive(bool value
)
737 { this->include_whole_archive_
= value
; }
740 set_no_whole_archive(bool value
)
741 { this->set_whole_archive(!value
); }
744 set_format(const char*);
747 bool do_static_search_
;
749 bool include_whole_archive_
;
750 Object_format input_format_
;
753 // A single file or library argument from the command line.
755 class Input_file_argument
758 // name: file name or library name
759 // is_lib: true if name is a library name: that is, emits the leading
760 // "lib" and trailing ".so"/".a" from the name
761 // extra_search_path: an extra directory to look for the file, prior
762 // to checking the normal library search path. If this is "",
763 // then no extra directory is added.
764 // just_symbols: whether this file only defines symbols.
765 // options: The position dependent options at this point in the
766 // command line, such as --whole-archive.
767 Input_file_argument()
768 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
772 Input_file_argument(const char* name
, bool is_lib
,
773 const char* extra_search_path
,
775 const Position_dependent_options
& options
)
776 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
777 just_symbols_(just_symbols
), options_(options
)
782 { return this->name_
.c_str(); }
784 const Position_dependent_options
&
786 { return this->options_
; }
790 { return this->is_lib_
; }
793 extra_search_path() const
795 return (this->extra_search_path_
.empty()
797 : this->extra_search_path_
.c_str());
800 // Return whether we should only read symbols from this file.
803 { return this->just_symbols_
; }
805 // Return whether this file may require a search using the -L
808 may_need_search() const
809 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
812 // We use std::string, not const char*, here for convenience when
813 // using script files, so that we do not have to preserve the string
817 std::string extra_search_path_
;
819 Position_dependent_options options_
;
822 // A file or library, or a group, from the command line.
827 // Create a file or library argument.
828 explicit Input_argument(Input_file_argument file
)
829 : is_file_(true), file_(file
), group_(NULL
)
832 // Create a group argument.
833 explicit Input_argument(Input_file_group
* group
)
834 : is_file_(false), group_(group
)
837 // Return whether this is a file.
840 { return this->is_file_
; }
842 // Return whether this is a group.
845 { return !this->is_file_
; }
847 // Return the information about the file.
848 const Input_file_argument
&
851 gold_assert(this->is_file_
);
855 // Return the information about the group.
856 const Input_file_group
*
859 gold_assert(!this->is_file_
);
866 gold_assert(!this->is_file_
);
872 Input_file_argument file_
;
873 Input_file_group
* group_
;
876 // A group from the command line. This is a set of arguments within
877 // --start-group ... --end-group.
879 class Input_file_group
882 typedef std::vector
<Input_argument
> Files
;
883 typedef Files::const_iterator const_iterator
;
889 // Add a file to the end of the group.
891 add_file(const Input_file_argument
& arg
)
892 { this->files_
.push_back(Input_argument(arg
)); }
894 // Iterators to iterate over the group contents.
898 { return this->files_
.begin(); }
902 { return this->files_
.end(); }
908 // A list of files from the command line or a script.
910 class Input_arguments
913 typedef std::vector
<Input_argument
> Input_argument_list
;
914 typedef Input_argument_list::const_iterator const_iterator
;
917 : input_argument_list_(), in_group_(false)
922 add_file(const Input_file_argument
& arg
);
924 // Start a group (the --start-group option).
928 // End a group (the --end-group option).
932 // Return whether we are currently in a group.
935 { return this->in_group_
; }
937 // The number of entries in the list.
940 { return this->input_argument_list_
.size(); }
942 // Iterators to iterate over the list of input files.
946 { return this->input_argument_list_
.begin(); }
950 { return this->input_argument_list_
.end(); }
952 // Return whether the list is empty.
955 { return this->input_argument_list_
.empty(); }
958 Input_argument_list input_argument_list_
;
962 // All the information read from the command line.
967 typedef Input_arguments::const_iterator const_iterator
;
971 // Process the command line options. This will exit with an
972 // appropriate error message if an unrecognized option is seen.
974 process(int argc
, char** argv
);
976 // Process one command-line option. This takes the index of argv to
977 // process, and returns the index for the next option.
979 process_one_option(int argc
, char** argv
, int i
, bool* no_more_options
);
981 // Handle a -l option.
983 process_l_option(int, char**, char*, bool);
985 // Handle a -R option when it means --rpath.
987 add_to_rpath(const char* arg
)
988 { this->options_
.add_to_rpath(arg
); }
990 // Add a file for which we just read the symbols.
992 add_just_symbols_file(const char* arg
)
994 this->inputs_
.add_file(Input_file_argument(arg
, false, "", true,
995 this->position_options_
));
998 // Handle a --start-group option.
1000 start_group(const char* arg
);
1002 // Handle a --end-group option.
1004 end_group(const char* arg
);
1006 // Get an option argument--a helper function for special processing.
1008 get_special_argument(const char* longname
, int argc
, char** argv
,
1009 const char* arg
, bool long_option
,
1012 // Get the general options.
1013 const General_options
&
1015 { return this->options_
; }
1017 // Get the position dependent options.
1018 const Position_dependent_options
&
1019 position_dependent_options() const
1020 { return this->position_options_
; }
1022 // Get the linker-script options.
1025 { return this->script_options_
; }
1027 // Get the version-script options: a convenience routine.
1028 const Version_script_info
&
1029 version_script() const
1030 { return *this->script_options_
.version_script_info(); }
1032 // The number of input files.
1034 number_of_input_files() const
1035 { return this->inputs_
.size(); }
1037 // Iterators to iterate over the list of input files.
1041 { return this->inputs_
.begin(); }
1045 { return this->inputs_
.end(); }
1048 Command_line(const Command_line
&);
1049 Command_line
& operator=(const Command_line
&);
1051 // Report usage error.
1053 usage() ATTRIBUTE_NORETURN
;
1055 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
1057 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
1059 // Apply a command line option.
1061 apply_option(const gold::options::One_option
&, const char*);
1065 add_file(const char* name
, bool is_lib
);
1067 // Examine the result of processing the command-line, and verify
1068 // the flags do not contradict each other or are otherwise illegal.
1070 normalize_options();
1072 General_options options_
;
1073 Position_dependent_options position_options_
;
1074 Script_options script_options_
;
1075 Input_arguments inputs_
;
1078 } // End namespace gold.
1080 #endif // !defined(GOLD_OPTIONS_H)