2007-10-23 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
[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
e44fcf3b
ILT
319 void
320 set_stats()
321 { this->print_stats_ = true; }
322
ad2d6943
ILT
323 void
324 set_sysroot(const char* arg)
325 { this->sysroot_ = arg; }
326
0c5e9c22
ILT
327 void
328 set_text_segment_address(const char* arg)
329 {
330 char* endptr;
331 this->text_segment_address_ = strtoull(arg, &endptr, 0);
332 if (*endptr != '\0'
333 || this->text_segment_address_ == -1U)
334 {
335 fprintf(stderr, _("%s: invalid argument to -Ttext: %s\n"),
336 program_name, arg);
337 ::exit(1);
338 }
339 }
340
fe9a4c12
ILT
341 int
342 parse_thread_count(const char* arg)
343 {
344 char* endptr;
345 int count = strtol(arg, &endptr, 0);
346 if (*endptr != '\0' || count < 0)
347 {
348 fprintf(stderr, _("%s: invalid thread count: %s\n"),
349 program_name, arg);
350 ::exit(1);
351 }
352 return count;
353 }
354
355 void
356 set_threads()
357 { this->threads_ = true; }
358
359 void
360 clear_threads()
361 { this->threads_ = false; }
362
363 void
364 set_thread_count(const char* arg)
365 {
366 int count = this->parse_thread_count(arg);
367 this->thread_count_initial_ = count;
368 this->thread_count_middle_ = count;
369 this->thread_count_final_ = count;
370 }
371
372 void
373 set_thread_count_initial(const char* arg)
374 { this->thread_count_initial_ = this->parse_thread_count(arg); }
375
376 void
377 set_thread_count_middle(const char* arg)
378 { this->thread_count_initial_ = this->parse_thread_count(arg); }
379
380 void
381 set_thread_count_final(const char* arg)
382 { this->thread_count_initial_ = this->parse_thread_count(arg); }
383
652ec9bd
ILT
384 void
385 ignore(const char*)
386 { }
387
35cdfc9a
ILT
388 void
389 set_execstack()
390 { this->execstack_ = EXECSTACK_YES; }
391
392 void
393 set_noexecstack()
394 { this->execstack_ = EXECSTACK_NO; }
395
396 // Handle the -z option.
397 void
398 handle_z_option(const char*);
399
ad2d6943
ILT
400 // Apply any sysroot to the directory lists.
401 void
402 add_sysroot();
403
a6badf5a 404 bool export_dynamic_;
dbe717ef 405 const char* dynamic_linker_;
bae7f79e 406 Dir_list search_path_;
ca3a67a5 407 int optimization_level_;
61ba1cf9 408 const char* output_file_name_;
bae7f79e 409 bool is_relocatable_;
9e2dcb77 410 Strip strip_;
51b08ebe 411 bool symbolic_;
7da52175 412 bool create_eh_frame_hdr_;
41f542e7 413 Dir_list rpath_;
15b3cfae 414 Dir_list rpath_link_;
92e059d8 415 bool is_shared_;
bae7f79e 416 bool is_static_;
e44fcf3b 417 bool print_stats_;
ad2d6943 418 std::string sysroot_;
0c5e9c22 419 uint64_t text_segment_address_;
fe9a4c12
ILT
420 bool threads_;
421 int thread_count_initial_;
422 int thread_count_middle_;
423 int thread_count_final_;
35cdfc9a 424 Execstack execstack_;
bae7f79e
ILT
425};
426
427// The current state of the position dependent options.
428
429class Position_dependent_options
430{
431 public:
432 Position_dependent_options();
433
61611222
ILT
434 // -Bdynamic/-Bstatic: Whether we are searching for a static archive
435 // -rather than a shared object.
bae7f79e 436 bool
dbe717ef 437 do_static_search() const
bae7f79e
ILT
438 { return this->do_static_search_; }
439
dbe717ef
ILT
440 // --as-needed: Whether to add a DT_NEEDED argument only if the
441 // dynamic object is used.
442 bool
443 as_needed() const
444 { return this->as_needed_; }
bae7f79e 445
4973341a
ILT
446 // --whole-archive: Whether to include the entire contents of an
447 // --archive.
448 bool
449 include_whole_archive() const
450 { return this->include_whole_archive_; }
451
bae7f79e
ILT
452 void
453 set_static_search()
454 { this->do_static_search_ = true; }
455
456 void
457 set_dynamic_search()
458 { this->do_static_search_ = false; }
459
dbe717ef
ILT
460 void
461 set_as_needed()
462 { this->as_needed_ = true; }
463
464 void
465 clear_as_needed()
466 { this->as_needed_ = false; }
467
4973341a
ILT
468 void
469 set_whole_archive()
470 { this->include_whole_archive_ = true; }
471
472 void
473 clear_whole_archive()
474 { this->include_whole_archive_ = false; }
475
dbe717ef 476 private:
bae7f79e 477 bool do_static_search_;
dbe717ef 478 bool as_needed_;
4973341a 479 bool include_whole_archive_;
bae7f79e
ILT
480};
481
482// A single file or library argument from the command line.
483
ead1e424 484class Input_file_argument
bae7f79e
ILT
485{
486 public:
51dee2fe
ILT
487 // name: file name or library name
488 // is_lib: true if name is a library name: that is, emits the leading
489 // "lib" and trailing ".so"/".a" from the name
490 // extra_search_path: an extra directory to look for the file, prior
491 // to checking the normal library search path. If this is "",
492 // then no extra directory is added.
493 // options: The position dependent options at this point in the
ad2d6943 494 // command line, such as --whole-archive.
ead1e424 495 Input_file_argument()
51dee2fe 496 : name_(), is_lib_(false), extra_search_path_(""), options_()
ead1e424
ILT
497 { }
498
499 Input_file_argument(const char* name, bool is_lib,
51dee2fe 500 const char* extra_search_path,
ead1e424 501 const Position_dependent_options& options)
51dee2fe
ILT
502 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
503 options_(options)
bae7f79e
ILT
504 { }
505
506 const char*
507 name() const
dbe717ef 508 { return this->name_.c_str(); }
bae7f79e
ILT
509
510 const Position_dependent_options&
511 options() const
512 { return this->options_; }
513
514 bool
515 is_lib() const
61ba1cf9 516 { return this->is_lib_; }
bae7f79e 517
51dee2fe
ILT
518 const char*
519 extra_search_path() const
520 {
521 return (this->extra_search_path_.empty()
522 ? NULL
523 : this->extra_search_path_.c_str());
524 }
525
526 // Return whether this file may require a search using the -L
527 // options.
528 bool
529 may_need_search() const
530 { return this->is_lib_ || !this->extra_search_path_.empty(); }
531
bae7f79e 532 private:
dbe717ef
ILT
533 // We use std::string, not const char*, here for convenience when
534 // using script files, so that we do not have to preserve the string
535 // in that case.
536 std::string name_;
61ba1cf9 537 bool is_lib_;
51dee2fe 538 std::string extra_search_path_;
bae7f79e
ILT
539 Position_dependent_options options_;
540};
541
ead1e424
ILT
542// A file or library, or a group, from the command line.
543
544class Input_argument
545{
546 public:
547 // Create a file or library argument.
548 explicit Input_argument(Input_file_argument file)
549 : is_file_(true), file_(file), group_(NULL)
550 { }
551
552 // Create a group argument.
553 explicit Input_argument(Input_file_group* group)
554 : is_file_(false), group_(group)
555 { }
556
557 // Return whether this is a file.
558 bool
559 is_file() const
560 { return this->is_file_; }
561
562 // Return whether this is a group.
563 bool
564 is_group() const
565 { return !this->is_file_; }
566
567 // Return the information about the file.
568 const Input_file_argument&
569 file() const
570 {
a3ad94ed 571 gold_assert(this->is_file_);
ead1e424
ILT
572 return this->file_;
573 }
574
575 // Return the information about the group.
576 const Input_file_group*
577 group() const
578 {
a3ad94ed 579 gold_assert(!this->is_file_);
ead1e424
ILT
580 return this->group_;
581 }
582
583 Input_file_group*
584 group()
585 {
a3ad94ed 586 gold_assert(!this->is_file_);
ead1e424
ILT
587 return this->group_;
588 }
589
590 private:
591 bool is_file_;
592 Input_file_argument file_;
593 Input_file_group* group_;
594};
595
596// A group from the command line. This is a set of arguments within
597// --start-group ... --end-group.
598
599class Input_file_group
92e059d8 600{
ead1e424
ILT
601 public:
602 typedef std::vector<Input_argument> Files;
603 typedef Files::const_iterator const_iterator;
604
605 Input_file_group()
606 : files_()
607 { }
608
609 // Add a file to the end of the group.
610 void
611 add_file(const Input_file_argument& arg)
612 { this->files_.push_back(Input_argument(arg)); }
613
614 // Iterators to iterate over the group contents.
615
616 const_iterator
617 begin() const
618 { return this->files_.begin(); }
619
620 const_iterator
621 end() const
622 { return this->files_.end(); }
623
624 private:
625 Files files_;
92e059d8
ILT
626};
627
dbe717ef
ILT
628// A list of files from the command line or a script.
629
630class Input_arguments
631{
632 public:
633 typedef std::vector<Input_argument> Input_argument_list;
634 typedef Input_argument_list::const_iterator const_iterator;
635
636 Input_arguments()
637 : input_argument_list_(), in_group_(false)
638 { }
639
640 // Add a file.
641 void
642 add_file(const Input_file_argument& arg);
643
644 // Start a group (the --start-group option).
645 void
646 start_group();
647
648 // End a group (the --end-group option).
649 void
650 end_group();
651
652 // Return whether we are currently in a group.
653 bool
654 in_group() const
655 { return this->in_group_; }
656
fe9a4c12
ILT
657 // The number of entries in the list.
658 int
659 size() const
660 { return this->input_argument_list_.size(); }
661
dbe717ef
ILT
662 // Iterators to iterate over the list of input files.
663
664 const_iterator
665 begin() const
666 { return this->input_argument_list_.begin(); }
667
668 const_iterator
669 end() const
670 { return this->input_argument_list_.end(); }
671
672 // Return whether the list is empty.
673 bool
674 empty() const
675 { return this->input_argument_list_.empty(); }
676
677 private:
678 Input_argument_list input_argument_list_;
679 bool in_group_;
680};
681
bae7f79e
ILT
682// All the information read from the command line.
683
684class Command_line
685{
686 public:
ead1e424
ILT
687 typedef Input_arguments::const_iterator const_iterator;
688
bae7f79e
ILT
689 Command_line();
690
691 // Process the command line options. This will exit with an
692 // appropriate error message if an unrecognized option is seen.
693 void
694 process(int argc, char** argv);
695
61ba1cf9
ILT
696 // Handle a -l option.
697 int
698 process_l_option(int, char**, char*);
699
ead1e424
ILT
700 // Handle a --start-group option.
701 void
702 start_group(const char* arg);
703
704 // Handle a --end-group option.
705 void
706 end_group(const char* arg);
707
61ba1cf9 708 // Get the general options.
bae7f79e
ILT
709 const General_options&
710 options() const
711 { return this->options_; }
712
fe9a4c12
ILT
713 // The number of input files.
714 int
715 number_of_input_files() const
716 { return this->inputs_.size(); }
717
ead1e424
ILT
718 // Iterators to iterate over the list of input files.
719
720 const_iterator
721 begin() const
722 { return this->inputs_.begin(); }
723
724 const_iterator
725 end() const
726 { return this->inputs_.end(); }
bae7f79e
ILT
727
728 private:
ead1e424
ILT
729 Command_line(const Command_line&);
730 Command_line& operator=(const Command_line&);
731
732 // Report usage error.
733 void
734 usage() ATTRIBUTE_NORETURN;
735 void
736 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
737 void
738 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
739
740 // Apply a command line option.
741 void
742 apply_option(const gold::options::One_option&, const char*);
743
744 // Add a file.
745 void
746 add_file(const char* name, bool is_lib);
bae7f79e 747
46738c9a
ILT
748 // Examine the result of processing the command-line, and verify
749 // the flags do not contradict each other or are otherwise illegal.
750 void
751 normalize_options();
752
bae7f79e
ILT
753 General_options options_;
754 Position_dependent_options position_options_;
ead1e424 755 Input_arguments inputs_;
bae7f79e
ILT
756};
757
758} // End namespace gold.
759
760#endif // !defined(GOLD_OPTIONS_H)
This page took 0.09123 seconds and 4 git commands to generate.