home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / ld / ldmain.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  33KB  |  1,251 lines

  1. /* Main program of GNU linker.
  2.    Copyright (C) 1991, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3.    Written by Steve Chamberlain steve@cygnus.com
  4.  
  5. This file is part of GLD, the Gnu Linker.
  6.  
  7. GLD is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GLD is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GLD; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20.  
  21.  
  22. #include "bfd.h"
  23. #include "sysdep.h"
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include "libiberty.h"
  27. #include "progress.h"
  28. #include "bfdlink.h"
  29.  
  30. #include "ld.h"
  31. #include "ldmain.h"
  32. #include "ldmisc.h"
  33. #include "ldwrite.h"
  34. #include "ldgram.h"
  35. #include "ldexp.h"
  36. #include "ldlang.h"
  37. #include "ldemul.h"
  38. #include "ldlex.h"
  39. #include "ldfile.h"
  40. #include "ldctor.h"
  41.  
  42. /* Somewhere above, sys/stat.h got included . . . . */
  43. #if !defined(S_ISDIR) && defined(S_IFDIR)
  44. #define    S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  45. #endif
  46.  
  47. #include <string.h>
  48.  
  49. static char *get_emulation PARAMS ((int, char **));
  50. static void set_scripts_dir PARAMS ((void));
  51.  
  52. /* EXPORTS */
  53.  
  54. char *default_target;
  55. const char *output_filename = "a.out";
  56.  
  57. /* Name this program was invoked by.  */
  58. char *program_name;
  59.  
  60. /* The file that we're creating */
  61. bfd *output_bfd = 0;
  62.  
  63. /* Set by -G argument, for MIPS ECOFF target.  */
  64. int g_switch_value = 8;
  65.  
  66. /* Nonzero means print names of input files as processed.  */
  67. boolean trace_files;
  68.  
  69. /* Nonzero means same, but note open failures, too.  */
  70. boolean trace_file_tries;
  71.  
  72. /* Nonzero means version number was printed, so exit successfully
  73.    instead of complaining if no input files are given.  */
  74. boolean version_printed;
  75.  
  76. /* Nonzero means link in every member of an archive.  */
  77. boolean whole_archive;
  78.  
  79. args_type command_line;
  80.  
  81. ld_config_type config;
  82.  
  83. static boolean check_for_scripts_dir PARAMS ((char *dir));
  84. static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
  85.                         const char *));
  86. static boolean multiple_definition PARAMS ((struct bfd_link_info *,
  87.                         const char *,
  88.                         bfd *, asection *, bfd_vma,
  89.                         bfd *, asection *, bfd_vma));
  90. static boolean multiple_common PARAMS ((struct bfd_link_info *,
  91.                     const char *, bfd *,
  92.                     enum bfd_link_hash_type, bfd_vma,
  93.                     bfd *, enum bfd_link_hash_type,
  94.                     bfd_vma));
  95. static boolean add_to_set PARAMS ((struct bfd_link_info *,
  96.                    struct bfd_link_hash_entry *,
  97.                    bfd_reloc_code_real_type,
  98.                    bfd *, asection *, bfd_vma));
  99. static boolean constructor_callback PARAMS ((struct bfd_link_info *,
  100.                          boolean constructor,
  101.                          const char *name,
  102.                          bfd *, asection *, bfd_vma));
  103. static boolean warning_callback PARAMS ((struct bfd_link_info *,
  104.                      const char *, const char *, bfd *,
  105.                      asection *, bfd_vma));
  106. static void warning_find_reloc PARAMS ((bfd *, asection *, PTR));
  107. static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
  108.                      const char *, bfd *,
  109.                      asection *, bfd_vma));
  110. static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
  111.                        const char *, bfd_vma,
  112.                        bfd *, asection *, bfd_vma));
  113. static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
  114.                     bfd *, asection *, bfd_vma));
  115. static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
  116.                      const char *, bfd *, asection *,
  117.                      bfd_vma));
  118. static boolean notice PARAMS ((struct bfd_link_info *, const char *,
  119.                    bfd *, asection *, bfd_vma));
  120.  
  121. static struct bfd_link_callbacks link_callbacks =
  122. {
  123.   add_archive_element,
  124.   multiple_definition,
  125.   multiple_common,
  126.   add_to_set,
  127.   constructor_callback,
  128.   warning_callback,
  129.   undefined_symbol,
  130.   reloc_overflow,
  131.   reloc_dangerous,
  132.   unattached_reloc,
  133.   notice
  134. };
  135.  
  136. struct bfd_link_info link_info;
  137.  
  138. static void
  139. remove_output ()
  140. {
  141.   if (output_filename) 
  142.     {
  143.       if (output_bfd && output_bfd->iostream) {
  144.     fclose((FILE *)(output_bfd->iostream));
  145.     output_bfd->iostream = NULL;
  146.       }
  147.       if (delete_output_file_on_failure)
  148.     unlink (output_filename);
  149.     }
  150. }
  151.  
  152. int
  153. main (argc, argv)
  154.      int argc;
  155.      char **argv;
  156. {
  157.   char *emulation;
  158.   long start_time = get_run_time ();
  159.  
  160.   program_name = argv[0];
  161.   xmalloc_set_program_name (program_name);
  162.  
  163.   START_PROGRESS (program_name, 0);
  164.  
  165.   bfd_init ();
  166.  
  167.   bfd_set_error_program_name (program_name);
  168.  
  169.   xatexit (remove_output);
  170.  
  171.   /* Initialize the data about options.  */
  172.   trace_files = trace_file_tries = version_printed = false;
  173.   whole_archive = false;
  174.   config.build_constructors = true;
  175.   config.dynamic_link = false;
  176.   command_line.force_common_definition = false;
  177.   command_line.interpreter = NULL;
  178.   command_line.rpath = NULL;
  179.  
  180.   link_info.callbacks = &link_callbacks;
  181.   link_info.relocateable = false;
  182.   link_info.shared = false;
  183.   link_info.symbolic = false;
  184.   link_info.static_link = false;
  185.   link_info.traditional_format = false;
  186.   link_info.strip = strip_none;
  187.   link_info.discard = discard_none;
  188.   link_info.lprefix_len = 1;
  189.   link_info.lprefix = "L";
  190.   link_info.keep_memory = true;
  191.   link_info.input_bfds = NULL;
  192.   link_info.create_object_symbols_section = NULL;
  193.   link_info.hash = NULL;
  194.   link_info.keep_hash = NULL;
  195.   link_info.notice_all = false;
  196.   link_info.notice_hash = NULL;
  197.   link_info.wrap_hash = NULL;
  198.   
  199.   ldfile_add_arch ("");
  200.  
  201.   config.make_executable = true;
  202.   force_make_executable = false;
  203.   config.magic_demand_paged = true;
  204.   config.text_read_only = true;
  205.   config.make_executable = true;
  206.  
  207.   emulation = get_emulation (argc, argv);
  208.   ldemul_choose_mode (emulation);
  209.   default_target = ldemul_choose_target ();
  210.   lang_init ();
  211.   ldemul_before_parse ();
  212.   lang_has_input_file = false;
  213.   parse_args (argc, argv);
  214.  
  215.   ldemul_set_symbols ();
  216.  
  217.   if (link_info.relocateable)
  218.     {
  219.       if (command_line.relax)
  220.     einfo ("%P%F: -relax and -r may not be used together\n");
  221.       if (link_info.shared)
  222.     einfo ("%P%F: -r and -shared may not be used together\n");
  223.     }
  224.  
  225.   /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols).  I
  226.      don't see how else this can be handled, since in this case we
  227.      must preserve all externally visible symbols.  */
  228.   if (link_info.relocateable && link_info.strip == strip_all)
  229.     {
  230.       link_info.strip = strip_debugger;
  231.       if (link_info.discard == discard_none)
  232.     link_info.discard = discard_all;
  233.     }
  234.  
  235.   /* This essentially adds another -L directory so this must be done after
  236.      the -L's in argv have been processed.  */
  237.   set_scripts_dir ();
  238.  
  239.   if (had_script == false)
  240.     {
  241.       /* Read the emulation's appropriate default script.  */
  242.       int isfile;
  243.       char *s = ldemul_get_script (&isfile);
  244.  
  245.       if (isfile)
  246.     ldfile_open_command_file (s);
  247.       else
  248.     {
  249.       if (trace_file_tries)
  250.         {
  251.           info_msg ("using internal linker script:\n");
  252.           info_msg ("==================================================\n");
  253.           info_msg (s);
  254.           info_msg ("\n==================================================\n");
  255.         }
  256.       lex_string = s;
  257.       lex_redirect (s);
  258.     }
  259.       parser_input = input_script;
  260.       yyparse ();
  261.       lex_string = NULL;
  262.     }
  263.  
  264.   lang_final ();
  265.  
  266.   if (lang_has_input_file == false)
  267.     {
  268.       if (version_printed)
  269.     xexit (0);
  270.       einfo ("%P%F: no input files\n");
  271.     }
  272.  
  273.   if (trace_files)
  274.     {
  275.       info_msg ("%P: mode %s\n", emulation);
  276.     }
  277.  
  278.   ldemul_after_parse ();
  279.  
  280.  
  281.   if (config.map_filename)
  282.     {
  283.       if (strcmp (config.map_filename, "-") == 0)
  284.     {
  285.       config.map_file = stdout;
  286.     }
  287.       else
  288.     {
  289.       config.map_file = fopen (config.map_filename, FOPEN_WT);
  290.       if (config.map_file == (FILE *) NULL)
  291.         {
  292.           bfd_set_error (bfd_error_system_call);
  293.           einfo ("%P%F: cannot open map file %s: %E\n",
  294.              config.map_filename);
  295.         }
  296.     }
  297.     }
  298.  
  299.  
  300.   lang_process ();
  301.  
  302.   /* Print error messages for any missing symbols, for any warning
  303.      symbols, and possibly multiple definitions */
  304.  
  305.  
  306.   if (config.text_read_only)
  307.     {
  308.       /* Look for a text section and mark the readonly attribute in it */
  309.       asection *found = bfd_get_section_by_name (output_bfd, ".text");
  310.  
  311.       if (found != (asection *) NULL)
  312.     {
  313.       found->flags |= SEC_READONLY;
  314.     }
  315.     }
  316.  
  317.   if (link_info.relocateable)
  318.     output_bfd->flags &= ~EXEC_P;
  319.   else
  320.     output_bfd->flags |= EXEC_P;
  321.  
  322.   ldwrite ();
  323.  
  324.   if (config.map_file != NULL)
  325.     lang_map ();
  326.   if (link_info.notice_all)
  327.     output_cref (config.map_file != NULL ? config.map_file : stdout);
  328.  
  329.   /* Even if we're producing relocateable output, some non-fatal errors should
  330.      be reported in the exit status.  (What non-fatal errors, if any, do we
  331.      want to ignore for relocateable output?)  */
  332.  
  333.   if (config.make_executable == false && force_make_executable == false)
  334.     {
  335.       if (trace_files == true)
  336.     {
  337.       einfo ("%P: link errors found, deleting executable `%s'\n",
  338.          output_filename);
  339.     }
  340.  
  341.       /* The file will be removed by remove_output.  */
  342.  
  343.       xexit (1);
  344.     }
  345.   else
  346.     {
  347.       if (! bfd_close (output_bfd))
  348.     einfo ("%F%B: final close failed: %E\n", output_bfd);
  349.  
  350.       /* If the --force-exe-suffix is enabled, and we're making an
  351.      executable file and it doesn't end in .exe, copy it to one which does. */
  352.  
  353.       if (! link_info.relocateable && command_line.force_exe_suffix)
  354.     {
  355.       int len = strlen (output_filename);
  356.       if (len < 4 
  357.           || (strcasecmp (output_filename + len - 4, ".exe") != 0
  358.           && strcasecmp (output_filename + len - 4, ".dll") != 0))
  359.         {
  360.           FILE *src;
  361.           FILE *dst;
  362.           const int bsize = 4096;
  363.           char *buf = xmalloc (bsize);
  364.           int l;
  365.           char *dst_name = xmalloc (len + 5);
  366.           strcpy (dst_name, output_filename);
  367.           strcat (dst_name, ".exe");
  368.           src = fopen (output_filename, FOPEN_RB);
  369.           dst = fopen (dst_name, FOPEN_WB);
  370.  
  371.           if (!src)
  372.         einfo ("%X%P: unable to open for source of copy `%s'\n", output_filename);
  373.           if (!dst)
  374.         einfo ("%X%P: unable to open for destination of copy `%s'\n", dst_name);
  375.           while ((l = fread (buf, 1, bsize, src)) > 0)
  376.         {
  377.           int done = fwrite (buf, 1, l, dst);
  378.           if (done != l)
  379.             {
  380.               einfo ("%P: Error writing file `%s'\n", dst_name);
  381.             }
  382.         }
  383.           fclose (src);
  384.           if (!fclose (dst))
  385.         {
  386.           einfo ("%P: Error closing file `%s'\n", dst_name);
  387.         }
  388.           free (dst_name);
  389.           free (buf);
  390.         }
  391.     }
  392.     }
  393.  
  394.   END_PROGRESS (program_name);
  395.  
  396.   if (config.stats)
  397.     {
  398.       extern char **environ;
  399. #ifdef HAVE_SBRK
  400.       char *lim = (char *) sbrk (0);
  401. #endif
  402.       long run_time = get_run_time () - start_time;
  403.  
  404.       fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
  405.            program_name, run_time / 1000000, run_time % 1000000);
  406. #ifdef HAVE_SBRK
  407.       fprintf (stderr, "%s: data size %ld\n", program_name,
  408.            (long) (lim - (char *) &environ));
  409. #endif
  410.     }
  411.  
  412.   /* Prevent remove_output from doing anything, after a successful link.  */
  413.   output_filename = NULL;
  414.  
  415.   xexit (0);
  416.   return 0;
  417. }
  418.  
  419. /* We need to find any explicitly given emulation in order to initialize the
  420.    state that's needed by the lex&yacc argument parser (parse_args).  */
  421.  
  422. static char *
  423. get_emulation (argc, argv)
  424.      int argc;
  425.      char **argv;
  426. {
  427.   char *emulation;
  428.   int i;
  429.  
  430.   emulation = (char *) getenv (EMULATION_ENVIRON);
  431.   if (emulation == NULL)
  432.     emulation = DEFAULT_EMULATION;
  433.  
  434.   for (i = 1; i < argc; i++)
  435.     {
  436.       if (!strncmp (argv[i], "-m", 2))
  437.     {
  438.       if (argv[i][2] == '\0')
  439.         {
  440.           /* -m EMUL */
  441.           if (i < argc - 1)
  442.         {
  443.           emulation = argv[i + 1];
  444.           i++;
  445.         }
  446.           else
  447.         {
  448.           einfo("%P%F: missing argument to -m\n");
  449.         }
  450.         }
  451.       else if (strcmp (argv[i], "-mips1") == 0
  452.            || strcmp (argv[i], "-mips2") == 0
  453.            || strcmp (argv[i], "-mips3") == 0)
  454.         {
  455.           /* FIXME: The arguments -mips1, -mips2 and -mips3 are
  456.          passed to the linker by some MIPS compilers.  They
  457.          generally tell the linker to use a slightly different
  458.          library path.  Perhaps someday these should be
  459.          implemented as emulations; until then, we just ignore
  460.          the arguments and hope that nobody ever creates
  461.          emulations named ips1, ips2 or ips3.  */
  462.         }
  463.       else if (strcmp (argv[i], "-m486") == 0)
  464.         {
  465.           /* FIXME: The argument -m486 is passed to the linker on
  466.          some Linux systems.  Hope that nobody creates an
  467.          emulation named 486.  */
  468.         }
  469.       else
  470.         {
  471.           /* -mEMUL */
  472.           emulation = &argv[i][2];
  473.         }
  474.     }
  475.     }
  476.  
  477.   return emulation;
  478. }
  479.  
  480. /* If directory DIR contains an "ldscripts" subdirectory,
  481.    add DIR to the library search path and return true,
  482.    else return false.  */
  483.  
  484. static boolean
  485. check_for_scripts_dir (dir)
  486.      char *dir;
  487. {
  488.   size_t dirlen;
  489.   char *buf;
  490.   struct stat s;
  491.   boolean res;
  492.  
  493.   dirlen = strlen (dir);
  494.   /* sizeof counts the terminating NUL.  */
  495.   buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
  496.   sprintf (buf, "%s/ldscripts", dir);
  497.  
  498.   res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
  499.   free (buf);
  500.   if (res)
  501.     ldfile_add_library_path (dir, false);
  502.   return res;
  503. }
  504.  
  505. /* Set the default directory for finding script files.
  506.    Libraries will be searched for here too, but that's ok.
  507.    We look for the "ldscripts" directory in:
  508.  
  509.    SCRIPTDIR (passed from Makefile)
  510.    the dir where this program is (for using it from the build tree)
  511.    the dir where this program is/../lib (for installing the tool suite elsewhere) */
  512.  
  513. static void
  514. set_scripts_dir ()
  515. {
  516.   char *end, *dir;
  517.   size_t dirlen;
  518.  
  519.   if (check_for_scripts_dir (SCRIPTDIR))
  520.     return;            /* We've been installed normally.  */
  521.  
  522.   /* Look for "ldscripts" in the dir where our binary is.  */
  523.   end = strrchr (program_name, '/');
  524.  
  525.   if (end == NULL)
  526.     {
  527.       /* Don't look for ldscripts in the current directory.  There is
  528.          too much potential for confusion.  */
  529.       return;
  530.     }
  531.  
  532.   dirlen = end - program_name;
  533.   /* Make a copy of program_name in dir.
  534.      Leave room for later "/../lib".  */
  535.   dir = (char *) xmalloc (dirlen + 8);
  536.   strncpy (dir, program_name, dirlen);
  537.   dir[dirlen] = '\0';
  538.  
  539.   if (check_for_scripts_dir (dir))
  540.     return;            /* Don't free dir.  */
  541.  
  542.   /* Look for "ldscripts" in <the dir where our binary is>/../lib.  */
  543.   strcpy (dir + dirlen, "/../lib");
  544.   if (check_for_scripts_dir (dir))
  545.     return;
  546.  
  547.   free (dir);            /* Well, we tried.  */
  548. }
  549.  
  550. void
  551. add_ysym (name)
  552.      const char *name;
  553. {
  554.   if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
  555.     {
  556.       link_info.notice_hash = ((struct bfd_hash_table *)
  557.                    xmalloc (sizeof (struct bfd_hash_table)));
  558.       if (! bfd_hash_table_init_n (link_info.notice_hash,
  559.                    bfd_hash_newfunc,
  560.                    61))
  561.     einfo ("%P%F: bfd_hash_table_init failed: %E\n");
  562.     }      
  563.  
  564.   if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
  565.       == (struct bfd_hash_entry *) NULL)
  566.     einfo ("%P%F: bfd_hash_lookup failed: %E\n");
  567. }
  568.  
  569. /* Record a symbol to be wrapped, from the --wrap option.  */
  570.  
  571. void
  572. add_wrap (name)
  573.      const char *name;
  574. {
  575.   if (link_info.wrap_hash == NULL)
  576.     {
  577.       link_info.wrap_hash = ((struct bfd_hash_table *)
  578.                  xmalloc (sizeof (struct bfd_hash_table)));
  579.       if (! bfd_hash_table_init_n (link_info.wrap_hash,
  580.                    bfd_hash_newfunc,
  581.                    61))
  582.     einfo ("%P%F: bfd_hash_table_init failed: %E\n");
  583.     }
  584.   if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
  585.     einfo ("%P%F: bfd_hash_lookup failed: %E\n");
  586. }
  587.  
  588. /* Handle the -retain-symbols-file option.  */
  589.  
  590. void
  591. add_keepsyms_file (filename)
  592.      const char *filename;
  593. {
  594.   FILE *file;
  595.   char *buf;
  596.   size_t bufsize;
  597.   int c;
  598.  
  599.   if (link_info.strip == strip_some)
  600.     einfo ("%X%P: error: duplicate retain-symbols-file\n");
  601.  
  602.   file = fopen (filename, "r");
  603.   if (file == (FILE *) NULL)
  604.     {
  605.       bfd_set_error (bfd_error_system_call);
  606.       einfo ("%X%P: %s: %E\n", filename);
  607.       return;
  608.     }
  609.  
  610.   link_info.keep_hash = ((struct bfd_hash_table *)
  611.              xmalloc (sizeof (struct bfd_hash_table)));
  612.   if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
  613.     einfo ("%P%F: bfd_hash_table_init failed: %E\n");
  614.  
  615.   bufsize = 100;
  616.   buf = (char *) xmalloc (bufsize);
  617.  
  618.   c = getc (file);
  619.   while (c != EOF)
  620.     {
  621.       while (isspace (c))
  622.     c = getc (file);
  623.  
  624.       if (c != EOF)
  625.     {
  626.       size_t len = 0;
  627.  
  628.       while (! isspace (c) && c != EOF)
  629.         {
  630.           buf[len] = c;
  631.           ++len;
  632.           if (len >= bufsize)
  633.         {
  634.           bufsize *= 2;
  635.           buf = xrealloc (buf, bufsize);
  636.         }
  637.           c = getc (file);
  638.         }
  639.  
  640.       buf[len] = '\0';
  641.  
  642.       if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
  643.           == (struct bfd_hash_entry *) NULL)
  644.         einfo ("%P%F: bfd_hash_lookup for insertion failed: %E\n");
  645.     }
  646.     }
  647.  
  648.   if (link_info.strip != strip_none)
  649.     einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
  650.  
  651.   link_info.strip = strip_some;
  652. }
  653.  
  654. /* Callbacks from the BFD linker routines.  */
  655.  
  656. /* This is called when BFD has decided to include an archive member in
  657.    a link.  */
  658.  
  659. /*ARGSUSED*/
  660. static boolean
  661. add_archive_element (info, abfd, name)
  662.      struct bfd_link_info *info;
  663.      bfd *abfd;
  664.      const char *name;
  665. {
  666.   lang_input_statement_type *input;
  667.  
  668.   input = ((lang_input_statement_type *)
  669.        xmalloc (sizeof (lang_input_statement_type)));
  670.   input->filename = abfd->filename;
  671.   input->local_sym_name = abfd->filename;
  672.   input->the_bfd = abfd;
  673.   input->asymbols = NULL;
  674.   input->next = NULL;
  675.   input->just_syms_flag = false;
  676.   input->loaded = false;
  677.   input->search_dirs_flag = false;
  678.  
  679.   /* FIXME: The following fields are not set: header.next,
  680.      header.type, closed, passive_position, symbol_count,
  681.      next_real_file, is_archive, target, real.  This bit of code is
  682.      from the old decode_library_subfile function.  I don't know
  683.      whether any of those fields matters.  */
  684.  
  685.   ldlang_add_file (input);
  686.  
  687.   if (config.map_file != (FILE *) NULL)
  688.     {
  689.       static boolean header_printed;
  690.       struct bfd_link_hash_entry *h;
  691.       bfd *from;
  692.       int len;
  693.  
  694.       h = bfd_link_hash_lookup (link_info.hash, name, false, false, true);
  695.  
  696.       if (h == NULL)
  697.     from = NULL;
  698.       else
  699.     {
  700.       switch (h->type)
  701.         {
  702.         default:
  703.           from = NULL;
  704.           break;
  705.  
  706.         case bfd_link_hash_defined:
  707.         case bfd_link_hash_defweak:
  708.           from = h->u.def.section->owner;
  709.           break;
  710.  
  711.         case bfd_link_hash_undefined:
  712.         case bfd_link_hash_undefweak:
  713.           from = h->u.undef.abfd;
  714.           break;
  715.  
  716.         case bfd_link_hash_common:
  717.           from = h->u.c.p->section->owner;
  718.           break;
  719.         }
  720.     }
  721.  
  722.       if (! header_printed)
  723.     {
  724.       char buf[100];
  725.  
  726.       sprintf (buf, "%-29s %s\n\n", "Archive member included",
  727.            "because of file (symbol)");
  728.       minfo ("%s", buf);
  729.       header_printed = true;
  730.     }
  731.  
  732.       if (bfd_my_archive (abfd) == NULL)
  733.     {
  734.       minfo ("%s", bfd_get_filename (abfd));
  735.       len = strlen (bfd_get_filename (abfd));
  736.     }
  737.       else
  738.     {
  739.       minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
  740.          bfd_get_filename (abfd));
  741.       len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
  742.          + strlen (bfd_get_filename (abfd))
  743.          + 2);
  744.     }
  745.  
  746.       if (len >= 29)
  747.     {
  748.       print_nl ();
  749.       len = 0;
  750.     }
  751.       while (len < 30)
  752.     {
  753.       print_space ();
  754.       ++len;
  755.     }
  756.  
  757.       if (from != NULL)
  758.     minfo ("%B ", from);
  759.       if (h != NULL)
  760.     minfo ("(%T)\n", h->root.string);
  761.       else
  762.     minfo ("(%s)\n", name);
  763.     }
  764.  
  765.   if (trace_files || trace_file_tries)
  766.     info_msg ("%I\n", input);
  767.  
  768.   return true;
  769. }
  770.  
  771. /* This is called when BFD has discovered a symbol which is defined
  772.    multiple times.  */
  773.  
  774. /*ARGSUSED*/
  775. static boolean
  776. multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
  777.      struct bfd_link_info *info;
  778.      const char *name;
  779.      bfd *obfd;
  780.      asection *osec;
  781.      bfd_vma oval;
  782.      bfd *nbfd;
  783.      asection *nsec;
  784.      bfd_vma nval;
  785. {
  786.   /* If either section has the output_section field set to
  787.      bfd_abs_section_ptr, it means that the section is being
  788.      discarded, and this is not really a multiple definition at all.
  789.      FIXME: It would be cleaner to somehow ignore symbols defined in
  790.      sections which are being discarded.  */
  791.   if ((osec->output_section != NULL
  792.        && bfd_is_abs_section (osec->output_section))
  793.       || (nsec->output_section != NULL
  794.       && bfd_is_abs_section (nsec->output_section)))
  795.     return true;
  796.  
  797.   einfo ("%X%C: multiple definition of `%T'\n",
  798.      nbfd, nsec, nval, name);
  799.   if (obfd != (bfd *) NULL)
  800.     einfo ("%D: first defined here\n", obfd, osec, oval);
  801.   return true;
  802. }
  803.  
  804. /* This is called when there is a definition of a common symbol, or
  805.    when a common symbol is found for a symbol that is already defined,
  806.    or when two common symbols are found.  We only do something if
  807.    -warn-common was used.  */
  808.  
  809. /*ARGSUSED*/
  810. static boolean
  811. multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
  812.      struct bfd_link_info *info;
  813.      const char *name;
  814.      bfd *obfd;
  815.      enum bfd_link_hash_type otype;
  816.      bfd_vma osize;
  817.      bfd *nbfd;
  818.      enum bfd_link_hash_type ntype;
  819.      bfd_vma nsize;
  820. {
  821.   if (! config.warn_common)
  822.     return true;
  823.  
  824.   if (ntype == bfd_link_hash_defined
  825.       || ntype == bfd_link_hash_defweak
  826.       || ntype == bfd_link_hash_indirect)
  827.     {
  828.       ASSERT (otype == bfd_link_hash_common);
  829.       einfo ("%B: warning: definition of `%T' overriding common\n",
  830.          nbfd, name);
  831.       if (obfd != NULL)
  832.     einfo ("%B: warning: common is here\n", obfd);
  833.     }
  834.   else if (otype == bfd_link_hash_defined
  835.        || otype == bfd_link_hash_defweak
  836.        || otype == bfd_link_hash_indirect)
  837.     {
  838.       ASSERT (ntype == bfd_link_hash_common);
  839.       einfo ("%B: warning: common of `%T' overridden by definition\n",
  840.          nbfd, name);
  841.       if (obfd != NULL)
  842.     einfo ("%B: warning: defined here\n", obfd);
  843.     }
  844.   else
  845.     {
  846.       ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
  847.       if (osize > nsize)
  848.     {
  849.       einfo ("%B: warning: common of `%T' overridden by larger common\n",
  850.          nbfd, name);
  851.       if (obfd != NULL)
  852.         einfo ("%B: warning: larger common is here\n", obfd);
  853.     }
  854.       else if (nsize > osize)
  855.     {
  856.       einfo ("%B: warning: common of `%T' overriding smaller common\n",
  857.          nbfd, name);
  858.       if (obfd != NULL)
  859.         einfo ("%B: warning: smaller common is here\n", obfd);
  860.     }
  861.       else
  862.     {
  863.       einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
  864.       if (obfd != NULL)
  865.         einfo ("%B: warning: previous common is here\n", obfd);
  866.     }
  867.     }
  868.  
  869.   return true;
  870. }
  871.  
  872. /* This is called when BFD has discovered a set element.  H is the
  873.    entry in the linker hash table for the set.  SECTION and VALUE
  874.    represent a value which should be added to the set.  */
  875.  
  876. /*ARGSUSED*/
  877. static boolean
  878. add_to_set (info, h, reloc, abfd, section, value)
  879.      struct bfd_link_info *info;
  880.      struct bfd_link_hash_entry *h;
  881.      bfd_reloc_code_real_type reloc;
  882.      bfd *abfd;
  883.      asection *section;
  884.      bfd_vma value;
  885. {
  886.   if (config.warn_constructors)
  887.     einfo ("%P: warning: global constructor %s used\n",
  888.        h->root.string);
  889.  
  890.   if (! config.build_constructors)
  891.     return true;
  892.  
  893.   ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value);
  894.  
  895.   if (h->type == bfd_link_hash_new)
  896.     {
  897.       h->type = bfd_link_hash_undefined;
  898.       h->u.undef.abfd = abfd;
  899.       /* We don't call bfd_link_add_undef to add this to the list of
  900.      undefined symbols because we are going to define it
  901.      ourselves.  */
  902.     }
  903.  
  904.   return true;
  905. }
  906.  
  907. /* This is called when BFD has discovered a constructor.  This is only
  908.    called for some object file formats--those which do not handle
  909.    constructors in some more clever fashion.  This is similar to
  910.    adding an element to a set, but less general.  */
  911.  
  912. static boolean
  913. constructor_callback (info, constructor, name, abfd, section, value)
  914.      struct bfd_link_info *info;
  915.      boolean constructor;
  916.      const char *name;
  917.      bfd *abfd;
  918.      asection *section;
  919.      bfd_vma value;
  920. {
  921.   char *s;
  922.   struct bfd_link_hash_entry *h;
  923.   char set_name[1 + sizeof "__CTOR_LIST__"];
  924.  
  925.   if (config.warn_constructors)
  926.     einfo ("%P: warning: global constructor %s used\n", name);
  927.  
  928.   if (! config.build_constructors)
  929.     return true;
  930.  
  931.   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
  932.      useful error message.  */
  933.   if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
  934.       && (link_info.relocateable
  935.       || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
  936.     einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n");
  937.  
  938.   s = set_name;
  939.   if (bfd_get_symbol_leading_char (abfd) != '\0')
  940.     *s++ = bfd_get_symbol_leading_char (abfd);
  941.   if (constructor)
  942.     strcpy (s, "__CTOR_LIST__");
  943.   else
  944.     strcpy (s, "__DTOR_LIST__");
  945.  
  946.   h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
  947.   if (h == (struct bfd_link_hash_entry *) NULL)
  948.     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
  949.   if (h->type == bfd_link_hash_new)
  950.     {
  951.       h->type = bfd_link_hash_undefined;
  952.       h->u.undef.abfd = abfd;
  953.       /* We don't call bfd_link_add_undef to add this to the list of
  954.      undefined symbols because we are going to define it
  955.      ourselves.  */
  956.     }
  957.  
  958.   ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
  959.   return true;
  960. }
  961.  
  962. /* A structure used by warning_callback to pass information through
  963.    bfd_map_over_sections.  */
  964.  
  965. struct warning_callback_info
  966. {
  967.   boolean found;
  968.   const char *warning;
  969.   const char *symbol;
  970.   asymbol **asymbols;
  971. };
  972.  
  973. /* This is called when there is a reference to a warning symbol.  */
  974.  
  975. /*ARGSUSED*/
  976. static boolean
  977. warning_callback (info, warning, symbol, abfd, section, address)
  978.      struct bfd_link_info *info;
  979.      const char *warning;
  980.      const char *symbol;
  981.      bfd *abfd;
  982.      asection *section;
  983.      bfd_vma address;
  984. {
  985.   /* This is a hack to support warn_multiple_gp.  FIXME: This should
  986.      have a cleaner interface, but what?  */
  987.   if (! config.warn_multiple_gp
  988.       && strcmp (warning, "using multiple gp values") == 0)
  989.     return true;
  990.  
  991.   if (section != NULL)
  992.     einfo ("%C: %s\n", abfd, section, address, warning);
  993.   else if (abfd == NULL)
  994.     einfo ("%P: %s\n", warning);
  995.   else if (symbol == NULL)
  996.     einfo ("%B: %s\n", abfd, warning);
  997.   else
  998.     {
  999.       lang_input_statement_type *entry;
  1000.       asymbol **asymbols;
  1001.       struct warning_callback_info info;
  1002.  
  1003.       /* Look through the relocs to see if we can find a plausible
  1004.      address.  */
  1005.  
  1006.       entry = (lang_input_statement_type *) abfd->usrdata;
  1007.       if (entry != NULL && entry->asymbols != NULL)
  1008.     asymbols = entry->asymbols;
  1009.       else
  1010.     {
  1011.       long symsize;
  1012.       long symbol_count;
  1013.  
  1014.       symsize = bfd_get_symtab_upper_bound (abfd);
  1015.       if (symsize < 0)
  1016.         einfo ("%B%F: could not read symbols: %E\n", abfd);
  1017.       asymbols = (asymbol **) xmalloc (symsize);
  1018.       symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
  1019.       if (symbol_count < 0)
  1020.         einfo ("%B%F: could not read symbols: %E\n", abfd);
  1021.       if (entry != NULL)
  1022.         {
  1023.           entry->asymbols = asymbols;
  1024.           entry->symbol_count = symbol_count;
  1025.         }
  1026.     }
  1027.  
  1028.       info.found = false;
  1029.       info.warning = warning;
  1030.       info.symbol = symbol;
  1031.       info.asymbols = asymbols;
  1032.       bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
  1033.  
  1034.       if (! info.found)
  1035.     einfo ("%B: %s\n", abfd, warning);
  1036.     }
  1037.  
  1038.   return true;
  1039. }
  1040.  
  1041. /* This is called by warning_callback for each section.  It checks the
  1042.    relocs of the section to see if it can find a reference to the
  1043.    symbol which triggered the warning.  If it can, it uses the reloc
  1044.    to give an error message with a file and line number.  */
  1045.  
  1046. static void
  1047. warning_find_reloc (abfd, sec, iarg)
  1048.      bfd *abfd;
  1049.      asection *sec;
  1050.      PTR iarg;
  1051. {
  1052.   struct warning_callback_info *info = (struct warning_callback_info *) iarg;
  1053.   long relsize;
  1054.   arelent **relpp;
  1055.   long relcount;
  1056.   arelent **p, **pend;
  1057.  
  1058.   if (info->found)
  1059.     return;
  1060.  
  1061.   relsize = bfd_get_reloc_upper_bound (abfd, sec);
  1062.   if (relsize < 0)
  1063.     einfo ("%B%F: could not read relocs: %E\n", abfd);
  1064.   if (relsize == 0)
  1065.     return;
  1066.  
  1067.   relpp = (arelent **) xmalloc (relsize);
  1068.   relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
  1069.   if (relcount < 0)
  1070.     einfo ("%B%F: could not read relocs: %E\n", abfd);
  1071.  
  1072.   p = relpp;
  1073.   pend = p + relcount;
  1074.   for (; p < pend && *p != NULL; p++)
  1075.     {
  1076.       arelent *q = *p;
  1077.  
  1078.       if (q->sym_ptr_ptr != NULL
  1079.       && *q->sym_ptr_ptr != NULL
  1080.       && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
  1081.     {
  1082.       /* We found a reloc for the symbol we are looking for.  */
  1083.       einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
  1084.       info->found = true;
  1085.       break;
  1086.     }
  1087.     }
  1088.  
  1089.   free (relpp);
  1090. }
  1091.  
  1092. /* This is called when an undefined symbol is found.  */
  1093.  
  1094. /*ARGSUSED*/
  1095. static boolean
  1096. undefined_symbol (info, name, abfd, section, address)
  1097.      struct bfd_link_info *info;
  1098.      const char *name;
  1099.      bfd *abfd;
  1100.      asection *section;
  1101.      bfd_vma address;
  1102. {
  1103.   static char *error_name;
  1104.   static unsigned int error_count;
  1105.  
  1106. #define MAX_ERRORS_IN_A_ROW 5
  1107.  
  1108.   if (config.warn_once)
  1109.     {
  1110.       static struct bfd_hash_table *hash;
  1111.  
  1112.       /* Only warn once about a particular undefined symbol.  */
  1113.  
  1114.       if (hash == NULL)
  1115.     {
  1116.       hash = ((struct bfd_hash_table *)
  1117.           xmalloc (sizeof (struct bfd_hash_table)));
  1118.       if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
  1119.         einfo ("%F%P: bfd_hash_table_init failed: %E\n");
  1120.     }
  1121.  
  1122.       if (bfd_hash_lookup (hash, name, false, false) != NULL)
  1123.     return true;
  1124.  
  1125.       if (bfd_hash_lookup (hash, name, true, true) == NULL)
  1126.     einfo ("%F%P: bfd_hash_lookup failed: %E\n");
  1127.     }
  1128.  
  1129.   /* We never print more than a reasonable number of errors in a row
  1130.      for a single symbol.  */
  1131.   if (error_name != (char *) NULL
  1132.       && strcmp (name, error_name) == 0)
  1133.     ++error_count;
  1134.   else
  1135.     {
  1136.       error_count = 0;
  1137.       if (error_name != (char *) NULL)
  1138.     free (error_name);
  1139.       error_name = buystring (name);
  1140.     }
  1141.  
  1142.   if (section != NULL)
  1143.     {
  1144.       if (error_count < MAX_ERRORS_IN_A_ROW)
  1145.     einfo ("%X%C: undefined reference to `%T'\n",
  1146.            abfd, section, address, name);
  1147.       else if (error_count == MAX_ERRORS_IN_A_ROW)
  1148.     einfo ("%D: more undefined references to `%T' follow\n",
  1149.            abfd, section, address, name);
  1150.     }
  1151.   else
  1152.     {
  1153.       if (error_count < MAX_ERRORS_IN_A_ROW)
  1154.     einfo ("%X%B: undefined reference to `%T'\n",
  1155.            abfd, name);
  1156.       else if (error_count == MAX_ERRORS_IN_A_ROW)
  1157.     einfo ("%B: more undefined references to `%T' follow\n",
  1158.            abfd, name);
  1159.     }
  1160.  
  1161.   return true;
  1162. }
  1163.  
  1164. /* This is called when a reloc overflows.  */
  1165.  
  1166. /*ARGSUSED*/
  1167. static boolean
  1168. reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
  1169.      struct bfd_link_info *info;
  1170.      const char *name;
  1171.      const char *reloc_name;
  1172.      bfd_vma addend;
  1173.      bfd *abfd;
  1174.      asection *section;
  1175.      bfd_vma address;
  1176. {
  1177.   if (abfd == (bfd *) NULL)
  1178.     einfo ("%P%X: generated");
  1179.   else
  1180.     einfo ("%X%C:", abfd, section, address);
  1181.   einfo (" relocation truncated to fit: %s %T", reloc_name, name);
  1182.   if (addend != 0)
  1183.     einfo ("+%v", addend);
  1184.   einfo ("\n");
  1185.   return true;
  1186. }
  1187.  
  1188. /* This is called when a dangerous relocation is made.  */
  1189.  
  1190. /*ARGSUSED*/
  1191. static boolean
  1192. reloc_dangerous (info, message, abfd, section, address)
  1193.      struct bfd_link_info *info;
  1194.      const char *message;
  1195.      bfd *abfd;
  1196.      asection *section;
  1197.      bfd_vma address;
  1198. {
  1199.   if (abfd == (bfd *) NULL)
  1200.     einfo ("%P%X: generated");
  1201.   else
  1202.     einfo ("%X%C:", abfd, section, address);
  1203.   einfo ("dangerous relocation: %s\n", message);
  1204.   return true;
  1205. }
  1206.  
  1207. /* This is called when a reloc is being generated attached to a symbol
  1208.    that is not being output.  */
  1209.  
  1210. /*ARGSUSED*/
  1211. static boolean
  1212. unattached_reloc (info, name, abfd, section, address)
  1213.      struct bfd_link_info *info;
  1214.      const char *name;
  1215.      bfd *abfd;
  1216.      asection *section;
  1217.      bfd_vma address;
  1218. {
  1219.   if (abfd == (bfd *) NULL)
  1220.     einfo ("%P%X: generated");
  1221.   else
  1222.     einfo ("%X%C:", abfd, section, address);
  1223.   einfo (" reloc refers to symbol `%T' which is not being output\n", name);
  1224.   return true;
  1225. }
  1226.  
  1227. /* This is called if link_info.notice_all is set, or when a symbol in
  1228.    link_info.notice_hash is found.  Symbols are put in notice_hash
  1229.    using the -y option.  */
  1230.  
  1231. static boolean
  1232. notice (info, name, abfd, section, value)
  1233.      struct bfd_link_info *info;
  1234.      const char *name;
  1235.      bfd *abfd;
  1236.      asection *section;
  1237.      bfd_vma value;
  1238. {
  1239.   if (! info->notice_all
  1240.       || (info->notice_hash != NULL
  1241.       && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL))
  1242.     einfo ("%B: %s %s\n", abfd,
  1243.        bfd_is_und_section (section) ? "reference to" : "definition of",
  1244.        name);
  1245.  
  1246.   if (info->notice_all)
  1247.     add_cref (name, abfd, section, value);
  1248.  
  1249.   return true;
  1250. }
  1251.