]>
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 <[email protected]>. | |
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 | ||
ad2d6943 ILT |
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. | |
57 | ||
58 | class Search_directory | |
59 | { | |
60 | public: | |
61 | // We need a default constructor because we put this in a | |
62 | // std::vector. | |
63 | Search_directory() | |
64 | : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false) | |
65 | { } | |
66 | ||
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()); } | |
71 | ||
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. | |
78 | void | |
79 | add_sysroot(const char* sysroot, const char* canonical_sysroot); | |
80 | ||
81 | // Get the directory name. | |
82 | const std::string& | |
83 | name() const | |
84 | { return this->name_; } | |
85 | ||
86 | // Return whether this directory is in the sysroot. | |
87 | bool | |
88 | is_in_sysroot() const | |
89 | { return this->is_in_sysroot_; } | |
90 | ||
91 | private: | |
92 | std::string name_; | |
93 | bool put_in_sysroot_; | |
94 | bool is_in_sysroot_; | |
95 | }; | |
96 | ||
bae7f79e ILT |
97 | // The position independent options which apply to the whole link. |
98 | // There are a lot of them. | |
99 | ||
100 | class General_options | |
101 | { | |
102 | public: | |
103 | General_options(); | |
104 | ||
a6badf5a ILT |
105 | // -E: export dynamic symbols. |
106 | bool | |
107 | export_dynamic() const | |
108 | { return this->export_dynamic_; } | |
109 | ||
dbe717ef ILT |
110 | // -I: dynamic linker name. |
111 | const char* | |
112 | dynamic_linker() const | |
113 | { return this->dynamic_linker_; } | |
114 | ||
bae7f79e | 115 | // -L: Library search path. |
ad2d6943 | 116 | typedef std::vector<Search_directory> Dir_list; |
bae7f79e ILT |
117 | |
118 | const Dir_list& | |
119 | search_path() const | |
120 | { return this->search_path_; } | |
121 | ||
ca3a67a5 ILT |
122 | // -O: optimization level (0: don't try to optimize output size). |
123 | int | |
124 | optimization_level() const | |
125 | { return this->optimization_level_; } | |
126 | ||
61ba1cf9 ILT |
127 | // -o: Output file name. |
128 | const char* | |
129 | output_file_name() const | |
130 | { return this->output_file_name_; } | |
131 | ||
bae7f79e ILT |
132 | // -r: Whether we are doing a relocatable link. |
133 | bool | |
134 | is_relocatable() const | |
135 | { return this->is_relocatable_; } | |
136 | ||
9e2dcb77 ILT |
137 | // -s: Strip all symbols. |
138 | bool | |
139 | strip_all() const | |
140 | { return this->strip_ == STRIP_ALL; } | |
141 | ||
142 | // -S: Strip debugging information. | |
143 | bool | |
144 | strip_debug() const | |
145 | { return this->strip_ == STRIP_ALL || this->strip_ == STRIP_DEBUG; } | |
146 | ||
7da52175 ILT |
147 | // --eh-frame-hdr: Whether to generate an exception frame header. |
148 | bool | |
149 | create_eh_frame_hdr() const | |
150 | { return this->create_eh_frame_hdr_; } | |
151 | ||
41f542e7 ILT |
152 | // --rpath: The runtime search path. |
153 | const Dir_list& | |
154 | rpath() const | |
155 | { return this->rpath_; } | |
156 | ||
15b3cfae ILT |
157 | // --rpath-link: The link time search patch for shared libraries. |
158 | const Dir_list& | |
159 | rpath_link() const | |
160 | { return this->rpath_link_; } | |
161 | ||
92e059d8 ILT |
162 | // --shared: Whether generating a shared object. |
163 | bool | |
164 | is_shared() const | |
165 | { return this->is_shared_; } | |
166 | ||
bae7f79e ILT |
167 | // --static: Whether doing a static link. |
168 | bool | |
169 | is_static() const | |
170 | { return this->is_static_; } | |
171 | ||
0c5e9c22 | 172 | // --stats: Print resource usage statistics. |
e44fcf3b ILT |
173 | bool |
174 | print_stats() const | |
175 | { return this->print_stats_; } | |
176 | ||
ad2d6943 ILT |
177 | // --sysroot: The system root of a cross-linker. |
178 | const std::string& | |
179 | sysroot() const | |
180 | { return this->sysroot_; } | |
181 | ||
0c5e9c22 ILT |
182 | // -Ttext: The address of the .text section |
183 | uint64_t | |
184 | text_segment_address() const | |
185 | { return this->text_segment_address_; } | |
186 | ||
187 | // Whether -Ttext was used. | |
188 | bool | |
189 | user_set_text_segment_address() const | |
190 | { return this->text_segment_address_ != -1U; } | |
191 | ||
bae7f79e | 192 | private: |
dbe717ef ILT |
193 | // Don't copy this structure. |
194 | General_options(const General_options&); | |
195 | General_options& operator=(const General_options&); | |
196 | ||
bae7f79e ILT |
197 | friend class Command_line; |
198 | friend class options::Command_line_options; | |
199 | ||
9e2dcb77 ILT |
200 | // Which symbols to strip. |
201 | enum Strip | |
202 | { | |
203 | // Don't strip any symbols. | |
204 | STRIP_NONE, | |
205 | // Strip all symbols. | |
206 | STRIP_ALL, | |
207 | // Strip debugging information. | |
208 | STRIP_DEBUG | |
209 | }; | |
210 | ||
a6badf5a ILT |
211 | void |
212 | set_export_dynamic() | |
213 | { this->export_dynamic_ = true; } | |
214 | ||
dbe717ef ILT |
215 | void |
216 | set_dynamic_linker(const char* arg) | |
217 | { this->dynamic_linker_ = arg; } | |
218 | ||
bae7f79e ILT |
219 | void |
220 | add_to_search_path(const char* arg) | |
ad2d6943 ILT |
221 | { this->search_path_.push_back(Search_directory(arg, false)); } |
222 | ||
223 | void | |
224 | add_to_search_path_with_sysroot(const char* arg) | |
225 | { this->search_path_.push_back(Search_directory(arg, true)); } | |
bae7f79e | 226 | |
ca3a67a5 ILT |
227 | void |
228 | set_optimization_level(const char* arg) | |
229 | { this->optimization_level_ = atoi(arg); } | |
230 | ||
61ba1cf9 ILT |
231 | void |
232 | set_output_file_name(const char* arg) | |
233 | { this->output_file_name_ = arg; } | |
234 | ||
bae7f79e ILT |
235 | void |
236 | set_relocatable() | |
237 | { this->is_relocatable_ = true; } | |
238 | ||
9e2dcb77 ILT |
239 | void |
240 | set_strip_all() | |
241 | { this->strip_ = STRIP_ALL; } | |
242 | ||
46738c9a ILT |
243 | // Note: normalize_options() depends on the fact that this turns off |
244 | // STRIP_ALL if it were already set. | |
9e2dcb77 ILT |
245 | void |
246 | set_strip_debug() | |
247 | { this->strip_ = STRIP_DEBUG; } | |
248 | ||
7da52175 | 249 | void |
192f9b85 | 250 | set_create_eh_frame_hdr() |
7da52175 ILT |
251 | { this->create_eh_frame_hdr_ = true; } |
252 | ||
41f542e7 ILT |
253 | void |
254 | add_to_rpath(const char* arg) | |
ad2d6943 | 255 | { this->rpath_.push_back(Search_directory(arg, false)); } |
41f542e7 | 256 | |
15b3cfae ILT |
257 | void |
258 | add_to_rpath_link(const char* arg) | |
ad2d6943 | 259 | { this->rpath_link_.push_back(Search_directory(arg, false)); } |
15b3cfae | 260 | |
92e059d8 ILT |
261 | void |
262 | set_shared() | |
263 | { this->is_shared_ = true; } | |
264 | ||
bae7f79e ILT |
265 | void |
266 | set_static() | |
267 | { this->is_static_ = true; } | |
268 | ||
e44fcf3b ILT |
269 | void |
270 | set_stats() | |
271 | { this->print_stats_ = true; } | |
272 | ||
ad2d6943 ILT |
273 | void |
274 | set_sysroot(const char* arg) | |
275 | { this->sysroot_ = arg; } | |
276 | ||
0c5e9c22 ILT |
277 | void |
278 | set_text_segment_address(const char* arg) | |
279 | { | |
280 | char* endptr; | |
281 | this->text_segment_address_ = strtoull(arg, &endptr, 0); | |
282 | if (*endptr != '\0' | |
283 | || this->text_segment_address_ == -1U) | |
284 | { | |
285 | fprintf(stderr, _("%s: invalid argument to -Ttext: %s\n"), | |
286 | program_name, arg); | |
287 | ::exit(1); | |
288 | } | |
289 | } | |
290 | ||
652ec9bd ILT |
291 | void |
292 | ignore(const char*) | |
293 | { } | |
294 | ||
ad2d6943 ILT |
295 | // Apply any sysroot to the directory lists. |
296 | void | |
297 | add_sysroot(); | |
298 | ||
a6badf5a | 299 | bool export_dynamic_; |
dbe717ef | 300 | const char* dynamic_linker_; |
bae7f79e | 301 | Dir_list search_path_; |
ca3a67a5 | 302 | int optimization_level_; |
61ba1cf9 | 303 | const char* output_file_name_; |
bae7f79e | 304 | bool is_relocatable_; |
9e2dcb77 | 305 | Strip strip_; |
7da52175 | 306 | bool create_eh_frame_hdr_; |
41f542e7 | 307 | Dir_list rpath_; |
15b3cfae | 308 | Dir_list rpath_link_; |
92e059d8 | 309 | bool is_shared_; |
bae7f79e | 310 | bool is_static_; |
e44fcf3b | 311 | bool print_stats_; |
ad2d6943 | 312 | std::string sysroot_; |
0c5e9c22 | 313 | uint64_t text_segment_address_; |
bae7f79e ILT |
314 | }; |
315 | ||
316 | // The current state of the position dependent options. | |
317 | ||
318 | class Position_dependent_options | |
319 | { | |
320 | public: | |
321 | Position_dependent_options(); | |
322 | ||
323 | // -Bstatic: Whether we are searching for a static archive rather | |
dbe717ef | 324 | // than a shared object. |
bae7f79e | 325 | bool |
dbe717ef | 326 | do_static_search() const |
bae7f79e ILT |
327 | { return this->do_static_search_; } |
328 | ||
dbe717ef ILT |
329 | // --as-needed: Whether to add a DT_NEEDED argument only if the |
330 | // dynamic object is used. | |
331 | bool | |
332 | as_needed() const | |
333 | { return this->as_needed_; } | |
bae7f79e | 334 | |
4973341a ILT |
335 | // --whole-archive: Whether to include the entire contents of an |
336 | // --archive. | |
337 | bool | |
338 | include_whole_archive() const | |
339 | { return this->include_whole_archive_; } | |
340 | ||
bae7f79e ILT |
341 | void |
342 | set_static_search() | |
343 | { this->do_static_search_ = true; } | |
344 | ||
345 | void | |
346 | set_dynamic_search() | |
347 | { this->do_static_search_ = false; } | |
348 | ||
dbe717ef ILT |
349 | void |
350 | set_as_needed() | |
351 | { this->as_needed_ = true; } | |
352 | ||
353 | void | |
354 | clear_as_needed() | |
355 | { this->as_needed_ = false; } | |
356 | ||
4973341a ILT |
357 | void |
358 | set_whole_archive() | |
359 | { this->include_whole_archive_ = true; } | |
360 | ||
361 | void | |
362 | clear_whole_archive() | |
363 | { this->include_whole_archive_ = false; } | |
364 | ||
dbe717ef | 365 | private: |
bae7f79e | 366 | bool do_static_search_; |
dbe717ef | 367 | bool as_needed_; |
4973341a | 368 | bool include_whole_archive_; |
bae7f79e ILT |
369 | }; |
370 | ||
371 | // A single file or library argument from the command line. | |
372 | ||
ead1e424 | 373 | class Input_file_argument |
bae7f79e ILT |
374 | { |
375 | public: | |
51dee2fe ILT |
376 | // name: file name or library name |
377 | // is_lib: true if name is a library name: that is, emits the leading | |
378 | // "lib" and trailing ".so"/".a" from the name | |
379 | // extra_search_path: an extra directory to look for the file, prior | |
380 | // to checking the normal library search path. If this is "", | |
381 | // then no extra directory is added. | |
382 | // options: The position dependent options at this point in the | |
ad2d6943 | 383 | // command line, such as --whole-archive. |
ead1e424 | 384 | Input_file_argument() |
51dee2fe | 385 | : name_(), is_lib_(false), extra_search_path_(""), options_() |
ead1e424 ILT |
386 | { } |
387 | ||
388 | Input_file_argument(const char* name, bool is_lib, | |
51dee2fe | 389 | const char* extra_search_path, |
ead1e424 | 390 | const Position_dependent_options& options) |
51dee2fe ILT |
391 | : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path), |
392 | options_(options) | |
bae7f79e ILT |
393 | { } |
394 | ||
395 | const char* | |
396 | name() const | |
dbe717ef | 397 | { return this->name_.c_str(); } |
bae7f79e ILT |
398 | |
399 | const Position_dependent_options& | |
400 | options() const | |
401 | { return this->options_; } | |
402 | ||
403 | bool | |
404 | is_lib() const | |
61ba1cf9 | 405 | { return this->is_lib_; } |
bae7f79e | 406 | |
51dee2fe ILT |
407 | const char* |
408 | extra_search_path() const | |
409 | { | |
410 | return (this->extra_search_path_.empty() | |
411 | ? NULL | |
412 | : this->extra_search_path_.c_str()); | |
413 | } | |
414 | ||
415 | // Return whether this file may require a search using the -L | |
416 | // options. | |
417 | bool | |
418 | may_need_search() const | |
419 | { return this->is_lib_ || !this->extra_search_path_.empty(); } | |
420 | ||
bae7f79e | 421 | private: |
dbe717ef ILT |
422 | // We use std::string, not const char*, here for convenience when |
423 | // using script files, so that we do not have to preserve the string | |
424 | // in that case. | |
425 | std::string name_; | |
61ba1cf9 | 426 | bool is_lib_; |
51dee2fe | 427 | std::string extra_search_path_; |
bae7f79e ILT |
428 | Position_dependent_options options_; |
429 | }; | |
430 | ||
ead1e424 ILT |
431 | // A file or library, or a group, from the command line. |
432 | ||
433 | class Input_argument | |
434 | { | |
435 | public: | |
436 | // Create a file or library argument. | |
437 | explicit Input_argument(Input_file_argument file) | |
438 | : is_file_(true), file_(file), group_(NULL) | |
439 | { } | |
440 | ||
441 | // Create a group argument. | |
442 | explicit Input_argument(Input_file_group* group) | |
443 | : is_file_(false), group_(group) | |
444 | { } | |
445 | ||
446 | // Return whether this is a file. | |
447 | bool | |
448 | is_file() const | |
449 | { return this->is_file_; } | |
450 | ||
451 | // Return whether this is a group. | |
452 | bool | |
453 | is_group() const | |
454 | { return !this->is_file_; } | |
455 | ||
456 | // Return the information about the file. | |
457 | const Input_file_argument& | |
458 | file() const | |
459 | { | |
a3ad94ed | 460 | gold_assert(this->is_file_); |
ead1e424 ILT |
461 | return this->file_; |
462 | } | |
463 | ||
464 | // Return the information about the group. | |
465 | const Input_file_group* | |
466 | group() const | |
467 | { | |
a3ad94ed | 468 | gold_assert(!this->is_file_); |
ead1e424 ILT |
469 | return this->group_; |
470 | } | |
471 | ||
472 | Input_file_group* | |
473 | group() | |
474 | { | |
a3ad94ed | 475 | gold_assert(!this->is_file_); |
ead1e424 ILT |
476 | return this->group_; |
477 | } | |
478 | ||
479 | private: | |
480 | bool is_file_; | |
481 | Input_file_argument file_; | |
482 | Input_file_group* group_; | |
483 | }; | |
484 | ||
485 | // A group from the command line. This is a set of arguments within | |
486 | // --start-group ... --end-group. | |
487 | ||
488 | class Input_file_group | |
92e059d8 | 489 | { |
ead1e424 ILT |
490 | public: |
491 | typedef std::vector<Input_argument> Files; | |
492 | typedef Files::const_iterator const_iterator; | |
493 | ||
494 | Input_file_group() | |
495 | : files_() | |
496 | { } | |
497 | ||
498 | // Add a file to the end of the group. | |
499 | void | |
500 | add_file(const Input_file_argument& arg) | |
501 | { this->files_.push_back(Input_argument(arg)); } | |
502 | ||
503 | // Iterators to iterate over the group contents. | |
504 | ||
505 | const_iterator | |
506 | begin() const | |
507 | { return this->files_.begin(); } | |
508 | ||
509 | const_iterator | |
510 | end() const | |
511 | { return this->files_.end(); } | |
512 | ||
513 | private: | |
514 | Files files_; | |
92e059d8 ILT |
515 | }; |
516 | ||
dbe717ef ILT |
517 | // A list of files from the command line or a script. |
518 | ||
519 | class Input_arguments | |
520 | { | |
521 | public: | |
522 | typedef std::vector<Input_argument> Input_argument_list; | |
523 | typedef Input_argument_list::const_iterator const_iterator; | |
524 | ||
525 | Input_arguments() | |
526 | : input_argument_list_(), in_group_(false) | |
527 | { } | |
528 | ||
529 | // Add a file. | |
530 | void | |
531 | add_file(const Input_file_argument& arg); | |
532 | ||
533 | // Start a group (the --start-group option). | |
534 | void | |
535 | start_group(); | |
536 | ||
537 | // End a group (the --end-group option). | |
538 | void | |
539 | end_group(); | |
540 | ||
541 | // Return whether we are currently in a group. | |
542 | bool | |
543 | in_group() const | |
544 | { return this->in_group_; } | |
545 | ||
546 | // Iterators to iterate over the list of input files. | |
547 | ||
548 | const_iterator | |
549 | begin() const | |
550 | { return this->input_argument_list_.begin(); } | |
551 | ||
552 | const_iterator | |
553 | end() const | |
554 | { return this->input_argument_list_.end(); } | |
555 | ||
556 | // Return whether the list is empty. | |
557 | bool | |
558 | empty() const | |
559 | { return this->input_argument_list_.empty(); } | |
560 | ||
561 | private: | |
562 | Input_argument_list input_argument_list_; | |
563 | bool in_group_; | |
564 | }; | |
565 | ||
bae7f79e ILT |
566 | // All the information read from the command line. |
567 | ||
568 | class Command_line | |
569 | { | |
570 | public: | |
ead1e424 ILT |
571 | typedef Input_arguments::const_iterator const_iterator; |
572 | ||
bae7f79e ILT |
573 | Command_line(); |
574 | ||
575 | // Process the command line options. This will exit with an | |
576 | // appropriate error message if an unrecognized option is seen. | |
577 | void | |
578 | process(int argc, char** argv); | |
579 | ||
61ba1cf9 ILT |
580 | // Handle a -l option. |
581 | int | |
582 | process_l_option(int, char**, char*); | |
583 | ||
ead1e424 ILT |
584 | // Handle a --start-group option. |
585 | void | |
586 | start_group(const char* arg); | |
587 | ||
588 | // Handle a --end-group option. | |
589 | void | |
590 | end_group(const char* arg); | |
591 | ||
61ba1cf9 | 592 | // Get the general options. |
bae7f79e ILT |
593 | const General_options& |
594 | options() const | |
595 | { return this->options_; } | |
596 | ||
ead1e424 ILT |
597 | // Iterators to iterate over the list of input files. |
598 | ||
599 | const_iterator | |
600 | begin() const | |
601 | { return this->inputs_.begin(); } | |
602 | ||
603 | const_iterator | |
604 | end() const | |
605 | { return this->inputs_.end(); } | |
bae7f79e ILT |
606 | |
607 | private: | |
ead1e424 ILT |
608 | Command_line(const Command_line&); |
609 | Command_line& operator=(const Command_line&); | |
610 | ||
611 | // Report usage error. | |
612 | void | |
613 | usage() ATTRIBUTE_NORETURN; | |
614 | void | |
615 | usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN; | |
616 | void | |
617 | usage(const char* msg, char opt) ATTRIBUTE_NORETURN; | |
618 | ||
619 | // Apply a command line option. | |
620 | void | |
621 | apply_option(const gold::options::One_option&, const char*); | |
622 | ||
623 | // Add a file. | |
624 | void | |
625 | add_file(const char* name, bool is_lib); | |
bae7f79e | 626 | |
46738c9a ILT |
627 | // Examine the result of processing the command-line, and verify |
628 | // the flags do not contradict each other or are otherwise illegal. | |
629 | void | |
630 | normalize_options(); | |
631 | ||
bae7f79e ILT |
632 | General_options options_; |
633 | Position_dependent_options position_options_; | |
ead1e424 | 634 | Input_arguments inputs_; |
bae7f79e ILT |
635 | }; |
636 | ||
637 | } // End namespace gold. | |
638 | ||
639 | #endif // !defined(GOLD_OPTIONS_H) |