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
;
51 class Command_line_options
;
55 } // End namespace gold::options.
57 // A directory to search. For each directory we record whether it is
58 // in the sysroot. We need to know this so that, if a linker script
59 // is found within the sysroot, we will apply the sysroot to any files
60 // named by that script.
62 class Search_directory
65 // We need a default constructor because we put this in a
68 : name_(NULL
), put_in_sysroot_(false), is_in_sysroot_(false)
71 // This is the usual constructor.
72 Search_directory(const char* name
, bool put_in_sysroot
)
73 : name_(name
), put_in_sysroot_(put_in_sysroot
), is_in_sysroot_(false)
74 { gold_assert(!this->name_
.empty()); }
76 // This is called if we have a sysroot. The sysroot is prefixed to
77 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
78 // set to true for any enries which are in the sysroot (this will
79 // naturally include any entries for which put_in_sysroot_ is true).
80 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
81 // passing SYSROOT to lrealpath.
83 add_sysroot(const char* sysroot
, const char* canonical_sysroot
);
85 // Get the directory name.
88 { return this->name_
; }
90 // Return whether this directory is in the sysroot.
93 { return this->is_in_sysroot_
; }
101 // The position independent options which apply to the whole link.
102 // There are a lot of them.
104 class General_options
109 // -E: export dynamic symbols.
111 export_dynamic() const
112 { return this->export_dynamic_
; }
114 // -I: dynamic linker name.
116 dynamic_linker() const
117 { return this->dynamic_linker_
; }
119 // -L: Library search path.
120 typedef std::vector
<Search_directory
> Dir_list
;
124 { return this->search_path_
; }
126 // -O: optimization level (0: don't try to optimize output size).
128 optimization_level() const
129 { return this->optimization_level_
; }
131 // -o: Output file name.
133 output_file_name() const
134 { return this->output_file_name_
; }
136 // -r: Whether we are doing a relocatable link.
138 is_relocatable() const
139 { return this->is_relocatable_
; }
141 // -s: Strip all symbols.
144 { return this->strip_
== STRIP_ALL
; }
146 // -S: Strip debugging information.
149 { return this->strip_
== STRIP_ALL
|| this->strip_
== STRIP_DEBUG
; }
151 // -Sgdb: strip only debugging information that's not used by
152 // gdb (at least, for gdb versions <= 6.7).
154 strip_debug_gdb() const
155 { return this->strip_debug() || this->strip_
== STRIP_DEBUG_UNUSED_BY_GDB
; }
157 // --allow-shlib-undefined: do not warn about unresolved symbols in
158 // --shared libraries.
160 allow_shlib_undefined() const
161 { return this->allow_shlib_undefined_
; }
163 // -Bsymbolic: bind defined symbols locally.
166 { return this->symbolic_
; }
168 // --demangle: demangle C++ symbols in our log messages.
171 { return this->demangle_
; }
173 // --detect-odr-violations: Whether to search for One Defn Rule violations.
175 detect_odr_violations() const
176 { return this->detect_odr_violations_
; }
178 // --eh-frame-hdr: Whether to generate an exception frame header.
180 create_eh_frame_hdr() const
181 { return this->create_eh_frame_hdr_
; }
183 // --rpath: The runtime search path.
186 { return this->rpath_
; }
188 // --rpath-link: The link time search patch for shared libraries.
191 { return this->rpath_link_
; }
193 // --shared: Whether generating a shared object.
196 { return this->is_shared_
; }
198 // --static: Whether doing a static link.
201 { return this->is_static_
; }
203 // --stats: Print resource usage statistics.
206 { return this->print_stats_
; }
208 // --sysroot: The system root of a cross-linker.
211 { return this->sysroot_
; }
213 // -Ttext: The address of the .text section
215 text_segment_address() const
216 { return this->text_segment_address_
; }
218 // Whether -Ttext was used.
220 user_set_text_segment_address() const
221 { return this->text_segment_address_
!= -1U; }
223 // --threads: Whether to use threads.
226 { return this->threads_
; }
228 // --thread-count-initial: Threads to use in initial pass.
230 thread_count_initial() const
231 { return this->thread_count_initial_
; }
233 // --thread-count-middle: Threads to use in middle pass.
235 thread_count_middle() const
236 { return this->thread_count_middle_
; }
238 // --thread-count-final: Threads to use in final pass.
240 thread_count_final() const
241 { return this->thread_count_final_
; }
243 // -z execstack, -z noexecstack
245 is_execstack_set() const
246 { return this->execstack_
!= EXECSTACK_FROM_INPUT
; }
249 is_stack_executable() const
250 { return this->execstack_
== EXECSTACK_YES
; }
253 // Don't copy this structure.
254 General_options(const General_options
&);
255 General_options
& operator=(const General_options
&);
257 friend class Command_line
;
258 friend class options::Command_line_options
;
260 // Which symbols to strip.
263 // Don't strip any symbols.
265 // Strip all symbols.
267 // Strip debugging information.
269 // Strip debugging information that's not used by gdb (at least <= 6.7)
270 STRIP_DEBUG_UNUSED_BY_GDB
273 // Whether to mark the stack as executable.
276 // Not set on command line.
277 EXECSTACK_FROM_INPUT
,
278 // Mark the stack as executable.
280 // Mark the stack as not executable.
286 { this->export_dynamic_
= true; }
289 set_dynamic_linker(const char* arg
)
290 { this->dynamic_linker_
= arg
; }
293 add_to_search_path(const char* arg
)
294 { this->search_path_
.push_back(Search_directory(arg
, false)); }
297 add_to_search_path_with_sysroot(const char* arg
)
298 { this->search_path_
.push_back(Search_directory(arg
, true)); }
301 set_optimization_level(const char* arg
)
302 { this->optimization_level_
= atoi(arg
); }
305 set_output_file_name(const char* arg
)
306 { this->output_file_name_
= arg
; }
310 { this->is_relocatable_
= true; }
314 { this->strip_
= STRIP_ALL
; }
316 // Note: normalize_options() depends on the fact that this turns off
317 // STRIP_ALL if it were already set.
320 { this->strip_
= STRIP_DEBUG
; }
323 set_strip_debug_gdb()
324 { this->strip_
= STRIP_DEBUG_UNUSED_BY_GDB
; }
327 set_allow_shlib_undefined()
328 { this->allow_shlib_undefined_
= true; }
331 set_no_allow_shlib_undefined()
332 { this->allow_shlib_undefined_
= false; }
336 { this->symbolic_
= true; }
340 { this->demangle_
= true; }
344 { this->demangle_
= false; }
347 set_detect_odr_violations()
348 { this->detect_odr_violations_
= true; }
351 set_create_eh_frame_hdr()
352 { this->create_eh_frame_hdr_
= true; }
355 add_to_rpath(const char* arg
)
356 { this->rpath_
.push_back(Search_directory(arg
, false)); }
359 add_to_rpath_link(const char* arg
)
360 { this->rpath_link_
.push_back(Search_directory(arg
, false)); }
364 { this->is_shared_
= true; }
368 { this->is_static_
= true; }
372 { this->print_stats_
= true; }
375 set_sysroot(const char* arg
)
376 { this->sysroot_
= arg
; }
379 set_text_segment_address(const char* arg
)
382 this->text_segment_address_
= strtoull(arg
, &endptr
, 0);
384 || this->text_segment_address_
== -1U)
386 fprintf(stderr
, _("%s: invalid argument to -Ttext: %s\n"),
393 parse_thread_count(const char* arg
)
396 int count
= strtol(arg
, &endptr
, 0);
397 if (*endptr
!= '\0' || count
< 0)
399 fprintf(stderr
, _("%s: invalid thread count: %s\n"),
408 { this->threads_
= true; }
412 { this->threads_
= false; }
415 set_thread_count(const char* arg
)
417 int count
= this->parse_thread_count(arg
);
418 this->thread_count_initial_
= count
;
419 this->thread_count_middle_
= count
;
420 this->thread_count_final_
= count
;
424 set_thread_count_initial(const char* arg
)
425 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
428 set_thread_count_middle(const char* arg
)
429 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
432 set_thread_count_final(const char* arg
)
433 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
441 { this->execstack_
= EXECSTACK_YES
; }
445 { this->execstack_
= EXECSTACK_NO
; }
447 // Handle the -z option.
449 handle_z_option(const char*);
451 // Apply any sysroot to the directory lists.
455 bool export_dynamic_
;
456 const char* dynamic_linker_
;
457 Dir_list search_path_
;
458 int optimization_level_
;
459 const char* output_file_name_
;
460 bool is_relocatable_
;
462 bool allow_shlib_undefined_
;
465 bool detect_odr_violations_
;
466 bool create_eh_frame_hdr_
;
468 Dir_list rpath_link_
;
472 std::string sysroot_
;
473 uint64_t text_segment_address_
;
475 int thread_count_initial_
;
476 int thread_count_middle_
;
477 int thread_count_final_
;
478 Execstack execstack_
;
481 // The current state of the position dependent options.
483 class Position_dependent_options
486 Position_dependent_options();
488 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
489 // -rather than a shared object.
491 do_static_search() const
492 { return this->do_static_search_
; }
494 // --as-needed: Whether to add a DT_NEEDED argument only if the
495 // dynamic object is used.
498 { return this->as_needed_
; }
500 // --whole-archive: Whether to include the entire contents of an
503 include_whole_archive() const
504 { return this->include_whole_archive_
; }
508 { this->do_static_search_
= true; }
512 { this->do_static_search_
= false; }
516 { this->as_needed_
= true; }
520 { this->as_needed_
= false; }
524 { this->include_whole_archive_
= true; }
527 clear_whole_archive()
528 { this->include_whole_archive_
= false; }
531 bool do_static_search_
;
533 bool include_whole_archive_
;
536 // A single file or library argument from the command line.
538 class Input_file_argument
541 // name: file name or library name
542 // is_lib: true if name is a library name: that is, emits the leading
543 // "lib" and trailing ".so"/".a" from the name
544 // extra_search_path: an extra directory to look for the file, prior
545 // to checking the normal library search path. If this is "",
546 // then no extra directory is added.
547 // options: The position dependent options at this point in the
548 // command line, such as --whole-archive.
549 Input_file_argument()
550 : name_(), is_lib_(false), extra_search_path_(""), options_()
553 Input_file_argument(const char* name
, bool is_lib
,
554 const char* extra_search_path
,
555 const Position_dependent_options
& options
)
556 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
562 { return this->name_
.c_str(); }
564 const Position_dependent_options
&
566 { return this->options_
; }
570 { return this->is_lib_
; }
573 extra_search_path() const
575 return (this->extra_search_path_
.empty()
577 : this->extra_search_path_
.c_str());
580 // Return whether this file may require a search using the -L
583 may_need_search() const
584 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
587 // We use std::string, not const char*, here for convenience when
588 // using script files, so that we do not have to preserve the string
592 std::string extra_search_path_
;
593 Position_dependent_options options_
;
596 // A file or library, or a group, from the command line.
601 // Create a file or library argument.
602 explicit Input_argument(Input_file_argument file
)
603 : is_file_(true), file_(file
), group_(NULL
)
606 // Create a group argument.
607 explicit Input_argument(Input_file_group
* group
)
608 : is_file_(false), group_(group
)
611 // Return whether this is a file.
614 { return this->is_file_
; }
616 // Return whether this is a group.
619 { return !this->is_file_
; }
621 // Return the information about the file.
622 const Input_file_argument
&
625 gold_assert(this->is_file_
);
629 // Return the information about the group.
630 const Input_file_group
*
633 gold_assert(!this->is_file_
);
640 gold_assert(!this->is_file_
);
646 Input_file_argument file_
;
647 Input_file_group
* group_
;
650 // A group from the command line. This is a set of arguments within
651 // --start-group ... --end-group.
653 class Input_file_group
656 typedef std::vector
<Input_argument
> Files
;
657 typedef Files::const_iterator const_iterator
;
663 // Add a file to the end of the group.
665 add_file(const Input_file_argument
& arg
)
666 { this->files_
.push_back(Input_argument(arg
)); }
668 // Iterators to iterate over the group contents.
672 { return this->files_
.begin(); }
676 { return this->files_
.end(); }
682 // A list of files from the command line or a script.
684 class Input_arguments
687 typedef std::vector
<Input_argument
> Input_argument_list
;
688 typedef Input_argument_list::const_iterator const_iterator
;
691 : input_argument_list_(), in_group_(false)
696 add_file(const Input_file_argument
& arg
);
698 // Start a group (the --start-group option).
702 // End a group (the --end-group option).
706 // Return whether we are currently in a group.
709 { return this->in_group_
; }
711 // The number of entries in the list.
714 { return this->input_argument_list_
.size(); }
716 // Iterators to iterate over the list of input files.
720 { return this->input_argument_list_
.begin(); }
724 { return this->input_argument_list_
.end(); }
726 // Return whether the list is empty.
729 { return this->input_argument_list_
.empty(); }
732 Input_argument_list input_argument_list_
;
736 // All the information read from the command line.
741 typedef Input_arguments::const_iterator const_iterator
;
745 // Process the command line options. This will exit with an
746 // appropriate error message if an unrecognized option is seen.
748 process(int argc
, char** argv
);
750 // Process one command-line option. This takes the index of argv to
751 // process, and returns the index for the next option.
753 process_one_option(int argc
, char** argv
, int i
, bool* no_more_options
);
755 // Handle a -l option.
757 process_l_option(int, char**, char*, bool);
759 // Handle a --start-group option.
761 start_group(const char* arg
);
763 // Handle a --end-group option.
765 end_group(const char* arg
);
767 // Get an option argument--a helper function for special processing.
769 get_special_argument(const char* longname
, int argc
, char** argv
,
770 const char* arg
, bool long_option
,
773 // Get the general options.
774 const General_options
&
776 { return this->options_
; }
778 // Get the position dependent options.
779 const Position_dependent_options
&
780 position_dependent_options() const
781 { return this->position_options_
; }
783 // The number of input files.
785 number_of_input_files() const
786 { return this->inputs_
.size(); }
788 // Iterators to iterate over the list of input files.
792 { return this->inputs_
.begin(); }
796 { return this->inputs_
.end(); }
799 Command_line(const Command_line
&);
800 Command_line
& operator=(const Command_line
&);
802 // Report usage error.
804 usage() ATTRIBUTE_NORETURN
;
806 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
808 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
810 // Apply a command line option.
812 apply_option(const gold::options::One_option
&, const char*);
816 add_file(const char* name
, bool is_lib
);
818 // Examine the result of processing the command-line, and verify
819 // the flags do not contradict each other or are otherwise illegal.
823 General_options options_
;
824 Position_dependent_options position_options_
;
825 Input_arguments inputs_
;
828 } // End namespace gold.
830 #endif // !defined(GOLD_OPTIONS_H)