Run "make dep-am" and regenerate
[binutils-gdb.git] / ld / ldlang.c
index 322ce5bc5d8707e39597fa7e97b3052533f73fcc..d97a55b084c26995af33427c0ecb25e5cbfe7120 100644 (file)
@@ -1,6 +1,6 @@
 /* Linker command language support.
    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
-   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
    Free Software Foundation, Inc.
 
    This file is part of the GNU Binutils.
@@ -20,8 +20,6 @@
    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
    MA 02110-1301, USA.  */
 
-#include <limits.h>
-
 #include "sysdep.h"
 #include "bfd.h"
 #include "libiberty.h"
@@ -64,6 +62,8 @@ static const char *current_target;
 static const char *output_target;
 static lang_statement_list_type statement_list;
 static struct bfd_hash_table lang_definedness_table;
+static lang_statement_list_type *stat_save[10];
+static lang_statement_list_type **stat_save_ptr = &stat_save[0];
 
 /* Forward declarations.  */
 static void exp_init_os (etree_type *);
@@ -925,6 +925,23 @@ lang_list_init (lang_statement_list_type *list)
   list->tail = &list->head;
 }
 
+void
+push_stat_ptr (lang_statement_list_type *new_ptr)
+{
+  if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0]))
+    abort ();
+  *stat_save_ptr++ = stat_ptr;
+  stat_ptr = new_ptr;
+}
+
+void
+pop_stat_ptr (void)
+{
+  if (stat_save_ptr <= stat_save)
+    abort ();
+  stat_ptr = *--stat_save_ptr;
+}
+
 /* Build a new statement node for the parse tree.  */
 
 static lang_statement_union_type *
@@ -1186,7 +1203,13 @@ lang_finish (void)
   In this case it is probably an error to create a region that has
   already been created.  If we are not inside a MEMORY block it is
   dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
-  and so we issue a warning.  */
+  and so we issue a warning.
+  
+  Each region has at least one name.  The first name is either
+  DEFAULT_MEMORY_REGION or the name given in the MEMORY block.  You can add
+  alias names to an existing region within a script with
+  REGION_ALIAS (alias, region_name).  Each name corresponds to at most one
+  region.  */
 
 static lang_memory_region_type *lang_memory_region_list;
 static lang_memory_region_type **lang_memory_region_list_tail
@@ -1195,28 +1218,31 @@ static lang_memory_region_type **lang_memory_region_list_tail
 lang_memory_region_type *
 lang_memory_region_lookup (const char *const name, bfd_boolean create)
 {
-  lang_memory_region_type *p;
+  lang_memory_region_name *n;
+  lang_memory_region_type *r;
   lang_memory_region_type *new;
 
   /* NAME is NULL for LMA memspecs if no region was specified.  */
   if (name == NULL)
     return NULL;
 
-  for (p = lang_memory_region_list; p != NULL; p = p->next)
-    if (strcmp (p->name, name) == 0)
-      {
-       if (create)
-         einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
-                name);
-       return p;
-      }
+  for (r = lang_memory_region_list; r != NULL; r = r->next)
+    for (n = &r->name_list; n != NULL; n = n->next)
+      if (strcmp (n->name, name) == 0)
+        {
+          if (create)
+            einfo (_("%P:%S: warning: redeclaration of memory region `%s'\n"),
+                   name);
+          return r;
+        }
 
   if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
-    einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
+    einfo (_("%P:%S: warning: memory region `%s' not declared\n"), name);
 
   new = stat_alloc (sizeof (lang_memory_region_type));
 
-  new->name = xstrdup (name);
+  new->name_list.name = xstrdup (name);
+  new->name_list.next = NULL;
   new->next = NULL;
   new->origin = 0;
   new->length = ~(bfd_size_type) 0;
@@ -1232,8 +1258,50 @@ lang_memory_region_lookup (const char *const name, bfd_boolean create)
   return new;
 }
 
