* emultempl/ppc64elf.em: Convert to C90 function definitions, remove
[deliverable/binutils-gdb.git] / ld / ldmain.c
index 03a5c5acdcca0d57e16571092f20c8119fa8d410..08a0ec4bc6b5a9ae25aab30da4a18e91c61ffb27 100644 (file)
@@ -1,6 +1,6 @@
 /* Main program of GNU linker.
    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
-   2002
+   2002, 2003
    Free Software Foundation, Inc.
    Written by Steve Chamberlain steve@cygnus.com
 
@@ -42,7 +42,7 @@
 #include "ldemul.h"
 #include "ldctor.h"
 
-/* Somewhere above, sys/stat.h got included . . . .  */
+/* Somewhere above, sys/stat.h got included.  */
 #if !defined(S_ISDIR) && defined(S_IFDIR)
 #define        S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
 #endif
@@ -55,10 +55,11 @@ extern PTR sbrk ();
 #endif
 #endif
 
-int main PARAMS ((int, char **));
+#ifndef TARGET_SYSTEM_ROOT
+#define TARGET_SYSTEM_ROOT ""
+#endif
 
-static char *get_emulation PARAMS ((int, char **));
-static void set_scripts_dir PARAMS ((void));
+int main PARAMS ((int, char **));
 
 /* EXPORTS */
 
@@ -68,6 +69,13 @@ const char *output_filename = "a.out";
 /* Name this program was invoked by.  */
 char *program_name;
 
+/* The prefix for system library directories.  */
+char *ld_sysroot;
+
+/* The canonical representation of ld_sysroot.  */
+char * ld_canon_sysroot;
+int ld_canon_sysroot_len;
+
 /* The file that we're creating.  */
 bfd *output_bfd = 0;
 
@@ -94,46 +102,49 @@ args_type command_line;
 
 ld_config_type config;
 
