2002-01-29 Chris Demetriou <cgd@broadcom.com>
[deliverable/binutils-gdb.git] / ld / ldlang.c
index 905d08c1015daa1b4ce895b3fea3eb691ee1aec9..a7b53dd1b0f6a0c57eaa2f216ab08e48d59f558c 100644 (file)
@@ -1,6 +1,6 @@
 /* Linker command language support.
    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
-   2001
+   2001, 2002
    Free Software Foundation, Inc.
 
 This file is part of GLD, the Gnu Linker.
@@ -23,6 +23,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "bfd.h"
 #include "sysdep.h"
 #include "libiberty.h"
+#include "safe-ctype.h"
 #include "obstack.h"
 #include "bfdlink.h"
 
@@ -39,8 +40,6 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "fnmatch.h"
 #include "demangle.h"
 
-#include <ctype.h>
-
 /* FORWARDS */
 static lang_statement_union_type *new_statement
   PARAMS ((enum statement_enum, size_t, lang_statement_list_type *));
@@ -252,47 +251,59 @@ walk_wild_section (ptr, file, callback, data)
       struct wildcard_list *sec;
 
       sec = ptr->section_list;
-      do
+      if (sec == NULL)
+       (*callback) (ptr, sec, s, file, data);
+
+      while (sec != NULL)
        {
          boolean skip = false;
+         struct name_list *list_tmp;
 
-         if (sec != NULL)
+         /* Don't process sections from files which were
+            excluded.  */
+         for (list_tmp = sec->spec.exclude_name_list;
+              list_tmp;
+              list_tmp = list_tmp->next)
            {
-             struct name_list *list_tmp;
+             if (wildcardp (list_tmp->name))
+               skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
+             else
+               skip = strcmp (list_tmp->name, file->filename) == 0;
 
-             /* Don't process sections from files which were
-                excluded.  */
-             for (list_tmp = sec->spec.exclude_name_list;
-                  list_tmp;
-                  list_tmp = list_tmp->next)
+             /* If this file is part of an archive, and the archive is
+                excluded, exclude this file.  */
+             if (! skip && file->the_bfd != NULL
+                 && file->the_bfd->my_archive != NULL
+                 && file->the_bfd->my_archive->filename != NULL)
                {
                  if (wildcardp (list_tmp->name))
-                   skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
+                   skip = fnmatch (list_tmp->name,
+                                   file->the_bfd->my_archive->filename,
+                                   0) == 0;
                  else
-                   skip = strcmp (list_tmp->name, file->filename) == 0;
-
-                 if (skip)
-                   break;
+                   skip = strcmp (list_tmp->name,
+                                  file->the_bfd->my_archive->filename) == 0;
                }
 
-             if (!skip && sec->spec.name != NULL)
-               {
-                 const char *sname = bfd_get_section_name (file->the_bfd, s);
+             if (skip)
+               break;
+           }
 
-                 if (wildcardp (sec->spec.name))
-                   skip = fnmatch (sec->spec.name, sname, 0) != 0;
-                 else
-                   skip = strcmp (sec->spec.name, sname) != 0;
-               }
+         if (!skip && sec->spec.name != NULL)
+           {
+             const char *sname = bfd_get_section_name (file->the_bfd, s);
+
+             if (wildcardp (sec->spec.name))
+               skip = fnmatch (sec->spec.name, sname, 0) != 0;
+             else
+               skip = strcmp (sec->spec.name, sname) != 0;
            }
 
          if (!skip)
            (*callback) (ptr, sec, s, file, data);
 
-         if (sec != NULL)
-           sec = sec->next;
+         sec = sec->next;
        }
-      while (sec != NULL);
     }
 }
 
