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 // The position independent options which apply to the whole link.
54 // There are a lot of them.
61 // -E: export dynamic symbols.
63 export_dynamic() const
64 { return this->export_dynamic_
; }
66 // -I: dynamic linker name.
68 dynamic_linker() const
69 { return this->dynamic_linker_
; }
71 // -L: Library search path.
72 typedef std::vector
<const char*> Dir_list
;
76 { return this->search_path_
; }
78 // -O: optimization level (0: don't try to optimize output size).
80 optimization_level() const
81 { return this->optimization_level_
; }
83 // -o: Output file name.
85 output_file_name() const
86 { return this->output_file_name_
; }
88 // -r: Whether we are doing a relocatable link.
90 is_relocatable() const
91 { return this->is_relocatable_
; }
93 // --eh-frame-hdr: Whether to generate an exception frame header.
95 create_eh_frame_hdr() const
96 { return this->create_eh_frame_hdr_
; }
98 // --rpath: The runtime search path.
101 { return this->rpath_
; }
103 // --rpath-link: The link time search patch for shared libraries.
106 { return this->rpath_link_
; }
108 // --shared: Whether generating a shared object.
111 { return this->is_shared_
; }
113 // --static: Whether doing a static link.
116 { return this->is_static_
; }
119 // Don't copy this structure.
120 General_options(const General_options
&);
121 General_options
& operator=(const General_options
&);
123 friend class Command_line
;
124 friend class options::Command_line_options
;
128 { this->export_dynamic_
= true; }
131 set_dynamic_linker(const char* arg
)
132 { this->dynamic_linker_
= arg
; }
135 add_to_search_path(const char* arg
)
136 { this->search_path_
.push_back(arg
); }
139 set_optimization_level(const char* arg
)
140 { this->optimization_level_
= atoi(arg
); }
143 set_output_file_name(const char* arg
)
144 { this->output_file_name_
= arg
; }
148 { this->is_relocatable_
= true; }
151 set_create_eh_frame_hdr()
152 { this->create_eh_frame_hdr_
= true; }
155 add_to_rpath(const char* arg
)
156 { this->rpath_
.push_back(arg
); }
159 add_to_rpath_link(const char* arg
)
160 { this->rpath_link_
.push_back(arg
); }
164 { this->is_shared_
= true; }
168 { this->is_static_
= true; }
174 bool export_dynamic_
;
175 const char* dynamic_linker_
;
176 Dir_list search_path_
;
177 int optimization_level_
;
178 const char* output_file_name_
;
179 bool is_relocatable_
;
180 bool create_eh_frame_hdr_
;
182 Dir_list rpath_link_
;
187 // The current state of the position dependent options.
189 class Position_dependent_options
192 Position_dependent_options();
194 // -Bstatic: Whether we are searching for a static archive rather
195 // than a shared object.
197 do_static_search() const
198 { return this->do_static_search_
; }
200 // --as-needed: Whether to add a DT_NEEDED argument only if the
201 // dynamic object is used.
204 { return this->as_needed_
; }
206 // --whole-archive: Whether to include the entire contents of an
209 include_whole_archive() const
210 { return this->include_whole_archive_
; }
214 { this->do_static_search_
= true; }
218 { this->do_static_search_
= false; }
222 { this->as_needed_
= true; }
226 { this->as_needed_
= false; }
230 { this->include_whole_archive_
= true; }
233 clear_whole_archive()
234 { this->include_whole_archive_
= false; }
237 bool do_static_search_
;
239 bool include_whole_archive_
;
242 // A single file or library argument from the command line.
244 class Input_file_argument
247 // name: file name or library name
248 // is_lib: true if name is a library name: that is, emits the leading
249 // "lib" and trailing ".so"/".a" from the name
250 // extra_search_path: an extra directory to look for the file, prior
251 // to checking the normal library search path. If this is "",
252 // then no extra directory is added.
253 // options: The position dependent options at this point in the
254 // command line, such as --group.
255 Input_file_argument()
256 : name_(), is_lib_(false), extra_search_path_(""), options_()
259 Input_file_argument(const char* name
, bool is_lib
,
260 const char* extra_search_path
,
261 const Position_dependent_options
& options
)
262 : name_(name
), is_lib_(is_lib
), extra_search_path_(extra_search_path
),
268 { return this->name_
.c_str(); }
270 const Position_dependent_options
&
272 { return this->options_
; }
276 { return this->is_lib_
; }
279 extra_search_path() const
281 return (this->extra_search_path_
.empty()
283 : this->extra_search_path_
.c_str());
286 // Return whether this file may require a search using the -L
289 may_need_search() const
290 { return this->is_lib_
|| !this->extra_search_path_
.empty(); }
293 // We use std::string, not const char*, here for convenience when
294 // using script files, so that we do not have to preserve the string
298 std::string extra_search_path_
;
299 Position_dependent_options options_
;
302 // A file or library, or a group, from the command line.
307 // Create a file or library argument.
308 explicit Input_argument(Input_file_argument file
)
309 : is_file_(true), file_(file
), group_(NULL
)
312 // Create a group argument.
313 explicit Input_argument(Input_file_group
* group
)
314 : is_file_(false), group_(group
)
317 // Return whether this is a file.
320 { return this->is_file_
; }
322 // Return whether this is a group.
325 { return !this->is_file_
; }
327 // Return the information about the file.
328 const Input_file_argument
&
331 gold_assert(this->is_file_
);
335 // Return the information about the group.
336 const Input_file_group
*
339 gold_assert(!this->is_file_
);
346 gold_assert(!this->is_file_
);
352 Input_file_argument file_
;
353 Input_file_group
* group_
;
356 // A group from the command line. This is a set of arguments within
357 // --start-group ... --end-group.
359 class Input_file_group
362 typedef std::vector
<Input_argument
> Files
;
363 typedef Files::const_iterator const_iterator
;
369 // Add a file to the end of the group.
371 add_file(const Input_file_argument
& arg
)
372 { this->files_
.push_back(Input_argument(arg
)); }
374 // Iterators to iterate over the group contents.
378 { return this->files_
.begin(); }
382 { return this->files_
.end(); }
388 // A list of files from the command line or a script.
390 class Input_arguments
393 typedef std::vector
<Input_argument
> Input_argument_list
;
394 typedef Input_argument_list::const_iterator const_iterator
;
397 : input_argument_list_(), in_group_(false)
402 add_file(const Input_file_argument
& arg
);
404 // Start a group (the --start-group option).
408 // End a group (the --end-group option).
412 // Return whether we are currently in a group.
415 { return this->in_group_
; }
417 // Iterators to iterate over the list of input files.
421 { return this->input_argument_list_
.begin(); }
425 { return this->input_argument_list_
.end(); }
427 // Return whether the list is empty.
430 { return this->input_argument_list_
.empty(); }
433 Input_argument_list input_argument_list_
;
437 // All the information read from the command line.
442 typedef Input_arguments::const_iterator const_iterator
;
446 // Process the command line options. This will exit with an
447 // appropriate error message if an unrecognized option is seen.
449 process(int argc
, char** argv
);
451 // Handle a -l option.
453 process_l_option(int, char**, char*);
455 // Handle a --start-group option.
457 start_group(const char* arg
);
459 // Handle a --end-group option.
461 end_group(const char* arg
);
463 // Get the general options.
464 const General_options
&
466 { return this->options_
; }
468 // Iterators to iterate over the list of input files.
472 { return this->inputs_
.begin(); }
476 { return this->inputs_
.end(); }
479 Command_line(const Command_line
&);
480 Command_line
& operator=(const Command_line
&);
482 // Report usage error.
484 usage() ATTRIBUTE_NORETURN
;
486 usage(const char* msg
, const char* opt
) ATTRIBUTE_NORETURN
;
488 usage(const char* msg
, char opt
) ATTRIBUTE_NORETURN
;
490 // Apply a command line option.
492 apply_option(const gold::options::One_option
&, const char*);
496 add_file(const char* name
, bool is_lib
);
498 General_options options_
;
499 Position_dependent_options position_options_
;
500 Input_arguments inputs_
;
503 } // End namespace gold.
505 #endif // !defined(GOLD_OPTIONS_H)