Implement -s and -S options which strip symbols.
[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
51 } // End namespace gold::options.
52
53 // A directory to search.  For each directory we record whether it is
54 // in the sysroot.  We need to know this so that, if a linker script
55 // is found within the sysroot, we will apply the sysroot to any files
56 // named by that script.
57
58 class Search_directory
59 {
60  public:
61   // We need a default constructor because we put this in a
62   // std::vector.
63   Search_directory()
64     : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
65   { }
66
67   // This is the usual constructor.
68   Search_directory(const char* name, bool put_in_sysroot)
69     : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
70   { gold_assert(!this->name_.empty()); }
71
72   // This is called if we have a sysroot.  The sysroot is prefixed to
73   // any entries for which put_in_sysroot_ is true.  is_in_sysroot_ is
74   // set to true for any enries which are in the sysroot (this will
75   // naturally include any entries for which put_in_sysroot_ is true).
76   // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
77   // passing SYSROOT to lrealpath.
78   void
79   add_sysroot(const char* sysroot, const char* canonical_sysroot);
80
81   // Get the directory name.
82   const std::string&
83   name() const
84   { return this->name_; }
85
86   // Return whether this directory is in the sysroot.
87   bool
88   is_in_sysroot() const
89   { return this->is_in_sysroot_; }
90
91  private:
92   std::string name_;
93   bool put_in_sysroot_;
94   bool is_in_sysroot_;
95 };
96
97 // The position independent options which apply to the whole link.
98 // There are a lot of them.
99
100 class General_options
101 {
102  public:
103   General_options();
104
105   // -E: export dynamic symbols.
106   bool
107   export_dynamic() const
108   { return this->export_dynamic_; }
109
110   // -I: dynamic linker name.
111   const char*
112   dynamic_linker() const
113   { return this->dynamic_linker_; }
114
115   // -L: Library search path.
116   typedef std::vector<Search_directory> Dir_list;
117
118   const Dir_list&
119   search_path() const
120   { return this->search_path_; }
121
122   // -O: optimization level (0: don't try to optimize output size).
123   int
124   optimization_level() const
125   { return this->optimization_level_; }
126
127   // -o: Output file name.
128   const char*
129   output_file_name() const
130   { return this->output_file_name_; }
131
132   // -r: Whether we are doing a relocatable link.
133   bool
134   is_relocatable() const
135   { return this->is_relocatable_; }
136
137   // -s: Strip all symbols.
138   bool
139   strip_all() const
140   { return this->strip_ == STRIP_ALL; }
141
142   // -S: Strip debugging information.
143   bool
144   strip_debug() const
145   { return this->strip_ == STRIP_ALL || this->strip_ == STRIP_DEBUG; }
146
147   // --eh-frame-hdr: Whether to generate an exception frame header.
148   bool
149   create_eh_frame_hdr() const
150   { return this->create_eh_frame_hdr_; }
151
152   // --rpath: The runtime search path.
153   const Dir_list&
154   rpath() const
155   { return this->rpath_; }
156
157   // --rpath-link: The link time search patch for shared libraries.
158   const Dir_list&
159   rpath_link() const
160   { return this->rpath_link_; }
161
162   // --shared: Whether generating a shared object.
163   bool
164   is_shared() const
165   { return this->is_shared_; }
166
167   // --static: Whether doing a static link.
168   bool
169   is_static() const
170   { return this->is_static_; }
171
172   // --sysroot: The system root of a cross-linker.
173   const std::string&
174   sysroot() const
175   { return this->sysroot_; }
176
177  private:
178   // Don't copy this structure.
179   General_options(const General_options&);
180   General_options& operator=(const General_options&);
181
182   friend class Command_line;
183   friend class options::Command_line_options;
184
185   // Which symbols to strip.
186   enum Strip
187   {
188     // Don't strip any symbols.
189     STRIP_NONE,
190     // Strip all symbols.
191     STRIP_ALL,
192     // Strip debugging information.
193     STRIP_DEBUG
194   };
195
196   void
197   set_export_dynamic()
198   { this->export_dynamic_ = true; }
199
200   void
201   set_dynamic_linker(const char* arg)
202   { this->dynamic_linker_ = arg; }
203
204   void
205   add_to_search_path(const char* arg)
206   { this->search_path_.push_back(Search_directory(arg, false)); }
207
208   void
209   add_to_search_path_with_sysroot(const char* arg)
210   { this->search_path_.push_back(Search_directory(arg, true)); }
211
212   void
213   set_optimization_level(const char* arg)
214   { this->optimization_level_ = atoi(arg); }
215
216   void
217   set_output_file_name(const char* arg)
218   { this->output_file_name_ = arg; }
219
220   void
221   set_relocatable()
222   { this->is_relocatable_ = true; }
223
224   void
225   set_strip_all()
226   { this->strip_ = STRIP_ALL; }
227
228   void
229   set_strip_debug()
230   { this->strip_ = STRIP_DEBUG; }
231
232   void
233   set_create_eh_frame_hdr()
234   { this->create_eh_frame_hdr_ = true; }
235
236   void
237   add_to_rpath(const char* arg)
238   { this->rpath_.push_back(Search_directory(arg, false)); }
239
240   void
241   add_to_rpath_link(const char* arg)
242   { this->rpath_link_.push_back(Search_directory(arg, false)); }
243
244   void
245   set_shared()
246   { this->is_shared_ = true; }
247
248   void
249   set_static()
250   { this->is_static_ = true; }
251
252   void
253   set_sysroot(const char* arg)
254   { this->sysroot_ = arg; }
255
256   void
257   ignore(const char*)
258   { }
259
260   // Apply any sysroot to the directory lists.
261   void
262   add_sysroot();
263
264   bool export_dynamic_;
265   const char* dynamic_linker_;
266   Dir_list search_path_;
267   int optimization_level_;
268   const char* output_file_name_;
269   bool is_relocatable_;
270   Strip strip_;
271   bool create_eh_frame_hdr_;
272   Dir_list rpath_;
273   Dir_list rpath_link_;
274   bool is_shared_;
275   bool is_static_;
276   std::string sysroot_;
277 };
278
279 // The current state of the position dependent options.
280
281 class Position_dependent_options
282 {
283  public:
284   Position_dependent_options();
285
286   // -Bstatic: Whether we are searching for a static archive rather
287   // than a shared object.
288   bool
289   do_static_search() const
290   { return this->do_static_search_; }
291
292   // --as-needed: Whether to add a DT_NEEDED argument only if the
293   // dynamic object is used.
294   bool
295   as_needed() const
296   { return this->as_needed_; }
297
298   // --whole-archive: Whether to include the entire contents of an
299   // --archive.
300   bool
301   include_whole_archive() const
302   { return this->include_whole_archive_; }
303
304   void
305   set_static_search()
306   { this->do_static_search_ = true; }
307
308   void
309   set_dynamic_search()
310   { this->do_static_search_ = false; }
311
312   void
313   set_as_needed()
314   { this->as_needed_ = true; }
315
316   void
317   clear_as_needed()
318   { this->as_needed_ = false; }
319
320   void
321   set_whole_archive()
322   { this->include_whole_archive_ = true; }
323
324   void
325   clear_whole_archive()
326   { this->include_whole_archive_ = false; }
327
328  private:
329   bool do_static_search_;
330   bool as_needed_;
331   bool include_whole_archive_;
332 };
333
334 // A single file or library argument from the command line.
335
336 class Input_file_argument
337 {
338  public:
339   // name: file name or library name
340   // is_lib: true if name is a library name: that is, emits the leading
341   //         "lib" and trailing ".so"/".a" from the name
342   // extra_search_path: an extra directory to look for the file, prior
343   //         to checking the normal library search path.  If this is "",
344   //         then no extra directory is added.
345   // options: The position dependent options at this point in the
346   //         command line, such as --whole-archive.
347   Input_file_argument()
348     : name_(), is_lib_(false), extra_search_path_(""), options_()
349   { }
350
351   Input_file_argument(const char* name, bool is_lib,
352                       const char* extra_search_path,
353                       const Position_dependent_options& options)
354     : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
355       options_(options)
356   { }
357
358   const char*
359   name() const
360   { return this->name_.c_str(); }
361
362   const Position_dependent_options&
363   options() const
364   { return this->options_; }
365
366   bool
367   is_lib() const
368   { return this->is_lib_; }
369
370   const char*
371   extra_search_path() const
372   {
373     return (this->extra_search_path_.empty()
374             ? NULL
375             : this->extra_search_path_.c_str());
376   }
377
378   // Return whether this file may require a search using the -L
379   // options.
380   bool
381   may_need_search() const
382   { return this->is_lib_ || !this->extra_search_path_.empty(); }
383
384  private:
385   // We use std::string, not const char*, here for convenience when
386   // using script files, so that we do not have to preserve the string
387   // in that case.
388   std::string name_;
389   bool is_lib_;
390   std::string extra_search_path_;
391   Position_dependent_options options_;
392 };
393
394 // A file or library, or a group, from the command line.
395
396 class Input_argument
397 {
398  public:
399   // Create a file or library argument.
400   explicit Input_argument(Input_file_argument file)
401     : is_file_(true), file_(file), group_(NULL)
402   { }
403
404   // Create a group argument.
405   explicit Input_argument(Input_file_group* group)
406     : is_file_(false), group_(group)
407   { }
408
409   // Return whether this is a file.
410   bool
411   is_file() const
412   { return this->is_file_; }
413
414   // Return whether this is a group.
415   bool
416   is_group() const
417   { return !this->is_file_; }
418
419   // Return the information about the file.
420   const Input_file_argument&
421   file() const
422   {
423     gold_assert(this->is_file_);
424     return this->file_;
425   }
426
427   // Return the information about the group.
428   const Input_file_group*
429   group() const
430   {
431     gold_assert(!this->is_file_);
432     return this->group_;
433   }
434
435   Input_file_group*
436   group()
437   {
438     gold_assert(!this->is_file_);
439     return this->group_;
440   }
441
442  private:
443   bool is_file_;
444   Input_file_argument file_;
445   Input_file_group* group_;
446 };
447
448 // A group from the command line.  This is a set of arguments within
449 // --start-group ... --end-group.
450
451 class Input_file_group
452 {
453  public:
454   typedef std::vector<Input_argument> Files;
455   typedef Files::const_iterator const_iterator;
456
457   Input_file_group()
458     : files_()
459   { }
460
461   // Add a file to the end of the group.
462   void
463   add_file(const Input_file_argument& arg)
464   { this->files_.push_back(Input_argument(arg)); }
465
466   // Iterators to iterate over the group contents.
467
468   const_iterator
469   begin() const
470   { return this->files_.begin(); }
471
472   const_iterator
473   end() const
474   { return this->files_.end(); }
475
476  private:
477   Files files_;
478 };
479
480 // A list of files from the command line or a script.
481
482 class Input_arguments
483 {
484  public:
485   typedef std::vector<Input_argument> Input_argument_list;
486   typedef Input_argument_list::const_iterator const_iterator;
487
488   Input_arguments()
489     : input_argument_list_(), in_group_(false)
490   { }
491
492   // Add a file.
493   void
494   add_file(const Input_file_argument& arg);
495
496   // Start a group (the --start-group option).
497   void
498   start_group();
499
500   // End a group (the --end-group option).
501   void
502   end_group();
503
504   // Return whether we are currently in a group.
505   bool
506   in_group() const
507   { return this->in_group_; }
508
509   // Iterators to iterate over the list of input files.
510
511   const_iterator
512   begin() const
513   { return this->input_argument_list_.begin(); }
514
515   const_iterator
516   end() const
517   { return this->input_argument_list_.end(); }
518
519   // Return whether the list is empty.
520   bool
521   empty() const
522   { return this->input_argument_list_.empty(); }
523
524  private:
525   Input_argument_list input_argument_list_;
526   bool in_group_;
527 };
528
529 // All the information read from the command line.
530
531 class Command_line
532 {
533  public:
534   typedef Input_arguments::const_iterator const_iterator;
535
536   Command_line();
537
538   // Process the command line options.  This will exit with an
539   // appropriate error message if an unrecognized option is seen.
540   void
541   process(int argc, char** argv);
542
543   // Handle a -l option.
544   int
545   process_l_option(int, char**, char*);
546
547   // Handle a --start-group option.
548   void
549   start_group(const char* arg);
550
551   // Handle a --end-group option.
552   void
553   end_group(const char* arg);
554
555   // Get the general options.
556   const General_options&
557   options() const
558   { return this->options_; }
559
560   // Iterators to iterate over the list of input files.
561
562   const_iterator
563   begin() const
564   { return this->inputs_.begin(); }
565
566   const_iterator
567   end() const
568   { return this->inputs_.end(); }
569
570  private:
571   Command_line(const Command_line&);
572   Command_line& operator=(const Command_line&);
573
574   // Report usage error.
575   void
576   usage() ATTRIBUTE_NORETURN;
577   void
578   usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
579   void
580   usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
581
582   // Apply a command line option.
583   void
584   apply_option(const gold::options::One_option&, const char*);
585
586   // Add a file.
587   void
588   add_file(const char* name, bool is_lib);
589
590   General_options options_;
591   Position_dependent_options position_options_;
592   Input_arguments inputs_;
593 };
594
595 } // End namespace gold.
596
597 #endif // !defined(GOLD_OPTIONS_H)