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 // -Bsymbolic: bind defined symbols locally.
154 { return this->symbolic_
; }
156 // --eh-frame-hdr: Whether to generate an exception frame header.
158 create_eh_frame_hdr() const
159 { return this->create_eh_frame_hdr_
; }
161 // --rpath: The runtime search path.
164 { return this->rpath_
; }
166 // --rpath-link: The link time search patch for shared libraries.
169 { return this->rpath_link_
; }
171 // --shared: Whether generating a shared object.
174 { return this->is_shared_
; }
176 // --static: Whether doing a static link.
179 { return this->is_static_
; }
181 // --stats: Print resource usage statistics.
184 { return this->print_stats_
; }
186 // --sysroot: The system root of a cross-linker.
189 { return this->sysroot_
; }
191 // -Ttext: The address of the .text section
193 text_segment_address() const
194 { return this->text_segment_address_
; }
196 // Whether -Ttext was used.
198 user_set_text_segment_address() const
199 { return this->text_segment_address_
!= -1U; }
201 // --threads: Whether to use threads.
204 { return this->threads_
; }
206 // --thread-count-initial: Threads to use in initial pass.
208 thread_count_initial() const
209 { return this->thread_count_initial_
; }
211 // --thread-count-middle: Threads to use in middle pass.
213 thread_count_middle() const
214 { return this->thread_count_middle_
; }
216 // --thread-count-final: Threads to use in final pass.
218 thread_count_final() const
219 { return this->thread_count_final_
; }
221 // -z execstack, -z noexecstack
223 is_execstack_set() const
224 { return this->execstack_
!= EXECSTACK_FROM_INPUT
; }
227 is_stack_executable() const
228 { return this->execstack_
== EXECSTACK_YES
; }
231 // Don't copy this structure.
232 General_options(const General_options
&);
233 General_options
& operator=(const General_options
&);
235 friend class Command_line
;
236 friend class options::Command_line_options
;
238 // Which symbols to strip.
241 // Don't strip any symbols.
243 // Strip all symbols.
245 // Strip debugging information.
249 // Whether to mark the stack as executable.
252 // Not set on command line.
253 EXECSTACK_FROM_INPUT
,
254 // Mark the stack as executable.
256 // Mark the stack as not executable.
262 { this->export_dynamic_
= true; }
265 set_dynamic_linker(const char* arg
)
266 { this->dynamic_linker_
= arg
; }
269 add_to_search_path(const char* arg
)
270 { this->search_path_
.push_back(Search_directory(arg
, false)); }
273 add_to_search_path_with_sysroot(const char* arg
)
274 { this->search_path_
.push_back(Search_directory(arg
, true)); }
277 set_optimization_level(const char* arg
)
278 { this->optimization_level_
= atoi(arg
); }
281 set_output_file_name(const char* arg
)
282 { this->output_file_name_
= arg
; }
286 { this->is_relocatable_
= true; }
290 { this->strip_
= STRIP_ALL
; }
292 // Note: normalize_options() depends on the fact that this turns off
293 // STRIP_ALL if it were already set.
296 { this->strip_
= STRIP_DEBUG
; }
300 { this->symbolic_
= true; }
303 set_create_eh_frame_hdr()
304 { this->create_eh_frame_hdr_
= true; }
307 add_to_rpath(const char* arg
)
308 { this->rpath_
.push_back(Search_directory(arg
, false)); }
311 add_to_rpath_link(const char* arg
)
312 { this->rpath_link_
.push_back(Search_directory(arg
, false)); }
316 { this->is_shared_
= true; }
320 { this->is_static_
= true; }
324 { this->print_stats_
= true; }
327 set_sysroot(const char* arg
)
328 { this->sysroot_
= arg
; }
331 set_text_segment_address(const char* arg
)
334 this->text_segment_address_
= strtoull(arg
, &endptr
, 0);
336 || this->text_segment_address_
== -1U)
338 fprintf(stderr
, _("%s: invalid argument to -Ttext: %s\n"),
345 parse_thread_count(const char* arg
)
348 int count
= strtol(arg
, &endptr
, 0);
349 if (*endptr
!= '\0' || count
< 0)
351 fprintf(stderr
, _("%s: invalid thread count: %s\n"),
360 { this->threads_
= true; }
364 { this->threads_
= false; }
367 set_thread_count(const char* arg
)
369 int count
= this->parse_thread_count(arg
);
370 this->thread_count_initial_
= count
;
371 this->thread_count_middle_
= count
;
372 this->thread_count_final_
= count
;
376 set_thread_count_initial(const char* arg
)
377 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
380 set_thread_count_middle(const char* arg
)
381 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
384 set_thread_count_final(const char* arg
)
385 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
393 { this->execstack_
= EXECSTACK_YES
; }
397 { this->execstack_
= EXECSTACK_NO
; }
399 // Handle the -z option.
401 handle_z_option(const char*);
403 // Apply any sysroot to the directory lists.
407 bool export_dynamic_
;
408 const char* dynamic_linker_
;
409 Dir_list search_path_
;
410 int optimization_level_
;
411 const char* output_file_name_
;
412 bool is_relocatable_
;
415 bool create_eh_frame_hdr_
;
417 Dir_list rpath_link_
;
421 std::string sysroot_
;
422 uint64_t text_segment_address_
;
424 int thread_count_initial_
;
425 int thread_count_middle_
;
426 int thread_count_final_
;
427 Execstack execstack_
;
430 // The current state of the position dependent options.
432 class Position_dependent_options
435 Position_dependent_options();
437 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
438 // -rather than a shared object.
440 do_static_search() const
441 { return this->do_static_search_
; }
443 // --as-needed: Whether to add a DT_NEEDED argument only if the
444 // dynamic object is used.
447 { return this->as_needed_
; }
449 // --whole-archive: Whether to include the entire contents of an
452 include_whole_archive() const
453 { return this->include_whole_archive_
; }
457 { this->do_static_search_
= true; }
461 { this->do_static_search_
= false; }
465 { this->as_needed_
= true; }
469 { this->as_needed_
= false; }
473 { this->include_whole_archive_
= true; }
476 clear_whole_archive()
477 { this->include_whole_archive_
= false; }
480 bool do_static_search_
;
482 bool include_whole_archive_
;
485 // A single file or library argument from the command line.
487 class Input_file_argument
490 // name: file name or library name
491 // is_lib: true if name is a library name: that is, emits the leading
492 // "lib" and trailing ".so"/".a" from the name
493 // extra_search_path: an extra directory to look for the file, prior
494 // to checking the normal library search path. If this is "",
495 // then no extra directory is added.
496 // options: The position dependent options at this point in the
497 // command line, such as --whole-archive.
498 Input_file_argument()
499 : name_(), is_lib_(false), extra_search_path_(""), options_()
502 Input_file_argument(const char* name
, bool is_lib
,
503 const char* extra_search_path
,
504 const Position_dependent_options
& options
)
505 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
511 { return this->name_
.c_str(); }
513 const Position_dependent_options
&
515 { return this->options_
; }
519 { return this->is_lib_
; }
522 extra_search_path() const
524 return (this->extra_search_path_
.empty()
526 : this->extra_search_path_
.c_str());
529 // Return whether this file may require a search using the -L
532 may_need_search() const
533 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
536 // We use std::string, not const char*, here for convenience when
537 // using script files, so that we do not have to preserve the string
541 std::string extra_search_path_
;
542 Position_dependent_options options_
;
545 // A file or library, or a group, from the command line.
550 // Create a file or library argument.
551 explicit Input_argument(Input_file_argument file
)
552 : is_file_(true), file_(file
), group_(NULL
)
555 // Create a group argument.
556 explicit Input_argument(Input_file_group
* group
)
557 : is_file_(false), group_(group
)
560 // Return whether this is a file.
563 { return this->is_file_
; }
565 // Return whether this is a group.
568 { return !this->is_file_
; }
570 // Return the information about the file.
571 const Input_file_argument
&
574 gold_assert(this->is_file_
);
578 // Return the information about the group.
579 const Input_file_group
*
582 gold_assert(!this->is_file_
);
589 gold_assert(!this->is_file_
);
595 Input_file_argument file_
;
596 Input_file_group
* group_
;
599 // A group from the command line. This is a set of arguments within
600 // --start-group ... --end-group.
602 class Input_file_group
605 typedef std::vector
<Input_argument
> Files
;
606 typedef Files::const_iterator const_iterator
;
612 // Add a file to the end of the group.
614 add_file(const Input_file_argument
& arg
)
615 { this->files_
.push_back(Input_argument(arg
)); }
617 // Iterators to iterate over the group contents.
621 { return this->files_
.begin(); }
625 { return this->files_
.end(); }
631 // A list of files from the command line or a script.
633 class Input_arguments
636 typedef std::vector
<Input_argument
> Input_argument_list
;
637 typedef Input_argument_list::const_iterator const_iterator
;
640 : input_argument_list_(), in_group_(false)
645 add_file(const Input_file_argument
& arg
);
647 // Start a group (the --start-group option).
651 // End a group (the --end-group option).
655 // Return whether we are currently in a group.
658 { return this->in_group_
; }
660 // The number of entries in the list.
663 { return this->input_argument_list_
.size(); }
665 // Iterators to iterate over the list of input files.
669 { return this->input_argument_list_
.begin(); }
673 { return this->input_argument_list_
.end(); }
675 // Return whether the list is empty.
678 { return this->input_argument_list_
.empty(); }
681 Input_argument_list input_argument_list_
;
685 // All the information read from the command line.
690 typedef Input_arguments::const_iterator const_iterator
;
694 // Process the command line options. This will exit with an
695 // appropriate error message if an unrecognized option is seen.
697 process(int argc
, char** argv
);
699 // Handle a -l option.
701 process_l_option(int, char**, char*, bool);
703 // Handle a --start-group option.
705 start_group(const char* arg
);
707 // Handle a --end-group option.
709 end_group(const char* arg
);
711 // Get an option argument--a helper function for special processing.
713 get_special_argument(const char* longname
, int argc
, char** argv
,
714 const char* arg
, bool long_option
,
717 // Get the general options.
718 const General_options
&
720 { return this->options_
; }
722 // Get the position dependent options.
723 const Position_dependent_options
&
724 position_dependent_options() const
725 { return this->position_options_
; }
727 // The number of input files.
729 number_of_input_files() const
730 { return this->inputs_
.size(); }
732 // Iterators to iterate over the list of input files.
736 { return this->inputs_
.begin(); }
740 { return this->inputs_
.end(); }
743 Command_line(const Command_line
&);
744 Command_line
& operator=(const Command_line
&);
746 // Report usage error.
748 usage() ATTRIBUTE_NORETURN
;
750 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
752 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
754 // Apply a command line option.
756 apply_option(const gold::options::One_option
&, const char*);
760 add_file(const char* name
, bool is_lib
);
762 // Examine the result of processing the command-line, and verify
763 // the flags do not contradict each other or are otherwise illegal.
767 General_options options_
;
768 Position_dependent_options position_options_
;
769 Input_arguments inputs_
;
772 } // End namespace gold.
774 #endif // !defined(GOLD_OPTIONS_H)