2007-02-08 Jeff Johnston <jjohnstn@redhat.com>
[deliverable/binutils-gdb.git] / ld / ldlang.c
index f376652dae2d7e85040f36ea0270c668ec9ae8ac..8a69c7028ded822501cbce1da019115cc1f11088 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
+   2001, 2002, 2003, 2004, 2005, 2006, 2007
    Free Software Foundation, Inc.
 
    This file is part of GLD, the Gnu Linker.
@@ -62,19 +62,15 @@ static lang_input_statement_type *first_file;
 static const char *current_target;
 static const char *output_target;
 static lang_statement_list_type statement_list;
-static struct lang_phdr *lang_phdr_list;
 static struct bfd_hash_table lang_definedness_table;
 
 /* Forward declarations.  */
 static void exp_init_os (etree_type *);
 static void init_map_userdata (bfd *, asection *, void *);
 static lang_input_statement_type *lookup_name (const char *);
-static bfd_boolean load_symbols (lang_input_statement_type *,
-                                lang_statement_list_type *);
 static struct bfd_hash_entry *lang_definedness_newfunc
  (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
 static void insert_undefined (const char *);
-static void print_all_symbols (asection *);
 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
 static void print_statement (lang_statement_union_type *,
                             lang_output_section_statement_type *);
@@ -85,6 +81,8 @@ static void print_input_section (asection *);
 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
 static void lang_record_phdrs (void);
 static void lang_do_version_exports_section (void);
+static void lang_finalize_version_expr_head
+  (struct bfd_elf_version_expr_head *);
 
 /* Exported variables.  */
 lang_output_section_statement_type *abs_output_section;
@@ -99,6 +97,7 @@ bfd_boolean lang_has_input_file = FALSE;
 bfd_boolean had_output_filename = FALSE;
 bfd_boolean lang_float_flag = FALSE;
 bfd_boolean delete_output_file_on_failure = FALSE;
+struct lang_phdr *lang_phdr_list;
 struct lang_nocrossrefs *nocrossref_list;
 static struct unique_sections *unique_section_list;
 static bfd_boolean ldlang_sysrooted_script = FALSE;
@@ -280,7 +279,7 @@ find_section (lang_input_statement_type *file,
 {
   section_iterator_callback_data cb_data = { NULL, FALSE };
 
-  bfd_get_section_by_name_if (file->the_bfd, sec->spec.name, 
+  bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
                              section_iterator_callback, &cb_data);
   *multiple_sections_found = cb_data.multiple_sections_found;
   return cb_data.found_section;
@@ -317,6 +316,124 @@ match_simple_wild (const char *pattern, const char *name)
   return TRUE;
 }
 
+/* Compare sections ASEC and BSEC according to SORT.  */
+
+static int
+compare_section (sort_type sort, asection *asec, asection *bsec)
+{
+  int ret;
+
+  switch (sort)
+    {
+    default:
+      abort ();
+
+    case by_alignment_name:
+      ret = (bfd_section_alignment (bsec->owner, bsec)
+            - bfd_section_alignment (asec->owner, asec));
+      if (ret)
+       break;
+      /* Fall through.  */
+
+    case by_name:
+      ret = strcmp (bfd_get_section_name (asec->owner, asec),
+                   bfd_get_section_name (bsec->owner, bsec));
+      break;
+
+    case by_name_alignment:
+      ret = strcmp (bfd_get_section_name (asec->owner, asec),
+                   bfd_get_section_name (bsec->owner, bsec));
+      if (ret)
+       break;
+      /* Fall through.  */
+
+    case by_alignment:
+      ret = (bfd_section_alignment (bsec->owner, bsec)
+            - bfd_section_alignment (asec->owner, asec));
+      break;
+    }
+
+  return ret;
+}
+
+/* Build a Binary Search Tree to sort sections, unlike insertion sort
+   used in wild_sort(). BST is considerably faster if the number of
+   of sections are large.  */
+
+static lang_section_bst_type **
+wild_sort_fast (lang_wild_statement_type *wild,
+               struct wildcard_list *sec,
+               lang_input_statement_type *file ATTRIBUTE_UNUSED,
+               asection *section)
+{
+  lang_section_bst_type **tree;
+
+  tree = &wild->tree;
+  if (!wild->filenames_sorted
+      && (sec == NULL || sec->spec.sorted == none))
+    {
+      /* Append at the right end of tree.  */
+      while (*tree)
+       tree = &((*tree)->right);
+      return tree;
+    }
+
+  while (*tree)
+    {
+      /* Find the correct node to append this section.  */
+      if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
+       tree = &((*tree)->left);
+      else
+       tree = &((*tree)->right);
+    }
+
+  return tree;
+}
+
+/* Use wild_sort_fast to build a BST to sort sections.  */
+
+static void
+output_section_callback_fast (lang_wild_statement_type *ptr,
+                             struct wildcard_list *sec,
+                             asection *section,
+                             lang_input_statement_type *file,
+                             void *output ATTRIBUTE_UNUSED)
+{
+  lang_section_bst_type *node;
+  lang_section_bst_type **tree;
+
+  if (unique_section_p (section))
+    return;
+
+  node = xmalloc (sizeof (lang_section_bst_type));
+  node->left = 0;
+  node->right = 0;
+  node->section = section;
+
+  tree = wild_sort_fast (ptr, sec, file, section);
+  if (tree != NULL)
+    *tree = node;
+}
+
+/* Convert a sorted sections' BST back to list form.  */
+
+static void
+output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
+                                     lang_section_bst_type *tree,
+                                     void *output)
+{
+  if (tree->left)
+    output_section_callback_tree_to_list (ptr, tree->left, output);
+
+  lang_add_section (&ptr->children, tree->section,
+                   (lang_output_section_statement_type *) output);
+
+  if (tree->right)
+    output_section_callback_tree_to_list (ptr, tree->right, output);
+
+  free (tree);
+}
+
 /* Specialized, optimized routines for handling different kinds of
    wildcards */
 
@@ -544,6 +661,11 @@ analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
   int data_counter;
 
   ptr->walk_wild_section_handler = walk_wild_section_general;
+  ptr->handler_data[0] = NULL;
+  ptr->handler_data[1] = NULL;
+  ptr->handler_data[2] = NULL;
+  ptr->handler_data[3] = NULL;
+  ptr->tree = NULL;
 
   /* Count how many wildcard_specs there are, and how many of those
      actually use wildcards in the name.  Also, bail out if any of the
@@ -669,7 +791,7 @@ walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
     {
       LANG_FOR_EACH_INPUT_STATEMENT (f)
        {
-         if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
+         if (fnmatch (file_spec, f->filename, 0) == 0)
            walk_wild_file (s, f, callback, data);
        }
     }
@@ -870,26 +992,26 @@ lang_add_input_file (const char *name,
   return new_afile (name, file_type, target, TRUE);
 }
 
-struct output_statement_hash_entry
+struct out_section_hash_entry
 {
   struct bfd_hash_entry root;
-  lang_output_section_statement_type os;
+  lang_statement_union_type s;
 };
 
 /* The hash table.  */
 
-static struct bfd_hash_table output_statement_table;
+static struct bfd_hash_table output_section_statement_table;
 
 /* Support routines for the hash table used by lang_output_section_find,
    initialize the table, fill in an entry and remove the table.  */
 
 static struct bfd_hash_entry *
-output_statement_newfunc (struct bfd_hash_entry *entry, 
-                         struct bfd_hash_table *table,
-                         const char *string)
+output_section_statement_newfunc (struct bfd_hash_entry *entry,
+                                 struct bfd_hash_table *table,
+                                 const char *string)
 {
   lang_output_section_statement_type **nextp;
-  struct output_statement_hash_entry *ret;
+  struct out_section_hash_entry *ret;
 
   if (entry == NULL)
     {
@@ -902,49 +1024,48 @@ output_statement_newfunc (struct bfd_hash_entry *entry,
   if (entry == NULL)
     return entry;
 
-  ret = (struct output_statement_hash_entry *) entry;
-  memset (&ret->os, 0, sizeof (ret->os));
-  ret->os.header.type = lang_output_section_statement_enum;
-  ret->os.subsection_alignment = -1;
-  ret->os.section_alignment = -1;
-  ret->os.block_value = 1;
-  lang_list_init (&ret->os.children);
-  lang_statement_append (stat_ptr,
-                        (lang_statement_union_type *) &ret->os,
-                        &ret->os.header.next);
+  ret = (struct out_section_hash_entry *) entry;
+  memset (&ret->s, 0, sizeof (ret->s));
+  ret->s.header.type = lang_output_section_statement_enum;
+  ret->s.output_section_statement.subsection_alignment = -1;
+  ret->s.output_section_statement.section_alignment = -1;
+  ret->s.output_section_statement.block_value = 1;
+  lang_list_init (&ret->s.output_section_statement.children);
+  lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
 
   /* For every output section statement added to the list, except the
      first one, lang_output_section_statement.tail points to the "next"
      field of the last element of the list.  */
   if (lang_output_section_statement.head != NULL)
-    ret->os.prev = (lang_output_section_statement_type *)
-      ((char *) lang_output_section_statement.tail
-       - offsetof (lang_output_section_statement_type, next));
+    ret->s.output_section_statement.prev
+      = ((lang_output_section_statement_type *)
+        ((char *) lang_output_section_statement.tail
+         - offsetof (lang_output_section_statement_type, next)));
 
   /* GCC's strict aliasing rules prevent us from just casting the
      address, so we store the pointer in a variable and cast that
      instead.  */
-  nextp = &ret->os.next;
+  nextp = &ret->s.output_section_statement.next;
   lang_statement_append (&lang_output_section_statement,
-                        (lang_statement_union_type *) &ret->os,
+                        &ret->s,
                         (lang_statement_union_type **) nextp);
   return &ret->root;
 }
 
 static void
-output_statement_table_init (void)
+output_section_statement_table_init (void)
 {
-  if (!bfd_hash_table_init_n (&output_statement_table,
-                             output_statement_newfunc,
-                             sizeof (struct output_statement_hash_entry),
+  if (!bfd_hash_table_init_n (&output_section_statement_table,
+                             output_section_statement_newfunc,
+                             sizeof (struct out_section_hash_entry),
                              61))
     einfo (_("%P%F: can not create hash table: %E\n"));
 }
 
 static void
-output_statement_table_free (void)
+output_section_statement_table_free (void)
 {
-  bfd_hash_table_free (&output_statement_table);
+  bfd_hash_table_free (&output_section_statement_table);
 }
 
 /* Build enough state so that the parser can build its tree.  */
@@ -956,7 +1077,7 @@ lang_init (void)
 
   stat_ptr = &statement_list;
 
-  output_statement_table_init ();
+  output_section_statement_table_init ();
 
   lang_list_init (stat_ptr);
 
@@ -986,7 +1107,7 @@ lang_init (void)
 void
 lang_finish (void)
 {
-  output_statement_table_free ();
+  output_section_statement_table_free ();
 }
 
 /*----------------------------------------------------------------------
@@ -1035,16 +1156,17 @@ lang_memory_region_lookup (const char *const name, bfd_boolean create)
 
   new->name = xstrdup (name);
   new->next = NULL;
-
-  *lang_memory_region_list_tail = new;
-  lang_memory_region_list_tail = &new->next;
   new->origin = 0;
-  new->flags = 0;
-  new->not_flags = 0;
   new->length = ~(bfd_size_type) 0;
   new->current = 0;
+  new->last_os = NULL;
+  new->flags = 0;
+  new->not_flags = 0;
   new->had_full_message = FALSE;
 
+  *lang_memory_region_list_tail = new;
+  lang_memory_region_list_tail = &new->next;
+
   return new;
 }
 
@@ -1073,24 +1195,25 @@ lang_memory_default (asection *section)
 lang_output_section_statement_type *
 lang_output_section_find (const char *const name)
 {
-  struct output_statement_hash_entry *entry;
+  struct out_section_hash_entry *entry;
   unsigned long hash;
 
-  entry = ((struct output_statement_hash_entry *)
-          bfd_hash_lookup (&output_statement_table, name, FALSE, FALSE));
+  entry = ((struct out_section_hash_entry *)
+          bfd_hash_lookup (&output_section_statement_table, name,
+                           FALSE, FALSE));
   if (entry == NULL)
     return NULL;
 
   hash = entry->root.hash;
   do
     {
-      if (entry->os.constraint != -1)
-       return &entry->os;
-      entry = (struct output_statement_hash_entry *) entry->root.next;
+      if (entry->s.output_section_statement.constraint != -1)
+       return &entry->s.output_section_statement;
+      entry = (struct out_section_hash_entry *) entry->root.next;
     }
   while (entry != NULL
         && entry->root.hash == hash
-        && strcmp (name, entry->os.name) == 0);
+        && strcmp (name, entry->s.output_section_statement.name) == 0);
 
   return NULL;
 }
@@ -1098,39 +1221,43 @@ lang_output_section_find (const char *const name)
 static lang_output_section_statement_type *
 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
 {
-  struct output_statement_hash_entry *entry;
-  struct output_statement_hash_entry *last_ent;
+  struct out_section_hash_entry *entry;
+  struct out_section_hash_entry *last_ent;
   unsigned long hash;
 
-  entry = ((struct output_statement_hash_entry *)
-          bfd_hash_lookup (&output_statement_table, name, TRUE, FALSE));
+  entry = ((struct out_section_hash_entry *)
+          bfd_hash_lookup (&output_section_statement_table, name,
+                           TRUE, FALSE));
   if (entry == NULL)
     {
       einfo (_("%P%F: failed creating section `%s': %E\n"), name);
       return NULL;
     }
 
-  if (entry->os.name != NULL)
+  if (entry->s.output_section_statement.name != NULL)
     {
       /* We have a section of this name, but it might not have the correct
         constraint.  */
       hash = entry->root.hash;
       do
        {
-         if (entry->os.constraint != -1
+         if (entry->s.output_section_statement.constraint != -1
              && (constraint == 0
-                 || (constraint == entry->os.constraint
+                 || (constraint == entry->s.output_section_statement.constraint
                      && constraint != SPECIAL)))
-           return &entry->os;
+           return &entry->s.output_section_statement;
          last_ent = entry;
-         entry = (struct output_statement_hash_entry *) entry->root.next;
+         entry = (struct out_section_hash_entry *) entry->root.next;
        }
       while (entry != NULL
             && entry->root.hash == hash
-            && strcmp (name, entry->os.name) == 0);
+            && strcmp (name, entry->s.output_section_statement.name) == 0);
 
-      entry = ((struct output_statement_hash_entry *)
-              output_statement_newfunc (NULL, &output_statement_table, name));
+      entry
+       = ((struct out_section_hash_entry *)
+          output_section_statement_newfunc (NULL,
+                                            &output_section_statement_table,
+                                            name));
       if (entry == NULL)
        {
          einfo (_("%P%F: failed creating section `%s': %E\n"), name);
@@ -1140,9 +1267,9 @@ lang_output_section_statement_lookup_1 (const char *const name, int constraint)
       last_ent->root.next = &entry->root;
     }
 
-  entry->os.name = name;
-  entry->os.constraint = constraint;
-  return &entry->os;
+  entry->s.output_section_statement.name = name;
+  entry->s.output_section_statement.constraint = constraint;
+  return &entry->s.output_section_statement;
 }
 
 lang_output_section_statement_type *
@@ -1328,7 +1455,6 @@ lang_insert_orphan (asection *s,
   lang_statement_list_type *old;
   lang_statement_list_type add;
   const char *ps;
-  etree_type *load_base;
   lang_output_section_statement_type *os;
   lang_output_section_statement_type **os_tail;
 
@@ -1372,20 +1498,10 @@ lang_insert_orphan (asection *s,
   if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
     address = exp_intop (0);
 
-  load_base = NULL;
-  if (after != NULL && after->load_base != NULL)
-    {
-      etree_type *lma_from_vma;
-      lma_from_vma = exp_binop ('-', after->load_base,
-                               exp_nameop (ADDR, after->name));
-      load_base = exp_binop ('+', lma_from_vma,
-                            exp_nameop (ADDR, secname));
-    }
-
   os_tail = ((lang_output_section_statement_type **)
             lang_output_section_statement.tail);
   os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
-                                           load_base, 0);
+                                           NULL, 0);
 
   if (add_child == NULL)
     add_child = &os->children;
@@ -1442,7 +1558,18 @@ lang_insert_orphan (asection *s,
        place->section = &output_bfd->sections;
 
       as = *place->section;
-      if (as != snew && as->prev != snew)
+
+      if (!as)
+       {
+         /* Put the section at the end of the list.  */
+
+         /* Unlink the section.  */
+         bfd_section_list_remove (output_bfd, snew);
+
+         /* Now tack it back on in the right place.  */
+         bfd_section_list_append (output_bfd, snew);
+       }
+      else if (as != snew && as->prev != snew)
        {
          /* Unlink the section.  */
          bfd_section_list_remove (output_bfd, snew);
@@ -1609,8 +1736,9 @@ lang_map (void)
        continue;
 
       for (s = file->the_bfd->sections; s != NULL; s = s->next)
-       if (s->output_section == NULL
-           || s->output_section->owner != output_bfd)
+       if ((s->output_section == NULL
+            || s->output_section->owner != output_bfd)
+           && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
          {
            if (! dis_header_printed)
              {
@@ -1666,7 +1794,7 @@ lang_map (void)
 
   fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
 
-  if (! command_line.reduce_memory_overheads)
+  if (! link_info.reduce_memory_overheads)
     {
       obstack_begin (&map_obstack, 1000);
       for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
@@ -1724,7 +1852,8 @@ sort_def_symbol (hash_entry, info)
 /* Initialize an output section.  */
 
 static void
-init_os (lang_output_section_statement_type *s, asection *isec)
+init_os (lang_output_section_statement_type *s, asection *isec,
+        flagword flags)
 {
   if (s->bfd_section != NULL)
     return;
@@ -1734,7 +1863,8 @@ init_os (lang_output_section_statement_type *s, asection *isec)
 
   s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
   if (s->bfd_section == NULL)
-    s->bfd_section = bfd_make_section (output_bfd, s->name);
+    s->bfd_section = bfd_make_section_with_flags (output_bfd, s->name,
+                                                 flags);
   if (s->bfd_section == NULL)
     {
       einfo (_("%P%F: output format %s cannot represent section called %s\n"),
@@ -1742,7 +1872,8 @@ init_os (lang_output_section_statement_type *s, asection *isec)
     }
   s->bfd_section->output_section = s->bfd_section;
   s->bfd_section->output_offset = 0;
-  if (!command_line.reduce_memory_overheads)
+
+  if (!link_info.reduce_memory_overheads)
     {
       fat_section_userdata_type *new
        = stat_alloc (sizeof (fat_section_userdata_type));
@@ -1750,7 +1881,6 @@ init_os (lang_output_section_statement_type *s, asection *isec)
       get_userdata (s->bfd_section) = new;
     }
 
-
   /* If there is a base address, make sure that any sections it might
      mention are initialized.  */
   if (s->addr_tree != NULL)
@@ -1812,7 +1942,7 @@ exp_init_os (etree_type *exp)
 
            os = lang_output_section_find (exp->name.name);
            if (os != NULL && os->bfd_section == NULL)
-             init_os (os, NULL);
+             init_os (os, NULL, 0);
          }
        }
       break;
@@ -1836,7 +1966,7 @@ section_already_linked (bfd *abfd, asection *sec, void *data)
     }
 
   if (!(abfd->flags & DYNAMIC))
-    bfd_section_already_linked (abfd, sec);
+    bfd_section_already_linked (abfd, sec, &link_info);
 }
 \f
 /* The wild routines.
@@ -1887,8 +2017,32 @@ lang_add_section (lang_statement_list_type *ptr,
       lang_input_section_type *new;
       flagword flags;
 
+      flags = section->flags;
+
+      /* We don't copy the SEC_NEVER_LOAD flag from an input section
+        to an output section, because we want to be able to include a
+        SEC_NEVER_LOAD section in the middle of an otherwise loaded
+        section (I don't know why we want to do this, but we do).
+        build_link_order in ldwrite.c handles this case by turning
+        the embedded SEC_NEVER_LOAD section into a fill.  */
+
+      flags &= ~ SEC_NEVER_LOAD;
+
+      switch (output->sectype)
+       {
+       case normal_section:
+         break;
+       case noalloc_section:
+         flags &= ~SEC_ALLOC;
+         break;
+       case noload_section:
+         flags &= ~SEC_LOAD;
+         flags |= SEC_NEVER_LOAD;
+         break;
+       }
+
       if (output->bfd_section == NULL)
-       init_os (output, section);
+       init_os (output, section, flags);
 
       first = ! output->bfd_section->linker_has_input;
       output->bfd_section->linker_has_input = 1;
@@ -1912,17 +2066,6 @@ lang_add_section (lang_statement_list_type *ptr,
       new->section = section;
       section->output_section = output->bfd_section;
 
-      flags = section->flags;
-
-      /* We don't copy the SEC_NEVER_LOAD flag from an input section
-        to an output section, because we want to be able to include a
-        SEC_NEVER_LOAD section in the middle of an otherwise loaded
-        section (I don't know why we want to do this, but we do).
-        build_link_order in ldwrite.c handles this case by turning
-        the embedded SEC_NEVER_LOAD section into a fill.  */
-
-      flags &= ~ SEC_NEVER_LOAD;
-
       /* If final link, don't copy the SEC_LINK_ONCE flags, they've
         already been processed.  One reason to do this is that on pe
         format targets, .text$foo sections go into .text and it's odd
@@ -1959,22 +2102,6 @@ lang_add_section (lang_statement_list_type *ptr,
       if ((section->flags & SEC_READONLY) == 0)
        output->bfd_section->flags &= ~SEC_READONLY;
 
-      switch (output->sectype)
-       {
-       case normal_section:
-         break;
-       case dsect_section:
-       case copy_section:
-       case info_section:
-       case overlay_section:
-         output->bfd_section->flags &= ~SEC_ALLOC;
-         break;
-       case noload_section:
-         output->bfd_section->flags &= ~SEC_LOAD;
-         output->bfd_section->flags |= SEC_NEVER_LOAD;
-         break;
-       }
-
       /* Copy over SEC_SMALL_DATA.  */
       if (section->flags & SEC_SMALL_DATA)
        output->bfd_section->flags |= SEC_SMALL_DATA;
@@ -1992,46 +2119,6 @@ lang_add_section (lang_statement_list_type *ptr,
     }
 }
 
-/* Compare sections ASEC and BSEC according to SORT.  */
-
-static int
-compare_section (sort_type sort, asection *asec, asection *bsec)
-{
-  int ret;
-
-  switch (sort)
-    {
-    default:
-      abort ();
-
-    case by_alignment_name:
-      ret = (bfd_section_alignment (bsec->owner, bsec)
-            - bfd_section_alignment (asec->owner, asec));
-      if (ret)
-       break;
-      /* Fall through.  */
-
-    case by_name:
-      ret = strcmp (bfd_get_section_name (asec->owner, asec),
-                   bfd_get_section_name (bsec->owner, bsec));
-      break;
-
-    case by_name_alignment:
-      ret = strcmp (bfd_get_section_name (asec->owner, asec),
-                   bfd_get_section_name (bsec->owner, bsec));
-      if (ret)
-       break;
-      /* Fall through.  */
-
-    case by_alignment:
-      ret = (bfd_section_alignment (bsec->owner, bsec)
-            - bfd_section_alignment (asec->owner, asec));
-      break;
-    }
-
-  return ret;
-}
-
 /* Handle wildcard sorting.  This returns the lang_input_section which
    should follow the one we are going to create for SECTION and FILE,
    based on the sorting requirements of WILD.  It returns NULL if the
@@ -2214,12 +2301,9 @@ lookup_name (const char *name)
       /* Use the local_sym_name as the name of the file that has
         already been loaded as filename might have been transformed
         via the search directory lookup mechanism.  */
-      const char * filename = search->local_sym_name;
+      const char *filename = search->local_sym_name;
 
-      if (filename == NULL && name == NULL)
-       return search;
       if (filename != NULL
-         && name != NULL
          && strcmp (filename, name) == 0)
        break;
     }
@@ -2229,11 +2313,8 @@ lookup_name (const char *name)
                        default_target, FALSE);
 
   /* If we have already added this file, or this file is not real
-     (FIXME: can that ever actually happen?) or the name is NULL
-     (FIXME: can that ever actually happen?) don't add this file.  */
-  if (search->loaded
-      || ! search->real
-      || search->filename == NULL)
+     don't add this file.  */
+  if (search->loaded || !search->real)
     return search;
 
   if (! load_symbols (search, NULL))
@@ -2269,7 +2350,7 @@ add_excluded_libs (const char *list)
       entry->name[end - p] = '\0';
       excluded_libs = entry;
       if (*end == '\0')
-        break;
+       break;
       p = end + 1;
     }
 }
@@ -2305,7 +2386,7 @@ check_excluded_libs (bfd *abfd)
 
 /* Get the symbols for an input file.  */
 
-static bfd_boolean
+bfd_boolean
 load_symbols (lang_input_statement_type *entry,
              lang_statement_list_type *place)
 {
@@ -2456,7 +2537,23 @@ wild (lang_wild_statement_type *s,
 {
   struct wildcard_list *sec;
 
-  walk_wild (s, output_section_callback, output);
+  if (s->handler_data[0]
+      && s->handler_data[0]->spec.sorted == by_name
+      && !s->filenames_sorted)
+    {
+      lang_section_bst_type *tree;
+
+      walk_wild (s, output_section_callback_fast, output);
+
+      tree = s->tree;
+      if (tree)
+       {
+         output_section_callback_tree_to_list (s, tree, output);
+         s->tree = NULL;
+       }
+    }
+  else
+    walk_wild (s, output_section_callback, output);
 
   if (default_common_section == NULL)
     for (sec = s->section_list; sec != NULL; sec = sec->next)
@@ -3055,7 +3152,8 @@ update_wild_statements (lang_statement_union_type *s)
 
            case lang_wild_statement_enum:
              sec = s->wild_statement.section_list;
-             if (sec != NULL)
+             for (sec = s->wild_statement.section_list; sec != NULL;
+                  sec = sec->next)
                {
                  switch (sec->spec.sorted)
                    {
@@ -3101,6 +3199,8 @@ map_input_to_output_sections
   (lang_statement_union_type *s, const char *target,
    lang_output_section_statement_type *os)
 {
+  flagword flags;
+
   for (; s != NULL; s = s->header.next)
     {
       switch (s->header.type)
@@ -3150,26 +3250,29 @@ map_input_to_output_sections
          /* Make sure that any sections mentioned in the expression
             are initialized.  */
          exp_init_os (s->data_statement.exp);
-         if (os != NULL && os->bfd_section == NULL)
-           init_os (os, NULL);
+         flags = SEC_HAS_CONTENTS;
          /* The output section gets contents, and then we inspect for
             any flags set in the input script which override any ALLOC.  */
-         os->bfd_section->flags |= SEC_HAS_CONTENTS;
          if (!(os->flags & SEC_NEVER_LOAD))
-           os->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
+           flags |= SEC_ALLOC | SEC_LOAD;
+         if (os->bfd_section == NULL)
+           init_os (os, NULL, flags);
+         else
+           os->bfd_section->flags |= flags;
          break;
-       case lang_fill_statement_enum:
        case lang_input_section_enum:
+         break;
+       case lang_fill_statement_enum:
        case lang_object_symbols_statement_enum:
        case lang_reloc_statement_enum:
        case lang_padding_statement_enum:
        case lang_input_statement_enum:
          if (os != NULL && os->bfd_section == NULL)
-           init_os (os, NULL);
+           init_os (os, NULL, 0);
          break;
        case lang_assignment_statement_enum:
          if (os != NULL && os->bfd_section == NULL)
-           init_os (os, NULL);
+           init_os (os, NULL, 0);
 
          /* Make sure that any sections mentioned in the assignment
             are initialized.  */
@@ -3179,7 +3282,7 @@ map_input_to_output_sections
          FAIL ();
          break;
        case lang_address_statement_enum:
-         /* Mark the specified section with the supplied address.  
+         /* Mark the specified section with the supplied address.
 
             If this section was actually a segment marker, then the
             directive is ignored if the linker script explicitly
@@ -3189,15 +3292,15 @@ map_input_to_output_sections
             section directive semantics for backwards compatibilty;
             linker scripts that do not specifically check for
             SEGMENT_START automatically get the old semantics.  */
-         if (!s->address_statement.segment 
+         if (!s->address_statement.segment
              || !s->address_statement.segment->used)
            {
              lang_output_section_statement_type *aos
                = (lang_output_section_statement_lookup
                   (s->address_statement.section_name));
-             
+
              if (aos->bfd_section == NULL)
-               init_os (aos, NULL);
+               init_os (aos, NULL, 0);
              aos->addr_tree = s->address_statement.address;
            }
          break;
@@ -3267,7 +3370,8 @@ strip_excluded_output_sections (void)
        {
          /* We don't set bfd_section to NULL since bfd_section of the
             removed output section statement may still be used.  */
-         os->ignored = TRUE;
+         if (!os->section_relative_symbol)
+           os->ignored = TRUE;
          output_section->flags |= SEC_EXCLUDE;
          bfd_section_list_remove (output_bfd, output_section);
          output_bfd->section_count--;
@@ -3308,14 +3412,8 @@ print_output_section_statement
 
          minfo ("0x%V %W", section->vma, section->size);
 
-         if (output_section_statement->load_base != NULL)
-           {
-             bfd_vma addr;
-
-             addr = exp_get_abs_int (output_section_statement->load_base, 0,
-                                     "load base");
-             minfo (_(" load address 0x%V"), addr);
-           }
+         if (section->vma != section->lma)
+           minfo (_(" load address 0x%V"), section->lma);
        }
 
       print_nl ();
@@ -3330,7 +3428,7 @@ print_output_section_statement
    correct expression, since the value of DST that is used on
    the right hand side will be its final value, not its value
    just before this expression is evaluated.  */
-   
+
 static bfd_boolean
 scan_for_self_assignment (const char * dst, etree_type * rhs)
 {
@@ -3487,8 +3585,7 @@ print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
 }
 
 static void
-print_all_symbols (sec)
-     asection *sec;
+print_all_symbols (asection *sec)
 {
   struct fat_user_section_struct *ud = get_userdata (sec);
   struct map_symbol_def *def;
@@ -3556,7 +3653,7 @@ print_input_section (asection *i)
 
   if (i->output_section != NULL && i->output_section->owner == output_bfd)
     {
-      if (command_line.reduce_memory_overheads)
+      if (link_info.reduce_memory_overheads)
        bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
       else
        print_all_symbols (i);
@@ -4037,7 +4134,7 @@ lang_check_section_addresses (void)
       sections[count] = s;
       count++;
     }
-  
+
   if (count <= 1)
     return;
 
@@ -4054,7 +4151,7 @@ lang_check_section_addresses (void)
         addresses because overlay sections can have overlapping VMAs
         but they must have distinct LMAs.  */
       os = s;
-      os_start = s_start; 
+      os_start = s_start;
       os_end = s_end;
       s = *spp++;
       s_start = bfd_section_lma (output_bfd, s);
@@ -4127,11 +4224,12 @@ lang_size_sections_1
          {
            bfd_vma newdot, after;
            lang_output_section_statement_type *os;
+           lang_memory_region_type *r;
 
            os = &s->output_section_statement;
            if (os->addr_tree != NULL)
              {
-               os->processed = FALSE;
+               os->processed_vma = FALSE;
                exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
 
                if (!expld.result.valid_p
@@ -4198,7 +4296,8 @@ lang_size_sections_1
                    /* If a loadable section is using the default memory
                       region, and some non default memory regions were
                       defined, issue an error message.  */
-                   if (!IGNORE_SECTION (os->bfd_section)
+                   if (!os->ignored
+                       && !IGNORE_SECTION (os->bfd_section)
                        && ! link_info.relocatable
                        && check_regions
                        && strcmp (os->region->name,
@@ -4259,24 +4358,120 @@ lang_size_sections_1
            lang_size_sections_1 (os->children.head, os, &os->children.head,
                                  os->fill, newdot, relax, check_regions);
 
-           os->processed = TRUE;
+           os->processed_vma = TRUE;
 
            if (bfd_is_abs_section (os->bfd_section) || os->ignored)
+             /* Except for some special linker created sections,
+                no output section should change from zero size
+                after strip_excluded_output_sections.  A non-zero
+                size on an ignored section indicates that some
+                input section was not sized early enough.  */
+             ASSERT (os->bfd_section->size == 0);
+           else
              {
-               ASSERT (os->bfd_section->size == 0);
-               break;
+               dot = os->bfd_section->vma;
+
+               /* Put the section within the requested block size, or
+                  align at the block boundary.  */
+               after = ((dot
+                         + TO_ADDR (os->bfd_section->size)
+                         + os->block_value - 1)
+                        & - (bfd_vma) os->block_value);
+
+               os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
              }
 
-           dot = os->bfd_section->vma;
+           /* Set section lma.  */
+           r = os->region;
+           if (r == NULL)
+             r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
 
-           /* Put the section within the requested block size, or
-              align at the block boundary.  */
-           after = ((dot
-                     + TO_ADDR (os->bfd_section->size)
-                     + os->block_value - 1)
-                    & - (bfd_vma) os->block_value);
+           if (os->load_base)
+             {
+               bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
+               os->bfd_section->lma = lma;
+             }
+           else if (os->region != NULL
+                    && os->lma_region != NULL
+                    && os->lma_region != os->region)
+             {
+               bfd_vma lma = os->lma_region->current;
+
+               if (os->section_alignment != -1)
+                 lma = align_power (lma, os->section_alignment);
+               os->bfd_section->lma = lma;
+             }
+           else if (r->last_os != NULL
+                    && (os->bfd_section->flags & SEC_ALLOC) != 0)
+             {
+               bfd_vma lma;
+               asection *last;
+
+               last = r->last_os->output_section_statement.bfd_section;
+
+               /* A backwards move of dot should be accompanied by
+                  an explicit assignment to the section LMA (ie.
+                  os->load_base set) because backwards moves normally
+                  create overlapping LMAs.  */
+               if (dot < last->vma
+                   && os->bfd_section->size != 0)
+                 {
+                   einfo (_("%P: warning: dot moved backwards before `%s'\n"),
+                          os->name);
+
+                   /* If dot moved backwards then leave lma equal to
+                      vma.  This is the old default lma, which might
+                      just happen to work when the backwards move is
+                      sufficiently large.  Nag anyway, so people fix
+                      their linker scripts.  */
+                 }
+               else
+                 {
+                   /* If the current vma overlaps the previous section,
+                      then set the current lma to that at the end of
+                      the previous section.  The previous section was
+                      probably an overlay.  */
+                   if ((dot >= last->vma
+                        && dot < last->vma + last->size)
+                       || (last->vma >= dot
+                           && last->vma < dot + os->bfd_section->size))
+                     lma = last->lma + last->size;
+
+                   /* Otherwise, keep the same lma to vma relationship
+                      as the previous section.  */
+                   else
+                     lma = dot + last->lma - last->vma;
+
+                   if (os->section_alignment != -1)
+                     lma = align_power (lma, os->section_alignment);
+                   os->bfd_section->lma = lma;
+                 }
+             }
+           os->processed_lma = TRUE;
+
+           if (bfd_is_abs_section (os->bfd_section) || os->ignored)
+             break;
 
-           os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
+           /* Keep track of normal sections using the default
+              lma region.  We use this to set the lma for
+              following sections.  Overlays or other linker
+              script assignment to lma might mean that the
+              default lma == vma is incorrect.
+              To avoid warnings about dot moving backwards when using
+              -Ttext, don't start tracking sections until we find one
+              of non-zero size or with lma set differently to vma.  */
+           if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
+                || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0)
+               && (os->bfd_section->flags & SEC_ALLOC) != 0
+               && (os->bfd_section->size != 0
+                   || (r->last_os == NULL
+                       && os->bfd_section->vma != os->bfd_section->lma)
+                   || (r->last_os != NULL
+                       && dot >= (r->last_os->output_section_statement
+                                  .bfd_section->vma)))
+               && os->lma_region == NULL
+               && !link_info.relocatable)
+             r->last_os = s;
 
            /* .tbss sections effectively have zero size.  */
            if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
@@ -4306,17 +4501,11 @@ lang_size_sections_1
                  os_region_check (os, os->region, os->addr_tree,
                                   os->bfd_section->vma);
 
-               /* If there's no load address specified, use the run
-                  region as the load region.  */
-               if (os->lma_region == NULL && os->load_base == NULL)
-                 os->lma_region = os->region;
-
                if (os->lma_region != NULL && os->lma_region != os->region)
                  {
-                   /* Set load_base, which will be handled later.  */
-                   os->load_base = exp_intop (os->lma_region->current);
-                   os->lma_region->current +=
-                     TO_ADDR (os->bfd_section->size);
+                   os->lma_region->current
+                     = os->bfd_section->lma + TO_ADDR (os->bfd_section->size);
+
                    if (check_regions)
                      os_region_check (os, os->lma_region, NULL,
                                       os->bfd_section->lma);
@@ -4432,12 +4621,20 @@ lang_size_sections_1
        case lang_assignment_statement_enum:
          {
            bfd_vma newdot = dot;
+           etree_type *tree = s->assignment_statement.exp;
 
-           exp_fold_tree (s->assignment_statement.exp,
+           exp_fold_tree (tree,
                           output_section_statement->bfd_section,
                           &newdot);
 
-           if (newdot != dot && !output_section_statement->ignored)
+           /* This symbol is relative to this section.  */
+           if ((tree->type.node_class == etree_provided
+                || tree->type.node_class == etree_assign)
+               && (tree->assign.dst [0] != '.'
+                   || tree->assign.dst [1] != '\0'))
+             output_section_statement->section_relative_symbol = 1;
+
+           if (!output_section_statement->ignored)
              {
                if (output_section_statement == abs_output_section)
                  {
@@ -4446,7 +4643,7 @@ lang_size_sections_1
                    lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
                                               FALSE)->current = newdot;
                  }
-               else
+               else if (newdot != dot)
                  {
                    /* Insert a pad after this statement.  We can't
                       put the pad before when relaxing, in case the
@@ -4542,6 +4739,7 @@ lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
          expld.dataseg.base -= maxpage;
          relro_end -= maxpage;
        }
+      lang_reset_memory_regions ();
       one_lang_size_sections_pass (relax, check_regions);
       if (expld.dataseg.relro_end > relro_end)
        {
@@ -4565,6 +4763,7 @@ lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
                  < old_min_base)
                expld.dataseg.base += expld.dataseg.pagesize;
              expld.dataseg.base -= (1 << max_alignment_power);
+             lang_reset_memory_regions ();
              one_lang_size_sections_pass (relax, check_regions);
            }
        }
@@ -4585,6 +4784,7 @@ lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
          && first + last <= expld.dataseg.pagesize)
        {
          expld.dataseg.phase = exp_dataseg_adjust;
+         lang_reset_memory_regions ();
          one_lang_size_sections_pass (relax, check_regions);
        }
     }
@@ -4595,11 +4795,10 @@ lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
 /* Worker function for lang_do_assignments.  Recursiveness goes here.  */
 
 static bfd_vma
-lang_do_assignments_1
-  (lang_statement_union_type *s,
-   lang_output_section_statement_type *output_section_statement,
-   fill_type *fill,
-   bfd_vma dot)
+lang_do_assignments_1 (lang_statement_union_type *s,
+                      lang_output_section_statement_type *current_os,
+                      fill_type *fill,
+                      bfd_vma dot)
 {
   for (; s != NULL; s = s->header.next)
     {
@@ -4607,9 +4806,7 @@ lang_do_assignments_1
        {
        case lang_constructors_statement_enum:
          dot = lang_do_assignments_1 (constructor_list.head,
-                                      output_section_statement,
-                                      fill,
-                                      dot);
+                                      current_os, fill, dot);
          break;
 
        case lang_output_section_statement_enum:
@@ -4620,31 +4817,22 @@ lang_do_assignments_1
            if (os->bfd_section != NULL && !os->ignored)
              {
                dot = os->bfd_section->vma;
+
                lang_do_assignments_1 (os->children.head, os, os->fill, dot);
+
                /* .tbss sections effectively have zero size.  */
                if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
                    || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
                    || link_info.relocatable)
                  dot += TO_ADDR (os->bfd_section->size);
              }
-           if (os->load_base)
-             {
-               /* If nothing has been placed into the output section then
-                  it won't have a bfd_section.  */
-               if (os->bfd_section && !os->ignored)
-                 {
-                   os->bfd_section->lma
-                     = exp_get_abs_int (os->load_base, 0, "load base");
-                 }
-             }
          }
          break;
 
        case lang_wild_statement_enum:
 
          dot = lang_do_assignments_1 (s->wild_statement.children.head,
-                                      output_section_statement,
-                                      fill, dot);
+                                      current_os, fill, dot);
          break;
 
        case lang_object_symbols_statement_enum:
@@ -4713,7 +4901,7 @@ lang_do_assignments_1
 
        case lang_assignment_statement_enum:
          exp_fold_tree (s->assignment_statement.exp,
-                        output_section_statement->bfd_section,
+                        current_os->bfd_section,
                         &dot);
          break;
 
@@ -4723,8 +4911,7 @@ lang_do_assignments_1
 
        case lang_group_statement_enum:
          dot = lang_do_assignments_1 (s->group_statement.children.head,
-                                      output_section_statement,
-                                      fill, dot);
+                                      current_os, fill, dot);
          break;
 
        default:
@@ -5309,14 +5496,17 @@ lang_reset_memory_regions (void)
 
   for (p = lang_memory_region_list; p != NULL; p = p->next)
     {
-      p->old_length = (bfd_size_type) (p->current - p->origin);
       p->current = p->origin;
+      p->last_os = NULL;
     }
 
   for (os = &lang_output_section_statement.head->output_section_statement;
        os != NULL;
        os = os->next)
-    os->processed = FALSE;
+    {
+      os->processed_vma = FALSE;
+      os->processed_lma = FALSE;
+    }
 
   for (o = output_bfd->sections; o != NULL; o = o->next)
     {
@@ -5423,7 +5613,7 @@ relax_sections (void)
 
   do
     {
-      relax_again = FALSE; 
+      relax_again = FALSE;
 
       /* Note: pe-dll.c does something like this also.  If you find
         you need to change this code, you probably need to change
@@ -5447,6 +5637,10 @@ relax_sections (void)
 void
 lang_process (void)
 {
+  /* Finalize dynamic list.  */
+  if (link_info.dynamic_list)
+    lang_finalize_version_expr_head (&link_info.dynamic_list->head);
+
   current_target = default_target;
 
   /* Open the output file.  */
@@ -5570,13 +5764,13 @@ lang_process (void)
 
   lang_do_assignments ();
 
+  ldemul_finish ();
+
   /* Make sure that the section addresses make sense.  */
   if (! link_info.relocatable
       && command_line.check_section_addresses)
     lang_check_section_addresses ();
 
-  /* Final stuffs.  */
-  ldemul_finish ();
   lang_end ();
 }
 
@@ -6236,10 +6430,6 @@ lang_leave_overlay (etree_type *lma_expr,
         an LMA region was specified.  */
       if (l->next == 0)
        l->os->load_base = lma_expr;
-      else if (lma_region == 0)
-       l->os->load_base = exp_binop ('+',
-                                     exp_nameop (LOADADDR, l->next->os->name),
-                                     exp_nameop (SIZEOF, l->next->os->name));
 
       if (phdrs != NULL && l->os->phdrs == NULL)
        l->os->phdrs = phdrs;
@@ -6731,7 +6921,7 @@ lang_do_version_exports_section (void)
       /* Do not free the contents, as we used them creating the regex.  */
 
       /* Do not include this section in the link.  */
-      sec->flags |= SEC_EXCLUDE;
+      sec->flags |= SEC_EXCLUDE | SEC_KEEP;
     }
 
   lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
@@ -6753,3 +6943,69 @@ lang_add_unique (const char *name)
   ent->next = unique_section_list;
   unique_section_list = ent;
 }
+
+/* Append the list of dynamic symbols to the existing one.  */
+
+void
+lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic)
+{
+  if (link_info.dynamic_list)
+    {
+      struct bfd_elf_version_expr *tail;
+      for (tail = dynamic; tail->next != NULL; tail = tail->next)
+       ;
+      tail->next = link_info.dynamic_list->head.list;
+      link_info.dynamic_list->head.list = dynamic;
+    }
+  else
+    {
+      struct bfd_elf_dynamic_list *d;
+
+      d = xcalloc (1, sizeof *d);
+      d->head.list = dynamic;
+      d->match = lang_vers_match;
+      link_info.dynamic_list = d;
+    }
+}
+
+/* Append the list of C++ typeinfo dynamic symbols to the existing
+   one.  */
+
+void
+lang_append_dynamic_list_cpp_typeinfo (void)
+{
+  const char * symbols [] =
+    {
+      "typeinfo name for*",
+      "typeinfo for*"
+    };
+  struct bfd_elf_version_expr *dynamic = NULL;
+  unsigned int i;
+
+  for (i = 0; i < ARRAY_SIZE (symbols); i++)
+    dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
+                                    FALSE);
+
+  lang_append_dynamic_list (dynamic);
+}
+
+/* Append the list of C++ operator new and delete dynamic symbols to the
+   existing one.  */
+
+void
+lang_append_dynamic_list_cpp_new (void)
+{
+  const char * symbols [] =
+    {
+      "operator new*",
+      "operator delete*"
+    };
+  struct bfd_elf_version_expr *dynamic = NULL;
+  unsigned int i;
+
+  for (i = 0; i < ARRAY_SIZE (symbols); i++)
+    dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
+                                    FALSE);
+
+  lang_append_dynamic_list (dynamic);
+}
This page took 0.040083 seconds and 4 git commands to generate.