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