2001-09-11 H.J. Lu <hjl@gnu.org>
[deliverable/binutils-gdb.git] / bfd / linker.c
index 6e9ee65581b1a3eceaca902db117ed72fd2321ac..df7ee4b16123bcb81312a15be4f729e7aae7b83e 100644 (file)
@@ -1,5 +1,5 @@
 /* linker.c -- BFD linker routines
-   Copyright (C) 1993, 94, 95, 96, 97, 98, 1999
+   Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
    Free Software Foundation, Inc.
    Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
 
@@ -60,7 +60,7 @@ SECTION
        a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>).  The a.out
        routines are used as examples throughout this section.
 
-@menu  
+@menu
 @* Creating a Linker Hash Table::
 @* Adding Symbols to the Hash Table::
 @* Performing the Final Link::
@@ -483,6 +483,8 @@ _bfd_link_hash_table_init (table, abfd, newfunc)
   table->creator = abfd->xvec;
   table->undefs = NULL;
   table->undefs_tail = NULL;
+  table->type = bfd_link_generic_hash_table;
+
   return bfd_hash_table_init (&table->table, newfunc);
 }
 
@@ -601,7 +603,7 @@ bfd_wrapped_link_hash_lookup (abfd, info, string, create, copy, follow)
    struct bfd_link_hash_entry * with no explicit cast required on the
    call.  */
 
-void 
+void
 bfd_link_hash_traverse (table, func, info)
      struct bfd_link_hash_table *table;
      boolean (*func) PARAMS ((struct bfd_link_hash_entry *, PTR));
@@ -1003,10 +1005,24 @@ _bfd_generic_link_add_archive_symbols (abfd, info, checkfn)
       arh = archive_hash_lookup (&arsym_hash, h->root.string, false, false);
       if (arh == (struct archive_hash_entry *) NULL)
        {
-         pundef = &(*pundef)->next;
-         continue;
-       }
+         /* If we haven't found the exact symbol we're looking for, 
+            let's look for its import thunk */
+         if (info->pei386_auto_import)
+           {
+             char *buf = (char *) bfd_malloc (strlen (h->root.string) + 10);
+             if (buf == NULL)
+               return false;
 
+             sprintf (buf, "__imp_%s", h->root.string);
+             arh = archive_hash_lookup (&arsym_hash, buf, false, false);
+             free(buf);
+           }
+         if (arh == (struct archive_hash_entry *) NULL)
+           {
+             pundef = &(*pundef)->next;
+             continue;
+           }
+       }
       /* Look at all the objects which define this symbol.  */
       for (l = arh->defs; l != (struct archive_list *) NULL; l = l->next)
        {
@@ -1361,7 +1377,7 @@ enum link_row
 
 enum link_action
 {
-  FAIL,                /* Abort. */
+  FAIL,                /* Abort.  */
   UND,         /* Mark symbol undefined.  */
   WEAK,                /* Mark symbol weak undefined.  */
   DEF,         /* Mark symbol defined.  */
@@ -1395,7 +1411,7 @@ static const enum link_action link_action[8][8] =
   /* UNDEFW_ROW        */  {WEAK,  NOACT, NOACT, REF,   REF,   NOACT, REFC,  WARNC },
   /* DEF_ROW   */  {DEF,   DEF,   DEF,   MDEF,  DEF,   CDEF,  MDEF,  CYCLE },
   /* DEFW_ROW  */  {DEFW,  DEFW,  DEFW,  NOACT, NOACT, NOACT, NOACT, CYCLE },
-  /* COMMON_ROW        */  {COM,   COM,   COM,   CREF,  CREF,  BIG,   REFC,  WARNC },
+  /* COMMON_ROW        */  {COM,   COM,   COM,   CREF,  COM,   BIG,   REFC,  WARNC },
   /* INDR_ROW  */  {IND,   IND,   IND,   MDEF,  IND,   CIND,  MIND,  CYCLE },
   /* WARN_ROW   */  {MWARN, WARN,  WARN,  CWARN, CWARN, WARN,  CWARN, MWARN },
   /* SET_ROW   */  {SET,   SET,   SET,   SET,   SET,   SET,   CYCLE, CYCLE }
@@ -1464,7 +1480,7 @@ hash_entry_bfd (h)
      or destructor names as collect2 does.
    HASHP, if not NULL, is a place to store the created hash table
      entry; if *HASHP is not NULL, the caller has already looked up
-     the hash table entry, and stored it in *HASHP. */
+     the hash table entry, and stored it in *HASHP.  */
 
 boolean
 _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section, value,
@@ -1698,7 +1714,7 @@ _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section, value,
        case BIG:
          /* We have found a common definition for a symbol which
             already had a common definition.  Use the maximum of the
-            two sizes.  */
+            two sizes, and use the section required by the larger symbol.  */
          BFD_ASSERT (h->type == bfd_link_hash_common);
          if (! ((*info->callbacks->multiple_common)
                 (info, h->root.string,
@@ -1717,6 +1733,25 @@ _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section, value,
              if (power > 4)
                power = 4;
              h->u.c.p->alignment_power = power;
+
+             /* Some systems have special treatment for small commons,
+                hence we want to select the section used by the larger
+                symbol.  This makes sure the symbol does not go in a
+                small common section if it is now too large.  */
+             if (section == bfd_com_section_ptr)
+               {
+                 h->u.c.p->section
+                   = bfd_make_section_old_way (abfd, "COMMON");
+                 h->u.c.p->section->flags = SEC_ALLOC;
+               }
+             else if (section->owner != abfd)
+               {
+                 h->u.c.p->section
+                   = bfd_make_section_old_way (abfd, section->name);
+                 h->u.c.p->section->flags = SEC_ALLOC;
+               }
+             else
+               h->u.c.p->section = section;
            }
          break;
 
@@ -1801,6 +1836,15 @@ _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section, value,
                                                copy, false);
            if (inh == (struct bfd_link_hash_entry *) NULL)
              return false;
+           if (inh->type == bfd_link_hash_indirect
+               && inh->u.i.link == h)
+             {
+               (*_bfd_error_handler)
+                 (_("%s: indirect symbol `%s' to `%s' is a loop"),  
+                  bfd_get_filename (abfd), name, string);
+               bfd_set_error (bfd_error_invalid_operation);
+               return false;
+             }
            if (inh->type == bfd_link_hash_new)
              {
                inh->type = bfd_link_hash_undefined;
@@ -2054,7 +2098,7 @@ _bfd_generic_final_link (abfd, info)
            }
        }
     }
-  
+
   return true;
 }
 
