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 // --allow-shlib-undefined: do not warn about unresolved symbols in
152 // --shared libraries.
154 allow_shlib_undefined() const
155 { return this->allow_shlib_undefined_; }
157 // -Bsymbolic: bind defined symbols locally.
160 { return this->symbolic_; }
162 // --detect-odr-violations: Whether to search for One Defn Rule violations.
164 detect_odr_violations() const
165 { return this->detect_odr_violations_; }
167 // --eh-frame-hdr: Whether to generate an exception frame header.
169 create_eh_frame_hdr() const
170 { return this->create_eh_frame_hdr_; }
172 // --rpath: The runtime search path.
175 { return this->rpath_; }
177 // --rpath-link: The link time search patch for shared libraries.
180 { return this->rpath_link_; }
182 // --shared: Whether generating a shared object.
185 { return this->is_shared_; }
187 // --static: Whether doing a static link.
190 { return this->is_static_; }
192 // --stats: Print resource usage statistics.
195 { return this->print_stats_; }
197 // --sysroot: The system root of a cross-linker.
200 { return this->sysroot_; }
202 // -Ttext: The address of the .text section
204 text_segment_address() const
205 { return this->text_segment_address_; }
207 // Whether -Ttext was used.
209 user_set_text_segment_address() const
210 { return this->text_segment_address_ != -1U; }
212 // --threads: Whether to use threads.
215 { return this->threads_; }
217 // --thread-count-initial: Threads to use in initial pass.
219 thread_count_initial() const
220 { return this->thread_count_initial_; }
222 // --thread-count-middle: Threads to use in middle pass.
224 thread_count_middle() const
225 { return this->thread_count_middle_; }
227 // --thread-count-final: Threads to use in final pass.
229 thread_count_final() const
230 { return this->thread_count_final_; }
232 // -z execstack, -z noexecstack
234 is_execstack_set() const
235 { return this->execstack_ != EXECSTACK_FROM_INPUT; }
238 is_stack_executable() const
239 { return this->execstack_ == EXECSTACK_YES; }
242 // Don't copy this structure.
243 General_options(const General_options&);
244 General_options& operator=(const General_options&);
246 friend class Command_line;
247 friend class options::Command_line_options;
249 // Which symbols to strip.
252 // Don't strip any symbols.
254 // Strip all symbols.
256 // Strip debugging information.
260 // Whether to mark the stack as executable.
263 // Not set on command line.
264 EXECSTACK_FROM_INPUT,
265 // Mark the stack as executable.
267 // Mark the stack as not executable.
273 { this->export_dynamic_ = true; }
276 set_dynamic_linker(const char* arg)
277 { this->dynamic_linker_ = arg; }
280 add_to_search_path(const char* arg)
281 { this->search_path_.push_back(Search_directory(arg, false)); }
284 add_to_search_path_with_sysroot(const char* arg)
285 { this->search_path_.push_back(Search_directory(arg, true)); }
288 set_optimization_level(const char* arg)
289 { this->optimization_level_ = atoi(arg); }
292 set_output_file_name(const char* arg)
293 { this->output_file_name_ = arg; }
297 { this->is_relocatable_ = true; }
301 { this->strip_ = STRIP_ALL; }
303 // Note: normalize_options() depends on the fact that this turns off
304 // STRIP_ALL if it were already set.
307 { this->strip_ = STRIP_DEBUG; }
310 set_allow_shlib_undefined()
311 { this->allow_shlib_undefined_ = true; }
314 set_no_allow_shlib_undefined()
315 { this->allow_shlib_undefined_ = false; }
319 { this->symbolic_ = true; }
322 set_detect_odr_violations()
323 { this->detect_odr_violations_ = true; }
326 set_create_eh_frame_hdr()
327 { this->create_eh_frame_hdr_ = true; }
330 add_to_rpath(const char* arg)
331 { this->rpath_.push_back(Search_directory(arg, false)); }
334 add_to_rpath_link(const char* arg)
335 { this->rpath_link_.push_back(Search_directory(arg, false)); }
339 { this->is_shared_ = true; }
343 { this->is_static_ = true; }
347 { this->print_stats_ = true; }
350 set_sysroot(const char* arg)
351 { this->sysroot_ = arg; }
354 set_text_segment_address(const char* arg)
357 this->text_segment_address_ = strtoull(arg, &endptr, 0);
359 || this->text_segment_address_ == -1U)
361 fprintf(stderr, _("%s: invalid argument to -Ttext: %s\n"),
368 parse_thread_count(const char* arg)
371 int count = strtol(arg, &endptr, 0);
372 if (*endptr != '\0' || count < 0)
374 fprintf(stderr, _("%s: invalid thread count: %s\n"),
383 { this->threads_ = true; }
387 { this->threads_ = false; }
390 set_thread_count(const char* arg)
392 int count = this->parse_thread_count(arg);
393 this->thread_count_initial_ = count;
394 this->thread_count_middle_ = count;
395 this->thread_count_final_ = count;
399 set_thread_count_initial(const char* arg)
400 { this->thread_count_initial_ = this->parse_thread_count(arg); }
403 set_thread_count_middle(const char* arg)
404 { this->thread_count_initial_ = this->parse_thread_count(arg); }
407 set_thread_count_final(const char* arg)
408 { this->thread_count_initial_ = this->parse_thread_count(arg); }
416 { this->execstack_ = EXECSTACK_YES; }
420 { this->execstack_ = EXECSTACK_NO; }
422 // Handle the -z option.
424 handle_z_option(const char*);
426 // Apply any sysroot to the directory lists.
430 bool export_dynamic_;
431 const char* dynamic_linker_;
432 Dir_list search_path_;
433 int optimization_level_;
434 const char* output_file_name_;
435 bool is_relocatable_;
437 bool allow_shlib_undefined_;
439 bool detect_odr_violations_;
440 bool create_eh_frame_hdr_;
442 Dir_list rpath_link_;
446 std::string sysroot_;
447 uint64_t text_segment_address_;
449 int thread_count_initial_;
450 int thread_count_middle_;
451 int thread_count_final_;
452 Execstack execstack_;
455 // The current state of the position dependent options.
457 class Position_dependent_options
460 Position_dependent_options();
462 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
463 // -rather than a shared object.
465 do_static_search() const
466 { return this->do_static_search_; }
468 // --as-needed: Whether to add a DT_NEEDED argument only if the
469 // dynamic object is used.
472 { return this->as_needed_; }
474 // --whole-archive: Whether to include the entire contents of an
477 include_whole_archive() const
478 { return this->include_whole_archive_; }
482 { this->do_static_search_ = true; }
486 { this->do_static_search_ = false; }
490 { this->as_needed_ = true; }
494 { this->as_needed_ = false; }
498 { this->include_whole_archive_ = true; }
501 clear_whole_archive()
502 { this->include_whole_archive_ = false; }
505 bool do_static_search_;
507 bool include_whole_archive_;
510 // A single file or library argument from the command line.
512 class Input_file_argument
515 // name: file name or library name
516 // is_lib: true if name is a library name: that is, emits the leading
517 // "lib" and trailing ".so"/".a" from the name
518 // extra_search_path: an extra directory to look for the file, prior
519 // to checking the normal library search path. If this is "",
520 // then no extra directory is added.
521 // options: The position dependent options at this point in the
522 // command line, such as --whole-archive.
523 Input_file_argument()
524 : name_(), is_lib_(false), extra_search_path_(""), options_()
527 Input_file_argument(const char* name, bool is_lib,
528 const char* extra_search_path,
529 const Position_dependent_options& options)
530 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
536 { return this->name_.c_str(); }
538 const Position_dependent_options&
540 { return this->options_; }
544 { return this->is_lib_; }
547 extra_search_path() const
549 return (this->extra_search_path_.empty()
551 : this->extra_search_path_.c_str());
554 // Return whether this file may require a search using the -L
557 may_need_search() const
558 { return this->is_lib_ || !this->extra_search_path_.empty(); }
561 // We use std::string, not const char*, here for convenience when
562 // using script files, so that we do not have to preserve the string
566 std::string extra_search_path_;
567 Position_dependent_options options_;
570 // A file or library, or a group, from the command line.
575 // Create a file or library argument.
576 explicit Input_argument(Input_file_argument file)
577 : is_file_(true), file_(file), group_(NULL)
580 // Create a group argument.
581 explicit Input_argument(Input_file_group* group)
582 : is_file_(false), group_(group)
585 // Return whether this is a file.
588 { return this->is_file_; }
590 // Return whether this is a group.
593 { return !this->is_file_; }
595 // Return the information about the file.
596 const Input_file_argument&
599 gold_assert(this->is_file_);
603 // Return the information about the group.
604 const Input_file_group*
607 gold_assert(!this->is_file_);
614 gold_assert(!this->is_file_);
620 Input_file_argument file_;
621 Input_file_group* group_;
624 // A group from the command line. This is a set of arguments within
625 // --start-group ... --end-group.
627 class Input_file_group
630 typedef std::vector<Input_argument> Files;
631 typedef Files::const_iterator const_iterator;
637 // Add a file to the end of the group.
639 add_file(const Input_file_argument& arg)
640 { this->files_.push_back(Input_argument(arg)); }
642 // Iterators to iterate over the group contents.
646 { return this->files_.begin(); }
650 { return this->files_.end(); }
656 // A list of files from the command line or a script.
658 class Input_arguments
661 typedef std::vector<Input_argument> Input_argument_list;
662 typedef Input_argument_list::const_iterator const_iterator;
665 : input_argument_list_(), in_group_(false)
670 add_file(const Input_file_argument& arg);
672 // Start a group (the --start-group option).
676 // End a group (the --end-group option).
680 // Return whether we are currently in a group.
683 { return this->in_group_; }
685 // The number of entries in the list.
688 { return this->input_argument_list_.size(); }
690 // Iterators to iterate over the list of input files.
694 { return this->input_argument_list_.begin(); }
698 { return this->input_argument_list_.end(); }
700 // Return whether the list is empty.
703 { return this->input_argument_list_.empty(); }
706 Input_argument_list input_argument_list_;
710 // All the information read from the command line.
715 typedef Input_arguments::const_iterator const_iterator;
719 // Process the command line options. This will exit with an
720 // appropriate error message if an unrecognized option is seen.
722 process(int argc, char** argv);
724 // Process one command-line option. This takes the index of argv to
725 // process, and returns the index for the next option.
727 process_one_option(int argc, char** argv, int i, bool* no_more_options);
729 // Handle a -l option.
731 process_l_option(int, char**, char*, bool);
733 // Handle a --start-group option.
735 start_group(const char* arg);
737 // Handle a --end-group option.
739 end_group(const char* arg);
741 // Get an option argument--a helper function for special processing.
743 get_special_argument(const char* longname, int argc, char** argv,
744 const char* arg, bool long_option,
747 // Get the general options.
748 const General_options&
750 { return this->options_; }
752 // Get the position dependent options.
753 const Position_dependent_options&
754 position_dependent_options() const
755 { return this->position_options_; }
757 // The number of input files.
759 number_of_input_files() const
760 { return this->inputs_.size(); }
762 // Iterators to iterate over the list of input files.
766 { return this->inputs_.begin(); }
770 { return this->inputs_.end(); }
773 Command_line(const Command_line&);
774 Command_line& operator=(const Command_line&);
776 // Report usage error.
778 usage() ATTRIBUTE_NORETURN;
780 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
782 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
784 // Apply a command line option.
786 apply_option(const gold::options::One_option&, const char*);
790 add_file(const char* name, bool is_lib);
792 // Examine the result of processing the command-line, and verify
793 // the flags do not contradict each other or are otherwise illegal.
797 General_options options_;
798 Position_dependent_options position_options_;
799 Input_arguments inputs_;
802 } // End namespace gold.
804 #endif // !defined(GOLD_OPTIONS_H)