home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / binutils.2 / ld / ldmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  25.8 KB  |  1,052 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2.    Written by Steve Chamberlain steve@cygnus.com
  3.  
  4. This file is part of GLD, the Gnu Linker.
  5.  
  6. GLD is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GLD is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GLD; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23.  
  24. #include "config.h"
  25. #include "ld.h"
  26. #include "ldmain.h"
  27. #include "ldmisc.h"
  28. #include "ldwrite.h"
  29. #include "ldgram.h"
  30. #include "ldsym.h"
  31. #include "ldlang.h"
  32. #include "ldemul.h"
  33. #include "ldlex.h"
  34. #include "ldfile.h"
  35. #include "ldindr.h"
  36. #include "ldwarn.h"
  37. #include "ldctor.h"
  38. #include "lderror.h"
  39.  
  40. /* IMPORTS */
  41. extern boolean lang_has_input_file;
  42. extern boolean trace_files;
  43.  
  44. /* EXPORTS */
  45.  
  46. char *default_target;
  47. char *output_filename = "a.out";
  48.  
  49. /* Name this program was invoked by.  */
  50. char *program_name;
  51.  
  52. /* The file that we're creating */
  53. bfd *output_bfd = 0;
  54.  
  55. extern boolean option_v;
  56.  
  57. /* set if -y on the command line */
  58. int had_y;
  59.  
  60. /* The local symbol prefix */
  61. char lprefix = 'L';
  62.  
  63. /* Set by -G argument, for MIPS ECOFF target.  */
  64. int g_switch_value = 8;
  65.  
  66. /* Count the number of global symbols multiply defined.  */
  67. int multiple_def_count;
  68.  
  69. /* Count the number of symbols defined through common declarations.
  70.    This count is referenced in symdef_library, linear_library, and
  71.    modified by enter_global_ref.
  72.  
  73.    It is incremented when a symbol is created as a common, and
  74.    decremented when the common declaration is overridden
  75.  
  76.    Another way of thinking of it is that this is a count of
  77.    all ldsym_types with a ->scoms field */
  78.  
  79. unsigned int commons_pending;
  80.  
  81. /* Count the number of global symbols referenced and not defined.
  82.    common symbols are not included in this count.   */
  83.  
  84. unsigned int undefined_global_sym_count;
  85.  
  86. /* Count the number of warning symbols encountered. */
  87. int warning_count;
  88.  
  89. /* have we had a load script ? */
  90. extern boolean had_script;
  91.  
  92. /* Nonzero means print names of input files as processed.  */
  93. boolean trace_files;
  94.  
  95. /* 1 => write load map.  */
  96. boolean write_map;
  97.  
  98.  
  99. int unix_relocate;
  100.  
  101. #ifdef GNU960
  102. /* Indicates whether output file will be b.out (default) or coff */
  103. enum target_flavour output_flavor = BFD_BOUT_FORMAT;
  104.  
  105. #endif
  106.  
  107. /* Force the make_executable to be output, even if there are non-fatal
  108.    errors */
  109. boolean force_make_executable;
  110.  
  111. /* A count of the total number of local symbols ever seen - by adding
  112.  the symbol_count field of each newly read afile.*/
  113.  
  114. unsigned int total_symbols_seen;
  115.  
  116. /* A count of the number of read files - the same as the number of elements
  117.  in file_chain
  118.  */
  119. unsigned int total_files_seen;
  120.  
  121. /* IMPORTS */
  122. args_type command_line;
  123. ld_config_type config;
  124. void
  125. main (argc, argv)
  126.      char **argv;
  127.      int argc;
  128. {
  129.   char *emulation;
  130.  
  131.   program_name = argv[0];
  132.   output_filename = "a.out";
  133.  
  134.   bfd_init ();
  135.  
  136. #ifdef GNU960
  137.   {
  138.     int i;
  139.  
  140.     check_v960 (argc, argv);
  141.     emulation = "gld960";
  142.     for (i = 1; i < argc; i++)
  143.       {
  144.     if (!strcmp (argv[i], "-Fcoff"))
  145.       {
  146.         emulation = "lnk960";
  147.         output_flavor = BFD_COFF_FORMAT;
  148.         break;
  149.       }
  150.       }
  151.   }
  152. #else
  153.   emulation = (char *) getenv (EMULATION_ENVIRON);
  154. #endif
  155.  
  156.   /* Initialize the data about options.  */
  157.  
  158.  
  159.   trace_files = false;
  160.   write_map = false;
  161.   config.relocateable_output = false;
  162.   unix_relocate = 0;
  163.   command_line.force_common_definition = false;
  164.  
  165.   init_bfd_error_vector ();
  166.   ldsym_init ();
  167.   ldfile_add_arch ("");
  168.  
  169.   config.make_executable = true;
  170.   force_make_executable = false;
  171.  
  172.  
  173.   /* Initialize the cumulative counts of symbols.  */
  174.   undefined_global_sym_count = 0;
  175.   warning_count = 0;
  176.   multiple_def_count = 0;
  177.   commons_pending = 0;
  178.  
  179.   config.magic_demand_paged = true;
  180.   config.text_read_only = true;
  181.   config.make_executable = true;
  182.   if (emulation == (char *) NULL)
  183.     {
  184.       emulation = DEFAULT_EMULATION;
  185.     }
  186.  
  187.   ldemul_choose_mode (emulation);
  188.   default_target = ldemul_choose_target ();
  189.   lang_init ();
  190.   ldemul_before_parse ();
  191.   lang_has_input_file = false;
  192.   parse_args (argc, argv);
  193.  
  194.   if (config.relocateable_output && command_line.relax)
  195.     {
  196.       einfo ("%P%F: -relax and -r may not be used together\n");
  197.     }
  198.   lang_final ();
  199.  
  200.   if (trace_files)
  201.     {
  202.       info ("%P: mode %s\n", emulation);
  203.     }
  204.   if (lang_has_input_file == false)
  205.     {
  206.       einfo ("%P%F: No input files\n");
  207.     }
  208.  
  209.   ldemul_after_parse ();
  210.  
  211.  
  212.   if (config.map_filename)
  213.     {
  214.       if (strcmp (config.map_filename, "-") == 0)
  215.     {
  216.       config.map_file = stdout;
  217.     }
  218.       else
  219.     {
  220.       config.map_file = fopen (config.map_filename, FOPEN_WT);
  221.       if (config.map_file == (FILE *) NULL)
  222.         {
  223.           einfo ("%P%F: can't open map file %s\n",
  224.              config.map_filename);
  225.         }
  226.     }
  227.     }
  228.  
  229.  
  230.   lang_process ();
  231.  
  232.   /* Print error messages for any missing symbols, for any warning
  233.      symbols, and possibly multiple definitions */
  234.  
  235.  
  236.   if (config.text_read_only)
  237.     {
  238.       /* Look for a text section and mark the readonly attribute in it */
  239.       asection *found = bfd_get_section_by_name (output_bfd, ".text");
  240.  
  241.       if (found != (asection *) NULL)
  242.     {
  243.       found->flags |= SEC_READONLY;
  244.     }
  245.     }
  246.  
  247.   if (config.relocateable_output)
  248.     output_bfd->flags &= ~EXEC_P;
  249.   else
  250.     output_bfd->flags |= EXEC_P;
  251.  
  252.   ldwrite ();
  253.  
  254.   /* Even if we're producing relocateable output, some non-fatal errors should
  255.      be reported in the exit status.  (What non-fatal errors, if any, do we
  256.      want to ignore for relocateable output?)  */
  257.  
  258.   if (config.make_executable == false && force_make_executable == false)
  259.     {
  260.       if (trace_files == true)
  261.     {
  262.       einfo ("%P: Link errors found, deleting executable `%s'\n",
  263.          output_filename);
  264.     }
  265.  
  266.       if (output_bfd->iostream)
  267.     fclose ((FILE *) (output_bfd->iostream));
  268.  
  269.       unlink (output_filename);
  270.       exit (1);
  271.     }
  272.   else
  273.     {
  274.       bfd_close (output_bfd);
  275.     }
  276.  
  277.   exit (0);
  278. }                /* main() */
  279.  
  280. void
  281. Q_read_entry_symbols (desc, entry)
  282.      bfd *desc;
  283.      struct lang_input_statement_struct *entry;
  284. {
  285.   if (entry->asymbols == (asymbol **) NULL)
  286.     {
  287.       bfd_size_type table_size = get_symtab_upper_bound (desc);
  288.  
  289.       entry->asymbols = (asymbol **) ldmalloc (table_size);
  290.       entry->symbol_count = bfd_canonicalize_symtab (desc, entry->asymbols);
  291.     }
  292. }
  293.  
  294. /*
  295.  * turn this item into a reference
  296.  */
  297. void
  298. refize (sp, nlist_p)
  299.      ldsym_type *sp;
  300.      asymbol **nlist_p;
  301. {
  302.   asymbol *sym = *nlist_p;
  303.  
  304.   sym->value = 0;
  305.   sym->flags = 0;
  306.   sym->section = &bfd_und_section;
  307.   sym->udata = (PTR) (sp->srefs_chain);
  308.   sp->srefs_chain = nlist_p;
  309. }
  310.  
  311. /*
  312. This function is called for each name which is seen which has a global
  313. scope. It enters the name into the global symbol table in the correct
  314. symbol on the correct chain. Remember that each ldsym_type has three
  315. chains attatched, one of all definitions of a symbol, one of all
  316. references of a symbol and one of all common definitions of a symbol.
  317.  
  318. When the function is over, the supplied is left connected to the bfd
  319. to which is was born, with its udata field pointing to the next member
  320. on the chain in which it has been inserted.
  321.  
  322. A certain amount of jigery pokery is necessary since commons come
  323. along and upset things, we only keep one item in the common chain; the
  324. one with the biggest size seen sofar. When another common comes along
  325. it either bumps the previous definition into the ref chain, since it
  326. is bigger, or gets turned into a ref on the spot since the one on the
  327. common chain is already bigger. If a real definition comes along then
  328. the common gets bumped off anyway.
  329.  
  330. Whilst all this is going on we keep a count of the number of multiple
  331. definitions seen, undefined global symbols and pending commons.
  332. */
  333.  
  334. extern boolean relaxing;
  335.  
  336. void
  337. DEFUN (Q_enter_global_ref, (nlist_p, name),
  338.        asymbol ** nlist_p AND    /* pointer into symbol table from incoming bfd */
  339.        CONST char *name /* name of symbol in linker table */ )
  340. {
  341.   asymbol *sym = *nlist_p;
  342.   ldsym_type *sp;
  343.  
  344.   /* Lookup the name from the incoming bfd's symbol table in the
  345.      linker's global symbol table */
  346.  
  347.  
  348.   flagword this_symbol_flags = sym->flags;
  349.  
  350.   sp = ldsym_get (name);
  351.  
  352.  
  353.   /* If this symbol already has udata, it means that something strange
  354.      has happened.
  355.  
  356.      The strange thing is that we've had an undefined symbol resolved by
  357.      an alias, but the thing the alias defined wasn't in the file. So
  358.      the symbol got a udata entry, but the file wasn't loaded.  Then
  359.      later on the file was loaded, but we don't need to do this
  360.      processing again */
  361.  
  362.  
  363.   if (sym->udata)
  364.     return;
  365.  
  366.  
  367.   if (flag_is_constructor (this_symbol_flags))
  368.     {
  369.       /* Add this constructor to the list we keep */
  370.       ldlang_add_constructor (sp);
  371.       /* Turn any commons into refs */
  372.       if (sp->scoms_chain != (asymbol **) NULL)
  373.     {
  374.       refize (sp, sp->scoms_chain);
  375.       sp->scoms_chain = 0;
  376.     }
  377.  
  378.  
  379.     }
  380.   else
  381.     {
  382.       if (bfd_is_com_section (sym->section))
  383.     {
  384.       /* If we have a definition of this symbol already then
  385.      this common turns into a reference. Also we only
  386.      ever point to the largest common, so if we
  387.      have a common, but it's bigger that the new symbol
  388.      the turn this into a reference too. */
  389.       if (sp->sdefs_chain)
  390.         {
  391.           /* This is a common symbol, but we already have a definition
  392.        for it, so just link it into the ref chain as if
  393.        it were a reference  */
  394.           refize (sp, nlist_p);
  395.         }
  396.       else if (sp->scoms_chain)
  397.         {
  398.           /* If we have a previous common, keep only the biggest */
  399.           if ((*(sp->scoms_chain))->value > sym->value)
  400.         {
  401.           /* other common is bigger, throw this one away */
  402.           refize (sp, nlist_p);
  403.         }
  404.           else if (sp->scoms_chain != nlist_p)
  405.         {
  406.           /* other common is smaller, throw that away */
  407.           refize (sp, sp->scoms_chain);
  408.           sp->scoms_chain = nlist_p;
  409.         }
  410.         }
  411.       else
  412.         {
  413.           /* This is the first time we've seen a common, so remember it
  414.        - if it was undefined before, we know it's defined now. If
  415.        the symbol has been marked as really being a constructor,
  416.        then treat this as a ref
  417.        */
  418.           if (sp->flags & SYM_CONSTRUCTOR)
  419.         {
  420.           /* Turn this into a ref */
  421.           refize (sp, nlist_p);
  422.         }
  423.           else
  424.         {
  425.           /* treat like a common */
  426.           if (sp->srefs_chain)
  427.             undefined_global_sym_count--;
  428.  
  429.           commons_pending++;
  430.           sp->scoms_chain = nlist_p;
  431.         }
  432.         }
  433.     }
  434.  
  435.       else if (sym->section != &bfd_und_section)
  436.     {
  437.       /* This is the definition of a symbol, add to def chain */
  438.       if (sp->sdefs_chain && (*(sp->sdefs_chain))->section != sym->section)
  439.         {
  440.           /* Multiple definition */
  441.           asymbol *sy = *(sp->sdefs_chain);
  442.           lang_input_statement_type *stat =
  443.           (lang_input_statement_type *) bfd_asymbol_bfd (sy)->usrdata;
  444.           lang_input_statement_type *stat1 =
  445.           (lang_input_statement_type *) bfd_asymbol_bfd (sym)->usrdata;
  446.           asymbol **stat1_symbols = stat1 ? stat1->asymbols : 0;
  447.           asymbol **stat_symbols = stat ? stat->asymbols : 0;
  448.  
  449.           multiple_def_count++;
  450.           einfo ("%X%C: multiple definition of `%T'\n",
  451.              bfd_asymbol_bfd (sym), sym->section, stat1_symbols, sym->value, sym);
  452.  
  453.           einfo ("%X%C: first seen here\n",
  454.         bfd_asymbol_bfd (sy), sy->section, stat_symbols, sy->value);
  455.         }
  456.       else
  457.         {
  458.           sym->udata = (PTR) (sp->sdefs_chain);
  459.           sp->sdefs_chain = nlist_p;
  460.         }
  461.       /* A definition overrides a common symbol */
  462.       if (sp->scoms_chain)
  463.         {
  464.           refize (sp, sp->scoms_chain);
  465.           sp->scoms_chain = 0;
  466.           commons_pending--;
  467.         }
  468.       else if (sp->srefs_chain && relaxing == false)
  469.         {
  470.           /* If previously was undefined, then remember as defined */
  471.           undefined_global_sym_count--;
  472.         }
  473.     }
  474.       else
  475.     {
  476.       if (sp->scoms_chain == (asymbol **) NULL
  477.           && sp->srefs_chain == (asymbol **) NULL
  478.           && sp->sdefs_chain == (asymbol **) NULL)
  479.         {
  480.           /* And it's the first time we've seen it */
  481.           undefined_global_sym_count++;
  482.  
  483.         }
  484.  
  485.       refize (sp, nlist_p);
  486.     }
  487.     }
  488.  
  489.   ASSERT (sp->sdefs_chain == 0 || sp->scoms_chain == 0);
  490.   ASSERT (sp->scoms_chain == 0 || (*(sp->scoms_chain))->udata == 0);
  491.  
  492.  
  493. }
  494.  
  495. static void
  496. Q_enter_file_symbols (entry)
  497.      lang_input_statement_type *entry;
  498. {
  499.   asymbol **q;
  500.  
  501.   entry->common_section =
  502.     bfd_make_section_old_way (entry->the_bfd, "COMMON");
  503.   entry->common_section->flags = SEC_NEVER_LOAD;
  504.   ldlang_add_file (entry);
  505.  
  506.  
  507.   if (trace_files || option_v)
  508.     {
  509.       info ("%I\n", entry);
  510.     }
  511.  
  512.   total_symbols_seen += entry->symbol_count;
  513.   total_files_seen++;
  514.   if (entry->symbol_count)
  515.     {
  516.       for (q = entry->asymbols; *q; q++)
  517.     {
  518.       asymbol *p = *q;
  519.  
  520.       if (had_y && p->name)
  521.         {
  522.           /* look up the symbol anyway to see if the trace bit was
  523.        set */
  524.           ldsym_type *s = ldsym_get (p->name);
  525.           if (s->flags & SYM_Y)
  526.         {
  527.           einfo ("%B: %s %T\n", entry->the_bfd,
  528.              p->section == &bfd_und_section ? "reference to" : "definition of ",
  529.              p);
  530.         }
  531.         }
  532.  
  533.       if (p->section == &bfd_ind_section)
  534.         {
  535.           add_indirect (q);
  536.         }
  537.       else if (p->flags & BSF_WARNING)
  538.         {
  539.           add_warning (p);
  540.         }
  541.       else if (p->section == &bfd_und_section
  542.            || (p->flags & BSF_GLOBAL)
  543.            || bfd_is_com_section (p->section)
  544.            || (p->flags & BSF_CONSTRUCTOR))
  545.  
  546.         {
  547.  
  548.           asymbol *p = *q;
  549.  
  550.           if (p->flags & BSF_INDIRECT)
  551.         {
  552.           add_indirect (q);
  553.         }
  554.           else if (p->flags & BSF_WARNING)
  555.         {
  556.           add_warning (p);
  557.         }
  558.           else if (p->section == &bfd_und_section
  559.                || (p->flags & BSF_GLOBAL)
  560.                || bfd_is_com_section (p->section)
  561.                || (p->flags & BSF_CONSTRUCTOR))
  562.         {
  563.           Q_enter_global_ref (q, p->name);
  564.         }
  565.  
  566.         }
  567.  
  568.     }
  569.     }
  570. }
  571.  
  572.  
  573. /* Searching libraries */
  574.  
  575. struct lang_input_statement_struct *decode_library_subfile ();
  576. void linear_library (), symdef_library ();
  577.  
  578. /* Search the library ENTRY, already open on descriptor DESC.
  579.    This means deciding which library members to load,
  580.    making a chain of `struct lang_input_statement_struct' for those members,
  581.    and entering their global symbols in the hash table.  */
  582.  
  583. void
  584. search_library (entry)
  585.      struct lang_input_statement_struct *entry;
  586. {
  587.  
  588.   /* No need to load a library if no undefined symbols */
  589.   if (!undefined_global_sym_count)
  590.     return;
  591.  
  592.   if (bfd_has_map (entry->the_bfd))
  593.     symdef_library (entry);
  594.   else
  595.     linear_library (entry);
  596.  
  597. }
  598.  
  599. #ifdef GNU960
  600. static
  601.   boolean
  602. gnu960_check_format (abfd, format)
  603.      bfd *abfd;
  604.      bfd_format format;
  605. {
  606.   boolean retval;
  607.  
  608.   if ((bfd_check_format (abfd, format) == true)
  609.       && (abfd->xvec->flavour == output_flavor))
  610.     {
  611.       return true;
  612.     }
  613.  
  614.  
  615.   return false;
  616. }
  617.  
  618. #endif
  619.  
  620. void
  621. ldmain_open_file_read_symbol (entry)
  622.      struct lang_input_statement_struct *entry;
  623. {
  624.   if (entry->asymbols == (asymbol **) NULL
  625.       && entry->real == true
  626.       && entry->filename != (char *) NULL)
  627.     {
  628.       ldfile_open_file (entry);
  629.  
  630.  
  631. #ifdef GNU960
  632.       if (gnu960_check_format (entry->the_bfd, bfd_object))
  633. #else
  634.       if (bfd_check_format (entry->the_bfd, bfd_object))
  635. #endif
  636.     {
  637.       entry->the_bfd->usrdata = (PTR) entry;
  638.  
  639.  
  640.       Q_read_entry_symbols (entry->the_bfd, entry);
  641.  
  642.       /* look through the sections in the file and see if any of them
  643.          are constructors */
  644.       ldlang_check_for_constructors (entry);
  645.  
  646.       Q_enter_file_symbols (entry);
  647.     }
  648. #ifdef GNU960
  649.       else if (gnu960_check_format (entry->the_bfd, bfd_archive))
  650. #else
  651.       else if (bfd_check_format (entry->the_bfd, bfd_archive))
  652. #endif
  653.     {
  654.       entry->the_bfd->usrdata = (PTR) entry;
  655.  
  656.       entry->subfiles = (lang_input_statement_type *) NULL;
  657.       search_library (entry);
  658.     }
  659.       else
  660.     {
  661.       einfo ("%F%B: malformed input file (not rel or archive) \n",
  662.          entry->the_bfd);
  663.     }
  664.     }
  665.  
  666. }
  667.  
  668. /* Construct and return a lang_input_statement_struct for a library member.
  669.    The library's lang_input_statement_struct is library_entry,
  670.    and the library is open on DESC.
  671.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  672.    We store the length of the member into *LENGTH_LOC.  */
  673.  
  674. lang_input_statement_type *
  675. decode_library_subfile (library_entry, subfile_offset)
  676.      struct lang_input_statement_struct *library_entry;
  677.      bfd *subfile_offset;
  678. {
  679.   register struct lang_input_statement_struct *subentry;
  680.  
  681.  
  682.   /* First, check if we already have a loaded
  683.      lang_input_statement_struct  for this library subfile.  If so,
  684.      just return it.  Otherwise, allocate some space and build a new one. */
  685.  
  686.   if (subfile_offset->usrdata
  687.       && ((struct lang_input_statement_struct *) subfile_offset->usrdata)->
  688.       loaded == true)
  689.     {
  690.       subentry = (struct lang_input_statement_struct *) subfile_offset->usrdata;
  691.     }
  692.   else
  693.     {
  694.       subentry =
  695.     (struct lang_input_statement_struct *)
  696.     ldmalloc ((bfd_size_type) (sizeof (struct lang_input_statement_struct)));
  697.  
  698.       subentry->filename = subfile_offset->filename;
  699.       subentry->local_sym_name = subfile_offset->filename;
  700.       subentry->asymbols = 0;
  701.       subentry->the_bfd = subfile_offset;
  702.       subentry->subfiles = 0;
  703.       subentry->next = 0;
  704.       subentry->superfile = library_entry;
  705.       subentry->is_archive = false;
  706.  
  707.       subentry->just_syms_flag = false;
  708.       subentry->loaded = false;
  709.       subentry->chain = 0;
  710.     }
  711.   return subentry;
  712. }
  713.  
  714. boolean subfile_wanted_p ();
  715. void
  716. clear_syms (entry, offset)
  717.      struct lang_input_statement_struct *entry;
  718.      file_ptr offset;
  719. {
  720.   carsym *car;
  721.   unsigned long indx = bfd_get_next_mapent (entry->the_bfd,
  722.                         BFD_NO_MORE_SYMBOLS,
  723.                         &car);
  724.  
  725.   while (indx != BFD_NO_MORE_SYMBOLS)
  726.     {
  727.       if (car->file_offset == offset)
  728.     {
  729.       car->name = 0;
  730.     }
  731.       indx = bfd_get_next_mapent (entry->the_bfd, indx, &car);
  732.     }
  733.  
  734. }
  735.  
  736. /* Search a library that has a map
  737.  */
  738. void
  739. symdef_library (entry)
  740.      struct lang_input_statement_struct *entry;
  741.  
  742. {
  743.   register struct lang_input_statement_struct *prev = 0;
  744.  
  745.   boolean not_finished = true;
  746.  
  747.   while (not_finished == true)
  748.     {
  749.       carsym *exported_library_name;
  750.       bfd *prev_archive_member_bfd = 0;
  751.  
  752.       int idx = bfd_get_next_mapent (entry->the_bfd,
  753.                      BFD_NO_MORE_SYMBOLS,
  754.                      &exported_library_name);
  755.  
  756.       not_finished = false;
  757.  
  758.       while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
  759.     {
  760.  
  761.       if (exported_library_name->name)
  762.         {
  763.  
  764.           ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
  765.  
  766.           /* If we find a symbol that appears to be needed, think carefully
  767.              about the archive member that the symbol is in.  */
  768.           /* So - if it exists, and is referenced somewhere and is
  769.              undefined or */
  770.           if (sp && sp->srefs_chain && !sp->sdefs_chain)
  771.         {
  772.           bfd *archive_member_bfd = bfd_get_elt_at_index (entry->the_bfd, idx);
  773.           struct lang_input_statement_struct *archive_member_lang_input_statement_struct;
  774.  
  775. #ifdef GNU960
  776.           if (archive_member_bfd && gnu960_check_format (archive_member_bfd, bfd_object))
  777. #else
  778.           if (archive_member_bfd && bfd_check_format (archive_member_bfd, bfd_object))
  779. #endif
  780.             {
  781.  
  782.               /* Don't think carefully about any archive member
  783.                  more than once in a given pass.  */
  784.               if (prev_archive_member_bfd != archive_member_bfd)
  785.             {
  786.  
  787.               prev_archive_member_bfd = archive_member_bfd;
  788.  
  789.               /* Read the symbol table of the archive member.  */
  790.  
  791.               if (archive_member_bfd->usrdata != (PTR) NULL)
  792.                 {
  793.  
  794.                   archive_member_lang_input_statement_struct = (lang_input_statement_type *) archive_member_bfd->usrdata;
  795.                 }
  796.               else
  797.                 {
  798.  
  799.                   archive_member_lang_input_statement_struct =
  800.                 decode_library_subfile (entry, archive_member_bfd);
  801.                   archive_member_bfd->usrdata = (PTR) archive_member_lang_input_statement_struct;
  802.  
  803.                 }
  804.  
  805.               if (archive_member_lang_input_statement_struct == 0)
  806.                 {
  807.                   einfo ("%F%I contains invalid archive member %s\n",
  808.                      entry, sp->name);
  809.                 }
  810.  
  811.               if (archive_member_lang_input_statement_struct->loaded == false)
  812.                 {
  813.  
  814.                   Q_read_entry_symbols (archive_member_bfd, archive_member_lang_input_statement_struct);
  815.                   /* Now scan the symbol table and decide whether to load.  */
  816.  
  817.  
  818.                   if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true)
  819.  
  820.                 {
  821.                   /* This member is needed; load it.
  822.                          Since we are loading something on this pass,
  823.                          we must make another pass through the symdef data.  */
  824.  
  825.                   not_finished = true;
  826.  
  827.                   Q_enter_file_symbols (archive_member_lang_input_statement_struct);
  828.  
  829.                   if (prev)
  830.                     prev->chain = archive_member_lang_input_statement_struct;
  831.                   else
  832.                     entry->subfiles = archive_member_lang_input_statement_struct;
  833.  
  834.  
  835.                   prev = archive_member_lang_input_statement_struct;
  836.  
  837.  
  838.                   /* Clear out this member's symbols from the symdef data
  839.                          so that following passes won't waste time on them.  */
  840.                   clear_syms (entry, exported_library_name->file_offset);
  841.                   archive_member_lang_input_statement_struct->loaded = true;
  842.                 }
  843.                 }
  844.             }
  845.             }
  846.         }
  847.         }
  848.       idx = bfd_get_next_mapent (entry->the_bfd, idx, &exported_library_name);
  849.     }
  850.     }
  851. }
  852.  
  853. void
  854. linear_library (entry)
  855.      struct lang_input_statement_struct *entry;
  856. {
  857.   boolean more_to_do = true;
  858.   register struct lang_input_statement_struct *prev = 0;
  859.  
  860.   if (entry->complained == false)
  861.     {
  862.       if (entry->the_bfd->xvec->flavour != bfd_target_ieee_flavour)
  863.  
  864.     {
  865.       /* IEEE can use table of contents, so this message is bogus */
  866.       einfo ("%P: library %s has bad table of contents, rerun ranlib\n",
  867.          entry->the_bfd->filename);
  868.     }
  869.       entry->complained = true;
  870.  
  871.     }
  872.   while (more_to_do)
  873.     {
  874.  
  875.       bfd *archive = bfd_openr_next_archived_file (entry->the_bfd, 0);
  876.  
  877.       more_to_do = false;
  878.       while (archive)
  879.     {
  880.       /* Don't check this file if it's already been read in
  881.          once */
  882.  
  883.       if (!archive->usrdata ||
  884.           !((lang_input_statement_type *) (archive->usrdata))->loaded)
  885.         {
  886. #ifdef GNU960
  887.           if (gnu960_check_format (archive, bfd_object))
  888. #else
  889.           if (bfd_check_format (archive, bfd_object))
  890. #endif
  891.         {
  892.           register struct lang_input_statement_struct *subentry;
  893.  
  894.           subentry = decode_library_subfile (entry,
  895.                              archive);
  896.  
  897.           archive->usrdata = (PTR) subentry;
  898.           if (!subentry)
  899.             return;
  900.           if (subentry->loaded == false)
  901.             {
  902.               Q_read_entry_symbols (archive, subentry);
  903.  
  904.               if (subfile_wanted_p (subentry) == true)
  905.             {
  906.               Q_enter_file_symbols (subentry);
  907.  
  908.               if (prev)
  909.                 prev->chain = subentry;
  910.               else
  911.                 entry->subfiles = subentry;
  912.               prev = subentry;
  913.  
  914.               more_to_do = true;
  915.               subentry->loaded = true;
  916.             }
  917.             }
  918.         }
  919.         }
  920.       archive = bfd_openr_next_archived_file (entry->the_bfd, archive);
  921.  
  922.     }
  923.  
  924.     }
  925. }
  926.  
  927. /* ENTRY is an entry for a file inside an archive
  928.     Its symbols have been read into core, but not entered into the
  929.     linker ymbol table
  930.     Return nonzero if we ought to load this file */
  931.  
  932. boolean
  933. subfile_wanted_p (entry)
  934.      struct lang_input_statement_struct *entry;
  935. {
  936.   asymbol **q;
  937.  
  938.   for (q = entry->asymbols; *q; q++)
  939.     {
  940.       asymbol *p = *q;
  941.  
  942.       /* If the symbol has an interesting definition, we could
  943.      potentially want it.  */
  944.  
  945.       if (p->flags & BSF_INDIRECT)
  946.     {
  947.       /**    add_indirect(q);*/
  948.     }
  949.  
  950.       if (bfd_is_com_section (p->section)
  951.       || (p->flags & BSF_GLOBAL)
  952.       || (p->flags & BSF_INDIRECT))
  953.     {
  954.       register ldsym_type *sp = ldsym_get_soft (p->name);
  955.  
  956.       /* If this symbol has not been hashed,
  957.          we can't be looking for it. */
  958.       if (sp != (ldsym_type *) NULL
  959.           && sp->sdefs_chain == (asymbol **) NULL)
  960.         {
  961.           if (sp->srefs_chain != (asymbol **) NULL
  962.           || sp->scoms_chain != (asymbol **) NULL)
  963.         {
  964.           /* This is a symbol we are looking for.  It is
  965.              either not yet defined or common.  If this is a
  966.              common symbol, then if the symbol in the object
  967.              file is common, we need to combine sizes.  But if
  968.              we already have a common symbol, and the symbol
  969.              in the object file is not common, we don't want
  970.              the object file: it is providing a definition for
  971.              a symbol that we already have a definition for
  972.              (this is the else condition below).  */
  973.           if (bfd_is_com_section (p->section))
  974.             {
  975.  
  976.               /* If the symbol in the table is a constructor, we won't to
  977.                  anything fancy with it */
  978.               if ((sp->flags & SYM_CONSTRUCTOR) == 0)
  979.             {
  980.               /* This libary member has something to
  981.                      say about this element. We should
  982.                      remember if its a new size  */
  983.               /* Move something from the ref list to the com list */
  984.               if (sp->scoms_chain)
  985.                 {
  986.                   /* Already a common symbol, maybe update it */
  987.                   if (p->value > (*(sp->scoms_chain))->value)
  988.                 {
  989.                   (*(sp->scoms_chain))->value = p->value;
  990.                 }
  991.                 }
  992.               else
  993.                 {
  994.                   /* Take a value from the ref chain
  995.                  Here we are moving a symbol from the owning bfd
  996.                  to another bfd. We must set up the
  997.                  common_section portion of the bfd thing */
  998.  
  999.  
  1000.  
  1001.                   sp->scoms_chain = sp->srefs_chain;
  1002.                   sp->srefs_chain =
  1003.                 (asymbol **) ((*(sp->srefs_chain))->udata);
  1004.                   (*(sp->scoms_chain))->udata = (PTR) NULL;
  1005.  
  1006.                   (*(sp->scoms_chain))->section = p->section;
  1007.                   (*(sp->scoms_chain))->flags = 0;
  1008.                   /* Remember the size of this item */
  1009.                   sp->scoms_chain[0]->value = p->value;
  1010.                   commons_pending++;
  1011.                   undefined_global_sym_count--;
  1012.                 }
  1013.               {
  1014.                 asymbol *com = *(sp->scoms_chain);
  1015.  
  1016.                 if (((lang_input_statement_type *)
  1017.                  (bfd_asymbol_bfd (com)->usrdata))->common_section ==
  1018.                 (asection *) NULL)
  1019.                   {
  1020.                 ((lang_input_statement_type *)
  1021.                  (bfd_asymbol_bfd (com)->usrdata))->common_section =
  1022.                   bfd_make_section_old_way (bfd_asymbol_bfd (com), "COMMON");
  1023.                   }
  1024.               }
  1025.             }
  1026.               ASSERT (p->udata == 0);
  1027.             }
  1028.           else if (sp->scoms_chain == (asymbol **) NULL)
  1029.             {
  1030.               if (write_map)
  1031.             {
  1032.               info ("%I needed due to %s\n", entry, sp->name);
  1033.             }
  1034.               return true;
  1035.             }
  1036.         }
  1037.         }
  1038.     }
  1039.     }
  1040.  
  1041.   return false;
  1042. }
  1043.  
  1044. void
  1045. add_ysym (text)
  1046.      char *text;
  1047. {
  1048.   ldsym_type *lookup = ldsym_get (text);
  1049.   lookup->flags |= SYM_Y;
  1050.   had_y = 1;
  1051. }
  1052.