Don't try to read past end of info buffer, and correct test results.
[deliverable/binutils-gdb.git] / ld / lexsup.c
index 35ed3c0913d0b10f917496562f9d1d022a75bb9c..3664a85c64b8b0abfd78270fa851535d29037ab6 100644 (file)
@@ -1,5 +1,6 @@
 /* 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
    Free Software Foundation, Inc.
 
 This file is part of GLD, the Gnu Linker.
@@ -127,11 +128,13 @@ int parsing_defsym = 0;
 #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)
 
 /* The long options.  This structure is used for both the option
    parsing and the help text.  */
 
-struct ld_option {
+struct ld_option
+{
   /* The long option information.  */
   struct option opt;
   /* The short option with the same meaning ('\0' if none).  */
@@ -146,12 +149,23 @@ struct ld_option {
     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[] = {
+static const struct ld_option ld_options[] =
+{
   { {NULL, required_argument, NULL, '\0'},
       'a', N_("KEYWORD"), N_("Shared library control for HP/UX compatibility"),
       ONE_DASH },
@@ -197,9 +211,9 @@ static const struct ld_option ld_options[] = {
       '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},
@@ -223,8 +237,8 @@ static const struct ld_option ld_options[] = {
       '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},
@@ -298,6 +312,8 @@ static const struct ld_option ld_options[] = {
       '\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 },
   { {"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},
@@ -307,7 +323,7 @@ static const struct ld_option ld_options[] = {
   { {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
       '\0', NULL, NULL, NO_HELP },
   { {"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},
@@ -377,7 +393,7 @@ static const struct ld_option ld_options[] = {
       '\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.  */
@@ -408,14 +424,16 @@ is_num (string, min, max, err)
 
 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
@@ -425,6 +443,7 @@ parse_args (argc, argv)
   shortopts[0] = '-';
   is = 1;
   il = 0;
+  irl = 0;
   for (i = 0; i < OPTION_COUNT; i++)
     {
       if (ld_options[i].shortopt != '\0')
@@ -445,12 +464,21 @@ parse_args (argc, argv)
        }
       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
@@ -516,12 +544,22 @@ parse_args (argc, argv)
 
       /* getopt_long_only is like getopt_long, but '-' as well as '--'
         can indicate a long option.  */
+      opterr = 0;
       optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
+      if (optc == '?')
+       {
+         --optind;
+         optc = getopt_long (argc, argv, shortopts, really_longopts, &longind);
+       }
 
       if (optc == -1)
        break;
+
       switch (optc)
        {
+       case '?':
+         fprintf (stderr, _("%s: unrecognized option '%s'\n"),
+                  program_name, argv[optind - 1]);
        default:
          fprintf (stderr,
                   _("%s: use the --help option for usage information\n"),
@@ -709,6 +747,9 @@ parse_args (argc, argv)
        case OPTION_NO_UNDEFINED:
          link_info.no_undefined = true;
          break;
+       case OPTION_ALLOW_SHLIB_UNDEFINED:
+         link_info.allow_shlib_undefined = true;
+         break;
        case OPTION_NO_WARN_MISMATCH:
          command_line.warn_mismatch = false;
          break;
@@ -925,7 +966,10 @@ parse_args (argc, argv)
          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);
@@ -943,7 +987,7 @@ parse_args (argc, argv)
        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 (_("Copyright 2001 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"));
@@ -1129,7 +1173,7 @@ set_section_start (sect, valstr)
 static void
 help ()
 {
-  int i;
+  unsigned i;
   const char **targets, **pp;
 
   printf (_("Usage: %s [options] file...\n"), program_name);
@@ -1141,7 +1185,7 @@ help ()
        {
          boolean comma;
          int len;
-         int j;
+         unsigned j;
 
          printf ("  ");
 
@@ -1178,13 +1222,17 @@ help ()
              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)
                    {
This page took 0.032163 seconds and 4 git commands to generate.