x86: respect {nooptimize} for LEA
[binutils-gdb.git] / ld / deffilep.y
index 3afb3ce3ce859ffcd370f53c99afbeadb3136c2e..66ce5d589ed79df32d8ae1bd5823a1fa4db5a064 100644 (file)
@@ -1,7 +1,6 @@
 %{ /* deffilep.y - parser for .def files */
 
-/*   Copyright 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006,
-     2007, 2009 Free Software Foundation, Inc.
+/*   Copyright (C) 1995-2023 Free Software Foundation, Inc.
 
      This file is part of GNU Binutils.
 
@@ -24,6 +23,7 @@
 #include "libiberty.h"
 #include "safe-ctype.h"
 #include "bfd.h"
+#include "bfdlink.h"
 #include "ld.h"
 #include "ldmisc.h"
 #include "deffile.h"
@@ -32,6 +32,8 @@
 
 #define ROUND_UP(a, b) (((a)+((b)-1))&~((b)-1))
 
+#define SYMBOL_LIST_ARRAY_GROW 64
+
 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
    as well as gratuitiously global symbol names, so we can have multiple
    yacc generated parsers in ld.  Note that these are only the variables
 #define        yylval  def_lval
 #define        yychar  def_char
 #define        yydebug def_debug
-#define        yypact  def_pact        
-#define        yyr1    def_r1                  
-#define        yyr2    def_r2                  
-#define        yydef   def_def         
-#define        yychk   def_chk         
-#define        yypgo   def_pgo         
-#define        yyact   def_act         
+#define        yypact  def_pact
+#define        yyr1    def_r1
+#define        yyr2    def_r2
+#define        yydef   def_def
+#define        yychk   def_chk
+#define        yypgo   def_pgo
+#define        yyact   def_act
 #define        yyexca  def_exca
 #define yyerrflag def_errflag
 #define yynerrs        def_nerrs
@@ -94,15 +96,16 @@ static void def_exports (const char *, const char *, int, int, const char *);
 static void def_heapsize (int, int);
 static void def_import (const char *, const char *, const char *, const char *,
                        int, const char *);
-static void def_image_name (const char *, int, int);
+static void def_image_name (const char *, bfd_vma, int);
 static void def_section (const char *, int);
 static void def_section_alt (const char *, const char *);
 static void def_stacksize (int, int);
 static void def_version (int, int);
 static void def_directive (char *);
 static void def_aligncomm (char *str, int align);
+static void def_exclude_symbols (char *str);
 static int def_parse (void);
-static int def_error (const char *);
+static void def_error (const char *);
 static int def_lex (void);
 
 static int lex_forced_token = 0;
@@ -115,21 +118,23 @@ static const char *lex_parse_string_end = 0;
   char *id;
   const char *id_const;
   int number;
+  bfd_vma vma;
   char *digits;
 };
 
 %token NAME LIBRARY DESCRIPTION STACKSIZE_K HEAPSIZE CODE DATAU DATAL
 %token SECTIONS EXPORTS IMPORTS VERSIONK BASE CONSTANTU CONSTANTL
-%token PRIVATEU PRIVATEL ALIGNCOMM
-%token READ WRITE EXECUTE SHARED NONAMEU NONAMEL DIRECTIVE EQUAL
+%token PRIVATEU PRIVATEL ALIGNCOMM EXCLUDE_SYMBOLS
+%token READ WRITE EXECUTE SHARED_K NONAMEU NONAMEL DIRECTIVE EQUAL
 %token <id> ID
 %token <digits> DIGITS
 %type  <number> NUMBER
+%type  <vma> VMA opt_base
 %type  <digits> opt_digits
-%type  <number> opt_base opt_ordinal
+%type  <number> opt_ordinal
 %type  <number> attr attr_list opt_number exp_opt_list exp_opt
 %type  <id> opt_name opt_name2 opt_equal_name anylang_id opt_id
-%type  <id> opt_equalequal_name
+%type  <id> opt_equalequal_name symbol_list
 %type  <id_const> keyword_as_name
 
 %%
@@ -138,7 +143,7 @@ start: start command
        | command
        ;
 
-command: 
+command:
                NAME opt_name opt_base { def_image_name ($2, $3, 0); }
        |       LIBRARY opt_name opt_base { def_image_name ($2, $3, 1); }
        |       DESCRIPTION ID { def_description ($2);}
@@ -147,12 +152,13 @@ command:
        |       CODE attr_list { def_section ("CODE", $2);}
        |       DATAU attr_list  { def_section ("DATA", $2);}
        |       SECTIONS seclist
-       |       EXPORTS explist 
+       |       EXPORTS explist
        |       IMPORTS implist
        |       VERSIONK NUMBER { def_version ($2, 0);}
        |       VERSIONK NUMBER '.' NUMBER { def_version ($2, $4);}
        |       DIRECTIVE ID { def_directive ($2);}
        |       ALIGNCOMM anylang_id ',' NUMBER { def_aligncomm ($2, $4);}
+       |       EXCLUDE_SYMBOLS symbol_list
        ;
 
 
@@ -186,24 +192,24 @@ exp_opt:
        |       PRIVATEU        { $$ = 8; }
        |       PRIVATEL        { $$ = 8; }
        ;
-implist:       
+implist:
                implist impline
        |       impline
        ;
 
 impline:
-               ID '=' ID '.' ID '.' ID opt_equalequal_name
-                 { def_import ($1, $3, $5, $7, -1, $8); }
+              ID '=' ID '.' ID '.' ID opt_equalequal_name
+                { def_import ($1, $3, $5, $7, -1, $8); }
        |       ID '=' ID '.' ID '.' NUMBER opt_equalequal_name
                                 { def_import ($1, $3, $5,  0, $7, $8); }
        |       ID '=' ID '.' ID opt_equalequal_name
-                 { def_import ($1, $3,  0, $5, -1, $6); }
+                { def_import ($1, $3,  0, $5, -1, $6); }
        |       ID '=' ID '.' NUMBER opt_equalequal_name
-                 { def_import ($1, $3,  0,  0, $5, $6); }
+                { def_import ($1, $3,  0,  0, $5, $6); }
        |       ID '.' ID '.' ID opt_equalequal_name
-                 { def_import( 0, $1, $3, $5, -1, $6); }
+                { def_import( 0, $1, $3, $5, -1, $6); }
        |       ID '.' ID opt_equalequal_name
-                 { def_import ( 0, $1,  0, $3, -1, $4); }
+                { def_import ( 0, $1,  0, $3, -1, $4); }
 ;
 
 seclist:
@@ -223,17 +229,17 @@ attr_list:
 
 opt_comma:
        ','
-       | 
+       |
        ;
 opt_number: ',' NUMBER { $$=$2;}
        |          { $$=-1;}
        ;
-       
+
 attr:
                READ    { $$ = 1;}
-       |       WRITE   { $$ = 2;}      
+       |       WRITE   { $$ = 2;}
        |       EXECUTE { $$=4;}
-       |       SHARED  { $$=8;}
+       |       SHARED_K { $$=8;}
        ;
 
 
@@ -245,6 +251,7 @@ keyword_as_name: BASE { $$ = "BASE"; }
         | DATAL { $$ = "data"; }
         | DESCRIPTION { $$ = "DESCRIPTION"; }
         | DIRECTIVE { $$ = "DIRECTIVE"; }
+        | EXCLUDE_SYMBOLS { $$ = "EXCLUDE_SYMBOLS"; }
         | EXECUTE { $$ = "EXECUTE"; }
         | EXPORTS { $$ = "EXPORTS"; }
         | HEAPSIZE { $$ = "HEAPSIZE"; }
@@ -261,7 +268,7 @@ keyword_as_name: BASE { $$ = "BASE"; }
         | PRIVATEU { $$ = "PRIVATE"; }
         | PRIVATEL { $$ = "private"; }
         | READ { $$ = "READ"; }
-        | SHARED  { $$ = "SHARED"; }
+        | SHARED_K  { $$ = "SHARED"; }
         | STACKSIZE_K { $$ = "STACKSIZE"; }
         | VERSIONK { $$ = "VERSION"; }
         | WRITE { $$ = "WRITE"; }
@@ -275,19 +282,19 @@ opt_name2: ID { $$ = $1; }
            $$ = name;
          }
        | '.' opt_name2
-         { 
+         {
            char *name = def_pool_alloc (strlen ($2) + 2);
            sprintf (name, ".%s", $2);
            $$ = name;
          }
        | keyword_as_name '.' opt_name2
-         { 
+         {
            char *name = def_pool_alloc (strlen ($1) + 1 + strlen ($3) + 1);
            sprintf (name, "%s.%s", $1, $3);
            $$ = name;
          }
        | ID '.' opt_name2
-         { 
+         {
            char *name = def_pool_alloc (strlen ($1) + 1 + strlen ($3) + 1);
            sprintf (name, "%s.%s", $1, $3);
            $$ = name;
@@ -302,18 +309,18 @@ opt_equalequal_name: EQUAL ID     { $$ = $2; }
        |                                                       { $$ = 0; }
        ;
 
-opt_ordinal: 
+opt_ordinal:
          '@' NUMBER     { $$ = $2;}
        |                { $$ = -1;}
        ;
 
 opt_equal_name:
-          '=' opt_name2        { $$ = $2; }
-        |              { $$ =  0; }                     
+         '=' opt_name2 { $$ = $2; }
+       |               { $$ =  0; }
        ;
 
-opt_base: BASE '=' NUMBER      { $$ = $3;}
-       |       { $$ = -1;}
+opt_base: BASE '=' VMA { $$ = $3;}
+       |       { $$ = (bfd_vma) -1;}
        ;
 
 anylang_id: ID         { $$ = $1; }
@@ -331,6 +338,12 @@ anylang_id: ID             { $$ = $1; }
          }
        ;
 
+symbol_list:
+       anylang_id { def_exclude_symbols ($1); }
+       |       symbol_list anylang_id { def_exclude_symbols ($2); }
+       |       symbol_list ',' anylang_id { def_exclude_symbols ($3); }
+       ;
+
 opt_digits: DIGITS     { $$ = $1; }
        |               { $$ = ""; }
        ;
@@ -340,6 +353,8 @@ opt_id: ID          { $$ = $1; }
        ;
 
 NUMBER: DIGITS         { $$ = strtoul ($1, 0, 0); }
+       ;
+VMA: DIGITS            { $$ = (bfd_vma) strtoull ($1, 0, 0); }
 
 %%
 
@@ -427,55 +442,40 @@ void
 def_file_free (def_file *fdef)
 {
   int i;
+  unsigned int ui;
 
   if (!fdef)
     return;
-  if (fdef->name)
-    free (fdef->name);
-  if (fdef->description)
-    free (fdef->description);
+  free (fdef->name);
+  free (fdef->description);
 
   if (fdef->section_defs)
     {
       for (i = 0; i < fdef->num_section_defs; i++)
        {
-         if (fdef->section_defs[i].name)
-           free (fdef->section_defs[i].name);
-         if (fdef->section_defs[i].class)
-           free (fdef->section_defs[i].class);
+         free (fdef->section_defs[i].name);
+         free (fdef->section_defs[i].class);
        }
       free (fdef->section_defs);
     }
 
-  if (fdef->exports)
+  for (i = 0; i < fdef->num_exports; i++)
     {
-      for (i = 0; i < fdef->num_exports; i++)
-       {
-         if (fdef->exports[i].internal_name
-             && fdef->exports[i].internal_name != fdef->exports[i].name)
-           free (fdef->exports[i].internal_name);
-         if (fdef->exports[i].name)
-           free (fdef->exports[i].name);
-         if (fdef->exports[i].its_name)
-           free (fdef->exports[i].its_name);
-       }
-      free (fdef->exports);
+      if (fdef->exports[i].internal_name != fdef->exports[i].name)
+        free (fdef->exports[i].internal_name);
+      free (fdef->exports[i].name);
+      free (fdef->exports[i].its_name);
     }
+  free (fdef->exports);
 
-  if (fdef->imports)
+  for (i = 0; i < fdef->num_imports; i++)
     {
-      for (i = 0; i < fdef->num_imports; i++)
-       {
-         if (fdef->imports[i].internal_name
-             && fdef->imports[i].internal_name != fdef->imports[i].name)
-           free (fdef->imports[i].internal_name);
-         if (fdef->imports[i].name)
-           free (fdef->imports[i].name);
-         if (fdef->imports[i].its_name)
-           free (fdef->imports[i].its_name);
-       }
-      free (fdef->imports);
+      if (fdef->imports[i].internal_name != fdef->imports[i].name)
+        free (fdef->imports[i].internal_name);
+      free (fdef->imports[i].name);
+      free (fdef->imports[i].its_name);
     }
+  free (fdef->imports);
 
   while (fdef->modules)
     {
@@ -494,6 +494,12 @@ def_file_free (def_file *fdef)
       free (c);
     }
 
+  for (ui = 0; ui < fdef->num_exclude_symbols; ui++)
+    {
+      free (fdef->exclude_symbols[ui].symbol_name);
+    }
+  free (fdef->exclude_symbols);
+
   free (fdef);
 }
 
@@ -509,7 +515,8 @@ def_file_print (FILE *file, def_file *fdef)
   if (fdef->is_dll != -1)
     fprintf (file, "  is dll: %s\n", fdef->is_dll ? "yes" : "no");
   if (fdef->base_address != (bfd_vma) -1)
-    fprintf (file, "  base address: 0x%08x\n", fdef->base_address);
+    fprintf (file, "  base address: 0x%" PRIx64 "\n",
+            (uint64_t) fdef->base_address);
   if (fdef->description)
     fprintf (file, "  description: `%s'\n", fdef->description);
   if (fdef->stack_reserve != -1)
@@ -614,22 +621,25 @@ cmp_export_elem (const def_file_export *e, const char *ex_name,
 
 /* Search the position of the identical element, or returns the position
    of the next higher element. If last valid element is smaller, then MAX
-   is returned.  */
+   is returned. The max parameter indicates the number of elements in the
+   array. On return, *is_ident indicates whether the returned array index
+   points at an element which is identical to the one searched for.  */
 
-static int
-find_export_in_list (def_file_export *b, int max,
+static unsigned int
+find_export_in_list (def_file_export *b, unsigned int max,
                     const char *ex_name, const char *in_name,
-                    const char *its_name, int ord, int *is_ident)
+                    const char *its_name, int ord, bool *is_ident)
 {
-  int e, l, r, p;
+  int e;
+  unsigned int l, r, p;
 
-  *is_ident = 0;
+  *is_ident = false;
   if (!max)
     return 0;
   if ((e = cmp_export_elem (b, ex_name, in_name, its_name, ord)) <= 0)
     {
       if (!e)
-        *is_ident = 1;
+       *is_ident = true;
       return 0;
     }
   if (max == 1)
@@ -639,7 +649,7 @@ find_export_in_list (def_file_export *b, int max,
   else if (!e || max == 2)
     {
       if (!e)
-       *is_ident = 1;
+       *is_ident = true;
       return max - 1;
     }
   l = 0; r = max - 1;
@@ -648,19 +658,19 @@ find_export_in_list (def_file_export *b, int max,
       p = (l + r) / 2;
       e = cmp_export_elem (b + p, ex_name, in_name, its_name, ord);
       if (!e)
-        {
-          *is_ident = 1;
-          return p;
-        }
+       {
+         *is_ident = true;
+         return p;
+       }
       else if (e < 0)
-        r = p - 1;
+       r = p - 1;
       else if (e > 0)
-        l = p + 1;
+       l = p + 1;
     }
   if ((e = cmp_export_elem (b + l, ex_name, in_name, its_name, ord)) > 0)
     ++l;
   else if (!e)
-    *is_ident = 1;
+    *is_ident = true;
   return l;
 }
 
@@ -670,11 +680,10 @@ def_file_add_export (def_file *fdef,
                     const char *internal_name,
                     int ordinal,
                     const char *its_name,
-                    int *is_dup)
+                    bool *is_dup)
 {
   def_file_export *e;
-  int pos;
-  int max_exports = ROUND_UP(fdef->num_exports, 32);
+  unsigned int pos;
 
   if (internal_name && !external_name)
     external_name = internal_name;
@@ -682,27 +691,27 @@ def_file_add_export (def_file *fdef,
     internal_name = external_name;
 
   /* We need to avoid duplicates.  */
-  *is_dup = 0;
+  *is_dup = false;
   pos = find_export_in_list (fdef->exports, fdef->num_exports,
                     external_name, internal_name,
                     its_name, ordinal, is_dup);
 
-  if (*is_dup != 0)
+  if (*is_dup)
     return (fdef->exports + pos);
 
-  if (fdef->num_exports >= max_exports)
+  if ((unsigned)fdef->num_exports >= fdef->max_exports)
     {
-      max_exports = ROUND_UP(fdef->num_exports + 1, 32);
-      if (fdef->exports)
-       fdef->exports = xrealloc (fdef->exports,
-                                max_exports * sizeof (def_file_export));
-      else
-       fdef->exports = xmalloc (max_exports * sizeof (def_file_export));
+      fdef->max_exports += SYMBOL_LIST_ARRAY_GROW;
+      fdef->exports = xrealloc (fdef->exports,
+                               fdef->max_exports * sizeof (def_file_export));
     }
 
   e = fdef->exports + pos;
-  if (pos != fdef->num_exports)
+  /* If we're inserting in the middle of the array, we need to move the
+     following elements forward.  */
+  if (pos != (unsigned)fdef->num_exports)
     memmove (&e[1], e, (sizeof (def_file_export) * (fdef->num_exports - pos)));
+  /* Wipe the element for use as a new entry.  */
   memset (e, 0, sizeof (def_file_export));
   e->name = xstrdup (external_name);
   e->internal_name = xstrdup (internal_name);
@@ -759,22 +768,25 @@ cmp_import_elem (const def_file_import *e, const char *ex_name,
 
 /* Search the position of the identical element, or returns the position
    of the next higher element. If last valid element is smaller, then MAX
-   is returned.  */
+   is returned. The max parameter indicates the number of elements in the
+   array. On return, *is_ident indicates whether the returned array index
+   points at an element which is identical to the one searched for.  */
 
-static int
-find_import_in_list (def_file_import *b, int max,
+static unsigned int
+find_import_in_list (def_file_import *b, unsigned int max,
                     const char *ex_name, const char *in_name,
-                    const char *module, int ord, int *is_ident)
+                    const char *module, int ord, bool *is_ident)
 {
-  int e, l, r, p;
+  int e;
+  unsigned int l, r, p;
 
-  *is_ident = 0;
+  *is_ident = false;
   if (!max)
     return 0;
   if ((e = cmp_import_elem (b, ex_name, in_name, module, ord)) <= 0)
     {
       if (!e)
-        *is_ident = 1;
+       *is_ident = true;
       return 0;
     }
   if (max == 1)
@@ -784,7 +796,7 @@ find_import_in_list (def_file_import *b, int max,
   else if (!e || max == 2)
     {
       if (!e)
-        *is_ident = 1;
+       *is_ident = true;
       return max - 1;
     }
   l = 0; r = max - 1;
@@ -793,22 +805,42 @@ find_import_in_list (def_file_import *b, int max,
       p = (l + r) / 2;
       e = cmp_import_elem (b + p, ex_name, in_name, module, ord);
       if (!e)
-        {
-          *is_ident = 1;
-          return p;
-        }
+       {
+         *is_ident = true;
+         return p;
+       }
       else if (e < 0)
-        r = p - 1;
+       r = p - 1;
       else if (e > 0)
-        l = p + 1;
+       l = p + 1;
     }
   if ((e = cmp_import_elem (b + l, ex_name, in_name, module, ord)) > 0)
     ++l;
   else if (!e)
-    *is_ident = 1;
+    *is_ident = true;
   return l;
 }
 
+static void
+fill_in_import (def_file_import *i,
+               const char *name,
+               def_file_module *module,
+               int ordinal,
+               const char *internal_name,
+               const char *its_name)
+{
+  memset (i, 0, sizeof (def_file_import));
+  if (name)
+    i->name = xstrdup (name);
+  i->module = module;
+  i->ordinal = ordinal;
+  if (internal_name)
+    i->internal_name = xstrdup (internal_name);
+  else
+    i->internal_name = i->name;
+  i->its_name = (its_name ? xstrdup (its_name) : NULL);
+}
+
 def_file_import *
 def_file_add_import (def_file *fdef,
                     const char *name,
@@ -816,50 +848,189 @@ def_file_add_import (def_file *fdef,
                     int ordinal,
                     const char *internal_name,
                     const char *its_name,
-                    int *is_dup)
+                    bool *is_dup)
 {
   def_file_import *i;
-  int pos;
-  int max_imports = ROUND_UP (fdef->num_imports, 16);
+  unsigned int pos;
 
   /* We need to avoid here duplicates.  */
-  *is_dup = 0;
+  *is_dup = false;
   pos = find_import_in_list (fdef->imports, fdef->num_imports,
                             name,
                             (!internal_name ? name : internal_name),
                             module, ordinal, is_dup);
-  if (*is_dup != 0)
+  if (*is_dup)
     return fdef->imports + pos;
 
-  if (fdef->num_imports >= max_imports)
+  if ((unsigned)fdef->num_imports >= fdef->max_imports)
     {
-      max_imports = ROUND_UP (fdef->num_imports+1, 16);
+      fdef->max_imports += SYMBOL_LIST_ARRAY_GROW;
+      fdef->imports = xrealloc (fdef->imports,
+                               fdef->max_imports * sizeof (def_file_import));
+    }
+  i = fdef->imports + pos;
+  /* If we're inserting in the middle of the array, we need to move the
+     following elements forward.  */
+  if (pos != (unsigned)fdef->num_imports)
+    memmove (i + 1, i, sizeof (def_file_import) * (fdef->num_imports - pos));
 
-      if (fdef->imports)
-       fdef->imports = xrealloc (fdef->imports,
-                                max_imports * sizeof (def_file_import));
-      else
-       fdef->imports = xmalloc (max_imports * sizeof (def_file_import));
+  fill_in_import (i, name, def_stash_module (fdef, module), ordinal,
+                 internal_name, its_name);
+  fdef->num_imports++;
+
+  return i;
+}
+
+int
+def_file_add_import_from (def_file *fdef,
+                         int num_imports,
+                         const char *name,
+                         const char *module,
+                         int ordinal,
+                         const char *internal_name,
+                         const char *its_name ATTRIBUTE_UNUSED)
+{
+  def_file_import *i;
+  bool is_dup;
+  unsigned int pos;
+
+  /* We need to avoid here duplicates.  */
+  is_dup = false;
+  pos = find_import_in_list (fdef->imports, fdef->num_imports,
+                            name, internal_name ? internal_name : name,
+                            module, ordinal, &is_dup);
+  if (is_dup)
+    return -1;
+  if (fdef->imports && pos != (unsigned)fdef->num_imports)
+    {
+      i = fdef->imports + pos;
+      if (i->module && strcmp (i->module->name, module) == 0)
+       return -1;
+    }
+
+  if ((unsigned)fdef->num_imports + num_imports - 1 >= fdef->max_imports)
+    {
+      fdef->max_imports = fdef->num_imports + num_imports +
+                         SYMBOL_LIST_ARRAY_GROW;
+
+      fdef->imports = xrealloc (fdef->imports,
+                               fdef->max_imports * sizeof (def_file_import));
     }
   i = fdef->imports + pos;
-  if (pos != fdef->num_imports)
-    memmove (&i[1], i, (sizeof (def_file_import) * (fdef->num_imports - pos)));
-  memset (i, 0, sizeof (def_file_import));
-  if (name)
-    i->name = xstrdup (name);
-  if (module)
-    i->module = def_stash_module (fdef, module);
-  i->ordinal = ordinal;
-  if (internal_name)
-    i->internal_name = xstrdup (internal_name);
-  else
-    i->internal_name = i->name;
-  i->its_name = (its_name ? xstrdup (its_name) : NULL);
+  /* If we're inserting in the middle of the array, we need to move the
+     following elements forward.  */
+  if (pos != (unsigned)fdef->num_imports)
+    memmove (i + num_imports, i,
+            sizeof (def_file_import) * (fdef->num_imports - pos));
+
+  return pos;
+}
+
+def_file_import *
+def_file_add_import_at (def_file *fdef,
+                       int pos,
+                       const char *name,
+                       const char *module,
+                       int ordinal,
+                       const char *internal_name,
+                       const char *its_name)
+{
+  def_file_import *i = fdef->imports + pos;
+
+  fill_in_import (i, name, def_stash_module (fdef, module), ordinal,
+                 internal_name, its_name);
   fdef->num_imports++;
 
   return i;
 }
 
+/* Search the position of the identical element, or returns the position
+   of the next higher element. If last valid element is smaller, then MAX
+   is returned. The max parameter indicates the number of elements in the
+   array. On return, *is_ident indicates whether the returned array index
+   points at an element which is identical to the one searched for.  */
+
+static unsigned int
+find_exclude_in_list (def_file_exclude_symbol *b, unsigned int max,
+                     const char *name, bool *is_ident)
+{
+  int e;
+  unsigned int l, r, p;
+
+  *is_ident = false;
+  if (!max)
+    return 0;
+  if ((e = strcmp (b[0].symbol_name, name)) <= 0)
+    {
+      if (!e)
+       *is_ident = true;
+      return 0;
+    }
+  if (max == 1)
+    return 1;
+  if ((e = strcmp (b[max - 1].symbol_name, name)) > 0)
+    return max;
+  else if (!e || max == 2)
+    {
+      if (!e)
+       *is_ident = true;
+      return max - 1;
+    }
+  l = 0; r = max - 1;
+  while (l < r)
+    {
+      p = (l + r) / 2;
+      e = strcmp (b[p].symbol_name, name);
+      if (!e)
+       {
+         *is_ident = true;
+         return p;
+       }
+      else if (e < 0)
+       r = p - 1;
+      else if (e > 0)
+       l = p + 1;
+    }
+  if ((e = strcmp (b[l].symbol_name, name)) > 0)
+    ++l;
+  else if (!e)
+    *is_ident = true;
+  return l;
+}
+
+static def_file_exclude_symbol *
+def_file_add_exclude_symbol (def_file *fdef, const char *name)
+{
+  def_file_exclude_symbol *e;
+  unsigned pos;
+  bool is_dup = false;
+
+  pos = find_exclude_in_list (fdef->exclude_symbols, fdef->num_exclude_symbols,
+                             name, &is_dup);
+
+  /* We need to avoid duplicates.  */
+  if (is_dup)
+    return (fdef->exclude_symbols + pos);
+
+  if (fdef->num_exclude_symbols >= fdef->max_exclude_symbols)
+    {
+      fdef->max_exclude_symbols += SYMBOL_LIST_ARRAY_GROW;
+      fdef->exclude_symbols = xrealloc (fdef->exclude_symbols,
+                                       fdef->max_exclude_symbols * sizeof (def_file_exclude_symbol));
+    }
+
+  e = fdef->exclude_symbols + pos;
+  /* If we're inserting in the middle of the array, we need to move the
+     following elements forward.  */
+  if (pos != fdef->num_exclude_symbols)
+    memmove (&e[1], e, (sizeof (def_file_exclude_symbol) * (fdef->num_exclude_symbols - pos)));
+  /* Wipe the element for use as a new entry.  */
+  memset (e, 0, sizeof (def_file_exclude_symbol));
+  e->symbol_name = xstrdup (name);
+  fdef->num_exclude_symbols++;
+  return e;
+}
+
 struct
 {
   char *param;
@@ -872,6 +1043,7 @@ diropts[] =
   { "-attr", SECTIONS },
   { "-export", EXPORTS },
   { "-aligncomm", ALIGNCOMM },
+  { "-exclude-symbols", EXCLUDE_SYMBOLS },
   { 0, 0 }
 };
 
@@ -895,10 +1067,10 @@ def_file_add_directive (def_file *my_def, const char *param, int len)
        break;
 
       /* Scan forward until we encounter any of:
-          - the end of the buffer
+         - the end of the buffer
          - the start of a new option
-         - a newline seperating options
-          - a NUL seperating options.  */
+         - a newline separating options
+         - a NUL separating options.  */
       for (tend = (char *) (param + 1);
           (tend < pend
            && !(ISSPACE (tend[-1]) && *tend == '-')
@@ -926,13 +1098,20 @@ def_file_add_directive (def_file *my_def, const char *param, int len)
 
       if (!diropts[i].param)
        {
-         char saved;
+         if (tend < pend)
+           {
+             char saved;
 
-         saved = * tend;
-         * tend = 0;
-         /* xgettext:c-format */
-         einfo (_("Warning: .drectve `%s' unrecognized\n"), param);
-         * tend = saved;
+             saved = * tend;
+             * tend = 0;
+             /* xgettext:c-format */
+             einfo (_("Warning: .drectve `%s' unrecognized\n"), param);
+             * tend = saved;
+           }
+         else
+           {
+             einfo (_("Warning: corrupt .drectve at end of def file\n"));
+           }
        }
 
       lex_parse_string = 0;
@@ -946,7 +1125,7 @@ def_file_add_directive (def_file *my_def, const char *param, int len)
 /* Parser Callbacks.  */
 
 static void
-def_image_name (const char *name, int base, int is_dll)
+def_image_name (const char *name, bfd_vma base, int is_dll)
 {
   /* If a LIBRARY or NAME statement is specified without a name, there is nothing
      to do here.  We retain the output filename specified on command line.  */
@@ -958,16 +1137,15 @@ def_image_name (const char *name, int base, int is_dll)
        einfo ("%s:%d: Warning: path components stripped from %s, '%s'\n",
               def_filename, linenumber, is_dll ? "LIBRARY" : "NAME",
               name);
-      if (def->name)
-       free (def->name);
-      /* Append the default suffix, if none specified.  */ 
+      free (def->name);
+      /* Append the default suffix, if none specified.  */
       if (strchr (image_name, '.') == 0)
        {
          const char * suffix = is_dll ? ".dll" : ".exe";
 
          def->name = xmalloc (strlen (image_name) + strlen (suffix) + 1);
          sprintf (def->name, "%s%s", image_name, suffix);
-        }
+       }
       else
        def->name = xstrdup (image_name);
     }
@@ -1078,7 +1256,7 @@ def_exports (const char *external_name,
             const char *its_name)
 {
   def_file_export *dfe;
-  int is_dup = 0;
+  bool is_dup = false;
 
   if (!internal_name && external_name)
     internal_name = external_name;
@@ -1114,8 +1292,8 @@ def_import (const char *internal_name,
 {
   char *buf = 0;
   const char *ext = dllext ? dllext : "dll";
-  int is_dup = 0;
-   
+  bool is_dup = false;
+
   buf = xmalloc (strlen (module) + strlen (ext) + 2);
   sprintf (buf, "%s.%s", module, ext);
   module = buf;
@@ -1147,7 +1325,7 @@ static void
 def_aligncomm (char *str, int align)
 {
   def_file_aligncomm *c, *p;
-  
+
   p = NULL;
   c = def->aligncomms;
   while (c != NULL)
@@ -1162,7 +1340,7 @@ def_aligncomm (char *str, int align)
            return;
        }
       if (e > 0)
-        break;
+       break;
       c = (p = c)->next;
     }
 
@@ -1181,12 +1359,17 @@ def_aligncomm (char *str, int align)
     }
 }
 
-static int
+static void
+def_exclude_symbols (char *str)
+{
+  def_file_add_exclude_symbol (def, str);
+}
+
+static void
 def_error (const char *err)
 {
   einfo ("%P: %s:%d: %s\n",
         def_filename ? def_filename : "<unknown-file>", linenumber, err);
-  return 0;
 }
 
 
@@ -1230,6 +1413,7 @@ tokens[] =
   { "data", DATAL },
   { "DESCRIPTION", DESCRIPTION },
   { "DIRECTIVE", DIRECTIVE },
+  { "EXCLUDE_SYMBOLS", EXCLUDE_SYMBOLS },
   { "EXECUTE", EXECUTE },
   { "EXPORTS", EXPORTS },
   { "HEAPSIZE", HEAPSIZE },
@@ -1243,7 +1427,7 @@ tokens[] =
   { "READ", READ },
   { "SECTIONS", SECTIONS },
   { "SEGMENTS", SECTIONS },
-  { "SHARED", SHARED },
+  { "SHARED", SHARED_K },
   { "STACKSIZE", STACKSIZE_K },
   { "VERSION", VERSIONK },
   { "WRITE", WRITE },
@@ -1353,12 +1537,12 @@ def_lex (void)
 
       if (q == '@')
        {
-          if (ISBLANK (c) ) /* '@' followed by whitespace.  */
+         if (ISBLANK (c) ) /* '@' followed by whitespace.  */
            return (q);
-          else if (ISDIGIT (c)) /* '@' followed by digit.  */
-            {
+         else if (ISDIGIT (c)) /* '@' followed by digit.  */
+           {
              def_ungetc (c);
-              return (q);
+             return (q);
            }
 #if TRACE
          printf ("lex: @ returns itself\n");
@@ -1374,7 +1558,7 @@ def_lex (void)
        def_ungetc (c);
       if (ISALPHA (q)) /* Check for tokens.  */
        {
-          for (i = 0; tokens[i].name; i++)
+         for (i = 0; tokens[i].name; i++)
            if (strcmp (tokens[i].name, buffer) == 0)
              {
 #if TRACE
@@ -1412,12 +1596,12 @@ def_lex (void)
     {
       c = def_getc ();
       if (c == '=')
-        {
+       {
 #if TRACE
-          printf ("lex: `==' returns EQUAL\n");
+         printf ("lex: `==' returns EQUAL\n");
 #endif
-                 return EQUAL;
-        }
+         return EQUAL;
+       }
       def_ungetc (c);
 #if TRACE
       printf ("lex: `=' returns itself\n");