2001-08-09 H.J. Lu <hjl@gnu.org>
[deliverable/binutils-gdb.git] / bfd / cofflink.c
index b648fa19eea1665eae450fae91012a59d0617e1d..e6dcd88edc17670b832e6974bb8127d937081c0f 100644 (file)
@@ -1,5 +1,6 @@
 /* COFF specific linker code.
-   Copyright 1994, 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
+   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
+   Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Cygnus Support.
 
 This file is part of BFD, the Binary File Descriptor library.
@@ -40,6 +41,15 @@ static int process_embedded_commands
   PARAMS ((bfd *, struct bfd_link_info *, bfd *));
 static void mark_relocs PARAMS ((struct coff_final_link_info *, bfd *));
 
+/* Return true if SYM is a weak, external symbol.  */
+#define IS_WEAK_EXTERNAL(abfd, sym)                    \
+  ((sym).n_sclass == C_WEAKEXT                         \
+   || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
+
+/* Return true if SYM is an external symbol.  */
+#define IS_EXTERNAL(abfd, sym)                         \
+  ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
+
 /* Define macros so that the ISFCN, et. al., macros work correctly.
    These macros are defined in include/coff/internal.h in terms of
    N_TMASK, etc.  These definitions require a user to define local
@@ -267,6 +277,16 @@ coff_link_check_ar_symbols (abfd, info, pneeded)
            return false;
          h = bfd_link_hash_lookup (info->hash, name, false, false, true);
 
+         /* auto import */
+         if (!h && info->pei386_auto_import)
+           {
+             if (!strncmp (name,"__imp_", 6))
+               {
+                 h =
+                    bfd_link_hash_lookup (info->hash, name + 6, false, false,
+                                          true);
+               }
+           }
          /* We are only interested in symbols that are currently
             undefined.  If a symbol is currently known to be common,
             COFF linkers do not bring in an object file which defines
@@ -395,8 +415,7 @@ coff_link_add_symbols (abfd, info)
              break;
            }
 
-         if (sym.n_sclass == C_WEAKEXT
-             || (obj_pe (abfd) && sym.n_sclass == C_NT_WEAK))
+         if (IS_WEAK_EXTERNAL (abfd, sym))
            flags = BSF_WEAK;
 
          addit = true;
@@ -920,7 +939,7 @@ _bfd_coff_final_link (abfd, info)
 
   if (! bfd_coff_final_link_postscript (abfd, & finfo))
     goto error_return;
-  
+
   /* Free up the buffers used by _bfd_coff_link_input_bfd.  */
 
   coff_debug_merge_hash_table_free (&finfo.debug_merge);
@@ -981,7 +1000,7 @@ _bfd_coff_final_link (abfd, info)
 
   /* If doing task linking (ld --task-link) then make a pass through the
      global symbols, writing out any that are defined, and making them
-     static. */
+     static.  */
   if (info->task_link)
     {
       finfo.failed = false;
@@ -1094,6 +1113,8 @@ _bfd_coff_final_link (abfd, info)
 
       if (! _bfd_stringtab_emit (abfd, finfo.strtab))
        return false;
+
+      obj_coff_strings_written (abfd) = true;
     }
 
   _bfd_stringtab_free (finfo.strtab);
@@ -1151,7 +1172,7 @@ dores_com (ptr, output_bfd, heap)
      bfd *output_bfd;
      int heap;
 {
-  if (coff_data(output_bfd)->pe) 
+  if (coff_data(output_bfd)->pe)
     {
       int val = strtoul (ptr, &ptr, 0);
       if (heap)
@@ -1159,7 +1180,7 @@ dores_com (ptr, output_bfd, heap)
       else
        pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve =val;
 
-      if (ptr[0] == ',') 
+      if (ptr[0] == ',')
        {
          int val = strtoul (ptr+1, &ptr, 0);
          if (heap)
@@ -1185,7 +1206,7 @@ char **dst;
 }
 
 /* Process any magic embedded commands in a section called .drectve */
-                       
+
 static int
 process_embedded_commands (output_bfd, info,  abfd)
      bfd *output_bfd;
@@ -1196,19 +1217,19 @@ process_embedded_commands (output_bfd, info,  abfd)
   char *s;
   char *e;
   char *copy;
-  if (!sec) 
+  if (!sec)
     return 1;
-  
+
   copy = bfd_malloc ((size_t) sec->_raw_size);
-  if (!copy) 
+  if (!copy)
     return 0;
-  if (! bfd_get_section_contents(abfd, sec, copy, 0, sec->_raw_size)) 
+  if (! bfd_get_section_contents(abfd, sec, copy, 0, sec->_raw_size))
     {
       free (copy);
       return 0;
     }
   e = copy + sec->_raw_size;
-  for (s = copy;  s < e ; ) 
+  for (s = copy;  s < e ; )
     {
       if (s[0]!= '-') {
        s++;
@@ -1229,7 +1250,7 @@ process_embedded_commands (output_bfd, info,  abfd)
          s = get_name(s, &name);
          s = get_name(s, &attribs);
          while (loop) {
-           switch (*attribs++) 
+           switch (*attribs++)
              {
              case 'W':
                had_write = 1;
@@ -1263,7 +1284,7 @@ process_embedded_commands (output_bfd, info,  abfd)
        {
          s = dores_com (s+6, output_bfd, 0);
        }
-      else 
+      else
        s++;
     }
   free (copy);
@@ -1273,7 +1294,7 @@ process_embedded_commands (output_bfd, info,  abfd)
 /* Place a marker against all symbols which are used by relocations.
    This marker can be picked up by the 'do we skip this symbol ?'
    loop in _bfd_coff_link_input_bfd() and used to prevent skipping
-   that symbol. 
+   that symbol.
    */
 
 static void
@@ -1285,14 +1306,13 @@ mark_relocs (finfo, input_bfd)
 
   if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
     return;
-  
+
   for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
     {
       struct internal_reloc *  internal_relocs;
       struct internal_reloc *  irel;
       struct internal_reloc *  irelend;
 
-      
       if ((a->flags & SEC_RELOC) == 0 || a->reloc_count  < 1)
        continue;
 
@@ -1305,7 +1325,7 @@ mark_relocs (finfo, input_bfd)
          ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
          : finfo->internal_relocs)
        );
-      
+
       if (internal_relocs == NULL)
        continue;
 
@@ -1316,7 +1336,7 @@ mark_relocs (finfo, input_bfd)
         been initialised to 0) for all of the symbols that are used
         in the relocation table.  This will then be picked up in the
         skip/don't pass */
-      
+
       for (; irel < irelend; irel++)
        {
          finfo->sym_indices[ irel->r_symndx ] = -1;
@@ -1399,8 +1419,8 @@ _bfd_coff_link_input_bfd (finfo, input_bfd)
       && finfo->info->relocateable)
     {
       /* mark the symbol array as 'not-used' */
-      memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp); 
-       
+      memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
+
       mark_relocs (finfo, input_bfd);
     }
 
@@ -1447,7 +1467,7 @@ _bfd_coff_link_input_bfd (finfo, input_bfd)
        dont_skip_symbol = *indexp;
       else
        dont_skip_symbol = false;
-      
+
       *indexp = -1;
 
       skip = false;
@@ -1491,6 +1511,21 @@ _bfd_coff_link_input_bfd (finfo, input_bfd)
            }
        }
 
+#ifndef COFF_WITH_PE
+      /* Skip section symbols for sections which are not going to be
+        emitted, or which belong to linkonce sections that are going
+        to be discarded.  */
+      if (!skip
+         && isym.n_sclass == C_STAT
+         && isym.n_type == T_NULL
+          && isym.n_numaux > 0)
+        {
+          if ((*secpp)->output_section == bfd_abs_section_ptr
+              || (*secpp)->kept_section)
+            skip = true;
+        }
+#endif
+
       /* If we stripping debugging symbols, and this is a debugging
          symbol, then skip it.  FIXME: gas sets the section to N_ABS
          for some types of debugging symbols; I don't know if this is
@@ -1833,12 +1868,8 @@ _bfd_coff_link_input_bfd (finfo, input_bfd)
            }
 
          /* If doing task linking, convert normal global function symbols to
-            static functions. */
-
-         if (finfo->info->task_link
-             && (isym.n_sclass == C_EXT
-                 || isym.n_sclass == C_WEAKEXT
-                 || (obj_pe (input_bfd) && isym.n_sclass == C_NT_WEAK)))
+            static functions.  */
+         if (finfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
            isym.n_sclass = C_STAT;
 
          /* Output the symbol.  */
@@ -2201,7 +2232,7 @@ _bfd_coff_link_input_bfd (finfo, input_bfd)
                                                 is.n_numaux, auxptr);
                        }
 
