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