-static void remove_output PARAMS ((void));
-static bfd_boolean check_for_scripts_dir PARAMS ((char *dir));
-static bfd_boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
-                                           const char *));
-static bfd_boolean multiple_definition PARAMS ((struct bfd_link_info *,
-                                           const char *,
-                                           bfd *, asection *, bfd_vma,
-                                           bfd *, asection *, bfd_vma));
-static bfd_boolean multiple_common PARAMS ((struct bfd_link_info *,
-                                       const char *, bfd *,
-                                       enum bfd_link_hash_type, bfd_vma,
-                                       bfd *, enum bfd_link_hash_type,
-                                       bfd_vma));
-static bfd_boolean add_to_set PARAMS ((struct bfd_link_info *,
-                                  struct bfd_link_hash_entry *,
-                                  bfd_reloc_code_real_type,
-                                  bfd *, asection *, bfd_vma));
-static bfd_boolean constructor_callback PARAMS ((struct bfd_link_info *,
-                                            bfd_boolean constructor,
-                                            const char *name,
-                                            bfd *, asection *, bfd_vma));
-static bfd_boolean warning_callback PARAMS ((struct bfd_link_info *,
-                                        const char *, const char *, bfd *,
-                                        asection *, bfd_vma));
-static void warning_find_reloc PARAMS ((bfd *, asection *, PTR));
-static bfd_boolean undefined_symbol PARAMS ((struct bfd_link_info *,
-                                        const char *, bfd *,
-                                        asection *, bfd_vma, bfd_boolean));
-static bfd_boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
-                                      const char *, bfd_vma,
-                                      bfd *, asection *, bfd_vma));
-static bfd_boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
-                                       bfd *, asection *, bfd_vma));
-static bfd_boolean unattached_reloc PARAMS ((struct bfd_link_info *,
-                                        const char *, bfd *, asection *,
-                                        bfd_vma));
-static bfd_boolean notice PARAMS ((struct bfd_link_info *, const char *,
-                              bfd *, asection *, bfd_vma));
-
-static struct bfd_link_callbacks link_callbacks = {
+static char *get_emulation
+  PARAMS ((int, char **));
+static void set_scripts_dir
+  PARAMS ((void));
+static void remove_output
+  PARAMS ((void));
+static bfd_boolean check_for_scripts_dir
+  PARAMS ((char *));
+static bfd_boolean add_archive_element
+  PARAMS ((struct bfd_link_info *, bfd *, const char *));
+static bfd_boolean multiple_definition
+  PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
+          bfd *, asection *, bfd_vma));
+static bfd_boolean multiple_common
+  PARAMS ((struct bfd_link_info *, const char *, bfd *,
+          enum bfd_link_hash_type, bfd_vma, bfd *, enum bfd_link_hash_type,
+          bfd_vma));
+static bfd_boolean add_to_set
+  PARAMS ((struct bfd_link_info *, struct bfd_link_hash_entry *,
+          bfd_reloc_code_real_type, bfd *, asection *, bfd_vma));
+static bfd_boolean constructor_callback
+  PARAMS ((struct bfd_link_info *, bfd_boolean, const char *, bfd *,
+          asection *, bfd_vma));
+static bfd_boolean warning_callback
+  PARAMS ((struct bfd_link_info *, const char *, const char *, bfd *,
+          asection *, bfd_vma));
+static void warning_find_reloc
+  PARAMS ((bfd *, asection *, PTR));
+static bfd_boolean undefined_symbol
+  PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
+          bfd_boolean));
+static bfd_boolean reloc_overflow
+  PARAMS ((struct bfd_link_info *, const char *, const char *, bfd_vma,
+          bfd *, asection *, bfd_vma));
+static bfd_boolean reloc_dangerous
+  PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
+static bfd_boolean unattached_reloc
+  PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
+static bfd_boolean notice
+  PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
+
+static struct bfd_link_callbacks link_callbacks =
+{
   add_archive_element,
   multiple_definition,
   multiple_common,
@@ -144,7 +155,8 @@ static struct bfd_link_callbacks link_callbacks = {
   reloc_overflow,
   reloc_dangerous,
   unattached_reloc,
-  notice
+  notice,
+  error_handler
 };
 
 struct bfd_link_info link_info;
@@ -189,6 +201,52 @@ main (argc, argv)
 
   xatexit (remove_output);
 
+#ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
+  ld_sysroot = make_relative_prefix (program_name, BINDIR,
+                                    TARGET_SYSTEM_ROOT);
+
+  if (ld_sysroot)
+    {
+      struct stat s;
+      int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode);
+
+      if (!res)
+       {
+         free (ld_sysroot);
+         ld_sysroot = NULL;
+       }
+    }
+
+  if (! ld_sysroot)
+    {
+      ld_sysroot = make_relative_prefix (program_name, TOOLBINDIR,
+                                        TARGET_SYSTEM_ROOT);
+
+      if (ld_sysroot)
+       {
+         struct stat s;
+         int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode);
+
+         if (!res)
+           {
+             free (ld_sysroot);
+             ld_sysroot = NULL;
+           }
+       }
+    }
+
+  if (! ld_sysroot)
+#endif
+    ld_sysroot = TARGET_SYSTEM_ROOT;
+
+  if (ld_sysroot && *ld_sysroot)
+    ld_canon_sysroot = lrealpath (ld_sysroot);
+
+  if (ld_canon_sysroot)
+    ld_canon_sysroot_len = strlen (ld_canon_sysroot);
+  else
+    ld_canon_sysroot_len = -1;
+
   /* Set the default BFD target based on the configured target.  Doing
      this permits the linker to be configured for a particular target,
      and linked against a shared BFD library which was configured for
@@ -233,13 +291,15 @@ main (argc, argv)
   link_info.emitrelocations = FALSE;
   link_info.task_link = FALSE;
   link_info.shared = FALSE;
+  link_info.pie = FALSE;
+  link_info.executable = FALSE;
   link_info.symbolic = FALSE;
   link_info.export_dynamic = FALSE;
   link_info.static_link = FALSE;
   link_info.traditional_format = FALSE;
   link_info.optimize = FALSE;
   link_info.no_undefined = FALSE;
-  link_info.allow_shlib_undefined = FALSE;
+  link_info.allow_shlib_undefined = TRUE;
   link_info.allow_multiple_definition = FALSE;
   link_info.allow_undefined_version = TRUE;
   link_info.keep_memory = TRUE;
@@ -248,6 +308,7 @@ main (argc, argv)
   link_info.new_dtags = FALSE;
   link_info.combreloc = TRUE;
   link_info.eh_frame_hdr = FALSE;
+  link_info.strip_discarded = TRUE;
   link_info.strip = strip_none;
   link_info.discard = discard_sec_merge;
   link_info.common_skip_ar_aymbols = bfd_link_common_skip_none;
@@ -270,6 +331,7 @@ main (argc, argv)
   link_info.spare_dynamic_tags = 5;
   link_info.flags = (bfd_vma) 0;
   link_info.flags_1 = (bfd_vma) 0;
+  link_info.relax_finalizing = FALSE;
 
   ldfile_add_arch ("");
 
@@ -308,6 +370,9 @@ main (argc, argv)
        einfo (_("%P%F: -f may not be used without -shared\n"));
     }
 
+  if (! link_info.shared || link_info.pie)
+    link_info.executable = TRUE;
+
   /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols).  I
      don't see how else this can be handled, since in this case we
      must preserve all externally visible symbols.  */