@@ -377,7 +388,7 @@ lang_for_each_statement_worker (func, s)
      void (*func) PARAMS ((lang_statement_union_type *));
      lang_statement_union_type *s;
 {
-  for (; s != (lang_statement_union_type *) NULL; s = s->next)
+  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
     {
       func (s);
 
@@ -1014,11 +1025,8 @@ section_already_linked (abfd, sec, data)
 
          /* Set the output_section field so that lang_add_section
             does not create a lang_input_section structure for this
-            section.  Since there might be a symbol in the section
-            being discarded, we must retain a pointer to the section
-            which we are really going to use.  */
+            section.  */
          sec->output_section = bfd_abs_section_ptr;
-         sec->kept_section = l->sec;
 
          return;
        }
@@ -1076,7 +1084,7 @@ already_linked_table_free ()
 /* Return true if the PATTERN argument is a wildcard pattern.
    Although backslashes are treated specially if a pattern contains
    wildcards, we do not consider the mere presence of a backslash to
-   be enough to cause the the pattern to be treated as a wildcard.
+   be enough to cause the pattern to be treated as a wildcard.
    That lets us handle DOS filenames more naturally.  */
 
 static boolean
@@ -1259,7 +1267,7 @@ wild_sort (wild, sec, file, section)
     return NULL;
 
   section_name = bfd_get_section_name (file->the_bfd, section);
-  for (l = wild->children.head; l != NULL; l = l->next)
+  for (l = wild->children.head; l != NULL; l = l->header.next)
     {
       lang_input_section_type *ls;
 
@@ -1389,14 +1397,14 @@ output_section_callback (ptr, sec, section, file, output)
         be NULL.  */
       if (list.head != NULL)
        {
-         ASSERT (list.head->next == NULL);
+         ASSERT (list.head->header.next == NULL);
 
          for (pp = &ptr->children.head;
               *pp != before;
-              pp = &(*pp)->next)
+              pp = &(*pp)->header.next)
            ASSERT (*pp != NULL);
 
-         list.head->next = *pp;
+         list.head->header.next = *pp;
          *pp = list.head;
        }
     }
@@ -1618,12 +1626,7 @@ stricpy (dest, src)
   char c;
 
   while ((c = *src++) != 0)
-    {
-      if (isupper ((unsigned char) c))
-       c = tolower (c);
-
-      *dest++ = c;
-    }
+    *dest++ = TOLOWER (c);
 
   *dest = 0;
 }
