1 // options.c -- handle command line options for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
27 #include "filenames.h"
28 #include "libiberty.h"
35 // The information we keep for a single command line option.
37 struct options::One_option
39 // The single character option name, or '\0' if this is only a long
43 // The long option name, or NULL if this is only a short option.
44 const char* long_option
;
46 // Description of the option for --help output, or NULL if there is none.
49 // How to print the option name in --help output, or NULL to use the
51 const char* help_output
;
53 // Long option dash control. This is ignored if long_option is
57 // Long option normally takes one dash; two dashes are also
60 // Long option normally takes two dashes; one dash is also
63 // Long option always takes two dashes.
67 // Function for special handling, or NULL. Returns the number of
68 // arguments to skip. This will normally be at least 1, but it may
69 // be 0 if this function changes *argv. ARG points to the location
70 // in *ARGV where the option starts, which may be helpful for a
72 int (*special
)(int argc
, char** argv
, char *arg
, bool long_option
,
75 // If this is a position independent option which does not take an
76 // argument, this is the member function to call to record it.
77 void (General_options::*general_noarg
)();
79 // If this is a position independent function which takes an
80 // argument, this is the member function to call to record it.
81 void (General_options::*general_arg
)(const char*);
83 // If this is a position dependent option which does not take an
84 // argument, this is the member function to call to record it.
85 void (Position_dependent_options::*dependent_noarg
)();
87 // If this is a position dependent option which takes an argument,
88 // this is the member function to record it.
89 void (Position_dependent_options::*dependent_arg
)(const char*);
91 // Return whether this option takes an argument.
93 takes_argument() const
94 { return this->general_arg
!= NULL
|| this->dependent_arg
!= NULL
; }
97 // We have a separate table for -z options.
99 struct options::One_z_option
101 // The name of the option.
104 // The member function in General_options called to record it.
105 void (General_options::*set
)();
108 class options::Command_line_options
111 static const One_option options
[];
112 static const int options_size
;
113 static const One_z_option z_options
[];
114 static const int z_options_size
;
117 } // End namespace gold.
122 // Handle the special -l option, which adds an input file.
125 library(int argc
, char** argv
, char* arg
, bool long_option
,
126 gold::Command_line
* cmdline
)
128 return cmdline
->process_l_option(argc
, argv
, arg
, long_option
);
131 // Handle the special -T/--script option, which reads a linker script.
134 invoke_script(int argc
, char** argv
, char* arg
, bool long_option
,
135 gold::Command_line
* cmdline
)
138 const char* script_name
= cmdline
->get_special_argument("script", argc
, argv
,
141 if (!read_commandline_script(script_name
, cmdline
))
142 gold::gold_error(_("%s: unable to parse script file %s\n"),
143 gold::program_name
, arg
);
147 // Handle the special --start-group option.
150 start_group(int, char**, char* arg
, bool, gold::Command_line
* cmdline
)
152 cmdline
->start_group(arg
);
156 // Handle the special --end-group option.
159 end_group(int, char**, char* arg
, bool, gold::Command_line
* cmdline
)
161 cmdline
->end_group(arg
);
165 // Report usage information for ld --help, and exit.
168 help(int, char**, char*, bool, gold::Command_line
*)
170 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name
);
172 const int options_size
= gold::options::Command_line_options::options_size
;
173 const gold::options::One_option
* options
=
174 gold::options::Command_line_options::options
;
175 for (int i
= 0; i
< options_size
; ++i
)
177 if (options
[i
].doc
== NULL
)
187 if (options
[j
].help_output
!= NULL
)
194 printf(options
[j
].help_output
);
195 len
+= std::strlen(options
[i
].help_output
);
200 if (options
[j
].short_option
!= '\0')
207 printf("-%c", options
[j
].short_option
);
212 if (options
[j
].long_option
!= NULL
)
219 if (options
[j
].dash
== gold::options::One_option::ONE_DASH
)
229 printf("%s", options
[j
].long_option
);
230 len
+= std::strlen(options
[j
].long_option
);
236 while (j
< options_size
&& options
[j
].doc
== NULL
);
243 for (; len
< 30; ++len
)
246 std::puts(options
[i
].doc
);
254 // Report version information.
257 version(int, char**, char* opt
, bool, gold::Command_line
*)
259 gold::print_version(opt
[0] == 'v' && opt
[1] == '\0');
264 // If the default sysroot is relocatable, try relocating it based on
268 get_relative_sysroot(const char* from
)
270 char* path
= make_relative_prefix(gold::program_name
, from
,
275 if (::stat(path
, &s
) == 0 && S_ISDIR(s
.st_mode
))
283 // Return the default sysroot. This is set by the --with-sysroot
284 // option to configure.
287 get_default_sysroot()
289 const char* sysroot
= TARGET_SYSTEM_ROOT
;
290 if (*sysroot
== '\0')
293 if (TARGET_SYSTEM_ROOT_RELOCATABLE
)
295 char* path
= get_relative_sysroot (BINDIR
);
297 path
= get_relative_sysroot (TOOLBINDIR
);
300 std::string ret
= path
;
309 } // End anonymous namespace.
314 // Helper macros used to specify the options. We could also do this
315 // using constructors, but then g++ would generate code to initialize
316 // the array. We want the array to be initialized statically so that
317 // we get better startup time.
319 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
320 { short_option, long_option, doc, help, options::One_option::dash, \
321 NULL, func, NULL, NULL, NULL }
322 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
323 { short_option, long_option, doc, help, options::One_option::dash, \
324 NULL, NULL, func, NULL, NULL }
325 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
326 { short_option, long_option, doc, help, options::One_option::dash, \
327 NULL, NULL, NULL, func, NULL }
328 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
329 { short_option, long_option, doc, help, options::One_option::dash, \
330 NULL, NULL, NULL, NULL, func }
331 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
332 { short_option, long_option, doc, help, options::One_option::dash, \
333 func, NULL, NULL, NULL, NULL }
335 // Here is the actual list of options which we accept.
337 const options::One_option
338 options::Command_line_options::options
[] =
340 GENERAL_NOARG('\0', "allow-shlib-undefined",
341 N_("Allow unresolved references in shared libraries"),
343 &General_options::set_allow_shlib_undefined
),
344 GENERAL_NOARG('\0', "no-allow-shlib-undefined",
345 N_("Do not allow unresolved references in shared libraries"),
347 &General_options::set_no_allow_shlib_undefined
),
348 POSDEP_NOARG('\0', "as-needed",
349 N_("Only set DT_NEEDED for dynamic libs if used"),
350 NULL
, TWO_DASHES
, &Position_dependent_options::set_as_needed
),
351 POSDEP_NOARG('\0', "no-as-needed",
352 N_("Always DT_NEEDED for dynamic libs (default)"),
353 NULL
, TWO_DASHES
, &Position_dependent_options::clear_as_needed
),
354 POSDEP_NOARG('\0', "Bdynamic",
355 N_("-l searches for shared libraries"),
357 &Position_dependent_options::set_dynamic_search
),
358 POSDEP_NOARG('\0', "Bstatic",
359 N_("-l does not search for shared libraries"),
361 &Position_dependent_options::set_static_search
),
362 GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
363 NULL
, ONE_DASH
, &General_options::set_symbolic
),
364 GENERAL_NOARG('\0', "detect-odr-violations",
365 N_("Try to detect violations of the One Definition Rule"),
366 NULL
, TWO_DASHES
, &General_options::set_detect_odr_violations
),
367 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
368 NULL
, TWO_DASHES
, &General_options::set_export_dynamic
),
369 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
370 NULL
, TWO_DASHES
, &General_options::set_create_eh_frame_hdr
),
371 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
372 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES
,
373 &General_options::set_dynamic_linker
),
374 SPECIAL('l', "library", N_("Search for library LIBNAME"),
375 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES
,
377 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
378 N_("-L DIR, --library-path DIR"), TWO_DASHES
,
379 &General_options::add_to_search_path
),
380 GENERAL_ARG('m', NULL
, N_("Ignored for compatibility"), NULL
, ONE_DASH
,
381 &General_options::ignore
),
382 GENERAL_ARG('o', "output", N_("Set output file name"),
383 N_("-o FILE, --output FILE"), TWO_DASHES
,
384 &General_options::set_output_file_name
),
385 GENERAL_ARG('O', NULL
, N_("Optimize output file size"),
386 N_("-O level"), ONE_DASH
,
387 &General_options::set_optimization_level
),
388 GENERAL_NOARG('r', NULL
, N_("Generate relocatable output"), NULL
,
389 ONE_DASH
, &General_options::set_relocatable
),
390 GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
391 N_("-R DIR, -rpath DIR"), ONE_DASH
,
392 &General_options::add_to_rpath
),
393 GENERAL_ARG('\0', "rpath-link",
394 N_("Add DIR to link time shared library search path"),
395 N_("--rpath-link DIR"), TWO_DASHES
,
396 &General_options::add_to_rpath_link
),
397 GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL
,
398 TWO_DASHES
, &General_options::set_strip_all
),
399 GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL
,
400 TWO_DASHES
, &General_options::set_strip_debug
),
401 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
402 NULL
, ONE_DASH
, &General_options::set_shared
),
403 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
404 NULL
, ONE_DASH
, &General_options::set_static
),
405 GENERAL_NOARG('\0', "stats", N_("Print resource usage statistics"),
406 NULL
, TWO_DASHES
, &General_options::set_stats
),
407 GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
408 N_("--sysroot DIR"), TWO_DASHES
, &General_options::set_sysroot
),
409 GENERAL_ARG('\0', "Ttext", N_("Set the address of the .text section"),
410 N_("-Ttext ADDRESS"), ONE_DASH
,
411 &General_options::set_text_segment_address
),
412 // This must come after -Ttext since it's a prefix of it.
413 SPECIAL('T', "script", N_("Read linker script"),
414 N_("-T FILE, --script FILE"), TWO_DASHES
,
416 GENERAL_NOARG('\0', "threads", N_("Run the linker multi-threaded"),
417 NULL
, TWO_DASHES
, &General_options::set_threads
),
418 GENERAL_NOARG('\0', "no-threads", N_("Do not run the linker multi-threaded"),
419 NULL
, TWO_DASHES
, &General_options::clear_threads
),
420 GENERAL_ARG('\0', "thread-count", N_("Number of threads to use"),
421 N_("--thread-count COUNT"), TWO_DASHES
,
422 &General_options::set_thread_count
),
423 GENERAL_ARG('\0', "thread-count-initial",
424 N_("Number of threads to use in initial pass"),
425 N_("--thread-count-initial COUNT"), TWO_DASHES
,
426 &General_options::set_thread_count_initial
),
427 GENERAL_ARG('\0', "thread-count-middle",
428 N_("Number of threads to use in middle pass"),
429 N_("--thread-count-middle COUNT"), TWO_DASHES
,
430 &General_options::set_thread_count_middle
),
431 GENERAL_ARG('\0', "thread-count-final",
432 N_("Number of threads to use in final pass"),
433 N_("--thread-count-final COUNT"), TWO_DASHES
,
434 &General_options::set_thread_count_final
),
435 POSDEP_NOARG('\0', "whole-archive",
436 N_("Include all archive contents"),
438 &Position_dependent_options::set_whole_archive
),
439 POSDEP_NOARG('\0', "no-whole-archive",
440 N_("Include only needed archive contents"),
442 &Position_dependent_options::clear_whole_archive
),
444 GENERAL_ARG('z', NULL
,
445 N_("Subcommands as follows:\n\
446 -z execstack Mark output as requiring executable stack\n\
447 -z noexecstack Mark output as not requiring executable stack"),
448 N_("-z SUBCOMMAND"), ONE_DASH
,
449 &General_options::handle_z_option
),
451 SPECIAL('(', "start-group", N_("Start a library search group"), NULL
,
452 TWO_DASHES
, &start_group
),
453 SPECIAL(')', "end-group", N_("End a library search group"), NULL
,
454 TWO_DASHES
, &end_group
),
455 SPECIAL('\0', "help", N_("Report usage information"), NULL
,
457 SPECIAL('v', "version", N_("Report version information"), NULL
,
458 TWO_DASHES
, &version
)
461 const int options::Command_line_options::options_size
=
462 sizeof (options
) / sizeof (options
[0]);
466 const options::One_z_option
467 options::Command_line_options::z_options
[] =
469 { "execstack", &General_options::set_execstack
},
470 { "noexecstack", &General_options::set_noexecstack
},
473 const int options::Command_line_options::z_options_size
=
474 sizeof(z_options
) / sizeof(z_options
[0]);
476 // The default values for the general options.
478 General_options::General_options()
479 : export_dynamic_(false),
480 dynamic_linker_(NULL
),
482 optimization_level_(0),
483 output_file_name_("a.out"),
484 is_relocatable_(false),
486 allow_shlib_undefined_(false),
488 detect_odr_violations_(false),
489 create_eh_frame_hdr_(false),
496 text_segment_address_(-1U), // -1 indicates value not set by user
498 thread_count_initial_(0),
499 thread_count_middle_(0),
500 thread_count_final_(0),
501 execstack_(EXECSTACK_FROM_INPUT
)
505 // The default values for the position dependent options.
507 Position_dependent_options::Position_dependent_options()
508 : do_static_search_(false),
510 include_whole_archive_(false)
514 // Handle the -z option.
517 General_options::handle_z_option(const char* arg
)
519 const int z_options_size
= options::Command_line_options::z_options_size
;
520 const gold::options::One_z_option
* z_options
=
521 gold::options::Command_line_options::z_options
;
522 for (int i
= 0; i
< z_options_size
; ++i
)
524 if (strcmp(arg
, z_options
[i
].name
) == 0)
526 (this->*(z_options
[i
].set
))();
531 fprintf(stderr
, _("%s: unrecognized -z subcommand: %s\n"),
536 // Add the sysroot, if any, to the search paths.
539 General_options::add_sysroot()
541 if (this->sysroot_
.empty())
543 this->sysroot_
= get_default_sysroot();
544 if (this->sysroot_
.empty())
548 const char* sysroot
= this->sysroot_
.c_str();
549 char* canonical_sysroot
= lrealpath(sysroot
);
551 for (Dir_list::iterator p
= this->search_path_
.begin();
552 p
!= this->search_path_
.end();
554 p
->add_sysroot(sysroot
, canonical_sysroot
);
556 free(canonical_sysroot
);
559 // Search_directory methods.
561 // This is called if we have a sysroot. Apply the sysroot if
562 // appropriate. Record whether the directory is in the sysroot.
565 Search_directory::add_sysroot(const char* sysroot
,
566 const char* canonical_sysroot
)
568 gold_assert(*sysroot
!= '\0');
569 if (this->put_in_sysroot_
)
571 if (!IS_DIR_SEPARATOR(this->name_
[0])
572 && !IS_DIR_SEPARATOR(sysroot
[strlen(sysroot
) - 1]))
573 this->name_
= '/' + this->name_
;
574 this->name_
= sysroot
+ this->name_
;
575 this->is_in_sysroot_
= true;
579 // Check whether this entry is in the sysroot. To do this
580 // correctly, we need to use canonical names. Otherwise we will
581 // get confused by the ../../.. paths that gcc tends to use.
582 char* canonical_name
= lrealpath(this->name_
.c_str());
583 int canonical_name_len
= strlen(canonical_name
);
584 int canonical_sysroot_len
= strlen(canonical_sysroot
);
585 if (canonical_name_len
> canonical_sysroot_len
586 && IS_DIR_SEPARATOR(canonical_name
[canonical_sysroot_len
]))
588 canonical_name
[canonical_sysroot_len
] = '\0';
589 if (FILENAME_CMP(canonical_name
, canonical_sysroot
) == 0)
590 this->is_in_sysroot_
= true;
592 free(canonical_name
);
596 // Input_arguments methods.
598 // Add a file to the list.
601 Input_arguments::add_file(const Input_file_argument
& file
)
603 if (!this->in_group_
)
604 this->input_argument_list_
.push_back(Input_argument(file
));
607 gold_assert(!this->input_argument_list_
.empty());
608 gold_assert(this->input_argument_list_
.back().is_group());
609 this->input_argument_list_
.back().group()->add_file(file
);
616 Input_arguments::start_group()
618 gold_assert(!this->in_group_
);
619 Input_file_group
* group
= new Input_file_group();
620 this->input_argument_list_
.push_back(Input_argument(group
));
621 this->in_group_
= true;
627 Input_arguments::end_group()
629 gold_assert(this->in_group_
);
630 this->in_group_
= false;
633 // Command_line options.
635 Command_line::Command_line()
636 : options_(), position_options_(), inputs_()
640 // Process the command line options. For process_one_option,
641 // i is the index of argv to process next, and the return value
642 // is the index of the next option to process (i+1 or i+2, or argc
643 // to indicate processing is done). no_more_options is set to true
644 // if (and when) "--" is seen as an option.
647 Command_line::process_one_option(int argc
, char** argv
, int i
,
648 bool* no_more_options
)
650 const int options_size
= options::Command_line_options::options_size
;
651 const options::One_option
* options
= options::Command_line_options::options
;
652 gold_assert(i
< argc
);
654 if (argv
[i
][0] != '-' || *no_more_options
)
656 this->add_file(argv
[i
], false);
660 // Option starting with '-'.
662 if (argv
[i
][1] == '-')
665 if (argv
[i
][2] == '\0')
667 *no_more_options
= true;
672 // Look for a long option match.
673 char* opt
= argv
[i
] + dashes
;
676 char* arg
= strchr(opt
, '=');
677 bool argument_with_equals
= arg
!= NULL
;
683 else if (i
+ 1 < argc
)
690 for (j
= 0; j
< options_size
; ++j
)
692 if (options
[j
].long_option
!= NULL
695 != options::One_option::EXACTLY_TWO_DASHES
))
696 && first
== options
[j
].long_option
[0]
697 && strcmp(opt
, options
[j
].long_option
) == 0)
699 if (options
[j
].special
)
701 // Restore the '=' we clobbered above.
702 if (arg
!= NULL
&& skiparg
== 0)
704 i
+= options
[j
].special(argc
- i
, argv
+ i
, opt
, true, this);
708 if (!options
[j
].takes_argument())
710 if (argument_with_equals
)
711 this->usage(_("unexpected argument"), argv
[i
]);
718 this->usage(_("missing argument"), argv
[i
]);
720 this->apply_option(options
[j
], arg
);
726 if (j
< options_size
)
729 // If we saw two dashes, we needed to have seen a long option.
731 this->usage(_("unknown option"), argv
[i
]);
733 // Look for a short option match. There may be more than one
734 // short option in a given argument.
736 char* s
= argv
[i
] + 1;
738 while (*s
!= '\0' && !done
)
742 for (j
= 0; j
< options_size
; ++j
)
744 if (options
[j
].short_option
== opt
)
746 if (options
[j
].special
)
748 // Undo the argument skip done above.
750 i
+= options
[j
].special(argc
- i
, argv
+ i
, s
, false,
757 if (options
[j
].takes_argument())
770 this->usage(_("missing argument"), opt
);
772 this->apply_option(options
[j
], arg
);
778 if (j
>= options_size
)
779 this->usage(_("unknown option"), *s
);
788 Command_line::process(int argc
, char** argv
)
790 bool no_more_options
= false;
793 i
= process_one_option(argc
, argv
, i
, &no_more_options
);
795 if (this->inputs_
.in_group())
797 fprintf(stderr
, _("%s: missing group end\n"), program_name
);
801 // FIXME: We should only do this when configured in native mode.
802 this->options_
.add_to_search_path_with_sysroot("/lib");
803 this->options_
.add_to_search_path_with_sysroot("/usr/lib");
805 this->options_
.add_sysroot();
807 // Ensure options don't contradict each other and are otherwise kosher.
808 this->normalize_options();
811 // Extract an option argument for a special option. LONGNAME is the
812 // long name of the option. This sets *PRET to the return value for
813 // the special function handler to skip to the next option.
816 Command_line::get_special_argument(const char* longname
, int argc
, char** argv
,
817 const char* arg
, bool long_option
,
822 size_t longlen
= strlen(longname
);
823 gold_assert(strncmp(arg
, longname
, longlen
) == 0);
832 gold_assert(*arg
== '\0');
851 this->usage(_("missing argument"), arg
);
854 // Ensure options don't contradict each other and are otherwise kosher.
857 Command_line::normalize_options()
859 // If the user specifies both -s and -r, convert the -s as -S.
860 // -r requires us to keep externally visible symbols!
861 if (this->options_
.strip_all() && this->options_
.is_relocatable())
863 // Clears the strip_all() status, replacing it with strip_debug().
864 this->options_
.set_strip_debug();
867 // FIXME: we can/should be doing a lot more sanity checking here.
871 // Apply a command line option.
874 Command_line::apply_option(const options::One_option
& opt
,
879 if (opt
.general_noarg
)
880 (this->options_
.*(opt
.general_noarg
))();
881 else if (opt
.dependent_noarg
)
882 (this->position_options_
.*(opt
.dependent_noarg
))();
889 (this->options_
.*(opt
.general_arg
))(arg
);
890 else if (opt
.dependent_arg
)
891 (this->position_options_
.*(opt
.dependent_arg
))(arg
);
897 // Add an input file or library.
900 Command_line::add_file(const char* name
, bool is_lib
)
902 Input_file_argument
file(name
, is_lib
, "", this->position_options_
);
903 this->inputs_
.add_file(file
);
906 // Handle the -l option, which requires special treatment.
909 Command_line::process_l_option(int argc
, char** argv
, char* arg
,
913 const char* libname
= this->get_special_argument("library", argc
, argv
, arg
,
915 this->add_file(libname
, true);
919 // Handle the --start-group option.
922 Command_line::start_group(const char* arg
)
924 if (this->inputs_
.in_group())
925 this->usage(_("may not nest groups"), arg
);
926 this->inputs_
.start_group();
929 // Handle the --end-group option.
932 Command_line::end_group(const char* arg
)
934 if (!this->inputs_
.in_group())
935 this->usage(_("group end without group start"), arg
);
936 this->inputs_
.end_group();
939 // Report a usage error. */
942 Command_line::usage()
945 _("%s: use the --help option for usage information\n"),
951 Command_line::usage(const char* msg
, const char *opt
)
955 program_name
, opt
, msg
);
960 Command_line::usage(const char* msg
, char opt
)
964 program_name
, opt
, msg
);
968 } // End namespace gold.