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