Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // options.h -- handle command line options for gold -*- C++ -*- |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. | |
22 | ||
bae7f79e ILT |
23 | // Command_line |
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. | |
31 | ||
32 | #ifndef GOLD_OPTIONS_H | |
33 | #define GOLD_OPTIONS_H | |
34 | ||
ca3a67a5 | 35 | #include <cstdlib> |
bae7f79e | 36 | #include <list> |
61ba1cf9 | 37 | #include <string> |
92e059d8 | 38 | #include <vector> |
bae7f79e | 39 | |
bae7f79e ILT |
40 | namespace gold |
41 | { | |
42 | ||
43 | class Command_line; | |
ead1e424 | 44 | class Input_file_group; |
bae7f79e ILT |
45 | |
46 | namespace options { | |
47 | ||
48 | class Command_line_options; | |
49 | struct One_option; | |
50 | ||
51 | } // End namespace gold::options. | |
52 | ||
53 | // The position independent options which apply to the whole link. | |
54 | // There are a lot of them. | |
55 | ||
56 | class General_options | |
57 | { | |
58 | public: | |
59 | General_options(); | |
60 | ||
a6badf5a ILT |
61 | // -E: export dynamic symbols. |
62 | bool | |
63 | export_dynamic() const | |
64 | { return this->export_dynamic_; } | |
65 | ||
dbe717ef ILT |
66 | // -I: dynamic linker name. |
67 | const char* | |
68 | dynamic_linker() const | |
69 | { return this->dynamic_linker_; } | |
70 | ||
bae7f79e | 71 | // -L: Library search path. |
41f542e7 | 72 | typedef std::vector<const char*> Dir_list; |
bae7f79e ILT |
73 | |
74 | const Dir_list& | |
75 | search_path() const | |
76 | { return this->search_path_; } | |
77 | ||
ca3a67a5 ILT |
78 | // -O: optimization level (0: don't try to optimize output size). |
79 | int | |
80 | optimization_level() const | |
81 | { return this->optimization_level_; } | |
82 | ||
61ba1cf9 ILT |
83 | // -o: Output file name. |
84 | const char* | |
85 | output_file_name() const | |
86 | { return this->output_file_name_; } | |
87 | ||
bae7f79e ILT |
88 | // -r: Whether we are doing a relocatable link. |
89 | bool | |
90 | is_relocatable() const | |
91 | { return this->is_relocatable_; } | |
92 | ||
7da52175 ILT |
93 | // --eh-frame-hdr: Whether to generate an exception frame header. |
94 | bool | |
95 | create_eh_frame_hdr() const | |
96 | { return this->create_eh_frame_hdr_; } | |
97 | ||
41f542e7 ILT |
98 | // --rpath: The runtime search path. |
99 | const Dir_list& | |
100 | rpath() const | |
101 | { return this->rpath_; } | |
102 | ||
15b3cfae ILT |
103 | // --rpath-link: The link time search patch for shared libraries. |
104 | const Dir_list& | |
105 | rpath_link() const | |
106 | { return this->rpath_link_; } | |
107 | ||
92e059d8 ILT |
108 | // --shared: Whether generating a shared object. |
109 | bool | |
110 | is_shared() const | |
111 | { return this->is_shared_; } | |
112 | ||
bae7f79e ILT |
113 | // --static: Whether doing a static link. |
114 | bool | |
115 | is_static() const | |
116 | { return this->is_static_; } | |
117 | ||
118 | private: | |
dbe717ef ILT |
119 | // Don't copy this structure. |
120 | General_options(const General_options&); | |
121 | General_options& operator=(const General_options&); | |
122 | ||
bae7f79e ILT |
123 | friend class Command_line; |
124 | friend class options::Command_line_options; | |
125 | ||
a6badf5a ILT |
126 | void |
127 | set_export_dynamic() | |
128 | { this->export_dynamic_ = true; } | |
129 | ||
dbe717ef ILT |
130 | void |
131 | set_dynamic_linker(const char* arg) | |
132 | { this->dynamic_linker_ = arg; } | |
133 | ||
bae7f79e ILT |
134 | void |
135 | add_to_search_path(const char* arg) | |
136 | { this->search_path_.push_back(arg); } | |
137 | ||
ca3a67a5 ILT |
138 | void |
139 | set_optimization_level(const char* arg) | |
140 | { this->optimization_level_ = atoi(arg); } | |
141 | ||
61ba1cf9 ILT |
142 | void |
143 | set_output_file_name(const char* arg) | |
144 | { this->output_file_name_ = arg; } | |
145 | ||
bae7f79e ILT |
146 | void |
147 | set_relocatable() | |
148 | { this->is_relocatable_ = true; } | |
149 | ||
7da52175 | 150 | void |
192f9b85 | 151 | set_create_eh_frame_hdr() |
7da52175 ILT |
152 | { this->create_eh_frame_hdr_ = true; } |
153 | ||
41f542e7 ILT |
154 | void |
155 | add_to_rpath(const char* arg) | |
156 | { this->rpath_.push_back(arg); } | |
157 | ||
15b3cfae ILT |
158 | void |
159 | add_to_rpath_link(const char* arg) | |
160 | { this->rpath_link_.push_back(arg); } | |
161 | ||
92e059d8 ILT |
162 | void |
163 | set_shared() | |
164 | { this->is_shared_ = true; } | |
165 | ||
bae7f79e ILT |
166 | void |
167 | set_static() | |
168 | { this->is_static_ = true; } | |
169 | ||
652ec9bd ILT |
170 | void |
171 | ignore(const char*) | |
172 | { } | |
173 | ||
a6badf5a | 174 | bool export_dynamic_; |
dbe717ef | 175 | const char* dynamic_linker_; |
bae7f79e | 176 | Dir_list search_path_; |
ca3a67a5 | 177 | int optimization_level_; |
61ba1cf9 | 178 | const char* output_file_name_; |
bae7f79e | 179 | bool is_relocatable_; |
7da52175 | 180 | bool create_eh_frame_hdr_; |
41f542e7 | 181 | Dir_list rpath_; |
15b3cfae | 182 | Dir_list rpath_link_; |
92e059d8 | 183 | bool is_shared_; |
bae7f79e | 184 | bool is_static_; |
bae7f79e ILT |
185 | }; |
186 | ||
187 | // The current state of the position dependent options. | |
188 | ||
189 | class Position_dependent_options | |
190 | { | |
191 | public: | |
192 | Position_dependent_options(); | |
193 | ||
194 | // -Bstatic: Whether we are searching for a static archive rather | |
dbe717ef | 195 | // than a shared object. |
bae7f79e | 196 | bool |
dbe717ef | 197 | do_static_search() const |
bae7f79e ILT |
198 | { return this->do_static_search_; } |
199 | ||
dbe717ef ILT |
200 | // --as-needed: Whether to add a DT_NEEDED argument only if the |
201 | // dynamic object is used. | |
202 | bool | |
203 | as_needed() const | |
204 | { return this->as_needed_; } | |
bae7f79e | 205 | |
4973341a ILT |
206 | // --whole-archive: Whether to include the entire contents of an |
207 | // --archive. | |
208 | bool | |
209 | include_whole_archive() const | |
210 | { return this->include_whole_archive_; } | |
211 | ||
bae7f79e ILT |
212 | void |
213 | set_static_search() | |
214 | { this->do_static_search_ = true; } | |
215 | ||
216 | void | |
217 | set_dynamic_search() | |
218 | { this->do_static_search_ = false; } | |
219 | ||
dbe717ef ILT |
220 | void |
221 | set_as_needed() | |
222 | { this->as_needed_ = true; } | |
223 | ||
224 | void | |
225 | clear_as_needed() | |
226 | { this->as_needed_ = false; } | |
227 | ||
4973341a ILT |
228 | void |
229 | set_whole_archive() | |
230 | { this->include_whole_archive_ = true; } | |
231 | ||
232 | void | |
233 | clear_whole_archive() | |
234 | { this->include_whole_archive_ = false; } | |
235 | ||
dbe717ef | 236 | private: |
bae7f79e | 237 | bool do_static_search_; |
dbe717ef | 238 | bool as_needed_; |
4973341a | 239 | bool include_whole_archive_; |
bae7f79e ILT |
240 | }; |
241 | ||
242 | // A single file or library argument from the command line. | |
243 | ||
ead1e424 | 244 | class Input_file_argument |
bae7f79e ILT |
245 | { |
246 | public: | |
ead1e424 | 247 | Input_file_argument() |
dbe717ef | 248 | : name_(), is_lib_(false), options_() |
ead1e424 ILT |
249 | { } |
250 | ||
251 | Input_file_argument(const char* name, bool is_lib, | |
252 | const Position_dependent_options& options) | |
61ba1cf9 | 253 | : name_(name), is_lib_(is_lib), options_(options) |
bae7f79e ILT |
254 | { } |
255 | ||
256 | const char* | |
257 | name() const | |
dbe717ef | 258 | { return this->name_.c_str(); } |
bae7f79e ILT |
259 | |
260 | const Position_dependent_options& | |
261 | options() const | |
262 | { return this->options_; } | |
263 | ||
264 | bool | |
265 | is_lib() const | |
61ba1cf9 | 266 | { return this->is_lib_; } |
bae7f79e ILT |
267 | |
268 | private: | |
dbe717ef ILT |
269 | // We use std::string, not const char*, here for convenience when |
270 | // using script files, so that we do not have to preserve the string | |
271 | // in that case. | |
272 | std::string name_; | |
61ba1cf9 | 273 | bool is_lib_; |
bae7f79e ILT |
274 | Position_dependent_options options_; |
275 | }; | |
276 | ||
ead1e424 ILT |
277 | // A file or library, or a group, from the command line. |
278 | ||
279 | class Input_argument | |
280 | { | |
281 | public: | |
282 | // Create a file or library argument. | |
283 | explicit Input_argument(Input_file_argument file) | |
284 | : is_file_(true), file_(file), group_(NULL) | |
285 | { } | |
286 | ||
287 | // Create a group argument. | |
288 | explicit Input_argument(Input_file_group* group) | |
289 | : is_file_(false), group_(group) | |
290 | { } | |
291 | ||
292 | // Return whether this is a file. | |
293 | bool | |
294 | is_file() const | |
295 | { return this->is_file_; } | |
296 | ||
297 | // Return whether this is a group. | |
298 | bool | |
299 | is_group() const | |
300 | { return !this->is_file_; } | |
301 | ||
302 | // Return the information about the file. | |
303 | const Input_file_argument& | |
304 | file() const | |
305 | { | |
a3ad94ed | 306 | gold_assert(this->is_file_); |
ead1e424 ILT |
307 | return this->file_; |
308 | } | |
309 | ||
310 | // Return the information about the group. | |
311 | const Input_file_group* | |
312 | group() const | |
313 | { | |
a3ad94ed | 314 | gold_assert(!this->is_file_); |
ead1e424 ILT |
315 | return this->group_; |
316 | } | |
317 | ||
318 | Input_file_group* | |
319 | group() | |
320 | { | |
a3ad94ed | 321 | gold_assert(!this->is_file_); |
ead1e424 ILT |
322 | return this->group_; |
323 | } | |
324 | ||
325 | private: | |
326 | bool is_file_; | |
327 | Input_file_argument file_; | |
328 | Input_file_group* group_; | |
329 | }; | |
330 | ||
331 | // A group from the command line. This is a set of arguments within | |
332 | // --start-group ... --end-group. | |
333 | ||
334 | class Input_file_group | |
92e059d8 | 335 | { |
ead1e424 ILT |
336 | public: |
337 | typedef std::vector<Input_argument> Files; | |
338 | typedef Files::const_iterator const_iterator; | |
339 | ||
340 | Input_file_group() | |
341 | : files_() | |
342 | { } | |
343 | ||
344 | // Add a file to the end of the group. | |
345 | void | |
346 | add_file(const Input_file_argument& arg) | |
347 | { this->files_.push_back(Input_argument(arg)); } | |
348 | ||
349 | // Iterators to iterate over the group contents. | |
350 | ||
351 | const_iterator | |
352 | begin() const | |
353 | { return this->files_.begin(); } | |
354 | ||
355 | const_iterator | |
356 | end() const | |
357 | { return this->files_.end(); } | |
358 | ||
359 | private: | |
360 | Files files_; | |
92e059d8 ILT |
361 | }; |
362 | ||
dbe717ef ILT |
363 | // A list of files from the command line or a script. |
364 | ||
365 | class Input_arguments | |
366 | { | |
367 | public: | |
368 | typedef std::vector<Input_argument> Input_argument_list; | |
369 | typedef Input_argument_list::const_iterator const_iterator; | |
370 | ||
371 | Input_arguments() | |
372 | : input_argument_list_(), in_group_(false) | |
373 | { } | |
374 | ||
375 | // Add a file. | |
376 | void | |
377 | add_file(const Input_file_argument& arg); | |
378 | ||
379 | // Start a group (the --start-group option). | |
380 | void | |
381 | start_group(); | |
382 | ||
383 | // End a group (the --end-group option). | |
384 | void | |
385 | end_group(); | |
386 | ||
387 | // Return whether we are currently in a group. | |
388 | bool | |
389 | in_group() const | |
390 | { return this->in_group_; } | |
391 | ||
392 | // Iterators to iterate over the list of input files. | |
393 | ||
394 | const_iterator | |
395 | begin() const | |
396 | { return this->input_argument_list_.begin(); } | |
397 | ||
398 | const_iterator | |
399 | end() const | |
400 | { return this->input_argument_list_.end(); } | |
401 | ||
402 | // Return whether the list is empty. | |
403 | bool | |
404 | empty() const | |
405 | { return this->input_argument_list_.empty(); } | |
406 | ||
407 | private: | |
408 | Input_argument_list input_argument_list_; | |
409 | bool in_group_; | |
410 | }; | |
411 | ||
bae7f79e ILT |
412 | // All the information read from the command line. |
413 | ||
414 | class Command_line | |
415 | { | |
416 | public: | |
ead1e424 ILT |
417 | typedef Input_arguments::const_iterator const_iterator; |
418 | ||
bae7f79e ILT |
419 | Command_line(); |
420 | ||
421 | // Process the command line options. This will exit with an | |
422 | // appropriate error message if an unrecognized option is seen. | |
423 | void | |
424 | process(int argc, char** argv); | |
425 | ||
61ba1cf9 ILT |
426 | // Handle a -l option. |
427 | int | |
428 | process_l_option(int, char**, char*); | |
429 | ||
ead1e424 ILT |
430 | // Handle a --start-group option. |
431 | void | |
432 | start_group(const char* arg); | |
433 | ||
434 | // Handle a --end-group option. | |
435 | void | |
436 | end_group(const char* arg); | |
437 | ||
61ba1cf9 | 438 | // Get the general options. |
bae7f79e ILT |
439 | const General_options& |
440 | options() const | |
441 | { return this->options_; } | |
442 | ||
ead1e424 ILT |
443 | // Iterators to iterate over the list of input files. |
444 | ||
445 | const_iterator | |
446 | begin() const | |
447 | { return this->inputs_.begin(); } | |
448 | ||
449 | const_iterator | |
450 | end() const | |
451 | { return this->inputs_.end(); } | |
bae7f79e ILT |
452 | |
453 | private: | |
ead1e424 ILT |
454 | Command_line(const Command_line&); |
455 | Command_line& operator=(const Command_line&); | |
456 | ||
457 | // Report usage error. | |
458 | void | |
459 | usage() ATTRIBUTE_NORETURN; | |
460 | void | |
461 | usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN; | |
462 | void | |
463 | usage(const char* msg, char opt) ATTRIBUTE_NORETURN; | |
464 | ||
465 | // Apply a command line option. | |
466 | void | |
467 | apply_option(const gold::options::One_option&, const char*); | |
468 | ||
469 | // Add a file. | |
470 | void | |
471 | add_file(const char* name, bool is_lib); | |
bae7f79e ILT |
472 | |
473 | General_options options_; | |
474 | Position_dependent_options position_options_; | |
ead1e424 | 475 | Input_arguments inputs_; |
bae7f79e ILT |
476 | }; |
477 | ||
478 | } // End namespace gold. | |
479 | ||
480 | #endif // !defined(GOLD_OPTIONS_H) |