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