build as needed
[platform/upstream/binutils.git] / ld / ldmain.c
index 3bdaf4d..a507680 100644 (file)
@@ -1,7 +1,5 @@
 /* Main program of GNU linker.
-   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
-   2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
-   Free Software Foundation, Inc.
+   Copyright (C) 1991-2014 Free Software Foundation, Inc.
    Written by Steve Chamberlain steve@cygnus.com
 
    This file is part of the GNU Binutils.
@@ -82,14 +80,14 @@ const char *ld_sysroot;
 char * ld_canon_sysroot;
 int ld_canon_sysroot_len;
 
-/* Set by -G argument, for MIPS ECOFF target.  */
+/* Set by -G argument, for targets like MIPS ELF.  */
 int g_switch_value = 8;
 
 /* Nonzero means print names of input files as processed.  */
 bfd_boolean trace_files;
 
-/* Nonzero means same, but note open failures, too.  */
-bfd_boolean trace_file_tries;
+/* Nonzero means report actions taken by the linker, and describe the linker script in use.  */
+bfd_boolean verbose;
 
 /* Nonzero means version number was printed, so exit successfully
    instead of complaining if no input files are given.  */
@@ -139,7 +137,7 @@ static bfd_boolean unattached_reloc
   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
 static bfd_boolean notice
   (struct bfd_link_info *, struct bfd_link_hash_entry *,
-   bfd *, asection *, bfd_vma, flagword, const char *);
+   struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword);
 
 static struct bfd_link_callbacks link_callbacks =
 {
@@ -191,6 +189,9 @@ main (int argc, char **argv)
 {
   char *emulation;
   long start_time = get_run_time ();
+#ifdef HAVE_SBRK
+  char *start_sbrk = (char *) sbrk (0);
+#endif
 
 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
   setlocale (LC_MESSAGES, "");
@@ -260,11 +261,11 @@ main (int argc, char **argv)
   config.make_executable = TRUE;
   config.magic_demand_paged = TRUE;
   config.text_read_only = TRUE;
+  link_info.disable_target_specific_optimizations = -1;
 
   command_line.warn_mismatch = TRUE;
   command_line.warn_search_mismatch = TRUE;
   command_line.check_section_addresses = -1;
-  command_line.disable_target_specific_optimizations = -1;
 
   /* We initialize DEMANGLING based on the environment variable
      COLLECT_NO_DEMANGLE.  The gcc collect2 program will demangle the
@@ -275,9 +276,11 @@ main (int argc, char **argv)
 
   link_info.allow_undefined_version = TRUE;
   link_info.keep_memory = TRUE;
+  link_info.new_dtags = TRUE;
   link_info.combreloc = TRUE;
   link_info.strip_discarded = TRUE;
   link_info.emit_hash = TRUE;
+  link_info.emit_gnu_hash = TRUE;
   link_info.callbacks = &link_callbacks;
   link_info.input_bfds_tail = &link_info.input_bfds;
   /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
@@ -288,6 +291,8 @@ main (int argc, char **argv)
   link_info.pei386_auto_import = -1;
   link_info.spare_dynamic_tags = 5;
   link_info.path_separator = ':';
+  if (getenv ("LD_AS_NEEDED") && atoi(getenv ("LD_AS_NEEDED")) > 0)
+    input_flags.add_DT_NEEDED_for_regular = TRUE;
 
   ldfile_add_arch ("");
   emulation = get_emulation (argc, argv);
@@ -305,8 +310,7 @@ main (int argc, char **argv)
 
 #ifdef ENABLE_PLUGINS
   /* Now all the plugin arguments have been gathered, we can load them.  */
-  if (plugin_load_plugins ())
-    einfo (_("%P%F: %s: error loading plugin\n"), plugin_error_plugin ());
+  plugin_load_plugins ();
 #endif /* ENABLE_PLUGINS */
 
   ldemul_set_symbols ();
@@ -340,7 +344,7 @@ main (int argc, char **argv)
       lex_string = NULL;
     }
 
-  if (trace_file_tries)
+  if (verbose)
     {
       if (saved_script_handle)
        info_msg (_("using external linker script:"));
@@ -378,6 +382,13 @@ main (int argc, char **argv)
 
   lang_final ();
 
+  /* If the only command line argument has been -v or --version or --verbose
+     then ignore any input files provided by linker scripts and exit now.
+     We do not want to create an output file when the linker is just invoked
+     to provide version information.  */
+  if (argc == 2 && version_printed)
+    xexit (0);
+
   if (!lang_has_input_file)
     {
       if (version_printed || command_line.print_output_format)
@@ -425,7 +436,14 @@ main (int argc, char **argv)
     output_cref (config.map_file != NULL ? config.map_file : stdout);
   if (nocrossref_list != NULL)
     check_nocrossrefs ();
+#if 0
+  {
+    struct bfd_link_hash_entry * h;
 
+    h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
+    fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
+  }
+#endif
   lang_finish ();
 
   /* Even if we're producing relocatable output, some non-fatal errors should
@@ -505,7 +523,7 @@ main (int argc, char **argv)
               program_name, run_time / 1000000, run_time % 1000000);
 #ifdef HAVE_SBRK
       fprintf (stderr, _("%s: data size %ld\n"), program_name,
-              (long) (lim - (char *) &environ));
+              (long) (lim - start_sbrk));
 #endif
       fflush (stderr);
     }
@@ -597,8 +615,10 @@ get_emulation (int argc, char **argv)
                   || strcmp (argv[i], "-mips5") == 0
                   || strcmp (argv[i], "-mips32") == 0
                   || strcmp (argv[i], "-mips32r2") == 0
+                  || strcmp (argv[i], "-mips32r6") == 0
                   || strcmp (argv[i], "-mips64") == 0
-                  || strcmp (argv[i], "-mips64r2") == 0)
+                  || strcmp (argv[i], "-mips64r2") == 0
+                  || strcmp (argv[i], "-mips64r6") == 0)
            {
              /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
                 passed to the linker by some MIPS compilers.  They
@@ -643,6 +663,23 @@ add_ysym (const char *name)
     einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
 }
 
+void
+add_ignoresym (struct bfd_link_info *info, const char *name)
+{
+  if (info->ignore_hash == NULL)
+    {
+      info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table));
+      if (! bfd_hash_table_init_n (info->ignore_hash,
+                                  bfd_hash_newfunc,
+                                  sizeof (struct bfd_hash_entry),
+                                  61))
+       einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
+    }
+
+  if (bfd_hash_lookup (info->ignore_hash, name, TRUE, TRUE) == NULL)
+    einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
+}
+
 /* Record a symbol to be wrapped, from the --wrap option.  */
 
 void
@@ -727,6 +764,7 @@ add_keepsyms_file (const char *filename)
 
   free (buf);
   link_info.strip = strip_some;
+  fclose (file);
 }
 \f
 /* Callbacks from the BFD linker routines.  */
@@ -823,7 +861,8 @@ add_archive_element (struct bfd_link_info *info,
        {
          char buf[100];
 
-         sprintf (buf, _("Archive member included because of file (symbol)\n\n"));
+         sprintf (buf, _("Archive member included "
+                         "to satisfy reference by file (symbol)\n\n"));
          minfo ("%s", buf);
          header_printed = TRUE;
        }
@@ -861,7 +900,7 @@ add_archive_element (struct bfd_link_info *info,
        minfo ("(%s)\n", name);
     }
 
-  if (trace_files || trace_file_tries)
+  if (trace_files || verbose)
     info_msg ("%I\n", &orig_input);
   return TRUE;
 }
@@ -934,10 +973,10 @@ multiple_definition (struct bfd_link_info *info,
   if (obfd != NULL)
     einfo (_("%D: first defined here\n"), obfd, osec, oval);
 
-  if (RELAXATION_ENABLED)
+  if (RELAXATION_ENABLED_BY_USER)
     {
       einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
-      command_line.disable_target_specific_optimizations = -1;
+      DISABLE_RELAXATION;
     }
 
   return TRUE;
@@ -1091,7 +1130,7 @@ constructor_callback (struct bfd_link_info *info,
 
   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
      useful error message.  */
-  if (bfd_reloc_type_lookup (link_info.output_bfd, BFD_RELOC_CTOR) == NULL
+  if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
       && (info->relocatable
          || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
     einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
@@ -1131,6 +1170,25 @@ struct warning_callback_info
   asymbol **asymbols;
 };
 
+/* Look through the relocs to see if we can find a plausible address
+   for SYMBOL in ABFD.  Return TRUE if found.  Otherwise return FALSE.  */
+
+static bfd_boolean
+symbol_warning (const char *warning, const char *symbol, bfd *abfd)
+{
+  struct warning_callback_info cinfo;
+
+  if (!bfd_generic_link_read_symbols (abfd))
+    einfo (_("%B%F: could not read symbols: %E\n"), abfd);
+
+  cinfo.found = FALSE;
+  cinfo.warning = warning;
+  cinfo.symbol = symbol;
+  cinfo.asymbols = bfd_get_outsymbols (abfd);
+  bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
+  return cinfo.found;
+}
+
 /* This is called when there is a reference to a warning symbol.  */
 
 static bfd_boolean
@@ -1153,24 +1211,14 @@ warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
     einfo ("%P: %s%s\n", _("warning: "), warning);
   else if (symbol == NULL)
     einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
-  else
+  else if (! symbol_warning (warning, symbol, abfd))
     {
-      struct warning_callback_info cinfo;
-
-      /* Look through the relocs to see if we can find a plausible
-        address.  */
-
-      if (!bfd_generic_link_read_symbols (abfd))
-       einfo (_("%B%F: could not read symbols: %E\n"), abfd);
-
-      cinfo.found = FALSE;
-      cinfo.warning = warning;
-      cinfo.symbol = symbol;
-      cinfo.asymbols = bfd_get_outsymbols (abfd);
-      bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
-
-      if (! cinfo.found)
-       einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
+      bfd *b;
+      /* Search all input files for a reference to SYMBOL.  */
+      for (b = info->input_bfds; b; b = b->link.next)
+       if (b != abfd && symbol_warning (warning, symbol, b))
+         return TRUE;
+      einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
     }
 
   return TRUE;
@@ -1228,7 +1276,7 @@ warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
 /* This is called when an undefined symbol is found.  */
 
 static bfd_boolean
-undefined_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
+undefined_symbol (struct bfd_link_info *info,
                  const char *name,
                  bfd *abfd,
                  asection *section,
@@ -1240,25 +1288,14 @@ undefined_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
 
 #define MAX_ERRORS_IN_A_ROW 5
 
+  if (info->ignore_hash != NULL
+      && bfd_hash_lookup (info->ignore_hash, name, FALSE, FALSE) != NULL)
+    return TRUE;
+
   if (config.warn_once)
     {
-      static struct bfd_hash_table *hash;
-
       /* Only warn once about a particular undefined symbol.  */
-      if (hash == NULL)
-       {
-         hash = (struct bfd_hash_table *)
-              xmalloc (sizeof (struct bfd_hash_table));
-         if (!bfd_hash_table_init (hash, bfd_hash_newfunc,
-                                   sizeof (struct bfd_hash_entry)))
-           einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
-       }
-
-      if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL)
-       return TRUE;
-
-      if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL)
-       einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
+      add_ignoresym (info, name);
     }
 
   /* We never print more than a reasonable number of errors in a row
@@ -1336,7 +1373,7 @@ int overflow_cutoff_limit = 10;
 /* This is called when a reloc overflows.  */
 
 static bfd_boolean
-reloc_overflow (struct bfd_link_info *info ATTRIBUTE_UNUSED,
+reloc_overflow (struct bfd_link_info *info,
                struct bfd_link_hash_entry *entry,
                const char *name,
                const char *reloc_name,
@@ -1375,7 +1412,7 @@ reloc_overflow (struct bfd_link_info *info ATTRIBUTE_UNUSED,
                 reloc_name, entry->root.string,
                 entry->u.def.section,
                 entry->u.def.section == bfd_abs_section_ptr
-                ? link_info.output_bfd : entry->u.def.section->owner);
+                ? info->output_bfd : entry->u.def.section->owner);
          break;
        default:
          abort ();
@@ -1430,11 +1467,11 @@ unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
 static bfd_boolean
 notice (struct bfd_link_info *info,
        struct bfd_link_hash_entry *h,
+       struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
        bfd *abfd,
        asection *section,
        bfd_vma value,
-       flagword flags ATTRIBUTE_UNUSED,
-       const char *string ATTRIBUTE_UNUSED)
+       flagword flags ATTRIBUTE_UNUSED)
 {
   const char *name;