/* Parse options for the GNU linker.
- Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000
+ Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
+ 2001, 2002
Free Software Foundation, Inc.
This file is part of GLD, the Gnu Linker.
#include "libiberty.h"
#include <stdio.h>
#include <string.h>
-#include <ctype.h>
+#include "safe-ctype.h"
#include "getopt.h"
#include "bfdlink.h"
#include "ld.h"
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
-/* Omit args to avoid the possibility of clashing with a system header
- that might disagree about consts. */
-unsigned long strtoul ();
-
static int is_num PARAMS ((const char *, int, int, int));
static void set_default_dirlist PARAMS ((char *dirlist_ptr));
static void set_section_start PARAMS ((char *sect, char *valstr));
#define OPTION_VERSION_EXPORTS_SECTION (OPTION_VERSION_SCRIPT + 1)
#define OPTION_WARN_COMMON (OPTION_VERSION_EXPORTS_SECTION + 1)
#define OPTION_WARN_CONSTRUCTORS (OPTION_WARN_COMMON + 1)
-#define OPTION_WARN_MULTIPLE_GP (OPTION_WARN_CONSTRUCTORS + 1)
+#define OPTION_WARN_FATAL (OPTION_WARN_CONSTRUCTORS + 1)
+#define OPTION_WARN_MULTIPLE_GP (OPTION_WARN_FATAL + 1)
#define OPTION_WARN_ONCE (OPTION_WARN_MULTIPLE_GP + 1)
#define OPTION_WARN_SECTION_ALIGN (OPTION_WARN_ONCE + 1)
#define OPTION_SPLIT_BY_RELOC (OPTION_WARN_SECTION_ALIGN + 1)
#define OPTION_FINI (OPTION_INIT + 1)
#define OPTION_SECTION_START (OPTION_FINI + 1)
#define OPTION_UNIQUE (OPTION_SECTION_START + 1)
+#define OPTION_TARGET_HELP (OPTION_UNIQUE + 1)
+#define OPTION_ALLOW_SHLIB_UNDEFINED (OPTION_TARGET_HELP + 1)
+#define OPTION_ALLOW_MULTIPLE_DEFINITION (OPTION_ALLOW_SHLIB_UNDEFINED + 1)
+#define OPTION_DISCARD_NONE (OPTION_ALLOW_MULTIPLE_DEFINITION + 1)
+#define OPTION_SPARE_DYNAMIC_TAGS (OPTION_DISCARD_NONE + 1)
+#define OPTION_NO_DEFINE_COMMON (OPTION_SPARE_DYNAMIC_TAGS + 1)
+#define OPTION_NOSTDLIB (OPTION_NO_DEFINE_COMMON + 1)
/* The long options. This structure is used for both the option
parsing and the help text. */
/* The documentation string. If this is NULL, this is a synonym for
the previous option. */
const char *doc;
- enum
- {
- /* Use one dash before long option name. */
- ONE_DASH,
- /* Use two dashes before long option name. */
- TWO_DASHES,
- /* Don't mention this option in --help output. */
- NO_HELP
- } control;
+ enum {
+ /* Use one dash before long option name. */
+ ONE_DASH,
+ /* Use two dashes before long option name. */
+ TWO_DASHES,
+ /* Only accept two dashes before the long option name.
+ This is an overloading of the use of this enum, since originally it
+ was only intended to tell the --help display function how to display
+ the long option name. This feature was added in order to resolve
+ the confusion about the -omagic command line switch. Is it setting
+ the output file name to "magic" or is it setting the NMAGIC flag on
+ the output ? It has been decided that it is setting the output file
+ name, and that if you want to set the NMAGIC flag you should use -N
+ or --omagic. */
+ EXACTLY_TWO_DASHES,
+ /* Don't mention this option in --help output. */
+ NO_HELP
+ } control;
};
static const struct ld_option ld_options[] =
TWO_DASHES },
{ {"soname", required_argument, NULL, OPTION_SONAME},
'h', N_("FILENAME"), N_("Set internal name of shared library"), ONE_DASH },
+ { {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
+ 'I', N_("PROGRAM"), N_("Set PROGRAM as the dynamic linker to use"), TWO_DASHES },
{ {"library", required_argument, NULL, 'l'},
'l', N_("LIBNAME"), N_("Search for library LIBNAME"), TWO_DASHES },
{ {"library-path", required_argument, NULL, 'L'},
'n', NULL, N_("Do not page align data"), TWO_DASHES },
{ {"omagic", no_argument, NULL, 'N'},
'N', NULL, N_("Do not page align data, do not make text readonly"),
- TWO_DASHES },
+ EXACTLY_TWO_DASHES },
{ {"output", required_argument, NULL, 'o'},
- 'o', N_("FILE"), N_("Set output file name"), TWO_DASHES },
+ 'o', N_("FILE"), N_("Set output file name"), EXACTLY_TWO_DASHES },
{ {NULL, required_argument, NULL, '\0'},
'O', NULL, N_("Optimize output file"), ONE_DASH },
{ {"Qy", no_argument, NULL, OPTION_IGNORE},
'T', N_("FILE"), N_("Read linker script"), TWO_DASHES },
{ {"undefined", required_argument, NULL, 'u'},
'u', N_("SYMBOL"), N_("Start with undefined reference to SYMBOL"), TWO_DASHES },
- { {"unique", no_argument, NULL, OPTION_UNIQUE},
- '\0', NULL, N_("Don't merge orphan sections with the same name"), TWO_DASHES },
+ { {"unique", optional_argument, NULL, OPTION_UNIQUE},
+ '\0', N_("[=SECTION]"), N_("Don't merge input [SECTION | orphan] sections"), TWO_DASHES },
{ {"Ur", no_argument, NULL, OPTION_UR},
'\0', NULL, N_("Build global constructor/destructor tables"), ONE_DASH },
{ {"version", no_argument, NULL, OPTION_VERSION},
{ {"discard-all", no_argument, NULL, 'x'},
'x', NULL, N_("Discard all local symbols"), TWO_DASHES },
{ {"discard-locals", no_argument, NULL, 'X'},
- 'X', NULL, N_("Discard temporary local symbols"), TWO_DASHES },
+ 'X', NULL, N_("Discard temporary local symbols (default)"), TWO_DASHES },
+ { {"discard-none", no_argument, NULL, OPTION_DISCARD_NONE},
+ '\0', NULL, N_("Don't discard any local symbols"), TWO_DASHES },
{ {"trace-symbol", required_argument, NULL, 'y'},
'y', N_("SYMBOL"), N_("Trace mentions of SYMBOL"), TWO_DASHES },
{ {NULL, required_argument, NULL, '\0'},
'\0', N_("SYMBOL=EXPRESSION"), N_("Define a symbol"), TWO_DASHES },
{ {"demangle", optional_argument, NULL, OPTION_DEMANGLE},
'\0', N_("[=STYLE]"), N_("Demangle symbol names [using STYLE]"), TWO_DASHES },
- { {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
- '\0', N_("PROGRAM"), N_("Set the dynamic linker to use"), TWO_DASHES },
{ {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
'\0', NULL, N_("Generate embedded relocs"), TWO_DASHES},
{ {"fini", required_argument, NULL, OPTION_FINI},
'\0', N_("SYMBOL"), N_("Call SYMBOL at load-time"), ONE_DASH },
{ {"Map", required_argument, NULL, OPTION_MAP},
'\0', N_("FILE"), N_("Write a map file"), ONE_DASH },
+ { {"no-define-common", no_argument, NULL, OPTION_NO_DEFINE_COMMON},
+ '\0', NULL, N_("Do not define Common storage"), TWO_DASHES },
{ {"no-demangle", no_argument, NULL, OPTION_NO_DEMANGLE },
'\0', NULL, N_("Do not demangle symbol names"), TWO_DASHES },
{ {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
'\0', NULL, N_("Use less memory and more disk I/O"), TWO_DASHES },
{ {"no-undefined", no_argument, NULL, OPTION_NO_UNDEFINED},
'\0', NULL, N_("Allow no undefined symbols"), TWO_DASHES },
+ { {"allow-shlib-undefined", no_argument, NULL, OPTION_ALLOW_SHLIB_UNDEFINED},
+ '\0', NULL, N_("Allow undefined symbols in shared objects"), TWO_DASHES },
+ { {"allow-multiple-definition", no_argument, NULL, OPTION_ALLOW_MULTIPLE_DEFINITION},
+ '\0', NULL, N_("Allow multiple definitions"), TWO_DASHES },
{ {"no-warn-mismatch", no_argument, NULL, OPTION_NO_WARN_MISMATCH},
'\0', NULL, N_("Don't warn about mismatched input files"), TWO_DASHES},
{ {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
'\0', NULL, N_("Create an output file even if errors occur"), TWO_DASHES },
{ {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
'\0', NULL, NULL, NO_HELP },
+ { {"nostdlib", no_argument, NULL, OPTION_NOSTDLIB},
+ '\0', NULL, N_("Only use library directories specified on\n\t\t\t\tthe command line"), ONE_DASH },
{ {"oformat", required_argument, NULL, OPTION_OFORMAT},
- '\0', N_("TARGET"), N_("Specify target of output file"), TWO_DASHES },
+ '\0', N_("TARGET"), N_("Specify target of output file"), EXACTLY_TWO_DASHES },
{ {"qmagic", no_argument, NULL, OPTION_IGNORE},
'\0', NULL, N_("Ignored for Linux compatibility"), ONE_DASH },
{ {"relax", no_argument, NULL, OPTION_RELAX},
'\0', NULL, N_("Sort common symbols by size"), TWO_DASHES },
{ {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
'\0', NULL, NULL, NO_HELP },
+ { {"spare-dynamic-tags", required_argument, NULL, OPTION_SPARE_DYNAMIC_TAGS},
+ '\0', N_("COUNT"), N_("How many tags to reserve in .dynamic section"), TWO_DASHES },
{ {"split-by-file", optional_argument, NULL, OPTION_SPLIT_BY_FILE},
'\0', N_("[=SIZE]"), N_("Split output sections every SIZE octets"), TWO_DASHES },
{ {"split-by-reloc", optional_argument, NULL, OPTION_SPLIT_BY_RELOC},
'\0', N_("[=COUNT]"), N_("Split output sections every COUNT relocs"), TWO_DASHES },
{ {"stats", no_argument, NULL, OPTION_STATS},
'\0', NULL, N_("Print memory usage statistics"), TWO_DASHES },
+ { {"target-help", no_argument, NULL, OPTION_TARGET_HELP},
+ '\0', NULL, N_("Display target specific options"), TWO_DASHES },
{ {"task-link", required_argument, NULL, OPTION_TASK_LINK},
'\0', N_("SYMBOL"), N_("Do task level linking"), TWO_DASHES },
{ {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
{ {"warn-section-align", no_argument, NULL, OPTION_WARN_SECTION_ALIGN},
'\0', NULL, N_("Warn if start of section changes due to alignment"),
TWO_DASHES },
+ { {"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL},
+ '\0', NULL, N_("Treat warnings as errors"),
+ TWO_DASHES },
{ {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
'\0', NULL, N_("Include all objects from following archives"), TWO_DASHES },
{ {"wrap", required_argument, NULL, OPTION_WRAP},
'\0', N_("[=WORDS]"), N_("Modify problematic branches in last WORDS (1-10,\n\t\t\t\tdefault 5) words of a page"), TWO_DASHES }
};
-#define OPTION_COUNT ((int) (sizeof ld_options / sizeof ld_options[0]))
+#define OPTION_COUNT ARRAY_SIZE (ld_options)
/* Test STRING for containing a string of digits that form a number
between MIN and MAX. The return value is the number or ERR. */
for (; *string; ++string)
{
- if (! isdigit (*string))
+ if (! ISDIGIT (*string))
{
result = err;
break;
void
parse_args (argc, argv)
- int argc;
+ unsigned argc;
char **argv;
{
- int i, is, il;
+ unsigned i;
+ int is, il, irl;
int ingroup = 0;
char *default_dirlist = NULL;
char shortopts[OPTION_COUNT * 3 + 2];
struct option longopts[OPTION_COUNT + 1];
+ struct option really_longopts[OPTION_COUNT + 1];
int last_optind;
/* Starting the short option string with '-' is for programs that
shortopts[0] = '-';
is = 1;
il = 0;
+ irl = 0;
for (i = 0; i < OPTION_COUNT; i++)
{
if (ld_options[i].shortopt != '\0')
}
if (ld_options[i].opt.name != NULL)
{
- longopts[il] = ld_options[i].opt;
- ++il;
+ if (ld_options[i].control == EXACTLY_TWO_DASHES)
+ {
+ really_longopts[irl] = ld_options[i].opt;
+ ++irl;
+ }
+ else
+ {
+ longopts[il] = ld_options[i].opt;
+ ++il;
+ }
}
}
shortopts[is] = '\0';
longopts[il].name = NULL;
+ really_longopts[irl].name = NULL;
/* The -G option is ambiguous on different platforms. Sometimes it
specifies the largest data size to put into the small data
for (i = 1; i < argc; i++)
if (strcmp (argv[i], "-G") == 0
&& (i + 1 >= argc
- || ! isdigit ((unsigned char) argv[i + 1][0])))
+ || ! ISDIGIT (argv[i + 1][0])))
argv[i] = (char *) "--shared";
/* Because we permit long options to start with a single dash, and
-nx, in which the -n is parsed as a single option, and we
loop around to pick up the -x. */
if (optind != last_optind)
- {
- if (ldemul_parse_args (argc, argv))
- continue;
- last_optind = optind;
- }
+ if (ldemul_parse_args (argc, argv))
+ continue;
/* getopt_long_only is like getopt_long, but '-' as well as '--'
can indicate a long option. */
+ opterr = 0;
+ last_optind = optind;
optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
+ if (optc == '?')
+ {
+ optind = last_optind;
+ optc = getopt_long (argc, argv, "-", really_longopts, &longind);
+ }
if (optc == -1)
break;
+
switch (optc)
{
+ case '?':
+ einfo (_("%P: unrecognized option '%s'\n"), argv[last_optind]);
default:
- fprintf (stderr,
- _("%s: use the --help option for usage information\n"),
- program_name);
- xexit (1);
+ einfo (_("%P%F: use the --help option for usage information\n"));
+
case 1: /* File name. */
lang_add_input_file (optarg, lang_input_file_is_file_enum,
(char *) NULL);
optarg);
cplus_demangle_set_style (style);
- }
+ }
break;
+ case 'I': /* Used on Solaris. */
case OPTION_DYNAMIC_LINKER:
command_line.interpreter = optarg;
break;
break;
case OPTION_EXPORT_DYNAMIC:
case 'E': /* HP/UX compatibility. */
- command_line.export_dynamic = true;
+ link_info.export_dynamic = true;
break;
case 'e':
lang_add_entry (optarg, true);
config.magic_demand_paged = false;
config.dynamic_link = false;
break;
+ case OPTION_NO_DEFINE_COMMON:
+ command_line.inhibit_common_definition = true;
+ break;
case OPTION_NO_DEMANGLE:
demangling = false;
break;
case OPTION_NO_UNDEFINED:
link_info.no_undefined = true;
break;
+ case OPTION_ALLOW_SHLIB_UNDEFINED:
+ link_info.allow_shlib_undefined = true;
+ break;
+ case OPTION_ALLOW_MULTIPLE_DEFINITION:
+ link_info.allow_multiple_definition = true;
+ break;
case OPTION_NO_WARN_MISMATCH:
command_line.warn_mismatch = false;
break;
case OPTION_NOINHIBIT_EXEC:
force_make_executable = true;
break;
+ case OPTION_NOSTDLIB:
+ config.only_cmd_line_lib_dirs = true;
+ break;
case OPTION_NO_WHOLE_ARCHIVE:
whole_archive = false;
break;
break;
case 'i':
case 'r':
+ if (optind == last_optind)
+ /* This can happen if the user put "-rpath,a" on the command
+ line. (Or something similar. The comma is important).
+ Getopt becomes confused and thinks that this is a -r option
+ but it cannot parse the text after the -r so it refuses to
+ increment the optind counter. Detect this case and issue
+ an error message here. We cannot just make this a warning,
+ increment optind, and continue because getopt is too confused
+ and will seg-fault the next time around. */
+ einfo(_("%P%F: bad -rpath option\n"));
+
link_info.relocateable = true;
config.build_constructors = false;
config.magic_demand_paged = false;
/* Fall through. */
case OPTION_RPATH:
if (command_line.rpath == NULL)
- command_line.rpath = buystring (optarg);
+ command_line.rpath = xstrdup (optarg);
else
{
size_t rpath_len = strlen (command_line.rpath);
do
{
size_t idx = 0;
+
while (optarg[idx] != '\0' && optarg[idx] == cp[idx])
++idx;
if (optarg[idx] == '\0'
break;
case OPTION_RPATH_LINK:
if (command_line.rpath_link == NULL)
- command_line.rpath_link = buystring (optarg);
+ command_line.rpath_link = xstrdup (optarg);
else
{
char *buf;
/* Check for <something>=<somthing>... */
optarg2 = strchr (optarg, '=');
if (optarg2 == NULL)
- {
- fprintf (stderr,
- _("%s: Invalid argument to option \"--section-start\"\n"),
- program_name);
- xexit (1);
- }
+ einfo (_("%P%F: invalid argument to option \"--section-start\"\n"));
- optarg2 ++;
+ optarg2++;
/* So far so good. Are all the args present? */
if ((*optarg == '\0') || (*optarg2 == '\0'))
- {
- fprintf (stderr,
- _("%s: Missing argument(s) to option \"--section-start\"\n"),
- program_name);
- xexit (1);
- }
+ einfo (_("%P%F: missing argument(s) to option \"--section-start\"\n"));
/* We must copy the section name as set_section_start
doesn't do it for us. */
set_section_start (sec_name, optarg2);
}
break;
+ case OPTION_TARGET_HELP:
+ /* Mention any target specific options. */
+ ldemul_list_emulation_options (stdout);
+ exit (0);
case OPTION_TBSS:
set_section_start (".bss", optarg);
break;
ldlang_add_undef (optarg);
break;
case OPTION_UNIQUE:
- config.unique_orphan_sections = true;
+ if (optarg != NULL)
+ lang_add_unique (optarg);
+ else
+ config.unique_orphan_sections = true;
break;
case OPTION_VERBOSE:
ldversion (1);
version_printed = true;
break;
case OPTION_VERSION:
- /* This output is intended to follow the GNU standards document. */
- printf ("GNU ld %s\n", ld_program_version);
- printf (_("Copyright 2000 Free Software Foundation, Inc.\n"));
- printf (_("\
-This program is free software; you may redistribute it under the terms of\n\
-the GNU General Public License. This program has absolutely no warranty.\n"));
- {
- ld_emulation_xfer_type **ptr = ld_emulations;
-
- printf (_(" Supported emulations:\n"));
- while (*ptr)
- {
- printf (" %s\n", (*ptr)->emulation_name);
- ptr++;
- }
- }
+ ldversion (2);
xexit (0);
break;
case OPTION_VERSION_SCRIPT:
version information. Read it, but don't assume that
we've seen a linker script. */
{
- boolean hold_had_script;
+ FILE *hold_script_handle;
- hold_had_script = had_script;
+ hold_script_handle = saved_script_handle;
ldfile_open_command_file (optarg);
- had_script = hold_had_script;
+ saved_script_handle = hold_script_handle;
parser_input = input_version_script;
yyparse ();
}
case OPTION_WARN_CONSTRUCTORS:
config.warn_constructors = true;
break;
+ case OPTION_WARN_FATAL:
+ config.fatal_warnings = true;
+ break;
case OPTION_WARN_MULTIPLE_GP:
config.warn_multiple_gp = true;
break;
case OPTION_WRAP:
add_wrap (optarg);
break;
+ case OPTION_DISCARD_NONE:
+ link_info.discard = discard_none;
+ break;
case 'X':
link_info.discard = discard_l;
break;
case 'y':
add_ysym (optarg);
break;
+ case OPTION_SPARE_DYNAMIC_TAGS:
+ link_info.spare_dynamic_tags = strtoul (optarg, NULL, 0);
+ break;
case OPTION_SPLIT_BY_RELOC:
if (optarg != NULL)
config.split_by_reloc = strtoul (optarg, NULL, 0);
break;
case '(':
if (ingroup)
- {
- fprintf (stderr,
- _("%s: may not nest groups (--help for usage)\n"),
- program_name);
- xexit (1);
- }
+ einfo (_("%P%F: may not nest groups (--help for usage)\n"));
+
lang_enter_group ();
ingroup = 1;
break;
case ')':
if (! ingroup)
- {
- fprintf (stderr,
- _("%s: group ended before it began (--help for usage)\n"),
- program_name);
- xexit (1);
- }
+ einfo (_("%P%F: group ended before it began (--help for usage)\n"));
+
lang_leave_group ();
ingroup = 0;
break;
- case OPTION_MPC860C0:
- link_info.mpc860c0 = 20; /* default value (in bytes) */
- if (optarg)
- {
- unsigned words;
-
- words = is_num (optarg, 1, 10, 0);
- if (words == 0)
- {
- fprintf (stderr,
- _("%s: Invalid argument to option \"mpc860c0\"\n"),
- program_name);
- xexit (1);
- }
- link_info.mpc860c0 = words * 4; /* convert words to bytes */
- }
- command_line.relax = true;
- break;
+ case OPTION_MPC860C0:
+ /* Default value (in bytes). */
+ link_info.mpc860c0 = 20;
+ if (optarg)
+ {
+ unsigned words;
+
+ words = is_num (optarg, 1, 10, 0);
+ if (words == 0)
+ einfo (_("%P%F: invalid argument to option \"mpc860c0\"\n"));
+
+ /* Convert words to bytes. */
+ link_info.mpc860c0 = words * 4;
+ }
+ command_line.relax = true;
+ break;
case OPTION_INIT:
link_info.init_function = optarg;
static void
help ()
{
- int i;
+ unsigned i;
const char **targets, **pp;
printf (_("Usage: %s [options] file...\n"), program_name);
{
boolean comma;
int len;
- int j;
+ unsigned j;
printf (" ");
if (ld_options[j].opt.name != NULL
&& ld_options[j].control != NO_HELP)
{
+ int two_dashes =
+ (ld_options[j].control == TWO_DASHES
+ || ld_options[j].control == EXACTLY_TWO_DASHES);
+
printf ("%s-%s%s",
comma ? ", " : "",
- ld_options[j].control == TWO_DASHES ? "-" : "",
+ two_dashes ? "-" : "",
ld_options[j].opt.name);
len += ((comma ? 2 : 0)
+ 1
- + (ld_options[j].control == TWO_DASHES ? 1 : 0)
+ + (two_dashes ? 1 : 0)
+ strlen (ld_options[j].opt.name));
if (ld_options[j].arg != NULL)
{