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