* ldlang.c (load_symbols): Check for archive before object. Use
[external/binutils.git] / ld / ldlang.c
1 /* Linker command language support.
2    Copyright (C) 1991, 92, 93, 94 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
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libiberty.h"
23 #include "bfdlink.h"
24
25 #include "ld.h"
26 #include "ldmain.h"
27 #include "ldgram.h"
28 #include "ldexp.h"
29 #include "ldlang.h"
30 #include "ldemul.h"
31 #include "ldlex.h"
32 #include "ldmisc.h"
33 #include "ldctor.h"
34 #include "ldfile.h"
35
36 /* FORWARDS */
37 static void print_statements PARAMS ((void));
38 static void print_statement PARAMS ((lang_statement_union_type *,
39                                       lang_output_section_statement_type *));
40 static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
41                                                          size_t,
42                                                          lang_statement_list_type*));
43
44
45 /* LOCALS */
46 static struct obstack stat_obstack;
47
48 #define obstack_chunk_alloc xmalloc
49 #define obstack_chunk_free free
50 static CONST char *startup_file;
51 static lang_statement_list_type input_file_chain;
52 static boolean placed_commons = false;
53 static lang_output_section_statement_type *default_common_section;
54 static boolean map_option_f;
55 static bfd_vma print_dot;
56 static lang_input_statement_type *first_file;
57 static lang_statement_list_type lang_output_section_statement;
58 static CONST char *current_target;
59 static CONST char *output_target;
60 static int longest_section_name = 8;
61 static lang_statement_list_type statement_list;
62
63 static void print_size PARAMS ((size_t value));
64 static void print_alignment PARAMS ((unsigned int value));
65 static void print_fill PARAMS ((fill_type value));
66 static void print_section PARAMS ((const char *name));
67 static void lang_for_each_statement_worker
68   PARAMS ((void (*func) (lang_statement_union_type *),
69            lang_statement_union_type *s));
70 static lang_input_statement_type *new_afile
71   PARAMS ((const char *name, lang_input_file_enum_type file_type,
72            const char *target, boolean add_to_list));
73 static void print_flags PARAMS ((int *ignore_flags));
74 static void init_os PARAMS ((lang_output_section_statement_type *s));
75 static void wild_section PARAMS ((lang_wild_statement_type *ptr,
76                                   const char *section,
77                                   lang_input_statement_type *file,
78                                   lang_output_section_statement_type *output));
79 static lang_input_statement_type *lookup_name PARAMS ((const char *name));
80 static void load_symbols PARAMS ((lang_input_statement_type *entry));
81 static void wild PARAMS ((lang_wild_statement_type *s,
82                           const char *section, const char *file,
83                           const char *target,
84                           lang_output_section_statement_type *output));
85 static bfd *open_output PARAMS ((const char *name));
86 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
87 static void open_input_bfds
88   PARAMS ((lang_statement_union_type *statement, boolean));
89 static void lang_reasonable_defaults PARAMS ((void));
90 static void lang_place_undefineds PARAMS ((void));
91 static void map_input_to_output_sections
92   PARAMS ((lang_statement_union_type *s,
93            const char *target,
94            lang_output_section_statement_type *output_section_statement));
95 static void print_output_section_statement
96   PARAMS ((lang_output_section_statement_type *output_section_statement));
97 static void print_assignment
98   PARAMS ((lang_assignment_statement_type *assignment,
99            lang_output_section_statement_type *output_section));
100 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
101 static void print_input_section PARAMS ((lang_input_section_type *in));
102 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
103 static void print_data_statement PARAMS ((lang_data_statement_type *data));
104 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
105 static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
106 static void print_wild_statement
107   PARAMS ((lang_wild_statement_type *w,
108            lang_output_section_statement_type *os));
109 static void print_group
110   PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
111 static void print_statement PARAMS ((lang_statement_union_type *s,
112                                      lang_output_section_statement_type *os));
113 static void print_statements PARAMS ((void));
114 static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
115                                    fill_type fill, unsigned int power,
116                                    asection *output_section_statement,
117                                    bfd_vma dot));
118 static bfd_vma size_input_section
119   PARAMS ((lang_statement_union_type **this_ptr,
120            lang_output_section_statement_type *output_section_statement,
121            fill_type fill, bfd_vma dot, boolean relax));
122 static void lang_finish PARAMS ((void));
123 static void lang_check PARAMS ((void));
124 static void lang_common PARAMS ((void));
125 static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
126 static void lang_place_orphans PARAMS ((void));
127 static int topower PARAMS ((int));
128 static void reset_memory_regions PARAMS ((void));
129
130 /* EXPORTS */
131 lang_output_section_statement_type *abs_output_section;
132 lang_statement_list_type *stat_ptr = &statement_list;
133 lang_statement_list_type file_chain = { 0 };
134 static const char *entry_symbol = 0;
135 boolean lang_has_input_file = false;
136 boolean had_output_filename = false;
137 boolean lang_float_flag = false;
138 boolean delete_output_file_on_failure = false;
139
140 etree_type *base; /* Relocation base - or null */
141
142
143 #if defined(__STDC__) || defined(ALMOST_STDC)
144 #define cat(a,b) a##b
145 #else
146 #define cat(a,b) a/**/b
147 #endif
148
149 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
150
151 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
152
153 #define outside_symbol_address(q) ((q)->value +   outside_section_address(q->section))
154
155 PTR
156 stat_alloc (size)
157      size_t size;
158 {
159   return obstack_alloc (&stat_obstack, size);
160 }
161
162 static void
163 print_size (value)
164      size_t value;
165 {
166   fprintf (config.map_file, "%5x", (unsigned) value);
167 }
168
169 static void
170 print_alignment (value)
171      unsigned int value;
172 {
173   fprintf (config.map_file, "2**%1u", value);
174 }
175
176 static void
177 print_fill (value)
178      fill_type value;
179 {
180   fprintf (config.map_file, "%04x", (unsigned) value);
181 }
182
183 static void
184 print_section (name)
185      CONST char *name;
186 {
187   fprintf (config.map_file, "%*s", -longest_section_name, name);
188 }
189
190 /*----------------------------------------------------------------------
191   lang_for_each_statement walks the parse tree and calls the provided
192   function for each node
193 */
194
195 static void
196 lang_for_each_statement_worker (func, s)
197      void (*func) PARAMS ((lang_statement_union_type *));
198      lang_statement_union_type *s;
199 {
200   for (; s != (lang_statement_union_type *) NULL; s = s->next)
201     {
202       func (s);
203
204       switch (s->header.type)
205         {
206         case lang_constructors_statement_enum:
207           lang_for_each_statement_worker (func, constructor_list.head);
208           break;
209         case lang_output_section_statement_enum:
210           lang_for_each_statement_worker
211             (func,
212              s->output_section_statement.children.head);
213           break;
214         case lang_wild_statement_enum:
215           lang_for_each_statement_worker
216             (func,
217              s->wild_statement.children.head);
218           break;
219         case lang_group_statement_enum:
220           lang_for_each_statement_worker (func,
221                                           s->group_statement.children.head);
222           break;
223         case lang_data_statement_enum:
224         case lang_reloc_statement_enum:
225         case lang_object_symbols_statement_enum:
226         case lang_output_statement_enum:
227         case lang_target_statement_enum:
228         case lang_input_section_enum:
229         case lang_input_statement_enum:
230         case lang_assignment_statement_enum:
231         case lang_padding_statement_enum:
232         case lang_address_statement_enum:
233           break;
234         default:
235           FAIL ();
236           break;
237         }
238     }
239 }
240
241 void
242 lang_for_each_statement (func)
243      void (*func) PARAMS ((lang_statement_union_type *));
244 {
245   lang_for_each_statement_worker (func,
246                                   statement_list.head);
247 }
248
249 /*----------------------------------------------------------------------*/
250 void
251 lang_list_init (list)
252      lang_statement_list_type *list;
253 {
254   list->head = (lang_statement_union_type *) NULL;
255   list->tail = &list->head;
256 }
257
258 /*----------------------------------------------------------------------
259
260   build a new statement node for the parse tree
261
262  */
263
264 static
265 lang_statement_union_type *
266 new_statement (type, size, list)
267      enum statement_enum type;
268      size_t size;
269      lang_statement_list_type * list;
270 {
271   lang_statement_union_type *new = (lang_statement_union_type *)
272   stat_alloc (size);
273
274   new->header.type = type;
275   new->header.next = (lang_statement_union_type *) NULL;
276   lang_statement_append (list, new, &new->header.next);
277   return new;
278 }
279
280 /*
281   Build a new input file node for the language. There are several ways
282   in which we treat an input file, eg, we only look at symbols, or
283   prefix it with a -l etc.
284
285   We can be supplied with requests for input files more than once;
286   they may, for example be split over serveral lines like foo.o(.text)
287   foo.o(.data) etc, so when asked for a file we check that we havn't
288   got it already so we don't duplicate the bfd.
289
290  */
291 static lang_input_statement_type *
292 new_afile (name, file_type, target, add_to_list)
293      CONST char *name;
294      lang_input_file_enum_type file_type;
295      CONST char *target;
296      boolean add_to_list;
297 {
298   lang_input_statement_type *p;
299
300   if (add_to_list)
301     p = new_stat (lang_input_statement, stat_ptr);
302   else
303     {
304       p = ((lang_input_statement_type *)
305            stat_alloc (sizeof (lang_input_statement_type)));
306       p->header.next = NULL;
307     }
308
309   lang_has_input_file = true;
310   p->target = target;
311   p->complained = false;
312   switch (file_type)
313     {
314     case lang_input_file_is_symbols_only_enum:
315       p->filename = name;
316       p->is_archive = false;
317       p->real = true;
318       p->local_sym_name = name;
319       p->just_syms_flag = true;
320       p->search_dirs_flag = false;
321       break;
322     case lang_input_file_is_fake_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 = false;
329       break;
330     case lang_input_file_is_l_enum:
331       p->is_archive = true;
332       p->filename = name;
333       p->real = true;
334       p->local_sym_name = concat ("-l", name, (const char *) NULL);
335       p->just_syms_flag = false;
336       p->search_dirs_flag = true;
337       break;
338     case lang_input_file_is_marker_enum:
339       p->filename = name;
340       p->is_archive = false;
341       p->real = false;
342       p->local_sym_name = name;
343       p->just_syms_flag = false;
344       p->search_dirs_flag = true;
345       break;
346     case lang_input_file_is_search_file_enum:
347       p->filename = name;
348       p->is_archive = false;
349       p->real = true;
350       p->local_sym_name = name;
351       p->just_syms_flag = false;
352       p->search_dirs_flag = true;
353       break;
354     case lang_input_file_is_file_enum:
355       p->filename = name;
356       p->is_archive = false;
357       p->real = true;
358       p->local_sym_name = name;
359       p->just_syms_flag = false;
360       p->search_dirs_flag = false;
361       break;
362     default:
363       FAIL ();
364     }
365   p->the_bfd = (bfd *) NULL;
366   p->asymbols = (asymbol **) NULL;
367   p->next_real_file = (lang_statement_union_type *) NULL;
368   p->next = (lang_statement_union_type *) NULL;
369   p->symbol_count = 0;
370   p->common_output_section = (asection *) NULL;
371   p->loaded = false;
372   lang_statement_append (&input_file_chain,
373                          (lang_statement_union_type *) p,
374                          &p->next_real_file);
375   return p;
376 }
377
378 lang_input_statement_type *
379 lang_add_input_file (name, file_type, target)
380      CONST char *name;
381      lang_input_file_enum_type file_type;
382      CONST char *target;
383 {
384   lang_has_input_file = true;
385   return new_afile (name, file_type, target, true);
386 }
387
388 /* Build enough state so that the parser can build its tree */
389 void
390 lang_init ()
391 {
392   obstack_begin (&stat_obstack, 1000);
393
394   stat_ptr = &statement_list;
395
396   lang_list_init (stat_ptr);
397
398   lang_list_init (&input_file_chain);
399   lang_list_init (&lang_output_section_statement);
400   lang_list_init (&file_chain);
401   first_file = lang_add_input_file ((char *) NULL,
402                                     lang_input_file_is_marker_enum,
403                                     (char *) NULL);
404   abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
405
406   abs_output_section->bfd_section = bfd_abs_section_ptr;
407
408 }
409
410 /*----------------------------------------------------------------------
411  A region is an area of memory declared with the
412  MEMORY {  name:org=exp, len=exp ... }
413  syntax.
414
415  We maintain a list of all the regions here
416
417  If no regions are specified in the script, then the default is used
418  which is created when looked up to be the entire data space
419 */
420
421 static lang_memory_region_type *lang_memory_region_list;
422 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
423
424 lang_memory_region_type *
425 lang_memory_region_lookup (name)
426      CONST char *CONST name;
427 {
428
429   lang_memory_region_type *p = lang_memory_region_list;
430
431   for (p = lang_memory_region_list;
432        p != (lang_memory_region_type *) NULL;
433        p = p->next)
434     {
435       if (strcmp (p->name, name) == 0)
436         {
437           return p;
438         }
439     }
440   if (strcmp (name, "*default*") == 0)
441     {
442       /* This is the default region, dig out first one on the list */
443       if (lang_memory_region_list != (lang_memory_region_type *) NULL)
444         {
445           return lang_memory_region_list;
446         }
447     }
448   {
449     lang_memory_region_type *new =
450     (lang_memory_region_type *) stat_alloc ((bfd_size_type) (sizeof (lang_memory_region_type)));
451
452     new->name = buystring (name);
453     new->next = (lang_memory_region_type *) NULL;
454
455     *lang_memory_region_list_tail = new;
456     lang_memory_region_list_tail = &new->next;
457     new->origin = 0;
458     new->length = ~(bfd_size_type)0;
459     new->current = 0;
460     new->had_full_message = false;
461
462     return new;
463   }
464 }
465
466
467 lang_output_section_statement_type *
468 lang_output_section_find (name)
469      CONST char *CONST name;
470 {
471   lang_statement_union_type *u;
472   lang_output_section_statement_type *lookup;
473
474   for (u = lang_output_section_statement.head;
475        u != (lang_statement_union_type *) NULL;
476        u = lookup->next)
477     {
478       lookup = &u->output_section_statement;
479       if (strcmp (name, lookup->name) == 0)
480         {
481           return lookup;
482         }
483     }
484   return (lang_output_section_statement_type *) NULL;
485 }
486
487 lang_output_section_statement_type *
488 lang_output_section_statement_lookup (name)
489      CONST char *CONST name;
490 {
491   lang_output_section_statement_type *lookup;
492
493   lookup = lang_output_section_find (name);
494   if (lookup == (lang_output_section_statement_type *) NULL)
495     {
496
497       lookup = (lang_output_section_statement_type *)
498         new_stat (lang_output_section_statement, stat_ptr);
499       lookup->region = (lang_memory_region_type *) NULL;
500       lookup->fill = 0;
501       lookup->block_value = 1;
502       lookup->name = name;
503
504       lookup->next = (lang_statement_union_type *) NULL;
505       lookup->bfd_section = (asection *) NULL;
506       lookup->processed = false;
507       lookup->loadable = 1;
508       lookup->addr_tree = (etree_type *) NULL;
509       lang_list_init (&lookup->children);
510
511       lookup->memspec = (CONST char *) NULL;
512       lookup->flags = 0;
513       lookup->subsection_alignment = -1;
514       lookup->section_alignment = -1;
515       lookup->load_base = (union etree_union *) NULL;
516
517       lang_statement_append (&lang_output_section_statement,
518                              (lang_statement_union_type *) lookup,
519                              &lookup->next);
520     }
521   return lookup;
522 }
523
524 /*ARGSUSED*/
525 static void
526 print_flags (ignore_flags)
527      int *ignore_flags;
528 {
529   fprintf (config.map_file, "(");
530 #if 0
531   if (flags->flag_read)
532     fprintf (outfile, "R");
533   if (flags->flag_write)
534     fprintf (outfile, "W");
535   if (flags->flag_executable)
536     fprintf (outfile, "X");
537   if (flags->flag_loadable)
538     fprintf (outfile, "L");
539 #endif
540  fprintf (config.map_file, ")");
541 }
542
543 void
544 lang_map ()
545 {
546   lang_memory_region_type *m;
547
548   fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n");
549 #ifdef HOST_64_BIT
550   fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n");
551 #else
552   fprintf (config.map_file,
553            "name\t\torigin   length   r_size   c_size    is    attributes\n");
554
555 #endif
556   for (m = lang_memory_region_list;
557        m != (lang_memory_region_type *) NULL;
558        m = m->next)
559     {
560       fprintf (config.map_file, "%-16s", m->name);
561       print_address (m->origin);
562       print_space ();
563       print_address ((bfd_vma)m->length);
564       print_space ();
565       print_address ((bfd_vma)m->old_length);
566       print_space();
567       print_address (m->current - m->origin);
568       print_space();
569       if (m->old_length)
570        fprintf (config.map_file, " %2d%%  ",
571                 (int) ((m->current - m->origin) * 100 / m->old_length));
572       print_flags (&m->flags);
573       fprintf (config.map_file, "\n");
574     }
575   fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
576   fprintf (config.map_file, "output   input     virtual\n");
577   fprintf (config.map_file, "section  section   address    tsize\n\n");
578
579   print_statements ();
580
581 }
582
583 /*
584  *
585  */
586 static void
587 init_os (s)
588      lang_output_section_statement_type * s;
589 {
590 /*  asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
591   section_userdata_type *new =
592   (section_userdata_type *)
593   stat_alloc ((bfd_size_type) (sizeof (section_userdata_type)));
594
595   s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
596   if (s->bfd_section == (asection *) NULL)
597     s->bfd_section = bfd_make_section (output_bfd, s->name);
598   if (s->bfd_section == (asection *) NULL)
599     {
600       einfo ("%P%F: output format %s cannot represent section called %s\n",
601              output_bfd->xvec->name, s->name);
602     }
603   s->bfd_section->output_section = s->bfd_section;
604 /*  s->bfd_section->flags = s->flags;*/
605
606   /* We initialize an output sections output offset to minus its own */
607   /* vma to allow us to output a section through itself */
608   s->bfd_section->output_offset = 0;
609   get_userdata (s->bfd_section) = (PTR) new;
610
611 }
612
613 /***********************************************************************
614   The wild routines.
615
616   These expand statements like *(.text) and foo.o to a list of
617   explicit actions, like foo.o(.text), bar.o(.text) and
618   foo.o(.text,.data) .
619
620   The toplevel routine, wild, takes a statement, section, file and
621   target. If either the section or file is null it is taken to be the
622   wildcard. Seperate lang_input_section statements are created for
623   each part of the expanstion, and placed after the statement provided.
624
625 */
626
627 void
628 wild_doit (ptr, section, output, file)
629      lang_statement_list_type * ptr;
630      asection * section;
631      lang_output_section_statement_type * output;
632      lang_input_statement_type * file;
633 {
634   if (output->bfd_section == (asection *) NULL)
635     init_os (output);
636
637   if (section != (asection *) NULL
638       && section->output_section == (asection *) NULL)
639   {
640     /* Add a section reference to the list */
641     lang_input_section_type *new = new_stat (lang_input_section, ptr);
642
643     new->section = section;
644     new->ifile = file;
645     section->output_section = output->bfd_section;
646
647     /* We don't copy the SEC_NEVER_LOAD flag from an input section to
648        an output section, because we want to be able to include a
649        SEC_NEVER_LOAD section in the middle of an otherwise loaded
650        section (I don't know why we want to do this, but we do).
651        build_link_order in ldwrite.c handles this case by turning the
652        embedded SEC_NEVER_LOAD section into a fill.  */
653     section->output_section->flags |=
654       section->flags & (flagword) (~ SEC_NEVER_LOAD);
655
656     if (!output->loadable) 
657     {
658       /* Turn off load flag */
659       output->bfd_section->flags &= ~SEC_LOAD;
660       output->bfd_section->flags |= SEC_NEVER_LOAD;
661     }
662     if (section->alignment_power > output->bfd_section->alignment_power)
663     {
664       output->bfd_section->alignment_power = section->alignment_power;
665     }
666     /* If supplied an aligmnet, then force it */
667     if (output->section_alignment != -1)
668     {
669       output->bfd_section->alignment_power = output->section_alignment;
670     }
671   }
672 }
673
674 static void
675 wild_section (ptr, section, file, output)
676      lang_wild_statement_type *ptr;
677      const char *section;
678      lang_input_statement_type *file;
679      lang_output_section_statement_type *output;
680 {
681   if (file->just_syms_flag == false)
682     {
683       register asection *s;
684
685       for (s = file->the_bfd->sections; s != NULL; s = s->next)
686         {
687           /* Attach all sections named SECTION.  If SECTION is NULL,
688              then attach all sections.
689
690              Previously, if SECTION was NULL, this code did not call
691              wild_doit if the SEC_IS_COMMON flag was set for the
692              section.  I did not understand that, and I took it out.
693              --ian@cygnus.com.  */
694
695           if (section == NULL
696               || strcmp (bfd_get_section_name (file->the_bfd, s),
697                          section) == 0)
698             wild_doit (&ptr->children, s, output, file);
699         }
700     }
701 }
702
703 /* passed a file name (which must have been seen already and added to
704    the statement tree. We will see if it has been opened already and
705    had its symbols read. If not then we'll read it.
706
707    Archives are pecuilar here. We may open them once, but if they do
708    not define anything we need at the time, they won't have all their
709    symbols read. If we need them later, we'll have to redo it.  */
710 static lang_input_statement_type *
711 lookup_name (name)
712      CONST char *name;
713 {
714   lang_input_statement_type *search;
715
716   for (search = (lang_input_statement_type *) input_file_chain.head;
717        search != (lang_input_statement_type *) NULL;
718        search = (lang_input_statement_type *) search->next_real_file)
719     {
720       if (search->filename == (char *) NULL && name == (char *) NULL)
721         return search;
722       if (search->filename != (char *) NULL
723           && name != (char *) NULL
724           && strcmp (search->filename, name) == 0)
725         break;
726     }
727
728   if (search == (lang_input_statement_type *) NULL)
729     search = new_afile (name, lang_input_file_is_file_enum, default_target,
730                         false);
731
732   /* If we have already added this file, or this file is not real
733      (FIXME: can that ever actually happen?) or the name is NULL
734      (FIXME: can that ever actually happen?) don't add this file.  */
735   if (search->loaded
736       || ! search->real
737       || search->filename == (const char *) NULL)
738     return search;
739
740   load_symbols (search);
741
742   return search;
743 }
744
745 /* Get the symbols for an input file.  */
746
747 static void
748 load_symbols (entry)
749      lang_input_statement_type *entry;
750 {
751   char **matching;
752
753   if (entry->loaded)
754     return;
755
756   ldfile_open_file (entry);
757
758   if (! bfd_check_format (entry->the_bfd, bfd_archive)
759       && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
760     {
761       bfd_error_type err;
762
763       err = bfd_get_error ();
764       if (err == bfd_error_file_ambiguously_recognized)
765         {
766           char **p;
767
768           einfo ("%B: file not recognized: %E\n", entry->the_bfd);
769           einfo ("%B: matching formats:", entry->the_bfd);
770           for (p = matching; *p != NULL; p++)
771             einfo (" %s", *p);
772           einfo ("%F\n");
773         }
774       else if (err != bfd_error_file_not_recognized)
775         einfo ("%F%B: file not recognized: %E\n", entry->the_bfd);
776
777       /* Try to interpret the file as a linker script.  */
778
779       bfd_close (entry->the_bfd);
780       entry->the_bfd = NULL;
781
782       ldfile_open_command_file (entry->filename);
783
784       ldfile_assumed_script = true;
785       parser_input = input_script;
786       yyparse ();
787       ldfile_assumed_script = false;
788
789       return;
790     }
791
792   /* We don't call ldlang_add_file for an archive.  Instead, the
793      add_symbols entry point will call ldlang_add_file, via the
794      add_archive_element callback, for each element of the archive
795      which is used.  */
796   if (bfd_get_format (entry->the_bfd) == bfd_object)
797     {
798       ldlang_add_file (entry);
799       if (trace_files || trace_file_tries)
800         info_msg ("%I\n", entry);
801     }
802
803   if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
804     einfo ("%F%B: could not read symbols: %E\n", entry->the_bfd);
805
806   entry->loaded = true;
807 }
808
809 static void
810 wild (s, section, file, target, output)
811      lang_wild_statement_type * s;
812      CONST char *section;
813      CONST char *file;
814      CONST char *target;
815      lang_output_section_statement_type * output;
816 {
817   lang_input_statement_type *f;
818
819   if (file == (char *) NULL)
820     {
821       /* Perform the iteration over all files in the list */
822       for (f = (lang_input_statement_type *) file_chain.head;
823            f != (lang_input_statement_type *) NULL;
824            f = (lang_input_statement_type *) f->next)
825         {
826           wild_section (s, section, f, output);
827         }
828     }
829   else
830     {
831       /* Perform the iteration over a single file */
832       wild_section (s, section, lookup_name (file), output);
833     }
834   if (section != (char *) NULL
835       && strcmp (section, "COMMON") == 0
836    && default_common_section == (lang_output_section_statement_type *) NULL)
837     {
838       /* Remember the section that common is going to incase we later
839          get something which doesn't know where to put it */
840       default_common_section = output;
841     }
842 }
843
844 /*
845   read in all the files
846   */
847
848 static bfd *
849 open_output (name)
850      CONST char *name;
851 {
852   bfd *output;
853
854   if (output_target == (char *) NULL)
855     {
856       if (current_target != (char *) NULL)
857         output_target = current_target;
858       else
859         output_target = default_target;
860     }
861   output = bfd_openw (name, output_target);
862
863   if (output == (bfd *) NULL)
864     {
865       if (bfd_get_error () == bfd_error_invalid_target)
866         {
867           einfo ("%P%F: target %s not found\n", output_target);
868         }
869       einfo ("%P%F: cannot open output file %s: %E\n", name);
870     }
871
872   delete_output_file_on_failure = 1;
873
874   /*  output->flags |= D_PAGED;*/
875
876   if (! bfd_set_format (output, bfd_object))
877     einfo ("%P%F:%s: can not make object file: %E\n", name);
878   if (! bfd_set_arch_mach (output,
879                            ldfile_output_architecture,
880                            ldfile_output_machine))
881     einfo ("%P%F:%s: can not set architecture: %E\n", name);
882
883   link_info.hash = bfd_link_hash_table_create (output);
884   if (link_info.hash == (struct bfd_link_hash_table *) NULL)
885     einfo ("%P%F: can not create link hash table: %E\n");
886
887   bfd_set_gp_size (output, g_switch_value);
888   return output;
889 }
890
891
892
893
894 static void
895 ldlang_open_output (statement)
896      lang_statement_union_type * statement;
897 {
898   switch (statement->header.type)
899     {
900     case lang_output_statement_enum:
901       ASSERT (output_bfd == (bfd *) NULL);
902       output_bfd = open_output (statement->output_statement.name);
903       ldemul_set_output_arch ();
904       if (config.magic_demand_paged && !link_info.relocateable)
905         output_bfd->flags |= D_PAGED;
906       else
907         output_bfd->flags &= ~D_PAGED;
908       if (config.text_read_only)
909         output_bfd->flags |= WP_TEXT;
910       else
911         output_bfd->flags &= ~WP_TEXT;
912       if (config.traditional_format)
913         output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
914       else
915         output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
916       break;
917
918     case lang_target_statement_enum:
919       current_target = statement->target_statement.target;
920       break;
921     default:
922       break;
923     }
924 }
925
926 /* Open all the input files.  */
927
928 static void
929 open_input_bfds (s, force)
930      lang_statement_union_type *s;
931      boolean force;
932 {
933   for (; s != (lang_statement_union_type *) NULL; s = s->next)
934     {
935       switch (s->header.type)
936         {
937         case lang_constructors_statement_enum:
938           open_input_bfds (constructor_list.head, force);
939           break;
940         case lang_output_section_statement_enum:
941           open_input_bfds (s->output_section_statement.children.head, force);
942           break;
943         case lang_wild_statement_enum:
944           /* Maybe we should load the file's symbols */
945           if (s->wild_statement.filename)
946             (void) lookup_name (s->wild_statement.filename);
947           open_input_bfds (s->wild_statement.children.head, force);
948           break;
949         case lang_group_statement_enum:
950           {
951             struct bfd_link_hash_entry *undefs;
952
953             /* We must continually search the entries in the group
954                until no new symbols are added to the list of undefined
955                symbols.  */
956
957             do
958               {
959                 undefs = link_info.hash->undefs_tail;
960                 open_input_bfds (s->group_statement.children.head, true);
961               }
962             while (undefs != link_info.hash->undefs_tail);
963           }
964           break;
965         case lang_target_statement_enum:
966           current_target = s->target_statement.target;
967           break;
968         case lang_input_statement_enum:
969           if (s->input_statement.real == true)
970             {
971               s->input_statement.target = current_target;
972
973               /* If we are being called from within a group, and this
974                  is an archive which has already been searched, then
975                  force it to be researched.  */
976               if (force
977                   && s->input_statement.loaded
978                   && bfd_check_format (s->input_statement.the_bfd,
979                                        bfd_archive))
980                 s->input_statement.loaded = false;
981
982               load_symbols (&s->input_statement);
983             }
984           break;
985         default:
986           break;
987         }
988     }
989 }
990
991 /* If there are [COMMONS] statements, put a wild one into the bss section */
992
993 static void
994 lang_reasonable_defaults ()
995 {
996 #if 0
997   lang_output_section_statement_lookup (".text");
998   lang_output_section_statement_lookup (".data");
999
1000   default_common_section =
1001     lang_output_section_statement_lookup (".bss");
1002
1003
1004   if (placed_commons == false)
1005     {
1006       lang_wild_statement_type *new =
1007       new_stat (lang_wild_statement,
1008                 &default_common_section->children);
1009
1010       new->section_name = "COMMON";
1011       new->filename = (char *) NULL;
1012       lang_list_init (&new->children);
1013     }
1014 #endif
1015
1016 }
1017
1018 /*
1019  Add the supplied name to the symbol table as an undefined reference.
1020  Remove items from the chain as we open input bfds
1021  */
1022 typedef struct ldlang_undef_chain_list
1023 {
1024   struct ldlang_undef_chain_list *next;
1025   char *name;
1026 }                       ldlang_undef_chain_list_type;
1027
1028 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1029
1030 void
1031 ldlang_add_undef (name)
1032      CONST char *CONST name;
1033 {
1034   ldlang_undef_chain_list_type *new =
1035   (ldlang_undef_chain_list_type
1036    *) stat_alloc ((bfd_size_type) (sizeof (ldlang_undef_chain_list_type)));
1037
1038   new->next = ldlang_undef_chain_list_head;
1039   ldlang_undef_chain_list_head = new;
1040
1041   new->name = buystring (name);
1042 }
1043
1044 /* Run through the list of undefineds created above and place them
1045    into the linker hash table as undefined symbols belonging to the
1046    script file.
1047 */
1048 static void
1049 lang_place_undefineds ()
1050 {
1051   ldlang_undef_chain_list_type *ptr;
1052
1053   for (ptr = ldlang_undef_chain_list_head;
1054        ptr != (ldlang_undef_chain_list_type *) NULL;
1055        ptr = ptr->next)
1056     {
1057       struct bfd_link_hash_entry *h;
1058
1059       h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1060       if (h == (struct bfd_link_hash_entry *) NULL)
1061         einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1062       if (h->type == bfd_link_hash_new)
1063         {
1064           h->type = bfd_link_hash_undefined;
1065           h->u.undef.abfd = NULL;
1066           bfd_link_add_undef (link_info.hash, h);
1067         }
1068     }
1069 }
1070
1071 /* Open input files and attatch to output sections */
1072 static void
1073 map_input_to_output_sections (s, target, output_section_statement)
1074      lang_statement_union_type * s;
1075      CONST char *target;
1076      lang_output_section_statement_type * output_section_statement;
1077 {
1078   for (; s != (lang_statement_union_type *) NULL; s = s->next)
1079     {
1080       switch (s->header.type)
1081         {
1082
1083
1084         case lang_wild_statement_enum:
1085           wild (&s->wild_statement, s->wild_statement.section_name,
1086                 s->wild_statement.filename, target,
1087                 output_section_statement);
1088
1089           break;
1090         case lang_constructors_statement_enum:
1091           map_input_to_output_sections (constructor_list.head,
1092                                         target,
1093                                         output_section_statement);
1094           break;
1095         case lang_output_section_statement_enum:
1096           map_input_to_output_sections (s->output_section_statement.children.head,
1097                                         target,
1098                                         &s->output_section_statement);
1099           break;
1100         case lang_output_statement_enum:
1101           break;
1102         case lang_target_statement_enum:
1103           target = s->target_statement.target;
1104           break;
1105         case lang_group_statement_enum:
1106           map_input_to_output_sections (s->group_statement.children.head,
1107                                         target,
1108                                         output_section_statement);
1109           break;
1110         case lang_fill_statement_enum:
1111         case lang_input_section_enum:
1112         case lang_object_symbols_statement_enum:
1113         case lang_data_statement_enum:
1114         case lang_reloc_statement_enum:
1115         case lang_assignment_statement_enum:
1116         case lang_padding_statement_enum:
1117         case lang_input_statement_enum:
1118           if (output_section_statement != NULL
1119               && output_section_statement->bfd_section == NULL)
1120             init_os (output_section_statement);
1121           break;
1122         case lang_afile_asection_pair_statement_enum:
1123           FAIL ();
1124           break;
1125         case lang_address_statement_enum:
1126           /* Mark the specified section with the supplied address */
1127           {
1128             lang_output_section_statement_type *os =
1129               lang_output_section_statement_lookup
1130                 (s->address_statement.section_name);
1131
1132             if (os->bfd_section == NULL)
1133               init_os (os);
1134             os->addr_tree = s->address_statement.address;
1135           }
1136           break;
1137         }
1138     }
1139 }
1140
1141 static void
1142 print_output_section_statement (output_section_statement)
1143      lang_output_section_statement_type * output_section_statement;
1144 {
1145   asection *section = output_section_statement->bfd_section;
1146
1147   print_nl ();
1148   print_section (output_section_statement->name);
1149
1150
1151   if (section)
1152   {
1153     print_dot = section->vma;
1154     print_space ();
1155     print_section ("");
1156     print_space ();
1157     print_address (section->vma);
1158     print_space ();
1159     print_size (section->_raw_size);
1160     print_space();
1161     print_size(section->_cooked_size);
1162     print_space ();
1163     print_alignment (section->alignment_power);
1164     print_space ();
1165 #if 0
1166     fprintf (config.map_file, "%s flags", output_section_statement->region->name);
1167     print_flags (stdout, &output_section_statement->flags);
1168 #endif
1169     if (section->flags & SEC_LOAD)
1170      fprintf (config.map_file, "load ");
1171     if (section->flags & SEC_ALLOC)
1172      fprintf (config.map_file, "alloc ");
1173     if (section->flags & SEC_RELOC)
1174      fprintf (config.map_file, "reloc ");
1175     if (section->flags & SEC_HAS_CONTENTS)
1176      fprintf (config.map_file, "contents ");
1177
1178   }
1179   else
1180   {
1181     fprintf (config.map_file, " (no attached output section)");
1182   }
1183   print_nl ();
1184   if (output_section_statement->load_base)
1185     {
1186       int b = exp_get_abs_int(output_section_statement->load_base,
1187                                 0, "output base", lang_final_phase_enum);
1188       printf("Output address   %08x\n", b);
1189     }
1190   if (output_section_statement->section_alignment >= 0
1191       || output_section_statement->section_alignment >= 0) 
1192   {
1193     printf("\t\t\t\t\tforced alignment ");
1194     if ( output_section_statement->section_alignment >= 0) 
1195     {
1196       printf("section 2**%d ",output_section_statement->section_alignment );
1197     }
1198     if ( output_section_statement->subsection_alignment >= 0) 
1199     {
1200       printf("subsection 2**%d ",output_section_statement->subsection_alignment );
1201     }
1202   
1203     print_nl ();
1204   }
1205   print_statement (output_section_statement->children.head,
1206                    output_section_statement);
1207
1208 }
1209
1210 static void
1211 print_assignment (assignment, output_section)
1212      lang_assignment_statement_type * assignment;
1213      lang_output_section_statement_type * output_section;
1214 {
1215   etree_value_type result;
1216
1217   print_section ("");
1218   print_space ();
1219   print_section ("");
1220   print_space ();
1221   print_address (print_dot);
1222   print_space ();
1223   result = exp_fold_tree (assignment->exp->assign.src,
1224                           output_section,
1225                           lang_final_phase_enum,
1226                           print_dot,
1227                           &print_dot);
1228
1229   if (result.valid)
1230     {
1231       print_address (result.value);
1232     }
1233   else
1234     {
1235       fprintf (config.map_file, "*undefined*");
1236     }
1237   print_space ();
1238   exp_print_tree (assignment->exp);
1239
1240   fprintf (config.map_file, "\n");
1241 }
1242
1243 static void
1244 print_input_statement (statm)
1245      lang_input_statement_type * statm;
1246 {
1247   if (statm->filename != (char *) NULL)
1248     {
1249       fprintf (config.map_file, "LOAD %s\n", statm->filename);
1250     }
1251 }
1252
1253 /* Print all the defined symbols for the abfd provided by in the supplied
1254    section.
1255 */
1256
1257 static boolean 
1258 print_one_symbol (hash_entry, ptr)
1259 struct  bfd_link_hash_entry *hash_entry;
1260 PTR ptr;
1261 {
1262   asection * sec = (asection *)ptr;
1263
1264   if (hash_entry->type == bfd_link_hash_defined) 
1265     {
1266       if (sec == hash_entry->u.def.section) {
1267         print_section ("");
1268         fprintf (config.map_file, " ");
1269         print_section ("");
1270         fprintf (config.map_file, " ");
1271         print_address (hash_entry->u.def.value + outside_section_address (sec));
1272         fprintf (config.map_file, "              %s", hash_entry->root.string);
1273         print_nl ();
1274       }
1275     }
1276
1277   return true;
1278 }
1279
1280 static void
1281 print_input_section (in)
1282      lang_input_section_type * in;
1283 {
1284   asection *i = in->section;
1285   bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1286
1287   if (size != 0)
1288     {
1289       print_section ("");
1290       fprintf (config.map_file, " ");
1291       print_section (i->name);
1292       fprintf (config.map_file, " ");
1293       if (i->output_section)
1294         {
1295           print_address (i->output_section->vma + i->output_offset);
1296           fprintf (config.map_file, " ");
1297           print_size (i->_raw_size);
1298           fprintf (config.map_file, " ");
1299           print_size(i->_cooked_size);
1300           fprintf (config.map_file, " ");
1301           print_alignment (i->alignment_power);
1302           fprintf (config.map_file, " ");
1303           if (in->ifile)
1304             {
1305
1306               bfd *abfd = in->ifile->the_bfd;
1307
1308               if (in->ifile->just_syms_flag == true)
1309                 {
1310                   fprintf (config.map_file, "symbols only ");
1311                 }
1312
1313               fprintf (config.map_file, " %s ", abfd->xvec->name);
1314               if (abfd->my_archive != (bfd *) NULL)
1315                 {
1316                   fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename,
1317                            abfd->filename);
1318                 }
1319               else
1320                 {
1321                   fprintf (config.map_file, "%s", abfd->filename);
1322                 }
1323               fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd));
1324               print_nl ();
1325
1326               /* Print all the symbols */
1327               bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1328             }
1329           else
1330             {
1331               print_nl ();
1332             }
1333
1334
1335           print_dot = outside_section_address (i) + size;
1336         }
1337       else
1338         {
1339           fprintf (config.map_file, "No output section allocated\n");
1340         }
1341     }
1342 }
1343
1344 static void
1345 print_fill_statement (fill)
1346      lang_fill_statement_type * fill;
1347 {
1348   fprintf (config.map_file, "FILL mask ");
1349   print_fill (fill->fill);
1350 }
1351
1352 static void
1353 print_data_statement (data)
1354      lang_data_statement_type * data;
1355 {
1356 /*  bfd_vma value; */
1357   print_section ("");
1358   print_space ();
1359   print_section ("");
1360   print_space ();
1361 /*  ASSERT(print_dot == data->output_vma);*/
1362
1363   print_address (data->output_vma + data->output_section->vma);
1364   print_space ();
1365   print_address (data->value);
1366   print_space ();
1367   switch (data->type)
1368     {
1369     case BYTE:
1370       fprintf (config.map_file, "BYTE ");
1371       print_dot += BYTE_SIZE;
1372       break;
1373     case SHORT:
1374       fprintf (config.map_file, "SHORT ");
1375       print_dot += SHORT_SIZE;
1376       break;
1377     case LONG:
1378       fprintf (config.map_file, "LONG ");
1379       print_dot += LONG_SIZE;
1380       break;
1381     case QUAD:
1382       fprintf (config.map_file, "QUAD ");
1383       print_dot += QUAD_SIZE;
1384       break;
1385     }
1386
1387   exp_print_tree (data->exp);
1388
1389   fprintf (config.map_file, "\n");
1390 }
1391
1392 /* Print a reloc statement.  */
1393
1394 static void
1395 print_reloc_statement (reloc)
1396      lang_reloc_statement_type *reloc;
1397 {
1398   print_section ("");
1399   print_space ();
1400   print_section ("");
1401   print_space ();
1402
1403 /*  ASSERT(print_dot == data->output_vma);*/
1404
1405   print_address (reloc->output_vma + reloc->output_section->vma);
1406   print_space ();
1407   print_address (reloc->addend_value);
1408   print_space ();
1409
1410   fprintf (config.map_file, "RELOC %s ", reloc->howto->name);
1411
1412   print_dot += bfd_get_reloc_size (reloc->howto);
1413
1414   exp_print_tree (reloc->addend_exp);
1415
1416   fprintf (config.map_file, "\n");
1417 }  
1418
1419 static void
1420 print_padding_statement (s)
1421      lang_padding_statement_type * s;
1422 {
1423   print_section ("");
1424   print_space ();
1425   print_section ("*fill*");
1426   print_space ();
1427   print_address (s->output_offset + s->output_section->vma);
1428   print_space ();
1429   print_size (s->size);
1430   print_space ();
1431   print_fill (s->fill);
1432   print_nl ();
1433
1434   print_dot = s->output_offset + s->output_section->vma + s->size;
1435
1436 }
1437
1438 static void
1439 print_wild_statement (w, os)
1440      lang_wild_statement_type * w;
1441      lang_output_section_statement_type * os;
1442 {
1443   fprintf (config.map_file, " from ");
1444   if (w->filename != (char *) NULL)
1445     {
1446       fprintf (config.map_file, "%s", w->filename);
1447     }
1448   else
1449     {
1450       fprintf (config.map_file, "*");
1451     }
1452   if (w->section_name != (char *) NULL)
1453     {
1454       fprintf (config.map_file, "(%s)", w->section_name);
1455     }
1456   else
1457     {
1458       fprintf (config.map_file, "(*)");
1459     }
1460   print_nl ();
1461   print_statement (w->children.head, os);
1462
1463 }
1464
1465 /* Print a group statement.  */
1466
1467 static void
1468 print_group (s, os)
1469      lang_group_statement_type *s;
1470      lang_output_section_statement_type *os;
1471 {
1472   fprintf (config.map_file, "START GROUP\n");
1473   print_statement (s->children.head, os);
1474   fprintf (config.map_file, "END GROUP\n");
1475 }
1476
1477 static void
1478 print_statement (s, os)
1479      lang_statement_union_type * s;
1480      lang_output_section_statement_type * os;
1481 {
1482   while (s)
1483     {
1484       switch (s->header.type)
1485         {
1486           case lang_constructors_statement_enum:
1487           fprintf (config.map_file, "constructors:\n");
1488           print_statement (constructor_list.head, os);
1489           break;
1490         case lang_wild_statement_enum:
1491           print_wild_statement (&s->wild_statement, os);
1492           break;
1493         default:
1494           fprintf (config.map_file, "Fail with %d\n", s->header.type);
1495           FAIL ();
1496           break;
1497         case lang_address_statement_enum:
1498           fprintf (config.map_file, "address\n");
1499           break;
1500         case lang_object_symbols_statement_enum:
1501           fprintf (config.map_file, "object symbols\n");
1502           break;
1503         case lang_fill_statement_enum:
1504           print_fill_statement (&s->fill_statement);
1505           break;
1506         case lang_data_statement_enum:
1507           print_data_statement (&s->data_statement);
1508           break;
1509         case lang_reloc_statement_enum:
1510           print_reloc_statement (&s->reloc_statement);
1511           break;
1512         case lang_input_section_enum:
1513           print_input_section (&s->input_section);
1514           break;
1515         case lang_padding_statement_enum:
1516           print_padding_statement (&s->padding_statement);
1517           break;
1518         case lang_output_section_statement_enum:
1519           print_output_section_statement (&s->output_section_statement);
1520           break;
1521         case lang_assignment_statement_enum:
1522           print_assignment (&s->assignment_statement,
1523                             os);
1524           break;
1525         case lang_target_statement_enum:
1526           fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
1527           break;
1528         case lang_output_statement_enum:
1529           fprintf (config.map_file, "OUTPUT(%s %s)\n",
1530                    s->output_statement.name,
1531                    output_target ? output_target : "");
1532           break;
1533         case lang_input_statement_enum:
1534           print_input_statement (&s->input_statement);
1535           break;
1536         case lang_group_statement_enum:
1537           print_group (&s->group_statement, os);
1538           break;
1539         case lang_afile_asection_pair_statement_enum:
1540           FAIL ();
1541           break;
1542         }
1543       s = s->next;
1544     }
1545 }
1546
1547
1548 static void
1549 print_statements ()
1550 {
1551   print_statement (statement_list.head,
1552                    abs_output_section);
1553
1554 }
1555
1556 static bfd_vma
1557 insert_pad (this_ptr, fill, power, output_section_statement, dot)
1558      lang_statement_union_type ** this_ptr;
1559      fill_type fill;
1560      unsigned int power;
1561      asection * output_section_statement;
1562      bfd_vma dot;
1563 {
1564   /* Align this section first to the
1565      input sections requirement, then
1566      to the output section's requirement.
1567      If this alignment is > than any seen before,
1568      then record it too. Perform the alignment by
1569      inserting a magic 'padding' statement.
1570      */
1571
1572   unsigned int alignment_needed = align_power (dot, power) - dot;
1573
1574   if (alignment_needed != 0)
1575     {
1576       lang_statement_union_type *new =
1577       (lang_statement_union_type *)
1578       stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type)));
1579
1580       /* Link into existing chain */
1581       new->header.next = *this_ptr;
1582       *this_ptr = new;
1583       new->header.type = lang_padding_statement_enum;
1584       new->padding_statement.output_section = output_section_statement;
1585       new->padding_statement.output_offset =
1586         dot - output_section_statement->vma;
1587       new->padding_statement.fill = fill;
1588       new->padding_statement.size = alignment_needed;
1589     }
1590
1591
1592   /* Remember the most restrictive alignment */
1593   if (power > output_section_statement->alignment_power)
1594     {
1595       output_section_statement->alignment_power = power;
1596     }
1597   output_section_statement->_raw_size += alignment_needed;
1598   return alignment_needed + dot;
1599
1600 }
1601
1602 /* Work out how much this section will move the dot point */
1603 static bfd_vma
1604 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
1605      lang_statement_union_type ** this_ptr;
1606      lang_output_section_statement_type * output_section_statement;
1607      fill_type fill;
1608      bfd_vma dot;
1609      boolean relax;
1610 {
1611   lang_input_section_type *is = &((*this_ptr)->input_section);
1612   asection *i = is->section;
1613
1614   if (is->ifile->just_syms_flag == false)
1615     {
1616       if (output_section_statement->subsection_alignment != -1)
1617        i->alignment_power =
1618         output_section_statement->subsection_alignment;
1619
1620       dot = insert_pad (this_ptr, fill, i->alignment_power,
1621                         output_section_statement->bfd_section, dot);
1622
1623       /* Remember where in the output section this input section goes */
1624
1625       i->output_offset = dot - output_section_statement->bfd_section->vma;
1626
1627       /* Mark how big the output section must be to contain this now
1628          */
1629       if (i->_cooked_size != 0)
1630         dot += i->_cooked_size;
1631       else
1632         dot += i->_raw_size;
1633       output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
1634     }
1635   else
1636     {
1637       i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1638     }
1639
1640   return dot;
1641 }
1642
1643 /* This variable indicates whether bfd_relax_section should be called
1644    again.  */
1645
1646 static boolean relax_again;
1647
1648 /* Set the sizes for all the output sections.  */
1649
1650 bfd_vma
1651 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
1652      lang_statement_union_type * s;
1653      lang_output_section_statement_type * output_section_statement;
1654      lang_statement_union_type ** prev;
1655      fill_type fill;
1656      bfd_vma dot;
1657      boolean relax;
1658 {
1659   /* Size up the sections from their constituent parts */
1660   for (; s != (lang_statement_union_type *) NULL; s = s->next)
1661   {
1662     switch (s->header.type)
1663     {
1664
1665      case lang_output_section_statement_enum:
1666      {
1667        bfd_vma after;
1668        lang_output_section_statement_type *os = &s->output_section_statement;
1669
1670        if (os->bfd_section == NULL)
1671          {
1672            /* This section was never actually created.  */
1673            break;
1674          }
1675
1676        /* If this is a COFF shared library section, use the size and
1677           address from the input section.  FIXME: This is COFF
1678           specific; it would be cleaner if there were some other way
1679           to do this, but nothing simple comes to mind.  */
1680        if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
1681          {
1682            asection *input;
1683
1684            if (os->children.head == NULL
1685                || os->children.head->next != NULL
1686                || os->children.head->header.type != lang_input_section_enum)
1687              einfo ("%P%X: Internal error on COFF shared library section %s",
1688                     os->name);
1689
1690            input = os->children.head->input_section.section;
1691            bfd_set_section_vma (os->bfd_section->owner,
1692                                 os->bfd_section,
1693                                 bfd_section_vma (input->owner, input));
1694            os->bfd_section->_raw_size = input->_raw_size;
1695            break;
1696          }
1697
1698        if (bfd_is_abs_section (os->bfd_section))
1699        {
1700          /* No matter what happens, an abs section starts at zero */
1701          ASSERT (os->bfd_section->vma == 0);
1702        }
1703        else
1704        {
1705          if (os->addr_tree == (etree_type *) NULL)
1706          {
1707            /* No address specified for this section, get one
1708               from the region specification
1709               */
1710            if (os->region == (lang_memory_region_type *) NULL)
1711            {
1712              os->region = lang_memory_region_lookup ("*default*");
1713            }
1714            dot = os->region->current;
1715            if (os->section_alignment == -1)
1716              dot = align_power (dot, os->bfd_section->alignment_power);
1717          }
1718          else
1719          {
1720            etree_value_type r;
1721
1722            r = exp_fold_tree (os->addr_tree,
1723                               abs_output_section,
1724                               lang_allocating_phase_enum,
1725                               dot, &dot);
1726            if (r.valid == false)
1727            {
1728              einfo ("%F%S: non constant address expression for section %s\n",
1729                     os->name);
1730            }
1731            dot = r.value;
1732          }
1733          /* The section starts here */
1734          /* First, align to what the section needs */
1735
1736          if (os->section_alignment != -1)
1737            dot = align_power (dot, os->section_alignment);
1738
1739          bfd_set_section_vma (0, os->bfd_section, dot);
1740          
1741          os->bfd_section->output_offset = 0;
1742        }
1743
1744        (void) lang_size_sections (os->children.head, os, &os->children.head,
1745                                   os->fill, dot, relax);
1746        /* Ignore the size of the input sections, use the vma and size to */
1747        /* align against */
1748
1749        after = ALIGN_N (os->bfd_section->vma +
1750                         os->bfd_section->_raw_size,
1751                         /* The coercion here is important, see ld.h.  */
1752                         (bfd_vma) os->block_value);
1753
1754        if (bfd_is_abs_section (os->bfd_section))
1755          ASSERT (after == os->bfd_section->vma);
1756        else
1757          os->bfd_section->_raw_size = after - os->bfd_section->vma;
1758        dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1759        os->processed = true;
1760
1761        /* Replace into region ? */
1762        if (os->region != (lang_memory_region_type *) NULL)
1763        {
1764          os->region->current = dot;
1765          /* Make sure this isn't silly */
1766          if (( os->region->current
1767               > os->region->origin + os->region->length)
1768              || ( os->region->origin > os->region->current ))
1769            {
1770              einfo ("%X%P: region %s is full (%B section %s)\n",
1771                     os->region->name,
1772                     os->bfd_section->owner,
1773                     os->bfd_section->name);
1774              /* Reset the region pointer */
1775              os->region->current = 0;
1776
1777            }
1778
1779        }
1780
1781
1782      }
1783
1784       break;
1785      case lang_constructors_statement_enum:
1786       dot = lang_size_sections (constructor_list.head,
1787                                 output_section_statement,
1788                                 &s->wild_statement.children.head,
1789                                 fill,
1790                                 dot, relax);
1791       break;
1792
1793      case lang_data_statement_enum:
1794      {
1795        unsigned int size = 0;
1796
1797        s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1798        s->data_statement.output_section =
1799         output_section_statement->bfd_section;
1800
1801        switch (s->data_statement.type)
1802        {
1803         case QUAD:
1804          size = QUAD_SIZE;
1805          break;
1806         case LONG:
1807          size = LONG_SIZE;
1808          break;
1809         case SHORT:
1810          size = SHORT_SIZE;
1811          break;
1812         case BYTE:
1813          size = BYTE_SIZE;
1814          break;
1815
1816        }
1817        dot += size;
1818        output_section_statement->bfd_section->_raw_size += size;
1819      }
1820       break;
1821
1822      case lang_reloc_statement_enum:
1823      {
1824        int size;
1825
1826        s->reloc_statement.output_vma =
1827          dot - output_section_statement->bfd_section->vma;
1828        s->reloc_statement.output_section =
1829          output_section_statement->bfd_section;
1830        size = bfd_get_reloc_size (s->reloc_statement.howto);
1831        dot += size;
1832        output_section_statement->bfd_section->_raw_size += size;
1833      }
1834      break;
1835      
1836      case lang_wild_statement_enum:
1837
1838       dot = lang_size_sections (s->wild_statement.children.head,
1839                                 output_section_statement,
1840                                 &s->wild_statement.children.head,
1841
1842                                 fill, dot, relax);
1843
1844       break;
1845
1846      case lang_object_symbols_statement_enum:
1847       link_info.create_object_symbols_section =
1848         output_section_statement->bfd_section;
1849       break;
1850      case lang_output_statement_enum:
1851      case lang_target_statement_enum:
1852       break;
1853      case lang_input_section_enum:
1854       {
1855         asection *i;
1856
1857         i = (*prev)->input_section.section;
1858         if (! relax)
1859           i->_cooked_size = i->_raw_size;
1860         else
1861           {
1862             boolean again;
1863
1864             if (! bfd_relax_section (i->owner, i, &link_info, &again))
1865               einfo ("%P%F: can't relax section: %E\n");
1866             if (again)
1867               relax_again = true;
1868           }
1869         dot = size_input_section (prev,
1870                                   output_section_statement,
1871                                   output_section_statement->fill,
1872                                   dot, relax);
1873       }
1874       break;
1875      case lang_input_statement_enum:
1876       break;
1877      case lang_fill_statement_enum:
1878       s->fill_statement.output_section = output_section_statement->bfd_section;
1879
1880       fill = s->fill_statement.fill;
1881       break;
1882      case lang_assignment_statement_enum:
1883      {
1884        bfd_vma newdot = dot;
1885
1886        exp_fold_tree (s->assignment_statement.exp,
1887                       output_section_statement,
1888                       lang_allocating_phase_enum,
1889                       dot,
1890                       &newdot);
1891
1892        if (newdot != dot && !relax)
1893          {
1894            /* The assignment changed dot.  Insert a pad.  */
1895            if (output_section_statement == abs_output_section)
1896              {
1897                /* If we don't have an output section, then just adjust
1898                   the default memory address.  */
1899                lang_memory_region_lookup ("*default*")->current = newdot;
1900              }
1901            else
1902              {
1903                lang_statement_union_type *new =
1904                  ((lang_statement_union_type *)
1905                   stat_alloc (sizeof (lang_padding_statement_type)));
1906
1907                /* Link into existing chain */
1908                new->header.next = *prev;
1909                *prev = new;
1910                new->header.type = lang_padding_statement_enum;
1911                new->padding_statement.output_section =
1912                  output_section_statement->bfd_section;
1913                new->padding_statement.output_offset =
1914                  dot - output_section_statement->bfd_section->vma;
1915                new->padding_statement.fill = fill;
1916                new->padding_statement.size = newdot - dot;
1917                output_section_statement->bfd_section->_raw_size +=
1918                  new->padding_statement.size;
1919              }
1920
1921            dot = newdot;
1922          }
1923      }
1924      break;
1925
1926    case lang_padding_statement_enum:
1927      /* If we are relaxing, and this is not the first pass, some
1928         padding statements may have been inserted during previous
1929         passes.  We may have to move the padding statement to a new
1930         location if dot has a different value at this point in this
1931         pass than it did at this point in the previous pass.  */
1932      s->padding_statement.output_offset =
1933        dot - output_section_statement->bfd_section->vma;
1934      dot += s->padding_statement.size;
1935      break;
1936
1937      case lang_group_statement_enum:
1938        dot = lang_size_sections (s->group_statement.children.head,
1939                                  output_section_statement,
1940                                  &s->group_statement.children.head,
1941                                  fill, dot, relax);
1942        break;
1943
1944      default:
1945       FAIL ();
1946       break;
1947
1948       /* This can only get here when relaxing is turned on */
1949
1950      case lang_address_statement_enum:
1951       break;
1952     }
1953     prev = &s->header.next;
1954   }
1955   return dot;
1956 }
1957
1958 bfd_vma
1959 lang_do_assignments (s, output_section_statement, fill, dot)
1960      lang_statement_union_type * s;
1961      lang_output_section_statement_type * output_section_statement;
1962      fill_type fill;
1963      bfd_vma dot;
1964 {
1965   for (; s != (lang_statement_union_type *) NULL; s = s->next)
1966     {
1967       switch (s->header.type)
1968         {
1969         case lang_constructors_statement_enum:
1970           dot = lang_do_assignments (constructor_list.head,
1971                                      output_section_statement,
1972                                      fill,
1973                                      dot);
1974           break;
1975
1976         case lang_output_section_statement_enum:
1977           {
1978             lang_output_section_statement_type *os =
1979               &(s->output_section_statement);
1980
1981             if (os->bfd_section != NULL)
1982               {
1983                 dot = os->bfd_section->vma;
1984                 (void) lang_do_assignments (os->children.head, os,
1985                                             os->fill, dot);
1986                 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1987               }
1988             if (os->load_base) 
1989               {
1990                 os->bfd_section->lma 
1991                   = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
1992               }
1993           }
1994           break;
1995         case lang_wild_statement_enum:
1996
1997           dot = lang_do_assignments (s->wild_statement.children.head,
1998                                      output_section_statement,
1999                                      fill, dot);
2000
2001           break;
2002
2003         case lang_object_symbols_statement_enum:
2004         case lang_output_statement_enum:
2005         case lang_target_statement_enum:
2006 #if 0
2007         case lang_common_statement_enum:
2008 #endif
2009           break;
2010         case lang_data_statement_enum:
2011           {
2012             etree_value_type value;
2013
2014             value = exp_fold_tree (s->data_statement.exp,
2015                                    abs_output_section,
2016                                    lang_final_phase_enum, dot, &dot);
2017             s->data_statement.value = value.value;
2018             if (value.valid == false)
2019               einfo ("%F%P: invalid data statement\n");
2020           }
2021           switch (s->data_statement.type)
2022             {
2023             case QUAD:
2024               dot += QUAD_SIZE;
2025               break;
2026             case LONG:
2027               dot += LONG_SIZE;
2028               break;
2029             case SHORT:
2030               dot += SHORT_SIZE;
2031               break;
2032             case BYTE:
2033               dot += BYTE_SIZE;
2034               break;
2035             }
2036           break;
2037
2038         case lang_reloc_statement_enum:
2039           {
2040             etree_value_type value;
2041
2042             value = exp_fold_tree (s->reloc_statement.addend_exp,
2043                                    abs_output_section,
2044                                    lang_final_phase_enum, dot, &dot);
2045             s->reloc_statement.addend_value = value.value;
2046             if (value.valid == false)
2047               einfo ("%F%P: invalid reloc statement\n");
2048           }
2049           dot += bfd_get_reloc_size (s->reloc_statement.howto);
2050           break;
2051
2052         case lang_input_section_enum:
2053           {
2054             asection *in = s->input_section.section;
2055
2056             if (in->_cooked_size != 0)
2057               dot += in->_cooked_size;
2058             else
2059               dot += in->_raw_size;
2060           }
2061           break;
2062
2063         case lang_input_statement_enum:
2064           break;
2065         case lang_fill_statement_enum:
2066           fill = s->fill_statement.fill;
2067           break;
2068         case lang_assignment_statement_enum:
2069           {
2070             exp_fold_tree (s->assignment_statement.exp,
2071                            output_section_statement,
2072                            lang_final_phase_enum,
2073                            dot,
2074                            &dot);
2075           }
2076
2077           break;
2078         case lang_padding_statement_enum:
2079           dot += s->padding_statement.size;
2080           break;
2081
2082         case lang_group_statement_enum:
2083           dot = lang_do_assignments (s->group_statement.children.head,
2084                                      output_section_statement,
2085                                      fill, dot);
2086
2087           break;
2088
2089         default:
2090           FAIL ();
2091           break;
2092         case lang_address_statement_enum:
2093           break;
2094         }
2095
2096     }
2097   return dot;
2098 }
2099
2100 static void
2101 lang_finish ()
2102 {
2103   struct bfd_link_hash_entry *h;
2104   boolean warn = link_info.relocateable ? false : true;
2105
2106   if (entry_symbol == (char *) NULL)
2107     {
2108       /* No entry has been specified.  Look for start, but don't warn
2109          if we don't find it.  */
2110       entry_symbol = "start";
2111       warn = false;
2112     }
2113
2114   h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2115   if (h != (struct bfd_link_hash_entry *) NULL
2116       && h->type == bfd_link_hash_defined)
2117     {
2118       bfd_vma val;
2119
2120       val = (h->u.def.value
2121              + bfd_get_section_vma (output_bfd,
2122                                     h->u.def.section->output_section)
2123              + h->u.def.section->output_offset);
2124       if (! bfd_set_start_address (output_bfd, val))
2125         einfo ("%P%F:%s: can't set start address\n", entry_symbol);
2126     }
2127   else
2128     {
2129       asection *ts;
2130
2131       /* Can't find the entry symbol.  Use the first address in the
2132          text section.  */
2133       ts = bfd_get_section_by_name (output_bfd, ".text");
2134       if (ts != (asection *) NULL)
2135         {
2136           if (warn)
2137             einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2138                    entry_symbol, bfd_get_section_vma (output_bfd, ts));
2139           if (! bfd_set_start_address (output_bfd,
2140                                        bfd_get_section_vma (output_bfd, ts)))
2141             einfo ("%P%F: can't set start address\n");
2142         }
2143       else
2144         {
2145           if (warn)
2146             einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2147                    entry_symbol);
2148         }
2149     }
2150 }
2151
2152 /* Check that the architecture of all the input files is compatible
2153    with the output file.  */
2154
2155 static void
2156 lang_check ()
2157 {
2158   lang_statement_union_type *file;
2159   bfd *input_bfd;
2160   CONST bfd_arch_info_type *compatible;
2161
2162   for (file = file_chain.head;
2163        file != (lang_statement_union_type *) NULL;
2164        file = file->input_statement.next)
2165     {
2166       input_bfd = file->input_statement.the_bfd;
2167       compatible = bfd_arch_get_compatible (input_bfd,
2168                                             output_bfd);
2169       if (compatible == NULL)
2170         einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2171                bfd_printable_name (input_bfd), input_bfd,
2172                bfd_printable_name (output_bfd));
2173     }
2174 }
2175
2176 /* Look through all the global common symbols and attach them to the
2177    correct section.  The -sort-common command line switch may be used
2178    to roughly sort the entries by size.  */
2179
2180 static void
2181 lang_common ()
2182 {
2183   if (link_info.relocateable
2184       && ! command_line.force_common_definition)
2185     return;
2186
2187   if (! config.sort_common)
2188     bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2189   else
2190     {
2191       unsigned int power;
2192
2193       for (power = 1; power < 4; power++)
2194         bfd_link_hash_traverse (link_info.hash, lang_one_common,
2195                                 (PTR) &power);
2196     }
2197 }
2198
2199 /* Place one common symbol in the correct section.  */
2200
2201 static boolean
2202 lang_one_common (h, info)
2203      struct bfd_link_hash_entry *h;
2204      PTR info;
2205 {
2206   unsigned int power_of_two;
2207   bfd_vma size;
2208   asection *section;
2209
2210   if (h->type != bfd_link_hash_common)
2211     return true;
2212
2213   size = h->u.c.size;
2214   power_of_two = h->u.c.alignment_power;
2215
2216   if (config.sort_common
2217       && power_of_two < *(unsigned int *) info
2218       && *(unsigned int *) info < 4)
2219     return true;
2220
2221   section = h->u.c.section;
2222
2223   /* Increase the size of the section.  */
2224   section->_raw_size = ALIGN_N (section->_raw_size,
2225                                 (bfd_size_type) (1 << power_of_two));
2226
2227   /* Adjust the alignment if necessary.  */
2228   if (power_of_two > section->alignment_power)
2229     section->alignment_power = power_of_two;
2230
2231   /* Change the symbol from common to defined.  */
2232   h->type = bfd_link_hash_defined;
2233   h->u.def.section = section;
2234   h->u.def.value = section->_raw_size;
2235
2236   /* Increase the size of the section.  */
2237   section->_raw_size += size;
2238
2239   if (config.map_file != NULL)
2240     fprintf (config.map_file, "Allocating common %s: %lx at %lx %s\n",
2241              h->root.string, (unsigned long) size,
2242              (unsigned long) h->u.def.value, section->owner->filename);
2243
2244   return true;
2245 }
2246
2247 /*
2248 run through the input files and ensure that every input
2249 section has somewhere to go. If one is found without
2250 a destination then create an input request and place it
2251 into the statement tree.
2252 */
2253
2254 static void
2255 lang_place_orphans ()
2256 {
2257   lang_input_statement_type *file;
2258
2259   for (file = (lang_input_statement_type *) file_chain.head;
2260        file != (lang_input_statement_type *) NULL;
2261        file = (lang_input_statement_type *) file->next)
2262     {
2263       asection *s;
2264
2265       if (file->just_syms_flag)
2266         continue;
2267
2268       for (s = file->the_bfd->sections;
2269            s != (asection *) NULL;
2270            s = s->next)
2271         {
2272           if (s->output_section == (asection *) NULL)
2273             {
2274               /* This section of the file is not attatched, root
2275                  around for a sensible place for it to go */
2276
2277               if (file->common_section == s)
2278                 {
2279                   /* This is a lonely common section which must
2280                      have come from an archive. We attatch to the
2281                      section with the wildcard  */
2282                   if (! link_info.relocateable
2283                       && ! command_line.force_common_definition)
2284                     {
2285                       if (default_common_section ==
2286                           (lang_output_section_statement_type *) NULL)
2287                         {
2288                           info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2289
2290                           default_common_section =
2291                             lang_output_section_statement_lookup (".bss");
2292
2293                         }
2294                       wild_doit (&default_common_section->children, s,
2295                                  default_common_section, file);
2296                     }
2297                 }
2298               else if (ldemul_place_orphan (file, s))
2299                 ;
2300               else
2301                 {
2302                   lang_output_section_statement_type *os =
2303                   lang_output_section_statement_lookup (s->name);
2304
2305                   wild_doit (&os->children, s, os, file);
2306                 }
2307             }
2308         }
2309     }
2310 }
2311
2312
2313 void
2314 lang_set_flags (ptr, flags)
2315      int *ptr;
2316      CONST char *flags;
2317 {
2318   boolean state = false;
2319
2320   *ptr = 0;
2321   while (*flags)
2322     {
2323       if (*flags == '!')
2324         {
2325           state = false;
2326           flags++;
2327         }
2328       else
2329         state = true;
2330       switch (*flags)
2331         {
2332         case 'R':
2333           /*      ptr->flag_read = state; */
2334           break;
2335         case 'W':
2336           /*      ptr->flag_write = state; */
2337           break;
2338         case 'X':
2339           /*      ptr->flag_executable= state;*/
2340           break;
2341         case 'L':
2342         case 'I':
2343           /*      ptr->flag_loadable= state;*/
2344           break;
2345         default:
2346           einfo ("%P%F: invalid syntax in flags\n");
2347           break;
2348         }
2349       flags++;
2350     }
2351 }
2352
2353 /* Call a function on each input file.  This function will be called
2354    on an archive, but not on the elements.  */
2355
2356 void
2357 lang_for_each_input_file (func)
2358      void (*func) PARAMS ((lang_input_statement_type *));
2359 {
2360   lang_input_statement_type *f;
2361
2362   for (f = (lang_input_statement_type *) input_file_chain.head;
2363        f != NULL;
2364        f = (lang_input_statement_type *) f->next_real_file)
2365     func (f);
2366 }
2367
2368 /* Call a function on each file.  The function will be called on all
2369    the elements of an archive which are included in the link, but will
2370    not be called on the archive file itself.  */
2371
2372 void
2373 lang_for_each_file (func)
2374      void (*func) PARAMS ((lang_input_statement_type *));
2375 {
2376   lang_input_statement_type *f;
2377
2378   for (f = (lang_input_statement_type *) file_chain.head;
2379        f != (lang_input_statement_type *) NULL;
2380        f = (lang_input_statement_type *) f->next)
2381     {
2382       func (f);
2383     }
2384 }
2385
2386 #if 0
2387
2388 /* Not used.  */
2389
2390 void
2391 lang_for_each_input_section (func)
2392      void (*func) PARAMS ((bfd * ab, asection * as));
2393 {
2394   lang_input_statement_type *f;
2395
2396   for (f = (lang_input_statement_type *) file_chain.head;
2397        f != (lang_input_statement_type *) NULL;
2398        f = (lang_input_statement_type *) f->next)
2399     {
2400       asection *s;
2401
2402       for (s = f->the_bfd->sections;
2403            s != (asection *) NULL;
2404            s = s->next)
2405         {
2406           func (f->the_bfd, s);
2407         }
2408     }
2409 }
2410
2411 #endif
2412
2413 void
2414 ldlang_add_file (entry)
2415      lang_input_statement_type * entry;
2416 {
2417   bfd **pp;
2418
2419   lang_statement_append (&file_chain,
2420                          (lang_statement_union_type *) entry,
2421                          &entry->next);
2422
2423   /* The BFD linker needs to have a list of all input BFDs involved in
2424      a link.  */
2425   ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
2426   ASSERT (entry->the_bfd != output_bfd);
2427   for (pp = &link_info.input_bfds;
2428        *pp != (bfd *) NULL;
2429        pp = &(*pp)->link_next)
2430     ;
2431   *pp = entry->the_bfd;
2432   entry->the_bfd->usrdata = (PTR) entry;
2433   bfd_set_gp_size (entry->the_bfd, g_switch_value);
2434 }
2435
2436 void
2437 lang_add_output (name, from_script)
2438      CONST char *name;
2439      int from_script;
2440 {
2441   /* Make -o on command line override OUTPUT in script.  */
2442   if (had_output_filename == false || !from_script)
2443     {
2444       output_filename = name;
2445       had_output_filename = true;
2446     }
2447 }
2448
2449
2450 static lang_output_section_statement_type *current_section;
2451
2452 static int topower(x)
2453      int x;
2454 {
2455   unsigned  int i = 1;
2456   int l;
2457   if (x < 0) return -1;
2458   for (l = 0; l < 32; l++) 
2459   {
2460     if (i >= x) return l;
2461     i<<=1;
2462   }
2463   return 0;
2464 }
2465 void
2466 lang_enter_output_section_statement (output_section_statement_name,
2467                                      address_exp, flags, block_value,
2468                                      align, subalign, ebase)
2469      const char *output_section_statement_name;
2470      etree_type * address_exp;
2471      int flags;
2472      bfd_vma block_value;
2473      etree_type *align;
2474      etree_type *subalign;
2475      etree_type *ebase;
2476 {
2477   lang_output_section_statement_type *os;
2478
2479   current_section =
2480    os =
2481     lang_output_section_statement_lookup (output_section_statement_name);
2482
2483
2484
2485   /* Add this statement to tree */
2486   /*  add_statement(lang_output_section_statement_enum,
2487       output_section_statement);*/
2488   /* Make next things chain into subchain of this */
2489
2490   if (os->addr_tree ==
2491       (etree_type *) NULL)
2492   {
2493     os->addr_tree =
2494      address_exp;
2495   }
2496   os->flags = flags;
2497   if (flags & SEC_NEVER_LOAD)
2498    os->loadable = 0;
2499   else
2500    os->loadable = 1;
2501   os->block_value = block_value ? block_value : 1;
2502   stat_ptr = &os->children;
2503
2504   os->subsection_alignment = topower(
2505    exp_get_value_int(subalign, -1,
2506                      "subsection alignment",
2507                      0));
2508   os->section_alignment = topower(
2509    exp_get_value_int(align, -1,
2510                      "section alignment", 0));
2511
2512   os->load_base = ebase;
2513 }
2514
2515
2516 void
2517 lang_final ()
2518 {
2519   lang_output_statement_type *new =
2520     new_stat (lang_output_statement, stat_ptr);
2521
2522   new->name = output_filename;
2523 }
2524
2525 /* Reset the current counters in the regions */
2526 static void
2527 reset_memory_regions ()
2528 {
2529   lang_memory_region_type *p = lang_memory_region_list;
2530
2531   for (p = lang_memory_region_list;
2532        p != (lang_memory_region_type *) NULL;
2533        p = p->next)
2534     {
2535       p->old_length = (bfd_size_type) (p->current - p->origin);
2536       p->current = p->origin;
2537     }
2538 }
2539
2540 void
2541 lang_process ()
2542 {
2543   lang_reasonable_defaults ();
2544   current_target = default_target;
2545
2546   lang_for_each_statement (ldlang_open_output); /* Open the output file */
2547
2548   ldemul_create_output_section_statements ();
2549
2550   /* Add to the hash table all undefineds on the command line */
2551   lang_place_undefineds ();
2552
2553   /* Create a bfd for each input file */
2554   current_target = default_target;
2555   open_input_bfds (statement_list.head, false);
2556
2557   /* Build all sets based on the information gathered from the input
2558      files.  */
2559   ldctor_build_sets ();
2560
2561   /* Size up the common data */
2562   lang_common ();
2563
2564   /* Run through the contours of the script and attatch input sections
2565      to the correct output sections
2566      */
2567   map_input_to_output_sections (statement_list.head, (char *) NULL,
2568                                 (lang_output_section_statement_type *) NULL);
2569
2570
2571   /* Find any sections not attatched explicitly and handle them */
2572   lang_place_orphans ();
2573
2574   ldemul_before_allocation ();
2575
2576   /* Now run around and relax if we can */
2577   if (command_line.relax)
2578     {
2579       /* First time round is a trial run to get the 'worst case'
2580          addresses of the objects if there was no relaxing.  */
2581       lang_size_sections (statement_list.head,
2582                           abs_output_section,
2583                           &(statement_list.head), 0, (bfd_vma) 0, false);
2584
2585
2586       reset_memory_regions ();
2587
2588       /* Keep relaxing until bfd_relax_section gives up.  */
2589       do
2590         {
2591           relax_again = false;
2592
2593           /* Do all the assignments with our current guesses as to
2594              section sizes.  */
2595           lang_do_assignments (statement_list.head,
2596                                abs_output_section,
2597                                (fill_type) 0, (bfd_vma) 0);
2598
2599           /* Perform another relax pass - this time we know where the
2600              globals are, so can make better guess.  */
2601           lang_size_sections (statement_list.head,
2602                               abs_output_section,
2603                               &(statement_list.head), 0, (bfd_vma) 0, true);
2604         }
2605       while (relax_again);
2606     }
2607   else
2608     {
2609       /* Size up the sections.  */
2610       lang_size_sections (statement_list.head,
2611                           abs_output_section,
2612                           &(statement_list.head), 0, (bfd_vma) 0, false);
2613     }
2614
2615   /* See if anything special should be done now we know how big
2616      everything is.  */
2617   ldemul_after_allocation ();
2618
2619   /* Do all the assignments, now that we know the final restingplaces
2620      of all the symbols */
2621
2622   lang_do_assignments (statement_list.head,
2623                        abs_output_section,
2624                        (fill_type) 0, (bfd_vma) 0);
2625
2626   /* Make sure that we're not mixing architectures */
2627
2628   lang_check ();
2629
2630   /* Final stuffs */
2631
2632   ldemul_finish ();
2633   lang_finish ();
2634 }
2635
2636 /* EXPORTED TO YACC */
2637
2638 void
2639 lang_add_wild (section_name, filename)
2640      CONST char *CONST section_name;
2641      CONST char *CONST filename;
2642 {
2643   lang_wild_statement_type *new = new_stat (lang_wild_statement,
2644                                             stat_ptr);
2645
2646   if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
2647     {
2648       placed_commons = true;
2649     }
2650   if (filename != (char *) NULL)
2651     {
2652       lang_has_input_file = true;
2653     }
2654   new->section_name = section_name;
2655   new->filename = filename;
2656   lang_list_init (&new->children);
2657 }
2658
2659 void
2660 lang_section_start (name, address)
2661      CONST char *name;
2662      etree_type * address;
2663 {
2664   lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
2665
2666   ad->section_name = name;
2667   ad->address = address;
2668 }
2669
2670 /* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
2671    because of a -e argument on the command line, or zero if this is
2672    called by ENTRY in a linker script.  Command line arguments take
2673    precedence.  */
2674
2675 void
2676 lang_add_entry (name, cmdline)
2677      CONST char *name;
2678      int cmdline;
2679 {
2680   static int from_cmdline;
2681
2682   if (entry_symbol == NULL
2683       || cmdline
2684       || ! from_cmdline)
2685     {
2686       entry_symbol = name;
2687       from_cmdline = cmdline;
2688     }
2689 }
2690
2691 void
2692 lang_add_target (name)
2693      CONST char *name;
2694 {
2695   lang_target_statement_type *new = new_stat (lang_target_statement,
2696                                               stat_ptr);
2697
2698   new->target = name;
2699
2700 }
2701
2702 void
2703 lang_add_map (name)
2704      CONST char *name;
2705 {
2706   while (*name)
2707     {
2708       switch (*name)
2709         {
2710           case 'F':
2711           map_option_f = true;
2712           break;
2713         }
2714       name++;
2715     }
2716 }
2717
2718 void
2719 lang_add_fill (exp)
2720      int exp;
2721 {
2722   lang_fill_statement_type *new = new_stat (lang_fill_statement,
2723                                             stat_ptr);
2724
2725   new->fill = exp;
2726 }
2727
2728 void
2729 lang_add_data (type, exp)
2730      int type;
2731      union etree_union *exp;
2732 {
2733
2734   lang_data_statement_type *new = new_stat (lang_data_statement,
2735                                             stat_ptr);
2736
2737   new->exp = exp;
2738   new->type = type;
2739
2740 }
2741
2742 /* Create a new reloc statement.  RELOC is the BFD relocation type to
2743    generate.  HOWTO is the corresponding howto structure (we could
2744    look this up, but the caller has already done so).  SECTION is the
2745    section to generate a reloc against, or NAME is the name of the
2746    symbol to generate a reloc against.  Exactly one of SECTION and
2747    NAME must be NULL.  ADDEND is an expression for the addend.  */
2748
2749 void
2750 lang_add_reloc (reloc, howto, section, name, addend)
2751      bfd_reloc_code_real_type reloc;
2752      const reloc_howto_type *howto;
2753      asection *section;
2754      const char *name;
2755      union etree_union *addend;
2756 {
2757   lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
2758   
2759   p->reloc = reloc;
2760   p->howto = howto;
2761   p->section = section;
2762   p->name = name;
2763   p->addend_exp = addend;
2764
2765   p->addend_value = 0;
2766   p->output_section = NULL;
2767   p->output_vma = 0;
2768 }
2769
2770 void
2771 lang_add_assignment (exp)
2772      etree_type * exp;
2773 {
2774   lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
2775                                                   stat_ptr);
2776
2777   new->exp = exp;
2778 }
2779
2780 void
2781 lang_add_attribute (attribute)
2782      enum statement_enum attribute;
2783 {
2784   new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2785 }
2786
2787 void
2788 lang_startup (name)
2789      CONST char *name;
2790 {
2791   if (startup_file != (char *) NULL)
2792     {
2793       einfo ("%P%Fmultiple STARTUP files\n");
2794     }
2795   first_file->filename = name;
2796   first_file->local_sym_name = name;
2797   first_file->real = true;
2798
2799   startup_file = name;
2800 }
2801
2802 void
2803 lang_float (maybe)
2804      boolean maybe;
2805 {
2806   lang_float_flag = maybe;
2807 }
2808
2809 void
2810 lang_leave_output_section_statement (fill, memspec)
2811      bfd_vma fill;
2812      CONST char *memspec;
2813 {
2814   current_section->fill = fill;
2815   current_section->region = lang_memory_region_lookup (memspec);
2816   stat_ptr = &statement_list;
2817 }
2818
2819 /*
2820  Create an absolute symbol with the given name with the value of the
2821  address of first byte of the section named.
2822
2823  If the symbol already exists, then do nothing.
2824 */
2825 void
2826 lang_abs_symbol_at_beginning_of (secname, name)
2827      const char *secname;
2828      const char *name;
2829 {
2830   struct bfd_link_hash_entry *h;
2831
2832   h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
2833   if (h == (struct bfd_link_hash_entry *) NULL)
2834     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
2835
2836   if (h->type == bfd_link_hash_new
2837       || h->type == bfd_link_hash_undefined)
2838     {
2839       asection *sec;
2840
2841       h->type = bfd_link_hash_defined;
2842
2843       sec = bfd_get_section_by_name (output_bfd, secname);
2844       if (sec == (asection *) NULL)
2845         h->u.def.value = 0;
2846       else
2847         h->u.def.value = bfd_get_section_vma (output_bfd, sec);
2848
2849       h->u.def.section = bfd_abs_section_ptr;
2850     }
2851 }
2852
2853 /*
2854  Create an absolute symbol with the given name with the value of the
2855  address of the first byte after the end of the section named.
2856
2857  If the symbol already exists, then do nothing.
2858 */
2859 void
2860 lang_abs_symbol_at_end_of (secname, name)
2861      const char *secname;
2862      const char *name;
2863 {
2864   struct bfd_link_hash_entry *h;
2865
2866   h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
2867   if (h == (struct bfd_link_hash_entry *) NULL)
2868     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
2869
2870   if (h->type == bfd_link_hash_new
2871       || h->type == bfd_link_hash_undefined)
2872     {
2873       asection *sec;
2874
2875       h->type = bfd_link_hash_defined;
2876
2877       sec = bfd_get_section_by_name (output_bfd, secname);
2878       if (sec == (asection *) NULL)
2879         h->u.def.value = 0;
2880       else
2881         h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
2882                           + bfd_section_size (output_bfd, sec));
2883
2884       h->u.def.section = bfd_abs_section_ptr;
2885     }
2886 }
2887
2888 void
2889 lang_statement_append (list, element, field)
2890      lang_statement_list_type * list;
2891      lang_statement_union_type * element;
2892      lang_statement_union_type ** field;
2893 {
2894   *(list->tail) = element;
2895   list->tail = field;
2896 }
2897
2898 /* Set the output format type.  -oformat overrides scripts.  */
2899 void
2900 lang_add_output_format (format, from_script)
2901      CONST char *format;
2902      int from_script;
2903 {
2904   if (output_target == NULL || !from_script)
2905     output_target = format;
2906 }
2907
2908 /* Enter a group.  This creates a new lang_group_statement, and sets
2909    stat_ptr to build new statements within the group.  */
2910
2911 void
2912 lang_enter_group ()
2913 {
2914   lang_group_statement_type *g;
2915
2916   g = new_stat (lang_group_statement, stat_ptr);
2917   lang_list_init (&g->children);
2918   stat_ptr = &g->children;
2919 }
2920
2921 /* Leave a group.  This just resets stat_ptr to start writing to the
2922    regular list of statements again.  Note that this will not work if
2923    groups can occur inside anything else which can adjust stat_ptr,
2924    but currently they can't.  */
2925
2926 void
2927 lang_leave_group ()
2928 {
2929   stat_ptr = &statement_list;
2930 }