@@ -1906,7 +1909,7 @@ open_input_bfds (s, force)
      lang_statement_union_type *s;
      boolean force;
 {
-  for (; s != (lang_statement_union_type *) NULL; s = s->next)
+  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
     {
       switch (s->header.type)
        {
@@ -1967,8 +1970,8 @@ open_input_bfds (s, force)
 
              if (add.head != NULL)
                {
-                 *add.tail = s->next;
-                 s->next = add.head;
+                 *add.tail = s->header.next;
+                 s->header.next = add.head;
                }
            }
          break;
@@ -2062,12 +2065,13 @@ map_input_to_output_sections (s, target, output_section_statement)
      const char *target;
      lang_output_section_statement_type *output_section_statement;
 {
-  for (; s != (lang_statement_union_type *) NULL; s = s->next)
+  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
     {
       switch (s->header.type)
        {
        case lang_wild_statement_enum:
          wild (&s->wild_statement, target, output_section_statement);
+         break;
        case lang_constructors_statement_enum:
          map_input_to_output_sections (constructor_list.head,
                                        target,
@@ -2513,7 +2517,7 @@ print_statement_list (s, os)
   while (s != NULL)
     {
       print_statement (s, os);
-      s = s->next;
+      s = s->header.next;
     }
 }
 
@@ -2619,7 +2623,7 @@ dprint_statement (s, n)
       while (s && --n >= 0)
        {
          print_statement (s, abs_output_section);
-         s = s->next;
+         s = s->header.next;
        }
     }
 
@@ -2635,15 +2639,11 @@ insert_pad (ptr, fill, alignment_needed, output_section, dot)
      bfd_vma dot;
 {
   lang_statement_union_type *pad;
-  size_t ptr_off;
 
-  /* ptr_off is zero, but let's not be too fast and loose with
-     pointers.  */
-  ptr_off = ((char *) &((lang_statement_union_type *) 0)->header.next
-            - (char *) 0);
+  pad = ((lang_statement_union_type *)
+        ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
   if (ptr != &statement_list.head
-      && ((pad = (lang_statement_union_type *) ((char *) ptr - ptr_off))
-         ->header.type == lang_padding_statement_enum)
+      && pad->header.type == lang_padding_statement_enum
       && pad->padding_statement.output_section == output_section)
     {
       /* Use the existing pad statement.  The above test on output
@@ -2836,7 +2836,7 @@ lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
                                                ldfile_output_machine);
 
   /* Size up the sections from their constituent parts.  */
-  for (; s != (lang_statement_union_type *) NULL; s = s->next)
+  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
     {
       switch (s->header.type)
        {
@@ -2859,7 +2859,7 @@ lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
                asection *input;
 
                if (os->children.head == NULL
-                   || os->children.head->next != NULL
+                   || os->children.head->header.next != NULL
                    || os->children.head->header.type != lang_input_section_enum)
                  einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
                         os->name);
@@ -3163,18 +3163,20 @@ lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
          break;
 
        case lang_padding_statement_enum:
-         if (relax)
-           {
-             /* If we are relaxing, and this is not the first pass,
-                we need to allow padding to shrink.  If padding is
-                needed on this pass, it will be added back in.  */
-             s->padding_statement.size = 0;
-             break;
-           }
-
-         dot += s->padding_statement.size / opb;
-         output_section_statement->bfd_section->_raw_size +=
-           s->padding_statement.size;
+         /* If this is the first time lang_size_sections is called,
+            we won't have any padding statements.  If this is the
+            second or later passes when relaxing, we should allow
+            padding to shrink.  If padding is needed on this pass, it
+            will be added back in.  */
+         s->padding_statement.size = 0;
+
+         /* Make sure output_offset is valid.  If relaxation shrinks
+            the section and this pad isn't needed, it's possible to
+            have output_offset larger than the final size of the
+            section.  bfd_set_section_contents will complain even for
+            a pad size of zero.  */
+         s->padding_statement.output_offset
+           = dot - output_section_statement->bfd_section->vma;
          break;
 
        case lang_group_statement_enum:
@@ -3188,8 +3190,7 @@ lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
          FAIL ();
          break;
 
-         /* This can only get here when relaxing is turned on.  */
-
+         /* We can only get here when relaxing is turned on.  */
        case lang_address_statement_enum:
          break;
        }
@@ -3208,7 +3209,7 @@ lang_do_assignments (s, output_section_statement, fill, dot)
   unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
                                                ldfile_output_machine);
 
-  for (; s != (lang_statement_union_type *) NULL; s = s->next)
+  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
     {
       switch (s->header.type)
        {
@@ -3570,6 +3571,8 @@ lang_check ()
 static void
 lang_common ()
 {
+  if (command_line.inhibit_common_definition)
+    return;
   if (link_info.relocateable
       && ! command_line.force_common_definition)
     return;
@@ -4017,12 +4020,13 @@ static void
 lang_gc_sections_1 (s)
      lang_statement_union_type *s;
 {
-  for (; s != (lang_statement_union_type *) NULL; s = s->next)
+  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
     {
       switch (s->header.type)
        {
        case lang_wild_statement_enum:
          lang_gc_wild (&s->wild_statement);
+         break;
        case lang_constructors_statement_enum:
          lang_gc_sections_1 (constructor_list.head);
          break;
@@ -4797,7 +4801,7 @@ lang_leave_overlay_section (fill, phdrs)
   clean = xmalloc (strlen (name) + 1);
   s2 = clean;
   for (s1 = name; *s1 != '\0'; s1++)
-    if (isalnum ((unsigned char) *s1) || *s1 == '_')
+    if (ISALNUM (*s1) || *s1 == '_')
       *s2++ = *s1;
   *s2 = '\0';
 
@@ -5042,6 +5046,16 @@ lang_register_vers_node (name, version, deps)
   struct bfd_elf_version_tree *t, **pp;
   struct bfd_elf_version_expr *e1;
 
+  if (name == NULL)
+    name = "";
+
+  if ((name[0] == '\0' && lang_elf_version_info != NULL)
+      || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
+    {
+      einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
+      return;
+    }
+
   /* Make sure this node has a unique name.  */
   for (t = lang_elf_version_info; t != NULL; t = t->next)
     if (strcmp (t->name, name) == 0)
@@ -5078,8 +5092,13 @@ lang_register_vers_node (name, version, deps)
 
   version->deps = deps;
   version->name = name;
-  ++version_index;
-  version->vernum = version_index;
+  if (name[0] != '\0')
+    {
+      ++version_index;
+      version->vernum = version_index;
+    }
+  else
+    version->vernum = 0;
 
   for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
     ;
This page took 0.028648 seconds and 4 git commands to generate.