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