Fix some gcc -Wall warnings:
[external/binutils.git] / ld / ldlang.c
1 /* Linker command language support.
2    Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3    Free Software Foundation, Inc.
4
5 This file is part of GLD, the Gnu Linker.
6
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 1, or (at your option)
10 any later version.
11
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libiberty.h"
25 #include "obstack.h"
26 #include "bfdlink.h"
27
28 #include "ld.h"
29 #include "ldmain.h"
30 #include "ldgram.h"
31 #include "ldexp.h"
32 #include "ldlang.h"
33 #include "ldemul.h"
34 #include "ldlex.h"
35 #include "ldmisc.h"
36 #include "ldctor.h"
37 #include "ldfile.h"
38 #include "fnmatch.h"
39
40 #include <ctype.h>
41
42 /* FORWARDS */
43 static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
44                                                          size_t,
45                                                          lang_statement_list_type*));
46
47
48 /* LOCALS */
49 static struct obstack stat_obstack;
50
51 #define obstack_chunk_alloc xmalloc
52 #define obstack_chunk_free free
53 static CONST char *startup_file;
54 static lang_statement_list_type input_file_chain;
55 static boolean placed_commons = false;
56 static lang_output_section_statement_type *default_common_section;
57 static boolean map_option_f;
58 static bfd_vma print_dot;
59 static lang_input_statement_type *first_file;
60 static lang_statement_list_type lang_output_section_statement;
61 static CONST char *current_target;
62 static CONST char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct lang_phdr *lang_phdr_list;
65
66 static void lang_for_each_statement_worker
67   PARAMS ((void (*func) (lang_statement_union_type *),
68            lang_statement_union_type *s));
69 static lang_input_statement_type *new_afile
70   PARAMS ((const char *name, lang_input_file_enum_type file_type,
71            const char *target, boolean add_to_list));
72 static void init_os PARAMS ((lang_output_section_statement_type *s));
73 static void exp_init_os PARAMS ((etree_type *));
74 static void section_already_linked PARAMS ((bfd *, asection *, PTR));
75 static boolean wildcardp PARAMS ((const char *));
76 static void wild_section PARAMS ((lang_wild_statement_type *ptr,
77                                   const char *section,
78                                   lang_input_statement_type *file,
79                                   lang_output_section_statement_type *output));
80 static lang_input_statement_type *lookup_name PARAMS ((const char *name));
81 static void load_symbols PARAMS ((lang_input_statement_type *entry,
82                                   lang_statement_list_type *));
83 static void wild_file PARAMS ((lang_wild_statement_type *, const char *,
84                                lang_input_statement_type *,
85                                lang_output_section_statement_type *));
86 static void wild PARAMS ((lang_wild_statement_type *s,
87                           const char *section, const char *file,
88                           const char *target,
89                           lang_output_section_statement_type *output));
90 static bfd *open_output PARAMS ((const char *name));
91 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
92 static void open_input_bfds
93   PARAMS ((lang_statement_union_type *statement, boolean));
94 static void lang_reasonable_defaults PARAMS ((void));
95 static void lang_place_undefineds PARAMS ((void));
96 static void map_input_to_output_sections
97   PARAMS ((lang_statement_union_type *s,
98            const char *target,
99            lang_output_section_statement_type *output_section_statement));
100 static void print_output_section_statement
101   PARAMS ((lang_output_section_statement_type *output_section_statement));
102 static void print_assignment
103   PARAMS ((lang_assignment_statement_type *assignment,
104            lang_output_section_statement_type *output_section));
105 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
106 static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
107 static void print_input_section PARAMS ((lang_input_section_type *in));
108 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
109 static void print_data_statement PARAMS ((lang_data_statement_type *data));
110 static void print_address_statement PARAMS ((lang_address_statement_type *));
111 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
112 static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
113 static void print_wild_statement
114   PARAMS ((lang_wild_statement_type *w,
115            lang_output_section_statement_type *os));
116 static void print_group
117   PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
118 static void print_statement PARAMS ((lang_statement_union_type *s,
119                                      lang_output_section_statement_type *os));
120 static void print_statement_list PARAMS ((lang_statement_union_type *s,
121                                           lang_output_section_statement_type *os));
122 static void print_statements PARAMS ((void));
123 static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
124                                    fill_type fill, unsigned int power,
125                                    asection *output_section_statement,
126                                    bfd_vma dot));
127 static bfd_vma size_input_section
128   PARAMS ((lang_statement_union_type **this_ptr,
129            lang_output_section_statement_type *output_section_statement,
130            fill_type fill, bfd_vma dot, boolean relax));
131 static void lang_finish PARAMS ((void));
132 static void ignore_bfd_errors PARAMS ((const char *, ...));
133 static void lang_check PARAMS ((void));
134 static void lang_common PARAMS ((void));
135 static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
136 static void lang_place_orphans PARAMS ((void));
137 static int topower PARAMS ((int));
138 static void lang_set_startof PARAMS ((void));
139 static void reset_memory_regions PARAMS ((void));
140 static void lang_record_phdrs PARAMS ((void));
141
142 /* EXPORTS */
143 lang_output_section_statement_type *abs_output_section;
144 lang_statement_list_type *stat_ptr = &statement_list;
145 lang_statement_list_type file_chain = { 0 };
146 const char *entry_symbol = NULL;
147 boolean entry_from_cmdline;
148 boolean lang_has_input_file = false;
149 boolean had_output_filename = false;
150 boolean lang_float_flag = false;
151 boolean delete_output_file_on_failure = false;
152 struct lang_nocrossrefs *nocrossref_list;
153
154 etree_type *base; /* Relocation base - or null */
155
156
157 #if defined(__STDC__) || defined(ALMOST_STDC)
158 #define cat(a,b) a##b
159 #else
160 #define cat(a,b) a/**/b
161 #endif
162
163 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
164
165 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
166
167 #define outside_symbol_address(q) ((q)->value +   outside_section_address(q->section))
168
169 #define SECTION_NAME_MAP_LENGTH (16)
170
171 PTR
172 stat_alloc (size)
173      size_t size;
174 {
175   return obstack_alloc (&stat_obstack, size);
176 }
177
178 /*----------------------------------------------------------------------
179   lang_for_each_statement walks the parse tree and calls the provided
180   function for each node
181 */
182
183 static void
184 lang_for_each_statement_worker (func, s)
185      void (*func) PARAMS ((lang_statement_union_type *));
186      lang_statement_union_type *s;
187 {
188   for (; s != (lang_statement_union_type *) NULL; s = s->next)
189     {
190       func (s);
191
192       switch (s->header.type)
193         {
194         case lang_constructors_statement_enum:
195           lang_for_each_statement_worker (func, constructor_list.head);
196           break;
197         case lang_output_section_statement_enum:
198           lang_for_each_statement_worker
199             (func,
200              s->output_section_statement.children.head);
201           break;
202         case lang_wild_statement_enum:
203           lang_for_each_statement_worker
204             (func,
205              s->wild_statement.children.head);
206           break;
207         case lang_group_statement_enum:
208           lang_for_each_statement_worker (func,
209                                           s->group_statement.children.head);
210           break;
211         case lang_data_statement_enum:
212         case lang_reloc_statement_enum:
213         case lang_object_symbols_statement_enum:
214         case lang_output_statement_enum:
215         case lang_target_statement_enum:
216         case lang_input_section_enum:
217         case lang_input_statement_enum:
218         case lang_assignment_statement_enum:
219         case lang_padding_statement_enum:
220         case lang_address_statement_enum:
221         case lang_fill_statement_enum:
222           break;
223         default:
224           FAIL ();
225           break;
226         }
227     }
228 }
229
230 void
231 lang_for_each_statement (func)
232      void (*func) PARAMS ((lang_statement_union_type *));
233 {
234   lang_for_each_statement_worker (func,
235                                   statement_list.head);
236 }
237
238 /*----------------------------------------------------------------------*/
239 void
240 lang_list_init (list)
241      lang_statement_list_type *list;
242 {
243   list->head = (lang_statement_union_type *) NULL;
244   list->tail = &list->head;
245 }
246
247 /*----------------------------------------------------------------------
248
249   build a new statement node for the parse tree
250
251  */
252
253 static
254 lang_statement_union_type *
255 new_statement (type, size, list)
256      enum statement_enum type;
257      size_t size;
258      lang_statement_list_type * list;
259 {
260   lang_statement_union_type *new = (lang_statement_union_type *)
261   stat_alloc (size);
262
263   new->header.type = type;
264   new->header.next = (lang_statement_union_type *) NULL;
265   lang_statement_append (list, new, &new->header.next);
266   return new;
267 }
268
269 /*
270   Build a new input file node for the language. There are several ways
271   in which we treat an input file, eg, we only look at symbols, or
272   prefix it with a -l etc.
273
274   We can be supplied with requests for input files more than once;
275   they may, for example be split over serveral lines like foo.o(.text)
276   foo.o(.data) etc, so when asked for a file we check that we havn't
277   got it already so we don't duplicate the bfd.
278
279  */
280 static lang_input_statement_type *
281 new_afile (name, file_type, target, add_to_list)
282      CONST char *name;
283      lang_input_file_enum_type file_type;
284      CONST char *target;
285      boolean add_to_list;
286 {
287   lang_input_statement_type *p;
288
289   if (add_to_list)
290     p = new_stat (lang_input_statement, stat_ptr);
291   else
292     {
293       p = ((lang_input_statement_type *)
294            stat_alloc (sizeof (lang_input_statement_type)));
295       p->header.next = NULL;
296     }
297
298   lang_has_input_file = true;
299   p->target = target;
300   switch (file_type)
301     {
302     case lang_input_file_is_symbols_only_enum:
303       p->filename = name;
304       p->is_archive = false;
305       p->real = true;
306       p->local_sym_name = name;
307       p->just_syms_flag = true;
308       p->search_dirs_flag = false;
309       break;
310     case lang_input_file_is_fake_enum:
311       p->filename = name;
312       p->is_archive = false;
313       p->real = false;
314       p->local_sym_name = name;
315       p->just_syms_flag = false;
316       p->search_dirs_flag = false;
317       break;
318     case lang_input_file_is_l_enum:
319       p->is_archive = true;
320       p->filename = name;
321       p->real = true;
322       p->local_sym_name = concat ("-l", name, (const char *) NULL);
323       p->just_syms_flag = false;
324       p->search_dirs_flag = true;
325       break;
326     case lang_input_file_is_marker_enum:
327       p->filename = name;
328       p->is_archive = false;
329       p->real = false;
330       p->local_sym_name = name;
331       p->just_syms_flag = false;
332       p->search_dirs_flag = true;
333       break;
334     case lang_input_file_is_search_file_enum:
335       p->filename = name;
336       p->is_archive = false;
337       p->real = true;
338       p->local_sym_name = name;
339       p->just_syms_flag = false;
340       p->search_dirs_flag = true;
341       break;
342     case lang_input_file_is_file_enum:
343       p->filename = name;
344       p->is_archive = false;
345       p->real = true;
346       p->local_sym_name = name;
347       p->just_syms_flag = false;
348       p->search_dirs_flag = false;
349       break;
350     default:
351       FAIL ();
352     }
353   p->the_bfd = (bfd *) NULL;
354   p->asymbols = (asymbol **) NULL;
355   p->next_real_file = (lang_statement_union_type *) NULL;
356   p->next = (lang_statement_union_type *) NULL;
357   p->symbol_count = 0;
358   p->dynamic = config.dynamic_link;
359   p->whole_archive = whole_archive;
360   p->loaded = false;
361   lang_statement_append (&input_file_chain,
362                          (lang_statement_union_type *) p,
363                          &p->next_real_file);
364   return p;
365 }
366
367 lang_input_statement_type *
368 lang_add_input_file (name, file_type, target)
369      CONST char *name;
370      lang_input_file_enum_type file_type;
371      CONST char *target;
372 {
373   lang_has_input_file = true;
374   return new_afile (name, file_type, target, true);
375 }
376
377 /* Build enough state so that the parser can build its tree */
378 void
379 lang_init ()
380 {
381   obstack_begin (&stat_obstack, 1000);
382
383   stat_ptr = &statement_list;
384
385   lang_list_init (stat_ptr);
386
387   lang_list_init (&input_file_chain);
388   lang_list_init (&lang_output_section_statement);
389   lang_list_init (&file_chain);
390   first_file = lang_add_input_file ((char *) NULL,
391                                     lang_input_file_is_marker_enum,
392                                     (char *) NULL);
393   abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
394
395   abs_output_section->bfd_section = bfd_abs_section_ptr;
396
397 }
398
399 /*----------------------------------------------------------------------
400  A region is an area of memory declared with the
401  MEMORY {  name:org=exp, len=exp ... }
402  syntax.
403
404  We maintain a list of all the regions here
405
406  If no regions are specified in the script, then the default is used
407  which is created when looked up to be the entire data space
408 */
409
410 static lang_memory_region_type *lang_memory_region_list;
411 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
412
413 lang_memory_region_type *
414 lang_memory_region_lookup (name)
415      CONST char *CONST name;
416 {
417   lang_memory_region_type *p;
418
419   for (p = lang_memory_region_list;
420        p != (lang_memory_region_type *) NULL;
421        p = p->next)
422     {
423       if (strcmp (p->name, name) == 0)
424         {
425           return p;
426         }
427     }
428
429 #if 0
430   /* This code used to always use the first region in the list as the
431      default region.  I changed it to instead use a region
432      encompassing all of memory as the default region.  This permits
433      NOLOAD sections to work reasonably without requiring a region.
434      People should specify what region they mean, if they really want
435      a region.  */
436   if (strcmp (name, "*default*") == 0)
437     {
438       if (lang_memory_region_list != (lang_memory_region_type *) NULL)
439         {
440           return lang_memory_region_list;
441         }
442     }
443 #endif
444
445   {
446     lang_memory_region_type *new =
447     (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
448
449     new->name = buystring (name);
450     new->next = (lang_memory_region_type *) NULL;
451
452     *lang_memory_region_list_tail = new;
453     lang_memory_region_list_tail = &new->next;
454     new->origin = 0;
455     new->flags = 0;
456     new->not_flags = 0;
457     new->length = ~(bfd_size_type)0;
458     new->current = 0;
459     new->had_full_message = false;
460
461     return new;
462   }
463 }
464
465
466 lang_memory_region_type *
467 lang_memory_default (section)
468      asection *section;
469 {
470   lang_memory_region_type *p;
471
472   flagword sec_flags = section->flags;
473
474   /* Override SEC_DATA to mean a writable section.  */
475   if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
476     sec_flags |= SEC_DATA;
477
478   for (p = lang_memory_region_list;
479        p != (lang_memory_region_type *) NULL;
480        p = p->next)
481     {
482       if ((p->flags & sec_flags) != 0
483           && (p->not_flags & sec_flags) == 0)
484         {
485           return p;
486         }
487     }
488   return lang_memory_region_lookup ("*default*");
489 }
490
491 lang_output_section_statement_type *
492 lang_output_section_find (name)
493      CONST char *CONST name;
494 {
495   lang_statement_union_type *u;
496   lang_output_section_statement_type *lookup;
497
498   for (u = lang_output_section_statement.head;
499        u != (lang_statement_union_type *) NULL;
500        u = lookup->next)
501     {
502       lookup = &u->output_section_statement;
503       if (strcmp (name, lookup->name) == 0)
504         {
505           return lookup;
506         }
507     }
508   return (lang_output_section_statement_type *) NULL;
509 }
510
511 lang_output_section_statement_type *
512 lang_output_section_statement_lookup (name)
513      CONST char *CONST name;
514 {
515   lang_output_section_statement_type *lookup;
516
517   lookup = lang_output_section_find (name);
518   if (lookup == (lang_output_section_statement_type *) NULL)
519     {
520
521       lookup = (lang_output_section_statement_type *)
522         new_stat (lang_output_section_statement, stat_ptr);
523       lookup->region = (lang_memory_region_type *) NULL;
524       lookup->fill = 0;
525       lookup->block_value = 1;
526       lookup->name = name;
527
528       lookup->next = (lang_statement_union_type *) NULL;
529       lookup->bfd_section = (asection *) NULL;
530       lookup->processed = false;
531       lookup->sectype = normal_section;
532       lookup->addr_tree = (etree_type *) NULL;
533       lang_list_init (&lookup->children);
534
535       lookup->memspec = (CONST char *) NULL;
536       lookup->flags = 0;
537       lookup->subsection_alignment = -1;
538       lookup->section_alignment = -1;
539       lookup->load_base = (union etree_union *) NULL;
540       lookup->phdrs = NULL;
541
542       lang_statement_append (&lang_output_section_statement,
543                              (lang_statement_union_type *) lookup,
544                              &lookup->next);
545     }
546   return lookup;
547 }
548
549 static void
550 lang_map_flags (flag)
551      flagword flag;
552 {
553   if (flag & SEC_ALLOC)
554     minfo ("a");
555
556   if (flag & SEC_CODE)
557     minfo ("x");
558
559   if (flag & SEC_READONLY)
560     minfo ("r");
561
562   if (flag & SEC_DATA)
563     minfo ("w");
564
565   if (flag & SEC_LOAD)
566     minfo ("l");
567 }
568
569 void
570 lang_map ()
571 {
572   lang_memory_region_type *m;
573
574   minfo ("\nMemory Configuration\n\n");
575   fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
576            "Name", "Origin", "Length", "Attributes");
577
578   for (m = lang_memory_region_list;
579        m != (lang_memory_region_type *) NULL;
580        m = m->next)
581     {
582       char buf[100];
583       int len;
584
585       fprintf (config.map_file, "%-16s ", m->name);
586
587       sprintf_vma (buf, m->origin);
588       minfo ("0x%s ", buf);
589       len = strlen (buf);
590       while (len < 16)
591         {
592           print_space ();
593           ++len;
594         }
595
596       minfo ("0x%V", m->length);
597       if (m->flags || m->not_flags)
598         {
599 #ifndef BFD64
600           minfo ("        ");
601 #endif
602           if (m->flags)
603             {
604               print_space ();
605               lang_map_flags (m->flags);
606             }
607
608           if (m->not_flags)
609             {
610               minfo (" !");
611               lang_map_flags (m->not_flags);
612             }
613         }
614
615       print_nl ();
616     }
617
618   fprintf (config.map_file, "\nLinker script and memory map\n\n");
619
620   print_statements ();
621 }
622
623 /* Initialize an output section.  */
624
625 static void
626 init_os (s)
627      lang_output_section_statement_type *s;
628 {
629   section_userdata_type *new;
630
631   if (s->bfd_section != NULL)
632     return;
633
634   if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
635     einfo ("%P%F: Illegal use of `%s' section", DISCARD_SECTION_NAME);
636
637   new = ((section_userdata_type *)
638          stat_alloc (sizeof (section_userdata_type)));
639
640   s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
641   if (s->bfd_section == (asection *) NULL)
642     s->bfd_section = bfd_make_section (output_bfd, s->name);
643   if (s->bfd_section == (asection *) NULL)
644     {
645       einfo ("%P%F: output format %s cannot represent section called %s\n",
646              output_bfd->xvec->name, s->name);
647     }
648   s->bfd_section->output_section = s->bfd_section;
649
650   /* We initialize an output sections output offset to minus its own */
651   /* vma to allow us to output a section through itself */
652   s->bfd_section->output_offset = 0;
653   get_userdata (s->bfd_section) = (PTR) new;
654
655   /* If there is a base address, make sure that any sections it might
656      mention are initialized.  */
657   if (s->addr_tree != NULL)
658     exp_init_os (s->addr_tree);
659 }
660
661 /* Make sure that all output sections mentioned in an expression are
662    initialized.  */
663
664 static void
665 exp_init_os (exp)
666      etree_type *exp;
667 {
668   switch (exp->type.node_class)
669     {
670     case etree_assign:
671       exp_init_os (exp->assign.src);
672       break;
673
674     case etree_binary:
675       exp_init_os (exp->binary.lhs);
676       exp_init_os (exp->binary.rhs);
677       break;
678
679     case etree_trinary:
680       exp_init_os (exp->trinary.cond);
681       exp_init_os (exp->trinary.lhs);
682       exp_init_os (exp->trinary.rhs);
683       break;
684
685     case etree_unary:
686       exp_init_os (exp->unary.child);
687       break;
688
689     case etree_name:
690       switch (exp->type.node_code)
691         {
692         case ADDR:
693         case LOADADDR:
694         case SIZEOF:
695           {
696             lang_output_section_statement_type *os;
697
698             os = lang_output_section_find (exp->name.name);
699             if (os != NULL && os->bfd_section == NULL)
700               init_os (os);
701           }
702         }
703       break;
704
705     default:
706       break;
707     }
708 }
709
710 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
711    once into the output.  This routine checks each sections, and
712    arranges to discard it if a section of the same name has already
713    been linked.  This code assumes that all relevant sections have the
714    SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
715    section name.  This is called via bfd_map_over_sections.  */
716
717 /*ARGSUSED*/
718 static void
719 section_already_linked (abfd, sec, data)
720      bfd *abfd;
721      asection *sec;
722      PTR data;
723 {
724   lang_input_statement_type *entry = (lang_input_statement_type *) data;
725   struct sec_link_once
726     {
727       struct sec_link_once *next;
728       asection *sec;
729     };
730   static struct sec_link_once *sec_link_once_list;
731   flagword flags;
732   const char *name;
733   struct sec_link_once *l;
734
735   /* If we are only reading symbols from this object, then we want to
736      discard all sections.  */
737   if (entry->just_syms_flag)
738     {
739       sec->output_section = bfd_abs_section_ptr;
740       sec->output_offset = sec->vma;
741       return;
742     }
743
744   flags = bfd_get_section_flags (abfd, sec);
745
746   if ((flags & SEC_LINK_ONCE) == 0)
747     return;
748
749   name = bfd_get_section_name (abfd, sec);
750
751   for (l = sec_link_once_list; l != NULL; l = l->next)
752     {
753       if (strcmp (name, bfd_get_section_name (l->sec->owner, l->sec)) == 0)
754         {
755           /* The section has already been linked.  See if we should
756              issue a warning.  */
757           switch (flags & SEC_LINK_DUPLICATES)
758             {
759             default:
760               abort ();
761
762             case SEC_LINK_DUPLICATES_DISCARD:
763               break;
764
765             case SEC_LINK_DUPLICATES_ONE_ONLY:
766               einfo ("%P: %B: warning: ignoring duplicate section `%s'\n",
767                      abfd, name);
768               break;
769
770             case SEC_LINK_DUPLICATES_SAME_CONTENTS:
771               /* FIXME: We should really dig out the contents of both
772                  sections and memcmp them.  The COFF/PE spec says that
773                  the Microsoft linker does not implement this
774                  correctly, so I'm not going to bother doing it
775                  either.  */
776               /* Fall through.  */
777             case SEC_LINK_DUPLICATES_SAME_SIZE:
778               if (bfd_section_size (abfd, sec)
779                   != bfd_section_size (l->sec->owner, l->sec))
780                 einfo ("%P: %B: warning: duplicate section `%s' has different size\n",
781                        abfd, name);
782               break;
783             }
784
785           /* Set the output_section field so that wild_doit does not
786              create a lang_input_section structure for this section.  */
787           sec->output_section = bfd_abs_section_ptr;
788
789           return;
790         }
791     }
792
793   /* This is the first section with this name.  Record it.  */
794
795   l = (struct sec_link_once *) xmalloc (sizeof *l);
796   l->sec = sec;
797   l->next = sec_link_once_list;
798   sec_link_once_list = l;
799 }
800 \f
801 /* The wild routines.
802
803    These expand statements like *(.text) and foo.o to a list of
804    explicit actions, like foo.o(.text), bar.o(.text) and
805    foo.o(.text, .data).  */
806
807 /* Return true if the PATTERN argument is a wildcard pattern.
808    Although backslashes are treated specially if a pattern contains
809    wildcards, we do not consider the mere presence of a backslash to
810    be enough to cause the the pattern to be treated as a wildcard.
811    That lets us handle DOS filenames more naturally.  */
812
813 static boolean
814 wildcardp (pattern)
815      const char *pattern;
816 {
817   const char *s;
818
819   for (s = pattern; *s != '\0'; ++s)
820     if (*s == '?'
821         || *s == '*'
822         || *s == '[')
823       return true;
824   return false;
825 }
826
827 /* Add SECTION to the output section OUTPUT.  Do this by creating a
828    lang_input_section statement which is placed at PTR.  FILE is the
829    input file which holds SECTION.  */
830
831 void
832 wild_doit (ptr, section, output, file)
833      lang_statement_list_type *ptr;
834      asection *section;
835      lang_output_section_statement_type *output;
836      lang_input_statement_type *file;
837 {
838   flagword flags;
839   boolean discard;
840
841   flags = bfd_get_section_flags (section->owner, section);
842
843   discard = false;
844
845   /* If we are doing a final link, discard sections marked with
846      SEC_EXCLUDE.  */
847   if (! link_info.relocateable
848       && (flags & SEC_EXCLUDE) != 0)
849     discard = true;
850
851   /* Discard input sections which are assigned to a section named
852      DISCARD_SECTION_NAME.  */
853   if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
854     discard = true;
855
856   /* Discard debugging sections if we are stripping debugging
857      information.  */
858   if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
859       && (flags & SEC_DEBUGGING) != 0)
860     discard = true;
861
862   if (discard)
863     {
864       if (section->output_section == NULL)
865         {
866           /* This prevents future calls from assigning this section.  */
867           section->output_section = bfd_abs_section_ptr;
868         }
869       return;
870     }
871
872   if (section->output_section == NULL)
873     {
874       boolean first;
875       lang_input_section_type *new;
876       flagword flags;
877
878       if (output->bfd_section == NULL)
879         {
880           init_os (output);
881           first = true;
882         }
883       else
884         first = false;
885
886       /* Add a section reference to the list */
887       new = new_stat (lang_input_section, ptr);
888
889       new->section = section;
890       new->ifile = file;
891       section->output_section = output->bfd_section;
892
893       flags = section->flags;
894
895       /* We don't copy the SEC_NEVER_LOAD flag from an input section
896          to an output section, because we want to be able to include a
897          SEC_NEVER_LOAD section in the middle of an otherwise loaded
898          section (I don't know why we want to do this, but we do).
899          build_link_order in ldwrite.c handles this case by turning
900          the embedded SEC_NEVER_LOAD section into a fill.  */
901
902       flags &= ~ SEC_NEVER_LOAD;
903
904       /* If final link, don't copy the SEC_LINK_ONCE flags, they've
905          already been processed.  One reason to do this is that on pe
906          format targets, .text$foo sections go into .text and it's odd
907          to see .text with SEC_LINK_ONCE set.  */
908
909       if (! link_info.relocateable)
910         flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
911
912       /* If this is not the first input section, and the SEC_READONLY
913          flag is not currently set, then don't set it just because the
914          input section has it set.  */
915
916       if (! first && (section->output_section->flags & SEC_READONLY) == 0)
917         flags &= ~ SEC_READONLY;
918
919       section->output_section->flags |= flags;
920
921       /* If SEC_READONLY is not set in the input section, then clear
922          it from the output section.  */
923       if ((section->flags & SEC_READONLY) == 0)
924         section->output_section->flags &= ~SEC_READONLY;
925
926       switch (output->sectype)
927         {
928         case normal_section:
929           break;
930         case dsect_section:
931         case copy_section:
932         case info_section:
933         case overlay_section:
934           output->bfd_section->flags &= ~SEC_ALLOC;
935           break;
936         case noload_section:
937           output->bfd_section->flags &= ~SEC_LOAD;
938           output->bfd_section->flags |= SEC_NEVER_LOAD;
939           break;
940         }
941
942       if (section->alignment_power > output->bfd_section->alignment_power)
943         output->bfd_section->alignment_power = section->alignment_power;
944
945       /* If supplied an aligment, then force it.  */
946       if (output->section_alignment != -1)
947         output->bfd_section->alignment_power = output->section_alignment;
948     }
949 }
950
951 /* Expand a wild statement for a particular FILE.  SECTION may be
952    NULL, in which case it is a wild card.  */
953
954 static void
955 wild_section (ptr, section, file, output)
956      lang_wild_statement_type *ptr;
957      const char *section;
958      lang_input_statement_type *file;
959      lang_output_section_statement_type *output;
960 {
961   if (file->just_syms_flag == false)
962     {
963       register asection *s;
964       boolean wildcard;
965
966       if (section == NULL)
967         wildcard = false;
968       else
969         wildcard = wildcardp (section);
970
971       for (s = file->the_bfd->sections; s != NULL; s = s->next)
972         {
973           boolean match;
974
975           /* Attach all sections named SECTION.  If SECTION is NULL,
976              then attach all sections.
977
978              Previously, if SECTION was NULL, this code did not call
979              wild_doit if the SEC_IS_COMMON flag was set for the
980              section.  I did not understand that, and I took it out.
981              --ian@cygnus.com.  */
982
983           if (section == NULL)
984             match = true;
985           else
986             {
987               const char *name;
988
989               name = bfd_get_section_name (file->the_bfd, s);
990               if (wildcard)
991                 match = fnmatch (section, name, 0) == 0 ? true : false;
992               else
993                 match = strcmp (section, name) == 0 ? true : false;
994             }
995           if (match)
996             wild_doit (&ptr->children, s, output, file);
997         }
998     }
999 }
1000
1001 /* This is passed a file name which must have been seen already and
1002    added to the statement tree.  We will see if it has been opened
1003    already and had its symbols read.  If not then we'll read it.  */
1004
1005 static lang_input_statement_type *
1006 lookup_name (name)
1007      const char *name;
1008 {
1009   lang_input_statement_type *search;
1010
1011   for (search = (lang_input_statement_type *) input_file_chain.head;
1012        search != (lang_input_statement_type *) NULL;
1013        search = (lang_input_statement_type *) search->next_real_file)
1014     {
1015       if (search->filename == (char *) NULL && name == (char *) NULL)
1016         return search;
1017       if (search->filename != (char *) NULL
1018           && name != (char *) NULL
1019           && strcmp (search->filename, name) == 0)
1020         break;
1021     }
1022
1023   if (search == (lang_input_statement_type *) NULL)
1024     search = new_afile (name, lang_input_file_is_file_enum, default_target,
1025                         false);
1026
1027   /* If we have already added this file, or this file is not real
1028      (FIXME: can that ever actually happen?) or the name is NULL
1029      (FIXME: can that ever actually happen?) don't add this file.  */
1030   if (search->loaded
1031       || ! search->real
1032       || search->filename == (const char *) NULL)
1033     return search;
1034
1035   load_symbols (search, (lang_statement_list_type *) NULL);
1036
1037   return search;
1038 }
1039
1040 /* Get the symbols for an input file.  */
1041
1042 static void
1043 load_symbols (entry, place)
1044      lang_input_statement_type *entry;
1045      lang_statement_list_type *place;
1046 {
1047   char **matching;
1048
1049   if (entry->loaded)
1050     return;
1051
1052   ldfile_open_file (entry);
1053
1054   if (! bfd_check_format (entry->the_bfd, bfd_archive)
1055       && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1056     {
1057       bfd_error_type err;
1058       lang_statement_list_type *hold;
1059
1060       err = bfd_get_error ();
1061       if (err == bfd_error_file_ambiguously_recognized)
1062         {
1063           char **p;
1064
1065           einfo ("%B: file not recognized: %E\n", entry->the_bfd);
1066           einfo ("%B: matching formats:", entry->the_bfd);
1067           for (p = matching; *p != NULL; p++)
1068             einfo (" %s", *p);
1069           einfo ("%F\n");
1070         }
1071       else if (err != bfd_error_file_not_recognized
1072                || place == NULL)
1073         einfo ("%F%B: file not recognized: %E\n", entry->the_bfd);
1074
1075       bfd_close (entry->the_bfd);
1076       entry->the_bfd = NULL;
1077
1078       /* See if the emulation has some special knowledge.  */
1079
1080       if (ldemul_unrecognized_file (entry))
1081         return;
1082
1083       /* Try to interpret the file as a linker script.  */
1084
1085       ldfile_open_command_file (entry->filename);
1086
1087       hold = stat_ptr;
1088       stat_ptr = place;
1089
1090       ldfile_assumed_script = true;
1091       parser_input = input_script;
1092       yyparse ();
1093       ldfile_assumed_script = false;
1094
1095       stat_ptr = hold;
1096
1097       return;
1098     }
1099
1100   /* We don't call ldlang_add_file for an archive.  Instead, the
1101      add_symbols entry point will call ldlang_add_file, via the
1102      add_archive_element callback, for each element of the archive
1103      which is used.  */
1104   switch (bfd_get_format (entry->the_bfd))
1105     {
1106     default:
1107       break;
1108
1109     case bfd_object:
1110       ldlang_add_file (entry);
1111       if (trace_files || trace_file_tries)
1112         info_msg ("%I\n", entry);
1113       break;
1114
1115     case bfd_archive:
1116       if (entry->whole_archive)
1117         {
1118           bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
1119                                                       (bfd *) NULL);
1120           while (member != NULL)
1121             {
1122               if (! bfd_check_format (member, bfd_object))
1123                 einfo ("%F%B: object %B in archive is not object\n",
1124                        entry->the_bfd, member);
1125               if (! ((*link_info.callbacks->add_archive_element)
1126                      (&link_info, member, "--whole-archive")))
1127                 abort ();
1128               if (! bfd_link_add_symbols (member, &link_info))
1129                 einfo ("%F%B: could not read symbols: %E\n", member);
1130               member = bfd_openr_next_archived_file (entry->the_bfd,
1131                                                      member);
1132             }
1133
1134           entry->loaded = true;
1135
1136           return;
1137         }
1138     }
1139
1140   if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
1141     einfo ("%F%B: could not read symbols: %E\n", entry->the_bfd);
1142
1143   entry->loaded = true;
1144 }
1145
1146 /* Handle a wild statement for a single file F.  */
1147
1148 static void
1149 wild_file (s, section, f, output)
1150      lang_wild_statement_type *s;
1151      const char *section;
1152      lang_input_statement_type *f;
1153      lang_output_section_statement_type *output;
1154 {
1155   if (f->the_bfd == NULL
1156       || ! bfd_check_format (f->the_bfd, bfd_archive))
1157     wild_section (s, section, f, output);
1158   else
1159     {
1160       bfd *member;
1161
1162       /* This is an archive file.  We must map each member of the
1163          archive separately.  */
1164       member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
1165       while (member != NULL)
1166         {
1167           /* When lookup_name is called, it will call the add_symbols
1168              entry point for the archive.  For each element of the
1169              archive which is included, BFD will call ldlang_add_file,
1170              which will set the usrdata field of the member to the
1171              lang_input_statement.  */
1172           if (member->usrdata != NULL)
1173             {
1174               wild_section (s, section,
1175                             (lang_input_statement_type *) member->usrdata,
1176                             output);
1177             }
1178
1179           member = bfd_openr_next_archived_file (f->the_bfd, member);
1180         }
1181     }
1182 }
1183
1184 /* Handle a wild statement.  SECTION or FILE or both may be NULL,
1185    indicating that it is a wildcard.  Separate lang_input_section
1186    statements are created for each part of the expansion; they are
1187    added after the wild statement S.  OUTPUT is the output section.  */
1188
1189 static void
1190 wild (s, section, file, target, output)
1191      lang_wild_statement_type *s;
1192      const char *section;
1193      const char *file;
1194      const char *target;
1195      lang_output_section_statement_type *output;
1196 {
1197   lang_input_statement_type *f;
1198
1199   if (file == (char *) NULL)
1200     {
1201       /* Perform the iteration over all files in the list */
1202       for (f = (lang_input_statement_type *) file_chain.head;
1203            f != (lang_input_statement_type *) NULL;
1204            f = (lang_input_statement_type *) f->next)
1205         {
1206           wild_file (s, section, f, output);
1207         }
1208     }
1209   else if (wildcardp (file))
1210     {
1211       for (f = (lang_input_statement_type *) file_chain.head;
1212            f != (lang_input_statement_type *) NULL;
1213            f = (lang_input_statement_type *) f->next)
1214         {
1215           if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
1216             wild_file (s, section, f, output);
1217         }
1218     }
1219   else
1220     {
1221       /* Perform the iteration over a single file */
1222       f = lookup_name (file);
1223       wild_file (s, section, f, output);
1224     }
1225
1226   if (section != (char *) NULL
1227       && strcmp (section, "COMMON") == 0
1228       && default_common_section == NULL)
1229     {
1230       /* Remember the section that common is going to in case we later
1231          get something which doesn't know where to put it.  */
1232       default_common_section = output;
1233     }
1234 }
1235
1236 /* Open the output file.  */
1237
1238 static bfd *
1239 open_output (name)
1240      const char *name;
1241 {
1242   bfd *output;
1243
1244   if (output_target == (char *) NULL)
1245     {
1246       if (current_target != (char *) NULL)
1247         output_target = current_target;
1248       else
1249         output_target = default_target;
1250     }
1251   output = bfd_openw (name, output_target);
1252
1253   if (output == (bfd *) NULL)
1254     {
1255       if (bfd_get_error () == bfd_error_invalid_target)
1256         {
1257           einfo ("%P%F: target %s not found\n", output_target);
1258         }
1259       einfo ("%P%F: cannot open output file %s: %E\n", name);
1260     }
1261
1262   delete_output_file_on_failure = true;
1263
1264   /*  output->flags |= D_PAGED;*/
1265
1266   if (! bfd_set_format (output, bfd_object))
1267     einfo ("%P%F:%s: can not make object file: %E\n", name);
1268   if (! bfd_set_arch_mach (output,
1269                            ldfile_output_architecture,
1270                            ldfile_output_machine))
1271     einfo ("%P%F:%s: can not set architecture: %E\n", name);
1272
1273   link_info.hash = bfd_link_hash_table_create (output);
1274   if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1275     einfo ("%P%F: can not create link hash table: %E\n");
1276
1277   bfd_set_gp_size (output, g_switch_value);
1278   return output;
1279 }
1280
1281
1282
1283
1284 static void
1285 ldlang_open_output (statement)
1286      lang_statement_union_type * statement;
1287 {
1288   switch (statement->header.type)
1289     {
1290     case lang_output_statement_enum:
1291       ASSERT (output_bfd == (bfd *) NULL);
1292       output_bfd = open_output (statement->output_statement.name);
1293       ldemul_set_output_arch ();
1294       if (config.magic_demand_paged && !link_info.relocateable)
1295         output_bfd->flags |= D_PAGED;
1296       else
1297         output_bfd->flags &= ~D_PAGED;
1298       if (config.text_read_only)
1299         output_bfd->flags |= WP_TEXT;
1300       else
1301         output_bfd->flags &= ~WP_TEXT;
1302       if (link_info.traditional_format)
1303         output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1304       else
1305         output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1306       break;
1307
1308     case lang_target_statement_enum:
1309       current_target = statement->target_statement.target;
1310       break;
1311     default:
1312       break;
1313     }
1314 }
1315
1316 /* Open all the input files.  */
1317
1318 static void
1319 open_input_bfds (s, force)
1320      lang_statement_union_type *s;
1321      boolean force;
1322 {
1323   for (; s != (lang_statement_union_type *) NULL; s = s->next)
1324     {
1325       switch (s->header.type)
1326         {
1327         case lang_constructors_statement_enum:
1328           open_input_bfds (constructor_list.head, force);
1329           break;
1330         case lang_output_section_statement_enum:
1331           open_input_bfds (s->output_section_statement.children.head, force);
1332           break;
1333         case lang_wild_statement_enum:
1334           /* Maybe we should load the file's symbols */
1335           if (s->wild_statement.filename
1336               && ! wildcardp (s->wild_statement.filename))
1337             (void) lookup_name (s->wild_statement.filename);
1338           open_input_bfds (s->wild_statement.children.head, force);
1339           break;
1340         case lang_group_statement_enum:
1341           {
1342             struct bfd_link_hash_entry *undefs;
1343
1344             /* We must continually search the entries in the group
1345                until no new symbols are added to the list of undefined
1346                symbols.  */
1347
1348             do
1349               {
1350                 undefs = link_info.hash->undefs_tail;
1351                 open_input_bfds (s->group_statement.children.head, true);
1352               }
1353             while (undefs != link_info.hash->undefs_tail);
1354           }
1355           break;
1356         case lang_target_statement_enum:
1357           current_target = s->target_statement.target;
1358           break;
1359         case lang_input_statement_enum:
1360           if (s->input_statement.real == true)
1361             {
1362               lang_statement_list_type add;
1363
1364               s->input_statement.target = current_target;
1365
1366               /* If we are being called from within a group, and this
1367                  is an archive which has already been searched, then
1368                  force it to be researched.  */
1369               if (force
1370                   && s->input_statement.loaded
1371                   && bfd_check_format (s->input_statement.the_bfd,
1372                                        bfd_archive))
1373                 s->input_statement.loaded = false;
1374
1375               lang_list_init (&add);
1376
1377               load_symbols (&s->input_statement, &add);
1378
1379               if (add.head != NULL)
1380                 {
1381                   *add.tail = s->next;
1382                   s->next = add.head;
1383                 }
1384             }
1385           break;
1386         default:
1387           break;
1388         }
1389     }
1390 }
1391
1392 /* If there are [COMMONS] statements, put a wild one into the bss section */
1393
1394 static void
1395 lang_reasonable_defaults ()
1396 {
1397 #if 0
1398   lang_output_section_statement_lookup (".text");
1399   lang_output_section_statement_lookup (".data");
1400
1401   default_common_section =
1402     lang_output_section_statement_lookup (".bss");
1403
1404
1405   if (placed_commons == false)
1406     {
1407       lang_wild_statement_type *new =
1408       new_stat (lang_wild_statement,
1409                 &default_common_section->children);
1410
1411       new->section_name = "COMMON";
1412       new->filename = (char *) NULL;
1413       lang_list_init (&new->children);
1414     }
1415 #endif
1416
1417 }
1418
1419 /*
1420  Add the supplied name to the symbol table as an undefined reference.
1421  Remove items from the chain as we open input bfds
1422  */
1423 typedef struct ldlang_undef_chain_list
1424 {
1425   struct ldlang_undef_chain_list *next;
1426   char *name;
1427 }                       ldlang_undef_chain_list_type;
1428
1429 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1430
1431 void
1432 ldlang_add_undef (name)
1433      CONST char *CONST name;
1434 {
1435   ldlang_undef_chain_list_type *new =
1436     ((ldlang_undef_chain_list_type *)
1437      stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1438
1439   new->next = ldlang_undef_chain_list_head;
1440   ldlang_undef_chain_list_head = new;
1441
1442   new->name = buystring (name);
1443 }
1444
1445 /* Run through the list of undefineds created above and place them
1446    into the linker hash table as undefined symbols belonging to the
1447    script file.
1448 */
1449 static void
1450 lang_place_undefineds ()
1451 {
1452   ldlang_undef_chain_list_type *ptr;
1453
1454   for (ptr = ldlang_undef_chain_list_head;
1455        ptr != (ldlang_undef_chain_list_type *) NULL;
1456        ptr = ptr->next)
1457     {
1458       struct bfd_link_hash_entry *h;
1459
1460       h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1461       if (h == (struct bfd_link_hash_entry *) NULL)
1462         einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
1463       if (h->type == bfd_link_hash_new)
1464         {
1465           h->type = bfd_link_hash_undefined;
1466           h->u.undef.abfd = NULL;
1467           bfd_link_add_undef (link_info.hash, h);
1468         }
1469     }
1470 }
1471
1472 /* Open input files and attatch to output sections */
1473 static void
1474 map_input_to_output_sections (s, target, output_section_statement)
1475      lang_statement_union_type * s;
1476      CONST char *target;
1477      lang_output_section_statement_type * output_section_statement;
1478 {
1479   for (; s != (lang_statement_union_type *) NULL; s = s->next)
1480     {
1481       switch (s->header.type)
1482         {
1483
1484
1485         case lang_wild_statement_enum:
1486           wild (&s->wild_statement, s->wild_statement.section_name,
1487                 s->wild_statement.filename, target,
1488                 output_section_statement);
1489
1490           break;
1491         case lang_constructors_statement_enum:
1492           map_input_to_output_sections (constructor_list.head,
1493                                         target,
1494                                         output_section_statement);
1495           break;
1496         case lang_output_section_statement_enum:
1497           map_input_to_output_sections (s->output_section_statement.children.head,
1498                                         target,
1499                                         &s->output_section_statement);
1500           break;
1501         case lang_output_statement_enum:
1502           break;
1503         case lang_target_statement_enum:
1504           target = s->target_statement.target;
1505           break;
1506         case lang_group_statement_enum:
1507           map_input_to_output_sections (s->group_statement.children.head,
1508                                         target,
1509                                         output_section_statement);
1510           break;
1511         case lang_fill_statement_enum:
1512         case lang_input_section_enum:
1513         case lang_object_symbols_statement_enum:
1514         case lang_data_statement_enum:
1515         case lang_reloc_statement_enum:
1516         case lang_padding_statement_enum:
1517         case lang_input_statement_enum:
1518           if (output_section_statement != NULL
1519               && output_section_statement->bfd_section == NULL)
1520             init_os (output_section_statement);
1521           break;
1522         case lang_assignment_statement_enum:
1523           if (output_section_statement != NULL
1524               && output_section_statement->bfd_section == NULL)
1525             init_os (output_section_statement);
1526
1527           /* Make sure that any sections mentioned in the assignment
1528              are initialized.  */
1529           exp_init_os (s->assignment_statement.exp);
1530           break;
1531         case lang_afile_asection_pair_statement_enum:
1532           FAIL ();
1533           break;
1534         case lang_address_statement_enum:
1535           /* Mark the specified section with the supplied address */
1536           {
1537             lang_output_section_statement_type *os =
1538               lang_output_section_statement_lookup
1539                 (s->address_statement.section_name);
1540
1541             if (os->bfd_section == NULL)
1542               init_os (os);
1543             os->addr_tree = s->address_statement.address;
1544           }
1545           break;
1546         }
1547     }
1548 }
1549
1550 static void
1551 print_output_section_statement (output_section_statement)
1552      lang_output_section_statement_type * output_section_statement;
1553 {
1554   asection *section = output_section_statement->bfd_section;
1555   int len;
1556
1557   if (output_section_statement != abs_output_section)
1558     {
1559       minfo ("\n%s", output_section_statement->name);
1560
1561       if (section != NULL)
1562         {
1563           print_dot = section->vma;
1564
1565           len = strlen (output_section_statement->name);
1566           if (len >= SECTION_NAME_MAP_LENGTH - 1)
1567             {
1568               print_nl ();
1569               len = 0;
1570             }
1571           while (len < SECTION_NAME_MAP_LENGTH)
1572             {
1573               print_space ();
1574               ++len;
1575             }
1576
1577           minfo ("0x%V %W", section->vma, section->_raw_size);
1578
1579           if (output_section_statement->load_base != NULL)
1580             {
1581               bfd_vma addr;
1582
1583               addr = exp_get_abs_int (output_section_statement->load_base, 0,
1584                                       "load base", lang_final_phase_enum);
1585               minfo (" load address 0x%V", addr);
1586             }
1587         }
1588
1589       print_nl ();
1590     }
1591
1592   print_statement_list (output_section_statement->children.head,
1593                         output_section_statement);
1594 }
1595
1596 static void
1597 print_assignment (assignment, output_section)
1598      lang_assignment_statement_type * assignment;
1599      lang_output_section_statement_type * output_section;
1600 {
1601   int i;
1602   etree_value_type result;
1603
1604   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1605     print_space ();
1606
1607   result = exp_fold_tree (assignment->exp->assign.src, output_section,
1608                           lang_final_phase_enum, print_dot, &print_dot);
1609   if (result.valid)
1610     minfo ("0x%V", result.value + result.section->bfd_section->vma);
1611   else
1612     {
1613       minfo ("*undef*   ");
1614 #ifdef BFD64
1615       minfo ("        ");
1616 #endif
1617     }
1618
1619   minfo ("                ");
1620
1621   exp_print_tree (assignment->exp);
1622
1623   print_nl ();
1624 }
1625
1626 static void
1627 print_input_statement (statm)
1628      lang_input_statement_type * statm;
1629 {
1630   if (statm->filename != (char *) NULL)
1631     {
1632       fprintf (config.map_file, "LOAD %s\n", statm->filename);
1633     }
1634 }
1635
1636 /* Print all symbols defined in a particular section.  This is called
1637    via bfd_link_hash_traverse.  */
1638
1639 static boolean 
1640 print_one_symbol (hash_entry, ptr)
1641      struct bfd_link_hash_entry *hash_entry;
1642      PTR ptr;
1643 {
1644   asection *sec = (asection *) ptr;
1645
1646   if ((hash_entry->type == bfd_link_hash_defined
1647        || hash_entry->type == bfd_link_hash_defweak)
1648       && sec == hash_entry->u.def.section)
1649     {
1650       int i;
1651
1652       for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1653         print_space ();
1654       minfo ("0x%V   ",
1655              (hash_entry->u.def.value
1656               + hash_entry->u.def.section->output_offset
1657               + hash_entry->u.def.section->output_section->vma));
1658
1659       minfo ("             %T\n", hash_entry->root.string);
1660     }
1661
1662   return true;
1663 }
1664
1665 /* Print information about an input section to the map file.  */
1666
1667 static void
1668 print_input_section (in)
1669      lang_input_section_type * in;
1670 {
1671   asection *i = in->section;
1672   bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1673
1674   if (size != 0)
1675     {
1676       print_space ();
1677
1678       minfo ("%s", i->name);
1679
1680       if (i->output_section != NULL)
1681         {
1682           int len;
1683
1684           len = 1 + strlen (i->name);
1685           if (len >= SECTION_NAME_MAP_LENGTH - 1)
1686             {
1687               print_nl ();
1688               len = 0;
1689             }
1690           while (len < SECTION_NAME_MAP_LENGTH)
1691             {
1692               print_space ();
1693               ++len;
1694             }
1695
1696           minfo ("0x%V %W %B\n",
1697                  i->output_section->vma + i->output_offset, size,
1698                  i->owner);
1699
1700           if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
1701             {
1702               len = SECTION_NAME_MAP_LENGTH + 3;
1703 #ifdef BFD64
1704               len += 16;
1705 #else
1706               len += 8;
1707 #endif
1708               while (len > 0)
1709                 {
1710                   print_space ();
1711                   --len;
1712                 }
1713
1714               minfo ("%W (size before relaxing)\n", i->_raw_size);
1715             }
1716
1717           bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1718
1719           print_dot = i->output_section->vma + i->output_offset + size;
1720         }
1721     }
1722 }
1723
1724 static void
1725 print_fill_statement (fill)
1726      lang_fill_statement_type * fill;
1727 {
1728   fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
1729 }
1730
1731 static void
1732 print_data_statement (data)
1733      lang_data_statement_type * data;
1734 {
1735   int i;
1736   bfd_vma addr;
1737   bfd_size_type size;
1738   const char *name;
1739
1740   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1741     print_space ();
1742
1743   addr = data->output_vma;
1744   if (data->output_section != NULL)
1745     addr += data->output_section->vma;
1746
1747   switch (data->type)
1748     {
1749     default:
1750       abort ();
1751     case BYTE:
1752       size = BYTE_SIZE;
1753       name = "BYTE";
1754       break;
1755     case SHORT:
1756       size = SHORT_SIZE;
1757       name = "SHORT";
1758       break;
1759     case LONG:
1760       size = LONG_SIZE;
1761       name = "LONG";
1762       break;
1763     case QUAD:
1764       size = QUAD_SIZE;
1765       name = "QUAD";
1766       break;
1767     case SQUAD:
1768       size = QUAD_SIZE;
1769       name = "SQUAD";
1770       break;
1771     }
1772
1773   minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
1774
1775   if (data->exp->type.node_class != etree_value)
1776     {
1777       print_space ();
1778       exp_print_tree (data->exp);
1779     }
1780
1781   print_nl ();
1782
1783   print_dot = addr + size;
1784 }
1785
1786 /* Print an address statement.  These are generated by options like
1787    -Ttext.  */
1788
1789 static void
1790 print_address_statement (address)
1791      lang_address_statement_type *address;
1792 {
1793   minfo ("Address of section %s set to ", address->section_name);
1794   exp_print_tree (address->address);
1795   print_nl ();
1796 }
1797
1798 /* Print a reloc statement.  */
1799
1800 static void
1801 print_reloc_statement (reloc)
1802      lang_reloc_statement_type *reloc;
1803 {
1804   int i;
1805   bfd_vma addr;
1806   bfd_size_type size;
1807
1808   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1809     print_space ();
1810
1811   addr = reloc->output_vma;
1812   if (reloc->output_section != NULL)
1813     addr += reloc->output_section->vma;
1814
1815   size = bfd_get_reloc_size (reloc->howto);
1816
1817   minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
1818
1819   if (reloc->name != NULL)
1820     minfo ("%s+", reloc->name);
1821   else
1822     minfo ("%s+", reloc->section->name);
1823
1824   exp_print_tree (reloc->addend_exp);
1825
1826   print_nl ();
1827
1828   print_dot = addr + size;
1829 }  
1830
1831 static void
1832 print_padding_statement (s)
1833      lang_padding_statement_type *s;
1834 {
1835   int len;
1836   bfd_vma addr;
1837
1838   minfo (" *fill*");
1839
1840   len = sizeof " *fill*" - 1;
1841   while (len < SECTION_NAME_MAP_LENGTH)
1842     {
1843       print_space ();
1844       ++len;
1845     }
1846
1847   addr = s->output_offset;
1848   if (s->output_section != NULL)
1849     addr += s->output_section->vma;
1850   minfo ("0x%V %W", addr, s->size);
1851
1852   if (s->fill != 0)
1853     minfo (" %u", s->fill);
1854
1855   print_nl ();
1856
1857   print_dot = addr + s->size;
1858 }
1859
1860 static void
1861 print_wild_statement (w, os)
1862      lang_wild_statement_type * w;
1863      lang_output_section_statement_type * os;
1864 {
1865   print_space ();
1866
1867   if (w->filename != NULL)
1868     minfo ("%s", w->filename);
1869   else
1870     minfo ("*");
1871
1872   if (w->section_name != NULL)
1873     minfo ("(%s)", w->section_name);
1874   else
1875     minfo ("(*)");
1876
1877   print_nl ();
1878
1879   print_statement_list (w->children.head, os);
1880 }
1881
1882 /* Print a group statement.  */
1883
1884 static void
1885 print_group (s, os)
1886      lang_group_statement_type *s;
1887      lang_output_section_statement_type *os;
1888 {
1889   fprintf (config.map_file, "START GROUP\n");
1890   print_statement_list (s->children.head, os);
1891   fprintf (config.map_file, "END GROUP\n");
1892 }
1893
1894 /* Print the list of statements in S.
1895    This can be called for any statement type.  */
1896
1897 static void
1898 print_statement_list (s, os)
1899      lang_statement_union_type *s;
1900      lang_output_section_statement_type *os;
1901 {
1902   while (s != NULL)
1903     {
1904       print_statement (s, os);
1905       s = s->next;
1906     }
1907 }
1908
1909 /* Print the first statement in statement list S.
1910    This can be called for any statement type.  */
1911
1912 static void
1913 print_statement (s, os)
1914      lang_statement_union_type *s;
1915      lang_output_section_statement_type *os;
1916 {
1917   switch (s->header.type)
1918     {
1919     default:
1920       fprintf (config.map_file, "Fail with %d\n", s->header.type);
1921       FAIL ();
1922       break;
1923     case lang_constructors_statement_enum:
1924       if (constructor_list.head != NULL)
1925         {
1926           minfo (" CONSTRUCTORS\n");
1927           print_statement_list (constructor_list.head, os);
1928         }
1929       break;
1930     case lang_wild_statement_enum:
1931       print_wild_statement (&s->wild_statement, os);
1932       break;
1933     case lang_address_statement_enum:
1934       print_address_statement (&s->address_statement);
1935       break;
1936     case lang_object_symbols_statement_enum:
1937       minfo (" CREATE_OBJECT_SYMBOLS\n");
1938       break;
1939     case lang_fill_statement_enum:
1940       print_fill_statement (&s->fill_statement);
1941       break;
1942     case lang_data_statement_enum:
1943       print_data_statement (&s->data_statement);
1944       break;
1945     case lang_reloc_statement_enum:
1946       print_reloc_statement (&s->reloc_statement);
1947       break;
1948     case lang_input_section_enum:
1949       print_input_section (&s->input_section);
1950       break;
1951     case lang_padding_statement_enum:
1952       print_padding_statement (&s->padding_statement);
1953       break;
1954     case lang_output_section_statement_enum:
1955       print_output_section_statement (&s->output_section_statement);
1956       break;
1957     case lang_assignment_statement_enum:
1958       print_assignment (&s->assignment_statement, os);
1959       break;
1960     case lang_target_statement_enum:
1961       fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
1962       break;
1963     case lang_output_statement_enum:
1964       minfo ("OUTPUT(%s", s->output_statement.name);
1965       if (output_target != NULL)
1966         minfo (" %s", output_target);
1967       minfo (")\n");
1968       break;
1969     case lang_input_statement_enum:
1970       print_input_statement (&s->input_statement);
1971       break;
1972     case lang_group_statement_enum:
1973       print_group (&s->group_statement, os);
1974       break;
1975     case lang_afile_asection_pair_statement_enum:
1976       FAIL ();
1977       break;
1978     }
1979 }
1980
1981 static void
1982 print_statements ()
1983 {
1984   print_statement_list (statement_list.head, abs_output_section);
1985 }
1986
1987 /* Print the first N statements in statement list S to STDERR.
1988    If N == 0, nothing is printed.
1989    If N < 0, the entire list is printed.
1990    Intended to be called from GDB.  */
1991
1992 void
1993 dprint_statement (s, n)
1994      lang_statement_union_type * s;
1995      int n;
1996 {
1997   FILE *map_save = config.map_file;
1998
1999   config.map_file = stderr;
2000
2001   if (n < 0)
2002     print_statement_list (s, abs_output_section);
2003   else
2004     {
2005       while (s && --n >= 0)
2006         {
2007           print_statement (s, abs_output_section);
2008           s = s->next;
2009         }
2010     }
2011
2012   config.map_file = map_save;
2013 }
2014
2015 static bfd_vma
2016 insert_pad (this_ptr, fill, power, output_section_statement, dot)
2017      lang_statement_union_type ** this_ptr;
2018      fill_type fill;
2019      unsigned int power;
2020      asection * output_section_statement;
2021      bfd_vma dot;
2022 {
2023   /* Align this section first to the
2024      input sections requirement, then
2025      to the output section's requirement.
2026      If this alignment is > than any seen before,
2027      then record it too. Perform the alignment by
2028      inserting a magic 'padding' statement.
2029      */
2030
2031   unsigned int alignment_needed = align_power (dot, power) - dot;
2032
2033   if (alignment_needed != 0)
2034     {
2035       lang_statement_union_type *new =
2036         ((lang_statement_union_type *)
2037          stat_alloc (sizeof (lang_padding_statement_type)));
2038
2039       /* Link into existing chain */
2040       new->header.next = *this_ptr;
2041       *this_ptr = new;
2042       new->header.type = lang_padding_statement_enum;
2043       new->padding_statement.output_section = output_section_statement;
2044       new->padding_statement.output_offset =
2045         dot - output_section_statement->vma;
2046       new->padding_statement.fill = fill;
2047       new->padding_statement.size = alignment_needed;
2048     }
2049
2050
2051   /* Remember the most restrictive alignment */
2052   if (power > output_section_statement->alignment_power)
2053     {
2054       output_section_statement->alignment_power = power;
2055     }
2056   output_section_statement->_raw_size += alignment_needed;
2057   return alignment_needed + dot;
2058
2059 }
2060
2061 /* Work out how much this section will move the dot point */
2062 static bfd_vma
2063 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
2064      lang_statement_union_type ** this_ptr;
2065      lang_output_section_statement_type * output_section_statement;
2066      fill_type fill;
2067      bfd_vma dot;
2068      boolean relax;
2069 {
2070   lang_input_section_type *is = &((*this_ptr)->input_section);
2071   asection *i = is->section;
2072
2073   if (is->ifile->just_syms_flag == false)
2074     {
2075       if (output_section_statement->subsection_alignment != -1)
2076        i->alignment_power =
2077         output_section_statement->subsection_alignment;
2078
2079       dot = insert_pad (this_ptr, fill, i->alignment_power,
2080                         output_section_statement->bfd_section, dot);
2081
2082       /* Remember where in the output section this input section goes */
2083
2084       i->output_offset = dot - output_section_statement->bfd_section->vma;
2085
2086       /* Mark how big the output section must be to contain this now
2087          */
2088       if (i->_cooked_size != 0)
2089         dot += i->_cooked_size;
2090       else
2091         dot += i->_raw_size;
2092       output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
2093     }
2094   else
2095     {
2096       i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2097     }
2098
2099   return dot;
2100 }
2101
2102 /* This variable indicates whether bfd_relax_section should be called
2103    again.  */
2104
2105 static boolean relax_again;
2106
2107 /* Set the sizes for all the output sections.  */
2108
2109 bfd_vma
2110 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2111      lang_statement_union_type * s;
2112      lang_output_section_statement_type * output_section_statement;
2113      lang_statement_union_type ** prev;
2114      fill_type fill;
2115      bfd_vma dot;
2116      boolean relax;
2117 {
2118   /* Size up the sections from their constituent parts */
2119   for (; s != (lang_statement_union_type *) NULL; s = s->next)
2120   {
2121     switch (s->header.type)
2122     {
2123
2124      case lang_output_section_statement_enum:
2125      {
2126        bfd_vma after;
2127        lang_output_section_statement_type *os = &s->output_section_statement;
2128
2129        if (os->bfd_section == NULL)
2130          {
2131            /* This section was never actually created.  */
2132            break;
2133          }
2134
2135        /* If this is a COFF shared library section, use the size and
2136           address from the input section.  FIXME: This is COFF
2137           specific; it would be cleaner if there were some other way
2138           to do this, but nothing simple comes to mind.  */
2139        if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2140          {
2141            asection *input;
2142
2143            if (os->children.head == NULL
2144                || os->children.head->next != NULL
2145                || os->children.head->header.type != lang_input_section_enum)
2146              einfo ("%P%X: Internal error on COFF shared library section %s\n",
2147                     os->name);
2148
2149            input = os->children.head->input_section.section;
2150            bfd_set_section_vma (os->bfd_section->owner,
2151                                 os->bfd_section,
2152                                 bfd_section_vma (input->owner, input));
2153            os->bfd_section->_raw_size = input->_raw_size;
2154            break;
2155          }
2156
2157        if (bfd_is_abs_section (os->bfd_section))
2158        {
2159          /* No matter what happens, an abs section starts at zero */
2160          ASSERT (os->bfd_section->vma == 0);
2161        }
2162        else
2163        {
2164          if (os->addr_tree == (etree_type *) NULL)
2165          {
2166            /* No address specified for this section, get one
2167               from the region specification
2168               */
2169            if (os->region == (lang_memory_region_type *) NULL
2170                || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2171                     & (SEC_ALLOC | SEC_LOAD)) != 0)
2172                    && os->region->name[0] == '*'
2173                    && strcmp (os->region->name, "*default*") == 0))
2174            {
2175              os->region = lang_memory_default (os->bfd_section);
2176            }
2177
2178            /* If a loadable section is using the default memory
2179               region, and some non default memory regions were
2180               defined, issue a warning.  */
2181            if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2182                 & (SEC_ALLOC | SEC_LOAD)) != 0
2183                && ! link_info.relocateable
2184                && strcmp (os->region->name, "*default*") == 0
2185                && lang_memory_region_list != NULL
2186                && (strcmp (lang_memory_region_list->name, "*default*") != 0
2187                    || lang_memory_region_list->next != NULL))
2188              einfo ("%P: warning: no memory region specified for section `%s'\n",
2189                     bfd_get_section_name (output_bfd, os->bfd_section));
2190
2191            dot = os->region->current;
2192            if (os->section_alignment == -1)
2193              {
2194                bfd_vma olddot;
2195
2196                olddot = dot;
2197                dot = align_power (dot, os->bfd_section->alignment_power);
2198                if (dot != olddot && config.warn_section_align)
2199                  einfo ("%P: warning: changing start of section %s by %u bytes\n",
2200                         os->name, (unsigned int) (dot - olddot));
2201              }
2202          }
2203          else
2204          {
2205            etree_value_type r;
2206
2207            r = exp_fold_tree (os->addr_tree,
2208                               abs_output_section,
2209                               lang_allocating_phase_enum,
2210                               dot, &dot);
2211            if (r.valid == false)
2212            {
2213              einfo ("%F%S: non constant address expression for section %s\n",
2214                     os->name);
2215            }
2216            dot = r.value + r.section->bfd_section->vma;
2217          }
2218          /* The section starts here */
2219          /* First, align to what the section needs */
2220
2221          if (os->section_alignment != -1)
2222            dot = align_power (dot, os->section_alignment);
2223
2224          bfd_set_section_vma (0, os->bfd_section, dot);
2225          
2226          os->bfd_section->output_offset = 0;
2227        }
2228
2229        (void) lang_size_sections (os->children.head, os, &os->children.head,
2230                                   os->fill, dot, relax);
2231        /* Ignore the size of the input sections, use the vma and size to */
2232        /* align against */
2233
2234        after = ALIGN_N (os->bfd_section->vma +
2235                         os->bfd_section->_raw_size,
2236                         /* The coercion here is important, see ld.h.  */
2237                         (bfd_vma) os->block_value);
2238
2239        if (bfd_is_abs_section (os->bfd_section))
2240          ASSERT (after == os->bfd_section->vma);
2241        else
2242          os->bfd_section->_raw_size = after - os->bfd_section->vma;
2243        dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2244        os->processed = true;
2245
2246        /* Replace into region ? */
2247        if (os->region != (lang_memory_region_type *) NULL)
2248          {
2249            os->region->current = dot;
2250            /* Make sure this isn't silly.  */
2251            if (os->region->current < os->region->origin
2252                || (os->region->current - os->region->origin
2253                    > os->region->length))
2254              {
2255                if (os->addr_tree != (etree_type *) NULL)
2256                  {
2257                    einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n",
2258                           os->region->current,
2259                           os->bfd_section->owner,
2260                           os->bfd_section->name,
2261                           os->region->name);
2262                  }
2263                else
2264                  {
2265                    einfo ("%X%P: region %s is full (%B section %s)\n",
2266                           os->region->name,
2267                           os->bfd_section->owner,
2268                           os->bfd_section->name);
2269                  }
2270                /* Reset the region pointer.  */
2271                os->region->current = os->region->origin;
2272              }
2273          }
2274      }
2275      break;
2276
2277      case lang_constructors_statement_enum:
2278       dot = lang_size_sections (constructor_list.head,
2279                                 output_section_statement,
2280                                 &s->wild_statement.children.head,
2281                                 fill,
2282                                 dot, relax);
2283       break;
2284
2285      case lang_data_statement_enum:
2286      {
2287        unsigned int size = 0;
2288
2289        s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
2290        s->data_statement.output_section =
2291         output_section_statement->bfd_section;
2292
2293        switch (s->data_statement.type)
2294          {
2295          case QUAD:
2296          case SQUAD:
2297            size = QUAD_SIZE;
2298            break;
2299          case LONG:
2300            size = LONG_SIZE;
2301            break;
2302          case SHORT:
2303            size = SHORT_SIZE;
2304            break;
2305          case BYTE:
2306            size = BYTE_SIZE;
2307            break;
2308          }
2309
2310        dot += size;
2311        output_section_statement->bfd_section->_raw_size += size;
2312        /* The output section gets contents, and then we inspect for
2313           any flags set in the input script which override any ALLOC */
2314        output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
2315        if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
2316          output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2317        }
2318      }
2319       break;
2320
2321      case lang_reloc_statement_enum:
2322      {
2323        int size;
2324
2325        s->reloc_statement.output_vma =
2326          dot - output_section_statement->bfd_section->vma;
2327        s->reloc_statement.output_section =
2328          output_section_statement->bfd_section;
2329        size = bfd_get_reloc_size (s->reloc_statement.howto);
2330        dot += size;
2331        output_section_statement->bfd_section->_raw_size += size;
2332      }
2333      break;
2334      
2335      case lang_wild_statement_enum:
2336
2337       dot = lang_size_sections (s->wild_statement.children.head,
2338                                 output_section_statement,
2339                                 &s->wild_statement.children.head,
2340
2341                                 fill, dot, relax);
2342
2343       break;
2344
2345      case lang_object_symbols_statement_enum:
2346       link_info.create_object_symbols_section =
2347         output_section_statement->bfd_section;
2348       break;
2349      case lang_output_statement_enum:
2350      case lang_target_statement_enum:
2351       break;
2352      case lang_input_section_enum:
2353       {
2354         asection *i;
2355
2356         i = (*prev)->input_section.section;
2357         if (! relax)
2358           {
2359             if (i->_cooked_size == 0)
2360               i->_cooked_size = i->_raw_size;
2361           }
2362         else
2363           {
2364             boolean again;
2365
2366             if (! bfd_relax_section (i->owner, i, &link_info, &again))
2367               einfo ("%P%F: can't relax section: %E\n");
2368             if (again)
2369               relax_again = true;
2370           }
2371         dot = size_input_section (prev,
2372                                   output_section_statement,
2373                                   output_section_statement->fill,
2374                                   dot, relax);
2375       }
2376       break;
2377      case lang_input_statement_enum:
2378       break;
2379      case lang_fill_statement_enum:
2380       s->fill_statement.output_section = output_section_statement->bfd_section;
2381
2382       fill = s->fill_statement.fill;
2383       break;
2384      case lang_assignment_statement_enum:
2385      {
2386        bfd_vma newdot = dot;
2387
2388        exp_fold_tree (s->assignment_statement.exp,
2389                       output_section_statement,
2390                       lang_allocating_phase_enum,
2391                       dot,
2392                       &newdot);
2393
2394        if (newdot != dot && !relax)
2395          {
2396            /* The assignment changed dot.  Insert a pad.  */
2397            if (output_section_statement == abs_output_section)
2398              {
2399                /* If we don't have an output section, then just adjust
2400                   the default memory address.  */
2401                lang_memory_region_lookup ("*default*")->current = newdot;
2402              }
2403            else
2404              {
2405                lang_statement_union_type *new =
2406                  ((lang_statement_union_type *)
2407                   stat_alloc (sizeof (lang_padding_statement_type)));
2408
2409                /* Link into existing chain */
2410                new->header.next = *prev;
2411                *prev = new;
2412                new->header.type = lang_padding_statement_enum;
2413                new->padding_statement.output_section =
2414                  output_section_statement->bfd_section;
2415                new->padding_statement.output_offset =
2416                  dot - output_section_statement->bfd_section->vma;
2417                new->padding_statement.fill = fill;
2418                new->padding_statement.size = newdot - dot;
2419                output_section_statement->bfd_section->_raw_size +=
2420                  new->padding_statement.size;
2421              }
2422
2423            dot = newdot;
2424          }
2425      }
2426      break;
2427
2428    case lang_padding_statement_enum:
2429      /* If we are relaxing, and this is not the first pass, some
2430         padding statements may have been inserted during previous
2431         passes.  We may have to move the padding statement to a new
2432         location if dot has a different value at this point in this
2433         pass than it did at this point in the previous pass.  */
2434      s->padding_statement.output_offset =
2435        dot - output_section_statement->bfd_section->vma;
2436      dot += s->padding_statement.size;
2437      output_section_statement->bfd_section->_raw_size +=
2438        s->padding_statement.size;
2439      break;
2440
2441      case lang_group_statement_enum:
2442        dot = lang_size_sections (s->group_statement.children.head,
2443                                  output_section_statement,
2444                                  &s->group_statement.children.head,
2445                                  fill, dot, relax);
2446        break;
2447
2448      default:
2449       FAIL ();
2450       break;
2451
2452       /* This can only get here when relaxing is turned on */
2453
2454      case lang_address_statement_enum:
2455       break;
2456     }
2457     prev = &s->header.next;
2458   }
2459   return dot;
2460 }
2461
2462 bfd_vma
2463 lang_do_assignments (s, output_section_statement, fill, dot)
2464      lang_statement_union_type * s;
2465      lang_output_section_statement_type * output_section_statement;
2466      fill_type fill;
2467      bfd_vma dot;
2468 {
2469   for (; s != (lang_statement_union_type *) NULL; s = s->next)
2470     {
2471       switch (s->header.type)
2472         {
2473         case lang_constructors_statement_enum:
2474           dot = lang_do_assignments (constructor_list.head,
2475                                      output_section_statement,
2476                                      fill,
2477                                      dot);
2478           break;
2479
2480         case lang_output_section_statement_enum:
2481           {
2482             lang_output_section_statement_type *os =
2483               &(s->output_section_statement);
2484
2485             if (os->bfd_section != NULL)
2486               {
2487                 dot = os->bfd_section->vma;
2488                 (void) lang_do_assignments (os->children.head, os,
2489                                             os->fill, dot);
2490                 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2491               }
2492             if (os->load_base) 
2493               {
2494                 /* If nothing has been placed into the output section then
2495                    it won't have a bfd_section. */
2496                 if (os->bfd_section) 
2497                   {
2498                     os->bfd_section->lma 
2499                       = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2500                   }
2501               }
2502           }
2503           break;
2504         case lang_wild_statement_enum:
2505
2506           dot = lang_do_assignments (s->wild_statement.children.head,
2507                                      output_section_statement,
2508                                      fill, dot);
2509
2510           break;
2511
2512         case lang_object_symbols_statement_enum:
2513         case lang_output_statement_enum:
2514         case lang_target_statement_enum:
2515 #if 0
2516         case lang_common_statement_enum:
2517 #endif
2518           break;
2519         case lang_data_statement_enum:
2520           {
2521             etree_value_type value;
2522
2523             value = exp_fold_tree (s->data_statement.exp,
2524                                    abs_output_section,
2525                                    lang_final_phase_enum, dot, &dot);
2526             s->data_statement.value = value.value;
2527             if (value.valid == false)
2528               einfo ("%F%P: invalid data statement\n");
2529           }
2530           switch (s->data_statement.type)
2531             {
2532             case QUAD:
2533             case SQUAD:
2534               dot += QUAD_SIZE;
2535               break;
2536             case LONG:
2537               dot += LONG_SIZE;
2538               break;
2539             case SHORT:
2540               dot += SHORT_SIZE;
2541               break;
2542             case BYTE:
2543               dot += BYTE_SIZE;
2544               break;
2545             }
2546           break;
2547
2548         case lang_reloc_statement_enum:
2549           {
2550             etree_value_type value;
2551
2552             value = exp_fold_tree (s->reloc_statement.addend_exp,
2553                                    abs_output_section,
2554                                    lang_final_phase_enum, dot, &dot);
2555             s->reloc_statement.addend_value = value.value;
2556             if (value.valid == false)
2557               einfo ("%F%P: invalid reloc statement\n");
2558           }
2559           dot += bfd_get_reloc_size (s->reloc_statement.howto);
2560           break;
2561
2562         case lang_input_section_enum:
2563           {
2564             asection *in = s->input_section.section;
2565
2566             if (in->_cooked_size != 0)
2567               dot += in->_cooked_size;
2568             else
2569               dot += in->_raw_size;
2570           }
2571           break;
2572
2573         case lang_input_statement_enum:
2574           break;
2575         case lang_fill_statement_enum:
2576           fill = s->fill_statement.fill;
2577           break;
2578         case lang_assignment_statement_enum:
2579           {
2580             exp_fold_tree (s->assignment_statement.exp,
2581                            output_section_statement,
2582                            lang_final_phase_enum,
2583                            dot,
2584                            &dot);
2585           }
2586
2587           break;
2588         case lang_padding_statement_enum:
2589           dot += s->padding_statement.size;
2590           break;
2591
2592         case lang_group_statement_enum:
2593           dot = lang_do_assignments (s->group_statement.children.head,
2594                                      output_section_statement,
2595                                      fill, dot);
2596
2597           break;
2598
2599         default:
2600           FAIL ();
2601           break;
2602         case lang_address_statement_enum:
2603           break;
2604         }
2605
2606     }
2607   return dot;
2608 }
2609
2610 /* Fix any .startof. or .sizeof. symbols.  When the assemblers see the
2611    operator .startof. (section_name), it produces an undefined symbol
2612    .startof.section_name.  Similarly, when it sees
2613    .sizeof. (section_name), it produces an undefined symbol
2614    .sizeof.section_name.  For all the output sections, we look for
2615    such symbols, and set them to the correct value.  */
2616
2617 static void
2618 lang_set_startof ()
2619 {
2620   asection *s;
2621
2622   if (link_info.relocateable)
2623     return;
2624
2625   for (s = output_bfd->sections; s != NULL; s = s->next)
2626     {
2627       const char *secname;
2628       char *buf;
2629       struct bfd_link_hash_entry *h;
2630
2631       secname = bfd_get_section_name (output_bfd, s);
2632       buf = xmalloc (10 + strlen (secname));
2633
2634       sprintf (buf, ".startof.%s", secname);
2635       h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2636       if (h != NULL && h->type == bfd_link_hash_undefined)
2637         {
2638           h->type = bfd_link_hash_defined;
2639           h->u.def.value = bfd_get_section_vma (output_bfd, s);
2640           h->u.def.section = bfd_abs_section_ptr;
2641         }
2642
2643       sprintf (buf, ".sizeof.%s", secname);
2644       h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2645       if (h != NULL && h->type == bfd_link_hash_undefined)
2646         {
2647           h->type = bfd_link_hash_defined;
2648           if (s->_cooked_size != 0)
2649             h->u.def.value = s->_cooked_size;
2650           else
2651             h->u.def.value = s->_raw_size;
2652           h->u.def.section = bfd_abs_section_ptr;
2653         }
2654
2655       free (buf);
2656     }
2657 }
2658
2659 static void
2660 lang_finish ()
2661 {
2662   struct bfd_link_hash_entry *h;
2663   boolean warn;
2664
2665   if (link_info.relocateable || link_info.shared)
2666     warn = false;
2667   else
2668     warn = true;
2669
2670   if (entry_symbol == (char *) NULL)
2671     {
2672       /* No entry has been specified.  Look for start, but don't warn
2673          if we don't find it.  */
2674       entry_symbol = "start";
2675       warn = false;
2676     }
2677
2678   h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2679   if (h != (struct bfd_link_hash_entry *) NULL
2680       && (h->type == bfd_link_hash_defined
2681           || h->type == bfd_link_hash_defweak)
2682       && h->u.def.section->output_section != NULL)
2683     {
2684       bfd_vma val;
2685
2686       val = (h->u.def.value
2687              + bfd_get_section_vma (output_bfd,
2688                                     h->u.def.section->output_section)
2689              + h->u.def.section->output_offset);
2690       if (! bfd_set_start_address (output_bfd, val))
2691         einfo ("%P%F:%s: can't set start address\n", entry_symbol);
2692     }
2693   else
2694     {
2695       asection *ts;
2696
2697       /* Can't find the entry symbol.  Use the first address in the
2698          text section.  */
2699       ts = bfd_get_section_by_name (output_bfd, ".text");
2700       if (ts != (asection *) NULL)
2701         {
2702           if (warn)
2703             einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2704                    entry_symbol, bfd_get_section_vma (output_bfd, ts));
2705           if (! bfd_set_start_address (output_bfd,
2706                                        bfd_get_section_vma (output_bfd, ts)))
2707             einfo ("%P%F: can't set start address\n");
2708         }
2709       else
2710         {
2711           if (warn)
2712             einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2713                    entry_symbol);
2714         }
2715     }
2716 }
2717
2718 /* This is a small function used when we want to ignore errors from
2719    BFD.  */
2720
2721 static void
2722 #ifdef ANSI_PROTOTYPES
2723 ignore_bfd_errors (const char *s, ...)
2724 #else
2725 ignore_bfd_errors (s)
2726      const char *s;
2727 #endif
2728 {
2729   /* Don't do anything.  */
2730 }
2731
2732 /* Check that the architecture of all the input files is compatible
2733    with the output file.  Also call the backend to let it do any
2734    other checking that is needed.  */
2735
2736 static void
2737 lang_check ()
2738 {
2739   lang_statement_union_type *file;
2740   bfd *input_bfd;
2741   CONST bfd_arch_info_type *compatible;
2742
2743   for (file = file_chain.head;
2744        file != (lang_statement_union_type *) NULL;
2745        file = file->input_statement.next)
2746     {
2747       input_bfd = file->input_statement.the_bfd;
2748       compatible = bfd_arch_get_compatible (input_bfd,
2749                                             output_bfd);
2750       if (compatible == NULL)
2751         {
2752           if (command_line.warn_mismatch)
2753             einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2754                    bfd_printable_name (input_bfd), input_bfd,
2755                    bfd_printable_name (output_bfd));
2756         }
2757       else
2758         {
2759           bfd_error_handler_type pfn = NULL;
2760
2761           /* If we aren't supposed to warn about mismatched input
2762              files, temporarily set the BFD error handler to a
2763              function which will do nothing.  We still want to call
2764              bfd_merge_private_bfd_data, since it may set up
2765              information which is needed in the output file.  */
2766           if (! command_line.warn_mismatch)
2767             pfn = bfd_set_error_handler (ignore_bfd_errors);
2768           if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
2769             {
2770               if (command_line.warn_mismatch)
2771                 einfo ("%E%X: failed to merge target specific data of file %B\n",
2772                        input_bfd);
2773             }
2774           if (! command_line.warn_mismatch)
2775             bfd_set_error_handler (pfn);
2776         }
2777     }
2778 }
2779
2780 /* Look through all the global common symbols and attach them to the
2781    correct section.  The -sort-common command line switch may be used
2782    to roughly sort the entries by size.  */
2783
2784 static void
2785 lang_common ()
2786 {
2787   if (link_info.relocateable
2788       && ! command_line.force_common_definition)
2789     return;
2790
2791   if (! config.sort_common)
2792     bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2793   else
2794     {
2795       int power;
2796
2797       for (power = 4; power >= 0; power--)
2798         bfd_link_hash_traverse (link_info.hash, lang_one_common,
2799                                 (PTR) &power);
2800     }
2801 }
2802
2803 /* Place one common symbol in the correct section.  */
2804
2805 static boolean
2806 lang_one_common (h, info)
2807      struct bfd_link_hash_entry *h;
2808      PTR info;
2809 {
2810   unsigned int power_of_two;
2811   bfd_vma size;
2812   asection *section;
2813
2814   if (h->type != bfd_link_hash_common)
2815     return true;
2816
2817   size = h->u.c.size;
2818   power_of_two = h->u.c.p->alignment_power;
2819
2820   if (config.sort_common
2821       && power_of_two < (unsigned int) *(int *) info)
2822     return true;
2823
2824   section = h->u.c.p->section;
2825
2826   /* Increase the size of the section.  */
2827   section->_raw_size = ALIGN_N (section->_raw_size,
2828                                 (bfd_size_type) (1 << power_of_two));
2829
2830   /* Adjust the alignment if necessary.  */
2831   if (power_of_two > section->alignment_power)
2832     section->alignment_power = power_of_two;
2833
2834   /* Change the symbol from common to defined.  */
2835   h->type = bfd_link_hash_defined;
2836   h->u.def.section = section;
2837   h->u.def.value = section->_raw_size;
2838
2839   /* Increase the size of the section.  */
2840   section->_raw_size += size;
2841
2842   /* Make sure the section is allocated in memory, and make sure that
2843      it is no longer a common section.  */
2844   section->flags |= SEC_ALLOC;
2845   section->flags &= ~ SEC_IS_COMMON;
2846
2847   if (config.map_file != NULL)
2848     {
2849       static boolean header_printed;
2850       int len;
2851       char *name;
2852       char buf[50];
2853
2854       if (! header_printed)
2855         {
2856           minfo ("\nAllocating common symbols\n");
2857           minfo ("Common symbol       size              file\n\n");
2858           header_printed = true;
2859         }
2860
2861       name = demangle (h->root.string);
2862       minfo ("%s", name);
2863       len = strlen (name);
2864       free (name);
2865
2866       if (len >= 19)
2867         {
2868           print_nl ();
2869           len = 0;
2870         }
2871       while (len < 20)
2872         {
2873           print_space ();
2874           ++len;
2875         }
2876
2877       minfo ("0x");
2878       if (size <= 0xffffffff)
2879         sprintf (buf, "%lx", (unsigned long) size);
2880       else
2881         sprintf_vma (buf, size);
2882       minfo ("%s", buf);
2883       len = strlen (buf);
2884
2885       while (len < 16)
2886         {
2887           print_space ();
2888           ++len;
2889         }
2890
2891       minfo ("%B\n", section->owner);
2892     }
2893
2894   return true;
2895 }
2896
2897 /*
2898 run through the input files and ensure that every input
2899 section has somewhere to go. If one is found without
2900 a destination then create an input request and place it
2901 into the statement tree.
2902 */
2903
2904 static void
2905 lang_place_orphans ()
2906 {
2907   lang_input_statement_type *file;
2908
2909   for (file = (lang_input_statement_type *) file_chain.head;
2910        file != (lang_input_statement_type *) NULL;
2911        file = (lang_input_statement_type *) file->next)
2912     {
2913       asection *s;
2914
2915       for (s = file->the_bfd->sections;
2916            s != (asection *) NULL;
2917            s = s->next)
2918         {
2919           if (s->output_section == (asection *) NULL)
2920             {
2921               /* This section of the file is not attatched, root
2922                  around for a sensible place for it to go */
2923
2924               if (file->just_syms_flag)
2925                 {
2926                   /* We are only retrieving symbol values from this
2927                      file.  We want the symbols to act as though the
2928                      values in the file are absolute.  */
2929                   s->output_section = bfd_abs_section_ptr;
2930                   s->output_offset = s->vma;
2931                 }
2932               else if (strcmp (s->name, "COMMON") == 0)
2933                 {
2934                   /* This is a lonely common section which must have
2935                      come from an archive.  We attach to the section
2936                      with the wildcard.  */
2937                   if (! link_info.relocateable
2938                       || command_line.force_common_definition)
2939                     {
2940                       if (default_common_section == NULL)
2941                         {
2942 #if 0
2943                           /* This message happens when using the
2944                              svr3.ifile linker script, so I have
2945                              disabled it.  */
2946                           info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2947 #endif
2948                           default_common_section =
2949                             lang_output_section_statement_lookup (".bss");
2950
2951                         }
2952                       wild_doit (&default_common_section->children, s,
2953                                  default_common_section, file);
2954                     }
2955                 }
2956               else if (ldemul_place_orphan (file, s))
2957                 ;
2958               else
2959                 {
2960                   lang_output_section_statement_type *os =
2961                   lang_output_section_statement_lookup (s->name);
2962
2963                   wild_doit (&os->children, s, os, file);
2964                 }
2965             }
2966         }
2967     }
2968 }
2969
2970
2971 void
2972 lang_set_flags (ptr, flags)
2973      lang_memory_region_type *ptr;
2974      CONST char *flags;
2975 {
2976   flagword *ptr_flags = &ptr->flags;
2977
2978   ptr->flags = ptr->not_flags = 0;
2979   while (*flags)
2980     {
2981       switch (*flags)
2982         {
2983         case '!':
2984           ptr_flags = (ptr_flags == &ptr->flags) ? &ptr->not_flags : &ptr->flags;
2985           break;
2986
2987         case 'A': case 'a':
2988           *ptr_flags |= SEC_ALLOC;
2989           break;
2990
2991         case 'R': case 'r':
2992           *ptr_flags |= SEC_READONLY;
2993           break;
2994
2995         case 'W': case 'w':
2996           *ptr_flags |= SEC_DATA;
2997           break;
2998
2999         case 'X': case 'x':
3000           *ptr_flags |= SEC_CODE;
3001           break;
3002
3003         case 'L': case 'l':
3004         case 'I': case 'i':
3005           *ptr_flags |= SEC_LOAD;
3006           break;
3007
3008         default:
3009           einfo ("%P%F: invalid syntax in flags\n");
3010           break;
3011         }
3012       flags++;
3013     }
3014 }
3015
3016 /* Call a function on each input file.  This function will be called
3017    on an archive, but not on the elements.  */
3018
3019 void
3020 lang_for_each_input_file (func)
3021      void (*func) PARAMS ((lang_input_statement_type *));
3022 {
3023   lang_input_statement_type *f;
3024
3025   for (f = (lang_input_statement_type *) input_file_chain.head;
3026        f != NULL;
3027        f = (lang_input_statement_type *) f->next_real_file)
3028     func (f);
3029 }
3030
3031 /* Call a function on each file.  The function will be called on all
3032    the elements of an archive which are included in the link, but will
3033    not be called on the archive file itself.  */
3034
3035 void
3036 lang_for_each_file (func)
3037      void (*func) PARAMS ((lang_input_statement_type *));
3038 {
3039   lang_input_statement_type *f;
3040
3041   for (f = (lang_input_statement_type *) file_chain.head;
3042        f != (lang_input_statement_type *) NULL;
3043        f = (lang_input_statement_type *) f->next)
3044     {
3045       func (f);
3046     }
3047 }
3048
3049 #if 0
3050
3051 /* Not used.  */
3052
3053 void
3054 lang_for_each_input_section (func)
3055      void (*func) PARAMS ((bfd * ab, asection * as));
3056 {
3057   lang_input_statement_type *f;
3058
3059   for (f = (lang_input_statement_type *) file_chain.head;
3060        f != (lang_input_statement_type *) NULL;
3061        f = (lang_input_statement_type *) f->next)
3062     {
3063       asection *s;
3064
3065       for (s = f->the_bfd->sections;
3066            s != (asection *) NULL;
3067            s = s->next)
3068         {
3069           func (f->the_bfd, s);
3070         }
3071     }
3072 }
3073
3074 #endif
3075
3076 void
3077 ldlang_add_file (entry)
3078      lang_input_statement_type * entry;
3079 {
3080   bfd **pp;
3081
3082   lang_statement_append (&file_chain,
3083                          (lang_statement_union_type *) entry,
3084                          &entry->next);
3085
3086   /* The BFD linker needs to have a list of all input BFDs involved in
3087      a link.  */
3088   ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3089   ASSERT (entry->the_bfd != output_bfd);
3090   for (pp = &link_info.input_bfds;
3091        *pp != (bfd *) NULL;
3092        pp = &(*pp)->link_next)
3093     ;
3094   *pp = entry->the_bfd;
3095   entry->the_bfd->usrdata = (PTR) entry;
3096   bfd_set_gp_size (entry->the_bfd, g_switch_value);
3097
3098   /* Look through the sections and check for any which should not be
3099      included in the link.  We need to do this now, so that we can
3100      notice when the backend linker tries to report multiple
3101      definition errors for symbols which are in sections we aren't
3102      going to link.  FIXME: It might be better to entirely ignore
3103      symbols which are defined in sections which are going to be
3104      discarded.  This would require modifying the backend linker for
3105      each backend which might set the SEC_LINK_ONCE flag.  If we do
3106      this, we should probably handle SEC_EXCLUDE in the same way.  */
3107
3108   bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
3109 }
3110
3111 void
3112 lang_add_output (name, from_script)
3113      CONST char *name;
3114      int from_script;
3115 {
3116   /* Make -o on command line override OUTPUT in script.  */
3117   if (had_output_filename == false || !from_script)
3118     {
3119       output_filename = name;
3120       had_output_filename = true;
3121     }
3122 }
3123
3124
3125 static lang_output_section_statement_type *current_section;
3126
3127 static int
3128 topower (x)
3129      int x;
3130 {
3131   unsigned int i = 1;
3132   int l;
3133
3134   if (x < 0)
3135     return -1;
3136
3137   for (l = 0; l < 32; l++) 
3138     {
3139       if (i >= (unsigned int) x)
3140         return l;
3141       i <<= 1;
3142     }
3143
3144   return 0;
3145 }
3146
3147 void
3148 lang_enter_output_section_statement (output_section_statement_name,
3149                                      address_exp, sectype, block_value,
3150                                      align, subalign, ebase)
3151      const char *output_section_statement_name;
3152      etree_type * address_exp;
3153      enum section_type sectype;
3154      bfd_vma block_value;
3155      etree_type *align;
3156      etree_type *subalign;
3157      etree_type *ebase;
3158 {
3159   lang_output_section_statement_type *os;
3160
3161   current_section =
3162    os =
3163     lang_output_section_statement_lookup (output_section_statement_name);
3164
3165
3166
3167   /* Add this statement to tree */
3168   /*  add_statement(lang_output_section_statement_enum,
3169       output_section_statement);*/
3170   /* Make next things chain into subchain of this */
3171
3172   if (os->addr_tree ==
3173       (etree_type *) NULL)
3174   {
3175     os->addr_tree =
3176      address_exp;
3177   }
3178   os->sectype = sectype;
3179   if (sectype != noload_section)
3180     os->flags = SEC_NO_FLAGS;
3181   else
3182     os->flags = SEC_NEVER_LOAD;
3183   os->block_value = block_value ? block_value : 1;
3184   stat_ptr = &os->children;
3185
3186   os->subsection_alignment = topower(
3187    exp_get_value_int(subalign, -1,
3188                      "subsection alignment",
3189                      0));
3190   os->section_alignment = topower(
3191    exp_get_value_int(align, -1,
3192                      "section alignment", 0));
3193
3194   os->load_base = ebase;
3195 }
3196
3197
3198 void
3199 lang_final ()
3200 {
3201   lang_output_statement_type *new =
3202     new_stat (lang_output_statement, stat_ptr);
3203
3204   new->name = output_filename;
3205 }
3206
3207 /* Reset the current counters in the regions */
3208 static void
3209 reset_memory_regions ()
3210 {
3211   lang_memory_region_type *p = lang_memory_region_list;
3212
3213   for (p = lang_memory_region_list;
3214        p != (lang_memory_region_type *) NULL;
3215        p = p->next)
3216     {
3217       p->old_length = (bfd_size_type) (p->current - p->origin);
3218       p->current = p->origin;
3219     }
3220 }
3221
3222 void
3223 lang_process ()
3224 {
3225   lang_reasonable_defaults ();
3226   current_target = default_target;
3227
3228   lang_for_each_statement (ldlang_open_output); /* Open the output file */
3229
3230   ldemul_create_output_section_statements ();
3231
3232   /* Add to the hash table all undefineds on the command line */
3233   lang_place_undefineds ();
3234
3235   /* Create a bfd for each input file */
3236   current_target = default_target;
3237   open_input_bfds (statement_list.head, false);
3238
3239   ldemul_after_open ();
3240
3241   /* Make sure that we're not mixing architectures.  We call this
3242      after all the input files have been opened, but before we do any
3243      other processing, so that any operations merge_private_bfd_data
3244      does on the output file will be known during the rest of the
3245      link.  */
3246   lang_check ();
3247
3248   /* Build all sets based on the information gathered from the input
3249      files.  */
3250   ldctor_build_sets ();
3251
3252   /* Size up the common data */
3253   lang_common ();
3254
3255   /* Run through the contours of the script and attach input sections
3256      to the correct output sections
3257      */
3258   map_input_to_output_sections (statement_list.head, (char *) NULL,
3259                                 (lang_output_section_statement_type *) NULL);
3260
3261
3262   /* Find any sections not attached explicitly and handle them */
3263   lang_place_orphans ();
3264
3265   ldemul_before_allocation ();
3266
3267   /* We must record the program headers before we try to fix the
3268      section positions, since they will affect SIZEOF_HEADERS.  */
3269   lang_record_phdrs ();
3270
3271   /* Now run around and relax if we can */
3272   if (command_line.relax)
3273     {
3274       /* First time round is a trial run to get the 'worst case'
3275          addresses of the objects if there was no relaxing.  */
3276       lang_size_sections (statement_list.head,
3277                           abs_output_section,
3278                           &(statement_list.head), 0, (bfd_vma) 0, false);
3279
3280       /* Keep relaxing until bfd_relax_section gives up.  */
3281       do
3282         {
3283           reset_memory_regions ();
3284
3285           relax_again = false;
3286
3287           /* Do all the assignments with our current guesses as to
3288              section sizes.  */
3289           lang_do_assignments (statement_list.head,
3290                                abs_output_section,
3291                                (fill_type) 0, (bfd_vma) 0);
3292
3293           /* Perform another relax pass - this time we know where the
3294              globals are, so can make better guess.  */
3295           lang_size_sections (statement_list.head,
3296                               abs_output_section,
3297                               &(statement_list.head), 0, (bfd_vma) 0, true);
3298         }
3299       while (relax_again);
3300     }
3301   else
3302     {
3303       /* Size up the sections.  */
3304       lang_size_sections (statement_list.head,
3305                           abs_output_section,
3306                           &(statement_list.head), 0, (bfd_vma) 0, false);
3307     }
3308
3309   /* See if anything special should be done now we know how big
3310      everything is.  */
3311   ldemul_after_allocation ();
3312
3313   /* Fix any .startof. or .sizeof. symbols.  */
3314   lang_set_startof ();
3315
3316   /* Do all the assignments, now that we know the final restingplaces
3317      of all the symbols */
3318
3319   lang_do_assignments (statement_list.head,
3320                        abs_output_section,
3321                        (fill_type) 0, (bfd_vma) 0);
3322
3323   /* Final stuffs */
3324
3325   ldemul_finish ();
3326   lang_finish ();
3327 }
3328
3329 /* EXPORTED TO YACC */
3330
3331 void
3332 lang_add_wild (section_name, filename)
3333      CONST char *CONST section_name;
3334      CONST char *CONST filename;
3335 {
3336   lang_wild_statement_type *new = new_stat (lang_wild_statement,
3337                                             stat_ptr);
3338
3339   if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
3340     {
3341       placed_commons = true;
3342     }
3343   if (filename != (char *) NULL)
3344     {
3345       lang_has_input_file = true;
3346     }
3347   new->section_name = section_name;
3348   new->filename = filename;
3349   lang_list_init (&new->children);
3350 }
3351
3352 void
3353 lang_section_start (name, address)
3354      CONST char *name;
3355      etree_type * address;
3356 {
3357   lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
3358
3359   ad->section_name = name;
3360   ad->address = address;
3361 }
3362
3363 /* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
3364    because of a -e argument on the command line, or zero if this is
3365    called by ENTRY in a linker script.  Command line arguments take
3366    precedence.  */
3367
3368 /* WINDOWS_NT.  When an entry point has been specified, we will also force
3369    this symbol to be defined by calling ldlang_add_undef (equivalent to 
3370    having switch -u entry_name on the command line).  The reason we do
3371    this is so that the user doesn't have to because they would have to use
3372    the -u switch if they were specifying an entry point other than 
3373    _mainCRTStartup.  Specifically, if creating a windows application, entry
3374    point _WinMainCRTStartup must be specified.
3375      What I have found for non console applications (entry not _mainCRTStartup)
3376    is that the .obj that contains mainCRTStartup is brought in since it is
3377    the first encountered in libc.lib and it has other symbols in it which will
3378    be pulled in by the link process.  To avoid this, adding -u with the entry
3379    point name specified forces the correct .obj to be used.  We can avoid
3380    making the user do this by always adding the entry point name as an
3381    undefined symbol.  */
3382
3383 void
3384 lang_add_entry (name, cmdline)
3385      CONST char *name;
3386      boolean cmdline;
3387 {
3388   if (entry_symbol == NULL
3389       || cmdline
3390       || ! entry_from_cmdline)
3391     {
3392       entry_symbol = name;
3393       entry_from_cmdline = cmdline;
3394     }
3395 #if 0 
3396   /* don't do this yet.  It seems to work (the executables run), but the 
3397      image created is very different from what I was getting before indicating
3398      that something else is being pulled in.  When everything else is working,
3399      then try to put this back in to see if it will do the right thing for
3400      other more complicated applications */
3401   ldlang_add_undef (name);
3402 #endif
3403 }
3404
3405 void
3406 lang_add_target (name)
3407      CONST char *name;
3408 {
3409   lang_target_statement_type *new = new_stat (lang_target_statement,
3410                                               stat_ptr);
3411
3412   new->target = name;
3413
3414 }
3415
3416 void
3417 lang_add_map (name)
3418      CONST char *name;
3419 {
3420   while (*name)
3421     {
3422       switch (*name)
3423         {
3424           case 'F':
3425           map_option_f = true;
3426           break;
3427         }
3428       name++;
3429     }
3430 }
3431
3432 void
3433 lang_add_fill (exp)
3434      int exp;
3435 {
3436   lang_fill_statement_type *new = new_stat (lang_fill_statement,
3437                                             stat_ptr);
3438
3439   new->fill = exp;
3440 }
3441
3442 void
3443 lang_add_data (type, exp)
3444      int type;
3445      union etree_union *exp;
3446 {
3447
3448   lang_data_statement_type *new = new_stat (lang_data_statement,
3449                                             stat_ptr);
3450
3451   new->exp = exp;
3452   new->type = type;
3453
3454 }
3455
3456 /* Create a new reloc statement.  RELOC is the BFD relocation type to
3457    generate.  HOWTO is the corresponding howto structure (we could
3458    look this up, but the caller has already done so).  SECTION is the
3459    section to generate a reloc against, or NAME is the name of the
3460    symbol to generate a reloc against.  Exactly one of SECTION and
3461    NAME must be NULL.  ADDEND is an expression for the addend.  */
3462
3463 void
3464 lang_add_reloc (reloc, howto, section, name, addend)
3465      bfd_reloc_code_real_type reloc;
3466      reloc_howto_type *howto;
3467      asection *section;
3468      const char *name;
3469      union etree_union *addend;
3470 {
3471   lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
3472   
3473   p->reloc = reloc;
3474   p->howto = howto;
3475   p->section = section;
3476   p->name = name;
3477   p->addend_exp = addend;
3478
3479   p->addend_value = 0;
3480   p->output_section = NULL;
3481   p->output_vma = 0;
3482 }
3483
3484 void
3485 lang_add_assignment (exp)
3486      etree_type * exp;
3487 {
3488   lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
3489                                                   stat_ptr);
3490
3491   new->exp = exp;
3492 }
3493
3494 void
3495 lang_add_attribute (attribute)
3496      enum statement_enum attribute;
3497 {
3498   new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
3499 }
3500
3501 void
3502 lang_startup (name)
3503      CONST char *name;
3504 {
3505   if (startup_file != (char *) NULL)
3506     {
3507       einfo ("%P%Fmultiple STARTUP files\n");
3508     }
3509   first_file->filename = name;
3510   first_file->local_sym_name = name;
3511   first_file->real = true;
3512
3513   startup_file = name;
3514 }
3515
3516 void
3517 lang_float (maybe)
3518      boolean maybe;
3519 {
3520   lang_float_flag = maybe;
3521 }
3522
3523 void
3524 lang_leave_output_section_statement (fill, memspec, phdrs)
3525      bfd_vma fill;
3526      const char *memspec;
3527      struct lang_output_section_phdr_list *phdrs;
3528 {
3529   current_section->fill = fill;
3530   current_section->region = lang_memory_region_lookup (memspec);
3531   current_section->phdrs = phdrs;
3532   stat_ptr = &statement_list;
3533 }
3534
3535 /*
3536  Create an absolute symbol with the given name with the value of the
3537  address of first byte of the section named.
3538
3539  If the symbol already exists, then do nothing.
3540 */
3541 void
3542 lang_abs_symbol_at_beginning_of (secname, name)
3543      const char *secname;
3544      const char *name;
3545 {
3546   struct bfd_link_hash_entry *h;
3547
3548   h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3549   if (h == (struct bfd_link_hash_entry *) NULL)
3550     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3551
3552   if (h->type == bfd_link_hash_new
3553       || h->type == bfd_link_hash_undefined)
3554     {
3555       asection *sec;
3556
3557       h->type = bfd_link_hash_defined;
3558
3559       sec = bfd_get_section_by_name (output_bfd, secname);
3560       if (sec == (asection *) NULL)
3561         h->u.def.value = 0;
3562       else
3563         h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3564
3565       h->u.def.section = bfd_abs_section_ptr;
3566     }
3567 }
3568
3569 /*
3570  Create an absolute symbol with the given name with the value of the
3571  address of the first byte after the end of the section named.
3572
3573  If the symbol already exists, then do nothing.
3574 */
3575 void
3576 lang_abs_symbol_at_end_of (secname, name)
3577      const char *secname;
3578      const char *name;
3579 {
3580   struct bfd_link_hash_entry *h;
3581
3582   h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3583   if (h == (struct bfd_link_hash_entry *) NULL)
3584     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3585
3586   if (h->type == bfd_link_hash_new
3587       || h->type == bfd_link_hash_undefined)
3588     {
3589       asection *sec;
3590
3591       h->type = bfd_link_hash_defined;
3592
3593       sec = bfd_get_section_by_name (output_bfd, secname);
3594       if (sec == (asection *) NULL)
3595         h->u.def.value = 0;
3596       else
3597         h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3598                           + bfd_section_size (output_bfd, sec));
3599
3600       h->u.def.section = bfd_abs_section_ptr;
3601     }
3602 }
3603
3604 void
3605 lang_statement_append (list, element, field)
3606      lang_statement_list_type * list;
3607      lang_statement_union_type * element;
3608      lang_statement_union_type ** field;
3609 {
3610   *(list->tail) = element;
3611   list->tail = field;
3612 }
3613
3614 /* Set the output format type.  -oformat overrides scripts.  */
3615
3616 void
3617 lang_add_output_format (format, big, little, from_script)
3618      const char *format;
3619      const char *big;
3620      const char *little;
3621      int from_script;
3622 {
3623   if (output_target == NULL || !from_script)
3624     {
3625       if (command_line.endian == ENDIAN_BIG
3626           && big != NULL)
3627         format = big;
3628       else if (command_line.endian == ENDIAN_LITTLE
3629                && little != NULL)
3630         format = little;
3631
3632       output_target = format;
3633     }
3634 }
3635
3636 /* Enter a group.  This creates a new lang_group_statement, and sets
3637    stat_ptr to build new statements within the group.  */
3638
3639 void
3640 lang_enter_group ()
3641 {
3642   lang_group_statement_type *g;
3643
3644   g = new_stat (lang_group_statement, stat_ptr);
3645   lang_list_init (&g->children);
3646   stat_ptr = &g->children;
3647 }
3648
3649 /* Leave a group.  This just resets stat_ptr to start writing to the
3650    regular list of statements again.  Note that this will not work if
3651    groups can occur inside anything else which can adjust stat_ptr,
3652    but currently they can't.  */
3653
3654 void
3655 lang_leave_group ()
3656 {
3657   stat_ptr = &statement_list;
3658 }
3659
3660 /* Add a new program header.  This is called for each entry in a PHDRS
3661    command in a linker script.  */
3662
3663 void
3664 lang_new_phdr (name, type, filehdr, phdrs, at, flags)
3665      const char *name;
3666      etree_type *type;
3667      boolean filehdr;
3668      boolean phdrs;
3669      etree_type *at;
3670      etree_type *flags;
3671 {
3672   struct lang_phdr *n, **pp;
3673
3674   n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
3675   n->next = NULL;
3676   n->name = name;
3677   n->type = exp_get_value_int (type, 0, "program header type",
3678                                lang_final_phase_enum);
3679   n->filehdr = filehdr;
3680   n->phdrs = phdrs;
3681   n->at = at;
3682   n->flags = flags;
3683
3684   for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
3685     ;
3686   *pp = n;
3687 }
3688
3689 /* Record the program header information in the output BFD.  FIXME: We
3690    should not be calling an ELF specific function here.  */
3691
3692 static void
3693 lang_record_phdrs ()
3694 {
3695   unsigned int alc;
3696   asection **secs;
3697   struct lang_output_section_phdr_list *last;
3698   struct lang_phdr *l;
3699   lang_statement_union_type *u;
3700
3701   alc = 10;
3702   secs = (asection **) xmalloc (alc * sizeof (asection *));
3703   last = NULL;
3704   for (l = lang_phdr_list; l != NULL; l = l->next)
3705     {
3706       unsigned int c;
3707       flagword flags;
3708       bfd_vma at;
3709
3710       c = 0;
3711       for (u = lang_output_section_statement.head;
3712            u != NULL;
3713            u = u->output_section_statement.next)
3714         {
3715           lang_output_section_statement_type *os;
3716           struct lang_output_section_phdr_list *pl;
3717
3718           os = &u->output_section_statement;
3719
3720           pl = os->phdrs;
3721           if (pl != NULL)
3722             last = pl;
3723           else
3724             {
3725               if (os->sectype == noload_section
3726                   || os->bfd_section == NULL
3727                   || (os->bfd_section->flags & SEC_ALLOC) == 0)
3728                 continue;
3729               pl = last;
3730             }
3731
3732           if (os->bfd_section == NULL)
3733             continue;
3734
3735           for (; pl != NULL; pl = pl->next)
3736             {
3737               if (strcmp (pl->name, l->name) == 0)
3738                 {
3739                   if (c >= alc)
3740                     {
3741                       alc *= 2;
3742                       secs = ((asection **)
3743                               xrealloc (secs, alc * sizeof (asection *)));
3744                     }
3745                   secs[c] = os->bfd_section;
3746                   ++c;
3747                   pl->used = true;
3748                 }
3749             }
3750         }
3751
3752       if (l->flags == NULL)
3753         flags = 0;
3754       else
3755         flags = exp_get_vma (l->flags, 0, "phdr flags",
3756                              lang_final_phase_enum);
3757
3758       if (l->at == NULL)
3759         at = 0;
3760       else
3761         at = exp_get_vma (l->at, 0, "phdr load address",
3762                           lang_final_phase_enum);
3763
3764       if (! bfd_record_phdr (output_bfd, l->type,
3765                              l->flags == NULL ? false : true,
3766                              flags,
3767                              l->at == NULL ? false : true,
3768                              at, l->filehdr, l->phdrs, c, secs))
3769         einfo ("%F%P: bfd_record_phdr failed: %E\n");
3770     }
3771
3772   free (secs);
3773
3774   /* Make sure all the phdr assignments succeeded.  */
3775   for (u = lang_output_section_statement.head;
3776        u != NULL;
3777        u = u->output_section_statement.next)
3778     {
3779       struct lang_output_section_phdr_list *pl;
3780
3781       if (u->output_section_statement.bfd_section == NULL)
3782         continue;
3783
3784       for (pl = u->output_section_statement.phdrs;
3785            pl != NULL;
3786            pl = pl->next)
3787         if (! pl->used && strcmp (pl->name, "NONE") != 0)
3788           einfo ("%X%P: section `%s' assigned to non-existent phdr `%s'\n",
3789                  u->output_section_statement.name, pl->name);
3790     }
3791 }
3792
3793 /* Record a list of sections which may not be cross referenced.  */
3794
3795 void
3796 lang_add_nocrossref (l)
3797      struct lang_nocrossref *l;
3798 {
3799   struct lang_nocrossrefs *n;
3800
3801   n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
3802   n->next = nocrossref_list;
3803   n->list = l;
3804   nocrossref_list = n;
3805
3806   /* Set notice_all so that we get informed about all symbols.  */
3807   link_info.notice_all = true;
3808 }
3809 \f
3810 /* Overlay handling.  We handle overlays with some static variables.  */
3811
3812 /* The overlay virtual address.  */
3813 static etree_type *overlay_vma;
3814
3815 /* The overlay load address.  */
3816 static etree_type *overlay_lma;
3817
3818 /* Whether nocrossrefs is set for this overlay.  */
3819 static int overlay_nocrossrefs;
3820
3821 /* An expression for the maximum section size seen so far.  */
3822 static etree_type *overlay_max;
3823
3824 /* A list of all the sections in this overlay.  */
3825
3826 struct overlay_list
3827 {
3828   struct overlay_list *next;
3829   lang_output_section_statement_type *os;
3830 };
3831
3832 static struct overlay_list *overlay_list;
3833
3834 /* Start handling an overlay.  */
3835
3836 void
3837 lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
3838      etree_type *vma_expr;
3839      etree_type *lma_expr;
3840      int nocrossrefs;
3841 {
3842   /* The grammar should prevent nested overlays from occurring.  */
3843   ASSERT (overlay_vma == NULL
3844           && overlay_lma == NULL
3845           && overlay_list == NULL
3846           && overlay_max == NULL);
3847
3848   overlay_vma = vma_expr;
3849   overlay_lma = lma_expr;
3850   overlay_nocrossrefs = nocrossrefs;
3851 }
3852
3853 /* Start a section in an overlay.  We handle this by calling
3854    lang_enter_output_section_statement with the correct VMA and LMA.  */
3855
3856 void
3857 lang_enter_overlay_section (name)
3858      const char *name;
3859 {
3860   struct overlay_list *n;
3861   etree_type *size;
3862
3863   lang_enter_output_section_statement (name, overlay_vma, normal_section,
3864                                        0, 0, 0, overlay_lma);
3865
3866   /* If this is the first section, then base the VMA and LMA of future
3867      sections on this one.  This will work correctly even if `.' is
3868      used in the addresses.  */
3869   if (overlay_list == NULL)
3870     {
3871       overlay_vma = exp_nameop (ADDR, name);
3872       overlay_lma = exp_nameop (LOADADDR, name);
3873     }
3874
3875   /* Remember the section.  */
3876   n = (struct overlay_list *) xmalloc (sizeof *n);
3877   n->os = current_section;
3878   n->next = overlay_list;
3879   overlay_list = n;
3880
3881   size = exp_nameop (SIZEOF, name);
3882
3883   /* Adjust the LMA for the next section.  */
3884   overlay_lma = exp_binop ('+', overlay_lma, size);
3885
3886   /* Arrange to work out the maximum section end address.  */
3887   if (overlay_max == NULL)
3888     overlay_max = size;
3889   else
3890     overlay_max = exp_binop (MAX, overlay_max, size);
3891 }
3892
3893 /* Finish a section in an overlay.  There isn't any special to do
3894    here.  */
3895
3896 void
3897 lang_leave_overlay_section (fill, phdrs)
3898      bfd_vma fill;
3899      struct lang_output_section_phdr_list *phdrs;
3900 {
3901   const char *name;
3902   char *clean, *s2;
3903   const char *s1;
3904   char *buf;
3905
3906   name = current_section->name;
3907
3908   lang_leave_output_section_statement (fill, "*default*", phdrs);
3909
3910   /* Define the magic symbols.  */
3911
3912   clean = xmalloc (strlen (name) + 1);
3913   s2 = clean;
3914   for (s1 = name; *s1 != '\0'; s1++)
3915     if (isalnum ((unsigned char) *s1) || *s1 == '_')
3916       *s2++ = *s1;
3917   *s2 = '\0';
3918
3919   buf = xmalloc (strlen (clean) + sizeof "__load_start_");
3920   sprintf (buf, "__load_start_%s", clean);
3921   lang_add_assignment (exp_assop ('=', buf,
3922                                   exp_nameop (LOADADDR, name)));
3923
3924   buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
3925   sprintf (buf, "__load_stop_%s", clean);
3926   lang_add_assignment (exp_assop ('=', buf,
3927                                   exp_binop ('+',
3928                                              exp_nameop (LOADADDR, name),
3929                                              exp_nameop (SIZEOF, name))));
3930
3931   free (clean);
3932 }
3933
3934 /* Finish an overlay.  If there are any overlay wide settings, this
3935    looks through all the sections in the overlay and sets them.  */
3936
3937 void
3938 lang_leave_overlay (fill, memspec, phdrs)
3939      bfd_vma fill;
3940      const char *memspec;
3941      struct lang_output_section_phdr_list *phdrs;
3942 {
3943   lang_memory_region_type *region;
3944   struct overlay_list *l;
3945   struct lang_nocrossref *nocrossref;
3946
3947   if (memspec == NULL)
3948     region = NULL;
3949   else
3950     region = lang_memory_region_lookup (memspec);
3951
3952   nocrossref = NULL;
3953
3954   l = overlay_list;
3955   while (l != NULL)
3956     {
3957       struct overlay_list *next;
3958
3959       if (fill != 0 && l->os->fill == 0)
3960         l->os->fill = fill;
3961       if (region != NULL && l->os->region == NULL)
3962         l->os->region = region;
3963       if (phdrs != NULL && l->os->phdrs == NULL)
3964         l->os->phdrs = phdrs;
3965
3966       if (overlay_nocrossrefs)
3967         {
3968           struct lang_nocrossref *nc;
3969
3970           nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
3971           nc->name = l->os->name;
3972           nc->next = nocrossref;
3973           nocrossref = nc;
3974         }
3975
3976       next = l->next;
3977       free (l);
3978       l = next;
3979     }
3980
3981   if (nocrossref != NULL)
3982     lang_add_nocrossref (nocrossref);
3983
3984   /* Update . for the end of the overlay.  */
3985   lang_add_assignment (exp_assop ('=', ".",
3986                                   exp_binop ('+', overlay_vma, overlay_max)));
3987
3988   overlay_vma = NULL;
3989   overlay_lma = NULL;
3990   overlay_nocrossrefs = 0;
3991   overlay_list = NULL;
3992   overlay_max = NULL;
3993 }
3994 \f
3995 /* Version handling.  This is only useful for ELF.  */
3996
3997 /* This global variable holds the version tree that we build.  */
3998
3999 struct bfd_elf_version_tree *lang_elf_version_info;
4000
4001 /* This is called for each variable name or match expression.  */
4002
4003 struct bfd_elf_version_expr *
4004 lang_new_vers_regex (orig, new)
4005      struct bfd_elf_version_expr *orig;
4006      const char *new;
4007 {
4008   struct bfd_elf_version_expr *ret;
4009
4010   ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4011   ret->next = orig;
4012   ret->match = new;
4013   return ret;
4014 }
4015
4016 /* This is called for each set of variable names and match
4017    expressions.  */
4018
4019 struct bfd_elf_version_tree *
4020 lang_new_vers_node (globals, locals)
4021      struct bfd_elf_version_expr *globals;
4022      struct bfd_elf_version_expr *locals;
4023 {
4024   struct bfd_elf_version_tree *ret;
4025
4026   ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
4027   ret->next = NULL;
4028   ret->name = NULL;
4029   ret->vernum = 0;
4030   ret->globals = globals;
4031   ret->locals = locals;
4032   ret->deps = NULL;
4033   ret->name_indx = (unsigned int) -1;
4034   ret->used = 0;
4035   return ret;
4036 }
4037
4038 /* This static variable keeps track of version indices.  */
4039
4040 static int version_index;
4041
4042 /* This is called when we know the name and dependencies of the
4043    version.  */
4044
4045 void
4046 lang_register_vers_node (name, version, deps)
4047      const char *name;
4048      struct bfd_elf_version_tree *version;
4049      struct bfd_elf_version_deps *deps;
4050 {
4051   struct bfd_elf_version_tree *t, **pp;
4052   struct bfd_elf_version_expr *e1;
4053
4054   /* Make sure this node has a unique name.  */
4055   for (t = lang_elf_version_info; t != NULL; t = t->next)
4056     if (strcmp (t->name, name) == 0)
4057       einfo ("%X%P: duplicate version tag `%s'\n", name);
4058
4059   /* Check the global and local match names, and make sure there
4060      aren't any duplicates.  */
4061
4062   for (e1 = version->globals; e1 != NULL; e1 = e1->next)
4063     {
4064       for (t = lang_elf_version_info; t != NULL; t = t->next)
4065         {
4066           struct bfd_elf_version_expr *e2;
4067
4068           for (e2 = t->locals; e2 != NULL; e2 = e2->next)
4069             if (strcmp (e1->match, e2->match) == 0)
4070               einfo ("%X%P: duplicate expression `%s' in version information\n",
4071                      e1->match);
4072         }
4073     }
4074
4075   for (e1 = version->locals; e1 != NULL; e1 = e1->next)
4076     {
4077       for (t = lang_elf_version_info; t != NULL; t = t->next)
4078         {
4079           struct bfd_elf_version_expr *e2;
4080
4081           for (e2 = t->globals; e2 != NULL; e2 = e2->next)
4082             if (strcmp (e1->match, e2->match) == 0)
4083               einfo ("%X%P: duplicate expression `%s' in version information\n",
4084                      e1->match);
4085         }
4086     }
4087
4088   version->deps = deps;
4089   version->name = name;
4090   ++version_index;
4091   version->vernum = version_index;
4092
4093   for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
4094     ;
4095   *pp = version;
4096 }
4097
4098 /* This is called when we see a version dependency.  */
4099
4100 struct bfd_elf_version_deps *
4101 lang_add_vers_depend (list, name)
4102      struct bfd_elf_version_deps *list;
4103      const char *name;
4104 {
4105   struct bfd_elf_version_deps *ret;
4106   struct bfd_elf_version_tree *t;
4107
4108   ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
4109   ret->next = list;
4110
4111   for (t = lang_elf_version_info; t != NULL; t = t->next)
4112     {
4113       if (strcmp (t->name, name) == 0)
4114         {
4115           ret->version_needed = t;
4116           return ret;
4117         }
4118     }
4119
4120   einfo ("%X%P: unable to find version dependency `%s'\n", name);
4121
4122   return ret;
4123 }