Recognize and ignore the --eh-frame-hdr option.
[external/binutils.git] / gold / options.h
1 // options.h -- handle command line options for gold  -*- C++ -*-
2
3 // Command_line
4 //   Holds everything we get from the command line.
5 // General_options (from Command_line::options())
6 //   Options which are not position dependent.
7 // Input_argument (from Command_line::inputs())
8 //   The list of input files, including -l options.
9 // Position_dependent_options (from Input_argument::options())
10 //   Position dependent options which apply to this argument.
11
12 #ifndef GOLD_OPTIONS_H
13 #define GOLD_OPTIONS_H
14
15 #include <list>
16 #include <string>
17 #include <vector>
18
19 namespace gold
20 {
21
22 class Command_line;
23 class Input_file_group;
24
25 namespace options {
26
27 class Command_line_options;
28 struct One_option;
29
30 } // End namespace gold::options.
31
32 // The position independent options which apply to the whole link.
33 // There are a lot of them.
34
35 class General_options
36 {
37  public:
38   General_options();
39
40   // -E: export dynamic symbols.
41   bool
42   export_dynamic() const
43   { return this->export_dynamic_; }
44
45   // -I: dynamic linker name.
46   const char*
47   dynamic_linker() const
48   { return this->dynamic_linker_; }
49
50   // -L: Library search path.
51   typedef std::vector<const char*> Dir_list;
52
53   const Dir_list&
54   search_path() const
55   { return this->search_path_; }
56
57   // -o: Output file name.
58   const char*
59   output_file_name() const
60   { return this->output_file_name_; }
61
62   // -r: Whether we are doing a relocatable link.
63   bool
64   is_relocatable() const
65   { return this->is_relocatable_; }
66
67   // --eh-frame-hdr: Whether to generate an exception frame header.
68   bool
69   create_eh_frame_hdr() const
70   { return this->create_eh_frame_hdr_; }
71
72   // --rpath: The runtime search path.
73   const Dir_list&
74   rpath() const
75   { return this->rpath_; }
76
77   // --shared: Whether generating a shared object.
78   bool
79   is_shared() const
80   { return this->is_shared_; }
81
82   // --static: Whether doing a static link.
83   bool
84   is_static() const
85   { return this->is_static_; }
86
87  private:
88   // Don't copy this structure.
89   General_options(const General_options&);
90   General_options& operator=(const General_options&);
91
92   friend class Command_line;
93   friend class options::Command_line_options;
94
95   void
96   set_export_dynamic()
97   { this->export_dynamic_ = true; }
98
99   void
100   set_dynamic_linker(const char* arg)
101   { this->dynamic_linker_ = arg; }
102
103   void
104   add_to_search_path(const char* arg)
105   { this->search_path_.push_back(arg); }
106
107   void
108   set_output_file_name(const char* arg)
109   { this->output_file_name_ = arg; }
110
111   void
112   set_relocatable()
113   { this->is_relocatable_ = true; }
114
115   void
116   create_eh_frame_hdr()
117   { this->create_eh_frame_hdr_ = true; }
118
119   void
120   add_to_rpath(const char* arg)
121   { this->rpath_.push_back(arg); }
122
123   void
124   set_shared()
125   { this->is_shared_ = true; }
126
127   void
128   set_static()
129   { this->is_static_ = true; }
130
131   void
132   ignore(const char*)
133   { }
134
135   bool export_dynamic_;
136   const char* dynamic_linker_;
137   Dir_list search_path_;
138   const char* output_file_name_;
139   bool is_relocatable_;
140   bool create_eh_frame_hdr_;
141   Dir_list rpath_;
142   bool is_shared_;
143   bool is_static_;
144 };
145
146 // The current state of the position dependent options.
147
148 class Position_dependent_options
149 {
150  public:
151   Position_dependent_options();
152
153   // -Bstatic: Whether we are searching for a static archive rather
154   // than a shared object.
155   bool
156   do_static_search() const
157   { return this->do_static_search_; }
158
159   // --as-needed: Whether to add a DT_NEEDED argument only if the
160   // dynamic object is used.
161   bool
162   as_needed() const
163   { return this->as_needed_; }
164
165   // --whole-archive: Whether to include the entire contents of an
166   // --archive.
167   bool
168   include_whole_archive() const
169   { return this->include_whole_archive_; }
170
171   void
172   set_static_search()
173   { this->do_static_search_ = true; }
174
175   void
176   set_dynamic_search()
177   { this->do_static_search_ = false; }
178
179   void
180   set_as_needed()
181   { this->as_needed_ = true; }
182
183   void
184   clear_as_needed()
185   { this->as_needed_ = false; }
186
187   void
188   set_whole_archive()
189   { this->include_whole_archive_ = true; }
190
191   void
192   clear_whole_archive()
193   { this->include_whole_archive_ = false; }
194
195  private:
196   bool do_static_search_;
197   bool as_needed_;
198   bool include_whole_archive_;
199 };
200
201 // A single file or library argument from the command line.
202
203 class Input_file_argument
204 {
205  public:
206   Input_file_argument()
207     : name_(), is_lib_(false), options_()
208   { }
209
210   Input_file_argument(const char* name, bool is_lib,
211                       const Position_dependent_options& options)
212     : name_(name), is_lib_(is_lib), options_(options)
213   { }
214
215   const char*
216   name() const
217   { return this->name_.c_str(); }
218
219   const Position_dependent_options&
220   options() const
221   { return this->options_; }
222
223   bool
224   is_lib() const
225   { return this->is_lib_; }
226
227  private:
228   // We use std::string, not const char*, here for convenience when
229   // using script files, so that we do not have to preserve the string
230   // in that case.
231   std::string name_;
232   bool is_lib_;
233   Position_dependent_options options_;
234 };
235
236 // A file or library, or a group, from the command line.
237
238 class Input_argument
239 {
240  public:
241   // Create a file or library argument.
242   explicit Input_argument(Input_file_argument file)
243     : is_file_(true), file_(file), group_(NULL)
244   { }
245
246   // Create a group argument.
247   explicit Input_argument(Input_file_group* group)
248     : is_file_(false), group_(group)
249   { }
250
251   // Return whether this is a file.
252   bool
253   is_file() const
254   { return this->is_file_; }
255
256   // Return whether this is a group.
257   bool
258   is_group() const
259   { return !this->is_file_; }
260
261   // Return the information about the file.
262   const Input_file_argument&
263   file() const
264   {
265     gold_assert(this->is_file_);
266     return this->file_;
267   }
268
269   // Return the information about the group.
270   const Input_file_group*
271   group() const
272   {
273     gold_assert(!this->is_file_);
274     return this->group_;
275   }
276
277   Input_file_group*
278   group()
279   {
280     gold_assert(!this->is_file_);
281     return this->group_;
282   }
283
284  private:
285   bool is_file_;
286   Input_file_argument file_;
287   Input_file_group* group_;
288 };
289
290 // A group from the command line.  This is a set of arguments within
291 // --start-group ... --end-group.
292
293 class Input_file_group
294 {
295  public:
296   typedef std::vector<Input_argument> Files;
297   typedef Files::const_iterator const_iterator;
298
299   Input_file_group()
300     : files_()
301   { }
302
303   // Add a file to the end of the group.
304   void
305   add_file(const Input_file_argument& arg)
306   { this->files_.push_back(Input_argument(arg)); }
307
308   // Iterators to iterate over the group contents.
309
310   const_iterator
311   begin() const
312   { return this->files_.begin(); }
313
314   const_iterator
315   end() const
316   { return this->files_.end(); }
317
318  private:
319   Files files_;
320 };
321
322 // A list of files from the command line or a script.
323
324 class Input_arguments
325 {
326  public:
327   typedef std::vector<Input_argument> Input_argument_list;
328   typedef Input_argument_list::const_iterator const_iterator;
329
330   Input_arguments()
331     : input_argument_list_(), in_group_(false)
332   { }
333
334   // Add a file.
335   void
336   add_file(const Input_file_argument& arg);
337
338   // Start a group (the --start-group option).
339   void
340   start_group();
341
342   // End a group (the --end-group option).
343   void
344   end_group();
345
346   // Return whether we are currently in a group.
347   bool
348   in_group() const
349   { return this->in_group_; }
350
351   // Iterators to iterate over the list of input files.
352
353   const_iterator
354   begin() const
355   { return this->input_argument_list_.begin(); }
356
357   const_iterator
358   end() const
359   { return this->input_argument_list_.end(); }
360
361   // Return whether the list is empty.
362   bool
363   empty() const
364   { return this->input_argument_list_.empty(); }
365
366  private:
367   Input_argument_list input_argument_list_;
368   bool in_group_;
369 };
370
371 // All the information read from the command line.
372
373 class Command_line
374 {
375  public:
376   typedef Input_arguments::const_iterator const_iterator;
377
378   Command_line();
379
380   // Process the command line options.  This will exit with an
381   // appropriate error message if an unrecognized option is seen.
382   void
383   process(int argc, char** argv);
384
385   // Handle a -l option.
386   int
387   process_l_option(int, char**, char*);
388
389   // Handle a --start-group option.
390   void
391   start_group(const char* arg);
392
393   // Handle a --end-group option.
394   void
395   end_group(const char* arg);
396
397   // Get the general options.
398   const General_options&
399   options() const
400   { return this->options_; }
401
402   // Iterators to iterate over the list of input files.
403
404   const_iterator
405   begin() const
406   { return this->inputs_.begin(); }
407
408   const_iterator
409   end() const
410   { return this->inputs_.end(); }
411
412  private:
413   Command_line(const Command_line&);
414   Command_line& operator=(const Command_line&);
415
416   // Report usage error.
417   void
418   usage() ATTRIBUTE_NORETURN;
419   void
420   usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
421   void
422   usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
423
424   // Apply a command line option.
425   void
426   apply_option(const gold::options::One_option&, const char*);
427
428   // Add a file.
429   void
430   add_file(const char* name, bool is_lib);
431
432   General_options options_;
433   Position_dependent_options position_options_;
434   Input_arguments inputs_;
435 };
436
437 } // End namespace gold.
438
439 #endif // !defined(GOLD_OPTIONS_H)