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;
52 class Command_line_options;
55 struct One_debug_option;
57 } // End namespace gold::options.
59 // A directory to search. For each directory we record whether it is
60 // in the sysroot. We need to know this so that, if a linker script
61 // is found within the sysroot, we will apply the sysroot to any files
62 // named by that script.
64 class Search_directory
67 // We need a default constructor because we put this in a
70 : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
73 // This is the usual constructor.
74 Search_directory(const char* name, bool put_in_sysroot)
75 : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
76 { gold_assert(!this->name_.empty()); }
78 // This is called if we have a sysroot. The sysroot is prefixed to
79 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
80 // set to true for any enries which are in the sysroot (this will
81 // naturally include any entries for which put_in_sysroot_ is true).
82 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
83 // passing SYSROOT to lrealpath.
85 add_sysroot(const char* sysroot, const char* canonical_sysroot);
87 // Get the directory name.
90 { return this->name_; }
92 // Return whether this directory is in the sysroot.
95 { return this->is_in_sysroot_; }
103 // The position independent options which apply to the whole link.
104 // There are a lot of them.
106 class General_options
111 // -E: export dynamic symbols.
113 export_dynamic() const
114 { return this->export_dynamic_; }
116 // -I: dynamic linker name.
118 dynamic_linker() const
119 { return this->dynamic_linker_; }
121 // -L: Library search path.
122 typedef std::vector<Search_directory> Dir_list;
126 { return this->search_path_; }
128 // -O: optimization level (0: don't try to optimize output size).
130 optimization_level() const
131 { return this->optimization_level_; }
133 // -o: Output file name.
135 output_file_name() const
136 { return this->output_file_name_; }
138 // -r: Whether we are doing a relocatable link.
140 is_relocatable() const
141 { return this->is_relocatable_; }
143 // -s: Strip all symbols.
146 { return this->strip_ == STRIP_ALL; }
148 // -S: Strip debugging information.
151 { return this->strip_ == STRIP_ALL || this->strip_ == STRIP_DEBUG; }
153 // --strip-debug-gdb: strip only debugging information that's not
154 // used by gdb (at least, for gdb versions <= 6.7).
156 strip_debug_gdb() const
157 { return this->strip_debug() || this->strip_ == STRIP_DEBUG_UNUSED_BY_GDB; }
159 // --allow-shlib-undefined: do not warn about unresolved symbols in
160 // --shared libraries.
162 allow_shlib_undefined() const
163 { return this->allow_shlib_undefined_; }
165 // -Bsymbolic: bind defined symbols locally.
168 { return this->symbolic_; }
170 // --compress-debug-sections: compress .debug_* sections in the
171 // output file using the given compression method. This is useful
172 // when the tools (such as gdb) support compressed sections.
174 compress_debug_sections() const
175 { return this->compress_debug_sections_ != NO_COMPRESSION; }
178 zlib_compress_debug_sections() const
179 { return this->compress_debug_sections_ == ZLIB_COMPRESSION; }
181 // --demangle: demangle C++ symbols in our log messages.
184 { return this->demangle_; }
186 // --detect-odr-violations: Whether to search for One Defn Rule violations.
188 detect_odr_violations() const
189 { return this->detect_odr_violations_; }
191 // --eh-frame-hdr: Whether to generate an exception frame header.
193 create_eh_frame_hdr() const
194 { return this->create_eh_frame_hdr_; }
196 // --rpath: The runtime search path.
199 { return this->rpath_; }
201 // --rpath-link: The link time search patch for shared libraries.
204 { return this->rpath_link_; }
206 // --shared: Whether generating a shared object.
209 { return this->is_shared_; }
211 // --static: Whether doing a static link.
214 { return this->is_static_; }
216 // --stats: Print resource usage statistics.
219 { return this->print_stats_; }
221 // --sysroot: The system root of a cross-linker.
224 { return this->sysroot_; }
226 // -Ttext: The address of the .text section
228 text_segment_address() const
229 { return this->text_segment_address_; }
231 // Whether -Ttext was used.
233 user_set_text_segment_address() const
234 { return this->text_segment_address_ != -1U; }
236 // --threads: Whether to use threads.
239 { return this->threads_; }
241 // --thread-count-initial: Threads to use in initial pass.
243 thread_count_initial() const
244 { return this->thread_count_initial_; }
246 // --thread-count-middle: Threads to use in middle pass.
248 thread_count_middle() const
249 { return this->thread_count_middle_; }
251 // --thread-count-final: Threads to use in final pass.
253 thread_count_final() const
254 { return this->thread_count_final_; }
256 // -z execstack, -z noexecstack
258 is_execstack_set() const
259 { return this->execstack_ != EXECSTACK_FROM_INPUT; }
262 is_stack_executable() const
263 { return this->execstack_ == EXECSTACK_YES; }
268 { return this->debug_; }
271 // Don't copy this structure.
272 General_options(const General_options&);
273 General_options& operator=(const General_options&);
275 friend class Command_line;
276 friend class options::Command_line_options;
278 // Which symbols to strip.
281 // Don't strip any symbols.
283 // Strip all symbols.
285 // Strip debugging information.
287 // Strip debugging information that's not used by gdb (at least <= 6.7)
288 STRIP_DEBUG_UNUSED_BY_GDB
291 // Whether to mark the stack as executable.
294 // Not set on command line.
295 EXECSTACK_FROM_INPUT,
296 // Mark the stack as executable.
298 // Mark the stack as not executable.
302 // What compression method to use
303 enum CompressionMethod
311 { this->export_dynamic_ = true; }
314 set_dynamic_linker(const char* arg)
315 { this->dynamic_linker_ = arg; }
318 add_to_search_path(const char* arg)
319 { this->search_path_.push_back(Search_directory(arg, false)); }
322 add_to_search_path_with_sysroot(const char* arg)
323 { this->search_path_.push_back(Search_directory(arg, true)); }
326 set_optimization_level(const char* arg)
327 { this->optimization_level_ = atoi(arg); }
330 set_output_file_name(const char* arg)
331 { this->output_file_name_ = arg; }
335 { this->is_relocatable_ = true; }
339 { this->strip_ = STRIP_ALL; }
341 // Note: normalize_options() depends on the fact that this turns off
342 // STRIP_ALL if it were already set.
345 { this->strip_ = STRIP_DEBUG; }
348 set_strip_debug_gdb()
349 { this->strip_ = STRIP_DEBUG_UNUSED_BY_GDB; }
352 set_allow_shlib_undefined()
353 { this->allow_shlib_undefined_ = true; }
356 set_no_allow_shlib_undefined()
357 { this->allow_shlib_undefined_ = false; }
361 { this->symbolic_ = true; }
363 void set_compress_debug_symbols(const char* arg)
365 if (strcmp(arg, "none") == 0)
366 this->compress_debug_sections_ = NO_COMPRESSION;
368 else if (strcmp(arg, "zlib") == 0)
369 this->compress_debug_sections_ = ZLIB_COMPRESSION;
372 gold_fatal(_("Unsupported argument to --compress-debug-symbols: %s"),
378 { this->demangle_ = true; }
382 { this->demangle_ = false; }
385 set_detect_odr_violations()
386 { this->detect_odr_violations_ = true; }
389 set_create_eh_frame_hdr()
390 { this->create_eh_frame_hdr_ = true; }
393 add_to_rpath(const char* arg)
394 { this->rpath_.push_back(Search_directory(arg, false)); }
397 add_to_rpath_link(const char* arg)
398 { this->rpath_link_.push_back(Search_directory(arg, false)); }
402 { this->is_shared_ = true; }
406 { this->is_static_ = true; }
410 { this->print_stats_ = true; }
413 set_sysroot(const char* arg)
414 { this->sysroot_ = arg; }
417 set_text_segment_address(const char* arg)
420 this->text_segment_address_ = strtoull(arg, &endptr, 0);
422 || this->text_segment_address_ == -1U)
424 fprintf(stderr, _("%s: invalid argument to -Ttext: %s\n"),
431 parse_thread_count(const char* arg)
434 int count = strtol(arg, &endptr, 0);
435 if (*endptr != '\0' || count < 0)
437 fprintf(stderr, _("%s: invalid thread count: %s\n"),
446 { this->threads_ = true; }
450 { this->threads_ = false; }
453 set_thread_count(const char* arg)
455 int count = this->parse_thread_count(arg);
456 this->thread_count_initial_ = count;
457 this->thread_count_middle_ = count;
458 this->thread_count_final_ = count;
462 set_thread_count_initial(const char* arg)
463 { this->thread_count_initial_ = this->parse_thread_count(arg); }
466 set_thread_count_middle(const char* arg)
467 { this->thread_count_initial_ = this->parse_thread_count(arg); }
470 set_thread_count_final(const char* arg)
471 { this->thread_count_initial_ = this->parse_thread_count(arg); }
479 { this->execstack_ = EXECSTACK_YES; }
483 { this->execstack_ = EXECSTACK_NO; }
486 set_debug(unsigned int flags)
487 { this->debug_ = flags; }
489 // Handle the -z option.
491 handle_z_option(const char*);
493 // Handle the --debug option.
495 handle_debug_option(const char*);
497 // Apply any sysroot to the directory lists.
501 bool export_dynamic_;
502 const char* dynamic_linker_;
503 Dir_list search_path_;
504 int optimization_level_;
505 const char* output_file_name_;
506 bool is_relocatable_;
508 bool allow_shlib_undefined_;
510 CompressionMethod compress_debug_sections_;
512 bool detect_odr_violations_;
513 bool create_eh_frame_hdr_;
515 Dir_list rpath_link_;
519 std::string sysroot_;
520 uint64_t text_segment_address_;
522 int thread_count_initial_;
523 int thread_count_middle_;
524 int thread_count_final_;
525 Execstack execstack_;
529 // The current state of the position dependent options.
531 class Position_dependent_options
534 Position_dependent_options();
536 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
537 // -rather than a shared object.
539 do_static_search() const
540 { return this->do_static_search_; }
542 // --as-needed: Whether to add a DT_NEEDED argument only if the
543 // dynamic object is used.
546 { return this->as_needed_; }
548 // --whole-archive: Whether to include the entire contents of an
551 include_whole_archive() const
552 { return this->include_whole_archive_; }
556 { this->do_static_search_ = true; }
560 { this->do_static_search_ = false; }
564 { this->as_needed_ = true; }
568 { this->as_needed_ = false; }
572 { this->include_whole_archive_ = true; }
575 clear_whole_archive()
576 { this->include_whole_archive_ = false; }
579 bool do_static_search_;
581 bool include_whole_archive_;
584 // A single file or library argument from the command line.
586 class Input_file_argument
589 // name: file name or library name
590 // is_lib: true if name is a library name: that is, emits the leading
591 // "lib" and trailing ".so"/".a" from the name
592 // extra_search_path: an extra directory to look for the file, prior
593 // to checking the normal library search path. If this is "",
594 // then no extra directory is added.
595 // options: The position dependent options at this point in the
596 // command line, such as --whole-archive.
597 Input_file_argument()
598 : name_(), is_lib_(false), extra_search_path_(""), options_()
601 Input_file_argument(const char* name, bool is_lib,
602 const char* extra_search_path,
603 const Position_dependent_options& options)
604 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
610 { return this->name_.c_str(); }
612 const Position_dependent_options&
614 { return this->options_; }
618 { return this->is_lib_; }
621 extra_search_path() const
623 return (this->extra_search_path_.empty()
625 : this->extra_search_path_.c_str());
628 // Return whether this file may require a search using the -L
631 may_need_search() const
632 { return this->is_lib_ || !this->extra_search_path_.empty(); }
635 // We use std::string, not const char*, here for convenience when
636 // using script files, so that we do not have to preserve the string
640 std::string extra_search_path_;
641 Position_dependent_options options_;
644 // A file or library, or a group, from the command line.
649 // Create a file or library argument.
650 explicit Input_argument(Input_file_argument file)
651 : is_file_(true), file_(file), group_(NULL)
654 // Create a group argument.
655 explicit Input_argument(Input_file_group* group)
656 : is_file_(false), group_(group)
659 // Return whether this is a file.
662 { return this->is_file_; }
664 // Return whether this is a group.
667 { return !this->is_file_; }
669 // Return the information about the file.
670 const Input_file_argument&
673 gold_assert(this->is_file_);
677 // Return the information about the group.
678 const Input_file_group*
681 gold_assert(!this->is_file_);
688 gold_assert(!this->is_file_);
694 Input_file_argument file_;
695 Input_file_group* group_;
698 // A group from the command line. This is a set of arguments within
699 // --start-group ... --end-group.
701 class Input_file_group
704 typedef std::vector<Input_argument> Files;
705 typedef Files::const_iterator const_iterator;
711 // Add a file to the end of the group.
713 add_file(const Input_file_argument& arg)
714 { this->files_.push_back(Input_argument(arg)); }
716 // Iterators to iterate over the group contents.
720 { return this->files_.begin(); }
724 { return this->files_.end(); }
730 // A list of files from the command line or a script.
732 class Input_arguments
735 typedef std::vector<Input_argument> Input_argument_list;
736 typedef Input_argument_list::const_iterator const_iterator;
739 : input_argument_list_(), in_group_(false)
744 add_file(const Input_file_argument& arg);
746 // Start a group (the --start-group option).
750 // End a group (the --end-group option).
754 // Return whether we are currently in a group.
757 { return this->in_group_; }
759 // The number of entries in the list.
762 { return this->input_argument_list_.size(); }
764 // Iterators to iterate over the list of input files.
768 { return this->input_argument_list_.begin(); }
772 { return this->input_argument_list_.end(); }
774 // Return whether the list is empty.
777 { return this->input_argument_list_.empty(); }
780 Input_argument_list input_argument_list_;
784 // All the information read from the command line.
789 typedef Input_arguments::const_iterator const_iterator;
793 // Process the command line options. This will exit with an
794 // appropriate error message if an unrecognized option is seen.
796 process(int argc, char** argv);
798 // Process one command-line option. This takes the index of argv to
799 // process, and returns the index for the next option.
801 process_one_option(int argc, char** argv, int i, bool* no_more_options);
803 // Handle a -l option.
805 process_l_option(int, char**, char*, bool);
807 // Handle a --start-group option.
809 start_group(const char* arg);
811 // Handle a --end-group option.
813 end_group(const char* arg);
815 // Get an option argument--a helper function for special processing.
817 get_special_argument(const char* longname, int argc, char** argv,
818 const char* arg, bool long_option,
821 // Get the general options.
822 const General_options&
824 { return this->options_; }
826 // Get the position dependent options.
827 const Position_dependent_options&
828 position_dependent_options() const
829 { return this->position_options_; }
831 // The number of input files.
833 number_of_input_files() const
834 { return this->inputs_.size(); }
836 // Iterators to iterate over the list of input files.
840 { return this->inputs_.begin(); }
844 { return this->inputs_.end(); }
847 Command_line(const Command_line&);
848 Command_line& operator=(const Command_line&);
850 // Report usage error.
852 usage() ATTRIBUTE_NORETURN;
854 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
856 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
858 // Apply a command line option.
860 apply_option(const gold::options::One_option&, const char*);
864 add_file(const char* name, bool is_lib);
866 // Examine the result of processing the command-line, and verify
867 // the flags do not contradict each other or are otherwise illegal.
871 General_options options_;
872 Position_dependent_options position_options_;
873 Input_arguments inputs_;
876 } // End namespace gold.
878 #endif // !defined(GOLD_OPTIONS_H)