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