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