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