1 // options.h -- handle command line options for gold -*- C++ -*-
4 // Holds everything we get from the command line.
5 // General_options (from Command_line::options())
6 // Options which are not position dependent.
7 // Input_argument (from Command_line::inputs())
8 // The list of input files, including -l options.
9 // Position_dependent_options (from Input_argument::options())
10 // Position dependent options which apply to this argument.
12 #ifndef GOLD_OPTIONS_H
13 #define GOLD_OPTIONS_H
24 class Input_file_group;
28 class Command_line_options;
31 } // End namespace gold::options.
33 // The position independent options which apply to the whole link.
34 // There are a lot of them.
41 // -E: export dynamic symbols.
43 export_dynamic() const
44 { return this->export_dynamic_; }
46 // -I: dynamic linker name.
48 dynamic_linker() const
49 { return this->dynamic_linker_; }
51 // -L: Library search path.
52 typedef std::vector<const char*> Dir_list;
56 { return this->search_path_; }
58 // -O: optimization level (0: don't try to optimize output size).
60 optimization_level() const
61 { return this->optimization_level_; }
63 // -o: Output file name.
65 output_file_name() const
66 { return this->output_file_name_; }
68 // -r: Whether we are doing a relocatable link.
70 is_relocatable() const
71 { return this->is_relocatable_; }
73 // --eh-frame-hdr: Whether to generate an exception frame header.
75 create_eh_frame_hdr() const
76 { return this->create_eh_frame_hdr_; }
78 // --rpath: The runtime search path.
81 { return this->rpath_; }
83 // --rpath-link: The link time search patch for shared libraries.
86 { return this->rpath_link_; }
88 // --shared: Whether generating a shared object.
91 { return this->is_shared_; }
93 // --static: Whether doing a static link.
96 { return this->is_static_; }
99 // Don't copy this structure.
100 General_options(const General_options&);
101 General_options& operator=(const General_options&);
103 friend class Command_line;
104 friend class options::Command_line_options;
108 { this->export_dynamic_ = true; }
111 set_dynamic_linker(const char* arg)
112 { this->dynamic_linker_ = arg; }
115 add_to_search_path(const char* arg)
116 { this->search_path_.push_back(arg); }
119 set_optimization_level(const char* arg)
120 { this->optimization_level_ = atoi(arg); }
123 set_output_file_name(const char* arg)
124 { this->output_file_name_ = arg; }
128 { this->is_relocatable_ = true; }
131 set_create_eh_frame_hdr()
132 { this->create_eh_frame_hdr_ = true; }
135 add_to_rpath(const char* arg)
136 { this->rpath_.push_back(arg); }
139 add_to_rpath_link(const char* arg)
140 { this->rpath_link_.push_back(arg); }
144 { this->is_shared_ = true; }
148 { this->is_static_ = true; }
154 bool export_dynamic_;
155 const char* dynamic_linker_;
156 Dir_list search_path_;
157 int optimization_level_;
158 const char* output_file_name_;
159 bool is_relocatable_;
160 bool create_eh_frame_hdr_;
162 Dir_list rpath_link_;
167 // The current state of the position dependent options.
169 class Position_dependent_options
172 Position_dependent_options();
174 // -Bstatic: Whether we are searching for a static archive rather
175 // than a shared object.
177 do_static_search() const
178 { return this->do_static_search_; }
180 // --as-needed: Whether to add a DT_NEEDED argument only if the
181 // dynamic object is used.
184 { return this->as_needed_; }
186 // --whole-archive: Whether to include the entire contents of an
189 include_whole_archive() const
190 { return this->include_whole_archive_; }
194 { this->do_static_search_ = true; }
198 { this->do_static_search_ = false; }
202 { this->as_needed_ = true; }
206 { this->as_needed_ = false; }
210 { this->include_whole_archive_ = true; }
213 clear_whole_archive()
214 { this->include_whole_archive_ = false; }
217 bool do_static_search_;
219 bool include_whole_archive_;
222 // A single file or library argument from the command line.
224 class Input_file_argument
227 Input_file_argument()
228 : name_(), is_lib_(false), options_()
231 Input_file_argument(const char* name, bool is_lib,
232 const Position_dependent_options& options)
233 : name_(name), is_lib_(is_lib), options_(options)
238 { return this->name_.c_str(); }
240 const Position_dependent_options&
242 { return this->options_; }
246 { return this->is_lib_; }
249 // We use std::string, not const char*, here for convenience when
250 // using script files, so that we do not have to preserve the string
254 Position_dependent_options options_;
257 // A file or library, or a group, from the command line.
262 // Create a file or library argument.
263 explicit Input_argument(Input_file_argument file)
264 : is_file_(true), file_(file), group_(NULL)
267 // Create a group argument.
268 explicit Input_argument(Input_file_group* group)
269 : is_file_(false), group_(group)
272 // Return whether this is a file.
275 { return this->is_file_; }
277 // Return whether this is a group.
280 { return !this->is_file_; }
282 // Return the information about the file.
283 const Input_file_argument&
286 gold_assert(this->is_file_);
290 // Return the information about the group.
291 const Input_file_group*
294 gold_assert(!this->is_file_);
301 gold_assert(!this->is_file_);
307 Input_file_argument file_;
308 Input_file_group* group_;
311 // A group from the command line. This is a set of arguments within
312 // --start-group ... --end-group.
314 class Input_file_group
317 typedef std::vector<Input_argument> Files;
318 typedef Files::const_iterator const_iterator;
324 // Add a file to the end of the group.
326 add_file(const Input_file_argument& arg)
327 { this->files_.push_back(Input_argument(arg)); }
329 // Iterators to iterate over the group contents.
333 { return this->files_.begin(); }
337 { return this->files_.end(); }
343 // A list of files from the command line or a script.
345 class Input_arguments
348 typedef std::vector<Input_argument> Input_argument_list;
349 typedef Input_argument_list::const_iterator const_iterator;
352 : input_argument_list_(), in_group_(false)
357 add_file(const Input_file_argument& arg);
359 // Start a group (the --start-group option).
363 // End a group (the --end-group option).
367 // Return whether we are currently in a group.
370 { return this->in_group_; }
372 // Iterators to iterate over the list of input files.
376 { return this->input_argument_list_.begin(); }
380 { return this->input_argument_list_.end(); }
382 // Return whether the list is empty.
385 { return this->input_argument_list_.empty(); }
388 Input_argument_list input_argument_list_;
392 // All the information read from the command line.
397 typedef Input_arguments::const_iterator const_iterator;
401 // Process the command line options. This will exit with an
402 // appropriate error message if an unrecognized option is seen.
404 process(int argc, char** argv);
406 // Handle a -l option.
408 process_l_option(int, char**, char*);
410 // Handle a --start-group option.
412 start_group(const char* arg);
414 // Handle a --end-group option.
416 end_group(const char* arg);
418 // Get the general options.
419 const General_options&
421 { return this->options_; }
423 // Iterators to iterate over the list of input files.
427 { return this->inputs_.begin(); }
431 { return this->inputs_.end(); }
434 Command_line(const Command_line&);
435 Command_line& operator=(const Command_line&);
437 // Report usage error.
439 usage() ATTRIBUTE_NORETURN;
441 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
443 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
445 // Apply a command line option.
447 apply_option(const gold::options::One_option&, const char*);
451 add_file(const char* name, bool is_lib);
453 General_options options_;
454 Position_dependent_options position_options_;
455 Input_arguments inputs_;
458 } // End namespace gold.
460 #endif // !defined(GOLD_OPTIONS_H)