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