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