*** empty log message ***
[deliverable/binutils-gdb.git] / gold / options.h
CommitLineData
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
40namespace gold
41{
42
43class Command_line;
ead1e424 44class Input_file_group;
bae7f79e
ILT
45
46namespace options {
47
48class Command_line_options;
49struct One_option;
35cdfc9a 50struct One_z_option;
bae7f79e
ILT
51
52} // End namespace gold::options.
53
ad2d6943
ILT
54// A directory to search. For each directory we record whether it is
55// in the sysroot. We need to know this so that, if a linker script
56// is found within the sysroot, we will apply the sysroot to any files
57// named by that script.
58
59class Search_directory
60{
61 public:
62 // We need a default constructor because we put this in a
63 // std::vector.
64 Search_directory()
65 : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
66 { }
67
68 // This is the usual constructor.
69 Search_directory(const char* name, bool put_in_sysroot)
70 : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
71 { gold_assert(!this->name_.empty()); }
72
73 // This is called if we have a sysroot. The sysroot is prefixed to
74 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
75 // set to true for any enries which are in the sysroot (this will
76 // naturally include any entries for which put_in_sysroot_ is true).
77 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
78 // passing SYSROOT to lrealpath.
79 void
80 add_sysroot(const char* sysroot, const char* canonical_sysroot);
81
82 // Get the directory name.
83 const std::string&
84 name() const
85 { return this->name_; }
86
87 // Return whether this directory is in the sysroot.
88 bool
89 is_in_sysroot() const
90 { return this->is_in_sysroot_; }
91
92 private:
93 std::string name_;
94 bool put_in_sysroot_;
95 bool is_in_sysroot_;
96};
97
bae7f79e
ILT
98// The position independent options which apply to the whole link.
99// There are a lot of them.
100
101class General_options
102{
103 public:
104 General_options();
105
a6badf5a
ILT
106 // -E: export dynamic symbols.
107 bool
108 export_dynamic() const
109 { return this->export_dynamic_; }
110
dbe717ef
ILT
111 // -I: dynamic linker name.
112 const char*
113 dynamic_linker() const
114 { return this->dynamic_linker_; }
115
bae7f79e 116 // -L: Library search path.
ad2d6943 117 typedef std::vector<Search_directory> Dir_list;
bae7f79e
ILT
118
119 const Dir_list&
120 search_path() const
121 { return this->search_path_; }
122
ca3a67a5
ILT
123 // -O: optimization level (0: don't try to optimize output size).
124 int
125 optimization_level() const
126 { return this->optimization_level_; }
127
61ba1cf9
ILT
128 // -o: Output file name.
129 const char*
130 output_file_name() const
131 { return this->output_file_name_; }
132
bae7f79e
ILT
133 // -r: Whether we are doing a relocatable link.
134 bool
135 is_relocatable() const
136 { return this->is_relocatable_; }
137
9e2dcb77
ILT
138 // -s: Strip all symbols.
139 bool
140 strip_all() const
141 { return this->strip_ == STRIP_ALL; }
142
143 // -S: Strip debugging information.
144 bool
145 strip_debug() const
146 { return this->strip_ == STRIP_ALL || this->strip_ == STRIP_DEBUG; }
147
51b08ebe
ILT
148 // -Bsymbolic: bind defined symbols locally.
149 bool
150 symbolic() const
151 { return this->symbolic_; }
152
7da52175
ILT
153 // --eh-frame-hdr: Whether to generate an exception frame header.
154 bool
155 create_eh_frame_hdr() const
156 { return this->create_eh_frame_hdr_; }
157
41f542e7
ILT
158 // --rpath: The runtime search path.
159 const Dir_list&
160 rpath() const
161 { return this->rpath_; }
162
15b3cfae
ILT
163 // --rpath-link: The link time search patch for shared libraries.
164 const Dir_list&
165 rpath_link() const
166 { return this->rpath_link_; }
167
92e059d8
ILT
168 // --shared: Whether generating a shared object.
169 bool
170 is_shared() const
171 { return this->is_shared_; }
172
bae7f79e
ILT
173 // --static: Whether doing a static link.
174 bool
175 is_static() const
176 { return this->is_static_; }
177
0c5e9c22 178 // --stats: Print resource usage statistics.
e44fcf3b
ILT
179 bool
180 print_stats() const
181 { return this->print_stats_; }
182
ad2d6943
ILT
183 // --sysroot: The system root of a cross-linker.
184 const std::string&
185 sysroot() const
186 { return this->sysroot_; }
187
0c5e9c22
ILT
188 // -Ttext: The address of the .text section
189 uint64_t
190 text_segment_address() const
191 { return this->text_segment_address_; }
192
193 // Whether -Ttext was used.
194 bool
195 user_set_text_segment_address() const
196 { return this->text_segment_address_ != -1U; }
197
fe9a4c12
ILT
198 // --threads: Whether to use threads.
199 bool
200 threads() const
201 { return this->threads_; }
202
203 // --thread-count-initial: Threads to use in initial pass.
204 int
205 thread_count_initial() const
206 { return this->thread_count_initial_; }
207
208 // --thread-count-middle: Threads to use in middle pass.
209 int
210 thread_count_middle() const
211 { return this->thread_count_middle_; }
212
213 // --thread-count-final: Threads to use in final pass.
214 int
215 thread_count_final() const
216 { return this->thread_count_final_; }
217
35cdfc9a
ILT
218 // -z execstack, -z noexecstack
219 bool
220 is_execstack_set() const
221 { return this->execstack_ != EXECSTACK_FROM_INPUT; }
222
223 bool
224 is_stack_executable() const
225 { return this->execstack_ == EXECSTACK_YES; }
226
bae7f79e 227 private:
dbe717ef
ILT
228 // Don't copy this structure.
229 General_options(const General_options&);
230 General_options& operator=(const General_options&);
231
bae7f79e
ILT
232 friend class Command_line;
233 friend class options::Command_line_options;
234
9e2dcb77
ILT
235 // Which symbols to strip.
236 enum Strip
237 {
238 // Don't strip any symbols.
239 STRIP_NONE,
240 // Strip all symbols.
241 STRIP_ALL,
242 // Strip debugging information.
243 STRIP_DEBUG
244 };
245
35cdfc9a
ILT
246 // Whether to mark the stack as executable.
247 enum Execstack
248 {
249 // Not set on command line.
250 EXECSTACK_FROM_INPUT,
251 // Mark the stack as executable.
252 EXECSTACK_YES,
253 // Mark the stack as not executable.
254 EXECSTACK_NO
255 };
256
a6badf5a
ILT
257 void
258 set_export_dynamic()
259 { this->export_dynamic_ = true; }
260
dbe717ef
ILT
261 void
262 set_dynamic_linker(const char* arg)
263 { this->dynamic_linker_ = arg; }
264
bae7f79e
ILT
265 void
266 add_to_search_path(const char* arg)
ad2d6943
ILT
267 { this->search_path_.push_back(Search_directory(arg, false)); }
268
269 void
270 add_to_search_path_with_sysroot(const char* arg)
271 { this->search_path_.push_back(Search_directory(arg, true)); }
bae7f79e 272
ca3a67a5
ILT
273 void
274 set_optimization_level(const char* arg)
275 { this->optimization_level_ = atoi(arg); }
276
61ba1cf9
ILT
277 void
278 set_output_file_name(const char* arg)
279 { this->output_file_name_ = arg; }
280
bae7f79e
ILT
281 void
282 set_relocatable()
283 { this->is_relocatable_ = true; }
284
9e2dcb77
ILT
285 void
286 set_strip_all()
287 { this->strip_ = STRIP_ALL; }
288
46738c9a
ILT
289 // Note: normalize_options() depends on the fact that this turns off
290 // STRIP_ALL if it were already set.
9e2dcb77
ILT
291 void
292 set_strip_debug()
293 { this->strip_ = STRIP_DEBUG; }
294
51b08ebe
ILT
295 void
296 set_symbolic()
297 { this->symbolic_ = true; }
298
7da52175 299 void
192f9b85 300 set_create_eh_frame_hdr()
7da52175
ILT
301 { this->create_eh_frame_hdr_ = true; }
302
41f542e7
ILT
303 void
304 add_to_rpath(const char* arg)
ad2d6943 305 { this->rpath_.push_back(Search_directory(arg, false)); }
41f542e7 306
15b3cfae
ILT
307 void
308 add_to_rpath_link(const char* arg)
ad2d6943 309 { this->rpath_link_.push_back(Search_directory(arg, false)); }
15b3cfae 310
92e059d8
ILT
311 void
312 set_shared()
313 { this->is_shared_ = true; }
314
bae7f79e
ILT
315 void
316 set_static()
317 { this->is_static_ = true; }
318
5d57def3
ILT
319 void
320 set_script(const char* arg)
321 {
322 fprintf(stderr, _("%s: cannot parse %s: -T/--script not yet supported\n"),
323 program_name, arg);
324 ::exit(1);
325 }
326
e44fcf3b
ILT
327 void
328 set_stats()
329 { this->print_stats_ = true; }
330
ad2d6943
ILT
331 void
332 set_sysroot(const char* arg)
333 { this->sysroot_ = arg; }
334
0c5e9c22
ILT
335 void
336 set_text_segment_address(const char* arg)
337 {
338 char* endptr;
339 this->text_segment_address_ = strtoull(arg, &endptr, 0);
340 if (*endptr != '\0'
341 || this->text_segment_address_ == -1U)
342 {
343 fprintf(stderr, _("%s: invalid argument to -Ttext: %s\n"),
344 program_name, arg);
345 ::exit(1);
346 }
347 }
348
fe9a4c12
ILT
349 int
350 parse_thread_count(const char* arg)
351 {
352 char* endptr;
353 int count = strtol(arg, &endptr, 0);
354 if (*endptr != '\0' || count < 0)
355 {
356 fprintf(stderr, _("%s: invalid thread count: %s\n"),
357 program_name, arg);
358 ::exit(1);
359 }
360 return count;
361 }
362
363 void
364 set_threads()
365 { this->threads_ = true; }
366
367 void
368 clear_threads()
369 { this->threads_ = false; }
370
371 void
372 set_thread_count(const char* arg)
373 {
374 int count = this->parse_thread_count(arg);
375 this->thread_count_initial_ = count;
376 this->thread_count_middle_ = count;
377 this->thread_count_final_ = count;
378 }
379
380 void
381 set_thread_count_initial(const char* arg)
382 { this->thread_count_initial_ = this->parse_thread_count(arg); }
383
384 void
385 set_thread_count_middle(const char* arg)
386 { this->thread_count_initial_ = this->parse_thread_count(arg); }
387
388 void
389 set_thread_count_final(const char* arg)
390 { this->thread_count_initial_ = this->parse_thread_count(arg); }
391
652ec9bd
ILT
392 void
393 ignore(const char*)
394 { }
395
35cdfc9a
ILT
396 void
397 set_execstack()
398 { this->execstack_ = EXECSTACK_YES; }
399
400 void
401 set_noexecstack()
402 { this->execstack_ = EXECSTACK_NO; }
403
404 // Handle the -z option.
405 void
406 handle_z_option(const char*);
407
ad2d6943
ILT
408 // Apply any sysroot to the directory lists.
409 void
410 add_sysroot();
411
a6badf5a 412 bool export_dynamic_;
dbe717ef 413 const char* dynamic_linker_;
bae7f79e 414 Dir_list search_path_;
ca3a67a5 415 int optimization_level_;
61ba1cf9 416 const char* output_file_name_;
bae7f79e 417 bool is_relocatable_;
9e2dcb77 418 Strip strip_;
51b08ebe 419 bool symbolic_;
7da52175 420 bool create_eh_frame_hdr_;
41f542e7 421 Dir_list rpath_;
15b3cfae 422 Dir_list rpath_link_;
92e059d8 423 bool is_shared_;
bae7f79e 424 bool is_static_;
e44fcf3b 425 bool print_stats_;
ad2d6943 426 std::string sysroot_;
0c5e9c22 427 uint64_t text_segment_address_;
fe9a4c12
ILT
428 bool threads_;
429 int thread_count_initial_;
430 int thread_count_middle_;
431 int thread_count_final_;
35cdfc9a 432 Execstack execstack_;
bae7f79e
ILT
433};
434
435// The current state of the position dependent options.
436
437class Position_dependent_options
438{
439 public:
440 Position_dependent_options();
441
61611222
ILT
442 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
443 // -rather than a shared object.
bae7f79e 444 bool
dbe717ef 445 do_static_search() const
bae7f79e
ILT
446 { return this->do_static_search_; }
447
dbe717ef
ILT
448 // --as-needed: Whether to add a DT_NEEDED argument only if the
449 // dynamic object is used.
450 bool
451 as_needed() const
452 { return this->as_needed_; }
bae7f79e 453
4973341a
ILT
454 // --whole-archive: Whether to include the entire contents of an
455 // --archive.
456 bool
457 include_whole_archive() const
458 { return this->include_whole_archive_; }
459
bae7f79e
ILT
460 void
461 set_static_search()
462 { this->do_static_search_ = true; }
463
464 void
465 set_dynamic_search()
466 { this->do_static_search_ = false; }
467
dbe717ef
ILT
468 void
469 set_as_needed()
470 { this->as_needed_ = true; }
471
472 void
473 clear_as_needed()
474 { this->as_needed_ = false; }
475
4973341a
ILT
476 void
477 set_whole_archive()
478 { this->include_whole_archive_ = true; }
479
480 void
481 clear_whole_archive()
482 { this->include_whole_archive_ = false; }
483
dbe717ef 484 private:
bae7f79e 485 bool do_static_search_;
dbe717ef 486 bool as_needed_;
4973341a 487 bool include_whole_archive_;
bae7f79e
ILT
488};
489
490// A single file or library argument from the command line.
491
ead1e424 492class Input_file_argument
bae7f79e
ILT
493{
494 public:
51dee2fe
ILT
495 // name: file name or library name
496 // is_lib: true if name is a library name: that is, emits the leading
497 // "lib" and trailing ".so"/".a" from the name
498 // extra_search_path: an extra directory to look for the file, prior
499 // to checking the normal library search path. If this is "",
500 // then no extra directory is added.
501 // options: The position dependent options at this point in the
ad2d6943 502 // command line, such as --whole-archive.
ead1e424 503 Input_file_argument()
51dee2fe 504 : name_(), is_lib_(false), extra_search_path_(""), options_()
ead1e424
ILT
505 { }
506
507 Input_file_argument(const char* name, bool is_lib,
51dee2fe 508 const char* extra_search_path,
ead1e424 509 const Position_dependent_options& options)
51dee2fe
ILT
510 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
511 options_(options)
bae7f79e
ILT
512 { }
513
514 const char*
515 name() const
dbe717ef 516 { return this->name_.c_str(); }
bae7f79e
ILT
517
518 const Position_dependent_options&
519 options() const
520 { return this->options_; }
521
522 bool
523 is_lib() const
61ba1cf9 524 { return this->is_lib_; }
bae7f79e 525
51dee2fe
ILT
526 const char*
527 extra_search_path() const
528 {
529 return (this->extra_search_path_.empty()
530 ? NULL
531 : this->extra_search_path_.c_str());
532 }
533
534 // Return whether this file may require a search using the -L
535 // options.
536 bool
537 may_need_search() const
538 { return this->is_lib_ || !this->extra_search_path_.empty(); }
539
bae7f79e 540 private:
dbe717ef
ILT
541 // We use std::string, not const char*, here for convenience when
542 // using script files, so that we do not have to preserve the string
543 // in that case.
544 std::string name_;
61ba1cf9 545 bool is_lib_;
51dee2fe 546 std::string extra_search_path_;
bae7f79e
ILT
547 Position_dependent_options options_;
548};
549
ead1e424
ILT
550// A file or library, or a group, from the command line.
551
552class Input_argument
553{
554 public:
555 // Create a file or library argument.
556 explicit Input_argument(Input_file_argument file)
557 : is_file_(true), file_(file), group_(NULL)
558 { }
559
560 // Create a group argument.
561 explicit Input_argument(Input_file_group* group)
562 : is_file_(false), group_(group)
563 { }
564
565 // Return whether this is a file.
566 bool
567 is_file() const
568 { return this->is_file_; }
569
570 // Return whether this is a group.
571 bool
572 is_group() const
573 { return !this->is_file_; }
574
575 // Return the information about the file.
576 const Input_file_argument&
577 file() const
578 {
a3ad94ed 579 gold_assert(this->is_file_);
ead1e424
ILT
580 return this->file_;
581 }
582
583 // Return the information about the group.
584 const Input_file_group*
585 group() const
586 {
a3ad94ed 587 gold_assert(!this->is_file_);
ead1e424
ILT
588 return this->group_;
589 }
590
591 Input_file_group*
592 group()
593 {
a3ad94ed 594 gold_assert(!this->is_file_);
ead1e424
ILT
595 return this->group_;
596 }
597
598 private:
599 bool is_file_;
600 Input_file_argument file_;
601 Input_file_group* group_;
602};
603
604// A group from the command line. This is a set of arguments within
605// --start-group ... --end-group.
606
607class Input_file_group
92e059d8 608{
ead1e424
ILT
609 public:
610 typedef std::vector<Input_argument> Files;
611 typedef Files::const_iterator const_iterator;
612
613 Input_file_group()
614 : files_()
615 { }
616
617 // Add a file to the end of the group.
618 void
619 add_file(const Input_file_argument& arg)
620 { this->files_.push_back(Input_argument(arg)); }
621
622 // Iterators to iterate over the group contents.
623
624 const_iterator
625 begin() const
626 { return this->files_.begin(); }
627
628 const_iterator
629 end() const
630 { return this->files_.end(); }
631
632 private:
633 Files files_;
92e059d8
ILT
634};
635
dbe717ef
ILT
636// A list of files from the command line or a script.
637
638class Input_arguments
639{
640 public:
641 typedef std::vector<Input_argument> Input_argument_list;
642 typedef Input_argument_list::const_iterator const_iterator;
643
644 Input_arguments()
645 : input_argument_list_(), in_group_(false)
646 { }
647
648 // Add a file.
649 void
650 add_file(const Input_file_argument& arg);
651
652 // Start a group (the --start-group option).
653 void
654 start_group();
655
656 // End a group (the --end-group option).
657 void
658 end_group();
659
660 // Return whether we are currently in a group.
661 bool
662 in_group() const
663 { return this->in_group_; }
664
fe9a4c12
ILT
665 // The number of entries in the list.
666 int
667 size() const
668 { return this->input_argument_list_.size(); }
669
dbe717ef
ILT
670 // Iterators to iterate over the list of input files.
671
672 const_iterator
673 begin() const
674 { return this->input_argument_list_.begin(); }
675
676 const_iterator
677 end() const
678 { return this->input_argument_list_.end(); }
679
680 // Return whether the list is empty.
681 bool
682 empty() const
683 { return this->input_argument_list_.empty(); }
684
685 private:
686 Input_argument_list input_argument_list_;
687 bool in_group_;
688};
689
bae7f79e
ILT
690// All the information read from the command line.
691
692class Command_line
693{
694 public:
ead1e424
ILT
695 typedef Input_arguments::const_iterator const_iterator;
696
bae7f79e
ILT
697 Command_line();
698
699 // Process the command line options. This will exit with an
700 // appropriate error message if an unrecognized option is seen.
701 void
702 process(int argc, char** argv);
703
61ba1cf9
ILT
704 // Handle a -l option.
705 int
706 process_l_option(int, char**, char*);
707
ead1e424
ILT
708 // Handle a --start-group option.
709 void
710 start_group(const char* arg);
711
712 // Handle a --end-group option.
713 void
714 end_group(const char* arg);
715
61ba1cf9 716 // Get the general options.
bae7f79e
ILT
717 const General_options&
718 options() const
719 { return this->options_; }
720
fe9a4c12
ILT
721 // The number of input files.
722 int
723 number_of_input_files() const
724 { return this->inputs_.size(); }
725
ead1e424
ILT
726 // Iterators to iterate over the list of input files.
727
728 const_iterator
729 begin() const
730 { return this->inputs_.begin(); }
731
732 const_iterator
733 end() const
734 { return this->inputs_.end(); }
bae7f79e
ILT
735
736 private:
ead1e424
ILT
737 Command_line(const Command_line&);
738 Command_line& operator=(const Command_line&);
739
740 // Report usage error.
741 void
742 usage() ATTRIBUTE_NORETURN;
743 void
744 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
745 void
746 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
747
748 // Apply a command line option.
749 void
750 apply_option(const gold::options::One_option&, const char*);
751
752 // Add a file.
753 void
754 add_file(const char* name, bool is_lib);
bae7f79e 755
46738c9a
ILT
756 // Examine the result of processing the command-line, and verify
757 // the flags do not contradict each other or are otherwise illegal.
758 void
759 normalize_options();
760
bae7f79e
ILT
761 General_options options_;
762 Position_dependent_options position_options_;
ead1e424 763 Input_arguments inputs_;
bae7f79e
ILT
764};
765
766} // End namespace gold.
767
768#endif // !defined(GOLD_OPTIONS_H)
This page took 0.0943889999999999 seconds and 4 git commands to generate.