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