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;
51 } // End namespace gold::options.
53 // A directory to search. For each directory we record whether it is
54 // in the sysroot. We need to know this so that, if a linker script
55 // is found within the sysroot, we will apply the sysroot to any files
56 // named by that script.
58 class Search_directory
61 // We need a default constructor because we put this in a
64 : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
67 // This is the usual constructor.
68 Search_directory(const char* name, bool put_in_sysroot)
69 : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
70 { gold_assert(!this->name_.empty()); }
72 // This is called if we have a sysroot. The sysroot is prefixed to
73 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
74 // set to true for any enries which are in the sysroot (this will
75 // naturally include any entries for which put_in_sysroot_ is true).
76 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
77 // passing SYSROOT to lrealpath.
79 add_sysroot(const char* sysroot, const char* canonical_sysroot);
81 // Get the directory name.
84 { return this->name_; }
86 // Return whether this directory is in the sysroot.
89 { return this->is_in_sysroot_; }
97 // The position independent options which apply to the whole link.
98 // There are a lot of them.
100 class General_options
105 // -E: export dynamic symbols.
107 export_dynamic() const
108 { return this->export_dynamic_; }
110 // -I: dynamic linker name.
112 dynamic_linker() const
113 { return this->dynamic_linker_; }
115 // -L: Library search path.
116 typedef std::vector<Search_directory> Dir_list;
120 { return this->search_path_; }
122 // -O: optimization level (0: don't try to optimize output size).
124 optimization_level() const
125 { return this->optimization_level_; }
127 // -o: Output file name.
129 output_file_name() const
130 { return this->output_file_name_; }
132 // -r: Whether we are doing a relocatable link.
134 is_relocatable() const
135 { return this->is_relocatable_; }
137 // -s: Strip all symbols.
140 { return this->strip_ == STRIP_ALL; }
142 // -S: Strip debugging information.
145 { return this->strip_ == STRIP_ALL || this->strip_ == STRIP_DEBUG; }
147 // -Bsymbolic: bind defined symbols locally.
150 { return this->symbolic_; }
152 // --eh-frame-hdr: Whether to generate an exception frame header.
154 create_eh_frame_hdr() const
155 { return this->create_eh_frame_hdr_; }
157 // --rpath: The runtime search path.
160 { return this->rpath_; }
162 // --rpath-link: The link time search patch for shared libraries.
165 { return this->rpath_link_; }
167 // --shared: Whether generating a shared object.
170 { return this->is_shared_; }
172 // --static: Whether doing a static link.
175 { return this->is_static_; }
177 // --stats: Print resource usage statistics.
180 { return this->print_stats_; }
182 // --sysroot: The system root of a cross-linker.
185 { return this->sysroot_; }
187 // -Ttext: The address of the .text section
189 text_segment_address() const
190 { return this->text_segment_address_; }
192 // Whether -Ttext was used.
194 user_set_text_segment_address() const
195 { return this->text_segment_address_ != -1U; }
197 // --threads: Whether to use threads.
200 { return this->threads_; }
202 // --thread-count-initial: Threads to use in initial pass.
204 thread_count_initial() const
205 { return this->thread_count_initial_; }
207 // --thread-count-middle: Threads to use in middle pass.
209 thread_count_middle() const
210 { return this->thread_count_middle_; }
212 // --thread-count-final: Threads to use in final pass.
214 thread_count_final() const
215 { return this->thread_count_final_; }
218 // Don't copy this structure.
219 General_options(const General_options&);
220 General_options& operator=(const General_options&);
222 friend class Command_line;
223 friend class options::Command_line_options;
225 // Which symbols to strip.
228 // Don't strip any symbols.
230 // Strip all symbols.
232 // Strip debugging information.
238 { this->export_dynamic_ = true; }
241 set_dynamic_linker(const char* arg)
242 { this->dynamic_linker_ = arg; }
245 add_to_search_path(const char* arg)
246 { this->search_path_.push_back(Search_directory(arg, false)); }
249 add_to_search_path_with_sysroot(const char* arg)
250 { this->search_path_.push_back(Search_directory(arg, true)); }
253 set_optimization_level(const char* arg)
254 { this->optimization_level_ = atoi(arg); }
257 set_output_file_name(const char* arg)
258 { this->output_file_name_ = arg; }
262 { this->is_relocatable_ = true; }
266 { this->strip_ = STRIP_ALL; }
268 // Note: normalize_options() depends on the fact that this turns off
269 // STRIP_ALL if it were already set.
272 { this->strip_ = STRIP_DEBUG; }
276 { this->symbolic_ = true; }
279 set_create_eh_frame_hdr()
280 { this->create_eh_frame_hdr_ = true; }
283 add_to_rpath(const char* arg)
284 { this->rpath_.push_back(Search_directory(arg, false)); }
287 add_to_rpath_link(const char* arg)
288 { this->rpath_link_.push_back(Search_directory(arg, false)); }
292 { this->is_shared_ = true; }
296 { this->is_static_ = true; }
300 { this->print_stats_ = true; }
303 set_sysroot(const char* arg)
304 { this->sysroot_ = arg; }
307 set_text_segment_address(const char* arg)
310 this->text_segment_address_ = strtoull(arg, &endptr, 0);
312 || this->text_segment_address_ == -1U)
314 fprintf(stderr, _("%s: invalid argument to -Ttext: %s\n"),
321 parse_thread_count(const char* arg)
324 int count = strtol(arg, &endptr, 0);
325 if (*endptr != '\0' || count < 0)
327 fprintf(stderr, _("%s: invalid thread count: %s\n"),
336 { this->threads_ = true; }
340 { this->threads_ = false; }
343 set_thread_count(const char* arg)
345 int count = this->parse_thread_count(arg);
346 this->thread_count_initial_ = count;
347 this->thread_count_middle_ = count;
348 this->thread_count_final_ = count;
352 set_thread_count_initial(const char* arg)
353 { this->thread_count_initial_ = this->parse_thread_count(arg); }
356 set_thread_count_middle(const char* arg)
357 { this->thread_count_initial_ = this->parse_thread_count(arg); }
360 set_thread_count_final(const char* arg)
361 { this->thread_count_initial_ = this->parse_thread_count(arg); }
367 // Apply any sysroot to the directory lists.
371 bool export_dynamic_;
372 const char* dynamic_linker_;
373 Dir_list search_path_;
374 int optimization_level_;
375 const char* output_file_name_;
376 bool is_relocatable_;
379 bool create_eh_frame_hdr_;
381 Dir_list rpath_link_;
385 std::string sysroot_;
386 uint64_t text_segment_address_;
388 int thread_count_initial_;
389 int thread_count_middle_;
390 int thread_count_final_;
393 // The current state of the position dependent options.
395 class Position_dependent_options
398 Position_dependent_options();
400 // -Bstatic: Whether we are searching for a static archive rather
401 // than a shared object.
403 do_static_search() const
404 { return this->do_static_search_; }
406 // --as-needed: Whether to add a DT_NEEDED argument only if the
407 // dynamic object is used.
410 { return this->as_needed_; }
412 // --whole-archive: Whether to include the entire contents of an
415 include_whole_archive() const
416 { return this->include_whole_archive_; }
420 { this->do_static_search_ = true; }
424 { this->do_static_search_ = false; }
428 { this->as_needed_ = true; }
432 { this->as_needed_ = false; }
436 { this->include_whole_archive_ = true; }
439 clear_whole_archive()
440 { this->include_whole_archive_ = false; }
443 bool do_static_search_;
445 bool include_whole_archive_;
448 // A single file or library argument from the command line.
450 class Input_file_argument
453 // name: file name or library name
454 // is_lib: true if name is a library name: that is, emits the leading
455 // "lib" and trailing ".so"/".a" from the name
456 // extra_search_path: an extra directory to look for the file, prior
457 // to checking the normal library search path. If this is "",
458 // then no extra directory is added.
459 // options: The position dependent options at this point in the
460 // command line, such as --whole-archive.
461 Input_file_argument()
462 : name_(), is_lib_(false), extra_search_path_(""), options_()
465 Input_file_argument(const char* name, bool is_lib,
466 const char* extra_search_path,
467 const Position_dependent_options& options)
468 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
474 { return this->name_.c_str(); }
476 const Position_dependent_options&
478 { return this->options_; }
482 { return this->is_lib_; }
485 extra_search_path() const
487 return (this->extra_search_path_.empty()
489 : this->extra_search_path_.c_str());
492 // Return whether this file may require a search using the -L
495 may_need_search() const
496 { return this->is_lib_ || !this->extra_search_path_.empty(); }
499 // We use std::string, not const char*, here for convenience when
500 // using script files, so that we do not have to preserve the string
504 std::string extra_search_path_;
505 Position_dependent_options options_;
508 // A file or library, or a group, from the command line.
513 // Create a file or library argument.
514 explicit Input_argument(Input_file_argument file)
515 : is_file_(true), file_(file), group_(NULL)
518 // Create a group argument.
519 explicit Input_argument(Input_file_group* group)
520 : is_file_(false), group_(group)
523 // Return whether this is a file.
526 { return this->is_file_; }
528 // Return whether this is a group.
531 { return !this->is_file_; }
533 // Return the information about the file.
534 const Input_file_argument&
537 gold_assert(this->is_file_);
541 // Return the information about the group.
542 const Input_file_group*
545 gold_assert(!this->is_file_);
552 gold_assert(!this->is_file_);
558 Input_file_argument file_;
559 Input_file_group* group_;
562 // A group from the command line. This is a set of arguments within
563 // --start-group ... --end-group.
565 class Input_file_group
568 typedef std::vector<Input_argument> Files;
569 typedef Files::const_iterator const_iterator;
575 // Add a file to the end of the group.
577 add_file(const Input_file_argument& arg)
578 { this->files_.push_back(Input_argument(arg)); }
580 // Iterators to iterate over the group contents.
584 { return this->files_.begin(); }
588 { return this->files_.end(); }
594 // A list of files from the command line or a script.
596 class Input_arguments
599 typedef std::vector<Input_argument> Input_argument_list;
600 typedef Input_argument_list::const_iterator const_iterator;
603 : input_argument_list_(), in_group_(false)
608 add_file(const Input_file_argument& arg);
610 // Start a group (the --start-group option).
614 // End a group (the --end-group option).
618 // Return whether we are currently in a group.
621 { return this->in_group_; }
623 // The number of entries in the list.
626 { return this->input_argument_list_.size(); }
628 // Iterators to iterate over the list of input files.
632 { return this->input_argument_list_.begin(); }
636 { return this->input_argument_list_.end(); }
638 // Return whether the list is empty.
641 { return this->input_argument_list_.empty(); }
644 Input_argument_list input_argument_list_;
648 // All the information read from the command line.
653 typedef Input_arguments::const_iterator const_iterator;
657 // Process the command line options. This will exit with an
658 // appropriate error message if an unrecognized option is seen.
660 process(int argc, char** argv);
662 // Handle a -l option.
664 process_l_option(int, char**, char*);
666 // Handle a --start-group option.
668 start_group(const char* arg);
670 // Handle a --end-group option.
672 end_group(const char* arg);
674 // Get the general options.
675 const General_options&
677 { return this->options_; }
679 // The number of input files.
681 number_of_input_files() const
682 { return this->inputs_.size(); }
684 // Iterators to iterate over the list of input files.
688 { return this->inputs_.begin(); }
692 { return this->inputs_.end(); }
695 Command_line(const Command_line&);
696 Command_line& operator=(const Command_line&);
698 // Report usage error.
700 usage() ATTRIBUTE_NORETURN;
702 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
704 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
706 // Apply a command line option.
708 apply_option(const gold::options::One_option&, const char*);
712 add_file(const char* name, bool is_lib);
714 // Examine the result of processing the command-line, and verify
715 // the flags do not contradict each other or are otherwise illegal.
719 General_options options_;
720 Position_dependent_options position_options_;
721 Input_arguments inputs_;
724 } // End namespace gold.
726 #endif // !defined(GOLD_OPTIONS_H)