1 // options.h -- handle command line options for gold -*- C++ -*-
3 // Copyright 2006, 2007 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)
76 { gold_assert(!this->name_
.empty()); }
78 // This is called if we have a sysroot. The sysroot is prefixed to
79 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
80 // set to true for any enries which are in the sysroot (this will
81 // naturally include any entries for which put_in_sysroot_ is true).
82 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
83 // passing SYSROOT to lrealpath.
85 add_sysroot(const char* sysroot
, const char* canonical_sysroot
);
87 // Get the directory name.
90 { return this->name_
; }
92 // Return whether this directory is in the sysroot.
95 { return this->is_in_sysroot_
; }
103 // The position independent options which apply to the whole link.
104 // There are a lot of them.
106 class General_options
111 // -E: export dynamic symbols.
113 export_dynamic() const
114 { return this->export_dynamic_
; }
116 // -I: dynamic linker name.
118 dynamic_linker() const
119 { return this->dynamic_linker_
; }
121 // -L: Library search path.
122 typedef std::vector
<Search_directory
> Dir_list
;
126 { return this->search_path_
; }
128 // -O: optimization level (0: don't try to optimize output size).
130 optimization_level() const
131 { return this->optimization_level_
; }
133 // -o: Output file name.
135 output_file_name() const
136 { return this->output_file_name_
; }
138 // -r: Whether we are doing a relocatable link.
140 is_relocatable() const
141 { return this->is_relocatable_
; }
143 // -s: Strip all symbols.
146 { return this->strip_
== STRIP_ALL
; }
148 // -S: Strip debugging information.
151 { return this->strip_
== STRIP_ALL
|| this->strip_
== STRIP_DEBUG
; }
153 // -Sgdb: strip only debugging information that's not used by
154 // gdb (at least, for gdb versions <= 6.7).
156 strip_debug_gdb() const
157 { return this->strip_debug() || this->strip_
== STRIP_DEBUG_UNUSED_BY_GDB
; }
159 // --allow-shlib-undefined: do not warn about unresolved symbols in
160 // --shared libraries.
162 allow_shlib_undefined() const
163 { return this->allow_shlib_undefined_
; }
165 // -Bsymbolic: bind defined symbols locally.
168 { return this->symbolic_
; }
170 // --demangle: demangle C++ symbols in our log messages.
173 { return this->demangle_
; }
175 // --detect-odr-violations: Whether to search for One Defn Rule violations.
177 detect_odr_violations() const
178 { return this->detect_odr_violations_
; }
180 // --eh-frame-hdr: Whether to generate an exception frame header.
182 create_eh_frame_hdr() const
183 { return this->create_eh_frame_hdr_
; }
185 // --rpath: The runtime search path.
188 { return this->rpath_
; }
190 // --rpath-link: The link time search patch for shared libraries.
193 { return this->rpath_link_
; }
195 // --shared: Whether generating a shared object.
198 { return this->is_shared_
; }
200 // --static: Whether doing a static link.
203 { return this->is_static_
; }
205 // --stats: Print resource usage statistics.
208 { return this->print_stats_
; }
210 // --sysroot: The system root of a cross-linker.
213 { return this->sysroot_
; }
215 // -Ttext: The address of the .text section
217 text_segment_address() const
218 { return this->text_segment_address_
; }
220 // Whether -Ttext was used.
222 user_set_text_segment_address() const
223 { return this->text_segment_address_
!= -1U; }
225 // --threads: Whether to use threads.
228 { return this->threads_
; }
230 // --thread-count-initial: Threads to use in initial pass.
232 thread_count_initial() const
233 { return this->thread_count_initial_
; }
235 // --thread-count-middle: Threads to use in middle pass.
237 thread_count_middle() const
238 { return this->thread_count_middle_
; }
240 // --thread-count-final: Threads to use in final pass.
242 thread_count_final() const
243 { return this->thread_count_final_
; }
245 // -z execstack, -z noexecstack
247 is_execstack_set() const
248 { return this->execstack_
!= EXECSTACK_FROM_INPUT
; }
251 is_stack_executable() const
252 { return this->execstack_
== EXECSTACK_YES
; }
257 { return this->debug_
; }
260 // Don't copy this structure.
261 General_options(const General_options
&);
262 General_options
& operator=(const General_options
&);
264 friend class Command_line
;
265 friend class options::Command_line_options
;
267 // Which symbols to strip.
270 // Don't strip any symbols.
272 // Strip all symbols.
274 // Strip debugging information.
276 // Strip debugging information that's not used by gdb (at least <= 6.7)
277 STRIP_DEBUG_UNUSED_BY_GDB
280 // Whether to mark the stack as executable.
283 // Not set on command line.
284 EXECSTACK_FROM_INPUT
,
285 // Mark the stack as executable.
287 // Mark the stack as not executable.
293 { this->export_dynamic_
= true; }
296 set_dynamic_linker(const char* arg
)
297 { this->dynamic_linker_
= arg
; }
300 add_to_search_path(const char* arg
)
301 { this->search_path_
.push_back(Search_directory(arg
, false)); }
304 add_to_search_path_with_sysroot(const char* arg
)
305 { this->search_path_
.push_back(Search_directory(arg
, true)); }
308 set_optimization_level(const char* arg
)
309 { this->optimization_level_
= atoi(arg
); }
312 set_output_file_name(const char* arg
)
313 { this->output_file_name_
= arg
; }
317 { this->is_relocatable_
= true; }
321 { this->strip_
= STRIP_ALL
; }
323 // Note: normalize_options() depends on the fact that this turns off
324 // STRIP_ALL if it were already set.
327 { this->strip_
= STRIP_DEBUG
; }
330 set_strip_debug_gdb()
331 { this->strip_
= STRIP_DEBUG_UNUSED_BY_GDB
; }
334 set_allow_shlib_undefined()
335 { this->allow_shlib_undefined_
= true; }
338 set_no_allow_shlib_undefined()
339 { this->allow_shlib_undefined_
= false; }
343 { this->symbolic_
= true; }
347 { this->demangle_
= true; }
351 { this->demangle_
= false; }
354 set_detect_odr_violations()
355 { this->detect_odr_violations_
= true; }
358 set_create_eh_frame_hdr()
359 { this->create_eh_frame_hdr_
= true; }
362 add_to_rpath(const char* arg
)
363 { this->rpath_
.push_back(Search_directory(arg
, false)); }
366 add_to_rpath_link(const char* arg
)
367 { this->rpath_link_
.push_back(Search_directory(arg
, false)); }
371 { this->is_shared_
= true; }
375 { this->is_static_
= true; }
379 { this->print_stats_
= true; }
382 set_sysroot(const char* arg
)
383 { this->sysroot_
= arg
; }
386 set_text_segment_address(const char* arg
)
389 this->text_segment_address_
= strtoull(arg
, &endptr
, 0);
391 || this->text_segment_address_
== -1U)
393 fprintf(stderr
, _("%s: invalid argument to -Ttext: %s\n"),
400 parse_thread_count(const char* arg
)
403 int count
= strtol(arg
, &endptr
, 0);
404 if (*endptr
!= '\0' || count
< 0)
406 fprintf(stderr
, _("%s: invalid thread count: %s\n"),
415 { this->threads_
= true; }
419 { this->threads_
= false; }
422 set_thread_count(const char* arg
)
424 int count
= this->parse_thread_count(arg
);
425 this->thread_count_initial_
= count
;
426 this->thread_count_middle_
= count
;
427 this->thread_count_final_
= count
;
431 set_thread_count_initial(const char* arg
)
432 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
435 set_thread_count_middle(const char* arg
)
436 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
439 set_thread_count_final(const char* arg
)
440 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
448 { this->execstack_
= EXECSTACK_YES
; }
452 { this->execstack_
= EXECSTACK_NO
; }
455 set_debug(unsigned int flags
)
456 { this->debug_
= flags
; }
458 // Handle the -z option.
460 handle_z_option(const char*);
462 // Handle the --debug option.
464 handle_debug_option(const char*);
466 // Apply any sysroot to the directory lists.
470 bool export_dynamic_
;
471 const char* dynamic_linker_
;
472 Dir_list search_path_
;
473 int optimization_level_
;
474 const char* output_file_name_
;
475 bool is_relocatable_
;
477 bool allow_shlib_undefined_
;
480 bool detect_odr_violations_
;
481 bool create_eh_frame_hdr_
;
483 Dir_list rpath_link_
;
487 std::string sysroot_
;
488 uint64_t text_segment_address_
;
490 int thread_count_initial_
;
491 int thread_count_middle_
;
492 int thread_count_final_
;
493 Execstack execstack_
;
497 // The current state of the position dependent options.
499 class Position_dependent_options
502 Position_dependent_options();
504 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
505 // -rather than a shared object.
507 do_static_search() const
508 { return this->do_static_search_
; }
510 // --as-needed: Whether to add a DT_NEEDED argument only if the
511 // dynamic object is used.
514 { return this->as_needed_
; }
516 // --whole-archive: Whether to include the entire contents of an
519 include_whole_archive() const
520 { return this->include_whole_archive_
; }
524 { this->do_static_search_
= true; }
528 { this->do_static_search_
= false; }
532 { this->as_needed_
= true; }
536 { this->as_needed_
= false; }
540 { this->include_whole_archive_
= true; }
543 clear_whole_archive()
544 { this->include_whole_archive_
= false; }
547 bool do_static_search_
;
549 bool include_whole_archive_
;
552 // A single file or library argument from the command line.
554 class Input_file_argument
557 // name: file name or library name
558 // is_lib: true if name is a library name: that is, emits the leading
559 // "lib" and trailing ".so"/".a" from the name
560 // extra_search_path: an extra directory to look for the file, prior
561 // to checking the normal library search path. If this is "",
562 // then no extra directory is added.
563 // options: The position dependent options at this point in the
564 // command line, such as --whole-archive.
565 Input_file_argument()
566 : name_(), is_lib_(false), extra_search_path_(""), options_()
569 Input_file_argument(const char* name
, bool is_lib
,
570 const char* extra_search_path
,
571 const Position_dependent_options
& options
)
572 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
578 { return this->name_
.c_str(); }
580 const Position_dependent_options
&
582 { return this->options_
; }
586 { return this->is_lib_
; }
589 extra_search_path() const
591 return (this->extra_search_path_
.empty()
593 : this->extra_search_path_
.c_str());
596 // Return whether this file may require a search using the -L
599 may_need_search() const
600 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
603 // We use std::string, not const char*, here for convenience when
604 // using script files, so that we do not have to preserve the string
608 std::string extra_search_path_
;
609 Position_dependent_options options_
;
612 // A file or library, or a group, from the command line.
617 // Create a file or library argument.
618 explicit Input_argument(Input_file_argument file
)
619 : is_file_(true), file_(file
), group_(NULL
)
622 // Create a group argument.
623 explicit Input_argument(Input_file_group
* group
)
624 : is_file_(false), group_(group
)
627 // Return whether this is a file.
630 { return this->is_file_
; }
632 // Return whether this is a group.
635 { return !this->is_file_
; }
637 // Return the information about the file.
638 const Input_file_argument
&
641 gold_assert(this->is_file_
);
645 // Return the information about the group.
646 const Input_file_group
*
649 gold_assert(!this->is_file_
);
656 gold_assert(!this->is_file_
);
662 Input_file_argument file_
;
663 Input_file_group
* group_
;
666 // A group from the command line. This is a set of arguments within
667 // --start-group ... --end-group.
669 class Input_file_group
672 typedef std::vector
<Input_argument
> Files
;
673 typedef Files::const_iterator const_iterator
;
679 // Add a file to the end of the group.
681 add_file(const Input_file_argument
& arg
)
682 { this->files_
.push_back(Input_argument(arg
)); }
684 // Iterators to iterate over the group contents.
688 { return this->files_
.begin(); }
692 { return this->files_
.end(); }
698 // A list of files from the command line or a script.
700 class Input_arguments
703 typedef std::vector
<Input_argument
> Input_argument_list
;
704 typedef Input_argument_list::const_iterator const_iterator
;
707 : input_argument_list_(), in_group_(false)
712 add_file(const Input_file_argument
& arg
);
714 // Start a group (the --start-group option).
718 // End a group (the --end-group option).
722 // Return whether we are currently in a group.
725 { return this->in_group_
; }
727 // The number of entries in the list.
730 { return this->input_argument_list_
.size(); }
732 // Iterators to iterate over the list of input files.
736 { return this->input_argument_list_
.begin(); }
740 { return this->input_argument_list_
.end(); }
742 // Return whether the list is empty.
745 { return this->input_argument_list_
.empty(); }
748 Input_argument_list input_argument_list_
;
752 // All the information read from the command line.
757 typedef Input_arguments::const_iterator const_iterator
;
761 // Process the command line options. This will exit with an
762 // appropriate error message if an unrecognized option is seen.
764 process(int argc
, char** argv
);
766 // Process one command-line option. This takes the index of argv to
767 // process, and returns the index for the next option.
769 process_one_option(int argc
, char** argv
, int i
, bool* no_more_options
);
771 // Handle a -l option.
773 process_l_option(int, char**, char*, bool);
775 // Handle a --start-group option.
777 start_group(const char* arg
);
779 // Handle a --end-group option.
781 end_group(const char* arg
);
783 // Get an option argument--a helper function for special processing.
785 get_special_argument(const char* longname
, int argc
, char** argv
,
786 const char* arg
, bool long_option
,
789 // Get the general options.
790 const General_options
&
792 { return this->options_
; }
794 // Get the position dependent options.
795 const Position_dependent_options
&
796 position_dependent_options() const
797 { return this->position_options_
; }
799 // The number of input files.
801 number_of_input_files() const
802 { return this->inputs_
.size(); }
804 // Iterators to iterate over the list of input files.
808 { return this->inputs_
.begin(); }
812 { return this->inputs_
.end(); }
815 Command_line(const Command_line
&);
816 Command_line
& operator=(const Command_line
&);
818 // Report usage error.
820 usage() ATTRIBUTE_NORETURN
;
822 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
824 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
826 // Apply a command line option.
828 apply_option(const gold::options::One_option
&, const char*);
832 add_file(const char* name
, bool is_lib
);
834 // Examine the result of processing the command-line, and verify
835 // the flags do not contradict each other or are otherwise illegal.
839 General_options options_
;
840 Position_dependent_options position_options_
;
841 Input_arguments inputs_
;
844 } // End namespace gold.
846 #endif // !defined(GOLD_OPTIONS_H)