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
44 class Input_file_group
;
48 class Command_line_options
;
52 } // End namespace gold::options.
54 // A directory to search. For each directory we record whether it is
55 // in the sysroot. We need to know this so that, if a linker script
56 // is found within the sysroot, we will apply the sysroot to any files
57 // named by that script.
59 class Search_directory
62 // We need a default constructor because we put this in a
65 : name_(NULL
), put_in_sysroot_(false), is_in_sysroot_(false)
68 // This is the usual constructor.
69 Search_directory(const char* name
, bool put_in_sysroot
)
70 : name_(name
), put_in_sysroot_(put_in_sysroot
), is_in_sysroot_(false)
71 { gold_assert(!this->name_
.empty()); }
73 // This is called if we have a sysroot. The sysroot is prefixed to
74 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
75 // set to true for any enries which are in the sysroot (this will
76 // naturally include any entries for which put_in_sysroot_ is true).
77 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
78 // passing SYSROOT to lrealpath.
80 add_sysroot(const char* sysroot
, const char* canonical_sysroot
);
82 // Get the directory name.
85 { return this->name_
; }
87 // Return whether this directory is in the sysroot.
90 { return this->is_in_sysroot_
; }
98 // The position independent options which apply to the whole link.
99 // There are a lot of them.
101 class General_options
106 // -E: export dynamic symbols.
108 export_dynamic() const
109 { return this->export_dynamic_
; }
111 // -I: dynamic linker name.
113 dynamic_linker() const
114 { return this->dynamic_linker_
; }
116 // -L: Library search path.
117 typedef std::vector
<Search_directory
> Dir_list
;
121 { return this->search_path_
; }
123 // -O: optimization level (0: don't try to optimize output size).
125 optimization_level() const
126 { return this->optimization_level_
; }
128 // -o: Output file name.
130 output_file_name() const
131 { return this->output_file_name_
; }
133 // -r: Whether we are doing a relocatable link.
135 is_relocatable() const
136 { return this->is_relocatable_
; }
138 // -s: Strip all symbols.
141 { return this->strip_
== STRIP_ALL
; }
143 // -S: Strip debugging information.
146 { return this->strip_
== STRIP_ALL
|| this->strip_
== STRIP_DEBUG
; }
148 // -Bsymbolic: bind defined symbols locally.
151 { return this->symbolic_
; }
153 // --eh-frame-hdr: Whether to generate an exception frame header.
155 create_eh_frame_hdr() const
156 { return this->create_eh_frame_hdr_
; }
158 // --rpath: The runtime search path.
161 { return this->rpath_
; }
163 // --rpath-link: The link time search patch for shared libraries.
166 { return this->rpath_link_
; }
168 // --shared: Whether generating a shared object.
171 { return this->is_shared_
; }
173 // --static: Whether doing a static link.
176 { return this->is_static_
; }
178 // --stats: Print resource usage statistics.
181 { return this->print_stats_
; }
183 // --sysroot: The system root of a cross-linker.
186 { return this->sysroot_
; }
188 // -Ttext: The address of the .text section
190 text_segment_address() const
191 { return this->text_segment_address_
; }
193 // Whether -Ttext was used.
195 user_set_text_segment_address() const
196 { return this->text_segment_address_
!= -1U; }
198 // --threads: Whether to use threads.
201 { return this->threads_
; }
203 // --thread-count-initial: Threads to use in initial pass.
205 thread_count_initial() const
206 { return this->thread_count_initial_
; }
208 // --thread-count-middle: Threads to use in middle pass.
210 thread_count_middle() const
211 { return this->thread_count_middle_
; }
213 // --thread-count-final: Threads to use in final pass.
215 thread_count_final() const
216 { return this->thread_count_final_
; }
218 // -z execstack, -z noexecstack
220 is_execstack_set() const
221 { return this->execstack_
!= EXECSTACK_FROM_INPUT
; }
224 is_stack_executable() const
225 { return this->execstack_
== EXECSTACK_YES
; }
228 // Don't copy this structure.
229 General_options(const General_options
&);
230 General_options
& operator=(const General_options
&);
232 friend class Command_line
;
233 friend class options::Command_line_options
;
235 // Which symbols to strip.
238 // Don't strip any symbols.
240 // Strip all symbols.
242 // Strip debugging information.
246 // Whether to mark the stack as executable.
249 // Not set on command line.
250 EXECSTACK_FROM_INPUT
,
251 // Mark the stack as executable.
253 // Mark the stack as not executable.
259 { this->export_dynamic_
= true; }
262 set_dynamic_linker(const char* arg
)
263 { this->dynamic_linker_
= arg
; }
266 add_to_search_path(const char* arg
)
267 { this->search_path_
.push_back(Search_directory(arg
, false)); }
270 add_to_search_path_with_sysroot(const char* arg
)
271 { this->search_path_
.push_back(Search_directory(arg
, true)); }
274 set_optimization_level(const char* arg
)
275 { this->optimization_level_
= atoi(arg
); }
278 set_output_file_name(const char* arg
)
279 { this->output_file_name_
= arg
; }
283 { this->is_relocatable_
= true; }
287 { this->strip_
= STRIP_ALL
; }
289 // Note: normalize_options() depends on the fact that this turns off
290 // STRIP_ALL if it were already set.
293 { this->strip_
= STRIP_DEBUG
; }
297 { this->symbolic_
= true; }
300 set_create_eh_frame_hdr()
301 { this->create_eh_frame_hdr_
= true; }
304 add_to_rpath(const char* arg
)
305 { this->rpath_
.push_back(Search_directory(arg
, false)); }
308 add_to_rpath_link(const char* arg
)
309 { this->rpath_link_
.push_back(Search_directory(arg
, false)); }
313 { this->is_shared_
= true; }
317 { this->is_static_
= true; }
320 set_script(const char* arg
)
322 fprintf(stderr
, _("%s: cannot parse %s: -T/--script not yet supported\n"),
329 { this->print_stats_
= true; }
332 set_sysroot(const char* arg
)
333 { this->sysroot_
= arg
; }
336 set_text_segment_address(const char* arg
)
339 this->text_segment_address_
= strtoull(arg
, &endptr
, 0);
341 || this->text_segment_address_
== -1U)
343 fprintf(stderr
, _("%s: invalid argument to -Ttext: %s\n"),
350 parse_thread_count(const char* arg
)
353 int count
= strtol(arg
, &endptr
, 0);
354 if (*endptr
!= '\0' || count
< 0)
356 fprintf(stderr
, _("%s: invalid thread count: %s\n"),
365 { this->threads_
= true; }
369 { this->threads_
= false; }
372 set_thread_count(const char* arg
)
374 int count
= this->parse_thread_count(arg
);
375 this->thread_count_initial_
= count
;
376 this->thread_count_middle_
= count
;
377 this->thread_count_final_
= count
;
381 set_thread_count_initial(const char* arg
)
382 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
385 set_thread_count_middle(const char* arg
)
386 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
389 set_thread_count_final(const char* arg
)
390 { this->thread_count_initial_
= this->parse_thread_count(arg
); }
398 { this->execstack_
= EXECSTACK_YES
; }
402 { this->execstack_
= EXECSTACK_NO
; }
404 // Handle the -z option.
406 handle_z_option(const char*);
408 // Apply any sysroot to the directory lists.
412 bool export_dynamic_
;
413 const char* dynamic_linker_
;
414 Dir_list search_path_
;
415 int optimization_level_
;
416 const char* output_file_name_
;
417 bool is_relocatable_
;
420 bool create_eh_frame_hdr_
;
422 Dir_list rpath_link_
;
426 std::string sysroot_
;
427 uint64_t text_segment_address_
;
429 int thread_count_initial_
;
430 int thread_count_middle_
;
431 int thread_count_final_
;
432 Execstack execstack_
;
435 // The current state of the position dependent options.
437 class Position_dependent_options
440 Position_dependent_options();
442 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
443 // -rather than a shared object.
445 do_static_search() const
446 { return this->do_static_search_
; }
448 // --as-needed: Whether to add a DT_NEEDED argument only if the
449 // dynamic object is used.
452 { return this->as_needed_
; }
454 // --whole-archive: Whether to include the entire contents of an
457 include_whole_archive() const
458 { return this->include_whole_archive_
; }
462 { this->do_static_search_
= true; }
466 { this->do_static_search_
= false; }
470 { this->as_needed_
= true; }
474 { this->as_needed_
= false; }
478 { this->include_whole_archive_
= true; }
481 clear_whole_archive()
482 { this->include_whole_archive_
= false; }
485 bool do_static_search_
;
487 bool include_whole_archive_
;
490 // A single file or library argument from the command line.
492 class Input_file_argument
495 // name: file name or library name
496 // is_lib: true if name is a library name: that is, emits the leading
497 // "lib" and trailing ".so"/".a" from the name
498 // extra_search_path: an extra directory to look for the file, prior
499 // to checking the normal library search path. If this is "",
500 // then no extra directory is added.
501 // options: The position dependent options at this point in the
502 // command line, such as --whole-archive.
503 Input_file_argument()
504 : name_(), is_lib_(false), extra_search_path_(""), options_()
507 Input_file_argument(const char* name
, bool is_lib
,
508 const char* extra_search_path
,
509 const Position_dependent_options
& options
)
510 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
516 { return this->name_
.c_str(); }
518 const Position_dependent_options
&
520 { return this->options_
; }
524 { return this->is_lib_
; }
527 extra_search_path() const
529 return (this->extra_search_path_
.empty()
531 : this->extra_search_path_
.c_str());
534 // Return whether this file may require a search using the -L
537 may_need_search() const
538 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
541 // We use std::string, not const char*, here for convenience when
542 // using script files, so that we do not have to preserve the string
546 std::string extra_search_path_
;
547 Position_dependent_options options_
;
550 // A file or library, or a group, from the command line.
555 // Create a file or library argument.
556 explicit Input_argument(Input_file_argument file
)
557 : is_file_(true), file_(file
), group_(NULL
)
560 // Create a group argument.
561 explicit Input_argument(Input_file_group
* group
)
562 : is_file_(false), group_(group
)
565 // Return whether this is a file.
568 { return this->is_file_
; }
570 // Return whether this is a group.
573 { return !this->is_file_
; }
575 // Return the information about the file.
576 const Input_file_argument
&
579 gold_assert(this->is_file_
);
583 // Return the information about the group.
584 const Input_file_group
*
587 gold_assert(!this->is_file_
);
594 gold_assert(!this->is_file_
);
600 Input_file_argument file_
;
601 Input_file_group
* group_
;
604 // A group from the command line. This is a set of arguments within
605 // --start-group ... --end-group.
607 class Input_file_group
610 typedef std::vector
<Input_argument
> Files
;
611 typedef Files::const_iterator const_iterator
;
617 // Add a file to the end of the group.
619 add_file(const Input_file_argument
& arg
)
620 { this->files_
.push_back(Input_argument(arg
)); }
622 // Iterators to iterate over the group contents.
626 { return this->files_
.begin(); }
630 { return this->files_
.end(); }
636 // A list of files from the command line or a script.
638 class Input_arguments
641 typedef std::vector
<Input_argument
> Input_argument_list
;
642 typedef Input_argument_list::const_iterator const_iterator
;
645 : input_argument_list_(), in_group_(false)
650 add_file(const Input_file_argument
& arg
);
652 // Start a group (the --start-group option).
656 // End a group (the --end-group option).
660 // Return whether we are currently in a group.
663 { return this->in_group_
; }
665 // The number of entries in the list.
668 { return this->input_argument_list_
.size(); }
670 // Iterators to iterate over the list of input files.
674 { return this->input_argument_list_
.begin(); }
678 { return this->input_argument_list_
.end(); }
680 // Return whether the list is empty.
683 { return this->input_argument_list_
.empty(); }
686 Input_argument_list input_argument_list_
;
690 // All the information read from the command line.
695 typedef Input_arguments::const_iterator const_iterator
;
699 // Process the command line options. This will exit with an
700 // appropriate error message if an unrecognized option is seen.
702 process(int argc
, char** argv
);
704 // Handle a -l option.
706 process_l_option(int, char**, char*);
708 // Handle a --start-group option.
710 start_group(const char* arg
);
712 // Handle a --end-group option.
714 end_group(const char* arg
);
716 // Get the general options.
717 const General_options
&
719 { return this->options_
; }
721 // The number of input files.
723 number_of_input_files() const
724 { return this->inputs_
.size(); }
726 // Iterators to iterate over the list of input files.
730 { return this->inputs_
.begin(); }
734 { return this->inputs_
.end(); }
737 Command_line(const Command_line
&);
738 Command_line
& operator=(const Command_line
&);
740 // Report usage error.
742 usage() ATTRIBUTE_NORETURN
;
744 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
746 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
748 // Apply a command line option.
750 apply_option(const gold::options::One_option
&, const char*);
754 add_file(const char* name
, bool is_lib
);
756 // Examine the result of processing the command-line, and verify
757 // the flags do not contradict each other or are otherwise illegal.
761 General_options options_
;
762 Position_dependent_options position_options_
;
763 Input_arguments inputs_
;
766 } // End namespace gold.
768 #endif // !defined(GOLD_OPTIONS_H)