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 // just_symbols: whether this file only defines symbols.
640 // options: The position dependent options at this point in the
641 // command line, such as --whole-archive.
642 Input_file_argument()
643 : name_(), is_lib_(false), extra_search_path_(""), just_symbols_(false),
647 Input_file_argument(const char* name
, bool is_lib
,
648 const char* extra_search_path
,
650 const Position_dependent_options
& options
)
651 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
652 just_symbols_(just_symbols
), options_(options
)
657 { return this->name_
.c_str(); }
659 const Position_dependent_options
&
661 { return this->options_
; }
665 { return this->is_lib_
; }
668 extra_search_path() const
670 return (this->extra_search_path_
.empty()
672 : this->extra_search_path_
.c_str());
675 // Return whether we should only read symbols from this file.
678 { return this->just_symbols_
; }
680 // Return whether this file may require a search using the -L
683 may_need_search() const
684 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
687 // We use std::string, not const char*, here for convenience when
688 // using script files, so that we do not have to preserve the string
692 std::string extra_search_path_
;
694 Position_dependent_options options_
;
697 // A file or library, or a group, from the command line.
702 // Create a file or library argument.
703 explicit Input_argument(Input_file_argument file
)
704 : is_file_(true), file_(file
), group_(NULL
)
707 // Create a group argument.
708 explicit Input_argument(Input_file_group
* group
)
709 : is_file_(false), group_(group
)
712 // Return whether this is a file.
715 { return this->is_file_
; }
717 // Return whether this is a group.
720 { return !this->is_file_
; }
722 // Return the information about the file.
723 const Input_file_argument
&
726 gold_assert(this->is_file_
);
730 // Return the information about the group.
731 const Input_file_group
*
734 gold_assert(!this->is_file_
);
741 gold_assert(!this->is_file_
);
747 Input_file_argument file_
;
748 Input_file_group
* group_
;
751 // A group from the command line. This is a set of arguments within
752 // --start-group ... --end-group.
754 class Input_file_group
757 typedef std::vector
<Input_argument
> Files
;
758 typedef Files::const_iterator const_iterator
;
764 // Add a file to the end of the group.
766 add_file(const Input_file_argument
& arg
)
767 { this->files_
.push_back(Input_argument(arg
)); }
769 // Iterators to iterate over the group contents.
773 { return this->files_
.begin(); }
777 { return this->files_
.end(); }
783 // A list of files from the command line or a script.
785 class Input_arguments
788 typedef std::vector
<Input_argument
> Input_argument_list
;
789 typedef Input_argument_list::const_iterator const_iterator
;
792 : input_argument_list_(), in_group_(false)
797 add_file(const Input_file_argument
& arg
);
799 // Start a group (the --start-group option).
803 // End a group (the --end-group option).
807 // Return whether we are currently in a group.
810 { return this->in_group_
; }
812 // The number of entries in the list.
815 { return this->input_argument_list_
.size(); }
817 // Iterators to iterate over the list of input files.
821 { return this->input_argument_list_
.begin(); }
825 { return this->input_argument_list_
.end(); }
827 // Return whether the list is empty.
830 { return this->input_argument_list_
.empty(); }
833 Input_argument_list input_argument_list_
;
837 // All the information read from the command line.
842 typedef Input_arguments::const_iterator const_iterator
;
844 Command_line(Script_options
*);
846 // Process the command line options. This will exit with an
847 // appropriate error message if an unrecognized option is seen.
849 process(int argc
, char** argv
);
851 // Process one command-line option. This takes the index of argv to
852 // process, and returns the index for the next option.
854 process_one_option(int argc
, char** argv
, int i
, bool* no_more_options
);
856 // Handle a -l option.
858 process_l_option(int, char**, char*, bool);
860 // Handle a -R option when it means --rpath.
862 add_to_rpath(const char* arg
)
863 { this->options_
.add_to_rpath(arg
); }
865 // Add a file for which we just read the symbols.
867 add_just_symbols_file(const char* arg
)
869 this->inputs_
.add_file(Input_file_argument(arg
, false, "", true,
870 this->position_options_
));
873 // Handle a --start-group option.
875 start_group(const char* arg
);
877 // Handle a --end-group option.
879 end_group(const char* arg
);
881 // Get an option argument--a helper function for special processing.
883 get_special_argument(const char* longname
, int argc
, char** argv
,
884 const char* arg
, bool long_option
,
887 // Get the general options.
888 const General_options
&
890 { return this->options_
; }
892 // Get the position dependent options.
893 const Position_dependent_options
&
894 position_dependent_options() const
895 { return this->position_options_
; }
897 // Get the options which may be set from a linker script.
900 { return this->options_
.script_options(); }
902 const Script_options
*
903 script_options() const
904 { return this->options_
.script_options(); }
906 // The number of input files.
908 number_of_input_files() const
909 { return this->inputs_
.size(); }
911 // Iterators to iterate over the list of input files.
915 { return this->inputs_
.begin(); }
919 { return this->inputs_
.end(); }
922 Command_line(const Command_line
&);
923 Command_line
& operator=(const Command_line
&);
925 // Report usage error.
927 usage() ATTRIBUTE_NORETURN
;
929 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
931 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
933 // Apply a command line option.
935 apply_option(const gold::options::One_option
&, const char*);
939 add_file(const char* name
, bool is_lib
);
941 // Examine the result of processing the command-line, and verify
942 // the flags do not contradict each other or are otherwise illegal.
946 General_options options_
;
947 Position_dependent_options position_options_
;
948 Input_arguments inputs_
;
951 } // End namespace gold.
953 #endif // !defined(GOLD_OPTIONS_H)