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