@@ -384,9 +449,7 @@ main (argc, argv)
     }
 
   if (trace_files)
-    {
-      info_msg (_("%P: mode %s\n"), emulation);
-    }
+    info_msg (_("%P: mode %s\n"), emulation);
 
   ldemul_after_parse ();
 
@@ -412,7 +475,6 @@ main (argc, argv)
 
   /* Print error messages for any missing symbols, for any warning
      symbols, and possibly multiple definitions.  */
-
   if (link_info.relocateable)
     output_bfd->flags &= ~EXEC_P;
   else
@@ -430,17 +492,13 @@ main (argc, argv)
   /* Even if we're producing relocateable output, some non-fatal errors should
      be reported in the exit status.  (What non-fatal errors, if any, do we
      want to ignore for relocateable output?)  */
-
   if (!config.make_executable && !force_make_executable)
     {
       if (trace_files)
-       {
-         einfo (_("%P: link errors found, deleting executable `%s'\n"),
-                output_filename);
-       }
+       einfo (_("%P: link errors found, deleting executable `%s'\n"),
+              output_filename);
 
       /* The file will be removed by remove_output.  */
-
       xexit (1);
     }
   else
@@ -454,6 +512,7 @@ main (argc, argv)
       if (! link_info.relocateable && command_line.force_exe_suffix)
        {
          int len = strlen (output_filename);
+
          if (len < 4
              || (strcasecmp (output_filename + len - 4, ".exe") != 0
                  && strcasecmp (output_filename + len - 4, ".dll") != 0))
@@ -464,6 +523,7 @@ main (argc, argv)
              char *buf = xmalloc (bsize);
              int l;
              char *dst_name = xmalloc (len + 5);
+
              strcpy (dst_name, output_filename);
              strcat (dst_name, ".exe");
              src = fopen (output_filename, FOPEN_RB);
@@ -476,16 +536,14 @@ main (argc, argv)
              while ((l = fread (buf, 1, bsize, src)) > 0)
                {
                  int done = fwrite (buf, 1, l, dst);
+
                  if (done != l)
-                   {
-                     einfo (_("%P: Error writing file `%s'\n"), dst_name);
-                   }
+                   einfo (_("%P: Error writing file `%s'\n"), dst_name);
                }
+
              fclose (src);
              if (fclose (dst) == EOF)
-               {
-                 einfo (_("%P: Error closing file `%s'\n"), dst_name);
-               }
+               einfo (_("%P: Error closing file `%s'\n"), dst_name);
              free (dst_name);
              free (buf);
            }
@@ -544,17 +602,16 @@ get_emulation (argc, argv)
                  i++;
                }
              else
