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.
122 General_options(Script_options
*);
124 // -e: set entry address.
127 { return this->script_options_
->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_
; }
166 // Return the default target.
168 default_target() const;
170 // -r: Whether we are doing a relocatable link.
173 { return this->is_relocatable_
; }
175 // -s: Strip all symbols.
178 { return this->strip_
== STRIP_ALL
; }
180 // -S: Strip debugging information.
183 { return this->strip_
== STRIP_ALL
|| this->strip_
== STRIP_DEBUG
; }
185 // --strip-debug-gdb: strip only debugging information that's not
186 // used by gdb (at least, for gdb versions <= 6.7).
188 strip_debug_gdb() const
189 { return this->strip_debug() || this->strip_
== STRIP_DEBUG_UNUSED_BY_GDB
; }
191 // --allow-shlib-undefined: do not warn about unresolved symbols in
192 // --shared libraries.
194 allow_shlib_undefined() const
195 { return this->allow_shlib_undefined_
; }
197 // -Bsymbolic: bind defined symbols locally.
200 { return this->symbolic_
; }
202 // --compress-debug-sections: compress .debug_* sections in the
203 // output file using the given compression method. This is useful
204 // when the tools (such as gdb) support compressed sections.
206 compress_debug_sections() const
207 { return this->compress_debug_sections_
!= NO_COMPRESSION
; }
210 zlib_compress_debug_sections() const
211 { return this->compress_debug_sections_
== ZLIB_COMPRESSION
; }
213 // --demangle: demangle C++ symbols in our log messages.
216 { return this->demangle_
; }
218 // --detect-odr-violations: Whether to search for One Defn Rule violations.
220 detect_odr_violations() const
221 { return this->detect_odr_violations_
; }
223 // --eh-frame-hdr: Whether to generate an exception frame header.
226 { return this->create_eh_frame_hdr_
; }
228 // --rpath: The runtime search path.
231 { return this->rpath_
; }
233 // --rpath-link: The link time search patch for shared libraries.
236 { return this->rpath_link_
; }
238 // --shared: Whether generating a shared object.
241 { return this->is_shared_
; }
243 // --static: Whether doing a static link.
246 { return this->is_static_
; }
248 // --stats: Print resource usage statistics.
251 { return this->print_stats_
; }
253 // --sysroot: The system root of a cross-linker.
256 { return this->sysroot_
; }
258 // --version-script: The version script to apply if --shared is true.
259 const Version_script_info
&
260 version_script() const
261 { return *this->script_options_
->version_script_info(); }
263 // -Tbss: The address of the BSS segment
266 { return this->bss_segment_address_
; }
268 // Whether -Tbss was used.
270 user_set_Tbss() const
271 { return this->bss_segment_address_
!= -1U; }
273 // -Tdata: The address of the data segment
276 { return this->data_segment_address_
; }
278 // Whether -Tdata was used.
280 user_set_Tdata() const
281 { return this->data_segment_address_
!= -1U; }
283 // -Ttext: The address of the .text section
286 { return this->text_segment_address_
; }
288 // Whether -Ttext was used.
290 user_set_Ttext() const
291 { return this->text_segment_address_
!= -1U; }
293 // --threads: Whether to use threads.
296 { return this->threads_
; }
298 // --thread-count-initial: Threads to use in initial pass.
300 thread_count_initial() const
301 { return this->thread_count_initial_
; }
303 // --thread-count-middle: Threads to use in middle pass.
305 thread_count_middle() const
306 { return this->thread_count_middle_
; }
308 // --thread-count-final: Threads to use in final pass.
310 thread_count_final() const
311 { return this->thread_count_final_
; }
313 // -z execstack, -z noexecstack
315 is_execstack_set() const
316 { return this->execstack_
!= EXECSTACK_FROM_INPUT
; }
319 is_stack_executable() const
320 { return this->execstack_
== EXECSTACK_YES
; }
325 { return this->debug_
; }
327 // Return the options which may be set from a linker script.
330 { return this->script_options_
; }
332 const Script_options
*
333 script_options() const
334 { return this->script_options_
; }
337 // Don't copy this structure.
338 General_options(const General_options
&);
339 General_options
& operator=(const General_options
&);
341 friend class Command_line
;
342 friend class options::Command_line_options
;
344 // Which symbols to strip.
347 // Don't strip any symbols.
349 // Strip all symbols.
351 // Strip debugging information.
353 // Strip debugging information that's not used by gdb (at least <= 6.7)
354 STRIP_DEBUG_UNUSED_BY_GDB
357 // Whether to mark the stack as executable.
360 // Not set on command line.
361 EXECSTACK_FROM_INPUT
,
362 // Mark the stack as executable.
364 // Mark the stack as not executable.
368 // What compression method to use
369 enum CompressionMethod
376 set_entry(const char* arg
)
377 { this->script_options_
->set_entry(arg
, strlen(arg
)); }
380 set_export_dynamic(bool value
)
381 { this->export_dynamic_
= value
; }
384 set_soname(const char* arg
)
385 { this->soname_
= arg
; }
388 set_dynamic_linker(const char* arg
)
389 { this->dynamic_linker_
= arg
; }
392 add_to_search_path(const char* arg
)
393 { this->search_path_
.push_back(Search_directory(arg
, false)); }
396 add_to_search_path_with_sysroot(const char* arg
)
397 { this->search_path_
.push_back(Search_directory(arg
, true)); }
400 set_optimize(const char* arg
)
403 this->optimization_level_
= strtol(arg
, &endptr
, 0);
404 if (*endptr
!= '\0' || this->optimization_level_
< 0)
405 gold_fatal(_("invalid optimization level: %s"), arg
);
409 set_output(const char* arg
)
410 { this->output_file_name_
= arg
; }
413 set_oformat(const char*);
416 set_relocatable(bool value
)
417 { this->is_relocatable_
= value
; }
421 { this->strip_
= STRIP_ALL
; }
423 // Note: normalize_options() depends on the fact that this turns off
424 // STRIP_ALL if it were already set.
426 set_strip_debug(bool)
427 { this->strip_
= STRIP_DEBUG
; }
430 set_strip_debug_gdb(bool)
431 { this->strip_
= STRIP_DEBUG_UNUSED_BY_GDB
; }
434 set_allow_shlib_undefined(bool value
)
435 { this->allow_shlib_undefined_
= value
; }
438 set_no_allow_shlib_undefined(bool value
)
439 { this->set_allow_shlib_undefined(!value
); }
442 set_Bsymbolic(bool value
)
443 { this->symbolic_
= value
; }
445 void set_compress_debug_sections(const char* arg
)
447 if (strcmp(arg
, "none") == 0)
448 this->compress_debug_sections_
= NO_COMPRESSION
;
450 else if (strcmp(arg
, "zlib") == 0)
451 this->compress_debug_sections_
= ZLIB_COMPRESSION
;
454 gold_fatal(_("unsupported argument to --compress-debug-sections: %s"),
459 add_to_defsym(const char* arg
);
462 set_demangle(bool value
)
463 { this->demangle_
= value
; }
466 set_no_demangle(bool value
)
467 { this->set_demangle(!value
); }
470 set_detect_odr_violations(bool value
)
471 { this->detect_odr_violations_
= value
; }
474 set_eh_frame_hdr(bool value
)
475 { this->create_eh_frame_hdr_
= value
; }
478 add_to_rpath(const char* arg
)
479 { this->rpath_
.push_back(Search_directory(arg
, false)); }
482 add_to_rpath_link(const char* arg
)
483 { this->rpath_link_
.push_back(Search_directory(arg
, false)); }
486 set_shared(bool value
)
487 { this->is_shared_
= value
; }
490 set_static(bool value
)
491 { this->is_static_
= value
; }
494 set_stats(bool value
)
495 { this->print_stats_
= value
; }
498 set_sysroot(const char* arg
)
499 { this->sysroot_
= arg
; }
502 set_segment_address(const char* name
, const char* arg
, uint64_t* val
)
505 *val
= strtoull(arg
, &endptr
, 0);
506 if (*endptr
!= '\0' || *val
== -1U)
507 gold_fatal(_("invalid argument to %s: %s"), name
, arg
);
511 set_Tbss(const char* arg
)
512 { this->set_segment_address("-Tbss", arg
, &this->bss_segment_address_
); }
515 set_Tdata(const char* arg
)
516 { this->set_segment_address("-Tdata", arg
, &this->data_segment_address_
); }
519 set_Ttext(const char* arg
)
520 { this->set_segment_address("-Ttext", arg
, &this->text_segment_address_
); }
523 parse_thread_count(const char* arg
)
526 const int count
= strtol(arg
, &endptr
, 0);
527 if (*endptr
!= '\0' || count
< 0)
528 gold_fatal(_("invalid thread count: %s"), arg
);
533 set_threads(bool value
)
535 #ifndef ENABLE_THREADS
537 gold_fatal(_("--threads not supported"));
539 this->threads_
= value
;
543 set_no_threads(bool value
)
544 { this->set_threads(!value
); }
547 set_thread_count(const char* arg
)
549 int count
= this->parse_thread_count(arg
);
550 this->thread_count_initial_
= count
;
551 this->thread_count_middle_
= count
;
552 this->thread_count_final_
= count
;
556 set_thread_count_initial(const char* arg
)
557 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
560 set_thread_count_middle(const char* arg
)
561 { this->thread_count_middle_
= this->parse_thread_count(arg
); }
564 set_thread_count_final(const char* arg
)
565 { this->thread_count_final_
= this->parse_thread_count(arg
); }
573 { this->execstack_
= EXECSTACK_YES
; }
576 set_noexecstack(bool)
577 { this->execstack_
= EXECSTACK_NO
; }
580 set_debug(unsigned int flags
)
581 { this->debug_
= flags
; }
583 // Handle the -z option.
585 handle_z_option(const char*);
587 // Handle the --debug option.
589 handle_debug_option(const char*);
591 // Apply any sysroot to the directory lists.
595 bool export_dynamic_
;
597 const char* dynamic_linker_
;
598 Dir_list search_path_
;
599 int optimization_level_
;
600 const char* output_file_name_
;
601 Object_format oformat_
;
602 const char* oformat_string_
;
603 bool is_relocatable_
;
605 bool allow_shlib_undefined_
;
607 CompressionMethod compress_debug_sections_
;
609 bool detect_odr_violations_
;
610 bool create_eh_frame_hdr_
;
612 Dir_list rpath_link_
;
616 std::string sysroot_
;
617 uint64_t bss_segment_address_
;
618 uint64_t data_segment_address_
;
619 uint64_t text_segment_address_
;
621 int thread_count_initial_
;
622 int thread_count_middle_
;
623 int thread_count_final_
;
624 Execstack execstack_
;
626 // Some options can also be set from linker scripts. Those are
628 Script_options
* script_options_
;
631 // The current state of the position dependent options.
633 class Position_dependent_options
636 typedef General_options::Object_format Object_format
;
638 Position_dependent_options();
640 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
641 // -rather than a shared object.
644 { return this->do_static_search_
; }
646 // --as-needed: Whether to add a DT_NEEDED argument only if the
647 // dynamic object is used.
650 { return this->as_needed_
; }
652 // --whole-archive: Whether to include the entire contents of an
655 whole_archive() const
656 { return this->include_whole_archive_
; }
658 // --format: The format of the input file.
661 { return this->input_format_
; }
664 set_Bstatic(bool value
)
665 { this->do_static_search_
= value
; }
668 set_Bdynamic(bool value
)
669 { this->set_Bstatic(!value
); }
672 set_as_needed(bool value
)
673 { this->as_needed_
= value
; }
676 set_no_as_needed(bool value
)
677 { this->set_as_needed(!value
); }
680 set_whole_archive(bool value
)
681 { this->include_whole_archive_
= value
; }
684 set_no_whole_archive(bool value
)
685 { this->set_whole_archive(!value
); }
688 set_format(const char*);
691 bool do_static_search_
;
693 bool include_whole_archive_
;
694 Object_format input_format_
;
697 // A single file or library argument from the command line.
699 class Input_file_argument
702 // name: file name or library name
703 // is_lib: true if name is a library name: that is, emits the leading
704 // "lib" and trailing ".so"/".a" from the name
705 // extra_search_path: an extra directory to look for the file, prior
706 // to checking the normal library search path. If this is "",
707 // then no extra directory is added.
708 // just_symbols: whether this file only defines symbols.
709 // options: The position dependent options at this point in the
710 // command line, such as --whole-archive.
711 Input_file_argument()
712 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
716 Input_file_argument(const char* name
, bool is_lib
,
717 const char* extra_search_path
,
719 const Position_dependent_options
& options
)
720 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
721 just_symbols_(just_symbols
), options_(options
)
726 { return this->name_
.c_str(); }
728 const Position_dependent_options
&
730 { return this->options_
; }
734 { return this->is_lib_
; }
737 extra_search_path() const
739 return (this->extra_search_path_
.empty()
741 : this->extra_search_path_
.c_str());
744 // Return whether we should only read symbols from this file.
747 { return this->just_symbols_
; }
749 // Return whether this file may require a search using the -L
752 may_need_search() const
753 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
756 // We use std::string, not const char*, here for convenience when
757 // using script files, so that we do not have to preserve the string
761 std::string extra_search_path_
;
763 Position_dependent_options options_
;
766 // A file or library, or a group, from the command line.
771 // Create a file or library argument.
772 explicit Input_argument(Input_file_argument file
)
773 : is_file_(true), file_(file
), group_(NULL
)
776 // Create a group argument.
777 explicit Input_argument(Input_file_group
* group
)
778 : is_file_(false), group_(group
)
781 // Return whether this is a file.
784 { return this->is_file_
; }
786 // Return whether this is a group.
789 { return !this->is_file_
; }
791 // Return the information about the file.
792 const Input_file_argument
&
795 gold_assert(this->is_file_
);
799 // Return the information about the group.
800 const Input_file_group
*
803 gold_assert(!this->is_file_
);
810 gold_assert(!this->is_file_
);
816 Input_file_argument file_
;
817 Input_file_group
* group_
;
820 // A group from the command line. This is a set of arguments within
821 // --start-group ... --end-group.
823 class Input_file_group
826 typedef std::vector
<Input_argument
> Files
;
827 typedef Files::const_iterator const_iterator
;
833 // Add a file to the end of the group.
835 add_file(const Input_file_argument
& arg
)
836 { this->files_
.push_back(Input_argument(arg
)); }
838 // Iterators to iterate over the group contents.
842 { return this->files_
.begin(); }
846 { return this->files_
.end(); }
852 // A list of files from the command line or a script.
854 class Input_arguments
857 typedef std::vector
<Input_argument
> Input_argument_list
;
858 typedef Input_argument_list::const_iterator const_iterator
;
861 : input_argument_list_(), in_group_(false)
866 add_file(const Input_file_argument
& arg
);
868 // Start a group (the --start-group option).
872 // End a group (the --end-group option).
876 // Return whether we are currently in a group.
879 { return this->in_group_
; }
881 // The number of entries in the list.
884 { return this->input_argument_list_
.size(); }
886 // Iterators to iterate over the list of input files.
890 { return this->input_argument_list_
.begin(); }
894 { return this->input_argument_list_
.end(); }
896 // Return whether the list is empty.
899 { return this->input_argument_list_
.empty(); }
902 Input_argument_list input_argument_list_
;
906 // All the information read from the command line.
911 typedef Input_arguments::const_iterator const_iterator
;
913 Command_line(Script_options
*);
915 // Process the command line options. This will exit with an
916 // appropriate error message if an unrecognized option is seen.
918 process(int argc
, char** argv
);
920 // Process one command-line option. This takes the index of argv to
921 // process, and returns the index for the next option.
923 process_one_option(int argc
, char** argv
, int i
, bool* no_more_options
);
925 // Handle a -l option.
927 process_l_option(int, char**, char*, bool);
929 // Handle a -R option when it means --rpath.
931 add_to_rpath(const char* arg
)
932 { this->options_
.add_to_rpath(arg
); }
934 // Add a file for which we just read the symbols.
936 add_just_symbols_file(const char* arg
)
938 this->inputs_
.add_file(Input_file_argument(arg
, false, "", true,
939 this->position_options_
));
942 // Handle a --start-group option.
944 start_group(const char* arg
);
946 // Handle a --end-group option.
948 end_group(const char* arg
);
950 // Get an option argument--a helper function for special processing.
952 get_special_argument(const char* longname
, int argc
, char** argv
,
953 const char* arg
, bool long_option
,
956 // Get the general options.
957 const General_options
&
959 { return this->options_
; }
961 // Get the position dependent options.
962 const Position_dependent_options
&
963 position_dependent_options() const
964 { return this->position_options_
; }
966 // Get the options which may be set from a linker script.
969 { return this->options_
.script_options(); }
971 const Script_options
*
972 script_options() const
973 { return this->options_
.script_options(); }
975 // The number of input files.
977 number_of_input_files() const
978 { return this->inputs_
.size(); }
980 // Iterators to iterate over the list of input files.
984 { return this->inputs_
.begin(); }
988 { return this->inputs_
.end(); }
991 Command_line(const Command_line
&);
992 Command_line
& operator=(const Command_line
&);
994 // Report usage error.
996 usage() ATTRIBUTE_NORETURN
;
998 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
1000 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
1002 // Apply a command line option.
1004 apply_option(const gold::options::One_option
&, const char*);
1008 add_file(const char* name
, bool is_lib
);
1010 // Examine the result of processing the command-line, and verify
1011 // the flags do not contradict each other or are otherwise illegal.
1013 normalize_options();
1015 General_options options_
;
1016 Position_dependent_options position_options_
;
1017 Input_arguments inputs_
;
1020 } // End namespace gold.
1022 #endif // !defined(GOLD_OPTIONS_H)