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 // -e: set entry address.
127 { return this->entry_
; }
129 // -E: export dynamic symbols.
131 export_dynamic() const
132 { return this->export_dynamic_
; }
134 // -h: shared library name.
137 { return this->soname_
; }
139 // -I: dynamic linker name.
141 dynamic_linker() const
142 { return this->dynamic_linker_
; }
144 // -L: Library search path.
145 typedef std::vector
<Search_directory
> Dir_list
;
149 { return this->search_path_
; }
151 // -O: optimization level (0: don't try to optimize output size).
154 { return this->optimization_level_
; }
156 // -o: Output file name.
158 output_file_name() const
159 { return this->output_file_name_
; }
161 // --oformat: Output format.
164 { return this->oformat_
; }
167 oformat_string() const
168 { return this->oformat_string_
; }
170 // Return the default target.
172 default_target() const;
174 // -q: Whether to emit relocations.
177 { return this->emit_relocs_
; }
179 // -r: Whether we are doing a relocatable link.
182 { return this->is_relocatable_
; }
184 // -s: Strip all symbols.
187 { return this->strip_
== STRIP_ALL
; }
189 // -S: Strip debugging information.
192 { return this->strip_
== STRIP_ALL
|| this->strip_
== STRIP_DEBUG
; }
194 // --strip-debug-gdb: strip only debugging information that's not
195 // used by gdb (at least, for gdb versions <= 6.7).
197 strip_debug_gdb() const
198 { return this->strip_debug() || this->strip_
== STRIP_DEBUG_UNUSED_BY_GDB
; }
200 // --allow-shlib-undefined: do not warn about unresolved symbols in
201 // --shared libraries.
203 allow_shlib_undefined() const
204 { return this->allow_shlib_undefined_
; }
206 // -Bsymbolic: bind defined symbols locally.
209 { return this->symbolic_
; }
211 // --compress-debug-sections: compress .debug_* sections in the
212 // output file using the given compression method. This is useful
213 // when the tools (such as gdb) support compressed sections.
215 compress_debug_sections() const
216 { return this->compress_debug_sections_
!= NO_COMPRESSION
; }
219 zlib_compress_debug_sections() const
220 { return this->compress_debug_sections_
== ZLIB_COMPRESSION
; }
222 // --demangle: demangle C++ symbols in our log messages.
225 { return this->demangle_
; }
227 // --detect-odr-violations: Whether to search for One Defn Rule violations.
229 detect_odr_violations() const
230 { return this->detect_odr_violations_
; }
232 // --eh-frame-hdr: Whether to generate an exception frame header.
235 { return this->create_eh_frame_hdr_
; }
237 // --rpath: The runtime search path.
240 { return this->rpath_
; }
242 // --rpath-link: The link time search patch for shared libraries.
245 { return this->rpath_link_
; }
247 // --shared: Whether generating a shared object.
250 { return this->is_shared_
; }
252 // This is not defined via a flag, but combines flags to say whether
253 // the output is position-independent or not.
255 output_is_position_independent() const
256 { return this->shared(); }
258 // --static: Whether doing a static link.
261 { return this->is_static_
; }
263 // --stats: Print resource usage statistics.
266 { return this->print_stats_
; }
268 // --sysroot: The system root of a cross-linker.
271 { return this->sysroot_
; }
273 // -Tbss: The address of the BSS segment
276 { return this->bss_segment_address_
; }
278 // Whether -Tbss was used.
280 user_set_Tbss() const
281 { return this->bss_segment_address_
!= -1U; }
283 // -Tdata: The address of the data segment
286 { return this->data_segment_address_
; }
288 // Whether -Tdata was used.
290 user_set_Tdata() const
291 { return this->data_segment_address_
!= -1U; }
293 // -Ttext: The address of the .text section
296 { return this->text_segment_address_
; }
298 // Whether -Ttext was used.
300 user_set_Ttext() const
301 { return this->text_segment_address_
!= -1U; }
303 // --threads: Whether to use threads.
306 { return this->threads_
; }
308 // --thread-count-initial: Threads to use in initial pass.
310 thread_count_initial() const
311 { return this->thread_count_initial_
; }
313 // --thread-count-middle: Threads to use in middle pass.
315 thread_count_middle() const
316 { return this->thread_count_middle_
; }
318 // --thread-count-final: Threads to use in final pass.
320 thread_count_final() const
321 { return this->thread_count_final_
; }
323 // -z execstack, -z noexecstack
325 is_execstack_set() const
326 { return this->execstack_
!= EXECSTACK_FROM_INPUT
; }
329 is_stack_executable() const
330 { return this->execstack_
== EXECSTACK_YES
; }
334 max_page_size() const
335 { return this->max_page_size_
; }
337 // -z common-page-size
339 common_page_size() const
340 { return this->common_page_size_
; }
345 { return this->debug_
; }
348 // Don't copy this structure.
349 General_options(const General_options
&);
350 General_options
& operator=(const General_options
&);
352 friend class Command_line
;
353 friend class options::Command_line_options
;
355 // Which symbols to strip.
358 // Don't strip any symbols.
360 // Strip all symbols.
362 // Strip debugging information.
364 // Strip debugging information that's not used by gdb (at least <= 6.7)
365 STRIP_DEBUG_UNUSED_BY_GDB
368 // Whether to mark the stack as executable.
371 // Not set on command line.
372 EXECSTACK_FROM_INPUT
,
373 // Mark the stack as executable.
375 // Mark the stack as not executable.
379 // What compression method to use
380 enum CompressionMethod
387 set_entry(const char* arg
)
388 { this->entry_
= arg
; }
391 set_export_dynamic(bool value
)
392 { this->export_dynamic_
= value
; }
395 set_soname(const char* arg
)
396 { this->soname_
= arg
; }
399 set_dynamic_linker(const char* arg
)
400 { this->dynamic_linker_
= arg
; }
403 add_to_search_path(const char* arg
)
404 { this->search_path_
.push_back(Search_directory(arg
, false)); }
407 add_to_search_path_with_sysroot(const char* arg
)
408 { this->search_path_
.push_back(Search_directory(arg
, true)); }
411 set_optimize(const char* arg
)
414 this->optimization_level_
= strtol(arg
, &endptr
, 0);
415 if (*endptr
!= '\0' || this->optimization_level_
< 0)
416 gold_fatal(_("invalid optimization level: %s"), arg
);
420 set_output(const char* arg
)
421 { this->output_file_name_
= arg
; }
424 set_oformat(const char*);
427 set_emit_relocs(bool value
)
428 { this->emit_relocs_
= value
; }
431 set_relocatable(bool value
)
432 { this->is_relocatable_
= value
; }
436 { this->strip_
= STRIP_ALL
; }
438 // Note: normalize_options() depends on the fact that this turns off
439 // STRIP_ALL if it were already set.
441 set_strip_debug(bool)
442 { this->strip_
= STRIP_DEBUG
; }
445 set_strip_debug_gdb(bool)
446 { this->strip_
= STRIP_DEBUG_UNUSED_BY_GDB
; }
449 set_allow_shlib_undefined(bool value
)
450 { this->allow_shlib_undefined_
= value
; }
453 set_no_allow_shlib_undefined(bool value
)
454 { this->set_allow_shlib_undefined(!value
); }
457 set_Bsymbolic(bool value
)
458 { this->symbolic_
= value
; }
460 void set_compress_debug_sections(const char* arg
)
462 if (strcmp(arg
, "none") == 0)
463 this->compress_debug_sections_
= NO_COMPRESSION
;
465 else if (strcmp(arg
, "zlib") == 0)
466 this->compress_debug_sections_
= ZLIB_COMPRESSION
;
469 gold_fatal(_("unsupported argument to --compress-debug-sections: %s"),
474 add_to_defsym(const char* arg
);
477 set_demangle(bool value
)
478 { this->demangle_
= value
; }
481 set_no_demangle(bool value
)
482 { this->set_demangle(!value
); }
485 set_detect_odr_violations(bool value
)
486 { this->detect_odr_violations_
= value
; }
489 set_eh_frame_hdr(bool value
)
490 { this->create_eh_frame_hdr_
= value
; }
493 add_to_rpath(const char* arg
)
494 { this->rpath_
.push_back(Search_directory(arg
, false)); }
497 add_to_rpath_link(const char* arg
)
498 { this->rpath_link_
.push_back(Search_directory(arg
, false)); }
501 set_shared(bool value
)
502 { this->is_shared_
= value
; }
505 set_static(bool value
)
506 { this->is_static_
= value
; }
509 set_stats(bool value
)
510 { this->print_stats_
= value
; }
513 set_sysroot(const char* arg
)
514 { this->sysroot_
= arg
; }
517 set_segment_address(const char* name
, const char* arg
, uint64_t* val
)
520 *val
= strtoull(arg
, &endptr
, 0);
521 if (*endptr
!= '\0' || *val
== -1U)
522 gold_fatal(_("invalid argument to %s: %s"), name
, arg
);
526 set_Tbss(const char* arg
)
527 { this->set_segment_address("-Tbss", arg
, &this->bss_segment_address_
); }
530 set_Tdata(const char* arg
)
531 { this->set_segment_address("-Tdata", arg
, &this->data_segment_address_
); }
534 set_Ttext(const char* arg
)
535 { this->set_segment_address("-Ttext", arg
, &this->text_segment_address_
); }
538 parse_thread_count(const char* arg
)
541 const int count
= strtol(arg
, &endptr
, 0);
542 if (*endptr
!= '\0' || count
< 0)
543 gold_fatal(_("invalid thread count: %s"), arg
);
548 set_threads(bool value
)
550 #ifndef ENABLE_THREADS
552 gold_fatal(_("--threads not supported"));
554 this->threads_
= value
;
558 set_no_threads(bool value
)
559 { this->set_threads(!value
); }
562 set_thread_count(const char* arg
)
564 int count
= this->parse_thread_count(arg
);
565 this->thread_count_initial_
= count
;
566 this->thread_count_middle_
= count
;
567 this->thread_count_final_
= count
;
571 set_thread_count_initial(const char* arg
)
572 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
575 set_thread_count_middle(const char* arg
)
576 { this->thread_count_middle_
= this->parse_thread_count(arg
); }
579 set_thread_count_final(const char* arg
)
580 { this->thread_count_final_
= this->parse_thread_count(arg
); }
588 { this->execstack_
= EXECSTACK_YES
; }
591 set_noexecstack(bool)
592 { this->execstack_
= EXECSTACK_NO
; }
595 set_max_page_size(const char* arg
)
598 this->max_page_size_
= strtoull(arg
, &endptr
, 0);
599 if (*endptr
!= '\0' || this->max_page_size_
== 0)
600 gold_fatal(_("invalid max-page-size: %s"), arg
);
604 set_common_page_size(const char* arg
)
607 this->common_page_size_
= strtoull(arg
, &endptr
, 0);
608 if (*endptr
!= '\0' || this->common_page_size_
== 0)
609 gold_fatal(_("invalid common-page-size: %s"), arg
);
613 set_debug(unsigned int flags
)
614 { this->debug_
= flags
; }
616 // Handle the -z option.
618 handle_z_option(const char*);
620 // Handle the --debug option.
622 handle_debug_option(const char*);
624 // Apply any sysroot to the directory lists.
629 bool export_dynamic_
;
631 const char* dynamic_linker_
;
632 Dir_list search_path_
;
633 int optimization_level_
;
634 const char* output_file_name_
;
635 Object_format oformat_
;
636 const char* oformat_string_
;
638 bool is_relocatable_
;
640 bool allow_shlib_undefined_
;
642 CompressionMethod compress_debug_sections_
;
644 bool detect_odr_violations_
;
645 bool create_eh_frame_hdr_
;
647 Dir_list rpath_link_
;
651 std::string sysroot_
;
652 uint64_t bss_segment_address_
;
653 uint64_t data_segment_address_
;
654 uint64_t text_segment_address_
;
656 int thread_count_initial_
;
657 int thread_count_middle_
;
658 int thread_count_final_
;
659 Execstack execstack_
;
660 uint64_t max_page_size_
;
661 uint64_t common_page_size_
;
665 // The current state of the position dependent options.
667 class Position_dependent_options
670 typedef General_options::Object_format Object_format
;
672 Position_dependent_options();
674 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
675 // -rather than a shared object.
678 { return this->do_static_search_
; }
680 // --as-needed: Whether to add a DT_NEEDED argument only if the
681 // dynamic object is used.
684 { return this->as_needed_
; }
686 // --whole-archive: Whether to include the entire contents of an
689 whole_archive() const
690 { return this->include_whole_archive_
; }
692 // --format: The format of the input file.
695 { return this->input_format_
; }
698 set_Bstatic(bool value
)
699 { this->do_static_search_
= value
; }
702 set_Bdynamic(bool value
)
703 { this->set_Bstatic(!value
); }
706 set_as_needed(bool value
)
707 { this->as_needed_
= value
; }
710 set_no_as_needed(bool value
)
711 { this->set_as_needed(!value
); }
714 set_whole_archive(bool value
)
715 { this->include_whole_archive_
= value
; }
718 set_no_whole_archive(bool value
)
719 { this->set_whole_archive(!value
); }
722 set_format(const char*);
725 bool do_static_search_
;
727 bool include_whole_archive_
;
728 Object_format input_format_
;
731 // A single file or library argument from the command line.
733 class Input_file_argument
736 // name: file name or library name
737 // is_lib: true if name is a library name: that is, emits the leading
738 // "lib" and trailing ".so"/".a" from the name
739 // extra_search_path: an extra directory to look for the file, prior
740 // to checking the normal library search path. If this is "",
741 // then no extra directory is added.
742 // just_symbols: whether this file only defines symbols.
743 // options: The position dependent options at this point in the
744 // command line, such as --whole-archive.
745 Input_file_argument()
746 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
750 Input_file_argument(const char* name
, bool is_lib
,
751 const char* extra_search_path
,
753 const Position_dependent_options
& options
)
754 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
755 just_symbols_(just_symbols
), options_(options
)
760 { return this->name_
.c_str(); }
762 const Position_dependent_options
&
764 { return this->options_
; }
768 { return this->is_lib_
; }
771 extra_search_path() const
773 return (this->extra_search_path_
.empty()
775 : this->extra_search_path_
.c_str());
778 // Return whether we should only read symbols from this file.
781 { return this->just_symbols_
; }
783 // Return whether this file may require a search using the -L
786 may_need_search() const
787 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
790 // We use std::string, not const char*, here for convenience when
791 // using script files, so that we do not have to preserve the string
795 std::string extra_search_path_
;
797 Position_dependent_options options_
;
800 // A file or library, or a group, from the command line.
805 // Create a file or library argument.
806 explicit Input_argument(Input_file_argument file
)
807 : is_file_(true), file_(file
), group_(NULL
)
810 // Create a group argument.
811 explicit Input_argument(Input_file_group
* group
)
812 : is_file_(false), group_(group
)
815 // Return whether this is a file.
818 { return this->is_file_
; }
820 // Return whether this is a group.
823 { return !this->is_file_
; }
825 // Return the information about the file.
826 const Input_file_argument
&
829 gold_assert(this->is_file_
);
833 // Return the information about the group.
834 const Input_file_group
*
837 gold_assert(!this->is_file_
);
844 gold_assert(!this->is_file_
);
850 Input_file_argument file_
;
851 Input_file_group
* group_
;
854 // A group from the command line. This is a set of arguments within
855 // --start-group ... --end-group.
857 class Input_file_group
860 typedef std::vector
<Input_argument
> Files
;
861 typedef Files::const_iterator const_iterator
;
867 // Add a file to the end of the group.
869 add_file(const Input_file_argument
& arg
)
870 { this->files_
.push_back(Input_argument(arg
)); }
872 // Iterators to iterate over the group contents.
876 { return this->files_
.begin(); }
880 { return this->files_
.end(); }
886 // A list of files from the command line or a script.
888 class Input_arguments
891 typedef std::vector
<Input_argument
> Input_argument_list
;
892 typedef Input_argument_list::const_iterator const_iterator
;
895 : input_argument_list_(), in_group_(false)
900 add_file(const Input_file_argument
& arg
);
902 // Start a group (the --start-group option).
906 // End a group (the --end-group option).
910 // Return whether we are currently in a group.
913 { return this->in_group_
; }
915 // The number of entries in the list.
918 { return this->input_argument_list_
.size(); }
920 // Iterators to iterate over the list of input files.
924 { return this->input_argument_list_
.begin(); }
928 { return this->input_argument_list_
.end(); }
930 // Return whether the list is empty.
933 { return this->input_argument_list_
.empty(); }
936 Input_argument_list input_argument_list_
;
940 // All the information read from the command line.
945 typedef Input_arguments::const_iterator const_iterator
;
949 // Process the command line options. This will exit with an
950 // appropriate error message if an unrecognized option is seen.
952 process(int argc
, char** argv
);
954 // Process one command-line option. This takes the index of argv to
955 // process, and returns the index for the next option.
957 process_one_option(int argc
, char** argv
, int i
, bool* no_more_options
);
959 // Handle a -l option.
961 process_l_option(int, char**, char*, bool);
963 // Handle a -R option when it means --rpath.
965 add_to_rpath(const char* arg
)
966 { this->options_
.add_to_rpath(arg
); }
968 // Add a file for which we just read the symbols.
970 add_just_symbols_file(const char* arg
)
972 this->inputs_
.add_file(Input_file_argument(arg
, false, "", true,
973 this->position_options_
));
976 // Handle a --start-group option.
978 start_group(const char* arg
);
980 // Handle a --end-group option.
982 end_group(const char* arg
);
984 // Get an option argument--a helper function for special processing.
986 get_special_argument(const char* longname
, int argc
, char** argv
,
987 const char* arg
, bool long_option
,
990 // Get the general options.
991 const General_options
&
993 { return this->options_
; }
995 // Get the position dependent options.
996 const Position_dependent_options
&
997 position_dependent_options() const
998 { return this->position_options_
; }
1000 // Get the linker-script options.
1003 { return this->script_options_
; }
1005 // Get the version-script options: a convenience routine.
1006 const Version_script_info
&
1007 version_script() const
1008 { return *this->script_options_
.version_script_info(); }
1010 // The number of input files.
1012 number_of_input_files() const
1013 { return this->inputs_
.size(); }
1015 // Iterators to iterate over the list of input files.
1019 { return this->inputs_
.begin(); }
1023 { return this->inputs_
.end(); }
1026 Command_line(const Command_line
&);
1027 Command_line
& operator=(const Command_line
&);
1029 // Report usage error.
1031 usage() ATTRIBUTE_NORETURN
;
1033 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
1035 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
1037 // Apply a command line option.
1039 apply_option(const gold::options::One_option
&, const char*);
1043 add_file(const char* name
, bool is_lib
);
1045 // Examine the result of processing the command-line, and verify
1046 // the flags do not contradict each other or are otherwise illegal.
1048 normalize_options();
1050 General_options options_
;
1051 Position_dependent_options position_options_
;
1052 Script_options script_options_
;
1053 Input_arguments inputs_
;
1056 } // End namespace gold.
1058 #endif // !defined(GOLD_OPTIONS_H)