-               {
-                 einfo (_("%P%F: missing argument to -m\n"));
-               }
+               einfo (_("%P%F: missing argument to -m\n"));
            }
          else if (strcmp (argv[i], "-mips1") == 0
                   || strcmp (argv[i], "-mips2") == 0
                   || strcmp (argv[i], "-mips3") == 0
-                  || strcmp (argv[i], "-mips32") == 0
-                  || strcmp (argv[i], "-mips64") == 0
                   || strcmp (argv[i], "-mips4") == 0
-                  || strcmp (argv[i], "-mips5") == 0)
+                  || strcmp (argv[i], "-mips5") == 0
+                  || strcmp (argv[i], "-mips32") == 0
+                  || strcmp (argv[i], "-mips32r2") == 0
+                  || strcmp (argv[i], "-mips64") == 0)
            {
              /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
                 passed to the linker by some MIPS compilers.  They
@@ -610,15 +667,37 @@ check_for_scripts_dir (dir)
    Libraries will be searched for here too, but that's ok.
    We look for the "ldscripts" directory in:
 
+   SCRIPTDIR (passed from Makefile)
+            (adjusted according to the current location of the binary)
    SCRIPTDIR (passed from Makefile)
    the dir where this program is (for using it from the build tree)
-   the dir where this program is/../lib (for installing the tool suite elsewhere) */
+   the dir where this program is/../lib
+            (for installing the tool suite elsewhere).  */
 
 static void
 set_scripts_dir ()
 {
   char *end, *dir;
   size_t dirlen;
+  bfd_boolean found;
+
+  dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
+  if (dir)
+    {
+      found = check_for_scripts_dir (dir);
+      free (dir);
+      if (found)
+       return;
+    }
+
+  dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
+  if (dir)
+    {
+      found = check_for_scripts_dir (dir);
+      free (dir);
+      if (found)
+       return;
+    }
 
   if (check_for_scripts_dir (SCRIPTDIR))
     /* We've been installed normally.  */
@@ -630,17 +709,16 @@ set_scripts_dir ()
   {
     /* We could have \foo\bar, or /foo\bar.  */
     char *bslash = strrchr (program_name, '\\');
+
     if (end == NULL || (bslash != NULL && bslash > end))
       end = bslash;
   }
 #endif
 
   if (end == NULL)
-    {
-      /* Don't look for ldscripts in the current directory.  There is
-         too much potential for confusion.  */
-      return;
-    }
+    /* Don't look for ldscripts in the current directory.  There is
+       too much potential for confusion.  */
+    return;
 
   dirlen = end - program_name;
   /* Make a copy of program_name in dir.
@@ -650,15 +728,14 @@ set_scripts_dir ()
   dir[dirlen] = '\0';
 
   if (check_for_scripts_dir (dir))
-    /* Don't free dir.  */
-    return;
+    {
+      free (dir);
+      return;
+    }
 
   /* Look for "ldscripts" in <the dir where our binary is>/../lib.  */
   strcpy (dir + dirlen, "/../lib");
-  if (check_for_scripts_dir (dir))
-    return;
-
-  /* Well, we tried.  */
+  check_for_scripts_dir (dir);
   free (dir);
 }
 
@@ -696,6 +773,7 @@ add_wrap (name)
                                   61))
        einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
     }
+
   if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL)
     einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
 }
@@ -763,6 +841,7 @@ add_keepsyms_file (filename)
   if (link_info.strip != strip_none)
     einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
 
+  free (buf);
   link_info.strip = strip_some;
 }
 \f
@@ -1081,7 +1160,8 @@ constructor_callback (info, constructor, name, abfd, section, value)
 /* A structure used by warning_callback to pass information through
    bfd_map_over_sections.  */
 
-struct warning_callback_info {
+struct warning_callback_info
+{
   bfd_boolean found;
   const char *warning;
   const char *symbol;
@@ -1119,7 +1199,6 @@ warning_callback (info, warning, symbol, abfd, section, address)
 
       /* Look through the relocs to see if we can find a plausible
         address.  */
-
       entry = (lang_input_statement_type *) abfd->usrdata;
       if (entry != NULL && entry->asymbols != NULL)
        asymbols = entry->asymbols;
@@ -1230,7 +1309,6 @@ undefined_symbol (info, name, abfd, section, address, fatal)
       static struct bfd_hash_table *hash;
 
       /* Only warn once about a particular undefined symbol.  */
-
       if (hash == NULL)
        {
          hash = ((struct bfd_hash_table *)
@@ -1289,6 +1367,15 @@ undefined_symbol (info, name, abfd, section, address, fatal)
   return TRUE;
 }
 
+/* Counter to limit the number of relocation overflow error messages
+   to print.  Errors are printed as it is decremented.  When it's
+   called and the counter is zero, a final message is printed
+   indicating more relocations were omitted.  When it gets to -1, no
+   such errors are printed.  If it's initially set to a value less
+   than -1, all such errors will be printed (--verbose does this).  */
+
+int overflow_cutoff_limit = 10;
+
 /* This is called when a reloc overflows.  */
 
 static bfd_boolean
@@ -1301,10 +1388,21 @@ reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
      asection *section;
      bfd_vma address;
 {
+  if (overflow_cutoff_limit == -1)
+    return TRUE;
+
   if (abfd == (bfd *) NULL)
     einfo (_("%P%X: generated"));
   else
     einfo ("%X%C:", abfd, section, address);
+
+  if (overflow_cutoff_limit >= 0
+      && overflow_cutoff_limit-- == 0)
+    {
+      einfo (_(" additional relocation overflows omitted from the output\n"));
+      return TRUE;
+    }
+
   einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name);
   if (addend != 0)
     einfo ("+%v", addend);
This page took 0.028383 seconds and 4 git commands to generate.