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