+void
+lang_memory_region_alias (const char * alias, const char * region_name)
+{
+  lang_memory_region_name * n;
+  lang_memory_region_type * r;
+  lang_memory_region_type * region;
+
+  /* The default region must be unique.  This ensures that it is not necessary
+     to iterate through the name list if someone wants the check if a region is
+     the default memory region.  */
+  if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0
+      || strcmp (alias, DEFAULT_MEMORY_REGION) == 0)
+    einfo (_("%F%P:%S: error: alias for default memory region\n"));
+
+  /* Look for the target region and check if the alias is not already
+     in use.  */
+  region = NULL;
+  for (r = lang_memory_region_list; r != NULL; r = r->next)
+    for (n = &r->name_list; n != NULL; n = n->next)
+      {
+        if (region == NULL && strcmp (n->name, region_name) == 0)
+          region = r;
+        if (strcmp (n->name, alias) == 0)
+          einfo (_("%F%P:%S: error: redefinition of memory region "
+                   "alias `%s'\n"),
+                 alias);
+      }
+
+  /* Check if the target region exists.  */
+  if (region == NULL)
+    einfo (_("%F%P:%S: error: memory region `%s' "
+             "for alias `%s' does not exist\n"),
+           region_name,
+           alias);
+
+  /* Add alias to region name list.  */
+  n = stat_alloc (sizeof (lang_memory_region_name));
+  n->name = xstrdup (alias);
+  n->next = region->name_list.next;
+  region->name_list.next = n;
+}
+
 static lang_memory_region_type *
-lang_memory_default (asection *section)
+lang_memory_default (asection * section)
 {
   lang_memory_region_type *p;
 
@@ -1254,8 +1322,13 @@ lang_memory_default (asection *section)
   return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
 }
 
+/* Find or create an output_section_statement with the given NAME.
+   If CONSTRAINT is non-zero match one with that constraint, otherwise
+   match any non-negative constraint.  If CREATE, always make a
+   new output_section_statement for SPECIAL CONSTRAINT.  */
+
 lang_output_section_statement_type *
-lang_output_section_statement_lookup (const char *const name,
+lang_output_section_statement_lookup (const char *name,
                                      int constraint,
                                      bfd_boolean create)
 {
@@ -1276,8 +1349,8 @@ lang_output_section_statement_lookup (const char *const name,
       /* We have a section of this name, but it might not have the correct
         constraint.  */
       struct out_section_hash_entry *last_ent;
-      unsigned long hash = entry->root.hash;
 
+      name = entry->s.output_section_statement.name;
       if (create && constraint == SPECIAL)
        /* Not traversing to the end reverses the order of the second
           and subsequent SPECIAL sections in the hash table chain,
@@ -1286,17 +1359,15 @@ lang_output_section_statement_lookup (const char *const name,
       else
        do
          {
-           if (entry->s.output_section_statement.constraint >= 0
-               && (constraint == 0
-                   || (constraint
-                       == entry->s.output_section_statement.constraint)))
+           if (constraint == entry->s.output_section_statement.constraint
+               || (constraint == 0
+                   && entry->s.output_section_statement.constraint >= 0))
              return &entry->s.output_section_statement;
            last_ent = entry;
            entry = (struct out_section_hash_entry *) entry->root.next;
          }
        while (entry != NULL
-              && entry->root.hash == hash
-              && strcmp (name, entry->s.output_section_statement.name) == 0);
+              && name == entry->s.output_section_statement.name);
 
       if (!create)
        return NULL;
@@ -1320,6 +1391,36 @@ lang_output_section_statement_lookup (const char *const name,
   return &entry->s.output_section_statement;
 }
 
+/* Find the next output_section_statement with the same name as OS.
+   If CONSTRAINT is non-zero, find one with that constraint otherwise
+   match any non-negative constraint.  */
+
+lang_output_section_statement_type *
+next_matching_output_section_statement (lang_output_section_statement_type *os,
+                                       int constraint)
+{
+  /* All output_section_statements are actually part of a
+     struct out_section_hash_entry.  */
+  struct out_section_hash_entry *entry = (struct out_section_hash_entry *)
+    ((char *) os
+     - offsetof (struct out_section_hash_entry, s.output_section_statement));
+  const char *name = os->name;
+
+  ASSERT (name == entry->root.string);
+  do
+    {
+      entry = (struct out_section_hash_entry *) entry->root.next;
+      if (entry == NULL
+         || name != entry->s.output_section_statement.name)
+       return NULL;
+    }
+  while (constraint != entry->s.output_section_statement.constraint
+        && (constraint != 0
+            || entry->s.output_section_statement.constraint < 0));
+
+  return &entry->s.output_section_statement;
+}
+
 /* A variant of lang_output_section_find used by place_orphan.
    Returns the output statement that should precede a new output
    statement for SEC.  If an exact match is found on certain flags,
@@ -1586,27 +1687,30 @@ lang_insert_orphan (asection *s,
                    etree_type *address,
                    lang_statement_list_type *add_child)
 {
-  lang_statement_list_type *old;
   lang_statement_list_type add;
   const char *ps;
   lang_output_section_statement_type *os;
   lang_output_section_statement_type **os_tail;
 
-  /* Start building a list of statements for this section.
-     First save the current statement pointer.  */
-  old = stat_ptr;
-
   /* If we have found an appropriate place for the output section
      statements for this orphan, add them to our own private list,
      inserting them later into the global statement list.  */
   if (after != NULL)
     {
-      stat_ptr = &add;
-      lang_list_init (stat_ptr);
+      lang_list_init (&add);
+      push_stat_ptr (&add);
     }
 
+  if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
+    address = exp_intop (0);
+
+  os_tail = ((lang_output_section_statement_type **)
+            lang_output_section_statement.tail);
+  os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
+                                           NULL, constraint);
+
   ps = NULL;
-  if (config.build_constructors)
+  if (config.build_constructors && *os_tail == os)
     {
       /* If the name of the section is representable in C, then create
         symbols to mark the start and the end of the section.  */
@@ -1625,34 +1729,22 @@ lang_insert_orphan (asection *s,
                              exp_intop ((bfd_vma) 1 << s->alignment_power));
          lang_add_assignment (exp_assop ('=', ".", e_align));
          lang_add_assignment (exp_provide (symname,
-                                           exp_nameop (NAME, "."),
+                                           exp_unop (ABSOLUTE,
+                                                     exp_nameop (NAME, ".")),
                                            FALSE));
        }
     }
 
-  if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
-    address = exp_intop (0);
-
-  os_tail = ((lang_output_section_statement_type **)
-            lang_output_section_statement.tail);
-  os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
-                                           NULL, constraint);
-
   if (add_child == NULL)
     add_child = &os->children;
   lang_add_section (add_child, s, os);
 
   lang_leave_output_section_statement (0, "*default*", NULL, NULL);
 
