2009-12-15 Tristan Gingold <gingold@adacore.com>
[binutils-gdb.git] / bfd / xcofflink.c
index 97c051a2b9fe6f8bd6ec64a0dbaae1f32522fb61..6ad148b5836bbbfd1ffafbdff61ca385c4f19269 100644 (file)
@@ -1,6 +1,6 @@
 /* POWER/PowerPC XCOFF linker support.
    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
    Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -86,6 +86,12 @@ struct xcoff_archive_info
      this archive in the .loader section.  */
   const char *imppath;
   const char *impfile;
+
+  /* True if the archive contains a dynamic object.  */
+  unsigned int contains_shared_object_p : 1;
+
+  /* True if the previous field is valid.  */
+  unsigned int know_contains_shared_object_p : 1;
 };
 
 struct xcoff_link_hash_table
@@ -681,12 +687,12 @@ bfd_boolean
 bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
                             const char **imppath, const char **impfile)
 {
-  const char *basename;
+  const char *base;
   size_t length;
   char *path;
 
-  basename = lbasename (filename);
-  length = basename - filename;
+  base = lbasename (filename);
+  length = base - filename;
   if (length == 0)
     /* The filename has no directory component, so use an empty path.  */
     *imppath = "";
@@ -705,7 +711,7 @@ bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
       path[length - 1] = 0;
       *imppath = path;
     }
-  *impfile = basename;
+  *impfile = base;
   return TRUE;
 }
 
@@ -1883,6 +1889,15 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
              || sym._n._n_n._n_offset == 0)
            copy = TRUE;
 
+         /* Ignore global linkage code when linking statically.  */
+         if (info->static_link
+             && (smtyp == XTY_SD || smtyp == XTY_LD)
+             && aux.x_csect.x_smclas == XMC_GL)
+           {
+             section = bfd_und_section_ptr;
+             value = 0;
+           }
+
          /* The AIX linker appears to only detect multiple symbol
             definitions when there is a reference to the symbol.  If
             a symbol is defined multiple times, and the only
@@ -1907,8 +1922,7 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
             We also have to handle the case of statically linking a
             shared object, which will cause symbol redefinitions,
             although this is an easier case to detect.  */
-
-         if (info->output_bfd->xvec == abfd->xvec)
+         else if (info->output_bfd->xvec == abfd->xvec)
            {
              if (! bfd_is_und_section (section))
                *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
@@ -1928,23 +1942,8 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
                  && ! bfd_is_com_section (section))
                {
                  /* This is a second definition of a defined symbol.  */
-                 if ((abfd->flags & DYNAMIC) != 0
-                     && ((*sym_hash)->smclas != XMC_GL
-                         || aux.x_csect.x_smclas == XMC_GL
-                         || ((*sym_hash)->root.u.def.section->owner->flags
-                             & DYNAMIC) == 0))
-                   {
-                     /* The new symbol is from a shared library, and
-                        either the existing symbol is not global
-                        linkage code or this symbol is global linkage
-                        code.  If the existing symbol is global
-                        linkage code and the new symbol is not, then
-                        we want to use the new symbol.  */
-                     section = bfd_und_section_ptr;
-                     value = 0;
-                   }
-                 else if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
-                          && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
+                 if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
+                     && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
                    {
                      /* The existing symbol is from a shared library.
                         Replace it.  */
@@ -2043,7 +2042,9 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
            {
              int flag;
 
-             if (smtyp == XTY_ER || smtyp == XTY_CM)
+             if (smtyp == XTY_ER
+                 || smtyp == XTY_CM
+                 || section == bfd_und_section_ptr)
                flag = XCOFF_REF_REGULAR;
              else
                flag = XCOFF_DEF_REGULAR;
@@ -2465,6 +2466,21 @@ _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
     }
 }
 \f
+bfd_boolean
+_bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
+                                struct bfd_link_info *info ATTRIBUTE_UNUSED,
+                                struct bfd_link_hash_entry *harg)
+{
+  struct xcoff_link_hash_entry *h;
+
+  if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
+    return FALSE;
+
+  h = (struct xcoff_link_hash_entry *) harg;
+  h->flags |= XCOFF_DEF_REGULAR;
+  return TRUE;
+}
+\f
 /* If symbol H has not been interpreted as a function descriptor,
    see whether it should be.  Set up its descriptor information if so.  */
 
