home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / amiga / binutils-2.5.2-src.lha / binutils-2.5.2 / ld / ldmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-10  |  23.6 KB  |  932 lines

  1. /* Main program of GNU linker.
  2.    Copyright (C) 1991, 92, 93, 94 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include "bfd.h"
  23. #include "sysdep.h"
  24. #include <stdio.h>
  25. #include "libiberty.h"
  26. #include "bfdlink.h"
  27.  
  28. #include "config.h"
  29. #include "ld.h"
  30. #include "ldmain.h"
  31. #include "ldmisc.h"
  32. #include "ldwrite.h"
  33. #include "ldgram.h"
  34. #include "ldexp.h"
  35. #include "ldlang.h"
  36. #include "ldemul.h"
  37. #include "ldlex.h"
  38. #include "ldfile.h"
  39. #include "ldctor.h"
  40.  
  41. /* Somewhere above, sys/stat.h got included . . . . */
  42. #if !defined(S_ISDIR) && defined(S_IFDIR)
  43. #define    S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  44. #endif
  45.  
  46. #include <string.h>
  47.  
  48. #ifndef __amigados__
  49. #define HAVE_SBRK
  50. #endif
  51.  
  52. static char *get_emulation PARAMS ((int, char **));
  53. static void set_scripts_dir PARAMS ((void));
  54.  
  55. /* EXPORTS */
  56.  
  57. char *default_target;
  58. const char *output_filename = "a.out";
  59.  
  60. /* Name this program was invoked by.  */
  61. char *program_name;
  62.  
  63. /* The file that we're creating */
  64. bfd *output_bfd = 0;
  65.  
  66. /* Set by -G argument, for MIPS ECOFF target.  */
  67. int g_switch_value = 8;
  68.  
  69. /* Nonzero means print names of input files as processed.  */
  70. boolean trace_files;
  71.  
  72. /* Nonzero means same, but note open failures, too.  */
  73. boolean trace_file_tries;
  74.  
  75. /* Nonzero means version number was printed, so exit successfully
  76.    instead of complaining if no input files are given.  */
  77. boolean version_printed;
  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 *));
  105. static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
  106.                      const char *, bfd *,
  107.                      asection *, bfd_vma));
  108. static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
  109.                        const char *, bfd_vma,
  110.                        bfd *, asection *, bfd_vma));
  111. static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
  112.                     bfd *, asection *, bfd_vma));
  113. static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
  114.                      const char *, bfd *, asection *,
  115.                      bfd_vma));
  116. static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
  117.                     bfd *, asection *, bfd_vma));
  118.  
  119. static struct bfd_link_callbacks link_callbacks =
  120. {
  121.   add_archive_element,
  122.   multiple_definition,
  123.   multiple_common,
  124.   add_to_set,
  125.   constructor_callback,
  126.   warning_callback,
  127.   undefined_symbol,
  128.   reloc_overflow,
  129.   reloc_dangerous,
  130.   unattached_reloc,
  131.   notice_ysym
  132. };
  133.  
  134. struct bfd_link_info link_info;
  135.  
  136. static void
  137. remove_output ()
  138. {
  139.   if (output_filename) 
  140.     {
  141.       if (output_bfd && output_bfd->iostream)
  142.     fclose((FILE *)(output_bfd->iostream));
  143.       if (delete_output_file_on_failure)
  144.     unlink (output_filename);
  145.     }
  146. }
  147.  
  148. int
  149. main (argc, argv)
  150.      int argc;
  151.      char **argv;
  152. {
  153.   char *emulation;
  154.   long start_time = get_run_time ();
  155.  
  156.   program_name = argv[0];
  157.   xmalloc_set_program_name (program_name);
  158.  
  159.   bfd_init ();
  160.  
  161.   xatexit (remove_output);
  162.  
  163.   /* Initialize the data about options.  */
  164.   trace_files = trace_file_tries = version_printed = false;
  165.   config.traditional_format = false;
  166.   config.build_constructors = true;
  167.   config.dynamic_link = false;
  168.   command_line.force_common_definition = false;
  169.   command_line.interpreter = NULL;
  170.   command_line.rpath = NULL;
  171.  
  172.   link_info.callbacks = &link_callbacks;
  173.   link_info.relocateable = false;
  174.   link_info.shared = false;
  175.   link_info.strip = strip_none;
  176.   link_info.discard = discard_none;
  177.   link_info.lprefix_len = 1;
  178.   link_info.lprefix = "L";
  179.   link_info.keep_memory = true;
  180.   link_info.input_bfds = NULL;
  181.   link_info.create_object_symbols_section = NULL;
  182.   link_info.hash = NULL;
  183.   link_info.keep_hash = NULL;
  184.   link_info.notice_hash = NULL;
  185.  
  186.   ldfile_add_arch ("");
  187.  
  188.   config.make_executable = true;
  189.   force_make_executable = false;
  190.   config.magic_demand_paged = true;
  191.   config.text_read_only = true;
  192.   config.make_executable = true;
  193.  
  194.   emulation = get_emulation (argc, argv);
  195.   ldemul_choose_mode (emulation);
  196.   default_target = ldemul_choose_target ();
  197.   lang_init ();
  198.   ldemul_before_parse ();
  199.   lang_has_input_file = false;
  200.   parse_args (argc, argv);
  201.  
  202.   if (link_info.relocateable)
  203.     {
  204.       if (command_line.relax)
  205.     einfo ("%P%F: -relax and -r may not be used together\n");
  206.       if (config.dynamic_link)
  207.     einfo ("%P%F: -r and -call_shared may not be used together\n");
  208.       if (link_info.shared)
  209.     einfo ("%P%F: -r and -shared may not be used together\n");
  210.     }
  211.  
  212.   /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols).  I
  213.      don't see how else this can be handled, since in this case we
  214.      must preserve all externally visible symbols.  */
  215.   if (link_info.relocateable && link_info.strip == strip_all)
  216.     {
  217.       link_info.strip = strip_debugger;
  218.       if (link_info.discard == discard_none)
  219.     link_info.discard = discard_all;
  220.     }
  221.  
  222.   /* This essentially adds another -L directory so this must be done after
  223.      the -L's in argv have been processed.  */
  224.   set_scripts_dir ();
  225.  
  226.   if (had_script == false)
  227.     {
  228.       /* Read the emulation's appropriate default script.  */
  229.       int isfile;
  230.       char *s = ldemul_get_script (&isfile);
  231.  
  232.       if (isfile)
  233.     ldfile_open_command_file (s);
  234.       else
  235.     lex_redirect (s);
  236.       parser_input = input_script;
  237.       yyparse ();
  238.     }
  239.  
  240.   lang_final ();
  241.  
  242.   if (lang_has_input_file == false)
  243.     {
  244.       if (version_printed)
  245.     xexit (0);
  246.       einfo ("%P%F: no input files\n");
  247.     }
  248.  
  249.   if (trace_files)
  250.     {
  251.       info_msg ("%P: mode %s\n", emulation);
  252.     }
  253.  
  254.   ldemul_after_parse ();
  255.  
  256.  
  257.   if (config.map_filename)
  258.     {
  259.       if (strcmp (config.map_filename, "-") == 0)
  260.     {
  261.       config.map_file = stdout;
  262.     }
  263.       else
  264.     {
  265.       config.map_file = fopen (config.map_filename, FOPEN_WT);
  266.       if (config.map_file == (FILE *) NULL)
  267.         {
  268.           einfo ("%P%F: cannot open map file %s: %E\n",
  269.              config.map_filename);
  270.         }
  271.     }
  272.     }
  273.  
  274.  
  275.   lang_process ();
  276.  
  277.   /* Print error messages for any missing symbols, for any warning
  278.      symbols, and possibly multiple definitions */
  279.  
  280.  
  281.   if (config.text_read_only)
  282.     {
  283.       /* Look for a text section and mark the readonly attribute in it */
  284.       asection *found = bfd_get_section_by_name (output_bfd, ".text");
  285.  
  286.       if (found != (asection *) NULL)
  287.     {
  288.       found->flags |= SEC_READONLY;
  289.     }
  290.     }
  291.  
  292.   if (link_info.relocateable)
  293.     output_bfd->flags &= ~EXEC_P;
  294.   else
  295.     output_bfd->flags |= EXEC_P;
  296.  
  297.   ldwrite ();
  298.  
  299.   /* Even if we're producing relocateable output, some non-fatal errors should
  300.      be reported in the exit status.  (What non-fatal errors, if any, do we
  301.      want to ignore for relocateable output?)  */
  302.  
  303.   if (config.make_executable == false && force_make_executable == false)
  304.     {
  305.       if (trace_files == true)
  306.     {
  307.       einfo ("%P: link errors found, deleting executable `%s'\n",
  308.          output_filename);
  309.     }
  310.  
  311.       if (output_bfd->iostream)
  312.     fclose ((FILE *) (output_bfd->iostream));
  313.  
  314.       unlink (output_filename);
  315.       xexit (1);
  316.     }
  317.   else
  318.     {
  319.       if (! bfd_close (output_bfd))
  320.     einfo ("%F%B: final close failed: %E\n", output_bfd);
  321.     }
  322.  
  323.   if (config.stats)
  324.     {
  325.       extern char **environ;
  326. #ifdef HAVE_SBRK
  327.       char *lim = (char *) sbrk (0);
  328. #endif
  329.       long run_time = get_run_time () - start_time;
  330.  
  331.       fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
  332.            program_name, run_time / 1000000, run_time % 1000000);
  333. #ifdef HAVE_SBRK
  334.       fprintf (stderr, "%s: data size %ld\n", program_name,
  335.            (long) (lim - (char *) &environ));
  336. #endif
  337.     }
  338.  
  339.   /* Prevent remove_output from doing anything, after a successful link.  */
  340.   output_filename = NULL;
  341.  
  342.   xexit (0);
  343.   return 0;
  344. }
  345.  
  346. /* We need to find any explicitly given emulation in order to initialize the
  347.    state that's needed by the lex&yacc argument parser (parse_args).  */
  348.  
  349. static char *
  350. get_emulation (argc, argv)
  351.      int argc;
  352.      char **argv;
  353. {
  354.   char *emulation;
  355.   int i;
  356.  
  357.   emulation = (char *) getenv (EMULATION_ENVIRON);
  358.   if (emulation == NULL)
  359.     emulation = DEFAULT_EMULATION;
  360.  
  361.   for (i = 1; i < argc; i++)
  362.     {
  363.       if (!strncmp (argv[i], "-m", 2))
  364.     {
  365.       if (argv[i][2] == '\0')
  366.         {
  367.           /* -m EMUL */
  368.           if (i < argc - 1)
  369.         {
  370.           emulation = argv[i + 1];
  371.           i++;
  372.         }
  373.           else
  374.         {
  375.           einfo("%P%F: missing argument to -m\n");
  376.         }
  377.         }
  378.       else if (strcmp (argv[i], "-mips1") == 0
  379.            || strcmp (argv[i], "-mips2") == 0
  380.            || strcmp (argv[i], "-mips3") == 0)
  381.         {
  382.           /* FIXME: The arguments -mips1, -mips2 and -mips3 are
  383.          passed to the linker by some MIPS compilers.  They
  384.          generally tell the linker to use a slightly different
  385.          library path.  Perhaps someday these should be
  386.          implemented as emulations; until then, we just ignore
  387.          the arguments and hope that nobody ever creates
  388.          emulations named ips1, ips2 or ips3.  */
  389.         }
  390.       else if (strcmp (argv[i], "-m486") == 0)
  391.         {
  392.           /* FIXME: The argument -m486 is passed to the linker on
  393.          some Linux systems.  Hope that nobody creates an
  394.          emulation named 486.  */
  395.         }
  396.       else
  397.         {
  398.           /* -mEMUL */
  399.           emulation = &argv[i][2];
  400.         }
  401.     }
  402.     }
  403.  
  404.   return emulation;
  405. }
  406.  
  407. /* If directory DIR contains an "ldscripts" subdirectory,
  408.    add DIR to the library search path and return true,
  409.    else return false.  */
  410.  
  411. static boolean
  412. check_for_scripts_dir (dir)
  413.      char *dir;
  414. {
  415.   size_t dirlen;
  416.   char *buf;
  417.   struct stat s;
  418.   boolean res;
  419.  
  420.   dirlen = strlen (dir);
  421.   /* sizeof counts the terminating NUL.  */
  422.   buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
  423.   sprintf (buf, "%s/ldscripts", dir);
  424.  
  425.   res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
  426.   free (buf);
  427.   if (res)
  428.     ldfile_add_library_path (dir, false);
  429.   return res;
  430. }
  431.  
  432. /* Set the default directory for finding script files.
  433.    Libraries will be searched for here too, but that's ok.
  434.    We look for the "ldscripts" directory in:
  435.  
  436.    SCRIPTDIR (passed from Makefile)
  437.    the dir where this program is (for using it from the build tree)
  438.    the dir where this program is/../lib (for installing the tool suite elsewhere) */
  439.  
  440. static void
  441. set_scripts_dir ()
  442. {
  443.   char *end, *dir;
  444.   size_t dirlen;
  445.  
  446.   if (check_for_scripts_dir (SCRIPTDIR))
  447.     return;            /* We've been installed normally.  */
  448.  
  449.   /* Look for "ldscripts" in the dir where our binary is.  */
  450.   end = strrchr (program_name, '/');
  451.   if (end)
  452.     {
  453.       dirlen = end - program_name;
  454.       /* Make a copy of program_name in dir.
  455.      Leave room for later "/../lib".  */
  456.       dir = (char *) xmalloc (dirlen + 8);
  457.       strncpy (dir, program_name, dirlen);
  458.       dir[dirlen] = '\0';
  459.     }
  460.   else
  461.     {
  462.       dirlen = 1;
  463.       dir = (char *) xmalloc (dirlen + 8);
  464.       strcpy (dir, ".");
  465.     }
  466.  
  467.   if (check_for_scripts_dir (dir))
  468.     return;            /* Don't free dir.  */
  469.  
  470.   /* Look for "ldscripts" in <the dir where our binary is>/../lib.  */
  471.   strcpy (dir + dirlen, "/../lib");
  472.   if (check_for_scripts_dir (dir))
  473.     return;
  474.  
  475.   free (dir);            /* Well, we tried.  */
  476. }
  477.  
  478. void
  479. add_ysym (name)
  480.      const char *name;
  481. {
  482.   if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
  483.     {
  484.       link_info.notice_hash = ((struct bfd_hash_table *)
  485.                    xmalloc (sizeof (struct bfd_hash_table)));
  486.       if (! bfd_hash_table_init_n (link_info.notice_hash,
  487.                    bfd_hash_newfunc,
  488.                    61))
  489.     einfo ("%P%F: bfd_hash_table_init failed: %E\n");
  490.     }      
  491.  
  492.   if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
  493.       == (struct bfd_hash_entry *) NULL)
  494.     einfo ("%P%F: bfd_hash_lookup failed: %E\n");
  495. }
  496.  
  497. /* Handle the -retain-symbols-file option.  */
  498.  
  499. void
  500. add_keepsyms_file (filename)
  501.      const char *filename;
  502. {
  503.   FILE *file;
  504.   char *buf;
  505.   size_t bufsize;
  506.   int c;
  507.  
  508.   if (link_info.strip == strip_some)
  509.     einfo ("%X%P: error: duplicate retain-symbols-file\n");
  510.  
  511.   file = fopen (filename, "r");
  512.   if (file == (FILE *) NULL)
  513.     {
  514.       bfd_set_error (bfd_error_system_call);
  515.       einfo ("%X%P: %s: %E", filename);
  516.       return;
  517.     }
  518.  
  519.   link_info.keep_hash = ((struct bfd_hash_table *)
  520.              xmalloc (sizeof (struct bfd_hash_table)));
  521.   if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
  522.     einfo ("%P%F: bfd_hash_table_init failed: %E\n");
  523.  
  524.   bufsize = 100;
  525.   buf = (char *) xmalloc (bufsize);
  526.  
  527.   c = getc (file);
  528.   while (c != EOF)
  529.     {
  530.       while (isspace (c))
  531.     c = getc (file);
  532.  
  533.       if (c != EOF)
  534.     {
  535.       size_t len = 0;
  536.  
  537.       while (! isspace (c) && c != EOF)
  538.         {
  539.           buf[len] = c;
  540.           ++len;
  541.           if (len >= bufsize)
  542.         {
  543.           bufsize *= 2;
  544.           buf = xrealloc (buf, bufsize);
  545.         }
  546.           c = getc (file);
  547.         }
  548.  
  549.       buf[len] = '\0';
  550.  
  551.       if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
  552.           == (struct bfd_hash_entry *) NULL)
  553.         einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
  554.     }
  555.     }
  556.  
  557.   if (link_info.strip != strip_none)
  558.     einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
  559.  
  560.   link_info.strip = strip_some;
  561. }
  562.  
  563. /* Callbacks from the BFD linker routines.  */
  564.  
  565. /* This is called when BFD has decided to include an archive member in
  566.    a link.  */
  567.  
  568. /*ARGSUSED*/
  569. static boolean
  570. add_archive_element (info, abfd, name)
  571.      struct bfd_link_info *info;
  572.      bfd *abfd;
  573.      const char *name;
  574. {
  575.   lang_input_statement_type *input;
  576.  
  577.   input = ((lang_input_statement_type *)
  578.        xmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
  579.   input->filename = abfd->filename;
  580.   input->local_sym_name = abfd->filename;
  581.   input->the_bfd = abfd;
  582.   input->asymbols = NULL;
  583.   input->next = NULL;
  584.   input->just_syms_flag = false;
  585.   input->loaded = false;
  586.   input->search_dirs_flag = false;
  587.  
  588.   /* FIXME: The following fields are not set: header.next,
  589.      header.type, closed, passive_position, symbol_count,
  590.      next_real_file, is_archive, target, real, common_section,
  591.      common_output_section, complained.  This bit of code is from the
  592.      old decode_library_subfile function.  I don't know whether any of
  593.      those fields matters.  */
  594.  
  595.   ldlang_add_file (input);
  596.  
  597.   if (config.map_file != (FILE *) NULL)
  598.     minfo ("%s needed due to %T\n", abfd->filename, name);
  599.  
  600.   if (trace_files || trace_file_tries)
  601.     info_msg ("%I\n", input);
  602.  
  603.   return true;
  604. }
  605.  
  606. /* This is called when BFD has discovered a symbol which is defined
  607.    multiple times.  */
  608.  
  609. /*ARGSUSED*/
  610. static boolean
  611. multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
  612.      struct bfd_link_info *info;
  613.      const char *name;
  614.      bfd *obfd;
  615.      asection *osec;
  616.      bfd_vma oval;
  617.      bfd *nbfd;
  618.      asection *nsec;
  619.      bfd_vma nval;
  620. {
  621.   einfo ("%X%C: multiple definition of `%T'\n",
  622.      nbfd, nsec, nval, name);
  623.   if (obfd != (bfd *) NULL)
  624.     einfo ("%D: first defined here\n", obfd, osec, oval);
  625.   return true;
  626. }
  627.  
  628. /* This is called when there is a definition of a common symbol, or
  629.    when a common symbol is found for a symbol that is already defined,
  630.    or when two common symbols are found.  We only do something if
  631.    -warn-common was used.  */
  632.  
  633. /*ARGSUSED*/
  634. static boolean
  635. multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
  636.      struct bfd_link_info *info;
  637.      const char *name;
  638.      bfd *obfd;
  639.      enum bfd_link_hash_type otype;
  640.      bfd_vma osize;
  641.      bfd *nbfd;
  642.      enum bfd_link_hash_type ntype;
  643.      bfd_vma nsize;
  644. {
  645.   if (! config.warn_common)
  646.     return true;
  647.  
  648.   if (ntype == bfd_link_hash_defined
  649.       || ntype == bfd_link_hash_indirect)
  650.     {
  651.       ASSERT (otype == bfd_link_hash_common);
  652.       einfo ("%B: warning: definition of `%T' overriding common\n",
  653.          nbfd, name);
  654.       if (obfd != NULL)
  655.     einfo ("%B: warning: common is here\n", obfd);
  656.     }
  657.   else if (otype == bfd_link_hash_defined
  658.        || otype == bfd_link_hash_indirect)
  659.     {
  660.       ASSERT (ntype == bfd_link_hash_common);
  661.       einfo ("%B: warning: common of `%T' overridden by definition\n",
  662.          nbfd, name);
  663.       if (obfd != NULL)
  664.     einfo ("%B: warning: defined here\n", obfd);
  665.     }
  666.   else
  667.     {
  668.       ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
  669.       if (osize > nsize)
  670.     {
  671.       einfo ("%B: warning: common of `%T' overridden by larger common\n",
  672.          nbfd, name);
  673.       if (obfd != NULL)
  674.         einfo ("%B: warning: larger common is here\n", obfd);
  675.     }
  676.       else if (nsize > osize)
  677.     {
  678.       einfo ("%B: warning: common of `%T' overriding smaller common\n",
  679.          nbfd, name);
  680.       if (obfd != NULL)
  681.         einfo ("%B: warning: smaller common is here\n", obfd);
  682.     }
  683.       else
  684.     {
  685.       einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
  686.       if (obfd != NULL)
  687.         einfo ("%B: warning: previous common is here\n", obfd);
  688.     }
  689.     }
  690.  
  691.   return true;
  692. }
  693.  
  694. /* This is called when BFD has discovered a set element.  H is the
  695.    entry in the linker hash table for the set.  SECTION and VALUE
  696.    represent a value which should be added to the set.  */
  697.  
  698. /*ARGSUSED*/
  699. static boolean
  700. add_to_set (info, h, reloc, abfd, section, value)
  701.      struct bfd_link_info *info;
  702.      struct bfd_link_hash_entry *h;
  703.      bfd_reloc_code_real_type reloc;
  704.      bfd *abfd;
  705.      asection *section;
  706.      bfd_vma value;
  707. {
  708.   if (! config.build_constructors)
  709.     return true;
  710.  
  711.   ldctor_add_set_entry (h, reloc, section, value);
  712.  
  713.   if (h->type == bfd_link_hash_new)
  714.     {
  715.       h->type = bfd_link_hash_undefined;
  716.       h->u.undef.abfd = abfd;
  717.       /* We don't call bfd_link_add_undef to add this to the list of
  718.      undefined symbols because we are going to define it
  719.      ourselves.  */
  720.     }
  721.  
  722.   return true;
  723. }
  724.  
  725. /* This is called when BFD has discovered a constructor.  This is only
  726.    called for some object file formats--those which do not handle
  727.    constructors in some more clever fashion.  This is similar to
  728.    adding an element to a set, but less general.  */
  729.  
  730. static boolean
  731. constructor_callback (info, constructor, name, abfd, section, value)
  732.      struct bfd_link_info *info;
  733.      boolean constructor;
  734.      const char *name;
  735.      bfd *abfd;
  736.      asection *section;
  737.      bfd_vma value;
  738. {
  739.   char *set_name;
  740.   char *s;
  741.   struct bfd_link_hash_entry *h;
  742.  
  743.   if (! config.build_constructors)
  744.     return true;
  745.  
  746.   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
  747.      useful error message.  */
  748.   if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL)
  749.     einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported");
  750.  
  751.   set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
  752.   s = set_name;
  753.   if (bfd_get_symbol_leading_char (abfd) != '\0')
  754.     *s++ = bfd_get_symbol_leading_char (abfd);
  755.   if (constructor)
  756.     strcpy (s, "__CTOR_LIST__");
  757.   else
  758.     strcpy (s, "__DTOR_LIST__");
  759.  
  760.   if (config.map_file != (FILE *) NULL)
  761.     fprintf (config.map_file,
  762.          "Adding %s to constructor/destructor set %s\n", name, set_name);
  763.  
  764.   h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
  765.   if (h == (struct bfd_link_hash_entry *) NULL)
  766.     einfo ("%P%F: bfd_link_hash_lookup failed: %E");
  767.   if (h->type == bfd_link_hash_new)
  768.     {
  769.       h->type = bfd_link_hash_undefined;
  770.       h->u.undef.abfd = abfd;
  771.       /* We don't call bfd_link_add_undef to add this to the list of
  772.      undefined symbols because we are going to define it
  773.      ourselves.  */
  774.     }
  775.  
  776.   ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value);
  777.   return true;
  778. }
  779.  
  780. /* This is called when there is a reference to a warning symbol.  */
  781.  
  782. /*ARGSUSED*/
  783. static boolean
  784. warning_callback (info, warning)
  785.      struct bfd_link_info *info;
  786.      const char *warning;
  787. {
  788.   einfo ("%P: %s\n", warning);
  789.   return true;
  790. }
  791.  
  792. /* This is called when an undefined symbol is found.  */
  793.  
  794. /*ARGSUSED*/
  795. static boolean
  796. undefined_symbol (info, name, abfd, section, address)
  797.      struct bfd_link_info *info;
  798.      const char *name;
  799.      bfd *abfd;
  800.      asection *section;
  801.      bfd_vma address;
  802. {
  803.   static char *error_name;
  804.   static unsigned int error_count;
  805.  
  806. #define MAX_ERRORS_IN_A_ROW 5
  807.  
  808.   if (config.warn_once)
  809.     {
  810.       static struct bfd_hash_table *hash;
  811.  
  812.       /* Only warn once about a particular undefined symbol.  */
  813.  
  814.       if (hash == NULL)
  815.     {
  816.       hash = ((struct bfd_hash_table *)
  817.           xmalloc (sizeof (struct bfd_hash_table)));
  818.       if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
  819.         einfo ("%F%P: bfd_hash_table_init failed: %E\n");
  820.     }
  821.  
  822.       if (bfd_hash_lookup (hash, name, false, false) != NULL)
  823.     return true;
  824.  
  825.       if (bfd_hash_lookup (hash, name, true, true) == NULL)
  826.     einfo ("%F%P: bfd_hash_lookup failed: %E\n");
  827.     }
  828.  
  829.   /* We never print more than a reasonable number of errors in a row
  830.      for a single symbol.  */
  831.   if (error_name != (char *) NULL
  832.       && strcmp (name, error_name) == 0)
  833.     ++error_count;
  834.   else
  835.     {
  836.       error_count = 0;
  837.       if (error_name != (char *) NULL)
  838.     free (error_name);
  839.       error_name = buystring (name);
  840.     }
  841.  
  842.   if (error_count < MAX_ERRORS_IN_A_ROW)
  843.     einfo ("%X%C: undefined reference to `%T'\n",
  844.        abfd, section, address, name);
  845.   else if (error_count == MAX_ERRORS_IN_A_ROW)
  846.     einfo ("%D: more undefined references to `%T' follow\n",
  847.        abfd, section, address, name);
  848.  
  849.   return true;
  850. }
  851.  
  852. /* This is called when a reloc overflows.  */
  853.  
  854. /*ARGSUSED*/
  855. static boolean
  856. reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
  857.      struct bfd_link_info *info;
  858.      const char *name;
  859.      const char *reloc_name;
  860.      bfd_vma addend;
  861.      bfd *abfd;
  862.      asection *section;
  863.      bfd_vma address;
  864. {
  865.   if (abfd == (bfd *) NULL)
  866.     einfo ("%P%X: generated");
  867.   else
  868.     einfo ("%X%C:", abfd, section, address);
  869.   einfo (" relocation truncated to fit: %s %T", reloc_name, name);
  870.   if (addend != 0)
  871.     einfo ("+%v", addend);
  872.   einfo ("\n");
  873.   return true;
  874. }
  875.  
  876. /* This is called when a dangerous relocation is made.  */
  877.  
  878. /*ARGSUSED*/
  879. static boolean
  880. reloc_dangerous (info, message, abfd, section, address)
  881.      struct bfd_link_info *info;
  882.      const char *message;
  883.      bfd *abfd;
  884.      asection *section;
  885.      bfd_vma address;
  886. {
  887.   if (abfd == (bfd *) NULL)
  888.     einfo ("%P%X: generated");
  889.   else
  890.     einfo ("%X%C:", abfd, section, address);
  891.   einfo ("dangerous relocation: %s\n", message);
  892.   return true;
  893. }
  894.  
  895. /* This is called when a reloc is being generated attached to a symbol
  896.    that is not being output.  */
  897.  
  898. /*ARGSUSED*/
  899. static boolean
  900. unattached_reloc (info, name, abfd, section, address)
  901.      struct bfd_link_info *info;
  902.      const char *name;
  903.      bfd *abfd;
  904.      asection *section;
  905.      bfd_vma address;
  906. {
  907.   if (abfd == (bfd *) NULL)
  908.     einfo ("%P%X: generated");
  909.   else
  910.     einfo ("%X%C:", abfd, section, address);
  911.   einfo (" reloc refers to symbol `%T' which is not being output\n", name);
  912.   return true;
  913. }
  914.  
  915. /* This is called when a symbol in notice_hash is found.  Symbols are
  916.    put in notice_hash using the -y option.  */
  917.  
  918. /*ARGSUSED*/
  919. static boolean
  920. notice_ysym (info, name, abfd, section, value)
  921.      struct bfd_link_info *info;
  922.      const char *name;
  923.      bfd *abfd;
  924.      asection *section;
  925.      bfd_vma value;
  926. {
  927.   einfo ("%B: %s %s\n", abfd,
  928.      bfd_is_und_section (section) ? "reference to" : "definition of",
  929.      name);
  930.   return true;
  931. }
  932.