Can now do a full static link of hello, world in C or C++
[platform/upstream/binutils.git] / gold / options.cc
1 // options.c -- handle command line options for gold
2
3 #include <iostream>
4
5 #include "gold.h"
6 #include "options.h"
7
8 namespace gold
9 {
10
11 // The information we keep for a single command line option.
12
13 struct options::One_option
14 {
15   // The single character option name, or '\0' if this is only a long
16   // option.
17   char short_option;
18
19   // The long option name, or NULL if this is only a short option.
20   const char* long_option;
21
22   // Description of the option for --help output, or NULL if there is none.
23   const char* doc;
24
25   // How to print the option name in --help output, or NULL to use the
26   // default.
27   const char* help_output;
28
29   // Long option dash control.  This is ignored if long_option is
30   // NULL.
31   enum
32     {
33       // Long option normally takes one dash; two dashes are also
34       // accepted.
35       ONE_DASH,
36       // Long option normally takes two dashes; one dash is also
37       // accepted.
38       TWO_DASHES,
39       // Long option always takes two dashes.
40       EXACTLY_TWO_DASHES
41     } dash;
42
43   // Function for special handling, or NULL.  Returns the number of
44   // arguments to skip.  This will normally be at least 1, but it may
45   // be 0 if this function changes *argv.  ARG points to the location
46   // in *ARGV where the option starts, which may be helpful for a
47   // short option.
48   int (*special)(int argc, char** argv, char *arg, Command_line*);
49
50   // If this is a position independent option which does not take an
51   // argument, this is the member function to call to record it.
52   void (General_options::*general_noarg)();
53
54   // If this is a position independent function which takes an
55   // argument, this is the member function to call to record it.
56   void (General_options::*general_arg)(const char*);
57
58   // If this is a position dependent option which does not take an
59   // argument, this is the member function to call to record it.
60   void (Position_dependent_options::*dependent_noarg)();
61
62   // If this is a position dependent option which takes an argument,
63   // this is the member function to record it.
64   void (Position_dependent_options::*dependent_arg)(const char*);
65
66   // Return whether this option takes an argument.
67   bool
68   takes_argument() const
69   { return this->general_arg != NULL || this->dependent_arg != NULL; }
70 };
71
72 class options::Command_line_options
73 {
74  public:
75   static const One_option options[];
76   static const int options_size;
77 };
78
79 } // End namespace gold.
80
81 namespace
82 {
83
84 // Handle the special -l option, which adds an input file.
85
86 int
87 library(int argc, char** argv, char* arg, gold::Command_line* cmdline)
88 {
89   return cmdline->process_l_option(argc, argv, arg);
90 }
91
92 // Handle the special --start-group option.
93
94 int
95 start_group(int, char**, char* arg, gold::Command_line* cmdline)
96 {
97   cmdline->start_group(arg);
98   return 1;
99 }
100
101 // Handle the special --end-group option.
102
103 int
104 end_group(int, char**, char* arg, gold::Command_line* cmdline)
105 {
106   cmdline->end_group(arg);
107   return 1;
108 }
109
110 // Report usage information for ld --help, and exit.
111
112 int
113 help(int, char**, char*, gold::Command_line*)
114 {
115   printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
116
117   const int options_size = gold::options::Command_line_options::options_size;
118   const gold::options::One_option* options =
119     gold::options::Command_line_options::options;
120   for (int i = 0; i < options_size; ++i)
121     {
122       if (options[i].doc == NULL)
123         continue;
124
125       printf("  ");
126       int len = 2;
127       bool comma = false;
128
129       int j = i;
130       do
131         {
132           if (options[j].help_output != NULL)
133             {
134               if (comma)
135                 {
136                   printf(", ");
137                   len += 2;
138                 }
139               printf(options[j].help_output);
140               len += std::strlen(options[i].help_output);
141             }
142           else
143             {
144               if (options[j].short_option != '\0')
145                 {
146                   if (comma)
147                     {
148                       printf(", ");
149                       len += 2;
150                     }
151                   printf("-%c", options[j].short_option);
152                   len += 2;
153                 }
154
155               if (options[j].long_option != NULL)
156                 {
157                   if (comma)
158                     {
159                       printf(", ");
160                       len += 2;
161                     }
162                   if (options[j].dash == gold::options::One_option::ONE_DASH)
163                     {
164                       printf("-");
165                       ++len;
166                     }
167                   else
168                     {
169                       printf("--");
170                       len += 2;
171                     }
172                   printf("%s", options[j].long_option);
173                   len += std::strlen(options[j].long_option);
174                 }
175             }
176           ++j;
177         }
178       while (j < options_size && options[j].doc == NULL);
179
180       if (len > 30)
181         {
182           printf("\n");
183           len = 0;
184         }
185       for (; len < 30; ++len)
186         std::putchar(' ');
187
188       std::puts(options[i].doc);
189     }
190
191   gold::gold_exit(true);
192
193   return 0;
194 }
195
196 } // End anonymous namespace.
197
198 namespace gold
199 {
200
201 // Helper macros used to specify the options.  We could also do this
202 // using constructors, but then g++ would generate code to initialize
203 // the array.  We want the array to be initialized statically so that
204 // we get better startup time.
205
206 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
207   { short_option, long_option, doc, help, options::One_option::dash, \
208       NULL, func, NULL, NULL, NULL }
209 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func)   \
210   { short_option, long_option, doc, help, options::One_option::dash, \
211       NULL, NULL, func, NULL, NULL }
212 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func)  \
213   { short_option, long_option, doc, help, options::One_option::dash, \
214       NULL,  NULL, NULL, func, NULL }
215 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func)    \
216   { short_option, long_option, doc, help, options::One_option::dash, \
217       NULL, NULL, NULL, NULL, func }
218 #define SPECIAL(short_option, long_option, doc, help, dash, func)       \
219   { short_option, long_option, doc, help, options::One_option::dash, \
220       func, NULL, NULL, NULL, NULL }
221
222 // Here is the actual list of options which we accept.
223
224 const options::One_option
225 options::Command_line_options::options[] =
226 {
227   SPECIAL('l', "library", N_("Search for library LIBNAME"),
228           N_("-lLIBNAME --library LIBNAME"), TWO_DASHES,
229           &library),
230   SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
231           TWO_DASHES, &start_group),
232   SPECIAL(')', "end-group", N_("End a library search group"), NULL,
233           TWO_DASHES, &end_group),
234   GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
235               N_("-L DIR, --library-path DIR"), TWO_DASHES,
236               &General_options::add_to_search_path),
237   GENERAL_ARG('o', "output", N_("Set output file name"),
238               N_("-o FILE, --output FILE"), TWO_DASHES,
239               &General_options::set_output_file_name),
240   GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
241                 ONE_DASH, &General_options::set_relocatable),
242   GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
243                 NULL, ONE_DASH, &General_options::set_shared),
244   GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
245                 NULL, ONE_DASH, &General_options::set_static),
246   SPECIAL('\0', "help", N_("Report usage information"), NULL,
247           TWO_DASHES, &help)
248 };
249
250 const int options::Command_line_options::options_size =
251   sizeof (options) / sizeof (options[0]);
252
253 // The default values for the general options.
254
255 General_options::General_options()
256   : search_path_(),
257     output_file_name_("a.out"),
258     is_relocatable_(false),
259     is_shared_(false),
260     is_static_(false)
261 {
262 }
263
264 // The default values for the position dependent options.
265
266 Position_dependent_options::Position_dependent_options()
267   : do_static_search_(false)
268 {
269 }
270
271 // Command_line options.
272
273 Command_line::Command_line()
274   : options_(), position_options_(), inputs_(), in_group_(false)
275 {
276 }
277
278 // Process the command line options.
279
280 void
281 Command_line::process(int argc, char** argv)
282 {
283   const int options_size = options::Command_line_options::options_size;
284   const options::One_option* options =
285     options::Command_line_options::options;
286   bool no_more_options = false;
287   int i = 0;
288   while (i < argc)
289     {
290       if (argv[i][0] != '-' || no_more_options)
291         {
292           this->add_file(argv[i], false);
293           ++i;
294           continue;
295         }
296
297       // Option starting with '-'.
298       int dashes = 1;
299       if (argv[i][1] == '-')
300         {
301           dashes = 2;
302           if (argv[i][2] == '\0')
303             {
304               no_more_options = true;
305               continue;
306             }
307         }
308
309       // Look for a long option match.
310       char* opt = argv[i] + dashes;
311       char first = opt[0];
312       int skiparg = 0;
313       char* arg = strchr(opt, '=');
314       if (arg != NULL)
315         *arg = '\0';
316       else if (i + 1 < argc)
317         {
318           arg = argv[i + 1];
319           skiparg = 1;
320         }
321
322       int j;
323       for (j = 0; j < options_size; ++j)
324         {
325           if (options[j].long_option != NULL
326               && (dashes == 2
327                   || (options[j].dash
328                       != options::One_option::EXACTLY_TWO_DASHES))
329               && first == options[j].long_option[0]
330               && strcmp(opt, options[j].long_option) == 0)
331             {
332               if (options[j].special)
333                 i += options[j].special(argc - 1, argv + i, opt, this);
334               else
335                 {
336                   if (!options[j].takes_argument())
337                     {
338                       arg = NULL;
339                       skiparg = 0;
340                     }
341                   else
342                     {
343                       if (arg == NULL)
344                         this->usage(_("missing argument"), argv[i]);
345                     }
346                   this->apply_option(options[j], arg);
347                   i += skiparg + 1;
348                 }
349               break;
350             }
351         }
352       if (j < options_size)
353         continue;
354
355       // If we saw two dashes, we need to see a long option.
356       if (dashes == 2)
357         this->usage(_("unknown option"), argv[i]);
358
359       // Look for a short option match.  There may be more than one
360       // short option in a given argument.
361       bool done = false;
362       char* s = argv[i] + 1;
363       ++i;
364       while (*s != '\0' && !done)
365         {
366           char opt = *s;
367           int j;
368           for (j = 0; j < options_size; ++j)
369             {
370               if (options[j].short_option == opt)
371                 {
372                   if (options[j].special)
373                     {
374                       // Undo the argument skip done above.
375                       --i;
376                       i += options[j].special(argc - i, argv + i, s, this);
377                       done = true;
378                     }
379                   else
380                     {
381                       arg = NULL;
382                       if (options[j].takes_argument())
383                         {
384                           if (s[1] != '\0')
385                             {
386                               arg = s + 1;
387                               done = true;
388                             }
389                           else if (i < argc)
390                             {
391                               arg = argv[i];
392                               ++i;
393                             }
394                           else
395                             this->usage(_("missing argument"), opt);
396                         }
397                       this->apply_option(options[j], arg);
398                     }
399                   break;
400                 }
401             }
402
403           if (j >= options_size)
404             this->usage(_("unknown option"), *s);
405
406           ++s;
407         }
408     }
409
410   if (this->in_group_)
411     {
412       fprintf(stderr, _("%s: missing group end"), program_name);
413       this->usage();
414     }
415
416   // FIXME: We should only do this when configured in native mode.
417   this->options_.add_to_search_path("/lib");
418   this->options_.add_to_search_path("/usr/lib");
419 }
420
421 // Apply a command line option.
422
423 void
424 Command_line::apply_option(const options::One_option& opt,
425                            const char* arg)
426 {
427   if (arg == NULL)
428     {
429       if (opt.general_noarg)
430         (this->options_.*(opt.general_noarg))();
431       else if (opt.dependent_noarg)
432         (this->position_options_.*(opt.dependent_noarg))();
433       else
434         gold_unreachable();
435     }
436   else
437     {
438       if (opt.general_arg)
439         (this->options_.*(opt.general_arg))(arg);
440       else if (opt.dependent_arg)
441         (this->position_options_.*(opt.dependent_arg))(arg);
442       else
443         gold_unreachable();
444     }
445 }
446
447 // Add an input file or library.
448
449 void
450 Command_line::add_file(const char* name, bool is_lib)
451 {
452   Input_file_argument file(name, is_lib, this->position_options_);
453   if (!this->in_group_)
454     this->inputs_.push_back(Input_argument(file));
455   else
456     {
457       assert(!this->inputs_.empty());
458       assert(this->inputs_.back().is_group());
459       this->inputs_.back().group()->add_file(file);
460     }
461 }
462
463 // Handle the -l option, which requires special treatment.
464
465 int
466 Command_line::process_l_option(int argc, char** argv, char* arg)
467 {
468   int ret;
469   const char* libname;
470   if (arg[1] != '\0')
471     {
472       ret = 1;
473       libname = arg + 1;
474     }
475   else if (argc > 1)
476     {
477       ret = 2;
478       libname = argv[argc + 1];
479     }
480   else
481     this->usage(_("missing argument"), arg);
482
483   this->add_file(libname, true);
484
485   return ret;
486 }
487
488 // Handle the --start-group option.
489
490 void
491 Command_line::start_group(const char* arg)
492 {
493   if (this->in_group_)
494     this->usage(_("may not nest groups"), arg);
495
496   // This object is leaked.
497   Input_file_group* group = new Input_file_group();
498   this->inputs_.push_back(Input_argument(group));
499
500   this->in_group_ = true;
501 }
502
503 // Handle the --end-group option.
504
505 void
506 Command_line::end_group(const char* arg)
507 {
508   if (!this->in_group_)
509     this->usage(_("group end without group start"), arg);
510   this->in_group_ = false;
511 }
512
513 // Report a usage error.  */
514
515 void
516 Command_line::usage()
517 {
518   fprintf(stderr,
519           _("%s: use the --help option for usage information\n"),
520           program_name);
521   gold_exit(false);
522 }
523
524 void
525 Command_line::usage(const char* msg, const char *opt)
526 {
527   fprintf(stderr,
528           _("%s: %s: %s\n"),
529           program_name, opt, msg);
530   this->usage();
531 }
532
533 void
534 Command_line::usage(const char* msg, char opt)
535 {
536   fprintf(stderr,
537           _("%s: -%c: %s\n"),
538           program_name, opt, msg);
539   this->usage();
540 }
541
542 } // End namespace gold.