@@ -2504,14 +2520,23 @@ xcoff_find_function (struct bfd_link_info *info,
 /* Return true if the given bfd contains at least one shared object.  */
 
 static bfd_boolean
-xcoff_archive_contains_shared_object_p (bfd *archive)
+xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
+                                       bfd *archive)
 {
+  struct xcoff_archive_info *archive_info;
   bfd *member;
 
-  member = bfd_openr_next_archived_file (archive, NULL);
-  while (member != NULL && (member->flags & DYNAMIC) == 0)
-    member = bfd_openr_next_archived_file (archive, member);
-  return member != NULL;
+  archive_info = xcoff_get_archive_info (info, archive);
+  if (!archive_info->know_contains_shared_object_p)
+    {
+      member = bfd_openr_next_archived_file (archive, NULL);
+      while (member != NULL && (member->flags & DYNAMIC) == 0)
+       member = bfd_openr_next_archived_file (archive, member);
+
+      archive_info->contains_shared_object_p = (member != NULL);
+      archive_info->know_contains_shared_object_p = 1;
+    }
+  return archive_info->contains_shared_object_p;
 }
 
 /* Symbol H qualifies for export by -bexpfull.  Return true if it also
@@ -2539,7 +2564,8 @@ xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
    specified by AUTO_EXPORT_FLAGS.  */
 
 static bfd_boolean
-xcoff_auto_export_p (struct xcoff_link_hash_entry *h,
+xcoff_auto_export_p (struct bfd_link_info *info,
+                    struct xcoff_link_hash_entry *h,
                     unsigned int auto_export_flags)
 {
   /* Don't automatically export things that were explicitly exported.  */
@@ -2576,7 +2602,7 @@ xcoff_auto_export_p (struct xcoff_link_hash_entry *h,
       owner = h->root.u.def.section->owner;
       if (owner != NULL
          && owner->my_archive != NULL
-         && xcoff_archive_contains_shared_object_p (owner->my_archive))
+         && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
        return FALSE;
     }
 
@@ -2710,6 +2736,10 @@ xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
          /* We handle writing out the contents of the descriptor in
             xcoff_write_global_symbol.  */
        }
+      else if (info->static_link)
+       /* We can't get a symbol value dynamically, so just assume
+          that it's undefined.  */
+       h->flags |= XCOFF_WAS_UNDEFINED;
       else if ((h->flags & XCOFF_CALLED) != 0)
        {
          /* This is a function symbol for which we need to create
@@ -3196,7 +3226,7 @@ xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
   struct xcoff_loader_info *ldinfo;
 
   ldinfo = (struct xcoff_loader_info *) data;
-  if (xcoff_auto_export_p (h, ldinfo->auto_export_flags))
+  if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
     {
       if (!xcoff_mark_symbol (ldinfo->info, h))
        ldinfo->failed = TRUE;
@@ -3314,19 +3344,6 @@ xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
   if (h->flags & XCOFF_RTINIT)
     return TRUE;
 
-  /* If this is a final link, and the symbol was defined as a common
-     symbol in a regular object file, and there was no definition in
-     any dynamic object, then the linker will have allocated space for
-     the symbol in a common section but the XCOFF_DEF_REGULAR flag
-     will not have been set.  */
-  if (h->root.type == bfd_link_hash_defined
-      && (h->flags & XCOFF_DEF_REGULAR) == 0
-      && (h->flags & XCOFF_REF_REGULAR) != 0
-      && (h->flags & XCOFF_DEF_DYNAMIC) == 0
-      && (bfd_is_abs_section (h->root.u.def.section)
-         || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
-    h->flags |= XCOFF_DEF_REGULAR;
-
   /* We don't want to garbage collect symbols which are not defined in
      XCOFF files.  This is a convenient place to mark them.  */
   if (xcoff_hash_table (ldinfo->info)->gc
@@ -3355,7 +3372,7 @@ xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
 
   if (xcoff_hash_table (ldinfo->info)->loader_section)
     {
-      if (xcoff_auto_export_p (h, ldinfo->auto_export_flags))
+      if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
        h->flags |= XCOFF_EXPORT;
 
       if (!xcoff_build_ldsym (ldinfo, h))
@@ -4289,6 +4306,7 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
 
   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
+  sym_hash = obj_xcoff_sym_hashes (input_bfd);
   isymp = finfo->internal_syms;
   indexp = finfo->sym_indices;
   csectpp = xcoff_data (input_bfd)->csects;
@@ -4336,6 +4354,16 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
                }
            }
 
+         /* Make __rtinit C_HIDEXT rather than C_EXT.  This avoids
+            multiple definition problems when linking a shared object
+            statically.  (The native linker doesn't enter __rtinit into
+            the normal table at all, but having a local symbol can make
+            the objdump output easier to read.)  */
+         if (isym.n_sclass == C_EXT
+             && *sym_hash
+             && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
+           isym.n_sclass = C_HIDEXT;
+
          /* The value of a C_FILE symbol is the symbol index of the
             next C_FILE symbol.  The value of the last C_FILE symbol
             is -1.  We try to get this right, below, just before we
@@ -4664,6 +4692,7 @@ xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
            }
        }
 
+      sym_hash += add;
       indexp += add;
       isymp += add;
       csectpp += add;