OSDN Git Service

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