@@ -2279,6 +2323,12 @@ _bfd_generic_link_output_symbols (output_bfd, input_bfd, info, psymalloc)
                case discard_all:
                  output = false;
                  break;
+               case discard_sec_merge:
+                 output = true;
+                 if (info->relocateable
+                     || ! (sym->section->flags & SEC_MERGE))
+                   break;
+                 /* FALLTHROUGH */
                case discard_l:
                  if (bfd_is_local_label (input_bfd, sym))
                    output = false;
@@ -2452,7 +2502,7 @@ _bfd_generic_reloc_link_order (abfd, info, sec, link_order)
   r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
   if (r == (arelent *) NULL)
     return false;
-      
+
   r->address = link_order->offset;
   r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
   if (r->howto == 0)
@@ -2524,7 +2574,7 @@ _bfd_generic_reloc_link_order (abfd, info, sec, link_order)
          break;
        }
       ok = bfd_set_section_contents (abfd, sec, (PTR) buf,
-                                    (file_ptr) 
+                                    (file_ptr)
                                      (link_order->offset *
                                       bfd_octets_per_byte (abfd)), size);
       free (buf);
@@ -2594,7 +2644,7 @@ _bfd_default_link_order (abfd, info, sec, link_order)
     case bfd_data_link_order:
       return bfd_set_section_contents (abfd, sec,
                                       (PTR) link_order->u.data.contents,
-                                      (file_ptr) 
+                                      (file_ptr)
                                        (link_order->offset *
                                         bfd_octets_per_byte (abfd)),
                                       link_order->size);
@@ -2603,7 +2653,6 @@ _bfd_default_link_order (abfd, info, sec, link_order)
 
 /* Default routine to handle a bfd_fill_link_order.  */
 
-/*ARGSUSED*/
 static boolean
 default_fill_link_order (abfd, info, sec, link_order)
      bfd *abfd;
@@ -2612,28 +2661,35 @@ default_fill_link_order (abfd, info, sec, link_order)
      struct bfd_link_order *link_order;
 {
   size_t size;
-  char *space;
+  unsigned char *space;
   size_t i;
-  int fill;
+  unsigned int fill;
+  file_ptr loc;
   boolean result;
 
   BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
 
   size = (size_t) link_order->size;
-  space = (char *) bfd_malloc (size);
-  if (space == NULL && size != 0)
+  if (size == 0)
+    return true;
+
+  space = (unsigned char *) bfd_malloc (size);
+  if (space == NULL)
     return false;
 
   fill = link_order->u.fill.value;
-  for (i = 0; i < size; i += 2)
+  for (i = 0; i < size; i += 4)
+    space[i] = fill >> 24;
+  for (i = 1; i < size; i += 4)
+    space[i] = fill >> 16;
+  for (i = 2; i < size; i += 4)
     space[i] = fill >> 8;
-  for (i = 1; i < size; i += 2)
+  for (i = 3; i < size; i += 4)
     space[i] = fill;
-  result = bfd_set_section_contents (abfd, sec, space,
-                                    (file_ptr) 
-                                     (link_order->offset * 
-                                      bfd_octets_per_byte (abfd)),
-                                    link_order->size);
+
+  loc = (file_ptr) (link_order->offset * bfd_octets_per_byte (abfd));
+  result = bfd_set_section_contents (abfd, sec, space, loc, link_order->size);
+
   free (space);
   return result;
 }
@@ -2732,7 +2788,7 @@ default_indirect_link_order (output_bfd, info, output_section, link_order,
              if (h != NULL)
                set_symbol_from_hash (sym, h);
            }
-       }         
+       }
     }
 
   /* Get and relocate the section contents.  */
@@ -2750,8 +2806,8 @@ default_indirect_link_order (output_bfd, info, output_section, link_order,
   if (! bfd_set_section_contents (output_bfd, output_section,
                                  (PTR) new_contents,
                                  (file_ptr)
-                                  (link_order->offset * 
-                                   bfd_octets_per_byte (output_bfd)), 
+                                  (link_order->offset *
+                                   bfd_octets_per_byte (output_bfd)),
                                   link_order->size))
     goto error_return;
 
@@ -2803,8 +2859,6 @@ DESCRIPTION
 
 */
 
-
-
 boolean
 _bfd_generic_link_split_section (abfd, sec)
      bfd *abfd ATTRIBUTE_UNUSED;
This page took 0.02686 seconds and 4 git commands to generate.