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
112 General_options(Script_options
*);
114 // -e: set entry address.
117 { return this->script_options_
->entry(); }
119 // -E: export dynamic symbols.
121 export_dynamic() const
122 { return this->export_dynamic_
; }
124 // -h: shared library name.
127 { return this->soname_
; }
129 // -I: dynamic linker name.
131 dynamic_linker() const
132 { return this->dynamic_linker_
; }
134 // -L: Library search path.
135 typedef std::vector
<Search_directory
> Dir_list
;
139 { return this->search_path_
; }
141 // -O: optimization level (0: don't try to optimize output size).
143 optimization_level() const
144 { return this->optimization_level_
; }
146 // -o: Output file name.
148 output_file_name() const
149 { return this->output_file_name_
; }
151 // -r: Whether we are doing a relocatable link.
153 is_relocatable() const
154 { return this->is_relocatable_
; }
156 // -s: Strip all symbols.
159 { return this->strip_
== STRIP_ALL
; }
161 // -S: Strip debugging information.
164 { return this->strip_
== STRIP_ALL
|| this->strip_
== STRIP_DEBUG
; }
166 // --strip-debug-gdb: strip only debugging information that's not
167 // used by gdb (at least, for gdb versions <= 6.7).
169 strip_debug_gdb() const
170 { return this->strip_debug() || this->strip_
== STRIP_DEBUG_UNUSED_BY_GDB
; }
172 // --allow-shlib-undefined: do not warn about unresolved symbols in
173 // --shared libraries.
175 allow_shlib_undefined() const
176 { return this->allow_shlib_undefined_
; }
178 // -Bsymbolic: bind defined symbols locally.
181 { return this->symbolic_
; }
183 // --compress-debug-sections: compress .debug_* sections in the
184 // output file using the given compression method. This is useful
185 // when the tools (such as gdb) support compressed sections.
187 compress_debug_sections() const
188 { return this->compress_debug_sections_
!= NO_COMPRESSION
; }
191 zlib_compress_debug_sections() const
192 { return this->compress_debug_sections_
== ZLIB_COMPRESSION
; }
194 // --demangle: demangle C++ symbols in our log messages.
197 { return this->demangle_
; }
199 // --detect-odr-violations: Whether to search for One Defn Rule violations.
201 detect_odr_violations() const
202 { return this->detect_odr_violations_
; }
204 // --eh-frame-hdr: Whether to generate an exception frame header.
206 create_eh_frame_hdr() const
207 { return this->create_eh_frame_hdr_
; }
209 // --rpath: The runtime search path.
212 { return this->rpath_
; }
214 // --rpath-link: The link time search patch for shared libraries.
217 { return this->rpath_link_
; }
219 // --shared: Whether generating a shared object.
222 { return this->is_shared_
; }
224 // --static: Whether doing a static link.
227 { return this->is_static_
; }
229 // --stats: Print resource usage statistics.
232 { return this->print_stats_
; }
234 // --sysroot: The system root of a cross-linker.
237 { return this->sysroot_
; }
239 // --version-script: The version script to apply if --shared is true.
240 const Version_script_info
&
241 version_script() const
242 { return *this->script_options_
->version_script_info(); }
244 // -Ttext: The address of the .text section
246 text_segment_address() const
247 { return this->text_segment_address_
; }
249 // Whether -Ttext was used.
251 user_set_text_segment_address() const
252 { return this->text_segment_address_
!= -1U; }
254 // --threads: Whether to use threads.
257 { return this->threads_
; }
259 // --thread-count-initial: Threads to use in initial pass.
261 thread_count_initial() const
262 { return this->thread_count_initial_
; }
264 // --thread-count-middle: Threads to use in middle pass.
266 thread_count_middle() const
267 { return this->thread_count_middle_
; }
269 // --thread-count-final: Threads to use in final pass.
271 thread_count_final() const
272 { return this->thread_count_final_
; }
274 // -z execstack, -z noexecstack
276 is_execstack_set() const
277 { return this->execstack_
!= EXECSTACK_FROM_INPUT
; }
280 is_stack_executable() const
281 { return this->execstack_
== EXECSTACK_YES
; }
286 { return this->debug_
; }
288 // Return the options which may be set from a linker script.
291 { return this->script_options_
; }
293 const Script_options
*
294 script_options() const
295 { return this->script_options_
; }
298 // Don't copy this structure.
299 General_options(const General_options
&);
300 General_options
& operator=(const General_options
&);
302 friend class Command_line
;
303 friend class options::Command_line_options
;
305 // Which symbols to strip.
308 // Don't strip any symbols.
310 // Strip all symbols.
312 // Strip debugging information.
314 // Strip debugging information that's not used by gdb (at least <= 6.7)
315 STRIP_DEBUG_UNUSED_BY_GDB
318 // Whether to mark the stack as executable.
321 // Not set on command line.
322 EXECSTACK_FROM_INPUT
,
323 // Mark the stack as executable.
325 // Mark the stack as not executable.
329 // What compression method to use
330 enum CompressionMethod
337 set_entry(const char* arg
)
338 { this->script_options_
->set_entry(arg
, strlen(arg
)); }
342 { this->export_dynamic_
= true; }
345 set_soname(const char* arg
)
346 { this->soname_
= arg
; }
349 set_dynamic_linker(const char* arg
)
350 { this->dynamic_linker_
= arg
; }
353 add_to_search_path(const char* arg
)
354 { this->search_path_
.push_back(Search_directory(arg
, false)); }
357 add_to_search_path_with_sysroot(const char* arg
)
358 { this->search_path_
.push_back(Search_directory(arg
, true)); }
361 set_optimization_level(const char* arg
)
364 this->optimization_level_
= strtol(arg
, &endptr
, 0);
365 if (*endptr
!= '\0' || this->optimization_level_
< 0)
366 gold_fatal(_("invalid optimization level: %s"), arg
);
370 set_output_file_name(const char* arg
)
371 { this->output_file_name_
= arg
; }
375 { this->is_relocatable_
= true; }
379 { this->strip_
= STRIP_ALL
; }
381 // Note: normalize_options() depends on the fact that this turns off
382 // STRIP_ALL if it were already set.
385 { this->strip_
= STRIP_DEBUG
; }
388 set_strip_debug_gdb()
389 { this->strip_
= STRIP_DEBUG_UNUSED_BY_GDB
; }
392 set_allow_shlib_undefined()
393 { this->allow_shlib_undefined_
= true; }
396 set_no_allow_shlib_undefined()
397 { this->allow_shlib_undefined_
= false; }
401 { this->symbolic_
= true; }
403 void set_compress_debug_sections(const char* arg
)
405 if (strcmp(arg
, "none") == 0)
406 this->compress_debug_sections_
= NO_COMPRESSION
;
408 else if (strcmp(arg
, "zlib") == 0)
409 this->compress_debug_sections_
= ZLIB_COMPRESSION
;
412 gold_fatal(_("unsupported argument to --compress-debug-sections: %s"),
417 define_symbol(const char* arg
);
421 { this->demangle_
= true; }
425 { this->demangle_
= false; }
428 set_detect_odr_violations()
429 { this->detect_odr_violations_
= true; }
432 set_create_eh_frame_hdr()
433 { this->create_eh_frame_hdr_
= true; }
436 add_to_rpath(const char* arg
)
437 { this->rpath_
.push_back(Search_directory(arg
, false)); }
440 add_to_rpath_link(const char* arg
)
441 { this->rpath_link_
.push_back(Search_directory(arg
, false)); }
445 { this->is_shared_
= true; }
449 { this->is_static_
= true; }
453 { this->print_stats_
= true; }
456 set_sysroot(const char* arg
)
457 { this->sysroot_
= arg
; }
460 set_text_segment_address(const char* arg
)
463 this->text_segment_address_
= strtoull(arg
, &endptr
, 0);
465 || this->text_segment_address_
== -1U)
466 gold_fatal(_("invalid argument to -Ttext: %s"), arg
);
470 parse_thread_count(const char* arg
)
473 const int count
= strtol(arg
, &endptr
, 0);
474 if (*endptr
!= '\0' || count
< 0)
475 gold_fatal(_("invalid thread count: %s"), arg
);
482 #ifndef ENABLE_THREADS
483 gold_fatal(_("--threads not supported"));
485 this->threads_
= true;
490 { this->threads_
= false; }
493 set_thread_count(const char* arg
)
495 int count
= this->parse_thread_count(arg
);
496 this->thread_count_initial_
= count
;
497 this->thread_count_middle_
= count
;
498 this->thread_count_final_
= count
;
502 set_thread_count_initial(const char* arg
)
503 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
506 set_thread_count_middle(const char* arg
)
507 { this->thread_count_middle_
= this->parse_thread_count(arg
); }
510 set_thread_count_final(const char* arg
)
511 { this->thread_count_final_
= this->parse_thread_count(arg
); }
519 { this->execstack_
= EXECSTACK_YES
; }
523 { this->execstack_
= EXECSTACK_NO
; }
526 set_debug(unsigned int flags
)
527 { this->debug_
= flags
; }
529 // Handle the -z option.
531 handle_z_option(const char*);
533 // Handle the --debug option.
535 handle_debug_option(const char*);
537 // Apply any sysroot to the directory lists.
541 bool export_dynamic_
;
543 const char* dynamic_linker_
;
544 Dir_list search_path_
;
545 int optimization_level_
;
546 const char* output_file_name_
;
547 bool is_relocatable_
;
549 bool allow_shlib_undefined_
;
551 CompressionMethod compress_debug_sections_
;
553 bool detect_odr_violations_
;
554 bool create_eh_frame_hdr_
;
556 Dir_list rpath_link_
;
560 std::string sysroot_
;
561 uint64_t text_segment_address_
;
563 int thread_count_initial_
;
564 int thread_count_middle_
;
565 int thread_count_final_
;
566 Execstack execstack_
;
568 // Some options can also be set from linker scripts. Those are
570 Script_options
* script_options_
;
573 // The current state of the position dependent options.
575 class Position_dependent_options
578 Position_dependent_options();
580 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
581 // -rather than a shared object.
583 do_static_search() const
584 { return this->do_static_search_
; }
586 // --as-needed: Whether to add a DT_NEEDED argument only if the
587 // dynamic object is used.
590 { return this->as_needed_
; }
592 // --whole-archive: Whether to include the entire contents of an
595 include_whole_archive() const
596 { return this->include_whole_archive_
; }
600 { this->do_static_search_
= true; }
604 { this->do_static_search_
= false; }
608 { this->as_needed_
= true; }
612 { this->as_needed_
= false; }
616 { this->include_whole_archive_
= true; }
619 clear_whole_archive()
620 { this->include_whole_archive_
= false; }
623 bool do_static_search_
;
625 bool include_whole_archive_
;
628 // A single file or library argument from the command line.
630 class Input_file_argument
633 // name: file name or library name
634 // is_lib: true if name is a library name: that is, emits the leading
635 // "lib" and trailing ".so"/".a" from the name
636 // extra_search_path: an extra directory to look for the file, prior
637 // to checking the normal library search path. If this is "",
638 // then no extra directory is added.
639 // options: The position dependent options at this point in the
640 // command line, such as --whole-archive.
641 Input_file_argument()
642 : name_(), is_lib_(false), extra_search_path_(""), options_()
645 Input_file_argument(const char* name
, bool is_lib
,
646 const char* extra_search_path
,
647 const Position_dependent_options
& options
)
648 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
654 { return this->name_
.c_str(); }
656 const Position_dependent_options
&
658 { return this->options_
; }
662 { return this->is_lib_
; }
665 extra_search_path() const
667 return (this->extra_search_path_
.empty()
669 : this->extra_search_path_
.c_str());
672 // Return whether this file may require a search using the -L
675 may_need_search() const
676 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
679 // We use std::string, not const char*, here for convenience when
680 // using script files, so that we do not have to preserve the string
684 std::string extra_search_path_
;
685 Position_dependent_options options_
;
688 // A file or library, or a group, from the command line.
693 // Create a file or library argument.
694 explicit Input_argument(Input_file_argument file
)
695 : is_file_(true), file_(file
), group_(NULL
)
698 // Create a group argument.
699 explicit Input_argument(Input_file_group
* group
)
700 : is_file_(false), group_(group
)
703 // Return whether this is a file.
706 { return this->is_file_
; }
708 // Return whether this is a group.
711 { return !this->is_file_
; }
713 // Return the information about the file.
714 const Input_file_argument
&
717 gold_assert(this->is_file_
);
721 // Return the information about the group.
722 const Input_file_group
*
725 gold_assert(!this->is_file_
);
732 gold_assert(!this->is_file_
);
738 Input_file_argument file_
;
739 Input_file_group
* group_
;
742 // A group from the command line. This is a set of arguments within
743 // --start-group ... --end-group.
745 class Input_file_group
748 typedef std::vector
<Input_argument
> Files
;
749 typedef Files::const_iterator const_iterator
;
755 // Add a file to the end of the group.
757 add_file(const Input_file_argument
& arg
)
758 { this->files_
.push_back(Input_argument(arg
)); }
760 // Iterators to iterate over the group contents.
764 { return this->files_
.begin(); }
768 { return this->files_
.end(); }
774 // A list of files from the command line or a script.
776 class Input_arguments
779 typedef std::vector
<Input_argument
> Input_argument_list
;
780 typedef Input_argument_list::const_iterator const_iterator
;
783 : input_argument_list_(), in_group_(false)
788 add_file(const Input_file_argument
& arg
);
790 // Start a group (the --start-group option).
794 // End a group (the --end-group option).
798 // Return whether we are currently in a group.
801 { return this->in_group_
; }
803 // The number of entries in the list.
806 { return this->input_argument_list_
.size(); }
808 // Iterators to iterate over the list of input files.
812 { return this->input_argument_list_
.begin(); }
816 { return this->input_argument_list_
.end(); }
818 // Return whether the list is empty.
821 { return this->input_argument_list_
.empty(); }
824 Input_argument_list input_argument_list_
;
828 // All the information read from the command line.
833 typedef Input_arguments::const_iterator const_iterator
;
835 Command_line(Script_options
*);
837 // Process the command line options. This will exit with an
838 // appropriate error message if an unrecognized option is seen.
840 process(int argc
, char** argv
);
842 // Process one command-line option. This takes the index of argv to
843 // process, and returns the index for the next option.
845 process_one_option(int argc
, char** argv
, int i
, bool* no_more_options
);
847 // Handle a -l option.
849 process_l_option(int, char**, char*, bool);
851 // Handle a --start-group option.
853 start_group(const char* arg
);
855 // Handle a --end-group option.
857 end_group(const char* arg
);
859 // Get an option argument--a helper function for special processing.
861 get_special_argument(const char* longname
, int argc
, char** argv
,
862 const char* arg
, bool long_option
,
865 // Get the general options.
866 const General_options
&
868 { return this->options_
; }
870 // Get the position dependent options.
871 const Position_dependent_options
&
872 position_dependent_options() const
873 { return this->position_options_
; }
875 // Get the options which may be set from a linker script.
878 { return this->options_
.script_options(); }
880 const Script_options
*
881 script_options() const
882 { return this->options_
.script_options(); }
884 // The number of input files.
886 number_of_input_files() const
887 { return this->inputs_
.size(); }
889 // Iterators to iterate over the list of input files.
893 { return this->inputs_
.begin(); }
897 { return this->inputs_
.end(); }
900 Command_line(const Command_line
&);
901 Command_line
& operator=(const Command_line
&);
903 // Report usage error.
905 usage() ATTRIBUTE_NORETURN
;
907 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
909 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
911 // Apply a command line option.
913 apply_option(const gold::options::One_option
&, const char*);
917 add_file(const char* name
, bool is_lib
);
919 // Examine the result of processing the command-line, and verify
920 // the flags do not contradict each other or are otherwise illegal.
924 General_options options_
;
925 Position_dependent_options position_options_
;
926 Input_arguments inputs_
;
929 } // End namespace gold.
931 #endif // !defined(GOLD_OPTIONS_H)