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
46 class Input_file_group
;
47 class Position_dependent_options
;
52 class Command_line_options
;
55 struct One_debug_option
;
57 } // End namespace gold::options.
59 // A directory to search. For each directory we record whether it is
60 // in the sysroot. We need to know this so that, if a linker script
61 // is found within the sysroot, we will apply the sysroot to any files
62 // named by that script.
64 class Search_directory
67 // We need a default constructor because we put this in a
70 : name_(NULL
), put_in_sysroot_(false), is_in_sysroot_(false)
73 // This is the usual constructor.
74 Search_directory(const char* name
, bool put_in_sysroot
)
75 : name_(name
), put_in_sysroot_(put_in_sysroot
), is_in_sysroot_(false)
77 if (this->name_
.empty())
81 // This is called if we have a sysroot. The sysroot is prefixed to
82 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
83 // set to true for any enries which are in the sysroot (this will
84 // naturally include any entries for which put_in_sysroot_ is true).
85 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
86 // passing SYSROOT to lrealpath.
88 add_sysroot(const char* sysroot
, const char* canonical_sysroot
);
90 // Get the directory name.
93 { return this->name_
; }
95 // Return whether this directory is in the sysroot.
98 { return this->is_in_sysroot_
; }
102 bool put_in_sysroot_
;
106 // The position independent options which apply to the whole link.
107 // There are a lot of them.
109 class General_options
116 // Straight binary format.
120 General_options(Script_options
*);
122 // -e: set entry address.
125 { return this->script_options_
->entry(); }
127 // -E: export dynamic symbols.
129 export_dynamic() const
130 { return this->export_dynamic_
; }
132 // -h: shared library name.
135 { return this->soname_
; }
137 // -I: dynamic linker name.
139 dynamic_linker() const
140 { return this->dynamic_linker_
; }
142 // -L: Library search path.
143 typedef std::vector
<Search_directory
> Dir_list
;
147 { return this->search_path_
; }
149 // -O: optimization level (0: don't try to optimize output size).
151 optimization_level() const
152 { return this->optimization_level_
; }
154 // -o: Output file name.
156 output_file_name() const
157 { return this->output_file_name_
; }
159 // --oformat: Output format.
162 output_format() const
163 { return this->output_format_
; }
165 // -r: Whether we are doing a relocatable link.
167 is_relocatable() const
168 { return this->is_relocatable_
; }
170 // -s: Strip all symbols.
173 { return this->strip_
== STRIP_ALL
; }
175 // -S: Strip debugging information.
178 { return this->strip_
== STRIP_ALL
|| this->strip_
== STRIP_DEBUG
; }
180 // --strip-debug-gdb: strip only debugging information that's not
181 // used by gdb (at least, for gdb versions <= 6.7).
183 strip_debug_gdb() const
184 { return this->strip_debug() || this->strip_
== STRIP_DEBUG_UNUSED_BY_GDB
; }
186 // --allow-shlib-undefined: do not warn about unresolved symbols in
187 // --shared libraries.
189 allow_shlib_undefined() const
190 { return this->allow_shlib_undefined_
; }
192 // -Bsymbolic: bind defined symbols locally.
195 { return this->symbolic_
; }
197 // --compress-debug-sections: compress .debug_* sections in the
198 // output file using the given compression method. This is useful
199 // when the tools (such as gdb) support compressed sections.
201 compress_debug_sections() const
202 { return this->compress_debug_sections_
!= NO_COMPRESSION
; }
205 zlib_compress_debug_sections() const
206 { return this->compress_debug_sections_
== ZLIB_COMPRESSION
; }
208 // --demangle: demangle C++ symbols in our log messages.
211 { return this->demangle_
; }
213 // --detect-odr-violations: Whether to search for One Defn Rule violations.
215 detect_odr_violations() const
216 { return this->detect_odr_violations_
; }
218 // --eh-frame-hdr: Whether to generate an exception frame header.
220 create_eh_frame_hdr() const
221 { return this->create_eh_frame_hdr_
; }
223 // --rpath: The runtime search path.
226 { return this->rpath_
; }
228 // --rpath-link: The link time search patch for shared libraries.
231 { return this->rpath_link_
; }
233 // --shared: Whether generating a shared object.
236 { return this->is_shared_
; }
238 // --static: Whether doing a static link.
241 { return this->is_static_
; }
243 // --stats: Print resource usage statistics.
246 { return this->print_stats_
; }
248 // --sysroot: The system root of a cross-linker.
251 { return this->sysroot_
; }
253 // --version-script: The version script to apply if --shared is true.
254 const Version_script_info
&
255 version_script() const
256 { return *this->script_options_
->version_script_info(); }
258 // -Ttext: The address of the .text section
260 text_segment_address() const
261 { return this->text_segment_address_
; }
263 // Whether -Ttext was used.
265 user_set_text_segment_address() const
266 { return this->text_segment_address_
!= -1U; }
268 // --threads: Whether to use threads.
271 { return this->threads_
; }
273 // --thread-count-initial: Threads to use in initial pass.
275 thread_count_initial() const
276 { return this->thread_count_initial_
; }
278 // --thread-count-middle: Threads to use in middle pass.
280 thread_count_middle() const
281 { return this->thread_count_middle_
; }
283 // --thread-count-final: Threads to use in final pass.
285 thread_count_final() const
286 { return this->thread_count_final_
; }
288 // -z execstack, -z noexecstack
290 is_execstack_set() const
291 { return this->execstack_
!= EXECSTACK_FROM_INPUT
; }
294 is_stack_executable() const
295 { return this->execstack_
== EXECSTACK_YES
; }
300 { return this->debug_
; }
302 // Return the options which may be set from a linker script.
305 { return this->script_options_
; }
307 const Script_options
*
308 script_options() const
309 { return this->script_options_
; }
312 // Don't copy this structure.
313 General_options(const General_options
&);
314 General_options
& operator=(const General_options
&);
316 friend class Command_line
;
317 friend class options::Command_line_options
;
319 // Which symbols to strip.
322 // Don't strip any symbols.
324 // Strip all symbols.
326 // Strip debugging information.
328 // Strip debugging information that's not used by gdb (at least <= 6.7)
329 STRIP_DEBUG_UNUSED_BY_GDB
332 // Whether to mark the stack as executable.
335 // Not set on command line.
336 EXECSTACK_FROM_INPUT
,
337 // Mark the stack as executable.
339 // Mark the stack as not executable.
343 // What compression method to use
344 enum CompressionMethod
351 set_entry(const char* arg
)
352 { this->script_options_
->set_entry(arg
, strlen(arg
)); }
356 { this->export_dynamic_
= true; }
359 set_soname(const char* arg
)
360 { this->soname_
= arg
; }
363 set_dynamic_linker(const char* arg
)
364 { this->dynamic_linker_
= arg
; }
367 add_to_search_path(const char* arg
)
368 { this->search_path_
.push_back(Search_directory(arg
, false)); }
371 add_to_search_path_with_sysroot(const char* arg
)
372 { this->search_path_
.push_back(Search_directory(arg
, true)); }
375 set_optimization_level(const char* arg
)
378 this->optimization_level_
= strtol(arg
, &endptr
, 0);
379 if (*endptr
!= '\0' || this->optimization_level_
< 0)
380 gold_fatal(_("invalid optimization level: %s"), arg
);
384 set_output_file_name(const char* arg
)
385 { this->output_file_name_
= arg
; }
388 set_output_format(const char*);
392 { this->is_relocatable_
= true; }
396 { this->strip_
= STRIP_ALL
; }
398 // Note: normalize_options() depends on the fact that this turns off
399 // STRIP_ALL if it were already set.
402 { this->strip_
= STRIP_DEBUG
; }
405 set_strip_debug_gdb()
406 { this->strip_
= STRIP_DEBUG_UNUSED_BY_GDB
; }
409 set_allow_shlib_undefined()
410 { this->allow_shlib_undefined_
= true; }
413 set_no_allow_shlib_undefined()
414 { this->allow_shlib_undefined_
= false; }
418 { this->symbolic_
= true; }
420 void set_compress_debug_sections(const char* arg
)
422 if (strcmp(arg
, "none") == 0)
423 this->compress_debug_sections_
= NO_COMPRESSION
;
425 else if (strcmp(arg
, "zlib") == 0)
426 this->compress_debug_sections_
= ZLIB_COMPRESSION
;
429 gold_fatal(_("unsupported argument to --compress-debug-sections: %s"),
434 define_symbol(const char* arg
);
438 { this->demangle_
= true; }
442 { this->demangle_
= false; }
445 set_detect_odr_violations()
446 { this->detect_odr_violations_
= true; }
449 set_create_eh_frame_hdr()
450 { this->create_eh_frame_hdr_
= true; }
453 add_to_rpath(const char* arg
)
454 { this->rpath_
.push_back(Search_directory(arg
, false)); }
457 add_to_rpath_link(const char* arg
)
458 { this->rpath_link_
.push_back(Search_directory(arg
, false)); }
462 { this->is_shared_
= true; }
466 { this->is_static_
= true; }
470 { this->print_stats_
= true; }
473 set_sysroot(const char* arg
)
474 { this->sysroot_
= arg
; }
477 set_text_segment_address(const char* arg
)
480 this->text_segment_address_
= strtoull(arg
, &endptr
, 0);
482 || this->text_segment_address_
== -1U)
483 gold_fatal(_("invalid argument to -Ttext: %s"), arg
);
487 parse_thread_count(const char* arg
)
490 const int count
= strtol(arg
, &endptr
, 0);
491 if (*endptr
!= '\0' || count
< 0)
492 gold_fatal(_("invalid thread count: %s"), arg
);
499 #ifndef ENABLE_THREADS
500 gold_fatal(_("--threads not supported"));
502 this->threads_
= true;
507 { this->threads_
= false; }
510 set_thread_count(const char* arg
)
512 int count
= this->parse_thread_count(arg
);
513 this->thread_count_initial_
= count
;
514 this->thread_count_middle_
= count
;
515 this->thread_count_final_
= count
;
519 set_thread_count_initial(const char* arg
)
520 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
523 set_thread_count_middle(const char* arg
)
524 { this->thread_count_middle_
= this->parse_thread_count(arg
); }
527 set_thread_count_final(const char* arg
)
528 { this->thread_count_final_
= this->parse_thread_count(arg
); }
536 { this->execstack_
= EXECSTACK_YES
; }
540 { this->execstack_
= EXECSTACK_NO
; }
543 set_debug(unsigned int flags
)
544 { this->debug_
= flags
; }
546 // Handle the -z option.
548 handle_z_option(const char*);
550 // Handle the --debug option.
552 handle_debug_option(const char*);
554 // Apply any sysroot to the directory lists.
558 bool export_dynamic_
;
560 const char* dynamic_linker_
;
561 Dir_list search_path_
;
562 int optimization_level_
;
563 const char* output_file_name_
;
564 Object_format output_format_
;
565 bool is_relocatable_
;
567 bool allow_shlib_undefined_
;
569 CompressionMethod compress_debug_sections_
;
571 bool detect_odr_violations_
;
572 bool create_eh_frame_hdr_
;
574 Dir_list rpath_link_
;
578 std::string sysroot_
;
579 uint64_t text_segment_address_
;
581 int thread_count_initial_
;
582 int thread_count_middle_
;
583 int thread_count_final_
;
584 Execstack execstack_
;
586 // Some options can also be set from linker scripts. Those are
588 Script_options
* script_options_
;
591 // The current state of the position dependent options.
593 class Position_dependent_options
596 typedef General_options::Object_format Object_format
;
598 Position_dependent_options();
600 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
601 // -rather than a shared object.
603 do_static_search() const
604 { return this->do_static_search_
; }
606 // --as-needed: Whether to add a DT_NEEDED argument only if the
607 // dynamic object is used.
610 { return this->as_needed_
; }
612 // --whole-archive: Whether to include the entire contents of an
615 include_whole_archive() const
616 { return this->include_whole_archive_
; }
618 // --format: The format of the input file.
621 { return this->input_format_
; }
625 { this->do_static_search_
= true; }
629 { this->do_static_search_
= false; }
633 { this->as_needed_
= true; }
637 { this->as_needed_
= false; }
641 { this->include_whole_archive_
= true; }
644 clear_whole_archive()
645 { this->include_whole_archive_
= false; }
648 set_input_format(const char*);
651 bool do_static_search_
;
653 bool include_whole_archive_
;
654 Object_format input_format_
;
657 // A single file or library argument from the command line.
659 class Input_file_argument
662 // name: file name or library name
663 // is_lib: true if name is a library name: that is, emits the leading
664 // "lib" and trailing ".so"/".a" from the name
665 // extra_search_path: an extra directory to look for the file, prior
666 // to checking the normal library search path. If this is "",
667 // then no extra directory is added.
668 // just_symbols: whether this file only defines symbols.
669 // options: The position dependent options at this point in the
670 // command line, such as --whole-archive.
671 Input_file_argument()
672 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
676 Input_file_argument(const char* name
, bool is_lib
,
677 const char* extra_search_path
,
679 const Position_dependent_options
& options
)
680 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
681 just_symbols_(just_symbols
), options_(options
)
686 { return this->name_
.c_str(); }
688 const Position_dependent_options
&
690 { return this->options_
; }
694 { return this->is_lib_
; }
697 extra_search_path() const
699 return (this->extra_search_path_
.empty()
701 : this->extra_search_path_
.c_str());
704 // Return whether we should only read symbols from this file.
707 { return this->just_symbols_
; }
709 // Return whether this file may require a search using the -L
712 may_need_search() const
713 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
716 // We use std::string, not const char*, here for convenience when
717 // using script files, so that we do not have to preserve the string
721 std::string extra_search_path_
;
723 Position_dependent_options options_
;
726 // A file or library, or a group, from the command line.
731 // Create a file or library argument.
732 explicit Input_argument(Input_file_argument file
)
733 : is_file_(true), file_(file
), group_(NULL
)
736 // Create a group argument.
737 explicit Input_argument(Input_file_group
* group
)
738 : is_file_(false), group_(group
)
741 // Return whether this is a file.
744 { return this->is_file_
; }
746 // Return whether this is a group.
749 { return !this->is_file_
; }
751 // Return the information about the file.
752 const Input_file_argument
&
755 gold_assert(this->is_file_
);
759 // Return the information about the group.
760 const Input_file_group
*
763 gold_assert(!this->is_file_
);
770 gold_assert(!this->is_file_
);
776 Input_file_argument file_
;
777 Input_file_group
* group_
;
780 // A group from the command line. This is a set of arguments within
781 // --start-group ... --end-group.
783 class Input_file_group
786 typedef std::vector
<Input_argument
> Files
;
787 typedef Files::const_iterator const_iterator
;
793 // Add a file to the end of the group.
795 add_file(const Input_file_argument
& arg
)
796 { this->files_
.push_back(Input_argument(arg
)); }
798 // Iterators to iterate over the group contents.
802 { return this->files_
.begin(); }
806 { return this->files_
.end(); }
812 // A list of files from the command line or a script.
814 class Input_arguments
817 typedef std::vector
<Input_argument
> Input_argument_list
;
818 typedef Input_argument_list::const_iterator const_iterator
;
821 : input_argument_list_(), in_group_(false)
826 add_file(const Input_file_argument
& arg
);
828 // Start a group (the --start-group option).
832 // End a group (the --end-group option).
836 // Return whether we are currently in a group.
839 { return this->in_group_
; }
841 // The number of entries in the list.
844 { return this->input_argument_list_
.size(); }
846 // Iterators to iterate over the list of input files.
850 { return this->input_argument_list_
.begin(); }
854 { return this->input_argument_list_
.end(); }
856 // Return whether the list is empty.
859 { return this->input_argument_list_
.empty(); }
862 Input_argument_list input_argument_list_
;
866 // All the information read from the command line.
871 typedef Input_arguments::const_iterator const_iterator
;
873 Command_line(Script_options
*);
875 // Process the command line options. This will exit with an
876 // appropriate error message if an unrecognized option is seen.
878 process(int argc
, char** argv
);
880 // Process one command-line option. This takes the index of argv to
881 // process, and returns the index for the next option.
883 process_one_option(int argc
, char** argv
, int i
, bool* no_more_options
);
885 // Handle a -l option.
887 process_l_option(int, char**, char*, bool);
889 // Handle a -R option when it means --rpath.
891 add_to_rpath(const char* arg
)
892 { this->options_
.add_to_rpath(arg
); }
894 // Add a file for which we just read the symbols.
896 add_just_symbols_file(const char* arg
)
898 this->inputs_
.add_file(Input_file_argument(arg
, false, "", true,
899 this->position_options_
));
902 // Handle a --start-group option.
904 start_group(const char* arg
);
906 // Handle a --end-group option.
908 end_group(const char* arg
);
910 // Get an option argument--a helper function for special processing.
912 get_special_argument(const char* longname
, int argc
, char** argv
,
913 const char* arg
, bool long_option
,
916 // Get the general options.
917 const General_options
&
919 { return this->options_
; }
921 // Get the position dependent options.
922 const Position_dependent_options
&
923 position_dependent_options() const
924 { return this->position_options_
; }
926 // Get the options which may be set from a linker script.
929 { return this->options_
.script_options(); }
931 const Script_options
*
932 script_options() const
933 { return this->options_
.script_options(); }
935 // The number of input files.
937 number_of_input_files() const
938 { return this->inputs_
.size(); }
940 // Iterators to iterate over the list of input files.
944 { return this->inputs_
.begin(); }
948 { return this->inputs_
.end(); }
951 Command_line(const Command_line
&);
952 Command_line
& operator=(const Command_line
&);
954 // Report usage error.
956 usage() ATTRIBUTE_NORETURN
;
958 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
960 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
962 // Apply a command line option.
964 apply_option(const gold::options::One_option
&, const char*);
968 add_file(const char* name
, bool is_lib
);
970 // Examine the result of processing the command-line, and verify
971 // the flags do not contradict each other or are otherwise illegal.
975 General_options options_
;
976 Position_dependent_options position_options_
;
977 Input_arguments inputs_
;
980 } // End namespace gold.
982 #endif // !defined(GOLD_OPTIONS_H)