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 // --demangle: demangle C++ symbols in our log messages.
165 { return this->demangle_; }
167 // --detect-odr-violations: Whether to search for One Defn Rule violations.
169 detect_odr_violations() const
170 { return this->detect_odr_violations_; }
172 // --eh-frame-hdr: Whether to generate an exception frame header.
174 create_eh_frame_hdr() const
175 { return this->create_eh_frame_hdr_; }
177 // --rpath: The runtime search path.
180 { return this->rpath_; }
182 // --rpath-link: The link time search patch for shared libraries.
185 { return this->rpath_link_; }
187 // --shared: Whether generating a shared object.
190 { return this->is_shared_; }
192 // --static: Whether doing a static link.
195 { return this->is_static_; }
197 // --stats: Print resource usage statistics.
200 { return this->print_stats_; }
202 // --sysroot: The system root of a cross-linker.
205 { return this->sysroot_; }
207 // -Ttext: The address of the .text section
209 text_segment_address() const
210 { return this->text_segment_address_; }
212 // Whether -Ttext was used.
214 user_set_text_segment_address() const
215 { return this->text_segment_address_ != -1U; }
217 // --threads: Whether to use threads.
220 { return this->threads_; }
222 // --thread-count-initial: Threads to use in initial pass.
224 thread_count_initial() const
225 { return this->thread_count_initial_; }
227 // --thread-count-middle: Threads to use in middle pass.
229 thread_count_middle() const
230 { return this->thread_count_middle_; }
232 // --thread-count-final: Threads to use in final pass.
234 thread_count_final() const
235 { return this->thread_count_final_; }
237 // -z execstack, -z noexecstack
239 is_execstack_set() const
240 { return this->execstack_ != EXECSTACK_FROM_INPUT; }
243 is_stack_executable() const
244 { return this->execstack_ == EXECSTACK_YES; }
247 // Don't copy this structure.
248 General_options(const General_options&);
249 General_options& operator=(const General_options&);
251 friend class Command_line;
252 friend class options::Command_line_options;
254 // Which symbols to strip.
257 // Don't strip any symbols.
259 // Strip all symbols.
261 // Strip debugging information.
265 // Whether to mark the stack as executable.
268 // Not set on command line.
269 EXECSTACK_FROM_INPUT,
270 // Mark the stack as executable.
272 // Mark the stack as not executable.
278 { this->export_dynamic_ = true; }
281 set_dynamic_linker(const char* arg)
282 { this->dynamic_linker_ = arg; }
285 add_to_search_path(const char* arg)
286 { this->search_path_.push_back(Search_directory(arg, false)); }
289 add_to_search_path_with_sysroot(const char* arg)
290 { this->search_path_.push_back(Search_directory(arg, true)); }
293 set_optimization_level(const char* arg)
294 { this->optimization_level_ = atoi(arg); }
297 set_output_file_name(const char* arg)
298 { this->output_file_name_ = arg; }
302 { this->is_relocatable_ = true; }
306 { this->strip_ = STRIP_ALL; }
308 // Note: normalize_options() depends on the fact that this turns off
309 // STRIP_ALL if it were already set.
312 { this->strip_ = STRIP_DEBUG; }
315 set_allow_shlib_undefined()
316 { this->allow_shlib_undefined_ = true; }
319 set_no_allow_shlib_undefined()
320 { this->allow_shlib_undefined_ = false; }
324 { this->symbolic_ = true; }
328 { this->demangle_ = true; }
332 { this->demangle_ = false; }
335 set_detect_odr_violations()
336 { this->detect_odr_violations_ = true; }
339 set_create_eh_frame_hdr()
340 { this->create_eh_frame_hdr_ = true; }
343 add_to_rpath(const char* arg)
344 { this->rpath_.push_back(Search_directory(arg, false)); }
347 add_to_rpath_link(const char* arg)
348 { this->rpath_link_.push_back(Search_directory(arg, false)); }
352 { this->is_shared_ = true; }
356 { this->is_static_ = true; }
360 { this->print_stats_ = true; }
363 set_sysroot(const char* arg)
364 { this->sysroot_ = arg; }
367 set_text_segment_address(const char* arg)
370 this->text_segment_address_ = strtoull(arg, &endptr, 0);
372 || this->text_segment_address_ == -1U)
374 fprintf(stderr, _("%s: invalid argument to -Ttext: %s\n"),
381 parse_thread_count(const char* arg)
384 int count = strtol(arg, &endptr, 0);
385 if (*endptr != '\0' || count < 0)
387 fprintf(stderr, _("%s: invalid thread count: %s\n"),
396 { this->threads_ = true; }
400 { this->threads_ = false; }
403 set_thread_count(const char* arg)
405 int count = this->parse_thread_count(arg);
406 this->thread_count_initial_ = count;
407 this->thread_count_middle_ = count;
408 this->thread_count_final_ = count;
412 set_thread_count_initial(const char* arg)
413 { this->thread_count_initial_ = this->parse_thread_count(arg); }
416 set_thread_count_middle(const char* arg)
417 { this->thread_count_initial_ = this->parse_thread_count(arg); }
420 set_thread_count_final(const char* arg)
421 { this->thread_count_initial_ = this->parse_thread_count(arg); }
429 { this->execstack_ = EXECSTACK_YES; }
433 { this->execstack_ = EXECSTACK_NO; }
435 // Handle the -z option.
437 handle_z_option(const char*);
439 // Apply any sysroot to the directory lists.
443 bool export_dynamic_;
444 const char* dynamic_linker_;
445 Dir_list search_path_;
446 int optimization_level_;
447 const char* output_file_name_;
448 bool is_relocatable_;
450 bool allow_shlib_undefined_;
453 bool detect_odr_violations_;
454 bool create_eh_frame_hdr_;
456 Dir_list rpath_link_;
460 std::string sysroot_;
461 uint64_t text_segment_address_;
463 int thread_count_initial_;
464 int thread_count_middle_;
465 int thread_count_final_;
466 Execstack execstack_;
469 // The current state of the position dependent options.
471 class Position_dependent_options
474 Position_dependent_options();
476 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
477 // -rather than a shared object.
479 do_static_search() const
480 { return this->do_static_search_; }
482 // --as-needed: Whether to add a DT_NEEDED argument only if the
483 // dynamic object is used.
486 { return this->as_needed_; }
488 // --whole-archive: Whether to include the entire contents of an
491 include_whole_archive() const
492 { return this->include_whole_archive_; }
496 { this->do_static_search_ = true; }
500 { this->do_static_search_ = false; }
504 { this->as_needed_ = true; }
508 { this->as_needed_ = false; }
512 { this->include_whole_archive_ = true; }
515 clear_whole_archive()
516 { this->include_whole_archive_ = false; }
519 bool do_static_search_;
521 bool include_whole_archive_;
524 // A single file or library argument from the command line.
526 class Input_file_argument
529 // name: file name or library name
530 // is_lib: true if name is a library name: that is, emits the leading
531 // "lib" and trailing ".so"/".a" from the name
532 // extra_search_path: an extra directory to look for the file, prior
533 // to checking the normal library search path. If this is "",
534 // then no extra directory is added.
535 // options: The position dependent options at this point in the
536 // command line, such as --whole-archive.
537 Input_file_argument()
538 : name_(), is_lib_(false), extra_search_path_(""), options_()
541 Input_file_argument(const char* name, bool is_lib,
542 const char* extra_search_path,
543 const Position_dependent_options& options)
544 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
550 { return this->name_.c_str(); }
552 const Position_dependent_options&
554 { return this->options_; }
558 { return this->is_lib_; }
561 extra_search_path() const
563 return (this->extra_search_path_.empty()
565 : this->extra_search_path_.c_str());
568 // Return whether this file may require a search using the -L
571 may_need_search() const
572 { return this->is_lib_ || !this->extra_search_path_.empty(); }
575 // We use std::string, not const char*, here for convenience when
576 // using script files, so that we do not have to preserve the string
580 std::string extra_search_path_;
581 Position_dependent_options options_;
584 // A file or library, or a group, from the command line.
589 // Create a file or library argument.
590 explicit Input_argument(Input_file_argument file)
591 : is_file_(true), file_(file), group_(NULL)
594 // Create a group argument.
595 explicit Input_argument(Input_file_group* group)
596 : is_file_(false), group_(group)
599 // Return whether this is a file.
602 { return this->is_file_; }
604 // Return whether this is a group.
607 { return !this->is_file_; }
609 // Return the information about the file.
610 const Input_file_argument&
613 gold_assert(this->is_file_);
617 // Return the information about the group.
618 const Input_file_group*
621 gold_assert(!this->is_file_);
628 gold_assert(!this->is_file_);
634 Input_file_argument file_;
635 Input_file_group* group_;
638 // A group from the command line. This is a set of arguments within
639 // --start-group ... --end-group.
641 class Input_file_group
644 typedef std::vector<Input_argument> Files;
645 typedef Files::const_iterator const_iterator;
651 // Add a file to the end of the group.
653 add_file(const Input_file_argument& arg)
654 { this->files_.push_back(Input_argument(arg)); }
656 // Iterators to iterate over the group contents.
660 { return this->files_.begin(); }
664 { return this->files_.end(); }
670 // A list of files from the command line or a script.
672 class Input_arguments
675 typedef std::vector<Input_argument> Input_argument_list;
676 typedef Input_argument_list::const_iterator const_iterator;
679 : input_argument_list_(), in_group_(false)
684 add_file(const Input_file_argument& arg);
686 // Start a group (the --start-group option).
690 // End a group (the --end-group option).
694 // Return whether we are currently in a group.
697 { return this->in_group_; }
699 // The number of entries in the list.
702 { return this->input_argument_list_.size(); }
704 // Iterators to iterate over the list of input files.
708 { return this->input_argument_list_.begin(); }
712 { return this->input_argument_list_.end(); }
714 // Return whether the list is empty.
717 { return this->input_argument_list_.empty(); }
720 Input_argument_list input_argument_list_;
724 // All the information read from the command line.
729 typedef Input_arguments::const_iterator const_iterator;
733 // Process the command line options. This will exit with an
734 // appropriate error message if an unrecognized option is seen.
736 process(int argc, char** argv);
738 // Process one command-line option. This takes the index of argv to
739 // process, and returns the index for the next option.
741 process_one_option(int argc, char** argv, int i, bool* no_more_options);
743 // Handle a -l option.
745 process_l_option(int, char**, char*, bool);
747 // Handle a --start-group option.
749 start_group(const char* arg);
751 // Handle a --end-group option.
753 end_group(const char* arg);
755 // Get an option argument--a helper function for special processing.
757 get_special_argument(const char* longname, int argc, char** argv,
758 const char* arg, bool long_option,
761 // Get the general options.
762 const General_options&
764 { return this->options_; }
766 // Get the position dependent options.
767 const Position_dependent_options&
768 position_dependent_options() const
769 { return this->position_options_; }
771 // The number of input files.
773 number_of_input_files() const
774 { return this->inputs_.size(); }
776 // Iterators to iterate over the list of input files.
780 { return this->inputs_.begin(); }
784 { return this->inputs_.end(); }
787 Command_line(const Command_line&);
788 Command_line& operator=(const Command_line&);
790 // Report usage error.
792 usage() ATTRIBUTE_NORETURN;
794 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
796 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
798 // Apply a command line option.
800 apply_option(const gold::options::One_option&, const char*);
804 add_file(const char* name, bool is_lib);
806 // Examine the result of processing the command-line, and verify
807 // the flags do not contradict each other or are otherwise illegal.
811 General_options options_;
812 Position_dependent_options position_options_;
813 Input_arguments inputs_;
816 } // End namespace gold.
818 #endif // !defined(GOLD_OPTIONS_H)