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