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 // --detect-odr-violations: Whether to search for One Defn Rule violations.
158 detect_odr_violations() const
159 { return this->detect_odr_violations_; }
161 // --eh-frame-hdr: Whether to generate an exception frame header.
163 create_eh_frame_hdr() const
164 { return this->create_eh_frame_hdr_; }
166 // --rpath: The runtime search path.
169 { return this->rpath_; }
171 // --rpath-link: The link time search patch for shared libraries.
174 { return this->rpath_link_; }
176 // --shared: Whether generating a shared object.
179 { return this->is_shared_; }
181 // --static: Whether doing a static link.
184 { return this->is_static_; }
186 // --stats: Print resource usage statistics.
189 { return this->print_stats_; }
191 // --sysroot: The system root of a cross-linker.
194 { return this->sysroot_; }
196 // -Ttext: The address of the .text section
198 text_segment_address() const
199 { return this->text_segment_address_; }
201 // Whether -Ttext was used.
203 user_set_text_segment_address() const
204 { return this->text_segment_address_ != -1U; }
206 // --threads: Whether to use threads.
209 { return this->threads_; }
211 // --thread-count-initial: Threads to use in initial pass.
213 thread_count_initial() const
214 { return this->thread_count_initial_; }
216 // --thread-count-middle: Threads to use in middle pass.
218 thread_count_middle() const
219 { return this->thread_count_middle_; }
221 // --thread-count-final: Threads to use in final pass.
223 thread_count_final() const
224 { return this->thread_count_final_; }
226 // -z execstack, -z noexecstack
228 is_execstack_set() const
229 { return this->execstack_ != EXECSTACK_FROM_INPUT; }
232 is_stack_executable() const
233 { return this->execstack_ == EXECSTACK_YES; }
236 // Don't copy this structure.
237 General_options(const General_options&);
238 General_options& operator=(const General_options&);
240 friend class Command_line;
241 friend class options::Command_line_options;
243 // Which symbols to strip.
246 // Don't strip any symbols.
248 // Strip all symbols.
250 // Strip debugging information.
254 // Whether to mark the stack as executable.
257 // Not set on command line.
258 EXECSTACK_FROM_INPUT,
259 // Mark the stack as executable.
261 // Mark the stack as not executable.
267 { this->export_dynamic_ = true; }
270 set_dynamic_linker(const char* arg)
271 { this->dynamic_linker_ = arg; }
274 add_to_search_path(const char* arg)
275 { this->search_path_.push_back(Search_directory(arg, false)); }
278 add_to_search_path_with_sysroot(const char* arg)
279 { this->search_path_.push_back(Search_directory(arg, true)); }
282 set_optimization_level(const char* arg)
283 { this->optimization_level_ = atoi(arg); }
286 set_output_file_name(const char* arg)
287 { this->output_file_name_ = arg; }
291 { this->is_relocatable_ = true; }
295 { this->strip_ = STRIP_ALL; }
297 // Note: normalize_options() depends on the fact that this turns off
298 // STRIP_ALL if it were already set.
301 { this->strip_ = STRIP_DEBUG; }
305 { this->symbolic_ = true; }
308 set_detect_odr_violations()
309 { this->detect_odr_violations_ = true; }
312 set_create_eh_frame_hdr()
313 { this->create_eh_frame_hdr_ = true; }
316 add_to_rpath(const char* arg)
317 { this->rpath_.push_back(Search_directory(arg, false)); }
320 add_to_rpath_link(const char* arg)
321 { this->rpath_link_.push_back(Search_directory(arg, false)); }
325 { this->is_shared_ = true; }
329 { this->is_static_ = true; }
333 { this->print_stats_ = true; }
336 set_sysroot(const char* arg)
337 { this->sysroot_ = arg; }
340 set_text_segment_address(const char* arg)
343 this->text_segment_address_ = strtoull(arg, &endptr, 0);
345 || this->text_segment_address_ == -1U)
347 fprintf(stderr, _("%s: invalid argument to -Ttext: %s\n"),
354 parse_thread_count(const char* arg)
357 int count = strtol(arg, &endptr, 0);
358 if (*endptr != '\0' || count < 0)
360 fprintf(stderr, _("%s: invalid thread count: %s\n"),
369 { this->threads_ = true; }
373 { this->threads_ = false; }
376 set_thread_count(const char* arg)
378 int count = this->parse_thread_count(arg);
379 this->thread_count_initial_ = count;
380 this->thread_count_middle_ = count;
381 this->thread_count_final_ = count;
385 set_thread_count_initial(const char* arg)
386 { this->thread_count_initial_ = this->parse_thread_count(arg); }
389 set_thread_count_middle(const char* arg)
390 { this->thread_count_initial_ = this->parse_thread_count(arg); }
393 set_thread_count_final(const char* arg)
394 { this->thread_count_initial_ = this->parse_thread_count(arg); }
402 { this->execstack_ = EXECSTACK_YES; }
406 { this->execstack_ = EXECSTACK_NO; }
408 // Handle the -z option.
410 handle_z_option(const char*);
412 // Apply any sysroot to the directory lists.
416 bool export_dynamic_;
417 const char* dynamic_linker_;
418 Dir_list search_path_;
419 int optimization_level_;
420 const char* output_file_name_;
421 bool is_relocatable_;
424 bool detect_odr_violations_;
425 bool create_eh_frame_hdr_;
427 Dir_list rpath_link_;
431 std::string sysroot_;
432 uint64_t text_segment_address_;
434 int thread_count_initial_;
435 int thread_count_middle_;
436 int thread_count_final_;
437 Execstack execstack_;
440 // The current state of the position dependent options.
442 class Position_dependent_options
445 Position_dependent_options();
447 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
448 // -rather than a shared object.
450 do_static_search() const
451 { return this->do_static_search_; }
453 // --as-needed: Whether to add a DT_NEEDED argument only if the
454 // dynamic object is used.
457 { return this->as_needed_; }
459 // --whole-archive: Whether to include the entire contents of an
462 include_whole_archive() const
463 { return this->include_whole_archive_; }
467 { this->do_static_search_ = true; }
471 { this->do_static_search_ = false; }
475 { this->as_needed_ = true; }
479 { this->as_needed_ = false; }
483 { this->include_whole_archive_ = true; }
486 clear_whole_archive()
487 { this->include_whole_archive_ = false; }
490 bool do_static_search_;
492 bool include_whole_archive_;
495 // A single file or library argument from the command line.
497 class Input_file_argument
500 // name: file name or library name
501 // is_lib: true if name is a library name: that is, emits the leading
502 // "lib" and trailing ".so"/".a" from the name
503 // extra_search_path: an extra directory to look for the file, prior
504 // to checking the normal library search path. If this is "",
505 // then no extra directory is added.
506 // options: The position dependent options at this point in the
507 // command line, such as --whole-archive.
508 Input_file_argument()
509 : name_(), is_lib_(false), extra_search_path_(""), options_()
512 Input_file_argument(const char* name, bool is_lib,
513 const char* extra_search_path,
514 const Position_dependent_options& options)
515 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
521 { return this->name_.c_str(); }
523 const Position_dependent_options&
525 { return this->options_; }
529 { return this->is_lib_; }
532 extra_search_path() const
534 return (this->extra_search_path_.empty()
536 : this->extra_search_path_.c_str());
539 // Return whether this file may require a search using the -L
542 may_need_search() const
543 { return this->is_lib_ || !this->extra_search_path_.empty(); }
546 // We use std::string, not const char*, here for convenience when
547 // using script files, so that we do not have to preserve the string
551 std::string extra_search_path_;
552 Position_dependent_options options_;
555 // A file or library, or a group, from the command line.
560 // Create a file or library argument.
561 explicit Input_argument(Input_file_argument file)
562 : is_file_(true), file_(file), group_(NULL)
565 // Create a group argument.
566 explicit Input_argument(Input_file_group* group)
567 : is_file_(false), group_(group)
570 // Return whether this is a file.
573 { return this->is_file_; }
575 // Return whether this is a group.
578 { return !this->is_file_; }
580 // Return the information about the file.
581 const Input_file_argument&
584 gold_assert(this->is_file_);
588 // Return the information about the group.
589 const Input_file_group*
592 gold_assert(!this->is_file_);
599 gold_assert(!this->is_file_);
605 Input_file_argument file_;
606 Input_file_group* group_;
609 // A group from the command line. This is a set of arguments within
610 // --start-group ... --end-group.
612 class Input_file_group
615 typedef std::vector<Input_argument> Files;
616 typedef Files::const_iterator const_iterator;
622 // Add a file to the end of the group.
624 add_file(const Input_file_argument& arg)
625 { this->files_.push_back(Input_argument(arg)); }
627 // Iterators to iterate over the group contents.
631 { return this->files_.begin(); }
635 { return this->files_.end(); }
641 // A list of files from the command line or a script.
643 class Input_arguments
646 typedef std::vector<Input_argument> Input_argument_list;
647 typedef Input_argument_list::const_iterator const_iterator;
650 : input_argument_list_(), in_group_(false)
655 add_file(const Input_file_argument& arg);
657 // Start a group (the --start-group option).
661 // End a group (the --end-group option).
665 // Return whether we are currently in a group.
668 { return this->in_group_; }
670 // The number of entries in the list.
673 { return this->input_argument_list_.size(); }
675 // Iterators to iterate over the list of input files.
679 { return this->input_argument_list_.begin(); }
683 { return this->input_argument_list_.end(); }
685 // Return whether the list is empty.
688 { return this->input_argument_list_.empty(); }
691 Input_argument_list input_argument_list_;
695 // All the information read from the command line.
700 typedef Input_arguments::const_iterator const_iterator;
704 // Process the command line options. This will exit with an
705 // appropriate error message if an unrecognized option is seen.
707 process(int argc, char** argv);
709 // Process one command-line option. This takes the index of argv to
710 // process, and returns the index for the next option.
712 process_one_option(int argc, char** argv, int i, bool* no_more_options);
714 // Handle a -l option.
716 process_l_option(int, char**, char*, bool);
718 // Handle a --start-group option.
720 start_group(const char* arg);
722 // Handle a --end-group option.
724 end_group(const char* arg);
726 // Get an option argument--a helper function for special processing.
728 get_special_argument(const char* longname, int argc, char** argv,
729 const char* arg, bool long_option,
732 // Get the general options.
733 const General_options&
735 { return this->options_; }
737 // Get the position dependent options.
738 const Position_dependent_options&
739 position_dependent_options() const
740 { return this->position_options_; }
742 // The number of input files.
744 number_of_input_files() const
745 { return this->inputs_.size(); }
747 // Iterators to iterate over the list of input files.
751 { return this->inputs_.begin(); }
755 { return this->inputs_.end(); }
758 Command_line(const Command_line&);
759 Command_line& operator=(const Command_line&);
761 // Report usage error.
763 usage() ATTRIBUTE_NORETURN;
765 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
767 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
769 // Apply a command line option.
771 apply_option(const gold::options::One_option&, const char*);
775 add_file(const char* name, bool is_lib);
777 // Examine the result of processing the command-line, and verify
778 // the flags do not contradict each other or are otherwise illegal.
782 General_options options_;
783 Position_dependent_options position_options_;
784 Input_arguments inputs_;
787 } // End namespace gold.
789 #endif // !defined(GOLD_OPTIONS_H)