-  if (config.build_constructors && *ps == '\0')
+  if (ps != NULL && *ps == '\0')
     {
       char *symname;
 
-      /* lang_leave_ouput_section_statement resets stat_ptr.
-        Put stat_ptr back where we want it.  */
-      if (after != NULL)
-       stat_ptr = &add;
-
       symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
       symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
       sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
@@ -1663,7 +1755,7 @@ lang_insert_orphan (asection *s,
 
   /* Restore the global list pointer.  */
   if (after != NULL)
-    stat_ptr = old;
+    pop_stat_ptr ();
 
   if (after != NULL && os->bfd_section != NULL)
     {
@@ -1749,8 +1841,8 @@ lang_insert_orphan (asection *s,
 
          /* Fix the global list pointer if we happened to tack our
             new list at the tail.  */
-         if (*old->tail == add.head)
-           old->tail = add.tail;
+         if (*stat_ptr->tail == add.head)
+           stat_ptr->tail = add.tail;
 
          /* Save the end of this list.  */
          place->stmt = add.tail;
@@ -1838,7 +1930,7 @@ lang_map (void)
       char buf[100];
       int len;
 
-      fprintf (config.map_file, "%-16s ", m->name);
+      fprintf (config.map_file, "%-16s ", m->name_list.name);
 
       sprintf_vma (buf, m->origin);
       minfo ("0x%s ", buf);
@@ -2481,8 +2573,6 @@ load_symbols (lang_input_statement_type *entry,
       && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
     {
       bfd_error_type err;
-      lang_statement_list_type *hold;
-      bfd_boolean bad_load = TRUE;
       bfd_boolean save_ldlang_sysrooted_script;
       bfd_boolean save_as_needed, save_add_needed;
 
@@ -2505,8 +2595,6 @@ load_symbols (lang_input_statement_type *entry,
       else if (err != bfd_error_file_not_recognized
               || place == NULL)
        einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
-      else
-       bad_load = FALSE;
 
       bfd_close (entry->the_bfd);
       entry->the_bfd = NULL;
@@ -2514,8 +2602,7 @@ load_symbols (lang_input_statement_type *entry,
       /* Try to interpret the file as a linker script.  */
       ldfile_open_command_file (entry->filename);
 
-      hold = stat_ptr;
-      stat_ptr = place;
+      push_stat_ptr (place);
       save_ldlang_sysrooted_script = ldlang_sysrooted_script;
       ldlang_sysrooted_script = entry->sysrooted;
       save_as_needed = as_needed;
@@ -2534,9 +2621,9 @@ load_symbols (lang_input_statement_type *entry,
       ldlang_sysrooted_script = save_ldlang_sysrooted_script;
       as_needed = save_as_needed;
       add_needed = save_add_needed;
-      stat_ptr = hold;
+      pop_stat_ptr ();
 
-      return ! bad_load;
+      return TRUE;
     }
 
   if (ldemul_recognized_file (entry))
@@ -3008,6 +3095,7 @@ open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
        case lang_input_statement_enum:
          if (s->input_statement.real)
            {
+             lang_statement_union_type **os_tail;
              lang_statement_list_type add;
 
              s->input_statement.target = current_target;
@@ -3023,6 +3111,7 @@ open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
                                       bfd_archive))
                s->input_statement.loaded = FALSE;
 
+             os_tail = lang_output_section_statement.tail;
              lang_list_init (&add);
 
              if (! load_symbols (&s->input_statement, &add))
@@ -3030,8 +3119,25 @@ open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
 
              if (add.head != NULL)
                {
-                 *add.tail = s->header.next;
-                 s->header.next = add.head;
+                 /* If this was a script with output sections then
+                    tack any added statements on to the end of the
+                    list.  This avoids having to reorder the output
+                    section statement list.  Very likely the user
+                    forgot -T, and whatever we do here will not meet
+                    naive user expectations.  */
+                 if (os_tail != lang_output_section_statement.tail)
+                   {
+                     einfo (_("%P: warning: %s contains output sections;"
+                              " did you forget -T?\n"),
+                            s->input_statement.filename);
+                     *stat_ptr->tail = add.head;
+                     stat_ptr->tail = add.tail;
+                   }
+                 else
+                   {
+                     *add.tail = s->header.next;
+                     s->header.next = add.head;
+                   }
                }
            }
          break;
@@ -3415,7 +3521,10 @@ process_insert_statements (void)
        {
          /* Keep pointers to the first and last output section
             statement in the sequence we may be about to move.  */
-         last_os = &(*s)->output_section_statement;
+         os = &(*s)->output_section_statement;
+
+         ASSERT (last_os == NULL || last_os->next == os);
+         last_os = os;
 
          /* Set constraint negative so that lang_output_section_find
             won't match this output section statement.  At this
@@ -4436,8 +4545,8 @@ lang_check_section_addresses (void)
      a bfd_vma quantity in decimal.  */
   for (m = lang_memory_region_list; m; m = m->next)
     if (m->had_full_message)
-      einfo (_("%X%P: region %s overflowed by %ld bytes\n"),
-            m->name, (long)(m->current - (m->origin + m->length)));
+      einfo (_("%X%P: region `%s' overflowed by %ld bytes\n"),
+            m->name_list.name, (long)(m->current - (m->origin + m->length)));
 
 }
 
@@ -4459,21 +4568,21 @@ os_region_check (lang_output_section_statement_type *os,
     {
       if (tree != NULL)
        {
-         einfo (_("%X%P: address 0x%v of %B section %s"
-                  " is not within region %s\n"),
+         einfo (_("%X%P: address 0x%v of %B section `%s'"
+                  " is not within region `%s'\n"),
                 region->current,
                 os->bfd_section->owner,
                 os->bfd_section->name,
-                region->name);
+                region->name_list.name);
        }
       else if (!region->had_full_message)
        {
          region->had_full_message = TRUE;
 
-         einfo (_("%X%P: %B section %s will not fit in region %s\n"),
+         einfo (_("%X%P: %B section `%s' will not fit in region `%s'\n"),
                 os->bfd_section->owner,
                 os->bfd_section->name,
-                region->name);
+                region->name_list.name);
        }
     }
 }
@@ -4562,8 +4671,8 @@ lang_size_sections_1
                       from the region specification.  */
                    if (os->region == NULL
                        || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
-                           && os->region->name[0] == '*'
-                           && strcmp (os->region->name,
+                           && os->region->name_list.name[0] == '*'
+                           && strcmp (os->region->name_list.name,
                                       DEFAULT_MEMORY_REGION) == 0))
                      {
                        os->region = lang_memory_default (os->bfd_section);
@@ -4576,10 +4685,10 @@ lang_size_sections_1
                        && !IGNORE_SECTION (os->bfd_section)
                        && ! link_info.relocatable
                        && check_regions
-                       && strcmp (os->region->name,
+                       && strcmp (os->region->name_list.name,
                                   DEFAULT_MEMORY_REGION) == 0
                        && lang_memory_region_list != NULL
-                       && (strcmp (lang_memory_region_list->name,
+                       && (strcmp (lang_memory_region_list->name_list.name,
                                    DEFAULT_MEMORY_REGION) != 0
                            || lang_memory_region_list->next != NULL)
                        && expld.phase != lang_mark_phase_enum)
@@ -4626,7 +4735,12 @@ lang_size_sections_1
                             os->name, (unsigned long) (newdot - savedot));
                  }
 
-               bfd_set_section_vma (0, os->bfd_section, newdot);
+               /* PR 6945: Do not update the vma's of output sections
+                  when performing a relocatable link on COFF objects.  */
+               if (! link_info.relocatable
+                   || (bfd_get_flavour (link_info.output_bfd)
+                       != bfd_target_coff_flavour))
+                 bfd_set_section_vma (0, os->bfd_section, newdot);
 
                os->bfd_section->output_offset = 0;
              }
@@ -5335,7 +5449,7 @@ lang_end (void)
   bfd_boolean warn;
 
   if ((link_info.relocatable && !link_info.gc_sections)
-      || link_info.shared)
+      || (link_info.shared && !link_info.executable))
     warn = entry_from_cmdline;
   else
     warn = TRUE;
@@ -5559,27 +5673,9 @@ lang_one_common (struct bfd_link_hash_entry *h, void *info)
     return TRUE;
 
   section = h->u.c.p->section;
-
-  /* Increase the size of the section to align the common sym.  */
-  section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
-  section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
-
-  /* Adjust the alignment if necessary.  */
-  if (power_of_two > section->alignment_power)
-    section->alignment_power = power_of_two;
-
-  /* Change the symbol from common to defined.  */
-  h->type = bfd_link_hash_defined;
-  h->u.def.section = section;
-  h->u.def.value = section->size;
-
-  /* Increase the size of the section.  */
-  section->size += size;
-
-  /* Make sure the section is allocated in memory, and make sure that
-     it is no longer a common section.  */
-  section->flags |= SEC_ALLOC;
-  section->flags &= ~SEC_IS_COMMON;
+  if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h))
+    einfo (_("%P%F: Could not define common symbol `%T': %E\n"),
+          h->root.string);
 
   if (config.map_file != NULL)
     {
@@ -5855,7 +5951,7 @@ lang_enter_output_section_statement (const char *output_section_statement_name,
   os->block_value = 1;
 
   /* Make next things chain into subchain of this.  */
-  stat_ptr = &os->children;
+  push_stat_ptr (&os->children);
 
   os->subsection_alignment =
     topower (exp_get_value_int (subalign, -1, "subsection alignment"));
@@ -6220,8 +6316,7 @@ lang_process (void)
   ldemul_finish ();
 
   /* Make sure that the section addresses make sense.  */
-  if (! link_info.relocatable
-      && command_line.check_section_addresses)
+  if (command_line.check_section_addresses)
     lang_check_section_addresses ();
 
   lang_end ();
@@ -6462,9 +6557,18 @@ lang_leave_output_section_statement (fill_type *fill, const char *memspec,
                    memspec, lma_memspec,
                    current_section->load_base != NULL,
                    current_section->addr_tree != NULL);
+
+  /* If this section has no load region or base, but has the same
+     region as the previous section, then propagate the previous
+     section's load region.  */
+
+  if (!current_section->lma_region && !current_section->load_base
+      && current_section->region == current_section->prev->region)
+    current_section->lma_region = current_section->prev->lma_region;
+  
   current_section->fill = fill;
   current_section->phdrs = phdrs;
-  stat_ptr = &statement_list;
+  pop_stat_ptr ();
 }
 
 /* Create an absolute symbol with the given name with the value of the
@@ -6581,7 +6685,7 @@ lang_enter_group (void)
 
   g = new_stat (lang_group_statement, stat_ptr);
   lang_list_init (&g->children);
-  stat_ptr = &g->children;
+  push_stat_ptr (&g->children);
 }
 
 /* Leave a group.  This just resets stat_ptr to start writing to the
@@ -6592,7 +6696,7 @@ lang_enter_group (void)
 void
 lang_leave_group (void)
 {
-  stat_ptr = &statement_list;
+  pop_stat_ptr ();
 }
 
 /* Add a new program header.  This is called for each entry in a PHDRS
@@ -6664,6 +6768,10 @@ lang_record_phdrs (void)
                  || (os->bfd_section->flags & SEC_ALLOC) == 0)
                continue;
 
+             /* Don't add orphans to PT_INTERP header.  */
+             if (l->type == 3)
+               continue;
+
              if (last == NULL)
                {
                  lang_output_section_statement_type * tmp_os;
@@ -6980,7 +7088,7 @@ lang_vers_match (struct bfd_elf_version_expr_head *head,
        java_sym = sym;
     }
 
-  if (head->htab && (prev == NULL || prev->symbol))
+  if (head->htab && (prev == NULL || prev->literal))
     {
       struct bfd_elf_version_expr e;
 
@@ -6989,9 +7097,9 @@ lang_vers_match (struct bfd_elf_version_expr_head *head,
        case 0:
          if (head->mask & BFD_ELF_VERSION_C_TYPE)
            {
-             e.symbol = sym;
+             e.pattern = sym;
              expr = htab_find (head->htab, &e);
-             while (expr && strcmp (expr->symbol, sym) == 0)
+             while (expr && strcmp (expr->pattern, sym) == 0)
                if (expr->mask == BFD_ELF_VERSION_C_TYPE)
                  goto out_ret;
                else
@@ -7001,9 +7109,9 @@ lang_vers_match (struct bfd_elf_version_expr_head *head,
        case BFD_ELF_VERSION_C_TYPE:
          if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
            {
-             e.symbol = cxx_sym;
+             e.pattern = cxx_sym;
              expr = htab_find (head->htab, &e);
-             while (expr && strcmp (expr->symbol, cxx_sym) == 0)
+             while (expr && strcmp (expr->pattern, cxx_sym) == 0)
                if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
                  goto out_ret;
                else
@@ -7013,9 +7121,9 @@ lang_vers_match (struct bfd_elf_version_expr_head *head,
        case BFD_ELF_VERSION_CXX_TYPE:
          if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
            {
-             e.symbol = java_sym;
+             e.pattern = java_sym;
              expr = htab_find (head->htab, &e);
-             while (expr && strcmp (expr->symbol, java_sym) == 0)
+             while (expr && strcmp (expr->pattern, java_sym) == 0)
                if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
                  goto out_ret;
                else
@@ -7028,7 +7136,7 @@ lang_vers_match (struct bfd_elf_version_expr_head *head,
     }
 
   /* Finally, try the wildcards.  */
-  if (prev == NULL || prev->symbol)
+  if (prev == NULL || prev->literal)
     expr = head->remaining;
   else
     expr = prev->next;
@@ -7061,7 +7169,7 @@ lang_vers_match (struct bfd_elf_version_expr_head *head,
 }
 
 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
-   return a string pointing to the symbol name.  */
+   return a pointer to the symbol name with any backslash quotes removed.  */
 
 static const char *
 realsymbol (const char *pattern)
@@ -7074,22 +7182,24 @@ realsymbol (const char *pattern)
     {
       /* It is a glob pattern only if there is no preceding
         backslash.  */
-      if (! backslash && (*p == '?' || *p == '*' || *p == '['))
-       {
-         free (symbol);
-         return NULL;
-       }
-
       if (backslash)
        {
          /* Remove the preceding backslash.  */
          *(s - 1) = *p;
+         backslash = FALSE;
          changed = TRUE;
        }
       else
-       *s++ = *p;
+       {
+         if (*p == '?' || *p == '*' || *p == '[')
+           {
+             free (symbol);
+             return NULL;
+           }
 
-      backslash = *p == '\\';
+         *s++ = *p;
+         backslash = *p == '\\';
+       }
     }
 
   if (changed)
@@ -7118,10 +7228,15 @@ lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
 
   ret = xmalloc (sizeof *ret);
   ret->next = orig;
-  ret->pattern = literal_p ? NULL : new;
   ret->symver = 0;
   ret->script = 0;
-  ret->symbol = literal_p ? new : realsymbol (new);
+  ret->literal = TRUE;
+  ret->pattern = literal_p ? new : realsymbol (new);
+  if (ret->pattern == NULL)
+    {
+      ret->pattern = new;
+      ret->literal = FALSE;
+    }
 
   if (lang == NULL || strcasecmp (lang, "C") == 0)
     ret->mask = BFD_ELF_VERSION_C_TYPE;
@@ -7165,7 +7280,7 @@ version_expr_head_hash (const void *p)
 {
   const struct bfd_elf_version_expr *e = p;
 
-  return htab_hash_string (e->symbol);
+  return htab_hash_string (e->pattern);
 }
 
 static int
@@ -7174,7 +7289,7 @@ version_expr_head_eq (const void *p1, const void *p2)
   const struct bfd_elf_version_expr *e1 = p1;
   const struct bfd_elf_version_expr *e2 = p2;
 
-  return strcmp (e1->symbol, e2->symbol) == 0;
+  return strcmp (e1->pattern, e2->pattern) == 0;
 }
 
 static void
@@ -7186,7 +7301,7 @@ lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
 
   for (e = head->list; e; e = e->next)
     {
-      if (e->symbol)
+      if (e->literal)
        count++;
       head->mask |= e->mask;
     }
@@ -7200,7 +7315,7 @@ lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
       for (e = head->list; e; e = next)
        {
          next = e->next;
-         if (!e->symbol)
+         if (!e->literal)
            {
              *remaining_loc = e;
              remaining_loc = &e->next;
@@ -7225,14 +7340,14 @@ lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
                      last = e1;
                      e1 = e1->next;
                    }
-                 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
+                 while (e1 && strcmp (e1->pattern, e->pattern) == 0);
 
                  if (last == NULL)
                    {
                      /* This is a duplicate.  */
                      /* FIXME: Memory leak.  Sometimes pattern is not
                         xmalloced alone, but in larger chunk of memory.  */
-                     /* free (e->symbol); */
+                     /* free (e->pattern); */
                      free (e);
                    }
                  else
@@ -7296,18 +7411,18 @@ lang_register_vers_node (const char *name,
        {
          struct bfd_elf_version_expr *e2;
 
-         if (t->locals.htab && e1->symbol)
+         if (t->locals.htab && e1->literal)
            {
              e2 = htab_find (t->locals.htab, e1);
-             while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
+             while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
                {
                  if (e1->mask == e2->mask)
                    einfo (_("%X%P: duplicate expression `%s'"
-                            " in version information\n"), e1->symbol);
+                            " in version information\n"), e1->pattern);
                  e2 = e2->next;
                }
            }
-         else if (!e1->symbol)
+         else if (!e1->literal)
            for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
              if (strcmp (e1->pattern, e2->pattern) == 0
                  && e1->mask == e2->mask)
@@ -7322,19 +7437,19 @@ lang_register_vers_node (const char *name,
        {
          struct bfd_elf_version_expr *e2;
 
-         if (t->globals.htab && e1->symbol)
+         if (t->globals.htab && e1->literal)
            {
              e2 = htab_find (t->globals.htab, e1);
-             while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
+             while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
                {
                  if (e1->mask == e2->mask)
                    einfo (_("%X%P: duplicate expression `%s'"
                             " in version information\n"),
-                          e1->symbol);
+                          e1->pattern);
                  e2 = e2->next;
                }
            }
-         else if (!e1->symbol)
+         else if (!e1->literal)
            for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
              if (strcmp (e1->pattern, e2->pattern) == 0
                  && e1->mask == e2->mask)