-                       skipping = false;
+                     skipping = false;
                    }
 
                  iline.l_addr.l_symndx = indx;
@@ -2399,8 +2430,8 @@ _bfd_coff_link_input_bfd (finfo, input_bfd)
                          /* This reloc is against a symbol we are
                              stripping.  This should have been handled
                             by the 'dont_skip_symbol' code in the while
-                            loop at the top of this function. */
-                         
+                            loop at the top of this function.  */
+
                          is = finfo->internal_syms + irel->r_symndx;
 
                          name = (_bfd_coff_internal_syment_name
@@ -2424,9 +2455,9 @@ _bfd_coff_link_input_bfd (finfo, input_bfd)
       if (secdata == NULL || secdata->stab_info == NULL)
        {
          if (! bfd_set_section_contents (output_bfd, o->output_section,
-                                         contents, 
-                                          (file_ptr) 
-                                          (o->output_offset * 
+                                         contents,
+                                          (file_ptr)
+                                          (o->output_offset *
                                            bfd_octets_per_byte (output_bfd)),
                                          (o->_cooked_size != 0
                                           ? o->_cooked_size
@@ -2548,20 +2579,25 @@ _bfd_coff_write_global_sym (h, data)
   /* If doing task linking and this is the pass where we convert
      defined globals to statics, then do that conversion now.  If the
      symbol is not being converted, just ignore it and it will be
-     output during a later pass. */
+     output during a later pass.  */
   if (finfo->global_to_static)
     {
-      if (isym.n_sclass != C_EXT
-         && isym.n_sclass != C_WEAKEXT
-         && (! obj_pe (output_bfd) || isym.n_sclass != C_NT_WEAK))
-       {
-         return true;
-       }
+      if (! IS_EXTERNAL (output_bfd, isym))
+       return true;
+
       isym.n_sclass = C_STAT;
     }
 
+  /* When a weak symbol is not overriden by a strong one,
+     turn it into an external symbol when not building a
+     shared or relocateable object.  */
+  if (! finfo->info->shared
+      && ! finfo->info->relocateable
+      && IS_WEAK_EXTERNAL (finfo->output_bfd, isym))
+    isym.n_sclass = C_EXT;
+
   isym.n_numaux = h->numaux;
-  
+
   bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
 
   symesz = bfd_coff_symesz (output_bfd);
@@ -2653,7 +2689,7 @@ _bfd_coff_write_global_sym (h, data)
 
 /* Write out task global symbols, converting them to statics.  Called
    via coff_link_hash_traverse.  Calls bfd_coff_write_global_sym to do
-   the dirty work, if the symbol we are processing needs conversion. */
+   the dirty work, if the symbol we are processing needs conversion.  */
 
 boolean
 _bfd_coff_write_task_globals (h, data)
@@ -2739,7 +2775,7 @@ _bfd_coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
          break;
        }
       ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
-                                    (file_ptr) 
+                                    (file_ptr)
                                      (link_order->offset *
                                       bfd_octets_per_byte (output_bfd)), size);
       free (buf);
@@ -2861,7 +2897,7 @@ _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
          return false;
        }
       else
-       {    
+       {
          h = obj_coff_sym_hashes (input_bfd)[symndx];
          sym = syms + symndx;
        }
@@ -2876,7 +2912,6 @@ _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
       else
        addend = 0;
 
-
       howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
                                       sym, &addend);
       if (howto == NULL)
@@ -2928,6 +2963,9 @@ _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
                     + sec->output_offset);
              }
 
+         else if (h->root.type == bfd_link_hash_undefweak)
+           val = 0;
+
          else if (! info->relocateable)
            {
              if (! ((*info->callbacks->undefined_symbol)
@@ -2939,7 +2977,7 @@ _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
 
       if (info->base_file)
        {
-         /* Emit a reloc if the backend thinks it needs it. */
+         /* Emit a reloc if the backend thinks it needs it.  */
          if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
            {
              /* Relocation to a symbol in a section which isn't
@@ -2948,9 +2986,9 @@ _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
                 reloc section.  Note that the base file is not
                 portable between systems.  We write out a long here,
                 and dlltool reads in a long.  */
-             long addr = (rel->r_vaddr 
-                          - input_section->vma 
-                          + input_section->output_offset 
+             long addr = (rel->r_vaddr
+                          - input_section->vma
+                          + input_section->output_offset
                           + input_section->output_section->vma);
              if (coff_data (output_bfd)->pe)
                addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
@@ -2962,7 +3000,7 @@ _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
                }
            }
        }
-  
+
       rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
                                        contents,
                                        rel->r_vaddr - input_section->vma,
@@ -3006,4 +3044,3 @@ _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
     }
   return true;
 }
-
This page took 0.031763 seconds and 4 git commands to generate.