2c0579e07f79dabdc00142cef9028ef6d134bb81
1 // options.c -- handle command line options for gold
11 // The information we keep for a single command line option.
13 struct options::One_option
15 // The single character option name, or '\0' if this is only a long
19 // The long option name, or NULL if this is only a short option.
20 const char* long_option
;
22 // Description of the option for --help output, or NULL if there is none.
25 // How to print the option name in --help output, or NULL to use the
27 const char* help_output
;
29 // Long option dash control. This is ignored if long_option is
33 // Long option normally takes one dash; two dashes are also
36 // Long option normally takes two dashes; one dash is also
39 // Long option always takes two dashes.
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
48 int (*special
)(int argc
, char** argv
, char *arg
, Command_line
*);
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
)();
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*);
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
)();
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*);
66 // Return whether this option takes an argument.
68 takes_argument() const
69 { return this->general_arg
!= NULL
|| this->dependent_arg
!= NULL
; }
72 class options::Command_line_options
75 static const One_option options
[];
76 static const int options_size
;
79 } // End namespace gold.
84 // Handle the special -l option, which adds an input file.
87 library(int argc
, char** argv
, char* arg
, gold::Command_line
* cmdline
)
89 return cmdline
->process_l_option(argc
, argv
, arg
);
92 // Handle the special --start-group option.
95 start_group(int, char**, char* arg
, gold::Command_line
* cmdline
)
97 cmdline
->start_group(arg
);
101 // Handle the special --end-group option.
104 end_group(int, char**, char* arg
, gold::Command_line
* cmdline
)
106 cmdline
->end_group(arg
);
110 // Report usage information for ld --help, and exit.
113 help(int, char**, char*, gold::Command_line
*)
115 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name
);
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
)
122 if (options
[i
].doc
== NULL
)
132 if (options
[j
].help_output
!= NULL
)
139 printf(options
[j
].help_output
);
140 len
+= std::strlen(options
[i
].help_output
);
145 if (options
[j
].short_option
!= '\0')
152 printf("-%c", options
[j
].short_option
);
157 if (options
[j
].long_option
!= NULL
)
164 if (options
[j
].dash
== gold::options::One_option::ONE_DASH
)
174 printf("%s", options
[j
].long_option
);
175 len
+= std::strlen(options
[j
].long_option
);
181 while (j
< options_size
&& options
[j
].doc
== NULL
);
188 for (; len
< 30; ++len
)
191 std::puts(options
[i
].doc
);
194 gold::gold_exit(true);
199 } // End anonymous namespace.
204 // Helper macros used to specify the options. We could also do this
205 // using constructors, but then g++ would generate code to initialize
206 // the array. We want the array to be initialized statically so that
207 // we get better startup time.
209 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
210 { short_option, long_option, doc, help, options::One_option::dash, \
211 NULL, func, NULL, NULL, NULL }
212 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
213 { short_option, long_option, doc, help, options::One_option::dash, \
214 NULL, NULL, func, NULL, NULL }
215 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
216 { short_option, long_option, doc, help, options::One_option::dash, \
217 NULL, NULL, NULL, func, NULL }
218 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
219 { short_option, long_option, doc, help, options::One_option::dash, \
220 NULL, NULL, NULL, NULL, func }
221 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
222 { short_option, long_option, doc, help, options::One_option::dash, \
223 func, NULL, NULL, NULL, NULL }
225 // Here is the actual list of options which we accept.
227 const options::One_option
228 options::Command_line_options::options
[] =
230 SPECIAL('l', "library", N_("Search for library LIBNAME"),
231 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES
,
233 SPECIAL('(', "start-group", N_("Start a library search group"), NULL
,
234 TWO_DASHES
, &start_group
),
235 SPECIAL(')', "end-group", N_("End a library search group"), NULL
,
236 TWO_DASHES
, &end_group
),
237 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
238 NULL
, TWO_DASHES
, &General_options::set_export_dynamic
),
239 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
240 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES
,
241 &General_options::set_dynamic_linker
),
242 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
243 N_("-L DIR, --library-path DIR"), TWO_DASHES
,
244 &General_options::add_to_search_path
),
245 GENERAL_ARG('m', NULL
, N_("Ignored for compatibility"), NULL
, ONE_DASH
,
246 &General_options::ignore
),
247 GENERAL_ARG('o', "output", N_("Set output file name"),
248 N_("-o FILE, --output FILE"), TWO_DASHES
,
249 &General_options::set_output_file_name
),
250 GENERAL_NOARG('r', NULL
, N_("Generate relocatable output"), NULL
,
251 ONE_DASH
, &General_options::set_relocatable
),
252 GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
253 N_("-R DIR, -rpath DIR"), ONE_DASH
,
254 &General_options::add_to_rpath
),
255 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
256 NULL
, TWO_DASHES
, &General_options::create_eh_frame_hdr
),
257 GENERAL_ARG('\0', "rpath-link",
258 N_("Add DIR to link time shared library search path"),
259 N_("--rpath-link DIR"), TWO_DASHES
,
260 &General_options::add_to_rpath_link
),
261 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
262 NULL
, ONE_DASH
, &General_options::set_shared
),
263 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
264 NULL
, ONE_DASH
, &General_options::set_static
),
265 POSDEP_NOARG('\0', "as-needed",
266 N_("Only set DT_NEEDED for dynamic libs if used"),
267 NULL
, TWO_DASHES
, &Position_dependent_options::set_as_needed
),
268 POSDEP_NOARG('\0', "no-as-needed",
269 N_("Always DT_NEEDED for dynamic libs (default)"),
270 NULL
, TWO_DASHES
, &Position_dependent_options::clear_as_needed
),
271 POSDEP_NOARG('\0', "whole-archive",
272 N_("Include all archive contents"),
274 &Position_dependent_options::set_whole_archive
),
275 POSDEP_NOARG('\0', "no-whole-archive",
276 N_("Include only needed archive contents"),
278 &Position_dependent_options::clear_whole_archive
),
279 SPECIAL('\0', "help", N_("Report usage information"), NULL
,
283 const int options::Command_line_options::options_size
=
284 sizeof (options
) / sizeof (options
[0]);
286 // The default values for the general options.
288 General_options::General_options()
289 : export_dynamic_(false),
290 dynamic_linker_(NULL
),
292 output_file_name_("a.out"),
293 is_relocatable_(false),
294 create_eh_frame_hdr_(false),
302 // The default values for the position dependent options.
304 Position_dependent_options::Position_dependent_options()
305 : do_static_search_(false),
307 include_whole_archive_(false)
311 // Input_arguments methods.
313 // Add a file to the list.
316 Input_arguments::add_file(const Input_file_argument
& file
)
318 if (!this->in_group_
)
319 this->input_argument_list_
.push_back(Input_argument(file
));
322 gold_assert(!this->input_argument_list_
.empty());
323 gold_assert(this->input_argument_list_
.back().is_group());
324 this->input_argument_list_
.back().group()->add_file(file
);
331 Input_arguments::start_group()
333 gold_assert(!this->in_group_
);
334 Input_file_group
* group
= new Input_file_group();
335 this->input_argument_list_
.push_back(Input_argument(group
));
336 this->in_group_
= true;
342 Input_arguments::end_group()
344 gold_assert(this->in_group_
);
345 this->in_group_
= false;
348 // Command_line options.
350 Command_line::Command_line()
351 : options_(), position_options_(), inputs_()
355 // Process the command line options.
358 Command_line::process(int argc
, char** argv
)
360 const int options_size
= options::Command_line_options::options_size
;
361 const options::One_option
* options
=
362 options::Command_line_options::options
;
363 bool no_more_options
= false;
367 if (argv
[i
][0] != '-' || no_more_options
)
369 this->add_file(argv
[i
], false);
374 // Option starting with '-'.
376 if (argv
[i
][1] == '-')
379 if (argv
[i
][2] == '\0')
381 no_more_options
= true;
386 // Look for a long option match.
387 char* opt
= argv
[i
] + dashes
;
390 char* arg
= strchr(opt
, '=');
393 else if (i
+ 1 < argc
)
400 for (j
= 0; j
< options_size
; ++j
)
402 if (options
[j
].long_option
!= NULL
405 != options::One_option::EXACTLY_TWO_DASHES
))
406 && first
== options
[j
].long_option
[0]
407 && strcmp(opt
, options
[j
].long_option
) == 0)
409 if (options
[j
].special
)
410 i
+= options
[j
].special(argc
- 1, argv
+ i
, opt
, this);
413 if (!options
[j
].takes_argument())
421 this->usage(_("missing argument"), argv
[i
]);
423 this->apply_option(options
[j
], arg
);
429 if (j
< options_size
)
432 // If we saw two dashes, we need to see a long option.
434 this->usage(_("unknown option"), argv
[i
]);
436 // Look for a short option match. There may be more than one
437 // short option in a given argument.
439 char* s
= argv
[i
] + 1;
441 while (*s
!= '\0' && !done
)
445 for (j
= 0; j
< options_size
; ++j
)
447 if (options
[j
].short_option
== opt
)
449 if (options
[j
].special
)
451 // Undo the argument skip done above.
453 i
+= options
[j
].special(argc
- i
, argv
+ i
, s
, this);
459 if (options
[j
].takes_argument())
472 this->usage(_("missing argument"), opt
);
474 this->apply_option(options
[j
], arg
);
480 if (j
>= options_size
)
481 this->usage(_("unknown option"), *s
);
487 if (this->inputs_
.in_group())
489 fprintf(stderr
, _("%s: missing group end"), program_name
);
493 // FIXME: We should only do this when configured in native mode.
494 this->options_
.add_to_search_path("/lib");
495 this->options_
.add_to_search_path("/usr/lib");
498 // Apply a command line option.
501 Command_line::apply_option(const options::One_option
& opt
,
506 if (opt
.general_noarg
)
507 (this->options_
.*(opt
.general_noarg
))();
508 else if (opt
.dependent_noarg
)
509 (this->position_options_
.*(opt
.dependent_noarg
))();
516 (this->options_
.*(opt
.general_arg
))(arg
);
517 else if (opt
.dependent_arg
)
518 (this->position_options_
.*(opt
.dependent_arg
))(arg
);
524 // Add an input file or library.
527 Command_line::add_file(const char* name
, bool is_lib
)
529 Input_file_argument
file(name
, is_lib
, this->position_options_
);
530 this->inputs_
.add_file(file
);
533 // Handle the -l option, which requires special treatment.
536 Command_line::process_l_option(int argc
, char** argv
, char* arg
)
548 libname
= argv
[argc
+ 1];
551 this->usage(_("missing argument"), arg
);
553 this->add_file(libname
, true);
558 // Handle the --start-group option.
561 Command_line::start_group(const char* arg
)
563 if (this->inputs_
.in_group())
564 this->usage(_("may not nest groups"), arg
);
565 this->inputs_
.start_group();
568 // Handle the --end-group option.
571 Command_line::end_group(const char* arg
)
573 if (!this->inputs_
.in_group())
574 this->usage(_("group end without group start"), arg
);
575 this->inputs_
.end_group();
578 // Report a usage error. */
581 Command_line::usage()
584 _("%s: use the --help option for usage information\n"),
590 Command_line::usage(const char* msg
, const char *opt
)
594 program_name
, opt
, msg
);
599 Command_line::usage(const char* msg
, char opt
)
603 program_name
, opt
, msg
);
607 } // End namespace gold.
This page took 0.041931 seconds and 4 git commands to generate.